Changeset View
Changeset View
Standalone View
Standalone View
kcms/componentchooser/componentchooseremail.cpp
Show All 16 Lines | |||||
17 | 17 | | |||
18 | #include <kemailsettings.h> | 18 | #include <kemailsettings.h> | ||
19 | #include <kopenwithdialog.h> | 19 | #include <kopenwithdialog.h> | ||
20 | 20 | | |||
21 | #include <KLocalizedString> | 21 | #include <KLocalizedString> | ||
22 | #include <KConfigGroup> | 22 | #include <KConfigGroup> | ||
23 | #include <KSharedConfig> | 23 | #include <KSharedConfig> | ||
24 | #include <KService> | 24 | #include <KService> | ||
25 | #include <KServiceTypeTrader> | ||||
26 | #include <KBuildSycocaProgressDialog> | ||||
27 | #include <KMimeTypeTrader> | ||||
28 | | ||||
25 | #include <QDBusConnection> | 29 | #include <QDBusConnection> | ||
26 | #include <QDBusMessage> | 30 | #include <QDBusMessage> | ||
27 | #include <QUrl> | 31 | #include <QUrl> | ||
28 | #include <QFile> | 32 | #include <QFile> | ||
29 | #include <QCheckBox> | 33 | #include <QCheckBox> | ||
34 | #include <QDBusConnection> | ||||
35 | #include <QDBusMessage> | ||||
30 | 36 | | |||
31 | // for chmod: | 37 | // for chmod: | ||
32 | #include <sys/types.h> | 38 | #include <sys/types.h> | ||
33 | #include <sys/stat.h> | 39 | #include <sys/stat.h> | ||
34 | #include <QStandardPaths> | 40 | #include <QStandardPaths> | ||
35 | 41 | | |||
36 | 42 | | |||
37 | CfgEmailClient::CfgEmailClient(QWidget *parent) | 43 | CfgEmailClient::CfgEmailClient(QWidget *parent) | ||
38 | : QWidget(parent), Ui::EmailClientConfig_UI(), CfgPlugin() | 44 | : QWidget(parent), Ui::EmailClientConfig_UI(), CfgPlugin() | ||
39 | { | 45 | { | ||
40 | setupUi( this ); | 46 | setupUi( this ); | ||
41 | 47 | | |||
42 | pSettings = new KEMailSettings(); | 48 | pSettings = new KEMailSettings(); | ||
43 | 49 | | |||
44 | connect(kmailCB, &QRadioButton::toggled, this, &CfgEmailClient::configChanged); | 50 | connect(emailClientsCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgEmailClient::selectEmailClient); | ||
45 | connect(txtEMailClient, &KLineEdit::textChanged, this, &CfgEmailClient::configChanged); | | |||
46 | #ifdef Q_OS_UNIX | | |||
47 | connect(chkRunTerminal, &QCheckBox::clicked, this, &CfgEmailClient::configChanged); | | |||
48 | #else | | |||
49 | chkRunTerminal->hide(); | | |||
50 | #endif | | |||
51 | connect(btnSelectEmail, &QToolButton::clicked, this, &CfgEmailClient::selectEmailClient); | | |||
52 | } | 51 | } | ||
53 | 52 | | |||
54 | CfgEmailClient::~CfgEmailClient() { | 53 | CfgEmailClient::~CfgEmailClient() { | ||
55 | delete pSettings; | 54 | delete pSettings; | ||
56 | } | 55 | } | ||
57 | 56 | | |||
58 | void CfgEmailClient::defaults() | 57 | void CfgEmailClient::defaults() | ||
59 | { | 58 | { | ||
60 | kmailCB->setChecked(true); | 59 | // select kmail if installed | ||
61 | txtEMailClient->clear(); | 60 | if (m_kmailIndex != -1) { | ||
62 | chkRunTerminal->setChecked(false); | 61 | emailClientsCombo->setCurrentIndex(m_kmailIndex); | ||
62 | } | ||||
63 | } | 63 | } | ||
64 | 64 | | |||
65 | bool CfgEmailClient::isDefaults() const | 65 | bool CfgEmailClient::isDefaults() const | ||
66 | { | 66 | { | ||
67 | return kmailCB->isChecked(); | 67 | // if kmail is installed and is selected | ||
68 | if (m_kmailIndex != -1) { | ||||
69 | return emailClientsCombo->currentIndex() == m_kmailIndex; | ||||
70 | } | ||||
71 | | ||||
72 | return true; | ||||
68 | } | 73 | } | ||
69 | 74 | | |||
ervin: Not a huge fan of static consts appearing like this in the middle of the file. Could you please… | |||||
70 | void CfgEmailClient::load(KConfig *) | 75 | void CfgEmailClient::load(KConfig *) | ||
71 | { | 76 | { | ||
72 | QString emailClient = pSettings->getSetting(KEMailSettings::ClientProgram); | 77 | KService::Ptr emailClientService = KMimeTypeTrader::self()->preferredService(QStringLiteral("x-scheme-handler/mailto")); | ||
73 | bool useKMail = (emailClient.isEmpty()); | | |||
74 | 78 | | |||
75 | kmailCB->setChecked(useKMail); | 79 | const auto emailClients = KServiceTypeTrader::self()->query(QStringLiteral("Application"), | ||
76 | otherCB->setChecked(!useKMail); | 80 | QStringLiteral("'Email' in Categories and 'x-scheme-handler/mailto' in ServiceTypes")); | ||
77 | txtEMailClient->setText(emailClient); | | |||
78 | txtEMailClient->setFixedHeight(txtEMailClient->sizeHint().height()); | | |||
79 | chkRunTerminal->setChecked((pSettings->getSetting(KEMailSettings::ClientTerminal) == QLatin1String("true"))); | | |||
80 | | ||||
81 | emit changed(false); | | |||
82 | 81 | | |||
82 | emailClientsCombo->clear(); | ||||
83 | m_kmailIndex = -1; | ||||
84 | m_currentIndex = -1; | ||||
85 | | ||||
86 | if (emailClients.isEmpty()) { | ||||
87 | emailClientsCombo->setEnabled(false); | ||||
88 | } else { | ||||
89 | for (const auto &service : emailClients) { | ||||
90 | emailClientsCombo->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | ||||
91 | | ||||
92 | if (emailClientService && emailClientService->storageId() == service->storageId()) { | ||||
93 | emailClientsCombo->setCurrentIndex(emailClientsCombo->count() - 1); | ||||
94 | m_currentIndex = emailClientsCombo->count() - 1; | ||||
95 | } | ||||
96 | if (service->storageId() == QStringLiteral("org.kde.kmail2.desktop") || | ||||
97 | service->storageId() == QStringLiteral("org.kde.kmail.desktop")) { | ||||
98 | m_kmailIndex = emailClientsCombo->count() - 1; | ||||
99 | } | ||||
83 | } | 100 | } | ||
84 | | ||||
85 | void CfgEmailClient::configChanged() | | |||
86 | { | | |||
87 | emit changed(true); | | |||
88 | } | 101 | } | ||
89 | 102 | | |||
90 | void CfgEmailClient::selectEmailClient() | 103 | emit changed(false); | ||
91 | { | | |||
92 | QList<QUrl> urlList; | | |||
93 | KOpenWithDialog dlg(urlList, i18n("Select preferred email client:"), QString(), this); | | |||
94 | // hide "Do not &close when command exits" here, we don't need it for a mail client | | |||
95 | dlg.hideNoCloseOnExit(); | | |||
96 | if (dlg.exec() != QDialog::Accepted) return; | | |||
97 | QString client = dlg.text(); | | |||
98 | m_emailClientService = dlg.service(); | | |||
99 | 104 | | |||
100 | // get the preferred Terminal Application | 105 | } | ||
101 | KConfigGroup confGroup( KSharedConfig::openConfig(), QStringLiteral("General") ); | | |||
102 | QString preferredTerminal = confGroup.readPathEntry("TerminalApplication", QStringLiteral("konsole")); | | |||
103 | preferredTerminal += QLatin1String(" -e "); | | |||
104 | 106 | | |||
105 | int len = preferredTerminal.length(); | 107 | void CfgEmailClient::selectEmailClient(int index) | ||
106 | bool b = client.left(len) == preferredTerminal; | | |||
107 | if (b) client = client.mid(len); | | |||
108 | if (!client.isEmpty()) | | |||
109 | { | 108 | { | ||
110 | chkRunTerminal->setChecked(b); | 109 | emit changed(m_currentIndex != index); | ||
111 | txtEMailClient->setText(client); | | |||
112 | } | | |||
113 | } | 110 | } | ||
114 | 111 | | |||
115 | | ||||
116 | void CfgEmailClient::save(KConfig *) | 112 | void CfgEmailClient::save(KConfig *) | ||
117 | { | 113 | { | ||
118 | if (kmailCB->isChecked()) | 114 | const QString &storageId = emailClientsCombo->currentData().toString(); | ||
119 | { | 115 | KService::Ptr emailClientService = KService::serviceByStorageId(storageId); | ||
116 | | ||||
117 | const bool kmailSelected = m_kmailIndex != -1 && emailClientsCombo->currentIndex() == m_kmailIndex; | ||||
118 | if (kmailSelected) { | ||||
120 | pSettings->setSetting(KEMailSettings::ClientProgram, QString()); | 119 | pSettings->setSetting(KEMailSettings::ClientProgram, QString()); | ||
121 | pSettings->setSetting(KEMailSettings::ClientTerminal, QStringLiteral("false")); | 120 | pSettings->setSetting(KEMailSettings::ClientTerminal, QStringLiteral("false")); | ||
122 | } | 121 | } else { | ||
123 | else | 122 | pSettings->setSetting(KEMailSettings::ClientProgram, emailClientService->storageId()); | ||
124 | { | 123 | pSettings->setSetting(KEMailSettings::ClientTerminal, QStringLiteral("false")); | ||
125 | pSettings->setSetting(KEMailSettings::ClientProgram, txtEMailClient->text()); | | |||
126 | pSettings->setSetting(KEMailSettings::ClientTerminal, (chkRunTerminal->isChecked()) ? "true" : "false"); | | |||
127 | } | 124 | } | ||
128 | 125 | | |||
meven: remove | |||||
129 | // Save the default email client in mimeapps.list into the group [Default Applications] | 126 | // Save the default email client in mimeapps.list into the group [Default Applications] | ||
130 | KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | 127 | KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | ||
To simplify reading this I'd go for: if (!service) { continue; } and then I'd put the result of none_of in a properly named intermediate variable before checking it in its own if. Will make the intents clearer. ervin: To simplify reading this I'd go for:
```
if (!service) {
continue;
}
```
and then I'd put… | |||||
I really meant if (!service) + continue to avoid yet another indentation level but OK, fair enough. ervin: I really meant if (!service) + continue to avoid yet another indentation level but OK, fair… | |||||
131 | if (profile->isConfigWritable(true)) { | 128 | if (profile->isConfigWritable(true)) { | ||
132 | KConfigGroup defaultApp(profile, "Default Applications"); | 129 | KConfigGroup defaultApp(profile, "Default Applications"); | ||
133 | if (kmailCB->isChecked()) { | 130 | if (emailClientService) { | ||
134 | QString kmailDesktop = QStringLiteral("org.kde.kmail.desktop"); | 131 | defaultApp.writeXdgListEntry("x-scheme-handler/mailto", QStringList(emailClientService->storageId())); | ||
135 | if (KService::serviceByDesktopName(QStringLiteral("org.kde.kmail2"))) { | | |||
136 | kmailDesktop = QStringLiteral("org.kde.kmail2.desktop"); | | |||
137 | } | | |||
138 | defaultApp.writeXdgListEntry("x-scheme-handler/mailto", QStringList(kmailDesktop)); | | |||
139 | } else if (m_emailClientService) { | | |||
140 | defaultApp.writeXdgListEntry("x-scheme-handler/mailto", QStringList(m_emailClientService->storageId())); | | |||
141 | } | 132 | } | ||
142 | profile->sync(); | 133 | profile->sync(); | ||
143 | } | 134 | | ||
135 | m_currentIndex = emailClientsCombo->currentIndex(); | ||||
136 | | ||||
137 | // refresh cache | ||||
138 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | ||||
144 | 139 | | |||
145 | // insure proper permissions -- contains sensitive data | 140 | // insure proper permissions -- contains sensitive data | ||
146 | QString cfgName(QStandardPaths::locate(QStandardPaths::ConfigLocation, QStringLiteral("emails"))); | 141 | QString cfgName(QStandardPaths::locate(QStandardPaths::ConfigLocation, QStringLiteral("emails"))); | ||
147 | if (!cfgName.isEmpty()) | 142 | if (!cfgName.isEmpty()) | ||
148 | ::chmod(QFile::encodeName(cfgName), 0600); | 143 | ::chmod(QFile::encodeName(cfgName), 0600); | ||
149 | QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/Component"), QStringLiteral("org.kde.Kcontrol"), QStringLiteral("KDE_emailSettingsChanged") ); | 144 | | ||
150 | QDBusConnection::sessionBus().send(message); | | |||
151 | emit changed(false); | 145 | emit changed(false); | ||
152 | } | 146 | } | ||
147 | } | ||||
153 | 148 | |
Not a huge fan of static consts appearing like this in the middle of the file. Could you please move them up before the ctor? Bonus point for putting them in the anonymous namespace.