From c982813acb084f026f40035e584d0ef7c3247071 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sat, 23 Jan 2016 15:12:13 +0300 Subject: [PATCH] Create basic cookies management dialog --- src/base/net/downloadmanager.cpp | 13 +++ src/base/net/downloadmanager.h | 2 + src/gui/cookiesdialog.cpp | 89 +++++++++++++++ src/gui/cookiesdialog.h | 61 +++++++++++ src/gui/cookiesdialog.ui | 179 +++++++++++++++++++++++++++++++ src/gui/gui.pri | 9 +- src/gui/mainwindow.cpp | 9 ++ src/gui/mainwindow.h | 1 + src/gui/mainwindow.ui | 9 ++ 9 files changed, 369 insertions(+), 3 deletions(-) create mode 100644 src/gui/cookiesdialog.cpp create mode 100644 src/gui/cookiesdialog.h create mode 100644 src/gui/cookiesdialog.ui diff --git a/src/base/net/downloadmanager.cpp b/src/base/net/downloadmanager.cpp index 408733394..28c6b40c4 100644 --- a/src/base/net/downloadmanager.cpp +++ b/src/base/net/downloadmanager.cpp @@ -75,6 +75,9 @@ namespace Preferences::instance()->setNetworkCookies(cookies); } + using QNetworkCookieJar::allCookies; + using QNetworkCookieJar::setAllCookies; + #ifndef QBT_USES_QT5 virtual bool deleteCookie(const QNetworkCookie &cookie) { @@ -188,6 +191,16 @@ bool DownloadManager::setCookiesFromUrl(const QList &cookieList, return m_networkManager.cookieJar()->setCookiesFromUrl(cookieList, url); } +QList DownloadManager::allCookies() const +{ + return static_cast(m_networkManager.cookieJar())->allCookies(); +} + +void DownloadManager::setAllCookies(const QList &cookieList) +{ + static_cast(m_networkManager.cookieJar())->setAllCookies(cookieList); +} + bool DownloadManager::deleteCookie(const QNetworkCookie &cookie) { return static_cast(m_networkManager.cookieJar())->deleteCookie(cookie); diff --git a/src/base/net/downloadmanager.h b/src/base/net/downloadmanager.h index 9cd34151a..35acc4777 100644 --- a/src/base/net/downloadmanager.h +++ b/src/base/net/downloadmanager.h @@ -54,6 +54,8 @@ namespace Net DownloadHandler *downloadUrl(const QString &url, bool saveToFile = false, qint64 limit = 0, bool handleRedirectToMagnet = false, const QString &userAgent = ""); QList cookiesForUrl(const QUrl &url) const; bool setCookiesFromUrl(const QList &cookieList, const QUrl &url); + QList allCookies() const; + void setAllCookies(const QList &cookieList); bool deleteCookie(const QNetworkCookie &cookie); private slots: diff --git a/src/gui/cookiesdialog.cpp b/src/gui/cookiesdialog.cpp new file mode 100644 index 000000000..1a1c03156 --- /dev/null +++ b/src/gui/cookiesdialog.cpp @@ -0,0 +1,89 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2016 Vladimir Golovnev + * + * 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) 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#include "cookiesdialog.h" + +#include "base/settingsstorage.h" +#include "base/net/downloadmanager.h" +#include "guiiconprovider.h" +#include "cookiesmodel.h" +#include "ui_cookiesdialog.h" + +#define SETTINGS_KEY(name) "CookiesDialog/" name +const QString KEY_GEOMETRY = SETTINGS_KEY("Geometry"); +const QString KEY_COOKIESVIEWSTATE = SETTINGS_KEY("CookiesViewState"); + +CookiesDialog::CookiesDialog(QWidget *parent) + : QDialog(parent) + , m_ui(new Ui::CookiesDialog) + , m_cookiesModel(new CookiesModel(Net::DownloadManager::instance()->allCookies(), this)) +{ + m_ui->setupUi(this); + + setWindowIcon(GuiIconProvider::instance()->getIcon("preferences-web-browser-cookies")); + m_ui->buttonAdd->setIcon(GuiIconProvider::instance()->getIcon("list-add")); + m_ui->buttonDelete->setIcon(GuiIconProvider::instance()->getIcon("list-remove")); + + m_ui->treeView->setModel(m_cookiesModel); + if (m_cookiesModel->rowCount() > 0) + m_ui->treeView->selectionModel()->setCurrentIndex( + m_cookiesModel->index(0, 0), + QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + + restoreGeometry(SettingsStorage::instance()->loadValue(KEY_GEOMETRY).toByteArray()); + m_ui->treeView->header()->restoreState( + SettingsStorage::instance()->loadValue(KEY_COOKIESVIEWSTATE).toByteArray()); +} + +CookiesDialog::~CookiesDialog() +{ + SettingsStorage::instance()->storeValue(KEY_GEOMETRY, saveGeometry()); + SettingsStorage::instance()->storeValue( + KEY_COOKIESVIEWSTATE, m_ui->treeView->header()->saveState()); + delete m_ui; +} + +void CookiesDialog::accept() +{ + Net::DownloadManager::instance()->setAllCookies(m_cookiesModel->cookies()); + QDialog::accept(); +} + +void CookiesDialog::onButtonAddClicked() +{ + int row = m_ui->treeView->selectionModel()->currentIndex().row() + 1; + + m_cookiesModel->insertRow(row); + m_ui->treeView->selectionModel()->setCurrentIndex( + m_cookiesModel->index(row, 0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); +} + +void CookiesDialog::onButtonDeleteClicked() +{ + m_cookiesModel->removeRow(m_ui->treeView->selectionModel()->currentIndex().row()); +} diff --git a/src/gui/cookiesdialog.h b/src/gui/cookiesdialog.h new file mode 100644 index 000000000..39cdf77cf --- /dev/null +++ b/src/gui/cookiesdialog.h @@ -0,0 +1,61 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2016 Vladimir Golovnev + * + * 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) 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#ifndef COOKIESDIALOG_H +#define COOKIESDIALOG_H + +#include + +namespace Ui +{ + class CookiesDialog; +} + +class CookiesModel; + +class CookiesDialog : public QDialog +{ + Q_OBJECT + +public: + explicit CookiesDialog(QWidget *parent = 0); + ~CookiesDialog(); + +public slots: + void accept() override; + +private slots: + void onButtonAddClicked(); + void onButtonDeleteClicked(); + +private: + Ui::CookiesDialog *m_ui; + CookiesModel *m_cookiesModel; +}; + +#endif // COOKIESDIALOG_H diff --git a/src/gui/cookiesdialog.ui b/src/gui/cookiesdialog.ui new file mode 100644 index 000000000..631cbc999 --- /dev/null +++ b/src/gui/cookiesdialog.ui @@ -0,0 +1,179 @@ + + + CookiesDialog + + + + 0 + 0 + 618 + 369 + + + + Manage Cookies + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + 20 + 20 + + + + + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 5 + + + + + + + + + + + + 20 + 20 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + CookiesDialog + accept() + + + 257 + 406 + + + 157 + 274 + + + + + buttonBox + rejected() + CookiesDialog + reject() + + + 325 + 406 + + + 286 + 274 + + + + + buttonAdd + clicked() + CookiesDialog + onButtonAddClicked() + + + 484 + 174 + + + 486 + 93 + + + + + buttonDelete + clicked() + CookiesDialog + onButtonDeleteClicked() + + + 483 + 226 + + + 485 + 296 + + + + + + onButtonAddClicked() + onButtonDeleteClicked() + + diff --git a/src/gui/gui.pri b/src/gui/gui.pri index 6f3fbd42e..41eca6818 100644 --- a/src/gui/gui.pri +++ b/src/gui/gui.pri @@ -49,7 +49,8 @@ HEADERS += \ $$PWD/search/pluginsourcedlg.h \ $$PWD/search/searchlistdelegate.h \ $$PWD/search/searchsortmodel.h \ - $$PWD/cookiesmodel.h + $$PWD/cookiesmodel.h \ + $$PWD/cookiesdialog.h SOURCES += \ $$PWD/mainwindow.cpp \ @@ -89,7 +90,8 @@ SOURCES += \ $$PWD/search/pluginsourcedlg.cpp \ $$PWD/search/searchlistdelegate.cpp \ $$PWD/search/searchsortmodel.cpp \ - $$PWD/cookiesmodel.cpp + $$PWD/cookiesmodel.cpp \ + $$PWD/cookiesdialog.cpp win32|macx { HEADERS += $$PWD/programupdater.h @@ -116,6 +118,7 @@ FORMS += \ $$PWD/search/searchwidget.ui \ $$PWD/search/pluginselectdlg.ui \ $$PWD/search/pluginsourcedlg.ui \ - $$PWD/search/searchtab.ui + $$PWD/search/searchtab.ui \ + $$PWD/cookiesdialog.ui RESOURCES += $$PWD/about.qrc diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index a33868b7a..b47d0dbbc 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -91,6 +91,7 @@ void qt_mac_set_dock_menu(QMenu *menu); #include "base/net/downloadmanager.h" #include "base/net/downloadhandler.h" #endif +#include "cookiesdialog.h" #define TIME_TRAY_BALLOON 5000 #define PREVENT_SUSPEND_INTERVAL 60000 @@ -166,6 +167,7 @@ MainWindow::MainWindow(QWidget *parent) actionStart_All->setIcon(GuiIconProvider::instance()->getIcon("media-playback-start")); action_Import_Torrent->setIcon(GuiIconProvider::instance()->getIcon("document-import")); menuAuto_Shutdown_on_downloads_completion->setIcon(GuiIconProvider::instance()->getIcon("application-exit")); + actionManageCookies->setIcon(GuiIconProvider::instance()->getIcon("preferences-web-browser-cookies")); QMenu *startAllMenu = new QMenu(this); startAllMenu->addAction(actionStart_All); @@ -261,6 +263,8 @@ MainWindow::MainWindow(QWidget *parent) actionCheck_for_updates->setVisible(false); #endif + connect(actionManageCookies, SIGNAL(triggered()), SLOT(manageCookies())); + m_pwr = new PowerManagement(this); preventTimer = new QTimer(this); connect(preventTimer, SIGNAL(timeout()), SLOT(checkForActiveTorrents())); @@ -475,6 +479,11 @@ void MainWindow::addToolbarContextMenu() } } +void MainWindow::manageCookies() +{ + CookiesDialog(this).exec(); +} + void MainWindow::toolbarMenuRequested(QPoint point) { toolbarMenu->exec(toolBar->mapToGlobal(point)); diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index 977ce58c0..48b0bae36 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -166,6 +166,7 @@ private slots: void pythonDownloadFailure(const QString &url, const QString &error); #endif void addToolbarContextMenu(); + void manageCookies(); private: QFileSystemWatcher *executable_watcher; diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index 0b422a8ab..e974647ee 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -77,6 +77,7 @@ + @@ -414,6 +415,14 @@ Check for Program Updates + + + Manage Cookies... + + + Manage stored network cookies + + true