From 721adaf8332cf0a67843d8734e9e51a58894f8de Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Tue, 5 Nov 2013 11:31:07 +0000 Subject: [PATCH 01/10] Make whitespace and indentation consistent --- src/cc-timezone-map.c | 87 +++---- src/test-timezone.c | 85 +++---- src/timezone-completion.c | 568 +++++++++++++++++++++++++--------------------- src/tz.c | 236 +++++++++---------- 4 files changed, 519 insertions(+), 457 deletions(-) diff --git a/src/cc-timezone-map.c b/src/cc-timezone-map.c index 652f838..4d7fa40 100644 --- a/src/cc-timezone-map.c +++ b/src/cc-timezone-map.c @@ -974,40 +974,41 @@ get_loc_for_xy (GtkWidget * widget, gint x, gint y) width = alloc.width; height = alloc.height; - if (x == priv->previous_x && y == priv->previous_y) { - priv->distances = g_list_next (priv->distances); - location = (CcTimezoneLocation*) priv->distances->data; - } else { - g_list_free (priv->distances); - priv->distances = NULL; - for (i = 0; i < array->len; i++) - { - gdouble pointx, pointy, dx, dy; - CcTimezoneLocation *loc = array->pdata[i]; + if (x == priv->previous_x && y == priv->previous_y) + { + priv->distances = g_list_next (priv->distances); + location = (CcTimezoneLocation*) priv->distances->data; + } else { + g_list_free (priv->distances); + priv->distances = NULL; + for (i = 0; i < array->len; i++) + { + gdouble pointx, pointy, dx, dy; + CcTimezoneLocation *loc = array->pdata[i]; - g_object_get_property(G_OBJECT (loc), "longitude", &glon); - g_object_get_property(G_OBJECT (loc), "latitude", &glat); - pointx = convert_longtitude_to_x (g_value_get_double(&glon), width); - pointy = convert_latitude_to_y (g_value_get_double(&glat), height); + g_object_get_property(G_OBJECT (loc), "longitude", &glon); + g_object_get_property(G_OBJECT (loc), "latitude", &glat); + pointx = convert_longtitude_to_x (g_value_get_double(&glon), width); + pointy = convert_latitude_to_y (g_value_get_double(&glat), height); - dx = pointx - x; - dy = pointy - y; + dx = pointx - x; + dy = pointy - y; - g_value_set_double(&gdist, (gdouble) dx * dx + dy * dy); - g_object_set_property(G_OBJECT (loc), "dist", &gdist); - priv->distances = g_list_prepend (priv->distances, loc); - } - priv->distances = g_list_sort (priv->distances, (GCompareFunc) sort_locations); - location = (CcTimezoneLocation*) priv->distances->data; - priv->previous_x = x; - priv->previous_y = y; - } + g_value_set_double(&gdist, (gdouble) dx * dx + dy * dy); + g_object_set_property(G_OBJECT (loc), "dist", &gdist); + priv->distances = g_list_prepend (priv->distances, loc); + } + priv->distances = g_list_sort (priv->distances, (GCompareFunc) sort_locations); + location = (CcTimezoneLocation*) priv->distances->data; + priv->previous_x = x; + priv->previous_y = y; + } - g_value_unset (&glon); - g_value_unset (&glat); - g_value_unset (&gdist); + g_value_unset (&glon); + g_value_unset (&glat); + g_value_unset (&gdist); - return location; + return location; } static gboolean @@ -1199,20 +1200,20 @@ cc_timezone_map_get_timezone_at_coords (CcTimezoneMap *map, gdouble lon, gdouble if (zone < G_N_ELEMENTS(olsen_map_timezones)) city = olsen_map_timezones[zone]; - if (city != NULL) { - return city; - } - else { - GtkAllocation alloc; - GValue val_zone = {0}; - g_value_init (&val_zone, G_TYPE_STRING); - gtk_widget_get_allocation (GTK_WIDGET (map), &alloc); - x = convert_longtitude_to_x(lon, alloc.width); - y = convert_latitude_to_y(lat, alloc.height); - CcTimezoneLocation * loc = get_loc_for_xy(GTK_WIDGET (map), x, y); - g_value_unset (&val_zone); - return g_value_get_string(&val_zone); - } + if (city != NULL) + { + return city; + } else { + GtkAllocation alloc; + GValue val_zone = {0}; + g_value_init (&val_zone, G_TYPE_STRING); + gtk_widget_get_allocation (GTK_WIDGET (map), &alloc); + x = convert_longtitude_to_x(lon, alloc.width); + y = convert_latitude_to_y(lat, alloc.height); + CcTimezoneLocation * loc = get_loc_for_xy(GTK_WIDGET (map), x, y); + g_value_unset (&val_zone); + return g_value_get_string(&val_zone); + } } void diff --git a/src/test-timezone.c b/src/test-timezone.c index 9aaf707..a2271c4 100644 --- a/src/test-timezone.c +++ b/src/test-timezone.c @@ -5,58 +5,61 @@ int main (int argc, char **argv) { - TzDB *db; - GPtrArray *locs; - guint i; - char *pixmap_dir; - int retval = 0; + TzDB *db; + GPtrArray *locs; + guint i; + char *pixmap_dir; + int retval = 0; setlocale (LC_ALL, ""); - if (argc == 2) { - pixmap_dir = g_strdup (argv[1]); - } else if (argc == 1) { - pixmap_dir = g_strdup ("data/"); - } else { - g_message ("Usage: %s [PIXMAP DIRECTORY]", argv[0]); - return 1; - } + if (argc == 2) + { + pixmap_dir = g_strdup (argv[1]); + } else if (argc == 1) { + pixmap_dir = g_strdup ("data/"); + } else { + g_message ("Usage: %s [PIXMAP DIRECTORY]", argv[0]); + return 1; + } - g_type_init(); - GValue zone = {0}; - g_value_init(&zone, G_TYPE_STRING); + g_type_init(); + GValue zone = {0}; + g_value_init(&zone, G_TYPE_STRING); - db = tz_load_db (); - locs = tz_get_locations (db); - for (i = 0; i < locs->len ; i++) { - CcTimezoneLocation *loc = locs->pdata[i]; + db = tz_load_db (); + locs = tz_get_locations (db); + for (i = 0; i < locs->len ; i++) + { + CcTimezoneLocation *loc = locs->pdata[i]; - TzInfo *info; - char *filename, *path; - gdouble selected_offset; - char buf[16]; - g_object_get_property(G_OBJECT (loc), "zone", &zone); + TzInfo *info; + char *filename, *path; + gdouble selected_offset; + char buf[16]; + g_object_get_property(G_OBJECT (loc), "zone", &zone); - info = tz_info_from_location (loc); - selected_offset = tz_location_get_utc_offset (loc) - / (60.0*60.0) + ((info->daylight) ? -1.0 : 0.0); + info = tz_info_from_location (loc); + selected_offset = tz_location_get_utc_offset (loc) + / (60.0*60.0) + ((info->daylight) ? -1.0 : 0.0); - filename = g_strdup_printf ("timezone_%s.png", + filename = g_strdup_printf ("timezone_%s.png", g_ascii_formatd (buf, sizeof (buf), "%g", selected_offset)); - path = g_build_filename (pixmap_dir, filename, NULL); + path = g_build_filename (pixmap_dir, filename, NULL); - if (g_file_test (path, G_FILE_TEST_IS_REGULAR) == FALSE) { - g_message ("File '%s' missing for zone '%s'", filename, g_value_get_string(&zone)); - retval = 1; - } + if (g_file_test (path, G_FILE_TEST_IS_REGULAR) == FALSE) + { + g_message ("File '%s' missing for zone '%s'", filename, g_value_get_string(&zone)); + retval = 1; + } - g_free (filename); - g_free (path); - tz_info_free (info); - } - tz_db_free (db); - g_free (pixmap_dir); + g_free (filename); + g_free (path); + tz_info_free (info); + } + tz_db_free (db); + g_free (pixmap_dir); - return retval; + return retval; } diff --git a/src/timezone-completion.c b/src/timezone-completion.c index 3d42168..f6ca8c8 100644 --- a/src/timezone-completion.c +++ b/src/timezone-completion.c @@ -138,13 +138,14 @@ json_parse_ready (GObject *object, GAsyncResult *res, gpointer user_data) g_cancellable_reset (priv->cancel); } - if (error != NULL) { - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - save_and_use_model (completion, priv->initial_model); - g_warning ("Could not parse geoname JSON data: %s", error->message); - g_error_free (error); - return; - } + if (error != NULL) + { + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + save_and_use_model (completion, priv->initial_model); + g_warning ("Could not parse geoname JSON data: %s", error->message); + g_error_free (error); + return; + } GtkListStore * store = gtk_list_store_new (CC_TIMEZONE_COMPLETION_LAST, G_TYPE_STRING, @@ -156,73 +157,85 @@ json_parse_ready (GObject *object, GAsyncResult *res, gpointer user_data) JsonReader * reader = json_reader_new (json_parser_get_root (JSON_PARSER (object))); - if (!json_reader_is_array (reader)) { - g_warning ("Could not parse geoname JSON data"); - save_and_use_model (completion, priv->initial_model); - g_object_unref (G_OBJECT (reader)); - return; - } + if (!json_reader_is_array (reader)) + { + g_warning ("Could not parse geoname JSON data"); + save_and_use_model (completion, priv->initial_model); + g_object_unref (G_OBJECT (reader)); + return; + } gint i, count = json_reader_count_elements (reader); - for (i = 0; i < count; ++i) { - if (!json_reader_read_element (reader, i)) - continue; + for (i = 0; i < count; ++i) + { + if (!json_reader_read_element (reader, i)) + continue; - if (json_reader_is_object (reader)) { - const gchar * name = NULL; - const gchar * admin1 = NULL; - const gchar * country = NULL; - const gchar * longitude = NULL; - const gchar * latitude = NULL; - gboolean skip = FALSE; - if (json_reader_read_member (reader, "name")) { - name = json_reader_get_string_value (reader); - json_reader_end_member (reader); - } - if (json_reader_read_member (reader, "admin1")) { - admin1 = json_reader_get_string_value (reader); - json_reader_end_member (reader); - } - if (json_reader_read_member (reader, "country")) { - country = json_reader_get_string_value (reader); - json_reader_end_member (reader); - } - if (json_reader_read_member (reader, "longitude")) { - longitude = json_reader_get_string_value (reader); - json_reader_end_member (reader); - } - if (json_reader_read_member (reader, "latitude")) { - latitude = json_reader_get_string_value (reader); - json_reader_end_member (reader); - } + if (json_reader_is_object (reader)) + { + const gchar * name = NULL; + const gchar * admin1 = NULL; + const gchar * country = NULL; + const gchar * longitude = NULL; + const gchar * latitude = NULL; + gboolean skip = FALSE; + if (json_reader_read_member (reader, "name")) + { + name = json_reader_get_string_value (reader); + json_reader_end_member (reader); + } + if (json_reader_read_member (reader, "admin1")) + { + admin1 = json_reader_get_string_value (reader); + json_reader_end_member (reader); + } + if (json_reader_read_member (reader, "country")) + { + country = json_reader_get_string_value (reader); + json_reader_end_member (reader); + } + if (json_reader_read_member (reader, "longitude")) + { + longitude = json_reader_get_string_value (reader); + json_reader_end_member (reader); + } + if (json_reader_read_member (reader, "latitude")) + { + latitude = json_reader_get_string_value (reader); + json_reader_end_member (reader); + } if (g_strcmp0(name, prev_name) == 0 && g_strcmp0(admin1, prev_admin1) == 0 && - g_strcmp0(country, prev_country) == 0) { - // Sometimes the data will have duplicate entries that only differ - // in longitude and latitude. e.g. "rio de janeiro", "wellington" - skip = TRUE; - } + g_strcmp0(country, prev_country) == 0) + { + // Sometimes the data will have duplicate entries that only differ + // in longitude and latitude. e.g. "rio de janeiro", "wellington" + skip = TRUE; + } - if (!skip) { - GtkTreeIter iter; - gtk_list_store_append (store, &iter); - gtk_list_store_set (store, &iter, - CC_TIMEZONE_COMPLETION_ZONE, NULL, - CC_TIMEZONE_COMPLETION_NAME, name, - CC_TIMEZONE_COMPLETION_ADMIN1, admin1, - CC_TIMEZONE_COMPLETION_COUNTRY, country, - CC_TIMEZONE_COMPLETION_LONGITUDE, longitude, - CC_TIMEZONE_COMPLETION_LATITUDE, latitude, - -1); - gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store), - CC_TIMEZONE_COMPLETION_NAME, sort_zone, - g_utf8_casefold(priv->request_text, -1), - g_free); - gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), - CC_TIMEZONE_COMPLETION_NAME, - GTK_SORT_ASCENDING); - } + if (!skip) + { + GtkTreeIter iter; + gtk_list_store_append (store, &iter); + gtk_list_store_set (store, &iter, + CC_TIMEZONE_COMPLETION_ZONE, NULL, + CC_TIMEZONE_COMPLETION_NAME, name, + CC_TIMEZONE_COMPLETION_ADMIN1, admin1, + CC_TIMEZONE_COMPLETION_COUNTRY, country, + CC_TIMEZONE_COMPLETION_LONGITUDE, longitude, + CC_TIMEZONE_COMPLETION_LATITUDE, latitude, + -1); + gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store), + CC_TIMEZONE_COMPLETION_NAME, + sort_zone, + g_utf8_casefold(priv->request_text, + -1), + g_free); + gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), + CC_TIMEZONE_COMPLETION_NAME, + GTK_SORT_ASCENDING); + } prev_name = name; prev_admin1 = admin1; @@ -232,19 +245,21 @@ json_parse_ready (GObject *object, GAsyncResult *res, gpointer user_data) json_reader_end_element (reader); } - if (strlen (priv->request_text) < 4) { - gchar * lower_text = g_ascii_strdown (priv->request_text, -1); - if (g_strcmp0 (lower_text, "ut") == 0 || - g_strcmp0 (lower_text, "utc") == 0) { - GtkTreeIter iter; - gtk_list_store_append (store, &iter); - gtk_list_store_set (store, &iter, - CC_TIMEZONE_COMPLETION_ZONE, "UTC", - CC_TIMEZONE_COMPLETION_NAME, "UTC", - -1); + if (strlen (priv->request_text) < 4) + { + gchar * lower_text = g_ascii_strdown (priv->request_text, -1); + if (g_strcmp0 (lower_text, "ut") == 0 || + g_strcmp0 (lower_text, "utc") == 0) + { + GtkTreeIter iter; + gtk_list_store_append (store, &iter); + gtk_list_store_set (store, &iter, + CC_TIMEZONE_COMPLETION_ZONE, "UTC", + CC_TIMEZONE_COMPLETION_NAME, "UTC", + -1); + } + g_free (lower_text); } - g_free (lower_text); - } save_and_use_model (completion, GTK_TREE_MODEL (store)); g_object_unref (G_OBJECT (reader)); @@ -260,17 +275,20 @@ geonames_data_ready (GObject *object, GAsyncResult *res, gpointer user_data) stream = g_file_read_finish (G_FILE (object), res, &error); - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) && priv->cancel) { - g_cancellable_reset (priv->cancel); - } + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) && priv->cancel) + { + g_cancellable_reset (priv->cancel); + } - if (error != NULL) { - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - save_and_use_model (completion, priv->initial_model); - g_warning ("Could not connect to geoname lookup server: %s", error->message); - g_error_free (error); - return; - } + if (error != NULL) + { + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + save_and_use_model (completion, priv->initial_model); + g_warning ("Could not connect to geoname lookup server: %s", + error->message); + g_error_free (error); + return; + } JsonParser * parser = json_parser_new (); json_parser_load_from_stream_async (parser, G_INPUT_STREAM (stream), priv->cancel, @@ -286,11 +304,12 @@ get_locale (void) const gchar *env = NULL; gint i; - for (i = 0; env_names[i]; i++) { - env = g_getenv (env_names[i]); - if (env != NULL && env[0] != 0) - break; - } + for (i = 0; env_names[i]; i++) + { + env = g_getenv (env_names[i]); + if (env != NULL && env[0] != 0) + break; + } if (env == NULL) return NULL; @@ -301,10 +320,11 @@ get_locale (void) if (split == NULL) return NULL; - if (split[0] == NULL) { - g_strfreev (split); - return NULL; - } + if (split[0] == NULL) + { + g_strfreev (split); + return NULL; + } gchar *locale = g_strdup (split[0]); g_strfreev (split); @@ -316,15 +336,16 @@ get_version (void) { static gchar *version = NULL; - if (version == NULL) { - gchar *stdout = NULL; - g_spawn_command_line_sync ("lsb_release -rs", &stdout, NULL, NULL, NULL); + if (version == NULL) + { + gchar *stdout = NULL; + g_spawn_command_line_sync ("lsb_release -rs", &stdout, NULL, NULL, NULL); - if (stdout != NULL) - version = g_strstrip (stdout); - else - version = g_strdup(""); - } + if (stdout != NULL) + version = g_strstrip (stdout); + else + version = g_strdup(""); + } return version; } @@ -336,15 +357,17 @@ request_zones (CcTimezoneCompletion * completion) priv->queued_request = 0; - if (priv->entry == NULL) { - return FALSE; - } + if (priv->entry == NULL) + { + return FALSE; + } /* Cancel any ongoing request */ - if (priv->cancel) { - g_cancellable_cancel (priv->cancel); - g_cancellable_reset (priv->cancel); - } + if (priv->cancel) + { + g_cancellable_cancel (priv->cancel); + g_cancellable_reset (priv->cancel); + } g_free (priv->request_text); const gchar * text = gtk_entry_get_text (priv->entry); @@ -371,21 +394,26 @@ entry_changed (GtkEntry * entry, CcTimezoneCompletion * completion) { CcTimezoneCompletionPrivate * priv = completion->priv; - if (priv->queued_request) { - g_source_remove (priv->queued_request); - priv->queued_request = 0; - } + if (priv->queued_request) + { + g_source_remove (priv->queued_request); + priv->queued_request = 0; + } /* See if we've already got this one */ const gchar * text = gtk_entry_get_text (priv->entry); gpointer data; - if (g_hash_table_lookup_extended (priv->request_table, text, NULL, &data)) { - gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (completion), GTK_TREE_MODEL (data)); - } - else { - priv->queued_request = g_timeout_add (300, (GSourceFunc)request_zones, completion); - gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (completion), NULL); - } + if (g_hash_table_lookup_extended (priv->request_table, text, NULL, &data)) + { + gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (completion), + GTK_TREE_MODEL (data)); + } + else + { + priv->queued_request = g_timeout_add (300, (GSourceFunc)request_zones, + completion); + gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (completion), NULL); + } gtk_entry_completion_complete (GTK_ENTRY_COMPLETION (completion)); } @@ -395,18 +423,21 @@ get_descendent (GtkWidget * parent, GType type) if (g_type_is_a (G_OBJECT_TYPE (parent), type)) return parent; - if (GTK_IS_CONTAINER (parent)) { - GList * children = gtk_container_get_children (GTK_CONTAINER (parent)); - GList * iter; - for (iter = children; iter; iter = iter->next) { - GtkWidget * found = get_descendent (GTK_WIDGET (iter->data), type); - if (found) { - g_list_free (children); - return found; - } + if (GTK_IS_CONTAINER (parent)) + { + GList * children = gtk_container_get_children (GTK_CONTAINER (parent)); + GList * iter; + for (iter = children; iter; iter = iter->next) + { + GtkWidget * found = get_descendent (GTK_WIDGET (iter->data), type); + if (found) + { + g_list_free (children); + return found; + } + } + g_list_free (children); } - g_list_free (children); - } return NULL; } @@ -429,20 +460,26 @@ find_popup_treeview (GtkWidget * widget, GtkTreeModel * model) GtkWindowGroup * group = gtk_window_get_group (GTK_WINDOW (toplevel)); GList * windows = gtk_window_group_list_windows (group); GList * iter; - for (iter = windows; iter; iter = iter->next) { - if (iter->data == toplevel) - continue; // Skip our own window, we don't have it - GtkWidget * view = get_descendent (GTK_WIDGET (iter->data), GTK_TYPE_TREE_VIEW); - if (view != NULL) { - GtkTreeModel * tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (view)); - if (GTK_IS_TREE_MODEL_FILTER (tree_model)) - tree_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (tree_model)); - if (tree_model == model) { - g_list_free (windows); - return GTK_TREE_VIEW (view); - } + for (iter = windows; iter; iter = iter->next) + { + if (iter->data == toplevel) + continue; // Skip our own window, we don't have it + GtkWidget * view = get_descendent (GTK_WIDGET (iter->data), + GTK_TYPE_TREE_VIEW); + if (view != NULL) + { + GtkTreeModel * tree_model = + gtk_tree_view_get_model (GTK_TREE_VIEW (view)); + if (GTK_IS_TREE_MODEL_FILTER (tree_model)) + tree_model = gtk_tree_model_filter_get_model ( + GTK_TREE_MODEL_FILTER (tree_model)); + if (tree_model == model) + { + g_list_free (windows); + return GTK_TREE_VIEW (view); + } + } } - } g_list_free (windows); return NULL; @@ -453,26 +490,30 @@ entry_keypress (GtkEntry * entry, GdkEventKey *event, CcTimezoneCompletion * co { if (event->keyval == GDK_KEY_ISO_Enter || event->keyval == GDK_KEY_KP_Enter || - event->keyval == GDK_KEY_Return) { - /* Make sure that user has a selection to choose, otherwise ignore */ - GtkTreeModel * model = gtk_entry_completion_get_model (GTK_ENTRY_COMPLETION (completion)); - GtkTreeView * view = find_popup_treeview (GTK_WIDGET (entry), model); - if (view == NULL) { - // Just beep if popup hasn't appeared yet. - gtk_widget_error_bell (GTK_WIDGET (entry)); - return TRUE; - } + event->keyval == GDK_KEY_Return) + { + /* Make sure that user has a selection to choose, otherwise ignore */ + GtkTreeModel * model = gtk_entry_completion_get_model ( + GTK_ENTRY_COMPLETION (completion)); + GtkTreeView * view = find_popup_treeview (GTK_WIDGET (entry), model); + if (view == NULL) + { + // Just beep if popup hasn't appeared yet. + gtk_widget_error_bell (GTK_WIDGET (entry)); + return TRUE; + } - GtkTreeSelection * sel = gtk_tree_view_get_selection (view); - GtkTreeModel * sel_model = NULL; - if (!gtk_tree_selection_get_selected (sel, &sel_model, NULL)) { - // No selection, we should help them out and select first item in list - GtkTreeIter iter; - if (gtk_tree_model_get_iter_first (sel_model, &iter)) - gtk_tree_selection_select_iter (sel, &iter); - // And fall through to normal handler code + GtkTreeSelection * sel = gtk_tree_view_get_selection (view); + GtkTreeModel * sel_model = NULL; + if (!gtk_tree_selection_get_selected (sel, &sel_model, NULL)) + { + // No selection, we should help them out and select first item in list + GtkTreeIter iter; + if (gtk_tree_model_get_iter_first (sel_model, &iter)) + gtk_tree_selection_select_iter (sel, &iter); + // And fall through to normal handler code + } } - } return FALSE; } @@ -482,32 +523,37 @@ cc_timezone_completion_watch_entry (CcTimezoneCompletion * completion, GtkEntry { CcTimezoneCompletionPrivate * priv = completion->priv; - if (priv->queued_request) { - g_source_remove (priv->queued_request); - priv->queued_request = 0; - } - if (priv->entry) { - g_signal_handler_disconnect (priv->entry, priv->changed_id); - priv->changed_id = 0; - g_signal_handler_disconnect (priv->entry, priv->keypress_id); - priv->keypress_id = 0; - g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry); - gtk_entry_set_completion (priv->entry, NULL); - } + if (priv->queued_request) + { + g_source_remove (priv->queued_request); + priv->queued_request = 0; + } + if (priv->entry) + { + g_signal_handler_disconnect (priv->entry, priv->changed_id); + priv->changed_id = 0; + g_signal_handler_disconnect (priv->entry, priv->keypress_id); + priv->keypress_id = 0; + g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry); + gtk_entry_set_completion (priv->entry, NULL); + } priv->entry = entry; - if (entry) { - guint id = g_signal_connect (entry, "changed", G_CALLBACK (entry_changed), completion); - priv->changed_id = id; + if (entry) + { + guint id = g_signal_connect (entry, "changed", + G_CALLBACK (entry_changed), completion); + priv->changed_id = id; - id = g_signal_connect (entry, "key-press-event", G_CALLBACK (entry_keypress), completion); - priv->keypress_id = id; + id = g_signal_connect (entry, "key-press-event", + G_CALLBACK (entry_keypress), completion); + priv->keypress_id = id; - g_object_add_weak_pointer (G_OBJECT (entry), (gpointer *)&priv->entry); + g_object_add_weak_pointer (G_OBJECT (entry), (gpointer *)&priv->entry); - gtk_entry_set_completion (entry, GTK_ENTRY_COMPLETION (completion)); - } + gtk_entry_set_completion (entry, GTK_ENTRY_COMPLETION (completion)); + } } static GtkListStore * @@ -525,37 +571,38 @@ get_initial_model (void) G_TYPE_STRING); gint i; - for (i = 0; i < locations->len; ++i) { - CcTimezoneLocation * loc = g_ptr_array_index (locations, i); - GtkTreeIter iter; - gtk_list_store_append (store, &iter); + for (i = 0; i < locations->len; ++i) + { + CcTimezoneLocation * loc = g_ptr_array_index (locations, i); + GtkTreeIter iter; + gtk_list_store_append (store, &iter); - gchar * zone; - gchar * country; - gchar * en_name; // FIXME: need something better for non-English locales - gdouble longitude; - gdouble latitude; - g_object_get (loc, "zone", &zone, "country", &country, "en_name", &en_name, - "longitude", &longitude, "latitude", &latitude, - NULL); + gchar * zone; + gchar * country; + gchar * en_name; // FIXME: need something better for non-English locales + gdouble longitude; + gdouble latitude; + g_object_get (loc, "zone", &zone, "country", &country, "en_name", &en_name, + "longitude", &longitude, "latitude", &latitude, + NULL); - gchar * longitude_s = g_strdup_printf ("%f", longitude); - gchar * latitude_s= g_strdup_printf ("%f", latitude); + gchar * longitude_s = g_strdup_printf ("%f", longitude); + gchar * latitude_s= g_strdup_printf ("%f", latitude); - gtk_list_store_set (store, &iter, - CC_TIMEZONE_COMPLETION_ZONE, NULL, - CC_TIMEZONE_COMPLETION_NAME, en_name, - CC_TIMEZONE_COMPLETION_COUNTRY, country, - CC_TIMEZONE_COMPLETION_LONGITUDE, longitude_s, - CC_TIMEZONE_COMPLETION_LATITUDE, latitude_s, - -1); + gtk_list_store_set (store, &iter, + CC_TIMEZONE_COMPLETION_ZONE, NULL, + CC_TIMEZONE_COMPLETION_NAME, en_name, + CC_TIMEZONE_COMPLETION_COUNTRY, country, + CC_TIMEZONE_COMPLETION_LONGITUDE, longitude_s, + CC_TIMEZONE_COMPLETION_LATITUDE, latitude_s, + -1); - g_free (latitude_s); - g_free (longitude_s); - g_free (en_name); - g_free (country); - g_free (zone); - } + g_free (latitude_s); + g_free (longitude_s); + g_free (en_name); + g_free (country); + g_free (zone); + } GtkTreeIter iter; gtk_list_store_append (store, &iter); @@ -581,13 +628,14 @@ data_func (GtkCellLayout *cell_layout, GtkCellRenderer *cell, -1); gchar * user_name; - if (country == NULL || country[0] == 0) { - user_name = g_strdup (name); - } else if (admin1 == NULL || admin1[0] == 0) { - user_name = g_strdup_printf ("%s (%s)", name, country); - } else { - user_name = g_strdup_printf ("%s (%s, %s)", name, admin1, country); - } + if (country == NULL || country[0] == 0) + { + user_name = g_strdup (name); + } else if (admin1 == NULL || admin1[0] == 0) { + user_name = g_strdup_printf ("%s (%s)", name, country); + } else { + user_name = g_strdup_printf ("%s (%s, %s)", name, admin1, country); + } g_object_set (G_OBJECT (cell), "markup", user_name, NULL); } @@ -641,49 +689,57 @@ cc_timezone_completion_dispose (GObject * object) CcTimezoneCompletion * completion = CC_TIMEZONE_COMPLETION (object); CcTimezoneCompletionPrivate * priv = completion->priv; - if (priv->changed_id) { - if (priv->entry) - g_signal_handler_disconnect (priv->entry, priv->changed_id); - priv->changed_id = 0; - } + if (priv->changed_id) + { + if (priv->entry) + g_signal_handler_disconnect (priv->entry, priv->changed_id); + priv->changed_id = 0; + } - if (priv->keypress_id) { - if (priv->entry) - g_signal_handler_disconnect (priv->entry, priv->keypress_id); - priv->keypress_id = 0; - } + if (priv->keypress_id) + { + if (priv->entry) + g_signal_handler_disconnect (priv->entry, priv->keypress_id); + priv->keypress_id = 0; + } - if (priv->entry != NULL) { - gtk_entry_set_completion (priv->entry, NULL); - g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry); - priv->entry = NULL; - } + if (priv->entry != NULL) + { + gtk_entry_set_completion (priv->entry, NULL); + g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry); + priv->entry = NULL; + } - if (priv->initial_model != NULL) { - g_object_unref (G_OBJECT (priv->initial_model)); - priv->initial_model = NULL; - } + if (priv->initial_model != NULL) + { + g_object_unref (G_OBJECT (priv->initial_model)); + priv->initial_model = NULL; + } - if (priv->queued_request) { - g_source_remove (priv->queued_request); - priv->queued_request = 0; - } + if (priv->queued_request) + { + g_source_remove (priv->queued_request); + priv->queued_request = 0; + } - if (priv->cancel != NULL) { - g_cancellable_cancel (priv->cancel); - g_object_unref (priv->cancel); - priv->cancel = NULL; - } + if (priv->cancel != NULL) + { + g_cancellable_cancel (priv->cancel); + g_object_unref (priv->cancel); + priv->cancel = NULL; + } - if (priv->request_text != NULL) { - g_free (priv->request_text); - priv->request_text = NULL; - } + if (priv->request_text != NULL) + { + g_free (priv->request_text); + priv->request_text = NULL; + } - if (priv->request_table != NULL) { - g_hash_table_destroy (priv->request_table); - priv->request_table = NULL; - } + if (priv->request_table != NULL) + { + g_hash_table_destroy (priv->request_table); + priv->request_table = NULL; + } return; } diff --git a/src/tz.c b/src/tz.c index 2aa10ca..969119a 100644 --- a/src/tz.c +++ b/src/tz.c @@ -50,16 +50,16 @@ G_DEFINE_TYPE (CcTimezoneLocation, cc_timezone_location, G_TYPE_OBJECT) struct _CcTimezoneLocationPrivate { - gchar *country; - gchar *full_country; - gchar *en_name; - gchar *state; - gdouble latitude; - gdouble longitude; - gchar *zone; - gchar *comment; + gchar *country; + gchar *full_country; + gchar *en_name; + gchar *state; + gdouble latitude; + gdouble longitude; + gchar *zone; + gchar *comment; - gdouble dist; /* distance to clicked point for comparison */ + gdouble dist; /* distance to clicked point for comparison */ }; enum { @@ -82,8 +82,7 @@ cc_timezone_location_get_property (GObject *object, GParamSpec *pspec) { CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; - switch (property_id) - { + switch (property_id) { case PROP_COUNTRY: g_value_set_string (value, priv->country); break; @@ -123,8 +122,7 @@ cc_timezone_location_set_property (GObject *object, GParamSpec *pspec) { CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; - switch (property_id) - { + switch (property_id) { case PROP_COUNTRY: priv->country = g_value_get_string(value); break; @@ -162,31 +160,31 @@ cc_timezone_location_dispose (GObject *object) { CcTimezoneLocationPrivate *priv = CC_TIMEZONE_LOCATION (object)->priv; - if (priv->country) + if (priv->country) { g_free (priv->country); priv->country = NULL; } - if (priv->full_country) + if (priv->full_country) { g_free (priv->full_country); priv->full_country = NULL; } - if (priv->state) + if (priv->state) { g_free (priv->state); priv->state = NULL; } - if (priv->zone) + if (priv->zone) { g_free (priv->zone); priv->zone = NULL; } - if (priv->comment) + if (priv->comment) { g_free (priv->comment); priv->comment = NULL; @@ -304,19 +302,20 @@ void parse_file (const char * filename, FILE *fh = fopen (filename, "r"); char buf[4096]; - if (!fh) { + if (!fh) + { g_warning ("Could not open *%s*\n", filename); fclose (fh); return; - } + } - while (fgets (buf, sizeof(buf), fh)) - { + while (fgets (buf, sizeof(buf), fh)) + { if (*buf == '#') continue; g_strchomp (buf); func (g_strsplit (buf,"\t", ncolumns), user_data); - } + } fclose (fh); } @@ -438,22 +437,25 @@ tz_load_db (void) char buf[4096]; tz_data_file = tz_data_file_get ("TZ_DATA_FILE", TZ_DATA_FILE); - if (!tz_data_file) { + if (!tz_data_file) + { g_warning ("Could not get the TimeZone data file name"); return NULL; - } + } admin1_file = tz_data_file_get ("ADMIN1_FILE", ADMIN1_FILE); - if (!admin1_file) { + if (!admin1_file) + { g_warning ("Could not get the admin1 data file name"); return NULL; - } + } country_file = tz_data_file_get ("COUNTRY_FILE", COUNTRY_FILE); - if (!country_file) { + if (!country_file) + { g_warning ("Could not get the country data file name"); return NULL; - } + } GHashTable *stateHash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); @@ -491,9 +493,9 @@ tz_load_db (void) void tz_db_free (TzDB *db) { - g_ptr_array_foreach (db->locations, (GFunc) g_object_unref, NULL); - g_ptr_array_free (db->locations, TRUE); - g_free (db); + g_ptr_array_foreach (db->locations, (GFunc) g_object_unref, NULL); + g_ptr_array_free (db->locations, TRUE); + g_free (db); } static gint @@ -548,103 +550,103 @@ convert_latitude_to_y (gdouble latitude, gdouble map_height) GPtrArray * tz_get_locations (TzDB *db) { - return db->locations; + return db->locations; } -glong + glong tz_location_get_utc_offset (CcTimezoneLocation *loc) { - TzInfo *tz_info; - glong offset; + TzInfo *tz_info; + glong offset; - tz_info = tz_info_from_location (loc); - offset = tz_info->utc_offset; - tz_info_free (tz_info); - return offset; + tz_info = tz_info_from_location (loc); + offset = tz_info->utc_offset; + tz_info_free (tz_info); + return offset; } gint tz_location_set_locally (CcTimezoneLocation *loc) { - time_t curtime; - struct tm *curzone; - gboolean is_dst = FALSE; - gint correction = 0; + time_t curtime; + struct tm *curzone; + gboolean is_dst = FALSE; + gint correction = 0; - g_return_val_if_fail (loc != NULL, 0); - g_return_val_if_fail (loc->priv->zone != NULL, 0); - - curtime = time (NULL); - curzone = localtime (&curtime); - is_dst = curzone->tm_isdst; + g_return_val_if_fail (loc != NULL, 0); + g_return_val_if_fail (loc->priv->zone != NULL, 0); - setenv ("TZ", loc->priv->zone, 1); + curtime = time (NULL); + curzone = localtime (&curtime); + is_dst = curzone->tm_isdst; + + setenv ("TZ", loc->priv->zone, 1); #if 0 - curtime = time (NULL); - curzone = localtime (&curtime); + curtime = time (NULL); + curzone = localtime (&curtime); - if (!is_dst && curzone->tm_isdst) { - correction = (60 * 60); - } - else if (is_dst && !curzone->tm_isdst) { - correction = 0; - } + if (!is_dst && curzone->tm_isdst) { + correction = (60 * 60); + } + else if (is_dst && !curzone->tm_isdst) { + correction = 0; + } #endif - return correction; + return correction; } -TzInfo * + TzInfo * tz_info_from_location (CcTimezoneLocation *loc) { - TzInfo *tzinfo; - time_t curtime; - struct tm *curzone; - - g_return_val_if_fail (loc != NULL, NULL); - g_return_val_if_fail (loc->priv->zone != NULL, NULL); - - setenv ("TZ", loc->priv->zone, 1); - + TzInfo *tzinfo; + time_t curtime; + struct tm *curzone; + + g_return_val_if_fail (loc != NULL, NULL); + g_return_val_if_fail (loc->priv->zone != NULL, NULL); + + setenv ("TZ", loc->priv->zone, 1); + #if 0 - tzset (); + tzset (); #endif - tzinfo = g_new0 (TzInfo, 1); + tzinfo = g_new0 (TzInfo, 1); - curtime = time (NULL); - curzone = localtime (&curtime); + curtime = time (NULL); + curzone = localtime (&curtime); #ifndef __sun - /* Currently this solution doesnt seem to work - I get that */ - /* America/Phoenix uses daylight savings, which is wrong */ - tzinfo->tzname_normal = g_strdup (curzone->tm_zone); - if (curzone->tm_isdst) - tzinfo->tzname_daylight = - g_strdup (&curzone->tm_zone[curzone->tm_isdst]); - else - tzinfo->tzname_daylight = NULL; + /* Currently this solution doesnt seem to work - I get that */ + /* America/Phoenix uses daylight savings, which is wrong */ + tzinfo->tzname_normal = g_strdup (curzone->tm_zone); + if (curzone->tm_isdst) + tzinfo->tzname_daylight = + g_strdup (&curzone->tm_zone[curzone->tm_isdst]); + else + tzinfo->tzname_daylight = NULL; - tzinfo->utc_offset = curzone->tm_gmtoff; + tzinfo->utc_offset = curzone->tm_gmtoff; #else - tzinfo->tzname_normal = NULL; - tzinfo->tzname_daylight = NULL; - tzinfo->utc_offset = 0; + tzinfo->tzname_normal = NULL; + tzinfo->tzname_daylight = NULL; + tzinfo->utc_offset = 0; #endif - tzinfo->daylight = curzone->tm_isdst; - - return tzinfo; + tzinfo->daylight = curzone->tm_isdst; + + return tzinfo; } -void + void tz_info_free (TzInfo *tzinfo) { - g_return_if_fail (tzinfo != NULL); - - if (tzinfo->tzname_normal) g_free (tzinfo->tzname_normal); - if (tzinfo->tzname_daylight) g_free (tzinfo->tzname_daylight); - g_free (tzinfo); + g_return_if_fail (tzinfo != NULL); + + if (tzinfo->tzname_normal) g_free (tzinfo->tzname_normal); + if (tzinfo->tzname_daylight) g_free (tzinfo->tzname_daylight); + g_free (tzinfo); } /* ----------------- * @@ -665,38 +667,38 @@ tz_data_file_get (gchar *env, gchar *defaultfile) 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; + gchar whole[10]; + gchar *fraction; + gint i; + float t1, t2; - t1 = g_strtod (whole, NULL); - t2 = g_strtod (fraction, NULL); + if (!pos || strlen(pos) < 4 || digits > 9) return 0.0; - if (t1 >= 0.0) return t1 + t2/pow (10.0, strlen(fraction)); - else return t1 - t2/pow (10.0, strlen(fraction)); + 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)); } #endif -static int + static int compare_country_names (const void *a, const void *b) { - const CcTimezoneLocation *tza = * (CcTimezoneLocation **) a; - const CcTimezoneLocation *tzb = * (CcTimezoneLocation **) b; - - return strcmp (tza->priv->zone, tzb->priv->zone); + const CcTimezoneLocation *tza = * (CcTimezoneLocation **) a; + const CcTimezoneLocation *tzb = * (CcTimezoneLocation **) b; + + return strcmp (tza->priv->zone, tzb->priv->zone); } -static void + static void sort_locations_by_country (GPtrArray *locations) { - qsort (locations->pdata, locations->len, sizeof (gpointer), - compare_country_names); + qsort (locations->pdata, locations->len, sizeof (gpointer), + compare_country_names); } -- 1.8.4.2