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 | : CfgPlugin(parent) | ||
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() | | |||
56 | { | | |||
57 | // select kmail if installed | | |||
58 | if (m_kmailIndex != -1) { | | |||
59 | emailClientsCombo->setCurrentIndex(m_kmailIndex); | | |||
60 | } | | |||
61 | } | | |||
62 | | ||||
63 | bool CfgEmailClient::isDefaults() const | | |||
64 | { | | |||
65 | // if kmail is installed and is selected | | |||
66 | if (m_kmailIndex != -1) { | | |||
67 | return emailClientsCombo->currentIndex() == m_kmailIndex; | | |||
68 | } | | |||
69 | | ||||
70 | return true; | | |||
71 | } | | |||
72 | | ||||
73 | void CfgEmailClient::load(KConfig *) | 52 | void CfgEmailClient::load(KConfig *) | ||
74 | { | 53 | { | ||
75 | const KService::Ptr emailClientService = KMimeTypeTrader::self()->preferredService(s_mimetype); | 54 | const KService::Ptr emailClientService = KMimeTypeTrader::self()->preferredService(s_mimetype); | ||
76 | 55 | | |||
77 | const auto emailClients = KServiceTypeTrader::self()->query(QStringLiteral("Application"), | 56 | const auto emailClients = KServiceTypeTrader::self()->query(QStringLiteral("Application"), | ||
78 | QStringLiteral("'Email' in Categories and 'x-scheme-handler/mailto' in ServiceTypes")); | 57 | QStringLiteral("'Email' in Categories and 'x-scheme-handler/mailto' in ServiceTypes")); | ||
79 | 58 | | |||
80 | emailClientsCombo->clear(); | 59 | clear(); | ||
81 | m_kmailIndex = -1; | | |||
82 | m_currentIndex = -1; | 60 | m_currentIndex = -1; | ||
61 | m_defaultIndex = -1; | ||||
83 | 62 | | |||
84 | for (const auto &service : emailClients) { | 63 | for (const auto &service : emailClients) { | ||
85 | 64 | | |||
86 | emailClientsCombo->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | 65 | addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | ||
87 | 66 | | |||
88 | if (emailClientService && emailClientService->storageId() == service->storageId()) { | 67 | if (emailClientService && emailClientService->storageId() == service->storageId()) { | ||
89 | emailClientsCombo->setCurrentIndex(emailClientsCombo->count() - 1); | 68 | setCurrentIndex(count() - 1); | ||
90 | m_currentIndex = emailClientsCombo->count() - 1; | 69 | m_currentIndex = count() - 1; | ||
91 | } | 70 | } | ||
92 | if (service->storageId() == QStringLiteral("org.kde.kmail2.desktop") || | 71 | if (service->storageId() == QStringLiteral("org.kde.kmail2.desktop") || | ||
93 | service->storageId() == QStringLiteral("org.kde.kmail.desktop")) { | 72 | service->storageId() == QStringLiteral("org.kde.kmail.desktop")) { | ||
94 | m_kmailIndex = emailClientsCombo->count() - 1; | 73 | m_defaultIndex = count() - 1; | ||
95 | } | 74 | } | ||
96 | } | 75 | } | ||
97 | 76 | | |||
98 | // add the Added association to x-scheme-handler/mailto from the mimeapps.list file | 77 | // 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); | 78 | const KSharedConfig::Ptr profile = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | ||
100 | const KConfigGroup addedApps(profile, s_AddedAssociations); | 79 | const KConfigGroup addedApps(profile, s_AddedAssociations); | ||
101 | const auto addedList = addedApps.readXdgListEntry(s_mimetype); | 80 | const auto addedList = addedApps.readXdgListEntry(s_mimetype); | ||
102 | 81 | | |||
103 | for (const auto &addedApp : addedList) { | 82 | for (const auto &addedApp : addedList) { | ||
104 | // without .desktop extension | 83 | // without .desktop extension | ||
105 | auto service = KService::serviceByStorageId(addedApp.mid(0, addedApp.length() -8)); | 84 | auto service = KService::serviceByStorageId(addedApp.mid(0, addedApp.length() -8)); | ||
106 | if (!service) { | 85 | if (!service) { | ||
107 | service = KService::serviceByStorageId(addedApp); | 86 | service = KService::serviceByStorageId(addedApp); | ||
108 | } | 87 | } | ||
109 | if (!service) { | 88 | if (!service) { | ||
110 | continue; | 89 | continue; | ||
111 | } | 90 | } | ||
112 | // avoid duplicates entry when email clients are present in mimeapps.list's Added Associations too | 91 | // 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(); }); | 92 | const bool isServiceAlreadyInserted = std::none_of(emailClients.constBegin(), emailClients.constEnd(), [service] (const KService::Ptr &serv) { return service->storageId() == serv->storageId(); }); | ||
114 | if (isServiceAlreadyInserted) { | 93 | if (isServiceAlreadyInserted) { | ||
115 | const auto icon = QIcon::fromTheme(!service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript")); | 94 | 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()); | 95 | addItem(icon, service->name() + " (" + KShell::tildeCollapse(service->entryPath()) + ")", service->storageId()); | ||
117 | 96 | | |||
118 | if (emailClientService && emailClientService->storageId() == service->storageId()) { | 97 | if (emailClientService && emailClientService->storageId() == service->storageId()) { | ||
119 | emailClientsCombo->setCurrentIndex(emailClientsCombo->count() - 1); | 98 | setCurrentIndex(count() - 1); | ||
120 | m_currentIndex = emailClientsCombo->count() - 1; | 99 | m_currentIndex = count() - 1; | ||
121 | } | 100 | } | ||
122 | } | 101 | } | ||
123 | } | 102 | } | ||
124 | 103 | | |||
125 | // add a other option to add a new email client with KOpenWithDialog | 104 | // add a other option to add a new email client with KOpenWithDialog | ||
126 | emailClientsCombo->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | 105 | addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | ||
127 | 106 | | |||
128 | emit changed(false); | 107 | emit changed(false); | ||
129 | } | 108 | } | ||
130 | 109 | | |||
131 | void CfgEmailClient::selectEmailClient(int index) | 110 | void CfgEmailClient::selectEmailClient(int index) | ||
132 | { | 111 | { | ||
133 | if (index == emailClientsCombo->count() -1) { | 112 | if (index == count() -1) { | ||
134 | // Other option | 113 | // Other option | ||
135 | 114 | | |||
136 | KOpenWithDialog dlg(s_mimetype, QString(), this); | 115 | KOpenWithDialog dlg(s_mimetype, QString(), this); | ||
137 | dlg.setSaveNewApplications(true); | 116 | dlg.setSaveNewApplications(true); | ||
138 | 117 | | |||
139 | if (dlg.exec() != QDialog::Accepted) { | 118 | if (dlg.exec() != QDialog::Accepted) { | ||
140 | // restore previous setting | 119 | // restore previous setting | ||
141 | emailClientsCombo->setCurrentIndex(m_currentIndex); | 120 | setCurrentIndex(m_currentIndex); | ||
142 | emit changed(false); | 121 | emit changed(false); | ||
143 | } else { | 122 | } else { | ||
144 | const auto service = dlg.service(); | 123 | const auto service = dlg.service(); | ||
145 | 124 | | |||
146 | const auto icon = QIcon::fromTheme(!service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript")); | 125 | 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()); | 126 | 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 | 127 | | |||
149 | // select newly inserted email client | 128 | // select newly inserted email client | ||
150 | emailClientsCombo->setCurrentIndex(emailClientsCombo->count() - 2); | 129 | setCurrentIndex(count() - 2); | ||
151 | 130 | | |||
152 | emit changed(true); | 131 | emit changed(true); | ||
153 | return; | 132 | return; | ||
154 | } | 133 | } | ||
155 | } else { | 134 | } else { | ||
156 | emit changed(m_currentIndex != index); | 135 | emit changed(m_currentIndex != index); | ||
157 | } | 136 | } | ||
158 | } | 137 | } | ||
159 | 138 | | |||
160 | void CfgEmailClient::save(KConfig *) | 139 | void CfgEmailClient::save(KConfig *) | ||
161 | { | 140 | { | ||
162 | const QString &storageId = emailClientsCombo->currentData().toString(); | 141 | const QString &storageId = currentData().toString(); | ||
163 | const KService::Ptr emailClientService = KService::serviceByStorageId(storageId); | 142 | const KService::Ptr emailClientService = KService::serviceByStorageId(storageId); | ||
164 | 143 | | |||
165 | const bool kmailSelected = m_kmailIndex != -1 && emailClientsCombo->currentIndex() == m_kmailIndex; | 144 | const bool kmailSelected = m_defaultIndex != -1 && currentIndex() == m_defaultIndex; | ||
166 | if (kmailSelected) { | 145 | if (kmailSelected) { | ||
167 | pSettings->setSetting(KEMailSettings::ClientProgram, QString()); | 146 | pSettings->setSetting(KEMailSettings::ClientProgram, QString()); | ||
168 | pSettings->setSetting(KEMailSettings::ClientTerminal, QStringLiteral("false")); | 147 | pSettings->setSetting(KEMailSettings::ClientTerminal, QStringLiteral("false")); | ||
169 | } else { | 148 | } else { | ||
170 | pSettings->setSetting(KEMailSettings::ClientProgram, emailClientService->storageId()); | 149 | pSettings->setSetting(KEMailSettings::ClientProgram, emailClientService->storageId()); | ||
171 | pSettings->setSetting(KEMailSettings::ClientTerminal, emailClientService->terminal() ? QStringLiteral("true") : QStringLiteral("false")); | 150 | pSettings->setSetting(KEMailSettings::ClientTerminal, emailClientService->terminal() ? QStringLiteral("true") : QStringLiteral("false")); | ||
172 | } | 151 | } | ||
173 | 152 | | |||
Show All 10 Lines | 155 | if (profile->isConfigWritable(true) && emailClientService) { | |||
184 | KConfigGroup addedApps(profile, "Added Associations"); | 163 | KConfigGroup addedApps(profile, "Added Associations"); | ||
185 | QStringList apps = addedApps.readXdgListEntry(s_mimetype); | 164 | QStringList apps = addedApps.readXdgListEntry(s_mimetype); | ||
186 | apps.removeAll(emailClientService->storageId()); | 165 | apps.removeAll(emailClientService->storageId()); | ||
187 | apps.prepend(emailClientService->storageId()); // make it the preferred app, i.e first in list | 166 | apps.prepend(emailClientService->storageId()); // make it the preferred app, i.e first in list | ||
188 | addedApps.writeXdgListEntry(s_mimetype, apps); | 167 | addedApps.writeXdgListEntry(s_mimetype, apps); | ||
189 | 168 | | |||
190 | profile->sync(); | 169 | profile->sync(); | ||
191 | 170 | | |||
192 | m_currentIndex = emailClientsCombo->currentIndex(); | 171 | m_currentIndex = currentIndex(); | ||
193 | | ||||
194 | // refresh cache | | |||
195 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | | |||
196 | 172 | | |||
197 | emit changed(false); | 173 | emit changed(false); | ||
198 | } | 174 | } | ||
199 | } | 175 | } | ||
200 | 176 | |
So here you do show an entryPath but not in the other components?