diff options
author | Michael Terry <mike@mterry.name> | 2011-02-23 13:28:53 -0500 |
---|---|---|
committer | Michael Terry <mike@mterry.name> | 2011-02-23 13:28:53 -0500 |
commit | b4a4c9682ca2413175386ad36d06fc4e1032badc (patch) | |
tree | 01676389bb60015b5b33088c8b15009f1aa251bb | |
parent | deafbc1da6b3c29e04455e46414342bcb9841c2a (diff) | |
download | ayatana-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
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | data/datetime-dialog.ui | 2 | ||||
-rw-r--r-- | libmap/Makefile.am | 1 | ||||
-rw-r--r-- | libmap/Makefile.in | 1 | ||||
-rw-r--r-- | libmap/README | 2 | ||||
-rw-r--r-- | libmap/cc-timezone-map.c | 466 | ||||
-rw-r--r-- | libmap/cc-timezone-map.h | 4 | ||||
-rw-r--r-- | libmap/tz.c | 49 | ||||
-rw-r--r-- | po/POTFILES.in | 2 | ||||
-rw-r--r-- | src/datetime-prefs-locations.c | 147 | ||||
-rw-r--r-- | src/datetime-prefs-locations.h | 3 | ||||
-rw-r--r-- | src/datetime-prefs.c | 29 | ||||
-rw-r--r-- | src/indicator-datetime.c | 173 | ||||
-rw-r--r-- | src/settings-shared.h | 19 | ||||
-rw-r--r-- | src/timezone-completion.c | 227 | ||||
-rw-r--r-- | src/timezone-completion.h | 12 | ||||
-rw-r--r-- | src/utils.c | 188 | ||||
-rw-r--r-- | src/utils.h | 3 |
18 files changed, 1120 insertions, 210 deletions
diff --git a/configure.ac b/configure.ac index bcedddc..08707bc 100644 --- a/configure.ac +++ b/configure.ac @@ -125,12 +125,14 @@ AS_IF([test "x$with_gtk" = x3], [PKG_CHECK_MODULES(PREF, gio-2.0 >= $GIO_REQUIRED_VERSION gtk+-3.0 >= $GTK3_REQUIRED_VERSION unique-3.0 + json-glib-1.0 polkit-gtk-1) ], [test "x$with_gtk" = x2], [PKG_CHECK_MODULES(PREF, gio-2.0 >= $GIO_REQUIRED_VERSION gtk+-2.0 >= $GTK_REQUIRED_VERSION unique-1.0 + json-glib-1.0 polkit-gtk-1) ], [AC_MSG_FAILURE([Value for --with-gtk was neither 2 nor 3])] diff --git a/data/datetime-dialog.ui b/data/datetime-dialog.ui index 26c368b..8b48702 100644 --- a/data/datetime-dialog.ui +++ b/data/datetime-dialog.ui @@ -98,6 +98,8 @@ <column type="gchararray"/> <!-- column-name Time --> <column type="gchararray"/> + <!-- column-name Zone --> + <column type="gchararray"/> </columns> </object> <object class="GtkDialog" id="timeDateDialog"> diff --git a/libmap/Makefile.am b/libmap/Makefile.am index 0cdeb34..a10b835 100644 --- a/libmap/Makefile.am +++ b/libmap/Makefile.am @@ -2,6 +2,7 @@ uidir = $(pkgdatadir)/libmap/ui dist_ui_DATA = \ data/bg.png \ data/cc.png \ + data/olsen_map.png \ data/pin.png \ data/timezone_0.png \ data/timezone_-10.png \ diff --git a/libmap/Makefile.in b/libmap/Makefile.in index b4ae6df..dd552a4 100644 --- a/libmap/Makefile.in +++ b/libmap/Makefile.in @@ -285,6 +285,7 @@ uidir = $(pkgdatadir)/libmap/ui dist_ui_DATA = \ data/bg.png \ data/cc.png \ + data/olsen_map.png \ data/pin.png \ data/timezone_0.png \ data/timezone_-10.png \ diff --git a/libmap/README b/libmap/README index 70d6f6c..c83c6d8 100644 --- a/libmap/README +++ b/libmap/README @@ -2,4 +2,4 @@ This static library is a copied version of the code in GNOME 3.0's control cente Ideally in the future, we can have all three packages using the same code. But for now, for time reasons (hah), it's just a copy. -To update this copy, put newer versions of the code and data files in place; then fix up any GTK3-isms. +To update this copy, put newer versions of the code and data files in place; then fix up any GTK3-isms and add cc_timezone_map_set_coords and friends. diff --git a/libmap/cc-timezone-map.c b/libmap/cc-timezone-map.c index 960a049..7647925 100644 --- a/libmap/cc-timezone-map.c +++ b/libmap/cc-timezone-map.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2010 Intel, Inc + * Copyright (C) 2011 Canonical Ltd. * * Portions from Ubiquity, Copyright (C) 2009 Canonical Ltd. * Written by Evan Dandrea <evand@ubuntu.com> @@ -48,10 +49,15 @@ struct _CcTimezoneMapPrivate GdkPixbuf *background; GdkPixbuf *color_map; + GdkPixbuf *olsen_map; guchar *visible_map_pixels; gint visible_map_rowstride; + gint olsen_map_channels; + guchar *olsen_map_pixels; + gint olsen_map_rowstride; + gdouble selected_offset; TzDB *tzdb; @@ -111,6 +117,388 @@ static CcTimezoneMapOffset color_codes[] = {-100, 0, 0, 0, 0 } }; +static const gchar * olsen_map_timezones[] = { + "Africa/Abidjan", + "Africa/Accra", + "Africa/Addis_Ababa", + "Africa/Algiers", + "Africa/Asmara", + "Africa/Bamako", + "Africa/Bangui", + "Africa/Banjul", + "Africa/Bissau", + "Africa/Blantyre", + "Africa/Brazzaville", + "Africa/Bujumbura", + "Africa/Cairo", + "Africa/Casablanca", + "Africa/Conakry", + "Africa/Dakar", + "Africa/Dar_es_Salaam", + "Africa/Djibouti", + "Africa/Douala", + "Africa/El_Aaiun", + "Africa/Freetown", + "Africa/Gaborone", + "Africa/Harare", + "Africa/Johannesburg", + "Africa/Kampala", + "Africa/Khartoum", + "Africa/Kigali", + "Africa/Kinshasa", + "Africa/Lagos", + "Africa/Libreville", + "Africa/Lome", + "Africa/Luanda", + "Africa/Lubumbashi", + "Africa/Lusaka", + "Africa/Malabo", + "Africa/Maputo", + "Africa/Maseru", + "Africa/Mbabane", + "Africa/Mogadishu", + "Africa/Monrovia", + "Africa/Nairobi", + "Africa/Ndjamena", + "Africa/Niamey", + "Africa/Nouakchott", + "Africa/Ouagadougou", + "Africa/Porto-Novo", + "Africa/Sao_Tome", + "Africa/Tripoli", + "Africa/Tunis", + "Africa/Windhoek", + "America/Adak", + "America/Anguilla", + "America/Antigua", + "America/Araguaina", + "America/Argentina/Buenos_Aires", + "America/Argentina/Catamarca", + "America/Argentina/Cordoba", + "America/Argentina/Jujuy", + "America/Argentina/La_Rioja", + "America/Argentina/Mendoza", + "America/Argentina/Rio_Gallegos", + "America/Argentina/San_Juan", + "America/Argentina/San_Luis", + "America/Argentina/Tucuman", + "America/Argentina/Ushuaia", + "America/Aruba", + "America/Asuncion", + "America/Atikokan", + "America/Bahia", + "America/Barbados", + "America/Belem", + "America/Belize", + "America/Blanc-Sablon", + "America/Boa_Vista", + "America/Bogota", + "America/Boise", + "America/Cambridge_Bay", + "America/Campo_Grande", + "America/Cancun", + "America/Caracas", + "America/Cayenne", + "America/Cayman", + "America/Chicago", + "America/Chihuahua", + "America/Coral_Harbour", + "America/Costa_Rica", + "America/Cuiaba", + "America/Curacao", + "America/Dawson", + "America/Dawson_Creek", + "America/Denver", + "America/Dominica", + "America/Edmonton", + "America/Eirunepe", + "America/El_Salvador", + "America/Fortaleza", + "America/Glace_Bay", + "America/Goose_Bay", + "America/Grand_Turk", + "America/Grenada", + "America/Guadeloupe", + "America/Guatemala", + "America/Guayaquil", + "America/Guyana", + "America/Halifax", + "America/Havana", + "America/Hermosillo", + "America/Indiana/Indianapolis", + "America/Indiana/Knox", + "America/Indiana/Marengo", + "America/Indiana/Petersburg", + "America/Indiana/Vevay", + "America/Indiana/Vincennes", + "America/Indiana/Winamac", + "America/Inuvik", + "America/Iqaluit", + "America/Jamaica", + "America/Juneau", + "America/Kentucky/Louisville", + "America/Kentucky/Monticello", + "America/La_Paz", + "America/Lima", + "America/Los_Angeles", + "America/Maceio", + "America/Managua", + "America/Manaus", + "America/Marigot", + "America/Martinique", + "America/Mazatlan", + "America/Menominee", + "America/Merida", + "America/Mexico_City", + "America/Miquelon", + "America/Moncton", + "America/Monterrey", + "America/Montevideo", + "America/Montreal", + "America/Montserrat", + "America/Nassau", + "America/New_York", + "America/Nipigon", + "America/Noronha", + "America/North_Dakota/Center", + "America/North_Dakota/Salem", + "America/Panama", + "America/Pangnirtung", + "America/Paramaribo", + "America/Phoenix", + "America/Port-au-Prince", + "America/Port_of_Spain", + "America/Porto_Velho", + "America/Puerto_Rico", + "America/Rainy_River", + "America/Rankin_Inlet", + "America/Recife", + "America/Regina", + "America/Resolute", + "America/Rio_Branco", + "America/Santarem", + "America/Santiago", + "America/Santo_Domingo", + "America/Sao_Paulo", + "America/St_Barthelemy", + "America/St_Johns", + "America/St_Kitts", + "America/St_Lucia", + "America/St_Thomas", + "America/St_Vincent", + "America/Tegucigalpa", + "America/Thunder_Bay", + "America/Tijuana", + "America/Toronto", + "America/Tortola", + "America/Vancouver", + "America/Whitehorse", + "America/Winnipeg", + "America/Yellowknife", + "Ameriica/Swift_Current", + "Arctic/Longyearbyen", + "Asia/Aden", + "Asia/Almaty", + "Asia/Amman", + "Asia/Anadyr", + "Asia/Aqtau", + "Asia/Aqtobe", + "Asia/Ashgabat", + "Asia/Baghdad", + "Asia/Bahrain", + "Asia/Baku", + "Asia/Bangkok", + "Asia/Beirut", + "Asia/Bishkek", + "Asia/Brunei", + "Asia/Choibalsan", + "Asia/Chongqing", + "Asia/Colombo", + "Asia/Damascus", + "Asia/Dhaka", + "Asia/Dili", + "Asia/Dubai", + "Asia/Dushanbe", + "Asia/Gaza", + "Asia/Harbin", + "Asia/Ho_Chi_Minh", + "Asia/Hong_Kong", + "Asia/Hovd", + "Asia/Irkutsk", + "Asia/Jakarta", + "Asia/Jayapura", + "Asia/Jerusalem", + "Asia/Kabul", + "Asia/Kamchatka", + "Asia/Karachi", + "Asia/Kashgar", + "Asia/Katmandu", + "Asia/Kolkata", + "Asia/Krasnoyarsk", + "Asia/Kuala_Lumpur", + "Asia/Kuching", + "Asia/Kuwait", + "Asia/Macau", + "Asia/Magadan", + "Asia/Makassar", + "Asia/Manila", + "Asia/Muscat", + "Asia/Nicosia", + "Asia/Novosibirsk", + "Asia/Omsk", + "Asia/Oral", + "Asia/Phnom_Penh", + "Asia/Pontianak", + "Asia/Pyongyang", + "Asia/Qatar", + "Asia/Qyzylorda", + "Asia/Rangoon", + "Asia/Riyadh", + "Asia/Sakhalin", + "Asia/Samarkand", + "Asia/Seoul", + "Asia/Shanghai", + "Asia/Singapore", + "Asia/Taipei", + "Asia/Tashkent", + "Asia/Tbilisi", + "Asia/Tehran", + "Asia/Thimphu", + "Asia/Tokyo", + "Asia/Ulaanbaatar", + "Asia/Urumqi", + "Asia/Vientiane", + "Asia/Vladivostok", + "Asia/Yakutsk", + "Asia/Yekaterinburg", + "Asia/Yerevan", + "Atlantic/Azores", + "Atlantic/Bermuda", + "Atlantic/Canary", + "Atlantic/Cape_Verde", + "Atlantic/Faroe", + "Atlantic/Madeira", + "Atlantic/Reykjavik", + "Atlantic/South_Georgia", + "Atlantic/St_Helena", + "Atlantic/Stanley", + "Australia/Adelaide", + "Australia/Brisbane", + "Australia/Broken_Hill", + "Australia/Currie", + "Australia/Darwin", + "Australia/Eucla", + "Australia/Hobart", + "Australia/Lindeman", + "Australia/Lord_Howe", + "Australia/Melbourne", + "Australia/Perth", + "Australia/Sydney", + "Europe/Amsterdam", + "Europe/Andorra", + "Europe/Athens", + "Europe/Belgrade", + "Europe/Berlin", + "Europe/Bratislava", + "Europe/Brussels", + "Europe/Bucharest", + "Europe/Budapest", + "Europe/Chisinau", + "Europe/Copenhagen", + "Europe/Dublin", + "Europe/Gibraltar", + "Europe/Guernsey", + "Europe/Helsinki", + "Europe/Isle_of_Man", + "Europe/Istanbul", + "Europe/Jersey", + "Europe/Kaliningrad", + "Europe/Kiev", + "Europe/Lisbon", + "Europe/Ljubljana", + "Europe/London", + "Europe/Luxembourg", + "Europe/Madrid", + "Europe/Malta", + "Europe/Marienhamn", + "Europe/Minsk", + "Europe/Monaco", + "Europe/Moscow", + "Europe/Oslo", + "Europe/Paris", + "Europe/Podgorica", + "Europe/Prague", + "Europe/Riga", + "Europe/Rome", + "Europe/Samara", + "Europe/San_Marino", + "Europe/Sarajevo", + "Europe/Simferopol", + "Europe/Skopje", + "Europe/Sofia", + "Europe/Stockholm", + "Europe/Tallinn", + "Europe/Tirane", + "Europe/Uzhgorod", + "Europe/Vaduz", + "Europe/Vatican", + "Europe/Vienna", + "Europe/Vilnius", + "Europe/Volgograd", + "Europe/Warsaw", + "Europe/Zagreb", + "Europe/Zaporozhye", + "Europe/Zurich", + "Indian/Antananarivo", + "Indian/Chagos", + "Indian/Christmas", + "Indian/Cocos", + "Indian/Comoro", + "Indian/Kerguelen", + "Indian/Mahe", + "Indian/Maldives", + "Indian/Mauritius", + "Indian/Mayotte", + "Indian/Reunion", + "Pacific/Apia", + "Pacific/Auckland", + "Pacific/Chatham", + "Pacific/Easter", + "Pacific/Efate", + "Pacific/Enderbury", + "Pacific/Fakaofo", + "Pacific/Fiji", + "Pacific/Funafuti", + "Pacific/Galapagos", + "Pacific/Gambier", + "Pacific/Guadalcanal", + "Pacific/Guam", + "Pacific/Honolulu", + "Pacific/Johnston", + "Pacific/Kiritimati", + "Pacific/Kosrae", + "Pacific/Kwajalein", + "Pacific/Majuro", + "Pacific/Marquesas", + "Pacific/Midway", + "Pacific/Nauru", + "Pacific/Niue", + "Pacific/Norfolk", + "Pacific/Noumea", + "Pacific/Pago_Pago", + "Pacific/Palau", + "Pacific/Pitcairn", + "Pacific/Ponape", + "Pacific/Port_Moresby", + "Pacific/Rarotonga", + "Pacific/Saipan", + "Pacific/Tahiti", + "Pacific/Tarawa", + "Pacific/Tongatapu", + "Pacific/Truk", + "Pacific/Wake", + "Pacific/Wallis" +}; static void cc_timezone_map_get_property (GObject *object, @@ -155,6 +543,16 @@ cc_timezone_map_dispose (GObject *object) priv->orig_color_map = NULL; } + if (priv->olsen_map) + { + g_object_unref (priv->olsen_map); + priv->olsen_map = NULL; + + priv->olsen_map_channels = 0; + priv->olsen_map_pixels = NULL; + priv->olsen_map_rowstride = 0; + } + if (priv->background) { g_object_unref (priv->background); @@ -513,12 +911,10 @@ set_location (CcTimezoneMap *map, tz_info_free (info); } -static gboolean -button_press_event (GtkWidget *widget, - GdkEventButton *event) +static TzLocation * +get_loc_for_xy (GtkWidget * widget, gint x, gint y) { CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (widget)->priv; - gint x, y; guchar r, g, b, a; guchar *pixels; gint rowstride; @@ -529,10 +925,6 @@ button_press_event (GtkWidget *widget, GList *distances = NULL; GtkAllocation alloc; - x = event->x; - y = event->y; - - rowstride = priv->visible_map_rowstride; pixels = priv->visible_map_pixels; @@ -578,11 +970,19 @@ button_press_event (GtkWidget *widget, } distances = g_list_sort (distances, (GCompareFunc) sort_locations); - - set_location (CC_TIMEZONE_MAP (widget), (TzLocation*) distances->data); + TzLocation * loc = (TzLocation*) distances->data; g_list_free (distances); + return loc; +} + +static gboolean +button_press_event (GtkWidget *widget, + GdkEventButton *event) +{ + TzLocation * loc = get_loc_for_xy (widget, event->x, event->y); + set_location (CC_TIMEZONE_MAP (widget), loc); return TRUE; } @@ -664,6 +1064,18 @@ cc_timezone_map_init (CcTimezoneMap *self) g_clear_error (&err); } + priv->olsen_map = gdk_pixbuf_new_from_file (DATADIR "/olsen_map.png", + &err); + if (!priv->olsen_map) + { + g_warning ("Could not load olsen map: %s", + (err) ? err->message : "Unknown error"); + g_clear_error (&err); + } + priv->olsen_map_channels = gdk_pixbuf_get_n_channels (priv->olsen_map); + priv->olsen_map_pixels = gdk_pixbuf_get_pixels (priv->olsen_map); + priv->olsen_map_rowstride = gdk_pixbuf_get_rowstride (priv->olsen_map); + priv->tzdb = tz_load_db (); g_signal_connect (self, "button-press-event", G_CALLBACK (button_press_event), @@ -704,6 +1116,40 @@ cc_timezone_map_set_timezone (CcTimezoneMap *map, gtk_widget_queue_draw (GTK_WIDGET (map)); } +void +cc_timezone_map_set_coords (CcTimezoneMap *map, gdouble lon, gdouble lat) +{ + const gchar * zone = cc_timezone_map_get_timezone_at_coords (map, lon, lat); + cc_timezone_map_set_timezone (map, zone); +} + +const gchar * +cc_timezone_map_get_timezone_at_coords (CcTimezoneMap *map, gdouble lon, gdouble lat) +{ + gint x = (int)(2048.0 / 360.0 * (180.0 + lon)); + gint y = (int)(1024.0 / 180.0 * (90.0 - lat)); + gint offset = map->priv->olsen_map_rowstride * y + x * map->priv->olsen_map_channels; + guchar color0 = map->priv->olsen_map_pixels[offset]; + guchar color1 = map->priv->olsen_map_pixels[offset + 1]; + gint zone = ((color0 & 248) << 1) + ((color1 >>4) & 15); + + const gchar * city = NULL; + if (zone < G_N_ELEMENTS(olsen_map_timezones)) + city = olsen_map_timezones[zone]; + + if (city != NULL) { + return city; + } + else { + GtkAllocation alloc; + gtk_widget_get_allocation (GTK_WIDGET (map), &alloc); + x = convert_longtitude_to_x(lon, alloc.width); + y = convert_latitude_to_y(lat, alloc.height); + TzLocation * loc = get_loc_for_xy(GTK_WIDGET (map), x, y); + return loc->zone; + } +} + TzLocation * cc_timezone_map_get_location (CcTimezoneMap *map) { diff --git a/libmap/cc-timezone-map.h b/libmap/cc-timezone-map.h index 3c57b27..8de986d 100644 --- a/libmap/cc-timezone-map.h +++ b/libmap/cc-timezone-map.h @@ -72,6 +72,10 @@ CcTimezoneMap *cc_timezone_map_new (void); void cc_timezone_map_set_timezone (CcTimezoneMap *map, const gchar *timezone); +void cc_timezone_map_set_coords (CcTimezoneMap *map, + gdouble lon, gdouble lat); +const gchar * cc_timezone_map_get_timezone_at_coords (CcTimezoneMap *map, + gdouble lon, gdouble lat); TzLocation * cc_timezone_map_get_location (CcTimezoneMap *map); G_END_DECLS diff --git a/libmap/tz.c b/libmap/tz.c index 3e6c8ae..b77a8ef 100644 --- a/libmap/tz.c +++ b/libmap/tz.c @@ -146,6 +146,55 @@ tz_db_free (TzDB *db) g_free (db); } +static gint +sort_locations (TzLocation *a, + TzLocation *b) +{ + if (a->dist > b->dist) + return 1; + + if (a->dist < b->dist) + return -1; + + return 0; +} + +static gdouble +convert_longtitude_to_x (gdouble longitude, gint map_width) +{ + const gdouble xdeg_offset = -6; + gdouble x; + + x = (map_width * (180.0 + longitude) / 360.0) + + (map_width * xdeg_offset / 180.0); + + return x; +} + +static gdouble +radians (gdouble degrees) +{ + return (degrees / 360.0) * G_PI * 2; +} + +static gdouble +convert_latitude_to_y (gdouble latitude, gdouble map_height) +{ + gdouble bottom_lat = -59; + gdouble top_lat = 81; + gdouble top_per, y, full_range, top_offset, map_range; + + top_per = top_lat / 180.0; + y = 1.25 * log (tan (G_PI_4 + 0.4 * radians (latitude))); + full_range = 4.6068250867599998; + top_offset = full_range * top_per; + map_range = fabs (1.25 * log (tan (G_PI_4 + 0.4 * radians (bottom_lat))) - top_offset); + y = fabs (y - top_offset); + y = y / map_range; + y = y * map_height; + return y; +} + GPtrArray * tz_get_locations (TzDB *db) { diff --git a/po/POTFILES.in b/po/POTFILES.in index 0fa22d4..84ce021 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -2,4 +2,6 @@ src/indicator-datetime.c src/datetime-service.c src/datetime-prefs.c src/datetime-prefs-locations.c +src/utils.c +src/settings-shared.h [type: gettext/glade]data/datetime-dialog.ui 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 |