1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-22 18:22:10 +02:00
falkonOfficial/src/preferences/autofillmanager.cpp

179 lines
5.4 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2011-10-17 09:57:07 +02:00
* Copyright (C) 2010-2011 David Rosca
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 "autofillmanager.h"
#include "ui_autofillmanager.h"
AutoFillManager::AutoFillManager(QWidget* parent)
: QWidget(parent)
, ui(new Ui::AutoFillManager)
, m_passwordsShown(false)
2011-03-02 16:57:41 +01:00
{
ui->setupUi(this);
connect(ui->removePass, SIGNAL(clicked()), this, SLOT(removePass()));
connect(ui->removeAllPass, SIGNAL(clicked()), this, SLOT(removeAllPass()));
connect(ui->editPass, SIGNAL(clicked()), this, SLOT(editPass()));
connect(ui->showPasswords, SIGNAL(clicked()), this, SLOT(showPasswords()));
2011-03-02 16:57:41 +01:00
connect(ui->removeExcept, SIGNAL(clicked()), this, SLOT(removeExcept()));
connect(ui->removeAllExcept, SIGNAL(clicked()), this, SLOT(removeAllExcept()));
QTimer::singleShot(0, this, SLOT(loadPasswords()));
}
void AutoFillManager::loadPasswords()
{
QSqlQuery query;
query.exec("SELECT server, password, id FROM autofill");
ui->treePass->clear();
while (query.next()) {
2011-03-02 16:57:41 +01:00
QTreeWidgetItem* item = new QTreeWidgetItem(ui->treePass);
item->setText(0, query.value(0).toString());
// item->setText(1, query.value(1).toString());
item->setText(1, "*****");
item->setWhatsThis(0, query.value(2).toString());
item->setWhatsThis(1, query.value(1).toString());
2011-03-02 16:57:41 +01:00
ui->treePass->addTopLevelItem(item);
}
query.exec("SELECT server, id FROM autofill_exceptions");
ui->treeExcept->clear();
while (query.next()) {
2011-03-02 16:57:41 +01:00
QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeExcept);
item->setText(0, query.value(0).toString());
item->setWhatsThis(0, query.value(1).toString());
ui->treeExcept->addTopLevelItem(item);
}
}
void AutoFillManager::showPasswords()
{
if (m_passwordsShown) {
return;
}
m_passwordsShown = true;
int result = QMessageBox::question(this, tr("Show Passwords"), tr("Are you sure that you want to show all passwords?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (result != QMessageBox::Yes) {
return;
}
for (int i = 0; i < ui->treePass->topLevelItemCount(); i++) {
QTreeWidgetItem* item = ui->treePass->topLevelItem(i);
if (!item) {
continue;
}
item->setText(1, item->whatsThis(1));
}
ui->showPasswords->hide();
delete ui->showPasswordsLayout;
}
2011-03-02 16:57:41 +01:00
void AutoFillManager::removePass()
{
QTreeWidgetItem* curItem = ui->treePass->currentItem();
if (!curItem) {
2011-03-02 16:57:41 +01:00
return;
}
QString id = curItem->whatsThis(0);
2011-03-02 16:57:41 +01:00
QSqlQuery query;
query.exec("DELETE FROM autofill WHERE id=" + id);
delete curItem;
2011-03-02 16:57:41 +01:00
}
void AutoFillManager::removeAllPass()
{
QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Confirmation"),
tr("Are you sure to delete all passwords on your computer?"), QMessageBox::Yes | QMessageBox::No);
if (button != QMessageBox::Yes) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
QSqlQuery query;
query.exec("DELETE FROM autofill");
ui->treePass->clear();
2011-03-02 16:57:41 +01:00
}
void AutoFillManager::editPass()
{
QTreeWidgetItem* curItem = ui->treePass->currentItem();
if (!curItem) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
bool ok;
QString text = QInputDialog::getText(this, tr("Edit password"), tr("Change password:"), QLineEdit::Normal, curItem->whatsThis(1), &ok);
2011-03-02 16:57:41 +01:00
if (ok && !text.isEmpty()) {
QSqlQuery query;
query.prepare("SELECT data, password FROM autofill WHERE id=?");
query.addBindValue(curItem->whatsThis(0));
query.exec();
query.next();
QString data = query.value(0).toString();
QString oldPass = "=" + query.value(1).toString();
data.replace(oldPass, "=" + text);
query.prepare("UPDATE autofill SET data=?, password=? WHERE id=?");
query.bindValue(0, data);
query.bindValue(1, text);
query.bindValue(2, curItem->whatsThis(0));
query.exec();
if (m_passwordsShown) {
curItem->setText(1, text);
}
curItem->setWhatsThis(1, text);
2011-03-02 16:57:41 +01:00
}
}
void AutoFillManager::removeExcept()
{
QTreeWidgetItem* curItem = ui->treeExcept->currentItem();
if (!curItem) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
QString id = curItem->whatsThis(0);
QSqlQuery query;
query.exec("DELETE FROM autofill_exceptions WHERE id=" + id);
delete curItem;
2011-03-02 16:57:41 +01:00
}
void AutoFillManager::removeAllExcept()
{
QSqlQuery query;
query.exec("DELETE FROM autofill_exceptions");
ui->treeExcept->clear();
2011-03-02 16:57:41 +01:00
}
void AutoFillManager::showExceptions()
{
ui->tabWidget->setCurrentIndex(1);
}
AutoFillManager::~AutoFillManager()
{
delete ui;
}