From a10f207618976b59a0cc43c01befbf67128c92a5 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Thu, 17 Feb 2011 14:30:43 -0500 Subject: first pass at preferences dialog --- src/utils.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/utils.c (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..69143b9 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,45 @@ +/* -*- Mode: C; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*- + +A dialog for setting time and date preferences. + +Copyright 2010 Canonical Ltd. + +Authors: + Michael Terry + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +#include +#include +#include +#include "utils.h" + +/* Check the system locale setting to see if the format is 24-hour + time or 12-hour time */ +gboolean +is_locale_12h (void) +{ + 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; +} + -- cgit v1.2.3 From b4a4c9682ca2413175386ad36d06fc4e1032badc Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Wed, 23 Feb 2011 13:28:53 -0500 Subject: grab timezone names from geomaps; flesh out support for timezone completion in main map and locations dialog; show times in locations dialog --- src/utils.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 69143b9..f73ed14 100644 --- a/src/utils.c +++ b/src/utils.c @@ -20,10 +20,17 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include #include #include #include #include "utils.h" +#include "settings-shared.h" /* Check the system locale setting to see if the format is 24-hour time or 12-hour time */ @@ -43,3 +50,184 @@ is_locale_12h (void) return TRUE; } +void +split_settings_location (const gchar * location, gchar ** zone, gchar ** name) +{ + gchar * location_dup = g_strdup (location); + gchar * first = strchr (location_dup, ' '); + + if (first) { + first[0] = 0; + } + + if (zone) { + *zone = location_dup; + } + + if (name) { + gchar * after = first ? g_strstrip (first + 1) : NULL; + if (after == NULL || after[0] == 0) { + /* Make up name from zone */ + gchar * slash = strrchr (location_dup, '/'); + after = slash ? slash + 1 : location_dup; + } + + *name = g_strdup (after); + } +} + +/* 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; +} + +/* Tries to figure out what our format string should be. Lots + of translator comments in here. */ +gchar * +generate_format_string_full (gboolean show_day, gboolean show_date) +{ + gboolean twelvehour = TRUE; + + GSettings * settings = g_settings_new (SETTINGS_INTERFACE); + gint time_mode = g_settings_get_enum (settings, SETTINGS_TIME_FORMAT_S); + gboolean show_seconds = g_settings_get_boolean (settings, SETTINGS_SHOW_SECONDS_S); + g_object_unref (settings); + + if (time_mode == SETTINGS_TIME_LOCALE) { + twelvehour = is_locale_12h(); + } else if (time_mode == SETTINGS_TIME_24_HOUR) { + twelvehour = FALSE; + } + + const gchar * time_string = NULL; + if (twelvehour) { + if (show_seconds) { + /* TRANSLATORS: A format string for the strftime function for + a clock showing 12-hour time with seconds. */ + time_string = T_("%l:%M:%S %p"); + } else { + time_string = T_(DEFAULT_TIME_12_FORMAT); + } + } else { + if (show_seconds) { + /* TRANSLATORS: A format string for the strftime function for + a clock showing 24-hour time with seconds. */ + time_string = T_("%H:%M:%S"); + } else { + time_string = T_(DEFAULT_TIME_24_FORMAT); + } + } + + /* Checkpoint, let's not fail */ + g_return_val_if_fail(time_string != NULL, g_strdup(DEFAULT_TIME_FORMAT)); + + /* If there's no date or day let's just leave now and + not worry about the rest of this code */ + if (!show_date && !show_day) { + return g_strdup(time_string); + } + + const gchar * date_string = NULL; + if (show_date && 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 = T_("%a %b %e"); + } else if (show_date) { + /* TRANSLATORS: This is a format string passed to strftime to represent + the month and the day of the month. */ + date_string = T_("%b %e"); + } else if (show_day) { + /* TRANSLATORS: This is a format string passed to strftime to represent + the day of the week. */ + date_string = T_("%a"); + } + + /* Check point, we should have a date string */ + g_return_val_if_fail(date_string != NULL, g_strdup(time_string)); + + /* 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(T_("%s, %s"), date_string, time_string); +} + +gchar * +generate_format_string_at_time (GDateTime * time) +{ + /* This is a bit less free-form than for the main "now" time label. */ + /* If it is today, just the time should be shown (e.g. “3:55 PM”) + If it is a different day this week, the day and time should be shown (e.g. “Wed 3:55 PM”) + If it is after this week, the day, date, and time should be shown (e.g. “Wed 21 Apr 3:55 PM”). + In addition, when presenting the times of upcoming events, the time should be followed by the timezone if it is different from the one the computer is currently set to. For example, “Wed 3:55 PM UTC−5”. */ + gboolean show_day = FALSE; + gboolean show_date = FALSE; + + GDateTime * now = g_date_time_new_now_local(); + + /* First, are we same day? */ + gint time_year, time_month, time_day; + gint now_year, now_month, now_day; + g_date_time_get_ymd(time, &time_year, &time_month, &time_day); + g_date_time_get_ymd(now, &now_year, &now_month, &now_day); + + if (time_year != now_year || + time_month != now_month || + time_day != now_day) { + /* OK, different days so we must at least show the day. */ + show_day = TRUE; + + /* Is it this week? */ + /* Here, we define "is this week" as yesterday, today, or the next five days */ + GDateTime * past = g_date_time_add_days(now, -1); + GDateTime * future = g_date_time_add_days(now, 5); + GDateTime * past_bound = g_date_time_new_local(g_date_time_get_year(past), + g_date_time_get_month(past), + g_date_time_get_day_of_month(past), + 0, 0, 0.0); + GDateTime * future_bound = g_date_time_new_local(g_date_time_get_year(future), + g_date_time_get_month(future), + g_date_time_get_day_of_month(future), + 23, 59, 59.9); + if (g_date_time_compare(time, past_bound) < 0 || + g_date_time_compare(time, future_bound) > 0) { + show_date = TRUE; + } + g_date_time_unref(past); + g_date_time_unref(future); + g_date_time_unref(past_bound); + g_date_time_unref(future_bound); + } + + return generate_format_string_full(show_day, show_date); +} + -- cgit v1.2.3 From b3a34d487c8c3724752b522fd352c96ba0bcf1a4 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Wed, 23 Feb 2011 14:07:19 -0500 Subject: add week-start controls and settings --- src/utils.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index f73ed14..89c499b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -50,6 +50,17 @@ is_locale_12h (void) return TRUE; } +/* Check the system locale setting to see if the week starts on Sunday or Monday */ +gboolean +is_locale_week_start_sunday (void) +{ + const char * week_1stday = nl_langinfo (_NL_TIME_WEEK_1STDAY); + + /* This appears to be a special value that libc uses for Sunday, it's not + really a string */ + return (GPOINTER_TO_INT (week_1stday) == 19971130); +} + void split_settings_location (const gchar * location, gchar ** zone, gchar ** name) { -- cgit v1.2.3 From 02a56bbe87dba95388735c9961345b5166940b1c Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Wed, 23 Feb 2011 15:13:42 -0500 Subject: disable clicking on locations; use pretty zone names in indicator --- src/utils.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 89c499b..20ae958 100644 --- a/src/utils.c +++ b/src/utils.c @@ -79,11 +79,16 @@ split_settings_location (const gchar * location, gchar ** zone, gchar ** name) gchar * after = first ? g_strstrip (first + 1) : NULL; if (after == NULL || after[0] == 0) { /* Make up name from zone */ - gchar * slash = strrchr (location_dup, '/'); - after = slash ? slash + 1 : location_dup; + gchar * chr = strrchr (location_dup, '/'); + after = g_strdup (chr ? chr + 1 : location_dup); + while ((chr = strchr (after, '_')) != NULL) { /* and turn underscores to spaces */ + *chr = ' '; + } + *name = after; + } + else { + *name = g_strdup (after); } - - *name = g_strdup (after); } } -- cgit v1.2.3 From 2a6b56d0f00d2e23ca48687fe1c81d4e88be3b28 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Fri, 25 Feb 2011 09:18:57 -0500 Subject: remove week-start preference --- src/utils.c | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 20ae958..7471926 100644 --- a/src/utils.c +++ b/src/utils.c @@ -50,17 +50,6 @@ is_locale_12h (void) return TRUE; } -/* Check the system locale setting to see if the week starts on Sunday or Monday */ -gboolean -is_locale_week_start_sunday (void) -{ - const char * week_1stday = nl_langinfo (_NL_TIME_WEEK_1STDAY); - - /* This appears to be a special value that libc uses for Sunday, it's not - really a string */ - return (GPOINTER_TO_INT (week_1stday) == 19971130); -} - void split_settings_location (const gchar * location, gchar ** zone, gchar ** name) { -- cgit v1.2.3