~ubuntu-branches/debian/sid/alarm-clock-applet/sid

« back to all changes in this revision

Viewing changes to src/alarm-gconf.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-05-30 23:24:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090530232427-88on1j2ily4ajxdz
Tags: upstream-0.2.6
ImportĀ upstreamĀ versionĀ 0.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * alarm-gconf.c -- GConf routines
 
3
 * 
 
4
 * Copyright (C) 2007-2008 Johannes H. Jensen <joh@pseudoberries.com>
 
5
 * 
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program 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 this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 * 
 
20
 * Authors:
 
21
 *              Johannes H. Jensen <joh@pseudoberries.com>
 
22
 */
 
23
 
 
24
#include <time.h>
 
25
#include <panel-applet-gconf.h>
 
26
 
 
27
#include "alarm-applet.h"
 
28
#include "alarm-gconf.h"
 
29
#include "edit-alarm.h"
 
30
#include "alarm.h"
 
31
 
 
32
/*
 
33
 * LabelType enum to GConf string value map
 
34
 */
 
35
GConfEnumStringPair label_type_enum_map [] = {
 
36
        { LABEL_TYPE_TIME,              "alarm-time"  },
 
37
        { LABEL_TYPE_REMAIN,    "remaining-time"  },
 
38
        { 0, NULL }
 
39
};
 
40
 
 
41
/*
 
42
 * GCONF CALLBACKS {{
 
43
 */
 
44
 
 
45
void
 
46
alarm_applet_gconf_show_label_changed (GConfClient  *client,
 
47
                                                                           guint         cnxn_id,
 
48
                                                                           GConfEntry   *entry,
 
49
                                                                           AlarmApplet  *applet)
 
50
{
 
51
        g_debug ("show_label_changed");
 
52
        
 
53
        if (!entry->value || entry->value->type != GCONF_VALUE_BOOL)
 
54
                return;
 
55
        
 
56
        applet->show_label = gconf_value_get_bool (entry->value);
 
57
        
 
58
        g_object_set (applet->label, "visible", applet->show_label, NULL);
 
59
        
 
60
        if (applet->preferences_dialog != NULL) {
 
61
                pref_update_label_show (applet);
 
62
        }
 
63
}
 
64
 
 
65
void
 
66
alarm_applet_gconf_label_type_changed (GConfClient  *client,
 
67
                                                                          guint         cnxn_id,
 
68
                                                                          GConfEntry   *entry,
 
69
                                                                          AlarmApplet  *applet)
 
70
{
 
71
        g_debug ("label_type_changed");
 
72
        
 
73
        const gchar *tmp;
 
74
        
 
75
        if (!entry->value || entry->value->type != GCONF_VALUE_STRING)
 
76
                return;
 
77
        
 
78
        tmp = gconf_value_get_string (entry->value);
 
79
        if (tmp) {
 
80
                if (!gconf_string_to_enum (label_type_enum_map, tmp, (gint *)&(applet->label_type))) {
 
81
                        // No match, set to default
 
82
                        applet->label_type = DEF_LABEL_TYPE;
 
83
                }
 
84
                
 
85
                alarm_applet_label_update (applet);
 
86
        }
 
87
        
 
88
        if (applet->preferences_dialog != NULL) {
 
89
                pref_update_label_type (applet);
 
90
        }
 
91
}
 
92
 
 
93
 
 
94
/*
 
95
 * Triggered on global changes to our gconf preference dir.
 
96
 * We do this because we're interested in the events where
 
97
 * an alarm directory is either added or deleted externally.
 
98
 * 
 
99
 * When this happens we update our list of alarms.
 
100
 */
 
101
void
 
102
alarm_applet_gconf_global_change (GConfClient  *client,
 
103
                                                                  guint         cnxn_id,
 
104
                                                                  GConfEntry   *entry,
 
105
                                                                  AlarmApplet  *applet)
 
106
{
 
107
        Alarm *a;
 
108
        GString *str;
 
109
        GList *l;
 
110
        gchar *dir;
 
111
        gint id, i, len;
 
112
        gboolean found = FALSE;
 
113
        
 
114
        //g_debug ("GLOBAL_change: %s", entry->key);
 
115
        
 
116
        /*
 
117
         * We're only interested in the first part of the key matching
 
118
         * {applet_gconf_pref_dir}/{something}
 
119
         * 
 
120
         * Here we extract {something}
 
121
         */
 
122
        dir = ALARM_GCONF_DIR;
 
123
        len = strlen (entry->key);
 
124
        str = g_string_new ("");
 
125
        
 
126
        for (i = strlen(dir) + 1; i < len; i++) {
 
127
                if (entry->key[i] == '/')
 
128
                        break;
 
129
                
 
130
                str = g_string_append_c (str, entry->key[i]);
 
131
        }
 
132
        
 
133
        //g_debug ("\tEXTRACTED: %s", str->str);
 
134
        
 
135
        // Check if the key is valid
 
136
        id = alarm_gconf_dir_get_id (str->str);
 
137
        
 
138
        if (id >= 0) {
 
139
                // Valid, probably an alarm which has been removed
 
140
                g_debug ("GLOBAL change ON alarm #%d", id);
 
141
                
 
142
                // Check if the alarm exists in our alarms list
 
143
                for (l = applet->alarms; l != NULL; l = l->next) {
 
144
                        a = ALARM (l->data);
 
145
                        if (a->id == id) {
 
146
                                // FOUND
 
147
                                found = TRUE;
 
148
                                break;
 
149
                        }
 
150
                }
 
151
                
 
152
                if (found && entry->value == NULL) {
 
153
                        // DELETED ALARM
 
154
                        g_debug ("\tDELETE alarm #%d %p", id, a);
 
155
                        
 
156
                        /* If there's a settings dialog open for this
 
157
                         * alarm, destroy it.
 
158
                         */
 
159
                        AlarmSettingsDialog *sdialog = g_hash_table_lookup (applet->edit_alarm_dialogs, (gconstpointer)a->id);
 
160
                        if (sdialog) {
 
161
                                alarm_settings_dialog_close (sdialog);
 
162
                        }
 
163
                        
 
164
                        /*
 
165
                         * Remove from list
 
166
                         */
 
167
                        alarm_applet_alarms_remove (applet, a);
 
168
                        
 
169
                        // Update view
 
170
                        if (applet->list_alarms_dialog)
 
171
                                list_alarms_update (applet);
 
172
                        
 
173
                } else if (!found && entry->value != NULL) {
 
174
                        // ADDED ALARM
 
175
                        /*
 
176
                         * Add to list
 
177
                         */
 
178
                        a = alarm_new (ALARM_GCONF_DIR, id);
 
179
                        
 
180
                        g_debug ("\tADD alarm #%d %p", id, a);
 
181
                        
 
182
                        alarm_applet_alarms_add (applet, a);
 
183
                        
 
184
                        // Update view
 
185
                        if (applet->list_alarms_dialog)
 
186
                                list_alarms_update (applet);
 
187
                }
 
188
        }
 
189
        
 
190
        g_string_free (str, TRUE);
 
191
}
 
192
 
 
193
 
 
194
/*
 
195
 * }} GCONF CALLBACKS
 
196
 */
 
197
 
 
198
/*
 
199
 * Init
 
200
 */
 
201
void
 
202
alarm_applet_gconf_init (AlarmApplet *applet)
 
203
{
 
204
        GConfClient *client;
 
205
        gchar       *key;
 
206
 
 
207
        client = gconf_client_get_default ();
 
208
        
 
209
        key = panel_applet_gconf_get_full_key (PANEL_APPLET (applet->parent), KEY_SHOW_LABEL);
 
210
        applet->listeners [0] =
 
211
                gconf_client_notify_add (
 
212
                                client, key,
 
213
                                (GConfClientNotifyFunc) alarm_applet_gconf_show_label_changed,
 
214
                                applet, NULL, NULL);
 
215
        g_free (key);
 
216
        
 
217
        key = panel_applet_gconf_get_full_key (PANEL_APPLET (applet->parent), KEY_LABEL_TYPE);
 
218
        applet->listeners [1] =
 
219
                gconf_client_notify_add (
 
220
                                client, key,
 
221
                                (GConfClientNotifyFunc) alarm_applet_gconf_label_type_changed,
 
222
                                applet, NULL, NULL);
 
223
        g_free (key);
 
224
        
 
225
        /*
 
226
         * Listen for changes to the alarms.
 
227
         * We want to know when an alarm is added and removed.
 
228
         */
 
229
        applet->listeners [2] =
 
230
                gconf_client_notify_add (
 
231
                                client, ALARM_GCONF_DIR,
 
232
                                (GConfClientNotifyFunc) alarm_applet_gconf_global_change,
 
233
                                applet, NULL, NULL);
 
234
        
 
235
}
 
236
 
 
237
/* Load gconf values into applet.
 
238
 * We are very paranoid about gconf here. 
 
239
 * We can't rely on the schemas to exist, and so if we don't get any
 
240
 * defaults from gconf, we set them manually.
 
241
 * Not only that, but if some error occurs while setting the
 
242
 * defaults in gconf, we already have them copied locally. */
 
243
void
 
244
alarm_applet_gconf_load (AlarmApplet *applet)
 
245
{
 
246
        GConfClient *client;
 
247
        GConfValue      *value;
 
248
        gchar       *key;
 
249
        gchar           *tmp;
 
250
        
 
251
        client = gconf_client_get_default ();
 
252
        
 
253
        // SHOW_LABEL:
 
254
        key = panel_applet_gconf_get_full_key (PANEL_APPLET (applet->parent), KEY_SHOW_LABEL);
 
255
        value = gconf_client_get (client, key, NULL);
 
256
        if (value == NULL) {
 
257
                // Schema defaults not found
 
258
                applet->show_label = DEF_SHOW_LABEL;
 
259
                panel_applet_gconf_set_bool (applet->parent, KEY_SHOW_LABEL, DEF_SHOW_LABEL, NULL);
 
260
        } else {
 
261
                applet->show_label = gconf_value_get_bool (value);
 
262
                gconf_value_free (value);
 
263
        }
 
264
        g_free (key);
 
265
        
 
266
        
 
267
        // LABEL_TYPE:
 
268
        tmp = panel_applet_gconf_get_string (applet->parent, KEY_LABEL_TYPE, NULL);
 
269
        if (tmp == NULL || !gconf_string_to_enum (label_type_enum_map, tmp, (gint *)&(applet->label_type))) {
 
270
                // Schema defaults not found or unable to map
 
271
                applet->label_type = DEF_LABEL_TYPE;
 
272
                panel_applet_gconf_set_string (applet->parent, KEY_LABEL_TYPE, gconf_enum_to_string (label_type_enum_map, DEF_LABEL_TYPE), NULL);
 
273
        }
 
274
        g_free(tmp);
 
275
}