~mterry/indicator-datetime/update-after-resume

« back to all changes in this revision

Viewing changes to src/timezone-completion.c

  • Committer: Ted Gould
  • Date: 2011-04-06 21:52:08 UTC
  • mfrom: (101.1.2 indicator-datetime)
  • Revision ID: ted@gould.cx-20110406215208-y1531aa10oxuic49
Adds language, country and version information to the URL

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
 
51
51
#define TIMEZONE_COMPLETION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), TIMEZONE_COMPLETION_TYPE, TimezoneCompletionPrivate))
52
52
 
53
 
#define GEONAME_URL "http://geoname-lookup.ubuntu.com/?query=%s&release=%s"
 
53
#define GEONAME_URL "http://geoname-lookup.ubuntu.com/?query=%s&release=%s&lang=%s"
54
54
 
55
55
/* Prototypes */
56
56
static void timezone_completion_class_init (TimezoneCompletionClass *klass);
280
280
                                      json_parse_ready, user_data);
281
281
}
282
282
 
 
283
/* Returns message locale, with possible country info too like en_US */
 
284
static gchar *
 
285
get_locale (void)
 
286
{
 
287
  /* Check LANGUAGE, LC_ALL, LC_MESSAGES, and LANG, treat as colon-separated */
 
288
  const gchar *env_names[] = {"LANGUAGE", "LC_ALL", "LC_MESSAGES", "LANG", NULL};
 
289
  const gchar *env = NULL;
 
290
  gint i;
 
291
 
 
292
  for (i = 0; env_names[i]; i++) {
 
293
    env = g_getenv (env_names[i]);
 
294
    if (env != NULL && env[0] != 0)
 
295
      break;
 
296
  }
 
297
 
 
298
  if (env == NULL)
 
299
    return NULL;
 
300
 
 
301
  /* Now, we split on colons as expected, but also on . and @ to filter out
 
302
     extra pieces of locale we don't care about as we only use first chunk. */
 
303
  gchar **split = g_strsplit_set (env, ":.@", 2);
 
304
  if (split == NULL)
 
305
    return NULL;
 
306
 
 
307
  if (split[0] == NULL) {
 
308
    g_strfreev (split);
 
309
    return NULL;
 
310
  }
 
311
 
 
312
  gchar *locale = g_strdup (split[0]);
 
313
  g_strfreev (split);
 
314
  return locale;
 
315
}
 
316
 
 
317
static gchar *
 
318
get_version (void)
 
319
{
 
320
  static gchar *version = NULL;
 
321
 
 
322
  if (version == NULL) {
 
323
    gchar *stdout = NULL;
 
324
    g_spawn_command_line_sync ("lsb_release -rs", &stdout, NULL, NULL, NULL);
 
325
 
 
326
    if (stdout != NULL)
 
327
      version = g_strstrip (stdout);
 
328
    else
 
329
      version = g_strdup("");
 
330
  }
 
331
 
 
332
  return version;
 
333
}
 
334
 
283
335
static gboolean
284
336
request_zones (TimezoneCompletion * completion)
285
337
{
302
354
  priv->request_text = g_strdup (text);
303
355
 
304
356
  gchar * escaped = g_uri_escape_string (text, NULL, FALSE);
305
 
  gchar * url = g_strdup_printf (GEONAME_URL, escaped, "11.04"); // FIXME: don't hardcode
 
357
  gchar * version = get_version ();
 
358
  gchar * locale = get_locale ();
 
359
  gchar * url = g_strdup_printf (GEONAME_URL, escaped, version, locale);
 
360
  g_free (locale);
 
361
  g_free (version);
 
362
  g_free (escaped);
306
363
 
307
364
  GFile * file =  g_file_new_for_uri (url);
 
365
  g_free (url);
 
366
 
308
367
  g_file_read_async (file, G_PRIORITY_DEFAULT, priv->cancel,
309
368
                     geonames_data_ready, completion);
310
369