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

« back to all changes in this revision

Viewing changes to src/alarm-applet.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:
21
21
 *              Johannes H. Jensen <joh@pseudoberries.com>
22
22
 */
23
23
 
 
24
#include <stdlib.h>
 
25
 
24
26
#include "alarm-applet.h"
25
27
 
26
28
#include "alarm.h"
27
 
#include "edit-alarm.h"
 
29
#include "alarm-settings.h"
28
30
 
29
31
/*
30
32
 * DEFINTIIONS {{
31
33
 */
32
34
 
33
 
gchar *supported_sound_mime_types [] = { "audio", "video", "application/ogg", NULL };
 
35
const gchar *supported_sound_mime_types [] = { "audio", "video", "application/ogg", NULL };
34
36
GHashTable *app_command_map = NULL;
35
37
 
36
38
/*
38
40
 */
39
41
 
40
42
/*
41
 
 * Snooze any running (read: playing sound) alarms.
 
43
 * Snooze any triggered alarms.
 
44
 *
 
45
 * Returns the number of snoozed alarms
42
46
 */
43
 
void
44
 
alarm_applet_snooze_alarms (AlarmApplet *applet)
 
47
guint
 
48
alarm_applet_alarms_snooze (AlarmApplet *applet)
45
49
{
46
50
        GList *l;
47
 
        Alarm *a;
 
51
        Alarm *a, *last_snoozed = NULL;
 
52
    guint n_snoozed = 0;
 
53
    gchar *summary, *body;
 
54
    const gchar *icon;
48
55
 
49
56
        g_debug ("Snoozing alarms...");
50
57
 
51
 
        // Loop through alarms and snooze all of 'em
 
58
        // Loop through alarms and snooze all triggered ones
52
59
        for (l = applet->alarms; l; l = l->next) {
53
60
                a = ALARM (l->data);
54
 
                alarm_snooze (a);
55
 
        }
56
 
 
57
 
        // Close notification if present.
58
 
#ifdef HAVE_LIBNOTIFY
59
 
        if (applet->notify) {
60
 
                alarm_applet_notification_close (applet);
61
 
        }
62
 
#endif
 
61
 
 
62
        if (a->triggered) {
 
63
            alarm_applet_alarm_snooze (applet, a);
 
64
            last_snoozed = a;
 
65
            n_snoozed++;
 
66
        }
 
67
        }
 
68
 
 
69
    // Show notification
 
70
    if (n_snoozed > 0) {
 
71
        if (n_snoozed == 1) {
 
72
            // Single alarm snoozed
 
73
            summary = g_strdup_printf (_("%s snoozed"), last_snoozed->message);
 
74
            body = g_strdup_printf (_("You can stop the alarm from the Alarm Clock menu."));
 
75
            icon = (last_snoozed->type == ALARM_TYPE_TIMER) ? TIMER_ICON : ALARM_ICON;
 
76
            alarm_applet_notification_show (applet, summary, body, icon);
 
77
        } else {
 
78
            // More than 1 alarm snoozed
 
79
            summary = g_strdup_printf (_("Snoozed %d alarms"), n_snoozed);
 
80
            body = g_strdup_printf (_("You can stop alarms from the Alarm Clock menu."));
 
81
            icon = "alarm-clock";
 
82
            alarm_applet_notification_show (applet, summary, body, icon);
 
83
        }
 
84
 
 
85
        g_free (summary);
 
86
        g_free (body);
 
87
    }
 
88
    
 
89
    // Reset the triggered counter
 
90
    applet->n_triggered = 0;
 
91
 
 
92
    // Update status icon
 
93
    alarm_applet_status_update (applet);
 
94
    
 
95
    return n_snoozed;
63
96
}
64
97
 
65
98
/*
66
 
 * Clear any running (read: playing sound) alarms.
 
99
 * Stop any running (read: playing sound) alarms.
67
100
 */
68
 
void
69
 
alarm_applet_clear_alarms (AlarmApplet *applet)
 
101
guint
 
102
alarm_applet_alarms_stop (AlarmApplet *applet)
70
103
{
71
104
        GList *l;
72
 
        Alarm *a;
73
 
 
74
 
        g_debug ("Clearing alarms...");
 
105
        Alarm *a, *last_stopped = NULL;
 
106
    guint n_stopped = 0;
 
107
    gchar *summary, *body;
 
108
    const gchar *icon;
 
109
    
 
110
        g_debug ("Stopping alarms...");
75
111
 
76
112
        // Loop through alarms and clear all of 'em
77
113
        for (l = applet->alarms; l; l = l->next) {
78
114
                a = ALARM (l->data);
79
 
                alarm_clear (a);
80
 
        }
81
 
 
82
 
        // Close notification if present.
83
 
#ifdef HAVE_LIBNOTIFY
84
 
        if (applet->notify) {
85
 
                alarm_applet_notification_close (applet);
86
 
        }
87
 
#endif
88
 
}
 
115
 
 
116
        if (alarm_is_playing (a)) {
 
117
                alarm_clear (a);
 
118
            last_stopped = a;
 
119
            n_stopped++;
 
120
        }
 
121
        }
 
122
 
 
123
    // Show notification
 
124
    if (n_stopped > 0) {
 
125
        if (n_stopped == 1) {
 
126
            // Single alarm stopped
 
127
            summary = g_strdup_printf (_("%s stopped"), last_stopped->message);
 
128
            body = g_strdup_printf (_("Repeating alarms will still continue according to schedule."));
 
129
            icon = (last_stopped->type == ALARM_TYPE_TIMER) ? TIMER_ICON : ALARM_ICON;
 
130
            alarm_applet_notification_show (applet, summary, body, icon);
 
131
        } else {
 
132
            // More than 1 alarm stopped
 
133
            summary = g_strdup_printf (_("Stopped %d alarm(s)"), n_stopped);
 
134
            body = g_strdup_printf (_("Repeating alarms will still continue according to schedule."));
 
135
            icon = "alarm-clock";
 
136
            alarm_applet_notification_show (applet, summary, body, icon);
 
137
        }
 
138
 
 
139
        g_free (summary);
 
140
        g_free (body);
 
141
    }
 
142
    
 
143
    // Reset the triggered counter
 
144
    applet->n_triggered = 0;
 
145
 
 
146
    // Update status icon
 
147
    alarm_applet_status_update (applet);
 
148
    
 
149
    return n_stopped;
 
150
}
 
151
 
 
152
/**
 
153
 * Snooze an alarm, according to UI settings
 
154
 */
 
155
void
 
156
alarm_applet_alarm_snooze (AlarmApplet *applet, Alarm *alarm)
 
157
{
 
158
    guint mins = applet->snooze_mins;
 
159
 
 
160
    if (alarm->type == ALARM_TYPE_CLOCK) {
 
161
        // Clocks always snooze for 9 minutes
 
162
        mins = ALARM_STD_SNOOZE;
 
163
    }
 
164
 
 
165
    g_debug ("AlarmApplet: snooze '%s' for %d minutes", alarm->message, mins);
 
166
 
 
167
    alarm_snooze (alarm, mins * 60);
 
168
 
 
169
    // Update status icon
 
170
    alarm_applet_status_update (applet);
 
171
}
 
172
 
 
173
/**
 
174
 * Stop an alarm, keeping UI consistent
 
175
 */
 
176
void
 
177
alarm_applet_alarm_stop (AlarmApplet *applet, Alarm *alarm)
 
178
{
 
179
    g_debug ("Stopping alarm #%d...", alarm->id);
 
180
 
 
181
    // Stop the alarm
 
182
    alarm_clear (alarm);
 
183
 
 
184
    // Update status icon
 
185
    alarm_applet_status_update (applet);
 
186
}
 
187
 
89
188
 
90
189
 
91
190
/*
102
201
        GList *l, *l2;
103
202
        gboolean found;
104
203
 
 
204
        const gchar* const *sysdirs;
 
205
        gchar *sounds_dir = NULL;
 
206
        gchar *tmp;
 
207
        gint i;
 
208
 
105
209
        //g_assert (applet->alarms);
106
210
 
107
211
        // Free old list
108
212
        if (applet->sounds != NULL)
109
213
                alarm_list_entry_list_free (&(applet->sounds));
110
214
 
111
 
        // Load stock sounds
112
 
        applet->sounds = alarm_list_entry_list_new ("file://" ALARM_SOUNDSDIR,
113
 
                                                                                                supported_sound_mime_types);
 
215
        // Locate gnome sounds
 
216
        sysdirs = g_get_system_data_dirs ();
 
217
        for (i = 0; !sounds_dir && sysdirs[i] != NULL; i++) {
 
218
                tmp = g_build_filename(sysdirs[i], "sounds/gnome/default/alerts", NULL);
 
219
                if (g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
 
220
                        // Load stock sounds
 
221
                        g_debug ("AlarmApplet: sounds_load: Found %s!", tmp);
 
222
                        sounds_dir = g_strdup_printf ("file://%s", tmp);
 
223
                        applet->sounds = alarm_list_entry_list_new (sounds_dir, supported_sound_mime_types);
 
224
                        g_free (sounds_dir);
 
225
                }
 
226
                g_free(tmp);
 
227
        }
 
228
 
 
229
        if (!sounds_dir) {
 
230
                g_warning ("AlarmApplet: Could not locate sounds!");
 
231
        }
114
232
 
115
233
        // Load custom sounds from alarms
116
234
        for (l = applet->alarms; l != NULL; l = l->next) {
190
308
                        ret_val = (gchar *) xmlNodeGetContent (element);
191
309
                    } else {
192
310
                                for (i = 0; sys_langs[i] != NULL; i++) {
193
 
                                    if (!strcmp (sys_langs[i], node_lang)) {
 
311
                                    if (!strcmp (sys_langs[i], (const char *)node_lang)) {
194
312
                                                ret_val = (gchar *) xmlNodeGetContent (element);
195
 
                                                /* since sys_langs is sorted from most desirable to
196
 
                                                 * least desirable, exit at first match
197
 
                                                 */
 
313
                                                // since sys_langs is sorted from most desirable to
 
314
                                                // least desirable, exit at first match
198
315
                                                break;
199
316
                                    }
200
317
                                }
214
331
        if (app_command_map == NULL) {
215
332
                app_command_map = g_hash_table_new (g_str_hash, g_str_equal);
216
333
 
217
 
                /* `rhythmbox-client --play' doesn't actually start playing unless
218
 
                 * Rhythmbox is already running. Sounds like a Bug. */
 
334
                // `rhythmbox-client --play' doesn't actually start playing unless
 
335
                // Rhythmbox is already running. Sounds like a Bug.
219
336
                g_hash_table_insert (app_command_map,
220
337
                                                         "rhythmbox", "rhythmbox-client --play");
221
338
 
244
361
        xmlNode *root, *section, *element;
245
362
    gchar *executable;
246
363
    const gchar *tmp;
 
364
        const gchar* const *sysdirs;
 
365
        gint i;
247
366
 
248
367
        if (applet->apps != NULL)
249
368
                alarm_list_entry_list_free (&(applet->apps));
250
369
 
251
 
        // We'll get the default media players from g-d-a.xml
252
 
        // from gnome-control-center
253
 
        filename = g_build_filename (ALARM_DATADIR,
254
 
                                                                 "gnome-control-center",
255
 
                                                                 "default-apps",
256
 
                                                                 "gnome-default-applications.xml",
257
 
                                                                 NULL);
258
 
 
259
 
        if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
260
 
                g_critical ("Could not find %s.", filename);
261
 
                return;
262
 
        }
263
 
 
264
 
    xml_doc = xmlParseFile (filename);
265
 
 
266
 
    if (!xml_doc) {
267
 
        g_warning ("Could not load %s.", filename);
268
 
        return;
269
 
    }
270
 
 
271
 
    root = xmlDocGetRootElement (xml_doc);
272
 
 
273
 
        for (section = root->children; section != NULL; section = section->next) {
274
 
                if (!xmlStrncmp (section->name, "media-players", 13)) {
275
 
                    for (element = section->children; element != NULL; element = element->next) {
276
 
                                if (!xmlStrncmp (element->name, "media-player", 12)) {
277
 
                                    executable = gnome_da_xml_get_string (element, "executable");
278
 
                                    if (is_executable_valid (executable)) {
279
 
                                        name = gnome_da_xml_get_string (element, "name");
280
 
                                        icon = gnome_da_xml_get_string (element, "icon-name");
281
 
 
282
 
                                        // Lookup executable in app command map
283
 
                                        tmp = get_app_command (executable);
284
 
                                        if (tmp)
285
 
                                                command = g_strdup (tmp);
286
 
                                        else {
287
 
                                                // Fall back to command specified in XML
288
 
                                                command = gnome_da_xml_get_string (element, "command");
289
 
                                        }
290
 
 
291
 
 
292
 
 
293
 
                                                g_debug ("LOAD-APPS: Adding '%s': %s [%s]", name, command, icon);
294
 
 
295
 
                                                entry = alarm_list_entry_new (name, command, icon);
296
 
 
297
 
                                                g_free (name);
298
 
                                                g_free (command);
299
 
                                                g_free (icon);
300
 
 
301
 
                                                applet->apps = g_list_append (applet->apps, entry);
302
 
                                    }
303
 
 
304
 
                                    if (executable)
305
 
                                        g_free (executable);
 
370
        // Locate g-d-a.xml
 
371
        sysdirs = g_get_system_data_dirs ();
 
372
        for (i = 0; sysdirs[i] != NULL; i++) {
 
373
                // We'll get the default media players from g-d-a.xml
 
374
                // from gnome-control-center
 
375
                filename = g_build_filename (sysdirs[i],
 
376
                                                                         "gnome-control-center",
 
377
                                                                         "default-apps",
 
378
                                                                         "gnome-default-applications.xml",
 
379
                                                                         NULL);
 
380
 
 
381
                if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
 
382
                        xml_doc = xmlParseFile (filename);
 
383
 
 
384
                        if (!xml_doc) {
 
385
                                g_warning ("Could not load %s.", filename);
 
386
                                continue;
 
387
                        }
 
388
 
 
389
                        root = xmlDocGetRootElement (xml_doc);
 
390
 
 
391
                        for (section = root->children; section != NULL; section = section->next) {
 
392
                                if (!xmlStrncmp (section->name, (const xmlChar *)"media-players", 13)) {
 
393
                                        for (element = section->children; element != NULL; element = element->next) {
 
394
                                                if (!xmlStrncmp (element->name, (const xmlChar *)"media-player", 12)) {
 
395
                                                        executable = gnome_da_xml_get_string (element, "executable");
 
396
                                                        if (is_executable_valid (executable)) {
 
397
                                                                name = gnome_da_xml_get_string (element, "name");
 
398
                                                                icon = gnome_da_xml_get_string (element, "icon-name");
 
399
 
 
400
                                                                // Lookup executable in app command map
 
401
                                                                tmp = get_app_command (executable);
 
402
                                                                if (tmp)
 
403
                                                                        command = g_strdup (tmp);
 
404
                                                                else {
 
405
                                                                        // Fall back to command specified in XML
 
406
                                                                        command = gnome_da_xml_get_string (element, "command");
 
407
                                                                }
 
408
 
 
409
 
 
410
 
 
411
                                                                g_debug ("LOAD-APPS: Adding '%s': %s [%s]", name, command, icon);
 
412
 
 
413
                                                                entry = alarm_list_entry_new (name, command, icon);
 
414
 
 
415
                                                                g_free (name);
 
416
                                                                g_free (command);
 
417
                                                                g_free (icon);
 
418
 
 
419
                                                                applet->apps = g_list_append (applet->apps, entry);
 
420
                                                        }
 
421
 
 
422
                                                        if (executable)
 
423
                                                                g_free (executable);
 
424
                                                }
 
425
                                        }
306
426
                                }
307
 
                    }
308
 
            }
 
427
                        }
 
428
 
 
429
                        g_free(filename);
 
430
                        break;
 
431
                }
 
432
 
 
433
                g_free(filename);
309
434
        }
310
435
 
311
 
 
312
 
 
313
 
 
314
 
        g_free (filename);
315
 
 
316
436
//      entry = alarm_list_entry_new("Rhythmbox Music Player", "rhythmbox", "rhythmbox");
317
437
//      applet->apps = g_list_append (applet->apps, entry);
318
438
}
319
439
 
320
 
 
321
 
/*
322
 
 * Alarm signals {{
323
 
 */
324
 
 
325
 
/*
326
 
 * Find the soonest-to-trigger upcoming alarm
327
 
 */
328
 
static void
329
 
alarm_applet_upcoming_alarm_update (AlarmApplet *applet)
330
 
{
331
 
        GList *l;
332
 
        Alarm *a;
333
 
 
334
 
        applet->upcoming_alarm = NULL;
335
 
 
336
 
        /* Loop through alarms looking for all active upcoming alarms and locate
337
 
         * the one which will trigger sooner.
338
 
         */
339
 
 
340
 
        for (l = applet->alarms; l; l = l->next) {
341
 
                a = ALARM (l->data);
342
 
 
343
 
                if (!a->active) continue;
344
 
 
345
 
                if (!applet->upcoming_alarm || a->timestamp < applet->upcoming_alarm->timestamp) {
346
 
                        // This alarm is most recent
347
 
                        applet->upcoming_alarm = a;
348
 
                }
349
 
        }
350
 
}
351
 
 
352
 
/*
353
 
 * Callback for when an alarm is activated / deactivated.
354
 
 * We use this to update our closest upcoming alarm.
355
 
 */
356
 
static void
357
 
alarm_active_changed (GObject *object,
358
 
                                          GParamSpec *param,
359
 
                                          gpointer data)
360
 
{
361
 
        Alarm *alarm            = ALARM (object);
362
 
        AlarmApplet *applet = (AlarmApplet *)data;
363
 
 
364
 
        g_debug ("alarm_active_changed: #%d to %d", alarm->id, alarm->active);
365
 
 
366
 
        // Check if this was the upcoming alarm
367
 
        if (!alarm->active) {
368
 
                if (applet->upcoming_alarm == alarm) {
369
 
                        applet->upcoming_alarm = NULL;
370
 
 
371
 
                        alarm_applet_upcoming_alarm_update (applet);
372
 
                        alarm_applet_label_update (applet);
373
 
                        alarm_applet_icon_update (applet);
374
 
                }
375
 
 
376
 
                return;
377
 
        }
378
 
 
379
 
        if (!applet->upcoming_alarm || alarm->timestamp < applet->upcoming_alarm->timestamp) {
380
 
                // We're next!
381
 
                applet->upcoming_alarm = alarm;
382
 
 
383
 
                alarm_applet_label_update (applet);
384
 
 
385
 
                return;
386
 
        }
387
 
}
388
 
 
389
 
/*
390
 
 * Callback for when an alarm is triggered
391
 
 * We show the notification bubble here if appropriate.
392
 
 */
393
 
static void
394
 
alarm_triggered (Alarm *alarm, gpointer data)
395
 
{
396
 
        AlarmApplet *applet = (AlarmApplet *)data;
397
 
 
398
 
        g_debug ("Alarm triggered: #%d", alarm->id);
399
 
 
400
 
        if (alarm->notify_bubble) {
401
 
                g_debug ("Alarm #%d NOTIFICATION DISPLAY", alarm->id);
402
 
                alarm_applet_notification_display (applet, alarm);
403
 
        }
404
 
}
405
 
 
406
 
/*
407
 
 * }} Alarm signals
408
 
 */
409
 
 
410
440
/*
411
441
 * Alarms list {{
412
442
 */
413
443
 
414
 
// TODO: Refactor to use a GHashTable instead
 
444
// TODO: Refactor to use a GHashTable instead?
415
445
void
416
446
alarm_applet_alarms_load (AlarmApplet *applet)
417
447
{
 
448
        GList *list = NULL;
 
449
    GList *l = NULL;
 
450
    
418
451
        if (applet->alarms != NULL) {
419
 
                GList *l;
420
 
 
421
 
                /* Free old alarm objects */
 
452
                // Free old alarm objects
422
453
                for (l = applet->alarms; l != NULL; l = l->next) {
423
454
                        g_object_unref (ALARM (l->data));
424
455
                }
425
456
 
426
 
                /* Free list */
 
457
                // Free list
427
458
                g_list_free (applet->alarms);
428
459
        }
429
460
 
430
 
        /* Fetch list of alarms */
431
 
        applet->alarms = alarm_get_list (ALARM_GCONF_DIR);
 
461
        // Fetch list of alarms and add them
 
462
    applet->alarms = NULL;
 
463
        list = alarm_get_list (ALARM_GCONF_DIR);
 
464
 
 
465
    for (l = list; l != NULL; l = l->next) {
 
466
        alarm_applet_alarms_add (applet, ALARM (l->data));
 
467
    }
432
468
}
433
469
 
434
470
void
436
472
{
437
473
        applet->alarms = g_list_append (applet->alarms, alarm);
438
474
 
 
475
    g_signal_connect (alarm, "notify", G_CALLBACK (alarm_applet_alarm_changed), applet);
439
476
        g_signal_connect (alarm, "notify::sound-file", G_CALLBACK (alarm_sound_file_changed), applet);
440
 
        g_signal_connect (alarm, "notify::active", G_CALLBACK (alarm_active_changed), applet);
441
 
        g_signal_connect (alarm, "notify::time", G_CALLBACK (alarm_active_changed), applet);
442
 
 
443
 
        g_signal_connect (alarm, "alarm", G_CALLBACK (alarm_triggered), applet);
 
477
 
 
478
        g_signal_connect (alarm, "alarm", G_CALLBACK (alarm_applet_alarm_triggered), applet);
 
479
        g_signal_connect (alarm, "cleared", G_CALLBACK (alarm_applet_alarm_cleared), applet);
 
480
 
 
481
    // Update alarm list window model
 
482
    if (applet->list_window) {
 
483
        alarm_list_window_alarm_add (applet->list_window, alarm);
 
484
    }
444
485
}
445
486
 
446
487
void
447
488
alarm_applet_alarms_remove (AlarmApplet *applet, Alarm *alarm)
448
489
{
449
 
        /*
450
 
         * Remove from list
451
 
         */
 
490
        // Remove from list
452
491
        applet->alarms = g_list_remove (applet->alarms, alarm);
453
492
 
454
 
        /*
455
 
         * Clear list store. This will decrease the refcount of our alarms by 1.
456
 
         */
457
 
        if (applet->list_alarms_store)
458
 
                gtk_list_store_clear (applet->list_alarms_store);
 
493
        // Clear list store. This will decrease the refcount of our alarms by 1.
 
494
        /*if (applet->list_alarms_store)
 
495
                gtk_list_store_clear (applet->list_alarms_store);*/
459
496
 
460
497
        g_debug ("alarm_applet_alarms_remove (..., %p): refcount = %d", alarm, G_OBJECT (alarm)->ref_count);
461
 
 
462
 
        /*
463
 
         * If this is the upcoming alarm, update it.
464
 
         */
465
 
        if (applet->upcoming_alarm == alarm) {
466
 
                alarm_applet_upcoming_alarm_update (applet);
467
 
        }
468
 
 
469
 
        /*
470
 
         * Remove any signal handlers for this alarm instance.
471
 
         */
 
498
    
 
499
        // Remove any signal handlers for this alarm instance.
472
500
        g_signal_handlers_disconnect_matched (alarm, 0, 0, 0, NULL, NULL, NULL);
473
501
 
474
 
        /*
475
 
         * Dereference alarm
476
 
         */
 
502
    // Update alarm list window model
 
503
    if (applet->list_window) {
 
504
        alarm_list_window_alarm_remove (applet->list_window, alarm);
 
505
    }
 
506
    
 
507
        // Dereference alarm
477
508
        g_object_unref (alarm);
478
509
}
479
510
 
481
512
 * }} Alarms list
482
513
 */
483
514
 
484
 
void
 
515
// TODO: Is this function needed?
 
516
/*void
485
517
alarm_applet_destroy (AlarmApplet *applet)
486
518
{
487
519
        GList *l;
490
522
 
491
523
        g_debug ("AlarmApplet DESTROY");
492
524
 
493
 
        // Remove any timers
494
 
        if (applet->timer_id) {
495
 
                g_source_remove (applet->timer_id);
496
 
        }
497
 
 
498
 
        // Destroy alarms list
499
 
        if (applet->list_alarms_dialog) {
500
 
                list_alarms_dialog_close (applet);
501
 
        }
 
525
        // TODO: Destroy alarms list
 
526
//      if (applet->list_alarms_dialog) {
 
527
//              list_alarms_dialog_close (applet);
 
528
//      }
502
529
 
503
530
        // Destroy preferences dialog
504
 
        if (applet->preferences_dialog) {
505
 
                gtk_widget_destroy (GTK_WIDGET (applet->preferences_dialog));
 
531
        if (applet->prefs_dialog) {
 
532
                gtk_widget_destroy (GTK_WIDGET (applet->prefs_dialog));
506
533
        }
507
534
 
508
535
        // Loop through all alarms and free like a mad man!
510
537
                a = ALARM (l->data);
511
538
 
512
539
                // Check if a dialog is open for this alarm
513
 
                dialog = (AlarmSettingsDialog *)g_hash_table_lookup (applet->edit_alarm_dialogs, (gconstpointer)a->id);
514
 
 
515
 
                if (dialog) {
516
 
                        alarm_settings_dialog_close (dialog);
517
 
                        //g_free (dialog);
518
 
                }
 
540
                //dialog = (AlarmSettingsDialog *)g_hash_table_lookup (applet->edit_alarm_dialogs, (gconstpointer)a->id);
519
541
 
520
542
                g_object_unref (a);
521
543
        }
536
558
        }
537
559
 
538
560
        // Free GConf dir
539
 
        g_free (applet->gconf_dir);
 
561
        //g_free (applet->gconf_dir);
540
562
 
541
563
        // Finally free the AlarmApplet struct itself
542
564
        g_free (applet);
 
565
}*/
 
566
 
 
567
 
 
568
static UniqueResponse
 
569
unique_app_message_cb (UniqueApp         *app,
 
570
                       UniqueCommand      command,
 
571
                       UniqueMessageData *message,
 
572
                       guint              time_,
 
573
                       gpointer           user_data)
 
574
{
 
575
    AlarmApplet *applet = (AlarmApplet *)user_data;
 
576
 
 
577
    UniqueResponse res;
 
578
 
 
579
    switch (command) {
 
580
        case UNIQUE_ACTIVATE:
 
581
            g_debug ("AlarmApplet: unique_app_message: ACTIVATE");
 
582
            if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (applet->action_toggle_list_win))) {
 
583
                // Already visible, present to user
 
584
                alarm_list_window_show (applet->list_window);
 
585
            } else {
 
586
                // Toggle list window visibility
 
587
                gtk_action_activate (GTK_ACTION (applet->action_toggle_list_win));
 
588
            }
 
589
            
 
590
            res = UNIQUE_RESPONSE_OK;
 
591
            break;
 
592
        default:
 
593
            g_warning ("AlarmApplet: unique_app_message: Unknown command %d",
 
594
                command);
 
595
            
 
596
            res = UNIQUE_RESPONSE_INVALID;
 
597
            break;
 
598
    }
 
599
 
 
600
    return res;
543
601
}
544
602
 
545
603
 
 
604
 
546
605
/*
547
606
 * INIT {{
548
607
 */
549
608
 
550
 
static gboolean
551
 
alarm_applet_factory (PanelApplet *panelapplet,
552
 
                                          const gchar *iid,
553
 
                                          gpointer data)
 
609
static AlarmApplet*
 
610
alarm_applet_init()
554
611
{
555
612
        AlarmApplet *applet;
556
 
 
557
 
        if (strcmp (iid, "OAFIID:AlarmClock") != 0)
558
 
                return FALSE;
559
 
 
560
 
        /* Initialize applet struct,
561
 
         * fill with zero's */
 
613
        
 
614
        // Initialize applet struct */
562
615
        applet = g_new0 (AlarmApplet, 1);
563
 
 
564
 
        applet->parent = panelapplet;
565
 
        applet->edit_alarm_dialogs = g_hash_table_new (NULL, NULL);
566
 
 
567
 
        /* Preferences (defaults).
568
 
         * ...gconf_get_string can return NULL if the key is not found. We can't
569
 
         * assume the schema provides the default values for strings. */
570
 
        applet->show_label = TRUE;
571
 
        applet->label_type = LABEL_TYPE_TIME;
572
 
 
573
 
        /* Prepare applet */
574
 
        panel_applet_add_preferences (applet->parent, ALARM_SCHEMA_DIR, NULL);
575
 
        panel_applet_set_flags (PANEL_APPLET (panelapplet), PANEL_APPLET_EXPAND_MINOR);
576
 
        applet->gconf_dir = panel_applet_get_preferences_key (applet->parent);
577
 
 
578
 
        /* Set up gconf handlers */
 
616
    
 
617
        //applet->edit_alarm_dialogs = g_hash_table_new (NULL, NULL);
 
618
 
 
619
        // Preferences (defaults).
 
620
        // ...gconf_get_string can return NULL if the key is not found. We can't
 
621
        // assume the schema provides the default values for strings.
 
622
 
 
623
    // TODO: Add to gconf
 
624
    applet->snooze_mins = 5;
 
625
 
 
626
        // Set up gconf handlers
579
627
        alarm_applet_gconf_init (applet);
580
628
 
581
 
        /* Load gconf values */
 
629
        // Load gconf values
582
630
        alarm_applet_gconf_load (applet);
583
631
 
584
 
        /* Load alarms */
 
632
        // Load alarms
585
633
        alarm_applet_alarms_load (applet);
586
 
 
587
 
        /* Find any upcoming active alarms */
588
 
        alarm_applet_upcoming_alarm_update (applet);
589
 
 
590
 
        /* Load sounds from alarms */
 
634
    
 
635
        // Load sounds from alarms
591
636
        alarm_applet_sounds_load (applet);
592
637
 
593
 
        /* Load apps for alarms */
 
638
        // Load apps for alarms
594
639
        alarm_applet_apps_load (applet);
595
 
 
596
 
        /* Connect sound_file notify callback to all alarms */
597
 
        alarm_signal_connect_list (applet->alarms, "notify::sound-file",
598
 
                                                           G_CALLBACK (alarm_sound_file_changed), applet);
599
 
 
600
 
        /* Connect active & time notify callback to all alarms */
601
 
        alarm_signal_connect_list (applet->alarms, "notify::active",
602
 
                                                           G_CALLBACK (alarm_active_changed), applet);
603
 
        alarm_signal_connect_list (applet->alarms, "notify::time",
604
 
                                                           G_CALLBACK (alarm_active_changed), applet);
605
 
 
606
 
        /* Connect alarm trigger notify to all alarms */
607
 
        alarm_signal_connect_list (applet->alarms, "alarm",
608
 
                                                           G_CALLBACK (alarm_triggered), applet);
609
 
 
610
 
        /* Set up properties menu */
611
 
        alarm_applet_menu_init (applet);
612
 
 
613
 
        /* Set up applet */
 
640
    
 
641
        // Set up applet UI
614
642
        alarm_applet_ui_init (applet);
615
643
 
616
 
        return TRUE;
617
 
}
618
 
 
619
 
PANEL_APPLET_BONOBO_FACTORY ("OAFIID:AlarmClock_Factory",
620
 
                             PANEL_TYPE_APPLET,
621
 
                             "alarm_clock",
622
 
                             VERSION,
623
 
                             alarm_applet_factory,
624
 
                             NULL);
625
 
 
 
644
        return applet;
 
645
}
 
646
 
 
647
/**
 
648
 * Alarm Clock main()
 
649
 */
 
650
int
 
651
main (int argc, char *argv[])
 
652
{
 
653
    UniqueApp *unique_app;
 
654
        AlarmApplet *applet;
 
655
 
 
656
    // Internationalization
 
657
    bindtextdomain (GETTEXT_PACKAGE, ALARM_CLOCK_DATADIR "/locale");
 
658
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
659
    textdomain (GETTEXT_PACKAGE);
 
660
 
 
661
    // Terminate on critical errors
 
662
    //g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
 
663
 
 
664
        // Initialize GTK+
 
665
        gtk_init (&argc, &argv);
 
666
 
 
667
    // Initialize unique app
 
668
    unique_app = unique_app_new ("com.pseudoberries.AlarmClock", NULL);
 
669
 
 
670
    // Check if we're already running
 
671
    if (unique_app_is_running (unique_app)) {
 
672
        g_printerr(_("%s is already running, exiting...\n"), PACKAGE);
 
673
 
 
674
        // Send activate message
 
675
        UniqueMessageData *message = unique_message_data_new ();
 
676
 
 
677
        unique_app_send_message (unique_app, UNIQUE_ACTIVATE, message);
 
678
 
 
679
        unique_message_data_free (message);
 
680
        g_object_unref (unique_app);
 
681
 
 
682
        return EXIT_SUCCESS;
 
683
    }
 
684
 
 
685
    
 
686
 
 
687
        // Initialize applet
 
688
        applet = alarm_applet_init ();
 
689
 
 
690
    // Connect unique app message-received signal
 
691
    g_signal_connect (unique_app, "message-received", 
 
692
        G_CALLBACK (unique_app_message_cb), applet);
 
693
    
 
694
        // Start main loop
 
695
        gtk_main ();
 
696
 
 
697
    g_object_unref (unique_app);
 
698
        
 
699
        return 0;
 
700
}
626
701
 
627
702
/*
628
703
 * }} INIT