File: alarm-actions.c

package info (click to toggle)
alarm-clock-applet 0.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,720 kB
  • sloc: ansic: 5,121; sh: 1,382; makefile: 189
file content (408 lines) | stat: -rw-r--r-- 11,151 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * alarm-actions.h -- Alarm actions
 * 
 * Copyright (C) 2010 Johannes H. Jensen <joh@pseudoberries.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Authors:
 * 		Johannes H. Jensen <joh@pseudoberries.com>
 */

#include "alarm-actions.h"
#include "alarm-applet.h"
#include "alarm-list-window.h"

#define GET_ACTION(name) GTK_ACTION (gtk_builder_get_object (builder, (name)))

/**
 * Initialize the various actions
 */
void
alarm_applet_actions_init (AlarmApplet *applet)
{
    GtkBuilder *builder = applet->ui;

    // Actions on one alarm
    applet->actions_alarm = gtk_action_group_new ("alarm");
    
    applet->action_edit = GET_ACTION ("edit-action");
    applet->action_delete = GET_ACTION ("delete-action");
    applet->action_enabled = GTK_TOGGLE_ACTION (GET_ACTION ("enabled-action"));
    applet->action_stop = GET_ACTION ("stop-action");
    applet->action_snooze = GET_ACTION ("snooze-action");

    gtk_action_group_add_action (applet->actions_alarm, applet->action_edit);
    gtk_action_group_add_action (applet->actions_alarm, applet->action_delete);
    gtk_action_group_add_action (applet->actions_alarm, GTK_ACTION (applet->action_enabled));
    gtk_action_group_add_action (applet->actions_alarm, applet->action_stop);
    gtk_action_group_add_action (applet->actions_alarm, applet->action_snooze);
                                            
    

    // Global actions
    applet->actions_global = gtk_action_group_new ("global");
    
    applet->action_new = GET_ACTION ("new-action");
    applet->action_stop_all = GET_ACTION ("stop-all-action");
    applet->action_snooze_all = GET_ACTION ("snooze-all-action");
    
    applet->action_toggle_list_win = GTK_TOGGLE_ACTION (GET_ACTION ("toggle-list-win-action"));
    gtk_action_set_accel_group (GTK_ACTION (applet->action_toggle_list_win),
        applet->list_window->accel_group);

    applet->action_toggle_autostart = GTK_TOGGLE_ACTION (GET_ACTION ("autostart-action"));
    applet->action_toggle_show_label = GTK_TOGGLE_ACTION (GET_ACTION ("show-label-action"));

    gtk_action_group_add_action (applet->actions_global, applet->action_new);
    gtk_action_group_add_action (applet->actions_global, applet->action_stop_all);
    gtk_action_group_add_action (applet->actions_global, applet->action_snooze_all);
    gtk_action_group_add_action_with_accel (applet->actions_global, 
        GTK_ACTION (applet->action_toggle_list_win), "Escape");
    gtk_action_group_add_action (applet->actions_global, GTK_ACTION (applet->action_toggle_autostart));
    gtk_action_group_add_action (applet->actions_global, GTK_ACTION (applet->action_toggle_show_label));

    gtk_action_connect_accelerator (GTK_ACTION (applet->action_toggle_list_win));
    
    // Update actions
    alarm_applet_actions_update_sensitive (applet);
}


//
// SINGLE ALARM ACTIONS:
//

/**
 * Edit alarm action
 */
void
alarm_action_edit (GtkAction *action, gpointer data)
{
    AlarmApplet *applet = (AlarmApplet *)data;
    AlarmListWindow *list_window = applet->list_window;
	Alarm *a = alarm_list_window_get_selected_alarm (list_window);
	
	if (!a) {
		// No alarms selected
		return;
	}
    
    g_debug ("AlarmAction: edit '%s'", a->message);
	
	// Stop alarm
    alarm_clear (a);

    // Show settings dialog for alarm
    alarm_settings_dialog_show (applet->settings_dialog, a);
}

/**
 * Delete alarm action
 */
void
alarm_action_delete (GtkAction *action, gpointer data)
{
    AlarmApplet *applet = (AlarmApplet *)data;
    AlarmListWindow *list_window = applet->list_window;
    AlarmSettingsDialog *sdialog = applet->settings_dialog;
    
	Alarm *a = alarm_list_window_get_selected_alarm (list_window);

	if (!a) {
		// No alarms selected
		return;
	}
    
    g_debug ("AlarmAction: delete '%s'", a->message);

    // If there's a settings dialog open for this alarm, close it.
    if (sdialog->alarm == a) {
        alarm_settings_dialog_close (sdialog);
    }

    // Disable, clear and delete alarm
    alarm_disable (a);
    alarm_clear (a);
    alarm_delete (a);

    // Remove from applet list
    alarm_applet_alarms_remove (applet, a);
}

/**
 * Enable alarm action
 */
void
alarm_action_enabled (GtkToggleAction *action, gpointer data)
{
    AlarmApplet *applet = (AlarmApplet *)data;
    AlarmListWindow *list_window = applet->list_window;
	Alarm *a = alarm_list_window_get_selected_alarm (list_window);
    gboolean active = gtk_toggle_action_get_active(action);
	
	if (!a || a->active == active) {
		// No alarms selected or no change
		return;
	}
    
    g_debug ("AlarmAction: enabled(%d) '%s'", active, a->message);

    alarm_set_enabled (a, active);
}

/**
 * Update enabled action state
 */
void
alarm_action_update_enabled (AlarmApplet *applet)
{
    Alarm *a = alarm_list_window_get_selected_alarm (applet->list_window);
    gboolean active = gtk_toggle_action_get_active(applet->action_enabled);
	
	if (!a || a->active == active) {
		// No alarms selected or no change
		return;
	}

    gtk_toggle_action_set_active (applet->action_enabled, a->active);
}

/**
 * Stop alarm action
 */
void
alarm_action_stop (GtkAction *action, gpointer data)
{
    AlarmApplet *applet = (AlarmApplet *)data;
    AlarmListWindow *list_window = applet->list_window;
    Alarm *a;
    
    if ((a = alarm_list_window_get_selected_alarm (list_window))) {
        g_debug ("AlarmAction: stop '%s'", a->message);

        alarm_clear (a);
    }
}

/**
 * Snooze alarm action
 */
void
alarm_action_snooze (GtkAction *action, gpointer data)
{
    AlarmApplet *applet = (AlarmApplet *)data;
    AlarmListWindow *list_window = applet->list_window;
    Alarm *a;
    
    if ((a = alarm_list_window_get_selected_alarm (list_window))) {
        g_debug ("AlarmAction: snooze '%s'", a->message);
        
        alarm_applet_alarm_snooze (applet, a);
    }
}


//
// GLOBAL ACTIONS
//

/**
 * New alarm action
 */
void
alarm_action_new (GtkAction *action, gpointer data)
{
    AlarmApplet *applet = (AlarmApplet *)data;
    AlarmListWindow *list_window = applet->list_window;
    Alarm *alarm;
	AlarmListEntry *entry;
    GtkTreeIter iter;
    GtkTreeSelection *selection;
	
    g_debug ("AlarmAction: new");
    
	// Create new alarm, will fall back to defaults.
	alarm = alarm_new (ALARM_GCONF_DIR, -1);
	
	// Set first sound / app in list
	if (applet->sounds != NULL) {
		entry = (AlarmListEntry *)applet->sounds->data;
		g_object_set (alarm, "sound-file", entry->data, NULL);
	}
	
	if (applet->apps != NULL) {
		entry = (AlarmListEntry *)applet->apps->data;
		g_object_set (alarm, "command", entry->data, NULL);
	}

	// Add alarm to list of alarms
    // This will indirectly add the alarm to the model
	alarm_applet_alarms_add (applet, alarm);

    // Select the new alarm in the list
    if (alarm_list_window_find_alarm (GTK_TREE_MODEL (list_window->model), alarm, &iter)) {
        selection = gtk_tree_view_get_selection (list_window->tree_view);
        gtk_tree_selection_select_iter (selection, &iter);
    }
	
	// Show edit alarm dialog
    alarm_settings_dialog_show (applet->settings_dialog, alarm);
}

/**
 * Stop all alarms action
 */
void
alarm_action_stop_all (GtkAction *action, gpointer data)
{
    AlarmApplet *applet = (AlarmApplet *)data;

    g_debug ("AlarmAction: stop all");

    alarm_applet_alarms_stop (applet);
}

/**
 * Snooze all alarms action
 */
void
alarm_action_snooze_all (GtkAction *action, gpointer data)
{
    AlarmApplet *applet = (AlarmApplet *)data;

    g_debug ("AlarmAction: snooze all");

    alarm_applet_alarms_snooze (applet);
}

/**
 * Toggle list window action
 */
void
alarm_action_toggle_list_win (GtkAction *action, gpointer data)
{
    AlarmApplet *applet = (AlarmApplet *)data;
    AlarmListWindow *list_window = applet->list_window;
    gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
	
    g_debug ("AlarmAction: toggle list window");
    
/*	if (!a || a->active == active) {
		// No alarms selected or no change
		return;
	}
     */

    if (active) {
        alarm_list_window_show (list_window);
    } else {
        alarm_list_window_hide (list_window);
    }
}

/**
 * Quit action
 */
void
alarm_action_quit (GtkAction *action, gpointer data)
{
//    AlarmApplet *applet = (AlarmApplet *)data;

    g_debug ("AlarmAction: Quit!");

    // TODO: Free up resources - maybe use gtk_quit_add() & friends
    gtk_main_quit ();
}

/*
 * Toggle autostart action
 */
void
alarm_action_toggle_autostart (GtkAction *action, gpointer data)
{
	gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
	gboolean autostart_state = prefs_autostart_get_state();
	//gboolean check_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (applet->pref_autostart_check));

	g_debug ("AlarmAction: toggle autostart to %d", active);

	if (active != autostart_state) {
		g_debug ("AlarmAction: set autostart to %d!", active);
		prefs_autostart_set_state (active);
	}
}

/*
 * Toggle show_label action
 */
void
alarm_action_toggle_show_label (GtkAction *action, gpointer data)
{
	AlarmApplet *applet = (AlarmApplet *)data;
	gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
	gboolean show_label_state = prefs_show_label_get(applet);
	//gboolean check_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (applet->pref_autostart_check));

	g_debug ("AlarmAction: toggle show_label to %d", active);

	if (active != show_label_state) {
		g_debug ("AlarmAction: set show_label to %d!", active);
		prefs_show_label_set (applet, active);
	}
}








/**
 * Update actions to a consistent state
 */
void
alarm_applet_actions_update_sensitive (AlarmApplet *applet)
{    
    //
    // Update single alarm actions:
    //
    
    // Determine whether there is a selected alarm
    Alarm *a = alarm_list_window_get_selected_alarm (applet->list_window);
    gboolean selected = (a != NULL);
    
    g_object_set (applet->actions_alarm, "sensitive", selected, NULL);
    
    g_object_set (applet->action_stop, 
        "sensitive", selected && a->triggered, NULL);

    g_object_set (applet->action_snooze, 
        "sensitive", selected && a->triggered, NULL);


    //
    // Update global actions
    //

    // If there are alarms triggered, snooze_all and stop_all should be sensitive
    g_object_set (applet->action_stop_all,
        "sensitive", applet->n_triggered > 0, NULL);

    g_object_set (applet->action_snooze_all,
        "sensitive", applet->n_triggered > 0, NULL);
    
}