24#include "gnome-keyring.h"
28static uint Q_DATETIME_TOTIME_T(
const QDateTime &dateTime)
30 if (!dateTime.isValid())
32 qint64 retval = dateTime.toMSecsSinceEpoch() / 1000;
33 if (quint64(retval) >= Q_UINT64_C(0xFFFFFFFF))
41 entry.
id = item->item_id;
42 entry.
password = QString::fromUtf8(item->secret);
44 for (
unsigned i = 0;
i < item->attributes->len; ++
i) {
45 GnomeKeyringAttribute attr = g_array_index(item->attributes, GnomeKeyringAttribute,
i);
47 if (strcmp(attr.name,
"host") == 0) {
48 entry.
host = QString::fromUtf8(attr.value.string);
50 else if (strcmp(attr.name,
"username") == 0) {
51 entry.
username = QString::fromUtf8(attr.value.string);
53 else if (strcmp(attr.name,
"data") == 0) {
54 entry.
data = attr.value.string;
56 else if (strcmp(attr.name,
"updated") == 0) {
57 entry.
updated = attr.value.integer;
66static GnomeKeyringAttributeList* createAttributes(
const PasswordEntry &entry)
68 GnomeKeyringAttributeList* attributes = gnome_keyring_attribute_list_new();
70 gnome_keyring_attribute_list_append_string(attributes,
"application",
"Falkon");
73 gnome_keyring_attribute_list_append_string(attributes,
"username",
value.constData());
77 gnome_keyring_attribute_list_append_string(attributes,
"data",
value.constData());
80 gnome_keyring_attribute_list_append_string(attributes,
"host",
value.constData());
82 gnome_keyring_attribute_list_append_uint32(attributes,
"updated", entry.
updated);
90 GnomeKeyringAttributeList* attributes = createAttributes(entry);
92 QByteArray pass = entry.
password.toUtf8();
93 QByteArray host = entry.
host.toUtf8();
95 GnomeKeyringResult result = gnome_keyring_item_create_sync(GNOME_KEYRING_DEFAULT,
96 GNOME_KEYRING_ITEM_GENERIC_SECRET,
103 gnome_keyring_attribute_list_free(attributes);
105 if (result != GNOME_KEYRING_RESULT_OK) {
106 qWarning() <<
"GnomeKeyringPasswordBackend::addEntry Cannot add entry to keyring!";
120 return GnomeKeyringPlugin::tr(
"Gnome Keyring");
129 QVector<PasswordEntry> list;
131 for (
const PasswordEntry &entry : std::as_const(m_allEntries)) {
132 if (entry.
host == host) {
138 std::sort(list.begin(), list.end());
155 stored.
updated = Q_DATETIME_TOTIME_T(QDateTime::currentDateTime());
159 m_allEntries.append(stored);
167 GnomeKeyringAttributeList* attributes = createAttributes(entry);
169 GnomeKeyringResult result = gnome_keyring_item_set_attributes_sync(GNOME_KEYRING_DEFAULT,
173 gnome_keyring_attribute_list_free(attributes);
175 if (result != GNOME_KEYRING_RESULT_OK) {
176 qWarning() <<
"GnomeKeyringPasswordBackend::updateEntry Cannot updated entry attributes in keyring!";
181 GnomeKeyringItemInfo* info;
182 result = gnome_keyring_item_get_info_full_sync(GNOME_KEYRING_DEFAULT, entry.
id.toUInt(),
183 GNOME_KEYRING_ITEM_INFO_SECRET, &info);
185 if (result != GNOME_KEYRING_RESULT_OK) {
186 qWarning() <<
"GnomeKeyringPasswordBackend::updateEntry Cannot get entry info from keyring!";
190 QByteArray pass = entry.
password.toUtf8();
191 gnome_keyring_item_info_set_secret(info, pass.constData());
193 result = gnome_keyring_item_set_info_sync(GNOME_KEYRING_DEFAULT, entry.
id.toUInt(), info);
195 gnome_keyring_item_info_free(info);
197 if (result != GNOME_KEYRING_RESULT_OK) {
198 qWarning() <<
"GnomeKeyringPasswordBackend::updateEntry Cannot set entry info in keyring!";
202 int index = m_allEntries.indexOf(entry);
205 m_allEntries[index] = entry;
215 entry.
updated = Q_DATETIME_TOTIME_T(QDateTime::currentDateTime());
217 GnomeKeyringAttributeList* attributes = createAttributes(entry);
219 GnomeKeyringResult result = gnome_keyring_item_set_attributes_sync(GNOME_KEYRING_DEFAULT,
223 gnome_keyring_attribute_list_free(attributes);
225 if (result != GNOME_KEYRING_RESULT_OK) {
226 qWarning() <<
"GnomeKeyringPasswordBackend::updateLastUsed Cannot updated entry in keyring!";
230 int index = m_allEntries.indexOf(entry);
233 m_allEntries[index] = entry;
241 GnomeKeyringResult result = gnome_keyring_item_delete_sync(GNOME_KEYRING_DEFAULT, entry.
id.toUInt());
243 if (result != GNOME_KEYRING_RESULT_OK) {
244 qWarning() <<
"GnomeKeyringPasswordBackend::removeEntry Cannot remove entry from keyring!";
248 int index = m_allEntries.indexOf(entry);
251 m_allEntries.remove(index);
259 for (
const PasswordEntry &entry : std::as_const(m_allEntries)) {
263 m_allEntries.clear();
266void GnomeKeyringPasswordBackend::initialize()
273 GnomeKeyringResult result = gnome_keyring_find_itemsv_sync(GNOME_KEYRING_ITEM_GENERIC_SECRET, &found,
274 "application", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING,
"Falkon",
277 if (result != GNOME_KEYRING_RESULT_OK && result != GNOME_KEYRING_RESULT_NO_MATCH) {
278 qWarning() <<
"GnomeKeyringPasswordBackend::initialize Cannot read items from keyring!";
282 bool migrate =
false;
283 if (result == GNOME_KEYRING_RESULT_NO_MATCH) {
284 result = gnome_keyring_find_itemsv_sync(GNOME_KEYRING_ITEM_GENERIC_SECRET, &found,
285 "application", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING,
"QupZilla",
288 if (result != GNOME_KEYRING_RESULT_OK && result != GNOME_KEYRING_RESULT_NO_MATCH) {
289 qWarning() <<
"GnomeKeyringPasswordBackend::initialize Cannot read items from keyring!";
293 if (result == GNOME_KEYRING_RESULT_OK) {
301 GnomeKeyringFound* item = (GnomeKeyringFound*) tmp->data;
302 m_allEntries.append(createEntry(item));
306 gnome_keyring_found_list_free(found);
QVector< PasswordEntry > getAllEntries()
QVector< PasswordEntry > getEntries(const QUrl &url)
void addEntry(const PasswordEntry &entry)
GnomeKeyringPasswordBackend()
bool updateEntry(const PasswordEntry &entry)
void removeEntry(const PasswordEntry &entry)
void updateLastUsed(PasswordEntry &entry)
static QString createHost(const QUrl &url)
static QByteArray urlEncodePassword(const QString &password)
int value(const QColor &c)