Commit 8f1043c6 authored by Méven Car's avatar Méven Car Committed by Méven Car

KCM/ComponentChooser Treat cases when there is no app for a usage

Summary: BUG: 417276

Test Plan:
Have no email client installed.
In kcm componentchooser
 - select another browser
 - Save

Before:
 - Crash
After:
 - No crash

Reviewers: bport, ervin, crossi, davidedmundson, #plasma

Reviewed By: bport, ervin, crossi

Subscribers: anthonyfieroni, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D27395
parent 07b4f3fb
......@@ -35,9 +35,10 @@ public:
virtual ~CfgPlugin(){}
virtual void load(KConfig *cfg)=0;
virtual void save(KConfig *cfg)=0;
bool hasChanged() const
{
return m_currentIndex != -1 && m_currentIndex != currentIndex();
return count() > 1 && m_currentIndex != currentIndex();
}
void defaults()
......@@ -47,6 +48,14 @@ public:
}
}
int validLastCurrentIndex() const
{
// m_currentIndex == -1 means there are no previously saved value
// or maybe there were no choices in the combobox
// return 0 in those cases
return m_currentIndex == -1 ? 0 : m_currentIndex;
}
bool isDefaults() const
{
return m_defaultIndex == -1 || m_defaultIndex == currentIndex();
......
......@@ -41,7 +41,7 @@ void CfgBrowser::selectBrowser(int index)
KOpenWithDialog dlg(QStringLiteral("x-scheme-handler/http"), QString(), this);
dlg.setSaveNewApplications(true);
if (dlg.exec() != QDialog::Accepted) {
setCurrentIndex(m_currentIndex);
setCurrentIndex(validLastCurrentIndex());
return;
}
......@@ -104,6 +104,11 @@ void CfgBrowser::load(KConfig *)
void CfgBrowser::save(KConfig *)
{
if (currentIndex() == count() - 1) {
// no browser installed, nor selected
return;
}
const QString browserStorageId = currentData().toString();
BrowserSettings settings;
......
......@@ -117,7 +117,7 @@ void CfgEmailClient::selectEmailClient(int index)
if (dlg.exec() != QDialog::Accepted) {
// restore previous setting
setCurrentIndex(m_currentIndex);
setCurrentIndex(validLastCurrentIndex());
emit changed(false);
} else {
const auto service = dlg.service();
......@@ -138,8 +138,17 @@ void CfgEmailClient::selectEmailClient(int index)
void CfgEmailClient::save(KConfig *)
{
if (currentIndex() == count() - 1) {
// no email client installed, nor selected
return;
}
const QString &storageId = currentData().toString();
const KService::Ptr emailClientService = KService::serviceByStorageId(storageId);
if (!emailClientService) {
// double checking, the selected email client might have been removed
return;
}
const bool kmailSelected = m_defaultIndex != -1 && currentIndex() == m_defaultIndex;
if (kmailSelected) {
......@@ -152,7 +161,7 @@ void CfgEmailClient::save(KConfig *)
// Save the default email client in mimeapps.list
KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation);
if (profile->isConfigWritable(true) && emailClientService) {
if (profile->isConfigWritable(true)) {
KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation);
......
......@@ -44,7 +44,7 @@ void CfgFileManager::selectFileManager(int index)
KOpenWithDialog dlg({}, i18n("Select preferred file manager:"), QString(), this);
dlg.setSaveNewApplications(true);
if (dlg.exec() != QDialog::Accepted) {
setCurrentIndex(m_currentIndex);
setCurrentIndex(validLastCurrentIndex());
return;
}
......@@ -113,26 +113,29 @@ static const char s_AddedAssociations[] = "Added Associations";
void CfgFileManager::save(KConfig *)
{
if (currentIndex() == count() - 1) {
// no filemanager installed, nor selected
return;
}
const QString storageId = currentData().toString();
if (!storageId.isEmpty()) {
m_currentIndex = currentIndex();
m_currentIndex = currentIndex();
// This is taken from filetypes/mimetypedata.cpp
KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation);
if (!profile->isConfigWritable(true)) // warn user if mimeapps.list is root-owned (#155126/#94504)
return;
KConfigGroup addedApps(profile, s_AddedAssociations);
QStringList userApps = addedApps.readXdgListEntry(mime);
userApps.removeAll(storageId); // remove if present, to make it first in the list
userApps.prepend(storageId);
addedApps.writeXdgListEntry(mime, userApps);
// This is taken from filetypes/mimetypedata.cpp
KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation);
if (!profile->isConfigWritable(true)) // warn user if mimeapps.list is root-owned (#155126/#94504)
return;
KConfigGroup addedApps(profile, s_AddedAssociations);
QStringList userApps = addedApps.readXdgListEntry(mime);
userApps.removeAll(storageId); // remove if present, to make it first in the list
userApps.prepend(storageId);
addedApps.writeXdgListEntry(mime, userApps);
// Save the default file manager as per mime-apps spec 1.0.1
KConfigGroup defaultApp(profile, s_DefaultApplications);
defaultApp.writeXdgListEntry(mime, QStringList(storageId));
// Save the default file manager as per mime-apps spec 1.0.1
KConfigGroup defaultApp(profile, s_DefaultApplications);
defaultApp.writeXdgListEntry(mime, QStringList(storageId));
profile->sync();
profile->sync();
emit changed(false);
}
emit changed(false);
}
......@@ -84,21 +84,25 @@ void CfgTerminalEmulator::load(KConfig *)
// add a other option to add a new terminal emulator with KOpenWithDialog
addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral());
emit changed(false);
emit changed(false);
}
void CfgTerminalEmulator::save(KConfig *)
{
if (currentIndex() == count() - 1) {
// no terminal installed, nor selected
return;
}
const QString terminal = currentData().toString();
m_currentIndex = currentIndex();
TerminalSettings settings;
TerminalSettings settings;
settings.setTerminalApplication(terminal);
settings.save();
settings.save();
m_currentIndex = currentIndex();
QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.klauncher5"),
QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.klauncher5"),
QStringLiteral("/KLauncher"),
QStringLiteral("org.kde.KLauncher"),
QStringLiteral("reparseConfiguration"));
......@@ -114,7 +118,7 @@ void CfgTerminalEmulator::selectTerminalApp()
dlg.hideRunInTerminal();
dlg.setSaveNewApplications(true);
if (dlg.exec() != QDialog::Accepted) {
setCurrentIndex(m_currentIndex);
setCurrentIndex(validLastCurrentIndex());
return;
}
const auto service = dlg.service();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment