File indexing completed on 2024-04-21 04:58:02

0001 /*
0002     SPDX-FileCopyrightText: 2001 Dawit Alemayehu <adawit@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "uachangerplugin.h"
0008 #include "interfaces/browser.h"
0009 
0010 #include <sys/utsname.h>
0011 
0012 #include <QMenu>
0013 #include <QWebEngineProfile>
0014 #include <QApplication>
0015 #include <QMetaObject>
0016 #include <QActionGroup>
0017 
0018 #include <kwidgetsaddons_version.h>
0019 #include <kactionmenu.h>
0020 #include <klocalizedstring.h>
0021 #include <kservice.h>
0022 #include <kconfiggroup.h>
0023 #include <kpluginfactory.h>
0024 #include <kprotocolmanager.h>
0025 #include <kactioncollection.h>
0026 #include <ksharedconfig.h>
0027 
0028 #include <KIO/ApplicationLauncherJob>
0029 #include <KIO/JobUiDelegate>
0030 #include <kparts/openurlarguments.h>
0031 
0032 #include <kio/job.h>
0033 
0034 using namespace KonqInterfaces;
0035 
0036 K_PLUGIN_CLASS_WITH_JSON(UAChangerPlugin, "uachangerplugin.json")
0037 
0038 #define UA_PTOS(x) (*it)->property(x).toString()
0039 #define QFL1(x) QLatin1String(x)
0040 
0041 UAChangerPlugin::UAChangerPlugin(QObject *parent,
0042                                  const QVariantList &)
0043     : KonqParts::Plugin(parent), m_part(nullptr)
0044 {
0045     m_pUAMenu = new KActionMenu(QIcon::fromTheme("preferences-web-browser-identification"),
0046                                 i18nc("@title:menu Changes the browser identification", "Change Browser Identification"),
0047                                 actionCollection());
0048     actionCollection()->addAction("changeuseragent", m_pUAMenu);
0049     m_pUAMenu->setPopupMode(QToolButton::InstantPopup);
0050     connect(m_pUAMenu->menu(), &QMenu::aboutToShow, this, &UAChangerPlugin::slotAboutToShow);
0051     initMenu();
0052 }
0053 
0054 UAChangerPlugin::~UAChangerPlugin()
0055 {
0056 }
0057 
0058 void UAChangerPlugin::slotAboutToShow()
0059 {
0060     clearMenu();
0061 
0062     KConfigGroup grp = KSharedConfig::openConfig("useragenttemplatesrc")->group("Templates");
0063     TemplateMap templates = grp.entryMap();
0064     QList<QAction*> actions = fillMenu(templates);
0065     actions.append(m_defaultAction);
0066 
0067     Browser *browser = Browser::browser(qApp);
0068     const QString currentUA = browser ? browser->userAgent() : QString();
0069 
0070     auto isCurrentUA = [currentUA](QAction *a){return currentUA == a->data().toString();};
0071     auto found = std::find_if(actions.constBegin(), actions.constEnd(), isCurrentUA);
0072     if (found != actions.constEnd()) {
0073         (*found)->setChecked(true);
0074     } else {
0075         m_defaultAction->setChecked(true);
0076     }
0077 }
0078 
0079 void UAChangerPlugin::initMenu()
0080 {
0081     m_actionGroup = new QActionGroup(m_pUAMenu->menu());
0082     m_actionGroup->setExclusive(true);
0083     m_defaultAction = new QAction(i18nc("@action:inmenu Uses the default browser identification", "Default Identification"), this);
0084     m_defaultAction->setCheckable(true);
0085     m_pUAMenu->menu()->addAction(m_defaultAction);
0086     m_actionGroup->addAction(m_defaultAction);
0087 
0088     connect(m_actionGroup, &QActionGroup::triggered, this, &UAChangerPlugin::slotItemSelected);
0089 }
0090 
0091 QList<QAction*> UAChangerPlugin::fillMenu(const TemplateMap &templates)
0092 {
0093     m_pUAMenu->menu()->addSeparator();
0094     QList<QAction*> actions;
0095     for (auto it = templates.constBegin(); it != templates.constEnd(); ++it) {
0096         QAction *a = new QAction(it.key());
0097         a->setData(it.value());
0098         m_pUAMenu->addAction(a);
0099         m_actionGroup->addAction(a);
0100         a->setCheckable(true);
0101         actions.append(a);
0102     }
0103     return actions;
0104 }
0105 
0106 void UAChangerPlugin::clearMenu()
0107 {
0108     QList<QAction*> actions = m_actionGroup->actions();
0109     for (QAction *a : actions) {
0110         if (a != m_defaultAction) {
0111             a->deleteLater();
0112         }
0113     }
0114 }
0115 
0116 void UAChangerPlugin::slotItemSelected(QAction *action)
0117 {
0118     Browser *browser = Browser::browser(qApp);
0119     if (!browser) {
0120         return;
0121     }
0122     QString uaString = action->data().toString();
0123     if (action == m_defaultAction) {
0124         uaString.clear();
0125     }
0126     browser->setTemporaryUserAgent(uaString);
0127 }
0128 
0129 #include "uachangerplugin.moc"