1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

Backend support for setting per-domain UserAgent.

see #399
This commit is contained in:
nowrep 2012-08-16 23:15:31 +02:00
parent 064fa19097
commit 3f5b6382a3
7 changed files with 103 additions and 27 deletions

View File

@ -47,6 +47,7 @@
#include "qzsettings.h" #include "qzsettings.h"
#include "clearprivatedata.h" #include "clearprivatedata.h"
#include "commandlineoptions.h" #include "commandlineoptions.h"
#include "useragentmanager.h"
#ifdef Q_WS_MAC #ifdef Q_WS_MAC
#include <QFileOpenEvent> #include <QFileOpenEvent>
@ -81,6 +82,7 @@ MainApplication::MainApplication(int &argc, char** argv)
, m_desktopNotifications(0) , m_desktopNotifications(0)
, m_searchEnginesManager(0) , m_searchEnginesManager(0)
, m_dbWriter(new DatabaseWriter(this)) , m_dbWriter(new DatabaseWriter(this))
, m_uaManager(new UserAgentManager)
, m_isPrivateSession(false) , m_isPrivateSession(false)
, m_isClosing(false) , m_isClosing(false)
, m_isStateChanged(false) , m_isStateChanged(false)
@ -350,7 +352,6 @@ void MainApplication::loadSettings()
setWheelScrollLines(settings.value("wheelScrollLines", wheelScrollLines()).toInt()); setWheelScrollLines(settings.value("wheelScrollLines", wheelScrollLines()).toInt());
m_websettings->setUserStyleSheetUrl(userStyleSheet(settings.value("userStyleSheet", "").toString())); m_websettings->setUserStyleSheetUrl(userStyleSheet(settings.value("userStyleSheet", "").toString()));
WebPage::setUserAgent(settings.value("UserAgent", "").toString());
settings.endGroup(); settings.endGroup();
settings.beginGroup("Browser-Fonts"); settings.beginGroup("Browser-Fonts");
@ -382,6 +383,7 @@ void MainApplication::loadSettings()
} }
qzSettings->loadSettings(); qzSettings->loadSettings();
m_uaManager->loadSettings();
} }
void MainApplication::reloadSettings() void MainApplication::reloadSettings()
@ -956,3 +958,8 @@ bool MainApplication::checkSettingsDir()
return dir.isReadable(); return dir.isReadable();
} }
MainApplication::~MainApplication()
{
delete m_uaManager;
}

View File

@ -46,6 +46,7 @@ class DesktopNotificationsFactory;
class IconProvider; class IconProvider;
class SearchEnginesManager; class SearchEnginesManager;
class DatabaseWriter; class DatabaseWriter;
class UserAgentManager;
class QT_QUPZILLA_EXPORT MainApplication : public QtSingleApplication class QT_QUPZILLA_EXPORT MainApplication : public QtSingleApplication
{ {
@ -58,6 +59,7 @@ public:
QString THEMESDIR; QString THEMESDIR;
explicit MainApplication(int &argc, char** argv); explicit MainApplication(int &argc, char** argv);
~MainApplication();
void connectDatabase(); void connectDatabase();
void loadSettings(); void loadSettings();
@ -94,7 +96,9 @@ public:
SearchEnginesManager* searchEnginesManager(); SearchEnginesManager* searchEnginesManager();
QNetworkDiskCache* networkCache(); QNetworkDiskCache* networkCache();
DesktopNotificationsFactory* desktopNotifications(); DesktopNotificationsFactory* desktopNotifications();
DatabaseWriter* dbWriter() { return m_dbWriter; } DatabaseWriter* dbWriter() { return m_dbWriter; }
UserAgentManager* uaManager() { return m_uaManager; }
#ifdef Q_WS_MAC #ifdef Q_WS_MAC
bool event(QEvent* e); bool event(QEvent* e);
@ -144,6 +148,7 @@ private:
DesktopNotificationsFactory* m_desktopNotifications; DesktopNotificationsFactory* m_desktopNotifications;
SearchEnginesManager* m_searchEnginesManager; SearchEnginesManager* m_searchEnginesManager;
DatabaseWriter* m_dbWriter; DatabaseWriter* m_dbWriter;
UserAgentManager* m_uaManager;
QList<QWeakPointer<QupZilla> > m_mainWindows; QList<QWeakPointer<QupZilla> > m_mainWindows;

View File

@ -180,7 +180,8 @@ SOURCES += \
tools/emptynetworkreply.cpp \ tools/emptynetworkreply.cpp \
3rdparty/processinfo.cpp \ 3rdparty/processinfo.cpp \
preferences/pluginsmanager.cpp \ preferences/pluginsmanager.cpp \
other/qzsettings.cpp other/qzsettings.cpp \
other/useragentmanager.cpp
HEADERS += \ HEADERS += \
webview/tabpreview.h \ webview/tabpreview.h \
@ -332,7 +333,8 @@ HEADERS += \
tools/emptynetworkreply.h \ tools/emptynetworkreply.h \
3rdparty/processinfo.h \ 3rdparty/processinfo.h \
preferences/pluginsmanager.h \ preferences/pluginsmanager.h \
other/qzsettings.h other/qzsettings.h \
other/useragentmanager.h
FORMS += \ FORMS += \
preferences/autofillmanager.ui \ preferences/autofillmanager.ui \

View File

@ -0,0 +1,52 @@
#include "useragentmanager.h"
#include "qupzilla.h"
#include "globalfunctions.h"
#include "settings.h"
UserAgentManager::UserAgentManager()
{
m_fakeUserAgent = QString("Mozilla/5.0 (%1) AppleWebKit/%2 (KHTML, like Gecko) Chrome/10.0 Safari/%2").arg(qz_buildSystem(), QupZilla::WEBKITVERSION);
}
void UserAgentManager::loadSettings()
{
Settings settings;
settings.beginGroup("Web-Browser-Settings");
m_globalUserAgent = settings.value("UserAgent", QString()).toString();
settings.endGroup();
settings.beginGroup("UserAgent-Settings");
m_usePerDomainUserAgent = settings.value("UsePerDomainUA", false).toBool();
QStringList domainList = settings.value("DomainList", QStringList()).toStringList();
QStringList userAgentsList = settings.value("UserAgentsList", QStringList()).toStringList();
settings.endGroup();
m_usePerDomainUserAgent = (m_usePerDomainUserAgent && domainList.count() == userAgentsList.count());
if (m_usePerDomainUserAgent) {
for (int i = 0; i < domainList.count(); ++i) {
m_userAgentsList[domainList.at(i)] = userAgentsList.at(i);
}
}
}
QString UserAgentManager::userAgentForUrl(const QUrl &url)
{
const QString &host = url.host();
if (m_usePerDomainUserAgent) {
QHashIterator<QString, QString> i(m_userAgentsList);
while (i.hasNext()) {
i.next();
if (host.endsWith(i.key())) {
return i.value();
}
}
}
if (host.contains("google")) {
return m_fakeUserAgent;
}
return m_globalUserAgent;
}

View File

@ -0,0 +1,28 @@
#ifndef USERAGENTMANAGER_H
#define USERAGENTMANAGER_H
#include <QHash>
#include "qz_namespace.h"
class QUrl;
class QT_QUPZILLA_EXPORT UserAgentManager
{
public:
explicit UserAgentManager();
void loadSettings();
QString userAgentForUrl(const QUrl &url);
private:
QString m_globalUserAgent;
QString m_fakeUserAgent;
bool m_usePerDomainUserAgent;
QHash<QString, QString> m_userAgentsList;
};
#endif // USERAGENTMANAGER_H

View File

@ -34,6 +34,7 @@
#include "adblockmanager.h" #include "adblockmanager.h"
#include "iconprovider.h" #include "iconprovider.h"
#include "qzsettings.h" #include "qzsettings.h"
#include "useragentmanager.h"
#ifdef NONBLOCK_JS_DIALOGS #ifdef NONBLOCK_JS_DIALOGS
#include "ui_jsconfirm.h" #include "ui_jsconfirm.h"
@ -55,8 +56,6 @@
#include <QWebFrame> #include <QWebFrame>
QString WebPage::s_lastUploadLocation = QDir::homePath(); QString WebPage::s_lastUploadLocation = QDir::homePath();
QString WebPage::s_userAgent;
QString WebPage::s_fakeUserAgent;
QUrl WebPage::s_lastUnsupportedUrl; QUrl WebPage::s_lastUnsupportedUrl;
QTime WebPage::s_lastUnsupportedUrlTime; QTime WebPage::s_lastUnsupportedUrlTime;
QList<WebPage*> WebPage::s_livingPages; QList<WebPage*> WebPage::s_livingPages;
@ -180,11 +179,6 @@ bool WebPage::isLoading() const
return m_loadProgress < 100; return m_loadProgress < 100;
} }
void WebPage::setUserAgent(const QString &agent)
{
s_userAgent = agent;
}
void WebPage::urlChanged(const QUrl &url) void WebPage::urlChanged(const QUrl &url)
{ {
Q_UNUSED(url) Q_UNUSED(url)
@ -570,27 +564,18 @@ void WebPage::cleanBlockedObjects()
QString WebPage::userAgentForUrl(const QUrl &url) const QString WebPage::userAgentForUrl(const QUrl &url) const
{ {
const QString &host = url.host(); QString userAgent = mApp->uaManager()->userAgentForUrl(url);
// Let Google services play nice with us if (userAgent.isEmpty()) {
if (host.contains("google")) { userAgent = QWebPage::userAgentForUrl(url);
if (s_fakeUserAgent.isEmpty()) {
s_fakeUserAgent = QString("Mozilla/5.0 (%1) AppleWebKit/%2 (KHTML, like Gecko) Chrome/10.0 Safari/%2").arg(qz_buildSystem(), QupZilla::WEBKITVERSION);
}
return s_fakeUserAgent;
}
if (s_userAgent.isEmpty()) {
s_userAgent = QWebPage::userAgentForUrl(url);
#ifdef Q_WS_MAC #ifdef Q_WS_MAC
#ifdef __i386__ || __x86_64__ #ifdef __i386__ || __x86_64__
m_userAgent.replace("PPC Mac OS X", "Intel Mac OS X"); userAgent.replace("PPC Mac OS X", "Intel Mac OS X");
#endif #endif
#endif #endif
} }
return s_userAgent; return userAgent;
} }
bool WebPage::supportsExtension(Extension extension) const bool WebPage::supportsExtension(Extension extension) const

View File

@ -74,7 +74,6 @@ public:
void addRejectedCerts(const QList<QSslCertificate> &certs); void addRejectedCerts(const QList<QSslCertificate> &certs);
bool containsRejectedCerts(const QList<QSslCertificate> &certs); bool containsRejectedCerts(const QList<QSslCertificate> &certs);
static void setUserAgent(const QString &agent);
QString userAgentForUrl(const QUrl &url) const; QString userAgentForUrl(const QUrl &url) const;
static bool isPointerSafeToUse(WebPage* page); static bool isPointerSafeToUse(WebPage* page);
@ -118,8 +117,6 @@ private:
void desktopServicesOpen(const QUrl &url); void desktopServicesOpen(const QUrl &url);
static QString s_lastUploadLocation; static QString s_lastUploadLocation;
static QString s_userAgent;
static QString s_fakeUserAgent;
static QUrl s_lastUnsupportedUrl; static QUrl s_lastUnsupportedUrl;
static QTime s_lastUnsupportedUrlTime; static QTime s_lastUnsupportedUrlTime;
static QList<WebPage*> s_livingPages; static QList<WebPage*> s_livingPages;