diff options
author | karl-qdh <karl@qdh.org.uk> | 2011-01-19 15:27:22 +0000 |
---|---|---|
committer | karl-qdh <karl@qdh.org.uk> | 2011-01-19 15:27:22 +0000 |
commit | 648676e3e3a6f9d4d08b396980712f651763c5a4 (patch) | |
tree | c5c056edf8bdedce5583c5fb082d94bea77e19ed /src/indicator-datetime.c | |
parent | add30ba3ce73f5b10f592cc1cd23132a9c04d980 (diff) | |
parent | cd14524ec56a149b9c9c9fa780698ea42738cb61 (diff) | |
download | ayatana-indicator-datetime-648676e3e3a6f9d4d08b396980712f651763c5a4.tar.gz ayatana-indicator-datetime-648676e3e3a6f9d4d08b396980712f651763c5a4.tar.bz2 ayatana-indicator-datetime-648676e3e3a6f9d4d08b396980712f651763c5a4.zip |
Merged gdbus etc... from trunk
Diffstat (limited to 'src/indicator-datetime.c')
-rw-r--r-- | src/indicator-datetime.c | 282 |
1 files changed, 208 insertions, 74 deletions
diff --git a/src/indicator-datetime.c b/src/indicator-datetime.c index c6dc2c1..5d92296 100644 --- a/src/indicator-datetime.c +++ b/src/indicator-datetime.c @@ -23,15 +23,16 @@ with this program. If not, see <http://www.gnu.org/licenses/>. #include "config.h" #endif +#include <locale.h> +#include <langinfo.h> +#include <string.h> + /* GStuff */ #include <glib.h> #include <glib-object.h> #include <glib/gi18n-lib.h> #include <gio/gio.h> -/* DBus Stuff */ -#include <dbus/dbus-glib.h> - /* Indicator Stuff */ #include <libindicator/indicator.h> #include <libindicator/indicator-object.h> @@ -76,6 +77,7 @@ struct _IndicatorDatetimePrivate { gboolean show_date; gboolean show_day; gchar * custom_string; + gboolean custom_show_seconds; guint idle_measure; gint max_width; @@ -83,7 +85,8 @@ struct _IndicatorDatetimePrivate { IndicatorServiceManager * sm; DbusmenuGtkMenu * menu; - DBusGProxy * service_proxy; + GCancellable * service_proxy_cancel; + GDBusProxy * service_proxy; IdoCalendarMenuItem *ido_calendar; GSettings * settings; @@ -127,13 +130,31 @@ enum { SETTINGS_TIME_CUSTOM = 3 }; -#define DEFAULT_TIME_12_FORMAT "%l:%M %p" -#define DEFAULT_TIME_24_FORMAT "%H:%M" +/* TRANSLATORS: A format string for the strftime function for + a clock showing 12-hour time without seconds. */ +#define DEFAULT_TIME_12_FORMAT N_("%l:%M %p") + +/* TRANSLATORS: A format string for the strftime function for + a clock showing 24-hour time without seconds. */ +#define DEFAULT_TIME_24_FORMAT N_("%H:%M") + #define DEFAULT_TIME_FORMAT DEFAULT_TIME_12_FORMAT #define INDICATOR_DATETIME_GET_PRIVATE(o) \ (G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_DATETIME_TYPE, IndicatorDatetimePrivate)) +enum { + STRFTIME_MASK_NONE = 0, /* Hours or minutes as we always test those */ + STRFTIME_MASK_SECONDS = 1 << 0, /* Seconds count */ + STRFTIME_MASK_AMPM = 1 << 1, /* AM/PM counts */ + STRFTIME_MASK_WEEK = 1 << 2, /* Day of the week maters (Sat, Sun, etc.) */ + STRFTIME_MASK_DAY = 1 << 3, /* Day of the month counts (Feb 1st) */ + STRFTIME_MASK_MONTH = 1 << 4, /* Which month matters */ + STRFTIME_MASK_YEAR = 1 << 5, /* Which year matters */ + /* Last entry, combines all previous */ + STRFTIME_MASK_ALL = (STRFTIME_MASK_SECONDS | STRFTIME_MASK_AMPM | STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR) +}; + GType indicator_datetime_get_type (void); static void indicator_datetime_class_init (IndicatorDatetimeClass *klass); @@ -150,7 +171,10 @@ static gchar * generate_format_string (IndicatorDatetime * self); static struct tm * update_label (IndicatorDatetime * io); static void guess_label_size (IndicatorDatetime * self); static void setup_timer (IndicatorDatetime * self, struct tm * ltime); -static void update_time (DBusGProxy * proxy, gpointer user_data); +static void update_time (IndicatorDatetime * self); +static void receive_signal (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name, GVariant * parameters, gpointer user_data); +static void service_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data); +static gint generate_strftime_bitmask (const char *time_str); /* Indicator Module Config */ INDICATOR_SET_VERSION @@ -235,6 +259,7 @@ indicator_datetime_init (IndicatorDatetime *self) self->priv->show_date = FALSE; self->priv->show_day = FALSE; self->priv->custom_string = g_strdup(DEFAULT_TIME_FORMAT); + self->priv->custom_show_seconds = FALSE; self->priv->time_string = generate_format_string(self); @@ -279,21 +304,53 @@ indicator_datetime_init (IndicatorDatetime *self) self->priv->sm = indicator_service_manager_new_version(SERVICE_NAME, SERVICE_VERSION); - DBusGConnection * session = dbus_g_bus_get(DBUS_BUS_SESSION, NULL); - if (session != NULL) { - self->priv->service_proxy = dbus_g_proxy_new_for_name(session, - SERVICE_NAME, - SERVICE_OBJ, - SERVICE_IFACE); - - dbus_g_proxy_add_signal(self->priv->service_proxy, "UpdateTime", G_TYPE_INVALID); - dbus_g_proxy_connect_signal(self->priv->service_proxy, - "UpdateTime", - G_CALLBACK(update_time), - self, - NULL); + self->priv->service_proxy_cancel = g_cancellable_new(); + + g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SERVICE_NAME, + SERVICE_OBJ, + SERVICE_IFACE, + self->priv->service_proxy_cancel, + service_proxy_cb, + self); + + return; +} + +/* Callback from trying to create the proxy for the serivce, this + could include starting the service. Sometime it'll fail and + we'll try to start that dang service again! */ +static void +service_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data) +{ + GError * error = NULL; + + IndicatorDatetime * self = INDICATOR_DATETIME(user_data); + g_return_if_fail(self != NULL); + + GDBusProxy * proxy = g_dbus_proxy_new_for_bus_finish(res, &error); + + IndicatorDatetimePrivate * priv = INDICATOR_DATETIME_GET_PRIVATE(self); + + if (priv->service_proxy_cancel != NULL) { + g_object_unref(priv->service_proxy_cancel); + priv->service_proxy_cancel = NULL; } + if (error != NULL) { + g_error("Could not grab DBus proxy for %s: %s", SERVICE_NAME, error->message); + g_error_free(error); + return; + } + + /* Okay, we're good to grab the proxy at this point, we're + sure that it's ours. */ + priv->service_proxy = proxy; + + g_signal_connect(proxy, "g-signal", G_CALLBACK(receive_signal), self); + return; } @@ -414,6 +471,7 @@ set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec if (newval != self->priv->time_mode) { update = TRUE; self->priv->time_mode = newval; + setup_timer(self, NULL); } break; } @@ -450,6 +508,8 @@ set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec self->priv->custom_string = NULL; } self->priv->custom_string = g_strdup(newstr); + gint time_mask = generate_strftime_bitmask(newstr); + self->priv->custom_show_seconds = (time_mask & STRFTIME_MASK_SECONDS); if (self->priv->time_mode == SETTINGS_TIME_CUSTOM) { update = TRUE; setup_timer(self, NULL); @@ -549,9 +609,10 @@ update_label (IndicatorDatetime * io) if (self->priv->label == NULL) return NULL; - gchar longstr[128]; + gchar longstr[256]; time_t t; struct tm *ltime; + gboolean use_markup; t = time(NULL); ltime = localtime(&t); @@ -561,10 +622,18 @@ update_label (IndicatorDatetime * io) return NULL; } - strftime(longstr, 128, self->priv->time_string, ltime); + strftime(longstr, 256, self->priv->time_string, ltime); gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL); - gtk_label_set_label(self->priv->label, utf8); + + if (pango_parse_markup(utf8, -1, 0, NULL, NULL, NULL, NULL)) + use_markup = TRUE; + + if (use_markup) + gtk_label_set_markup(self->priv->label, utf8); + else + gtk_label_set_text(self->priv->label, utf8); + g_free(utf8); if (self->priv->idle_measure == 0) { @@ -574,17 +643,29 @@ update_label (IndicatorDatetime * io) return ltime; } -/* Recieves the signal from the service that we should update - the time right now. Usually from a timezone switch. */ +/* Update the time right now. Usually the result of a timezone switch. */ static void -update_time (DBusGProxy * proxy, gpointer user_data) +update_time (IndicatorDatetime * self) { - IndicatorDatetime * self = INDICATOR_DATETIME(user_data); struct tm * ltime = update_label(self); setup_timer(self, ltime); return; } +/* Receives all signals from the service, routed to the appropriate functions */ +static void +receive_signal (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name, + GVariant * parameters, gpointer user_data) +{ + IndicatorDatetime * self = INDICATOR_DATETIME(user_data); + + if (g_strcmp0(signal_name, "UpdateTime") == 0) { + update_time(self); + } + + return; +} + /* Runs every minute and updates the time */ gboolean timer_func (gpointer user_data) @@ -605,7 +686,8 @@ setup_timer (IndicatorDatetime * self, struct tm * ltime) self->priv->timer = 0; } - if (self->priv->show_seconds) { + if (self->priv->show_seconds || + (self->priv->time_mode == SETTINGS_TIME_CUSTOM && self->priv->custom_show_seconds)) { self->priv->timer = g_timeout_add_seconds(1, timer_func, self); } else { if (ltime == NULL) { @@ -627,7 +709,12 @@ static gint measure_string (GtkStyle * style, PangoContext * context, const gchar * string) { PangoLayout * layout = pango_layout_new(context); - pango_layout_set_text(layout, string, -1); + + if (pango_parse_markup(string, -1, 0, NULL, NULL, NULL, NULL)) + pango_layout_set_markup(layout, string, -1); + else + pango_layout_set_text(layout, string, -1); + pango_layout_set_font_description(layout, style->font_desc); gint width; @@ -644,18 +731,6 @@ struct _strftime_type_t { gint mask; }; -enum { - STRFTIME_MASK_NONE = 0, /* Hours or minutes as we always test those */ - STRFTIME_MASK_SECONDS = 1 << 0, /* Seconds count */ - STRFTIME_MASK_AMPM = 1 << 1, /* AM/PM counts */ - STRFTIME_MASK_WEEK = 1 << 2, /* Day of the week maters (Sat, Sun, etc.) */ - STRFTIME_MASK_DAY = 1 << 3, /* Day of the month counts (Feb 1st) */ - STRFTIME_MASK_MONTH = 1 << 4, /* Which month matters */ - STRFTIME_MASK_YEAR = 1 << 5, /* Which year matters */ - /* Last entry, combines all previous */ - STRFTIME_MASK_ALL = (STRFTIME_MASK_SECONDS | STRFTIME_MASK_AMPM | STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR) -}; - /* A table taken from the man page of strftime to what the different characters can effect. These are worst case in that we need to test the length based on all these things to ensure that we have @@ -701,21 +776,21 @@ const static strftime_type_t strftime_type[] = { ensure that we can figure out which of the things we need to check in determining the length. */ static gint -generate_strftime_bitmask (IndicatorDatetime * self) +generate_strftime_bitmask (const char *time_str) { gint retval = 0; - glong strlength = g_utf8_strlen(self->priv->time_string, -1); + glong strlength = g_utf8_strlen(time_str, -1); gint i; - g_debug("Evaluating bitmask for '%s'", self->priv->time_string); + g_debug("Evaluating bitmask for '%s'", time_str); for (i = 0; i < strlength; i++) { - if (self->priv->time_string[i] == '%' && i + 1 < strlength) { - gchar evalchar = self->priv->time_string[i + 1]; + if (time_str[i] == '%' && i + 1 < strlength) { + gchar evalchar = time_str[i + 1]; /* If we're using alternate formats we need to skip those characters */ if (evalchar == 'E' || evalchar == 'O') { if (i + 2 < strlength) { - evalchar = self->priv->time_string[i + 2]; + evalchar = time_str[i + 2]; } else { continue; } @@ -809,7 +884,10 @@ guess_label_size (IndicatorDatetime * self) GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(self->priv->label)); PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(self->priv->label)); gint * max_width = &(self->priv->max_width); - gint posibilitymask = generate_strftime_bitmask(self); + gint posibilitymask = generate_strftime_bitmask(self->priv->time_string); + + /* Reset max width */ + *max_width = 0; /* Build the array of possibilities that we want to test */ GArray * timevals = g_array_new(FALSE, TRUE, sizeof(struct tm)); @@ -818,9 +896,9 @@ guess_label_size (IndicatorDatetime * self) g_debug("Checking against %d posible times", timevals->len); gint check_time; for (check_time = 0; check_time < timevals->len; check_time++) { - gchar longstr[128]; - strftime(longstr, 128, self->priv->time_string, &(g_array_index(timevals, struct tm, check_time))); - + gchar longstr[256]; + strftime(longstr, 256, self->priv->time_string, &(g_array_index(timevals, struct tm, check_time))); + gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL); gint length = measure_string(style, context, utf8); g_free(utf8); @@ -850,6 +928,74 @@ style_changed (GtkWidget * widget, GtkStyle * oldstyle, gpointer data) return; } +/* Translate msg according to the locale specified by LC_TIME */ +static char * +T_(const char *msg) +{ + /* General strategy here is to make sure LANGUAGE is empty (since that + trumps all LC_* vars) and then to temporarily swap LC_TIME and + LC_MESSAGES. Then have gettext translate msg. + + We strdup the strings because the setlocale & *env functions do not + guarantee anything about the storage used for the string, and thus + the string may not be portably safe after multiple calls. + + Note that while you might think g_dcgettext would do the trick here, + that actually looks in /usr/share/locale/XX/LC_TIME, not the + LC_MESSAGES directory, so we won't find any translation there. + */ + char *message_locale = g_strdup(setlocale(LC_MESSAGES, NULL)); + char *time_locale = g_strdup(setlocale(LC_TIME, NULL)); + char *language = g_strdup(g_getenv("LANGUAGE")); + char *rv; + g_unsetenv("LANGUAGE"); + setlocale(LC_MESSAGES, time_locale); + + /* Get the LC_TIME version */ + rv = _(msg); + + /* Put everything back the way it was */ + setlocale(LC_MESSAGES, message_locale); + g_setenv("LANGUAGE", language, TRUE); + g_free(message_locale); + g_free(time_locale); + g_free(language); + return rv; +} + +/* Check the system locale setting to see if the format is 24-hour + time or 12-hour time */ +static gboolean +is_locale_12h() +{ + static const char *formats_24h[] = {"%H", "%R", "%T", "%OH", "%k", NULL}; + const char *t_fmt = nl_langinfo(T_FMT); + int i; + + for (i = 0; formats_24h[i]; ++i) { + if (strstr(t_fmt, formats_24h[i])) { + return FALSE; + } + } + + return TRUE; +} + +/* Respond to changes in the screen to update the text gravity */ +static void +update_text_gravity (GtkWidget *widget, GdkScreen *previous_screen, gpointer data) +{ + IndicatorDatetime * self = INDICATOR_DATETIME(data); + if (self->priv->label == NULL) return; + + PangoLayout *layout; + PangoContext *context; + + layout = gtk_label_get_layout (GTK_LABEL(self->priv->label)); + context = pango_layout_get_context(layout); + pango_context_set_base_gravity(context, PANGO_GRAVITY_AUTO); +} + /* Tries to figure out what our format string should be. Lots of translator comments in here. */ static gchar * @@ -862,17 +1008,7 @@ generate_format_string (IndicatorDatetime * self) gboolean twelvehour = TRUE; if (self->priv->time_mode == SETTINGS_TIME_LOCALE) { - /* TRANSLATORS: This string is used to determine the default - clock style for your locale. If it is the string '12' then - the default will be a 12-hour clock using AM/PM string. If - it is '24' then it will be a 24-hour clock. Users may over - ride this setting so it's still important to translate the - other strings no matter how this is set. */ - const gchar * locale_default = _("12"); - - if (g_strcmp0(locale_default, "24") == 0) { - twelvehour = FALSE; - } + twelvehour = is_locale_12h(); } else if (self->priv->time_mode == SETTINGS_TIME_24_HOUR) { twelvehour = FALSE; } @@ -882,21 +1018,17 @@ generate_format_string (IndicatorDatetime * self) if (self->priv->show_seconds) { /* TRANSLATORS: A format string for the strftime function for a clock showing 12-hour time with seconds. */ - time_string = _("%l:%M:%S %p"); + time_string = T_("%l:%M:%S %p"); } else { - /* TRANSLATORS: A format string for the strftime function for - a clock showing 12-hour time. */ - time_string = _(DEFAULT_TIME_12_FORMAT); + time_string = T_(DEFAULT_TIME_12_FORMAT); } } else { if (self->priv->show_seconds) { /* TRANSLATORS: A format string for the strftime function for a clock showing 24-hour time with seconds. */ - time_string = _("%H:%M:%S"); + time_string = T_("%H:%M:%S"); } else { - /* TRANSLATORS: A format string for the strftime function for - a clock showing 24-hour time. */ - time_string = _(DEFAULT_TIME_24_FORMAT); + time_string = T_(DEFAULT_TIME_24_FORMAT); } } @@ -913,15 +1045,15 @@ generate_format_string (IndicatorDatetime * self) if (self->priv->show_date && self->priv->show_day) { /* TRANSLATORS: This is a format string passed to strftime to represent the day of the week, the month and the day of the month. */ - date_string = _("%a %b %e"); + date_string = T_("%a %b %e"); } else if (self->priv->show_date) { /* TRANSLATORS: This is a format string passed to strftime to represent the month and the day of the month. */ - date_string = _("%b %e"); + date_string = T_("%b %e"); } else if (self->priv->show_day) { /* TRANSLATORS: This is a format string passed to strftime to represent the day of the week. */ - date_string = _("%a"); + date_string = T_("%a"); } /* Check point, we should have a date string */ @@ -930,7 +1062,7 @@ generate_format_string (IndicatorDatetime * self) /* TRANSLATORS: This is a format string passed to strftime to combine the date and the time. The value of "%s, %s" would result in a string like this in US English 12-hour time: 'Fri Jul 16, 11:50 AM' */ - return g_strdup_printf(_("%s, %s"), date_string, time_string); + return g_strdup_printf(T_("%s, %s"), date_string, time_string); } /* Whenever we have a property change on a DbusmenuMenuitem @@ -1101,8 +1233,10 @@ get_label (IndicatorObject * io) /* If there's not a label, we'll build ourselves one */ if (self->priv->label == NULL) { self->priv->label = GTK_LABEL(gtk_label_new("Time")); + gtk_label_set_justify (GTK_LABEL(self->priv->label), GTK_JUSTIFY_CENTER); g_object_ref(G_OBJECT(self->priv->label)); g_signal_connect(G_OBJECT(self->priv->label), "style-set", G_CALLBACK(style_changed), self); + g_signal_connect(G_OBJECT(self->priv->label), "screen-changed", G_CALLBACK(update_text_gravity), self); guess_label_size(self); update_label(self); gtk_widget_show(GTK_WIDGET(self->priv->label)); |