~ubuntu-branches/ubuntu/vivid/alarm-clock-applet/vivid

« back to all changes in this revision

Viewing changes to src/alarm-actions.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-03-17 09:02:44 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100317090244-ni0ye04mva2hxe10
Tags: 0.3.0-1
* New upstream release
* debian/control:
  + No change bump of Standards-Version to 3.8.4
  + Update build-deps:
    - Drop libglade, libpanel-applet, libgnomevfs2, libgnome{2,ui}
    - Add libxml2-dev and libunique-dev, intltool
* debian/patches/01_update-alarms-eta,patch:
  + Dropped, applied upstream
* debian/(alarm-clock-applet.1, alarm-clock-applet.manpages):
  + Add manpage for alarm-clock-applet, now that the binary is moved to
    /usr/bin.

Show diffs side-by-side

added added

removed removed

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