~ubuntu-branches/ubuntu/raring/gdm/raring

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Gunnar Hjalmarsson
  • Date: 2012-01-31 17:45:20 UTC
  • Revision ID: package-import@ubuntu.com-20120131174520-5y15hrfu5v9e6orf
Tags: 3.0.4-0ubuntu15
* debian/patches/45_time_display_on_greeter.patch:
  - Adapt to forwarded version of the patch.
  - Let the LANG environment variable serve as fallback when
    determining the display language for the weekday, since LANG
    has been redefined in Ubuntu to represent language instead of
    regional formats. This change also ought to make the patch work
    as intended upstream.
* debian/gdm.upstart:
  - No need to export LC_MESSAGES since it's not set any longer.
* debian/patches/ubuntu_no_LANG_setting_in_Xsession.patch:
  - Renamed from "ubuntu_i18n_oneiric.patch".
  - No need to unset LC_MESSAGES etc. since they are not set any
    longer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include <dirent.h>
32
32
#include <sys/stat.h>
33
33
#include <locale.h>
 
34
#include <langinfo.h>
34
35
 
35
36
#include <glib.h>
36
37
#include <glib/gi18n.h>
47
48
        char      *time_format;
48
49
        char      *tooltip_format;
49
50
        guint      update_clock_id;
50
 
        guint      should_show_seconds : 1;
51
 
        guint      should_show_date : 1;
52
51
};
53
52
 
54
53
static void     gdm_clock_widget_class_init  (GdmClockWidgetClass *klass);
58
57
 
59
58
G_DEFINE_TYPE (GdmClockWidget, gdm_clock_widget, GTK_TYPE_ALIGNMENT)
60
59
 
 
60
gboolean
 
61
is_24h (void)
 
62
{
 
63
        static const char *formats_24h[] = {"%H", "%R", "%T", "%OH", "%k", NULL};
 
64
        const char        *t_fmt = nl_langinfo(T_FMT);
 
65
        int                i;
 
66
 
 
67
        for (i = 0; formats_24h[i]; ++i) {
 
68
                if (strstr (t_fmt, formats_24h[i]) != NULL) {
 
69
                        return TRUE;
 
70
                }
 
71
        }
 
72
        return FALSE;
 
73
}
 
74
 
61
75
static void
62
 
update_time_format (GdmClockWidget *clock)
 
76
update_clock_format (GdmClockWidget *clock)
63
77
{
64
 
        time_t      t;
65
 
        struct tm  *tm;
66
 
        char        ampm[16];
67
 
        char       *clock_format;
 
78
        char       *time_format;
68
79
        char       *tooltip_format;
69
80
 
70
 
        time (&t);
71
 
        tm = localtime (&t);
72
 
        setlocale(LC_TIME, "");
73
 
        strftime(ampm, sizeof(ampm), "%p", tm);
74
 
 
75
 
        if (strlen(ampm) > 0) {
76
 
                clock_format = "%l:%M %p";
 
81
        setlocale (LC_TIME, "");
 
82
        if (is_24h ()) {
 
83
                time_format = "%H:%M";
77
84
        } else {
78
 
                clock_format = "%H:%M";
 
85
                time_format = "%l:%M %p";
79
86
        }
80
87
        tooltip_format = "%x";
81
88
 
82
89
        g_free (clock->priv->time_format);
83
 
        clock->priv->time_format = g_locale_from_utf8 (clock_format, -1, NULL, NULL, NULL);
 
90
        clock->priv->time_format = g_locale_from_utf8 (time_format, -1, NULL, NULL, NULL);
84
91
 
85
92
        g_free (clock->priv->tooltip_format);
86
 
 
87
 
        if (tooltip_format != NULL) {
88
 
                clock->priv->tooltip_format = g_locale_from_utf8 (tooltip_format, -1, NULL, NULL, NULL);
89
 
        } else {
90
 
                clock->priv->tooltip_format = NULL;
91
 
        }
 
93
        clock->priv->tooltip_format = g_locale_from_utf8 (tooltip_format, -1, NULL, NULL, NULL);
92
94
}
93
95
 
94
96
static void
95
97
update_clock (GtkLabel   *label,
96
 
              const char *clock_format,
 
98
              const char *time_format,
97
99
              const char *tooltip_format)
98
100
{
99
101
        time_t     t;
100
102
        struct tm *tm;
101
 
        char       showed_time[32];
 
103
        char       displayed_time[32];
 
104
        char      *msg_locale;
102
105
        char       weekday[32];
103
106
        char       buf[256];
104
107
        char      *utf8;
110
113
                g_warning ("Unable to get broken down local time");
111
114
                return;
112
115
        }
113
 
        if (strftime (showed_time, sizeof (showed_time), clock_format, tm) == 0) {
114
 
                g_warning ("Couldn't format time: %s", clock_format);
115
 
                strcpy (showed_time, "???");
 
116
        if (strftime (displayed_time, sizeof (displayed_time), time_format, tm) == 0) {
 
117
                g_warning ("Couldn't format time: %s", time_format);
 
118
                strcpy (displayed_time, "???");
116
119
        }
117
120
 
118
 
        setlocale( LC_TIME, getenv("LC_MESSAGES") );
119
 
        strftime(weekday, sizeof(weekday), "%a", tm);
120
 
        sprintf(buf, "%s %s", weekday, showed_time);
 
121
        if ((msg_locale = getenv ("LC_MESSAGES")) == NULL)
 
122
                msg_locale = getenv ("LANG");
 
123
        setlocale (LC_TIME, msg_locale);
 
124
        if (strftime (weekday, sizeof (weekday), "%a", tm) == 0) {
 
125
                g_warning ("Couldn't format weekday: %%a");
 
126
                strcpy (weekday, "???");
 
127
        }
 
128
        sprintf (buf, "%s %s", weekday, displayed_time);
121
129
 
122
130
        utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
123
131
        markup = g_strdup_printf ("<b><span foreground=\"white\">%s</span></b>", utf8);
125
133
        g_free (markup);
126
134
        g_free (utf8);
127
135
 
128
 
        if (tooltip_format != NULL) {
129
 
                setlocale(LC_TIME, "");
130
 
                if (strftime (buf, sizeof (buf), tooltip_format, tm) == 0) {
131
 
                        g_warning ("Couldn't format tooltip date: %s", tooltip_format);
132
 
                        strcpy (buf, "???");
133
 
                }
134
 
                utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
135
 
                gtk_widget_set_tooltip_text (GTK_WIDGET (label), utf8);
136
 
                g_free (utf8);
137
 
        } else {
138
 
                gtk_widget_set_has_tooltip (GTK_WIDGET (label), FALSE);
 
136
        setlocale (LC_TIME, "");
 
137
        if (strftime (buf, sizeof (buf), tooltip_format, tm) == 0) {
 
138
                g_warning ("Couldn't format tooltip date: %s", tooltip_format);
 
139
                strcpy (buf, "???");
139
140
        }
 
141
        utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
 
142
        gtk_widget_set_tooltip_text (GTK_WIDGET (label), utf8);
 
143
        g_free (utf8);
140
144
}
141
145
 
142
146
static void
153
157
 
154
158
        g_get_current_time (&tv);
155
159
        timeouttime = (G_USEC_PER_SEC - tv.tv_usec) / 1000 + 1;
156
 
 
157
 
        /* timeout of one minute if we don't care about the seconds */
158
 
        if (! clock->priv->should_show_seconds) {
159
 
                timeouttime += 1000 * (59 - now % 60);
160
 
        }
 
160
        timeouttime += 1000 * (59 - now % 60);
161
161
 
162
162
        clock->priv->update_clock_id = g_timeout_add (timeouttime,
163
163
                                                      (GSourceFunc)update_timeout_cb,
274
274
        gtk_widget_show (widget->priv->label);
275
275
        gtk_box_pack_start (GTK_BOX (box), widget->priv->label, FALSE, FALSE, 0);
276
276
 
277
 
        update_time_format (widget);
 
277
        update_clock_format (widget);
278
278
        update_timeout_cb (widget);
279
279
}
280
280