Changeset View
Standalone View
kcms/componentchooser/componentchooseremail.cpp
Show All 17 Lines | |||||
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> | 25 | #include <KServiceTypeTrader> | ||
26 | #include <KBuildSycocaProgressDialog> | | |||
27 | #include <KMimeTypeTrader> | 26 | #include <KMimeTypeTrader> | ||
28 | #include <KShell> | 27 | #include <KShell> | ||
29 | 28 | | |||
30 | #include <QDBusConnection> | 29 | #include <QDBusConnection> | ||
31 | #include <QDBusMessage> | 30 | #include <QDBusMessage> | ||
32 | #include <QUrl> | 31 | #include <QUrl> | ||
33 | #include <QFile> | 32 | #include <QFile> | ||
34 | #include <QCheckBox> | 33 | #include <QCheckBox> | ||
35 | 34 | | |||
36 | namespace { | 35 | namespace { | ||
37 | static const char s_AddedAssociations[] = "Added Associations"; | 36 | static const char s_AddedAssociations[] = "Added Associations"; | ||
38 | static const auto s_mimetype = QStringLiteral("x-scheme-handler/mailto"); | 37 | static const auto s_mimetype = QStringLiteral("x-scheme-handler/mailto"); | ||
39 | } | 38 | } | ||
40 | 39 | | |||
41 | CfgEmailClient::CfgEmailClient(QWidget *parent) | 40 | CfgEmailClient::CfgEmailClient(QWidget *parent) | ||
42 | : QWidget(parent), Ui::EmailClientConfig_UI(), CfgPlugin() | 41 | : QComboBox(parent), CfgPlugin() | ||
43 | { | 42 | { | ||
44 | setupUi( this ); | | |||
45 | | ||||
46 | pSettings = new KEMailSettings(); | 43 | pSettings = new KEMailSettings(); | ||
47 | 44 | | |||
48 | connect(emailClientsCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgEmailClient::selectEmailClient); | 45 | connect(this, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgEmailClient::selectEmailClient); | ||
49 | } | 46 | } | ||
50 | 47 | | |||
51 | CfgEmailClient::~CfgEmailClient() { | 48 | CfgEmailClient::~CfgEmailClient() { | ||
52 | delete pSettings; | 49 | delete pSettings; | ||
53 | } | 50 | } | ||
54 | 51 | | |||
55 | void CfgEmailClient::defaults() | 52 | void CfgEmailClient::defaults() | ||
56 | { | 53 | { | ||
57 | // select kmail if installed | 54 | // select kmail if installed | ||
58 | if (m_kmailIndex != -1) { | 55 | if (m_kmailIndex != -1) { | ||
59 | emailClientsCombo->setCurrentIndex(m_kmailIndex); | 56 | setCurrentIndex(m_kmailIndex); | ||
60 | } | 57 | } | ||
61 | } | 58 | } | ||
62 | 59 | | |||
63 | bool CfgEmailClient::isDefaults() const | 60 | bool CfgEmailClient::isDefaults() const | ||
64 | { | 61 | { | ||
65 | // if kmail is installed and is selected | 62 | // if kmail is installed and is selected | ||
66 | if (m_kmailIndex != -1) { | 63 | if (m_kmailIndex != -1) { | ||
67 | return emailClientsCombo->currentIndex() == m_kmailIndex; | 64 | return currentIndex() == m_kmailIndex; | ||
68 | } | 65 | } | ||
69 | 66 | | |||
70 | return true; | 67 | return true; | ||
71 | } | 68 | } | ||
72 | 69 | | |||
70 | bool CfgEmailClient::hasChanged() const | ||||
71 | { | ||||
72 | return m_currentIndex != -1 && m_currentIndex != currentIndex(); | ||||
73 | } | ||||
74 | | ||||
73 | void CfgEmailClient::load(KConfig *) | 75 | void CfgEmailClient::load(KConfig *) | ||
74 | { | 76 | { | ||
75 | const KService::Ptr emailClientService = KMimeTypeTrader::self()->preferredService(s_mimetype); | 77 | const KService::Ptr emailClientService = KMimeTypeTrader::self()->preferredService(s_mimetype); | ||
76 | 78 | | |||
77 | const auto emailClients = KServiceTypeTrader::self()->query(QStringLiteral("Application"), | 79 | const auto emailClients = KServiceTypeTrader::self()->query(QStringLiteral("Application"), | ||
78 | QStringLiteral("'Email' in Categories and 'x-scheme-handler/mailto' in ServiceTypes")); | 80 | QStringLiteral("'Email' in Categories and 'x-scheme-handler/mailto' in ServiceTypes")); | ||
79 | 81 | | |||
80 | emailClientsCombo->clear(); | 82 | clear(); | ||
81 | m_kmailIndex = -1; | 83 | m_kmailIndex = -1; | ||
82 | m_currentIndex = -1; | 84 | m_currentIndex = -1; | ||
83 | 85 | | |||
84 | for (const auto &service : emailClients) { | 86 | for (const auto &service : emailClients) { | ||
85 | 87 | | |||
86 | emailClientsCombo->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | 88 | addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | ||
87 | 89 | | |||
88 | if (emailClientService && emailClientService->storageId() == service->storageId()) { | 90 | if (emailClientService && emailClientService->storageId() == service->storageId()) { | ||
89 | emailClientsCombo->setCurrentIndex(emailClientsCombo->count() - 1); | 91 | setCurrentIndex(count() - 1); | ||
90 | m_currentIndex = emailClientsCombo->count() - 1; | 92 | m_currentIndex = count() - 1; | ||
91 | } | 93 | } | ||
92 | if (service->storageId() == QStringLiteral("org.kde.kmail2.desktop") || | 94 | if (service->storageId() == QStringLiteral("org.kde.kmail2.desktop") || | ||
93 | service->storageId() == QStringLiteral("org.kde.kmail.desktop")) { | 95 | service->storageId() == QStringLiteral("org.kde.kmail.desktop")) { | ||
94 | m_kmailIndex = emailClientsCombo->count() - 1; | 96 | m_kmailIndex = count() - 1; | ||
95 | } | 97 | } | ||
96 | } | 98 | } | ||
97 | 99 | | |||
98 | // add the Added association to x-scheme-handler/mailto from the mimeapps.list file | 100 | // add the Added association to x-scheme-handler/mailto from the mimeapps.list file | ||
99 | const KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | 101 | const KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | ||
100 | const KConfigGroup addedApps(profile, s_AddedAssociations); | 102 | const KConfigGroup addedApps(profile, s_AddedAssociations); | ||
101 | const auto addedList = addedApps.readXdgListEntry(s_mimetype); | 103 | const auto addedList = addedApps.readXdgListEntry(s_mimetype); | ||
102 | 104 | | |||
103 | for (const auto &addedApp : addedList) { | 105 | for (const auto &addedApp : addedList) { | ||
104 | // without .desktop extension | 106 | // without .desktop extension | ||
105 | auto service = KService::serviceByStorageId(addedApp.mid(0, addedApp.length() -8)); | 107 | auto service = KService::serviceByStorageId(addedApp.mid(0, addedApp.length() -8)); | ||
106 | if (!service) { | 108 | if (!service) { | ||
107 | service = KService::serviceByStorageId(addedApp); | 109 | service = KService::serviceByStorageId(addedApp); | ||
108 | } | 110 | } | ||
109 | if (!service) { | 111 | if (!service) { | ||
110 | continue; | 112 | continue; | ||
111 | } | 113 | } | ||
112 | // avoid duplicates entry when email clients are present in mimeapps.list's Added Associations too | 114 | // avoid duplicates entry when email clients are present in mimeapps.list's Added Associations too | ||
113 | const bool isServiceAlreadyInserted = std::none_of(emailClients.constBegin(), emailClients.constEnd(), [service] (const KService::Ptr &serv) { return service->storageId() == serv->storageId(); }); | 115 | const bool isServiceAlreadyInserted = std::none_of(emailClients.constBegin(), emailClients.constEnd(), [service] (const KService::Ptr &serv) { return service->storageId() == serv->storageId(); }); | ||
114 | if (isServiceAlreadyInserted) { | 116 | if (isServiceAlreadyInserted) { | ||
115 | const auto icon = QIcon::fromTheme(!service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript")); | 117 | const auto icon = QIcon::fromTheme(!service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript")); | ||
116 | emailClientsCombo->addItem(icon, service->name() + " (" + KShell::tildeCollapse(service->entryPath()) + ")", service->storageId()); | 118 | addItem(icon, service->name() + " (" + KShell::tildeCollapse(service->entryPath()) + ")", service->storageId()); | ||
117 | 119 | | |||
118 | if (emailClientService && emailClientService->storageId() == service->storageId()) { | 120 | if (emailClientService && emailClientService->storageId() == service->storageId()) { | ||
119 | emailClientsCombo->setCurrentIndex(emailClientsCombo->count() - 1); | 121 | setCurrentIndex(count() - 1); | ||
120 | m_currentIndex = emailClientsCombo->count() - 1; | 122 | m_currentIndex = count() - 1; | ||
121 | } | 123 | } | ||
122 | } | 124 | } | ||
123 | } | 125 | } | ||
124 | 126 | | |||
125 | // add a other option to add a new email client with KOpenWithDialog | 127 | // add a other option to add a new email client with KOpenWithDialog | ||
126 | emailClientsCombo->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | 128 | addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | ||
127 | 129 | | |||
128 | emit changed(false); | 130 | emit changed(false); | ||
129 | } | 131 | } | ||
130 | 132 | | |||
131 | void CfgEmailClient::selectEmailClient(int index) | 133 | void CfgEmailClient::selectEmailClient(int index) | ||
132 | { | 134 | { | ||
133 | if (index == emailClientsCombo->count() -1) { | 135 | if (index == count() -1) { | ||
134 | // Other option | 136 | // Other option | ||
135 | 137 | | |||
136 | KOpenWithDialog dlg(s_mimetype, QString(), this); | 138 | KOpenWithDialog dlg(s_mimetype, QString(), this); | ||
137 | dlg.setSaveNewApplications(true); | 139 | dlg.setSaveNewApplications(true); | ||
138 | 140 | | |||
139 | if (dlg.exec() != QDialog::Accepted) { | 141 | if (dlg.exec() != QDialog::Accepted) { | ||
140 | // restore previous setting | 142 | // restore previous setting | ||
141 | emailClientsCombo->setCurrentIndex(m_currentIndex); | 143 | setCurrentIndex(m_currentIndex); | ||
142 | emit changed(false); | 144 | emit changed(false); | ||
143 | } else { | 145 | } else { | ||
144 | const auto service = dlg.service(); | 146 | const auto service = dlg.service(); | ||
145 | 147 | | |||
146 | const auto icon = QIcon::fromTheme(!service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript")); | 148 | const auto icon = QIcon::fromTheme(!service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript")); | ||
147 | emailClientsCombo->insertItem(emailClientsCombo->count() - 1, icon, service->name() + " (" + KShell::tildeCollapse(service->entryPath()) + ")", service->storageId()); | 149 | insertItem(count() - 1, icon, service->name() + " (" + KShell::tildeCollapse(service->entryPath()) + ")", service->storageId()); | ||
broulik: So here you do show an `entryPath` but not in the other components? | |||||
For email I have a slightly more advanced logic to read and display the "Added Associations" section of mimeapps.list and add it to the list of choices. I did this because at least for thunderbird, when it set itself as default browser it creates a new entry to "Added Associations" and use this new entry as choice in "Default Applications". I may want to reconsider and simply add whatever service is in Default Application and display it, ignoring entries in "Added Associations", as I do elsewhere. meven: For email I have a slightly more advanced logic to read and display the "Added Associations"… | |||||
148 | 150 | | |||
149 | // select newly inserted email client | 151 | // select newly inserted email client | ||
150 | emailClientsCombo->setCurrentIndex(emailClientsCombo->count() - 2); | 152 | setCurrentIndex(count() - 2); | ||
151 | 153 | | |||
152 | emit changed(true); | 154 | emit changed(true); | ||
153 | return; | 155 | return; | ||
154 | } | 156 | } | ||
155 | } else { | 157 | } else { | ||
156 | emit changed(m_currentIndex != index); | 158 | emit changed(m_currentIndex != index); | ||
157 | } | 159 | } | ||
158 | } | 160 | } | ||
159 | 161 | | |||
160 | void CfgEmailClient::save(KConfig *) | 162 | void CfgEmailClient::save(KConfig *) | ||
161 | { | 163 | { | ||
162 | const QString &storageId = emailClientsCombo->currentData().toString(); | 164 | const QString &storageId = currentData().toString(); | ||
163 | const KService::Ptr emailClientService = KService::serviceByStorageId(storageId); | 165 | const KService::Ptr emailClientService = KService::serviceByStorageId(storageId); | ||
164 | 166 | | |||
165 | const bool kmailSelected = m_kmailIndex != -1 && emailClientsCombo->currentIndex() == m_kmailIndex; | 167 | const bool kmailSelected = m_kmailIndex != -1 && currentIndex() == m_kmailIndex; | ||
166 | if (kmailSelected) { | 168 | if (kmailSelected) { | ||
167 | pSettings->setSetting(KEMailSettings::ClientProgram, QString()); | 169 | pSettings->setSetting(KEMailSettings::ClientProgram, QString()); | ||
168 | pSettings->setSetting(KEMailSettings::ClientTerminal, QStringLiteral("false")); | 170 | pSettings->setSetting(KEMailSettings::ClientTerminal, QStringLiteral("false")); | ||
169 | } else { | 171 | } else { | ||
170 | pSettings->setSetting(KEMailSettings::ClientProgram, emailClientService->storageId()); | 172 | pSettings->setSetting(KEMailSettings::ClientProgram, emailClientService->storageId()); | ||
171 | pSettings->setSetting(KEMailSettings::ClientTerminal, emailClientService->terminal() ? QStringLiteral("true") : QStringLiteral("false")); | 173 | pSettings->setSetting(KEMailSettings::ClientTerminal, emailClientService->terminal() ? QStringLiteral("true") : QStringLiteral("false")); | ||
172 | } | 174 | } | ||
173 | 175 | | |||
Show All 10 Lines | 178 | if (profile->isConfigWritable(true) && emailClientService) { | |||
184 | KConfigGroup addedApps(profile, "Added Associations"); | 186 | KConfigGroup addedApps(profile, "Added Associations"); | ||
185 | QStringList apps = addedApps.readXdgListEntry(s_mimetype); | 187 | QStringList apps = addedApps.readXdgListEntry(s_mimetype); | ||
186 | apps.removeAll(emailClientService->storageId()); | 188 | apps.removeAll(emailClientService->storageId()); | ||
187 | apps.prepend(emailClientService->storageId()); // make it the preferred app, i.e first in list | 189 | apps.prepend(emailClientService->storageId()); // make it the preferred app, i.e first in list | ||
188 | addedApps.writeXdgListEntry(s_mimetype, apps); | 190 | addedApps.writeXdgListEntry(s_mimetype, apps); | ||
189 | 191 | | |||
190 | profile->sync(); | 192 | profile->sync(); | ||
191 | 193 | | |||
192 | m_currentIndex = emailClientsCombo->currentIndex(); | 194 | m_currentIndex = currentIndex(); | ||
193 | | ||||
194 | // refresh cache | | |||
195 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | | |||
196 | 195 | | |||
197 | emit changed(false); | 196 | emit changed(false); | ||
198 | } | 197 | } | ||
199 | } | 198 | } | ||
200 | 199 | |
So here you do show an entryPath but not in the other components?