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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/*
* Copyright 2021 Marius Gripsgard <marius@ubports.com>
* Copyright 2021 Robert Tari <robert@tari.in>
*
* 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 LOMIRI_FEATURES_ENABLED
# include <lomiri-url-dispatcher.h>
#endif
// TODO: make case insensitive
gboolean
is_xdg_current_desktop (const gchar* desktop, const gchar* session)
{
const gchar *xdg_current_desktop;
xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
if (xdg_current_desktop != NULL) {
gchar **desktop_names = g_strsplit (xdg_current_desktop, ":", 0);
for (int i = 0; desktop_names[i]; ++i) {
if (!g_strcmp0 (desktop_names[i], desktop)) {
g_strfreev (desktop_names);
return TRUE;
}
}
g_strfreev (desktop_names);
}
if (session != NULL)
{
const gchar *desktop_session = g_getenv ("DESKTOP_SESSION");
if (desktop_session != NULL && g_str_equal(desktop_session, session))
{
return TRUE;
}
}
return FALSE;
}
gboolean
ayatana_common_utils_is_lomiri ()
{
return is_xdg_current_desktop(DESKTOP_LOMIRI, SESSION_LOMIRI);
}
gboolean
ayatana_common_utils_is_gnome ()
{
return is_xdg_current_desktop(DESKTOP_GNOME, SESSION_GNOME);
}
gboolean
ayatana_common_utils_is_unity ()
{
return is_xdg_current_desktop(DESKTOP_UNITY, SESSION_UNITY);
}
gboolean
ayatana_common_utils_is_mate ()
{
return is_xdg_current_desktop(DESKTOP_MATE, SESSION_MATE);
}
gboolean
ayatana_common_utils_is_xfce ()
{
return is_xdg_current_desktop(DESKTOP_XFCE, SESSION_XFCE);
}
gboolean
ayatana_common_utils_is_pantheon ()
{
return is_xdg_current_desktop(DESKTOP_PANTHEON, SESSION_PANTHEON);
}
gboolean
ayatana_common_utils_is_budgie ()
{
return is_xdg_current_desktop(DESKTOP_BUDGIE, SESSION_BUDGIE);
}
gboolean
ayatana_common_utils_have_unity_program (const gchar *program)
{
if (!ayatana_common_utils_is_unity())
return FALSE;
return ayatana_common_utils_have_program(program);
}
gboolean
ayatana_common_utils_have_gnome_program (const gchar *program)
{
if (!ayatana_common_utils_is_gnome())
return FALSE;
return ayatana_common_utils_have_program(program);
}
gboolean
ayatana_common_utils_have_mate_program (const gchar *program)
{
if (!ayatana_common_utils_is_mate())
return FALSE;
return ayatana_common_utils_have_program(program);
}
gboolean
ayatana_common_utils_have_xfce_program (const gchar *program)
{
if (!ayatana_common_utils_is_xfce())
return FALSE;
return ayatana_common_utils_have_program(program);
}
gboolean
ayatana_common_utils_have_pantheon_program (const gchar *program)
{
if (!ayatana_common_utils_is_pantheon())
return FALSE;
return ayatana_common_utils_have_program(program);
}
gboolean
ayatana_common_utils_have_budgie_program (const gchar *program)
{
if (!ayatana_common_utils_is_budgie())
return FALSE;
return ayatana_common_utils_have_program(program);
}
// Bit of a hacky way? should use xdg open
char *
find_browser ()
{
static char * browser_path = NULL;
if (browser_path == NULL)
{
gchar **browser_names = g_strsplit ("x-www-browser,google-chrome,firefox,chromium", ",", 0);
for (int i = 0; browser_names[i]; ++i) {
char* 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
ayatana_common_utils_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
ayatana_common_utils_open_url (const gchar * url)
{
if (ayatana_common_utils_is_lomiri())
{
#ifdef LOMIRI_FEATURES_ENABLED
lomiri_url_dispatch_send(url, NULL, NULL);
return TRUE;
#else
g_warning("Built without url-dispatcher, not able to open URL '%s'", url);
#endif
}
char * browser = find_browser();
if (browser != NULL)
return ayatana_common_utils_execute_command(g_strdup_printf("%s '%s'", browser, url));
else
return FALSE;
}
gboolean
ayatana_common_utils_have_program (const gchar * program)
{
gchar *path;
gboolean have;
path = g_find_program_in_path(program);
have = path != NULL;
g_free(path);
return have;
}
gboolean
ayatana_common_utils_zenity_warning (const char * icon_name,
const char * title,
const char * text)
{
char * command_line;
int exit_status;
GError * error;
gboolean confirmed;
char * zenity;
confirmed = FALSE;
zenity = g_find_program_in_path ("zenity");
if (zenity)
{
command_line = g_strdup_printf ("%s"
" --warning"
" --icon-name=\"%s\""
" --title=\"%s\""
" --text=\"%s\""
" --no-wrap",
zenity,
icon_name,
title,
text);
/* Treat errors as user confirmation.
Otherwise how will the user ever log out? */
exit_status = -1;
error = NULL;
if (!g_spawn_command_line_sync (command_line, NULL, NULL, &exit_status, &error))
{
confirmed = TRUE;
}
else
{
#if GLIB_CHECK_VERSION(2, 70, 0)
confirmed = g_spawn_check_wait_status (exit_status, &error);
#else
confirmed = g_spawn_check_exit_status (exit_status, &error);
#endif
}
g_free (command_line);
}
g_free (zenity);
return confirmed;
}
void ayatana_common_utils_ellipsize(char *sText)
{
guint nMaxLetters = 50;
glong nLetters = g_utf8_strlen(sText, -1);
GSettingsSchemaSource *pSource = g_settings_schema_source_get_default();
if (pSource != NULL)
{
GSettingsSchema *pSchema = g_settings_schema_source_lookup(pSource, "org.ayatana.common", FALSE);
if (pSchema != NULL)
{
g_settings_schema_unref(pSchema);
GSettings *pSettings = g_settings_new("org.ayatana.common");
nMaxLetters = g_settings_get_uint(pSettings, "max-menu-text-length");
g_object_unref(pSettings);
}
}
if (nMaxLetters > 0 && nLetters > nMaxLetters + 4)
{
gchar *pLastChar = g_utf8_offset_to_pointer(sText, nMaxLetters);
memcpy(pLastChar, "...\0", 4);
}
}
|