2012-08-17 20:56:01 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2012-08-17 20:56:01 +02:00
|
|
|
*
|
|
|
|
* 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2012-08-16 23:15:31 +02:00
|
|
|
#include "useragentmanager.h"
|
2014-02-19 22:07:21 +01:00
|
|
|
#include "browserwindow.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2012-08-16 23:15:31 +02:00
|
|
|
#include "settings.h"
|
|
|
|
|
2015-01-27 11:01:52 +01:00
|
|
|
#include <QWebEnginePage> // QTWEBKIT_VERSION_CHECK macro
|
2013-01-09 19:12:42 +01:00
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
UserAgentManager::UserAgentManager(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_usePerDomainUserAgent(false)
|
2012-08-16 23:15:31 +02:00
|
|
|
{
|
2015-02-08 11:45:15 +01:00
|
|
|
m_fakeUserAgent = QString("Mozilla/5.0 (%1) AppleWebKit/%2 (KHTML, like Gecko) Chrome/37.0 Safari/537.36").arg(QzTools::operatingSystem());
|
2012-08-16 23:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserAgentManager::loadSettings()
|
|
|
|
{
|
|
|
|
Settings settings;
|
|
|
|
settings.beginGroup("Web-Browser-Settings");
|
|
|
|
m_globalUserAgent = settings.value("UserAgent", QString()).toString();
|
|
|
|
settings.endGroup();
|
|
|
|
|
2012-08-17 20:56:01 +02:00
|
|
|
settings.beginGroup("User-Agent-Settings");
|
2012-08-16 23:15:31 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-17 20:56:01 +02:00
|
|
|
QString UserAgentManager::userAgentForUrl(const QUrl &url) const
|
2012-08-16 23:15:31 +02:00
|
|
|
{
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString host = url.host();
|
2012-08-16 23:15:31 +02:00
|
|
|
|
|
|
|
if (m_usePerDomainUserAgent) {
|
|
|
|
QHashIterator<QString, QString> i(m_userAgentsList);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (host.endsWith(i.key())) {
|
|
|
|
return i.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 12:10:28 +01:00
|
|
|
#if QTWEBKIT_TO_2_3
|
2012-09-04 12:42:45 +02:00
|
|
|
if (host.contains(QLatin1String("google"))) {
|
2012-08-16 23:15:31 +02:00
|
|
|
return m_fakeUserAgent;
|
|
|
|
}
|
2012-12-22 12:20:23 +01:00
|
|
|
#endif
|
2012-08-16 23:15:31 +02:00
|
|
|
|
2013-05-18 23:37:50 +02:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// Facebook chat is not working with default user-agent
|
|
|
|
if (host.endsWith(QLatin1String("facebook.com"))) {
|
|
|
|
return "Mozilla/5.0 (Windows XP) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-16 23:15:31 +02:00
|
|
|
return m_globalUserAgent;
|
|
|
|
}
|
2012-08-17 20:56:01 +02:00
|
|
|
|
|
|
|
QString UserAgentManager::globalUserAgent() const
|
|
|
|
{
|
|
|
|
return m_globalUserAgent;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UserAgentManager::usePerDomainUserAgents() const
|
|
|
|
{
|
|
|
|
return m_usePerDomainUserAgent;
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<QString, QString> UserAgentManager::perDomainUserAgentsList() const
|
|
|
|
{
|
|
|
|
return m_userAgentsList;
|
|
|
|
}
|