2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 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 "autofillmanager.h"
|
2013-05-17 15:42:06 +02:00
|
|
|
#include "ui_autofillmanager.h"
|
2013-01-24 17:47:50 +01:00
|
|
|
#include "autofill.h"
|
2013-05-14 22:45:20 +02:00
|
|
|
#include "passwordmanager.h"
|
2013-05-17 15:42:06 +02:00
|
|
|
#include "passwordbackends/passwordbackend.h"
|
2013-05-14 22:45:20 +02:00
|
|
|
#include "mainapplication.h"
|
2013-05-17 15:42:06 +02:00
|
|
|
#include "settings.h"
|
2013-12-26 20:49:59 +01:00
|
|
|
#include "qztools.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2013-05-17 15:42:06 +02:00
|
|
|
#include <QUrl>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QInputDialog>
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
2011-10-26 19:23:50 +02:00
|
|
|
AutoFillManager::AutoFillManager(QWidget* parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
, ui(new Ui::AutoFillManager)
|
2013-05-18 19:50:12 +02:00
|
|
|
, m_passwordManager(mApp->autoFill()->passwordManager())
|
2011-10-26 19:23:50 +02:00
|
|
|
, m_passwordsShown(false)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2013-07-11 18:18:32 +02:00
|
|
|
if (isRightToLeft()) {
|
|
|
|
ui->treePass->headerItem()->setTextAlignment(0, Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
ui->treePass->headerItem()->setTextAlignment(1, Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
ui->treePass->headerItem()->setTextAlignment(2, Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
ui->treePass->setLayoutDirection(Qt::LeftToRight);
|
|
|
|
ui->treeExcept->setLayoutDirection(Qt::LeftToRight);
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
connect(ui->removePass, SIGNAL(clicked()), this, SLOT(removePass()));
|
|
|
|
connect(ui->removeAllPass, SIGNAL(clicked()), this, SLOT(removeAllPass()));
|
|
|
|
connect(ui->editPass, SIGNAL(clicked()), this, SLOT(editPass()));
|
2011-10-26 19:23:50 +02:00
|
|
|
connect(ui->showPasswords, SIGNAL(clicked()), this, SLOT(showPasswords()));
|
2013-01-31 19:16:11 +01:00
|
|
|
connect(ui->search, SIGNAL(textChanged(QString)), ui->treePass, SLOT(filterString(QString)));
|
2013-05-17 15:42:06 +02:00
|
|
|
connect(ui->changeBackend, SIGNAL(clicked()), this, SLOT(changePasswordBackend()));
|
2013-05-18 19:50:12 +02:00
|
|
|
connect(ui->backendOptions, SIGNAL(clicked()), this, SLOT(showBackendOptions()));
|
2013-06-08 14:58:36 +02:00
|
|
|
connect(m_passwordManager, SIGNAL(passwordBackendChanged()), this, SLOT(currentPasswordBackendChanged()));
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
connect(ui->removeExcept, SIGNAL(clicked()), this, SLOT(removeExcept()));
|
|
|
|
connect(ui->removeAllExcept, SIGNAL(clicked()), this, SLOT(removeAllExcept()));
|
|
|
|
|
2012-02-06 20:10:50 +01:00
|
|
|
QMenu* menu = new QMenu(this);
|
|
|
|
menu->addAction(tr("Import Passwords from File..."), this, SLOT(importPasswords()));
|
|
|
|
menu->addAction(tr("Export Passwords to File..."), this, SLOT(exportPasswords()));
|
|
|
|
ui->importExport->setMenu(menu);
|
2013-01-31 19:16:11 +01:00
|
|
|
ui->search->setPlaceholderText(tr("Search"));
|
2012-02-06 20:10:50 +01:00
|
|
|
|
2013-05-17 15:42:06 +02:00
|
|
|
// Password backends
|
2013-05-18 19:50:12 +02:00
|
|
|
ui->currentBackend->setText(QString("<b>%1</b>").arg(m_passwordManager->activeBackend()->name()));
|
|
|
|
ui->backendOptions->setVisible(m_passwordManager->activeBackend()->hasSettings());
|
2013-05-17 15:42:06 +02:00
|
|
|
|
|
|
|
// Load passwords
|
2011-03-02 16:57:41 +01:00
|
|
|
QTimer::singleShot(0, this, SLOT(loadPasswords()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillManager::loadPasswords()
|
|
|
|
{
|
2012-03-13 17:51:06 +01:00
|
|
|
ui->showPasswords->setText(tr("Show Passwords"));
|
2012-03-23 14:23:54 +01:00
|
|
|
m_passwordsShown = false;
|
2012-03-13 17:51:06 +01:00
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
QVector<PasswordEntry> allEntries = mApp->autoFill()->getAllFormData();
|
2012-03-13 17:51:06 +01:00
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
ui->treePass->clear();
|
|
|
|
foreach (const PasswordEntry &entry, allEntries) {
|
2011-03-02 16:57:41 +01:00
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(ui->treePass);
|
2013-05-14 22:45:20 +02:00
|
|
|
item->setText(0, entry.host);
|
|
|
|
item->setText(1, entry.username);
|
2011-11-20 17:09:10 +01:00
|
|
|
item->setText(2, "*****");
|
2013-05-14 22:45:20 +02:00
|
|
|
|
|
|
|
QVariant v;
|
|
|
|
v.setValue<PasswordEntry>(entry);
|
|
|
|
item->setData(0, Qt::UserRole + 10, v);
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->treePass->addTopLevelItem(item);
|
|
|
|
}
|
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
QSqlQuery query;
|
2011-03-02 16:57:41 +01:00
|
|
|
query.exec("SELECT server, id FROM autofill_exceptions");
|
|
|
|
ui->treeExcept->clear();
|
2011-11-06 17:01:23 +01:00
|
|
|
while (query.next()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeExcept);
|
|
|
|
item->setText(0, query.value(0).toString());
|
2012-03-13 17:51:06 +01:00
|
|
|
item->setData(0, Qt::UserRole + 10, query.value(1).toString());
|
2011-03-02 16:57:41 +01:00
|
|
|
ui->treeExcept->addTopLevelItem(item);
|
|
|
|
}
|
2012-09-01 13:56:00 +02:00
|
|
|
|
|
|
|
ui->treePass->sortByColumn(-1);
|
|
|
|
ui->treeExcept->sortByColumn(-1);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:42:06 +02:00
|
|
|
void AutoFillManager::changePasswordBackend()
|
|
|
|
{
|
2013-05-18 19:50:12 +02:00
|
|
|
QHash<QString, PasswordBackend*> backends = m_passwordManager->availableBackends();
|
2013-05-17 15:42:06 +02:00
|
|
|
QStringList items;
|
|
|
|
|
|
|
|
int current = 0;
|
|
|
|
|
2013-05-19 10:34:54 +02:00
|
|
|
QHashIterator<QString, PasswordBackend*> i(backends);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (i.value() == m_passwordManager->activeBackend()) {
|
2013-05-17 15:42:06 +02:00
|
|
|
current = items.size();
|
|
|
|
}
|
2013-05-19 10:34:54 +02:00
|
|
|
items << i.value()->name();
|
2013-05-17 15:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString item = QInputDialog::getItem(this, tr("Change backend..."), tr("Change backend:"), items, current, false);
|
|
|
|
|
2013-05-19 10:34:54 +02:00
|
|
|
// Switch backends
|
2013-05-17 15:42:06 +02:00
|
|
|
if (!item.isEmpty()) {
|
2013-05-18 13:29:11 +02:00
|
|
|
PasswordBackend* backend = 0;
|
2013-05-19 10:34:54 +02:00
|
|
|
|
|
|
|
QHashIterator<QString, PasswordBackend*> i(backends);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (i.value()->name() == item) {
|
|
|
|
backend = i.value();
|
2013-05-17 15:42:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-18 19:50:12 +02:00
|
|
|
if (backend) {
|
2013-06-08 14:58:36 +02:00
|
|
|
m_passwordManager->switchBackend(backends.key(backend));
|
2013-05-18 19:50:12 +02:00
|
|
|
}
|
2013-05-17 15:42:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-18 19:50:12 +02:00
|
|
|
void AutoFillManager::showBackendOptions()
|
|
|
|
{
|
|
|
|
PasswordBackend* backend = m_passwordManager->activeBackend();
|
|
|
|
|
|
|
|
if (backend->hasSettings()) {
|
|
|
|
backend->showSettings(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-26 19:23:50 +02:00
|
|
|
void AutoFillManager::showPasswords()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_passwordsShown) {
|
2011-11-21 18:24:56 +01:00
|
|
|
for (int i = 0; i < ui->treePass->topLevelItemCount(); i++) {
|
|
|
|
QTreeWidgetItem* item = ui->treePass->topLevelItem(i);
|
|
|
|
if (!item) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
item->setText(2, "*****");
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->showPasswords->setText(tr("Show Passwords"));
|
|
|
|
m_passwordsShown = false;
|
|
|
|
|
2011-10-26 19:23:50 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-11-21 18:24:56 +01:00
|
|
|
|
2011-10-26 19:23:50 +02:00
|
|
|
m_passwordsShown = true;
|
|
|
|
|
|
|
|
int result = QMessageBox::question(this, tr("Show Passwords"), tr("Are you sure that you want to show all passwords?"),
|
2011-11-06 17:01:23 +01:00
|
|
|
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
|
|
|
|
if (result != QMessageBox::Yes) {
|
2011-10-26 19:23:50 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-26 19:23:50 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < ui->treePass->topLevelItemCount(); i++) {
|
|
|
|
QTreeWidgetItem* item = ui->treePass->topLevelItem(i);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!item) {
|
2011-10-26 19:23:50 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-03-13 17:51:06 +01:00
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
item->setText(2, item->data(0, Qt::UserRole + 10).value<PasswordEntry>().password);
|
2011-10-26 19:23:50 +02:00
|
|
|
}
|
|
|
|
|
2011-11-21 18:24:56 +01:00
|
|
|
ui->showPasswords->setText(tr("Hide Passwords"));
|
2011-10-26 19:23:50 +02:00
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
void AutoFillManager::removePass()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* curItem = ui->treePass->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!curItem) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2013-05-14 22:45:20 +02:00
|
|
|
|
|
|
|
PasswordEntry entry = curItem->data(0, Qt::UserRole + 10).value<PasswordEntry>();
|
|
|
|
mApp->autoFill()->removeEntry(entry);
|
2011-10-26 19:23:50 +02:00
|
|
|
|
|
|
|
delete curItem;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillManager::removeAllPass()
|
|
|
|
{
|
|
|
|
QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Confirmation"),
|
2011-11-06 17:01:23 +01:00
|
|
|
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-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
mApp->autoFill()->removeAllEntries();
|
2011-10-26 19:23:50 +02:00
|
|
|
ui->treePass->clear();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillManager::editPass()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* curItem = ui->treePass->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!curItem) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2013-05-14 22:45:20 +02:00
|
|
|
|
|
|
|
PasswordEntry entry = curItem->data(0, Qt::UserRole + 10).value<PasswordEntry>();
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
bool ok;
|
2013-05-14 22:45:20 +02:00
|
|
|
QString text = QInputDialog::getText(this, tr("Edit password"), tr("Change password:"), QLineEdit::Normal, entry.password, &ok);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2013-06-08 15:11:38 +02:00
|
|
|
if (ok && !text.isEmpty() && text != entry.password) {
|
2013-05-20 13:33:13 +02:00
|
|
|
QByteArray oldPass = "=" + PasswordManager::urlEncodePassword(entry.password);
|
|
|
|
entry.data.replace(oldPass, "=" + PasswordManager::urlEncodePassword(text));
|
2013-05-14 22:45:20 +02:00
|
|
|
entry.password = text;
|
|
|
|
|
2013-06-08 15:11:38 +02:00
|
|
|
if (mApp->autoFill()->updateEntry(entry)) {
|
|
|
|
QVariant v;
|
|
|
|
v.setValue<PasswordEntry>(entry);
|
|
|
|
curItem->setData(0, Qt::UserRole + 10, v);
|
2013-05-14 22:45:20 +02:00
|
|
|
|
2013-06-08 15:11:38 +02:00
|
|
|
if (m_passwordsShown) {
|
|
|
|
curItem->setText(2, text);
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillManager::removeExcept()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* curItem = ui->treeExcept->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!curItem) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-03-13 17:51:06 +01:00
|
|
|
QString id = curItem->data(0, Qt::UserRole + 10).toString();
|
2011-03-02 16:57:41 +01:00
|
|
|
QSqlQuery query;
|
2012-08-23 15:37:43 +02:00
|
|
|
query.prepare("DELETE FROM autofill_exceptions WHERE id=?");
|
|
|
|
query.addBindValue(id);
|
|
|
|
query.exec();
|
2011-10-26 19:23:50 +02:00
|
|
|
|
|
|
|
delete curItem;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillManager::removeAllExcept()
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.exec("DELETE FROM autofill_exceptions");
|
2011-10-26 19:23:50 +02:00
|
|
|
|
|
|
|
ui->treeExcept->clear();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillManager::showExceptions()
|
|
|
|
{
|
|
|
|
ui->tabWidget->setCurrentIndex(1);
|
|
|
|
}
|
|
|
|
|
2012-02-06 20:10:50 +01:00
|
|
|
void AutoFillManager::importPasswords()
|
|
|
|
{
|
2013-12-26 20:49:59 +01:00
|
|
|
m_fileName = QzTools::getOpenFileName("AutoFill-ImportPasswords", this, tr("Choose file..."), QDir::homePath() + "/passwords.xml", "*.xml");
|
2013-05-23 18:16:20 +02:00
|
|
|
|
|
|
|
if (m_fileName.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTimer::singleShot(0, this, SLOT(slotImportPasswords()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillManager::exportPasswords()
|
|
|
|
{
|
2013-12-26 20:49:59 +01:00
|
|
|
m_fileName = QzTools::getSaveFileName("AutoFill-ExportPasswords", this, tr("Choose file..."), QDir::homePath() + "/passwords.xml", "*.xml");
|
2013-05-23 18:16:20 +02:00
|
|
|
|
|
|
|
if (m_fileName.isEmpty()) {
|
2012-02-06 20:10:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-23 18:16:20 +02:00
|
|
|
QTimer::singleShot(0, this, SLOT(slotExportPasswords()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutoFillManager::slotImportPasswords()
|
|
|
|
{
|
|
|
|
QFile file(m_fileName);
|
|
|
|
|
2012-02-06 20:10:50 +01:00
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
ui->importExportLabel->setText(tr("Cannot read file!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-22 15:43:44 +02:00
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
|
|
|
|
|
|
bool status = mApp->autoFill()->importPasswords(file.readAll());
|
2012-02-06 20:10:50 +01:00
|
|
|
file.close();
|
|
|
|
|
2012-02-06 21:53:31 +01:00
|
|
|
ui->importExportLabel->setText(status ? tr("Successfully imported") : tr("Error while importing!"));
|
2012-02-06 20:10:50 +01:00
|
|
|
loadPasswords();
|
2013-05-22 15:43:44 +02:00
|
|
|
|
|
|
|
QApplication::restoreOverrideCursor();
|
2012-02-06 20:10:50 +01:00
|
|
|
}
|
|
|
|
|
2013-05-23 18:16:20 +02:00
|
|
|
void AutoFillManager::slotExportPasswords()
|
2012-02-06 20:10:50 +01:00
|
|
|
{
|
2013-05-23 18:16:20 +02:00
|
|
|
QFile file(m_fileName);
|
2012-02-06 20:10:50 +01:00
|
|
|
|
|
|
|
if (!file.open(QFile::WriteOnly)) {
|
|
|
|
ui->importExportLabel->setText(tr("Cannot write to file!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-22 15:43:44 +02:00
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
|
|
|
|
|
|
file.write(mApp->autoFill()->exportPasswords());
|
2012-02-06 20:10:50 +01:00
|
|
|
file.close();
|
|
|
|
|
2012-02-06 21:53:31 +01:00
|
|
|
ui->importExportLabel->setText(tr("Successfully exported"));
|
2013-05-22 15:43:44 +02:00
|
|
|
|
|
|
|
QApplication::restoreOverrideCursor();
|
2012-02-06 20:10:50 +01:00
|
|
|
}
|
|
|
|
|
2013-06-08 14:58:36 +02:00
|
|
|
void AutoFillManager::currentPasswordBackendChanged()
|
|
|
|
{
|
|
|
|
ui->currentBackend->setText(QString("<b>%1</b>").arg(m_passwordManager->activeBackend()->name()));
|
|
|
|
ui->backendOptions->setVisible(m_passwordManager->activeBackend()->hasSettings());
|
|
|
|
|
|
|
|
QTimer::singleShot(0, this, SLOT(loadPasswords()));
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
AutoFillManager::~AutoFillManager()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|