Changeset View
Changeset View
Standalone View
Standalone View
plugins/uachanger/uachangerplugin.cpp
Show All 16 Lines | 1 | /* | |||
---|---|---|---|---|---|
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | 18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "uachangerplugin.h" | 21 | #include "uachangerplugin.h" | ||
22 | 22 | | |||
23 | #include <sys/utsname.h> | 23 | #include <sys/utsname.h> | ||
24 | 24 | | |||
25 | #include <QMenu> | ||||
25 | #include <QRegExp> | 26 | #include <QRegExp> | ||
26 | #include <QDBusConnection> | 27 | | ||
27 | #include <QDBusMessage> | | |||
28 | #include <kicon.h> | | |||
29 | #include <kactionmenu.h> | 28 | #include <kactionmenu.h> | ||
30 | #include <kservicetypetrader.h> | 29 | #include <kservicetypetrader.h> | ||
31 | #include <krun.h> | 30 | #include <krun.h> | ||
32 | #include <kdebug.h> | 31 | #include <klocalizedstring.h> | ||
33 | #include <kaction.h> | | |||
34 | #include <klocale.h> | | |||
35 | #include <kglobal.h> | | |||
36 | #include <kconfig.h> | | |||
37 | #include <kio/job.h> | | |||
38 | #include <kio/scheduler.h> | | |||
39 | #include <kservice.h> | 32 | #include <kservice.h> | ||
40 | #include <kcomponentdata.h> | 33 | #include <kconfiggroup.h> | ||
41 | #include <kmenu.h> | | |||
42 | #include <KConfigGroup> | | |||
43 | #include <kparts/part.h> | | |||
44 | #include <kpluginfactory.h> | 34 | #include <kpluginfactory.h> | ||
45 | #include <kprotocolmanager.h> | 35 | #include <kprotocolmanager.h> | ||
46 | #include <kaboutdata.h> | | |||
47 | #include <kactioncollection.h> | 36 | #include <kactioncollection.h> | ||
37 | #include <ksharedconfig.h> | ||||
38 | | ||||
39 | #include <kparts/openurlarguments.h> | ||||
dfaure: (not needed) | |||||
40 | | ||||
41 | #include <kio/job.h> | ||||
42 | #include <kio/scheduler.h> | ||||
48 | 43 | | |||
49 | static const KAboutData aboutdata("uachangerplugin", 0, ki18n("Change Browser Identification"), "1.0"); | | |||
50 | K_PLUGIN_FACTORY(UAChangerPluginFactory, registerPlugin<UAChangerPlugin>();) | 44 | K_PLUGIN_FACTORY(UAChangerPluginFactory, registerPlugin<UAChangerPlugin>();) | ||
51 | K_EXPORT_PLUGIN(UAChangerPluginFactory(aboutdata)) | | |||
52 | 45 | | |||
53 | #define UA_PTOS(x) (*it)->property(x).toString() | 46 | #define UA_PTOS(x) (*it)->property(x).toString() | ||
54 | #define QFL1(x) QLatin1String(x) | 47 | #define QFL1(x) QLatin1String(x) | ||
55 | 48 | | |||
56 | UAChangerPlugin::UAChangerPlugin(QObject *parent, | 49 | UAChangerPlugin::UAChangerPlugin(QObject *parent, | ||
57 | const QVariantList &) | 50 | const QVariantList &) | ||
58 | : KParts::Plugin(parent), | 51 | : KParts::Plugin(parent), | ||
59 | m_bSettingsLoaded(false), m_part(0L), m_config(0L) | 52 | m_bSettingsLoaded(false), m_part(0L), m_config(0L) | ||
60 | { | 53 | { | ||
61 | setComponentData(UAChangerPlugin::componentData()); | 54 | m_pUAMenu = new KActionMenu(QIcon::fromTheme("preferences-web-browser-identification"), | ||
62 | 55 | i18n("Change Browser Identification"), | |||
63 | m_pUAMenu = new KActionMenu(KIcon("preferences-web-browser-identification"), i18n("Change Browser &Identification"), | | |||
64 | actionCollection()); | 56 | actionCollection()); | ||
65 | actionCollection()->addAction("changeuseragent", m_pUAMenu); | 57 | actionCollection()->addAction("changeuseragent", m_pUAMenu); | ||
66 | m_pUAMenu->setDelayed(false); | 58 | m_pUAMenu->setDelayed(false); | ||
67 | connect(m_pUAMenu->menu(), SIGNAL(aboutToShow()), | 59 | connect(m_pUAMenu->menu(), &QMenu::aboutToShow, this, &UAChangerPlugin::slotAboutToShow); | ||
68 | this, SLOT(slotAboutToShow())); | | |||
69 | 60 | | |||
70 | if (parent) { | 61 | if (parent!=nullptr) { | ||
71 | m_part = qobject_cast<KParts::ReadOnlyPart *>(parent); | 62 | m_part = qobject_cast<KParts::ReadOnlyPart *>(parent); | ||
72 | connect(m_part, SIGNAL(started(KIO::Job*)), this, | 63 | connect(m_part, &KParts::ReadOnlyPart::started, this, &UAChangerPlugin::slotEnableMenu); | ||
73 | SLOT(slotEnableMenu())); | 64 | connect(m_part, QOverload<>::of(&KParts::ReadOnlyPart::completed), this, &UAChangerPlugin::slotEnableMenu); | ||
74 | connect(m_part, SIGNAL(completed()), this, | 65 | connect(m_part, QOverload<bool>::of(&KParts::ReadOnlyPart::completed), this, &UAChangerPlugin::slotEnableMenu); | ||
75 | SLOT(slotEnableMenu())); | | |||
76 | connect(m_part, SIGNAL(completed(bool)), this, | | |||
77 | SLOT(slotEnableMenu())); | | |||
78 | } | 66 | } | ||
79 | } | 67 | } | ||
80 | 68 | | |||
81 | UAChangerPlugin::~UAChangerPlugin() | 69 | UAChangerPlugin::~UAChangerPlugin() | ||
82 | { | 70 | { | ||
83 | saveSettings(); | 71 | saveSettings(); | ||
84 | slotReloadDescriptions(); | 72 | slotReloadDescriptions(); | ||
85 | } | 73 | } | ||
Show All 13 Lines | 82 | { | |||
99 | 87 | | |||
100 | m_mapAlias.clear(); | 88 | m_mapAlias.clear(); | ||
101 | m_lstAlias.clear(); | 89 | m_lstAlias.clear(); | ||
102 | m_lstIdentity.clear(); | 90 | m_lstIdentity.clear(); | ||
103 | 91 | | |||
104 | struct utsname utsn; | 92 | struct utsname utsn; | ||
105 | uname(&utsn); | 93 | uname(&utsn); | ||
106 | 94 | | |||
107 | QStringList languageList = KLocale::global()->languageList(); | 95 | QStringList languageList = KLocalizedString::languages(); | ||
108 | if (!languageList.isEmpty()) { | 96 | if (!languageList.isEmpty()) { | ||
109 | const int index = languageList.indexOf(QFL1("C")); | 97 | const int index = languageList.indexOf(QFL1("C")); | ||
110 | if (index > -1) { | 98 | if (index > -1) { | ||
Are you sure the KConfigGroup keeps the KSharedConfig alive? It would be good if it did, but I'm not sure it does. Any reason not to use QLocale().uiLanguages()? dfaure: Are you sure the KConfigGroup keeps the KSharedConfig alive? It would be good if it did, but… | |||||
Have used the KSharedConfig::openConfig()->group(...) construct before and it seems to work, but no longer necessary because of below. marten: Have used the KSharedConfig::openConfig()->group(...) construct before and it seems to work… | |||||
111 | if (languageList.contains(QFL1("en"))) { | 99 | if (languageList.contains(QFL1("en"))) { | ||
112 | languageList.removeAt(index); | 100 | languageList.removeAt(index); | ||
113 | } else { | 101 | } else { | ||
114 | languageList[index] = QFL1("en"); | 102 | languageList[index] = QFL1("en"); | ||
115 | } | 103 | } | ||
116 | } | 104 | } | ||
pino: please use KLocalizedString::languages() instead | |||||
Didn't know about KLocalizedString::languages(), thanks for the pointer. marten: Didn't know about KLocalizedString::languages(), thanks for the pointer. | |||||
117 | } | 105 | } | ||
118 | 106 | | |||
119 | KService::List::ConstIterator it = list.constBegin(); | 107 | KService::List::ConstIterator it = list.constBegin(); | ||
120 | KService::List::ConstIterator lastItem = list.constEnd(); | 108 | KService::List::ConstIterator lastItem = list.constEnd(); | ||
121 | 109 | | |||
122 | for (; it != lastItem; ++it) { | 110 | for (; it != lastItem; ++it) { | ||
123 | QString ua = UA_PTOS("X-KDE-UA-FULL"); | 111 | QString ua = UA_PTOS("X-KDE-UA-FULL"); | ||
124 | QString tag = UA_PTOS("X-KDE-UA-TAG"); | 112 | QString tag = UA_PTOS("X-KDE-UA-TAG"); | ||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | |||||
192 | } | 180 | } | ||
193 | } | 181 | } | ||
194 | 182 | | |||
195 | void UAChangerPlugin::slotEnableMenu() | 183 | void UAChangerPlugin::slotEnableMenu() | ||
196 | { | 184 | { | ||
197 | m_currentURL = m_part->url(); | 185 | m_currentURL = m_part->url(); | ||
198 | 186 | | |||
199 | // This plugin works on local files, http[s], and webdav[s]. | 187 | // This plugin works on local files, http[s], and webdav[s]. | ||
200 | QString proto = m_currentURL.protocol(); | 188 | const QString proto = m_currentURL.scheme(); | ||
201 | if (m_currentURL.isLocalFile() || | 189 | if (m_currentURL.isLocalFile() || | ||
202 | proto.startsWith("http") || proto.startsWith("webdav")) { | 190 | proto.startsWith("http") || proto.startsWith("webdav")) { | ||
203 | if (!m_pUAMenu->isEnabled()) { | 191 | if (!m_pUAMenu->isEnabled()) { | ||
204 | m_pUAMenu->setEnabled(true); | 192 | m_pUAMenu->setEnabled(true); | ||
205 | } | 193 | } | ||
206 | } else { | 194 | } else { | ||
207 | m_pUAMenu->setEnabled(false); | 195 | m_pUAMenu->setEnabled(false); | ||
208 | } | 196 | } | ||
209 | } | 197 | } | ||
210 | 198 | | |||
211 | void UAChangerPlugin::slotAboutToShow() | 199 | void UAChangerPlugin::slotAboutToShow() | ||
212 | { | 200 | { | ||
213 | if (!m_config) { | 201 | if (!m_config) { | ||
214 | m_config = new KConfig("kio_httprc"); | 202 | m_config = new KConfig("kio_httprc"); | ||
215 | parseDescFiles(); | 203 | parseDescFiles(); | ||
216 | } | 204 | } | ||
217 | 205 | | |||
218 | if (!m_bSettingsLoaded) { | 206 | if (!m_bSettingsLoaded) { | ||
219 | loadSettings(); | 207 | loadSettings(); | ||
220 | } | 208 | } | ||
221 | 209 | | |||
222 | if (m_pUAMenu->menu()->actions().isEmpty()) { // need to create the actions | 210 | if (m_pUAMenu->menu()->actions().isEmpty()) { // need to create the actions | ||
223 | m_pUAMenu->menu()->addTitle(i18n("Identify As")); // imho title doesn't need colon.. | | |||
224 | | ||||
225 | m_defaultAction = new QAction(i18n("Default Identification"), this); | 211 | m_defaultAction = new QAction(i18n("Default Identification"), this); | ||
226 | m_defaultAction->setCheckable(true); | 212 | m_defaultAction->setCheckable(true); | ||
227 | connect(m_defaultAction, SIGNAL(triggered()), this, SLOT(slotDefault())); | 213 | connect(m_defaultAction, &QAction::triggered, this, &UAChangerPlugin::slotDefault); | ||
228 | m_pUAMenu->menu()->addAction(m_defaultAction); | 214 | m_pUAMenu->menu()->addAction(m_defaultAction); | ||
229 | 215 | | |||
230 | m_pUAMenu->menu()->addSeparator(); | 216 | m_pUAMenu->menu()->addSeparator(); | ||
231 | 217 | | |||
232 | m_actionGroup = new QActionGroup(m_pUAMenu->menu()); | 218 | m_actionGroup = new QActionGroup(m_pUAMenu->menu()); | ||
233 | AliasConstIterator map = m_mapAlias.constBegin(); | 219 | AliasConstIterator map = m_mapAlias.constBegin(); | ||
234 | for (; map != m_mapAlias.constEnd(); ++map) { | 220 | for (; map != m_mapAlias.constEnd(); ++map) { | ||
235 | QMenu *browserMenu = m_pUAMenu->menu()->addMenu(m_mapBrowser.value(map.key())); | 221 | QMenu *browserMenu = m_pUAMenu->menu()->addMenu(m_mapBrowser.value(map.key())); | ||
236 | BrowserGroup::ConstIterator e = map.value().begin(); | 222 | BrowserGroup::ConstIterator e = map.value().begin(); | ||
237 | for (; e != map.value().end(); ++e) { | 223 | for (; e != map.value().end(); ++e) { | ||
238 | QAction *action = new QAction(m_lstAlias[*e], m_actionGroup); | 224 | QAction *action = new QAction(m_lstAlias[*e], m_actionGroup); | ||
239 | action->setCheckable(true); | 225 | action->setCheckable(true); | ||
240 | action->setData(*e); | 226 | action->setData(*e); | ||
241 | browserMenu->addAction(action); | 227 | browserMenu->addAction(action); | ||
242 | } | 228 | } | ||
243 | } | 229 | } | ||
244 | connect(m_actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotItemSelected(QAction*))); | 230 | connect(m_actionGroup, &QActionGroup::triggered, this, &UAChangerPlugin::slotItemSelected); | ||
245 | 231 | | |||
246 | m_pUAMenu->menu()->addSeparator(); | 232 | m_pUAMenu->menu()->addSeparator(); | ||
247 | 233 | | |||
248 | /* useless here, imho.. | 234 | /* useless here, imho.. | ||
249 | m_pUAMenu->menu()->insertItem( i18n("Reload Identifications"), this, | 235 | m_pUAMenu->menu()->insertItem( i18n("Reload Identifications"), this, | ||
250 | SLOT(slotReloadDescriptions()), | 236 | SLOT(slotReloadDescriptions()), | ||
251 | 0, ++count );*/ | 237 | 0, ++count );*/ | ||
252 | 238 | | |||
253 | m_applyEntireSiteAction = new QAction(i18n("Apply to Entire Site"), this); | 239 | m_applyEntireSiteAction = new QAction(i18n("Apply to Entire Site"), this); | ||
254 | m_applyEntireSiteAction->setCheckable(true); | 240 | m_applyEntireSiteAction->setCheckable(true); | ||
255 | connect(m_applyEntireSiteAction, SIGNAL(triggered()), this, SLOT(slotApplyToDomain())); | 241 | m_applyEntireSiteAction->setChecked(m_bApplyToDomain); | ||
256 | m_pUAMenu->menu()->addAction(i18n("Apply to Entire Site")); | 242 | connect(m_applyEntireSiteAction, &QAction::triggered, this, &UAChangerPlugin::slotApplyToDomain); | ||
dfaure: (wow that was broken) | |||||
243 | m_pUAMenu->menu()->addAction(m_applyEntireSiteAction); | ||||
257 | 244 | | |||
258 | m_pUAMenu->menu()->addAction(i18n("Configure..."), this, | 245 | m_pUAMenu->menu()->addAction(i18n("Configure..."), this, &UAChangerPlugin::slotConfigure); | ||
259 | SLOT(slotConfigure())); | | |||
260 | } | 246 | } | ||
261 | 247 | | |||
262 | // Reflect current settings in the actions | 248 | // Reflect current settings in the actions | ||
263 | 249 | | |||
264 | QString host = m_currentURL.isLocalFile() ? QFL1("localhost") : m_currentURL.host(); | 250 | QString host = m_currentURL.isLocalFile() ? QFL1("localhost") : m_currentURL.host(); | ||
265 | m_currentUserAgent = KProtocolManager::userAgentForHost(host); | 251 | m_currentUserAgent = KProtocolManager::userAgentForHost(host); | ||
266 | //kDebug(90130) << "User Agent: " << m_currentUserAgent; | 252 | //qDebug() << "User Agent: " << m_currentUserAgent; | ||
267 | m_defaultAction->setChecked(m_currentUserAgent == KProtocolManager::defaultUserAgent()); | 253 | m_defaultAction->setChecked(m_currentUserAgent == KProtocolManager::defaultUserAgent()); | ||
268 | 254 | | |||
269 | m_applyEntireSiteAction->setChecked(m_bApplyToDomain); | 255 | m_applyEntireSiteAction->setChecked(m_bApplyToDomain); | ||
270 | Q_FOREACH (QAction *action, m_actionGroup->actions()) { | 256 | Q_FOREACH (QAction *action, m_actionGroup->actions()) { | ||
271 | const int id = action->data().toInt(); | 257 | const int id = action->data().toInt(); | ||
272 | action->setChecked(m_lstIdentity[id] == m_currentUserAgent); | 258 | action->setChecked(m_lstIdentity[id] == m_currentUserAgent); | ||
273 | } | 259 | } | ||
274 | 260 | | |||
Show All 14 Lines | 274 | if (m_lstIdentity[id] == m_currentUserAgent) { | |||
289 | return; | 275 | return; | ||
290 | } | 276 | } | ||
291 | 277 | | |||
292 | m_currentUserAgent = m_lstIdentity[id]; | 278 | m_currentUserAgent = m_lstIdentity[id]; | ||
293 | QString host = m_currentURL.isLocalFile() ? QFL1("localhost") : filterHost(m_currentURL.host()); | 279 | QString host = m_currentURL.isLocalFile() ? QFL1("localhost") : filterHost(m_currentURL.host()); | ||
294 | 280 | | |||
295 | KConfigGroup grp = m_config->group(host.toLower()); | 281 | KConfigGroup grp = m_config->group(host.toLower()); | ||
296 | grp.writeEntry("UserAgent", m_currentUserAgent); | 282 | grp.writeEntry("UserAgent", m_currentUserAgent); | ||
297 | //kDebug(90130) << "Writing out UserAgent=" << m_currentUserAgent << "for host=" << host; | 283 | //qDebug() << "Writing out UserAgent=" << m_currentUserAgent << "for host=" << host; | ||
298 | grp.sync(); | 284 | grp.sync(); | ||
299 | 285 | | |||
300 | // Reload the page with the new user-agent string | 286 | // Reload the page with the new user-agent string | ||
301 | reloadPage(); | 287 | reloadPage(); | ||
302 | } | 288 | } | ||
303 | 289 | | |||
304 | void UAChangerPlugin::slotDefault() | 290 | void UAChangerPlugin::slotDefault() | ||
305 | { | 291 | { | ||
306 | if (m_currentUserAgent == KProtocolManager::defaultUserAgent()) { | 292 | if (m_currentUserAgent == KProtocolManager::defaultUserAgent()) { | ||
307 | return; // don't flicker! | 293 | return; // don't flicker! | ||
308 | } | 294 | } | ||
309 | // We have no choice but delete all higher domain level settings here since it | 295 | // We have no choice but delete all higher domain level settings here since it | ||
310 | // affects what will be matched. | 296 | // affects what will be matched. | ||
311 | QStringList partList = m_currentURL.host().split(' ', QString::SkipEmptyParts); | 297 | QStringList partList = m_currentURL.host().split(QLatin1Char(' '), | ||
298 | #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) | ||||
299 | Qt::SkipEmptyParts); | ||||
300 | #else | ||||
301 | QString::SkipEmptyParts); | ||||
302 | #endif | ||||
312 | if (!partList.isEmpty()) { | 303 | if (!partList.isEmpty()) { | ||
313 | partList.removeFirst(); | 304 | partList.removeFirst(); | ||
314 | 305 | | |||
315 | QStringList domains; | 306 | QStringList domains; | ||
316 | // Remove the exact name match... | 307 | // Remove the exact name match... | ||
317 | domains << m_currentURL.host(); | 308 | domains << m_currentURL.host(); | ||
318 | 309 | | |||
319 | while (partList.count()) { | 310 | while (partList.count()) { | ||
320 | if (partList.count() == 2) | 311 | if (partList.count() == 2) | ||
321 | if (partList[0].length() <= 2 && partList[1].length() == 2) { | 312 | if (partList[0].length() <= 2 && partList[1].length() == 2) { | ||
322 | break; | 313 | break; | ||
323 | } | 314 | } | ||
324 | 315 | | |||
325 | if (partList.count() == 1) { | 316 | if (partList.count() == 1) { | ||
326 | break; | 317 | break; | ||
327 | } | 318 | } | ||
328 | 319 | | |||
329 | domains << partList.join(QFL1(".")); | 320 | domains << partList.join(QFL1(".")); | ||
330 | partList.removeFirst(); | 321 | partList.removeFirst(); | ||
331 | } | 322 | } | ||
332 | 323 | | |||
333 | KConfigGroup grp(m_config, QString()); | 324 | KConfigGroup grp(m_config, QString()); | ||
334 | for (QStringList::Iterator it = domains.begin(); it != domains.end(); it++) { | 325 | for (QStringList::Iterator it = domains.begin(); it != domains.end(); it++) { | ||
335 | //kDebug () << "Domain to remove: " << *it; | 326 | //qDebug () << "Domain to remove: " << *it; | ||
336 | if (grp.hasGroup(*it)) { | 327 | if (grp.hasGroup(*it)) { | ||
337 | grp.deleteGroup(*it); | 328 | grp.deleteGroup(*it); | ||
338 | } else if (grp.hasKey(*it)) { | 329 | } else if (grp.hasKey(*it)) { | ||
339 | grp.deleteEntry(*it); | 330 | grp.deleteEntry(*it); | ||
340 | } | 331 | } | ||
341 | } | 332 | } | ||
342 | } else if (m_currentURL.isLocalFile() && m_config->hasGroup("localhost")) { | 333 | } else if (m_currentURL.isLocalFile() && m_config->hasGroup("localhost")) { | ||
343 | m_config->deleteGroup("localhost"); | 334 | m_config->deleteGroup("localhost"); | ||
Show All 36 Lines | 358 | { | |||
380 | } | 371 | } | ||
381 | 372 | | |||
382 | // Return the TLD if apply to domain or | 373 | // Return the TLD if apply to domain or | ||
383 | return (m_bApplyToDomain ? findTLD(hostname) : hostname); | 374 | return (m_bApplyToDomain ? findTLD(hostname) : hostname); | ||
384 | } | 375 | } | ||
385 | 376 | | |||
386 | QString UAChangerPlugin::findTLD(const QString &hostname) | 377 | QString UAChangerPlugin::findTLD(const QString &hostname) | ||
387 | { | 378 | { | ||
388 | QStringList domains; | 379 | // As per the documentation for QUrl::topLevelDomain(), the "entire site" | ||
389 | QStringList partList = hostname.split(' ', QString::SkipEmptyParts); | 380 | // for a hostname is considered to be the TLD suffix as returned by that | ||
390 | 381 | // function, prefixed by the hostname component immediately before it. | |||
391 | if (partList.count()) { | 382 | // For example, QUrl::topLevelDomain("http://www.kde.org") gives ".org" | ||
392 | partList.removeFirst(); // Remove hostname | 383 | // so the returned result is "kde.org". | ||
393 | } | 384 | | ||
394 | 385 | QUrl u; | |||
QUrl u; u.setScheme("http"); u.setHostName(hostname); would be better in case of special characters in the hostname. dfaure: `QUrl u; u.setScheme("http"); u.setHostName(hostname);` would be better in case of special… | |||||
395 | while (partList.count()) { | 386 | u.setScheme("http"); | ||
396 | // We only have a TLD left. | 387 | u.setHost(hostname); // gives http://hostname/ | ||
397 | if (partList.count() == 1) { | 388 | | ||
398 | break; | 389 | const QString tld = u.topLevelDomain(QUrl::EncodeUnicode); | ||
399 | } | 390 | if (tld.isEmpty()) return hostname; // name has no valid TLD | ||
400 | 391 | | |||
401 | if (partList.count() == 2) { | 392 | const QString prefix = hostname.chopped(tld.length()); // remaining prefix of name | ||
402 | // The .name domain uses <name>.<surname>.name | 393 | const int idx = prefix.lastIndexOf(QLatin1Char('.')); | ||
403 | // Although the TLD is striclty speaking .name, for our purpose | 394 | const QString prev = prefix.mid(idx+1); // works even if no '.' | ||
dfaure: could be const too | |||||
404 | // it should be <surname>.name since people should not be able | 395 | return prev+tld; | ||
405 | // to set cookies for everyone with the same surname. | | |||
406 | // Matches <surname>.name | | |||
407 | if (partList[1].toLower() == QFL1("name")) { | | |||
408 | break; | | |||
409 | } else if (partList[1].length() == 2) { | | |||
410 | // If this is a TLD, we should stop. (e.g. co.uk) | | |||
411 | // We assume this is a TLD if it ends with .xx.yy or .x.yy | | |||
412 | if (partList[0].length() <= 2) { | | |||
413 | break; // This is a TLD. | | |||
414 | } | | |||
415 | | ||||
416 | // Catch some TLDs that we miss with the previous check | | |||
417 | // e.g. com.au, org.uk, mil.co | | |||
418 | QByteArray t = partList[0].toLower().toUtf8(); | | |||
419 | if ((t == "com") || (t == "net") || (t == "org") || (t == "gov") || | | |||
420 | (t == "edu") || (t == "mil") || (t == "int")) { | | |||
421 | break; | | |||
422 | } | | |||
423 | } | | |||
424 | } | | |||
425 | | ||||
426 | domains.append(partList.join(QFL1("."))); | | |||
427 | partList.removeFirst(); // Remove part | | |||
428 | } | | |||
429 | | ||||
430 | if (domains.isEmpty()) { | | |||
431 | return hostname; | | |||
432 | } | | |||
433 | | ||||
434 | return domains[0]; | | |||
435 | } | 396 | } | ||
dfaure: Please use `return hostname;` without parenthesis. `return` isn't a function. | |||||
436 | 397 | | |||
437 | void UAChangerPlugin::saveSettings() | 398 | void UAChangerPlugin::saveSettings() | ||
438 | { | 399 | { | ||
439 | if (!m_bSettingsLoaded) { | 400 | if (!m_bSettingsLoaded) { | ||
440 | return; | 401 | return; | ||
dfaure: same | |||||
441 | } | 402 | } | ||
442 | 403 | | |||
443 | KConfig cfg("uachangerrc", KConfig::NoGlobals); | 404 | KConfig cfg("uachangerrc", KConfig::NoGlobals); | ||
444 | KConfigGroup grp = cfg.group("General"); | 405 | KConfigGroup grp = cfg.group("General"); | ||
445 | 406 | | |||
446 | grp.writeEntry("applyToDomain", m_bApplyToDomain); | 407 | grp.writeEntry("applyToDomain", m_bApplyToDomain); | ||
447 | } | 408 | } | ||
448 | 409 | | |||
Show All 15 Lines |
(not needed)