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
1=== modified file 'src/service.c'
2--- src/service.c 2014-04-02 15:02:59 +0000
3+++ src/service.c 2014-07-16 16:32:32 +0000
4@@ -329,14 +329,90 @@
5 ****
6 ***/
7
8+static GHashTable*
9+get_os_release (void)
10+{
11+ static const char * const os_release = "/etc/os-release";
12+ GHashTable * hash;
13+ GIOChannel * io;
14+
15+ hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
16+
17+ if ((io = g_io_channel_new_file (os_release, "r", NULL)))
18+ {
19+ GString * key = g_string_new (NULL);
20+
21+ for (;;)
22+ {
23+ GIOStatus status;
24+ char * in;
25+ GError * error;
26+ gchar * val;
27+
28+ /* read a line */
29+ status = g_io_channel_read_line_string (io, key, NULL, NULL);
30+ if (status == G_IO_STATUS_EOF)
31+ break;
32+
33+ /* ignore blank lines & comments */
34+ if (!key->len || key->str[0]=='#')
35+ continue;
36+
37+ /* split into key=value */
38+ in = strchr(key->str, '=');
39+ if (!in)
40+ continue;
41+ *in++ = '\0';
42+
43+ /* unmunge the value component */
44+ g_strstrip(in); /* eat linefeed */
45+ error = NULL;
46+ val = g_shell_unquote (in, &error);
47+ if (error != NULL)
48+ {
49+ g_warning("Unable to unquote \"%s\": %s", in, error->message);
50+ val = g_strdup(in);
51+ g_clear_error(&error);
52+ }
53+
54+ g_debug("from \"%s\": key [%s] val [%s]", os_release, key->str, val);
55+ g_hash_table_insert (hash, g_strdup(key->str), val); /* hash owns val now */
56+ }
57+
58+ g_string_free(key, TRUE);
59+ g_io_channel_unref(io);
60+ }
61+
62+ return hash;
63+}
64+
65+static const char*
66+get_distro_name (void)
67+{
68+ static char * distro_name = NULL;
69+
70+ if (distro_name == NULL)
71+ {
72+ GHashTable * os_release = get_os_release();
73+ gpointer value = g_hash_table_lookup(os_release, "NAME");
74+ if (value == NULL)
75+ value = "Ubuntu"; /* fallback value */
76+ distro_name = g_strdup(value);
77+ g_hash_table_destroy(os_release);
78+ }
79+
80+ return distro_name;
81+}
82+
83 static GMenuModel *
84 create_admin_section (void)
85 {
86 GMenu * menu;
87-
88+ gchar * help_label = g_strdup_printf(_("%s Help"), get_distro_name());
89 menu = g_menu_new ();
90 g_menu_append (menu, _("About This Computer"), "indicator.about");
91- g_menu_append (menu, _("Ubuntu Help"), "indicator.help");
92+ g_menu_append (menu, help_label, "indicator.help");
93+ g_free (help_label);
94 return G_MENU_MODEL (menu);
95 }
96

Subscribers

People subscribed via source and target branches