Commit 0fcfa3cc authored by Vincent Untz's avatar Vincent Untz

[clock] Make it possible to change the name of a location

For this, we need to make a difference between a city of a location, and
the name that is used for it. We save both in gconf (and we gracefully
handle migration from old configurations, of course).
parent cc886094
...@@ -544,7 +544,7 @@ clock_location_tile_refresh (ClockLocationTile *this, gboolean force_refresh) ...@@ -544,7 +544,7 @@ clock_location_tile_refresh (ClockLocationTile *this, gboolean force_refresh)
priv->last_offset = clock_location_get_offset (priv->location); priv->last_offset = clock_location_get_offset (priv->location);
tmp = g_strdup_printf ("<big><b>%s</b></big>", tmp = g_strdup_printf ("<big><b>%s</b></big>",
clock_location_get_name (priv->location)); clock_location_get_display_name (priv->location));
gtk_label_set_markup (GTK_LABEL (priv->city_label), tmp); gtk_label_set_markup (GTK_LABEL (priv->city_label), tmp);
g_free (tmp); g_free (tmp);
......
...@@ -31,6 +31,7 @@ G_DEFINE_TYPE (ClockLocation, clock_location, G_TYPE_OBJECT) ...@@ -31,6 +31,7 @@ G_DEFINE_TYPE (ClockLocation, clock_location, G_TYPE_OBJECT)
typedef struct { typedef struct {
gchar *name; gchar *name;
gchar *city;
SystemTimezone *systz; SystemTimezone *systz;
...@@ -76,6 +77,7 @@ static gchar *clock_location_get_valid_weather_code (const gchar *code); ...@@ -76,6 +77,7 @@ static gchar *clock_location_get_valid_weather_code (const gchar *code);
ClockLocation * ClockLocation *
clock_location_find_and_ref (GList *locations, clock_location_find_and_ref (GList *locations,
const gchar *name, const gchar *name,
const gchar *city,
const gchar *timezone, const gchar *timezone,
gfloat latitude, gfloat latitude,
gfloat longitude, gfloat longitude,
...@@ -91,6 +93,7 @@ clock_location_find_and_ref (GList *locations, ...@@ -91,6 +93,7 @@ clock_location_find_and_ref (GList *locations,
priv->longitude == longitude && priv->longitude == longitude &&
g_strcmp0 (priv->weather_code, code) == 0 && g_strcmp0 (priv->weather_code, code) == 0 &&
g_strcmp0 (priv->timezone, timezone) == 0 && g_strcmp0 (priv->timezone, timezone) == 0 &&
g_strcmp0 (priv->city, city) == 0 &&
g_strcmp0 (priv->name, name) == 0) g_strcmp0 (priv->name, name) == 0)
break; break;
} }
...@@ -102,7 +105,8 @@ clock_location_find_and_ref (GList *locations, ...@@ -102,7 +105,8 @@ clock_location_find_and_ref (GList *locations,
} }
ClockLocation * ClockLocation *
clock_location_new (const gchar *name, const gchar *timezone, clock_location_new (const gchar *name, const gchar *city,
const gchar *timezone,
gfloat latitude, gfloat longitude, gfloat latitude, gfloat longitude,
const gchar *code, WeatherPrefs *prefs) const gchar *code, WeatherPrefs *prefs)
{ {
...@@ -113,6 +117,7 @@ clock_location_new (const gchar *name, const gchar *timezone, ...@@ -113,6 +117,7 @@ clock_location_new (const gchar *name, const gchar *timezone,
priv = PRIVATE (this); priv = PRIVATE (this);
priv->name = g_strdup (name); priv->name = g_strdup (name);
priv->city = g_strdup (city);
priv->timezone = g_strdup (timezone); priv->timezone = g_strdup (timezone);
/* initialize priv->tzname */ /* initialize priv->tzname */
...@@ -170,6 +175,7 @@ clock_location_init (ClockLocation *this) ...@@ -170,6 +175,7 @@ clock_location_init (ClockLocation *this)
ClockLocationPrivate *priv = PRIVATE (this); ClockLocationPrivate *priv = PRIVATE (this);
priv->name = NULL; priv->name = NULL;
priv->city = NULL;
priv->systz = system_timezone_new (); priv->systz = system_timezone_new ();
...@@ -196,6 +202,11 @@ clock_location_finalize (GObject *g_obj) ...@@ -196,6 +202,11 @@ clock_location_finalize (GObject *g_obj)
priv->name = NULL; priv->name = NULL;
} }
if (priv->city) {
g_free (priv->city);
priv->city = NULL;
}
if (priv->systz) { if (priv->systz) {
g_object_unref (priv->systz); g_object_unref (priv->systz);
priv->systz = NULL; priv->systz = NULL;
...@@ -229,6 +240,17 @@ clock_location_finalize (GObject *g_obj) ...@@ -229,6 +240,17 @@ clock_location_finalize (GObject *g_obj)
G_OBJECT_CLASS (clock_location_parent_class)->finalize (g_obj); G_OBJECT_CLASS (clock_location_parent_class)->finalize (g_obj);
} }
const gchar *
clock_location_get_display_name (ClockLocation *loc)
{
ClockLocationPrivate *priv = PRIVATE (loc);
if (priv->name && priv->name[0])
return priv->name;
else
return priv->city;
}
const gchar * const gchar *
clock_location_get_name (ClockLocation *loc) clock_location_get_name (ClockLocation *loc)
{ {
...@@ -250,6 +272,27 @@ clock_location_set_name (ClockLocation *loc, const gchar *name) ...@@ -250,6 +272,27 @@ clock_location_set_name (ClockLocation *loc, const gchar *name)
priv->name = g_strdup (name); priv->name = g_strdup (name);
} }
const gchar *
clock_location_get_city (ClockLocation *loc)
{
ClockLocationPrivate *priv = PRIVATE (loc);
return priv->city;
}
void
clock_location_set_city (ClockLocation *loc, const gchar *city)
{
ClockLocationPrivate *priv = PRIVATE (loc);
if (priv->city) {
g_free (priv->city);
priv->city = NULL;
}
priv->city = g_strdup (city);
}
gchar * gchar *
clock_location_get_timezone (ClockLocation *loc) clock_location_get_timezone (ClockLocation *loc)
{ {
...@@ -823,7 +866,7 @@ setup_weather_updates (ClockLocation *loc) ...@@ -823,7 +866,7 @@ setup_weather_updates (ClockLocation *loc)
return; return;
dms = rad2dms (priv->latitude, priv->longitude); dms = rad2dms (priv->latitude, priv->longitude);
wl = weather_location_new (priv->name, priv->weather_code, wl = weather_location_new (priv->city, priv->weather_code,
NULL, NULL, dms, NULL, NULL); NULL, NULL, dms, NULL, NULL);
priv->weather_info = priv->weather_info =
......
...@@ -31,13 +31,15 @@ typedef struct ...@@ -31,13 +31,15 @@ typedef struct
GType clock_location_get_type (void); GType clock_location_get_type (void);
ClockLocation *clock_location_new (const gchar *name, const gchar *timezone, ClockLocation *clock_location_new (const gchar *name, const gchar *city,
const gchar *timezone,
gfloat latitude, gfloat longitude, gfloat latitude, gfloat longitude,
const gchar *code, const gchar *code,
WeatherPrefs *prefs); WeatherPrefs *prefs);
ClockLocation *clock_location_find_and_ref (GList *locations, ClockLocation *clock_location_find_and_ref (GList *locations,
const gchar *name, const gchar *name,
const gchar *city,
const gchar *timezone, const gchar *timezone,
gfloat latitude, gfloat latitude,
gfloat longitude, gfloat longitude,
...@@ -45,9 +47,14 @@ ClockLocation *clock_location_find_and_ref (GList *locations, ...@@ -45,9 +47,14 @@ ClockLocation *clock_location_find_and_ref (GList *locations,
gchar *clock_location_get_tzname (ClockLocation *loc); gchar *clock_location_get_tzname (ClockLocation *loc);
const gchar *clock_location_get_display_name (ClockLocation *loc);
const gchar *clock_location_get_name (ClockLocation *loc); const gchar *clock_location_get_name (ClockLocation *loc);
void clock_location_set_name (ClockLocation *loc, const gchar *name); void clock_location_set_name (ClockLocation *loc, const gchar *name);
const gchar *clock_location_get_city (ClockLocation *loc);
void clock_location_set_city (ClockLocation *loc, const gchar *city);
gchar *clock_location_get_timezone (ClockLocation *loc); gchar *clock_location_get_timezone (ClockLocation *loc);
void clock_location_set_timezone (ClockLocation *loc, const gchar *timezone); void clock_location_set_timezone (ClockLocation *loc, const gchar *timezone);
......
...@@ -1000,8 +1000,8 @@ sort_locations_by_name (gconstpointer a, gconstpointer b) ...@@ -1000,8 +1000,8 @@ sort_locations_by_name (gconstpointer a, gconstpointer b)
ClockLocation *loc_a = (ClockLocation *) a; ClockLocation *loc_a = (ClockLocation *) a;
ClockLocation *loc_b = (ClockLocation *) b; ClockLocation *loc_b = (ClockLocation *) b;
const char *name_a = clock_location_get_name (loc_a); const char *name_a = clock_location_get_display_name (loc_a);
const char *name_b = clock_location_get_name (loc_b); const char *name_b = clock_location_get_display_name (loc_b);
return strcmp (name_a, name_b); return strcmp (name_a, name_b);
} }
...@@ -1032,7 +1032,7 @@ create_cities_store (ClockData *cd) ...@@ -1032,7 +1032,7 @@ create_cities_store (ClockData *cd)
gtk_list_store_append (cd->cities_store, &iter); gtk_list_store_append (cd->cities_store, &iter);
gtk_list_store_set (cd->cities_store, &iter, gtk_list_store_set (cd->cities_store, &iter,
COL_CITY_NAME, clock_location_get_name (loc), COL_CITY_NAME, clock_location_get_display_name (loc),
/* FIXME: translate the timezone */ /* FIXME: translate the timezone */
COL_CITY_TZ, clock_location_get_timezone (loc), COL_CITY_TZ, clock_location_get_timezone (loc),
COL_CITY_LOC, loc, COL_CITY_LOC, loc,
...@@ -2108,6 +2108,7 @@ location_start_element (GMarkupParseContext *context, ...@@ -2108,6 +2108,7 @@ location_start_element (GMarkupParseContext *context,
const gchar *att_name; const gchar *att_name;
gchar *name = NULL; gchar *name = NULL;
gchar *city = NULL;
gchar *timezone = NULL; gchar *timezone = NULL;
gfloat latitude = 0.0; gfloat latitude = 0.0;
gfloat longitude = 0.0; gfloat longitude = 0.0;
...@@ -2129,6 +2130,8 @@ location_start_element (GMarkupParseContext *context, ...@@ -2129,6 +2130,8 @@ location_start_element (GMarkupParseContext *context,
att_name = attribute_names[++index]) { att_name = attribute_names[++index]) {
if (strcmp (att_name, "name") == 0) { if (strcmp (att_name, "name") == 0) {
name = (gchar *)attribute_values[index]; name = (gchar *)attribute_values[index];
} else if (strcmp (att_name, "city") == 0) {
city = (gchar *)attribute_values[index];
} else if (strcmp (att_name, "timezone") == 0) { } else if (strcmp (att_name, "timezone") == 0) {
timezone = (gchar *)attribute_values[index]; timezone = (gchar *)attribute_values[index];
} else if (strcmp (att_name, "latitude") == 0) { } else if (strcmp (att_name, "latitude") == 0) {
...@@ -2147,14 +2150,18 @@ location_start_element (GMarkupParseContext *context, ...@@ -2147,14 +2150,18 @@ location_start_element (GMarkupParseContext *context,
setlocale (LC_NUMERIC, ""); setlocale (LC_NUMERIC, "");
if (!name || !timezone) { if ((!name && !city) || !timezone) {
return; return;
} }
loc = clock_location_find_and_ref (cd->locations, name, timezone, /* migration from the old configuration, when name == city */
latitude, longitude, code); if (!city)
city = name;
loc = clock_location_find_and_ref (cd->locations, name, city,
timezone, latitude, longitude, code);
if (!loc) if (!loc)
loc = clock_location_new (name, timezone, loc = clock_location_new (name, city, timezone,
latitude, longitude, code, &prefs); latitude, longitude, code, &prefs);
if (current && clock_location_is_current_timezone (loc)) if (current && clock_location_is_current_timezone (loc))
...@@ -2857,16 +2864,20 @@ prefs_locations_changed (GtkTreeSelection *selection, ClockData *cd) ...@@ -2857,16 +2864,20 @@ prefs_locations_changed (GtkTreeSelection *selection, ClockData *cd)
static gchar * static gchar *
loc_to_string (ClockLocation *loc) loc_to_string (ClockLocation *loc)
{ {
const gchar *name, *city;
gfloat latitude, longitude; gfloat latitude, longitude;
gchar *ret; gchar *ret;
name = clock_location_get_name (loc);
city = clock_location_get_city (loc);
clock_location_get_coords (loc, &latitude, &longitude); clock_location_get_coords (loc, &latitude, &longitude);
setlocale (LC_NUMERIC, "POSIX"); setlocale (LC_NUMERIC, "POSIX");
ret = g_markup_printf_escaped ret = g_markup_printf_escaped
("<location name=\"%s\" timezone=\"%s\" latitude=\"%f\" longitude=\"%f\" code=\"%s\" current=\"%s\"/>", ("<location name=\"%s\" city=\"%s\" timezone=\"%s\" latitude=\"%f\" longitude=\"%f\" code=\"%s\" current=\"%s\"/>",
clock_location_get_name (loc), name ? name : "",
city ? city : "",
clock_location_get_timezone (loc), clock_location_get_timezone (loc),
latitude, longitude, latitude, longitude,
clock_location_get_weather_code (loc), clock_location_get_weather_code (loc),
...@@ -2919,7 +2930,7 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd) ...@@ -2919,7 +2930,7 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd)
GtkWidget *lon_combo = _clock_get_widget (cd, "edit-location-longitude-combo"); GtkWidget *lon_combo = _clock_get_widget (cd, "edit-location-longitude-combo");
const gchar *timezone, *weather_code; const gchar *timezone, *weather_code;
gchar *name; gchar *city, *name;
GWeatherLocation *gloc; GWeatherLocation *gloc;
gfloat lat = 0; gfloat lat = 0;
...@@ -2931,13 +2942,18 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd) ...@@ -2931,13 +2942,18 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd)
return; return;
} }
city = NULL;
weather_code = NULL;
name = NULL;
gloc = gweather_location_entry_get_location (cd->location_entry); gloc = gweather_location_entry_get_location (cd->location_entry);
if (gloc) { if (gloc) {
name = gweather_location_get_city_name (gloc); city = gweather_location_get_city_name (gloc);
weather_code = gweather_location_get_code (gloc); weather_code = gweather_location_get_code (gloc);
} else { }
if (gweather_location_entry_has_custom_text (cd->location_entry)) {
name = gtk_editable_get_chars (GTK_EDITABLE (cd->location_entry), 0, -1); name = gtk_editable_get_chars (GTK_EDITABLE (cd->location_entry), 0, -1);
weather_code = NULL;
} }
sscanf (gtk_entry_get_text (GTK_ENTRY (lat_entry)), "%f", &lat); sscanf (gtk_entry_get_text (GTK_ENTRY (lat_entry)), "%f", &lat);
...@@ -2954,6 +2970,7 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd) ...@@ -2954,6 +2970,7 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd)
if (loc) { if (loc) {
clock_location_set_timezone (loc, timezone); clock_location_set_timezone (loc, timezone);
clock_location_set_name (loc, name); clock_location_set_name (loc, name);
clock_location_set_city (loc, city);
clock_location_set_coords (loc, lat, lon); clock_location_set_coords (loc, lat, lon);
clock_location_set_weather_code (loc, weather_code); clock_location_set_weather_code (loc, weather_code);
} else { } else {
...@@ -2962,7 +2979,7 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd) ...@@ -2962,7 +2979,7 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd)
prefs.temperature_unit = cd->temperature_unit; prefs.temperature_unit = cd->temperature_unit;
prefs.speed_unit = cd->speed_unit; prefs.speed_unit = cd->speed_unit;
loc = clock_location_new (name, timezone, lat, lon, weather_code, &prefs); loc = clock_location_new (name, city, timezone, lat, lon, weather_code, &prefs);
/* has the side-effect of setting the current location if /* has the side-effect of setting the current location if
* there's none and this one can be considered as a current one * there's none and this one can be considered as a current one
*/ */
...@@ -2971,6 +2988,7 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd) ...@@ -2971,6 +2988,7 @@ run_prefs_edit_save (GtkButton *button, ClockData *cd)
cd->locations = g_list_append (cd->locations, loc); cd->locations = g_list_append (cd->locations, loc);
} }
g_free (name); g_free (name);
g_free (city);
/* This will update everything related to locations to take into /* This will update everything related to locations to take into
* account the new location (via the gconf notification) */ * account the new location (via the gconf notification) */
...@@ -3178,6 +3196,7 @@ edit_tree_row (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpoint ...@@ -3178,6 +3196,7 @@ edit_tree_row (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpoint
{ {
ClockData *cd = data; ClockData *cd = data;
ClockLocation *loc; ClockLocation *loc;
const char *name;
gchar *tmp; gchar *tmp;
gfloat lat, lon; gfloat lat, lon;
...@@ -3197,8 +3216,12 @@ edit_tree_row (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpoint ...@@ -3197,8 +3216,12 @@ edit_tree_row (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpoint
gtk_tree_model_get (model, iter, COL_CITY_LOC, &loc, -1); gtk_tree_model_get (model, iter, COL_CITY_LOC, &loc, -1);
gweather_location_entry_set_city (cd->location_entry, gweather_location_entry_set_city (cd->location_entry,
clock_location_get_name (loc), clock_location_get_city (loc),
clock_location_get_weather_code (loc)); clock_location_get_weather_code (loc));
name = clock_location_get_name (loc);
if (name && name[0]) {
gtk_entry_set_text (GTK_ENTRY (cd->location_entry), name);
}
clock_location_get_coords (loc, &lat, &lon); clock_location_get_coords (loc, &lat, &lon);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment