From ca30cfc23ec5fcfc0eec5a9874a6d70702c2f926 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Tue, 31 Jul 2012 23:32:55 +0200 Subject: Restored User Agent Manager and cleaned up --- src/CMakeLists.txt | 7 ++ src/useragent/useragentinfo.cpp | 202 +++++++++++++++++++++++++++++++++++++ src/useragent/useragentinfo.h | 86 ++++++++++++++++ src/useragent/useragentmanager.cpp | 190 ++++++++++++++++++++++++++++++++++ src/useragent/useragentmanager.h | 70 +++++++++++++ src/useragent/useragentsettings.ui | 69 +++++++++++++ src/useragent/useragentwidget.cpp | 96 ++++++++++++++++++ src/useragent/useragentwidget.h | 53 ++++++++++ 8 files changed, 773 insertions(+) create mode 100644 src/useragent/useragentinfo.cpp create mode 100644 src/useragent/useragentinfo.h create mode 100644 src/useragent/useragentmanager.cpp create mode 100644 src/useragent/useragentmanager.h create mode 100644 src/useragent/useragentsettings.ui create mode 100644 src/useragent/useragentwidget.cpp create mode 100644 src/useragent/useragentwidget.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 35f166e7..3e937655 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -64,6 +64,10 @@ set(rekonq_KDEINIT_SRCS tabwindow/tabpreviewpopup.cpp tabwindow/tabwindow.cpp #---------------------------------------- + useragent/useragentinfo.cpp + useragent/useragentmanager.cpp + useragent/useragentwidget.cpp + #---------------------------------------- webwindow/networkaccessmanager.cpp webwindow/webpage.cpp webwindow/webwindow.cpp @@ -110,6 +114,8 @@ KDE4_ADD_UI_FILES( rekonq_KDEINIT_SRCS sync/sync_google_settings.ui sync/sync_opera_settings.ui sync/sync_host_type.ui + # ---------------------------------------- + useragent/useragentsettings.ui ) @@ -124,6 +130,7 @@ INCLUDE_DIRECTORIES ( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/sync ${CMAKE_CURRENT_SOURCE_DIR}/urlbar ${CMAKE_CURRENT_SOURCE_DIR}/tabwindow + ${CMAKE_CURRENT_SOURCE_DIR}/useragent ${CMAKE_CURRENT_SOURCE_DIR}/webwindow ${CMAKE_CURRENT_BINARY_DIR} ${KDE4_INCLUDES} diff --git a/src/useragent/useragentinfo.cpp b/src/useragent/useragentinfo.cpp new file mode 100644 index 00000000..00f0a9db --- /dev/null +++ b/src/useragent/useragentinfo.cpp @@ -0,0 +1,202 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (c) 2001 by Dawit Alemayehu +* Copyright (C) 2010-2012 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + +// Self Includes +#include "useragentinfo.h" + +// Standard Includes +#include +#include + +// KDE Includes +#include +#include + +#include +#include + +#include + +// Qt includes +#include + +UserAgentInfo::UserAgentInfo() +{ + m_providers = KServiceTypeTrader::self()->query("UserAgentStrings"); +} + + +KService::List UserAgentInfo::availableProviders() const +{ + return m_providers; +} + + +QString UserAgentInfo::userAgentString(int i) +{ + if (i < 0 || !providerExists(i)) + { + kDebug() << "oh oh... wrong index on the user agent choice! INDEX = " << i; + return QL1S("Default"); + } + + QString tmp = m_providers.at(i)->property("X-KDE-UA-FULL").toString(); + + struct utsname utsn; + uname(&utsn); + + tmp.replace(QL1S("appSysName"), QString(utsn.sysname)); + tmp.replace(QL1S("appSysRelease"), QString(utsn.release)); + tmp.replace(QL1S("appMachineType"), QString(utsn.machine)); + + QStringList languageList = KGlobal::locale()->languageList(); + if (languageList.count()) + { + int ind = languageList.indexOf(QL1S("C")); + if (ind >= 0) + { + if (languageList.contains(QL1S("en"))) + languageList.removeAt(ind); + else + languageList.value(ind) = QL1S("en"); + } + } + + tmp.replace(QL1S("appLanguage"), QString("%1").arg(languageList.join(", "))); + tmp.replace(QL1S("appPlatform"), QL1S("X11")); + + return tmp; +} + + +QString UserAgentInfo::userAgentName(int i) +{ + if (i < 0 || !providerExists(i)) + { + kDebug() << "oh oh... wrong index on the user agent choice! INDEX = " << i; + return QL1S("Default"); + } + + return m_providers.at(i)->property("X-KDE-UA-NAME").toString(); +} + + +QString UserAgentInfo::userAgentVersion(int i) +{ + if (i < 0 || !providerExists(i)) + { + kDebug() << "oh oh... wrong index on the user agent choice! INDEX = " << i; + return QL1S("Default"); + } + + return m_providers.at(i)->property("X-KDE-UA-VERSION").toString(); +} + + +QString UserAgentInfo::userAgentDescription(int i) +{ + if (i < 0 || !providerExists(i)) + { + kDebug() << "oh oh... wrong index on the user agent choice! INDEX = " << i; + return QL1S("Default"); + } + + QString systemName = m_providers.at(i)->property("X-KDE-UA-SYSNAME").toString(); + QString systemRelease = m_providers.at(i)->property("X-KDE-UA-SYSRELEASE").toString(); + QString systemSummary; + + if (!systemName.isEmpty() && !systemRelease.isEmpty()) + { + systemSummary = i18nc("describe UA platform, eg: firefox 3.1 \"on Windows XP\"", " on %1 %2", systemName, systemRelease); + } + + return userAgentName(i) % QL1C(' ') % userAgentVersion(i) % systemSummary; +} + + +QStringList UserAgentInfo::availableUserAgents() +{ + QStringList UAs; + int n = m_providers.count(); + for (int i = 0; i < n; ++i) + { + UAs << userAgentDescription(i); + } + return UAs; +} + + +bool UserAgentInfo::setUserAgentForHost(int uaIndex, const QString &host) +{ + KConfig config("kio_httprc", KConfig::NoGlobals); + + QStringList modifiedHosts = config.groupList(); + KConfigGroup hostGroup(&config, host); + + if (uaIndex == -1) + { + if (!hostGroup.exists()) + { + kDebug() << "Host does NOT exists!"; + return false; + } + hostGroup.deleteGroup(); + KProtocolManager::reparseConfiguration(); + return true; + } + + hostGroup.writeEntry(QL1S("UserAgent"), userAgentString(uaIndex)); + + KProtocolManager::reparseConfiguration(); + return true; +} + + +int UserAgentInfo::uaIndexForHost(const QString &host) +{ + QString KDEUserAgent = KProtocolManager::userAgentForHost(host); + + int n = m_providers.count(); + for (int i = 0; i < n; ++i) + { + QString rekonqUserAgent = userAgentString(i); + if (KDEUserAgent == rekonqUserAgent) + return i; + } + return -1; +} + + +bool UserAgentInfo::providerExists(int i) +{ + KService::Ptr s = m_providers.at(i); + if (s.isNull()) + { + return false; + } + return true; +} diff --git a/src/useragent/useragentinfo.h b/src/useragent/useragentinfo.h new file mode 100644 index 00000000..3162fa82 --- /dev/null +++ b/src/useragent/useragentinfo.h @@ -0,0 +1,86 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (c) 2001 by Dawit Alemayehu +* Copyright (C) 2010-2012 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + + +#ifndef USER_AGENT_INFO_H +#define USER_AGENT_INFO_H + + +// Rekonq Includes +#include "rekonq_defines.h" + +// KDE Includes +#include + +// Qt Includes +#include + + +class UserAgentInfo +{ +public: + UserAgentInfo(); + + /** + * Lists all available providers + * + */ + KService::List availableProviders() const; + + /** + * Lists all available User Agents + * + * @returns the list of the UA descriptions + */ + QStringList availableUserAgents(); + + /** + * Set User Agent for host + * + * @param uaIndex the index of the UA description. @see availableUserAgents() + * @param host the host to se the UA + */ + bool setUserAgentForHost(int uaIndex, const QString &host); + + /** + * @returns the index of the UA set for the @p host + */ + int uaIndexForHost(const QString &); + +private: + QString userAgentString(int); + QString userAgentName(int); + QString userAgentVersion(int); + QString userAgentDescription(int); + + bool providerExists(int); + +private: + KService::List m_providers; +}; + +#endif // USER_AGENT_INFO_H diff --git a/src/useragent/useragentmanager.cpp b/src/useragent/useragentmanager.cpp new file mode 100644 index 00000000..d66fce36 --- /dev/null +++ b/src/useragent/useragentmanager.cpp @@ -0,0 +1,190 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011-2012 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + +// Self Includes +#include "useragentmanager.h" +#include "useragentmanager.moc" + +// Local Includes +#include "useragentinfo.h" +#include "useragentwidget.h" + +#include "webwindow.h" +#include "webpage.h" + +// KDE Includes +#include +#include +#include + + +QWeakPointer UserAgentManager::s_userAgentManager; + + +UserAgentManager *UserAgentManager::self() +{ + if (s_userAgentManager.isNull()) + { + s_userAgentManager = new UserAgentManager(qApp); + } + return s_userAgentManager.data(); +} + + +// ---------------------------------------------------------------------------------------------- + + +UserAgentManager::UserAgentManager(QObject *parent) + : QObject(parent) + , m_uaSettingsAction(0) +{ + m_uaSettingsAction = new KAction(KIcon("preferences-web-browser-identification"), i18n("Browser Identification"), this); + connect(m_uaSettingsAction, SIGNAL(triggered(bool)), this, SLOT(showSettings())); +} + + +void UserAgentManager::showSettings() +{ + QPointer dialog = new KDialog(m_uaTab.data()); + dialog->setCaption(i18nc("@title:window", "User Agent Settings")); + dialog->setButtons(KDialog::Ok); + + UserAgentWidget widget; + dialog->setMainWidget(&widget); + dialog->exec(); + + dialog->deleteLater(); +} + + +void UserAgentManager::populateUAMenuForTabUrl(KMenu *uaMenu, WebWindow *uaTab) +{ + if (!m_uaTab.isNull()) + { + m_uaTab.clear(); + } + + m_uaTab = uaTab; + + bool defaultUA = true; + + QAction *a, *defaultAction; + + // just to be sure... + uaMenu->clear(); + + defaultAction = new QAction(i18nc("Default rekonq user agent", "Default"), uaMenu); + defaultAction->setData(-1); + defaultAction->setCheckable(true); + connect(defaultAction, SIGNAL(triggered(bool)), this, SLOT(setUserAgent())); + + uaMenu->addAction(defaultAction); + uaMenu->addSeparator(); + + // Main Browsers Menus + KMenu *ffMenu = new KMenu(i18n("Firefox"), uaMenu); + uaMenu->addMenu(ffMenu); + + KMenu *ieMenu = new KMenu(i18n("Internet Explorer"), uaMenu); + uaMenu->addMenu(ieMenu); + + KMenu *nsMenu = new KMenu(i18n("Netscape"), uaMenu); + uaMenu->addMenu(nsMenu); + + KMenu *opMenu = new KMenu(i18n("Opera"), uaMenu); + uaMenu->addMenu(opMenu); + + KMenu *sfMenu = new KMenu(i18n("Safari"), uaMenu); + uaMenu->addMenu(sfMenu); + + KMenu *otMenu = new KMenu(i18n("Other"), uaMenu); + uaMenu->addMenu(otMenu); + + UserAgentInfo uaInfo; + QStringList UAlist = uaInfo.availableUserAgents(); + const KService::List providers = uaInfo.availableProviders(); + int uaIndex = uaInfo.uaIndexForHost(m_uaTab.data()->url().host()); + + for (int i = 0; i < UAlist.count(); ++i) + { + QString uaDesc = UAlist.at(i); + + a = new QAction(uaDesc, uaMenu); + a->setData(i); + a->setCheckable(true); + connect(a, SIGNAL(triggered(bool)), this, SLOT(setUserAgent())); + + if (i == uaIndex) + { + a->setChecked(true); + defaultUA = false; + } + + QString tag = providers.at(i)->property("X-KDE-UA-TAG").toString(); + if (tag == QL1S("FF")) + { + ffMenu->addAction(a); + } + else if (tag == QL1S("IE")) + { + ieMenu->addAction(a); + } + else if (tag == QL1S("NN")) + { + nsMenu->addAction(a); + } + else if (tag == QL1S("OPR")) + { + opMenu->addAction(a); + } + else if (tag == QL1S("SAF")) + { + sfMenu->addAction(a); + } + else // OTHERs + { + otMenu->addAction(a); + } + } + defaultAction->setChecked(defaultUA); + + uaMenu->addSeparator(); + uaMenu->addAction(m_uaSettingsAction); +} + + +void UserAgentManager::setUserAgent() +{ + QAction *sender = static_cast(QObject::sender()); + + int uaIndex = sender->data().toInt(); + + UserAgentInfo uaInfo; + uaInfo.setUserAgentForHost(uaIndex, m_uaTab.data()->url().host()); + + // reload tab + m_uaTab.data()->page()->triggerAction(QWebPage::Reload); +} diff --git a/src/useragent/useragentmanager.h b/src/useragent/useragentmanager.h new file mode 100644 index 00000000..9b906a53 --- /dev/null +++ b/src/useragent/useragentmanager.h @@ -0,0 +1,70 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011-2012 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + +#ifndef USER_AGENT_MANAGER_H +#define USER_AGENT_MANAGER_H + + +// Qt Includes +#include +#include + +// Forward Declarations +class WebWindow; + +class KAction; +class KMenu; + + +class UserAgentManager : public QObject +{ + Q_OBJECT + +public: + /** + * Entry point. + * Access to UserAgentManager class by using + * UserAgentManager::self()->thePublicMethodYouNeed() + */ + static UserAgentManager *self(); + + void populateUAMenuForTabUrl(KMenu *, WebWindow *); + +private: + UserAgentManager(QObject *parent = 0); + +private Q_SLOTS: + void showSettings(); + void setUserAgent(); + +private: + KAction *m_uaSettingsAction; + QWeakPointer m_uaTab; + + static QWeakPointer s_userAgentManager; +}; + +#endif // USER_AGENT_MANAGER_H diff --git a/src/useragent/useragentsettings.ui b/src/useragent/useragentsettings.ui new file mode 100644 index 00000000..e2cd5454 --- /dev/null +++ b/src/useragent/useragentsettings.ui @@ -0,0 +1,69 @@ + + + UserAgent + + + + 0 + 0 + 609 + 496 + + + + + + + false + + + true + + + + Host + + + + + Identification + + + + + + + + + + Delete + + + + + + + Delete All + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + diff --git a/src/useragent/useragentwidget.cpp b/src/useragent/useragentwidget.cpp new file mode 100644 index 00000000..92ba3ce4 --- /dev/null +++ b/src/useragent/useragentwidget.cpp @@ -0,0 +1,96 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2010-2011 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + +// Self Includes +#include "useragentwidget.h" +#include "useragentwidget.moc" + +// KDE Includes +#include + + +UserAgentWidget::UserAgentWidget(QWidget *parent) + : QWidget(parent) +{ + setupUi(this); + + connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteUserAgent())); + connect(deleteAllButton, SIGNAL(clicked()), this, SLOT(deleteAll())); + + KConfig config("kio_httprc", KConfig::NoGlobals); + + QStringList hosts = config.groupList(); + Q_FOREACH(const QString & host, hosts) + { + QStringList tmp; + tmp << host; + + KConfigGroup hostGroup(&config, host); + tmp << hostGroup.readEntry(QL1S("UserAgent"), QString()); + + QTreeWidgetItem *item = new QTreeWidgetItem(sitePolicyTreeWidget, tmp); + sitePolicyTreeWidget->addTopLevelItem(item); + } +} + + +void UserAgentWidget::deleteUserAgent() +{ + QTreeWidgetItem *item = sitePolicyTreeWidget->currentItem(); + if (!item) + return; + + sitePolicyTreeWidget->takeTopLevelItem(sitePolicyTreeWidget->indexOfTopLevelItem(item)); + + QString host = item->text(0); + + KConfig config("kio_httprc", KConfig::NoGlobals); + KConfigGroup group(&config, host); + if (group.exists()) + { + group.deleteGroup(); + KProtocolManager::reparseConfiguration(); + } +} + + +void UserAgentWidget::deleteAll() +{ + sitePolicyTreeWidget->clear(); + + KConfig config("kio_httprc", KConfig::NoGlobals); + + QStringList list = config.groupList(); + Q_FOREACH(const QString & groupName, list) + { + KConfigGroup group(&config, groupName); + group.deleteGroup(); + } + KConfigGroup group(&config, QString()); + group.deleteGroup(); + + KProtocolManager::reparseConfiguration(); +} diff --git a/src/useragent/useragentwidget.h b/src/useragent/useragentwidget.h new file mode 100644 index 00000000..1c850d9e --- /dev/null +++ b/src/useragent/useragentwidget.h @@ -0,0 +1,53 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2010-2011 by Andrea Diamantini +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* ============================================================ */ + + +#ifndef USER_AGENT_WIDGET_H +#define USER_AGENT_WIDGET_H + + +// Rekonq Includes +#include "rekonq_defines.h" + +// Qt Includes +#include + +// Ui Includes +#include "ui_useragentsettings.h" + + +class UserAgentWidget : public QWidget, private Ui::UserAgent +{ + Q_OBJECT + +public: + UserAgentWidget(QWidget *parent = 0); + +private Q_SLOTS: + void deleteUserAgent(); + void deleteAll(); +}; + +#endif -- cgit v1.2.1