1 /*
2  * alarm-actions.h -- Alarm actions
3  *
4  * Copyright (C) 2010 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 "alarm-actions.h"
25 #include "alarm-applet.h"
26 #include "alarm-list-window.h"
27 
28 #define GET_ACTION(name) GTK_ACTION (gtk_builder_get_object (builder, (name)))
29 
30 /**
31  * Initialize the various actions
32  */
33 void
alarm_applet_actions_init(AlarmApplet * applet)34 alarm_applet_actions_init (AlarmApplet *applet)
35 {
36     GtkBuilder *builder = applet->ui;
37 
38     // Actions on one alarm
39     applet->actions_alarm = gtk_action_group_new ("alarm");
40 
41     applet->action_edit = GET_ACTION ("edit-action");
42     applet->action_delete = GET_ACTION ("delete-action");
43     applet->action_enabled = GTK_TOGGLE_ACTION (GET_ACTION ("enabled-action"));
44     applet->action_stop = GET_ACTION ("stop-action");
45     applet->action_snooze = GET_ACTION ("snooze-action");
46 
47     gtk_action_group_add_action (applet->actions_alarm, applet->action_edit);
48     gtk_action_group_add_action (applet->actions_alarm, applet->action_delete);
49     gtk_action_group_add_action (applet->actions_alarm, GTK_ACTION (applet->action_enabled));
50     gtk_action_group_add_action (applet->actions_alarm, applet->action_stop);
51     gtk_action_group_add_action (applet->actions_alarm, applet->action_snooze);
52 
53 
54 
55     // Global actions
56     applet->actions_global = gtk_action_group_new ("global");
57 
58     applet->action_new = GET_ACTION ("new-action");
59     applet->action_stop_all = GET_ACTION ("stop-all-action");
60     applet->action_snooze_all = GET_ACTION ("snooze-all-action");
61 
62     applet->action_toggle_list_win = GTK_TOGGLE_ACTION (GET_ACTION ("toggle-list-win-action"));
63     gtk_action_set_accel_group (GTK_ACTION (applet->action_toggle_list_win),
64         applet->list_window->accel_group);
65 
66     applet->action_toggle_autostart = GTK_TOGGLE_ACTION (GET_ACTION ("autostart-action"));
67     applet->action_toggle_show_label = GTK_TOGGLE_ACTION (GET_ACTION ("show-label-action"));
68 
69     gtk_action_group_add_action (applet->actions_global, applet->action_new);
70     gtk_action_group_add_action (applet->actions_global, applet->action_stop_all);
71     gtk_action_group_add_action (applet->actions_global, applet->action_snooze_all);
72     gtk_action_group_add_action_with_accel (applet->actions_global,
73         GTK_ACTION (applet->action_toggle_list_win), "Escape");
74     gtk_action_group_add_action (applet->actions_global, GTK_ACTION (applet->action_toggle_autostart));
75     gtk_action_group_add_action (applet->actions_global, GTK_ACTION (applet->action_toggle_show_label));
76 
77     gtk_action_connect_accelerator (GTK_ACTION (applet->action_toggle_list_win));
78 
79     // Update actions
80     alarm_applet_actions_update_sensitive (applet);
81 }
82 
83 
84 //
85 // SINGLE ALARM ACTIONS:
86 //
87 
88 /**
89  * Edit alarm action
90  */
91 void
alarm_action_edit(GtkAction * action,gpointer data)92 alarm_action_edit (GtkAction *action, gpointer data)
93 {
94     AlarmApplet *applet = (AlarmApplet *)data;
95     AlarmListWindow *list_window = applet->list_window;
96 	Alarm *a = alarm_list_window_get_selected_alarm (list_window);
97 
98 	if (!a) {
99 		// No alarms selected
100 		return;
101 	}
102 
103     g_debug ("AlarmAction: edit '%s'", a->message);
104 
105 	// Stop alarm
106     alarm_clear (a);
107 
108     // Show settings dialog for alarm
109     alarm_settings_dialog_show (applet->settings_dialog, a);
110 }
111 
112 /**
113  * Delete alarm action
114  */
115 void
alarm_action_delete(GtkAction * action,gpointer data)116 alarm_action_delete (GtkAction *action, gpointer data)
117 {
118     AlarmApplet *applet = (AlarmApplet *)data;
119     AlarmListWindow *list_window = applet->list_window;
120     AlarmSettingsDialog *sdialog = applet->settings_dialog;
121 
122 	Alarm *a = alarm_list_window_get_selected_alarm (list_window);
123 
124 	if (!a) {
125 		// No alarms selected
126 		return;
127 	}
128 
129     g_debug ("AlarmAction: delete '%s'", a->message);
130 
131     // If there's a settings dialog open for this alarm, close it.
132     if (sdialog->alarm == a) {
133         alarm_settings_dialog_close (sdialog);
134     }
135 
136     // Disable, clear and delete alarm
137     alarm_disable (a);
138     alarm_clear (a);
139     alarm_delete (a);
140 
141     // Remove from applet list
142     alarm_applet_alarms_remove (applet, a);
143 }
144 
145 /**
146  * Enable alarm action
147  */
148 void
alarm_action_enabled(GtkToggleAction * action,gpointer data)149 alarm_action_enabled (GtkToggleAction *action, gpointer data)
150 {
151     AlarmApplet *applet = (AlarmApplet *)data;
152     AlarmListWindow *list_window = applet->list_window;
153 	Alarm *a = alarm_list_window_get_selected_alarm (list_window);
154     gboolean active = gtk_toggle_action_get_active(action);
155 
156 	if (!a || a->active == active) {
157 		// No alarms selected or no change
158 		return;
159 	}
160 
161     g_debug ("AlarmAction: enabled(%d) '%s'", active, a->message);
162 
163     alarm_set_enabled (a, active);
164 }
165 
166 /**
167  * Update enabled action state
168  */
169 void
alarm_action_update_enabled(AlarmApplet * applet)170 alarm_action_update_enabled (AlarmApplet *applet)
171 {
172     Alarm *a = alarm_list_window_get_selected_alarm (applet->list_window);
173     gboolean active = gtk_toggle_action_get_active(applet->action_enabled);
174 
175 	if (!a || a->active == active) {
176 		// No alarms selected or no change
177 		return;
178 	}
179 
180     gtk_toggle_action_set_active (applet->action_enabled, a->active);
181 }
182 
183 /**
184  * Stop alarm action
185  */
186 void
alarm_action_stop(GtkAction * action,gpointer data)187 alarm_action_stop (GtkAction *action, gpointer data)
188 {
189     AlarmApplet *applet = (AlarmApplet *)data;
190     AlarmListWindow *list_window = applet->list_window;
191     Alarm *a;
192 
193     if ((a = alarm_list_window_get_selected_alarm (list_window))) {
194         g_debug ("AlarmAction: stop '%s'", a->message);
195 
196         alarm_clear (a);
197     }
198 }
199 
200 /**
201  * Snooze alarm action
202  */
203 void
alarm_action_snooze(GtkAction * action,gpointer data)204 alarm_action_snooze (GtkAction *action, gpointer data)
205 {
206     AlarmApplet *applet = (AlarmApplet *)data;
207     AlarmListWindow *list_window = applet->list_window;
208     Alarm *a;
209 
210     if ((a = alarm_list_window_get_selected_alarm (list_window))) {
211         g_debug ("AlarmAction: snooze '%s'", a->message);
212 
213         alarm_applet_alarm_snooze (applet, a);
214     }
215 }
216 
217 
218 //
219 // GLOBAL ACTIONS
220 //
221 
222 /**
223  * New alarm action
224  */
225 void
alarm_action_new(GtkAction * action,gpointer data)226 alarm_action_new (GtkAction *action, gpointer data)
227 {
228     AlarmApplet *applet = (AlarmApplet *)data;
229     AlarmListWindow *list_window = applet->list_window;
230     Alarm *alarm;
231 	AlarmListEntry *entry;
232     GtkTreeIter iter;
233     GtkTreeSelection *selection;
234 
235     g_debug ("AlarmAction: new");
236 
237 	// Create new alarm, will fall back to defaults.
238 	alarm = alarm_new (ALARM_GCONF_DIR, -1);
239 
240 	// Set first sound / app in list
241 	if (applet->sounds != NULL) {
242 		entry = (AlarmListEntry *)applet->sounds->data;
243 		g_object_set (alarm, "sound-file", entry->data, NULL);
244 	}
245 
246 	if (applet->apps != NULL) {
247 		entry = (AlarmListEntry *)applet->apps->data;
248 		g_object_set (alarm, "command", entry->data, NULL);
249 	}
250 
251 	// Add alarm to list of alarms
252     // This will indirectly add the alarm to the model
253 	alarm_applet_alarms_add (applet, alarm);
254 
255     // Select the new alarm in the list
256     if (alarm_list_window_find_alarm (GTK_TREE_MODEL (list_window->model), alarm, &iter)) {
257         selection = gtk_tree_view_get_selection (list_window->tree_view);
258         gtk_tree_selection_select_iter (selection, &iter);
259     }
260 
261 	// Show edit alarm dialog
262     alarm_settings_dialog_show (applet->settings_dialog, alarm);
263 }
264 
265 /**
266  * Stop all alarms action
267  */
268 void
alarm_action_stop_all(GtkAction * action,gpointer data)269 alarm_action_stop_all (GtkAction *action, gpointer data)
270 {
271     AlarmApplet *applet = (AlarmApplet *)data;
272 
273     g_debug ("AlarmAction: stop all");
274 
275     alarm_applet_alarms_stop (applet);
276 }
277 
278 /**
279  * Snooze all alarms action
280  */
281 void
alarm_action_snooze_all(GtkAction * action,gpointer data)282 alarm_action_snooze_all (GtkAction *action, gpointer data)
283 {
284     AlarmApplet *applet = (AlarmApplet *)data;
285 
286     g_debug ("AlarmAction: snooze all");
287 
288     alarm_applet_alarms_snooze (applet);
289 }
290 
291 /**
292  * Toggle list window action
293  */
294 void
alarm_action_toggle_list_win(GtkAction * action,gpointer data)295 alarm_action_toggle_list_win (GtkAction *action, gpointer data)
296 {
297     AlarmApplet *applet = (AlarmApplet *)data;
298     AlarmListWindow *list_window = applet->list_window;
299     gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
300 
301     g_debug ("AlarmAction: toggle list window");
302 
303 /*	if (!a || a->active == active) {
304 		// No alarms selected or no change
305 		return;
306 	}
307      */
308 
309     if (active) {
310         alarm_list_window_show (list_window);
311     } else {
312         alarm_list_window_hide (list_window);
313     }
314 }
315 
316 /**
317  * Quit action
318  */
319 void
alarm_action_quit(GtkAction * action,gpointer data)320 alarm_action_quit (GtkAction *action, gpointer data)
321 {
322 //    AlarmApplet *applet = (AlarmApplet *)data;
323 
324     g_debug ("AlarmAction: Quit!");
325 
326     // TODO: Free up resources - maybe use gtk_quit_add() & friends
327     gtk_main_quit ();
328 }
329 
330 /*
331  * Toggle autostart action
332  */
333 void
alarm_action_toggle_autostart(GtkAction * action,gpointer data)334 alarm_action_toggle_autostart (GtkAction *action, gpointer data)
335 {
336 	gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
337 	gboolean autostart_state = prefs_autostart_get_state();
338 	//gboolean check_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (applet->pref_autostart_check));
339 
340 	g_debug ("AlarmAction: toggle autostart to %d", active);
341 
342 	if (active != autostart_state) {
343 		g_debug ("AlarmAction: set autostart to %d!", active);
344 		prefs_autostart_set_state (active);
345 	}
346 }
347 
348 /*
349  * Toggle show_label action
350  */
351 void
alarm_action_toggle_show_label(GtkAction * action,gpointer data)352 alarm_action_toggle_show_label (GtkAction *action, gpointer data)
353 {
354 	AlarmApplet *applet = (AlarmApplet *)data;
355 	gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
356 	gboolean show_label_state = prefs_show_label_get(applet);
357 	//gboolean check_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (applet->pref_autostart_check));
358 
359 	g_debug ("AlarmAction: toggle show_label to %d", active);
360 
361 	if (active != show_label_state) {
362 		g_debug ("AlarmAction: set show_label to %d!", active);
363 		prefs_show_label_set (applet, active);
364 	}
365 }
366 
367 
368 
369 
370 
371 
372 
373 
374 /**
375  * Update actions to a consistent state
376  */
377 void
alarm_applet_actions_update_sensitive(AlarmApplet * applet)378 alarm_applet_actions_update_sensitive (AlarmApplet *applet)
379 {
380     //
381     // Update single alarm actions:
382     //
383 
384     // Determine whether there is a selected alarm
385     Alarm *a = alarm_list_window_get_selected_alarm (applet->list_window);
386     gboolean selected = (a != NULL);
387 
388     g_object_set (applet->actions_alarm, "sensitive", selected, NULL);
389 
390     g_object_set (applet->action_stop,
391         "sensitive", selected && a->triggered, NULL);
392 
393     g_object_set (applet->action_snooze,
394         "sensitive", selected && a->triggered, NULL);
395 
396 
397     //
398     // Update global actions
399     //
400 
401     // If there are alarms triggered, snooze_all and stop_all should be sensitive
402     g_object_set (applet->action_stop_all,
403         "sensitive", applet->n_triggered > 0, NULL);
404 
405     g_object_set (applet->action_snooze_all,
406         "sensitive", applet->n_triggered > 0, NULL);
407 
408 }
409