1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-21 17:52:10 +02:00

[PasswordManager] Ported importing/exporting for multiple backends.

However, for some reason it doesn't work for KWallet backend.
This commit is contained in:
nowrep 2013-05-22 15:43:44 +02:00
parent 3abfaed452
commit 7e2b631f34
5 changed files with 38 additions and 35 deletions

View File

@ -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<PasswordEntry> 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();

View File

@ -61,8 +61,8 @@ public:
void post(const QNetworkRequest &request, const QByteArray &outgoingData);
QVector<PasswordEntry> completePage(WebPage* page);
static QByteArray exportPasswords();
static bool importPasswords(const QByteArray &data);
QByteArray exportPasswords();
bool importPasswords(const QByteArray &data);
private:
PasswordManager* m_manager;

View File

@ -191,4 +191,3 @@ PasswordManager::~PasswordManager()
{
delete m_databaseBackend;
}

View File

@ -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 {

View File

@ -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()