Changeset View
Changeset View
Standalone View
Standalone View
kcms/componentchooser/componentchooserbrowser.cpp
Show All 11 Lines | |||||
12 | * published by the Free Software Foundationi * | 12 | * published by the Free Software Foundationi * | ||
13 | * * | 13 | * * | ||
14 | ***************************************************************************/ | 14 | ***************************************************************************/ | ||
15 | 15 | | |||
16 | #include "componentchooserbrowser.h" | 16 | #include "componentchooserbrowser.h" | ||
17 | #include <kopenwithdialog.h> | 17 | #include <kopenwithdialog.h> | ||
18 | #include "browser_settings.h" | 18 | #include "browser_settings.h" | ||
19 | 19 | | |||
20 | #include <KBuildSycocaProgressDialog> | | |||
21 | #include <KLocalizedString> | 20 | #include <KLocalizedString> | ||
22 | #include <KServiceTypeTrader> | 21 | #include <KServiceTypeTrader> | ||
23 | #include <KMimeTypeTrader> | 22 | #include <KMimeTypeTrader> | ||
24 | 23 | | |||
25 | #include <QUrl> | 24 | #include <QUrl> | ||
26 | #include <QDBusConnection> | 25 | #include <QDBusConnection> | ||
27 | #include <QDBusMessage> | 26 | #include <QDBusMessage> | ||
28 | 27 | | |||
29 | CfgBrowser::CfgBrowser(QWidget *parent) | 28 | CfgBrowser::CfgBrowser(QWidget *parent) | ||
30 | : QWidget(parent), Ui::BrowserConfig_UI(),CfgPlugin() | 29 | : QComboBox(parent) | ||
31 | { | 30 | { | ||
32 | setupUi(this); | 31 | connect(this, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgBrowser::selectBrowser); | ||
33 | connect(browserCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgBrowser::selectBrowser); | | |||
34 | | ||||
35 | } | 32 | } | ||
36 | 33 | | |||
37 | CfgBrowser::~CfgBrowser() { | 34 | CfgBrowser::~CfgBrowser() { | ||
38 | } | 35 | } | ||
39 | 36 | | |||
40 | void CfgBrowser::selectBrowser(int index) | 37 | void CfgBrowser::selectBrowser(int index) | ||
41 | { | 38 | { | ||
42 | if (index == browserCombo->count() -1) { | 39 | if (index == count() -1) { | ||
43 | QList<QUrl> urlList; | 40 | QList<QUrl> urlList; | ||
44 | KOpenWithDialog dlg(QStringLiteral("x-scheme-handler/http"), QString(), this); | 41 | KOpenWithDialog dlg(QStringLiteral("x-scheme-handler/http"), QString(), this); | ||
45 | dlg.setSaveNewApplications(true); | 42 | dlg.setSaveNewApplications(true); | ||
46 | if (dlg.exec() != QDialog::Accepted) { | 43 | if (dlg.exec() != QDialog::Accepted) { | ||
47 | browserCombo->setCurrentIndex(m_currentIndex); | 44 | setCurrentIndex(m_currentIndex); | ||
48 | return; | 45 | return; | ||
49 | } | 46 | } | ||
50 | 47 | | |||
51 | const auto service = dlg.service(); | 48 | const auto service = dlg.service(); | ||
52 | 49 | | |||
53 | // check if the selected service is already in the list | 50 | // check if the selected service is already in the list | ||
54 | const auto matching = browserCombo->model()->match(browserCombo->model()->index(0,0), Qt::UserRole, service->storageId()); | 51 | const auto matching = model()->match(model()->index(0,0), Qt::UserRole, service->storageId()); | ||
55 | if (!matching.isEmpty()) { | 52 | if (!matching.isEmpty()) { | ||
56 | const int index = matching.at(0).row(); | 53 | const int index = matching.at(0).row(); | ||
57 | browserCombo->setCurrentIndex(index); | 54 | setCurrentIndex(index); | ||
58 | emit changed(index != m_currentIndex); | 55 | emit changed(index != m_currentIndex); | ||
59 | } else { | 56 | } else { | ||
60 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | 57 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | ||
61 | browserCombo->insertItem(browserCombo->count() -1, QIcon::fromTheme(icon), service->name(), service->storageId()); | 58 | insertItem(count() -1, QIcon::fromTheme(icon), service->name(), service->storageId()); | ||
62 | browserCombo->setCurrentIndex(browserCombo->count() - 2); | 59 | setCurrentIndex(count() - 2); | ||
63 | 60 | | |||
64 | emit changed(true); | 61 | emit changed(true); | ||
65 | } | 62 | } | ||
66 | 63 | | |||
67 | } else { | 64 | } else { | ||
68 | emit changed(index != m_currentIndex); | 65 | emit changed(index != m_currentIndex); | ||
69 | } | 66 | } | ||
70 | } | 67 | } | ||
71 | 68 | | |||
72 | void CfgBrowser::defaults() | 69 | void CfgBrowser::defaults() | ||
73 | { | 70 | { | ||
74 | if (m_falkonIndex != -1) { | 71 | if (m_falkonIndex != -1) { | ||
75 | browserCombo->setCurrentIndex(m_falkonIndex); | 72 | setCurrentIndex(m_falkonIndex); | ||
76 | } | 73 | } | ||
77 | } | 74 | } | ||
78 | 75 | | |||
79 | bool CfgBrowser::isDefaults() const | 76 | bool CfgBrowser::isDefaults() const | ||
80 | { | 77 | { | ||
81 | return m_falkonIndex == -1 || m_falkonIndex == browserCombo->currentIndex(); | 78 | return m_falkonIndex == -1 || m_falkonIndex == currentIndex(); | ||
79 | } | ||||
80 | | ||||
81 | bool CfgBrowser::hasChanged() const | ||||
82 | { | ||||
83 | return m_currentIndex != -1 && m_currentIndex != currentIndex(); | ||||
82 | } | 84 | } | ||
83 | 85 | | |||
84 | void CfgBrowser::load(KConfig *) | 86 | void CfgBrowser::load(KConfig *) | ||
85 | { | 87 | { | ||
86 | const auto browser = KMimeTypeTrader::self()->preferredService("x-scheme-handler/http"); | 88 | const auto browser = KMimeTypeTrader::self()->preferredService("x-scheme-handler/http"); | ||
87 | 89 | | |||
88 | browserCombo->clear(); | 90 | clear(); | ||
89 | m_currentIndex = -1; | 91 | m_currentIndex = -1; | ||
90 | m_falkonIndex = -1; | 92 | m_falkonIndex = -1; | ||
91 | 93 | | |||
92 | const auto constraint = QStringLiteral("'WebBrowser' in Categories and" | 94 | const auto constraint = QStringLiteral("'WebBrowser' in Categories and" | ||
93 | " ('x-scheme-handler/http' in ServiceTypes or 'x-scheme-handler/https' in ServiceTypes)"); | 95 | " ('x-scheme-handler/http' in ServiceTypes or 'x-scheme-handler/https' in ServiceTypes)"); | ||
94 | const auto browsers = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | 96 | const auto browsers = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | ||
95 | for (const auto &service : browsers) { | 97 | for (const auto &service : browsers) { | ||
96 | browserCombo->addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | 98 | addItem(QIcon::fromTheme(service->icon()), service->name(), service->storageId()); | ||
97 | 99 | | |||
98 | if (browser->storageId() == service->storageId()) { | 100 | if (browser->storageId() == service->storageId()) { | ||
99 | browserCombo->setCurrentIndex(browserCombo->count() - 1); | 101 | setCurrentIndex(count() - 1); | ||
100 | m_currentIndex = browserCombo->count() - 1; | 102 | m_currentIndex = count() - 1; | ||
101 | } | 103 | } | ||
102 | if (service->storageId() == QStringLiteral("org.kde.falkon.desktop")) { | 104 | if (service->storageId() == QStringLiteral("org.kde.falkon.desktop")) { | ||
broulik: As a future step I would like those default components not hardcoded in the code | |||||
ervin: Yes, it screams for GUI / config separation (in another commit) | |||||
103 | m_falkonIndex = browserCombo->count() - 1; | 105 | m_falkonIndex = count() - 1; | ||
104 | } | 106 | } | ||
105 | } | 107 | } | ||
106 | 108 | | |||
107 | if (browser && m_currentIndex == -1) { | 109 | if (browser && m_currentIndex == -1) { | ||
108 | // we have a browser specified by the user | 110 | // we have a browser specified by the user | ||
109 | browserCombo->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), browser->name(), browser->storageId()); | 111 | addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), browser->name(), browser->storageId()); | ||
110 | browserCombo->setCurrentIndex(browserCombo->count() - 1); | 112 | setCurrentIndex(count() - 1); | ||
111 | m_currentIndex = browserCombo->count() - 1; | 113 | m_currentIndex = count() - 1; | ||
Something funky is going on with this one: I configured a custom kdialog --sorry "%f" command line. It only shows up as "kdialog" so I can't tell what the actual command was and not edit it later. More importantly, though, I then changed my browser to be Kate. While it initially showed "Kate" with proper icon, when I re-open the KCM, it shows "kdialog" again as custom command, while still opening Kate when I open http URLs. broulik: Something funky is going on with this one:
I configured a custom `kdialog --sorry "%f"`… | |||||
112 | } | 114 | } | ||
113 | 115 | | |||
114 | // add a other option to add a new browser | 116 | // add a other option to add a new browser | ||
115 | browserCombo->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | 117 | addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | ||
Don't use QStringLiteral(), use QString() for a null string. broulik: Don't use `QStringLiteral()`, use `QString()` for a null string.
But in this case you can just… | |||||
116 | 118 | | |||
117 | emit changed(false); | 119 | emit changed(false); | ||
118 | } | 120 | } | ||
119 | 121 | | |||
120 | void CfgBrowser::save(KConfig *) | 122 | void CfgBrowser::save(KConfig *) | ||
121 | { | 123 | { | ||
122 | const QString browserStorageId = browserCombo->currentData().toString(); | 124 | const QString browserStorageId = currentData().toString(); | ||
123 | 125 | | |||
124 | BrowserSettings settings; | 126 | BrowserSettings settings; | ||
125 | settings.setBrowserApplication(browserStorageId); | 127 | settings.setBrowserApplication(browserStorageId); | ||
126 | settings.save(); | 128 | settings.save(); | ||
127 | 129 | | |||
128 | // Save the default browser as scheme handler for http(s) in mimeapps.list | 130 | // Save the default browser as scheme handler for http(s) in mimeapps.list | ||
129 | KSharedConfig::Ptr mimeAppList = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | 131 | KSharedConfig::Ptr mimeAppList = KSharedConfig::openConfig(QStringLiteral("mimeapps.list"), KConfig::NoGlobals, QStandardPaths::GenericConfigLocation); | ||
130 | if (mimeAppList->isConfigWritable(true /*warn user if not writable*/)) { | 132 | if (mimeAppList->isConfigWritable(true /*warn user if not writable*/)) { | ||
131 | KConfigGroup defaultApp(mimeAppList, "Default Applications"); | 133 | KConfigGroup defaultApp(mimeAppList, "Default Applications"); | ||
132 | defaultApp.writeXdgListEntry(QStringLiteral("x-scheme-handler/http"), QStringList(browserStorageId)); | 134 | defaultApp.writeXdgListEntry(QStringLiteral("x-scheme-handler/http"), QStringList(browserStorageId)); | ||
133 | defaultApp.writeXdgListEntry(QStringLiteral("x-scheme-handler/https"), QStringList(browserStorageId)); | 135 | defaultApp.writeXdgListEntry(QStringLiteral("x-scheme-handler/https"), QStringList(browserStorageId)); | ||
134 | mimeAppList->sync(); | 136 | mimeAppList->sync(); | ||
135 | 137 | | |||
136 | KBuildSycocaProgressDialog::rebuildKSycoca(this); | | |||
137 | | ||||
138 | QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.klauncher5"), | 138 | QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.klauncher5"), | ||
139 | QStringLiteral("/KLauncher"), | 139 | QStringLiteral("/KLauncher"), | ||
140 | QStringLiteral("org.kde.KLauncher"), | 140 | QStringLiteral("org.kde.KLauncher"), | ||
141 | QStringLiteral("reparseConfiguration")); | 141 | QStringLiteral("reparseConfiguration")); | ||
142 | QDBusConnection::sessionBus().send(message); | 142 | QDBusConnection::sessionBus().send(message); | ||
143 | 143 | | |||
144 | m_currentIndex = browserCombo->currentIndex(); | 144 | m_currentIndex = currentIndex(); | ||
145 | 145 | | |||
146 | emit changed(false); | 146 | emit changed(false); | ||
147 | } | 147 | } | ||
148 | } | 148 | } |
As a future step I would like those default components not hardcoded in the code