Falkon Develop
Cross-platform Qt-based web browser
databasepasswordbackend.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2013-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
19#include "mainapplication.h"
20#include "autofill.h"
21#include "sqldatabase.h"
22
23#include <QVector>
24
27{
28}
29
31{
32 return AutoFill::tr("Database (plaintext)");
33}
34
35QVector<PasswordEntry> DatabasePasswordBackend::getEntries(const QUrl &url)
36{
37 const QString host = PasswordManager::createHost(url);
38
39 QSqlQuery query(SqlDatabase::instance()->database());
40 query.prepare(QSL("SELECT id, username, password, data FROM autofill "
41 "WHERE server=? ORDER BY last_used DESC"));
42 query.addBindValue(host);
43 query.exec();
44
45 QVector<PasswordEntry> list;
46
47 while (query.next()) {
48 PasswordEntry data;
49 data.id = query.value(0);
50 data.host = host;
51 data.username = query.value(1).toString();
52 data.password = query.value(2).toString();
53 data.data = query.value(3).toByteArray();
54
55 list.append(data);
56 }
57
58 return list;
59}
60
62{
63 QVector<PasswordEntry> list;
64
65 QSqlQuery query(SqlDatabase::instance()->database());
66 query.prepare(QSL("SELECT id, server, username, password, data FROM autofill"));
67 query.exec();
68
69 while (query.next()) {
70 PasswordEntry data;
71 data.id = query.value(0);
72 data.host = query.value(1).toString();
73 data.username = query.value(2).toString();
74 data.password = query.value(3).toString();
75 data.data = query.value(4).toByteArray();
76
77 list.append(data);
78 }
79
80 return list;
81}
82
84{
85 // Data is empty only for HTTP/FTP authorization
86 if (entry.data.isEmpty()) {
87 // Multiple-usernames for HTTP/FTP authorization not supported
88 QSqlQuery query(SqlDatabase::instance()->database());
89 query.prepare(QSL("SELECT username FROM autofill WHERE server=?"));
90 query.addBindValue(entry.host);
91 query.exec();
92
93 if (query.next()) {
94 return;
95 }
96 }
97
98 QSqlQuery query(SqlDatabase::instance()->database());
99 query.prepare(QSL("INSERT INTO autofill (server, data, username, password, last_used) "
100 "VALUES (?,?,?,?,strftime('%s', 'now'))"));
101 query.bindValue(0, entry.host);
102 query.bindValue(1, entry.data);
103 query.bindValue(2, entry.username);
104 query.bindValue(3, entry.password);
105 query.exec();
106}
107
109{
110 QSqlQuery query(SqlDatabase::instance()->database());
111
112 // Data is empty only for HTTP/FTP authorization
113 if (entry.data.isEmpty()) {
114 query.prepare(QSL("UPDATE autofill SET username=?, password=? WHERE server=?"));
115 query.bindValue(0, entry.username);
116 query.bindValue(1, entry.password);
117 query.bindValue(2, entry.host);
118 }
119 else {
120 query.prepare(QSL("UPDATE autofill SET data=?, username=?, password=? WHERE id=?"));
121 query.addBindValue(entry.data);
122 query.addBindValue(entry.username);
123 query.addBindValue(entry.password);
124 query.addBindValue(entry.id);
125 }
126
127 return query.exec();
128}
129
131{
132 QSqlQuery query(SqlDatabase::instance()->database());
133 query.prepare(QSL("UPDATE autofill SET last_used=strftime('%s', 'now') WHERE id=?"));
134 query.addBindValue(entry.id);
135 query.exec();
136}
137
139{
140 QSqlQuery query(SqlDatabase::instance()->database());
141 query.prepare(QSL("DELETE FROM autofill WHERE id=?"));
142 query.addBindValue(entry.id);
143 query.exec();
144}
145
147{
148 QSqlQuery query(SqlDatabase::instance()->database());
149 query.exec(QSL("DELETE FROM autofill"));
150}
QVector< PasswordEntry > getAllEntries() override
QString name() const override
QVector< PasswordEntry > getEntries(const QUrl &url) override
bool updateEntry(const PasswordEntry &entry) override
void removeEntry(const PasswordEntry &entry) override
void addEntry(const PasswordEntry &entry) override
void updateLastUsed(PasswordEntry &entry) override
static QString createHost(const QUrl &url)
static SqlDatabase * instance()
#define QSL(x)
Definition: qzcommon.h:40
QString password
QString host
QByteArray data
QString username
QVariant id