[almanah] VFS: Included the GSettings backend in the VFS



commit 61a38bc46c09201f7bcc76800361cc0209884a85
Author: Álvaro Peña <alvaropg gmail com>
Date:   Wed Jun 10 14:02:33 2015 +0200

    VFS: Included the GSettings backend in the VFS
    
    The GSettings backend has been added to the VFS to allow the
    creation of tests. The VFS uses the GSettings backend to retrieve
    the encryption key fingerprint (if selected)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=659500

 src/application.c      |    2 +-
 src/import-operation.c |    5 +++-
 src/storage-manager.c  |   27 +++++++++++++++---
 src/storage-manager.h  |    3 +-
 src/vfs.c              |   69 ++++++++++++++++++++++++-----------------------
 src/vfs.h              |    4 ++-
 6 files changed, 67 insertions(+), 43 deletions(-)
---
diff --git a/src/application.c b/src/application.c
index cf8b4c8..de6f794 100644
--- a/src/application.c
+++ b/src/application.c
@@ -247,7 +247,7 @@ startup (GApplication *application)
 
        /* Open the DB */
        db_filename = g_build_filename (g_get_user_data_dir (), "diary.db", NULL);
-       priv->storage_manager = almanah_storage_manager_new (db_filename);
+       priv->storage_manager = almanah_storage_manager_new (db_filename, priv->settings);
        g_free (db_filename);
 
        if (almanah_storage_manager_connect (priv->storage_manager, &error) == FALSE) {
diff --git a/src/import-operation.c b/src/import-operation.c
index 3434b92..70770be 100644
--- a/src/import-operation.c
+++ b/src/import-operation.c
@@ -437,6 +437,7 @@ import_database (AlmanahImportOperation *self, GFile *source, AlmanahImportProgr
        AlmanahStorageManager *database;
        AlmanahStorageManagerIter iter;
        gboolean success = FALSE;
+       GSettings *settings;
 
        /* Get the display name for use with set_entry(), below */
        file_info = g_file_query_info (source, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 
G_FILE_QUERY_INFO_NONE, cancellable, error);
@@ -448,7 +449,9 @@ import_database (AlmanahImportOperation *self, GFile *source, AlmanahImportProgr
 
        /* Open the database */
        path = g_file_get_path (source);
-       database = almanah_storage_manager_new (path);
+       settings = g_settings_new ("org.gnome.almanah");
+       database = almanah_storage_manager_new (path, settings);
+       g_object_unref (settings);
        g_free (path);
 
        /* Connect to the database */
diff --git a/src/storage-manager.c b/src/storage-manager.c
index 439f5ac..adcb606 100644
--- a/src/storage-manager.c
+++ b/src/storage-manager.c
@@ -42,10 +42,12 @@ static gboolean simple_query (AlmanahStorageManager *self, const gchar *query, G
 struct _AlmanahStorageManagerPrivate {
        gchar *filename;
        sqlite3 *connection;
+       GSettings *settings;
 };
 
 enum {
        PROP_FILENAME = 1,
+       PROP_SETTINGS
 };
 
 enum {
@@ -86,6 +88,12 @@ almanah_storage_manager_class_init (AlmanahStorageManagerClass *klass)
                                                              NULL,
                                                              G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | 
G_PARAM_STATIC_STRINGS));
 
+       g_object_class_install_property (gobject_class, PROP_SETTINGS,
+                                        g_param_spec_object ("settings",
+                                                             "The application settings object", "The 
application settings object to retrieve encryption key.",
+                                                             G_TYPE_SETTINGS,
+                                                             G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
+
        storage_manager_signals[SIGNAL_DISCONNECTED] = g_signal_new ("disconnected",
                                                                     G_TYPE_FROM_CLASS (klass),
                                                                     G_SIGNAL_RUN_LAST,
@@ -134,20 +142,22 @@ almanah_storage_manager_init (AlmanahStorageManager *self)
 /**
  * almanah_storage_manager_new:
  * @filename: database filename to open
+ * @settings: the %GSettings used to retrieve encryption key
  *
- * Creates a new #AlmanahStorageManager, connected to the given database @filename.
- *
- * If @encryption_key is %NULL, encryption will be disabled.
+ * Creates a new #AlmanahStorageManager, connected to the given database @filename, using
+ * GSettings to retrieve the encryption key configured by the user in order to encrypt
+ * the database.
  *
  * Return value: the new #AlmanahStorageManager
  **/
 AlmanahStorageManager *
-almanah_storage_manager_new (const gchar *filename)
+almanah_storage_manager_new (const gchar *filename, GSettings *settings)
 {
        AlmanahStorageManager *sm;
 
        sm = g_object_new (ALMANAH_TYPE_STORAGE_MANAGER,
                           "filename", filename,
+                          "settings", settings,
                           NULL);
 
        return sm;
@@ -173,6 +183,9 @@ almanah_storage_manager_get_property (GObject *object, guint property_id, GValue
                case PROP_FILENAME:
                        g_value_set_string (value, g_strdup (priv->filename));
                        break;
+               case PROP_SETTINGS:
+                       g_value_set_object (value, priv->settings);
+                       break;
                default:
                        /* We don't have any other property... */
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -191,6 +204,10 @@ almanah_storage_manager_set_property (GObject *object, guint property_id, const
                                g_free (priv->filename);
                        priv->filename = g_strdup (g_value_get_string (value));
                        break;
+               case PROP_SETTINGS:
+                       g_clear_object (&priv->settings);
+                       g_set_object (&priv->settings, g_value_get_object (value));
+                       break;
                default:
                        /* We don't have any other property... */
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -224,7 +241,7 @@ gboolean
 almanah_storage_manager_connect (AlmanahStorageManager *self, GError **error)
 {
        /* Our beautiful SQLite VFS */
-       almanah_vfs_init();
+       almanah_vfs_init(self->priv->settings);
 
        /* Open the plain database */
        if (sqlite3_open_v2 (self->priv->filename, &(self->priv->connection), SQLITE_OPEN_READWRITE | 
SQLITE_OPEN_CREATE, "almanah") != SQLITE_OK) {
diff --git a/src/storage-manager.h b/src/storage-manager.h
index a40a919..9a2551a 100644
--- a/src/storage-manager.h
+++ b/src/storage-manager.h
@@ -22,6 +22,7 @@
 
 #include <glib.h>
 #include <glib-object.h>
+#include <gio/gio.h>
 
 #include "entry.h"
 
@@ -68,7 +69,7 @@ typedef void (*AlmanahStorageManagerSearchCallback) (AlmanahStorageManager *stor
 
 GType almanah_storage_manager_get_type (void);
 GQuark almanah_storage_manager_error_quark (void);
-AlmanahStorageManager *almanah_storage_manager_new (const gchar *filename) G_GNUC_WARN_UNUSED_RESULT 
G_GNUC_MALLOC;
+AlmanahStorageManager *almanah_storage_manager_new (const gchar *filename, GSettings *settings) 
G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
 
 gboolean almanah_storage_manager_connect (AlmanahStorageManager *self, GError **error);
 gboolean almanah_storage_manager_disconnect (AlmanahStorageManager *self, GError **error);
diff --git a/src/vfs.c b/src/vfs.c
index baee5d2..ae0d835 100644
--- a/src/vfs.c
+++ b/src/vfs.c
@@ -92,6 +92,8 @@ struct _AlmanahSQLiteVFS
        gsize    plain_buffer_size;     /* Reserved memory size */
        goffset  plain_offset;
        gsize    plain_size;            /* Data size (plain_size <= plain_buffer_size) */
+
+       GSettings *settings;
 };
 
 typedef struct _CipherOperation CipherOperation;
@@ -440,15 +442,13 @@ encrypt_database (AlmanahSQLiteVFS *self,  const gchar *encryption_key, gboolean
 }
 
 static gchar *
-get_encryption_key (void)
+get_encryption_key (AlmanahSQLiteVFS *self)
 {
-       GSettings *settings;
        gchar *encryption_key;
        gchar **key_parts;
        guint i;
 
-       settings = g_settings_new ("org.gnome.almanah");
-       encryption_key = g_settings_get_string (settings, "encryption-key");
+       encryption_key = g_settings_get_string (self->settings, "encryption-key");
        if (encryption_key == NULL || encryption_key[0] == '\0') {
                g_free (encryption_key);
                return NULL;
@@ -606,7 +606,7 @@ almanah_vfs_io_close (sqlite3_file *pFile)
        GError *child_error = NULL;
        int rc;
 
-       encryption_key = get_encryption_key ();
+       encryption_key = get_encryption_key (self);
        if (encryption_key == NULL) {
                if (self->decrypted) {
                        /* Save the data from memory to plain file */
@@ -928,11 +928,11 @@ almanah_vfs_io_device_characteristis(__attribute__ ((unused)) sqlite3_file *pFil
 ** Open a file handle.
 */
 static int
-almanah_vfs_open (__attribute__ ((unused)) sqlite3_vfs *pVfs,
-         const char *zName,
-         sqlite3_file *pFile,
-         int flags,
-         int *pOutFlags)
+almanah_vfs_open (sqlite3_vfs *pVfs,
+                 const char *zName,
+                 sqlite3_file *pFile,
+                 int flags,
+                 int *pOutFlags)
 {
        static const sqlite3_io_methods almanah_vfs_io = {
                1,
@@ -1057,6 +1057,7 @@ almanah_vfs_open (__attribute__ ((unused)) sqlite3_vfs *pVfs,
                }
        }
 
+       self->settings = (GSettings *) pVfs->pAppData;
        self->base.pMethods = &almanah_vfs_io;
 
        return SQLITE_OK;
@@ -1246,33 +1247,33 @@ almanah_vfs_current_time (__attribute__ ((unused)) sqlite3_vfs *pVfs, double *pT
 **   sqlite3_vfs_register(sqlite3_demovfs(), 0);
 */
 sqlite3_vfs*
-sqlite3_almanah_vfs (void)
+sqlite3_almanah_vfs (GSettings *settings)
 {
-       static sqlite3_vfs almanah_vfs = {
-               1,
-               sizeof(AlmanahSQLiteVFS),
-               MAXPATHNAME,
-               0,
-               "almanah",
-               0,
-               almanah_vfs_open,
-               almanah_vfs_delete,
-               almanah_vfs_access,
-               almanah_vfs_full_pathname,
-               almanah_vfs_dl_open,
-               almanah_vfs_dl_error,
-               almanah_vfs_dl_sym,
-               almanah_vfs_dl_close,
-               almanah_vfs_randomness,
-               almanah_vfs_sleep,
-               almanah_vfs_current_time,
-       };
-
-       return &almanah_vfs;
+       sqlite3_vfs *almanah_vfs;
+
+       almanah_vfs = (sqlite3_vfs *) g_new0(sqlite3_vfs, 1);
+       almanah_vfs->iVersion = 1;
+       almanah_vfs->szOsFile = sizeof(AlmanahSQLiteVFS);
+       almanah_vfs->mxPathname = MAXPATHNAME;
+       almanah_vfs->zName = "almanah";
+       almanah_vfs->pAppData = settings;
+       almanah_vfs->xOpen = almanah_vfs_open;
+       almanah_vfs->xDelete = almanah_vfs_delete;
+       almanah_vfs->xAccess = almanah_vfs_access;
+       almanah_vfs->xFullPathname = almanah_vfs_full_pathname;
+       almanah_vfs->xDlOpen = almanah_vfs_dl_open;
+       almanah_vfs->xDlError = almanah_vfs_dl_error;
+       almanah_vfs->xDlSym  = almanah_vfs_dl_sym;
+       almanah_vfs->xDlClose = almanah_vfs_dl_close;
+       almanah_vfs->xRandomness = almanah_vfs_randomness;
+       almanah_vfs->xSleep = almanah_vfs_sleep;
+       almanah_vfs->xCurrentTime = almanah_vfs_current_time;
+
+       return almanah_vfs;
 }
 
 int
-almanah_vfs_init (void)
+almanah_vfs_init (GSettings *settings)
 {
-       return sqlite3_vfs_register (sqlite3_almanah_vfs (), 0);
+       return sqlite3_vfs_register (sqlite3_almanah_vfs (settings), 0);
 }
diff --git a/src/vfs.h b/src/vfs.h
index 552c768..4c71b2f 100644
--- a/src/vfs.h
+++ b/src/vfs.h
@@ -17,4 +17,6 @@
  * along with Almanah.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-int almanah_vfs_init(void);
+#include <gio/gio.h>
+
+int almanah_vfs_init(GSettings *settings);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]