1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

New User Agent Manager allows setting per-site User Agents.

closes #399
This commit is contained in:
nowrep 2012-08-17 20:56:01 +02:00
parent a6564b5eea
commit e517b9ec65
7 changed files with 582 additions and 31 deletions

View File

@ -181,7 +181,8 @@ SOURCES += \
3rdparty/processinfo.cpp \ 3rdparty/processinfo.cpp \
preferences/pluginsmanager.cpp \ preferences/pluginsmanager.cpp \
other/qzsettings.cpp \ other/qzsettings.cpp \
other/useragentmanager.cpp other/useragentmanager.cpp \
preferences/useragentdialog.cpp
HEADERS += \ HEADERS += \
webview/tabpreview.h \ webview/tabpreview.h \
@ -334,7 +335,8 @@ HEADERS += \
3rdparty/processinfo.h \ 3rdparty/processinfo.h \
preferences/pluginsmanager.h \ preferences/pluginsmanager.h \
other/qzsettings.h \ other/qzsettings.h \
other/useragentmanager.h other/useragentmanager.h \
preferences/useragentdialog.h
FORMS += \ FORMS += \
preferences/autofillmanager.ui \ preferences/autofillmanager.ui \
@ -377,7 +379,8 @@ FORMS += \
bookmarksimport/bookmarksimportdialog.ui \ bookmarksimport/bookmarksimportdialog.ui \
other/checkboxdialog.ui \ other/checkboxdialog.ui \
other/iconchooser.ui \ other/iconchooser.ui \
adblock/adblockaddsubscriptiondialog.ui adblock/adblockaddsubscriptiondialog.ui \
preferences/useragentdialog.ui
RESOURCES += \ RESOURCES += \
data/icons.qrc \ data/icons.qrc \

View File

@ -1,3 +1,20 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
*
* 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/>.
* ============================================================ */
#include "useragentmanager.h" #include "useragentmanager.h"
#include "qupzilla.h" #include "qupzilla.h"
#include "globalfunctions.h" #include "globalfunctions.h"
@ -15,7 +32,7 @@ void UserAgentManager::loadSettings()
m_globalUserAgent = settings.value("UserAgent", QString()).toString(); m_globalUserAgent = settings.value("UserAgent", QString()).toString();
settings.endGroup(); settings.endGroup();
settings.beginGroup("UserAgent-Settings"); settings.beginGroup("User-Agent-Settings");
m_usePerDomainUserAgent = settings.value("UsePerDomainUA", false).toBool(); m_usePerDomainUserAgent = settings.value("UsePerDomainUA", false).toBool();
QStringList domainList = settings.value("DomainList", QStringList()).toStringList(); QStringList domainList = settings.value("DomainList", QStringList()).toStringList();
QStringList userAgentsList = settings.value("UserAgentsList", QStringList()).toStringList(); QStringList userAgentsList = settings.value("UserAgentsList", QStringList()).toStringList();
@ -30,7 +47,7 @@ void UserAgentManager::loadSettings()
} }
} }
QString UserAgentManager::userAgentForUrl(const QUrl &url) QString UserAgentManager::userAgentForUrl(const QUrl &url) const
{ {
const QString &host = url.host(); const QString &host = url.host();
@ -50,3 +67,18 @@ QString UserAgentManager::userAgentForUrl(const QUrl &url)
return m_globalUserAgent; return m_globalUserAgent;
} }
QString UserAgentManager::globalUserAgent() const
{
return m_globalUserAgent;
}
bool UserAgentManager::usePerDomainUserAgents() const
{
return m_usePerDomainUserAgent;
}
QHash<QString, QString> UserAgentManager::perDomainUserAgentsList() const
{
return m_userAgentsList;
}

View File

@ -1,3 +1,20 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
*
* 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/>.
* ============================================================ */
#ifndef USERAGENTMANAGER_H #ifndef USERAGENTMANAGER_H
#define USERAGENTMANAGER_H #define USERAGENTMANAGER_H
@ -14,7 +31,11 @@ public:
void loadSettings(); void loadSettings();
QString userAgentForUrl(const QUrl &url); QString userAgentForUrl(const QUrl &url) const;
QString globalUserAgent() const;
bool usePerDomainUserAgents() const;
QHash<QString, QString> perDomainUserAgentsList() const;
private: private:
QString m_globalUserAgent; QString m_globalUserAgent;
@ -22,7 +43,6 @@ private:
bool m_usePerDomainUserAgent; bool m_usePerDomainUserAgent;
QHash<QString, QString> m_userAgentsList; QHash<QString, QString> m_userAgentsList;
}; };
#endif // USERAGENTMANAGER_H #endif // USERAGENTMANAGER_H

View File

@ -2170,24 +2170,24 @@
</widget> </widget>
<widget class="QWidget" name="stackedWidgetPage7"> <widget class="QWidget" name="stackedWidgetPage7">
<layout class="QGridLayout" name="gridLayout_13"> <layout class="QGridLayout" name="gridLayout_13">
<item row="7" column="0" colspan="4"> <item row="9" column="0" colspan="4">
<widget class="QLabel" name="label_9"> <widget class="QLabel" name="label_9">
<property name="text"> <property name="text">
<string>&lt;b&gt;Other&lt;/b&gt;</string> <string>&lt;b&gt;Other&lt;/b&gt;</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="8" column="0" colspan="4"> <item row="10" column="0" colspan="4">
<widget class="QLabel" name="label_15"> <widget class="QLabel" name="label_15">
<property name="text"> <property name="text">
<string>StyleSheet automatically loaded with all websites: </string> <string>StyleSheet automatically loaded with all websites: </string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="9" column="1"> <item row="11" column="1">
<widget class="QLineEdit" name="userStyleSheet"/> <widget class="QLineEdit" name="userStyleSheet"/>
</item> </item>
<item row="9" column="2"> <item row="11" column="2">
<widget class="QToolButton" name="chooseUserStylesheet"> <widget class="QToolButton" name="chooseUserStylesheet">
<property name="text"> <property name="text">
<string>...</string> <string>...</string>
@ -2225,26 +2225,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="10" column="0" colspan="2">
<widget class="QCheckBox" name="changeUserAgent">
<property name="text">
<string>Change browser identification:</string>
</property>
</widget>
</item>
<item row="11" column="1" colspan="2">
<widget class="QComboBox" name="userAgentCombo">
<property name="maximumSize">
<size>
<width>330</width>
<height>16777215</height>
</size>
</property>
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="12" column="0"> <item row="12" column="0">
<spacer name="verticalSpacer_15"> <spacer name="verticalSpacer_15">
<property name="orientation"> <property name="orientation">
@ -2287,6 +2267,53 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="7" column="0" colspan="4">
<widget class="QLabel" name="label_60">
<property name="text">
<string>&lt;b&gt;Change browser identification&lt;/b&gt;</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_17">
<item>
<spacer name="horizontalSpacer_26">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="uaManager">
<property name="text">
<string>User Agent Manager</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_25">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>

View File

@ -0,0 +1,223 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
*
* 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/>.
* ============================================================ */
#include "useragentdialog.h"
#include "ui_useragentdialog.h"
#include "useragentmanager.h"
#include "globalfunctions.h"
#include "mainapplication.h"
#include "settings.h"
#include <QFormLayout>
#include <QLineEdit>
#include <QLabel>
UserAgentDialog::UserAgentDialog(QWidget* parent)
: QDialog(parent)
, ui(new Ui::UserAgentDialog)
, m_manager(mApp->uaManager())
{
ui->setupUi(this);
const QString &os = qz_buildSystem();
m_knownUserAgents << QString("Opera/9.80 (%1) Presto/2.10.229 Version/11.61").arg(os)
<< QString("Mozilla/5.0 (%1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7").arg(os)
<< QString("Mozilla/5.0 (%1) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1").arg(os)
<< QString("Mozilla/5.0 (%1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1").arg(os);
ui->globalComboBox->addItems(m_knownUserAgents);
const QString &globalUserAgent = m_manager->globalUserAgent();
ui->changeGlobal->setChecked(!globalUserAgent.isEmpty());
ui->globalComboBox->lineEdit()->setText(globalUserAgent);
ui->globalComboBox->lineEdit()->setCursorPosition(0);
ui->changePerSite->setChecked(m_manager->usePerDomainUserAgents());
QHashIterator<QString, QString> i(m_manager->perDomainUserAgentsList());
while (i.hasNext()) {
i.next();
QTableWidgetItem* siteItem = new QTableWidgetItem(i.key());
QTableWidgetItem* userAgentItem = new QTableWidgetItem(i.value());
int row = ui->table->rowCount();
ui->table->insertRow(row);
ui->table->setItem(row, 0, siteItem);
ui->table->setItem(row, 1, userAgentItem);
}
connect(ui->add, SIGNAL(clicked()), this, SLOT(addSite()));
connect(ui->remove, SIGNAL(clicked()), this, SLOT(removeSite()));
connect(ui->edit, SIGNAL(clicked()), this, SLOT(editSite()));
connect(ui->table, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(editSite()));
connect(ui->changeGlobal, SIGNAL(clicked(bool)), this, SLOT(enableGlobalComboBox(bool)));
connect(ui->changePerSite, SIGNAL(clicked(bool)), this, SLOT(enablePerSiteFrame(bool)));
enableGlobalComboBox(ui->changeGlobal->isChecked());
enablePerSiteFrame(ui->changePerSite->isChecked());
}
void UserAgentDialog::addSite()
{
QString site;
QString userAgent;
if (showEditDialog(tr("Add new site"), &site, &userAgent)) {
QTableWidgetItem* siteItem = new QTableWidgetItem(site);
QTableWidgetItem* userAgentItem = new QTableWidgetItem(userAgent);
int row = ui->table->rowCount();
ui->table->insertRow(row);
ui->table->setItem(row, 0, siteItem);
ui->table->setItem(row, 1, userAgentItem);
}
}
void UserAgentDialog::removeSite()
{
int row = ui->table->currentRow();
QTableWidgetItem* siteItem = ui->table->item(row, 0);
QTableWidgetItem* userAgentItem = ui->table->item(row, 1);
if (siteItem && userAgentItem) {
delete siteItem;
delete userAgentItem;
ui->table->removeRow(row);
}
}
void UserAgentDialog::editSite()
{
int row = ui->table->currentRow();
QTableWidgetItem* siteItem = ui->table->item(row, 0);
QTableWidgetItem* userAgentItem = ui->table->item(row, 1);
if (siteItem && userAgentItem) {
QString site = siteItem->text();
QString userAgent = userAgentItem->text();
if (showEditDialog(tr("Edit site"), &site, &userAgent)) {
siteItem->setText(site);
userAgentItem->setText(userAgent);
}
}
}
void UserAgentDialog::accept()
{
QString globalUserAgent = ui->changeGlobal->isChecked() ? ui->globalComboBox->currentText() : QString();
QStringList domainList;
QStringList userAgentsList;
for (int i = 0; i < ui->table->rowCount(); ++i) {
QTableWidgetItem* siteItem = ui->table->item(i, 0);
QTableWidgetItem* userAgentItem = ui->table->item(i, 1);
if (!siteItem || !userAgentItem) {
continue;
}
QString domain = siteItem->text().trimmed();
QString userAgent = userAgentItem->text().trimmed();
if (domain.isEmpty() || userAgent.isEmpty()) {
continue;
}
domainList.append(domain);
userAgentsList.append(userAgent);
}
Settings settings;
settings.beginGroup("Web-Browser-Settings");
settings.setValue("UserAgent", globalUserAgent);
settings.endGroup();
settings.beginGroup("User-Agent-Settings");
settings.setValue("UsePerDomainUA", ui->changePerSite->isChecked());
settings.setValue("DomainList", domainList);
settings.setValue("UserAgentsList", userAgentsList);
settings.endGroup();
m_manager->loadSettings();
close();
}
void UserAgentDialog::enableGlobalComboBox(bool enable)
{
ui->globalComboBox->setEnabled(enable);
}
void UserAgentDialog::enablePerSiteFrame(bool enable)
{
ui->perSiteFrame->setEnabled(enable);
}
bool UserAgentDialog::showEditDialog(const QString &title, QString* rSite, QString* rUserAgent)
{
QDialog* dialog = new QDialog(this);
QFormLayout* layout = new QFormLayout(dialog);
QLineEdit* editSite = new QLineEdit(dialog);
QComboBox* editAgent = new QComboBox(dialog);
editAgent->setEditable(true);
editAgent->addItems(m_knownUserAgents);
editAgent->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
QDialogButtonBox* box = new QDialogButtonBox(dialog);
box->addButton(QDialogButtonBox::Ok);
box->addButton(QDialogButtonBox::Cancel);
connect(box, SIGNAL(rejected()), dialog, SLOT(reject()));
connect(box, SIGNAL(accepted()), dialog, SLOT(accept()));
layout->addRow(new QLabel(tr("Site domain: ")), editSite);
layout->addRow(new QLabel(tr("User Agent: ")), editAgent);
layout->addRow(box);
if (rSite && rUserAgent) {
editSite->setText(*rSite);
editAgent->lineEdit()->setText(*rUserAgent);
editSite->setFocus();
editAgent->lineEdit()->setCursorPosition(0);
}
dialog->setWindowTitle(title);
dialog->setMinimumSize(550, 100);
if (dialog->exec()) {
*rSite = editSite->text();
*rUserAgent = editAgent->currentText();
return !rSite->isEmpty() && !rUserAgent->isEmpty();
}
return false;
}
UserAgentDialog::~UserAgentDialog()
{
delete ui;
}

View File

@ -0,0 +1,60 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
*
* 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/>.
* ============================================================ */
#ifndef USERAGENTDIALOG_H
#define USERAGENTDIALOG_H
#include <QDialog>
#include <QStringList>
#include "qz_namespace.h"
class UserAgentManager;
namespace Ui
{
class UserAgentDialog;
}
class QT_QUPZILLA_EXPORT UserAgentDialog : public QDialog
{
Q_OBJECT
public:
explicit UserAgentDialog(QWidget* parent = 0);
~UserAgentDialog();
private slots:
void addSite();
void removeSite();
void editSite();
void accept();
void enableGlobalComboBox(bool enable);
void enablePerSiteFrame(bool enable);
private:
bool showEditDialog(const QString &title, QString* rSite, QString* rUserAgent);
Ui::UserAgentDialog* ui;
UserAgentManager* m_manager;
QStringList m_knownUserAgents;
};
#endif // USERAGENTDIALOG_H

View File

@ -0,0 +1,186 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UserAgentDialog</class>
<widget class="QDialog" name="UserAgentDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>504</width>
<height>377</height>
</rect>
</property>
<property name="windowTitle">
<string>User Agent Manager</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="changeGlobal">
<property name="text">
<string>Change global User Agent</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="globalComboBox">
<property name="editable">
<bool>true</bool>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToMinimumContentsLength</enum>
</property>
<property name="frame">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line"/>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="changePerSite">
<property name="text">
<string>Use different User Agents for specified sites</string>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="perSiteFrame">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="topMargin">
<number>4</number>
</property>
<item>
<widget class="QTableWidget" name="table">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="showGrid">
<bool>false</bool>
</property>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderDefaultSectionSize">
<number>25</number>
</attribute>
<column>
<property name="text">
<string>Site</string>
</property>
</column>
<column>
<property name="text">
<string>User Agent</string>
</property>
</column>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="add">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="remove">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="edit">
<property name="text">
<string>Edit</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>UserAgentDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>UserAgentDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>