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 
26 #include "alarm-applet.h"
27 #include "alarm-gconf.h"
28 #include "alarm-settings.h"
29 #include "alarm.h"
30 
31 /**
32  * Triggered on global changes to our gconf preference dir.
33  * We do this because we're interested in the events where
34  * an alarm directory is either added or deleted externally.
35  *
36  * When this happens we update our list of alarms.
37  */
38 static void
alarm_applet_gconf_global_change(GConfClient * client,guint cnxn_id,GConfEntry * entry,AlarmApplet * applet)39 alarm_applet_gconf_global_change (GConfClient  *client,
40 								  guint         cnxn_id,
41 								  GConfEntry   *entry,
42 								  AlarmApplet  *applet)
43 {
44 	Alarm *a;
45 	GString *str;
46 	GList *l;
47 	gchar *dir;
48 	gint id, i, len;
49 	gboolean found = FALSE;
50 
51 	g_debug ("GLOBAL_change: %s", entry->key);
52 
53 	/*
54 	 * We're only interested in the first part of the key matching
55 	 * {applet_gconf_pref_dir}/{something}
56 	 *
57 	 * Here we extract {something}
58 	 */
59 	dir = ALARM_GCONF_DIR;
60 	len = strlen (entry->key);
61 	str = g_string_new ("");
62 
63 	for (i = strlen(dir) + 1; i < len; i++) {
64 		if (entry->key[i] == '/')
65 			break;
66 
67 		str = g_string_append_c (str, entry->key[i]);
68 	}
69 
70 	//g_debug ("\tEXTRACTED: %s", str->str);
71 
72 	// Check if the key is valid
73 	id = alarm_gconf_dir_get_id (str->str);
74 
75 	if (id >= 0) {
76 		// Valid, probably an alarm which has been removed
77 		g_debug ("GLOBAL change ON alarm #%d", id);
78 
79         // Check if the alarm exists in our alarms list
80 		for (l = applet->alarms; l != NULL; l = l->next) {
81 			a = ALARM (l->data);
82 			if (a->id == id) {
83 				// FOUND
84 				found = TRUE;
85 				break;
86 			}
87 		}
88 
89 		if (found && entry->value == NULL) {
90 			// DELETED ALARM
91 			g_debug ("\tDELETE alarm #%d %p", id, a);
92 
93 			/* If there's a settings dialog open for this
94 			 * alarm, close it.
95 			 */
96             if (applet->settings_dialog->alarm == a) {
97 				alarm_settings_dialog_close (applet->settings_dialog);
98 			}
99 
100 			/*
101 			 * Remove from list
102 			 */
103 			alarm_applet_alarms_remove (applet, a);
104 
105 		} else if (!found && entry->value != NULL) {
106 			// ADDED ALARM
107 			/*
108 			 * Add to list
109 			 */
110 			a = alarm_new (ALARM_GCONF_DIR, id);
111 
112 			g_debug ("\tADD alarm #%d %p", id, a);
113 
114 			alarm_applet_alarms_add (applet, a);
115 
116 		} else if (found) {
117             //alarm_list_window_alarm_update (applet->list_window, a);
118         }
119 	}
120 
121 	g_string_free (str, TRUE);
122 }
123 
124 
125 /*
126  * }} GCONF CALLBACKS
127  */
128 
129 /*
130  * Init
131  */
132 void
alarm_applet_gconf_init(AlarmApplet * applet)133 alarm_applet_gconf_init (AlarmApplet *applet)
134 {
135 	GConfClient *client = gconf_client_get_default ();
136 
137 	gconf_client_add_dir (client, ALARM_GCONF_DIR, GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
138 
139 	/*
140 	 * Listen for changes to the alarms.
141 	 * We want to know when an alarm is added and removed.
142 	 */
143 	gconf_client_notify_add (
144 			client, ALARM_GCONF_DIR,
145 			(GConfClientNotifyFunc) alarm_applet_gconf_global_change,
146 			applet, NULL, NULL);
147 
148 }
149