source: proiecte/PPPP/gdm/gui/simple-greeter/gdm-clock-widget.c @ 134

Last change on this file since 134 was 134, checked in by (none), 14 years ago

gdm sources with the modifications for webcam

File size: 7.6 KB
Line 
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
43struct GdmClockWidgetPrivate
44{
45        GtkWidget *label;
46        char      *time_format;
47        guint      update_clock_id;
48        guint      should_show_seconds : 1;
49};
50
51static void     gdm_clock_widget_class_init  (GdmClockWidgetClass *klass);
52static void     gdm_clock_widget_init        (GdmClockWidget      *clock_widget);
53static void     gdm_clock_widget_finalize    (GObject             *object);
54static gboolean update_timeout_cb            (GdmClockWidget      *clock);
55
56G_DEFINE_TYPE (GdmClockWidget, gdm_clock_widget, GTK_TYPE_ALIGNMENT)
57
58static char *
59get_time_format (GdmClockWidget *clock)
60{
61        const char *time_format;
62        const char *date_format;
63        char       *clock_format;
64        char       *result;
65
66        time_format = clock->priv->should_show_seconds ? _("%l:%M:%S %p") : _("%l:%M %p");
67        /* translators: replace %e with %d if, when the day of the
68         *              month as a decimal number is a single digit, it
69         *              should begin with a 0 in your locale (e.g. "May
70         *              01" instead of "May  1").
71         */
72        date_format = _("%a %b %e");
73        /* translators: reverse the order of these arguments
74         *              if the time should come before the
75         *              date on a clock in your locale.
76         */
77        clock_format = g_strdup_printf (_("%1$s, %2$s"),
78                                        date_format,
79                                        time_format);
80
81        result = g_locale_from_utf8 (clock_format, -1, NULL, NULL, NULL);
82        g_free (clock_format);
83
84        return result;
85}
86
87static void
88update_clock (GtkLabel   *label,
89              const char *format)
90{
91        time_t     t;
92        struct tm *tm;
93        char       buf[256];
94        char      *utf8;
95
96        time (&t);
97        tm = localtime (&t);
98        if (tm == NULL) {
99                g_warning ("Unable to get broken down local time");
100                return;
101        }
102        if (strftime (buf, sizeof (buf), format, tm) == 0) {
103                g_warning ("Couldn't format time: %s", format);
104                strcpy (buf, "???");
105        }
106        utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
107        gtk_label_set_text (label, utf8);
108        g_free (utf8);
109}
110
111static void
112set_clock_timeout (GdmClockWidget *clock,
113                   time_t          now)
114{
115        GTimeVal       tv;
116        int            timeouttime;
117
118        if (clock->priv->update_clock_id > 0) {
119                g_source_remove (clock->priv->update_clock_id);
120                clock->priv->update_clock_id = 0;
121        }
122
123        g_get_current_time (&tv);
124        timeouttime = (G_USEC_PER_SEC - tv.tv_usec) / 1000 + 1;
125
126        /* timeout of one minute if we don't care about the seconds */
127        if (! clock->priv->should_show_seconds) {
128                timeouttime += 1000 * (59 - now % 60);
129        }
130
131        clock->priv->update_clock_id = g_timeout_add (timeouttime,
132                                                      (GSourceFunc)update_timeout_cb,
133                                                      clock);
134}
135
136static gboolean
137update_timeout_cb (GdmClockWidget *clock)
138{
139        time_t     new_time;
140
141        time (&new_time);
142
143        if (clock->priv->label != NULL) {
144                update_clock (GTK_LABEL (clock->priv->label),
145                              clock->priv->time_format);
146        }
147
148        set_clock_timeout (clock, new_time);
149
150        return FALSE;
151}
152
153static void
154remove_timeout (GdmClockWidget *clock)
155{
156        if (clock->priv->update_clock_id > 0) {
157                g_source_remove (clock->priv->update_clock_id);
158                clock->priv->update_clock_id = 0;
159        }
160}
161
162static void
163gdm_clock_widget_size_request (GtkWidget      *widget,
164                               GtkRequisition *requisition)
165{
166        PangoFontMetrics *metrics;
167        PangoContext     *context;
168        int               ascent;
169        int               descent;
170        int               padding;
171
172        if (GTK_WIDGET_CLASS (gdm_clock_widget_parent_class)->size_request) {
173                GTK_WIDGET_CLASS (gdm_clock_widget_parent_class)->size_request (widget, requisition);
174        }
175
176        gtk_widget_ensure_style (widget);
177        context = gtk_widget_get_pango_context (widget);
178        metrics = pango_context_get_metrics (context,
179                                             widget->style->font_desc,
180                                             pango_context_get_language (context));
181
182        ascent = pango_font_metrics_get_ascent (metrics);
183        descent = pango_font_metrics_get_descent (metrics);
184        padding = PANGO_PIXELS (ascent + descent) / 2.0;
185        requisition->height += padding;
186
187        pango_font_metrics_unref (metrics);
188}
189
190static void
191gdm_clock_widget_class_init (GdmClockWidgetClass *klass)
192{
193        GObjectClass *object_class;
194        GtkWidgetClass *widget_class;
195
196        object_class = G_OBJECT_CLASS (klass);
197        widget_class = GTK_WIDGET_CLASS (klass);
198
199        object_class->finalize = gdm_clock_widget_finalize;
200        widget_class->size_request = gdm_clock_widget_size_request;
201
202        g_type_class_add_private (klass, sizeof (GdmClockWidgetPrivate));
203}
204
205static void
206gdm_clock_widget_init (GdmClockWidget *widget)
207{
208        GtkWidget *box;
209
210        widget->priv = GDM_CLOCK_WIDGET_GET_PRIVATE (widget);
211
212        box = gtk_hbox_new (FALSE, 6);
213        gtk_widget_show (box);
214        gtk_container_add (GTK_CONTAINER (widget), box);
215
216        widget->priv->label = gtk_label_new ("");
217        gtk_widget_show (widget->priv->label);
218        gtk_box_pack_start (GTK_BOX (box), widget->priv->label, FALSE, FALSE, 0);
219
220        widget->priv->time_format = get_time_format (widget);
221        update_timeout_cb (widget);
222}
223
224static void
225gdm_clock_widget_finalize (GObject *object)
226{
227        GdmClockWidget *clock_widget;
228
229        g_return_if_fail (object != NULL);
230        g_return_if_fail (GDM_IS_CLOCK_WIDGET (object));
231
232        clock_widget = GDM_CLOCK_WIDGET (object);
233
234        g_return_if_fail (clock_widget->priv != NULL);
235
236        remove_timeout (clock_widget);
237
238        G_OBJECT_CLASS (gdm_clock_widget_parent_class)->finalize (object);
239}
240
241GtkWidget *
242gdm_clock_widget_new (void)
243{
244        GObject *object;
245
246        object = g_object_new (GDM_TYPE_CLOCK_WIDGET,
247                               NULL);
248
249        return GTK_WIDGET (object);
250}
Note: See TracBrowser for help on using the repository browser.