From ce025cac0c53d6c06296725e06cbd4cb7e435318 Mon Sep 17 00:00:00 2001 From: Frank Osterfeld Date: Sat, 4 Jan 2014 13:15:08 +0100 Subject: [PATCH] Move GnomeKeyring to separate files --- CMakeLists.txt | 2 +- gnomekeyring.cpp | 68 ++++++++++++++ gnomekeyring_p.h | 84 +++++++++++++++++ keychain_dbus.cpp => keychain_unix.cpp | 125 +------------------------ translations/qtkeychain_de.ts | 52 +++++----- translations/qtkeychain_ro.ts | 52 +++++----- 6 files changed, 206 insertions(+), 177 deletions(-) create mode 100644 gnomekeyring.cpp create mode 100644 gnomekeyring_p.h rename keychain_dbus.cpp => keychain_unix.cpp (76%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c86316e..1624578 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,7 +102,7 @@ if(APPLE) endif() if(UNIX AND NOT APPLE) - list(APPEND qtkeychain_SOURCES keychain_dbus.cpp) + list(APPEND qtkeychain_SOURCES keychain_unix.cpp gnomekeyring.cpp) qt_add_dbus_interface(qtkeychain_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/org.kde.KWallet.xml kwallet_interface KWalletInterface) list(APPEND qtkeychain_LIBRARIES ${QTDBUS_LIBRARIES}) endif() diff --git a/gnomekeyring.cpp b/gnomekeyring.cpp new file mode 100644 index 0000000..4191df9 --- /dev/null +++ b/gnomekeyring.cpp @@ -0,0 +1,68 @@ +#include "gnomekeyring_p.h" + +const char* GnomeKeyring::GNOME_KEYRING_DEFAULT = NULL; + +bool GnomeKeyring::isSupported() +{ + const GnomeKeyring& keyring = instance(); + return keyring.isLoaded() && + keyring.NETWORK_PASSWORD && + keyring.find_password && + keyring.store_password && + keyring.delete_password; +} + +GnomeKeyring::gpointer GnomeKeyring::store_network_password( const gchar* keyring, const gchar* display_name, + const gchar* user, const gchar* server, const gchar* password, + OperationDoneCallback callback, gpointer data, GDestroyNotify destroy_data ) +{ + if ( !isSupported() ) + return 0; + return instance().store_password( instance().NETWORK_PASSWORD, + keyring, display_name, password, callback, data, destroy_data, + "user", user, "server", server, static_cast(0) ); +} + +GnomeKeyring::gpointer GnomeKeyring::find_network_password( const gchar* user, const gchar* server, + OperationGetStringCallback callback, gpointer data, GDestroyNotify destroy_data ) +{ + if ( !isSupported() ) + return 0; + return instance().find_password( instance().NETWORK_PASSWORD, + callback, data, destroy_data, + "user", user, "server", server, static_cast(0) ); +} + +GnomeKeyring::gpointer GnomeKeyring::delete_network_password( const gchar* user, + const gchar* server, + OperationDoneCallback callback, + gpointer data, + GDestroyNotify destroy_data ) +{ + if ( !isSupported() ) + return 0; + return instance().delete_password( instance().NETWORK_PASSWORD, + callback, data, destroy_data, + "user", user, "server", server, static_cast(0) ); +} + +GnomeKeyring::GnomeKeyring() + : QLibrary("gnome-keyring", 0) +{ + static const PasswordSchema schema = { + ITEM_NETWORK_PASSWORD, + {{ "user", ATTRIBUTE_TYPE_STRING }, + { "server", ATTRIBUTE_TYPE_STRING }, + { 0, static_cast( 0 ) }} + }; + + NETWORK_PASSWORD = &schema; + find_password = reinterpret_cast( resolve( "gnome_keyring_find_password" ) ); + store_password = reinterpret_cast( resolve( "gnome_keyring_store_password" ) ); + delete_password = reinterpret_cast( resolve( "gnome_keyring_delete_password" ) ); +} + +GnomeKeyring& GnomeKeyring::instance() { + static GnomeKeyring keyring; + return keyring; +} diff --git a/gnomekeyring_p.h b/gnomekeyring_p.h new file mode 100644 index 0000000..abe049e --- /dev/null +++ b/gnomekeyring_p.h @@ -0,0 +1,84 @@ +#ifndef QTKEYCHAIN_GNOME_P_H +#define QTKEYCHAIN_GNOME_P_H + +#include + +class GnomeKeyring : private QLibrary { +public: + enum Result { + RESULT_OK, + RESULT_DENIED, + RESULT_NO_KEYRING_DAEMON, + RESULT_ALREADY_UNLOCKED, + RESULT_NO_SUCH_KEYRING, + RESULT_BAD_ARGUMENTS, + RESULT_IO_ERROR, + RESULT_CANCELLED, + RESULT_KEYRING_ALREADY_EXISTS, + RESULT_NO_MATCH + }; + + enum ItemType { + ITEM_GENERIC_SECRET = 0, + ITEM_NETWORK_PASSWORD, + ITEM_NOTE, + ITEM_CHAINED_KEYRING_PASSWORD, + ITEM_ENCRYPTION_KEY_PASSWORD, + ITEM_PK_STORAGE = 0x100 + }; + + enum AttributeType { + ATTRIBUTE_TYPE_STRING, + ATTRIBUTE_TYPE_UINT32 + }; + + typedef char gchar; + typedef void* gpointer; + typedef struct { + ItemType item_type; + struct { + const gchar* name; + AttributeType type; + } attributes[32]; + } PasswordSchema; + + typedef void ( *OperationGetStringCallback )( Result result, const char* string, gpointer data ); + typedef void ( *OperationDoneCallback )( Result result, gpointer data ); + typedef void ( *GDestroyNotify )( gpointer data ); + + static const char* GNOME_KEYRING_DEFAULT; + + static bool isSupported(); + + static gpointer store_network_password( const gchar* keyring, const gchar* display_name, + const gchar* user, const gchar* server, const gchar* password, + OperationDoneCallback callback, gpointer data, GDestroyNotify destroy_data ); + + static gpointer find_network_password( const gchar* user, const gchar* server, + OperationGetStringCallback callback, gpointer data, GDestroyNotify destroy_data ); + + static gpointer delete_network_password( const gchar* user, const gchar* server, + OperationDoneCallback callback, gpointer data, GDestroyNotify destroy_data ); +private: + GnomeKeyring(); + + static GnomeKeyring& instance(); + + const PasswordSchema* NETWORK_PASSWORD; + typedef gpointer ( store_password_fn )( const PasswordSchema* schema, const gchar* keyring, + const gchar* display_name, const gchar* password, + OperationDoneCallback callback, gpointer data, GDestroyNotify destroy_data, + ... ); + typedef gpointer ( find_password_fn )( const PasswordSchema* schema, + OperationGetStringCallback callback, gpointer data, GDestroyNotify destroy_data, + ... ); + typedef gpointer ( delete_password_fn )( const PasswordSchema* schema, + OperationDoneCallback callback, gpointer data, GDestroyNotify destroy_data, + ... ); + find_password_fn* find_password; + store_password_fn* store_password; + delete_password_fn* delete_password; +}; + + +#endif diff --git a/keychain_dbus.cpp b/keychain_unix.cpp similarity index 76% rename from keychain_dbus.cpp rename to keychain_unix.cpp index dbb0fd9..c464697 100644 --- a/keychain_dbus.cpp +++ b/keychain_unix.cpp @@ -7,6 +7,7 @@ * details, check the accompanying file 'COPYING'. * *****************************************************************************/ #include "keychain_p.h" +#include "gnomekeyring_p.h" #include @@ -24,130 +25,6 @@ static QString dataKey( const QString& key ) return QString::fromLatin1( "%1/data" ).arg( key ); } -class GnomeKeyring : private QLibrary { -public: - enum Result { - RESULT_OK, - RESULT_DENIED, - RESULT_NO_KEYRING_DAEMON, - RESULT_ALREADY_UNLOCKED, - RESULT_NO_SUCH_KEYRING, - RESULT_BAD_ARGUMENTS, - RESULT_IO_ERROR, - RESULT_CANCELLED, - RESULT_KEYRING_ALREADY_EXISTS, - RESULT_NO_MATCH - }; - - enum ItemType { - ITEM_GENERIC_SECRET = 0, - ITEM_NETWORK_PASSWORD, - ITEM_NOTE, - ITEM_CHAINED_KEYRING_PASSWORD, - ITEM_ENCRYPTION_KEY_PASSWORD, - ITEM_PK_STORAGE = 0x100 - }; - - enum AttributeType { - ATTRIBUTE_TYPE_STRING, - ATTRIBUTE_TYPE_UINT32 - }; - - typedef char gchar; - typedef void* gpointer; - typedef struct { - ItemType item_type; - struct { - const gchar* name; - AttributeType type; - } attributes[32]; - } PasswordSchema; - - typedef void ( *OperationGetStringCallback )( Result result, const char* string, gpointer data ); - typedef void ( *OperationDoneCallback )( Result result, gpointer data ); - typedef void ( *GDestroyNotify )( gpointer data ); - - static const char* GNOME_KEYRING_DEFAULT; - - static bool isSupported() - { - const GnomeKeyring& keyring = instance(); - return keyring.isLoaded() && - keyring.NETWORK_PASSWORD && - keyring.find_password && - keyring.store_password && - keyring.delete_password; - } - - static gpointer store_network_password( const gchar* keyring, const gchar* display_name, - const gchar* user, const gchar* server, const gchar* password, - OperationDoneCallback callback, gpointer data, GDestroyNotify destroy_data ) - { - if ( !isSupported() ) - return 0; - return instance().store_password( instance().NETWORK_PASSWORD, - keyring, display_name, password, callback, data, destroy_data, - "user", user, "server", server, static_cast(0) ); - } - - static gpointer find_network_password( const gchar* user, const gchar* server, - OperationGetStringCallback callback, gpointer data, GDestroyNotify destroy_data ) - { - if ( !isSupported() ) - return 0; - return instance().find_password( instance().NETWORK_PASSWORD, - callback, data, destroy_data, - "user", user, "server", server, static_cast(0) ); - } - - static gpointer delete_network_password( const gchar* user, const gchar* server, - OperationDoneCallback callback, gpointer data, GDestroyNotify destroy_data ) - { - if ( !isSupported() ) - return 0; - return instance().delete_password( instance().NETWORK_PASSWORD, - callback, data, destroy_data, - "user", user, "server", server, static_cast(0) ); - } - -private: - GnomeKeyring(): QLibrary("gnome-keyring", 0) { - static const PasswordSchema schema = { - ITEM_NETWORK_PASSWORD, - {{ "user", ATTRIBUTE_TYPE_STRING }, - { "server", ATTRIBUTE_TYPE_STRING }, - { 0, static_cast( 0 ) }} - }; - - NETWORK_PASSWORD = &schema; - find_password = reinterpret_cast( resolve( "gnome_keyring_find_password" ) ); - store_password = reinterpret_cast( resolve( "gnome_keyring_store_password" ) ); - delete_password = reinterpret_cast( resolve( "gnome_keyring_delete_password" ) ); - } - - static GnomeKeyring& instance() { - static GnomeKeyring keyring; - return keyring; - } - - const PasswordSchema* NETWORK_PASSWORD; - typedef gpointer ( store_password_fn )( const PasswordSchema* schema, const gchar* keyring, - const gchar* display_name, const gchar* password, - OperationDoneCallback callback, gpointer data, GDestroyNotify destroy_data, - ... ); - typedef gpointer ( find_password_fn )( const PasswordSchema* schema, - OperationGetStringCallback callback, gpointer data, GDestroyNotify destroy_data, - ... ); - typedef gpointer ( delete_password_fn )( const PasswordSchema* schema, - OperationDoneCallback callback, gpointer data, GDestroyNotify destroy_data, - ... ); - find_password_fn* find_password; - store_password_fn* store_password; - delete_password_fn* delete_password; -}; - -const char* GnomeKeyring::GNOME_KEYRING_DEFAULT = NULL; - enum KeyringBackend { Backend_GnomeKeyring, Backend_Kwallet diff --git a/translations/qtkeychain_de.ts b/translations/qtkeychain_de.ts index 826f7ca..3b2d97f 100644 --- a/translations/qtkeychain_de.ts +++ b/translations/qtkeychain_de.ts @@ -4,47 +4,47 @@ QKeychain::ReadPasswordJobPrivate - + Unknown error Unbekannter Fehler - + D-Bus is not running - + No keychain service available Kein Schlüsselbund-Dienst verfügbar - + Could not open wallet: %1; %2 Konnte Brieftasche nicht öffnen: %1; %2 - + Access to keychain denied Zugriff auf Schlüsselbund verweigert - + Could not determine data type: %1; %2 Datentyp kann nicht ermittelt werden: %1: %2 - + Unsupported entry type 'Map' - + Unknown kwallet entry type '%1' - + Could not read password: %1; %2 Passwort konnte nicht ausgelesen werden: %1; %2 @@ -54,7 +54,7 @@ Passwort nicht gefunden - + Entry not found Eintrag nicht gefunden @@ -68,24 +68,24 @@ QKeychain::WritePasswordJobPrivate - - + + Unknown error Unbekannter Fehler - + D-Bus is not running - - + + Could not open wallet: %1; %2 Konnte Brieftasche nicht öffnen: %1; %2 - + Access to keychain denied Zugriff auf Schlüsselbund verweigert @@ -118,52 +118,52 @@ QObject - + Access to keychain denied Zugriff auf Schlüsselbund verweigert - + No keyring daemon Kein Schlüsselbund-Dienst - + Already unlocked Bereits entsperrt - + No such keyring Kein solcher Schlüsselbund - + Bad arguments Ungültige Argumente - + I/O error Ein-/Ausgabe-Fehler - + Cancelled Abgebrochen - + Keyring already exists Schlüsselbund existiert bereits - + No match Kein Treffer - + Unknown error Unbekannter Fehler diff --git a/translations/qtkeychain_ro.ts b/translations/qtkeychain_ro.ts index e49452c..e1c8ceb 100644 --- a/translations/qtkeychain_ro.ts +++ b/translations/qtkeychain_ro.ts @@ -4,48 +4,48 @@ QKeychain::ReadPasswordJobPrivate - + Unknown error Eroare necunoscută - + D-Bus is not running D-Bus nu rulează - + No keychain service available Nu există niciun serviciu de chei disponibil Kein Schlüsselbund-Dienst verfügbar - + Could not open wallet: %1; %2 Nu se poate deschide portofelul: %1; %2 - + Access to keychain denied Acces interzis la serviciul de chei - + Could not determine data type: %1; %2 Nu se poate stabili tipul de date: %1: %2 - + Unsupported entry type 'Map' Tip de înregistrare nesuportat 'Map' - + Unknown kwallet entry type '%1' Tip de înregistrare kwallet necunoscut '%1' - + Could not read password: %1; %2 Nu se poate citi parola: %1; %2 @@ -55,7 +55,7 @@ Parola nu a fost găsită - + Entry not found Înregistrarea nu a fost găsită @@ -69,24 +69,24 @@ QKeychain::WritePasswordJobPrivate - - + + Unknown error Eroare necunoscută - + D-Bus is not running D-Bus nu rulează - - + + Could not open wallet: %1; %2 Nu se poate deschide portofelul: %1; %2 - + Access to keychain denied Acces interzis la serviciul de chei @@ -119,52 +119,52 @@ QObject - + Access to keychain denied Acces interzis la serviciul de chei - + No keyring daemon Niciun demon pentru inelul de chei - + Already unlocked Deja deblocat - + No such keyring Nu există astfel de inel de chei - + Bad arguments Argumente greșite - + I/O error Eroare de I/E - + Cancelled Anulat - + Keyring already exists Inelul de chei deja există - + No match Nicio potrivire - + Unknown error Eroare necunoscută