diff options
Diffstat (limited to 'gnome-initial-setup/pages/timezone')
22 files changed, 3230 insertions, 0 deletions
diff --git a/gnome-initial-setup/pages/timezone/backward b/gnome-initial-setup/pages/timezone/backward new file mode 100644 index 0000000..8594be6 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/backward @@ -0,0 +1,128 @@ +# This file is in the public domain, so clarified as of +# 2009-05-17 by Arthur David Olson. + +# This file provides links between current names for time zones +# and their old names. Many names changed in late 1993. + +# Link TARGET LINK-NAME +Link Africa/Nairobi Africa/Asmera +Link Africa/Abidjan Africa/Timbuktu +Link America/Argentina/Catamarca America/Argentina/ComodRivadavia +Link America/Adak America/Atka +Link America/Argentina/Buenos_Aires America/Buenos_Aires +Link America/Argentina/Catamarca America/Catamarca +Link America/Atikokan America/Coral_Harbour +Link America/Argentina/Cordoba America/Cordoba +Link America/Tijuana America/Ensenada +Link America/Indiana/Indianapolis America/Fort_Wayne +Link America/Indiana/Indianapolis America/Indianapolis +Link America/Argentina/Jujuy America/Jujuy +Link America/Indiana/Knox America/Knox_IN +Link America/Kentucky/Louisville America/Louisville +Link America/Argentina/Mendoza America/Mendoza +Link America/Toronto America/Montreal +Link America/Rio_Branco America/Porto_Acre +Link America/Argentina/Cordoba America/Rosario +Link America/Tijuana America/Santa_Isabel +Link America/Denver America/Shiprock +Link America/Port_of_Spain America/Virgin +Link Pacific/Auckland Antarctica/South_Pole +Link Asia/Ashgabat Asia/Ashkhabad +Link Asia/Kolkata Asia/Calcutta +Link Asia/Shanghai Asia/Chongqing +Link Asia/Shanghai Asia/Chungking +Link Asia/Dhaka Asia/Dacca +Link Asia/Shanghai Asia/Harbin +Link Asia/Urumqi Asia/Kashgar +Link Asia/Kathmandu Asia/Katmandu +Link Asia/Macau Asia/Macao +Link Asia/Yangon Asia/Rangoon +Link Asia/Ho_Chi_Minh Asia/Saigon +Link Asia/Jerusalem Asia/Tel_Aviv +Link Asia/Thimphu Asia/Thimbu +Link Asia/Makassar Asia/Ujung_Pandang +Link Asia/Ulaanbaatar Asia/Ulan_Bator +Link Atlantic/Faroe Atlantic/Faeroe +Link Europe/Oslo Atlantic/Jan_Mayen +Link Australia/Sydney Australia/ACT +Link Australia/Sydney Australia/Canberra +Link Australia/Lord_Howe Australia/LHI +Link Australia/Sydney Australia/NSW +Link Australia/Darwin Australia/North +Link Australia/Brisbane Australia/Queensland +Link Australia/Adelaide Australia/South +Link Australia/Hobart Australia/Tasmania +Link Australia/Melbourne Australia/Victoria +Link Australia/Perth Australia/West +Link Australia/Broken_Hill Australia/Yancowinna +Link America/Rio_Branco Brazil/Acre +Link America/Noronha Brazil/DeNoronha +Link America/Sao_Paulo Brazil/East +Link America/Manaus Brazil/West +Link America/Halifax Canada/Atlantic +Link America/Winnipeg Canada/Central +# This line is commented out, as the name exceeded the 14-character limit +# and was an unused misnomer. +#Link America/Regina Canada/East-Saskatchewan +Link America/Toronto Canada/Eastern +Link America/Edmonton Canada/Mountain +Link America/St_Johns Canada/Newfoundland +Link America/Vancouver Canada/Pacific +Link America/Regina Canada/Saskatchewan +Link America/Whitehorse Canada/Yukon +Link America/Santiago Chile/Continental +Link Pacific/Easter Chile/EasterIsland +Link America/Havana Cuba +Link Africa/Cairo Egypt +Link Europe/Dublin Eire +Link Europe/London Europe/Belfast +Link Europe/Chisinau Europe/Tiraspol +Link Europe/London GB +Link Europe/London GB-Eire +Link Etc/GMT GMT+0 +Link Etc/GMT GMT-0 +Link Etc/GMT GMT0 +Link Etc/GMT Greenwich +Link Asia/Hong_Kong Hongkong +Link Atlantic/Reykjavik Iceland +Link Asia/Tehran Iran +Link Asia/Jerusalem Israel +Link America/Jamaica Jamaica +Link Asia/Tokyo Japan +Link Pacific/Kwajalein Kwajalein +Link Africa/Tripoli Libya +Link America/Tijuana Mexico/BajaNorte +Link America/Mazatlan Mexico/BajaSur +Link America/Mexico_City Mexico/General +Link Pacific/Auckland NZ +Link Pacific/Chatham NZ-CHAT +Link America/Denver Navajo +Link Asia/Shanghai PRC +Link Pacific/Honolulu Pacific/Johnston +Link Pacific/Pohnpei Pacific/Ponape +Link Pacific/Pago_Pago Pacific/Samoa +Link Pacific/Chuuk Pacific/Truk +Link Pacific/Chuuk Pacific/Yap +Link Europe/Warsaw Poland +Link Europe/Lisbon Portugal +Link Asia/Taipei ROC +Link Asia/Seoul ROK +Link Asia/Singapore Singapore +Link Europe/Istanbul Turkey +Link Etc/UCT UCT +Link America/Anchorage US/Alaska +Link America/Adak US/Aleutian +Link America/Phoenix US/Arizona +Link America/Chicago US/Central +Link America/Indiana/Indianapolis US/East-Indiana +Link America/New_York US/Eastern +Link Pacific/Honolulu US/Hawaii +Link America/Indiana/Knox US/Indiana-Starke +Link America/Detroit US/Michigan +Link America/Denver US/Mountain +Link America/Los_Angeles US/Pacific +Link Pacific/Pago_Pago US/Samoa +Link Etc/UTC UTC +Link Etc/UTC Universal +Link Europe/Moscow W-SU +Link Etc/UTC Zulu
\ No newline at end of file diff --git a/gnome-initial-setup/pages/timezone/cc-timezone-map.c b/gnome-initial-setup/pages/timezone/cc-timezone-map.c new file mode 100644 index 0000000..863b4c9 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/cc-timezone-map.c @@ -0,0 +1,530 @@ +/* + * Copyright (C) 2010 Intel, Inc + * + * Portions from Ubiquity, Copyright (C) 2009 Canonical Ltd. + * Written by Evan Dandrea <evand@ubuntu.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + * + * Author: Thomas Wood <thomas.wood@intel.com> + * + */ + +#include "cc-timezone-map.h" +#include <math.h> +#include <string.h> +#include "tz.h" + +#define PIN_HOT_POINT_X 8 +#define PIN_HOT_POINT_Y 15 + +#define DATETIME_RESOURCE_PATH "/org/gnome/control-center/datetime" + +typedef struct +{ + gdouble offset; + guchar red; + guchar green; + guchar blue; + guchar alpha; +} CcTimezoneMapOffset; + +struct _CcTimezoneMap +{ + GtkWidget parent_instance; + + GdkTexture *orig_background; + GdkTexture *orig_background_dim; + + GdkTexture *background; + GdkTexture *pin; + + TzDB *tzdb; + TzLocation *location; + + gchar *bubble_text; +}; + +G_DEFINE_TYPE (CcTimezoneMap, cc_timezone_map, GTK_TYPE_WIDGET) + +enum +{ + LOCATION_CHANGED, + LAST_SIGNAL +}; + +static guint signals[LAST_SIGNAL]; + +static GdkTexture * +texture_from_resource (const gchar *resource_path, + GError **error) +{ + g_autofree gchar *full_path = g_strdup_printf ("resource://%s", resource_path); + g_autoptr(GFile) file = g_file_new_for_uri (full_path); + g_autoptr(GdkTexture) texture = gdk_texture_new_from_file (file, error); + + return g_steal_pointer (&texture); +} + +static void +cc_timezone_map_dispose (GObject *object) +{ + CcTimezoneMap *self = CC_TIMEZONE_MAP (object); + + g_clear_object (&self->orig_background); + g_clear_object (&self->orig_background_dim); + g_clear_object (&self->background); + g_clear_object (&self->pin); + g_clear_pointer (&self->bubble_text, g_free); + + G_OBJECT_CLASS (cc_timezone_map_parent_class)->dispose (object); +} + +static void +cc_timezone_map_finalize (GObject *object) +{ + CcTimezoneMap *self = CC_TIMEZONE_MAP (object); + + g_clear_pointer (&self->tzdb, tz_db_free); + + G_OBJECT_CLASS (cc_timezone_map_parent_class)->finalize (object); +} + +/* GtkWidget functions */ +static void +cc_timezone_map_measure (GtkWidget *widget, + GtkOrientation orientation, + gint for_size, + gint *minimum, + gint *natural, + gint *minimum_baseline, + gint *natural_baseline) +{ + CcTimezoneMap *map = CC_TIMEZONE_MAP (widget); + gint size; + + switch (orientation) + { + case GTK_ORIENTATION_HORIZONTAL: + size = gdk_texture_get_width (map->orig_background); + break; + + case GTK_ORIENTATION_VERTICAL: + size = gdk_texture_get_height (map->orig_background); + break; + } + + if (minimum != NULL) + *minimum = size; + if (natural != NULL) + *natural = size; +} + +static void +cc_timezone_map_size_allocate (GtkWidget *widget, + gint width, + gint height, + gint baseline) +{ + CcTimezoneMap *map = CC_TIMEZONE_MAP (widget); + GdkTexture *texture; + + if (!gtk_widget_is_sensitive (widget)) + texture = map->orig_background_dim; + else + texture = map->orig_background; + + g_clear_object (&map->background); + map->background = g_object_ref (texture); + + GTK_WIDGET_CLASS (cc_timezone_map_parent_class)->size_allocate (widget, + width, + height, + baseline); +} + +static gdouble +convert_longitude_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; +} + +static void +draw_text_bubble (CcTimezoneMap *map, + GtkSnapshot *snapshot, + gint width, + gint height, + gdouble pointx, + gdouble pointy) +{ + static const double corner_radius = 9.0; + static const double margin_top = 12.0; + static const double margin_bottom = 12.0; + static const double margin_left = 24.0; + static const double margin_right = 24.0; + + GskRoundedRect rounded_rect; + PangoRectangle text_rect; + PangoLayout *layout; + GdkRGBA rgba; + double x; + double y; + double bubble_width; + double bubble_height; + + if (!map->bubble_text) + return; + + layout = gtk_widget_create_pango_layout (GTK_WIDGET (map), NULL); + + /* Layout the text */ + pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER); + pango_layout_set_spacing (layout, 3); + pango_layout_set_markup (layout, map->bubble_text, -1); + + pango_layout_get_pixel_extents (layout, NULL, &text_rect); + + /* Calculate the bubble size based on the text layout size */ + bubble_width = text_rect.width + margin_left + margin_right; + bubble_height = text_rect.height + margin_top + margin_bottom; + + if (pointx < width / 2) + x = pointx + 25; + else + x = pointx - bubble_width - 25; + + y = pointy - bubble_height / 2; + + /* Make sure it fits in the visible area */ + x = CLAMP (x, 0, width - bubble_width); + y = CLAMP (y, 0, height - bubble_height); + + gtk_snapshot_save (snapshot); + + gsk_rounded_rect_init (&rounded_rect, + &GRAPHENE_RECT_INIT (x, y, bubble_width, bubble_height), + &GRAPHENE_SIZE_INIT (corner_radius, corner_radius), + &GRAPHENE_SIZE_INIT (corner_radius, corner_radius), + &GRAPHENE_SIZE_INIT (corner_radius, corner_radius), + &GRAPHENE_SIZE_INIT (corner_radius, corner_radius)); + + gtk_snapshot_push_rounded_clip (snapshot, &rounded_rect); + + rgba = (GdkRGBA) { + .red = 0.2, + .green = 0.2, + .blue = 0.2, + .alpha = 0.7, + }; + gtk_snapshot_append_color (snapshot, + &rgba, + &GRAPHENE_RECT_INIT (x, y, bubble_width, bubble_height)); + + + rgba = (GdkRGBA) { + .red = 1.0, + .green = 1.0, + .blue = 1.0, + .alpha = 1.0, + }; + gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (x + margin_left, y + margin_top)); + gtk_snapshot_append_layout (snapshot, layout, &rgba); + + gtk_snapshot_pop (snapshot); + gtk_snapshot_restore (snapshot); + + g_object_unref (layout); +} + +static void +cc_timezone_map_snapshot (GtkWidget *widget, + GtkSnapshot *snapshot) +{ + CcTimezoneMap *map = CC_TIMEZONE_MAP (widget); + gdouble pointx, pointy; + gint width, height; + + width = gtk_widget_get_width (widget); + height = gtk_widget_get_height (widget); + + /* paint background */ + gtk_snapshot_append_texture (snapshot, + map->background, + &GRAPHENE_RECT_INIT (0, 0, width, height)); + + if (map->location) + { + pointx = convert_longitude_to_x (map->location->longitude, width); + pointy = convert_latitude_to_y (map->location->latitude, height); + + pointx = CLAMP (floor (pointx), 0, width); + pointy = CLAMP (floor (pointy), 0, height); + + draw_text_bubble (map, snapshot, width, height, pointx, pointy); + + if (map->pin) + { + gtk_snapshot_append_texture (snapshot, + map->pin, + &GRAPHENE_RECT_INIT (pointx - PIN_HOT_POINT_X, + pointy - PIN_HOT_POINT_Y, + gdk_texture_get_width (map->pin), + gdk_texture_get_height (map->pin))); + } + } +} + +static void +update_cursor (GtkWidget *widget) +{ + const gchar *cursor_name = NULL; + + if (!gtk_widget_get_realized (widget)) + return; + + if (gtk_widget_is_sensitive (widget)) + cursor_name = "pointer"; + + gtk_widget_set_cursor_from_name (widget, cursor_name); +} + +static void +cc_timezone_map_state_flags_changed (GtkWidget *widget, + GtkStateFlags prev_state) +{ + update_cursor (widget); + + if (GTK_WIDGET_CLASS (cc_timezone_map_parent_class)->state_flags_changed) + GTK_WIDGET_CLASS (cc_timezone_map_parent_class)->state_flags_changed (widget, prev_state); +} + + +static void +cc_timezone_map_class_init (CcTimezoneMapClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + object_class->dispose = cc_timezone_map_dispose; + object_class->finalize = cc_timezone_map_finalize; + + widget_class->measure = cc_timezone_map_measure; + widget_class->size_allocate = cc_timezone_map_size_allocate; + widget_class->snapshot = cc_timezone_map_snapshot; + widget_class->state_flags_changed = cc_timezone_map_state_flags_changed; + + signals[LOCATION_CHANGED] = g_signal_new ("location-changed", + CC_TYPE_TIMEZONE_MAP, + G_SIGNAL_RUN_FIRST, + 0, + NULL, + NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, + G_TYPE_POINTER); +} + + +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 void +set_location (CcTimezoneMap *map, + TzLocation *location) +{ + g_autoptr(TzInfo) info = NULL; + + map->location = location; + + info = tz_info_from_location (map->location); + + gtk_widget_queue_draw (GTK_WIDGET (map)); + + g_signal_emit (map, signals[LOCATION_CHANGED], 0, map->location); +} + +static gboolean +map_clicked_cb (GtkGestureClick *self, + gint n_press, + gdouble x, + gdouble y, + CcTimezoneMap *map) +{ + const GPtrArray *array; + gint width, height; + GList *distances = NULL; + gint i; + + /* work out the coordinates */ + + array = tz_get_locations (map->tzdb); + + width = gtk_widget_get_width (GTK_WIDGET (map)); + height = gtk_widget_get_height (GTK_WIDGET (map)); + + for (i = 0; i < array->len; i++) + { + gdouble pointx, pointy, dx, dy; + TzLocation *loc = array->pdata[i]; + + pointx = convert_longitude_to_x (loc->longitude, width); + pointy = convert_latitude_to_y (loc->latitude, height); + + dx = pointx - x; + dy = pointy - y; + + loc->dist = dx * dx + dy * dy; + distances = g_list_prepend (distances, loc); + + } + distances = g_list_sort (distances, (GCompareFunc) sort_locations); + + + set_location (map, (TzLocation*) distances->data); + + g_list_free (distances); + + return TRUE; +} + +static void +cc_timezone_map_init (CcTimezoneMap *map) +{ + GtkGesture *click_gesture; + GError *err = NULL; + + map->orig_background = texture_from_resource (DATETIME_RESOURCE_PATH "/bg.png", &err); + if (!map->orig_background) + { + g_warning ("Could not load background image: %s", + (err) ? err->message : "Unknown error"); + g_clear_error (&err); + } + + map->orig_background_dim = texture_from_resource (DATETIME_RESOURCE_PATH "/bg_dim.png", &err); + if (!map->orig_background_dim) + { + g_warning ("Could not load background image: %s", + (err) ? err->message : "Unknown error"); + g_clear_error (&err); + } + + map->pin = texture_from_resource (DATETIME_RESOURCE_PATH "/pin.png", &err); + if (!map->pin) + { + g_warning ("Could not load pin icon: %s", + (err) ? err->message : "Unknown error"); + g_clear_error (&err); + } + + map->tzdb = tz_load_db (); + + click_gesture = gtk_gesture_click_new (); + g_signal_connect (click_gesture, "pressed", G_CALLBACK (map_clicked_cb), map); + gtk_widget_add_controller (GTK_WIDGET (map), GTK_EVENT_CONTROLLER (click_gesture)); +} + +CcTimezoneMap * +cc_timezone_map_new (void) +{ + return g_object_new (CC_TYPE_TIMEZONE_MAP, NULL); +} + +gboolean +cc_timezone_map_set_timezone (CcTimezoneMap *map, + const gchar *timezone) +{ + GPtrArray *locations; + guint i; + g_autofree gchar *real_tz = NULL; + gboolean ret; + + real_tz = tz_info_get_clean_name (map->tzdb, timezone); + + locations = tz_get_locations (map->tzdb); + ret = FALSE; + + for (i = 0; i < locations->len; i++) + { + TzLocation *loc = locations->pdata[i]; + + if (!g_strcmp0 (loc->zone, real_tz ? real_tz : timezone)) + { + set_location (map, loc); + ret = TRUE; + break; + } + } + + if (ret) + gtk_widget_queue_draw (GTK_WIDGET (map)); + + return ret; +} + +void +cc_timezone_map_set_bubble_text (CcTimezoneMap *map, + const gchar *text) +{ + g_free (map->bubble_text); + map->bubble_text = g_strdup (text); + + gtk_widget_queue_draw (GTK_WIDGET (map)); +} + +TzLocation * +cc_timezone_map_get_location (CcTimezoneMap *map) +{ + return map->location; +} diff --git a/gnome-initial-setup/pages/timezone/cc-timezone-map.h b/gnome-initial-setup/pages/timezone/cc-timezone-map.h new file mode 100644 index 0000000..f4c99e5 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/cc-timezone-map.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2010 Intel, Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + * + * Author: Thomas Wood <thomas.wood@intel.com> + * + */ + +#pragma once + +#include <gtk/gtk.h> +#include "tz.h" + +G_BEGIN_DECLS + +#define CC_TYPE_TIMEZONE_MAP (cc_timezone_map_get_type ()) +G_DECLARE_FINAL_TYPE (CcTimezoneMap, cc_timezone_map, CC, TIMEZONE_MAP, GtkWidget) + +CcTimezoneMap *cc_timezone_map_new (void); + +gboolean cc_timezone_map_set_timezone (CcTimezoneMap *map, + const gchar *timezone); +void cc_timezone_map_set_bubble_text (CcTimezoneMap *map, + const gchar *text); +TzLocation * cc_timezone_map_get_location (CcTimezoneMap *map); + +G_END_DECLS diff --git a/gnome-initial-setup/pages/timezone/data/bg.png b/gnome-initial-setup/pages/timezone/data/bg.png Binary files differnew file mode 100644 index 0000000..43e4016 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/data/bg.png diff --git a/gnome-initial-setup/pages/timezone/data/bg_dim.png b/gnome-initial-setup/pages/timezone/data/bg_dim.png Binary files differnew file mode 100644 index 0000000..8db7a19 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/data/bg_dim.png diff --git a/gnome-initial-setup/pages/timezone/data/pin.png b/gnome-initial-setup/pages/timezone/data/pin.png Binary files differnew file mode 100644 index 0000000..c347a9e --- /dev/null +++ b/gnome-initial-setup/pages/timezone/data/pin.png diff --git a/gnome-initial-setup/pages/timezone/datetime.gresource.xml b/gnome-initial-setup/pages/timezone/datetime.gresource.xml new file mode 100644 index 0000000..5a3dbb3 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/datetime.gresource.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<gresources> + <gresource prefix="/org/gnome/control-center/datetime"> + <file>backward</file> + <file alias="bg.png">data/bg.png</file> + <file alias="bg_dim.png">data/bg_dim.png</file> + <file alias="pin.png">data/pin.png</file> + </gresource> +</gresources> diff --git a/gnome-initial-setup/pages/timezone/gis-bubble-widget.c b/gnome-initial-setup/pages/timezone/gis-bubble-widget.c new file mode 100644 index 0000000..a6e8ab7 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/gis-bubble-widget.c @@ -0,0 +1,141 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* + * Copyright (C) 2013 Red Hat + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + * + * Written by: + * Jasper St. Pierre <jstpierre@mecheye.net> + */ + +#include "config.h" + +#include "gis-bubble-widget.h" + +struct _GisBubbleWidgetPrivate +{ + GtkWidget *icon; + GtkWidget *label; +}; +typedef struct _GisBubbleWidgetPrivate GisBubbleWidgetPrivate; + +G_DEFINE_TYPE_WITH_PRIVATE (GisBubbleWidget, gis_bubble_widget, ADW_TYPE_BIN); + +enum { + PROP_0, + PROP_LABEL, + PROP_ICON_NAME, + PROP_LAST, +}; + +static GParamSpec *obj_props[PROP_LAST]; + +static void +gis_bubble_widget_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + GisBubbleWidget *widget = GIS_BUBBLE_WIDGET (object); + GisBubbleWidgetPrivate *priv = gis_bubble_widget_get_instance_private (widget); + + switch (prop_id) + { + case PROP_LABEL: + g_value_set_string (value, gtk_label_get_label (GTK_LABEL (priv->label))); + break; + case PROP_ICON_NAME: + g_value_set_string (value, gtk_image_get_icon_name (GTK_IMAGE (priv->icon))); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gis_bubble_widget_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + GisBubbleWidget *widget = GIS_BUBBLE_WIDGET (object); + GisBubbleWidgetPrivate *priv = gis_bubble_widget_get_instance_private (widget); + + switch (prop_id) + { + case PROP_LABEL: + gtk_label_set_label (GTK_LABEL (priv->label), g_value_get_string (value)); + break; + case PROP_ICON_NAME: + g_object_set (GTK_IMAGE (priv->icon), + "icon-name", g_value_get_string (value), + NULL); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +add_style_from_resource (const char *resource) +{ + GtkCssProvider *provider; + GFile *file; + char *uri; + + provider = gtk_css_provider_new (); + + uri = g_strconcat ("resource://", resource, NULL); + file = g_file_new_for_uri (uri); + + gtk_css_provider_load_from_file (provider, file); + + gtk_style_context_add_provider_for_display (gdk_display_get_default (), + GTK_STYLE_PROVIDER (provider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + g_object_unref (provider); + g_object_unref (file); + g_free (uri); +} + +static void +gis_bubble_widget_class_init (GisBubbleWidgetClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/initial-setup/gis-bubble-widget.ui"); + + gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GisBubbleWidget, icon); + gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GisBubbleWidget, label); + + gtk_widget_class_set_css_name (GTK_WIDGET_CLASS (klass), "bubble"); + + object_class->set_property = gis_bubble_widget_set_property; + object_class->get_property = gis_bubble_widget_get_property; + + obj_props[PROP_LABEL] = g_param_spec_string ("label", "", "", NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + obj_props[PROP_ICON_NAME] = g_param_spec_string ("icon-name", "", "", NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties (object_class, PROP_LAST, obj_props); + + add_style_from_resource ("/org/gnome/initial-setup/gis-bubble-widget.css"); +} + +static void +gis_bubble_widget_init (GisBubbleWidget *widget) +{ + gtk_widget_init_template (GTK_WIDGET (widget)); +} diff --git a/gnome-initial-setup/pages/timezone/gis-bubble-widget.css b/gnome-initial-setup/pages/timezone/gis-bubble-widget.css new file mode 100644 index 0000000..768e69c --- /dev/null +++ b/gnome-initial-setup/pages/timezone/gis-bubble-widget.css @@ -0,0 +1,8 @@ + +bubble { + border-radius: 20px; + background-color: rgba(0, 0, 0, 0.6); + color: white; + font-weight: bold; + padding: 2em; +} diff --git a/gnome-initial-setup/pages/timezone/gis-bubble-widget.h b/gnome-initial-setup/pages/timezone/gis-bubble-widget.h new file mode 100644 index 0000000..6029166 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/gis-bubble-widget.h @@ -0,0 +1,53 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* + * Copyright (C) 2013 Red Hat + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + * + * Written by: + * Jasper St. Pierre <jstpierre@mecheye.net> + */ + +#ifndef __GIS_BUBBLE_WIDGET_H__ +#define __GIS_BUBBLE_WIDGET_H__ + +#include <adwaita.h> + +G_BEGIN_DECLS + +#define GIS_TYPE_BUBBLE_WIDGET (gis_bubble_widget_get_type ()) +#define GIS_BUBBLE_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIS_TYPE_BUBBLE_WIDGET, GisBubbleWidget)) +#define GIS_BUBBLE_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIS_TYPE_BUBBLE_WIDGET, GisBubbleWidgetClass)) +#define GIS_IS_BUBBLE_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIS_TYPE_BUBBLE_WIDGET)) +#define GIS_IS_BUBBLE_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIS_TYPE_BUBBLE_WIDGET)) +#define GIS_BUBBLE_WIDGET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIS_TYPE_BUBBLE_WIDGET, GisBubbleWidgetClass)) + +typedef struct _GisBubbleWidget GisBubbleWidget; +typedef struct _GisBubbleWidgetClass GisBubbleWidgetClass; + +struct _GisBubbleWidget +{ + AdwBin parent; +}; + +struct _GisBubbleWidgetClass +{ + AdwBinClass parent_class; +}; + +GType gis_bubble_widget_get_type (void); + +G_END_DECLS + +#endif /* __GIS_BUBBLE_WIDGET_H__ */ diff --git a/gnome-initial-setup/pages/timezone/gis-bubble-widget.ui b/gnome-initial-setup/pages/timezone/gis-bubble-widget.ui new file mode 100644 index 0000000..89dd121 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/gis-bubble-widget.ui @@ -0,0 +1,33 @@ +<?xml version="1.0"?> +<interface> + <template class="GisBubbleWidget" parent="AdwBin"> + <child> + <object class="GtkAspectFrame" id="aspect_frame"> + <property name="visible">True</property> + <child> + <object class="GtkBox" id="box"> + <property name="orientation">vertical</property> + <property name="width-request">128</property> + <property name="height-request">128</property> + <property name="halign">center</property> + <property name="valign">center</property> + <child> + <object class="GtkImage" id="icon"> + <property name="vexpand">True</property> + <property name="pixel-size">64</property> + </object> + </child> + <child> + <object class="GtkLabel" id="label"> + <property name="visible">True</property> + <property name="vexpand">True</property> + <property name="wrap">True</property> + <property name="justify">center</property> + </object> + </child> + </object> + </child> + </object> + </child> + </template> +</interface> diff --git a/gnome-initial-setup/pages/timezone/gis-location-entry.c b/gnome-initial-setup/pages/timezone/gis-location-entry.c new file mode 100644 index 0000000..de8fdb8 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/gis-location-entry.c @@ -0,0 +1,871 @@ +/* gweather-location-entry.c - Location-selecting text entry + * + * SPDX-FileCopyrightText: 2008, Red Hat, Inc. + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#include "config.h" + +#include "gis-location-entry.h" + +#include <string.h> +#include <glib/gi18n.h> +#include <gio/gio.h> +#include <geocode-glib/geocode-glib.h> + +/** + * GisLocationEntry: + * + * A subclass of [class@Gtk.SearchEntry] that provides autocompletion on + * [struct@GWeather.Location]s. + * + */ + +struct _GisLocationEntryPrivate { + GtkWidget *entry; + GWeatherLocation *location; + GWeatherLocation *top; + gboolean show_named_timezones; + GCancellable *cancellable; + GtkTreeModel *model; +}; + +static void editable_iface_init (GtkEditableInterface *iface); + +G_DEFINE_TYPE_WITH_CODE (GisLocationEntry, gis_location_entry, GTK_TYPE_WIDGET, + G_ADD_PRIVATE (GisLocationEntry) + G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE, editable_iface_init)); + +enum { + PROP_0, + + PROP_TOP, + PROP_SHOW_NAMED_TIMEZONES, + PROP_LOCATION, + + LAST_PROP +}; + +static void set_property (GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec); +static void get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec); + +static void set_location_internal (GisLocationEntry *entry, + GtkTreeModel *model, + GtkTreeIter *iter, + GWeatherLocation *loc); +static void +fill_location_entry_model (GtkListStore *store, GWeatherLocation *loc, + const char *parent_display_name, + const char *parent_sort_local_name, + const char *parent_compare_local_name, + const char *parent_compare_english_name, + gboolean show_named_timezones); + +enum LOC +{ + LOC_GIS_LOCATION_ENTRY_COL_DISPLAY_NAME = 0, + LOC_GIS_LOCATION_ENTRY_COL_LOCATION, + LOC_GIS_LOCATION_ENTRY_COL_LOCAL_SORT_NAME, + LOC_GIS_LOCATION_ENTRY_COL_LOCAL_COMPARE_NAME, + LOC_GIS_LOCATION_ENTRY_COL_ENGLISH_COMPARE_NAME, + LOC_GIS_LOCATION_ENTRY_NUM_COLUMNS +}; + +enum PLACE +{ + PLACE_GIS_LOCATION_ENTRY_COL_DISPLAY_NAME = 0, + PLACE_GIS_LOCATION_ENTRY_COL_PLACE, + PLACE_GIS_LOCATION_ENTRY_COL_LOCAL_SORT_NAME, + PLACE_GIS_LOCATION_ENTRY_COL_LOCAL_COMPARE_NAME +}; + +static gboolean matcher (GtkEntryCompletion *completion, const char *key, + GtkTreeIter *iter, gpointer user_data); +static gboolean match_selected (GtkEntryCompletion *completion, + GtkTreeModel *model, + GtkTreeIter *iter, + gpointer entry); +static void entry_changed (GisLocationEntry *entry); +static void _no_matches (GtkEntryCompletion *completion, GisLocationEntry *entry); + +static GtkEditable* +gis_location_entry_get_delegate (GtkEditable *editable) +{ + GisLocationEntry *entry = GIS_LOCATION_ENTRY (editable); + GisLocationEntryPrivate *priv = gis_location_entry_get_instance_private (entry); + + return GTK_EDITABLE (priv->entry); +} + +static void +editable_iface_init (GtkEditableInterface *iface) +{ + iface->get_delegate = gis_location_entry_get_delegate; +} + +static void +gis_location_entry_init (GisLocationEntry *entry) +{ + GtkEntryCompletion *completion; + GisLocationEntryPrivate *priv; + + priv = entry->priv = gis_location_entry_get_instance_private (entry); + + priv->entry = gtk_entry_new (); + gtk_widget_set_parent (priv->entry, GTK_WIDGET (entry)); + gtk_editable_init_delegate (GTK_EDITABLE (entry)); + + completion = gtk_entry_completion_new (); + + gtk_entry_completion_set_popup_set_width (completion, TRUE); + gtk_entry_completion_set_text_column (completion, LOC_GIS_LOCATION_ENTRY_COL_DISPLAY_NAME); + gtk_entry_completion_set_match_func (completion, matcher, NULL, NULL); + gtk_entry_completion_set_inline_completion (completion, TRUE); + + g_signal_connect (completion, "match-selected", + G_CALLBACK (match_selected), entry); + + g_signal_connect (completion, "no-matches", + G_CALLBACK (_no_matches), entry); + + gtk_entry_set_completion (GTK_ENTRY (entry->priv->entry), completion); + g_object_unref (completion); + + g_signal_connect (entry, "changed", + G_CALLBACK (entry_changed), NULL); +} + +static void +finalize (GObject *object) +{ + GisLocationEntry *entry; + GisLocationEntryPrivate *priv; + + entry = GIS_LOCATION_ENTRY (object); + priv = entry->priv; + + g_clear_object (&priv->location); + g_clear_object (&priv->top); + g_clear_object (&priv->model); + + G_OBJECT_CLASS (gis_location_entry_parent_class)->finalize (object); +} + +static void +dispose (GObject *object) +{ + GisLocationEntry *entry; + GisLocationEntryPrivate *priv; + + entry = GIS_LOCATION_ENTRY (object); + priv = entry->priv; + + if (priv->cancellable) { + g_cancellable_cancel (priv->cancellable); + g_object_unref (priv->cancellable); + priv->cancellable = NULL; + } + + gtk_editable_finish_delegate (GTK_EDITABLE (entry)); + g_clear_pointer (&priv->entry, gtk_widget_unparent); + + G_OBJECT_CLASS (gis_location_entry_parent_class)->dispose (object); +} + +static int +tree_compare_local_name (GtkTreeModel *model, + GtkTreeIter *a, + GtkTreeIter *b, + gpointer user_data) +{ + g_autofree gchar *name_a = NULL, *name_b = NULL; + + gtk_tree_model_get (model, a, + LOC_GIS_LOCATION_ENTRY_COL_LOCAL_SORT_NAME, &name_a, + -1); + gtk_tree_model_get (model, b, + LOC_GIS_LOCATION_ENTRY_COL_LOCAL_SORT_NAME, &name_b, + -1); + + return g_utf8_collate (name_a, name_b); +} + + +static void +constructed (GObject *object) +{ + GisLocationEntry *entry; + GtkListStore *store = NULL; + GtkEntryCompletion *completion; + + entry = GIS_LOCATION_ENTRY (object); + + if (!entry->priv->top) + entry->priv->top = gweather_location_get_world (); + + store = gtk_list_store_new (5, G_TYPE_STRING, GWEATHER_TYPE_LOCATION, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); + gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store), + tree_compare_local_name, NULL, NULL); + fill_location_entry_model (store, entry->priv->top, NULL, NULL, NULL, NULL, entry->priv->show_named_timezones); + + entry->priv->model = GTK_TREE_MODEL (store); + completion = gtk_entry_get_completion (GTK_ENTRY (entry->priv->entry)); + gtk_entry_completion_set_match_func (completion, matcher, NULL, NULL); + gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (store)); + + G_OBJECT_CLASS (gis_location_entry_parent_class)->constructed (object); +} + +static void +gis_location_entry_class_init (GisLocationEntryClass *location_entry_class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (location_entry_class); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (location_entry_class); + + object_class->constructed = constructed; + object_class->finalize = finalize; + object_class->set_property = set_property; + object_class->get_property = get_property; + object_class->dispose = dispose; + + /* properties */ + g_object_class_install_property ( + object_class, PROP_TOP, + g_param_spec_object ("top", + "Top Location", + "The GWeatherLocation whose children will be used to fill in the entry", + GWEATHER_TYPE_LOCATION, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property ( + object_class, PROP_SHOW_NAMED_TIMEZONES, + g_param_spec_boolean ("show-named-timezones", + "Show named timezones", + "Whether UTC and other named timezones are shown in the list of locations", + FALSE, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property ( + object_class, PROP_LOCATION, + g_param_spec_object ("location", + "Location", + "The selected GWeatherLocation", + GWEATHER_TYPE_LOCATION, + G_PARAM_READWRITE)); + + gtk_editable_install_properties (object_class, LAST_PROP); + + gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT); +} + +static void +set_property (GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + GisLocationEntry *entry = GIS_LOCATION_ENTRY (object); + + if (gtk_editable_delegate_set_property (object, prop_id, value, pspec)) + return; + + switch (prop_id) { + case PROP_TOP: + entry->priv->top = g_value_dup_object (value); + break; + case PROP_SHOW_NAMED_TIMEZONES: + entry->priv->show_named_timezones = g_value_get_boolean (value); + break; + case PROP_LOCATION: + gis_location_entry_set_location (GIS_LOCATION_ENTRY (object), + g_value_get_object (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + GisLocationEntry *entry = GIS_LOCATION_ENTRY (object); + + if (gtk_editable_delegate_get_property (object, prop_id, value, pspec)) + return; + + switch (prop_id) { + case PROP_SHOW_NAMED_TIMEZONES: + g_value_set_boolean (value, entry->priv->show_named_timezones); + break; + case PROP_LOCATION: + g_value_set_object (value, entry->priv->location); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +entry_changed (GisLocationEntry *entry) +{ + GtkEntryCompletion *completion; + const gchar *text; + + completion = gtk_entry_get_completion (GTK_ENTRY (entry->priv->entry)); + + if (entry->priv->cancellable) { + g_cancellable_cancel (entry->priv->cancellable); + g_object_unref (entry->priv->cancellable); + entry->priv->cancellable = NULL; + } + + gtk_entry_completion_set_match_func (completion, matcher, NULL, NULL); + gtk_entry_completion_set_model (completion, entry->priv->model); + + text = gtk_editable_get_text (GTK_EDITABLE (entry)); + + if (!text || *text == '\0') + set_location_internal (entry, NULL, NULL, NULL); +} + +static void +set_entry_text (GisLocationEntry *entry, + const char *text) +{ + GisLocationEntryPrivate *priv = entry->priv; + + if (g_strcmp0 (gtk_editable_get_text (GTK_EDITABLE (priv->entry)), text) != 0) + gtk_editable_set_text (GTK_EDITABLE (priv->entry), text); +} + +static void +set_location_internal (GisLocationEntry *entry, + GtkTreeModel *model, + GtkTreeIter *iter, + GWeatherLocation *loc) +{ + GisLocationEntryPrivate *priv; + char *name; + + priv = entry->priv; + + g_clear_object (&priv->location); + + g_assert (iter == NULL || loc == NULL); + + g_signal_handlers_block_by_func (entry, entry_changed, NULL); + + if (iter) { + gtk_tree_model_get (model, iter, + LOC_GIS_LOCATION_ENTRY_COL_DISPLAY_NAME, &name, + LOC_GIS_LOCATION_ENTRY_COL_LOCATION, &priv->location, + -1); + set_entry_text (entry, name); + g_free (name); + } else if (loc) { + priv->location = g_object_ref (loc); + set_entry_text (entry, gweather_location_get_name (loc)); + } else { + priv->location = NULL; + set_entry_text (entry, ""); + } + + g_signal_handlers_unblock_by_func (entry, entry_changed, NULL); + + gtk_editable_set_position (GTK_EDITABLE (entry), -1); + g_object_notify (G_OBJECT (entry), "location"); +} + +/** + * gis_location_entry_set_location: + * @entry: a #GisLocationEntry + * @loc: (allow-none): a #GWeatherLocation in @entry, or %NULL to + * clear @entry + * + * Sets @entry's location to @loc, and updates the text of the + * entry accordingly. + * Note that if the database contains a location that compares + * equal to @loc, that will be chosen in place of @loc. + **/ +void +gis_location_entry_set_location (GisLocationEntry *entry, + GWeatherLocation *loc) +{ + GtkEntryCompletion *completion; + GtkTreeModel *model; + GtkTreeIter iter; + g_autoptr(GWeatherLocation) cmploc = NULL; + + g_return_if_fail (GIS_IS_LOCATION_ENTRY (entry)); + + completion = gtk_entry_get_completion (GTK_ENTRY (entry->priv->entry)); + model = gtk_entry_completion_get_model (completion); + + if (loc == NULL) { + set_location_internal (entry, model, NULL, NULL); + return; + } + + gtk_tree_model_get_iter_first (model, &iter); + do { + gtk_tree_model_get (model, &iter, + LOC_GIS_LOCATION_ENTRY_COL_LOCATION, &cmploc, + -1); + if (gweather_location_equal (loc, cmploc)) { + set_location_internal (entry, model, &iter, NULL); + return; + } + + g_clear_object (&cmploc); + } while (gtk_tree_model_iter_next (model, &iter)); + + set_location_internal (entry, model, NULL, loc); +} + +/** + * gis_location_entry_get_location: + * @entry: a #GisLocationEntry + * + * Gets the location that was set by a previous call to + * gis_location_entry_set_location() or was selected by the user. + * + * Return value: (transfer full) (allow-none): the selected location + * (which you must unref when you are done with it), or %NULL if no + * location is selected. + **/ +GWeatherLocation * +gis_location_entry_get_location (GisLocationEntry *entry) +{ + g_return_val_if_fail (GIS_IS_LOCATION_ENTRY (entry), NULL); + + if (entry->priv->location) + return g_object_ref (entry->priv->location); + else + return NULL; +} + +/** + * gis_location_entry_set_city: + * @entry: a #GisLocationEntry + * @city_name: (allow-none): the city name, or %NULL + * @code: the METAR station code + * + * Sets @entry's location to a city with the given @code, and given + * @city_name, if non-%NULL. If there is no matching city, sets + * @entry's location to %NULL. + * + * Return value: %TRUE if @entry's location could be set to a matching city, + * %FALSE otherwise. + **/ +gboolean +gis_location_entry_set_city (GisLocationEntry *entry, + const char *city_name, + const char *code) +{ + GtkEntryCompletion *completion; + GtkTreeModel *model; + GtkTreeIter iter; + const char *cmpcode; + + g_return_val_if_fail (GIS_IS_LOCATION_ENTRY (entry), FALSE); + g_return_val_if_fail (code != NULL, FALSE); + + completion = gtk_entry_get_completion (GTK_ENTRY (entry->priv->entry)); + model = gtk_entry_completion_get_model (completion); + + gtk_tree_model_get_iter_first (model, &iter); + do { + g_autoptr(GWeatherLocation) cmploc = NULL; + gtk_tree_model_get (model, &iter, + LOC_GIS_LOCATION_ENTRY_COL_LOCATION, &cmploc, + -1); + + cmpcode = gweather_location_get_code (cmploc); + if (!cmpcode || strcmp (cmpcode, code) != 0) { + continue; + } + + if (city_name) { + g_autofree gchar *cmpname = gweather_location_get_city_name (cmploc); + if (!cmpname || strcmp (cmpname, city_name) != 0) { + continue; + } + } + + set_location_internal (entry, model, &iter, NULL); + return TRUE; + } while (gtk_tree_model_iter_next (model, &iter)); + + set_location_internal (entry, model, NULL, NULL); + + return FALSE; +} + +static void +fill_location_entry_model (GtkListStore *store, GWeatherLocation *loc, + const char *parent_display_name, + const char *parent_sort_local_name, + const char *parent_compare_local_name, + const char *parent_compare_english_name, + gboolean show_named_timezones) +{ + g_autoptr(GWeatherLocation) child = NULL; + char *display_name, *local_sort_name, *local_compare_name, *english_compare_name; + + switch (gweather_location_get_level (loc)) { + case GWEATHER_LOCATION_WORLD: + case GWEATHER_LOCATION_REGION: + /* Ignore these levels of hierarchy; just recurse, passing on + * the names from the parent node. + */ + while ((child = gweather_location_next_child (loc, child))) + fill_location_entry_model (store, child, + parent_display_name, + parent_sort_local_name, + parent_compare_local_name, + parent_compare_english_name, + show_named_timezones); + break; + + case GWEATHER_LOCATION_COUNTRY: + /* Recurse, initializing the names to the country name */ + while ((child = gweather_location_next_child (loc, child))) + fill_location_entry_model (store, child, + gweather_location_get_name (loc), + gweather_location_get_sort_name (loc), + gweather_location_get_sort_name (loc), + gweather_location_get_english_sort_name (loc), + show_named_timezones); + break; + + case GWEATHER_LOCATION_ADM1: + /* Recurse, adding the ADM1 name to the country name */ + /* Translators: this is the name of a location followed by a region, for example: + * 'London, United Kingdom' + * You shouldn't need to translate this string unless the language has a different comma. + */ + display_name = g_strdup_printf (_("%s, %s"), gweather_location_get_name (loc), parent_display_name); + local_sort_name = g_strdup_printf ("%s, %s", parent_sort_local_name, gweather_location_get_sort_name (loc)); + local_compare_name = g_strdup_printf ("%s, %s", gweather_location_get_sort_name (loc), parent_compare_local_name); + english_compare_name = g_strdup_printf ("%s, %s", gweather_location_get_english_sort_name (loc), parent_compare_english_name); + + while ((child = gweather_location_next_child (loc, child))) + fill_location_entry_model (store, child, + display_name, local_sort_name, local_compare_name, english_compare_name, + show_named_timezones); + + g_free (display_name); + g_free (local_compare_name); + g_free (english_compare_name); + break; + + case GWEATHER_LOCATION_CITY: + /* If there are multiple (<location>) children, we use the one + * closest to the city center. + * + * Locations are already sorted by increasing distance from + * the city. + */ + case GWEATHER_LOCATION_WEATHER_STATION: + /* <location> with no parent <city> */ + /* Translators: this is the name of a location followed by a region, for example: + * 'London, United Kingdom' + * You shouldn't need to translate this string unless the language has a different comma. + */ + display_name = g_strdup_printf (_("%s, %s"), + gweather_location_get_name (loc), parent_display_name); + local_sort_name = g_strdup_printf ("%s, %s", + parent_sort_local_name, gweather_location_get_sort_name (loc)); + local_compare_name = g_strdup_printf ("%s, %s", + gweather_location_get_sort_name (loc), parent_compare_local_name); + english_compare_name = g_strdup_printf ("%s, %s", + gweather_location_get_english_sort_name (loc), parent_compare_english_name); + + gtk_list_store_insert_with_values (store, NULL, -1, + LOC_GIS_LOCATION_ENTRY_COL_LOCATION, loc, + LOC_GIS_LOCATION_ENTRY_COL_DISPLAY_NAME, display_name, + LOC_GIS_LOCATION_ENTRY_COL_LOCAL_SORT_NAME, local_sort_name, + LOC_GIS_LOCATION_ENTRY_COL_LOCAL_COMPARE_NAME, local_compare_name, + LOC_GIS_LOCATION_ENTRY_COL_ENGLISH_COMPARE_NAME, english_compare_name, + -1); + + g_free (display_name); + g_free (local_compare_name); + g_free (english_compare_name); + break; + + case GWEATHER_LOCATION_NAMED_TIMEZONE: + if (show_named_timezones) { + gtk_list_store_insert_with_values (store, NULL, -1, + LOC_GIS_LOCATION_ENTRY_COL_LOCATION, loc, + LOC_GIS_LOCATION_ENTRY_COL_DISPLAY_NAME, gweather_location_get_name (loc), + LOC_GIS_LOCATION_ENTRY_COL_LOCAL_SORT_NAME, gweather_location_get_sort_name (loc), + LOC_GIS_LOCATION_ENTRY_COL_LOCAL_COMPARE_NAME, gweather_location_get_sort_name (loc), + LOC_GIS_LOCATION_ENTRY_COL_ENGLISH_COMPARE_NAME, gweather_location_get_english_sort_name (loc), + -1); + } + break; + + case GWEATHER_LOCATION_DETACHED: + g_assert_not_reached (); + } +} + +static char * +find_word (const char *full_name, const char *word, int word_len, + gboolean whole_word, gboolean is_first_word) +{ + char *p; + + if (word == NULL || *word == '\0') + return NULL; + + p = (char *)full_name - 1; + while ((p = strchr (p + 1, *word))) { + if (strncmp (p, word, word_len) != 0) + continue; + + if (p > (char *)full_name) { + char *prev = g_utf8_prev_char (p); + + /* Make sure p points to the start of a word */ + if (g_unichar_isalpha (g_utf8_get_char (prev))) + continue; + + /* If we're matching the first word of the key, it has to + * match the first word of the location, city, state, or + * country, or the abbreviation (in parenthesis). + * Eg, it either matches the start of the string + * (which we already know it doesn't at this point) or + * it is preceded by the string ", " or "(" (which isn't actually + * a perfect test. FIXME) + */ + if (is_first_word) { + if (prev == (char *)full_name || + ((prev - 1 <= full_name && strncmp (prev - 1, ", ", 2) != 0) + && *prev != '(')) + continue; + } + } + + if (whole_word && g_unichar_isalpha (g_utf8_get_char (p + word_len))) + continue; + + return p; + } + return NULL; +} + +static gboolean +match_compare_name (const char *key, const char *name) +{ + gboolean is_first_word = TRUE; + size_t len; + + /* Ignore whitespace before the string */ + key += strspn (key, " "); + + /* All but the last word in KEY must match a full word from NAME, + * in order (but possibly skipping some words from NAME). + */ + len = strcspn (key, " "); + while (key[len]) { + name = find_word (name, key, len, TRUE, is_first_word); + if (!name) + return FALSE; + + key += len; + while (*key && !g_unichar_isalpha (g_utf8_get_char (key))) + key = g_utf8_next_char (key); + while (*name && !g_unichar_isalpha (g_utf8_get_char (name))) + name = g_utf8_next_char (name); + + len = strcspn (key, " "); + is_first_word = FALSE; + } + + /* The last word in KEY must match a prefix of a following word in NAME */ + if (len == 0) { + return TRUE; + } else { + // if we get here, key[len] == 0, so... + g_assert (len == strlen(key)); + return find_word (name, key, len, FALSE, is_first_word) != NULL; + } +} + +static gboolean +matcher (GtkEntryCompletion *completion, const char *key, + GtkTreeIter *iter, gpointer user_data) +{ + char *local_compare_name, *english_compare_name; + gboolean match; + + gtk_tree_model_get (gtk_entry_completion_get_model (completion), iter, + LOC_GIS_LOCATION_ENTRY_COL_LOCAL_COMPARE_NAME, &local_compare_name, + LOC_GIS_LOCATION_ENTRY_COL_ENGLISH_COMPARE_NAME, &english_compare_name, + -1); + + match = match_compare_name (key, local_compare_name) || + match_compare_name (key, english_compare_name) || + g_ascii_strcasecmp (key, english_compare_name) == 0; + + g_free (local_compare_name); + g_free (english_compare_name); + return match; +} + +static gboolean +match_selected (GtkEntryCompletion *completion, + GtkTreeModel *model, + GtkTreeIter *iter, + gpointer entry) +{ + GisLocationEntryPrivate *priv; + + priv = ((GisLocationEntry *)entry)->priv; + + if (model != priv->model) { + GeocodePlace *place; + char *display_name; + GeocodeLocation *loc; + GWeatherLocation *location; + GWeatherLocation *scope = NULL; + const char* country_code; + + gtk_tree_model_get (model, iter, + PLACE_GIS_LOCATION_ENTRY_COL_PLACE, &place, + PLACE_GIS_LOCATION_ENTRY_COL_DISPLAY_NAME, &display_name, + -1); + + country_code = geocode_place_get_country_code (place); + if (country_code != NULL && gweather_location_get_level (priv->top) == GWEATHER_LOCATION_WORLD) + scope = gweather_location_find_by_country_code (priv->top, country_code); + if (!scope) + scope = priv->top; + + loc = geocode_place_get_location (place); + location = gweather_location_new_detached (display_name, + NULL, + geocode_location_get_latitude (loc), + geocode_location_get_longitude (loc)); + + set_location_internal (entry, model, NULL, location); + + g_object_unref (place); + g_object_unref (location); + g_free (display_name); + } else { + set_location_internal (entry, model, iter, NULL); + } + return TRUE; +} + +static gboolean +new_matcher (GtkEntryCompletion *completion, const char *key, + GtkTreeIter *iter, gpointer user_data) +{ + return TRUE; +} + +static void +fill_store (gpointer data, gpointer user_data) +{ + GeocodePlace *place = GEOCODE_PLACE (data); + GeocodeLocation *loc = geocode_place_get_location (place); + const char *display_name; + char *normalized; + char *compare_name; + + display_name = geocode_location_get_description (loc); + normalized = g_utf8_normalize (display_name, -1, G_NORMALIZE_ALL); + compare_name = g_utf8_casefold (normalized, -1); + + g_debug ("Adding geocode match %s", display_name); + + gtk_list_store_insert_with_values (user_data, NULL, -1, + PLACE_GIS_LOCATION_ENTRY_COL_PLACE, place, + PLACE_GIS_LOCATION_ENTRY_COL_DISPLAY_NAME, display_name, + PLACE_GIS_LOCATION_ENTRY_COL_LOCAL_SORT_NAME, compare_name, + PLACE_GIS_LOCATION_ENTRY_COL_LOCAL_COMPARE_NAME, compare_name, + -1); + + g_free (normalized); + g_free (compare_name); +} + +static void +_got_places (GObject *source_object, + GAsyncResult *result, + gpointer user_data) +{ + g_autolist(GeocodePlace) places = NULL; + GisLocationEntry *self = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GtkListStore) store = NULL; + GtkEntryCompletion *completion; + + places = geocode_forward_search_finish (GEOCODE_FORWARD (source_object), result, &error); + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { + /* return without touching anything if cancelled (the entry might have been disposed) */ + g_debug ("Geocode query cancelled"); + return; + } + + self = GIS_LOCATION_ENTRY (user_data); + completion = gtk_entry_get_completion (GTK_ENTRY (self->priv->entry)); + + if (places == NULL) { + g_debug ("No geocode results, restoring default model"); + gtk_entry_completion_set_match_func (completion, matcher, NULL, NULL); + gtk_entry_completion_set_model (completion, self->priv->model); + } else { + store = gtk_list_store_new (5, G_TYPE_STRING, GEOCODE_TYPE_PLACE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); + gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store), + tree_compare_local_name, NULL, NULL); + g_list_foreach (places, fill_store, store); + gtk_entry_completion_set_match_func (completion, new_matcher, NULL, NULL); + gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (store)); + } + + g_clear_object (&self->priv->cancellable); +} + +static void +_no_matches (GtkEntryCompletion *completion, GisLocationEntry *entry) { + const gchar *key = gtk_editable_get_text (GTK_EDITABLE (entry->priv->entry)); + GeocodeForward *forward; + + if (entry->priv->cancellable) { + g_cancellable_cancel (entry->priv->cancellable); + g_object_unref (entry->priv->cancellable); + entry->priv->cancellable = NULL; + } + + entry->priv->cancellable = g_cancellable_new (); + + g_debug ("Starting geocode query for %s", key); + forward = geocode_forward_new_for_string(key); + geocode_forward_search_async (forward, entry->priv->cancellable, _got_places, entry); +} + +/** + * gis_location_entry_new: + * @top: the top-level location for the entry. + * + * Creates a new #GisLocationEntry. + * + * @top will normally be the location returned from + * gweather_location_get_world(), but you can create an entry that + * only accepts a smaller set of locations if you want. + * + * Return value: the new #GisLocationEntry + **/ +GtkWidget * +gis_location_entry_new (GWeatherLocation *top) +{ + return g_object_new (GIS_TYPE_LOCATION_ENTRY, + "top", top, + NULL); +} diff --git a/gnome-initial-setup/pages/timezone/gis-location-entry.h b/gnome-initial-setup/pages/timezone/gis-location-entry.h new file mode 100644 index 0000000..ebd114f --- /dev/null +++ b/gnome-initial-setup/pages/timezone/gis-location-entry.h @@ -0,0 +1,46 @@ +/* gweather-location-entry.h - Location-selecting text entry + * + * SPDX-FileCopyrightText: 2008, Red Hat, Inc. + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#pragma once + +#include <adwaita.h> +#include <libgweather/gweather.h> + +G_BEGIN_DECLS + +typedef struct _GisLocationEntry GisLocationEntry; +typedef struct _GisLocationEntryClass GisLocationEntryClass; +typedef struct _GisLocationEntryPrivate GisLocationEntryPrivate; + +#define GIS_TYPE_LOCATION_ENTRY (gis_location_entry_get_type ()) +#define GIS_LOCATION_ENTRY(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GIS_TYPE_LOCATION_ENTRY, GisLocationEntry)) +#define GIS_LOCATION_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIS_TYPE_LOCATION_ENTRY, GisLocationEntryClass)) +#define GIS_IS_LOCATION_ENTRY(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GIS_TYPE_LOCATION_ENTRY)) +#define GIS_IS_LOCATION_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIS_TYPE_LOCATION_ENTRY)) +#define GIS_LOCATION_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIS_TYPE_LOCATION_ENTRY, GisLocationEntryClass)) + +struct _GisLocationEntry { + AdwBin parent; + + /*< private >*/ + GisLocationEntryPrivate *priv; +}; + +struct _GisLocationEntryClass { + AdwBinClass parent_class; +}; + +GType gis_location_entry_get_type (void); + +GtkWidget * gis_location_entry_new (GWeatherLocation *top); +void gis_location_entry_set_location (GisLocationEntry *entry, + GWeatherLocation *loc); +GWeatherLocation * gis_location_entry_get_location (GisLocationEntry *entry); +gboolean gis_location_entry_set_city (GisLocationEntry *entry, + const char *city_name, + const char *code); + +G_END_DECLS diff --git a/gnome-initial-setup/pages/timezone/gis-timezone-page.c b/gnome-initial-setup/pages/timezone/gis-timezone-page.c new file mode 100644 index 0000000..2ca34d0 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/gis-timezone-page.c @@ -0,0 +1,539 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* + * Copyright (C) 2012 Red Hat + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + * + * Written by: + * Jasper St. Pierre <jstpierre@mecheye.net> + */ + +/* Timezone page {{{1 */ + +#define PAGE_ID "timezone" + +#include "config.h" +#include "gis-timezone-page.h" + +#include <glib/gi18n.h> +#include <gio/gio.h> + +#include <stdlib.h> +#include <string.h> + +#define GNOME_DESKTOP_USE_UNSTABLE_API +#include <libgnome-desktop/gnome-languages.h> +#include <libgnome-desktop/gnome-wall-clock.h> +#include <gdesktop-enums.h> +#include <geoclue.h> +#include <geocode-glib/geocode-glib.h> + +#include <libgweather/gweather.h> + +#include "timedated.h" +#include "cc-datetime-resources.h" +#include "timezone-resources.h" + +#include "cc-timezone-map.h" +#include "gis-bubble-widget.h" + +#include "gis-page-header.h" +#include "gis-location-entry.h" + +#define DEFAULT_TZ "Europe/London" +#define DESKTOP_ID "gnome-datetime-panel" + +#define CLOCK_SCHEMA "org.gnome.desktop.interface" +#define CLOCK_FORMAT_KEY "clock-format" + +/* FIXME: Drop this when we depend on a version of GeoClue which has + * https://gitlab.freedesktop.org/geoclue/geoclue/-/merge_requests/73 */ +typedef GClueSimple MyGClueSimple; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (MyGClueSimple, g_object_unref) + +static void stop_geolocation (GisTimezonePage *page); + +struct _GisTimezonePagePrivate +{ + GtkWidget *map; + GtkWidget *search_entry; + GtkWidget *search_overlay; + + GCancellable *geoclue_cancellable; + GClueClient *geoclue_client; + GClueSimple *geoclue_simple; + gboolean in_geoclue_callback; + GWeatherLocation *current_location; + Timedate1 *dtm; + GCancellable *dtm_cancellable; + + GnomeWallClock *clock; + GDesktopClockFormat clock_format; + gboolean in_search; + + gulong search_entry_text_changed_id; +}; +typedef struct _GisTimezonePagePrivate GisTimezonePagePrivate; + +G_DEFINE_TYPE_WITH_PRIVATE (GisTimezonePage, gis_timezone_page, GIS_TYPE_PAGE); + +static void +set_timezone_cb (GObject *source, + GAsyncResult *res, + gpointer user_data) +{ + Timedate1 *dtm = TIMEDATE1 (source); + g_autoptr(GError) error = NULL; + + if (!timedate1_call_set_timezone_finish (dtm, + res, + &error)) { + /* TODO: display any error in a user friendly way */ + g_warning ("Could not set system timezone: %s", error->message); + } +} + + +static void +queue_set_timezone (GisTimezonePage *page, + const char *tzid) +{ + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + + /* for now just do it */ + timedate1_call_set_timezone (priv->dtm, + tzid, + TRUE, + priv->dtm_cancellable, + set_timezone_cb, + page); +} + +static void +set_location (GisTimezonePage *page, + GWeatherLocation *location) +{ + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + + g_clear_object (&priv->current_location); + + gtk_widget_set_visible (priv->search_overlay, (location == NULL)); + gis_page_set_complete (GIS_PAGE (page), (location != NULL)); + + if (location) + { + GTimeZone *zone; + const char *tzid; + + priv->current_location = g_object_ref (location); + + zone = gweather_location_get_timezone (location); + tzid = g_time_zone_get_identifier (zone); + + cc_timezone_map_set_timezone (CC_TIMEZONE_MAP (priv->map), tzid); + + /* If this location is manually set, stop waiting for geolocation. */ + if (!priv->in_geoclue_callback) + stop_geolocation (page); + } +} + +static void +on_location_notify (GClueSimple *simple, + GParamSpec *pspec, + gpointer user_data) +{ + GisTimezonePage *page = user_data; + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + GClueLocation *location; + gdouble latitude, longitude; + g_autoptr(GWeatherLocation) world = gweather_location_get_world (); + g_autoptr(GWeatherLocation) glocation = NULL; + + location = gclue_simple_get_location (simple); + + latitude = gclue_location_get_latitude (location); + longitude = gclue_location_get_longitude (location); + + glocation = gweather_location_find_nearest_city (world, latitude, longitude); + priv->in_geoclue_callback = TRUE; + set_location (page, glocation); + priv->in_geoclue_callback = FALSE; +} + +static void +on_geoclue_simple_ready (GObject *source_object, + GAsyncResult *res, + gpointer user_data) +{ + GisTimezonePage *page = user_data; + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + g_autoptr(GError) local_error = NULL; + g_autoptr(MyGClueSimple) geoclue_simple = NULL; + + /* This function may be called in an idle callback once @page has been + * disposed, if going through cancellation. So don’t dereference @priv or + * @page until the error has been checked. */ + geoclue_simple = gclue_simple_new_finish (res, &local_error); + if (local_error != NULL) + { + if (!g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_info ("Failed to connect to GeoClue2 service: %s", local_error->message); + return; + } + + priv->geoclue_simple = g_steal_pointer (&geoclue_simple); + priv->geoclue_client = gclue_simple_get_client (priv->geoclue_simple); + gclue_client_set_distance_threshold (priv->geoclue_client, + GEOCODE_LOCATION_ACCURACY_CITY); + + g_signal_connect (priv->geoclue_simple, "notify::location", + G_CALLBACK (on_location_notify), page); + + on_location_notify (priv->geoclue_simple, NULL, page); +} + +static void +get_location_from_geoclue_async (GisTimezonePage *page) +{ + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + + gclue_simple_new (DESKTOP_ID, + GCLUE_ACCURACY_LEVEL_CITY, + priv->geoclue_cancellable, + on_geoclue_simple_ready, + page); +} + +static void +entry_text_changed (GtkEditable *editable, + gpointer user_data) +{ + GisTimezonePage *page = GIS_TIMEZONE_PAGE (user_data); + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + + stop_geolocation (GIS_TIMEZONE_PAGE (user_data)); + g_signal_handler_disconnect (priv->search_entry, + priv->search_entry_text_changed_id); + priv->search_entry_text_changed_id = 0; +} + +static void +entry_location_changed (GObject *object, GParamSpec *param, GisTimezonePage *page) +{ + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + GisLocationEntry *entry = GIS_LOCATION_ENTRY (object); + g_autoptr(GWeatherLocation) location = NULL; + + location = gis_location_entry_get_location (entry); + if (!location) + return; + + priv->in_search = TRUE; + set_location (page, location); + priv->in_search = FALSE; +} + +#define GETTEXT_PACKAGE_TIMEZONES "gnome-control-center-2.0-timezones" + +static char * +translated_city_name (TzLocation *loc) +{ + char *country; + char *name; + char *zone_translated; + char **split_translated; + gint length; + + /* Load the translation for it */ + zone_translated = g_strdup (dgettext (GETTEXT_PACKAGE_TIMEZONES, loc->zone)); + g_strdelimit (zone_translated, "_", ' '); + split_translated = g_regex_split_simple ("[\\x{2044}\\x{2215}\\x{29f8}\\x{ff0f}/]", + zone_translated, + 0, 0); + g_free (zone_translated); + + length = g_strv_length (split_translated); + + country = gnome_get_country_from_code (loc->country, NULL); + /* Translators: "city, country" */ + name = g_strdup_printf (C_("timezone loc", "%s, %s"), + split_translated[length-1], + country); + g_free (country); + g_strfreev (split_translated); + + return name; +} + +static void +update_timezone (GisTimezonePage *page, TzLocation *location) +{ + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + char *tz_desc; + char *bubble_text; + char *city_country; + char *utc_label; + char *time_label; + GTimeZone *zone; + GDateTime *date; + gboolean use_ampm; + + if (priv->clock_format == G_DESKTOP_CLOCK_FORMAT_12H) + use_ampm = TRUE; + else + use_ampm = FALSE; + + zone = g_time_zone_new (location->zone); + date = g_date_time_new_now (zone); + g_time_zone_unref (zone); + + /* Update the text bubble in the timezone map */ + city_country = translated_city_name (location); + + /* Translators: UTC here means the Coordinated Universal Time. + * %:::z will be replaced by the offset from UTC e.g. UTC+02 + */ + utc_label = g_date_time_format (date, _("UTC%:::z")); + + if (use_ampm) + /* Translators: This is the time format used in 12-hour mode. */ + time_label = g_date_time_format (date, _("%l:%M %p")); + else + /* Translators: This is the time format used in 24-hour mode. */ + time_label = g_date_time_format (date, _("%R")); + + /* Translators: "timezone (utc shift)" */ + tz_desc = g_strdup_printf (C_("timezone map", "%s (%s)"), + g_date_time_get_timezone_abbreviation (date), + utc_label); + bubble_text = g_strdup_printf ("<b>%s</b>\n" + "<small>%s</small>\n" + "<b>%s</b>", + tz_desc, + city_country, + time_label); + cc_timezone_map_set_bubble_text (CC_TIMEZONE_MAP (priv->map), bubble_text); + + g_free (tz_desc); + g_free (city_country); + g_free (utc_label); + g_free (time_label); + g_free (bubble_text); + + g_date_time_unref (date); +} + +static void +map_location_changed (CcTimezoneMap *map, + TzLocation *location, + GisTimezonePage *page) +{ + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + + gtk_widget_set_visible (priv->search_overlay, (location == NULL)); + gis_page_set_complete (GIS_PAGE (page), (location != NULL)); + + if (!priv->in_search) + gtk_editable_set_text (GTK_EDITABLE (priv->search_entry), ""); + + update_timezone (page, location); + queue_set_timezone (page, location->zone); +} + +static void +on_clock_changed (GnomeWallClock *clock, + GParamSpec *pspec, + GisTimezonePage *page) +{ + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + TzLocation *location; + + if (!gtk_widget_get_mapped (priv->map)) + return; + + if (gtk_widget_is_visible (priv->search_overlay)) + return; + + location = cc_timezone_map_get_location (CC_TIMEZONE_MAP (priv->map)); + if (location) + update_timezone (page, location); +} + +static void +entry_mapped (GtkWidget *widget, + gpointer user_data) +{ + gtk_widget_grab_focus (widget); +} + +static void +stop_geolocation (GisTimezonePage *page) +{ + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + + if (priv->geoclue_cancellable) + { + g_cancellable_cancel (priv->geoclue_cancellable); + g_clear_object (&priv->geoclue_cancellable); + } + + if (priv->geoclue_client) + { + gclue_client_call_stop (priv->geoclue_client, NULL, NULL, NULL); + priv->geoclue_client = NULL; + } + g_clear_object (&priv->geoclue_simple); +} + +static void +gis_timezone_page_root (GtkWidget *widget) +{ + GisTimezonePage *page = GIS_TIMEZONE_PAGE (widget); + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + + GTK_WIDGET_CLASS (gis_timezone_page_parent_class)->root (widget); + + if (priv->geoclue_cancellable == NULL) + { + priv->geoclue_cancellable = g_cancellable_new (); + get_location_from_geoclue_async (page); + } +} + +static void +gis_timezone_page_constructed (GObject *object) +{ + GisTimezonePage *page = GIS_TIMEZONE_PAGE (object); + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + GError *error; + GSettings *settings; + + G_OBJECT_CLASS (gis_timezone_page_parent_class)->constructed (object); + + priv->dtm_cancellable = g_cancellable_new (); + + error = NULL; + priv->dtm = timedate1_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + "org.freedesktop.timedate1", + "/org/freedesktop/timedate1", + priv->dtm_cancellable, + &error); + if (priv->dtm == NULL) { + g_error ("Failed to create proxy for timedated: %s", error->message); + exit (1); + } + + priv->clock = g_object_new (GNOME_TYPE_WALL_CLOCK, NULL); + g_signal_connect (priv->clock, "notify::clock", G_CALLBACK (on_clock_changed), page); + + settings = g_settings_new (CLOCK_SCHEMA); + priv->clock_format = g_settings_get_enum (settings, CLOCK_FORMAT_KEY); + g_object_unref (settings); + + set_location (page, NULL); + + priv->search_entry_text_changed_id = + g_signal_connect (priv->search_entry, "changed", + G_CALLBACK (entry_text_changed), page); + g_signal_connect (priv->search_entry, "notify::location", + G_CALLBACK (entry_location_changed), page); + g_signal_connect (priv->search_entry, "map", + G_CALLBACK (entry_mapped), page); + g_signal_connect (priv->map, "location-changed", + G_CALLBACK (map_location_changed), page); + + gtk_widget_show (GTK_WIDGET (page)); +} + +static void +gis_timezone_page_dispose (GObject *object) +{ + GisTimezonePage *page = GIS_TIMEZONE_PAGE (object); + GisTimezonePagePrivate *priv = gis_timezone_page_get_instance_private (page); + + stop_geolocation (page); + + if (priv->dtm_cancellable != NULL) + { + g_cancellable_cancel (priv->dtm_cancellable); + g_clear_object (&priv->dtm_cancellable); + } + + g_clear_object (&priv->dtm); + g_clear_object (&priv->clock); + + G_OBJECT_CLASS (gis_timezone_page_parent_class)->dispose (object); +} + +static void +gis_timezone_page_locale_changed (GisPage *page) +{ + gis_page_set_title (GIS_PAGE (page), _("Time Zone")); +} + +static gboolean +gis_timezone_page_apply (GisPage *page, + GCancellable *cancellable) +{ + /* Once the user accepts the location, it would be unkind to change it if + * GeoClue suddenly tells us we're somewhere else. + */ + stop_geolocation (GIS_TIMEZONE_PAGE (page)); + + return FALSE; +} + +static void +gis_timezone_page_class_init (GisTimezonePageClass *klass) +{ + GisPageClass *page_class = GIS_PAGE_CLASS (klass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/initial-setup/gis-timezone-page.ui"); + + gtk_widget_class_bind_template_child_private (widget_class, GisTimezonePage, map); + gtk_widget_class_bind_template_child_private (widget_class, GisTimezonePage, search_entry); + gtk_widget_class_bind_template_child_private (widget_class, GisTimezonePage, search_overlay); + + page_class->page_id = PAGE_ID; + page_class->locale_changed = gis_timezone_page_locale_changed; + page_class->apply = gis_timezone_page_apply; + object_class->constructed = gis_timezone_page_constructed; + object_class->dispose = gis_timezone_page_dispose; + widget_class->root = gis_timezone_page_root; +} + +static void +gis_timezone_page_init (GisTimezonePage *page) +{ + g_resources_register (timezone_get_resource ()); + g_resources_register (datetime_get_resource ()); + g_type_ensure (CC_TYPE_TIMEZONE_MAP); + g_type_ensure (GIS_TYPE_BUBBLE_WIDGET); + g_type_ensure (GIS_TYPE_PAGE_HEADER); + g_type_ensure (GIS_TYPE_LOCATION_ENTRY); + + gtk_widget_init_template (GTK_WIDGET (page)); +} + +GisPage * +gis_prepare_timezone_page (GisDriver *driver) +{ + return g_object_new (GIS_TYPE_TIMEZONE_PAGE, + "driver", driver, + NULL); +} diff --git a/gnome-initial-setup/pages/timezone/gis-timezone-page.h b/gnome-initial-setup/pages/timezone/gis-timezone-page.h new file mode 100644 index 0000000..e9ba8ed --- /dev/null +++ b/gnome-initial-setup/pages/timezone/gis-timezone-page.h @@ -0,0 +1,57 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* + * Copyright (C) 2012 Red Hat + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + * + * Written by: + * Jasper St. Pierre <jstpierre@mecheye.net> + */ + +#ifndef __GIS_TIMEZONE_PAGE_H__ +#define __GIS_TIMEZONE_PAGE_H__ + +#include <glib-object.h> + +#include "gnome-initial-setup.h" + +G_BEGIN_DECLS + +#define GIS_TYPE_TIMEZONE_PAGE (gis_timezone_page_get_type ()) +#define GIS_TIMEZONE_PAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIS_TYPE_TIMEZONE_PAGE, GisTimezonePage)) +#define GIS_TIMEZONE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIS_TYPE_TIMEZONE_PAGE, GisTimezonePageClass)) +#define GIS_IS_TIMEZONE_PAGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIS_TYPE_TIMEZONE_PAGE)) +#define GIS_IS_TIMEZONE_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIS_TYPE_TIMEZONE_PAGE)) +#define GIS_TIMEZONE_PAGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIS_TYPE_TIMEZONE_PAGE, GisTimezonePageClass)) + +typedef struct _GisTimezonePage GisTimezonePage; +typedef struct _GisTimezonePageClass GisTimezonePageClass; + +struct _GisTimezonePage +{ + GisPage parent; +}; + +struct _GisTimezonePageClass +{ + GisPageClass parent_class; +}; + +GType gis_timezone_page_get_type (void); + +GisPage *gis_prepare_timezone_page (GisDriver *driver); + +G_END_DECLS + +#endif /* __GIS_TIMEZONE_PAGE_H__ */ diff --git a/gnome-initial-setup/pages/timezone/gis-timezone-page.ui b/gnome-initial-setup/pages/timezone/gis-timezone-page.ui new file mode 100644 index 0000000..a48b53e --- /dev/null +++ b/gnome-initial-setup/pages/timezone/gis-timezone-page.ui @@ -0,0 +1,59 @@ +<?xml version="1.0" encoding="UTF-8"?> +<interface> + <template class="GisTimezonePage" parent="GisPage"> + <child> + <object class="GtkBox" id="box"> + <property name="orientation">vertical</property> + <property name="halign">center</property> + <property name="valign">fill</property> + <child> + <object class="GisPageHeader" id="header"> + <property name="margin_top">24</property> + <property name="title" translatable="yes">Time Zone</property> + <property name="subtitle" translatable="yes">The time zone will be set automatically if your location can be found. You can also search for a city to set it yourself.</property> + <property name="icon_name">find-location-symbolic</property> + <property name="show_icon" bind-source="GisTimezonePage" bind-property="small-screen" bind-flags="invert-boolean|sync-create"/> + </object> + </child> + <child> + <object class="GtkBox" id="page_box"> + <property name="margin_top">18</property> + <property name="valign">center</property> + <property name="orientation">vertical</property> + <property name="spacing">14</property> + <child> + <object class="GisLocationEntry" id="search_entry"> + <property name="halign">center</property> + <property name="max-width-chars">55</property> + </object> + </child> + <child> + <object class="GtkFrame" id="map_frame"> + <property name="margin_bottom">18</property> + <property name="hexpand">True</property> + <property name="label_xalign">0</property> + <child> + <object class="GtkOverlay" id="map_overlay"> + <child> + <object class="CcTimezoneMap" id="map"> + <property name="hexpand">True</property> + </object> + </child> + <child type="overlay"> + <object class="GisBubbleWidget" id="search_overlay"> + <property name="label" translatable="yes">Please search for a nearby city</property> + <property name="icon-name">edit-find-symbolic</property> + <property name="halign">center</property> + <property name="valign">center</property> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + </template> +</interface> diff --git a/gnome-initial-setup/pages/timezone/meson.build b/gnome-initial-setup/pages/timezone/meson.build new file mode 100644 index 0000000..54a974e --- /dev/null +++ b/gnome-initial-setup/pages/timezone/meson.build @@ -0,0 +1,45 @@ +sources += gnome.gdbus_codegen( + 'timedated', + 'timedated1-interface.xml', + interface_prefix: 'org.freedesktop.', +) + +sources += gnome.compile_resources( + 'cc-datetime-resources', + files('datetime.gresource.xml'), + c_name: 'datetime' +) + +sources += gnome.compile_resources( + 'timezone-resources', + files('timezone.gresource.xml'), + c_name: 'timezone' +) + +sources += files( + 'cc-timezone-map.c', + 'cc-timezone-map.h', + 'tz.c', + 'tz.h', + 'gis-bubble-widget.c', + 'gis-bubble-widget.h', + 'gis-location-entry.c', + 'gis-location-entry.h', + 'gis-timezone-page.c', + 'gis-timezone-page.h' +) + +executable('test-gis-location-entry', + files( + 'gis-location-entry.c', + 'gis-location-entry.h', + 'test-gis-location-entry.c', + ), + dependencies: [ + libadwaita_dep, + gweather_dep, + geocode_glib_2_dep, + ], + include_directories: config_h_dir, + install: false, +)
\ No newline at end of file diff --git a/gnome-initial-setup/pages/timezone/test-gis-location-entry.c b/gnome-initial-setup/pages/timezone/test-gis-location-entry.c new file mode 100644 index 0000000..c6413e8 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/test-gis-location-entry.c @@ -0,0 +1,54 @@ +#include "config.h" + +#include "gis-location-entry.h" + +#include <adwaita.h> + +static void +entry_location_changed (GObject *object, + GParamSpec *param, + gpointer data) +{ + GtkLabel *label = GTK_LABEL (data); + GisLocationEntry *entry = GIS_LOCATION_ENTRY (object); + g_autoptr(GWeatherLocation) location = NULL; + const gchar *message = NULL; + + location = gis_location_entry_get_location (entry); + message = location != NULL ? gweather_location_get_name (location) : "No location selected"; + g_message ("%s: %s", G_STRLOC, message); + gtk_label_set_text (label, message); +} + +static void +activate_cb (GtkApplication *app) +{ + GtkWidget *window = gtk_application_window_new (app); + GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); + GtkWidget *label = gtk_label_new ("Pick a location…"); + GtkWidget *entry = gis_location_entry_new (NULL); + + gtk_box_append (GTK_BOX (box), label); + gtk_box_append (GTK_BOX (box), entry); + + g_signal_connect (entry, "notify::location", + G_CALLBACK (entry_location_changed), label); + + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + gtk_window_set_default_size (GTK_WINDOW (window), 1024, 768); + gtk_window_set_child (GTK_WINDOW (window), box); + gtk_window_present (GTK_WINDOW (window)); +} + +int +main (int argc, + char *argv[]) +{ + g_autoptr (AdwApplication) app = NULL; + + app = adw_application_new ("org.gnome.InitialSetup.TestLocationEntry", G_APPLICATION_FLAGS_NONE); + + g_signal_connect (app, "activate", G_CALLBACK (activate_cb), NULL); + + return g_application_run (G_APPLICATION (app), argc, argv); +} diff --git a/gnome-initial-setup/pages/timezone/timedated1-interface.xml b/gnome-initial-setup/pages/timezone/timedated1-interface.xml new file mode 100644 index 0000000..3370e0e --- /dev/null +++ b/gnome-initial-setup/pages/timezone/timedated1-interface.xml @@ -0,0 +1,28 @@ +<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" +"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> +<node> + <interface name="org.freedesktop.timedate1"> + <property name="Timezone" type="s" access="read"/> + <property name="LocalRTC" type="b" access="read"/> + <property name="CanNTP" type="b" access="read"/> + <property name="NTP" type="b" access="read"/> + <method name="SetTime"> + <arg name="usec_utc" type="x" direction="in"/> + <arg name="relative" type="b" direction="in"/> + <arg name="user_interaction" type="b" direction="in"/> + </method> + <method name="SetTimezone"> + <arg name="timezone" type="s" direction="in"/> + <arg name="user_interaction" type="b" direction="in"/> + </method> + <method name="SetLocalRTC"> + <arg name="local_rtc" type="b" direction="in"/> + <arg name="fix_system" type="b" direction="in"/> + <arg name="user_interaction" type="b" direction="in"/> + </method> + <method name="SetNTP"> + <arg name="use_ntp" type="b" direction="in"/> + <arg name="user_interaction" type="b" direction="in"/> + </method> + </interface> +</node> diff --git a/gnome-initial-setup/pages/timezone/timezone.gresource.xml b/gnome-initial-setup/pages/timezone/timezone.gresource.xml new file mode 100644 index 0000000..cf36818 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/timezone.gresource.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<gresources> + <gresource prefix="/org/gnome/initial-setup"> + <file preprocess="xml-stripblanks" alias="gis-timezone-page.ui">gis-timezone-page.ui</file> + <file preprocess="xml-stripblanks" alias="gis-bubble-widget.ui">gis-bubble-widget.ui</file> + <file alias="gis-bubble-widget.css">gis-bubble-widget.css</file> + </gresource> +</gresources> diff --git a/gnome-initial-setup/pages/timezone/tz.c b/gnome-initial-setup/pages/timezone/tz.c new file mode 100644 index 0000000..4fe2305 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/tz.c @@ -0,0 +1,492 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* Generic timezone utilities. + * + * Copyright (C) 2000-2001 Ximian, Inc. + * + * Authors: Hans Petter Jansson <hpj@ximian.com> + * + * Largely based on Michael Fulbright's work on Anaconda. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + */ + + +#include <glib.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <time.h> +#include <math.h> +#include <string.h> +#include <ctype.h> +#include "tz.h" +#include "cc-datetime-resources.h" + + +/* Forward declarations for private functions */ + +static float convert_pos (gchar *pos, int digits); +static int compare_country_names (const void *a, const void *b); +static void sort_locations_by_country (GPtrArray *locations); +static gchar * tz_data_file_get (void); +static void load_backward_tz (TzDB *tz_db); + +/* ---------------- * + * Public interface * + * ---------------- */ +TzDB * +tz_load_db (void) +{ + g_autofree gchar *tz_data_file = NULL; + TzDB *tz_db; + FILE *tzfile; + char buf[4096]; + + tz_data_file = tz_data_file_get (); + if (!tz_data_file) { + g_warning ("Could not get the TimeZone data file name"); + return NULL; + } + tzfile = fopen (tz_data_file, "r"); + if (!tzfile) { + g_warning ("Could not open *%s*\n", tz_data_file); + return NULL; + } + + tz_db = g_new0 (TzDB, 1); + tz_db->locations = g_ptr_array_new (); + + while (fgets (buf, sizeof(buf), tzfile)) + { + g_auto(GStrv) tmpstrarr = NULL; + g_autofree gchar *latstr = NULL; + g_autofree gchar *lngstr = NULL; + gchar *p; + TzLocation *loc; + + if (*buf == '#') continue; + + g_strchomp(buf); + tmpstrarr = g_strsplit(buf,"\t", 6); + + latstr = g_strdup (tmpstrarr[1]); + p = latstr + 1; + while (*p != '-' && *p != '+') p++; + lngstr = g_strdup (p); + *p = '\0'; + + loc = g_new0 (TzLocation, 1); + loc->country = g_strdup (tmpstrarr[0]); + loc->zone = g_strdup (tmpstrarr[2]); + loc->latitude = convert_pos (latstr, 2); + loc->longitude = convert_pos (lngstr, 3); + +#ifdef __sun + if (tmpstrarr[3] && *tmpstrarr[3] == '-' && tmpstrarr[4]) + loc->comment = g_strdup (tmpstrarr[4]); + + if (tmpstrarr[3] && *tmpstrarr[3] != '-' && !islower(loc->zone)) { + TzLocation *locgrp; + + /* duplicate entry */ + locgrp = g_new0 (TzLocation, 1); + locgrp->country = g_strdup (tmpstrarr[0]); + locgrp->zone = g_strdup (tmpstrarr[3]); + locgrp->latitude = convert_pos (latstr, 2); + locgrp->longitude = convert_pos (lngstr, 3); + locgrp->comment = (tmpstrarr[4]) ? g_strdup (tmpstrarr[4]) : NULL; + + g_ptr_array_add (tz_db->locations, (gpointer) locgrp); + } +#else + loc->comment = (tmpstrarr[3]) ? g_strdup(tmpstrarr[3]) : NULL; +#endif + + g_ptr_array_add (tz_db->locations, (gpointer) loc); + } + + fclose (tzfile); + + /* now sort by country */ + sort_locations_by_country (tz_db->locations); + + /* Load up the hashtable of backward links */ + load_backward_tz (tz_db); + + return tz_db; +} + +static void +tz_location_free (TzLocation *loc) +{ + g_free (loc->country); + g_free (loc->zone); + g_free (loc->comment); + + g_free (loc); +} + +void +tz_db_free (TzDB *db) +{ + g_ptr_array_foreach (db->locations, (GFunc) tz_location_free, NULL); + g_ptr_array_free (db->locations, TRUE); + g_hash_table_destroy (db->backward); + g_free (db); +} + +GPtrArray * +tz_get_locations (TzDB *db) +{ + return db->locations; +} + + +gchar * +tz_location_get_country (TzLocation *loc) +{ + return loc->country; +} + + +gchar * +tz_location_get_zone (TzLocation *loc) +{ + return loc->zone; +} + + +gchar * +tz_location_get_comment (TzLocation *loc) +{ + return loc->comment; +} + + +void +tz_location_get_position (TzLocation *loc, double *longitude, double *latitude) +{ + *longitude = loc->longitude; + *latitude = loc->latitude; +} + +/* For timezone map display purposes, we try to highlight regions of the + * world that keep the same time. There is no reasonable API to discover + * this; at the moment we just group timezones by their non-daylight-savings + * UTC offset and hope that's good enough. However, in some cases that + * produces confusing results. For example, Irish Standard Time is legally + * defined as the country's summer time, with a negative DST offset in + * winter; but this results in the same observed clock times as countries + * that observe Western European (Summer) Time, not those that observe + * Central European (Summer) Time, so we should group Ireland with the + * former, matching the grouping implied by data/timezone_*.png. + * + * This is something of a hack, and there remain other problems with + * timezone grouping: for example, grouping timezones north and south of the + * equator together where DST is observed at different times of the year is + * dubious. + */ +struct { + const char *zone; + gint offset; +} base_offset_overrides[] = { + { "Europe/Dublin", 0 }, +}; + +glong +tz_location_get_base_utc_offset (TzLocation *loc) +{ + g_autoptr(TzInfo) tz_info = NULL; + glong offset; + guint i; + + tz_info = tz_info_from_location (loc); + offset = tz_info->utc_offset + (tz_info->daylight ? -3600 : 0); + + for (i = 0; i < G_N_ELEMENTS (base_offset_overrides); i++) { + if (g_str_equal (loc->zone, base_offset_overrides[i].zone)) { + offset = base_offset_overrides[i].offset; + break; + } + } + + return offset; +} + +TzInfo * +tz_info_from_location (TzLocation *loc) +{ + TzInfo *tzinfo; + time_t curtime; + struct tm *curzone; + g_autofree gchar *tz_env_value = NULL; + + g_return_val_if_fail (loc != NULL, NULL); + g_return_val_if_fail (loc->zone != NULL, NULL); + + tz_env_value = g_strdup (getenv ("TZ")); + setenv ("TZ", loc->zone, 1); + +#if 0 + tzset (); +#endif + tzinfo = g_new0 (TzInfo, 1); + + curtime = time (NULL); + curzone = localtime (&curtime); + +#ifndef __sun + tzinfo->tzname = g_strdup (curzone->tm_zone); + tzinfo->utc_offset = curzone->tm_gmtoff; +#else + tzinfo->tzname = NULL; + tzinfo->utc_offset = 0; +#endif + + tzinfo->daylight = curzone->tm_isdst; + + if (tz_env_value) + setenv ("TZ", tz_env_value, 1); + else + unsetenv ("TZ"); + + return tzinfo; +} + + +void +tz_info_free (TzInfo *tzinfo) +{ + g_return_if_fail (tzinfo != NULL); + + if (tzinfo->tzname) g_free (tzinfo->tzname); + g_free (tzinfo); +} + +struct { + const char *orig; + const char *dest; +} aliases[] = { + { "Asia/Istanbul", "Europe/Istanbul" }, /* Istanbul is in both Europe and Asia */ + { "Europe/Nicosia", "Asia/Nicosia" }, /* Ditto */ + { "EET", "Europe/Istanbul" }, /* Same tz as the 2 above */ + { "HST", "Pacific/Honolulu" }, + { "WET", "Europe/Brussels" }, /* Other name for the mainland Europe tz */ + { "CET", "Europe/Brussels" }, /* ditto */ + { "MET", "Europe/Brussels" }, + { "Etc/Zulu", "Etc/GMT" }, + { "Etc/UTC", "Etc/GMT" }, + { "GMT", "Etc/GMT" }, + { "Greenwich", "Etc/GMT" }, + { "Etc/UCT", "Etc/GMT" }, + { "Etc/GMT0", "Etc/GMT" }, + { "Etc/GMT+0", "Etc/GMT" }, + { "Etc/GMT-0", "Etc/GMT" }, + { "Etc/Universal", "Etc/GMT" }, + { "PST8PDT", "America/Los_Angeles" }, /* Other name for the Atlantic tz */ + { "EST", "America/New_York" }, /* Other name for the Eastern tz */ + { "EST5EDT", "America/New_York" }, /* ditto */ + { "CST6CDT", "America/Chicago" }, /* Other name for the Central tz */ + { "MST", "America/Denver" }, /* Other name for the mountain tz */ + { "MST7MDT", "America/Denver" }, /* ditto */ +}; + +static gboolean +compare_timezones (const char *a, + const char *b) +{ + if (g_str_equal (a, b)) + return TRUE; + if (strchr (b, '/') == NULL) { + g_autofree gchar *prefixed = NULL; + + prefixed = g_strdup_printf ("/%s", b); + if (g_str_has_suffix (a, prefixed)) + return TRUE; + } + + return FALSE; +} + +char * +tz_info_get_clean_name (TzDB *tz_db, + const char *tz) +{ + char *ret; + const char *timezone; + guint i; + gboolean replaced; + + /* Remove useless prefixes */ + if (g_str_has_prefix (tz, "right/")) + tz = tz + strlen ("right/"); + else if (g_str_has_prefix (tz, "posix/")) + tz = tz + strlen ("posix/"); + + /* Here start the crazies */ + replaced = FALSE; + + for (i = 0; i < G_N_ELEMENTS (aliases); i++) { + if (compare_timezones (tz, aliases[i].orig)) { + replaced = TRUE; + timezone = aliases[i].dest; + break; + } + } + + /* Try again! */ + if (!replaced) { + /* Ignore crazy solar times from the '80s */ + if (g_str_has_prefix (tz, "Asia/Riyadh") || + g_str_has_prefix (tz, "Mideast/Riyadh")) { + timezone = "Asia/Riyadh"; + replaced = TRUE; + } + } + + if (!replaced) + timezone = tz; + + ret = g_hash_table_lookup (tz_db->backward, timezone); + if (ret == NULL) + return g_strdup (timezone); + return g_strdup (ret); +} + +/* ----------------- * + * Private functions * + * ----------------- */ + +static gchar * +tz_data_file_get (void) +{ + gchar *file; + + file = g_strdup (TZ_DATA_FILE); + + return file; +} + +static float +convert_pos (gchar *pos, int digits) +{ + gchar whole[10]; + gchar *fraction; + gint i; + float t1, t2; + + if (!pos || strlen(pos) < 4 || digits > 9) return 0.0; + + for (i = 0; i < digits + 1; i++) whole[i] = pos[i]; + whole[i] = '\0'; + fraction = pos + digits + 1; + + t1 = g_strtod (whole, NULL); + t2 = g_strtod (fraction, NULL); + + if (t1 >= 0.0) return t1 + t2/pow (10.0, strlen(fraction)); + else return t1 - t2/pow (10.0, strlen(fraction)); +} + + +#if 0 + +/* Currently not working */ +static void +free_tzdata (TzLocation *tz) +{ + + if (tz->country) + g_free(tz->country); + if (tz->zone) + g_free(tz->zone); + if (tz->comment) + g_free(tz->comment); + + g_free(tz); +} +#endif + + +static int +compare_country_names (const void *a, const void *b) +{ + const TzLocation *tza = * (TzLocation **) a; + const TzLocation *tzb = * (TzLocation **) b; + + return strcmp (tza->zone, tzb->zone); +} + + +static void +sort_locations_by_country (GPtrArray *locations) +{ + qsort (locations->pdata, locations->len, sizeof (gpointer), + compare_country_names); +} + +static void +load_backward_tz (TzDB *tz_db) +{ + g_auto(GStrv) lines = NULL; + g_autoptr(GBytes) bytes = NULL; + const char *contents; + guint i; + + tz_db->backward = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + bytes = g_resources_lookup_data ("/org/gnome/control-center/datetime/backward", + G_RESOURCE_LOOKUP_FLAGS_NONE, NULL); + contents = (const char *) g_bytes_get_data (bytes, NULL); + + lines = g_strsplit (contents, "\n", -1); + + for (i = 0; lines[i] != NULL; i++) + { + g_auto(GStrv) items = NULL; + guint j; + char *real, *alias; + + if (g_ascii_strncasecmp (lines[i], "Link\t", 5) != 0) + continue; + + items = g_strsplit (lines[i], "\t", -1); + real = NULL; + alias = NULL; + /* Skip the "Link<tab>" part */ + for (j = 1; items[j] != NULL; j++) + { + if (items[j][0] == '\0') + continue; + if (real == NULL) + { + real = items[j]; + continue; + } + alias = items[j]; + break; + } + + if (real == NULL || alias == NULL) + g_warning ("Could not parse line: %s", lines[i]); + + /* We don't need more than one name for it */ + if (g_str_equal (real, "Etc/UTC") || + g_str_equal (real, "Etc/UCT")) + real = "Etc/GMT"; + + g_hash_table_insert (tz_db->backward, g_strdup (alias), g_strdup (real)); + } +} + diff --git a/gnome-initial-setup/pages/timezone/tz.h b/gnome-initial-setup/pages/timezone/tz.h new file mode 100644 index 0000000..feef165 --- /dev/null +++ b/gnome-initial-setup/pages/timezone/tz.h @@ -0,0 +1,90 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* Generic timezone utilities. + * + * Copyright (C) 2000-2001 Ximian, Inc. + * + * Authors: Hans Petter Jansson <hpj@ximian.com> + * + * Largely based on Michael Fulbright's work on Anaconda. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include <glib.h> + +G_BEGIN_DECLS + +#ifndef __sun +# define TZ_DATA_FILE "/usr/share/zoneinfo/zone.tab" +#else +# define TZ_DATA_FILE "/usr/share/lib/zoneinfo/tab/zone_sun.tab" +#endif + +typedef struct _TzDB TzDB; +typedef struct _TzLocation TzLocation; +typedef struct _TzInfo TzInfo; + + +struct _TzDB +{ + GPtrArray *locations; + GHashTable *backward; +}; + +struct _TzLocation +{ + gchar *country; + gdouble latitude; + gdouble longitude; + gchar *zone; + gchar *comment; + + gdouble dist; /* distance to clicked point for comparison */ +}; + +/* see the glibc info page information on time zone information */ +/* tzname is the default name for the timezone */ +/* utc_offset is offset in seconds from utc */ +/* daylight if non-zero then location obeys daylight savings */ + +struct _TzInfo +{ + gchar *tzname; + glong utc_offset; + gint daylight; +}; + + +TzDB *tz_load_db (void); +void tz_db_free (TzDB *db); +char * tz_info_get_clean_name (TzDB *tz_db, + const char *tz); +GPtrArray *tz_get_locations (TzDB *db); +void tz_location_get_position (TzLocation *loc, + double *longitude, double *latitude); +char *tz_location_get_country (TzLocation *loc); +gchar *tz_location_get_zone (TzLocation *loc); +gchar *tz_location_get_comment (TzLocation *loc); +glong tz_location_get_base_utc_offset (TzLocation *loc); +gint tz_location_set_locally (TzLocation *loc); +TzInfo *tz_info_from_location (TzLocation *loc); +void tz_info_free (TzInfo *tz_info); + + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (TzDB, tz_db_free) +G_DEFINE_AUTOPTR_CLEANUP_FUNC (TzInfo, tz_info_free) + +G_END_DECLS |