1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
4
* Copyright (C) Philip Withnall 2008 <philip@tecnocode.co.uk>
6
* Almanah is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* Almanah is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with Almanah. If not, see <http://www.gnu.org/licenses/>.
22
#include <glib/gi18n.h>
24
#ifdef ENABLE_ENCRYPTION
25
#define LIBCRYPTUI_API_SUBJECT_TO_CHANGE
26
#include <libcryptui/cryptui-key-combo.h>
27
#include <libcryptui/cryptui-keyset.h>
28
#include <libcryptui/cryptui.h>
30
#endif /* ENABLE_ENCRYPTION */
32
#include "preferences-dialog.h"
33
#include "interface.h"
35
#include "main-window.h"
37
static void almanah_preferences_dialog_init (AlmanahPreferencesDialog *self);
38
static void almanah_preferences_dialog_dispose (GObject *object);
39
#ifdef ENABLE_ENCRYPTION
40
static void pd_key_combo_changed_cb (GtkComboBox *combo_box, AlmanahPreferencesDialog *preferences_dialog);
41
static void pd_new_key_button_clicked_cb (GtkButton *button, AlmanahPreferencesDialog *preferences_dialog);
42
#endif /* ENABLE_ENCRYPTION */
43
#ifdef ENABLE_SPELL_CHECKING
44
static void pd_response_cb (GtkDialog *dialog, gint response_id, AlmanahPreferencesDialog *preferences_dialog);
45
static void spell_checking_enabled_notify_cb (GConfClient *client, guint connection_id, GConfEntry *entry, AlmanahPreferencesDialog *self);
46
static void pd_spell_checking_enabled_check_button_toggled_cb (GtkToggleButton *toggle_button, gpointer user_data);
47
#endif /* ENABLE_SPELL_CHECKING */
49
struct _AlmanahPreferencesDialogPrivate {
50
#ifdef ENABLE_ENCRYPTION
51
CryptUIKeyset *keyset;
52
CryptUIKeyStore *key_store;
53
GtkComboBox *key_combo;
54
#endif /* ENABLE_ENCRYPTION */
55
#ifdef ENABLE_SPELL_CHECKING
56
guint spell_checking_enabled_id;
57
GtkCheckButton *spell_checking_enabled_check_button;
58
#endif /* ENABLE_SPELL_CHECKING */
61
G_DEFINE_TYPE (AlmanahPreferencesDialog, almanah_preferences_dialog, GTK_TYPE_DIALOG)
62
#define ALMANAH_PREFERENCES_DIALOG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ALMANAH_TYPE_PREFERENCES_DIALOG, AlmanahPreferencesDialogPrivate))
65
almanah_preferences_dialog_class_init (AlmanahPreferencesDialogClass *klass)
67
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
68
g_type_class_add_private (klass, sizeof (AlmanahPreferencesDialogPrivate));
69
gobject_class->dispose = almanah_preferences_dialog_dispose;
73
almanah_preferences_dialog_init (AlmanahPreferencesDialog *self)
75
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_PREFERENCES_DIALOG, AlmanahPreferencesDialogPrivate);
77
g_signal_connect (self, "response", G_CALLBACK (pd_response_cb), self);
78
gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
79
gtk_window_set_modal (GTK_WINDOW (self), FALSE);
80
gtk_window_set_title (GTK_WINDOW (self), _("Almanah Preferences"));
81
gtk_widget_set_size_request (GTK_WIDGET (self), 400, -1);
82
gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
86
almanah_preferences_dialog_dispose (GObject *object)
88
AlmanahPreferencesDialogPrivate *priv = ALMANAH_PREFERENCES_DIALOG (object)->priv;
90
#ifdef ENABLE_ENCRYPTION
91
if (priv->keyset != NULL) {
92
g_object_unref (priv->keyset);
96
if (priv->key_store != NULL) {
97
g_object_unref (priv->key_store);
98
priv->key_store = NULL;
100
#endif /* ENABLE_ENCRYPTION */
101
#ifdef ENABLE_SPELL_CHECKING
102
gconf_client_notify_remove (almanah->gconf_client, priv->spell_checking_enabled_id);
103
#endif /* ENABLE_SPELL_CHECKING */
105
/* Chain up to the parent class */
106
G_OBJECT_CLASS (almanah_preferences_dialog_parent_class)->dispose (object);
109
AlmanahPreferencesDialog *
110
almanah_preferences_dialog_new (void)
114
#ifdef ENABLE_ENCRYPTION
115
GtkWidget *label, *button;
116
AtkObject *a11y_label, *a11y_key_combo;
118
#endif /* ENABLE_ENCRYPTION */
119
AlmanahPreferencesDialog *preferences_dialog;
120
AlmanahPreferencesDialogPrivate *priv;
121
GError *error = NULL;
122
const gchar *interface_filename = almanah_get_interface_filename ();
123
const gchar *object_names[] = {
124
"almanah_preferences_dialog",
128
builder = gtk_builder_new ();
130
if (gtk_builder_add_objects_from_file (builder, interface_filename, (gchar**) object_names, &error) == FALSE) {
132
GtkWidget *dialog = gtk_message_dialog_new (NULL,
136
_("UI file \"%s\" could not be loaded"), interface_filename);
137
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
138
gtk_dialog_run (GTK_DIALOG (dialog));
139
gtk_widget_destroy (dialog);
141
g_error_free (error);
142
g_object_unref (builder);
147
gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
148
preferences_dialog = ALMANAH_PREFERENCES_DIALOG (gtk_builder_get_object (builder, "almanah_preferences_dialog"));
149
gtk_builder_connect_signals (builder, preferences_dialog);
151
if (preferences_dialog == NULL) {
152
g_object_unref (builder);
156
priv = ALMANAH_PREFERENCES_DIALOG (preferences_dialog)->priv;
157
table = GTK_TABLE (gtk_builder_get_object (builder, "almanah_pd_table"));
159
#ifdef ENABLE_ENCRYPTION
160
/* Grab our child widgets */
161
label = gtk_label_new (_("Encryption Key"));
162
almanah_interface_embolden_label (GTK_LABEL (label));
163
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
164
gtk_table_attach (table, label, 1, 2, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
166
priv->keyset = cryptui_keyset_new ("openpgp", FALSE);
167
priv->key_store = cryptui_key_store_new (priv->keyset, FALSE, _("None (don't encrypt)"));
168
priv->key_combo = cryptui_key_combo_new (priv->key_store);
170
gtk_table_attach_defaults (table, GTK_WIDGET (priv->key_combo), 2, 3, 1, 2);
172
/* Set up the accessibility relationships */
173
a11y_label = gtk_widget_get_accessible (GTK_WIDGET (label));
174
a11y_key_combo = gtk_widget_get_accessible (GTK_WIDGET (priv->key_combo));
175
atk_object_add_relationship (a11y_label, ATK_RELATION_LABEL_FOR, a11y_key_combo);
176
atk_object_add_relationship (a11y_key_combo, ATK_RELATION_LABELLED_BY, a11y_label);
178
/* Set the selected key combo value */
179
key = gconf_client_get_string (almanah->gconf_client, ENCRYPTION_KEY_GCONF_PATH, NULL);
180
if (key != NULL && *key == '\0') {
185
cryptui_key_combo_set_key (priv->key_combo, key);
188
g_signal_connect (priv->key_combo, "changed", G_CALLBACK (pd_key_combo_changed_cb), preferences_dialog);
190
button = gtk_button_new_with_mnemonic (_("New _Key"));
191
gtk_table_attach (table, button, 3, 4, 1, 2, GTK_FILL, GTK_FILL, 0, 0);
192
g_signal_connect (button, "clicked", G_CALLBACK (pd_new_key_button_clicked_cb), preferences_dialog);
193
#endif /* ENABLE_ENCRYPTION */
195
#ifdef ENABLE_SPELL_CHECKING
196
/* Set up the "Enable spell checking" check button */
197
priv->spell_checking_enabled_check_button = GTK_CHECK_BUTTON (gtk_check_button_new_with_mnemonic (_("Enable _spell checking")));
198
gtk_table_attach_defaults (table, GTK_WIDGET (priv->spell_checking_enabled_check_button), 1, 4, 2, 3);
200
spell_checking_enabled_notify_cb (NULL, 0, NULL, preferences_dialog);
201
g_signal_connect (priv->spell_checking_enabled_check_button, "toggled", G_CALLBACK (pd_spell_checking_enabled_check_button_toggled_cb), preferences_dialog);
202
priv->spell_checking_enabled_id = gconf_client_notify_add (almanah->gconf_client, "/apps/almanah/spell_checking_enabled",
203
(GConfClientNotifyFunc) spell_checking_enabled_notify_cb, preferences_dialog,
205
#endif /* ENABLE_SPELL_CHECKING */
207
g_object_unref (builder);
209
return preferences_dialog;
212
#ifdef ENABLE_ENCRYPTION
214
pd_key_combo_changed_cb (GtkComboBox *combo_box, AlmanahPreferencesDialog *preferences_dialog)
217
GError *error = NULL;
219
/* Save the new encryption key to GConf */
220
key = cryptui_key_combo_get_key (preferences_dialog->priv->key_combo);
224
if (gconf_client_set_string (almanah->gconf_client, ENCRYPTION_KEY_GCONF_PATH, key, &error) == FALSE) {
225
GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (almanah->preferences_dialog),
226
GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
227
_("Error saving the encryption key"));
228
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
229
gtk_dialog_run (GTK_DIALOG (dialog));
230
gtk_widget_destroy (dialog);
232
g_error_free (error);
237
pd_new_key_button_clicked_cb (GtkButton *button, AlmanahPreferencesDialog *preferences_dialog)
239
/* NOTE: pilfered from cryptui_need_to_get_keys */
240
gchar *argv[2] = { "seahorse", NULL };
241
GError *error = NULL;
243
if (g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error) == FALSE) {
244
GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (almanah->preferences_dialog),
245
GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
246
_("Error opening Seahorse"));
247
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
248
gtk_dialog_run (GTK_DIALOG (dialog));
249
gtk_widget_destroy (dialog);
251
g_error_free (error);
254
#endif /* ENABLE_ENCRYPTION */
257
pd_response_cb (GtkDialog *dialog, gint response_id, AlmanahPreferencesDialog *preferences_dialog)
259
gtk_widget_hide_all (GTK_WIDGET (dialog));
263
spell_checking_enabled_notify_cb (GConfClient *client, guint connection_id, GConfEntry *entry, AlmanahPreferencesDialog *self)
267
enabled = gconf_client_get_bool (almanah->gconf_client, "/apps/almanah/spell_checking_enabled", NULL);
270
g_debug ("spell_checking_enabled_notify_cb called with %u.", enabled);
272
g_signal_handlers_block_by_func (self->priv->spell_checking_enabled_check_button, pd_spell_checking_enabled_check_button_toggled_cb, self);
273
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->priv->spell_checking_enabled_check_button), enabled);
274
g_signal_handlers_unblock_by_func (self->priv->spell_checking_enabled_check_button, pd_spell_checking_enabled_check_button_toggled_cb, self);
276
if (enabled == TRUE) {
277
GError *error = NULL;
279
almanah_main_window_enable_spell_checking (ALMANAH_MAIN_WINDOW (almanah->main_window), &error);
282
GtkWidget *dialog = gtk_message_dialog_new (NULL,
283
GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
284
_("Spelling checker could not be initialized"));
285
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
286
gtk_dialog_run (GTK_DIALOG (dialog));
287
gtk_widget_destroy (dialog);
289
g_error_free (error);
292
almanah_main_window_disable_spell_checking (ALMANAH_MAIN_WINDOW (almanah->main_window));
297
pd_spell_checking_enabled_check_button_toggled_cb (GtkToggleButton *toggle_button, gpointer user_data)
299
gconf_client_set_bool (almanah->gconf_client, "/apps/almanah/spell_checking_enabled",
300
gtk_toggle_button_get_active (toggle_button), NULL);