Skip to content

Commit

Permalink
Add a way to modify the user agent string from within the application.
Browse files Browse the repository at this point in the history
  • Loading branch information
William Witt authored and icefox committed Feb 20, 2010
1 parent 0405e86 commit 8929862
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/browsermainwindow.cpp
Expand Up @@ -85,6 +85,7 @@
#include "tabbar.h"
#include "tabwidget.h"
#include "toolbarsearch.h"
#include "useragentmenu.h"
#include "webview.h"
#include "webviewsearch.h"

Expand Down Expand Up @@ -835,6 +836,9 @@ void BrowserMainWindow::setupMenu()
this, SLOT(showSearchDialog()));
m_toolsMenu->addAction(m_toolsSearchManagerAction);

m_toolsUserAgentMenu = new UserAgentMenu(m_toolsMenu);
m_toolsMenu->addMenu(m_toolsUserAgentMenu);

m_adBlockDialogAction = new QAction(m_toolsMenu);
connect(m_adBlockDialogAction, SIGNAL(triggered()),
AdBlockManager::instance(), SLOT(showDialog()));
Expand Down Expand Up @@ -996,6 +1000,7 @@ void BrowserMainWindow::retranslate()
m_toolsPreferencesAction->setText(tr("Options..."));
m_toolsPreferencesAction->setShortcut(tr("Ctrl+,"));
m_toolsSearchManagerAction->setText(tr("Configure Search Engines..."));
m_toolsUserAgentMenu->setTitle(tr("UserAgent Switcher"));
m_adBlockDialogAction->setText(tr("&Ad Block..."));

m_helpMenu->setTitle(tr("&Help"));
Expand Down
2 changes: 2 additions & 0 deletions src/browsermainwindow.h
Expand Up @@ -75,6 +75,7 @@ class QSplitter;
class QFrame;
class HistoryMenu;
class BookmarksMenuBarMenu;
class UserAgentMenu;

/*!
The MainWindow of the Browser Application.
Expand Down Expand Up @@ -233,6 +234,7 @@ private slots:
QAction *m_toolsEnableInspectorAction;
QAction *m_toolsPreferencesAction;
QAction *m_toolsSearchManagerAction;
UserAgentMenu *m_toolsUserAgentMenu;
QAction *m_adBlockDialogAction;

QMenu *m_helpMenu;
Expand Down
5 changes: 5 additions & 0 deletions src/data/useragents/useragents.qrc
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/useragents" >
<file>useragents.xml</file>
</qresource>
</RCC>
9 changes: 9 additions & 0 deletions src/data/useragents/useragents.xml
@@ -0,0 +1,9 @@
<useragentswitcher>
<useragent description="Internet Explorer 6" useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" appcodename="Mozilla" appname="Microsoft Internet Explorer" appversion="4.0 (compatible; MSIE 6.0; Windows NT 5.1)" platform="Win32" vendor="" vendorsub=""/>
<useragent description="Internet Explorer 7" useragent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)" appcodename="Mozilla" appname="Microsoft Internet Explorer" appversion="4.0 (compatible; MSIE 7.0; Windows NT 6.0)" platform="Win32" vendor="" vendorsub=""/>
<useragent description="Internet Explorer 8" useragent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)" appcodename="Mozilla" appname="Microsoft Internet Explorer" appversion="4.0 (compatible; MSIE 8.0; Windows NT 6.1)" platform="Win32" vendor="" vendorsub=""/>
<useragent description="Googlebot 2.1" useragent="Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" appcodename="" appname="" appversion="" platform="" vendor="" vendorsub=""/>
<useragent description="Msnbot 1.1" useragent="msnbot/1.1 (+http://search.msn.com/msnbot.htm)" appcodename="" appname="" appversion="" platform="" vendor="" vendorsub=""/>
<useragent description="Yahoo Slurp" useragent="Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)" appcodename="" appname="" appversion="" platform="" vendor="" vendorsub=""/>
<useragent description="iPhone 3.0" useragent="Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16" appcodename="Mozilla" appname="Netscape" appversion="5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16" platform="iPhone" vendor="Apple Computer, Inc." vendorsub=""/>
</useragentswitcher>
8 changes: 6 additions & 2 deletions src/src.pri
Expand Up @@ -7,7 +7,9 @@ win32 : Debug : CONFIG += console
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD

QT += webkit network
QT += webkit \
network\
xml

# Share object files for faster compiling
RCC_DIR = $$PWD/.rcc
Expand Down Expand Up @@ -97,12 +99,14 @@ include(network/network.pri)
include(opensearch/opensearch.pri)
include(qwebplugins/qwebplugins.pri)
include(utils/utils.pri)
include(useragent/useragent.pri)

RESOURCES += \
$$PWD/data/data.qrc \
$$PWD/data/graphics/graphics.qrc \
$$PWD/data/searchengines/searchengines.qrc \
$$PWD/htmls/htmls.qrc
$$PWD/htmls/htmls.qrc \
data/useragents/useragents.qrc

DISTFILES += ../AUTHORS \
../ChangeLog \
Expand Down
59 changes: 59 additions & 0 deletions src/useragent/useragent.cpp
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2010, William C. Witt
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Arora nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include "useragent.h"

UserAgent::UserAgent()
{
}

UserAgent::UserAgent(const QString &name, const QString &userAgentString)
{
this->name = name;
this->userAgentString = userAgentString;
}

QString UserAgent::getName()
{
return name;
}

void UserAgent::setName(const QString &name)
{
this->name = name;
}

QString UserAgent::getUserAgentString()
{
return userAgentString;
}

void UserAgent::setUserAgentString(const QString &userAgentString)
{
this->userAgentString = userAgentString;
}
52 changes: 52 additions & 0 deletions src/useragent/useragent.h
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2010, William C. Witt
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Arora nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#ifndef USERAGENT_H
#define USERAGENT_H

#include <qstring.h>

class UserAgent
{
public:
UserAgent();
UserAgent(const QString &name, const QString &userAgentString);

QString getName();
void setName(const QString &name);

QString getUserAgentString();
void setUserAgentString(const QString &userAgentString);


private:
QString name;
QString userAgentString;
};

#endif // USERAGENT_H
11 changes: 11 additions & 0 deletions src/useragent/useragent.pri
@@ -0,0 +1,11 @@
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD

HEADERS += \
useragent.h \
useragentmenu.h \
useragentmenuaction.h
SOURCES += \
useragent.cpp \
useragentmenu.cpp \
useragentmenuaction.cpp
82 changes: 82 additions & 0 deletions src/useragent/useragentmenu.cpp
@@ -0,0 +1,82 @@
/**
* Copyright (c) 2010, William C. Witt
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Arora nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include "useragentmenu.h"

#include "useragentmenuaction.h"
#include <QtXml>

UserAgentMenu::UserAgentMenu(QWidget *parent)
: QMenu(parent)
{
buildUserAgentMenu();
}

void UserAgentMenu::buildUserAgentMenu()
{
DefaultUA *defaultUa = new DefaultUA(this);
defaultUa->setText(tr("Default"));
connect(defaultUa, SIGNAL(triggered()), defaultUa, SLOT(onAction()));
addAction(defaultUa);

addSeparator();

QDomDocument doc(tr("useragentswitcher"));
QFile file(tr(":/useragents/useragents.xml"));
if (!doc.setContent(&file)) {
file.close();
}
file.close();
QDomElement root = doc.documentElement();
QDomNode n = root.firstChild();
while (!n.isNull()) {
QDomElement e = n.toElement();
if (!e.isNull()) {
if (e.tagName() == tr("useragent")) {
UserAgent ua(e.attribute(tr("description"), tr("")), e.attribute(tr("useragent"),tr("")));
UserAgentMenuAction *uama = new UserAgentMenuAction(this);
uama->setUserAgent(ua);
connect(uama, SIGNAL(triggered()), uama, SLOT(onAction()));
addAction(uama);
}
}
n = n.nextSibling();
}
}


DefaultUA::DefaultUA(QObject *parent)
: QAction(parent)
{
}

void DefaultUA::onAction()
{
QSettings setting;
setting.remove(QLatin1String("useragent"));
}
55 changes: 55 additions & 0 deletions src/useragent/useragentmenu.h
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2010, William C. Witt
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Arora nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#ifndef USERAGENTMENU_H
#define USERAGENTMENU_H

#include <qmenu.h>

class UserAgentMenu : public QMenu
{
Q_OBJECT

public:
UserAgentMenu(QWidget *parent = 0);
void buildUserAgentMenu();
};

class DefaultUA : public QAction
{
Q_OBJECT

public:
DefaultUA(QObject *parent = 0);

public slots:
void onAction();

};

#endif // USERAGENTMENU_H

0 comments on commit 8929862

Please sign in to comment.