aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
blob: 991f4ec7d55a5d9279bbbb9b6aa333f937c36bd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright 2017 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "utils.h"

GHashTable*
get_os_release (void)
{
  static const char * const os_release = "/etc/os-release";
  GHashTable * hash;
  GIOChannel * io;

  hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

  if ((io = g_io_channel_new_file (os_release, "r", NULL)))
    {
      GString * key = g_string_new (NULL);

      for (;;)
        {
          GIOStatus status;
          char * in;
          GError * error;
          gchar * val;

          /* read a line */
          status = g_io_channel_read_line_string (io, key, NULL, NULL);
          if (status == G_IO_STATUS_EOF)
            break;

          /* ignore blank lines & comments */
          if (!key->len || key->str[0]=='#')
            continue;

          /* split into key=value */
          in = strchr(key->str, '=');
          if (!in)
            continue;
          *in++ = '\0';

          /* unmunge the value component */
          g_strstrip(in); /* eat linefeed */
          error = NULL;
          val = g_shell_unquote (in, &error);
          if (error != NULL)
            {
              g_warning("Unable to unquote \"%s\": %s", in, error->message);
              val = g_strdup(in);
              g_clear_error(&error);
            }

          g_debug("from \"%s\": key [%s] val [%s]", os_release, key->str, val);
          g_hash_table_insert (hash, g_strdup(key->str), val); /* hash owns val now */
        }

      g_string_free(key, TRUE);
      g_io_channel_unref(io);
    }

  return hash;
}

const char*
get_distro_name (void)
{
  static char * distro_name = NULL;

  if (distro_name == NULL)
    {
      GHashTable * os_release = get_os_release();
      gpointer value = g_hash_table_lookup(os_release, "NAME");
      if (value == NULL)
        value = "GNU/Linux"; /* fallback value */
      distro_name = g_strdup(value);
      g_hash_table_destroy(os_release);
    }

  return distro_name;
}

const char*
get_distro_url (void)
{
  static char * distro_url = NULL;

  if (distro_url == NULL)
    {
      GHashTable * os_release = get_os_release();
      gpointer value = g_hash_table_lookup(os_release, "SUPPORT_URL");

      if (value == NULL)
      {
        value = g_hash_table_lookup(os_release, "HOME_URL");

        if (value == NULL)
        {
            value = "https://www.gnu.org"; /* fallback value */
        }
      }

      distro_url = g_strdup(value);
      g_hash_table_destroy(os_release);
    }

  return distro_url;
}

const char*
get_distro_bts_url (void)
{
  static char * distro_bts_url = NULL;

  if (distro_bts_url == NULL)
    {
      GHashTable * os_release = get_os_release();
      gpointer value = g_hash_table_lookup(os_release, "BUG_REPORT_URL");
      if (value == NULL)
        value = "https://github.com/AyatanaIndicators/ayatana-indicator-session/issues"; /* fallback value */
      distro_bts_url = g_strdup(value);
      g_hash_table_destroy(os_release);
    }

  return distro_bts_url;
}

const char*
get_desktop_name (void)
{
  static char * desktop_name = NULL;
  const char * xdg_current_desktop;

  if (desktop_name == NULL)
    {
      xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
      if (xdg_current_desktop != NULL) {
        desktop_name = g_strsplit (xdg_current_desktop, ":", 0)[0];
      }
    }

  return desktop_name;
}