File indexing completed on 2024-05-12 04:58:13

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "useragentmanager.h"
0019 #include "browserwindow.h"
0020 #include "qztools.h"
0021 #include "settings.h"
0022 #include "mainapplication.h"
0023 
0024 #include <QWebEngineProfile>
0025 #include <QRegularExpression>
0026 
0027 UserAgentManager::UserAgentManager(QObject* parent)
0028     : QObject(parent)
0029     , m_usePerDomainUserAgent(false)
0030 {
0031     m_defaultUserAgent = mApp->webProfile()->httpUserAgent();
0032     m_defaultUserAgent.replace(QRegularExpression(QSL("(QtWebEngine/[^\\s]+)")), QSL("Falkon/%1 \\1").arg(QString::fromLatin1(Qz::VERSION)));
0033 }
0034 
0035 void UserAgentManager::loadSettings()
0036 {
0037     Settings settings;
0038     settings.beginGroup(QSL("Web-Browser-Settings"));
0039     m_globalUserAgent = settings.value(QSL("UserAgent"), QString()).toString();
0040     settings.endGroup();
0041 
0042     settings.beginGroup(QSL("User-Agent-Settings"));
0043     m_usePerDomainUserAgent = settings.value(QSL("UsePerDomainUA"), false).toBool();
0044     QStringList domainList = settings.value(QSL("DomainList"), QStringList()).toStringList();
0045     QStringList userAgentsList = settings.value(QSL("UserAgentsList"), QStringList()).toStringList();
0046     settings.endGroup();
0047 
0048     m_usePerDomainUserAgent = (m_usePerDomainUserAgent && domainList.count() == userAgentsList.count());
0049 
0050     if (m_usePerDomainUserAgent) {
0051         for (int i = 0; i < domainList.count(); ++i) {
0052             m_userAgentsList[domainList.at(i)] = userAgentsList.at(i);
0053         }
0054     }
0055 
0056     const QString userAgent = m_globalUserAgent.isEmpty() ? m_defaultUserAgent : m_globalUserAgent;
0057     mApp->webProfile()->setHttpUserAgent(userAgent);
0058 }
0059 
0060 QString UserAgentManager::globalUserAgent() const
0061 {
0062     return m_globalUserAgent;
0063 }
0064 
0065 QString UserAgentManager::defaultUserAgent() const
0066 {
0067     return m_defaultUserAgent;
0068 }
0069 
0070 bool UserAgentManager::usePerDomainUserAgents() const
0071 {
0072     return m_usePerDomainUserAgent;
0073 }
0074 
0075 QHash<QString, QString> UserAgentManager::perDomainUserAgentsList() const
0076 {
0077     return m_userAgentsList;
0078 }