~drbob/+junk/diary-main

« back to all changes in this revision

Viewing changes to src/storage-manager.c

  • Committer: Philip Withnall
  • Date: 2008-05-20 22:03:22 UTC
  • Revision ID: philip@tecnocode.co.uk-20080520220322-juxtwwdrosxq5v9u
2008-05-20  Philip Withnall  <philip@tecnocode.co.uk>

        * configure.ac:
        * src/storage-manager.c: Made encrypted database support optional,
        and cleaned up the code a little.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include "interface.h"
31
31
#include "link.h"
32
32
#include "storage-manager.h"
 
33
#include "config.h"
33
34
 
34
35
static void diary_storage_manager_init (DiaryStorageManager *self);
35
36
static void diary_storage_manager_finalize (GObject *object);
158
159
                diary_storage_manager_query_async (self, queries[i++], NULL, NULL);
159
160
}
160
161
 
 
162
#ifdef ENABLE_ENCRYPTION
161
163
typedef struct {
162
164
        GIOChannel *cipher_io_channel;
163
165
        GIOChannel *plain_io_channel;
258
260
                g_io_channel_unref (operation->plain_io_channel);
259
261
        }
260
262
 
 
263
        gpgme_signers_clear (operation->context);
261
264
        gpgme_release (operation->context);
262
265
        g_free (operation);
263
266
}
331
334
        }
332
335
 
333
336
        /* Set up signing and the recipient */
334
 
        gpgme_error = gpgme_get_key (operation->context, encryption_key, &(gpgme_keys[0]), FALSE);
 
337
        gpgme_error = gpgme_get_key (operation->context, encryption_key, &gpgme_keys[0], FALSE);
335
338
        if (gpgme_error != GPG_ERR_NO_ERROR || gpgme_keys[0] == NULL) {
336
339
                cipher_operation_free (operation);
337
340
                g_set_error (error, DIARY_STORAGE_MANAGER_ERROR, DIARY_STORAGE_MANAGER_ERROR_GETTING_KEY,
350
353
 
351
354
        /* Encrypt and sign! */
352
355
        gpgme_error = gpgme_op_encrypt_sign_start (operation->context, gpgme_keys, 0, operation->gpgme_plain, operation->gpgme_cipher);
 
356
        gpgme_key_unref (gpgme_keys[0]);
 
357
 
353
358
        if (gpgme_error != GPG_ERR_NO_ERROR) {
354
 
                gpgme_key_unref (gpgme_keys[0]);
355
 
                gpgme_key_unref (gpgme_keys[0]); /* Unref twice as gpgme_signers_add refs it */
356
359
                cipher_operation_free (operation);
357
360
 
358
361
                g_set_error (error, DIARY_STORAGE_MANAGER_ERROR, DIARY_STORAGE_MANAGER_ERROR_ENCRYPTING,
361
364
                return FALSE;
362
365
        }
363
366
 
364
 
        /* Clean up */
365
 
        gpgme_key_unref (gpgme_keys[0]);
366
 
        gpgme_key_unref (gpgme_keys[0]); /* Unref twice as gpgme_signers_add refs it */
367
 
 
368
367
        /* The operation will be completed in the idle function */
369
368
        g_idle_add ((GSourceFunc) database_idle_cb, operation);
370
369
 
 
370
        /* Delete the plain file and wait for the idle function to quit us */
 
371
        if (g_unlink (self->priv->plain_filename) != 0) {
 
372
                g_set_error (error, DIARY_STORAGE_MANAGER_ERROR, DIARY_STORAGE_MANAGER_ERROR_ENCRYPTING,
 
373
                             _("Could not delete plain database file \"%s\"."),
 
374
                             self->priv->plain_filename);
 
375
                return FALSE;
 
376
        }
 
377
 
371
378
        return TRUE;
372
379
}
373
380
 
 
381
static gchar *
 
382
get_encryption_key (void)
 
383
{
 
384
        gchar **key_parts;
 
385
        guint i;
 
386
        gchar *encryption_key;
 
387
 
 
388
        encryption_key = gconf_client_get_string (diary->gconf_client, "/desktop/pgp/default_key", NULL);
 
389
        if (encryption_key == NULL || encryption_key[0] == '\0')
 
390
                return NULL;
 
391
 
 
392
        /* Key is generally in the form openpgp:FOOBARKEY, and GPGME doesn't
 
393
         * like the openpgp: prefix, so it must be removed. */
 
394
        key_parts = g_strsplit (encryption_key, ":", 2);
 
395
        g_free (encryption_key);
 
396
 
 
397
        for (i = 0; key_parts[i] != NULL; i++) {
 
398
                if (strcmp (key_parts[i], "openpgp") != 0)
 
399
                        encryption_key = key_parts[i];
 
400
                else
 
401
                        g_free (key_parts[i]);
 
402
        }
 
403
        g_free (key_parts);
 
404
 
 
405
        return encryption_key;
 
406
}
 
407
#endif /* ENABLE_ENCRYPTION */
 
408
 
374
409
void
375
410
diary_storage_manager_connect (DiaryStorageManager *self)
376
411
{
 
412
#ifdef ENABLE_ENCRYPTION
377
413
        /* If we're decrypting, don't bother if the cipher file doesn't exist
378
414
         * (i.e. the database hasn't yet been created). */
379
415
        if (g_file_test (self->priv->filename, G_FILE_TEST_IS_REGULAR) == TRUE) {
397
433
        }
398
434
 
399
435
        self->priv->decrypted = TRUE;
 
436
#else
 
437
        self->priv->decrypted = FALSE;
 
438
#endif /* ENABLE_ENCRYPTION */
400
439
 
401
440
        /* Open the plain database */
402
441
        if (sqlite3_open (self->priv->plain_filename, &(self->priv->connection)) != SQLITE_OK) {
415
454
void
416
455
diary_storage_manager_disconnect (DiaryStorageManager *self)
417
456
{
 
457
#ifdef ENABLE_ENCRYPTION
418
458
        gchar *encryption_key;
419
459
        GError *error = NULL;
420
 
        gchar **key_parts;
421
 
        guint i;
 
460
#endif /* ENABLE_ENCRYPTION */
422
461
 
423
462
        /* Close the DB connection */
424
463
        sqlite3_close (self->priv->connection);
429
468
                return;
430
469
        }
431
470
 
432
 
        encryption_key = gconf_client_get_string (diary->gconf_client, "/desktop/pgp/default_key", NULL);
433
 
        if (encryption_key == NULL || encryption_key[0] == '\0')
 
471
#ifdef ENABLE_ENCRYPTION
 
472
        encryption_key = get_encryption_key ();
 
473
        if (encryption_key == NULL)
434
474
                return;
435
475
 
436
 
        /* Key is generally in the form openpgp:FOOBARKEY, and GPGME doesn't
437
 
         * like the openpgp: prefix, so it must be removed. */
438
 
        key_parts = g_strsplit (encryption_key, ":", 2);
439
 
        g_free (encryption_key);
440
 
 
441
 
        for (i = 0; key_parts[i] != NULL; i++) {
442
 
                if (strcmp (key_parts[i], "openpgp") != 0)
443
 
                        encryption_key = key_parts[i];
444
 
                else
445
 
                        g_free (key_parts[i]);
446
 
        }
447
 
        g_free (key_parts);
448
 
 
449
476
        /* Encrypt the plain DB file */
450
 
        if (encrypt_database (self, encryption_key, &error) == TRUE) {
451
 
                /* Delete the plain file and wait for the idle function to quit us */
452
 
                if (g_unlink (self->priv->plain_filename) != 0) {
453
 
                        gchar *error_message = g_strdup_printf (_("Could not delete plain database file \"%s\"."), self->priv->plain_filename);
454
 
                        diary_interface_error (error_message, diary->main_window);
455
 
                        g_free (error_message);
456
 
                }
457
 
        } else {
 
477
        if (encrypt_database (self, encryption_key, &error) != TRUE) {
458
478
                if (error->code == DIARY_STORAGE_MANAGER_ERROR_GETTING_KEY) {
459
479
                        /* Log an error about being unable to get the key
460
480
                         * then continue without encrypting. */
471
491
        }
472
492
 
473
493
        g_free (encryption_key);
 
494
#endif /* ENABLE_ENCRYPTION */
474
495
}
475
496
 
476
497
DiaryQueryResults *