evolution-3.6.4/calendar/alarm-notify/config-data.c

No issues found

  1 /*
  2  * Evolution calendar - Configuration values for the alarm notification daemon
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  *
 18  * Authors:
 19  *		Federico Mena-Quintero <federico@ximian.com>
 20  *
 21  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 22  *
 23  */
 24 
 25 #ifdef HAVE_CONFIG_H
 26 #include <config.h>
 27 #endif
 28 
 29 #include <string.h>
 30 #include "config-data.h"
 31 
 32 /* Whether we have initied ourselves by reading
 33  * the data from the configuration engine. */
 34 static gboolean inited = FALSE;
 35 static GSettings *calendar_settings = NULL;
 36 
 37 /* Copied from ../calendar-config.c; returns whether the locale has 'am' and
 38  * 'pm' strings defined.
 39  */
 40 static gboolean
 41 locale_supports_12_hour_format (void)
 42 {
 43 	gchar s[16];
 44 	time_t t = 0;
 45 
 46 	strftime (s, sizeof s, "%p", gmtime (&t));
 47 	return s[0] != '\0';
 48 }
 49 
 50 static void
 51 do_cleanup (void)
 52 {
 53 	g_object_unref (calendar_settings);
 54 	calendar_settings = FALSE;
 55 
 56 	inited = FALSE;
 57 }
 58 
 59 /* Ensures that the configuration values have been read */
 60 static void
 61 ensure_inited (void)
 62 {
 63 	if (inited)
 64 		return;
 65 
 66 	inited = TRUE;
 67 
 68 	calendar_settings = g_settings_new ("org.gnome.evolution.calendar");
 69 
 70 	g_atexit ((GVoidFunc) do_cleanup);
 71 }
 72 
 73 icaltimezone *
 74 config_data_get_timezone (void)
 75 {
 76 	gchar *location;
 77 	icaltimezone *local_timezone;
 78 
 79 	ensure_inited ();
 80 
 81 	if (g_settings_get_boolean (calendar_settings, "use-system-timezone"))
 82 		location = e_cal_util_get_system_timezone_location ();
 83 	else {
 84 		location = g_settings_get_string (calendar_settings, "timezone");
 85 	}
 86 
 87 	if (location && location[0])
 88 		local_timezone = icaltimezone_get_builtin_timezone (location);
 89 	else
 90 		local_timezone = icaltimezone_get_utc_timezone ();
 91 
 92 	g_free (location);
 93 
 94 	return local_timezone;
 95 }
 96 
 97 gboolean
 98 config_data_get_24_hour_format (void)
 99 {
100 	ensure_inited ();
101 
102 	if (locale_supports_12_hour_format ()) {
103 		return g_settings_get_boolean (calendar_settings, "use-24hour-format");
104 	}
105 
106 	return TRUE;
107 }
108 
109 gboolean
110 config_data_get_notify_with_tray (void)
111 {
112 	ensure_inited ();
113 
114 	return g_settings_get_boolean (calendar_settings, "notify-with-tray");
115 }
116 
117 static void
118 source_written_cb (GObject *source_object,
119                    GAsyncResult *result,
120                    gpointer user_data)
121 {
122 	GError *error = NULL;
123 
124 	e_source_write_finish (E_SOURCE (source_object), result, &error);
125 
126 	if (error != NULL) {
127 		g_warning (
128 			"Failed to write source changes: %s",
129 			error->message);
130 		g_error_free (error);
131 	}
132 }
133 
134 /**
135  * config_data_set_last_notification_time:
136  * @t: A time value.
137  *
138  * Saves the last notification time so that it can be fetched the next time the
139  * alarm daemon is run.  This way the daemon can show alarms that should have
140  * triggered while it was not running.
141  **/
142 void
143 config_data_set_last_notification_time (ECalClient *cal,
144                                         time_t t)
145 {
146 	time_t current_t, now = time (NULL);
147 
148 	g_return_if_fail (t != -1);
149 
150 	if (cal != NULL) {
151 		ESource *source;
152 		ESourceAlarms *extension;
153 		GTimeVal tv = {0};
154 		const gchar *extension_name;
155 		gchar *iso8601;
156 
157 		source = e_client_get_source (E_CLIENT (cal));
158 		extension_name = E_SOURCE_EXTENSION_ALARMS;
159 		extension = e_source_get_extension (source, extension_name);
160 
161 		iso8601 = (gchar *) e_source_alarms_get_last_notified (extension);
162 		if (iso8601 != NULL)
163 			g_time_val_from_iso8601 (iso8601, &tv);
164 
165 		if (t > (time_t) tv.tv_sec || (time_t) tv.tv_sec > now) {
166 			tv.tv_sec = (glong) t;
167 			iso8601 = g_time_val_to_iso8601 (&tv);
168 			e_source_alarms_set_last_notified (extension, iso8601);
169 			g_free (iso8601);
170 
171 			e_source_write (source, NULL, source_written_cb, NULL);
172 		}
173 	}
174 
175 	/* we only store the new notification time if it is bigger
176 	 * than the already stored one */
177 	current_t = g_settings_get_int (calendar_settings, "last-notification-time");
178 	if (t > current_t || current_t > now)
179 		g_settings_set_int (calendar_settings, "last-notification-time", t);
180 }
181 
182 /**
183  * config_data_get_last_notification_time:
184  *
185  * Queries the last saved value for alarm notification times.
186  *
187  * Return value: The last saved value, or -1 if no value had been saved before.
188  **/
189 time_t
190 config_data_get_last_notification_time (ECalClient *cal)
191 {
192 	time_t value, now;
193 
194 	if (cal != NULL) {
195 		ESource *source;
196 		ESourceAlarms *extension;
197 		GTimeVal tmval = {0};
198 		const gchar *extension_name;
199 		const gchar *last_notified;
200 		time_t now, val;
201 
202 		source = e_client_get_source (E_CLIENT (cal));
203 		extension_name = E_SOURCE_EXTENSION_ALARMS;
204 
205 		if (!e_source_has_extension (source, extension_name))
206 			goto skip;
207 
208 		extension = e_source_get_extension (source, extension_name);
209 		last_notified = e_source_alarms_get_last_notified (extension);
210 
211 		if (last_notified == NULL || *last_notified == '\0')
212 			goto skip;
213 
214 		if (!g_time_val_from_iso8601 (last_notified, &tmval))
215 			goto skip;
216 
217 		now = time (NULL);
218 		val = (time_t) tmval.tv_sec;
219 
220 		if (val > now)
221 			val = now;
222 
223 		return val;
224 	}
225 
226 skip:
227 	value = g_settings_get_int (calendar_settings, "last-notification-time");
228 	now = time (NULL);
229 	if (value > now)
230 		value = now;
231 
232 	return value;
233 }
234 
235 /**
236  * config_data_save_blessed_program:
237  * @program: a program name
238  *
239  * Saves a program name as "blessed"
240  **/
241 void
242 config_data_save_blessed_program (const gchar *program)
243 {
244 	gchar **list;
245 	gint i;
246 	GPtrArray *array = g_ptr_array_new ();
247 
248 	list = g_settings_get_strv (calendar_settings, "notify-programs");
249 	for (i = 0; i < g_strv_length (list); i++)
250 		g_ptr_array_add (array, list[i]);
251 
252 	g_ptr_array_add (array, (gpointer) program);
253 	g_ptr_array_add (array, NULL);
254 
255 	g_settings_set_strv (
256 		calendar_settings, "notify-programs",
257 		(const gchar *const *) array->pdata);
258 
259 	g_strfreev (list);
260 	g_ptr_array_free (array, TRUE);
261 }
262 
263 /**
264  * config_data_is_blessed_program:
265  * @program: a program name
266  *
267  * Checks to see if a program is blessed
268  *
269  * Return value: TRUE if program is blessed, FALSE otherwise
270  **/
271 gboolean
272 config_data_is_blessed_program (const gchar *program)
273 {
274 	gchar **list;
275 	gint i = 0;
276 	gboolean found = FALSE;
277 
278 	list = g_settings_get_strv (calendar_settings, "notify-programs");
279 	if (!list)
280 		return FALSE;
281 
282 	while (list[i] != NULL) {
283 		if (!found)
284 			found = strcmp ((gchar *) list[i], program) == 0;
285 		i++;
286 	}
287 
288 	g_strfreev (list);
289 
290 	return found;
291 }
292 
293 static gboolean can_debug = FALSE;
294 static GStaticRecMutex rec_mutex = G_STATIC_REC_MUTEX_INIT;
295 
296 void
297 config_data_init_debugging (void)
298 {
299 	can_debug = g_getenv ("ALARMS_DEBUG") != NULL;
300 }
301 
302 /* returns whether started debugging;
303  * call config_data_stop_debugging() when started and you are done with it
304  */
305 gboolean
306 config_data_start_debugging (void)
307 {
308 	g_static_rec_mutex_lock (&rec_mutex);
309 
310 	if (can_debug)
311 		return TRUE;
312 
313 	g_static_rec_mutex_unlock (&rec_mutex);
314 
315 	return FALSE;
316 }
317 
318 void
319 config_data_stop_debugging (void)
320 {
321 	g_static_rec_mutex_unlock (&rec_mutex);
322 }