158
159
diary_storage_manager_query_async (self, queries[i++], NULL, NULL);
162
#ifdef ENABLE_ENCRYPTION
162
164
GIOChannel *cipher_io_channel;
163
165
GIOChannel *plain_io_channel;
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,
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]);
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);
358
361
g_set_error (error, DIARY_STORAGE_MANAGER_ERROR, DIARY_STORAGE_MANAGER_ERROR_ENCRYPTING,
365
gpgme_key_unref (gpgme_keys[0]);
366
gpgme_key_unref (gpgme_keys[0]); /* Unref twice as gpgme_signers_add refs it */
368
367
/* The operation will be completed in the idle function */
369
368
g_idle_add ((GSourceFunc) database_idle_cb, operation);
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);
382
get_encryption_key (void)
386
gchar *encryption_key;
388
encryption_key = gconf_client_get_string (diary->gconf_client, "/desktop/pgp/default_key", NULL);
389
if (encryption_key == NULL || encryption_key[0] == '\0')
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);
397
for (i = 0; key_parts[i] != NULL; i++) {
398
if (strcmp (key_parts[i], "openpgp") != 0)
399
encryption_key = key_parts[i];
401
g_free (key_parts[i]);
405
return encryption_key;
407
#endif /* ENABLE_ENCRYPTION */
375
410
diary_storage_manager_connect (DiaryStorageManager *self)
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) {
399
435
self->priv->decrypted = TRUE;
437
self->priv->decrypted = FALSE;
438
#endif /* ENABLE_ENCRYPTION */
401
440
/* Open the plain database */
402
441
if (sqlite3_open (self->priv->plain_filename, &(self->priv->connection)) != SQLITE_OK) {
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)
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);
441
for (i = 0; key_parts[i] != NULL; i++) {
442
if (strcmp (key_parts[i], "openpgp") != 0)
443
encryption_key = key_parts[i];
445
g_free (key_parts[i]);
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);
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. */