aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
blob: 82f9cff0bbd6eb235d7ee8bbaa94d49b7accb100 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright 2021 Marius Gripsgard <marius@ubports.com>
 *
 * 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"

#include <string.h>

#ifdef HAS_URLDISPATCHER
# include <url-dispatcher.h>
#endif

// TODO: make case insensitive
gboolean
is_xdg_current_desktop (const gchar* desktop)
{
  const gchar *xdg_current_desktop;
  gchar **desktop_names;
  int i;

  xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
  if (xdg_current_desktop != NULL) {
    desktop_names = g_strsplit (xdg_current_desktop, ":", 0);
    for (i = 0; desktop_names[i]; ++i) {
      if (!g_strcmp0 (desktop_names[i], desktop)) {
        g_strfreev (desktop_names);
        return TRUE;
      }
    }
    g_strfreev (desktop_names);
  }
  return FALSE;
}

gboolean
is_lomiri ()
{
  // For legacy reasons keep the MIR_SOCKET hack
  return (g_getenv ("MIR_SOCKET") != NULL ||
          is_xdg_current_desktop(DESKTOP_LOMIRI));
}

gboolean
is_gnome ()
{
  return is_xdg_current_desktop(DESKTOP_GNOME);
}

gboolean
is_unity ()
{
  return is_xdg_current_desktop(DESKTOP_UNITY);
}

gboolean
is_mate ()
{
  return is_xdg_current_desktop(DESKTOP_MATE);
}

gboolean
is_xfce ()
{
  return is_xdg_current_desktop(DESKTOP_XFCE);
}

gboolean
is_pantheon ()
{
  return is_xdg_current_desktop(DESKTOP_PANTHEON);
}

// Bit of a hacky way? should use xdg open
char *
find_browser ()
{
  static char * browser_path = NULL;
  char* tmp_browser_path;
  gchar **browser_names;

  int i;

  if (browser_path == NULL)
  {
    browser_names = g_strsplit ("x-www-browser,google-chrome,firefox,chromium", ",", 0);

    for (i = 0; browser_names[i]; ++i) {
      tmp_browser_path = g_find_program_in_path (browser_names[i]);

      if (tmp_browser_path) {
        browser_path = g_strdup (tmp_browser_path);
        g_free (tmp_browser_path);
        g_strfreev (browser_names);
        break;
      }
    }
  }

  return browser_path;
}

gboolean
execute_command (const gchar * cmd)
{
  GError * err = NULL;

  g_debug ("Issuing command '%s'", cmd);

  if (!g_spawn_command_line_async (cmd, &err))
  {
    g_warning ("Unable to start %s: %s", cmd, err->message);
    g_error_free (err);
    return FALSE;
  }

  return TRUE;
}

gboolean
open_url (const gchar * url)
{
  char * browser = NULL;

  if (is_lomiri())
  {
#ifdef HAS_URLDISPATCHER
    url_dispatch_send("settings:///system/battery", NULL, NULL);
    return TRUE;
#else
    g_warning("Built without url-dispatcher, is not able to open url");
#endif
  }

  if (browser == NULL)
    browser = find_browser();

  if (browser != NULL)
    return execute_command(g_strdup_printf("%s '%s'", browser, url));
  else
    return FALSE;

}

gboolean
have_program (const gchar * program)
{
  gchar *path;
  gboolean have;

  path = g_find_program_in_path(program);
  have = path != NULL;
  g_free(path);

  return have;
}