aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Terry <mike@mterry.name>2011-02-23 13:28:53 -0500
committerMichael Terry <mike@mterry.name>2011-02-23 13:28:53 -0500
commitb4a4c9682ca2413175386ad36d06fc4e1032badc (patch)
tree01676389bb60015b5b33088c8b15009f1aa251bb /src
parentdeafbc1da6b3c29e04455e46414342bcb9841c2a (diff)
downloadayatana-indicator-datetime-b4a4c9682ca2413175386ad36d06fc4e1032badc.tar.gz
ayatana-indicator-datetime-b4a4c9682ca2413175386ad36d06fc4e1032badc.tar.bz2
ayatana-indicator-datetime-b4a4c9682ca2413175386ad36d06fc4e1032badc.zip
grab timezone names from geomaps; flesh out support for timezone completion in main map and locations dialog; show times in locations dialog
Diffstat (limited to 'src')
-rw-r--r--src/datetime-prefs-locations.c147
-rw-r--r--src/datetime-prefs-locations.h3
-rw-r--r--src/datetime-prefs.c29
-rw-r--r--src/indicator-datetime.c173
-rw-r--r--src/settings-shared.h19
-rw-r--r--src/timezone-completion.c227
-rw-r--r--src/timezone-completion.h12
-rw-r--r--src/utils.c188
-rw-r--r--src/utils.h3
9 files changed, 602 insertions, 199 deletions
diff --git a/src/datetime-prefs-locations.c b/src/datetime-prefs-locations.c
index b13d082..e29580f 100644
--- a/src/datetime-prefs-locations.c
+++ b/src/datetime-prefs-locations.c
@@ -24,11 +24,13 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
#include "config.h"
#endif
-#include <glib/gi18n.h>
+#include <stdlib.h>
+#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include "datetime-prefs-locations.h"
#include "settings-shared.h"
+#include "utils.h"
#include "timezone-completion.h"
#define DATETIME_DIALOG_UI_FILE PKGDATADIR "/datetime-dialog.ui"
@@ -86,14 +88,105 @@ handle_edit (GtkCellRendererText * renderer, gchar * path, gchar * new_text,
}
}
+static gboolean
+timezone_selected (GtkEntryCompletion * widget, GtkTreeModel * model,
+ GtkTreeIter * iter, gpointer user_data)
+{
+ GValue zone_value = {0}, name_value = {0};
+ const gchar * zone, * name;
+
+ gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_ZONE, &zone_value);
+ zone = g_value_get_string (&zone_value);
+
+ gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_NAME, &name_value);
+ name = g_value_get_string (&name_value);
+
+ if (zone == NULL || zone[0] == 0) {
+ GValue lon_value = {0}, lat_value = {0};
+ const gchar * strlon, * strlat;
+ gdouble lon = 0.0, lat = 0.0;
+
+ gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_LONGITUDE, &lon_value);
+ strlon = g_value_get_string (&lon_value);
+ if (strlon != NULL && strlon[0] != 0) {
+ lon = strtod(strlon, NULL);
+ }
+
+ gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_LATITUDE, &lat_value);
+ strlat = g_value_get_string (&lat_value);
+ if (strlat != NULL && strlat[0] != 0) {
+ lat = strtod(strlat, NULL);
+ }
+
+ CcTimezoneMap * tzmap = CC_TIMEZONE_MAP (g_object_get_data (G_OBJECT (widget), "tzmap"));
+ zone = cc_timezone_map_get_timezone_at_coords (tzmap, lon, lat);
+ }
+
+ GtkListStore * store = GTK_LIST_STORE (g_object_get_data (G_OBJECT (widget), "store"));
+ GtkTreeIter * store_iter = (GtkTreeIter *)g_object_get_data (G_OBJECT (widget), "store_iter");
+ if (store != NULL && store_iter != NULL) {
+ gtk_list_store_set (store, store_iter, 0, name, 2, zone, -1);
+ }
+
+ g_value_unset (&name_value);
+ g_value_unset (&zone_value);
+
+ return FALSE; // Do normal action too
+}
+
static void
handle_edit_started (GtkCellRendererText * renderer, GtkCellEditable * editable,
gchar * path, TimezoneCompletion * completion)
{
if (GTK_IS_ENTRY (editable)) {
GtkEntry *entry = GTK_ENTRY (editable);
- gtk_entry_set_completion (entry, GTK_ENTRY_COMPLETION (timezone_completion_new ()));
+ gtk_entry_set_completion (entry, GTK_ENTRY_COMPLETION (completion));
+ timezone_completion_watch_entry (completion, entry);
+
+ GtkListStore * store = GTK_LIST_STORE (g_object_get_data (G_OBJECT (completion), "store"));
+ GtkTreeIter * store_iter = g_new(GtkTreeIter, 1);
+ if (gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (store), store_iter, path)) {
+ g_object_set_data_full (G_OBJECT (completion), "store_iter", store_iter, g_free);
+ }
+ }
+}
+
+static gboolean
+update_times (GtkListStore * store)
+{
+ /* For each entry, check zone in column 2 and set column 1 to it's time */
+
+ GDateTime * now = g_date_time_new_now_local ();
+
+ GtkTreeIter iter;
+ if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) {
+ do {
+ GValue zone_value = {0};
+ const gchar * strzone;
+
+ gtk_tree_model_get_value (GTK_TREE_MODEL (store), &iter, 2, &zone_value);
+ strzone = g_value_get_string (&zone_value);
+
+ if (strzone != NULL && strzone[0] != 0) {
+ GTimeZone * tz = g_time_zone_new (strzone);
+ GDateTime * now_tz = g_date_time_to_timezone (now, tz);
+ gchar * format = generate_format_string_at_time (now_tz);
+ gchar * time_str = g_date_time_format (now_tz, format);
+
+ gtk_list_store_set (store, &iter, 1, time_str, -1);
+
+ g_free (time_str);
+ g_free (format);
+ g_date_time_unref (now_tz);
+ g_time_zone_unref (tz);
+ }
+
+ g_value_unset (&zone_value);
+ } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter));
}
+
+ g_date_time_unref (now);
+ return TRUE;
}
static void
@@ -106,30 +199,50 @@ fill_from_settings (GObject * store, GSettings * conf)
gchar ** striter;
GtkTreeIter iter;
for (striter = locations; *striter; ++striter) {
+ gchar * zone, * name;
+ split_settings_location (*striter, &zone, &name);
+
gtk_list_store_append (GTK_LIST_STORE (store), &iter);
- gtk_list_store_set (GTK_LIST_STORE (store), &iter, 0, *striter, -1);
+ gtk_list_store_set (GTK_LIST_STORE (store), &iter, 0, name, 2, zone, -1);
+
+ g_free (zone);
+ g_free (name);
}
g_strfreev (locations);
}
static void
-save_to_settings (GtkWidget * dlg, GObject * store)
+dialog_closed (GtkWidget * dlg, GObject * store)
{
+ /* Cleanup a tad */
+ guint time_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dlg), "time-id"));
+ g_source_remove (time_id);
+
+ /* Now save to settings */
GVariantBuilder builder;
g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
GtkTreeIter iter;
if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) {
do {
- GValue value = {0};
- const gchar * strval;
- gtk_tree_model_get_value (GTK_TREE_MODEL (store), &iter, 0, &value);
- strval = g_value_get_string (&value);
- if (strval != NULL && strval[0] != 0) {
- g_variant_builder_add (&builder, "s", strval);
+ GValue zone_value = {0}, name_value = {0};
+ const gchar * strzone, * strname;
+
+ gtk_tree_model_get_value (GTK_TREE_MODEL (store), &iter, 0, &name_value);
+ gtk_tree_model_get_value (GTK_TREE_MODEL (store), &iter, 2, &zone_value);
+
+ strzone = g_value_get_string (&zone_value);
+ strname = g_value_get_string (&name_value);
+
+ if (strzone != NULL && strzone[0] != 0 && strname != NULL && strname[0] != 0) {
+ gchar * settings_string = g_strdup_printf("%s %s", strzone, strname);
+ g_variant_builder_add (&builder, "s", settings_string);
+ g_free (settings_string);
}
- g_value_unset (&value);
+
+ g_value_unset (&zone_value);
+ g_value_unset (&name_value);
} while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter));
}
@@ -149,7 +262,7 @@ selection_changed (GtkTreeSelection * selection, GtkWidget * remove_button)
}
GtkWidget *
-datetime_setup_locations_dialog (GtkWindow * parent)
+datetime_setup_locations_dialog (GtkWindow * parent, CcTimezoneMap * map)
{
GError * error = NULL;
GtkBuilder * builder = gtk_builder_new ();
@@ -173,6 +286,10 @@ datetime_setup_locations_dialog (GtkWindow * parent)
/* Configure tree */
TimezoneCompletion * completion = timezone_completion_new ();
+ g_object_set_data (G_OBJECT (completion), "tzmap", map);
+ g_object_set_data (G_OBJECT (completion), "store", store);
+ g_signal_connect (completion, "match-selected", G_CALLBACK (timezone_selected), NULL);
+
GtkCellRenderer * cell = gtk_cell_renderer_text_new ();
g_object_set (cell, "editable", TRUE, NULL);
g_signal_connect (cell, "editing-started", G_CALLBACK (handle_edit_started), completion);
@@ -195,9 +312,13 @@ datetime_setup_locations_dialog (GtkWindow * parent)
fill_from_settings (store, conf);
+ guint time_id = g_timeout_add_seconds (2, (GSourceFunc)update_times, store);
+ update_times (GTK_LIST_STORE (store));
+
g_object_set_data_full (G_OBJECT (dlg), "conf", g_object_ref (conf), g_object_unref);
g_object_set_data_full (G_OBJECT (dlg), "completion", completion, g_object_unref);
- g_signal_connect (dlg, "destroy", G_CALLBACK (save_to_settings), store);
+ g_object_set_data (G_OBJECT (dlg), "time-id", GINT_TO_POINTER(time_id));
+ g_signal_connect (dlg, "destroy", G_CALLBACK (dialog_closed), store);
gtk_window_set_transient_for (GTK_WINDOW (dlg), parent);
diff --git a/src/datetime-prefs-locations.h b/src/datetime-prefs-locations.h
index d5dd534..1760567 100644
--- a/src/datetime-prefs-locations.h
+++ b/src/datetime-prefs-locations.h
@@ -24,10 +24,11 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
#define __DATETIME_PREFS_LOCATIONS_H__
#include <gtk/gtk.h>
+#include "cc-timezone-map.h"
G_BEGIN_DECLS
-GtkWidget * datetime_setup_locations_dialog (GtkWindow * parent);
+GtkWidget * datetime_setup_locations_dialog (GtkWindow * parent, CcTimezoneMap * map);
G_END_DECLS
diff --git a/src/datetime-prefs.c b/src/datetime-prefs.c
index dbacee1..622a999 100644
--- a/src/datetime-prefs.c
+++ b/src/datetime-prefs.c
@@ -25,6 +25,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
#include "config.h"
#endif
+#include <stdlib.h>
#include <libintl.h>
#include <locale.h>
#include <langinfo.h>
@@ -104,7 +105,7 @@ add_widget_dependency (GtkWidget * parent, GtkWidget * dependent)
widget_dependency_cb (parent, NULL, dependent);
}
-static void
+/*static void
polkit_dependency_cb (GtkWidget * parent, GParamSpec *pspec, GtkWidget * dependent)
{
gboolean authorized, sensitive;
@@ -122,7 +123,7 @@ add_polkit_dependency (GtkWidget * parent, GtkWidget * dependent)
g_signal_connect (parent, "notify::sensitive", G_CALLBACK(polkit_dependency_cb),
dependent);
polkit_dependency_cb (parent, NULL, dependent);
-}
+}*/
static void
dbus_set_answered (GObject *object, GAsyncResult *res, gpointer command)
@@ -370,7 +371,7 @@ setup_time_spinner (GtkWidget * spinner, GtkWidget * other, gboolean is_time)
static void
show_locations (GtkWidget * button, GtkWidget * dlg)
{
- GtkWidget * locationsDlg = datetime_setup_locations_dialog (GTK_WINDOW (dlg));
+ GtkWidget * locationsDlg = datetime_setup_locations_dialog (GTK_WINDOW (dlg), tzmap);
gtk_widget_show_all (locationsDlg);
}
@@ -387,6 +388,25 @@ timezone_selected (GtkEntryCompletion * widget, GtkTreeModel * model,
if (strval != NULL && strval[0] != 0) {
cc_timezone_map_set_timezone (tzmap, strval);
}
+ else {
+ GValue lon_value = {0}, lat_value = {0};
+ const gchar * strlon, * strlat;
+ gdouble lon = 0.0, lat = 0.0;
+
+ gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_LONGITUDE, &lon_value);
+ strlon = g_value_get_string (&lon_value);
+ if (strlon != NULL && strlon[0] != 0) {
+ lon = strtod(strlon, NULL);
+ }
+
+ gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_LATITUDE, &lat_value);
+ strlat = g_value_get_string (&lat_value);
+ if (strlat != NULL && strlat[0] != 0) {
+ lat = strtod(strlat, NULL);
+ }
+
+ cc_timezone_map_set_coords (tzmap, lon, lat);
+ }
g_value_unset (&value);
@@ -427,6 +447,7 @@ create_dialog (void)
TimezoneCompletion * completion = timezone_completion_new ();
gtk_entry_set_completion (GTK_ENTRY (WIG ("timezoneEntry")),
GTK_ENTRY_COMPLETION (completion));
+ timezone_completion_watch_entry (completion, GTK_ENTRY (WIG ("timezoneEntry")));
g_signal_connect (completion, "match-selected", G_CALLBACK (timezone_selected), NULL);
/* Set up settings bindings */
@@ -464,7 +485,7 @@ create_dialog (void)
add_widget_dependency (WIG ("showClockCheck"), WIG ("clockOptions"));
add_widget_dependency (WIG ("showLocationsCheck"), WIG ("locationsButton"));
add_widget_dependency (WIG ("manualTimeRadio"), WIG ("manualOptions"));
- add_polkit_dependency (polkit_button, WIG ("timeDateOptions"));
+ //add_polkit_dependency (polkit_button, WIG ("timeDateOptions"));
/* Hacky proxy test for whether evolution-data-server is installed */
gchar * evo_path = g_find_program_in_path ("evolution");
diff --git a/src/indicator-datetime.c b/src/indicator-datetime.c
index 5e73612..fb421cb 100644
--- a/src/indicator-datetime.c
+++ b/src/indicator-datetime.c
@@ -119,23 +119,6 @@ struct _indicator_item_t {
#define PROP_SHOW_DATE_S "show-date"
#define PROP_CUSTOM_TIME_FORMAT_S "custom-time-format"
-enum {
- SETTINGS_TIME_LOCALE = 0,
- SETTINGS_TIME_12_HOUR = 1,
- SETTINGS_TIME_24_HOUR = 2,
- SETTINGS_TIME_CUSTOM = 3
-};
-
-/* 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))
@@ -165,7 +148,6 @@ static const gchar * get_accessible_desc (IndicatorObject * io);
static GVariant * bind_enum_set (const GValue * value, const GVariantType * type, gpointer user_data);
static gboolean bind_enum_get (GValue * value, GVariant * variant, gpointer user_data);
static gchar * generate_format_string_now (IndicatorDatetime * self);
-static gchar * generate_format_string_at_time (IndicatorDatetime * self, GDateTime * time);
static void update_label (IndicatorDatetime * io, GDateTime ** datetime);
static void guess_label_size (IndicatorDatetime * self);
static void setup_timer (IndicatorDatetime * self, GDateTime * datetime);
@@ -643,7 +625,7 @@ set_label_to_time_in_zone (IndicatorDatetime * self, GtkLabel * label,
gchar * timestr;
if (format == NULL) {
- gchar * format_for_time = generate_format_string_at_time(self, datetime_now);
+ gchar * format_for_time = generate_format_string_at_time(datetime_now);
timestr = g_date_time_format(datetime_now, format_for_time);
g_free(format_for_time);
}
@@ -987,41 +969,6 @@ 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;
-}
-
/* Respond to changes in the screen to update the text gravity */
static void
update_text_gravity (GtkWidget *widget, GdkScreen *previous_screen, gpointer data)
@@ -1037,71 +984,6 @@ update_text_gravity (GtkWidget *widget, GdkScreen *previous_screen, gpointer dat
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 *
-generate_format_string_full (IndicatorDatetime * self, gboolean show_day, gboolean show_date)
-{
- gboolean twelvehour = TRUE;
-
- if (self->priv->time_mode == SETTINGS_TIME_LOCALE) {
- twelvehour = is_locale_12h();
- } else if (self->priv->time_mode == SETTINGS_TIME_24_HOUR) {
- twelvehour = FALSE;
- }
-
- const gchar * time_string = NULL;
- if (twelvehour) {
- if (self->priv->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 (self->priv->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);
-}
-
static gchar *
generate_format_string_now (IndicatorDatetime * self)
{
@@ -1109,62 +991,11 @@ generate_format_string_now (IndicatorDatetime * self)
return g_strdup(self->priv->custom_string);
}
else {
- return generate_format_string_full(self,
- self->priv->show_day,
+ return generate_format_string_full(self->priv->show_day,
self->priv->show_date);
}
}
-static gchar *
-generate_format_string_at_time (IndicatorDatetime * self, 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(self, show_day, show_date);
-}
-
static void
timezone_update_labels (indicator_item_t * mi_data)
{
diff --git a/src/settings-shared.h b/src/settings-shared.h
index 1a66688..d866b81 100644
--- a/src/settings-shared.h
+++ b/src/settings-shared.h
@@ -22,6 +22,8 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef __DATETIME_SETTINGS_SHARED_H__
#define __DATETIME_SETTINGS_SHARED_H__
+#include <glib/gi18n-lib.h>
+
#define SETTINGS_INTERFACE "com.canonical.indicator.datetime"
#define SETTINGS_SHOW_CLOCK_S "show-clock"
#define SETTINGS_TIME_FORMAT_S "time-format"
@@ -36,4 +38,21 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
#define SETTINGS_SHOW_LOCATIONS_S "show-locations"
#define SETTINGS_LOCATIONS_S "locations"
+enum {
+ SETTINGS_TIME_LOCALE = 0,
+ SETTINGS_TIME_12_HOUR = 1,
+ SETTINGS_TIME_24_HOUR = 2,
+ SETTINGS_TIME_CUSTOM = 3
+};
+
+/* 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
+
#endif
diff --git a/src/timezone-completion.c b/src/timezone-completion.c
index d98654a..a1b4d00 100644
--- a/src/timezone-completion.c
+++ b/src/timezone-completion.c
@@ -22,6 +22,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
#include "config.h"
#endif
+#include <json-glib/json-glib.h>
#include <gdk/gdk.h>
#include <glib/gi18n.h>
#include "timezone-completion.h"
@@ -36,10 +37,17 @@ enum {
typedef struct _TimezoneCompletionPrivate TimezoneCompletionPrivate;
struct _TimezoneCompletionPrivate
{
- void * placeholder;
+ GtkEntry * entry;
+ guint queued_request;
+ guint changed_id;
+ GCancellable * cancel;
+ gchar * request_text;
+ GHashTable * request_table;
};
-#define TIMEZONE_COMPLETION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TIMEZONE_COMPLETION_TYPE, TimezoneCompletionPrivate))
+#define TIMEZONE_COMPLETION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), TIMEZONE_COMPLETION_TYPE, TimezoneCompletionPrivate))
+
+#define GEONAME_URL "http://geoname-lookup.ubuntu.com/?query=%s&release=%s"
/* Prototypes */
static void timezone_completion_class_init (TimezoneCompletionClass *klass);
@@ -49,13 +57,186 @@ static void timezone_completion_finalize (GObject *object);
G_DEFINE_TYPE (TimezoneCompletion, timezone_completion, GTK_TYPE_ENTRY_COMPLETION);
+static void
+json_parse_ready (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ TimezoneCompletion * completion = TIMEZONE_COMPLETION (user_data);
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE(completion);
+ GError * error = NULL;
+
+ json_parser_load_from_stream_finish (JSON_PARSER (object), res, &error);
+
+ if (priv->cancel && (error == NULL || error->code != G_IO_ERROR_CANCELLED)) {
+ g_cancellable_reset (priv->cancel);
+ }
+
+ if (error != NULL) {
+ g_warning ("Could not parse geoname JSON data: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+g_print("got json\n");
+ GtkListStore * store = GTK_LIST_STORE (gtk_entry_completion_get_model (GTK_ENTRY_COMPLETION (completion)));
+ JsonReader * reader = json_reader_new (json_parser_get_root (JSON_PARSER (object)));
+
+ if (!json_reader_is_array (reader))
+ return;
+
+ gint i, count = json_reader_count_elements (reader);
+ for (i = 0; i < count; ++i) {
+ if (!json_reader_read_element (reader, i))
+ continue;
+
+ if (json_reader_is_object (reader)) {
+ const gchar * name = NULL;
+ const gchar * admin1 = NULL;
+ const gchar * country = NULL;
+ const gchar * longitude = NULL;
+ const gchar * latitude = NULL;
+ if (json_reader_read_member (reader, "name")) {
+ name = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+ if (json_reader_read_member (reader, "admin1")) {
+ admin1 = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+ if (json_reader_read_member (reader, "country")) {
+ country = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+ if (json_reader_read_member (reader, "longitude")) {
+ longitude = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+ if (json_reader_read_member (reader, "latitude")) {
+ latitude = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+
+g_print("adding %s\n", name);
+ GtkTreeIter iter;
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter,
+ TIMEZONE_COMPLETION_ZONE, NULL,
+ TIMEZONE_COMPLETION_NAME, name,
+ TIMEZONE_COMPLETION_ADMIN1, admin1,
+ TIMEZONE_COMPLETION_COUNTRY, country,
+ TIMEZONE_COMPLETION_LONGITUDE, longitude,
+ TIMEZONE_COMPLETION_LATITUDE, latitude,
+ -1);
+ }
+
+ json_reader_end_element (reader);
+ }
+
+ g_hash_table_insert (priv->request_table, g_strdup (priv->request_text), NULL);
+}
+
+static void
+geonames_data_ready (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ TimezoneCompletion * completion = TIMEZONE_COMPLETION (user_data);
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+ GError * error = NULL;
+ GFileInputStream * stream;
+
+ stream = g_file_read_finish (G_FILE (object), res, &error);
+
+ if (priv->cancel && (error == NULL || error->code != G_IO_ERROR_CANCELLED)) {
+ g_cancellable_reset (priv->cancel);
+ }
+
+ if (error != NULL) {
+ g_warning ("Could not connect to geoname lookup server: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+ JsonParser * parser = json_parser_new ();
+ json_parser_load_from_stream_async (parser, G_INPUT_STREAM (stream), priv->cancel,
+ json_parse_ready, user_data);
+}
+
+static gboolean
+request_zones (TimezoneCompletion * completion)
+{
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+
+ priv->queued_request = 0;
+
+g_print("requesting json?\n");
+ if (priv->entry == NULL) {
+ return FALSE;
+ }
+
+ const gchar * text = gtk_entry_get_text (priv->entry);
+
+ if (g_hash_table_lookup_extended (priv->request_table, text, NULL, NULL))
+ return FALSE; // already looked this up
+
+ /* Cancel any ongoing request */
+ if (priv->cancel) {
+ g_cancellable_cancel (priv->cancel);
+ g_cancellable_reset (priv->cancel);
+ }
+ g_free (priv->request_text);
+
+ priv->request_text = g_strdup (text);
+
+g_print("requesting json now\n");
+ gchar * escaped = g_uri_escape_string (text, NULL, FALSE);
+ gchar * url = g_strdup_printf (GEONAME_URL, escaped, "11.04");
+
+ GFile * file = g_file_new_for_uri (url);
+ g_file_read_async (file, G_PRIORITY_DEFAULT, priv->cancel,
+ geonames_data_ready, completion);
+
+ return FALSE;
+}
+
+static void
+entry_changed (GtkEntry * entry, TimezoneCompletion * completion)
+{
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+
+ if (priv->queued_request) {
+ g_source_remove (priv->queued_request);
+ }
+ priv->queued_request = g_timeout_add (300, (GSourceFunc)request_zones, completion);
+}
+
+void
+timezone_completion_watch_entry (TimezoneCompletion * completion, GtkEntry * entry)
+{
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+
+ if (priv->entry) {
+ g_source_remove (priv->changed_id);
+ g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry);
+ }
+
+ guint id = g_signal_connect (entry, "changed", G_CALLBACK (entry_changed), completion);
+ priv->changed_id = id;
+
+ priv->entry = entry;
+ g_object_add_weak_pointer (G_OBJECT (entry), (gpointer *)&priv->entry);
+}
+
static GtkListStore *
get_initial_model (void)
{
TzDB * db = tz_load_db ();
GPtrArray * locations = tz_get_locations (db);
- GtkListStore * store = gtk_list_store_new (TIMEZONE_COMPLETION_LAST, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
+ GtkListStore * store = gtk_list_store_new (TIMEZONE_COMPLETION_LAST,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING);
gint i;
for (i = 0; i < locations->len; ++i) {
@@ -100,23 +281,57 @@ timezone_completion_class_init (TimezoneCompletionClass *klass)
}
static void
-timezone_completion_init (TimezoneCompletion *self)
+timezone_completion_init (TimezoneCompletion * self)
{
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (self);
+
GtkListStore * model = get_initial_model ();
gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (self), GTK_TREE_MODEL (model));
gtk_entry_completion_set_text_column (GTK_ENTRY_COMPLETION (self), TIMEZONE_COMPLETION_NAME);
+
+ priv->cancel = g_cancellable_new ();
+
+ priv->request_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
return;
}
static void
-timezone_completion_dispose (GObject *object)
+timezone_completion_dispose (GObject * object)
{
G_OBJECT_CLASS (timezone_completion_parent_class)->dispose (object);
+
+ TimezoneCompletion * completion = TIMEZONE_COMPLETION (object);
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+
+ if (priv->changed_id) {
+ g_source_remove (priv->changed_id);
+ priv->changed_id = 0;
+ }
+
+ if (priv->entry != NULL) {
+ g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry);
+ }
+
+ if (priv->queued_request) {
+ g_source_remove (priv->queued_request);
+ priv->queued_request = 0;
+ }
+
+ if (priv->cancel != NULL) {
+ g_cancellable_cancel (priv->cancel);
+ g_object_unref (priv->cancel);
+ priv->cancel = NULL;
+ }
+
+ g_free (priv->request_text);
+ g_hash_table_destroy (priv->request_table);
+
return;
}
static void
-timezone_completion_finalize (GObject *object)
+timezone_completion_finalize (GObject * object)
{
G_OBJECT_CLASS (timezone_completion_parent_class)->finalize (object);
return;
diff --git a/src/timezone-completion.h b/src/timezone-completion.h
index 6b3ac2b..fdfb234 100644
--- a/src/timezone-completion.h
+++ b/src/timezone-completion.h
@@ -45,13 +45,17 @@ struct _TimezoneCompletion {
GtkEntryCompletion parent;
};
-#define TIMEZONE_COMPLETION_ZONE 0
-#define TIMEZONE_COMPLETION_NAME 1
-#define TIMEZONE_COMPLETION_COUNTRY 2
-#define TIMEZONE_COMPLETION_LAST 3
+#define TIMEZONE_COMPLETION_ZONE 0
+#define TIMEZONE_COMPLETION_NAME 1
+#define TIMEZONE_COMPLETION_ADMIN1 2
+#define TIMEZONE_COMPLETION_COUNTRY 3
+#define TIMEZONE_COMPLETION_LONGITUDE 4
+#define TIMEZONE_COMPLETION_LATITUDE 5
+#define TIMEZONE_COMPLETION_LAST 6
GType timezone_completion_get_type (void);
TimezoneCompletion * timezone_completion_new ();
+void timezone_completion_watch_entry (TimezoneCompletion * completion, GtkEntry * entry);
G_END_DECLS
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 <http://www.gnu.org/licenses/>.
*/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <glib/gi18n-lib.h>
+#include <gio/gio.h>
#include <locale.h>
#include <langinfo.h>
#include <string.h>
#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);
+}
+
diff --git a/src/utils.h b/src/utils.h
index f6305c8..5f7842c 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -28,6 +28,9 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
G_BEGIN_DECLS
gboolean is_locale_12h (void);
+void split_settings_location (const gchar * location, gchar ** zone, gchar ** name);
+gchar * generate_format_string_full (gboolean show_day, gboolean show_date);
+gchar * generate_format_string_at_time (GDateTime * time);
G_END_DECLS