2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "cookiemanager.h"
|
|
|
|
#include "ui_cookiemanager.h"
|
|
|
|
#include "qupzilla.h"
|
|
|
|
#include "cookiejar.h"
|
|
|
|
#include "mainapplication.h"
|
2011-09-21 14:20:49 +02:00
|
|
|
#include "globalfunctions.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-10-26 19:23:50 +02:00
|
|
|
CookieManager::CookieManager(QWidget* parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
, ui(new Ui::CookieManager)
|
2012-02-13 16:59:30 +01:00
|
|
|
, m_refreshCookieJar(true)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-10-26 19:23:50 +02:00
|
|
|
setWindowModality(Qt::WindowModal);
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->setupUi(this);
|
2011-09-21 14:20:49 +02:00
|
|
|
qz_centerWidgetOnScreen(this);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
connect(ui->cookieTree, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
|
2011-03-02 16:57:41 +01:00
|
|
|
connect(ui->removeAll, SIGNAL(clicked()), this, SLOT(removeAll()));
|
2011-11-06 12:05:16 +01:00
|
|
|
connect(ui->removeOne, SIGNAL(clicked()), this, SLOT(removeCookie()));
|
2011-03-02 16:57:41 +01:00
|
|
|
connect(ui->close, SIGNAL(clicked(QAbstractButton*)), this, SLOT(hide()));
|
|
|
|
connect(ui->search, SIGNAL(returnPressed()), this, SLOT(search()));
|
2011-07-30 17:57:14 +02:00
|
|
|
connect(ui->search, SIGNAL(textChanged(QString)), ui->cookieTree, SLOT(filterString(QString)));
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
ui->search->setInactiveText(tr("Search"));
|
2011-07-30 17:57:14 +02:00
|
|
|
ui->cookieTree->setDefaultItemShowMode(TreeWidget::ItemsCollapsed);
|
2011-12-10 13:15:07 +01:00
|
|
|
ui->cookieTree->sortItems(0, Qt::AscendingOrder);
|
2012-02-13 18:22:24 +01:00
|
|
|
|
|
|
|
QShortcut* removeShortcut = new QShortcut(QKeySequence("Del"), this);
|
|
|
|
connect(removeShortcut, SIGNAL(activated()), this, SLOT(removeCookie()));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieManager::removeAll()
|
|
|
|
{
|
|
|
|
QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Confirmation"),
|
2011-11-06 17:01:23 +01:00
|
|
|
tr("Are you sure to delete all cookies on your computer?"), QMessageBox::Yes | QMessageBox::No);
|
|
|
|
if (button != QMessageBox::Yes) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
QList<QNetworkCookie> emptyList;
|
|
|
|
mApp->cookieJar()->setAllCookies(emptyList);
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->cookieTree->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieManager::removeCookie()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* current = ui->cookieTree->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!current) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
QList<QNetworkCookie> allCookies = mApp->cookieJar()->getAllCookies();
|
|
|
|
|
2011-11-06 12:05:16 +01:00
|
|
|
int indexToNavigate = -1;
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
if (current->text(1).isEmpty()) { //Remove whole cookie group
|
|
|
|
QString domain = current->whatsThis(0);
|
2012-02-13 18:22:24 +01:00
|
|
|
foreach(const QNetworkCookie & cookie, allCookies) {
|
|
|
|
if (cookie.domain() == domain || cookie.domain() == domain.mid(1)) {
|
|
|
|
allCookies.removeOne(cookie);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-11-06 12:05:16 +01:00
|
|
|
indexToNavigate = ui->cookieTree->indexOfTopLevelItem(current) - 1;
|
2012-02-13 16:59:30 +01:00
|
|
|
ui->cookieTree->deleteItem(current);
|
2011-11-06 12:05:16 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-02-13 18:22:24 +01:00
|
|
|
const QNetworkCookie &cookie = qvariant_cast<QNetworkCookie>(current->data(0, Qt::UserRole + 10));
|
|
|
|
allCookies.removeOne(cookie);
|
2012-02-13 16:59:30 +01:00
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
indexToNavigate = ui->cookieTree->indexOfTopLevelItem(current->parent());
|
2012-02-13 16:59:30 +01:00
|
|
|
ui->cookieTree->deleteItem(current);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
mApp->cookieJar()->setAllCookies(allCookies);
|
2011-11-06 12:05:16 +01:00
|
|
|
|
|
|
|
if (indexToNavigate > 0 && ui->cookieTree->topLevelItemCount() >= indexToNavigate) {
|
|
|
|
QTreeWidgetItem* scrollItem = ui->cookieTree->topLevelItem(indexToNavigate);
|
2012-02-13 16:59:30 +01:00
|
|
|
if (scrollItem) {
|
|
|
|
ui->cookieTree->setCurrentItem(scrollItem);
|
|
|
|
ui->cookieTree->scrollToItem(scrollItem);
|
|
|
|
}
|
2011-11-06 12:05:16 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!ui->search->text().isEmpty()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
search();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 17:03:04 +01:00
|
|
|
void CookieManager::currentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* parent)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!current) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
if (current->text(1).isEmpty()) {
|
|
|
|
ui->name->setText(tr("<cookie not selected>"));
|
|
|
|
ui->value->setText(tr("<cookie not selected>"));
|
|
|
|
ui->server->setText(tr("<cookie not selected>"));
|
|
|
|
ui->path->setText(tr("<cookie not selected>"));
|
|
|
|
ui->secure->setText(tr("<cookie not selected>"));
|
|
|
|
ui->expiration->setText(tr("<cookie not selected>"));
|
2011-11-06 12:05:16 +01:00
|
|
|
|
|
|
|
ui->removeOne->setText(tr("Remove cookies"));
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-11-06 12:05:16 +01:00
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
const QNetworkCookie &cookie = qvariant_cast<QNetworkCookie>(current->data(0, Qt::UserRole + 10));
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
ui->name->setText(cookie.name());
|
|
|
|
ui->value->setText(cookie.value());
|
|
|
|
ui->server->setText(cookie.domain());
|
|
|
|
ui->path->setText(cookie.path());
|
|
|
|
cookie.isSecure() ? ui->secure->setText(tr("Secure only")) : ui->secure->setText(tr("All connections"));
|
|
|
|
cookie.isSessionCookie() ? ui->expiration->setText(tr("Session cookie")) : ui->expiration->setText(QDateTime(cookie.expirationDate()).toString("hh:mm:ss dddd d. MMMM yyyy"));
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
ui->removeOne->setText(tr("Remove cookie"));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
void CookieManager::refreshTable()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-02-13 16:59:30 +01:00
|
|
|
QTimer::singleShot(0, this, SLOT(slotRefreshTable()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieManager::slotRefreshTable()
|
|
|
|
{
|
2012-02-13 18:22:24 +01:00
|
|
|
const QList<QNetworkCookie>& allCookies = mApp->cookieJar()->getAllCookies();
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-13 16:59:30 +01:00
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->cookieTree->clear();
|
|
|
|
|
2012-02-13 16:59:30 +01:00
|
|
|
int counter = 0;
|
2012-02-13 18:22:24 +01:00
|
|
|
QString cookieDomain;
|
|
|
|
for (int i = 0; i < allCookies.count(); ++i) {
|
2012-02-15 21:21:16 +01:00
|
|
|
const QNetworkCookie &cookie = allCookies.at(i);
|
2011-03-02 16:57:41 +01:00
|
|
|
QTreeWidgetItem* item;
|
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
cookieDomain = cookie.domain();
|
|
|
|
if (cookieDomain.startsWith(".")) {
|
|
|
|
cookieDomain = cookieDomain.mid(1);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
const QList<QTreeWidgetItem*>& findParent = ui->cookieTree->findItems(cookieDomain, 0);
|
2011-03-02 16:57:41 +01:00
|
|
|
if (findParent.count() == 1) {
|
|
|
|
item = new QTreeWidgetItem(findParent.at(0));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-03-02 16:57:41 +01:00
|
|
|
QTreeWidgetItem* newParent = new QTreeWidgetItem(ui->cookieTree);
|
2012-02-13 18:22:24 +01:00
|
|
|
newParent->setText(0, cookieDomain);
|
2011-03-02 16:57:41 +01:00
|
|
|
newParent->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon));
|
2012-02-13 18:22:24 +01:00
|
|
|
newParent->setWhatsThis(0, cookie.domain());
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->cookieTree->addTopLevelItem(newParent);
|
|
|
|
item = new QTreeWidgetItem(newParent);
|
|
|
|
}
|
|
|
|
|
2012-02-13 18:22:24 +01:00
|
|
|
item->setText(0, "." + cookieDomain);
|
|
|
|
item->setText(1, cookie.name());
|
|
|
|
item->setData(0, Qt::UserRole + 10, qVariantFromValue(cookie));
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->cookieTree->addTopLevelItem(item);
|
2012-02-13 16:59:30 +01:00
|
|
|
|
|
|
|
++counter;
|
|
|
|
if (counter > 50) {
|
|
|
|
QApplication::processEvents();
|
|
|
|
counter = 0;
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-02-13 16:59:30 +01:00
|
|
|
QApplication::restoreOverrideCursor();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieManager::search()
|
|
|
|
{
|
2011-11-06 12:05:16 +01:00
|
|
|
ui->cookieTree->filterString(ui->search->text());
|
|
|
|
// QString searchText = ui->search->text();
|
|
|
|
// if (searchText.isEmpty()) {
|
|
|
|
// refreshTable(false);
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// refreshTable(false);
|
|
|
|
// ui->cookieTree->setUpdatesEnabled(false);
|
|
|
|
|
|
|
|
// QList<QTreeWidgetItem*> items = ui->cookieTree->findItems(".*"+searchText+"*", Qt::MatchRecursive | Qt::MatchWildcard);
|
|
|
|
|
|
|
|
// QList<QTreeWidgetItem*> foundItems;
|
|
|
|
// foreach(QTreeWidgetItem* fitem, items) {
|
|
|
|
// if (!fitem->text(0).startsWith("."))
|
|
|
|
// continue;
|
|
|
|
// QTreeWidgetItem* item = new QTreeWidgetItem();
|
|
|
|
// item->setText(0, fitem->text(0));
|
|
|
|
// item->setText(1, fitem->text(1));
|
|
|
|
// item->setWhatsThis(1, fitem->whatsThis(1));
|
|
|
|
// foundItems.append(item);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ui->cookieTree->clear();
|
|
|
|
// ui->cookieTree->addTopLevelItems(foundItems);
|
|
|
|
// ui->cookieTree->setUpdatesEnabled(true);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CookieManager::~CookieManager()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|