diff --git a/src/lib/autofill/autofill.cpp b/src/lib/autofill/autofill.cpp index 681d4305e..bf1dc826b 100644 --- a/src/lib/autofill/autofill.cpp +++ b/src/lib/autofill/autofill.cpp @@ -264,17 +264,18 @@ QByteArray AutoFill::exportPasswords() stream.writeStartElement("passwords"); stream.writeAttribute("version", "1.0"); - QSqlQuery query; - query.exec("SELECT server, username, password, data FROM autofill"); - while (query.next()) { + QVector entries = m_manager->getAllEntries(); + + foreach (const PasswordEntry &entry, entries) { stream.writeStartElement("entry"); - stream.writeTextElement("server", query.value(0).toString()); - stream.writeTextElement("username", query.value(1).toString()); - stream.writeTextElement("password", query.value(2).toString()); - stream.writeTextElement("data", query.value(3).toString()); + stream.writeTextElement("server", entry.host); + stream.writeTextElement("username", entry.username); + stream.writeTextElement("password", entry.password); + stream.writeTextElement("data", entry.data); stream.writeEndElement(); } + QSqlQuery query; query.exec("SELECT server FROM autofill_exceptions"); while (query.next()) { stream.writeStartElement("exception"); @@ -300,23 +301,20 @@ bool AutoFill::importPasswords(const QByteArray &data) if (xml.isStartElement()) { if (xml.name() == QLatin1String("entry")) { - QString server; - QString username; - QString password; - QByteArray data; + PasswordEntry entry; while (xml.readNext()) { if (xml.name() == QLatin1String("server")) { - server = xml.readElementText(); + entry.host = xml.readElementText(); } else if (xml.name() == QLatin1String("username")) { - username = xml.readElementText(); + entry.username = xml.readElementText(); } else if (xml.name() == QLatin1String("password")) { - password = xml.readElementText(); + entry.password = xml.readElementText(); } else if (xml.name() == QLatin1String("data")) { - data = xml.readElementText().toUtf8(); + entry.data = xml.readElementText().toUtf8(); } if (xml.isEndElement() && xml.name() == QLatin1String("entry")) { @@ -324,21 +322,18 @@ bool AutoFill::importPasswords(const QByteArray &data) } } - if (!server.isEmpty() && !password.isEmpty() && !data.isEmpty()) { - QSqlQuery query; - query.prepare("SELECT id FROM autofill WHERE server=? AND password=? AND data=?"); - query.addBindValue(server); - query.addBindValue(password); - query.addBindValue(data); - query.exec(); + if (entry.isValid()) { + bool containsEntry = false; - if (!query.next()) { - query.prepare("INSERT INTO autofill (server, username, password, data) VALUES (?,?,?,?)"); - query.addBindValue(server); - query.addBindValue(username); - query.addBindValue(password); - query.addBindValue(data); - query.exec(); + foreach (const PasswordEntry &e, m_manager->getEntries(QUrl(entry.host))) { + if (e.username == entry.username) { + containsEntry = true; + break; + } + } + + if (!containsEntry) { + m_manager->addEntry(entry); } } } @@ -370,6 +365,7 @@ bool AutoFill::importPasswords(const QByteArray &data) } } } + db.commit(); return !xml.hasError(); diff --git a/src/lib/autofill/autofill.h b/src/lib/autofill/autofill.h index b0a282b31..c23f3413b 100644 --- a/src/lib/autofill/autofill.h +++ b/src/lib/autofill/autofill.h @@ -61,8 +61,8 @@ public: void post(const QNetworkRequest &request, const QByteArray &outgoingData); QVector completePage(WebPage* page); - static QByteArray exportPasswords(); - static bool importPasswords(const QByteArray &data); + QByteArray exportPasswords(); + bool importPasswords(const QByteArray &data); private: PasswordManager* m_manager; diff --git a/src/lib/autofill/passwordmanager.cpp b/src/lib/autofill/passwordmanager.cpp index b06ddaeea..bc21cd3cb 100644 --- a/src/lib/autofill/passwordmanager.cpp +++ b/src/lib/autofill/passwordmanager.cpp @@ -191,4 +191,3 @@ PasswordManager::~PasswordManager() { delete m_databaseBackend; } - diff --git a/src/lib/autofill/passwordmanager.h b/src/lib/autofill/passwordmanager.h index e482d57c0..e2b1af30a 100644 --- a/src/lib/autofill/passwordmanager.h +++ b/src/lib/autofill/passwordmanager.h @@ -38,7 +38,7 @@ struct QT_QUPZILLA_EXPORT PasswordEntry { PasswordEntry() : updated(-1) { } bool isValid() const { - return !password.isEmpty(); + return !password.isEmpty() && !host.isEmpty(); } bool operator==(const PasswordEntry &other) const { diff --git a/src/lib/preferences/autofillmanager.cpp b/src/lib/preferences/autofillmanager.cpp index e2a462cb0..b3eb6e3c8 100644 --- a/src/lib/preferences/autofillmanager.cpp +++ b/src/lib/preferences/autofillmanager.cpp @@ -287,11 +287,15 @@ void AutoFillManager::importPasswords() return; } - bool status = AutoFill::importPasswords(file.readAll()); + QApplication::setOverrideCursor(Qt::WaitCursor); + + bool status = mApp->autoFill()->importPasswords(file.readAll()); file.close(); ui->importExportLabel->setText(status ? tr("Successfully imported") : tr("Error while importing!")); loadPasswords(); + + QApplication::restoreOverrideCursor(); } void AutoFillManager::exportPasswords() @@ -307,10 +311,14 @@ void AutoFillManager::exportPasswords() return; } - file.write(AutoFill::exportPasswords()); + QApplication::setOverrideCursor(Qt::WaitCursor); + + file.write(mApp->autoFill()->exportPasswords()); file.close(); ui->importExportLabel->setText(tr("Successfully exported")); + + QApplication::restoreOverrideCursor(); } AutoFillManager::~AutoFillManager()