1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

[AutoFill] Support for saving passwords of multiple users per site.

This commit is contained in:
nowrep 2013-02-08 15:07:56 +01:00
parent 8988f9c86e
commit 3ccc0a67c1
6 changed files with 77 additions and 57 deletions

View File

@ -4,6 +4,7 @@ Version 1.4.0
* can now be compiled using Qt 5 * can now be compiled using Qt 5
* QtWebKit 2.3 new features - caret browsing, animated scrolling * QtWebKit 2.3 new features - caret browsing, animated scrolling
* added support for FTP listing files and downloading * added support for FTP listing files and downloading
* added support for saving passwords of multiple users per site
* asking user whether to allow site to use notifications/geolocation * asking user whether to allow site to use notifications/geolocation
* option to set JavaScript privacy permissions * option to set JavaScript privacy permissions
* option to specify default search engine used in locationbar * option to specify default search engine used in locationbar

View File

@ -110,7 +110,21 @@ void AutoFill::blockStoringforUrl(const QUrl &url)
mApp->dbWriter()->executeQuery(query); mApp->dbWriter()->executeQuery(query);
} }
QList<AutoFillData> AutoFill::getFormData(const QUrl &url) AutoFillData AutoFill::getFirstFormData(const QUrl &url)
{
QList<AutoFillData> list = getFormData(url, 1);
if (list.isEmpty()) {
AutoFillData data;
data.id = -1;
return data;
}
return list.first();
}
QList<AutoFillData> AutoFill::getFormData(const QUrl &url, int limit)
{ {
QList<AutoFillData> list; QList<AutoFillData> list;
@ -119,10 +133,20 @@ QList<AutoFillData> AutoFill::getFormData(const QUrl &url)
server = url.toString(); server = url.toString();
} }
QString queryString = "SELECT id, username, password, data FROM autofill "
"WHERE server=? ORDER BY last_used DESC";
if (limit > 0) {
queryString.append(QLatin1String(" LIMIT ?"));
}
QSqlQuery query; QSqlQuery query;
query.prepare("SELECT id, username, password, data FROM autofill " query.prepare(queryString);
"WHERE server=? ORDER BY last_used DESC");
query.addBindValue(server); query.addBindValue(server);
if (limit > 0) {
query.addBindValue(limit);
}
query.exec(); query.exec();
while (query.next()) { while (query.next()) {
@ -136,6 +160,7 @@ QList<AutoFillData> AutoFill::getFormData(const QUrl &url)
} }
return list; return list;
} }
void AutoFill::updateLastUsed(int id) void AutoFill::updateLastUsed(int id)
@ -224,27 +249,15 @@ void AutoFill::updateEntry(const QUrl &url, const QString &name, const QString &
mApp->dbWriter()->executeQuery(query); mApp->dbWriter()->executeQuery(query);
} }
void AutoFill::updateEntry(const QUrl &url, const PageFormData &formData) void AutoFill::updateEntry(const PageFormData &formData, const AutoFillData &updateData)
{ {
QSqlQuery query; QSqlQuery query;
QString server = url.host(); query.prepare("UPDATE autofill SET data=?, username=?, password=? WHERE id=?");
if (server.isEmpty()) {
server = url.toString();
}
query.prepare("SELECT data FROM autofill WHERE server=?");
query.addBindValue(server);
query.exec();
if (!query.next()) {
return;
}
query.prepare("UPDATE autofill SET data=?, username=?, password=? WHERE server=?");
query.addBindValue(formData.postData); query.addBindValue(formData.postData);
query.addBindValue(formData.username); query.addBindValue(formData.username);
query.addBindValue(formData.password); query.addBindValue(formData.password);
query.addBindValue(server); query.addBindValue(updateData.id);
mApp->dbWriter()->executeQuery(query); mApp->dbWriter()->executeQuery(query);
} }
@ -259,23 +272,12 @@ void AutoFill::completePage(WebPage* page)
return; return;
} }
QString server = pageUrl.host(); const AutoFillData data = getFirstFormData(pageUrl);
if (server.isEmpty()) {
server = pageUrl.toString();
}
QSqlQuery query;
query.prepare("SELECT data FROM autofill WHERE server=?");
query.addBindValue(server);
query.exec();
query.next();
QByteArray data = query.value(0).toByteArray();
if (data.isEmpty()) {
return;
}
if (data.isValid()) {
PageFormCompleter completer(page); PageFormCompleter completer(page);
completer.completePage(data); completer.completePage(data.postData);
}
} }
void AutoFill::post(const QNetworkRequest &request, const QByteArray &outgoingData) void AutoFill::post(const QNetworkRequest &request, const QByteArray &outgoingData)
@ -308,16 +310,23 @@ void AutoFill::post(const QNetworkRequest &request, const QByteArray &outgoingDa
return; return;
} }
bool updateData = false; AutoFillData updateData = { -1, QString(), QString(), QByteArray() };
if (isStored(siteUrl)) {
const AutoFillData data = getFormData(siteUrl).first();
if (data.username == formData.username && data.password == formData.password) { if (isStored(siteUrl)) {
const QList<AutoFillData> &list = getFormData(siteUrl);
foreach(const AutoFillData & data, list) {
if (data.username == formData.username) {
updateLastUsed(data.id); updateLastUsed(data.id);
if (data.password == formData.password) {
return; return;
} }
updateData = true; updateData = data;
break;
}
}
} }
AutoFillNotification* aWidget = new AutoFillNotification(siteUrl, formData, updateData); AutoFillNotification* aWidget = new AutoFillNotification(siteUrl, formData, updateData);

View File

@ -36,6 +36,10 @@ struct AutoFillData {
QString username; QString username;
QString password; QString password;
QByteArray postData; QByteArray postData;
bool isValid() const {
return id > -1;
}
}; };
class QT_QUPZILLA_EXPORT AutoFill : public QObject class QT_QUPZILLA_EXPORT AutoFill : public QObject
@ -49,14 +53,16 @@ public:
bool isStoringEnabled(const QUrl &url); bool isStoringEnabled(const QUrl &url);
void blockStoringforUrl(const QUrl &url); void blockStoringforUrl(const QUrl &url);
QList<AutoFillData> getFormData(const QUrl &url); AutoFillData getFirstFormData(const QUrl &url);
QList<AutoFillData> getFormData(const QUrl &url, int limit = 0);
void updateLastUsed(int id); void updateLastUsed(int id);
void addEntry(const QUrl &url, const QString &name, const QString &pass); void addEntry(const QUrl &url, const QString &name, const QString &pass);
void addEntry(const QUrl &url, const PageFormData &formData); void addEntry(const QUrl &url, const PageFormData &formData);
void updateEntry(const QUrl &url, const QString &name, const QString &pass); void updateEntry(const QUrl &url, const QString &name, const QString &pass);
void updateEntry(const QUrl &url, const PageFormData &formData); void updateEntry(const PageFormData &formData, const AutoFillData &updateData);
void post(const QNetworkRequest &request, const QByteArray &outgoingData); void post(const QNetworkRequest &request, const QByteArray &outgoingData);
void completePage(WebPage* frame); void completePage(WebPage* frame);

View File

@ -22,12 +22,12 @@
#include "animatedwidget.h" #include "animatedwidget.h"
#include "iconprovider.h" #include "iconprovider.h"
AutoFillNotification::AutoFillNotification(const QUrl &url, const PageFormData &formData, bool updateData) AutoFillNotification::AutoFillNotification(const QUrl &url, const PageFormData &formData, const AutoFillData &updateData)
: AnimatedWidget(AnimatedWidget::Down, 300, 0) : AnimatedWidget(AnimatedWidget::Down, 300, 0)
, ui(new Ui::AutoFillWidget) , ui(new Ui::AutoFillWidget)
, m_updateData(updateData)
, m_url(url) , m_url(url)
, m_formData(formData) , m_formData(formData)
, m_updateData(updateData)
{ {
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(widget()); ui->setupUi(widget());
@ -44,8 +44,8 @@ AutoFillNotification::AutoFillNotification(const QUrl &url, const PageFormData &
userPart = tr("for <b>%1</b>").arg(m_formData.username); userPart = tr("for <b>%1</b>").arg(m_formData.username);
} }
if (updateData) { if (m_updateData.isValid()) {
ui->label->setText(tr("Do you want QupZilla to update saved password %1?").arg(hostPart)); ui->label->setText(tr("Do you want QupZilla to update saved password %1?").arg(userPart));
ui->remember->setVisible(false); ui->remember->setVisible(false);
ui->never->setVisible(false); ui->never->setVisible(false);
@ -67,7 +67,7 @@ AutoFillNotification::AutoFillNotification(const QUrl &url, const PageFormData &
void AutoFillNotification::update() void AutoFillNotification::update()
{ {
mApp->autoFill()->updateEntry(m_url, m_formData); mApp->autoFill()->updateEntry(m_formData, m_updateData);
hide(); hide();
} }

View File

@ -23,6 +23,7 @@
#include "qz_namespace.h" #include "qz_namespace.h"
#include "animatedwidget.h" #include "animatedwidget.h"
#include "pageformcompleter.h" #include "pageformcompleter.h"
#include "autofill.h"
namespace Ui namespace Ui
{ {
@ -37,7 +38,8 @@ class QT_QUPZILLA_EXPORT AutoFillNotification : public AnimatedWidget
public: public:
explicit AutoFillNotification(const QUrl &url, explicit AutoFillNotification(const QUrl &url,
const PageFormData &formData, bool updateData); const PageFormData &formData,
const AutoFillData &updateData);
~AutoFillNotification(); ~AutoFillNotification();
private slots: private slots:
@ -48,9 +50,9 @@ private slots:
private: private:
Ui::AutoFillWidget* ui; Ui::AutoFillWidget* ui;
bool m_updateData;
QUrl m_url; QUrl m_url;
PageFormData m_formData; PageFormData m_formData;
AutoFillData m_updateData;
}; };
#endif // AUTOFILLWIDGET_H #endif // AUTOFILLWIDGET_H

View File

@ -289,8 +289,9 @@ void NetworkManager::authentication(QNetworkReply* reply, QAuthenticator* auth)
QString storedUser; QString storedUser;
QString storedPassword; QString storedPassword;
if (fill->isStored(reply->url())) { if (fill->isStored(reply->url())) {
const AutoFillData &data = fill->getFormData(reply->url()).first(); const AutoFillData &data = fill->getFirstFormData(reply->url());
if (data.isValid()) {
save->setChecked(true); save->setChecked(true);
shouldUpdateEntry = true; shouldUpdateEntry = true;
storedUser = data.username; storedUser = data.username;
@ -298,6 +299,7 @@ void NetworkManager::authentication(QNetworkReply* reply, QAuthenticator* auth)
user->setText(storedUser); user->setText(storedUser);
pass->setText(storedPassword); pass->setText(storedPassword);
} }
}
emit wantsFocus(reply->url()); emit wantsFocus(reply->url());
//Do not save when private browsing is enabled //Do not save when private browsing is enabled