~ubuntu-branches/ubuntu/oneiric/gdm3/oneiric

« back to all changes in this revision

Viewing changes to gui/simple-greeter/gdm-clock-widget.c

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2010-03-25 20:02:20 UTC
  • Revision ID: james.westby@ubuntu.com-20100325200220-12cap62s6p304nuh
Tags: upstream-2.29.92
ImportĀ upstreamĀ versionĀ 2.29.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 
4
 * Copyright (C) 2008 Red Hat, Inc.
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 *
 
20
 * Written by: William Jon McCann <mccann@jhu.edu>
 
21
 *             Ray Strode <rstrode@redhat.com>
 
22
 */
 
23
 
 
24
#include "config.h"
 
25
 
 
26
#include <stdlib.h>
 
27
#include <stdio.h>
 
28
#include <unistd.h>
 
29
#include <string.h>
 
30
#include <errno.h>
 
31
#include <dirent.h>
 
32
#include <sys/stat.h>
 
33
 
 
34
#include <glib.h>
 
35
#include <glib/gi18n.h>
 
36
#include <glib/gstdio.h>
 
37
#include <gtk/gtk.h>
 
38
 
 
39
#include "gdm-clock-widget.h"
 
40
 
 
41
#define GDM_CLOCK_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_CLOCK_WIDGET, GdmClockWidgetPrivate))
 
42
 
 
43
struct GdmClockWidgetPrivate
 
44
{
 
45
        GtkWidget *label;
 
46
        char      *time_format;
 
47
        char      *tooltip_format;
 
48
        guint      update_clock_id;
 
49
        guint      should_show_seconds : 1;
 
50
        guint      should_show_date : 1;
 
51
};
 
52
 
 
53
static void     gdm_clock_widget_class_init  (GdmClockWidgetClass *klass);
 
54
static void     gdm_clock_widget_init        (GdmClockWidget      *clock_widget);
 
55
static void     gdm_clock_widget_finalize    (GObject             *object);
 
56
static gboolean update_timeout_cb            (GdmClockWidget      *clock);
 
57
 
 
58
G_DEFINE_TYPE (GdmClockWidget, gdm_clock_widget, GTK_TYPE_ALIGNMENT)
 
59
 
 
60
static void
 
61
update_time_format (GdmClockWidget *clock)
 
62
{
 
63
        char       *clock_format;
 
64
        char       *tooltip_format;
 
65
 
 
66
        if (clock->priv->should_show_date && clock->priv->should_show_seconds) {
 
67
                /* translators: This is the time format to use when both
 
68
                 * the date and time with seconds are being shown together.
 
69
                 */
 
70
                clock_format = _("%a %b %e, %l:%M:%S %p");
 
71
                tooltip_format = NULL;
 
72
        } else if (clock->priv->should_show_date && !clock->priv->should_show_seconds) {
 
73
                /* translators: This is the time format to use when both
 
74
                 * the date and time without seconds are being shown together.
 
75
                 */
 
76
                clock_format = _("%a %b %e, %l:%M %p");
 
77
 
 
78
                tooltip_format = NULL;
 
79
        } else if (!clock->priv->should_show_date && clock->priv->should_show_seconds) {
 
80
                /* translators: This is the time format to use when there is
 
81
                 * no date, just weekday and time with seconds.
 
82
                 */
 
83
                clock_format = _("%a %l:%M:%S %p");
 
84
 
 
85
                /* translators: This is the time format to use for the date
 
86
                 */
 
87
                tooltip_format = _("%x");
 
88
        } else {
 
89
                /* translators: This is the time format to use when there is
 
90
                 * no date, just weekday and time without seconds.
 
91
                 */
 
92
                clock_format = _("%a %l:%M %p");
 
93
 
 
94
                tooltip_format = _("%x");
 
95
        }
 
96
 
 
97
        g_free (clock->priv->time_format);
 
98
        clock->priv->time_format = g_locale_from_utf8 (clock_format, -1, NULL, NULL, NULL);
 
99
 
 
100
        g_free (clock->priv->tooltip_format);
 
101
 
 
102
        if (tooltip_format != NULL) {
 
103
                clock->priv->tooltip_format = g_locale_from_utf8 (tooltip_format, -1, NULL, NULL, NULL);
 
104
        } else {
 
105
                clock->priv->tooltip_format = NULL;
 
106
        }
 
107
}
 
108
 
 
109
static void
 
110
update_clock (GtkLabel   *label,
 
111
              const char *clock_format,
 
112
              const char *tooltip_format)
 
113
{
 
114
        time_t     t;
 
115
        struct tm *tm;
 
116
        char       buf[256];
 
117
        char      *utf8;
 
118
 
 
119
        time (&t);
 
120
        tm = localtime (&t);
 
121
        if (tm == NULL) {
 
122
                g_warning ("Unable to get broken down local time");
 
123
                return;
 
124
        }
 
125
        if (strftime (buf, sizeof (buf), clock_format, tm) == 0) {
 
126
                g_warning ("Couldn't format time: %s", clock_format);
 
127
                strcpy (buf, "???");
 
128
        }
 
129
        utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
 
130
        gtk_label_set_text (label, utf8);
 
131
        g_free (utf8);
 
132
 
 
133
        if (tooltip_format != NULL) {
 
134
                if (strftime (buf, sizeof (buf), tooltip_format, tm) == 0) {
 
135
                        g_warning ("Couldn't format tooltip date: %s", tooltip_format);
 
136
                        strcpy (buf, "???");
 
137
                }
 
138
                utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
 
139
                gtk_widget_set_tooltip_text (GTK_WIDGET (label), utf8);
 
140
                g_free (utf8);
 
141
        } else {
 
142
                gtk_widget_set_has_tooltip (GTK_WIDGET (label), FALSE);
 
143
        }
 
144
}
 
145
 
 
146
static void
 
147
set_clock_timeout (GdmClockWidget *clock,
 
148
                   time_t          now)
 
149
{
 
150
        GTimeVal       tv;
 
151
        int            timeouttime;
 
152
 
 
153
        if (clock->priv->update_clock_id > 0) {
 
154
                g_source_remove (clock->priv->update_clock_id);
 
155
                clock->priv->update_clock_id = 0;
 
156
        }
 
157
 
 
158
        g_get_current_time (&tv);
 
159
        timeouttime = (G_USEC_PER_SEC - tv.tv_usec) / 1000 + 1;
 
160
 
 
161
        /* timeout of one minute if we don't care about the seconds */
 
162
        if (! clock->priv->should_show_seconds) {
 
163
                timeouttime += 1000 * (59 - now % 60);
 
164
        }
 
165
 
 
166
        clock->priv->update_clock_id = g_timeout_add (timeouttime,
 
167
                                                      (GSourceFunc)update_timeout_cb,
 
168
                                                      clock);
 
169
}
 
170
 
 
171
static gboolean
 
172
update_timeout_cb (GdmClockWidget *clock)
 
173
{
 
174
        time_t     new_time;
 
175
 
 
176
        time (&new_time);
 
177
 
 
178
        if (clock->priv->label != NULL) {
 
179
                update_clock (GTK_LABEL (clock->priv->label),
 
180
                              clock->priv->time_format,
 
181
                              clock->priv->tooltip_format);
 
182
        }
 
183
 
 
184
        set_clock_timeout (clock, new_time);
 
185
 
 
186
        return FALSE;
 
187
}
 
188
 
 
189
static void
 
190
remove_timeout (GdmClockWidget *clock)
 
191
{
 
192
        if (clock->priv->update_clock_id > 0) {
 
193
                g_source_remove (clock->priv->update_clock_id);
 
194
                clock->priv->update_clock_id = 0;
 
195
        }
 
196
}
 
197
 
 
198
static void
 
199
gdm_clock_widget_size_request (GtkWidget      *widget,
 
200
                               GtkRequisition *requisition)
 
201
{
 
202
        PangoFontMetrics *metrics;
 
203
        PangoContext     *context;
 
204
        int               ascent;
 
205
        int               descent;
 
206
        int               padding;
 
207
 
 
208
        if (GTK_WIDGET_CLASS (gdm_clock_widget_parent_class)->size_request) {
 
209
                GTK_WIDGET_CLASS (gdm_clock_widget_parent_class)->size_request (widget, requisition);
 
210
        }
 
211
 
 
212
        gtk_widget_ensure_style (widget);
 
213
        context = gtk_widget_get_pango_context (widget);
 
214
        metrics = pango_context_get_metrics (context,
 
215
                                             widget->style->font_desc,
 
216
                                             pango_context_get_language (context));
 
217
 
 
218
        ascent = pango_font_metrics_get_ascent (metrics);
 
219
        descent = pango_font_metrics_get_descent (metrics);
 
220
        padding = PANGO_PIXELS (ascent + descent) / 2.0;
 
221
        requisition->height += padding;
 
222
 
 
223
        pango_font_metrics_unref (metrics);
 
224
}
 
225
 
 
226
static void
 
227
gdm_clock_widget_class_init (GdmClockWidgetClass *klass)
 
228
{
 
229
        GObjectClass *object_class;
 
230
        GtkWidgetClass *widget_class;
 
231
 
 
232
        object_class = G_OBJECT_CLASS (klass);
 
233
        widget_class = GTK_WIDGET_CLASS (klass);
 
234
 
 
235
        object_class->finalize = gdm_clock_widget_finalize;
 
236
        widget_class->size_request = gdm_clock_widget_size_request;
 
237
 
 
238
        g_type_class_add_private (klass, sizeof (GdmClockWidgetPrivate));
 
239
}
 
240
 
 
241
static void
 
242
gdm_clock_widget_init (GdmClockWidget *widget)
 
243
{
 
244
        GtkWidget *box;
 
245
 
 
246
        widget->priv = GDM_CLOCK_WIDGET_GET_PRIVATE (widget);
 
247
 
 
248
        box = gtk_hbox_new (FALSE, 6);
 
249
        gtk_widget_show (box);
 
250
        gtk_container_add (GTK_CONTAINER (widget), box);
 
251
 
 
252
        widget->priv->label = gtk_label_new ("");
 
253
 
 
254
        gtk_widget_show (widget->priv->label);
 
255
        gtk_box_pack_start (GTK_BOX (box), widget->priv->label, FALSE, FALSE, 0);
 
256
 
 
257
        update_time_format (widget);
 
258
        update_timeout_cb (widget);
 
259
}
 
260
 
 
261
static void
 
262
gdm_clock_widget_finalize (GObject *object)
 
263
{
 
264
        GdmClockWidget *clock_widget;
 
265
 
 
266
        g_return_if_fail (object != NULL);
 
267
        g_return_if_fail (GDM_IS_CLOCK_WIDGET (object));
 
268
 
 
269
        clock_widget = GDM_CLOCK_WIDGET (object);
 
270
 
 
271
        g_return_if_fail (clock_widget->priv != NULL);
 
272
 
 
273
        remove_timeout (clock_widget);
 
274
 
 
275
        G_OBJECT_CLASS (gdm_clock_widget_parent_class)->finalize (object);
 
276
}
 
277
 
 
278
GtkWidget *
 
279
gdm_clock_widget_new (void)
 
280
{
 
281
        GObject *object;
 
282
 
 
283
        object = g_object_new (GDM_TYPE_CLOCK_WIDGET,
 
284
                               NULL);
 
285
 
 
286
        return GTK_WIDGET (object);
 
287
}