diff --git a/kcms/componentchooser/componentchooser.h b/kcms/componentchooser/componentchooser.h --- a/kcms/componentchooser/componentchooser.h +++ b/kcms/componentchooser/componentchooser.h @@ -35,9 +35,10 @@ 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 @@ } } + 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(); diff --git a/kcms/componentchooser/componentchooserbrowser.cpp b/kcms/componentchooser/componentchooserbrowser.cpp --- a/kcms/componentchooser/componentchooserbrowser.cpp +++ b/kcms/componentchooser/componentchooserbrowser.cpp @@ -41,7 +41,7 @@ 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::save(KConfig *) { + if (currentIndex() == count() - 1) { + // no browser installed, nor selected + return; + } + const QString browserStorageId = currentData().toString(); BrowserSettings settings; diff --git a/kcms/componentchooser/componentchooseremail.cpp b/kcms/componentchooser/componentchooseremail.cpp --- a/kcms/componentchooser/componentchooseremail.cpp +++ b/kcms/componentchooser/componentchooseremail.cpp @@ -117,7 +117,7 @@ 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::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 @@ // 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); diff --git a/kcms/componentchooser/componentchooserfilemanager.cpp b/kcms/componentchooser/componentchooserfilemanager.cpp --- a/kcms/componentchooser/componentchooserfilemanager.cpp +++ b/kcms/componentchooser/componentchooserfilemanager.cpp @@ -44,7 +44,7 @@ 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 @@ 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); } diff --git a/kcms/componentchooser/componentchooserterminal.cpp b/kcms/componentchooser/componentchooserterminal.cpp --- a/kcms/componentchooser/componentchooserterminal.cpp +++ b/kcms/componentchooser/componentchooserterminal.cpp @@ -84,21 +84,25 @@ // 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 @@ dlg.hideRunInTerminal(); dlg.setSaveNewApplications(true); if (dlg.exec() != QDialog::Accepted) { - setCurrentIndex(m_currentIndex); + setCurrentIndex(validLastCurrentIndex()); return; } const auto service = dlg.service();