diff options
author | Andrea Diamantini <adjam7@gmail.com> | 2011-11-24 00:31:34 +0100 |
---|---|---|
committer | Andrea Diamantini <adjam7@gmail.com> | 2011-11-26 00:16:20 +0100 |
commit | e64eeadc4ccf6cc6b76700183714db4cab24e5fb (patch) | |
tree | 31580395ea6e6f3ab04e1819e2f379276ac25c34 /src/useragent | |
parent | Clean up webview contextual && tools menu (diff) | |
download | rekonq-e64eeadc4ccf6cc6b76700183714db4cab24e5fb.tar.xz |
Moving UA management code to a new UserAgentManager
Diffstat (limited to 'src/useragent')
-rw-r--r-- | src/useragent/useragentmanager.cpp | 138 | ||||
-rw-r--r-- | src/useragent/useragentmanager.h | 56 |
2 files changed, 194 insertions, 0 deletions
diff --git a/src/useragent/useragentmanager.cpp b/src/useragent/useragentmanager.cpp new file mode 100644 index 00000000..db6e0708 --- /dev/null +++ b/src/useragent/useragentmanager.cpp @@ -0,0 +1,138 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011 by Andrea Diamantini <adjam7 at gmail dot 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 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* 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/>. +* +* ============================================================ */ + + +// Self Includes +#include "useragentmanager.h" +#include "useragentmanager.moc" + +// Local Includes +#include "useragentinfo.h" +#include "useragentwidget.h" +#include "application.h" +#include "mainwindow.h" +#include "webtab.h" + +// KDE Includes +#include <KAction> +#include <KDialog> +#include <KMenu> + + +UserAgentManager::UserAgentManager(QObject *parent) + : QObject(parent) + , _uaSettingsAction(0) + , _uaMenu(new KMenu) +{ + _uaSettingsAction = new KAction(KIcon("preferences-web-browser-identification"), i18n("Browser Identification"), this); + connect(_uaSettingsAction, SIGNAL(triggered(bool)), this, SLOT(showSettings())); + + connect(_uaMenu, SIGNAL(aboutToShow()), this, SLOT(populateUserAgentMenu())); +} + + +UserAgentManager::~UserAgentManager() +{ + delete _uaMenu; +} + + +KMenu *UserAgentManager::userAgentMenu() +{ + return _uaMenu; +} + + +void UserAgentManager::showSettings() +{ + QPointer<KDialog> dialog = new KDialog; + dialog->setCaption(i18nc("@title:window", "User Agent Settings")); + dialog->setButtons(KDialog::Ok); + + UserAgentWidget widget; + dialog->setMainWidget(&widget); + dialog->exec(); + + dialog->deleteLater(); +} + + +void UserAgentManager::populateUserAgentMenu() +{ + bool defaultUA = true; + KUrl url = rApp->mainWindow()->currentTab()->url(); + + QAction *a, *defaultAction; + + // just to be sure... + _uaMenu->clear(); + + defaultAction = new QAction(i18nc("Default rekonq user agent", "Default"), this); + defaultAction->setData(-1); + defaultAction->setCheckable(true); + connect(defaultAction, SIGNAL(triggered(bool)), this, SLOT(setUserAgent())); + + _uaMenu->addAction(defaultAction); + _uaMenu->addSeparator(); + + UserAgentInfo uaInfo; + QStringList UAlist = uaInfo.availableUserAgents(); + int uaIndex = uaInfo.uaIndexForHost(url.host()); + + for (int i = 0; i < UAlist.count(); ++i) + { + QString uaDesc = UAlist.at(i); + + a = new QAction(uaDesc, this); + a->setData(i); + a->setCheckable(true); + connect(a, SIGNAL(triggered(bool)), this, SLOT(setUserAgent())); + + if (i == uaIndex) + { + a->setChecked(true); + defaultUA = false; + } + _uaMenu->addAction(a); + } + defaultAction->setChecked(defaultUA); + + _uaMenu->addSeparator(); + _uaMenu->addAction(_uaSettingsAction); +} + + +void UserAgentManager::setUserAgent() +{ + QAction *sender = static_cast<QAction *>(QObject::sender()); + + QString desc = sender->text(); + int uaIndex = sender->data().toInt(); + + KUrl url = rApp->mainWindow()->currentTab()->url(); + UserAgentInfo uaInfo; + uaInfo.setUserAgentForHost(uaIndex, url.host()); + rApp->mainWindow()->currentTab()->page()->triggerAction(QWebPage::Reload); +} diff --git a/src/useragent/useragentmanager.h b/src/useragent/useragentmanager.h new file mode 100644 index 00000000..ae38ac6d --- /dev/null +++ b/src/useragent/useragentmanager.h @@ -0,0 +1,56 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011 by Andrea Diamantini <adjam7 at gmail dot 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 2 of +* the License or (at your option) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* 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 USER_AGENT_MANAGER_H +#define USER_AGENT_MANAGER_H + + +#include <QObject> + +class KAction; +class KMenu; + +class UserAgentManager : public QObject +{ + Q_OBJECT + +public: + UserAgentManager(QObject *parent = 0); + ~UserAgentManager(); + + KMenu *userAgentMenu(); + +private Q_SLOTS: + void showSettings(); + void setUserAgent(); + void populateUserAgentMenu(); + +private: + KAction *_uaSettingsAction; + KMenu *_uaMenu; +}; + +#endif // USER_AGENT_MANAGER_H |