aboutsummaryrefslogtreecommitdiffstats
path: root/applets/clock/clock.c
blob: a71f895c34f0415ea7038bd131471fa908500a71 (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */

/*
 * (C) 2006-2013 Intel Corp
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *         Ross Burton <ross.burton@intel.com>
 *
 * Licensed under the GPL v2 or greater.
 */

#include <config.h>
#include <time.h>
#include <gtk/gtk.h>
#include <matchbox-panel/mb-panel.h>

typedef struct {
        GtkLabel *label;

        guint timeout_id;
} ClockApplet;

static void
clock_applet_free (ClockApplet *applet)
{
        g_source_remove (applet->timeout_id);

        g_slice_free (ClockApplet, applet);
}

/* Called every minute */
static gboolean
timeout (ClockApplet *applet)
{
        time_t t;
        char str[6], *markup;

        t = time (NULL);
        strftime (str, sizeof (str), "%H:%M", localtime (&t));

        markup = g_strdup_printf("<b>%s</b>", str);

        gtk_label_set_markup (applet->label, markup);

        g_free (markup);

        /* Keep going */
        return TRUE;
}

/* Called on the next minute after applet creation */
static gboolean
initial_timeout (ClockApplet *applet)
{
        timeout (applet);

        /* Install a new timeout that is called every minute */
        applet->timeout_id = g_timeout_add_seconds (60,
                                                    (GSourceFunc) timeout,
                                                    applet);

        /* Don't call this again */
        return FALSE;
}

G_MODULE_EXPORT GtkWidget *
mb_panel_applet_create (const char    *id,
                        GtkOrientation orientation)
{
        ClockApplet *applet;
        GtkWidget *label;
        time_t t;
        struct tm *local_time;

        applet = g_slice_new0 (ClockApplet);

        label = gtk_label_new (NULL);
        applet->label = GTK_LABEL (label);

        gtk_widget_set_name (label, "MatchboxPanelClock");

        g_object_weak_ref (G_OBJECT (label),
                           (GWeakNotify) clock_applet_free,
                           applet);

        if (orientation == GTK_ORIENTATION_VERTICAL) {
                gtk_label_set_angle (GTK_LABEL (label), 90.0);
        }

        /* Set up a timeout to be called when we hit the next minute */
        t = time (NULL);
        local_time = localtime (&t);

        applet->timeout_id = g_timeout_add_seconds (60 - local_time->tm_sec,
                                                    (GSourceFunc) initial_timeout,
                                                    applet);

        timeout (applet);

        gtk_widget_show (label);

        return label;
};