aboutsummaryrefslogtreecommitdiffstats
path: root/src/location-gnome-clock.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/location-gnome-clock.c')
-rw-r--r--src/location-gnome-clock.c135
1 files changed, 92 insertions, 43 deletions
diff --git a/src/location-gnome-clock.c b/src/location-gnome-clock.c
index 10b95eb..2bb1949 100644
--- a/src/location-gnome-clock.c
+++ b/src/location-gnome-clock.c
@@ -32,88 +32,137 @@
#endif
+/* Find current selected city for the clock applet with the specified id.
+ Returns NULL if not found. */
+static char *
+find_current_city(GConfClient *client, const char *id)
+{
+ char *current_city = NULL;
+ char *cities_key = g_strdup_printf("/apps/panel/applets/%s"
+ "/prefs/cities", id);
+ GSList *cities = gconf_client_get_list(client,
+ cities_key,
+ GCONF_VALUE_STRING, NULL);
+
+ if (cities == NULL) {
+ fprintf(stderr, _("Error reading city list: `%s'.\n"),
+ cities_key);
+ g_free(cities_key);
+ return NULL;
+ }
+
+ g_free(cities_key);
+
+ for (GSList *city = cities; city != NULL;
+ city = g_slist_next(city)) {
+ char *city_spec = city->data;
+ char *c = strstr(city_spec, "current=\"true\"");
+ if (c) current_city = g_strdup(city_spec);
+ g_free(city->data);
+ }
+ g_slist_free(cities);
+
+ return current_city;
+}
+
int
location_gnome_clock_init(location_gnome_clock_state_t *state)
{
g_type_init();
- GError *error = NULL;
GConfClient *client = gconf_client_get_default();
- GSList *applets = gconf_client_all_dirs(client, "/apps/panel/applets",
- &error);
- if (error) {
- fputs(_("Cannot list dirs in `/apps/panel/applets'.\n"),
- stderr);
+ /* Get a list of active applets in the panel. */
+ GSList *applets = gconf_client_get_list(client,
+ "/apps/panel/general/applet_id_list",
+ GCONF_VALUE_STRING, NULL);
+ if (applets == NULL) {
+ /* JDS has an alternate list of applets. */
+ applets = gconf_client_get_list(client,
+ "/apps/panel/general/applet_id_list_jds",
+ GCONF_VALUE_STRING, NULL);
+ }
+
+ if (applets == NULL) {
+ fputs(_("Cannot list GNOME panel applets.\n"), stderr);
+ g_slist_free(applets);
g_object_unref(client);
return -1;
}
- char *cities_key = NULL;
+ /* Go through each applet and check if it is a clock applet.
+ When a clock applet is found, check whether there is a
+ city selected as the current city. */
+ char *current_city = NULL;
+
+ /* Keep track of the number of clock applets found to be able to give
+ better error output if something fails. */
+ int clock_applet_count = 0;
+
for (GSList *applet = applets; applet != NULL;
applet = g_slist_next(applet)) {
- char *path = applet->data;
- if (cities_key == NULL) {
- char *key = g_strdup_printf("%s/bonobo_iid", path);
+ char *id = applet->data;
+ if (current_city == NULL) {
+ char *key = g_strdup_printf("/apps/panel/applets/%s"
+ "/bonobo_iid", id);
char *bonobo_iid = gconf_client_get_string(client, key,
- &error);
-
- if (!error && bonobo_iid != NULL) {
- if (!strcmp(bonobo_iid,
- "OAFIID:GNOME_ClockApplet")) {
- cities_key = g_strdup_printf(
- "%s/prefs/cities", path);
+ NULL);
+
+ /* Try both gnome-panel 2.30.x and earlier bonobo_iid
+ key and newer applet_iid. */
+ if (bonobo_iid != NULL &&
+ !strcmp(bonobo_iid, "OAFIID:GNOME_ClockApplet")) {
+ clock_applet_count += 1;
+ current_city = find_current_city(client, id);
+ } else {
+ g_free(key);
+ key = g_strdup_printf("/apps/panel/applets/%s"
+ "/applet_iid", id);
+ char *applet_iid = gconf_client_get_string(client, key,
+ NULL);
+
+ if (applet_iid != NULL &&
+ !strcmp(applet_iid, "ClockAppletFactory::ClockApplet")) {
+ clock_applet_count += 1;
+ current_city = find_current_city(client, id);
}
- g_free(bonobo_iid);
+
+ g_free(applet_iid);
}
+ g_free(bonobo_iid);
g_free(key);
}
- g_free(path);
+ g_free(id);
}
g_slist_free(applets);
+ g_object_unref(client);
- if (cities_key == NULL) {
- fputs(_("No clock applet was found.\n"), stderr);
- g_object_unref(client);
- return -1;
- }
+ /* Check whether an applet and a current city was found. */
- GSList *cities = gconf_client_get_list(client, cities_key,
- GCONF_VALUE_STRING, &error);
- if (error) {
- fprintf(stderr, _("Error reading city list: `%s'.\n"),
- cities_key);
- g_free(cities_key);
- g_object_unref(client);
+ if (clock_applet_count == 0) {
+ fputs(_("No clock applet was found.\n"), stderr);
return -1;
}
- g_free(cities_key);
-
- char *current_city = NULL;
- for (GSList *city = cities; city != NULL;
- city = g_slist_next(city)) {
- char *city_spec = city->data;
- char *c = strstr(city_spec, "current=\"true\"");
- if (c) current_city = g_strdup(city_spec);
- g_free(city->data);
- }
- g_slist_free(cities);
-
if (current_city == NULL) {
fputs(_("No city selected as current city.\n"), stderr);
return -1;
}
+ /* Find coords for selected city and parse as number. */
+
char *lat_str = strstr(current_city, "latitude=\"");
char *lon_str = strstr(current_city, "longitude=\"");
if (lat_str == NULL || lon_str == NULL) {
fputs(_("Location not specified for city.\n"), stderr);
+ g_free(current_city);
return -1;
}
+ g_free(current_city);
+
char *lat_num_str = lat_str + strlen("latitude=\"");
char *lon_num_str = lon_str + strlen("longitude=\"");