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

« back to all changes in this revision

Viewing changes to src/ui.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:
23
23
 
24
24
#include <time.h>
25
25
#include <string.h>
26
 
#ifdef HAVE_LIBNOTIFY
27
26
#include <libnotify/notify.h>
28
 
#endif
29
27
 
30
28
#include "alarm-applet.h"
 
29
#include "alarm-actions.h"
31
30
#include "ui.h"
32
 
#include "alarms-list.h"
33
31
 
34
32
enum
35
33
{
36
 
    PIXBUF_COL,
 
34
    GICON_COL,
37
35
    TEXT_COL,
38
36
    N_COLUMNS
39
37
};
40
38
 
 
39
static void alarm_applet_status_init (AlarmApplet *applet);
 
40
 
 
41
/*
 
42
 * Load a user interface by name
 
43
 */
 
44
GtkBuilder *
 
45
alarm_applet_ui_load (const char *name, AlarmApplet *applet)
 
46
{
 
47
    GtkBuilder *builder = NULL;
 
48
    GError *error = NULL;
 
49
    char *filename;
 
50
   
 
51
    filename = alarm_applet_get_data_path (name);
 
52
 
 
53
    g_assert(filename != NULL);
 
54
    
 
55
    builder = gtk_builder_new();
 
56
    
 
57
    g_debug ("Loading UI from %s...", filename);
 
58
 
 
59
    if (gtk_builder_add_from_file (builder, filename, &error)) {
 
60
        /* Connect signals */
 
61
        gtk_builder_connect_signals (builder, applet);
 
62
    } else {
 
63
        g_critical("Couldn't load the interface '%s'. %s", filename, error->message);
 
64
        g_error_free (error);
 
65
    }
 
66
 
 
67
    g_free (filename);
 
68
 
 
69
    return builder;
 
70
}
 
71
 
41
72
void
42
73
display_error_dialog (const gchar *message, const gchar *secondary_text, GtkWindow *parent)
43
74
{
46
77
        dialog = gtk_message_dialog_new (parent,
47
78
                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
48
79
                                                                        GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
49
 
                                                                        message);
 
80
                                                                        "%s", message);
50
81
        
51
82
        if (secondary_text != NULL) {
52
83
                gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
53
 
                                                                                                  secondary_text);
 
84
                                                                                                  "%s", secondary_text);
54
85
        }
55
86
        
56
87
        gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
58
89
        gtk_widget_destroy (dialog);
59
90
}
60
91
 
61
 
 
62
 
// TODO: Should display any triggered alarms / etc.
63
 
void
64
 
alarm_applet_label_update (AlarmApplet *applet)
65
 
{
66
 
        Alarm *a;
67
 
        struct tm *tm;
68
 
        guint hour, min, sec, now;
69
 
        gchar *tmp;
70
 
        
71
 
        if (!applet->upcoming_alarm) {
72
 
                // No upcoming alarms
73
 
                g_object_set (applet->label, "label", ALARM_DEF_LABEL, NULL);
74
 
                return;
75
 
        }
76
 
        
77
 
        a = applet->upcoming_alarm;
78
 
        
79
 
        if (applet->label_type == LABEL_TYPE_REMAIN) {
80
 
                /* Remaining time */
81
 
                tm = alarm_get_remain (a);
82
 
        } else {
83
 
                /* Alarm time */
84
 
                tm = alarm_get_time (a);
85
 
        }
86
 
        
87
 
        tmp = g_strdup_printf(_("%02d:%02d:%02d"), tm->tm_hour, tm->tm_min, tm->tm_sec);
88
 
        
89
 
        g_object_set(applet->label, "label", tmp, NULL);
90
 
        g_free(tmp);
91
 
}
92
 
 
93
 
// TODO: Refactor for more fancy tooltip with alarm summary.
94
 
void
95
 
alarm_applet_update_tooltip (AlarmApplet *applet)
96
 
{
97
 
        struct tm *time, *remain;
98
 
        GList *l;
99
 
        Alarm *a;
100
 
        GString *tip;
101
 
        guint count = 0;
102
 
 
103
 
        tip = g_string_new ("");
104
 
        
105
 
        // Find all active alarms
106
 
        for (l = applet->alarms; l; l = l->next) {
107
 
                a = ALARM (l->data);
108
 
                
109
 
                if (!a->active) continue;
110
 
                
111
 
                count++;
112
 
                
113
 
                time   = alarm_get_time (a);
114
 
                remain = alarm_get_remain (a);
115
 
                
116
 
                g_string_append_printf (tip, _("\n(%c) <b>%s</b> @%02d:%02d:%02d (-%02d:%02d:%02d)"), (a->type == ALARM_TYPE_TIMER) ? 'T' : 'A', a->message,
117
 
                                                                                                                time->tm_hour, time->tm_min, time->tm_sec, remain->tm_hour, remain->tm_min, remain->tm_sec);
118
 
        }
119
 
        
120
 
        if (count > 0) {
121
 
                tip = g_string_prepend (tip, _("Active alarms:"));
122
 
        } else {
123
 
                tip = g_string_append (tip, _("No active alarms"));
124
 
        }
125
 
        
126
 
        tip = g_string_append (tip, _("\n\nClick to snooze alarms"));
127
 
        tip = g_string_append (tip, _("\nDouble click to edit alarms"));
128
 
        
129
 
        gtk_widget_set_tooltip_markup (GTK_WIDGET (applet->parent), tip->str);
130
 
        
131
 
        g_string_free (tip, TRUE);
132
 
}
133
 
 
134
92
static gboolean
135
93
is_separator (GtkTreeModel *model, GtkTreeIter *iter, gpointer sep_index)
136
94
{
154
112
        GtkTreeModel *model;
155
113
        GtkCellRenderer *renderer;
156
114
        GtkTreeIter iter;
157
 
        GdkPixbuf *pixbuf = NULL;
158
 
        GtkIconTheme *theme;
159
115
        AlarmListEntry *entry;
160
116
        
161
117
        g_debug ("fill_combo_box... %d", g_list_length (list));
162
118
 
163
 
//      if (theme == NULL) {
164
 
        theme = gtk_icon_theme_get_default ();
165
 
//      }
166
 
 
167
119
        gtk_combo_box_set_row_separator_func (combo_box, is_separator,
168
120
                                          GINT_TO_POINTER (g_list_length (list)), NULL);
169
121
 
170
 
        model = GTK_TREE_MODEL (gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING));
 
122
        model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_ICON, G_TYPE_STRING));
171
123
        gtk_combo_box_set_model (combo_box, model);
172
124
        
173
125
        gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box));
178
130
        gtk_cell_renderer_set_fixed_size (renderer, -1, 22);
179
131
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
180
132
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
181
 
                                        "pixbuf", PIXBUF_COL,
 
133
                                        "gicon", GICON_COL,
182
134
                                        NULL);
183
135
 
184
136
        renderer = gtk_cell_renderer_text_new ();
188
140
                                        NULL);
189
141
 
190
142
        for (l = list; l != NULL; l = g_list_next (l)) {
 
143
                GIcon *icon;
 
144
                
191
145
                entry = (AlarmListEntry *) l->data;
192
 
                
193
 
                pixbuf = gtk_icon_theme_load_icon (theme, entry->icon, 22, 0, NULL);
194
 
                
 
146
                icon = g_icon_new_for_string (entry->icon, NULL);
 
147
 
195
148
                gtk_list_store_append (GTK_LIST_STORE (model), &iter);
196
149
                gtk_list_store_set (GTK_LIST_STORE (model), &iter,
197
 
                                        PIXBUF_COL, pixbuf,
198
 
                                        TEXT_COL, entry->name,
199
 
                                        -1);
200
 
                
201
 
                if (pixbuf)
202
 
                        g_object_unref (pixbuf);
 
150
                                                        GICON_COL, icon,
 
151
                                                        TEXT_COL, entry->name,
 
152
                                                        -1);
 
153
 
 
154
                g_object_unref (icon);
203
155
        }
204
156
 
205
157
        gtk_list_store_append (GTK_LIST_STORE (model), &iter);
206
158
        gtk_list_store_set (GTK_LIST_STORE (model), &iter, -1);
207
159
        gtk_list_store_append (GTK_LIST_STORE (model), &iter);
208
160
        gtk_list_store_set (GTK_LIST_STORE (model), &iter,
209
 
                        PIXBUF_COL, NULL,
 
161
                        GICON_COL, NULL,
210
162
                        TEXT_COL, custom_label,
211
163
                        -1);
212
164
}
213
165
 
214
 
 
215
 
static gboolean
216
 
button_cb (GtkWidget *widget,
217
 
                                                GdkEventButton *event,
218
 
                                                gpointer data)
219
 
{
220
 
        AlarmApplet *applet = (AlarmApplet *)data;
221
 
        
222
 
        g_debug("BUTTON: %d", event->button);
223
 
        
224
 
        /* React only to left mouse button */
225
 
        if (event->button == 2 || event->button == 3) {
226
 
                return FALSE;
227
 
        }
228
 
        
229
 
        if (event->type == GDK_2BUTTON_PRESS || event->type == GDK_3BUTTON_PRESS) {
230
 
                /* Double click: Open list alarms */
231
 
                list_alarms_dialog_display (applet);
232
 
        } else {
233
 
                alarm_applet_snooze_alarms (applet);
234
 
        }
235
 
        
236
 
        /* Show edit alarms dialog */
237
 
        //display_list_alarms_dialog (applet);
238
 
        
239
 
        return TRUE;
240
 
}
241
 
 
242
 
/* Taken from the GeyesApplet */
243
 
static void
244
 
applet_back_change (PanelApplet                 *a,
245
 
                                        PanelAppletBackgroundType       type,
246
 
                                        GdkColor                        *color,
247
 
                                        GdkPixmap                       *pixmap,
248
 
                                        AlarmApplet                     *applet) 
249
 
{
250
 
        /* taken from the TrashApplet */
251
 
        GtkRcStyle *rc_style;
252
 
        GtkStyle *style;
253
 
 
254
 
        /* reset style */
255
 
        gtk_widget_set_style (GTK_WIDGET (applet->parent), NULL);
256
 
        rc_style = gtk_rc_style_new ();
257
 
        gtk_widget_modify_style (GTK_WIDGET (applet->parent), rc_style);
258
 
        gtk_rc_style_unref (rc_style);
259
 
 
260
 
        switch (type) {
261
 
                case PANEL_COLOR_BACKGROUND:
262
 
                        gtk_widget_modify_bg (GTK_WIDGET (applet->parent),
263
 
                                        GTK_STATE_NORMAL, color);
264
 
                        break;
265
 
 
266
 
                case PANEL_PIXMAP_BACKGROUND:
267
 
                        style = gtk_style_copy (GTK_WIDGET (
268
 
                                        applet->parent)->style);
269
 
                        if (style->bg_pixmap[GTK_STATE_NORMAL])
270
 
                                g_object_unref
271
 
                                        (style->bg_pixmap[GTK_STATE_NORMAL]);
272
 
                        style->bg_pixmap[GTK_STATE_NORMAL] = g_object_ref
273
 
                                (pixmap);
274
 
                        gtk_widget_set_style (GTK_WIDGET (applet->parent),
275
 
                                        style);
276
 
                        g_object_unref (style);
277
 
                        break;
278
 
 
279
 
                case PANEL_NO_BACKGROUND:
280
 
                default:
281
 
                        break;
282
 
        }
283
 
 
284
 
}
285
 
 
286
 
/* Taken from gnome-panel/button-widget.c */
287
 
void
288
 
alarm_applet_icon_update (AlarmApplet *applet)
289
 
{
290
 
        static const gchar *prev_icon = ALARM_ICON;
291
 
        
292
 
        GtkWidget *w;
293
 
        GdkPixbuf *icon;
294
 
        gint      size, pbsize;
295
 
        const gchar *icon_name = ALARM_ICON;
296
 
        
297
 
        if (applet->upcoming_alarm && applet->upcoming_alarm->type == ALARM_TYPE_TIMER) {
298
 
                icon_name = TIMER_ICON;
299
 
        }
300
 
        
301
 
        w = GTK_WIDGET (applet->parent);
302
 
        
303
 
        if (panel_applet_get_orient (applet->parent) == PANEL_APPLET_ORIENT_UP ||
304
 
                panel_applet_get_orient (applet->parent) == PANEL_APPLET_ORIENT_DOWN)
305
 
                size = w->allocation.height;
306
 
        else
307
 
                size = w->allocation.width;
308
 
        
309
 
        if (size < 22)
310
 
                size = 16;
311
 
        else if (size < 24)
312
 
                size = 22;
313
 
        else if (size < 32)
314
 
                size = 24;
315
 
        else if (size < 48)
316
 
                size = 32;
317
 
        else if (size < 64)
318
 
                size = 48;
319
 
        else
320
 
                size = 64;
321
 
 
322
 
        // Reload icon only if the size is different.}
323
 
        icon = gtk_image_get_pixbuf (GTK_IMAGE (applet->icon));
324
 
        
325
 
        if (icon) {
326
 
                if (IS_HORIZONTAL (applet->parent))
327
 
                        pbsize = gdk_pixbuf_get_height (icon);
328
 
                else
329
 
                        pbsize = gdk_pixbuf_get_width (icon);
330
 
                
331
 
                if (prev_icon == icon_name && pbsize == size) {
332
 
                        // Do nothing
333
 
                        //g_debug ("load_icon: Existing size the same.");
334
 
                        return;
335
 
                }
336
 
        }
337
 
        
338
 
        g_debug ("Resizing icon to %dx%d...", size, size);
339
 
        
340
 
        icon = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
341
 
                                                                         icon_name,
342
 
                                                                         size,
343
 
                                                                         0, NULL);
344
 
        
345
 
        if (icon == NULL) {
346
 
                g_critical ("Icon not found.");
347
 
                return;
348
 
        }
349
 
        
350
 
        g_object_set (applet->icon, "pixbuf", icon, NULL);
351
 
        
352
 
        if (icon)
353
 
                g_object_unref (icon);
354
 
        
355
 
        prev_icon = icon_name;
356
 
}
357
 
 
358
 
static void
359
 
change_size_cb (GtkWidget         *widget,
360
 
                                GtkAllocation *alloc,
361
 
                                AlarmApplet       *applet) 
362
 
{       
363
 
        alarm_applet_icon_update (applet);
364
 
}
365
 
 
366
 
 
367
 
 
368
 
static void
369
 
update_orient (AlarmApplet *applet)
370
 
{
371
 
        const gchar *text;
372
 
        int          min_width;
373
 
        gdouble      new_angle;
374
 
        gdouble      angle;
375
 
        gint             width;
376
 
        GtkWidget       *box;
377
 
        
378
 
        if (IS_HORIZONTAL (applet->parent) && ORIENT_IS_HORIZONTAL (applet->orient)) {
379
 
                // Nothing to do, both old and new orientation is horizontal.
380
 
                return;
381
 
        }
382
 
        
383
 
        /* Do we need to repack? */
384
 
        if (IS_HORIZONTAL (applet->parent) != ORIENT_IS_HORIZONTAL (applet->orient)) {
385
 
                g_debug ("update_orient: REPACK");
386
 
                
387
 
                // Add new
388
 
                if (IS_HORIZONTAL (applet->parent))
389
 
                        box = gtk_hbox_new(FALSE, 6);
390
 
                else
391
 
                        box = gtk_vbox_new(FALSE, 6);
392
 
                
393
 
                /* Keep children */
394
 
                g_object_ref (applet->icon);
395
 
                g_object_ref (applet->label);
396
 
                
397
 
                /* Remove old */
398
 
                gtk_container_remove (GTK_CONTAINER (applet->box), applet->icon);
399
 
                gtk_container_remove (GTK_CONTAINER (applet->box), applet->label);
400
 
                gtk_container_remove (GTK_CONTAINER (applet->parent), applet->box);
401
 
                
402
 
                /* Pack */
403
 
                gtk_box_pack_start_defaults(GTK_BOX (box), applet->icon);
404
 
                gtk_box_pack_start_defaults(GTK_BOX (box), applet->label);
405
 
                
406
 
                applet->box = box;
407
 
                
408
 
                /* Add to container and show */
409
 
                gtk_container_add (GTK_CONTAINER (applet->parent), box);
410
 
                gtk_widget_show_all (GTK_WIDGET (applet->parent));
411
 
        }
412
 
        
413
 
        switch (panel_applet_get_orient (applet->parent)) {
414
 
        case PANEL_APPLET_ORIENT_LEFT:
415
 
                new_angle = 270;
416
 
                break;
417
 
        case PANEL_APPLET_ORIENT_RIGHT:
418
 
                new_angle = 90;
419
 
                break;
420
 
        default:
421
 
                new_angle = 0;
422
 
                break;
423
 
        }
424
 
        
425
 
        
426
 
        angle = gtk_label_get_angle (GTK_LABEL (applet->label));
427
 
        if (angle != new_angle) {
428
 
                //unfix_size (cd);
429
 
                gtk_label_set_angle (GTK_LABEL (applet->label), new_angle);
430
 
        }
431
 
}
432
 
 
433
 
static void
434
 
orient_change_cb (PanelApplet *a,
435
 
                                          PanelAppletOrient orient,
436
 
                                          AlarmApplet *applet)
437
 
{
438
 
        g_debug ("applet_orient_change");
439
 
        
440
 
        update_orient (applet);
441
 
        
442
 
        // Store new orientation
443
 
        applet->orient = panel_applet_get_orient (applet->parent);
444
 
}
445
 
 
446
 
 
447
 
static void
448
 
unrealize_cb (GtkWidget *object, AlarmApplet *applet)
449
 
{
450
 
        alarm_applet_destroy (applet);
451
 
}
452
 
 
453
 
#ifdef HAVE_LIBNOTIFY
454
 
static void
455
 
alarm_applet_notification_action_cb (NotifyNotification *notify,
456
 
                                                                         gchar *action,
457
 
                                                                         gpointer data)
458
 
{
459
 
        Alarm *alarm = ALARM (data);
460
 
        
461
 
        g_debug ("NOTIFY ACTION: %s", action);
462
 
        
463
 
        if (strcmp (action, "clear") == 0) {
464
 
                g_debug ("NOTIFY CLEAR #%d", alarm->id);
465
 
                alarm_clear (alarm);
466
 
        } else {
467
 
                // "snooze"
468
 
                g_debug ("NOTIFY SNOOZE #%d", alarm->id);
469
 
                alarm_snooze (alarm);
470
 
        }
471
 
}
472
 
#endif
473
 
 
474
 
/* Taken from the battery applet */
475
 
gboolean
476
 
alarm_applet_notification_display (AlarmApplet *applet, Alarm *alarm)
477
 
{
478
 
#ifdef HAVE_LIBNOTIFY
479
 
        GError *error = NULL;
480
 
        //GdkPixbuf *icon;
481
 
        gboolean result;
482
 
        const gchar *message;
483
 
        const gchar *icon = (alarm->type == ALARM_TYPE_CLOCK) ? ALARM_ICON : TIMER_ICON;
484
 
        
485
 
        if (!notify_is_initted () && !notify_init (_("Alarm Applet")))
486
 
                return FALSE;
487
 
        
488
 
        message = alarm->message;
489
 
        
490
 
        applet->notify = notify_notification_new (_("Alarm!"), message, icon, GTK_WIDGET (applet->icon));
491
 
        
492
 
        notify_notification_set_timeout (applet->notify, NOTIFY_EXPIRES_NEVER);
493
 
        notify_notification_add_action (applet->notify, "clear", "Clear", alarm_applet_notification_action_cb, alarm, NULL);
494
 
        
495
 
        if (alarm->snooze) {
496
 
                notify_notification_add_action (applet->notify, "snooze", "Snooze", alarm_applet_notification_action_cb, alarm, NULL);
497
 
        }
498
 
 
499
 
        result = notify_notification_show (applet->notify, &error);
500
 
        
501
 
        if (error)
502
 
        {
503
 
           g_warning (error->message);
504
 
           g_error_free (error);
505
 
        }
506
 
 
507
 
        return result;
508
 
#else
509
 
        return FALSE;
510
 
#endif
511
 
}
512
 
 
513
 
gboolean
514
 
alarm_applet_notification_close (AlarmApplet *applet)
515
 
{
516
 
#ifdef HAVE_LIBNOTIFY
517
 
        gboolean result;
518
 
        GError *error = NULL;
519
 
        
520
 
        result = notify_notification_close (applet->notify, &error);
521
 
        
522
 
        if (error)
523
 
        {
524
 
           g_warning (error->message);
525
 
           g_error_free (error);
526
 
        }
527
 
        
528
 
        g_object_unref (applet->notify);
529
 
        applet->notify = NULL;
530
 
#endif
531
 
}
532
 
 
533
 
/*
534
 
 * Updates label etc
 
166
/**
 
167
 * Show a notification
535
168
 */
536
 
static gboolean
537
 
alarm_applet_ui_update (AlarmApplet *applet)
 
169
void
 
170
alarm_applet_notification_show (AlarmApplet *applet,
 
171
                                const gchar *summary,
 
172
                                const gchar *body,
 
173
                                const gchar *icon)
 
174
 
538
175
{
539
 
        if (applet->show_label) {
540
 
                alarm_applet_label_update (applet);
 
176
    NotifyNotification *n;
 
177
    GError *error = NULL;
 
178
 
 
179
    n = notify_notification_new_with_status_icon (summary, body, icon,
 
180
                                                  applet->status_icon);
 
181
    
 
182
    if (!notify_notification_show (n, &error)) {
 
183
        g_warning ("Failed to send notification: %s", error->message);
 
184
                g_error_free (error);
541
185
        }
542
 
        
543
 
        alarm_applet_update_tooltip (applet);
544
 
        
545
 
        return TRUE;
 
186
 
 
187
        g_object_unref(G_OBJECT(n));
546
188
}
547
189
 
 
190
 
 
191
 
 
192
 
 
193
 
548
194
void
549
195
alarm_applet_ui_init (AlarmApplet *applet)
550
196
{
551
 
        GtkWidget *hbox;
552
 
        GdkPixbuf *icon;
553
 
        
554
 
        /* Set up PanelApplet signals */
555
 
        g_signal_connect (G_OBJECT(applet->parent), "button-press-event",
556
 
                                          G_CALLBACK(button_cb), applet);
557
 
        
558
 
        g_signal_connect (G_OBJECT(applet->parent), "unrealize",
559
 
                                          G_CALLBACK(unrealize_cb), applet);
560
 
        
561
 
        g_signal_connect (G_OBJECT(applet->parent), "change-background",
562
 
                                          G_CALLBACK (applet_back_change), applet);
563
 
        
564
 
        g_signal_connect (G_OBJECT(applet->parent), "change-orient",
565
 
                                          G_CALLBACK (orient_change_cb), applet);
566
 
        
567
 
        g_signal_connect (G_OBJECT(applet->parent), "size-allocate",
568
 
                                          G_CALLBACK (change_size_cb), applet);
569
 
        
570
 
        /* Set up container box */
571
 
        if (IS_HORIZONTAL (applet->parent))
572
 
                applet->box = gtk_hbox_new(FALSE, 6);
573
 
        else
574
 
                applet->box = gtk_vbox_new(FALSE, 6);
575
 
        
576
 
        /* Store orientation for future reference */
577
 
        applet->orient = panel_applet_get_orient (applet->parent);
578
 
        
579
 
        /* Set up icon and label */
580
 
        applet->icon = gtk_image_new ();
581
 
        alarm_applet_icon_update (applet);
582
 
        
583
 
        applet->label = g_object_new(GTK_TYPE_LABEL,
584
 
                                                                 "label", ALARM_DEF_LABEL,
585
 
                                                                 "use-markup", TRUE,
586
 
                                                                 "visible", applet->show_label,
587
 
                                                                 "no-show-all", TRUE,                   /* So gtk_widget_show_all() won't set visible to TRUE */
588
 
                                                                 NULL);
589
 
        
590
 
        /* Set up UI updater */
591
 
        alarm_applet_ui_update (applet);
592
 
        applet->timer_id = g_timeout_add_seconds (1, (GSourceFunc)alarm_applet_ui_update, applet);
593
 
        
594
 
        /* Pack */
595
 
        gtk_box_pack_start_defaults(GTK_BOX (applet->box), applet->icon);
596
 
        gtk_box_pack_start_defaults(GTK_BOX (applet->box), applet->label);
597
 
        
598
 
        /* Update orientation */
599
 
        update_orient (applet);
600
 
        
601
 
        /* Add to container and show */
602
 
        gtk_container_add (GTK_CONTAINER (applet->parent), applet->box);
603
 
        gtk_widget_show_all (GTK_WIDGET (applet->parent));
604
 
        
605
 
        alarm_applet_update_tooltip (applet);
606
 
}
607
 
 
608
 
 
609
 
 
610
 
 
611
 
 
612
 
static void
613
 
menu_snooze_alarm_cb (BonoboUIComponent *component,
614
 
                                         AlarmApplet *applet,
615
 
                                         const gchar *cname)
616
 
{
617
 
        g_debug("menu_snooze_alarm");
618
 
        
619
 
        alarm_applet_snooze_alarms (applet);
620
 
}
621
 
 
622
 
static void
623
 
menu_clear_alarm_cb (BonoboUIComponent *component,
624
 
                                         AlarmApplet *applet,
625
 
                                         const gchar *cname)
626
 
{
627
 
        g_debug("menu_clear_alarm");
628
 
        
629
 
        alarm_applet_clear_alarms (applet);
630
 
}
631
 
 
632
 
static void
633
 
menu_list_alarms_cb (BonoboUIComponent *component,
634
 
                                         gpointer data,
635
 
                                         const gchar *cname)
636
 
{
637
 
        AlarmApplet *applet = (AlarmApplet *)data;
638
 
        list_alarms_dialog_display (applet);
639
 
}
640
 
 
641
 
static void
642
 
menu_preferences_cb (BonoboUIComponent *component,
643
 
                                         AlarmApplet *applet,
644
 
                                         const gchar *cname)
645
 
{
646
 
        /* Construct the preferences dialog and show it here */
647
 
        g_debug("preferences_dialog");
648
 
        
649
 
        preferences_dialog_display (applet);
650
 
}
651
 
 
652
 
static void
653
 
menu_about_cb (BonoboUIComponent *component,
654
 
                           AlarmApplet *applet,
655
 
                           const gchar *cname)
656
 
{
657
 
        /* Construct the about dialog and show it here */
658
 
        g_debug("about_dialog");
659
 
        
660
 
        static const gchar *const authors[] = {
661
 
            "Johannes H. Jensen <joh@pseudoberries.com>",
662
 
            NULL
663
 
    };
664
 
    static const gchar *const documenters[] = {
665
 
            "Johannes H. Jensen <joh@pseudoberries.com>",
666
 
            NULL
667
 
    };
668
 
    static const gchar *const artists[] = {
669
 
            "Lasse Gulvåg Sætre <lassegs@gmail.com>",
670
 
            NULL
671
 
    };
672
 
 
673
 
    gtk_show_about_dialog (NULL,
674
 
                                           "program-name",      ALARM_NAME,
675
 
                                           "title",             _("About " ALARM_NAME),
676
 
                           "version",       VERSION,
677
 
                           "copyright",     "\xC2\xA9 2007 Johannes H. Jensen",
678
 
                           "website",           "http://alarm-clock.pseudoberries.com/",
679
 
                           "authors",       authors,
680
 
                           "documenters",   documenters,
681
 
                           "artists",       artists, 
682
 
                           "logo-icon-name",ALARM_ICON,
683
 
                           NULL);
684
 
}
685
 
 
686
 
/*
687
 
 * Set up menu
688
 
 */
689
 
void
690
 
alarm_applet_menu_init (AlarmApplet *applet)
691
 
{
692
 
        GtkIconInfo *info = gtk_icon_theme_lookup_icon (gtk_icon_theme_get_default(), SNOOZE_ICON, 16, 0);
693
 
        
694
 
        g_debug ("ICON: %s", gtk_icon_info_get_filename (info));
695
 
        
696
 
        static const gchar *menu_xml =
697
 
                "<popup name=\"button3\">\n"
698
 
                "   <menuitem name=\"Snooze Item\" "
699
 
                "                         verb=\"SnoozeAlarm\" "
700
 
                "                       _label=\"_Snooze alarms\" "
701
 
                "                  pixtype=\"filename\" "
702
 
                "                  pixname=\"%s\"/>\n"
703
 
                "   <menuitem name=\"Clear Item\" "
704
 
                "                         verb=\"ClearAlarm\" "
705
 
                "                       _label=\"_Clear alarms\" "
706
 
                "                  pixtype=\"stock\" "
707
 
                "                  pixname=\"gtk-clear\"/>\n"
708
 
                "   <separator/>\n"
709
 
                "   <menuitem name=\"List Alarms\" "
710
 
                "                         verb=\"ListAlarms\" "
711
 
                "                       _label=\"_Edit Alarms\" "
712
 
                "                  pixtype=\"stock\" "
713
 
                "                  pixname=\"gtk-edit\"/>\n"
714
 
                "   <menuitem name=\"Preferences Item\" "
715
 
                "             verb=\"Preferences\" "
716
 
                "           _label=\"_Preferences...\"\n"
717
 
                "          pixtype=\"stock\" "
718
 
                "          pixname=\"gtk-properties\"/>\n"
719
 
                "   <menuitem name=\"About Item\" "
720
 
                "             verb=\"About\" "
721
 
                "           _label=\"_About...\"\n"
722
 
                "          pixtype=\"stock\" "
723
 
                "          pixname=\"gtk-about\"/>\n"
724
 
                "</popup>\n";
725
 
        
726
 
        static const BonoboUIVerb menu_verbs [] = {
727
 
                        BONOBO_UI_VERB ("SnoozeAlarm", menu_snooze_alarm_cb),
728
 
                        BONOBO_UI_VERB ("ClearAlarm", menu_clear_alarm_cb),
729
 
                        BONOBO_UI_VERB ("ListAlarms", menu_list_alarms_cb),
730
 
                        BONOBO_UI_VERB ("Preferences", menu_preferences_cb),
731
 
                        BONOBO_UI_VERB ("About", menu_about_cb),
732
 
                        BONOBO_UI_VERB_END
733
 
        };
734
 
        
735
 
        gchar *xml = g_strdup_printf (menu_xml, gtk_icon_info_get_filename (info));
736
 
        
737
 
        panel_applet_setup_menu (PANEL_APPLET (applet->parent),
738
 
                                 xml,
739
 
                                 menu_verbs,
740
 
                                 applet);
741
 
        
742
 
        g_free (xml);
743
 
        gtk_icon_info_free (info);
744
 
}
745
 
 
746
 
 
 
197
    /* Load UI with GtkBuilder */
 
198
    applet->ui = alarm_applet_ui_load ("alarm-clock.ui", applet);
 
199
 
 
200
    
 
201
    /* Initialize status icon */
 
202
    alarm_applet_status_init(applet);
 
203
 
 
204
    /* Initialize libnotify */
 
205
    if (!notify_init (PACKAGE_NAME)) {
 
206
        g_critical ("Could not intialize libnotify!");
 
207
    }
 
208
 
 
209
        /* Initialize alarm list window */
 
210
        applet->list_window = alarm_list_window_new (applet);
 
211
 
 
212
    /* Initialize alarm settings dialog */
 
213
    applet->settings_dialog = alarm_settings_dialog_new (applet);
 
214
    
 
215
    /* Connect signals */
 
216
    //gtk_builder_connect_signals (applet->ui, applet);
 
217
    
 
218
    /* Initialize actions */
 
219
    alarm_applet_actions_init (applet);
 
220
 
 
221
    /* Initialize preferences dialog */
 
222
    prefs_init (applet);
 
223
}
 
224
 
 
225
/*
 
226
 * Initialize status icon
 
227
 */
 
228
static void
 
229
alarm_applet_status_init (AlarmApplet *applet)
 
230
{
 
231
    applet->status_icon = GTK_STATUS_ICON (gtk_builder_get_object (applet->ui, "status_icon"));
 
232
    applet->status_menu = GTK_WIDGET (gtk_builder_get_object (applet->ui, "status_menu"));
 
233
 
 
234
    gtk_status_icon_set_visible (applet->status_icon, TRUE);
 
235
}
 
236
 
 
237
/*
 
238
 * Update the status icon
 
239
 */
 
240
void
 
241
alarm_applet_status_update (AlarmApplet *applet)
 
242
{
 
243
    gtk_status_icon_set_blinking (applet->status_icon, applet->n_triggered > 0);
 
244
}
 
245
 
 
246
/*
 
247
 * Status icon callbacks:
 
248
 */
 
249
 
 
250
G_MODULE_EXPORT void
 
251
alarm_applet_status_activate (GtkStatusIcon *status_icon,
 
252
                                                          gpointer       user_data)
 
253
{
 
254
    AlarmApplet *applet = (AlarmApplet *)user_data;
 
255
 
 
256
    // Snooze triggered alarms if any
 
257
    if (applet->n_triggered > 0) {
 
258
        gtk_action_activate (applet->action_snooze_all);
 
259
    } else {
 
260
        // No alarms triggered, toggle list window
 
261
        gtk_action_activate (GTK_ACTION (applet->action_toggle_list_win));
 
262
    }
 
263
}
 
264
 
 
265
G_MODULE_EXPORT void
 
266
alarm_applet_status_popup (GtkStatusIcon  *status_icon,
 
267
                           guint           button,
 
268
                           guint           activate_time,
 
269
                           gpointer        user_data)
 
270
{
 
271
    AlarmApplet *applet = (AlarmApplet *)user_data;
 
272
 
 
273
    gtk_menu_popup (GTK_MENU (applet->status_menu),
 
274
                    NULL,
 
275
                    NULL,
 
276
                    gtk_status_icon_position_menu,
 
277
                    status_icon,
 
278
                    button,
 
279
                    activate_time);
 
280
}
 
281
 
 
282
/*
 
283
 * Menu callbacks:
 
284
 */
 
285
/*
 
286
G_MODULE_EXPORT void
 
287
status_menu_snooze_cb (GtkMenuItem *menuitem,
 
288
                       gpointer     user_data)
 
289
{
 
290
    AlarmApplet *applet = (AlarmApplet *)user_data;
 
291
    
 
292
        alarm_applet_alarms_snooze (applet);
 
293
}
 
294
 
 
295
G_MODULE_EXPORT void
 
296
status_menu_stop_cb (GtkMenuItem *menuitem,
 
297
                      gpointer     user_data)
 
298
{
 
299
    AlarmApplet *applet = (AlarmApplet *)user_data;
 
300
    
 
301
        alarm_applet_alarms_stop (applet);
 
302
}
 
303
*/
 
304
 
 
305
void
 
306
alarm_applet_status_menu_edit_cb (GtkMenuItem *menuitem,
 
307
                     gpointer     user_data)
 
308
{
 
309
    AlarmApplet *applet = (AlarmApplet *)user_data;
 
310
 
 
311
    if (gtk_toggle_action_get_active (applet->action_toggle_list_win)) {
 
312
        alarm_list_window_show (applet->list_window);
 
313
    } else {
 
314
        gtk_toggle_action_set_active (applet->action_toggle_list_win, TRUE);
 
315
    }
 
316
}       
 
317
 
 
318
void
 
319
alarm_applet_status_menu_prefs_cb (GtkMenuItem *menuitem,
 
320
                      gpointer     user_data)
 
321
{
 
322
    AlarmApplet *applet = (AlarmApplet *)user_data;
 
323
        
 
324
        prefs_dialog_show (applet);
 
325
}
 
326
 
 
327
void
 
328
alarm_applet_status_menu_about_cb (GtkMenuItem *menuitem,
 
329
                      gpointer     user_data)
 
330
{
 
331
    AlarmApplet *applet = (AlarmApplet *)user_data;
 
332
    gchar *title;
 
333
    
 
334
    gboolean visible;   
 
335
    GtkAboutDialog *dialog = GTK_ABOUT_DIALOG (
 
336
        gtk_builder_get_object (applet->ui, "about-dialog"));
 
337
    
 
338
    g_object_get (dialog, "visible", &visible, NULL);
 
339
 
 
340
    if (!visible) {
 
341
        // About Alarm Clock
 
342
        title = g_strdup_printf (_("About %s"), _(ALARM_NAME));
 
343
        g_object_set (G_OBJECT (dialog),
 
344
                      "program-name", _(ALARM_NAME),
 
345
                      "title", title,
 
346
                      "version", VERSION,
 
347
                      NULL);
 
348
        g_free (title);
 
349
 
 
350
        gtk_dialog_run (GTK_DIALOG (dialog));
 
351
        
 
352
    } else {
 
353
        // Already visible, present it
 
354
        gtk_window_present (GTK_WINDOW (dialog));
 
355
    }
 
356
}
747
357
 
748
358
/*
749
359
 * An error callback for MediaPlayers
750
360
 */
751
361
void
752
 
media_player_error_cb (MediaPlayer *player, GError *err, GtkWindow *parent)
 
362
media_player_error_cb (MediaPlayer *player, GError *err, gpointer data)
753
363
{
 
364
        GtkWindow *parent = GTK_WINDOW (data);
754
365
        gchar *uri, *tmp;
755
366
        
756
367
        uri = media_player_get_uri (player);
757
 
        tmp = g_strdup_printf (_("%s: %s"), uri, err->message);
 
368
        tmp = g_strdup_printf ("%s: %s", uri, err->message);
758
369
        
759
370
        g_critical (_("Could not play '%s': %s"), uri, err->message);
760
371
        display_error_dialog (_("Could not play"), tmp, parent);
764
375
}
765
376
 
766
377
 
 
378
/**
 
379
 * Alarm changed signal handler
 
380
 *
 
381
 * Here we update any actions/views, if necessary
 
382
 */
 
383
void
 
384
alarm_applet_alarm_changed (GObject *object,  GParamSpec *pspec, gpointer data)
 
385
{
 
386
    AlarmApplet *applet = (AlarmApplet *)data;
 
387
    Alarm *alarm = ALARM (object);
 
388
    const gchar *pname = pspec->name;
 
389
    
 
390
    g_debug ("AlarmApplet: Alarm '%s' %s changed", alarm->message, pname);
 
391
 
 
392
    // Update Actions
 
393
    if (g_strcmp0 (pname, "active") == 0) {
 
394
        alarm_action_update_enabled (applet);
 
395
    }
 
396
 
 
397
    // Update List Window
 
398
    if (applet->list_window && GTK_WIDGET_VISIBLE (applet->list_window->window)) {
 
399
        // Should really check that the changed param is relevant...
 
400
        alarm_list_window_alarm_update (applet->list_window, alarm);
 
401
    }
 
402
 
 
403
    // Update Settings
 
404
    if (applet->settings_dialog && applet->settings_dialog->alarm == alarm) {
 
405
        g_debug ("TODO: Update settings dialog");
 
406
    }
 
407
}
 
408
 
 
409
/**
 
410
 * Alarm 'alarm' signal handler
 
411
 *
 
412
 * Here we update any actions/views, if necessary
 
413
 */
 
414
void
 
415
alarm_applet_alarm_triggered (Alarm *alarm, gpointer data)
 
416
{
 
417
    AlarmApplet *applet = (AlarmApplet *)data;
 
418
    gchar *summary, *body;
 
419
    const gchar *icon;
 
420
 
 
421
    g_debug ("AlarmApplet: Alarm '%s' triggered", alarm->message);
 
422
    
 
423
    // Keep track of how many alarms have been triggered
 
424
    applet->n_triggered++;
 
425
 
 
426
    // Show notification
 
427
    summary = g_strdup_printf ("%s", alarm->message);
 
428
    body = g_strdup_printf (_("You can snooze or stop alarms from the Alarm Clock menu."));
 
429
    icon = (alarm->type == ALARM_TYPE_TIMER) ? TIMER_ICON : ALARM_ICON;
 
430
    alarm_applet_notification_show (applet, summary, body, icon);
 
431
    
 
432
    g_free (summary);
 
433
    g_free (body);
 
434
 
 
435
    // Update status icon
 
436
    alarm_applet_status_update (applet);
 
437
 
 
438
    // Update actions
 
439
    alarm_applet_actions_update_sensitive (applet);
 
440
}
 
441
 
 
442
/**
 
443
 * Alarm 'cleared' signal handler
 
444
 *
 
445
 * Here we update any actions/views, if necessary
 
446
 */
 
447
void
 
448
alarm_applet_alarm_cleared (Alarm *alarm, gpointer data)
 
449
{
 
450
    AlarmApplet *applet = (AlarmApplet *)data;
 
451
 
 
452
    g_debug ("AlarmApplet: Alarm '%s' cleared", alarm->message);
 
453
    
 
454
    // Keep track of how many alarms have been triggered
 
455
    applet->n_triggered--;
 
456
    
 
457
    // Update status icon
 
458
    alarm_applet_status_update (applet);
 
459
 
 
460
    // Update actions
 
461
    alarm_applet_actions_update_sensitive (applet);
 
462
}