~ubuntu-branches/ubuntu/oneiric/almanah/oneiric

« back to all changes in this revision

Viewing changes to src/preferences-dialog.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2009-05-16 15:49:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090516154921-2uhvlj8btb4qygmd
Tags: 0.6.1-0ubuntu1
* New upstream release (LP: #368078)
* debian/control:
  - Bump Standards-Version to 3.8.1
  - Add intltool, libedataserver1.2-dev, libedataserverui1.2-dev, 
    libecal1.2-dev, libcryptui-dev as build dependency
  - Remove build dependency on autotools-dev and chrpath
* debian/{control/compat/rules}: Move to mimalistic dh7 style
* Update debian/watch
* Update copyright information

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/*
 
3
 * Almanah
 
4
 * Copyright (C) Philip Withnall 2008 <philip@tecnocode.co.uk>
 
5
 * 
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
#include <glib.h>
 
22
#include <glib/gi18n.h>
 
23
#include <gtk/gtk.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>
 
29
#include <atk/atk.h>
 
30
#endif /* ENABLE_ENCRYPTION */
 
31
 
 
32
#include "preferences-dialog.h"
 
33
#include "interface.h"
 
34
#include "main.h"
 
35
#include "main-window.h"
 
36
 
 
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 */
 
48
 
 
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 */
 
59
};
 
60
 
 
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))
 
63
 
 
64
static void
 
65
almanah_preferences_dialog_class_init (AlmanahPreferencesDialogClass *klass)
 
66
{
 
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;
 
70
}
 
71
 
 
72
static void
 
73
almanah_preferences_dialog_init (AlmanahPreferencesDialog *self)
 
74
{
 
75
        self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, ALMANAH_TYPE_PREFERENCES_DIALOG, AlmanahPreferencesDialogPrivate);
 
76
 
 
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);
 
83
}
 
84
 
 
85
static void
 
86
almanah_preferences_dialog_dispose (GObject *object)
 
87
{
 
88
        AlmanahPreferencesDialogPrivate *priv = ALMANAH_PREFERENCES_DIALOG (object)->priv;
 
89
 
 
90
#ifdef ENABLE_ENCRYPTION
 
91
        if (priv->keyset != NULL) {
 
92
                g_object_unref (priv->keyset);
 
93
                priv->keyset = NULL;
 
94
        }
 
95
 
 
96
        if (priv->key_store != NULL) {
 
97
                g_object_unref (priv->key_store);
 
98
                priv->key_store = NULL;
 
99
        }
 
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 */
 
104
 
 
105
        /* Chain up to the parent class */
 
106
        G_OBJECT_CLASS (almanah_preferences_dialog_parent_class)->dispose (object);
 
107
}
 
108
 
 
109
AlmanahPreferencesDialog *
 
110
almanah_preferences_dialog_new (void)
 
111
{
 
112
        GtkBuilder *builder;
 
113
        GtkTable *table;
 
114
#ifdef ENABLE_ENCRYPTION
 
115
        GtkWidget *label, *button;
 
116
        AtkObject *a11y_label, *a11y_key_combo;
 
117
        gchar *key;
 
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",
 
125
                NULL
 
126
        };
 
127
 
 
128
        builder = gtk_builder_new ();
 
129
 
 
130
        if (gtk_builder_add_objects_from_file (builder, interface_filename, (gchar**) object_names, &error) == FALSE) {
 
131
                /* Show an error */
 
132
                GtkWidget *dialog = gtk_message_dialog_new (NULL,
 
133
                                GTK_DIALOG_MODAL,
 
134
                                GTK_MESSAGE_ERROR,
 
135
                                GTK_BUTTONS_OK,
 
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);
 
140
 
 
141
                g_error_free (error);
 
142
                g_object_unref (builder);
 
143
 
 
144
                return NULL;
 
145
        }
 
146
 
 
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);
 
150
 
 
151
        if (preferences_dialog == NULL) {
 
152
                g_object_unref (builder);
 
153
                return NULL;
 
154
        }
 
155
 
 
156
        priv = ALMANAH_PREFERENCES_DIALOG (preferences_dialog)->priv;
 
157
        table = GTK_TABLE (gtk_builder_get_object (builder, "almanah_pd_table"));
 
158
 
 
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);
 
165
 
 
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);
 
169
 
 
170
        gtk_table_attach_defaults (table, GTK_WIDGET (priv->key_combo), 2, 3, 1, 2);
 
171
 
 
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);
 
177
 
 
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') {
 
181
                g_free (key);
 
182
                key = NULL;
 
183
        }
 
184
 
 
185
        cryptui_key_combo_set_key (priv->key_combo, key);
 
186
        g_free (key);
 
187
 
 
188
        g_signal_connect (priv->key_combo, "changed", G_CALLBACK (pd_key_combo_changed_cb), preferences_dialog);
 
189
 
 
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 */
 
194
 
 
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);
 
199
 
 
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,
 
204
                                                                   NULL, NULL);
 
205
#endif /* ENABLE_SPELL_CHECKING */
 
206
 
 
207
        g_object_unref (builder);
 
208
 
 
209
        return preferences_dialog;
 
210
}
 
211
 
 
212
#ifdef ENABLE_ENCRYPTION
 
213
static void
 
214
pd_key_combo_changed_cb (GtkComboBox *combo_box, AlmanahPreferencesDialog *preferences_dialog)
 
215
{
 
216
        const gchar *key;
 
217
        GError *error = NULL;
 
218
 
 
219
        /* Save the new encryption key to GConf */
 
220
        key = cryptui_key_combo_get_key (preferences_dialog->priv->key_combo);
 
221
        if (key == NULL)
 
222
                key = "";
 
223
 
 
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);
 
231
 
 
232
                g_error_free (error);
 
233
        }
 
234
}
 
235
 
 
236
static void
 
237
pd_new_key_button_clicked_cb (GtkButton *button, AlmanahPreferencesDialog *preferences_dialog)
 
238
{
 
239
        /* NOTE: pilfered from cryptui_need_to_get_keys */
 
240
        gchar *argv[2] = { "seahorse", NULL };
 
241
        GError *error = NULL;
 
242
 
 
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);
 
250
 
 
251
                g_error_free (error);
 
252
        }
 
253
}
 
254
#endif /* ENABLE_ENCRYPTION */
 
255
 
 
256
static void
 
257
pd_response_cb (GtkDialog *dialog, gint response_id, AlmanahPreferencesDialog *preferences_dialog)
 
258
{
 
259
        gtk_widget_hide_all (GTK_WIDGET (dialog));
 
260
}
 
261
 
 
262
static void
 
263
spell_checking_enabled_notify_cb (GConfClient *client, guint connection_id, GConfEntry *entry, AlmanahPreferencesDialog *self)
 
264
{
 
265
        gboolean enabled;
 
266
 
 
267
        enabled = gconf_client_get_bool (almanah->gconf_client, "/apps/almanah/spell_checking_enabled", NULL);
 
268
 
 
269
        if (almanah->debug)
 
270
                g_debug ("spell_checking_enabled_notify_cb called with %u.", enabled);
 
271
 
 
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);
 
275
 
 
276
        if (enabled == TRUE) {
 
277
                GError *error = NULL;
 
278
 
 
279
                almanah_main_window_enable_spell_checking (ALMANAH_MAIN_WINDOW (almanah->main_window), &error);
 
280
 
 
281
                if (error != NULL) {
 
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);
 
288
 
 
289
                        g_error_free (error);
 
290
                }
 
291
        } else {
 
292
                almanah_main_window_disable_spell_checking (ALMANAH_MAIN_WINDOW (almanah->main_window));
 
293
        }
 
294
}
 
295
 
 
296
static void
 
297
pd_spell_checking_enabled_check_button_toggled_cb (GtkToggleButton *toggle_button, gpointer user_data)
 
298
{
 
299
        gconf_client_set_bool (almanah->gconf_client, "/apps/almanah/spell_checking_enabled",
 
300
                               gtk_toggle_button_get_active (toggle_button), NULL);
 
301
}