Debian Codegraph @

src/utils.c

raw symbols SCIP
1 /*
2 * Copyright 2021 Marius Gripsgard <[email protected]>
3 * Copyright 2021-2023 Robert Tari <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 3, as published
7 * by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranties of
11 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12 * PURPOSE. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "utils.h"
19 #include <glib/gi18n-lib.h>
20 #include <string.h>
21
22 #ifdef LOMIRI_FEATURES_ENABLED
23 # include <lomiri-url-dispatcher.h>
24 #endif
25
26 static gboolean bI18nInit = FALSE;
27
28 static void initI18n ()
29 {
30 if (!bI18nInit)
31 {
32 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
33 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
34 bI18nInit = TRUE;
35 }
36 }
37
38 // TODO: make case insensitive
39 gboolean
40 is_xdg_current_desktop (const gchar* desktop, const gchar* session)
41 {
42 const gchar *xdg_current_desktop;
43
44 xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
45 if (xdg_current_desktop != NULL) {
46 gchar **desktop_names = g_strsplit (xdg_current_desktop, ":", 0);
47 for (int i = 0; desktop_names[i]; ++i) {
48 if (!g_strcmp0 (desktop_names[i], desktop)) {
49 g_strfreev (desktop_names);
50 return TRUE;
51 }
52 }
53 g_strfreev (desktop_names);
54 }
55
56 if (session != NULL)
57 {
58 const gchar *xdg_session_desktop = g_getenv ("XDG_SESSION_DESKTOP");
59 if (xdg_session_desktop != NULL && g_str_equal(xdg_session_desktop, session))
60 {
61 return TRUE;
62 }
63
64 const gchar *desktop_session = g_getenv ("DESKTOP_SESSION");
65 if (desktop_session != NULL && g_str_equal(desktop_session, session))
66 {
67 return TRUE;
68 }
69 }
70
71 return FALSE;
72 }
73
74 gboolean
75 ayatana_common_utils_is_lomiri ()
76 {
77 return is_xdg_current_desktop(DESKTOP_LOMIRI, SESSION_LOMIRI);
78 }
79
80 gboolean
81 ayatana_common_utils_is_ubuntutouch ()
82 {
83 return is_xdg_current_desktop(DESKTOP_UBUNTUTOUCH, SESSION_UBUNTUTOUCH);
84 }
85
86 gboolean
87 ayatana_common_utils_is_gnome ()
88 {
89 return is_xdg_current_desktop(DESKTOP_GNOME, SESSION_GNOME);
90 }
91
92 gboolean
93 ayatana_common_utils_is_unity ()
94 {
95 return is_xdg_current_desktop(DESKTOP_UNITY, SESSION_UNITY);
96 }
97
98 gboolean
99 ayatana_common_utils_is_mate ()
100 {
101 return is_xdg_current_desktop(DESKTOP_MATE, SESSION_MATE);
102 }
103
104 gboolean
105 ayatana_common_utils_is_xfce ()
106 {
107 return is_xdg_current_desktop(DESKTOP_XFCE, SESSION_XFCE);
108 }
109
110 gboolean
111 ayatana_common_utils_is_pantheon ()
112 {
113 return is_xdg_current_desktop(DESKTOP_PANTHEON, SESSION_PANTHEON);
114 }
115
116 gboolean
117 ayatana_common_utils_is_budgie ()
118 {
119 return is_xdg_current_desktop(DESKTOP_BUDGIE, SESSION_BUDGIE);
120 }
121
122 gboolean
123 ayatana_common_utils_have_unity_program (const gchar *program)
124 {
125 if (!ayatana_common_utils_is_unity())
126 return FALSE;
127
128 return ayatana_common_utils_have_program(program);
129 }
130
131 gboolean
132 ayatana_common_utils_have_gnome_program (const gchar *program)
133 {
134 if (!ayatana_common_utils_is_gnome())
135 return FALSE;
136
137 return ayatana_common_utils_have_program(program);
138 }
139
140 gboolean
141 ayatana_common_utils_have_mate_program (const gchar *program)
142 {
143 if (!ayatana_common_utils_is_mate())
144 return FALSE;
145
146 return ayatana_common_utils_have_program(program);
147 }
148
149 gboolean
150 ayatana_common_utils_have_xfce_program (const gchar *program)
151 {
152 if (!ayatana_common_utils_is_xfce())
153 return FALSE;
154
155 return ayatana_common_utils_have_program(program);
156 }
157
158 gboolean
159 ayatana_common_utils_have_pantheon_program (const gchar *program)
160 {
161 if (!ayatana_common_utils_is_pantheon())
162 return FALSE;
163
164 return ayatana_common_utils_have_program(program);
165 }
166
167 gboolean
168 ayatana_common_utils_have_budgie_program (const gchar *program)
169 {
170 if (!ayatana_common_utils_is_budgie())
171 return FALSE;
172
173 return ayatana_common_utils_have_program(program);
174 }
175
176
177 // Bit of a hacky way? should use xdg open
178 char *
179 find_browser ()
180 {
181 static char * browser_path = NULL;
182
183 if (browser_path == NULL)
184 {
185 gchar **browser_names = g_strsplit ("x-www-browser,google-chrome,firefox,chromium", ",", 0);
186
187 for (int i = 0; browser_names[i]; ++i) {
188 char* tmp_browser_path = g_find_program_in_path (browser_names[i]);
189
190 if (tmp_browser_path) {
191 browser_path = g_strdup (tmp_browser_path);
192 g_free (tmp_browser_path);
193 g_strfreev (browser_names);
194 break;
195 }
196 }
197 }
198
199 return browser_path;
200 }
201
202 gboolean
203 ayatana_common_utils_execute_command (const gchar * cmd)
204 {
205 GError * err = NULL;
206
207 g_debug ("Issuing command '%s'", cmd);
208
209 if (!g_spawn_command_line_async (cmd, &err))
210 {
211 g_warning ("Unable to start %s: %s", cmd, err->message);
212 g_error_free (err);
213 return FALSE;
214 }
215
216 return TRUE;
217 }
218
219 gboolean
220 ayatana_common_utils_open_url (const gchar * url)
221 {
222 if (ayatana_common_utils_is_lomiri())
223 {
224 #ifdef LOMIRI_FEATURES_ENABLED
225 lomiri_url_dispatch_send(url, NULL, NULL);
226 return TRUE;
227 #else
228 g_warning("Built without url-dispatcher, not able to open URL '%s'", url);
229 #endif
230 }
231
232 char * browser = find_browser();
233
234 if (browser != NULL)
235 return ayatana_common_utils_execute_command(g_strdup_printf("%s '%s'", browser, url));
236 else
237 return FALSE;
238
239 }
240
241 gboolean
242 ayatana_common_utils_have_program (const gchar * program)
243 {
244 gchar *path;
245 gboolean have;
246
247 path = g_find_program_in_path(program);
248 have = path != NULL;
249 g_free(path);
250
251 return have;
252 }
253
254 gboolean
255 ayatana_common_utils_zenity_warning (const char * icon_name,
256 const char * title,
257 const char * text)
258 {
259 char * command_line;
260 int exit_status;
261 GError * error;
262 gboolean confirmed;
263 char * zenity;
264
265 confirmed = FALSE;
266 zenity = g_find_program_in_path ("zenity");
267
268 if (zenity)
269 {
270 command_line = g_strdup_printf ("%s"
271 " --warning"
272 " --icon-name=\"%s\""
273 " --title=\"%s\""
274 " --text=\"%s\""
275 " --no-wrap",
276 zenity,
277 icon_name,
278 title,
279 text);
280
281 /* Treat errors as user confirmation.
282 Otherwise how will the user ever log out? */
283 exit_status = -1;
284 error = NULL;
285 if (!g_spawn_command_line_sync (command_line, NULL, NULL, &exit_status, &error))
286 {
287 confirmed = TRUE;
288 }
289 else
290 {
291 #if GLIB_CHECK_VERSION(2, 70, 0)
292 confirmed = g_spawn_check_wait_status (exit_status, &error);
293 #else
294 confirmed = g_spawn_check_exit_status (exit_status, &error);
295 #endif
296 }
297
298 g_free (command_line);
299 }
300 g_free (zenity);
301 return confirmed;
302 }
303
304 void ayatana_common_utils_ellipsize(char *sText)
305 {
306 guint nMaxLetters = 50;
307 glong nLetters = g_utf8_strlen(sText, -1);
308 GSettingsSchemaSource *pSource = g_settings_schema_source_get_default();
309
310 if (pSource != NULL)
311 {
312 GSettingsSchema *pSchema = g_settings_schema_source_lookup(pSource, "org.ayatana.common", FALSE);
313
314 if (pSchema != NULL)
315 {
316 g_settings_schema_unref(pSchema);
317 GSettings *pSettings = g_settings_new("org.ayatana.common");
318 nMaxLetters = g_settings_get_uint(pSettings, "max-menu-text-length");
319 g_object_unref(pSettings);
320 }
321 }
322
323 if (nMaxLetters > 0 && nLetters > nMaxLetters + 4)
324 {
325 gchar *pLastChar = g_utf8_offset_to_pointer(sText, nMaxLetters);
326 memcpy(pLastChar, "...\0", 4);
327 }
328 }
329
330 gboolean ayatana_common_utils_execute_command_warn (const gchar *sProgram, const gchar *sArgs)
331 {
332 gboolean bHasProgram = ayatana_common_utils_have_program (sProgram);
333
334 if (!bHasProgram)
335 {
336 initI18n ();
337 gchar *sMessage = g_strdup_printf (_("The %s program is required for this action, but it was not found."), sProgram);
338 ayatana_common_utils_zenity_warning ("dialog-warning", _("Warning"), sMessage);
339 g_free (sMessage);
340
341 return FALSE;
342 }
343
344 gchar *sCommand = g_strdup_printf ("%s %s", sProgram, sArgs);
345 gboolean bSuccess = ayatana_common_utils_execute_command (sCommand);
346 g_free (sCommand);
347
348 return bSuccess;
349 }
350
351 gboolean ayatana_common_utils_zenity_question (const gchar *sIcon, const gchar *sTitle, const gchar *sText, const gchar *sOk, const gchar *sCancel)
352 {
353 gchar *sZenity = g_find_program_in_path ("zenity");
354 gchar *sCommand = g_strdup_printf ("%s --question --icon-name=\"%s\" --title=\"%s\" --text=\"%s\" --ok-label=\"%s\" --cancel-label=\"%s\" --no-wrap", sZenity, sIcon, sTitle, sText, sOk, sCancel);
355 g_free (sZenity);
356 int nStatus = -1;
357 GError *pError = NULL;
358 gboolean bConfirmed = FALSE;
359
360 if (!g_spawn_command_line_sync (sCommand, NULL, NULL, &nStatus, &pError))
361 {
362 bConfirmed = TRUE;
363 }
364 else
365 {
366 #if GLIB_CHECK_VERSION(2, 70, 0)
367 bConfirmed = g_spawn_check_wait_status (nStatus, &pError);
368 #else
369 bConfirmed = g_spawn_check_exit_status (nStatus, &pError);
370 #endif
371 }
372
373 if (pError)
374 {
375 if (!g_error_matches (pError, G_IO_ERROR, G_IO_ERROR_CANCELLED))
376 {
377 g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, pError->message);
378 }
379
380 g_clear_error (&pError);
381 }
382
383 g_free (sCommand);
384
385 return bConfirmed;
386 }