Merge lp:~charlesk/indicator-session/lp-1331873-use-etc-release-for-distro-name into lp:indicator-session/14.10

Proposed by Charles Kerr
Status: Merged
Approved by: Ted Gould
Approved revision: 455
Merged at revision: 449
Proposed branch: lp:~charlesk/indicator-session/lp-1331873-use-etc-release-for-distro-name
Merge into: lp:indicator-session/14.10
Diff against target: 95 lines (+78/-2)
1 file modified
src/service.c (+78/-2)
To merge this branch: bzr merge lp:~charlesk/indicator-session/lp-1331873-use-etc-release-for-distro-name
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Ted Gould (community) Approve
Review via email: mp+226934@code.launchpad.net

Commit message

Add support for getting the distro name from /etc/os-release.

Description of the change

Add support for getting the distro name from /etc/os-release.

get_os_release() returns a string-to-string GHashTable of the parsed file with unescaped value strings.

We only need one key out of this file, so a GHashTable is admittedly a bit of overkill, but separating the parser code from the use code like this makes the code more readable and makes the parser code self-contained.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ted Gould (ted) :
review: Needs Fixing
450. By Charles Kerr

don't mark 'Ubuntu' for translation.

451. By Charles Kerr

better i18n markup of the help string

452. By Charles Kerr

make the os-release parser slightly less unreadable.

453. By Charles Kerr

make the os-release parser slightly more readable.

Revision history for this message
Charles Kerr (charlesk) :
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
454. By Charles Kerr

tweak unescape comment for readability

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
455. By Charles Kerr

use g_shell_unquote() to unmunge the values in /etc/os-release

Revision history for this message
Ted Gould (ted) wrote :

Great!

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/service.c'
--- src/service.c 2014-04-02 15:02:59 +0000
+++ src/service.c 2014-07-16 16:32:32 +0000
@@ -329,14 +329,90 @@
329****329****
330***/330***/
331331
332static GHashTable*
333get_os_release (void)
334{
335 static const char * const os_release = "/etc/os-release";
336 GHashTable * hash;
337 GIOChannel * io;
338
339 hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
340
341 if ((io = g_io_channel_new_file (os_release, "r", NULL)))
342 {
343 GString * key = g_string_new (NULL);
344
345 for (;;)
346 {
347 GIOStatus status;
348 char * in;
349 GError * error;
350 gchar * val;
351
352 /* read a line */
353 status = g_io_channel_read_line_string (io, key, NULL, NULL);
354 if (status == G_IO_STATUS_EOF)
355 break;
356
357 /* ignore blank lines & comments */
358 if (!key->len || key->str[0]=='#')
359 continue;
360
361 /* split into key=value */
362 in = strchr(key->str, '=');
363 if (!in)
364 continue;
365 *in++ = '\0';
366
367 /* unmunge the value component */
368 g_strstrip(in); /* eat linefeed */
369 error = NULL;
370 val = g_shell_unquote (in, &error);
371 if (error != NULL)
372 {
373 g_warning("Unable to unquote \"%s\": %s", in, error->message);
374 val = g_strdup(in);
375 g_clear_error(&error);
376 }
377
378 g_debug("from \"%s\": key [%s] val [%s]", os_release, key->str, val);
379 g_hash_table_insert (hash, g_strdup(key->str), val); /* hash owns val now */
380 }
381
382 g_string_free(key, TRUE);
383 g_io_channel_unref(io);
384 }
385
386 return hash;
387}
388
389static const char*
390get_distro_name (void)
391{
392 static char * distro_name = NULL;
393
394 if (distro_name == NULL)
395 {
396 GHashTable * os_release = get_os_release();
397 gpointer value = g_hash_table_lookup(os_release, "NAME");
398 if (value == NULL)
399 value = "Ubuntu"; /* fallback value */
400 distro_name = g_strdup(value);
401 g_hash_table_destroy(os_release);
402 }
403
404 return distro_name;
405}
406
332static GMenuModel *407static GMenuModel *
333create_admin_section (void)408create_admin_section (void)
334{409{
335 GMenu * menu;410 GMenu * menu;
336411 gchar * help_label = g_strdup_printf(_("%s Help"), get_distro_name());
337 menu = g_menu_new ();412 menu = g_menu_new ();
338 g_menu_append (menu, _("About This Computer"), "indicator.about");413 g_menu_append (menu, _("About This Computer"), "indicator.about");
339 g_menu_append (menu, _("Ubuntu Help"), "indicator.help");414 g_menu_append (menu, help_label, "indicator.help");
415 g_free (help_label);
340 return G_MENU_MODEL (menu);416 return G_MENU_MODEL (menu);
341}417}
342418

Subscribers

People subscribed via source and target branches