/src/pango/subprojects/glib/gio/gdesktopappinfo.c
Line | Count | Source |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright (C) 2006-2007 Red Hat, Inc. |
4 | | * Copyright © 2007 Ryan Lortie |
5 | | * |
6 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
7 | | * |
8 | | * This library is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * This library is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General |
19 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
20 | | * |
21 | | * Author: Alexander Larsson <alexl@redhat.com> |
22 | | * Ryan Lortie <desrt@desrt.ca> |
23 | | */ |
24 | | |
25 | | /* Prelude {{{1 */ |
26 | | |
27 | | #include "config.h" |
28 | | |
29 | | /* For the #GDesktopAppInfoLookup macros; since macro deprecation is implemented |
30 | | * in the preprocessor, we need to define this before including glib.h*/ |
31 | | #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS |
32 | | #define GLIB_DISABLE_DEPRECATION_WARNINGS |
33 | | #endif |
34 | | |
35 | | #include <errno.h> |
36 | | #include <string.h> |
37 | | #include <unistd.h> |
38 | | |
39 | | #ifdef HAVE_CRT_EXTERNS_H |
40 | | #include <crt_externs.h> |
41 | | #endif |
42 | | |
43 | | #include "gcontenttypeprivate.h" |
44 | | #include "gdesktopappinfo.h" |
45 | | #ifdef G_OS_UNIX |
46 | | #include "glib-unix.h" |
47 | | #endif |
48 | | #include "gfile.h" |
49 | | #include "gioerror.h" |
50 | | #include "gthemedicon.h" |
51 | | #include "gfileicon.h" |
52 | | #include <glib/gstdio.h> |
53 | | #include "glibintl.h" |
54 | | #include "glib-private.h" |
55 | | #include "giomodule-priv.h" |
56 | | #include "gappinfo.h" |
57 | | #include "gappinfoprivate.h" |
58 | | #include "glocalfilemonitor.h" |
59 | | #include "gutilsprivate.h" |
60 | | |
61 | | #ifdef G_OS_UNIX |
62 | | #include "gdocumentportal.h" |
63 | | #endif |
64 | | |
65 | | /** |
66 | | * GDesktopAppInfo: |
67 | | * |
68 | | * `GDesktopAppInfo` is an implementation of [iface@Gio.AppInfo] based on |
69 | | * desktop files. |
70 | | * |
71 | | * Note that `<gio/gdesktopappinfo.h>` belongs to the UNIX-specific |
72 | | * GIO interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config |
73 | | * file or the `GioUnix-2.0` GIR namespace when using it. |
74 | | */ |
75 | | |
76 | 0 | #define DEFAULT_APPLICATIONS_GROUP "Default Applications" |
77 | 0 | #define ADDED_ASSOCIATIONS_GROUP "Added Associations" |
78 | 0 | #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations" |
79 | 0 | #define MIME_CACHE_GROUP "MIME Cache" |
80 | 0 | #define GENERIC_NAME_KEY "GenericName" |
81 | 0 | #define FULL_NAME_KEY "X-GNOME-FullName" |
82 | 0 | #define KEYWORDS_KEY "Keywords" |
83 | 0 | #define STARTUP_WM_CLASS_KEY "StartupWMClass" |
84 | | |
85 | | enum { |
86 | | PROP_0, |
87 | | PROP_FILENAME |
88 | | }; |
89 | | |
90 | | static void g_desktop_app_info_iface_init (GAppInfoIface *iface); |
91 | | static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info, |
92 | | GError **error); |
93 | | static gboolean g_desktop_app_info_load_file (GDesktopAppInfo *self); |
94 | | |
95 | | struct _GDesktopAppInfo |
96 | | { |
97 | | GObject parent_instance; |
98 | | |
99 | | char *desktop_id; |
100 | | char *filename; |
101 | | char *app_id; |
102 | | |
103 | | GKeyFile *keyfile; |
104 | | |
105 | | char *name; |
106 | | char *generic_name; |
107 | | char *fullname; |
108 | | char *comment; |
109 | | char *icon_name; |
110 | | GIcon *icon; |
111 | | char **keywords; |
112 | | char **only_show_in; |
113 | | char **not_show_in; |
114 | | char *try_exec; |
115 | | char *exec; |
116 | | char *binary; |
117 | | char *path; |
118 | | char *categories; |
119 | | char *startup_wm_class; |
120 | | char **mime_types; |
121 | | char **actions; |
122 | | |
123 | | guint nodisplay : 1; |
124 | | guint hidden : 1; |
125 | | guint terminal : 1; |
126 | | guint startup_notify : 1; |
127 | | guint no_fuse : 1; |
128 | | }; |
129 | | |
130 | | typedef enum { |
131 | | UPDATE_MIME_NONE = 1 << 0, |
132 | | UPDATE_MIME_SET_DEFAULT = 1 << 1, |
133 | | UPDATE_MIME_SET_NON_DEFAULT = 1 << 2, |
134 | | UPDATE_MIME_REMOVE = 1 << 3, |
135 | | UPDATE_MIME_SET_LAST_USED = 1 << 4, |
136 | | } UpdateMimeFlags; |
137 | | |
138 | 0 | G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT, |
139 | 0 | G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_desktop_app_info_iface_init)) |
140 | 0 |
|
141 | 0 | /* DesktopFileDir implementation {{{1 */ |
142 | 0 |
|
143 | 0 | typedef struct |
144 | 0 | { |
145 | 0 | gatomicrefcount ref_count; |
146 | 0 | gchar *path; |
147 | 0 | gchar *alternatively_watching; |
148 | 0 | gboolean is_config; |
149 | 0 | gboolean is_setup; |
150 | 0 | GFileMonitor *monitor; |
151 | 0 | GHashTable *app_names; |
152 | 0 | GHashTable *mime_tweaks; |
153 | 0 | GHashTable *memory_index; |
154 | 0 | GHashTable *memory_implementations; |
155 | 0 | } DesktopFileDir; |
156 | 0 |
|
157 | 0 | static GPtrArray *desktop_file_dirs = NULL; |
158 | 0 | static const gchar *desktop_file_dirs_config_dir = NULL; |
159 | 0 | static DesktopFileDir *desktop_file_dir_user_config = NULL; /* (owned) */ |
160 | 0 | static DesktopFileDir *desktop_file_dir_user_data = NULL; /* (owned) */ |
161 | 0 | static GMutex desktop_file_dir_lock; |
162 | 0 | static const gchar *gio_launch_desktop_path = NULL; |
163 | 0 |
|
164 | 0 | /* Monitor 'changed' signal handler {{{2 */ |
165 | 0 | static void desktop_file_dir_reset (DesktopFileDir *dir); |
166 | 0 |
|
167 | 0 | static DesktopFileDir * |
168 | 0 | desktop_file_dir_ref (DesktopFileDir *dir) |
169 | 0 | { |
170 | 0 | g_atomic_ref_count_inc (&dir->ref_count); |
171 | |
|
172 | 0 | return dir; |
173 | 0 | } |
174 | | |
175 | | static void |
176 | | desktop_file_dir_unref (DesktopFileDir *dir) |
177 | 0 | { |
178 | 0 | if (g_atomic_ref_count_dec (&dir->ref_count)) |
179 | 0 | { |
180 | 0 | desktop_file_dir_reset (dir); |
181 | 0 | g_free (dir->path); |
182 | 0 | g_free (dir); |
183 | 0 | } |
184 | 0 | } |
185 | | |
186 | | /*< internal > |
187 | | * desktop_file_dir_get_alternative_dir: |
188 | | * @dir: a #DesktopFileDir |
189 | | * |
190 | | * Gets the "alternative" directory to monitor in case the path |
191 | | * doesn't exist. |
192 | | * |
193 | | * If the path exists this will return NULL, otherwise it will return a |
194 | | * parent directory of the path. |
195 | | * |
196 | | * This is used to avoid inotify on a non-existent directory (which |
197 | | * results in polling). |
198 | | * |
199 | | * See https://bugzilla.gnome.org/show_bug.cgi?id=522314 for more info. |
200 | | */ |
201 | | static gchar * |
202 | | desktop_file_dir_get_alternative_dir (DesktopFileDir *dir) |
203 | 0 | { |
204 | 0 | gchar *parent; |
205 | | |
206 | | /* If the directory itself exists then we need no alternative. */ |
207 | 0 | if (g_access (dir->path, R_OK | X_OK) == 0) |
208 | 0 | return NULL; |
209 | | |
210 | | /* Otherwise, try the parent directories until we find one. */ |
211 | 0 | parent = g_path_get_dirname (dir->path); |
212 | |
|
213 | 0 | while (g_access (parent, R_OK | X_OK) != 0) |
214 | 0 | { |
215 | 0 | gchar *tmp = parent; |
216 | |
|
217 | 0 | parent = g_path_get_dirname (tmp); |
218 | | |
219 | | /* If somehow we get to '/' or '.' then just stop... */ |
220 | 0 | if (g_str_equal (parent, tmp)) |
221 | 0 | { |
222 | 0 | g_free (tmp); |
223 | 0 | break; |
224 | 0 | } |
225 | | |
226 | 0 | g_free (tmp); |
227 | 0 | } |
228 | |
|
229 | 0 | return parent; |
230 | 0 | } |
231 | | |
232 | | static void |
233 | | desktop_file_dir_changed (GFileMonitor *monitor, |
234 | | GFile *file, |
235 | | GFile *other_file, |
236 | | GFileMonitorEvent event_type, |
237 | | gpointer user_data) |
238 | 0 | { |
239 | 0 | DesktopFileDir *dir = user_data; |
240 | 0 | gboolean do_nothing = FALSE; |
241 | | |
242 | | /* We are not interested in receiving notifications forever just |
243 | | * because someone asked about one desktop file once. |
244 | | * |
245 | | * After we receive the first notification, reset the dir, destroying |
246 | | * the monitor. We will take this as a hint, next time that we are |
247 | | * asked, that we need to check if everything is up to date. |
248 | | * |
249 | | * If this is a notification for a parent directory (because the |
250 | | * desktop directory didn't exist) then we shouldn't fire the signal |
251 | | * unless something actually changed. |
252 | | */ |
253 | 0 | g_mutex_lock (&desktop_file_dir_lock); |
254 | |
|
255 | 0 | if (dir->alternatively_watching) |
256 | 0 | { |
257 | 0 | gchar *alternative_dir; |
258 | |
|
259 | 0 | alternative_dir = desktop_file_dir_get_alternative_dir (dir); |
260 | 0 | do_nothing = alternative_dir && g_str_equal (dir->alternatively_watching, alternative_dir); |
261 | 0 | g_free (alternative_dir); |
262 | 0 | } |
263 | |
|
264 | 0 | if (!do_nothing) |
265 | 0 | desktop_file_dir_reset (dir); |
266 | |
|
267 | 0 | g_mutex_unlock (&desktop_file_dir_lock); |
268 | | |
269 | | /* Notify anyone else who may be interested */ |
270 | 0 | if (!do_nothing) |
271 | 0 | g_app_info_monitor_fire (); |
272 | 0 | } |
273 | | |
274 | | /* Internal utility functions {{{2 */ |
275 | | |
276 | | /*< internal > |
277 | | * desktop_file_dir_app_name_is_masked: |
278 | | * @dir: a #DesktopFileDir |
279 | | * @app_name: an application ID |
280 | | * |
281 | | * Checks if @app_name is masked for @dir. |
282 | | * |
283 | | * An application is masked if a similarly-named desktop file exists in |
284 | | * a desktop file directory with higher precedence. Masked desktop |
285 | | * files should be ignored. |
286 | | */ |
287 | | static gboolean |
288 | | desktop_file_dir_app_name_is_masked (DesktopFileDir *dir, |
289 | | const gchar *app_name) |
290 | 0 | { |
291 | 0 | guint i; |
292 | |
|
293 | 0 | for (i = 0; i < desktop_file_dirs->len; i++) |
294 | 0 | { |
295 | 0 | DesktopFileDir *i_dir = g_ptr_array_index (desktop_file_dirs, i); |
296 | |
|
297 | 0 | if (dir == i_dir) |
298 | 0 | return FALSE; |
299 | 0 | if (i_dir->app_names && g_hash_table_contains (i_dir->app_names, app_name)) |
300 | 0 | return TRUE; |
301 | 0 | } |
302 | | |
303 | 0 | return FALSE; |
304 | 0 | } |
305 | | |
306 | | /* Not much to go on from https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html |
307 | | * so validate it as a non-empty alphanumeric ASCII string with `-` and `_` allowed. |
308 | | * |
309 | | * Validation is important as the desktop IDs are used to construct filenames, |
310 | | * and may be set by an unprivileged caller if running in a setuid program. */ |
311 | | static gboolean |
312 | | validate_xdg_desktop (const gchar *desktop) |
313 | 0 | { |
314 | 0 | gsize i; |
315 | |
|
316 | 0 | for (i = 0; desktop[i] != '\0'; i++) |
317 | 0 | if (desktop[i] != '-' && desktop[i] != '_' && |
318 | 0 | !g_ascii_isalnum (desktop[i])) |
319 | 0 | return FALSE; |
320 | | |
321 | 0 | if (i == 0) |
322 | 0 | return FALSE; |
323 | | |
324 | 0 | return TRUE; |
325 | 0 | } |
326 | | |
327 | | static char ** |
328 | | get_valid_current_desktops (const char *value) |
329 | 0 | { |
330 | 0 | char **tmp; |
331 | 0 | gsize i; |
332 | 0 | GPtrArray *valid_desktops; |
333 | |
|
334 | 0 | if (value == NULL) |
335 | 0 | value = g_getenv ("XDG_CURRENT_DESKTOP"); |
336 | 0 | if (value == NULL) |
337 | 0 | value = ""; |
338 | |
|
339 | 0 | tmp = g_strsplit (value, G_SEARCHPATH_SEPARATOR_S, 0); |
340 | 0 | valid_desktops = g_ptr_array_new_full (g_strv_length (tmp) + 1, g_free); |
341 | 0 | for (i = 0; tmp[i]; i++) |
342 | 0 | { |
343 | 0 | if (validate_xdg_desktop (tmp[i])) |
344 | 0 | g_ptr_array_add (valid_desktops, tmp[i]); |
345 | 0 | else |
346 | 0 | g_free (tmp[i]); |
347 | 0 | } |
348 | 0 | g_ptr_array_add (valid_desktops, NULL); |
349 | 0 | g_free (tmp); |
350 | 0 | tmp = (char **) g_ptr_array_steal (valid_desktops, NULL); |
351 | 0 | g_ptr_array_unref (valid_desktops); |
352 | 0 | return tmp; |
353 | 0 | } |
354 | | |
355 | | static const gchar * const * |
356 | | get_lowercase_current_desktops (void) |
357 | 0 | { |
358 | 0 | static gchar **result; |
359 | |
|
360 | 0 | if (g_once_init_enter_pointer (&result)) |
361 | 0 | { |
362 | 0 | char **tmp = get_valid_current_desktops (NULL); |
363 | 0 | gsize i, j; |
364 | |
|
365 | 0 | for (i = 0; tmp[i]; i++) |
366 | 0 | { |
367 | | /* Convert to lowercase. */ |
368 | 0 | for (j = 0; tmp[i][j]; j++) |
369 | 0 | tmp[i][j] = g_ascii_tolower (tmp[i][j]); |
370 | 0 | } |
371 | |
|
372 | 0 | g_once_init_leave_pointer (&result, tmp); |
373 | 0 | } |
374 | |
|
375 | 0 | return (const gchar **) result; |
376 | 0 | } |
377 | | |
378 | | static const gchar * const * |
379 | | get_current_desktops (const gchar *value) |
380 | 0 | { |
381 | 0 | static gchar **result; |
382 | |
|
383 | 0 | if (g_once_init_enter_pointer (&result)) |
384 | 0 | { |
385 | 0 | char **tmp = get_valid_current_desktops (value); |
386 | |
|
387 | 0 | g_once_init_leave_pointer (&result, tmp); |
388 | 0 | } |
389 | |
|
390 | 0 | return (const gchar **) result; |
391 | 0 | } |
392 | | |
393 | | /*< internal > |
394 | | * add_to_table_if_appropriate: |
395 | | * @apps: a string to GDesktopAppInfo hash table |
396 | | * @app_name: the name of the application |
397 | | * @info: a #GDesktopAppInfo, or NULL |
398 | | * |
399 | | * If @info is non-%NULL and non-hidden, then add it to @apps, using |
400 | | * @app_name as a key. |
401 | | * |
402 | | * If @info is non-%NULL then this function will consume the passed-in |
403 | | * reference. |
404 | | */ |
405 | | static void |
406 | | add_to_table_if_appropriate (GHashTable *apps, |
407 | | const gchar *app_name, |
408 | | GDesktopAppInfo *info) |
409 | 0 | { |
410 | 0 | if (!info) |
411 | 0 | return; |
412 | | |
413 | 0 | if (info->hidden) |
414 | 0 | { |
415 | 0 | g_object_unref (info); |
416 | 0 | return; |
417 | 0 | } |
418 | | |
419 | 0 | g_free (info->desktop_id); |
420 | 0 | info->desktop_id = g_strdup (app_name); |
421 | |
|
422 | 0 | g_hash_table_insert (apps, g_strdup (info->desktop_id), info); |
423 | 0 | } |
424 | | |
425 | | enum |
426 | | { |
427 | | DESKTOP_KEY_Exec, |
428 | | DESKTOP_KEY_GenericName, |
429 | | DESKTOP_KEY_Keywords, |
430 | | DESKTOP_KEY_Name, |
431 | | DESKTOP_KEY_X_GNOME_FullName, |
432 | | |
433 | | N_DESKTOP_KEYS |
434 | | }; |
435 | | |
436 | | const gchar desktop_key_match_category[N_DESKTOP_KEYS] = { |
437 | | /* Note: lower numbers are a better match. |
438 | | * |
439 | | * In case we want two keys to match at the same level, we can just |
440 | | * use the same number for the two different keys. |
441 | | */ |
442 | | [DESKTOP_KEY_Name] = 1, |
443 | | [DESKTOP_KEY_Exec] = 2, |
444 | | [DESKTOP_KEY_Keywords] = 3, |
445 | | [DESKTOP_KEY_GenericName] = 4, |
446 | | [DESKTOP_KEY_X_GNOME_FullName] = 5 |
447 | | }; |
448 | | |
449 | | typedef enum { |
450 | | /* Lower numbers have higher priority. |
451 | | * Prefix match should put before substring match, independently of |
452 | | * category relevance, i.e. a prefix match in 'Keyword' category will |
453 | | * come before a substring match in a more relevant category like 'Name'. |
454 | | */ |
455 | | MATCH_TYPE_PREFIX = 1, |
456 | | MATCH_TYPE_SUBSTRING = 2 |
457 | | } MatchType; |
458 | | |
459 | | /* Common prefix commands to ignore from Exec= lines */ |
460 | | const char * const exec_key_match_blocklist[] = { |
461 | | "bash", |
462 | | "env", |
463 | | "flatpak", |
464 | | "gjs", |
465 | | "pkexec", |
466 | | "python", |
467 | | "python2", |
468 | | "python3", |
469 | | "sh", |
470 | | "wine", |
471 | | "wine64", |
472 | | NULL |
473 | | }; |
474 | | |
475 | | static gchar * |
476 | | desktop_key_get_name (guint key_id) |
477 | 0 | { |
478 | 0 | switch (key_id) |
479 | 0 | { |
480 | 0 | case DESKTOP_KEY_Exec: |
481 | 0 | return "Exec"; |
482 | 0 | case DESKTOP_KEY_GenericName: |
483 | 0 | return GENERIC_NAME_KEY; |
484 | 0 | case DESKTOP_KEY_Keywords: |
485 | 0 | return KEYWORDS_KEY; |
486 | 0 | case DESKTOP_KEY_Name: |
487 | 0 | return "Name"; |
488 | 0 | case DESKTOP_KEY_X_GNOME_FullName: |
489 | 0 | return FULL_NAME_KEY; |
490 | 0 | default: |
491 | 0 | g_assert_not_reached (); |
492 | 0 | } |
493 | 0 | } |
494 | | |
495 | | /* Search global state {{{2 |
496 | | * |
497 | | * We only ever search under a global lock, so we can use (and reuse) |
498 | | * some global data to reduce allocations made while searching. |
499 | | * |
500 | | * In short, we keep around arrays of results that we expand as needed |
501 | | * (and never shrink). |
502 | | * |
503 | | * static_token_results: this is where we append the results for each |
504 | | * token within a given desktop directory, as we handle it (which is |
505 | | * a union of all matches for this term) |
506 | | * |
507 | | * static_search_results: this is where we build the complete results |
508 | | * for a single directory (which is an intersection of the matches |
509 | | * found for each term) |
510 | | * |
511 | | * static_total_results: this is where we build the complete results |
512 | | * across all directories (which is a union of the matches found in |
513 | | * each directory) |
514 | | * |
515 | | * The app_names that enter these tables are always pointer-unique (in |
516 | | * the sense that string equality is the same as pointer equality). |
517 | | * This can be guaranteed for two reasons: |
518 | | * |
519 | | * - we mask appids so that a given appid will only ever appear within |
520 | | * the highest-precedence directory that contains it. We never |
521 | | * return search results from a lower-level directory if a desktop |
522 | | * file exists in a higher-level one. |
523 | | * |
524 | | * - within a given directory, the string is unique because it's the |
525 | | * key in the hashtable of all app_ids for that directory. |
526 | | * |
527 | | * We perform a merging of the results in merge_token_results(). This |
528 | | * works by ordering the two lists and moving through each of them (at |
529 | | * the same time) looking for common elements, rejecting uncommon ones. |
530 | | * "Order" here need not mean any particular thing, as long as it is |
531 | | * some order. Because of the uniqueness of our strings, we can use |
532 | | * pointer order. That's what's going on in compare_results() below. |
533 | | */ |
534 | | struct search_result |
535 | | { |
536 | | const gchar *app_name; |
537 | | gint category; |
538 | | gint match_type; |
539 | | gint token_pos; |
540 | | }; |
541 | | |
542 | | static struct search_result *static_token_results; |
543 | | static gint static_token_results_size; |
544 | | static gint static_token_results_allocated; |
545 | | static struct search_result *static_search_results; |
546 | | static gint static_search_results_size; |
547 | | static gint static_search_results_allocated; |
548 | | static struct search_result *static_total_results; |
549 | | static gint static_total_results_size; |
550 | | static gint static_total_results_allocated; |
551 | | |
552 | | /* And some functions for performing nice operations against it */ |
553 | | static gint |
554 | | compare_results (gconstpointer a, |
555 | | gconstpointer b) |
556 | 0 | { |
557 | 0 | const struct search_result *ra = a; |
558 | 0 | const struct search_result *rb = b; |
559 | |
|
560 | 0 | if (ra->app_name < rb->app_name) |
561 | 0 | { |
562 | 0 | return -1; |
563 | 0 | } |
564 | 0 | else if (ra->app_name > rb->app_name) |
565 | 0 | { |
566 | 0 | return 1; |
567 | 0 | } |
568 | 0 | else |
569 | 0 | { |
570 | | /* We prioritize prefix matches over category relevance e.g. a prefix match in 'Keyword' |
571 | | * category is better than a substring match in a more relevance category like 'Name'. |
572 | | */ |
573 | 0 | if (ra->match_type != rb->match_type) |
574 | 0 | return ra->match_type - rb->match_type; |
575 | | |
576 | 0 | return ra->category - rb->category; |
577 | 0 | } |
578 | 0 | } |
579 | | |
580 | | static gint |
581 | | compare_categories (gconstpointer a, |
582 | | gconstpointer b) |
583 | 0 | { |
584 | 0 | const struct search_result *ra = a; |
585 | 0 | const struct search_result *rb = b; |
586 | | |
587 | | /* We prioritize prefix matches over category relevance e.g. a prefix match in 'Keyword' |
588 | | * category is better than a substring match in a more relevance category like 'Name'. |
589 | | */ |
590 | 0 | if (ra->match_type != rb->match_type) |
591 | 0 | return ra->match_type - rb->match_type; |
592 | | |
593 | 0 | if (ra->category != rb->category) |
594 | 0 | return ra->category - rb->category; |
595 | | |
596 | | /* We prefer matches that occur earlier in the string. Eg. this will match 'Calculator' |
597 | | * before 'LibreOffice Calc' when searching for 'calc'. |
598 | | */ |
599 | 0 | return ra->token_pos - rb->token_pos; |
600 | 0 | } |
601 | | |
602 | | static void |
603 | | add_token_result (const gchar *app_name, |
604 | | guint16 category, |
605 | | guint16 match_type, |
606 | | guint16 token_pos) |
607 | 0 | { |
608 | 0 | if G_UNLIKELY (static_token_results_size == static_token_results_allocated) |
609 | 0 | { |
610 | 0 | static_token_results_allocated = MAX (16, static_token_results_allocated * 2); |
611 | 0 | static_token_results = g_renew (struct search_result, static_token_results, static_token_results_allocated); |
612 | 0 | } |
613 | |
|
614 | 0 | static_token_results[static_token_results_size].app_name = app_name; |
615 | 0 | static_token_results[static_token_results_size].category = category; |
616 | 0 | static_token_results[static_token_results_size].match_type = match_type; |
617 | 0 | static_token_results[static_token_results_size].token_pos = token_pos; |
618 | 0 | static_token_results_size++; |
619 | 0 | } |
620 | | |
621 | | static void |
622 | | merge_token_results (gboolean first) |
623 | 0 | { |
624 | 0 | if (static_token_results_size != 0) |
625 | 0 | qsort (static_token_results, static_token_results_size, sizeof (struct search_result), compare_results); |
626 | | |
627 | | /* If this is the first token then we are basically merging a list with |
628 | | * itself -- we only perform de-duplication. |
629 | | * |
630 | | * If this is not the first token then we are doing a real merge. |
631 | | */ |
632 | 0 | if (first) |
633 | 0 | { |
634 | 0 | const gchar *last_name = NULL; |
635 | 0 | gint i; |
636 | | |
637 | | /* We must de-duplicate, but we do so by taking the best category |
638 | | * in each case. |
639 | | * |
640 | | * The final list can be as large as the input here, so make sure |
641 | | * we have enough room (even if it's too much room). |
642 | | */ |
643 | |
|
644 | 0 | if G_UNLIKELY (static_search_results_allocated < static_token_results_size) |
645 | 0 | { |
646 | 0 | static_search_results_allocated = static_token_results_allocated; |
647 | 0 | static_search_results = g_renew (struct search_result, |
648 | 0 | static_search_results, |
649 | 0 | static_search_results_allocated); |
650 | 0 | } |
651 | |
|
652 | 0 | for (i = 0; i < static_token_results_size; i++) |
653 | 0 | { |
654 | | /* The list is sorted so that the best match for a given id |
655 | | * will be at the front, so once we have copied an id, skip |
656 | | * the rest of the entries for the same id. |
657 | | */ |
658 | 0 | if (static_token_results[i].app_name == last_name) |
659 | 0 | continue; |
660 | | |
661 | 0 | last_name = static_token_results[i].app_name; |
662 | |
|
663 | 0 | static_search_results[static_search_results_size++] = static_token_results[i]; |
664 | 0 | } |
665 | 0 | } |
666 | 0 | else |
667 | 0 | { |
668 | 0 | const gchar *last_name = NULL; |
669 | 0 | gint i, j = 0; |
670 | 0 | gint k = 0; |
671 | | |
672 | | /* We only ever remove items from the results list, so no need to |
673 | | * resize to ensure that we have enough room. |
674 | | */ |
675 | 0 | for (i = 0; i < static_token_results_size; i++) |
676 | 0 | { |
677 | 0 | if (static_token_results[i].app_name == last_name) |
678 | 0 | continue; |
679 | | |
680 | 0 | last_name = static_token_results[i].app_name; |
681 | | |
682 | | /* Now we only want to have a result in static_search_results |
683 | | * if we already have it there *and* we have it in |
684 | | * static_token_results as well. The category will be the |
685 | | * lesser of the two. |
686 | | * |
687 | | * Skip past the results in static_search_results that are not |
688 | | * going to be matches. |
689 | | */ |
690 | 0 | while (k < static_search_results_size && |
691 | 0 | static_search_results[k].app_name < static_token_results[i].app_name) |
692 | 0 | k++; |
693 | |
|
694 | 0 | if (k < static_search_results_size && |
695 | 0 | static_search_results[k].app_name == static_token_results[i].app_name) |
696 | 0 | { |
697 | | /* We have a match. |
698 | | * |
699 | | * Category should be the worse of the two (ie: |
700 | | * numerically larger). |
701 | | * |
702 | | * Match type should also be the worse, so if an app has two |
703 | | * prefix matches it will has higher priority than one prefix |
704 | | * matches and one substring matches, for example, LibreOffice |
705 | | * Writer should be higher priority than LibreOffice Draw with |
706 | | * `lib w`. |
707 | | * |
708 | | * We prioritize tokens that occur near the start of the string |
709 | | * over tokens that appear near the end. |
710 | | * |
711 | | * (This ignores the difference between partly prefix matches and |
712 | | * all substring matches, however most time we just focus on exact |
713 | | * prefix matches, who cares the 10th-20th search results?) |
714 | | */ |
715 | 0 | static_search_results[j].app_name = static_search_results[k].app_name; |
716 | 0 | static_search_results[j].category = MAX (static_search_results[k].category, |
717 | 0 | static_token_results[i].category); |
718 | 0 | static_search_results[j].match_type = MAX (static_search_results[k].match_type, |
719 | 0 | static_token_results[i].match_type); |
720 | 0 | static_search_results[j].token_pos = MAX (static_search_results[k].token_pos, |
721 | 0 | static_token_results[i].token_pos); |
722 | 0 | j++; |
723 | 0 | } |
724 | 0 | } |
725 | |
|
726 | 0 | static_search_results_size = j; |
727 | 0 | } |
728 | | |
729 | | /* Clear it out for next time... */ |
730 | 0 | static_token_results_size = 0; |
731 | 0 | } |
732 | | |
733 | | static void |
734 | | reset_total_search_results (void) |
735 | 0 | { |
736 | 0 | static_total_results_size = 0; |
737 | 0 | } |
738 | | |
739 | | static void |
740 | | sort_total_search_results (void) |
741 | 0 | { |
742 | 0 | if (static_total_results_size != 0) |
743 | 0 | qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories); |
744 | 0 | } |
745 | | |
746 | | static void |
747 | | merge_directory_results (void) |
748 | 0 | { |
749 | 0 | if G_UNLIKELY (static_total_results_size + static_search_results_size > static_total_results_allocated) |
750 | 0 | { |
751 | 0 | static_total_results_allocated = MAX (16, static_total_results_allocated); |
752 | 0 | while (static_total_results_allocated < static_total_results_size + static_search_results_size) |
753 | 0 | static_total_results_allocated *= 2; |
754 | 0 | static_total_results = g_renew (struct search_result, static_total_results, static_total_results_allocated); |
755 | 0 | } |
756 | |
|
757 | 0 | if (static_search_results_size != 0) |
758 | 0 | memcpy (static_total_results + static_total_results_size, |
759 | 0 | static_search_results, |
760 | 0 | static_search_results_size * sizeof (struct search_result)); |
761 | |
|
762 | 0 | static_total_results_size += static_search_results_size; |
763 | | |
764 | | /* Clear it out for next time... */ |
765 | 0 | static_search_results_size = 0; |
766 | 0 | } |
767 | | |
768 | | /* Support for unindexed DesktopFileDirs {{{2 */ |
769 | | static void |
770 | | get_apps_from_dir (GHashTable **apps, |
771 | | const char *dirname, |
772 | | const char *prefix) |
773 | 0 | { |
774 | 0 | const char *basename; |
775 | 0 | GDir *dir; |
776 | |
|
777 | 0 | dir = g_dir_open (dirname, 0, NULL); |
778 | |
|
779 | 0 | if (dir == NULL) |
780 | 0 | return; |
781 | | |
782 | 0 | while ((basename = g_dir_read_name (dir)) != NULL) |
783 | 0 | { |
784 | 0 | gchar *filename; |
785 | |
|
786 | 0 | filename = g_build_filename (dirname, basename, NULL); |
787 | |
|
788 | 0 | if (g_str_has_suffix (basename, ".desktop")) |
789 | 0 | { |
790 | 0 | gchar *app_name; |
791 | |
|
792 | 0 | app_name = g_strconcat (prefix, basename, NULL); |
793 | |
|
794 | 0 | if (*apps == NULL) |
795 | 0 | *apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
796 | |
|
797 | 0 | g_hash_table_insert (*apps, app_name, g_strdup (filename)); |
798 | 0 | } |
799 | 0 | else if (g_file_test (filename, G_FILE_TEST_IS_DIR)) |
800 | 0 | { |
801 | 0 | gchar *subprefix; |
802 | |
|
803 | 0 | subprefix = g_strconcat (prefix, basename, "-", NULL); |
804 | 0 | get_apps_from_dir (apps, filename, subprefix); |
805 | 0 | g_free (subprefix); |
806 | 0 | } |
807 | |
|
808 | 0 | g_free (filename); |
809 | 0 | } |
810 | |
|
811 | 0 | g_dir_close (dir); |
812 | 0 | } |
813 | | |
814 | | typedef struct |
815 | | { |
816 | | gchar **additions; |
817 | | gchar **removals; |
818 | | gchar **defaults; |
819 | | } UnindexedMimeTweaks; |
820 | | |
821 | | static void |
822 | | free_mime_tweaks (gpointer data) |
823 | 0 | { |
824 | 0 | UnindexedMimeTweaks *tweaks = data; |
825 | |
|
826 | 0 | g_strfreev (tweaks->additions); |
827 | 0 | g_strfreev (tweaks->removals); |
828 | 0 | g_strfreev (tweaks->defaults); |
829 | |
|
830 | 0 | g_slice_free (UnindexedMimeTweaks, tweaks); |
831 | 0 | } |
832 | | |
833 | | static UnindexedMimeTweaks * |
834 | | desktop_file_dir_unindexed_get_tweaks (DesktopFileDir *dir, |
835 | | const gchar *mime_type) |
836 | 0 | { |
837 | 0 | UnindexedMimeTweaks *tweaks; |
838 | 0 | gchar *unaliased_type; |
839 | |
|
840 | 0 | unaliased_type = _g_unix_content_type_unalias (mime_type); |
841 | 0 | tweaks = g_hash_table_lookup (dir->mime_tweaks, unaliased_type); |
842 | |
|
843 | 0 | if (tweaks == NULL) |
844 | 0 | { |
845 | 0 | tweaks = g_slice_new0 (UnindexedMimeTweaks); |
846 | 0 | g_hash_table_insert (dir->mime_tweaks, unaliased_type, tweaks); |
847 | 0 | } |
848 | 0 | else |
849 | 0 | g_free (unaliased_type); |
850 | |
|
851 | 0 | return tweaks; |
852 | 0 | } |
853 | | |
854 | | /* consumes 'to_add' */ |
855 | | static void |
856 | | expand_strv (gchar ***strv_ptr, |
857 | | gchar **to_add, |
858 | | gchar * const *blocklist) |
859 | 0 | { |
860 | 0 | guint strv_len, add_len; |
861 | 0 | gchar **strv; |
862 | 0 | guint i, j; |
863 | |
|
864 | 0 | if (!*strv_ptr) |
865 | 0 | { |
866 | 0 | *strv_ptr = to_add; |
867 | 0 | return; |
868 | 0 | } |
869 | | |
870 | 0 | strv = *strv_ptr; |
871 | 0 | strv_len = g_strv_length (strv); |
872 | 0 | add_len = g_strv_length (to_add); |
873 | 0 | strv = g_renew (gchar *, strv, strv_len + add_len + 1); |
874 | |
|
875 | 0 | for (i = 0; to_add[i]; i++) |
876 | 0 | { |
877 | | /* Don't add blocklisted strings */ |
878 | 0 | if (blocklist) |
879 | 0 | for (j = 0; blocklist[j]; j++) |
880 | 0 | if (g_str_equal (to_add[i], blocklist[j])) |
881 | 0 | goto no_add; |
882 | | |
883 | | /* Don't add duplicates already in the list */ |
884 | 0 | for (j = 0; j < strv_len; j++) |
885 | 0 | if (g_str_equal (to_add[i], strv[j])) |
886 | 0 | goto no_add; |
887 | | |
888 | 0 | strv[strv_len++] = to_add[i]; |
889 | 0 | continue; |
890 | | |
891 | 0 | no_add: |
892 | 0 | g_free (to_add[i]); |
893 | 0 | } |
894 | | |
895 | 0 | strv[strv_len] = NULL; |
896 | 0 | *strv_ptr = strv; |
897 | |
|
898 | 0 | g_free (to_add); |
899 | 0 | } |
900 | | |
901 | | static void |
902 | | desktop_file_dir_unindexed_read_mimeapps_list (DesktopFileDir *dir, |
903 | | const gchar *filename, |
904 | | const gchar *added_group, |
905 | | gboolean tweaks_permitted) |
906 | 0 | { |
907 | 0 | UnindexedMimeTweaks *tweaks; |
908 | 0 | char **desktop_file_ids; |
909 | 0 | GKeyFile *key_file; |
910 | 0 | gchar **mime_types; |
911 | 0 | int i; |
912 | |
|
913 | 0 | key_file = g_key_file_new (); |
914 | 0 | if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL)) |
915 | 0 | { |
916 | 0 | g_key_file_free (key_file); |
917 | 0 | return; |
918 | 0 | } |
919 | | |
920 | 0 | mime_types = g_key_file_get_keys (key_file, added_group, NULL, NULL); |
921 | |
|
922 | 0 | if G_UNLIKELY (mime_types != NULL && !tweaks_permitted) |
923 | 0 | { |
924 | 0 | g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific " |
925 | 0 | "mimeapps.list file may add or remove associations.", filename, added_group); |
926 | 0 | g_strfreev (mime_types); |
927 | 0 | mime_types = NULL; |
928 | 0 | } |
929 | |
|
930 | 0 | if (mime_types != NULL) |
931 | 0 | { |
932 | 0 | for (i = 0; mime_types[i] != NULL; i++) |
933 | 0 | { |
934 | 0 | desktop_file_ids = g_key_file_get_string_list (key_file, added_group, mime_types[i], NULL, NULL); |
935 | |
|
936 | 0 | if (desktop_file_ids) |
937 | 0 | { |
938 | 0 | tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]); |
939 | 0 | expand_strv (&tweaks->additions, desktop_file_ids, tweaks->removals); |
940 | 0 | } |
941 | 0 | } |
942 | |
|
943 | 0 | g_strfreev (mime_types); |
944 | 0 | } |
945 | |
|
946 | 0 | mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL); |
947 | |
|
948 | 0 | if G_UNLIKELY (mime_types != NULL && !tweaks_permitted) |
949 | 0 | { |
950 | 0 | g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific " |
951 | 0 | "mimeapps.list file may add or remove associations.", filename, REMOVED_ASSOCIATIONS_GROUP); |
952 | 0 | g_strfreev (mime_types); |
953 | 0 | mime_types = NULL; |
954 | 0 | } |
955 | |
|
956 | 0 | if (mime_types != NULL) |
957 | 0 | { |
958 | 0 | for (i = 0; mime_types[i] != NULL; i++) |
959 | 0 | { |
960 | 0 | desktop_file_ids = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP, mime_types[i], NULL, NULL); |
961 | |
|
962 | 0 | if (desktop_file_ids) |
963 | 0 | { |
964 | 0 | tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]); |
965 | 0 | expand_strv (&tweaks->removals, desktop_file_ids, tweaks->additions); |
966 | 0 | } |
967 | 0 | } |
968 | |
|
969 | 0 | g_strfreev (mime_types); |
970 | 0 | } |
971 | |
|
972 | 0 | mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL); |
973 | |
|
974 | 0 | if (mime_types != NULL) |
975 | 0 | { |
976 | 0 | for (i = 0; mime_types[i] != NULL; i++) |
977 | 0 | { |
978 | 0 | desktop_file_ids = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP, mime_types[i], NULL, NULL); |
979 | |
|
980 | 0 | if (desktop_file_ids) |
981 | 0 | { |
982 | 0 | tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]); |
983 | 0 | expand_strv (&tweaks->defaults, desktop_file_ids, NULL); |
984 | 0 | } |
985 | 0 | } |
986 | |
|
987 | 0 | g_strfreev (mime_types); |
988 | 0 | } |
989 | |
|
990 | 0 | g_key_file_free (key_file); |
991 | 0 | } |
992 | | |
993 | | static void |
994 | | desktop_file_dir_unindexed_read_mimeapps_lists (DesktopFileDir *dir) |
995 | 0 | { |
996 | 0 | const gchar * const *desktops; |
997 | 0 | gchar *filename; |
998 | 0 | gint i; |
999 | |
|
1000 | 0 | dir->mime_tweaks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_mime_tweaks); |
1001 | | |
1002 | | /* We process in order of precedence, using a blocklisting approach to |
1003 | | * avoid recording later instructions that conflict with ones we found |
1004 | | * earlier. |
1005 | | * |
1006 | | * We first start with the XDG_CURRENT_DESKTOP files, in precedence |
1007 | | * order. |
1008 | | */ |
1009 | 0 | desktops = get_lowercase_current_desktops (); |
1010 | 0 | for (i = 0; desktops[i]; i++) |
1011 | 0 | { |
1012 | 0 | filename = g_strdup_printf ("%s/%s-mimeapps.list", dir->path, desktops[i]); |
1013 | 0 | desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE); |
1014 | 0 | g_free (filename); |
1015 | 0 | } |
1016 | | |
1017 | | /* Next, the non-desktop-specific mimeapps.list */ |
1018 | 0 | filename = g_strdup_printf ("%s/mimeapps.list", dir->path); |
1019 | 0 | desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, TRUE); |
1020 | 0 | g_free (filename); |
1021 | | |
1022 | | /* The remaining files are only checked for in directories that might |
1023 | | * contain desktop files (ie: not the config dirs). |
1024 | | */ |
1025 | 0 | if (dir->is_config) |
1026 | 0 | return; |
1027 | | |
1028 | | /* We have 'defaults.list' which was only ever understood by GLib. It |
1029 | | * exists widely, but it has never been part of any spec and it should |
1030 | | * be treated as deprecated. This will be removed in a future |
1031 | | * version. |
1032 | | */ |
1033 | 0 | filename = g_strdup_printf ("%s/defaults.list", dir->path); |
1034 | 0 | desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE); |
1035 | 0 | g_free (filename); |
1036 | | |
1037 | | /* Finally, the mimeinfo.cache, which is just a cached copy of what we |
1038 | | * would find in the MimeTypes= lines of all of the desktop files. |
1039 | | */ |
1040 | 0 | filename = g_strdup_printf ("%s/mimeinfo.cache", dir->path); |
1041 | 0 | desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, MIME_CACHE_GROUP, TRUE); |
1042 | 0 | g_free (filename); |
1043 | 0 | } |
1044 | | |
1045 | | static void |
1046 | | desktop_file_dir_unindexed_init (DesktopFileDir *dir) |
1047 | 0 | { |
1048 | 0 | if (!dir->is_config) |
1049 | 0 | get_apps_from_dir (&dir->app_names, dir->path, ""); |
1050 | |
|
1051 | 0 | desktop_file_dir_unindexed_read_mimeapps_lists (dir); |
1052 | 0 | } |
1053 | | |
1054 | | static GDesktopAppInfo * |
1055 | | g_desktop_app_info_new_from_filename_unlocked (const char *filename) |
1056 | 0 | { |
1057 | 0 | GDesktopAppInfo *info = NULL; |
1058 | |
|
1059 | 0 | info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL); |
1060 | |
|
1061 | 0 | if (!g_desktop_app_info_load_file (info)) |
1062 | 0 | g_clear_object (&info); |
1063 | |
|
1064 | 0 | return info; |
1065 | 0 | } |
1066 | | |
1067 | | static GDesktopAppInfo * |
1068 | | desktop_file_dir_unindexed_get_app (DesktopFileDir *dir, |
1069 | | const gchar *desktop_id) |
1070 | 0 | { |
1071 | 0 | const gchar *filename; |
1072 | |
|
1073 | 0 | filename = g_hash_table_lookup (dir->app_names, desktop_id); |
1074 | |
|
1075 | 0 | if (!filename) |
1076 | 0 | return NULL; |
1077 | | |
1078 | 0 | return g_desktop_app_info_new_from_filename_unlocked (filename); |
1079 | 0 | } |
1080 | | |
1081 | | static void |
1082 | | desktop_file_dir_unindexed_get_all (DesktopFileDir *dir, |
1083 | | GHashTable *apps) |
1084 | 0 | { |
1085 | 0 | GHashTableIter iter; |
1086 | 0 | gpointer app_name; |
1087 | 0 | gpointer filename; |
1088 | |
|
1089 | 0 | if (dir->app_names == NULL) |
1090 | 0 | return; |
1091 | | |
1092 | 0 | g_hash_table_iter_init (&iter, dir->app_names); |
1093 | 0 | while (g_hash_table_iter_next (&iter, &app_name, &filename)) |
1094 | 0 | { |
1095 | 0 | if (desktop_file_dir_app_name_is_masked (dir, app_name)) |
1096 | 0 | continue; |
1097 | | |
1098 | 0 | add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename_unlocked (filename)); |
1099 | 0 | } |
1100 | 0 | } |
1101 | | |
1102 | | typedef struct _MemoryIndexEntry MemoryIndexEntry; |
1103 | | typedef GHashTable MemoryIndex; |
1104 | | |
1105 | | struct _MemoryIndexEntry |
1106 | | { |
1107 | | const gchar *app_name; /* pointer to the hashtable key */ |
1108 | | gint match_category; /* the entry key (Name, Exec, ...) */ |
1109 | | gint token_pos; /* the position of the token in the field */ |
1110 | | MemoryIndexEntry *next; |
1111 | | }; |
1112 | | |
1113 | | static void |
1114 | | memory_index_entry_free (gpointer data) |
1115 | 0 | { |
1116 | 0 | MemoryIndexEntry *mie = data; |
1117 | |
|
1118 | 0 | while (mie) |
1119 | 0 | { |
1120 | 0 | MemoryIndexEntry *next = mie->next; |
1121 | |
|
1122 | 0 | g_slice_free (MemoryIndexEntry, mie); |
1123 | 0 | mie = next; |
1124 | 0 | } |
1125 | 0 | } |
1126 | | |
1127 | | static void |
1128 | | memory_index_add_token (MemoryIndex *mi, |
1129 | | const gchar *token, |
1130 | | gint match_category, |
1131 | | gint token_pos, |
1132 | | const gchar *app_name) |
1133 | 0 | { |
1134 | 0 | MemoryIndexEntry *mie, *first; |
1135 | |
|
1136 | 0 | mie = g_slice_new (MemoryIndexEntry); |
1137 | 0 | mie->app_name = app_name; |
1138 | 0 | mie->match_category = match_category; |
1139 | 0 | mie->token_pos = token_pos; |
1140 | |
|
1141 | 0 | first = g_hash_table_lookup (mi, token); |
1142 | |
|
1143 | 0 | if (first) |
1144 | 0 | { |
1145 | 0 | mie->next = first->next; |
1146 | 0 | first->next = mie; |
1147 | 0 | } |
1148 | 0 | else |
1149 | 0 | { |
1150 | 0 | mie->next = NULL; |
1151 | 0 | g_hash_table_insert (mi, g_strdup (token), mie); |
1152 | 0 | } |
1153 | 0 | } |
1154 | | |
1155 | | static void |
1156 | | memory_index_add_string (MemoryIndex *mi, |
1157 | | const gchar *string, |
1158 | | gint match_category, |
1159 | | const gchar *app_name) |
1160 | 0 | { |
1161 | 0 | gchar **tokens, **alternates; |
1162 | 0 | gint i, n; |
1163 | |
|
1164 | 0 | tokens = g_str_tokenize_and_fold (string, NULL, &alternates); |
1165 | |
|
1166 | 0 | for (i = 0; tokens[i]; i++) |
1167 | 0 | memory_index_add_token (mi, tokens[i], match_category, i, app_name); |
1168 | |
|
1169 | 0 | n = i; |
1170 | 0 | for (i = 0; alternates[i]; i++) |
1171 | 0 | memory_index_add_token (mi, alternates[i], match_category, n + i, app_name); |
1172 | |
|
1173 | 0 | g_strfreev (alternates); |
1174 | 0 | g_strfreev (tokens); |
1175 | 0 | } |
1176 | | |
1177 | | static MemoryIndex * |
1178 | | memory_index_new (void) |
1179 | 0 | { |
1180 | 0 | return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, memory_index_entry_free); |
1181 | 0 | } |
1182 | | |
1183 | | static void |
1184 | | desktop_file_dir_unindexed_setup_search (DesktopFileDir *dir) |
1185 | 0 | { |
1186 | 0 | GHashTableIter iter; |
1187 | 0 | gpointer app, path; |
1188 | |
|
1189 | 0 | dir->memory_index = memory_index_new (); |
1190 | 0 | dir->memory_implementations = memory_index_new (); |
1191 | | |
1192 | | /* Nothing to search? */ |
1193 | 0 | if (dir->app_names == NULL) |
1194 | 0 | return; |
1195 | | |
1196 | 0 | g_hash_table_iter_init (&iter, dir->app_names); |
1197 | 0 | while (g_hash_table_iter_next (&iter, &app, &path)) |
1198 | 0 | { |
1199 | 0 | GKeyFile *key_file; |
1200 | |
|
1201 | 0 | if (desktop_file_dir_app_name_is_masked (dir, app)) |
1202 | 0 | continue; |
1203 | | |
1204 | 0 | key_file = g_key_file_new (); |
1205 | |
|
1206 | 0 | if (g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL) && |
1207 | 0 | !g_key_file_get_boolean (key_file, "Desktop Entry", "Hidden", NULL)) |
1208 | 0 | { |
1209 | | /* Index the interesting keys... */ |
1210 | 0 | gchar **implements; |
1211 | 0 | gsize i; |
1212 | |
|
1213 | 0 | for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++) |
1214 | 0 | { |
1215 | 0 | const gchar *value; |
1216 | 0 | gchar *raw; |
1217 | |
|
1218 | 0 | if (!desktop_key_match_category[i]) |
1219 | 0 | continue; |
1220 | | |
1221 | 0 | raw = g_key_file_get_locale_string (key_file, "Desktop Entry", desktop_key_get_name (i), NULL, NULL); |
1222 | 0 | value = raw; |
1223 | |
|
1224 | 0 | if (i == DESKTOP_KEY_Exec && raw != NULL) |
1225 | 0 | { |
1226 | | /* Special handling: only match basename of first field */ |
1227 | 0 | gchar *space; |
1228 | 0 | gchar *slash; |
1229 | | |
1230 | | /* Remove extra arguments, if any */ |
1231 | 0 | space = raw + strcspn (raw, " \t\n"); /* IFS */ |
1232 | 0 | *space = '\0'; |
1233 | | |
1234 | | /* Skip the pathname, if any */ |
1235 | 0 | if ((slash = strrchr (raw, '/'))) |
1236 | 0 | value = slash + 1; |
1237 | | |
1238 | | /* Don't match on blocklisted binaries like interpreters */ |
1239 | 0 | if (g_strv_contains (exec_key_match_blocklist, value)) |
1240 | 0 | value = NULL; |
1241 | 0 | } |
1242 | |
|
1243 | 0 | if (value) |
1244 | 0 | memory_index_add_string (dir->memory_index, value, desktop_key_match_category[i], app); |
1245 | |
|
1246 | 0 | g_free (raw); |
1247 | 0 | } |
1248 | | |
1249 | | /* Make note of the Implements= line */ |
1250 | 0 | implements = g_key_file_get_string_list (key_file, "Desktop Entry", "Implements", NULL, NULL); |
1251 | 0 | for (i = 0; implements && implements[i]; i++) |
1252 | 0 | memory_index_add_token (dir->memory_implementations, implements[i], i, 0, app); |
1253 | 0 | g_strfreev (implements); |
1254 | 0 | } |
1255 | |
|
1256 | 0 | g_key_file_free (key_file); |
1257 | 0 | } |
1258 | 0 | } |
1259 | | |
1260 | | static void |
1261 | | desktop_file_dir_unindexed_search (DesktopFileDir *dir, |
1262 | | const gchar *search_token) |
1263 | 0 | { |
1264 | 0 | GHashTableIter iter; |
1265 | 0 | gpointer key, value; |
1266 | |
|
1267 | 0 | g_assert (search_token != NULL); |
1268 | | |
1269 | 0 | if (!dir->memory_index) |
1270 | 0 | desktop_file_dir_unindexed_setup_search (dir); |
1271 | |
|
1272 | 0 | g_hash_table_iter_init (&iter, dir->memory_index); |
1273 | 0 | while (g_hash_table_iter_next (&iter, &key, &value)) |
1274 | 0 | { |
1275 | 0 | MemoryIndexEntry *mie = value; |
1276 | 0 | const char *p; |
1277 | 0 | MatchType match_type; |
1278 | | |
1279 | | /* strstr(haystack, needle) returns haystack if needle is empty, so if |
1280 | | * needle is not empty and return value equals to haystack means a prefix |
1281 | | * match. |
1282 | | */ |
1283 | 0 | p = strstr (key, search_token); |
1284 | 0 | if (p == NULL) |
1285 | 0 | continue; |
1286 | 0 | else if (p == key && *search_token != '\0') |
1287 | 0 | match_type = MATCH_TYPE_PREFIX; |
1288 | 0 | else |
1289 | 0 | match_type = MATCH_TYPE_SUBSTRING; |
1290 | | |
1291 | 0 | while (mie) |
1292 | 0 | { |
1293 | 0 | add_token_result (mie->app_name, mie->match_category, match_type, mie->token_pos); |
1294 | 0 | mie = mie->next; |
1295 | 0 | } |
1296 | 0 | } |
1297 | 0 | } |
1298 | | |
1299 | | static gboolean |
1300 | | array_contains (GPtrArray *array, |
1301 | | const gchar *str) |
1302 | 0 | { |
1303 | 0 | guint i; |
1304 | |
|
1305 | 0 | for (i = 0; i < array->len; i++) |
1306 | 0 | if (g_str_equal (array->pdata[i], str)) |
1307 | 0 | return TRUE; |
1308 | | |
1309 | 0 | return FALSE; |
1310 | 0 | } |
1311 | | |
1312 | | static void |
1313 | | desktop_file_dir_unindexed_mime_lookup (DesktopFileDir *dir, |
1314 | | const gchar *mime_type, |
1315 | | GPtrArray *hits, |
1316 | | GPtrArray *blocklist) |
1317 | 0 | { |
1318 | 0 | UnindexedMimeTweaks *tweaks; |
1319 | 0 | gint i; |
1320 | |
|
1321 | 0 | tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type); |
1322 | |
|
1323 | 0 | if (!tweaks) |
1324 | 0 | return; |
1325 | | |
1326 | 0 | if (tweaks->additions) |
1327 | 0 | { |
1328 | 0 | for (i = 0; tweaks->additions[i]; i++) |
1329 | 0 | { |
1330 | 0 | gchar *app_name = tweaks->additions[i]; |
1331 | |
|
1332 | 0 | if (!desktop_file_dir_app_name_is_masked (dir, app_name) && |
1333 | 0 | !array_contains (blocklist, app_name) && !array_contains (hits, app_name)) |
1334 | 0 | g_ptr_array_add (hits, app_name); |
1335 | 0 | } |
1336 | 0 | } |
1337 | |
|
1338 | 0 | if (tweaks->removals) |
1339 | 0 | { |
1340 | 0 | for (i = 0; tweaks->removals[i]; i++) |
1341 | 0 | { |
1342 | 0 | gchar *app_name = tweaks->removals[i]; |
1343 | |
|
1344 | 0 | if (!desktop_file_dir_app_name_is_masked (dir, app_name) && |
1345 | 0 | !array_contains (blocklist, app_name) && !array_contains (hits, app_name)) |
1346 | 0 | g_ptr_array_add (blocklist, app_name); |
1347 | 0 | } |
1348 | 0 | } |
1349 | 0 | } |
1350 | | |
1351 | | static void |
1352 | | desktop_file_dir_unindexed_default_lookup (DesktopFileDir *dir, |
1353 | | const gchar *mime_type, |
1354 | | GPtrArray *results) |
1355 | 0 | { |
1356 | 0 | UnindexedMimeTweaks *tweaks; |
1357 | 0 | gint i; |
1358 | |
|
1359 | 0 | tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type); |
1360 | |
|
1361 | 0 | if (!tweaks || !tweaks->defaults) |
1362 | 0 | return; |
1363 | | |
1364 | 0 | for (i = 0; tweaks->defaults[i]; i++) |
1365 | 0 | { |
1366 | 0 | gchar *app_name = tweaks->defaults[i]; |
1367 | |
|
1368 | 0 | if (!array_contains (results, app_name)) |
1369 | 0 | g_ptr_array_add (results, app_name); |
1370 | 0 | } |
1371 | 0 | } |
1372 | | |
1373 | | static void |
1374 | | desktop_file_dir_unindexed_get_implementations (DesktopFileDir *dir, |
1375 | | GList **results, |
1376 | | const gchar *interface) |
1377 | 0 | { |
1378 | 0 | MemoryIndexEntry *mie; |
1379 | |
|
1380 | 0 | if (!dir->memory_index) |
1381 | 0 | desktop_file_dir_unindexed_setup_search (dir); |
1382 | |
|
1383 | 0 | for (mie = g_hash_table_lookup (dir->memory_implementations, interface); mie; mie = mie->next) |
1384 | 0 | *results = g_list_prepend (*results, g_strdup (mie->app_name)); |
1385 | 0 | } |
1386 | | |
1387 | | /* DesktopFileDir "API" {{{2 */ |
1388 | | |
1389 | | /*< internal > |
1390 | | * desktop_file_dir_new: |
1391 | | * @data_dir: an XDG_DATA_DIR |
1392 | | * |
1393 | | * Creates a #DesktopFileDir for the corresponding @data_dir. |
1394 | | */ |
1395 | | static DesktopFileDir * |
1396 | | desktop_file_dir_new (const gchar *data_dir) |
1397 | 0 | { |
1398 | 0 | DesktopFileDir *dir = g_new0 (DesktopFileDir, 1); |
1399 | |
|
1400 | 0 | g_atomic_ref_count_init (&dir->ref_count); |
1401 | 0 | dir->path = g_build_filename (data_dir, "applications", NULL); |
1402 | |
|
1403 | 0 | return g_steal_pointer (&dir); |
1404 | 0 | } |
1405 | | |
1406 | | /*< internal > |
1407 | | * desktop_file_dir_new_for_config: |
1408 | | * @config_dir: an XDG_CONFIG_DIR |
1409 | | * |
1410 | | * Just the same as desktop_file_dir_new() except that it does not |
1411 | | * add the "applications" directory. It also marks the directory as |
1412 | | * config-only, which prevents us from attempting to find desktop files |
1413 | | * here. |
1414 | | */ |
1415 | | static DesktopFileDir * |
1416 | | desktop_file_dir_new_for_config (const gchar *config_dir) |
1417 | 0 | { |
1418 | 0 | DesktopFileDir *dir = g_new0 (DesktopFileDir, 1); |
1419 | |
|
1420 | 0 | g_atomic_ref_count_init (&dir->ref_count); |
1421 | 0 | dir->path = g_strdup (config_dir); |
1422 | 0 | dir->is_config = TRUE; |
1423 | |
|
1424 | 0 | return g_steal_pointer (&dir); |
1425 | 0 | } |
1426 | | |
1427 | | /*< internal > |
1428 | | * desktop_file_dir_reset: |
1429 | | * @dir: a #DesktopFileDir |
1430 | | * |
1431 | | * Cleans up @dir, releasing most resources that it was using. |
1432 | | */ |
1433 | | static void |
1434 | | desktop_file_dir_reset (DesktopFileDir *dir) |
1435 | 0 | { |
1436 | 0 | if (dir->alternatively_watching) |
1437 | 0 | { |
1438 | 0 | g_free (dir->alternatively_watching); |
1439 | 0 | dir->alternatively_watching = NULL; |
1440 | 0 | } |
1441 | |
|
1442 | 0 | if (dir->monitor) |
1443 | 0 | { |
1444 | 0 | g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir); |
1445 | 0 | g_file_monitor_cancel (dir->monitor); |
1446 | 0 | g_object_unref (dir->monitor); |
1447 | 0 | dir->monitor = NULL; |
1448 | 0 | } |
1449 | |
|
1450 | 0 | if (dir->app_names) |
1451 | 0 | { |
1452 | 0 | g_hash_table_unref (dir->app_names); |
1453 | 0 | dir->app_names = NULL; |
1454 | 0 | } |
1455 | |
|
1456 | 0 | if (dir->memory_index) |
1457 | 0 | { |
1458 | 0 | g_hash_table_unref (dir->memory_index); |
1459 | 0 | dir->memory_index = NULL; |
1460 | 0 | } |
1461 | |
|
1462 | 0 | if (dir->mime_tweaks) |
1463 | 0 | { |
1464 | 0 | g_hash_table_unref (dir->mime_tweaks); |
1465 | 0 | dir->mime_tweaks = NULL; |
1466 | 0 | } |
1467 | |
|
1468 | 0 | if (dir->memory_implementations) |
1469 | 0 | { |
1470 | 0 | g_hash_table_unref (dir->memory_implementations); |
1471 | 0 | dir->memory_implementations = NULL; |
1472 | 0 | } |
1473 | |
|
1474 | 0 | dir->is_setup = FALSE; |
1475 | 0 | } |
1476 | | |
1477 | | static void |
1478 | | closure_notify_cb (gpointer data, |
1479 | | GClosure *closure) |
1480 | 0 | { |
1481 | 0 | DesktopFileDir *dir = data; |
1482 | 0 | desktop_file_dir_unref (dir); |
1483 | 0 | } |
1484 | | |
1485 | | /*< internal > |
1486 | | * desktop_file_dir_init: |
1487 | | * @dir: a #DesktopFileDir |
1488 | | * |
1489 | | * Does initial setup for @dir |
1490 | | * |
1491 | | * You should only call this if @dir is not already setup. |
1492 | | */ |
1493 | | static void |
1494 | | desktop_file_dir_init (DesktopFileDir *dir) |
1495 | 0 | { |
1496 | 0 | const gchar *watch_dir; |
1497 | |
|
1498 | 0 | g_assert (!dir->is_setup); |
1499 | | |
1500 | 0 | g_assert (!dir->alternatively_watching); |
1501 | 0 | g_assert (!dir->monitor); |
1502 | | |
1503 | 0 | dir->alternatively_watching = desktop_file_dir_get_alternative_dir (dir); |
1504 | 0 | watch_dir = dir->alternatively_watching ? dir->alternatively_watching : dir->path; |
1505 | | |
1506 | | /* There is a very thin race here if the watch_dir has been _removed_ |
1507 | | * between when we checked for it and when we establish the watch. |
1508 | | * Removes probably don't happen in usual operation, and even if it |
1509 | | * does (and we catch the unlikely race), the only degradation is that |
1510 | | * we will fall back to polling. |
1511 | | */ |
1512 | 0 | dir->monitor = g_local_file_monitor_new_in_worker (watch_dir, TRUE, G_FILE_MONITOR_NONE, |
1513 | 0 | desktop_file_dir_changed, |
1514 | 0 | desktop_file_dir_ref (dir), |
1515 | 0 | closure_notify_cb, NULL); |
1516 | |
|
1517 | 0 | desktop_file_dir_unindexed_init (dir); |
1518 | |
|
1519 | 0 | dir->is_setup = TRUE; |
1520 | 0 | } |
1521 | | |
1522 | | /*< internal > |
1523 | | * desktop_file_dir_get_app: |
1524 | | * @dir: a DesktopFileDir |
1525 | | * @desktop_id: the desktop ID to load |
1526 | | * |
1527 | | * Creates the #GDesktopAppInfo for the given @desktop_id if it exists |
1528 | | * within @dir, even if it is hidden. |
1529 | | * |
1530 | | * This function does not check if @desktop_id would be masked by a |
1531 | | * directory with higher precedence. The caller must do so. |
1532 | | */ |
1533 | | static GDesktopAppInfo * |
1534 | | desktop_file_dir_get_app (DesktopFileDir *dir, |
1535 | | const gchar *desktop_id) |
1536 | 0 | { |
1537 | 0 | if (!dir->app_names) |
1538 | 0 | return NULL; |
1539 | | |
1540 | 0 | return desktop_file_dir_unindexed_get_app (dir, desktop_id); |
1541 | 0 | } |
1542 | | |
1543 | | /*< internal > |
1544 | | * desktop_file_dir_get_all: |
1545 | | * @dir: a DesktopFileDir |
1546 | | * @apps: a #GHashTable<string, GDesktopAppInfo> |
1547 | | * |
1548 | | * Loads all desktop files in @dir and adds them to @apps, careful to |
1549 | | * ensure we don't add any files masked by a similarly-named file in a |
1550 | | * higher-precedence directory. |
1551 | | */ |
1552 | | static void |
1553 | | desktop_file_dir_get_all (DesktopFileDir *dir, |
1554 | | GHashTable *apps) |
1555 | 0 | { |
1556 | 0 | desktop_file_dir_unindexed_get_all (dir, apps); |
1557 | 0 | } |
1558 | | |
1559 | | /*< internal > |
1560 | | * desktop_file_dir_mime_lookup: |
1561 | | * @dir: a #DesktopFileDir |
1562 | | * @mime_type: the mime type to look up |
1563 | | * @hits: the array to store the hits |
1564 | | * @blocklist: the array to store the blocklist |
1565 | | * |
1566 | | * Does a lookup of a mimetype against one desktop file directory, |
1567 | | * recording any hits and blocklisting and "Removed" associations (so |
1568 | | * later directories don't record them as hits). |
1569 | | * |
1570 | | * The items added to @hits are duplicated, but the ones in @blocklist |
1571 | | * are weak pointers. This facilitates simply freeing the blocklist |
1572 | | * (which is only used for internal bookkeeping) but using the pdata of |
1573 | | * @hits as the result of the operation. |
1574 | | */ |
1575 | | static void |
1576 | | desktop_file_dir_mime_lookup (DesktopFileDir *dir, |
1577 | | const gchar *mime_type, |
1578 | | GPtrArray *hits, |
1579 | | GPtrArray *blocklist) |
1580 | 0 | { |
1581 | 0 | desktop_file_dir_unindexed_mime_lookup (dir, mime_type, hits, blocklist); |
1582 | 0 | } |
1583 | | |
1584 | | /*< internal > |
1585 | | * desktop_file_dir_default_lookup: |
1586 | | * @dir: a #DesktopFileDir |
1587 | | * @mime_type: the mime type to look up |
1588 | | * @results: an array to store the results in |
1589 | | * |
1590 | | * Collects the "default" applications for a given mime type from @dir. |
1591 | | */ |
1592 | | static void |
1593 | | desktop_file_dir_default_lookup (DesktopFileDir *dir, |
1594 | | const gchar *mime_type, |
1595 | | GPtrArray *results) |
1596 | 0 | { |
1597 | 0 | desktop_file_dir_unindexed_default_lookup (dir, mime_type, results); |
1598 | 0 | } |
1599 | | |
1600 | | /*< internal > |
1601 | | * desktop_file_dir_search: |
1602 | | * @dir: a #DesktopFileDir |
1603 | | * @term: a normalised and casefolded search term |
1604 | | * |
1605 | | * Finds the names of applications in @dir that match @term. |
1606 | | */ |
1607 | | static void |
1608 | | desktop_file_dir_search (DesktopFileDir *dir, |
1609 | | const gchar *search_token) |
1610 | 0 | { |
1611 | 0 | desktop_file_dir_unindexed_search (dir, search_token); |
1612 | 0 | } |
1613 | | |
1614 | | static void |
1615 | | desktop_file_dir_get_implementations (DesktopFileDir *dir, |
1616 | | GList **results, |
1617 | | const gchar *interface) |
1618 | 0 | { |
1619 | 0 | desktop_file_dir_unindexed_get_implementations (dir, results, interface); |
1620 | 0 | } |
1621 | | |
1622 | | /* Lock/unlock and global setup API {{{2 */ |
1623 | | |
1624 | | static void |
1625 | | desktop_file_dirs_lock (void) |
1626 | 0 | { |
1627 | 0 | guint i; |
1628 | 0 | const gchar *user_config_dir = g_get_user_config_dir (); |
1629 | |
|
1630 | 0 | g_mutex_lock (&desktop_file_dir_lock); |
1631 | | |
1632 | | /* If the XDG dirs configuration has changed (expected only during tests), |
1633 | | * clear and reload the state. */ |
1634 | 0 | if (desktop_file_dirs_config_dir != NULL && |
1635 | 0 | g_strcmp0 (desktop_file_dirs_config_dir, user_config_dir) != 0) |
1636 | 0 | { |
1637 | 0 | g_debug ("%s: Resetting desktop app info dirs from %s to %s", |
1638 | 0 | G_STRFUNC, desktop_file_dirs_config_dir, user_config_dir); |
1639 | |
|
1640 | 0 | g_ptr_array_set_size (desktop_file_dirs, 0); |
1641 | 0 | g_clear_pointer (&desktop_file_dir_user_config, desktop_file_dir_unref); |
1642 | 0 | g_clear_pointer (&desktop_file_dir_user_data, desktop_file_dir_unref); |
1643 | 0 | } |
1644 | |
|
1645 | 0 | if (desktop_file_dirs == NULL || desktop_file_dirs->len == 0) |
1646 | 0 | { |
1647 | 0 | const char * const *dirs; |
1648 | 0 | gint i; |
1649 | |
|
1650 | 0 | if (desktop_file_dirs == NULL) |
1651 | 0 | desktop_file_dirs = g_ptr_array_new_with_free_func ((GDestroyNotify) desktop_file_dir_unref); |
1652 | | |
1653 | | /* First, the configs. Highest priority: the user's ~/.config */ |
1654 | 0 | desktop_file_dir_user_config = desktop_file_dir_new_for_config (user_config_dir); |
1655 | 0 | g_ptr_array_add (desktop_file_dirs, desktop_file_dir_ref (desktop_file_dir_user_config)); |
1656 | | |
1657 | | /* Next, the system configs (/etc/xdg, and so on). */ |
1658 | 0 | dirs = g_get_system_config_dirs (); |
1659 | 0 | for (i = 0; dirs[i]; i++) |
1660 | 0 | g_ptr_array_add (desktop_file_dirs, desktop_file_dir_new_for_config (dirs[i])); |
1661 | | |
1662 | | /* Now the data. Highest priority: the user's ~/.local/share/applications */ |
1663 | 0 | desktop_file_dir_user_data = desktop_file_dir_new (g_get_user_data_dir ()); |
1664 | 0 | g_ptr_array_add (desktop_file_dirs, desktop_file_dir_ref (desktop_file_dir_user_data)); |
1665 | | |
1666 | | /* Following that, XDG_DATA_DIRS/applications, in order */ |
1667 | 0 | dirs = g_get_system_data_dirs (); |
1668 | 0 | for (i = 0; dirs[i]; i++) |
1669 | 0 | g_ptr_array_add (desktop_file_dirs, desktop_file_dir_new (dirs[i])); |
1670 | | |
1671 | | /* The list of directories will never change after this, unless |
1672 | | * g_get_user_config_dir() changes due to %G_TEST_OPTION_ISOLATE_DIRS. */ |
1673 | 0 | desktop_file_dirs_config_dir = user_config_dir; |
1674 | 0 | } |
1675 | |
|
1676 | 0 | for (i = 0; i < desktop_file_dirs->len; i++) |
1677 | 0 | if (!((DesktopFileDir *) g_ptr_array_index (desktop_file_dirs, i))->is_setup) |
1678 | 0 | desktop_file_dir_init (g_ptr_array_index (desktop_file_dirs, i)); |
1679 | 0 | } |
1680 | | |
1681 | | static void |
1682 | | desktop_file_dirs_unlock (void) |
1683 | 0 | { |
1684 | 0 | g_mutex_unlock (&desktop_file_dir_lock); |
1685 | 0 | } |
1686 | | |
1687 | | static void |
1688 | | desktop_file_dirs_invalidate_user_config (void) |
1689 | 0 | { |
1690 | 0 | g_mutex_lock (&desktop_file_dir_lock); |
1691 | |
|
1692 | 0 | if (desktop_file_dir_user_config != NULL) |
1693 | 0 | desktop_file_dir_reset (desktop_file_dir_user_config); |
1694 | |
|
1695 | 0 | g_mutex_unlock (&desktop_file_dir_lock); |
1696 | 0 | } |
1697 | | |
1698 | | static void |
1699 | | desktop_file_dirs_invalidate_user_data (void) |
1700 | 0 | { |
1701 | 0 | g_mutex_lock (&desktop_file_dir_lock); |
1702 | |
|
1703 | 0 | if (desktop_file_dir_user_data != NULL) |
1704 | 0 | desktop_file_dir_reset (desktop_file_dir_user_data); |
1705 | |
|
1706 | 0 | g_mutex_unlock (&desktop_file_dir_lock); |
1707 | 0 | } |
1708 | | |
1709 | | /* GDesktopAppInfo implementation {{{1 */ |
1710 | | /* GObject implementation {{{2 */ |
1711 | | static void |
1712 | | g_desktop_app_info_finalize (GObject *object) |
1713 | 0 | { |
1714 | 0 | GDesktopAppInfo *info; |
1715 | |
|
1716 | 0 | info = G_DESKTOP_APP_INFO (object); |
1717 | |
|
1718 | 0 | g_free (info->desktop_id); |
1719 | 0 | g_free (info->filename); |
1720 | |
|
1721 | 0 | if (info->keyfile) |
1722 | 0 | g_key_file_unref (info->keyfile); |
1723 | |
|
1724 | 0 | g_free (info->name); |
1725 | 0 | g_free (info->generic_name); |
1726 | 0 | g_free (info->fullname); |
1727 | 0 | g_free (info->comment); |
1728 | 0 | g_free (info->icon_name); |
1729 | 0 | if (info->icon) |
1730 | 0 | g_object_unref (info->icon); |
1731 | 0 | g_strfreev (info->keywords); |
1732 | 0 | g_strfreev (info->only_show_in); |
1733 | 0 | g_strfreev (info->not_show_in); |
1734 | 0 | g_free (info->try_exec); |
1735 | 0 | g_free (info->exec); |
1736 | 0 | g_free (info->binary); |
1737 | 0 | g_free (info->path); |
1738 | 0 | g_free (info->categories); |
1739 | 0 | g_free (info->startup_wm_class); |
1740 | 0 | g_strfreev (info->mime_types); |
1741 | 0 | g_free (info->app_id); |
1742 | 0 | g_strfreev (info->actions); |
1743 | |
|
1744 | 0 | G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object); |
1745 | 0 | } |
1746 | | |
1747 | | static void |
1748 | | g_desktop_app_info_set_property (GObject *object, |
1749 | | guint prop_id, |
1750 | | const GValue *value, |
1751 | | GParamSpec *pspec) |
1752 | 0 | { |
1753 | 0 | GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object); |
1754 | |
|
1755 | 0 | switch (prop_id) |
1756 | 0 | { |
1757 | 0 | case PROP_FILENAME: |
1758 | 0 | self->filename = g_value_dup_string (value); |
1759 | 0 | break; |
1760 | | |
1761 | 0 | default: |
1762 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
1763 | 0 | break; |
1764 | 0 | } |
1765 | 0 | } |
1766 | | |
1767 | | static void |
1768 | | g_desktop_app_info_get_property (GObject *object, |
1769 | | guint prop_id, |
1770 | | GValue *value, |
1771 | | GParamSpec *pspec) |
1772 | 0 | { |
1773 | 0 | GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object); |
1774 | |
|
1775 | 0 | switch (prop_id) |
1776 | 0 | { |
1777 | 0 | case PROP_FILENAME: |
1778 | 0 | g_value_set_string (value, self->filename); |
1779 | 0 | break; |
1780 | 0 | default: |
1781 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
1782 | 0 | break; |
1783 | 0 | } |
1784 | 0 | } |
1785 | | |
1786 | | static void |
1787 | | g_desktop_app_info_class_init (GDesktopAppInfoClass *klass) |
1788 | 0 | { |
1789 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
1790 | |
|
1791 | 0 | gobject_class->get_property = g_desktop_app_info_get_property; |
1792 | 0 | gobject_class->set_property = g_desktop_app_info_set_property; |
1793 | 0 | gobject_class->finalize = g_desktop_app_info_finalize; |
1794 | | |
1795 | | /** |
1796 | | * GDesktopAppInfo:filename: |
1797 | | * |
1798 | | * The origin filename of this [class@GioUnix.DesktopAppInfo] |
1799 | | */ |
1800 | 0 | g_object_class_install_property (gobject_class, |
1801 | 0 | PROP_FILENAME, |
1802 | 0 | g_param_spec_string ("filename", NULL, NULL, NULL, |
1803 | 0 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
1804 | 0 | } |
1805 | | |
1806 | | static void |
1807 | | g_desktop_app_info_init (GDesktopAppInfo *local) |
1808 | 0 | { |
1809 | 0 | } |
1810 | | |
1811 | | /* Construction... {{{2 */ |
1812 | | |
1813 | | /*< internal > |
1814 | | * binary_from_exec: |
1815 | | * @exec: an exec line |
1816 | | * |
1817 | | * Returns the first word in an exec line (ie: the binary name). |
1818 | | * |
1819 | | * If @exec is " progname --foo %F" then returns "progname". |
1820 | | */ |
1821 | | static char * |
1822 | | binary_from_exec (const char *exec) |
1823 | 0 | { |
1824 | 0 | const char *p, *start; |
1825 | |
|
1826 | 0 | p = exec; |
1827 | 0 | while (*p == ' ') |
1828 | 0 | p++; |
1829 | 0 | start = p; |
1830 | 0 | while (*p != ' ' && *p != 0) |
1831 | 0 | p++; |
1832 | |
|
1833 | 0 | return g_strndup (start, p - start); |
1834 | 0 | } |
1835 | | |
1836 | | /*< internal > |
1837 | | * g_desktop_app_info_get_desktop_id_for_filename |
1838 | | * @self: #GDesktopAppInfo to get desktop id of |
1839 | | * |
1840 | | * Tries to find the desktop ID for a particular `.desktop` filename, as per the |
1841 | | * [Desktop Entry Specification](https://specifications.freedesktop.org/desktop- |
1842 | | * entry-spec/desktop-entry-spec-latest.html#desktop-file-id). |
1843 | | * |
1844 | | * Returns: desktop id or basename if filename is unknown. |
1845 | | */ |
1846 | | static char * |
1847 | | g_desktop_app_info_get_desktop_id_for_filename (GDesktopAppInfo *self) |
1848 | 0 | { |
1849 | 0 | guint i; |
1850 | 0 | gchar *desktop_id = NULL; |
1851 | |
|
1852 | 0 | g_return_val_if_fail (self->filename != NULL, NULL); |
1853 | | |
1854 | 0 | for (i = 0; i < desktop_file_dirs->len; i++) |
1855 | 0 | { |
1856 | 0 | DesktopFileDir *dir = g_ptr_array_index (desktop_file_dirs, i); |
1857 | 0 | GHashTable *app_names; |
1858 | 0 | GHashTableIter iter; |
1859 | 0 | gpointer key, value; |
1860 | |
|
1861 | 0 | app_names = dir->app_names; |
1862 | |
|
1863 | 0 | if (!app_names) |
1864 | 0 | continue; |
1865 | | |
1866 | 0 | g_hash_table_iter_init (&iter, app_names); |
1867 | 0 | while (g_hash_table_iter_next (&iter, &key, &value)) |
1868 | 0 | { |
1869 | 0 | if (!strcmp (value, self->filename)) |
1870 | 0 | { |
1871 | 0 | desktop_id = g_strdup (key); |
1872 | 0 | break; |
1873 | 0 | } |
1874 | 0 | } |
1875 | |
|
1876 | 0 | if (desktop_id) |
1877 | 0 | break; |
1878 | 0 | } |
1879 | |
|
1880 | 0 | if (!desktop_id) |
1881 | 0 | desktop_id = g_path_get_basename (self->filename); |
1882 | |
|
1883 | 0 | return g_steal_pointer (&desktop_id); |
1884 | 0 | } |
1885 | | |
1886 | | static gboolean |
1887 | | is_invalid_key_error (const GError *error) |
1888 | 0 | { |
1889 | 0 | return (error != NULL && |
1890 | 0 | !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND) && |
1891 | 0 | !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)); |
1892 | 0 | } |
1893 | | |
1894 | | static gboolean |
1895 | | g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info, |
1896 | | GKeyFile *key_file) |
1897 | 0 | { |
1898 | 0 | char *start_group; |
1899 | 0 | char *type; |
1900 | 0 | char *try_exec; |
1901 | 0 | char *exec; |
1902 | 0 | char *path; |
1903 | 0 | gboolean bus_activatable; |
1904 | 0 | GError *local_error = NULL; |
1905 | |
|
1906 | 0 | start_group = g_key_file_get_start_group (key_file); |
1907 | 0 | if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0) |
1908 | 0 | { |
1909 | 0 | g_free (start_group); |
1910 | 0 | return FALSE; |
1911 | 0 | } |
1912 | 0 | g_free (start_group); |
1913 | |
|
1914 | 0 | type = g_key_file_get_string (key_file, |
1915 | 0 | G_KEY_FILE_DESKTOP_GROUP, |
1916 | 0 | G_KEY_FILE_DESKTOP_KEY_TYPE, |
1917 | 0 | NULL); |
1918 | 0 | if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0) |
1919 | 0 | { |
1920 | 0 | g_free (type); |
1921 | 0 | return FALSE; |
1922 | 0 | } |
1923 | 0 | g_free (type); |
1924 | |
|
1925 | 0 | path = g_key_file_get_string (key_file, |
1926 | 0 | G_KEY_FILE_DESKTOP_GROUP, |
1927 | 0 | G_KEY_FILE_DESKTOP_KEY_PATH, |
1928 | 0 | &local_error); |
1929 | 0 | if (is_invalid_key_error (local_error)) |
1930 | 0 | { |
1931 | 0 | g_error_free (local_error); |
1932 | 0 | return FALSE; |
1933 | 0 | } |
1934 | 0 | g_clear_error (&local_error); |
1935 | |
|
1936 | 0 | try_exec = g_key_file_get_string (key_file, |
1937 | 0 | G_KEY_FILE_DESKTOP_GROUP, |
1938 | 0 | G_KEY_FILE_DESKTOP_KEY_TRY_EXEC, |
1939 | 0 | &local_error); |
1940 | 0 | if (is_invalid_key_error (local_error)) |
1941 | 0 | { |
1942 | 0 | g_free (path); |
1943 | 0 | g_error_free (local_error); |
1944 | 0 | return FALSE; |
1945 | 0 | } |
1946 | 0 | g_clear_error (&local_error); |
1947 | |
|
1948 | 0 | if (try_exec && try_exec[0] != '\0') |
1949 | 0 | { |
1950 | 0 | char *t; |
1951 | | /* Use the desktop file path (if any) as working dir to search program */ |
1952 | 0 | t = GLIB_PRIVATE_CALL (g_find_program_for_path) (try_exec, NULL, path); |
1953 | 0 | if (t == NULL) |
1954 | 0 | { |
1955 | 0 | g_free (path); |
1956 | 0 | g_free (try_exec); |
1957 | 0 | return FALSE; |
1958 | 0 | } |
1959 | 0 | g_free (t); |
1960 | 0 | } |
1961 | | |
1962 | 0 | exec = g_key_file_get_string (key_file, |
1963 | 0 | G_KEY_FILE_DESKTOP_GROUP, |
1964 | 0 | G_KEY_FILE_DESKTOP_KEY_EXEC, |
1965 | 0 | &local_error); |
1966 | 0 | if (is_invalid_key_error (local_error)) |
1967 | 0 | { |
1968 | 0 | g_free (path); |
1969 | 0 | g_free (try_exec); |
1970 | 0 | g_error_free (local_error); |
1971 | 0 | return FALSE; |
1972 | 0 | } |
1973 | 0 | g_clear_error (&local_error); |
1974 | |
|
1975 | 0 | if (exec && exec[0] != '\0') |
1976 | 0 | { |
1977 | 0 | gint argc; |
1978 | 0 | char **argv; |
1979 | 0 | if (!g_shell_parse_argv (exec, &argc, &argv, NULL)) |
1980 | 0 | { |
1981 | 0 | g_free (path); |
1982 | 0 | g_free (exec); |
1983 | 0 | g_free (try_exec); |
1984 | 0 | return FALSE; |
1985 | 0 | } |
1986 | 0 | else |
1987 | 0 | { |
1988 | 0 | char *t; |
1989 | | |
1990 | | /* Since @exec is not an empty string, there must be at least one |
1991 | | * argument, so dereferencing argv[0] should return non-NULL. */ |
1992 | 0 | g_assert (argc > 0); |
1993 | | /* Use the desktop file path (if any) as working dir to search program */ |
1994 | 0 | t = GLIB_PRIVATE_CALL (g_find_program_for_path) (argv[0], NULL, path); |
1995 | 0 | g_strfreev (argv); |
1996 | |
|
1997 | 0 | if (t == NULL) |
1998 | 0 | { |
1999 | 0 | g_free (path); |
2000 | 0 | g_free (exec); |
2001 | 0 | g_free (try_exec); |
2002 | 0 | return FALSE; |
2003 | 0 | } |
2004 | 0 | g_free (t); |
2005 | 0 | } |
2006 | 0 | } |
2007 | | |
2008 | 0 | info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL); |
2009 | 0 | info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL); |
2010 | 0 | info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL); |
2011 | 0 | info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL); |
2012 | 0 | info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL); |
2013 | 0 | info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE; |
2014 | 0 | info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL); |
2015 | 0 | info->only_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN, NULL, NULL); |
2016 | 0 | info->not_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN, NULL, NULL); |
2017 | 0 | info->try_exec = try_exec; |
2018 | 0 | info->exec = exec; |
2019 | 0 | info->path = g_steal_pointer (&path); |
2020 | 0 | info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE; |
2021 | 0 | info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE; |
2022 | 0 | info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE; |
2023 | 0 | info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE; |
2024 | 0 | info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL); |
2025 | 0 | info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL); |
2026 | 0 | info->mime_types = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL, NULL); |
2027 | 0 | bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL); |
2028 | 0 | info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL); |
2029 | | |
2030 | | /* Remove the special-case: no Actions= key just means 0 extra actions */ |
2031 | 0 | if (info->actions == NULL) |
2032 | 0 | info->actions = g_new0 (gchar *, 0 + 1); |
2033 | |
|
2034 | 0 | info->icon = NULL; |
2035 | 0 | if (info->icon_name) |
2036 | 0 | { |
2037 | 0 | if (g_path_is_absolute (info->icon_name)) |
2038 | 0 | { |
2039 | 0 | GFile *file; |
2040 | |
|
2041 | 0 | file = g_file_new_for_path (info->icon_name); |
2042 | 0 | info->icon = g_file_icon_new (file); |
2043 | 0 | g_object_unref (file); |
2044 | 0 | } |
2045 | 0 | else |
2046 | 0 | { |
2047 | 0 | char *p; |
2048 | | |
2049 | | /* Work around a common mistake in desktop files */ |
2050 | 0 | if ((p = strrchr (info->icon_name, '.')) != NULL && |
2051 | 0 | (strcmp (p, ".png") == 0 || |
2052 | 0 | strcmp (p, ".xpm") == 0 || |
2053 | 0 | strcmp (p, ".svg") == 0)) |
2054 | 0 | *p = 0; |
2055 | |
|
2056 | 0 | info->icon = g_themed_icon_new (info->icon_name); |
2057 | 0 | } |
2058 | 0 | } |
2059 | |
|
2060 | 0 | if (info->exec) |
2061 | 0 | info->binary = binary_from_exec (info->exec); |
2062 | |
|
2063 | 0 | if (info->path && info->path[0] == '\0') |
2064 | 0 | { |
2065 | 0 | g_free (info->path); |
2066 | 0 | info->path = NULL; |
2067 | 0 | } |
2068 | | |
2069 | | /* Can only be DBusActivatable if we know the filename, which means |
2070 | | * that this won't work for the load-from-keyfile case. |
2071 | | */ |
2072 | 0 | if (bus_activatable && info->filename) |
2073 | 0 | { |
2074 | 0 | gchar *basename; |
2075 | 0 | gchar *last_dot; |
2076 | |
|
2077 | 0 | basename = g_path_get_basename (info->filename); |
2078 | 0 | last_dot = strrchr (basename, '.'); |
2079 | |
|
2080 | 0 | if (last_dot && g_str_equal (last_dot, ".desktop")) |
2081 | 0 | { |
2082 | 0 | *last_dot = '\0'; |
2083 | |
|
2084 | 0 | if (g_dbus_is_name (basename) && basename[0] != ':') |
2085 | 0 | info->app_id = g_strdup (basename); |
2086 | 0 | } |
2087 | |
|
2088 | 0 | g_free (basename); |
2089 | 0 | } |
2090 | |
|
2091 | 0 | if (info->filename) |
2092 | 0 | info->desktop_id = g_desktop_app_info_get_desktop_id_for_filename (info); |
2093 | |
|
2094 | 0 | info->keyfile = g_key_file_ref (key_file); |
2095 | |
|
2096 | 0 | return TRUE; |
2097 | 0 | } |
2098 | | |
2099 | | static gboolean |
2100 | | g_desktop_app_info_load_file (GDesktopAppInfo *self) |
2101 | 0 | { |
2102 | 0 | GKeyFile *key_file; |
2103 | 0 | gboolean retval = FALSE; |
2104 | |
|
2105 | 0 | g_return_val_if_fail (self->filename != NULL, FALSE); |
2106 | | |
2107 | 0 | key_file = g_key_file_new (); |
2108 | |
|
2109 | 0 | if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL)) |
2110 | 0 | retval = g_desktop_app_info_load_from_keyfile (self, key_file); |
2111 | |
|
2112 | 0 | g_key_file_unref (key_file); |
2113 | 0 | return retval; |
2114 | 0 | } |
2115 | | |
2116 | | /** |
2117 | | * g_desktop_app_info_new_from_keyfile: |
2118 | | * @key_file: an opened [type@GLib.KeyFile] |
2119 | | * |
2120 | | * Creates a new [class@GioUnix.DesktopAppInfo]. |
2121 | | * |
2122 | | * Returns: (nullable): a new [class@GioUnix.DesktopAppInfo] or `NULL` on error. |
2123 | | * |
2124 | | * Since: 2.18 |
2125 | | **/ |
2126 | | GDesktopAppInfo * |
2127 | | g_desktop_app_info_new_from_keyfile (GKeyFile *key_file) |
2128 | 0 | { |
2129 | 0 | GDesktopAppInfo *info; |
2130 | |
|
2131 | 0 | info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL); |
2132 | 0 | info->filename = NULL; |
2133 | |
|
2134 | 0 | desktop_file_dirs_lock (); |
2135 | |
|
2136 | 0 | if (!g_desktop_app_info_load_from_keyfile (info, key_file)) |
2137 | 0 | g_clear_object (&info); |
2138 | |
|
2139 | 0 | desktop_file_dirs_unlock (); |
2140 | |
|
2141 | 0 | return info; |
2142 | 0 | } |
2143 | | |
2144 | | /** |
2145 | | * g_desktop_app_info_new_from_filename: |
2146 | | * @filename: (type filename): the path of a desktop file, in the GLib |
2147 | | * filename encoding |
2148 | | * |
2149 | | * Creates a new [class@GioUnix.DesktopAppInfo]. |
2150 | | * |
2151 | | * Returns: (nullable): a new [class@GioUnix.DesktopAppInfo] or `NULL` on error. |
2152 | | **/ |
2153 | | GDesktopAppInfo * |
2154 | | g_desktop_app_info_new_from_filename (const char *filename) |
2155 | 0 | { |
2156 | 0 | GDesktopAppInfo *info = NULL; |
2157 | |
|
2158 | 0 | desktop_file_dirs_lock (); |
2159 | |
|
2160 | 0 | info = g_desktop_app_info_new_from_filename_unlocked (filename); |
2161 | |
|
2162 | 0 | desktop_file_dirs_unlock (); |
2163 | |
|
2164 | 0 | return info; |
2165 | 0 | } |
2166 | | |
2167 | | /** |
2168 | | * g_desktop_app_info_new: |
2169 | | * @desktop_id: the desktop file ID |
2170 | | * |
2171 | | * Creates a new [class@GioUnix.DesktopAppInfo] based on a desktop file ID. |
2172 | | * |
2173 | | * A desktop file ID is the basename of the desktop file, including the |
2174 | | * `.desktop` extension. GIO is looking for a desktop file with this name |
2175 | | * in the `applications` subdirectories of the XDG |
2176 | | * data directories (i.e. the directories specified in the `XDG_DATA_HOME` |
2177 | | * and `XDG_DATA_DIRS` environment variables). GIO also supports the |
2178 | | * prefix-to-subdirectory mapping that is described in the |
2179 | | * [Menu Spec](http://standards.freedesktop.org/menu-spec/latest/) |
2180 | | * (i.e. a desktop ID of `kde-foo.desktop` will match |
2181 | | * `/usr/share/applications/kde/foo.desktop`). |
2182 | | * |
2183 | | * Returns: (nullable): a new [class@GioUnix.DesktopAppInfo], or `NULL` if no |
2184 | | * desktop file with that ID exists. |
2185 | | */ |
2186 | | GDesktopAppInfo * |
2187 | | g_desktop_app_info_new (const char *desktop_id) |
2188 | 0 | { |
2189 | 0 | GDesktopAppInfo *appinfo = NULL; |
2190 | 0 | guint i; |
2191 | |
|
2192 | 0 | desktop_file_dirs_lock (); |
2193 | |
|
2194 | 0 | for (i = 0; i < desktop_file_dirs->len; i++) |
2195 | 0 | { |
2196 | 0 | appinfo = desktop_file_dir_get_app (g_ptr_array_index (desktop_file_dirs, i), desktop_id); |
2197 | |
|
2198 | 0 | if (appinfo) |
2199 | 0 | break; |
2200 | 0 | } |
2201 | |
|
2202 | 0 | desktop_file_dirs_unlock (); |
2203 | |
|
2204 | 0 | if (appinfo == NULL) |
2205 | 0 | return NULL; |
2206 | | |
2207 | 0 | g_free (appinfo->desktop_id); |
2208 | 0 | appinfo->desktop_id = g_strdup (desktop_id); |
2209 | |
|
2210 | 0 | if (g_desktop_app_info_get_is_hidden (appinfo)) |
2211 | 0 | { |
2212 | 0 | g_object_unref (appinfo); |
2213 | 0 | appinfo = NULL; |
2214 | 0 | } |
2215 | |
|
2216 | 0 | return appinfo; |
2217 | 0 | } |
2218 | | |
2219 | | static GAppInfo * |
2220 | | g_desktop_app_info_dup (GAppInfo *appinfo) |
2221 | 0 | { |
2222 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
2223 | 0 | GDesktopAppInfo *new_info; |
2224 | |
|
2225 | 0 | new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL); |
2226 | |
|
2227 | 0 | new_info->filename = g_strdup (info->filename); |
2228 | 0 | new_info->desktop_id = g_strdup (info->desktop_id); |
2229 | |
|
2230 | 0 | if (info->keyfile) |
2231 | 0 | new_info->keyfile = g_key_file_ref (info->keyfile); |
2232 | |
|
2233 | 0 | new_info->name = g_strdup (info->name); |
2234 | 0 | new_info->generic_name = g_strdup (info->generic_name); |
2235 | 0 | new_info->fullname = g_strdup (info->fullname); |
2236 | 0 | new_info->keywords = g_strdupv (info->keywords); |
2237 | 0 | new_info->comment = g_strdup (info->comment); |
2238 | 0 | new_info->nodisplay = info->nodisplay; |
2239 | 0 | new_info->icon_name = g_strdup (info->icon_name); |
2240 | 0 | if (info->icon) |
2241 | 0 | new_info->icon = g_object_ref (info->icon); |
2242 | 0 | new_info->only_show_in = g_strdupv (info->only_show_in); |
2243 | 0 | new_info->not_show_in = g_strdupv (info->not_show_in); |
2244 | 0 | new_info->try_exec = g_strdup (info->try_exec); |
2245 | 0 | new_info->exec = g_strdup (info->exec); |
2246 | 0 | new_info->binary = g_strdup (info->binary); |
2247 | 0 | new_info->path = g_strdup (info->path); |
2248 | 0 | new_info->app_id = g_strdup (info->app_id); |
2249 | 0 | new_info->hidden = info->hidden; |
2250 | 0 | new_info->terminal = info->terminal; |
2251 | 0 | new_info->startup_notify = info->startup_notify; |
2252 | |
|
2253 | 0 | return G_APP_INFO (new_info); |
2254 | 0 | } |
2255 | | |
2256 | | /* GAppInfo interface implementation functions {{{2 */ |
2257 | | |
2258 | | static gboolean |
2259 | | g_desktop_app_info_equal (GAppInfo *appinfo1, |
2260 | | GAppInfo *appinfo2) |
2261 | 0 | { |
2262 | 0 | GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1); |
2263 | 0 | GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2); |
2264 | |
|
2265 | 0 | if (info1->desktop_id == NULL || |
2266 | 0 | info2->desktop_id == NULL) |
2267 | 0 | return info1 == info2; |
2268 | | |
2269 | 0 | return strcmp (info1->desktop_id, info2->desktop_id) == 0; |
2270 | 0 | } |
2271 | | |
2272 | | static const char * |
2273 | | g_desktop_app_info_get_id (GAppInfo *appinfo) |
2274 | 0 | { |
2275 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
2276 | |
|
2277 | 0 | return info->desktop_id; |
2278 | 0 | } |
2279 | | |
2280 | | static const char * |
2281 | | g_desktop_app_info_get_name (GAppInfo *appinfo) |
2282 | 0 | { |
2283 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
2284 | |
|
2285 | 0 | if (info->name == NULL) |
2286 | 0 | return _("Unnamed"); |
2287 | 0 | return info->name; |
2288 | 0 | } |
2289 | | |
2290 | | static const char * |
2291 | | g_desktop_app_info_get_display_name (GAppInfo *appinfo) |
2292 | 0 | { |
2293 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
2294 | |
|
2295 | 0 | if (info->fullname == NULL) |
2296 | 0 | return g_desktop_app_info_get_name (appinfo); |
2297 | 0 | return info->fullname; |
2298 | 0 | } |
2299 | | |
2300 | | /** |
2301 | | * g_desktop_app_info_get_is_hidden: |
2302 | | * @info: a [class@GioUnix.DesktopAppInfo]. |
2303 | | * |
2304 | | * A desktop file is hidden if the |
2305 | | * [`Hidden` key](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html#key-hidden) |
2306 | | * in it is set to `True`. |
2307 | | * |
2308 | | * Returns: `TRUE` if hidden, `FALSE` otherwise. |
2309 | | **/ |
2310 | | gboolean |
2311 | | g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info) |
2312 | 0 | { |
2313 | 0 | return info->hidden; |
2314 | 0 | } |
2315 | | |
2316 | | /** |
2317 | | * g_desktop_app_info_get_filename: |
2318 | | * @info: a [class@GioUnix.DesktopAppInfo] |
2319 | | * |
2320 | | * When @info was created from a known filename, return it. |
2321 | | * |
2322 | | * In some situations such as a [class@GioUnix.DesktopAppInfo] returned |
2323 | | * from [ctor@GioUnix.DesktopAppInfo.new_from_keyfile], this function |
2324 | | * will return `NULL`. |
2325 | | * |
2326 | | * Returns: (nullable) (type filename): The full path to the file for @info, |
2327 | | * or `NULL` if not known. |
2328 | | * Since: 2.24 |
2329 | | */ |
2330 | | const char * |
2331 | | g_desktop_app_info_get_filename (GDesktopAppInfo *info) |
2332 | 0 | { |
2333 | 0 | return info->filename; |
2334 | 0 | } |
2335 | | |
2336 | | static const char * |
2337 | | g_desktop_app_info_get_description (GAppInfo *appinfo) |
2338 | 0 | { |
2339 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
2340 | |
|
2341 | 0 | return info->comment; |
2342 | 0 | } |
2343 | | |
2344 | | static const char * |
2345 | | g_desktop_app_info_get_executable (GAppInfo *appinfo) |
2346 | 0 | { |
2347 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
2348 | |
|
2349 | 0 | return info->binary; |
2350 | 0 | } |
2351 | | |
2352 | | static const char * |
2353 | | g_desktop_app_info_get_commandline (GAppInfo *appinfo) |
2354 | 0 | { |
2355 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
2356 | |
|
2357 | 0 | return info->exec; |
2358 | 0 | } |
2359 | | |
2360 | | static GIcon * |
2361 | | g_desktop_app_info_get_icon (GAppInfo *appinfo) |
2362 | 0 | { |
2363 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
2364 | |
|
2365 | 0 | return info->icon; |
2366 | 0 | } |
2367 | | |
2368 | | /** |
2369 | | * g_desktop_app_info_get_categories: |
2370 | | * @info: a [class@GioUnix.DesktopAppInfo] |
2371 | | * |
2372 | | * Gets the categories from the desktop file. |
2373 | | * |
2374 | | * Returns: (nullable): The unparsed |
2375 | | * [`Categories` key](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html#key-categories) |
2376 | | * from the desktop file; |
2377 | | * i.e. no attempt is made to split it by `;` or validate it. |
2378 | | */ |
2379 | | const char * |
2380 | | g_desktop_app_info_get_categories (GDesktopAppInfo *info) |
2381 | 0 | { |
2382 | 0 | return info->categories; |
2383 | 0 | } |
2384 | | |
2385 | | /** |
2386 | | * g_desktop_app_info_get_keywords: |
2387 | | * @info: a [class@GioUnix.DesktopAppInfo] |
2388 | | * |
2389 | | * Gets the keywords from the desktop file. |
2390 | | * |
2391 | | * Returns: (transfer none): The value of the |
2392 | | * [`Keywords` key](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html#key-keywords) |
2393 | | * |
2394 | | * Since: 2.32 |
2395 | | */ |
2396 | | const char * const * |
2397 | | g_desktop_app_info_get_keywords (GDesktopAppInfo *info) |
2398 | 0 | { |
2399 | 0 | return (const char * const *)info->keywords; |
2400 | 0 | } |
2401 | | |
2402 | | /** |
2403 | | * g_desktop_app_info_get_generic_name: |
2404 | | * @info: a [class@GioUnix.DesktopAppInfo] |
2405 | | * |
2406 | | * Gets the generic name from the desktop file. |
2407 | | * |
2408 | | * Returns: (nullable): The value of the |
2409 | | * [`GenericName` key](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html#key-genericname) |
2410 | | */ |
2411 | | const char * |
2412 | | g_desktop_app_info_get_generic_name (GDesktopAppInfo *info) |
2413 | 0 | { |
2414 | 0 | return info->generic_name; |
2415 | 0 | } |
2416 | | |
2417 | | /** |
2418 | | * g_desktop_app_info_get_nodisplay: |
2419 | | * @info: a [class@GioUnix.DesktopAppInfo] |
2420 | | * |
2421 | | * Gets the value of the |
2422 | | * [`NoDisplay` key](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html#key-nodisplay) |
2423 | | * which helps determine if the application info should be shown in menus. See |
2424 | | * `G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY` and [method@Gio.AppInfo.should_show]. |
2425 | | * |
2426 | | * Returns: The value of the `NoDisplay` key |
2427 | | * |
2428 | | * Since: 2.30 |
2429 | | */ |
2430 | | gboolean |
2431 | | g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info) |
2432 | 0 | { |
2433 | 0 | return info->nodisplay; |
2434 | 0 | } |
2435 | | |
2436 | | /** |
2437 | | * g_desktop_app_info_get_show_in: |
2438 | | * @info: a [class@GioUnix.DesktopAppInfo] |
2439 | | * @desktop_env: (nullable): a string specifying a desktop name |
2440 | | * |
2441 | | * Checks if the application info should be shown in menus that list available |
2442 | | * applications for a specific name of the desktop, based on the |
2443 | | * [`OnlyShowIn`](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html#key-onlyshowin) |
2444 | | * and [`NotShowIn`](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html#key-notshowin) |
2445 | | * keys. |
2446 | | * |
2447 | | * @desktop_env should typically be given as `NULL`, in which case the |
2448 | | * `XDG_CURRENT_DESKTOP` environment variable is consulted. If you want |
2449 | | * to override the default mechanism then you may specify @desktop_env, |
2450 | | * but this is not recommended. |
2451 | | * |
2452 | | * Note that [method@Gio.AppInfo.should_show] for @info will include this check |
2453 | | * (with `NULL` for @desktop_env) as well as additional checks. |
2454 | | * |
2455 | | * Returns: `TRUE` if the @info should be shown in @desktop_env according to the |
2456 | | * `OnlyShowIn` and `NotShowIn` keys, `FALSE` otherwise. |
2457 | | * |
2458 | | * Since: 2.30 |
2459 | | */ |
2460 | | gboolean |
2461 | | g_desktop_app_info_get_show_in (GDesktopAppInfo *info, |
2462 | | const gchar *desktop_env) |
2463 | 0 | { |
2464 | 0 | const gchar *specified_envs[] = { desktop_env, NULL }; |
2465 | 0 | const gchar * const *envs; |
2466 | 0 | gint i; |
2467 | |
|
2468 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE); |
2469 | | |
2470 | 0 | if (desktop_env) |
2471 | 0 | envs = specified_envs; |
2472 | 0 | else |
2473 | 0 | envs = get_current_desktops (NULL); |
2474 | |
|
2475 | 0 | for (i = 0; envs[i]; i++) |
2476 | 0 | { |
2477 | 0 | gint j; |
2478 | |
|
2479 | 0 | if (info->only_show_in) |
2480 | 0 | for (j = 0; info->only_show_in[j]; j++) |
2481 | 0 | if (g_str_equal (info->only_show_in[j], envs[i])) |
2482 | 0 | return TRUE; |
2483 | | |
2484 | 0 | if (info->not_show_in) |
2485 | 0 | for (j = 0; info->not_show_in[j]; j++) |
2486 | 0 | if (g_str_equal (info->not_show_in[j], envs[i])) |
2487 | 0 | return FALSE; |
2488 | 0 | } |
2489 | | |
2490 | 0 | return info->only_show_in == NULL; |
2491 | 0 | } |
2492 | | |
2493 | | /* Launching... {{{2 */ |
2494 | | |
2495 | | static char * |
2496 | | expand_macro_single (char macro, const char *uri) |
2497 | 0 | { |
2498 | 0 | GFile *file; |
2499 | 0 | char *result = NULL; |
2500 | 0 | char *path = NULL; |
2501 | 0 | char *name; |
2502 | |
|
2503 | 0 | file = g_file_new_for_uri (uri); |
2504 | |
|
2505 | 0 | switch (macro) |
2506 | 0 | { |
2507 | 0 | case 'u': |
2508 | 0 | case 'U': |
2509 | 0 | result = g_shell_quote (uri); |
2510 | 0 | break; |
2511 | 0 | case 'f': |
2512 | 0 | case 'F': |
2513 | 0 | path = g_file_get_path (file); |
2514 | 0 | if (path) |
2515 | 0 | result = g_shell_quote (path); |
2516 | 0 | break; |
2517 | 0 | case 'd': |
2518 | 0 | case 'D': |
2519 | 0 | path = g_file_get_path (file); |
2520 | 0 | if (path) |
2521 | 0 | { |
2522 | 0 | name = g_path_get_dirname (path); |
2523 | 0 | result = g_shell_quote (name); |
2524 | 0 | g_free (name); |
2525 | 0 | } |
2526 | 0 | break; |
2527 | 0 | case 'n': |
2528 | 0 | case 'N': |
2529 | 0 | path = g_file_get_path (file); |
2530 | 0 | if (path) |
2531 | 0 | { |
2532 | 0 | name = g_path_get_basename (path); |
2533 | 0 | result = g_shell_quote (name); |
2534 | 0 | g_free (name); |
2535 | 0 | } |
2536 | 0 | break; |
2537 | 0 | } |
2538 | | |
2539 | 0 | g_object_unref (file); |
2540 | 0 | g_free (path); |
2541 | |
|
2542 | 0 | return result; |
2543 | 0 | } |
2544 | | |
2545 | | static char * |
2546 | | expand_macro_uri (char macro, const char *uri, gboolean force_file_uri, char force_file_uri_macro) |
2547 | 0 | { |
2548 | 0 | char *expanded = NULL; |
2549 | |
|
2550 | 0 | g_return_val_if_fail (uri != NULL, NULL); |
2551 | | |
2552 | 0 | if (!force_file_uri || |
2553 | | /* Pass URI if it contains an anchor */ |
2554 | 0 | strchr (uri, '#') != NULL) |
2555 | 0 | { |
2556 | 0 | expanded = expand_macro_single (macro, uri); |
2557 | 0 | } |
2558 | 0 | else |
2559 | 0 | { |
2560 | 0 | expanded = expand_macro_single (force_file_uri_macro, uri); |
2561 | 0 | if (expanded == NULL) |
2562 | 0 | expanded = expand_macro_single (macro, uri); |
2563 | 0 | } |
2564 | |
|
2565 | 0 | return expanded; |
2566 | 0 | } |
2567 | | |
2568 | | static void |
2569 | | expand_macro (char macro, |
2570 | | GString *exec, |
2571 | | GDesktopAppInfo *info, |
2572 | | GList **uri_list) |
2573 | 0 | { |
2574 | 0 | GList *uris = *uri_list; |
2575 | 0 | char *expanded = NULL; |
2576 | 0 | gboolean force_file_uri; |
2577 | 0 | char force_file_uri_macro; |
2578 | 0 | const char *uri; |
2579 | |
|
2580 | 0 | g_return_if_fail (exec != NULL); |
2581 | | |
2582 | | /* On %u and %U, pass POSIX file path pointing to the URI via |
2583 | | * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't |
2584 | | * running or the URI doesn't have a POSIX file path via FUSE |
2585 | | * we'll just pass the URI. |
2586 | | */ |
2587 | 0 | force_file_uri_macro = macro; |
2588 | 0 | force_file_uri = FALSE; |
2589 | 0 | if (!info->no_fuse) |
2590 | 0 | { |
2591 | 0 | switch (macro) |
2592 | 0 | { |
2593 | 0 | case 'u': |
2594 | 0 | force_file_uri_macro = 'f'; |
2595 | 0 | force_file_uri = TRUE; |
2596 | 0 | break; |
2597 | 0 | case 'U': |
2598 | 0 | force_file_uri_macro = 'F'; |
2599 | 0 | force_file_uri = TRUE; |
2600 | 0 | break; |
2601 | 0 | default: |
2602 | 0 | break; |
2603 | 0 | } |
2604 | 0 | } |
2605 | | |
2606 | 0 | switch (macro) |
2607 | 0 | { |
2608 | 0 | case 'u': |
2609 | 0 | case 'f': |
2610 | 0 | case 'd': |
2611 | 0 | case 'n': |
2612 | 0 | if (uris) |
2613 | 0 | { |
2614 | 0 | uri = uris->data; |
2615 | 0 | expanded = expand_macro_uri (macro, uri, |
2616 | 0 | force_file_uri, force_file_uri_macro); |
2617 | 0 | if (expanded) |
2618 | 0 | { |
2619 | 0 | g_string_append (exec, expanded); |
2620 | 0 | g_free (expanded); |
2621 | 0 | } |
2622 | 0 | uris = uris->next; |
2623 | 0 | } |
2624 | |
|
2625 | 0 | break; |
2626 | | |
2627 | 0 | case 'U': |
2628 | 0 | case 'F': |
2629 | 0 | case 'D': |
2630 | 0 | case 'N': |
2631 | 0 | while (uris) |
2632 | 0 | { |
2633 | 0 | uri = uris->data; |
2634 | 0 | expanded = expand_macro_uri (macro, uri, |
2635 | 0 | force_file_uri, force_file_uri_macro); |
2636 | 0 | if (expanded) |
2637 | 0 | { |
2638 | 0 | g_string_append (exec, expanded); |
2639 | 0 | g_free (expanded); |
2640 | 0 | } |
2641 | |
|
2642 | 0 | uris = uris->next; |
2643 | |
|
2644 | 0 | if (uris != NULL && expanded) |
2645 | 0 | g_string_append_c (exec, ' '); |
2646 | 0 | } |
2647 | |
|
2648 | 0 | break; |
2649 | | |
2650 | 0 | case 'i': |
2651 | 0 | if (info->icon_name) |
2652 | 0 | { |
2653 | 0 | g_string_append (exec, "--icon "); |
2654 | 0 | expanded = g_shell_quote (info->icon_name); |
2655 | 0 | g_string_append (exec, expanded); |
2656 | 0 | g_free (expanded); |
2657 | 0 | } |
2658 | 0 | break; |
2659 | | |
2660 | 0 | case 'c': |
2661 | 0 | if (info->name) |
2662 | 0 | { |
2663 | 0 | expanded = g_shell_quote (info->name); |
2664 | 0 | g_string_append (exec, expanded); |
2665 | 0 | g_free (expanded); |
2666 | 0 | } |
2667 | 0 | break; |
2668 | | |
2669 | 0 | case 'k': |
2670 | 0 | if (info->filename) |
2671 | 0 | { |
2672 | 0 | expanded = g_shell_quote (info->filename); |
2673 | 0 | g_string_append (exec, expanded); |
2674 | 0 | g_free (expanded); |
2675 | 0 | } |
2676 | 0 | break; |
2677 | | |
2678 | 0 | case 'm': /* deprecated */ |
2679 | 0 | break; |
2680 | | |
2681 | 0 | case '%': |
2682 | 0 | g_string_append_c (exec, '%'); |
2683 | 0 | break; |
2684 | 0 | } |
2685 | | |
2686 | 0 | *uri_list = uris; |
2687 | 0 | } |
2688 | | |
2689 | | static gboolean |
2690 | | expand_application_parameters (GDesktopAppInfo *info, |
2691 | | const gchar *exec_line, |
2692 | | GList **uris, |
2693 | | int *argc, |
2694 | | char ***argv, |
2695 | | GError **error) |
2696 | 0 | { |
2697 | 0 | GList *uri_list = *uris; |
2698 | 0 | const char *p = exec_line; |
2699 | 0 | GString *expanded_exec; |
2700 | 0 | gboolean res; |
2701 | |
|
2702 | 0 | if (exec_line == NULL) |
2703 | 0 | { |
2704 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
2705 | 0 | _("Desktop file didn’t specify Exec field")); |
2706 | 0 | return FALSE; |
2707 | 0 | } |
2708 | | |
2709 | 0 | expanded_exec = g_string_new (NULL); |
2710 | |
|
2711 | 0 | while (*p) |
2712 | 0 | { |
2713 | 0 | if (p[0] == '%' && p[1] != '\0') |
2714 | 0 | { |
2715 | 0 | expand_macro (p[1], expanded_exec, info, uris); |
2716 | 0 | p++; |
2717 | 0 | } |
2718 | 0 | else |
2719 | 0 | g_string_append_c (expanded_exec, *p); |
2720 | |
|
2721 | 0 | p++; |
2722 | 0 | } |
2723 | | |
2724 | | /* No file substitutions */ |
2725 | 0 | if (uri_list == *uris && uri_list != NULL) |
2726 | 0 | { |
2727 | | /* If there is no macro default to %f. This is also what KDE does */ |
2728 | 0 | g_string_append_c (expanded_exec, ' '); |
2729 | 0 | expand_macro ('f', expanded_exec, info, uris); |
2730 | 0 | } |
2731 | |
|
2732 | 0 | res = g_shell_parse_argv (expanded_exec->str, argc, argv, error); |
2733 | 0 | g_string_free (expanded_exec, TRUE); |
2734 | 0 | return res; |
2735 | 0 | } |
2736 | | |
2737 | | static gboolean |
2738 | | prepend_terminal_to_vector (int *argc, |
2739 | | char ***argv, |
2740 | | const char *path, |
2741 | | const char *working_dir) |
2742 | 0 | { |
2743 | 0 | #ifndef G_OS_WIN32 |
2744 | 0 | char **real_argv; |
2745 | 0 | size_t real_argc; |
2746 | 0 | size_t i; |
2747 | 0 | size_t term_argc; |
2748 | 0 | char *found_terminal; |
2749 | 0 | char **the_argv; |
2750 | 0 | const char *term_arg = NULL; |
2751 | 0 | static const struct { |
2752 | 0 | const char *exec; |
2753 | 0 | const char *exec_arg; |
2754 | 0 | } known_terminals[] = { |
2755 | 0 | { "xdg-terminal-exec", NULL }, |
2756 | 0 | { "kgx", "-e" }, |
2757 | 0 | { "gnome-terminal", "--" }, |
2758 | 0 | { "mate-terminal", "-x" }, |
2759 | 0 | { "xfce4-terminal", "-x" }, |
2760 | 0 | { "tilix", "-e" }, |
2761 | 0 | { "konsole", "-e" }, |
2762 | 0 | { "nxterm", "-e" }, |
2763 | 0 | { "color-xterm", "-e" }, |
2764 | 0 | { "rxvt", "-e" }, |
2765 | 0 | { "dtterm", "-e" }, |
2766 | 0 | { "xterm", "-e" } |
2767 | 0 | }; |
2768 | |
|
2769 | 0 | g_return_val_if_fail (argc != NULL, FALSE); |
2770 | 0 | g_return_val_if_fail (argv != NULL, FALSE); |
2771 | | |
2772 | | /* sanity */ |
2773 | 0 | if(*argv == NULL) |
2774 | 0 | *argc = 0; |
2775 | |
|
2776 | 0 | the_argv = *argv; |
2777 | | |
2778 | | /* compute size if not given */ |
2779 | 0 | if (*argc < 0) |
2780 | 0 | { |
2781 | 0 | for ((*argc) = 0; the_argv[*argc] != NULL; (*argc)++) |
2782 | 0 | ; |
2783 | 0 | } |
2784 | |
|
2785 | 0 | for (i = 0, found_terminal = NULL; i < G_N_ELEMENTS (known_terminals); i++) |
2786 | 0 | { |
2787 | 0 | found_terminal = GLIB_PRIVATE_CALL (g_find_program_for_path) (known_terminals[i].exec, |
2788 | 0 | path, working_dir); |
2789 | 0 | if (found_terminal != NULL) |
2790 | 0 | { |
2791 | 0 | term_arg = known_terminals[i].exec_arg; |
2792 | 0 | break; |
2793 | 0 | } |
2794 | 0 | } |
2795 | |
|
2796 | 0 | if (found_terminal == NULL) |
2797 | 0 | { |
2798 | 0 | g_debug ("Couldn’t find a known terminal"); |
2799 | 0 | return FALSE; |
2800 | 0 | } |
2801 | | |
2802 | | /* check if the terminal require an option */ |
2803 | 0 | term_argc = term_arg ? 2 : 1; |
2804 | |
|
2805 | 0 | real_argc = term_argc + *argc; |
2806 | 0 | real_argv = g_new (char *, real_argc + 1); |
2807 | |
|
2808 | 0 | i = 0; |
2809 | 0 | real_argv[i++] = found_terminal; |
2810 | |
|
2811 | 0 | if (term_arg) |
2812 | 0 | real_argv[i++] = g_strdup (term_arg); |
2813 | |
|
2814 | 0 | g_assert (i == term_argc); |
2815 | 0 | for (int j = 0; j < *argc; j++) |
2816 | 0 | real_argv[i++] = the_argv[j]; |
2817 | |
|
2818 | 0 | real_argv[i] = NULL; |
2819 | |
|
2820 | 0 | g_free (*argv); |
2821 | 0 | *argv = real_argv; |
2822 | 0 | *argc = real_argc; |
2823 | |
|
2824 | 0 | return TRUE; |
2825 | | #else |
2826 | | return FALSE; |
2827 | | #endif /* G_OS_WIN32 */ |
2828 | 0 | } |
2829 | | |
2830 | | static GList * |
2831 | | create_files_for_uris (GList *uris) |
2832 | 0 | { |
2833 | 0 | GList *res; |
2834 | 0 | GList *iter; |
2835 | |
|
2836 | 0 | res = NULL; |
2837 | |
|
2838 | 0 | for (iter = uris; iter; iter = iter->next) |
2839 | 0 | { |
2840 | 0 | GFile *file = g_file_new_for_uri ((char *)iter->data); |
2841 | 0 | res = g_list_prepend (res, file); |
2842 | 0 | } |
2843 | |
|
2844 | 0 | return g_list_reverse (res); |
2845 | 0 | } |
2846 | | |
2847 | | static void |
2848 | | notify_desktop_launch (GDBusConnection *session_bus, |
2849 | | GDesktopAppInfo *info, |
2850 | | long pid, |
2851 | | const char *display, |
2852 | | const char *sn_id, |
2853 | | GList *uris) |
2854 | 0 | { |
2855 | 0 | GDBusMessage *msg; |
2856 | 0 | GVariantBuilder uri_variant; |
2857 | 0 | GVariantBuilder extras_variant; |
2858 | 0 | GList *iter; |
2859 | 0 | const char *desktop_file_id; |
2860 | 0 | const char *gio_desktop_file; |
2861 | |
|
2862 | 0 | if (session_bus == NULL) |
2863 | 0 | return; |
2864 | | |
2865 | 0 | g_variant_builder_init_static (&uri_variant, G_VARIANT_TYPE ("as")); |
2866 | 0 | for (iter = uris; iter; iter = iter->next) |
2867 | 0 | g_variant_builder_add (&uri_variant, "s", iter->data); |
2868 | |
|
2869 | 0 | g_variant_builder_init_static (&extras_variant, G_VARIANT_TYPE ("a{sv}")); |
2870 | 0 | if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL)) |
2871 | 0 | g_variant_builder_add (&extras_variant, "{sv}", |
2872 | 0 | "startup-id", |
2873 | 0 | g_variant_new ("s", |
2874 | 0 | sn_id)); |
2875 | 0 | gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE"); |
2876 | 0 | if (gio_desktop_file != NULL) |
2877 | 0 | g_variant_builder_add (&extras_variant, "{sv}", |
2878 | 0 | "origin-desktop-file", |
2879 | 0 | g_variant_new_bytestring (gio_desktop_file)); |
2880 | 0 | if (g_get_prgname () != NULL) |
2881 | 0 | g_variant_builder_add (&extras_variant, "{sv}", |
2882 | 0 | "origin-prgname", |
2883 | 0 | g_variant_new_bytestring (g_get_prgname ())); |
2884 | 0 | g_variant_builder_add (&extras_variant, "{sv}", |
2885 | 0 | "origin-pid", |
2886 | 0 | g_variant_new ("x", |
2887 | 0 | (gint64)getpid ())); |
2888 | |
|
2889 | 0 | if (info->filename) |
2890 | 0 | desktop_file_id = info->filename; |
2891 | 0 | else if (info->desktop_id) |
2892 | 0 | desktop_file_id = info->desktop_id; |
2893 | 0 | else |
2894 | 0 | desktop_file_id = ""; |
2895 | |
|
2896 | 0 | msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo", |
2897 | 0 | "org.gtk.gio.DesktopAppInfo", |
2898 | 0 | "Launched"); |
2899 | 0 | g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})", |
2900 | 0 | g_variant_new_bytestring (desktop_file_id), |
2901 | 0 | display ? display : "", |
2902 | 0 | (gint64)pid, |
2903 | 0 | &uri_variant, |
2904 | 0 | &extras_variant)); |
2905 | 0 | g_dbus_connection_send_message (session_bus, |
2906 | 0 | msg, 0, |
2907 | 0 | NULL, |
2908 | 0 | NULL); |
2909 | 0 | g_object_unref (msg); |
2910 | 0 | } |
2911 | | |
2912 | | static void |
2913 | | emit_launch_started (GAppLaunchContext *context, |
2914 | | GDesktopAppInfo *info, |
2915 | | const gchar *startup_id) |
2916 | 0 | { |
2917 | 0 | GVariantBuilder builder; |
2918 | 0 | GVariant *platform_data = NULL; |
2919 | |
|
2920 | 0 | if (startup_id) |
2921 | 0 | { |
2922 | 0 | g_variant_builder_init_static (&builder, G_VARIANT_TYPE_ARRAY); |
2923 | 0 | g_variant_builder_add (&builder, "{sv}", |
2924 | 0 | "startup-notification-id", |
2925 | 0 | g_variant_new_string (startup_id)); |
2926 | 0 | platform_data = g_variant_ref_sink (g_variant_builder_end (&builder)); |
2927 | 0 | } |
2928 | 0 | g_signal_emit_by_name (context, "launch-started", info, platform_data); |
2929 | 0 | g_clear_pointer (&platform_data, g_variant_unref); |
2930 | 0 | } |
2931 | | |
2932 | 0 | #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH) |
2933 | | |
2934 | | static gboolean |
2935 | | g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info, |
2936 | | GDBusConnection *session_bus, |
2937 | | const gchar *exec_line, |
2938 | | GList *uris, |
2939 | | GAppLaunchContext *launch_context, |
2940 | | GSpawnFlags spawn_flags, |
2941 | | GSpawnChildSetupFunc user_setup, |
2942 | | gpointer user_setup_data, |
2943 | | GDesktopAppLaunchCallback pid_callback, |
2944 | | gpointer pid_callback_data, |
2945 | | gint stdin_fd, |
2946 | | gint stdout_fd, |
2947 | | gint stderr_fd, |
2948 | | GError **error) |
2949 | 0 | { |
2950 | 0 | gboolean completed = FALSE; |
2951 | 0 | GList *old_uris; |
2952 | 0 | GList *dup_uris; |
2953 | |
|
2954 | 0 | char **argv, **envp; |
2955 | 0 | int argc; |
2956 | |
|
2957 | 0 | g_return_val_if_fail (info != NULL, FALSE); |
2958 | | |
2959 | 0 | argv = NULL; |
2960 | |
|
2961 | 0 | if (launch_context) |
2962 | 0 | envp = g_app_launch_context_get_environment (launch_context); |
2963 | 0 | else |
2964 | 0 | envp = g_get_environ (); |
2965 | | |
2966 | | /* The GList* passed to expand_application_parameters() will be modified |
2967 | | * internally by expand_macro(), so we need to pass a copy of it instead, |
2968 | | * and also use that copy to control the exit condition of the loop below. |
2969 | | */ |
2970 | 0 | dup_uris = uris; |
2971 | 0 | do |
2972 | 0 | { |
2973 | 0 | GPid pid; |
2974 | 0 | GList *launched_uris; |
2975 | 0 | GList *iter; |
2976 | 0 | char *sn_id = NULL; |
2977 | 0 | char **wrapped_argv; |
2978 | 0 | int i; |
2979 | |
|
2980 | 0 | old_uris = dup_uris; |
2981 | 0 | if (!expand_application_parameters (info, exec_line, &dup_uris, &argc, &argv, error)) |
2982 | 0 | goto out; |
2983 | | |
2984 | | /* Get the subset of URIs we're launching with this process */ |
2985 | 0 | launched_uris = NULL; |
2986 | 0 | for (iter = old_uris; iter != NULL && iter != dup_uris; iter = iter->next) |
2987 | 0 | launched_uris = g_list_prepend (launched_uris, iter->data); |
2988 | 0 | launched_uris = g_list_reverse (launched_uris); |
2989 | |
|
2990 | 0 | if (info->terminal && !prepend_terminal_to_vector (&argc, &argv, |
2991 | 0 | g_environ_getenv (envp, "PATH"), |
2992 | 0 | info->path)) |
2993 | 0 | { |
2994 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
2995 | 0 | _("Unable to find terminal required for application")); |
2996 | 0 | goto out; |
2997 | 0 | } |
2998 | | |
2999 | 0 | if (info->filename) |
3000 | 0 | envp = g_environ_setenv (envp, |
3001 | 0 | "GIO_LAUNCHED_DESKTOP_FILE", |
3002 | 0 | info->filename, |
3003 | 0 | TRUE); |
3004 | |
|
3005 | 0 | sn_id = NULL; |
3006 | 0 | if (launch_context) |
3007 | 0 | { |
3008 | 0 | GList *launched_files = create_files_for_uris (launched_uris); |
3009 | |
|
3010 | 0 | if (info->startup_notify) |
3011 | 0 | { |
3012 | 0 | sn_id = g_app_launch_context_get_startup_notify_id (launch_context, |
3013 | 0 | G_APP_INFO (info), |
3014 | 0 | launched_files); |
3015 | 0 | if (sn_id) |
3016 | 0 | { |
3017 | 0 | envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE); |
3018 | 0 | envp = g_environ_setenv (envp, "XDG_ACTIVATION_TOKEN", sn_id, TRUE); |
3019 | 0 | } |
3020 | 0 | } |
3021 | |
|
3022 | 0 | g_list_free_full (launched_files, g_object_unref); |
3023 | |
|
3024 | 0 | emit_launch_started (launch_context, info, sn_id); |
3025 | 0 | } |
3026 | |
|
3027 | 0 | g_assert (argc > 0); |
3028 | | |
3029 | 0 | if (!g_path_is_absolute (argv[0]) || |
3030 | 0 | !g_file_test (argv[0], G_FILE_TEST_IS_EXECUTABLE) || |
3031 | 0 | g_file_test (argv[0], G_FILE_TEST_IS_DIR)) |
3032 | 0 | { |
3033 | 0 | char *program = g_steal_pointer (&argv[0]); |
3034 | 0 | char *program_path = NULL; |
3035 | |
|
3036 | 0 | if (!g_path_is_absolute (program)) |
3037 | 0 | { |
3038 | 0 | const char *env_path = g_environ_getenv (envp, "PATH"); |
3039 | |
|
3040 | 0 | program_path = GLIB_PRIVATE_CALL (g_find_program_for_path) (program, |
3041 | 0 | env_path, |
3042 | 0 | info->path); |
3043 | 0 | } |
3044 | |
|
3045 | 0 | if (program_path) |
3046 | 0 | { |
3047 | 0 | argv[0] = g_steal_pointer (&program_path); |
3048 | 0 | } |
3049 | 0 | else |
3050 | 0 | { |
3051 | 0 | if (sn_id) |
3052 | 0 | g_app_launch_context_launch_failed (launch_context, sn_id); |
3053 | |
|
3054 | 0 | g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT, |
3055 | 0 | _("Program ‘%s’ not found in $PATH"), |
3056 | 0 | program); |
3057 | |
|
3058 | 0 | g_free (program); |
3059 | 0 | g_clear_pointer (&sn_id, g_free); |
3060 | 0 | g_clear_list (&launched_uris, NULL); |
3061 | 0 | goto out; |
3062 | 0 | } |
3063 | | |
3064 | 0 | g_free (program); |
3065 | 0 | } |
3066 | | |
3067 | 0 | if (g_once_init_enter_pointer (&gio_launch_desktop_path)) |
3068 | 0 | { |
3069 | 0 | const gchar *tmp = NULL; |
3070 | 0 | gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) (); |
3071 | | |
3072 | | /* Allow test suite to specify path to gio-launch-desktop */ |
3073 | 0 | if (!is_setuid) |
3074 | 0 | tmp = g_getenv ("GIO_LAUNCH_DESKTOP"); |
3075 | | |
3076 | | /* Allow build system to specify path to gio-launch-desktop */ |
3077 | 0 | if (tmp == NULL && g_file_test (GIO_LAUNCH_DESKTOP, G_FILE_TEST_IS_EXECUTABLE)) |
3078 | 0 | tmp = GIO_LAUNCH_DESKTOP; |
3079 | | |
3080 | | /* Fall back on usual searching in $PATH */ |
3081 | 0 | if (tmp == NULL) |
3082 | 0 | tmp = "gio-launch-desktop"; |
3083 | 0 | g_once_init_leave_pointer (&gio_launch_desktop_path, tmp); |
3084 | 0 | } |
3085 | |
|
3086 | 0 | wrapped_argv = g_new (char *, argc + 2); |
3087 | 0 | wrapped_argv[0] = g_strdup (gio_launch_desktop_path); |
3088 | |
|
3089 | 0 | for (i = 0; i < argc; i++) |
3090 | 0 | wrapped_argv[i + 1] = g_steal_pointer (&argv[i]); |
3091 | |
|
3092 | 0 | wrapped_argv[i + 1] = NULL; |
3093 | 0 | g_free (argv); |
3094 | 0 | argv = NULL; |
3095 | |
|
3096 | 0 | if (!g_spawn_async_with_fds (info->path, |
3097 | 0 | wrapped_argv, |
3098 | 0 | envp, |
3099 | 0 | spawn_flags, |
3100 | 0 | user_setup, |
3101 | 0 | user_setup_data, |
3102 | 0 | &pid, |
3103 | 0 | stdin_fd, |
3104 | 0 | stdout_fd, |
3105 | 0 | stderr_fd, |
3106 | 0 | error)) |
3107 | 0 | { |
3108 | 0 | if (sn_id) |
3109 | 0 | g_app_launch_context_launch_failed (launch_context, sn_id); |
3110 | |
|
3111 | 0 | g_free (sn_id); |
3112 | 0 | g_list_free (launched_uris); |
3113 | 0 | g_clear_pointer (&wrapped_argv, g_strfreev); |
3114 | |
|
3115 | 0 | goto out; |
3116 | 0 | } |
3117 | | |
3118 | 0 | if (pid_callback != NULL) |
3119 | 0 | pid_callback (info, pid, pid_callback_data); |
3120 | |
|
3121 | 0 | if (launch_context != NULL) |
3122 | 0 | { |
3123 | 0 | GVariantBuilder builder; |
3124 | 0 | GVariant *platform_data; |
3125 | |
|
3126 | 0 | g_variant_builder_init_static (&builder, G_VARIANT_TYPE_ARRAY); |
3127 | 0 | g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid)); |
3128 | 0 | if (sn_id) |
3129 | 0 | g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id)); |
3130 | 0 | platform_data = g_variant_ref_sink (g_variant_builder_end (&builder)); |
3131 | 0 | g_signal_emit_by_name (launch_context, "launched", info, platform_data); |
3132 | 0 | g_variant_unref (platform_data); |
3133 | 0 | } |
3134 | |
|
3135 | 0 | notify_desktop_launch (session_bus, |
3136 | 0 | info, |
3137 | 0 | pid, |
3138 | 0 | NULL, |
3139 | 0 | sn_id, |
3140 | 0 | launched_uris); |
3141 | |
|
3142 | 0 | g_free (sn_id); |
3143 | 0 | g_list_free (launched_uris); |
3144 | |
|
3145 | 0 | g_strfreev (wrapped_argv); |
3146 | 0 | wrapped_argv = NULL; |
3147 | 0 | } |
3148 | 0 | while (dup_uris != NULL); |
3149 | | |
3150 | 0 | completed = TRUE; |
3151 | |
|
3152 | 0 | out: |
3153 | 0 | g_strfreev (argv); |
3154 | 0 | g_strfreev (envp); |
3155 | |
|
3156 | 0 | return completed; |
3157 | 0 | } |
3158 | | |
3159 | | static gchar * |
3160 | | object_path_from_appid (const gchar *appid) |
3161 | 0 | { |
3162 | 0 | gchar *appid_path, *iter; |
3163 | |
|
3164 | 0 | appid_path = g_strconcat ("/", appid, NULL); |
3165 | 0 | for (iter = appid_path; *iter; iter++) |
3166 | 0 | { |
3167 | 0 | if (*iter == '.') |
3168 | 0 | *iter = '/'; |
3169 | |
|
3170 | 0 | if (*iter == '-') |
3171 | 0 | *iter = '_'; |
3172 | 0 | } |
3173 | |
|
3174 | 0 | return appid_path; |
3175 | 0 | } |
3176 | | |
3177 | | static GVariant * |
3178 | | g_desktop_app_info_make_platform_data (GDesktopAppInfo *info, |
3179 | | GList *uris, |
3180 | | GAppLaunchContext *launch_context) |
3181 | 0 | { |
3182 | 0 | GVariantBuilder builder; |
3183 | |
|
3184 | 0 | g_variant_builder_init_static (&builder, G_VARIANT_TYPE_VARDICT); |
3185 | |
|
3186 | 0 | if (launch_context) |
3187 | 0 | { |
3188 | 0 | GList *launched_files = create_files_for_uris (uris); |
3189 | |
|
3190 | 0 | if (info->startup_notify) |
3191 | 0 | { |
3192 | 0 | gchar *sn_id; |
3193 | |
|
3194 | 0 | sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files); |
3195 | 0 | if (sn_id) |
3196 | 0 | { |
3197 | 0 | g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_string (sn_id)); |
3198 | 0 | g_variant_builder_add (&builder, "{sv}", "activation-token", g_variant_new_take_string (g_steal_pointer (&sn_id))); |
3199 | 0 | } |
3200 | 0 | } |
3201 | |
|
3202 | 0 | g_list_free_full (launched_files, g_object_unref); |
3203 | 0 | } |
3204 | |
|
3205 | 0 | return g_variant_builder_end (&builder); |
3206 | 0 | } |
3207 | | |
3208 | | typedef struct |
3209 | | { |
3210 | | GDesktopAppInfo *info; /* (owned) */ |
3211 | | GAppLaunchContext *launch_context; /* (owned) (nullable) */ |
3212 | | GAsyncReadyCallback callback; |
3213 | | gchar *startup_id; /* (owned) (nullable) */ |
3214 | | gpointer user_data; |
3215 | | } LaunchUrisWithDBusData; |
3216 | | |
3217 | | static void |
3218 | | launch_uris_with_dbus_data_free (LaunchUrisWithDBusData *data) |
3219 | 0 | { |
3220 | 0 | g_clear_object (&data->info); |
3221 | 0 | g_clear_object (&data->launch_context); |
3222 | 0 | g_free (data->startup_id); |
3223 | |
|
3224 | 0 | g_free (data); |
3225 | 0 | } |
3226 | | |
3227 | | static void |
3228 | | launch_uris_with_dbus_signal_cb (GObject *object, |
3229 | | GAsyncResult *result, |
3230 | | gpointer user_data) |
3231 | 0 | { |
3232 | 0 | LaunchUrisWithDBusData *data = user_data; |
3233 | 0 | GVariantBuilder builder; |
3234 | |
|
3235 | 0 | if (data->launch_context) |
3236 | 0 | { |
3237 | 0 | if (g_task_had_error (G_TASK (result))) |
3238 | 0 | { |
3239 | 0 | if (data->startup_id != NULL) |
3240 | 0 | g_app_launch_context_launch_failed (data->launch_context, data->startup_id); |
3241 | 0 | } |
3242 | 0 | else |
3243 | 0 | { |
3244 | 0 | GVariant *platform_data; |
3245 | |
|
3246 | 0 | g_variant_builder_init_static (&builder, G_VARIANT_TYPE_ARRAY); |
3247 | | /* the docs guarantee `pid` will be set, but we can’t |
3248 | | * easily know it for a D-Bus process, so set it to zero */ |
3249 | 0 | g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (0)); |
3250 | 0 | if (data->startup_id) |
3251 | 0 | g_variant_builder_add (&builder, "{sv}", |
3252 | 0 | "startup-notification-id", |
3253 | 0 | g_variant_new_string (data->startup_id)); |
3254 | 0 | platform_data = g_variant_ref_sink (g_variant_builder_end (&builder)); |
3255 | 0 | g_signal_emit_by_name (data->launch_context, |
3256 | 0 | "launched", |
3257 | 0 | data->info, |
3258 | 0 | platform_data); |
3259 | 0 | g_variant_unref (platform_data); |
3260 | 0 | } |
3261 | 0 | } |
3262 | |
|
3263 | 0 | if (data->callback) |
3264 | 0 | data->callback (object, result, data->user_data); |
3265 | 0 | else if (!g_task_had_error (G_TASK (result))) |
3266 | 0 | g_variant_unref (g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), |
3267 | 0 | result, NULL)); |
3268 | |
|
3269 | 0 | launch_uris_with_dbus_data_free (data); |
3270 | 0 | } |
3271 | | |
3272 | | static void |
3273 | | launch_uris_with_dbus (GDesktopAppInfo *info, |
3274 | | GDBusConnection *session_bus, |
3275 | | GList *uris, |
3276 | | GAppLaunchContext *launch_context, |
3277 | | GCancellable *cancellable, |
3278 | | GAsyncReadyCallback callback, |
3279 | | gpointer user_data) |
3280 | 0 | { |
3281 | 0 | GVariant *platform_data; |
3282 | 0 | GVariantBuilder builder; |
3283 | 0 | GVariantDict dict; |
3284 | 0 | gchar *object_path; |
3285 | 0 | LaunchUrisWithDBusData *data; |
3286 | |
|
3287 | 0 | g_variant_builder_init_static (&builder, G_VARIANT_TYPE_TUPLE); |
3288 | |
|
3289 | 0 | if (uris) |
3290 | 0 | { |
3291 | 0 | GList *iter; |
3292 | |
|
3293 | 0 | g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY); |
3294 | 0 | for (iter = uris; iter; iter = iter->next) |
3295 | 0 | g_variant_builder_add (&builder, "s", iter->data); |
3296 | 0 | g_variant_builder_close (&builder); |
3297 | 0 | } |
3298 | |
|
3299 | 0 | platform_data = g_desktop_app_info_make_platform_data (info, uris, launch_context); |
3300 | |
|
3301 | 0 | g_variant_builder_add_value (&builder, platform_data); |
3302 | 0 | object_path = object_path_from_appid (info->app_id); |
3303 | |
|
3304 | 0 | data = g_new0 (LaunchUrisWithDBusData, 1); |
3305 | 0 | data->info = g_object_ref (info); |
3306 | 0 | data->callback = callback; |
3307 | 0 | data->user_data = user_data; |
3308 | 0 | data->launch_context = launch_context ? g_object_ref (launch_context) : NULL; |
3309 | 0 | g_variant_dict_init (&dict, platform_data); |
3310 | 0 | g_variant_dict_lookup (&dict, "desktop-startup-id", "s", &data->startup_id); |
3311 | |
|
3312 | 0 | if (launch_context) |
3313 | 0 | emit_launch_started (launch_context, info, data->startup_id); |
3314 | |
|
3315 | 0 | g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application", |
3316 | 0 | uris ? "Open" : "Activate", g_variant_builder_end (&builder), |
3317 | 0 | NULL, G_DBUS_CALL_FLAGS_NONE, -1, |
3318 | 0 | cancellable, launch_uris_with_dbus_signal_cb, g_steal_pointer (&data)); |
3319 | 0 | g_free (object_path); |
3320 | |
|
3321 | 0 | g_variant_dict_clear (&dict); |
3322 | 0 | } |
3323 | | |
3324 | | static gboolean |
3325 | | g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info, |
3326 | | GDBusConnection *session_bus, |
3327 | | GList *uris, |
3328 | | GAppLaunchContext *launch_context, |
3329 | | GCancellable *cancellable, |
3330 | | GAsyncReadyCallback callback, |
3331 | | gpointer user_data) |
3332 | 0 | { |
3333 | 0 | GList *ruris = uris; |
3334 | 0 | char *app_id = NULL; |
3335 | |
|
3336 | 0 | g_return_val_if_fail (info != NULL, FALSE); |
3337 | | |
3338 | 0 | #ifdef G_OS_UNIX |
3339 | 0 | app_id = g_desktop_app_info_get_string (info, "X-Flatpak"); |
3340 | 0 | if (app_id && *app_id) |
3341 | 0 | { |
3342 | 0 | ruris = g_document_portal_add_documents (uris, app_id, NULL); |
3343 | 0 | if (ruris == NULL) |
3344 | 0 | ruris = uris; |
3345 | 0 | } |
3346 | 0 | #endif |
3347 | |
|
3348 | 0 | launch_uris_with_dbus (info, session_bus, ruris, launch_context, |
3349 | 0 | cancellable, callback, user_data); |
3350 | |
|
3351 | 0 | if (ruris != uris) |
3352 | 0 | g_list_free_full (ruris, g_free); |
3353 | |
|
3354 | 0 | g_free (app_id); |
3355 | |
|
3356 | 0 | return TRUE; |
3357 | 0 | } |
3358 | | |
3359 | | static gboolean |
3360 | | g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo, |
3361 | | GList *uris, |
3362 | | GAppLaunchContext *launch_context, |
3363 | | GSpawnFlags spawn_flags, |
3364 | | GSpawnChildSetupFunc user_setup, |
3365 | | gpointer user_setup_data, |
3366 | | GDesktopAppLaunchCallback pid_callback, |
3367 | | gpointer pid_callback_data, |
3368 | | gint stdin_fd, |
3369 | | gint stdout_fd, |
3370 | | gint stderr_fd, |
3371 | | GError **error) |
3372 | 0 | { |
3373 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
3374 | 0 | GDBusConnection *session_bus; |
3375 | 0 | gboolean success = TRUE; |
3376 | |
|
3377 | 0 | session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
3378 | |
|
3379 | 0 | if (session_bus && info->app_id) |
3380 | | /* This is non-blocking API. Similar to launching via fork()/exec() |
3381 | | * we don't wait around to see if the program crashed during startup. |
3382 | | * This is what startup-notification's job is... |
3383 | | */ |
3384 | 0 | g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context, |
3385 | 0 | NULL, NULL, NULL); |
3386 | 0 | else |
3387 | 0 | success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context, |
3388 | 0 | spawn_flags, user_setup, user_setup_data, |
3389 | 0 | pid_callback, pid_callback_data, |
3390 | 0 | stdin_fd, stdout_fd, stderr_fd, error); |
3391 | |
|
3392 | 0 | if (session_bus != NULL) |
3393 | 0 | { |
3394 | | /* This asynchronous flush holds a reference until it completes, |
3395 | | * which ensures that the following unref won't immediately kill |
3396 | | * the connection if we were the initial owner. |
3397 | | */ |
3398 | 0 | g_dbus_connection_flush (session_bus, NULL, NULL, NULL); |
3399 | 0 | g_object_unref (session_bus); |
3400 | 0 | } |
3401 | |
|
3402 | 0 | return success; |
3403 | 0 | } |
3404 | | |
3405 | | static gboolean |
3406 | | g_desktop_app_info_launch_uris (GAppInfo *appinfo, |
3407 | | GList *uris, |
3408 | | GAppLaunchContext *launch_context, |
3409 | | GError **error) |
3410 | 0 | { |
3411 | 0 | return g_desktop_app_info_launch_uris_internal (appinfo, uris, |
3412 | 0 | launch_context, |
3413 | 0 | _SPAWN_FLAGS_DEFAULT, |
3414 | 0 | NULL, NULL, NULL, NULL, |
3415 | 0 | -1, -1, -1, |
3416 | 0 | error); |
3417 | 0 | } |
3418 | | |
3419 | | typedef struct |
3420 | | { |
3421 | | GList *uris; /* (element-type utf8) (owned) (nullable) */ |
3422 | | GAppLaunchContext *context; /* (owned) (nullable) */ |
3423 | | } LaunchUrisData; |
3424 | | |
3425 | | static void |
3426 | | launch_uris_data_free (LaunchUrisData *data) |
3427 | 0 | { |
3428 | 0 | g_clear_object (&data->context); |
3429 | 0 | g_list_free_full (data->uris, g_free); |
3430 | 0 | g_free (data); |
3431 | 0 | } |
3432 | | |
3433 | | static void |
3434 | | launch_uris_with_dbus_cb (GObject *object, |
3435 | | GAsyncResult *result, |
3436 | | gpointer user_data) |
3437 | 0 | { |
3438 | 0 | GTask *task = G_TASK (user_data); |
3439 | 0 | GError *local_error = NULL; |
3440 | 0 | GVariant *ret; |
3441 | |
|
3442 | 0 | ret = g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &local_error); |
3443 | 0 | if (local_error != NULL) |
3444 | 0 | { |
3445 | 0 | g_dbus_error_strip_remote_error (local_error); |
3446 | 0 | g_task_return_error (task, g_steal_pointer (&local_error)); |
3447 | 0 | } |
3448 | 0 | else |
3449 | 0 | { |
3450 | 0 | g_task_return_boolean (task, TRUE); |
3451 | 0 | g_variant_unref (ret); |
3452 | 0 | } |
3453 | |
|
3454 | 0 | g_object_unref (task); |
3455 | 0 | } |
3456 | | |
3457 | | static void |
3458 | | launch_uris_flush_cb (GObject *object, |
3459 | | GAsyncResult *result, |
3460 | | gpointer user_data) |
3461 | 0 | { |
3462 | 0 | GTask *task = G_TASK (user_data); |
3463 | |
|
3464 | 0 | g_dbus_connection_flush_finish (G_DBUS_CONNECTION (object), result, NULL); |
3465 | 0 | g_task_return_boolean (task, TRUE); |
3466 | 0 | g_object_unref (task); |
3467 | 0 | } |
3468 | | |
3469 | | static void |
3470 | | launch_uris_bus_get_cb (GObject *object, |
3471 | | GAsyncResult *result, |
3472 | | gpointer user_data) |
3473 | 0 | { |
3474 | 0 | GTask *task = G_TASK (user_data); |
3475 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (g_task_get_source_object (task)); |
3476 | 0 | LaunchUrisData *data = g_task_get_task_data (task); |
3477 | 0 | GCancellable *cancellable = g_task_get_cancellable (task); |
3478 | 0 | GDBusConnection *session_bus; |
3479 | 0 | GError *local_error = NULL; |
3480 | |
|
3481 | 0 | session_bus = g_bus_get_finish (result, NULL); |
3482 | |
|
3483 | 0 | if (session_bus && info->app_id) |
3484 | 0 | { |
3485 | | /* FIXME: The g_document_portal_add_documents() function, which is called |
3486 | | * from the g_desktop_app_info_launch_uris_with_dbus() function, still |
3487 | | * uses blocking calls. |
3488 | | */ |
3489 | 0 | g_desktop_app_info_launch_uris_with_dbus (info, session_bus, |
3490 | 0 | data->uris, data->context, |
3491 | 0 | cancellable, |
3492 | 0 | launch_uris_with_dbus_cb, |
3493 | 0 | g_steal_pointer (&task)); |
3494 | 0 | } |
3495 | 0 | else |
3496 | 0 | { |
3497 | | /* FIXME: The D-Bus message from the notify_desktop_launch() function |
3498 | | * can be still lost even if flush is called later. See: |
3499 | | * https://gitlab.freedesktop.org/dbus/dbus/issues/72 |
3500 | | */ |
3501 | 0 | g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, |
3502 | 0 | data->uris, data->context, |
3503 | 0 | _SPAWN_FLAGS_DEFAULT, NULL, |
3504 | 0 | NULL, NULL, NULL, -1, -1, -1, |
3505 | 0 | &local_error); |
3506 | 0 | if (local_error != NULL) |
3507 | 0 | { |
3508 | 0 | g_task_return_error (task, g_steal_pointer (&local_error)); |
3509 | 0 | g_object_unref (task); |
3510 | 0 | } |
3511 | 0 | else if (session_bus) |
3512 | 0 | g_dbus_connection_flush (session_bus, |
3513 | 0 | cancellable, |
3514 | 0 | launch_uris_flush_cb, |
3515 | 0 | g_steal_pointer (&task)); |
3516 | 0 | else |
3517 | 0 | { |
3518 | 0 | g_task_return_boolean (task, TRUE); |
3519 | 0 | g_clear_object (&task); |
3520 | 0 | } |
3521 | 0 | } |
3522 | |
|
3523 | 0 | g_clear_object (&session_bus); |
3524 | 0 | } |
3525 | | |
3526 | | static void |
3527 | | g_desktop_app_info_launch_uris_async (GAppInfo *appinfo, |
3528 | | GList *uris, |
3529 | | GAppLaunchContext *context, |
3530 | | GCancellable *cancellable, |
3531 | | GAsyncReadyCallback callback, |
3532 | | gpointer user_data) |
3533 | 0 | { |
3534 | 0 | GTask *task; |
3535 | 0 | LaunchUrisData *data; |
3536 | |
|
3537 | 0 | task = g_task_new (appinfo, cancellable, callback, user_data); |
3538 | 0 | g_task_set_source_tag (task, g_desktop_app_info_launch_uris_async); |
3539 | |
|
3540 | 0 | data = g_new0 (LaunchUrisData, 1); |
3541 | 0 | data->uris = g_list_copy_deep (uris, (GCopyFunc) g_strdup, NULL); |
3542 | 0 | g_set_object (&data->context, context); |
3543 | 0 | g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) launch_uris_data_free); |
3544 | |
|
3545 | 0 | g_bus_get (G_BUS_TYPE_SESSION, cancellable, launch_uris_bus_get_cb, task); |
3546 | 0 | } |
3547 | | |
3548 | | static gboolean |
3549 | | g_desktop_app_info_launch_uris_finish (GAppInfo *appinfo, |
3550 | | GAsyncResult *result, |
3551 | | GError **error) |
3552 | 0 | { |
3553 | 0 | g_return_val_if_fail (g_task_is_valid (result, appinfo), FALSE); |
3554 | | |
3555 | 0 | return g_task_propagate_boolean (G_TASK (result), error); |
3556 | 0 | } |
3557 | | |
3558 | | static gboolean |
3559 | | g_desktop_app_info_supports_uris (GAppInfo *appinfo) |
3560 | 0 | { |
3561 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
3562 | |
|
3563 | 0 | return info->exec && |
3564 | 0 | ((strstr (info->exec, "%u") != NULL) || |
3565 | 0 | (strstr (info->exec, "%U") != NULL)); |
3566 | 0 | } |
3567 | | |
3568 | | static gboolean |
3569 | | g_desktop_app_info_supports_files (GAppInfo *appinfo) |
3570 | 0 | { |
3571 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
3572 | |
|
3573 | 0 | return info->exec && |
3574 | 0 | ((strstr (info->exec, "%f") != NULL) || |
3575 | 0 | (strstr (info->exec, "%F") != NULL)); |
3576 | 0 | } |
3577 | | |
3578 | | static gboolean |
3579 | | g_desktop_app_info_launch (GAppInfo *appinfo, |
3580 | | GList *files, |
3581 | | GAppLaunchContext *launch_context, |
3582 | | GError **error) |
3583 | 0 | { |
3584 | 0 | GList *uris; |
3585 | 0 | char *uri; |
3586 | 0 | gboolean res; |
3587 | |
|
3588 | 0 | uris = NULL; |
3589 | 0 | while (files) |
3590 | 0 | { |
3591 | 0 | uri = g_file_get_uri (files->data); |
3592 | 0 | uris = g_list_prepend (uris, uri); |
3593 | 0 | files = files->next; |
3594 | 0 | } |
3595 | |
|
3596 | 0 | uris = g_list_reverse (uris); |
3597 | |
|
3598 | 0 | res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error); |
3599 | |
|
3600 | 0 | g_list_free_full (uris, g_free); |
3601 | |
|
3602 | 0 | return res; |
3603 | 0 | } |
3604 | | |
3605 | | /** |
3606 | | * g_desktop_app_info_launch_uris_as_manager_with_fds: |
3607 | | * @appinfo: a [class@GioUnix.DesktopAppInfo] |
3608 | | * @uris: (element-type utf8): List of URIs |
3609 | | * @launch_context: (nullable): a [class@Gio.AppLaunchContext] |
3610 | | * @spawn_flags: [flags@GLib.SpawnFlags], used for each process |
3611 | | * @user_setup: (scope async) (nullable) (closure user_setup_data): a |
3612 | | * [callback@GLib.SpawnChildSetupFunc], used once for each process. |
3613 | | * @user_setup_data: User data for @user_setup |
3614 | | * @pid_callback: (scope call) (nullable) (closure pid_callback_data): Callback for child processes |
3615 | | * @pid_callback_data: User data for @callback |
3616 | | * @stdin_fd: file descriptor to use for child’s stdin, or `-1` |
3617 | | * @stdout_fd: file descriptor to use for child’s stdout, or `-1` |
3618 | | * @stderr_fd: file descriptor to use for child’s stderr, or `-1` |
3619 | | * @error: return location for a #GError, or `NULL` |
3620 | | * |
3621 | | * Equivalent to [method@GioUnix.DesktopAppInfo.launch_uris_as_manager] but |
3622 | | * allows you to pass in file descriptors for the stdin, stdout and stderr |
3623 | | * streams of the launched process. |
3624 | | * |
3625 | | * If application launching occurs via some non-spawn mechanism (e.g. D-Bus |
3626 | | * activation) then @stdin_fd, @stdout_fd and @stderr_fd are ignored. |
3627 | | * |
3628 | | * Returns: `TRUE` on successful launch, `FALSE` otherwise. |
3629 | | * |
3630 | | * Since: 2.58 |
3631 | | */ |
3632 | | gboolean |
3633 | | g_desktop_app_info_launch_uris_as_manager_with_fds (GDesktopAppInfo *appinfo, |
3634 | | GList *uris, |
3635 | | GAppLaunchContext *launch_context, |
3636 | | GSpawnFlags spawn_flags, |
3637 | | GSpawnChildSetupFunc user_setup, |
3638 | | gpointer user_setup_data, |
3639 | | GDesktopAppLaunchCallback pid_callback, |
3640 | | gpointer pid_callback_data, |
3641 | | gint stdin_fd, |
3642 | | gint stdout_fd, |
3643 | | gint stderr_fd, |
3644 | | GError **error) |
3645 | 0 | { |
3646 | 0 | return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo, |
3647 | 0 | uris, |
3648 | 0 | launch_context, |
3649 | 0 | spawn_flags, |
3650 | 0 | user_setup, |
3651 | 0 | user_setup_data, |
3652 | 0 | pid_callback, |
3653 | 0 | pid_callback_data, |
3654 | 0 | stdin_fd, |
3655 | 0 | stdout_fd, |
3656 | 0 | stderr_fd, |
3657 | 0 | error); |
3658 | 0 | } |
3659 | | |
3660 | | /** |
3661 | | * g_desktop_app_info_launch_uris_as_manager: |
3662 | | * @appinfo: a [class@GioUnix.DesktopAppInfo] |
3663 | | * @uris: (element-type utf8): List of URIs |
3664 | | * @launch_context: (nullable): a [class@Gio.AppLaunchContext] |
3665 | | * @spawn_flags: [flags@GLib.SpawnFlags], used for each process |
3666 | | * @user_setup: (scope async) (nullable): a [callback@GLib.SpawnChildSetupFunc], |
3667 | | * used once for each process. |
3668 | | * @user_setup_data: (closure user_setup) (nullable): User data for @user_setup |
3669 | | * @pid_callback: (scope call) (nullable): Callback for child processes |
3670 | | * @pid_callback_data: (closure pid_callback) (nullable): User data for @callback |
3671 | | * @error: return location for a #GError, or `NULL` |
3672 | | * |
3673 | | * This function performs the equivalent of [method@Gio.AppInfo.launch_uris], |
3674 | | * but is intended primarily for operating system components that |
3675 | | * launch applications. Ordinary applications should use |
3676 | | * [method@Gio.AppInfo.launch_uris]. |
3677 | | * |
3678 | | * If the application is launched via GSpawn, then @spawn_flags, @user_setup |
3679 | | * and @user_setup_data are used for the call to [func@GLib.spawn_async]. |
3680 | | * Additionally, @pid_callback (with @pid_callback_data) will be called to |
3681 | | * inform about the PID of the created process. See |
3682 | | * [func@GLib.spawn_async_with_pipes] for information on certain parameter |
3683 | | * conditions that can enable an optimized [`posix_spawn()`](man:posix_spawn(3)) |
3684 | | * code path to be used. |
3685 | | * |
3686 | | * If application launching occurs via some other mechanism (for example, D-Bus |
3687 | | * activation) then @spawn_flags, @user_setup, @user_setup_data, |
3688 | | * @pid_callback and @pid_callback_data are ignored. |
3689 | | * |
3690 | | * Returns: `TRUE` on successful launch, `FALSE` otherwise. |
3691 | | */ |
3692 | | gboolean |
3693 | | g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo, |
3694 | | GList *uris, |
3695 | | GAppLaunchContext *launch_context, |
3696 | | GSpawnFlags spawn_flags, |
3697 | | GSpawnChildSetupFunc user_setup, |
3698 | | gpointer user_setup_data, |
3699 | | GDesktopAppLaunchCallback pid_callback, |
3700 | | gpointer pid_callback_data, |
3701 | | GError **error) |
3702 | 0 | { |
3703 | 0 | return g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo, |
3704 | 0 | uris, |
3705 | 0 | launch_context, |
3706 | 0 | spawn_flags, |
3707 | 0 | user_setup, |
3708 | 0 | user_setup_data, |
3709 | 0 | pid_callback, |
3710 | 0 | pid_callback_data, |
3711 | 0 | -1, -1, -1, |
3712 | 0 | error); |
3713 | 0 | } |
3714 | | |
3715 | | /* OnlyShowIn API support {{{2 */ |
3716 | | |
3717 | | /** |
3718 | | * g_desktop_app_info_set_desktop_env: |
3719 | | * @desktop_env: a string specifying what desktop this is |
3720 | | * |
3721 | | * Sets the name of the desktop that the application is running in. |
3722 | | * |
3723 | | * This is used by [method@Gio.AppInfo.should_show] and |
3724 | | * [method@GioUnix.DesktopAppInfo.get_show_in] to evaluate the |
3725 | | * [`OnlyShowIn`](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html#key-onlyshowin) |
3726 | | * and [`NotShowIn`](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html#key-notshowin) |
3727 | | * keys. |
3728 | | * |
3729 | | * Should be called only once; subsequent calls are ignored. |
3730 | | * |
3731 | | * Deprecated:2.42:do not use this API. Since 2.42 the value of the |
3732 | | * `XDG_CURRENT_DESKTOP` environment variable will be used. |
3733 | | */ |
3734 | | void |
3735 | | g_desktop_app_info_set_desktop_env (const gchar *desktop_env) |
3736 | 0 | { |
3737 | 0 | get_current_desktops (desktop_env); |
3738 | 0 | } |
3739 | | |
3740 | | static gboolean |
3741 | | g_desktop_app_info_should_show (GAppInfo *appinfo) |
3742 | 0 | { |
3743 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
3744 | |
|
3745 | 0 | if (info->nodisplay) |
3746 | 0 | return FALSE; |
3747 | | |
3748 | 0 | return g_desktop_app_info_get_show_in (info, NULL); |
3749 | 0 | } |
3750 | | |
3751 | | /* mime types/default apps support {{{2 */ |
3752 | | |
3753 | | typedef enum { |
3754 | | CONF_DIR, |
3755 | | APP_DIR, |
3756 | | MIMETYPE_DIR |
3757 | | } DirType; |
3758 | | |
3759 | | static char * |
3760 | | ensure_dir (DirType type, |
3761 | | GError **error) |
3762 | 0 | { |
3763 | 0 | char *path, *display_name; |
3764 | 0 | int errsv; |
3765 | |
|
3766 | 0 | switch (type) |
3767 | 0 | { |
3768 | 0 | case CONF_DIR: |
3769 | 0 | path = g_build_filename (g_get_user_config_dir (), NULL); |
3770 | 0 | break; |
3771 | | |
3772 | 0 | case APP_DIR: |
3773 | 0 | path = g_build_filename (g_get_user_data_dir (), "applications", NULL); |
3774 | 0 | break; |
3775 | | |
3776 | 0 | case MIMETYPE_DIR: |
3777 | 0 | path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL); |
3778 | 0 | break; |
3779 | | |
3780 | 0 | default: |
3781 | 0 | g_assert_not_reached (); |
3782 | 0 | } |
3783 | | |
3784 | 0 | g_debug ("%s: Ensuring %s", G_STRFUNC, path); |
3785 | |
|
3786 | 0 | errno = 0; |
3787 | 0 | if (g_mkdir_with_parents (path, 0700) == 0) |
3788 | 0 | return path; |
3789 | | |
3790 | 0 | errsv = errno; |
3791 | 0 | display_name = g_filename_display_name (path); |
3792 | 0 | if (type == APP_DIR) |
3793 | 0 | g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), |
3794 | 0 | _("Can’t create user application configuration folder %s: %s"), |
3795 | 0 | display_name, g_strerror (errsv)); |
3796 | 0 | else |
3797 | 0 | g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), |
3798 | 0 | _("Can’t create user MIME configuration folder %s: %s"), |
3799 | 0 | display_name, g_strerror (errsv)); |
3800 | |
|
3801 | 0 | g_free (display_name); |
3802 | 0 | g_free (path); |
3803 | |
|
3804 | 0 | return NULL; |
3805 | 0 | } |
3806 | | |
3807 | | static gboolean |
3808 | | update_mimeapps_list (const char *desktop_id, |
3809 | | const char *content_type, |
3810 | | UpdateMimeFlags flags, |
3811 | | GError **error) |
3812 | 0 | { |
3813 | 0 | char *dirname, *old_filename, *filename, *string; |
3814 | 0 | GKeyFile *key_file; |
3815 | 0 | gboolean load_succeeded, res; |
3816 | 0 | char **old_list, **list; |
3817 | 0 | gsize length, data_size; |
3818 | 0 | char *data; |
3819 | 0 | int i, j, k; |
3820 | 0 | char **content_types; |
3821 | | |
3822 | | /* Don't add both at start and end */ |
3823 | 0 | g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) && |
3824 | 0 | (flags & UPDATE_MIME_SET_NON_DEFAULT))); |
3825 | | |
3826 | 0 | dirname = ensure_dir (CONF_DIR, error); |
3827 | 0 | if (!dirname) |
3828 | 0 | return FALSE; |
3829 | | |
3830 | 0 | filename = g_build_filename (dirname, "mimeapps.list", NULL); |
3831 | |
|
3832 | 0 | while (g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) |
3833 | 0 | { |
3834 | 0 | old_filename = filename; |
3835 | 0 | filename = g_file_read_link (old_filename, error); |
3836 | 0 | g_free (old_filename); |
3837 | 0 | if (filename == NULL) |
3838 | 0 | return FALSE; |
3839 | 0 | } |
3840 | | |
3841 | 0 | g_free (dirname); |
3842 | |
|
3843 | 0 | key_file = g_key_file_new (); |
3844 | 0 | load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL); |
3845 | 0 | if (!load_succeeded || |
3846 | 0 | (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) && |
3847 | 0 | !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) && |
3848 | 0 | !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))) |
3849 | 0 | { |
3850 | 0 | g_key_file_free (key_file); |
3851 | 0 | key_file = g_key_file_new (); |
3852 | 0 | } |
3853 | |
|
3854 | 0 | if (content_type) |
3855 | 0 | { |
3856 | 0 | content_types = g_new (char *, 2); |
3857 | 0 | content_types[0] = g_strdup (content_type); |
3858 | 0 | content_types[1] = NULL; |
3859 | 0 | } |
3860 | 0 | else |
3861 | 0 | { |
3862 | 0 | content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL); |
3863 | 0 | } |
3864 | |
|
3865 | 0 | for (k = 0; content_types && content_types[k]; k++) |
3866 | 0 | { |
3867 | | /* set as default, if requested so */ |
3868 | 0 | string = g_key_file_get_string (key_file, |
3869 | 0 | DEFAULT_APPLICATIONS_GROUP, |
3870 | 0 | content_types[k], |
3871 | 0 | NULL); |
3872 | |
|
3873 | 0 | if (g_strcmp0 (string, desktop_id) != 0 && |
3874 | 0 | (flags & UPDATE_MIME_SET_DEFAULT)) |
3875 | 0 | { |
3876 | 0 | g_free (string); |
3877 | 0 | string = g_strdup (desktop_id); |
3878 | | |
3879 | | /* add in the non-default list too, if it's not already there */ |
3880 | 0 | flags |= UPDATE_MIME_SET_NON_DEFAULT; |
3881 | 0 | } |
3882 | |
|
3883 | 0 | if (string == NULL || desktop_id == NULL) |
3884 | 0 | g_key_file_remove_key (key_file, |
3885 | 0 | DEFAULT_APPLICATIONS_GROUP, |
3886 | 0 | content_types[k], |
3887 | 0 | NULL); |
3888 | 0 | else |
3889 | 0 | g_key_file_set_string (key_file, |
3890 | 0 | DEFAULT_APPLICATIONS_GROUP, |
3891 | 0 | content_types[k], |
3892 | 0 | string); |
3893 | |
|
3894 | 0 | g_free (string); |
3895 | 0 | } |
3896 | |
|
3897 | 0 | if (content_type) |
3898 | 0 | { |
3899 | | /* reuse the list from above */ |
3900 | 0 | } |
3901 | 0 | else |
3902 | 0 | { |
3903 | 0 | g_strfreev (content_types); |
3904 | 0 | content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL); |
3905 | 0 | } |
3906 | |
|
3907 | 0 | for (k = 0; content_types && content_types[k]; k++) |
3908 | 0 | { |
3909 | | /* Add to the right place in the list */ |
3910 | |
|
3911 | 0 | length = 0; |
3912 | 0 | old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP, |
3913 | 0 | content_types[k], &length, NULL); |
3914 | |
|
3915 | 0 | list = g_new (char *, 1 + length + 1); |
3916 | |
|
3917 | 0 | i = 0; |
3918 | | |
3919 | | /* if we're adding a last-used hint, just put the application in front of the list */ |
3920 | 0 | if (flags & UPDATE_MIME_SET_LAST_USED) |
3921 | 0 | { |
3922 | | /* avoid adding this again as non-default later */ |
3923 | 0 | if (flags & UPDATE_MIME_SET_NON_DEFAULT) |
3924 | 0 | flags ^= UPDATE_MIME_SET_NON_DEFAULT; |
3925 | |
|
3926 | 0 | list[i++] = g_strdup (desktop_id); |
3927 | 0 | } |
3928 | |
|
3929 | 0 | if (old_list) |
3930 | 0 | { |
3931 | 0 | for (j = 0; old_list[j] != NULL; j++) |
3932 | 0 | { |
3933 | 0 | if (g_strcmp0 (old_list[j], desktop_id) != 0) |
3934 | 0 | { |
3935 | | /* rewrite other entries if they're different from the new one */ |
3936 | 0 | list[i++] = g_strdup (old_list[j]); |
3937 | 0 | } |
3938 | 0 | else if (flags & UPDATE_MIME_SET_NON_DEFAULT) |
3939 | 0 | { |
3940 | | /* we encountered an old entry which is equal to the one we're adding as non-default, |
3941 | | * don't change its position in the list. |
3942 | | */ |
3943 | 0 | flags ^= UPDATE_MIME_SET_NON_DEFAULT; |
3944 | 0 | list[i++] = g_strdup (old_list[j]); |
3945 | 0 | } |
3946 | 0 | } |
3947 | 0 | } |
3948 | | |
3949 | | /* add it at the end of the list */ |
3950 | 0 | if (flags & UPDATE_MIME_SET_NON_DEFAULT) |
3951 | 0 | list[i++] = g_strdup (desktop_id); |
3952 | |
|
3953 | 0 | list[i] = NULL; |
3954 | |
|
3955 | 0 | g_strfreev (old_list); |
3956 | |
|
3957 | 0 | if (list[0] == NULL || desktop_id == NULL) |
3958 | 0 | g_key_file_remove_key (key_file, |
3959 | 0 | ADDED_ASSOCIATIONS_GROUP, |
3960 | 0 | content_types[k], |
3961 | 0 | NULL); |
3962 | 0 | else |
3963 | 0 | g_key_file_set_string_list (key_file, |
3964 | 0 | ADDED_ASSOCIATIONS_GROUP, |
3965 | 0 | content_types[k], |
3966 | 0 | (const char * const *)list, i); |
3967 | |
|
3968 | 0 | g_strfreev (list); |
3969 | 0 | } |
3970 | |
|
3971 | 0 | if (content_type) |
3972 | 0 | { |
3973 | | /* reuse the list from above */ |
3974 | 0 | } |
3975 | 0 | else |
3976 | 0 | { |
3977 | 0 | g_strfreev (content_types); |
3978 | 0 | content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL); |
3979 | 0 | } |
3980 | |
|
3981 | 0 | for (k = 0; content_types && content_types[k]; k++) |
3982 | 0 | { |
3983 | | /* Remove from removed associations group (unless remove) */ |
3984 | |
|
3985 | 0 | length = 0; |
3986 | 0 | old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP, |
3987 | 0 | content_types[k], &length, NULL); |
3988 | |
|
3989 | 0 | list = g_new (char *, 1 + length + 1); |
3990 | |
|
3991 | 0 | i = 0; |
3992 | 0 | if (flags & UPDATE_MIME_REMOVE) |
3993 | 0 | list[i++] = g_strdup (desktop_id); |
3994 | 0 | if (old_list) |
3995 | 0 | { |
3996 | 0 | for (j = 0; old_list[j] != NULL; j++) |
3997 | 0 | { |
3998 | 0 | if (g_strcmp0 (old_list[j], desktop_id) != 0) |
3999 | 0 | list[i++] = g_strdup (old_list[j]); |
4000 | 0 | } |
4001 | 0 | } |
4002 | 0 | list[i] = NULL; |
4003 | |
|
4004 | 0 | g_strfreev (old_list); |
4005 | |
|
4006 | 0 | if (list[0] == NULL || desktop_id == NULL) |
4007 | 0 | g_key_file_remove_key (key_file, |
4008 | 0 | REMOVED_ASSOCIATIONS_GROUP, |
4009 | 0 | content_types[k], |
4010 | 0 | NULL); |
4011 | 0 | else |
4012 | 0 | g_key_file_set_string_list (key_file, |
4013 | 0 | REMOVED_ASSOCIATIONS_GROUP, |
4014 | 0 | content_types[k], |
4015 | 0 | (const char * const *)list, i); |
4016 | |
|
4017 | 0 | g_strfreev (list); |
4018 | 0 | } |
4019 | |
|
4020 | 0 | g_strfreev (content_types); |
4021 | |
|
4022 | 0 | data = g_key_file_to_data (key_file, &data_size, error); |
4023 | 0 | g_key_file_free (key_file); |
4024 | |
|
4025 | 0 | res = g_file_set_contents_full (filename, data, data_size, |
4026 | 0 | G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING, |
4027 | 0 | 0600, error); |
4028 | |
|
4029 | 0 | desktop_file_dirs_invalidate_user_config (); |
4030 | |
|
4031 | 0 | g_free (filename); |
4032 | 0 | g_free (data); |
4033 | |
|
4034 | 0 | return res; |
4035 | 0 | } |
4036 | | |
4037 | | static gboolean |
4038 | | g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo, |
4039 | | const char *content_type, |
4040 | | GError **error) |
4041 | 0 | { |
4042 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
4043 | |
|
4044 | 0 | if (!g_desktop_app_info_ensure_saved (info, error)) |
4045 | 0 | return FALSE; |
4046 | | |
4047 | 0 | if (!info->desktop_id) |
4048 | 0 | { |
4049 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
4050 | 0 | _("Application information lacks an identifier")); |
4051 | 0 | return FALSE; |
4052 | 0 | } |
4053 | | |
4054 | | /* both add support for the content type and set as last used */ |
4055 | 0 | return update_mimeapps_list (info->desktop_id, content_type, |
4056 | 0 | UPDATE_MIME_SET_NON_DEFAULT | |
4057 | 0 | UPDATE_MIME_SET_LAST_USED, |
4058 | 0 | error); |
4059 | 0 | } |
4060 | | |
4061 | | static gboolean |
4062 | | g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo, |
4063 | | const char *content_type, |
4064 | | GError **error) |
4065 | 0 | { |
4066 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
4067 | |
|
4068 | 0 | if (!g_desktop_app_info_ensure_saved (info, error)) |
4069 | 0 | return FALSE; |
4070 | | |
4071 | 0 | if (!info->desktop_id) |
4072 | 0 | { |
4073 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
4074 | 0 | _("Application information lacks an identifier")); |
4075 | 0 | return FALSE; |
4076 | 0 | } |
4077 | | |
4078 | 0 | return update_mimeapps_list (info->desktop_id, content_type, |
4079 | 0 | UPDATE_MIME_SET_DEFAULT, |
4080 | 0 | error); |
4081 | 0 | } |
4082 | | |
4083 | | static void |
4084 | | update_program_done (GPid pid, |
4085 | | gint status, |
4086 | | gpointer data) |
4087 | 0 | { |
4088 | | /* Did the application exit correctly */ |
4089 | 0 | if (g_spawn_check_wait_status (status, NULL)) |
4090 | 0 | { |
4091 | | /* Here we could clean out any caches in use */ |
4092 | 0 | } |
4093 | 0 | } |
4094 | | |
4095 | | static void |
4096 | | run_update_command (char *command, |
4097 | | char *subdir) |
4098 | 0 | { |
4099 | 0 | char *argv[3] = { |
4100 | 0 | NULL, |
4101 | 0 | NULL, |
4102 | 0 | NULL, |
4103 | 0 | }; |
4104 | 0 | GPid pid = 0; |
4105 | 0 | GError *local_error = NULL; |
4106 | |
|
4107 | 0 | argv[0] = command; |
4108 | 0 | argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL); |
4109 | |
|
4110 | 0 | if (g_spawn_async ("/", argv, |
4111 | 0 | NULL, /* envp */ |
4112 | 0 | G_SPAWN_SEARCH_PATH | |
4113 | 0 | G_SPAWN_STDOUT_TO_DEV_NULL | |
4114 | 0 | G_SPAWN_STDERR_TO_DEV_NULL | |
4115 | 0 | G_SPAWN_DO_NOT_REAP_CHILD, |
4116 | 0 | NULL, NULL, /* No setup function */ |
4117 | 0 | &pid, |
4118 | 0 | &local_error)) |
4119 | 0 | g_child_watch_add (pid, update_program_done, NULL); |
4120 | 0 | else |
4121 | 0 | { |
4122 | | /* If we get an error at this point, it's quite likely the user doesn't |
4123 | | * have an installed copy of either 'update-mime-database' or |
4124 | | * 'update-desktop-database'. I don't think we want to popup an error |
4125 | | * dialog at this point, so we just do a g_warning to give the user a |
4126 | | * chance of debugging it. |
4127 | | */ |
4128 | 0 | g_warning ("%s", local_error->message); |
4129 | 0 | g_error_free (local_error); |
4130 | 0 | } |
4131 | |
|
4132 | 0 | g_free (argv[1]); |
4133 | 0 | } |
4134 | | |
4135 | | static gboolean |
4136 | | g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo, |
4137 | | const char *extension, |
4138 | | GError **error) |
4139 | 0 | { |
4140 | 0 | char *filename, *basename, *mimetype; |
4141 | 0 | char *dirname; |
4142 | 0 | gboolean res; |
4143 | |
|
4144 | 0 | if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error)) |
4145 | 0 | return FALSE; |
4146 | | |
4147 | 0 | dirname = ensure_dir (MIMETYPE_DIR, error); |
4148 | 0 | if (!dirname) |
4149 | 0 | return FALSE; |
4150 | | |
4151 | 0 | basename = g_strdup_printf ("user-extension-%s.xml", extension); |
4152 | 0 | filename = g_build_filename (dirname, basename, NULL); |
4153 | 0 | g_free (basename); |
4154 | 0 | g_free (dirname); |
4155 | |
|
4156 | 0 | mimetype = g_strdup_printf ("application/x-extension-%s", extension); |
4157 | |
|
4158 | 0 | if (!g_file_test (filename, G_FILE_TEST_EXISTS)) |
4159 | 0 | { |
4160 | 0 | char *contents; |
4161 | |
|
4162 | 0 | contents = |
4163 | 0 | g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
4164 | 0 | "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n" |
4165 | 0 | " <mime-type type=\"%s\">\n" |
4166 | 0 | " <comment>%s document</comment>\n" |
4167 | 0 | " <glob pattern=\"*.%s\"/>\n" |
4168 | 0 | " </mime-type>\n" |
4169 | 0 | "</mime-info>\n", mimetype, extension, extension); |
4170 | |
|
4171 | 0 | g_file_set_contents_full (filename, contents, -1, |
4172 | 0 | G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING, |
4173 | 0 | 0600, NULL); |
4174 | 0 | g_free (contents); |
4175 | |
|
4176 | 0 | run_update_command ("update-mime-database", "mime"); |
4177 | 0 | } |
4178 | 0 | g_free (filename); |
4179 | |
|
4180 | 0 | res = g_desktop_app_info_set_as_default_for_type (appinfo, |
4181 | 0 | mimetype, |
4182 | 0 | error); |
4183 | |
|
4184 | 0 | g_free (mimetype); |
4185 | |
|
4186 | 0 | return res; |
4187 | 0 | } |
4188 | | |
4189 | | static gboolean |
4190 | | g_desktop_app_info_add_supports_type (GAppInfo *appinfo, |
4191 | | const char *content_type, |
4192 | | GError **error) |
4193 | 0 | { |
4194 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
4195 | |
|
4196 | 0 | if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error)) |
4197 | 0 | return FALSE; |
4198 | | |
4199 | 0 | return update_mimeapps_list (info->desktop_id, content_type, |
4200 | 0 | UPDATE_MIME_SET_NON_DEFAULT, |
4201 | 0 | error); |
4202 | 0 | } |
4203 | | |
4204 | | static gboolean |
4205 | | g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo) |
4206 | 0 | { |
4207 | 0 | return TRUE; |
4208 | 0 | } |
4209 | | |
4210 | | static gboolean |
4211 | | g_desktop_app_info_remove_supports_type (GAppInfo *appinfo, |
4212 | | const char *content_type, |
4213 | | GError **error) |
4214 | 0 | { |
4215 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
4216 | |
|
4217 | 0 | if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error)) |
4218 | 0 | return FALSE; |
4219 | | |
4220 | 0 | return update_mimeapps_list (info->desktop_id, content_type, |
4221 | 0 | UPDATE_MIME_REMOVE, |
4222 | 0 | error); |
4223 | 0 | } |
4224 | | |
4225 | | static const char ** |
4226 | | g_desktop_app_info_get_supported_types (GAppInfo *appinfo) |
4227 | 0 | { |
4228 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
4229 | |
|
4230 | 0 | return (const char**) info->mime_types; |
4231 | 0 | } |
4232 | | |
4233 | | /* Saving and deleting {{{2 */ |
4234 | | |
4235 | | static gboolean |
4236 | | g_desktop_app_info_ensure_saved (GDesktopAppInfo *info, |
4237 | | GError **error) |
4238 | 0 | { |
4239 | 0 | GKeyFile *key_file; |
4240 | 0 | char *dirname; |
4241 | 0 | char *filename; |
4242 | 0 | char *data, *desktop_id; |
4243 | 0 | gsize data_size; |
4244 | 0 | int fd; |
4245 | 0 | gboolean res; |
4246 | |
|
4247 | 0 | if (info->filename != NULL) |
4248 | 0 | return TRUE; |
4249 | | |
4250 | | /* This is only used for object created with |
4251 | | * g_app_info_create_from_commandline. All other |
4252 | | * object should have a filename |
4253 | | */ |
4254 | | |
4255 | 0 | dirname = ensure_dir (APP_DIR, error); |
4256 | 0 | if (!dirname) |
4257 | 0 | return FALSE; |
4258 | | |
4259 | 0 | key_file = g_key_file_new (); |
4260 | |
|
4261 | 0 | g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4262 | 0 | "Encoding", "UTF-8"); |
4263 | 0 | g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4264 | 0 | G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0"); |
4265 | 0 | g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4266 | 0 | G_KEY_FILE_DESKTOP_KEY_TYPE, |
4267 | 0 | G_KEY_FILE_DESKTOP_TYPE_APPLICATION); |
4268 | 0 | if (info->terminal) |
4269 | 0 | g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4270 | 0 | G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE); |
4271 | 0 | if (info->nodisplay) |
4272 | 0 | g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4273 | 0 | G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE); |
4274 | |
|
4275 | 0 | g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4276 | 0 | G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec); |
4277 | |
|
4278 | 0 | g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4279 | 0 | G_KEY_FILE_DESKTOP_KEY_NAME, info->name); |
4280 | |
|
4281 | 0 | if (info->generic_name != NULL) |
4282 | 0 | g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4283 | 0 | GENERIC_NAME_KEY, info->generic_name); |
4284 | |
|
4285 | 0 | if (info->fullname != NULL) |
4286 | 0 | g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4287 | 0 | FULL_NAME_KEY, info->fullname); |
4288 | |
|
4289 | 0 | g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4290 | 0 | G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment); |
4291 | |
|
4292 | 0 | g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, |
4293 | 0 | G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE); |
4294 | |
|
4295 | 0 | data = g_key_file_to_data (key_file, &data_size, NULL); |
4296 | 0 | g_key_file_free (key_file); |
4297 | |
|
4298 | 0 | desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name); |
4299 | 0 | filename = g_build_filename (dirname, desktop_id, NULL); |
4300 | 0 | g_free (desktop_id); |
4301 | 0 | g_free (dirname); |
4302 | |
|
4303 | 0 | fd = g_mkstemp (filename); |
4304 | 0 | if (fd == -1) |
4305 | 0 | { |
4306 | 0 | char *display_name; |
4307 | |
|
4308 | 0 | display_name = g_filename_display_name (filename); |
4309 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
4310 | 0 | _("Can’t create user desktop file %s"), display_name); |
4311 | 0 | g_free (display_name); |
4312 | 0 | g_free (filename); |
4313 | 0 | g_free (data); |
4314 | 0 | return FALSE; |
4315 | 0 | } |
4316 | | |
4317 | 0 | desktop_id = g_path_get_basename (filename); |
4318 | | |
4319 | | /* FIXME - actually handle error */ |
4320 | 0 | (void) g_close (fd, NULL); |
4321 | |
|
4322 | 0 | res = g_file_set_contents_full (filename, data, data_size, |
4323 | 0 | G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING, |
4324 | 0 | 0600, error); |
4325 | 0 | g_free (data); |
4326 | 0 | if (!res) |
4327 | 0 | { |
4328 | 0 | g_free (desktop_id); |
4329 | 0 | g_free (filename); |
4330 | 0 | return FALSE; |
4331 | 0 | } |
4332 | | |
4333 | 0 | info->filename = filename; |
4334 | 0 | info->desktop_id = desktop_id; |
4335 | |
|
4336 | 0 | run_update_command ("update-desktop-database", "applications"); |
4337 | | |
4338 | | /* We just dropped a file in the user's desktop file directory. Save |
4339 | | * the monitor the bother of having to notice it and invalidate |
4340 | | * immediately. |
4341 | | * |
4342 | | * This means that calls directly following this will be able to see |
4343 | | * the results immediately. |
4344 | | */ |
4345 | 0 | desktop_file_dirs_invalidate_user_data (); |
4346 | |
|
4347 | 0 | return TRUE; |
4348 | 0 | } |
4349 | | |
4350 | | static gboolean |
4351 | | g_desktop_app_info_can_delete (GAppInfo *appinfo) |
4352 | 0 | { |
4353 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
4354 | |
|
4355 | 0 | if (info->filename) |
4356 | 0 | { |
4357 | 0 | if (strstr (info->filename, "/userapp-")) |
4358 | 0 | return g_access (info->filename, W_OK) == 0; |
4359 | 0 | } |
4360 | | |
4361 | 0 | return FALSE; |
4362 | 0 | } |
4363 | | |
4364 | | static gboolean |
4365 | | g_desktop_app_info_delete (GAppInfo *appinfo) |
4366 | 0 | { |
4367 | 0 | GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo); |
4368 | |
|
4369 | 0 | if (info->filename) |
4370 | 0 | { |
4371 | 0 | if (g_remove (info->filename) == 0) |
4372 | 0 | { |
4373 | 0 | update_mimeapps_list (info->desktop_id, NULL, |
4374 | 0 | UPDATE_MIME_NONE, |
4375 | 0 | NULL); |
4376 | |
|
4377 | 0 | g_free (info->filename); |
4378 | 0 | info->filename = NULL; |
4379 | 0 | g_free (info->desktop_id); |
4380 | 0 | info->desktop_id = NULL; |
4381 | |
|
4382 | 0 | return TRUE; |
4383 | 0 | } |
4384 | 0 | } |
4385 | | |
4386 | 0 | return FALSE; |
4387 | 0 | } |
4388 | | |
4389 | | /* Create for commandline {{{2 */ |
4390 | | |
4391 | | GAppInfo * |
4392 | | g_app_info_create_from_commandline_impl (const char *commandline, |
4393 | | const char *application_name, |
4394 | | GAppInfoCreateFlags flags, |
4395 | | GError **error) |
4396 | 0 | { |
4397 | 0 | char **split; |
4398 | 0 | char *basename; |
4399 | 0 | GDesktopAppInfo *info; |
4400 | |
|
4401 | 0 | g_return_val_if_fail (commandline, NULL); |
4402 | | |
4403 | 0 | info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL); |
4404 | |
|
4405 | 0 | info->filename = NULL; |
4406 | 0 | info->desktop_id = NULL; |
4407 | |
|
4408 | 0 | info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0; |
4409 | 0 | info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0; |
4410 | 0 | info->hidden = FALSE; |
4411 | 0 | if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0) |
4412 | 0 | info->exec = g_strconcat (commandline, " %u", NULL); |
4413 | 0 | else |
4414 | 0 | info->exec = g_strconcat (commandline, " %f", NULL); |
4415 | 0 | info->nodisplay = TRUE; |
4416 | 0 | info->binary = binary_from_exec (info->exec); |
4417 | |
|
4418 | 0 | if (application_name) |
4419 | 0 | info->name = g_strdup (application_name); |
4420 | 0 | else |
4421 | 0 | { |
4422 | | /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */ |
4423 | 0 | split = g_strsplit (commandline, " ", 2); |
4424 | 0 | basename = split[0] ? g_path_get_basename (split[0]) : NULL; |
4425 | 0 | g_strfreev (split); |
4426 | 0 | info->name = basename; |
4427 | 0 | if (info->name == NULL) |
4428 | 0 | info->name = g_strdup ("custom"); |
4429 | 0 | } |
4430 | 0 | info->comment = g_strdup_printf (_("Custom definition for %s"), info->name); |
4431 | |
|
4432 | 0 | return G_APP_INFO (info); |
4433 | 0 | } |
4434 | | |
4435 | | /* GAppInfo interface init */ |
4436 | | |
4437 | | static void |
4438 | | g_desktop_app_info_iface_init (GAppInfoIface *iface) |
4439 | 0 | { |
4440 | 0 | iface->dup = g_desktop_app_info_dup; |
4441 | 0 | iface->equal = g_desktop_app_info_equal; |
4442 | 0 | iface->get_id = g_desktop_app_info_get_id; |
4443 | 0 | iface->get_name = g_desktop_app_info_get_name; |
4444 | 0 | iface->get_description = g_desktop_app_info_get_description; |
4445 | 0 | iface->get_executable = g_desktop_app_info_get_executable; |
4446 | 0 | iface->get_icon = g_desktop_app_info_get_icon; |
4447 | 0 | iface->launch = g_desktop_app_info_launch; |
4448 | 0 | iface->supports_uris = g_desktop_app_info_supports_uris; |
4449 | 0 | iface->supports_files = g_desktop_app_info_supports_files; |
4450 | 0 | iface->launch_uris = g_desktop_app_info_launch_uris; |
4451 | 0 | iface->launch_uris_async = g_desktop_app_info_launch_uris_async; |
4452 | 0 | iface->launch_uris_finish = g_desktop_app_info_launch_uris_finish; |
4453 | 0 | iface->should_show = g_desktop_app_info_should_show; |
4454 | 0 | iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type; |
4455 | 0 | iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension; |
4456 | 0 | iface->add_supports_type = g_desktop_app_info_add_supports_type; |
4457 | 0 | iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type; |
4458 | 0 | iface->remove_supports_type = g_desktop_app_info_remove_supports_type; |
4459 | 0 | iface->can_delete = g_desktop_app_info_can_delete; |
4460 | 0 | iface->do_delete = g_desktop_app_info_delete; |
4461 | 0 | iface->get_commandline = g_desktop_app_info_get_commandline; |
4462 | 0 | iface->get_display_name = g_desktop_app_info_get_display_name; |
4463 | 0 | iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type; |
4464 | 0 | iface->get_supported_types = g_desktop_app_info_get_supported_types; |
4465 | 0 | } |
4466 | | |
4467 | | /* Recommended applications {{{2 */ |
4468 | | |
4469 | | /* Converts content_type into a list of itself with all of its parent |
4470 | | * types (if include_fallback is enabled) or just returns a single-item |
4471 | | * list with the unaliased content type. |
4472 | | */ |
4473 | | static gchar ** |
4474 | | get_list_of_mimetypes (const gchar *content_type, |
4475 | | gboolean include_fallback) |
4476 | 0 | { |
4477 | 0 | gchar *unaliased; |
4478 | 0 | GPtrArray *array; |
4479 | |
|
4480 | 0 | array = g_ptr_array_new (); |
4481 | 0 | unaliased = _g_unix_content_type_unalias (content_type); |
4482 | 0 | g_ptr_array_add (array, unaliased); |
4483 | |
|
4484 | 0 | if (include_fallback) |
4485 | 0 | { |
4486 | 0 | guint i; |
4487 | | |
4488 | | /* Iterate the array as we grow it, until we have nothing more to add */ |
4489 | 0 | for (i = 0; i < array->len; i++) |
4490 | 0 | { |
4491 | 0 | gchar **parents = _g_unix_content_type_get_parents (g_ptr_array_index (array, i)); |
4492 | 0 | gint j; |
4493 | |
|
4494 | 0 | for (j = 0; parents[j]; j++) |
4495 | | /* Don't add duplicates */ |
4496 | 0 | if (!array_contains (array, parents[j])) |
4497 | 0 | g_ptr_array_add (array, parents[j]); |
4498 | 0 | else |
4499 | 0 | g_free (parents[j]); |
4500 | | |
4501 | | /* We already stole or freed each element. Free the container. */ |
4502 | 0 | g_free (parents); |
4503 | 0 | } |
4504 | 0 | } |
4505 | |
|
4506 | 0 | g_ptr_array_add (array, NULL); |
4507 | |
|
4508 | 0 | return (gchar **) g_ptr_array_free (array, FALSE); |
4509 | 0 | } |
4510 | | |
4511 | | static gchar ** |
4512 | | g_desktop_app_info_get_desktop_ids_for_content_type (const gchar *content_type, |
4513 | | gboolean include_fallback) |
4514 | 0 | { |
4515 | 0 | GPtrArray *hits, *blocklist; |
4516 | 0 | gchar **types; |
4517 | 0 | guint i, j; |
4518 | |
|
4519 | 0 | hits = g_ptr_array_new (); |
4520 | 0 | blocklist = g_ptr_array_new (); |
4521 | |
|
4522 | 0 | types = get_list_of_mimetypes (content_type, include_fallback); |
4523 | |
|
4524 | 0 | desktop_file_dirs_lock (); |
4525 | |
|
4526 | 0 | for (i = 0; types[i]; i++) |
4527 | 0 | for (j = 0; j < desktop_file_dirs->len; j++) |
4528 | 0 | desktop_file_dir_mime_lookup (g_ptr_array_index (desktop_file_dirs, j), types[i], hits, blocklist); |
4529 | | |
4530 | | /* We will keep the hits past unlocking, so we must dup them */ |
4531 | 0 | for (i = 0; i < hits->len; i++) |
4532 | 0 | hits->pdata[i] = g_strdup (hits->pdata[i]); |
4533 | |
|
4534 | 0 | desktop_file_dirs_unlock (); |
4535 | |
|
4536 | 0 | g_ptr_array_add (hits, NULL); |
4537 | |
|
4538 | 0 | g_ptr_array_free (blocklist, TRUE); |
4539 | 0 | g_strfreev (types); |
4540 | |
|
4541 | 0 | return (gchar **) g_ptr_array_free (hits, FALSE); |
4542 | 0 | } |
4543 | | |
4544 | | GList * |
4545 | | g_app_info_get_recommended_for_type_impl (const gchar *content_type) |
4546 | 0 | { |
4547 | 0 | gchar **desktop_ids; |
4548 | 0 | GList *infos; |
4549 | 0 | gint i; |
4550 | |
|
4551 | 0 | g_return_val_if_fail (content_type != NULL, NULL); |
4552 | | |
4553 | 0 | desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE); |
4554 | |
|
4555 | 0 | infos = NULL; |
4556 | 0 | for (i = 0; desktop_ids[i]; i++) |
4557 | 0 | { |
4558 | 0 | GDesktopAppInfo *info; |
4559 | |
|
4560 | 0 | info = g_desktop_app_info_new (desktop_ids[i]); |
4561 | 0 | if (info) |
4562 | 0 | infos = g_list_prepend (infos, info); |
4563 | 0 | } |
4564 | |
|
4565 | 0 | g_strfreev (desktop_ids); |
4566 | |
|
4567 | 0 | return g_list_reverse (infos); |
4568 | 0 | } |
4569 | | |
4570 | | GList * |
4571 | | g_app_info_get_fallback_for_type_impl (const gchar *content_type) |
4572 | 0 | { |
4573 | 0 | gchar **recommended_ids; |
4574 | 0 | gchar **all_ids; |
4575 | 0 | GList *infos; |
4576 | 0 | gint i; |
4577 | |
|
4578 | 0 | g_return_val_if_fail (content_type != NULL, NULL); |
4579 | | |
4580 | 0 | recommended_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE); |
4581 | 0 | all_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE); |
4582 | |
|
4583 | 0 | infos = NULL; |
4584 | 0 | for (i = 0; all_ids[i]; i++) |
4585 | 0 | { |
4586 | 0 | GDesktopAppInfo *info; |
4587 | 0 | gint j; |
4588 | | |
4589 | | /* Don't return the ones on the recommended list */ |
4590 | 0 | for (j = 0; recommended_ids[j]; j++) |
4591 | 0 | if (g_str_equal (all_ids[i], recommended_ids[j])) |
4592 | 0 | break; |
4593 | |
|
4594 | 0 | if (recommended_ids[j]) |
4595 | 0 | continue; |
4596 | | |
4597 | 0 | info = g_desktop_app_info_new (all_ids[i]); |
4598 | |
|
4599 | 0 | if (info) |
4600 | 0 | infos = g_list_prepend (infos, info); |
4601 | 0 | } |
4602 | |
|
4603 | 0 | g_strfreev (recommended_ids); |
4604 | 0 | g_strfreev (all_ids); |
4605 | |
|
4606 | 0 | return g_list_reverse (infos); |
4607 | 0 | } |
4608 | | |
4609 | | GList * |
4610 | | g_app_info_get_all_for_type_impl (const char *content_type) |
4611 | 0 | { |
4612 | 0 | gchar **desktop_ids; |
4613 | 0 | GList *infos; |
4614 | 0 | gint i; |
4615 | |
|
4616 | 0 | g_return_val_if_fail (content_type != NULL, NULL); |
4617 | | |
4618 | 0 | desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE); |
4619 | |
|
4620 | 0 | infos = NULL; |
4621 | 0 | for (i = 0; desktop_ids[i]; i++) |
4622 | 0 | { |
4623 | 0 | GDesktopAppInfo *info; |
4624 | |
|
4625 | 0 | info = g_desktop_app_info_new (desktop_ids[i]); |
4626 | 0 | if (info) |
4627 | 0 | infos = g_list_prepend (infos, info); |
4628 | 0 | } |
4629 | |
|
4630 | 0 | g_strfreev (desktop_ids); |
4631 | |
|
4632 | 0 | return g_list_reverse (infos); |
4633 | 0 | } |
4634 | | |
4635 | | void |
4636 | | g_app_info_reset_type_associations_impl (const char *content_type) |
4637 | 0 | { |
4638 | 0 | update_mimeapps_list (NULL, content_type, |
4639 | 0 | UPDATE_MIME_NONE, |
4640 | 0 | NULL); |
4641 | 0 | } |
4642 | | |
4643 | | GAppInfo * |
4644 | | g_app_info_get_default_for_type_impl (const char *content_type, |
4645 | | gboolean must_support_uris) |
4646 | 0 | { |
4647 | 0 | GPtrArray *blocklist; |
4648 | 0 | GPtrArray *results; |
4649 | 0 | GAppInfo *info; |
4650 | 0 | gchar **types; |
4651 | 0 | guint i, j, k; |
4652 | |
|
4653 | 0 | g_return_val_if_fail (content_type != NULL, NULL); |
4654 | | |
4655 | 0 | types = get_list_of_mimetypes (content_type, TRUE); |
4656 | |
|
4657 | 0 | blocklist = g_ptr_array_new (); |
4658 | 0 | results = g_ptr_array_new (); |
4659 | 0 | info = NULL; |
4660 | |
|
4661 | 0 | desktop_file_dirs_lock (); |
4662 | |
|
4663 | 0 | for (i = 0; types[i]; i++) |
4664 | 0 | { |
4665 | | /* Collect all the default apps for this type */ |
4666 | 0 | for (j = 0; j < desktop_file_dirs->len; j++) |
4667 | 0 | desktop_file_dir_default_lookup (g_ptr_array_index (desktop_file_dirs, j), types[i], results); |
4668 | | |
4669 | | /* Consider the associations as well... */ |
4670 | 0 | for (j = 0; j < desktop_file_dirs->len; j++) |
4671 | 0 | desktop_file_dir_mime_lookup (g_ptr_array_index (desktop_file_dirs, j), types[i], results, blocklist); |
4672 | | |
4673 | | /* (If any), see if one of those apps is installed... */ |
4674 | 0 | for (j = 0; j < results->len; j++) |
4675 | 0 | { |
4676 | 0 | const gchar *desktop_id = g_ptr_array_index (results, j); |
4677 | |
|
4678 | 0 | for (k = 0; k < desktop_file_dirs->len; k++) |
4679 | 0 | { |
4680 | 0 | info = (GAppInfo *) desktop_file_dir_get_app (g_ptr_array_index (desktop_file_dirs, k), desktop_id); |
4681 | |
|
4682 | 0 | if (info) |
4683 | 0 | { |
4684 | 0 | if (!must_support_uris || g_app_info_supports_uris (info)) |
4685 | 0 | goto out; |
4686 | | |
4687 | 0 | g_clear_object (&info); |
4688 | 0 | } |
4689 | 0 | } |
4690 | 0 | } |
4691 | | |
4692 | | /* Reset the list, ready to try again with the next (parent) |
4693 | | * mimetype, but keep the blocklist in place. |
4694 | | */ |
4695 | 0 | g_ptr_array_set_size (results, 0); |
4696 | 0 | } |
4697 | | |
4698 | 0 | out: |
4699 | 0 | desktop_file_dirs_unlock (); |
4700 | |
|
4701 | 0 | g_ptr_array_unref (blocklist); |
4702 | 0 | g_ptr_array_unref (results); |
4703 | 0 | g_strfreev (types); |
4704 | |
|
4705 | 0 | return info; |
4706 | 0 | } |
4707 | | |
4708 | | GAppInfo * |
4709 | | g_app_info_get_default_for_uri_scheme_impl (const char *uri_scheme) |
4710 | 0 | { |
4711 | 0 | GAppInfo *app_info; |
4712 | 0 | char *content_type, *scheme_down; |
4713 | |
|
4714 | 0 | g_return_val_if_fail (uri_scheme != NULL && *uri_scheme != '\0', NULL); |
4715 | | |
4716 | 0 | scheme_down = g_ascii_strdown (uri_scheme, -1); |
4717 | 0 | content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down); |
4718 | 0 | g_free (scheme_down); |
4719 | 0 | app_info = g_app_info_get_default_for_type (content_type, FALSE); |
4720 | 0 | g_free (content_type); |
4721 | |
|
4722 | 0 | return app_info; |
4723 | 0 | } |
4724 | | |
4725 | | /* "Get all" API {{{2 */ |
4726 | | |
4727 | | /** |
4728 | | * g_desktop_app_info_get_implementations: |
4729 | | * @interface: the name of the interface |
4730 | | * |
4731 | | * Gets all applications that implement @interface. |
4732 | | * |
4733 | | * An application implements an interface if that interface is listed in |
4734 | | * the `Implements` line of the desktop file of the application. |
4735 | | * |
4736 | | * Returns: (element-type GDesktopAppInfo) (transfer full): a list of |
4737 | | * [class@GioUnix.DesktopAppInfo] objects. |
4738 | | * |
4739 | | * Since: 2.42 |
4740 | | **/ |
4741 | | GList * |
4742 | | g_desktop_app_info_get_implementations (const gchar *interface) |
4743 | 0 | { |
4744 | 0 | GList *result = NULL; |
4745 | 0 | GList **ptr; |
4746 | 0 | guint i; |
4747 | |
|
4748 | 0 | desktop_file_dirs_lock (); |
4749 | |
|
4750 | 0 | for (i = 0; i < desktop_file_dirs->len; i++) |
4751 | 0 | desktop_file_dir_get_implementations (g_ptr_array_index (desktop_file_dirs, i), &result, interface); |
4752 | |
|
4753 | 0 | desktop_file_dirs_unlock (); |
4754 | |
|
4755 | 0 | ptr = &result; |
4756 | 0 | while (*ptr) |
4757 | 0 | { |
4758 | 0 | gchar *name = (*ptr)->data; |
4759 | 0 | GDesktopAppInfo *app; |
4760 | |
|
4761 | 0 | app = g_desktop_app_info_new (name); |
4762 | 0 | g_free (name); |
4763 | |
|
4764 | 0 | if (app) |
4765 | 0 | { |
4766 | 0 | (*ptr)->data = app; |
4767 | 0 | ptr = &(*ptr)->next; |
4768 | 0 | } |
4769 | 0 | else |
4770 | 0 | *ptr = g_list_delete_link (*ptr, *ptr); |
4771 | 0 | } |
4772 | |
|
4773 | 0 | return result; |
4774 | 0 | } |
4775 | | |
4776 | | /** |
4777 | | * g_desktop_app_info_search: |
4778 | | * @search_string: the search string to use |
4779 | | * |
4780 | | * Searches desktop files for ones that match @search_string. |
4781 | | * |
4782 | | * The return value is an array of strvs. Each strv contains a list of |
4783 | | * applications that matched @search_string with an equal score. The |
4784 | | * outer list is sorted by score so that the first strv contains the |
4785 | | * best-matching applications, and so on. |
4786 | | * The algorithm for determining matches is undefined and may change at |
4787 | | * any time. |
4788 | | * |
4789 | | * None of the search results are subjected to the normal validation |
4790 | | * checks performed by [ctor@GioUnix.DesktopAppInfo.new] (for example, |
4791 | | * checking that the executable referenced by a result exists), and so it is |
4792 | | * possible for [ctor@GioUnix.DesktopAppInfo.new] to return `NULL` when passed |
4793 | | * an app ID returned by this function. It is expected that calling code will |
4794 | | * do this when subsequently creating a [class@GioUnix.DesktopAppInfo] for |
4795 | | * each result. |
4796 | | * |
4797 | | * Returns: (array zero-terminated=1) (element-type GStrv) (transfer full): a |
4798 | | * list of strvs. Free each item with [func@GLib.strfreev] and free the outer |
4799 | | * list with [func@GLib.free]. |
4800 | | */ |
4801 | | gchar *** |
4802 | | g_desktop_app_info_search (const gchar *search_string) |
4803 | 0 | { |
4804 | 0 | gchar **search_tokens; |
4805 | 0 | gint last_category = -1; |
4806 | 0 | gint last_match_type = -1; |
4807 | 0 | gint last_token_pos = -1; |
4808 | 0 | gchar ***results; |
4809 | 0 | gint n_groups = 0; |
4810 | 0 | gint start_of_group; |
4811 | 0 | gint i, j; |
4812 | 0 | guint k; |
4813 | |
|
4814 | 0 | search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL); |
4815 | |
|
4816 | 0 | desktop_file_dirs_lock (); |
4817 | |
|
4818 | 0 | reset_total_search_results (); |
4819 | |
|
4820 | 0 | for (k = 0; k < desktop_file_dirs->len; k++) |
4821 | 0 | { |
4822 | 0 | for (j = 0; search_tokens[j]; j++) |
4823 | 0 | { |
4824 | 0 | desktop_file_dir_search (g_ptr_array_index (desktop_file_dirs, k), search_tokens[j]); |
4825 | 0 | merge_token_results (j == 0); |
4826 | 0 | } |
4827 | 0 | merge_directory_results (); |
4828 | 0 | } |
4829 | |
|
4830 | 0 | sort_total_search_results (); |
4831 | | |
4832 | | /* Count the total number of unique categories and match types */ |
4833 | 0 | for (i = 0; i < static_total_results_size; i++) |
4834 | 0 | if (static_total_results[i].category != last_category || |
4835 | 0 | static_total_results[i].match_type != last_match_type || |
4836 | 0 | static_total_results[i].token_pos != last_token_pos) |
4837 | 0 | { |
4838 | 0 | last_category = static_total_results[i].category; |
4839 | 0 | last_match_type = static_total_results[i].match_type; |
4840 | 0 | last_token_pos = static_total_results[i].token_pos; |
4841 | 0 | n_groups++; |
4842 | 0 | } |
4843 | |
|
4844 | 0 | results = g_new (gchar **, n_groups + 1); |
4845 | | |
4846 | | /* Start loading into the results list */ |
4847 | 0 | start_of_group = 0; |
4848 | 0 | for (i = 0; i < n_groups; i++) |
4849 | 0 | { |
4850 | 0 | gint n_items_in_group = 0; |
4851 | 0 | gint this_category; |
4852 | 0 | gint this_match_type; |
4853 | 0 | gint this_token_pos; |
4854 | 0 | gint j; |
4855 | |
|
4856 | 0 | this_category = static_total_results[start_of_group].category; |
4857 | 0 | this_match_type = static_total_results[start_of_group].match_type; |
4858 | 0 | this_token_pos = static_total_results[start_of_group].token_pos; |
4859 | |
|
4860 | 0 | while (start_of_group + n_items_in_group < static_total_results_size && |
4861 | 0 | static_total_results[start_of_group + n_items_in_group].category == this_category && |
4862 | 0 | static_total_results[start_of_group + n_items_in_group].match_type == this_match_type && |
4863 | 0 | static_total_results[start_of_group + n_items_in_group].token_pos == this_token_pos) |
4864 | 0 | n_items_in_group++; |
4865 | |
|
4866 | 0 | results[i] = g_new (gchar *, n_items_in_group + 1); |
4867 | 0 | for (j = 0; j < n_items_in_group; j++) |
4868 | 0 | results[i][j] = g_strdup (static_total_results[start_of_group + j].app_name); |
4869 | 0 | results[i][j] = NULL; |
4870 | |
|
4871 | 0 | start_of_group += n_items_in_group; |
4872 | 0 | } |
4873 | 0 | results[i] = NULL; |
4874 | |
|
4875 | 0 | desktop_file_dirs_unlock (); |
4876 | |
|
4877 | 0 | g_strfreev (search_tokens); |
4878 | |
|
4879 | 0 | return results; |
4880 | 0 | } |
4881 | | |
4882 | | GList * |
4883 | | g_app_info_get_all_impl (void) |
4884 | 0 | { |
4885 | 0 | GHashTable *apps; |
4886 | 0 | GHashTableIter iter; |
4887 | 0 | gpointer value; |
4888 | 0 | guint i; |
4889 | 0 | GList *infos; |
4890 | |
|
4891 | 0 | apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
4892 | |
|
4893 | 0 | desktop_file_dirs_lock (); |
4894 | |
|
4895 | 0 | for (i = 0; i < desktop_file_dirs->len; i++) |
4896 | 0 | desktop_file_dir_get_all (g_ptr_array_index (desktop_file_dirs, i), apps); |
4897 | |
|
4898 | 0 | desktop_file_dirs_unlock (); |
4899 | |
|
4900 | 0 | infos = NULL; |
4901 | 0 | g_hash_table_iter_init (&iter, apps); |
4902 | 0 | while (g_hash_table_iter_next (&iter, NULL, &value)) |
4903 | 0 | { |
4904 | 0 | if (value) |
4905 | 0 | infos = g_list_prepend (infos, value); |
4906 | 0 | } |
4907 | |
|
4908 | 0 | g_hash_table_destroy (apps); |
4909 | |
|
4910 | 0 | return infos; |
4911 | 0 | } |
4912 | | |
4913 | | /* GDesktopAppInfoLookup interface {{{2 */ |
4914 | | |
4915 | | /** |
4916 | | * GDesktopAppInfoLookup: |
4917 | | * |
4918 | | * #GDesktopAppInfoLookup is an opaque data structure and can only be accessed |
4919 | | * using the following functions. |
4920 | | * |
4921 | | * Deprecated: 2.28: The [iface@GioUnix.DesktopAppInfoLookup] interface is |
4922 | | * deprecated and unused by GIO. |
4923 | | **/ |
4924 | | |
4925 | | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
4926 | | |
4927 | | typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface; |
4928 | 0 | G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT) |
4929 | 0 |
|
4930 | 0 | static void |
4931 | 0 | g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface) |
4932 | 0 | { |
4933 | 0 | } |
4934 | | |
4935 | | /* "Get for mime type" APIs {{{2 */ |
4936 | | |
4937 | | /** |
4938 | | * g_desktop_app_info_lookup_get_default_for_uri_scheme: |
4939 | | * @lookup: a [iface@GioUnix.DesktopAppInfoLookup] |
4940 | | * @uri_scheme: a string containing a URI scheme. |
4941 | | * |
4942 | | * Gets the default application for launching applications |
4943 | | * using this URI scheme for a particular [iface@GioUnix.DesktopAppInfoLookup] |
4944 | | * implementation. |
4945 | | * |
4946 | | * The [iface@GioUnix.DesktopAppInfoLookup] interface and this function is used |
4947 | | * to implement [func@Gio.AppInfo.get_default_for_uri_scheme] backends |
4948 | | * in a GIO module. There is no reason for applications to use it |
4949 | | * directly. Applications should use |
4950 | | * [func@Gio.AppInfo.get_default_for_uri_scheme]. |
4951 | | * |
4952 | | * Returns: (transfer full) (nullable): [iface@Gio.AppInfo] for given |
4953 | | * @uri_scheme or `NULL` on error. |
4954 | | * |
4955 | | * Deprecated: 2.28: The [iface@GioUnix.DesktopAppInfoLookup] interface is |
4956 | | * deprecated and unused by GIO. |
4957 | | */ |
4958 | | GAppInfo * |
4959 | | g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup, |
4960 | | const char *uri_scheme) |
4961 | 0 | { |
4962 | 0 | GDesktopAppInfoLookupIface *iface; |
4963 | |
|
4964 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL); |
4965 | | |
4966 | 0 | iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup); |
4967 | |
|
4968 | 0 | return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme); |
4969 | 0 | } |
4970 | | |
4971 | | G_GNUC_END_IGNORE_DEPRECATIONS |
4972 | | |
4973 | | /* Misc getter APIs {{{2 */ |
4974 | | |
4975 | | /** |
4976 | | * g_desktop_app_info_get_startup_wm_class: |
4977 | | * @info: a [class@GioUnix.DesktopAppInfo] that supports startup notify |
4978 | | * |
4979 | | * Retrieves the `StartupWMClass` field from @info. This represents the |
4980 | | * `WM_CLASS` property of the main window of the application, if launched |
4981 | | * through @info. |
4982 | | * |
4983 | | * Returns: (nullable) (transfer none): the startup WM class, or `NULL` if none |
4984 | | * is set in the desktop file. |
4985 | | * |
4986 | | * Since: 2.34 |
4987 | | */ |
4988 | | const char * |
4989 | | g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info) |
4990 | 0 | { |
4991 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL); |
4992 | | |
4993 | 0 | return info->startup_wm_class; |
4994 | 0 | } |
4995 | | |
4996 | | /** |
4997 | | * g_desktop_app_info_get_string: |
4998 | | * @info: a [class@GioUnix.DesktopAppInfo] |
4999 | | * @key: the key to look up |
5000 | | * |
5001 | | * Looks up a string value in the keyfile backing @info. |
5002 | | * |
5003 | | * The @key is looked up in the `Desktop Entry` group. |
5004 | | * |
5005 | | * Returns: (nullable): a newly allocated string, or `NULL` if the key is not |
5006 | | * found |
5007 | | * |
5008 | | * Since: 2.36 |
5009 | | */ |
5010 | | char * |
5011 | | g_desktop_app_info_get_string (GDesktopAppInfo *info, |
5012 | | const char *key) |
5013 | 0 | { |
5014 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL); |
5015 | | |
5016 | 0 | return g_key_file_get_string (info->keyfile, |
5017 | 0 | G_KEY_FILE_DESKTOP_GROUP, key, NULL); |
5018 | 0 | } |
5019 | | |
5020 | | /** |
5021 | | * g_desktop_app_info_get_locale_string: |
5022 | | * @info: a [class@GioUnix.DesktopAppInfo] |
5023 | | * @key: the key to look up |
5024 | | * |
5025 | | * Looks up a localized string value in the keyfile backing @info |
5026 | | * translated to the current locale. |
5027 | | * |
5028 | | * The @key is looked up in the `Desktop Entry` group. |
5029 | | * |
5030 | | * Returns: (nullable): a newly allocated string, or `NULL` if the key is not |
5031 | | * found |
5032 | | * |
5033 | | * Since: 2.56 |
5034 | | */ |
5035 | | char * |
5036 | | g_desktop_app_info_get_locale_string (GDesktopAppInfo *info, |
5037 | | const char *key) |
5038 | 0 | { |
5039 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL); |
5040 | 0 | g_return_val_if_fail (key != NULL && *key != '\0', NULL); |
5041 | | |
5042 | 0 | return g_key_file_get_locale_string (info->keyfile, |
5043 | 0 | G_KEY_FILE_DESKTOP_GROUP, |
5044 | 0 | key, NULL, NULL); |
5045 | 0 | } |
5046 | | |
5047 | | /** |
5048 | | * g_desktop_app_info_get_boolean: |
5049 | | * @info: a [class@GioUnix.DesktopAppInfo] |
5050 | | * @key: the key to look up |
5051 | | * |
5052 | | * Looks up a boolean value in the keyfile backing @info. |
5053 | | * |
5054 | | * The @key is looked up in the `Desktop Entry` group. |
5055 | | * |
5056 | | * Returns: the boolean value, or `FALSE` if the key is not found |
5057 | | * |
5058 | | * Since: 2.36 |
5059 | | */ |
5060 | | gboolean |
5061 | | g_desktop_app_info_get_boolean (GDesktopAppInfo *info, |
5062 | | const char *key) |
5063 | 0 | { |
5064 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE); |
5065 | | |
5066 | 0 | return g_key_file_get_boolean (info->keyfile, |
5067 | 0 | G_KEY_FILE_DESKTOP_GROUP, key, NULL); |
5068 | 0 | } |
5069 | | |
5070 | | /** |
5071 | | * g_desktop_app_info_get_string_list: |
5072 | | * @info: a [class@GioUnix.DesktopAppInfo] |
5073 | | * @key: the key to look up |
5074 | | * @length: (out) (optional): return location for the number of returned |
5075 | | * strings, or `NULL` |
5076 | | * |
5077 | | * Looks up a string list value in the keyfile backing @info. |
5078 | | * |
5079 | | * The @key is looked up in the `Desktop Entry` group. |
5080 | | * |
5081 | | * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): |
5082 | | * a `NULL`-terminated string array or `NULL` if the specified |
5083 | | * key cannot be found. The array should be freed with [func@GLib.strfreev]. |
5084 | | * |
5085 | | * Since: 2.60 |
5086 | | */ |
5087 | | gchar ** |
5088 | | g_desktop_app_info_get_string_list (GDesktopAppInfo *info, |
5089 | | const char *key, |
5090 | | gsize *length) |
5091 | 0 | { |
5092 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL); |
5093 | | |
5094 | 0 | return g_key_file_get_string_list (info->keyfile, |
5095 | 0 | G_KEY_FILE_DESKTOP_GROUP, key, length, NULL); |
5096 | 0 | } |
5097 | | |
5098 | | /** |
5099 | | * g_desktop_app_info_has_key: |
5100 | | * @info: a [class@GioUnix.DesktopAppInfo] |
5101 | | * @key: the key to look up |
5102 | | * |
5103 | | * Returns whether @key exists in the `Desktop Entry` group |
5104 | | * of the keyfile backing @info. |
5105 | | * |
5106 | | * Returns: `TRUE` if the @key exists |
5107 | | * |
5108 | | * Since: 2.36 |
5109 | | */ |
5110 | | gboolean |
5111 | | g_desktop_app_info_has_key (GDesktopAppInfo *info, |
5112 | | const char *key) |
5113 | 0 | { |
5114 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE); |
5115 | | |
5116 | 0 | return g_key_file_has_key (info->keyfile, |
5117 | 0 | G_KEY_FILE_DESKTOP_GROUP, key, NULL); |
5118 | 0 | } |
5119 | | |
5120 | | /* Desktop actions support {{{2 */ |
5121 | | |
5122 | | /** |
5123 | | * g_desktop_app_info_list_actions: |
5124 | | * @info: a [class@GioUnix.DesktopAppInfo] |
5125 | | * |
5126 | | * Returns the list of |
5127 | | * [‘additional application actions’](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s11.html) |
5128 | | * supported on the desktop file, as per the desktop file specification. |
5129 | | * |
5130 | | * As per the specification, this is the list of actions that are |
5131 | | * explicitly listed in the `Actions` key of the `Desktop Entry` group. |
5132 | | * |
5133 | | * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a |
5134 | | * list of strings, always non-`NULL` |
5135 | | * |
5136 | | * Since: 2.38 |
5137 | | **/ |
5138 | | const gchar * const * |
5139 | | g_desktop_app_info_list_actions (GDesktopAppInfo *info) |
5140 | 0 | { |
5141 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL); |
5142 | | |
5143 | 0 | return (const gchar **) info->actions; |
5144 | 0 | } |
5145 | | |
5146 | | static gboolean |
5147 | | app_info_has_action (GDesktopAppInfo *info, |
5148 | | const gchar *action_name) |
5149 | 0 | { |
5150 | 0 | gint i; |
5151 | |
|
5152 | 0 | for (i = 0; info->actions[i]; i++) |
5153 | 0 | if (g_str_equal (info->actions[i], action_name)) |
5154 | 0 | return TRUE; |
5155 | | |
5156 | 0 | return FALSE; |
5157 | 0 | } |
5158 | | |
5159 | | /** |
5160 | | * g_desktop_app_info_get_action_name: |
5161 | | * @info: a [class@GioUnix.DesktopAppInfo] |
5162 | | * @action_name: the name of the action as from |
5163 | | * [method@GioUnix.DesktopAppInfo.list_actions] |
5164 | | * |
5165 | | * Gets the user-visible display name of the |
5166 | | * [‘additional application actions’](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s11.html) |
5167 | | * specified by @action_name. |
5168 | | * |
5169 | | * This corresponds to the `Name` key within the keyfile group for the |
5170 | | * action. |
5171 | | * |
5172 | | * Returns: (transfer full): the locale-specific action name |
5173 | | * |
5174 | | * Since: 2.38 |
5175 | | */ |
5176 | | gchar * |
5177 | | g_desktop_app_info_get_action_name (GDesktopAppInfo *info, |
5178 | | const gchar *action_name) |
5179 | 0 | { |
5180 | 0 | gchar *group_name; |
5181 | 0 | gchar *result; |
5182 | |
|
5183 | 0 | g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL); |
5184 | 0 | g_return_val_if_fail (action_name != NULL, NULL); |
5185 | 0 | g_return_val_if_fail (app_info_has_action (info, action_name), NULL); |
5186 | | |
5187 | 0 | group_name = g_strdup_printf ("Desktop Action %s", action_name); |
5188 | 0 | result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL); |
5189 | 0 | g_free (group_name); |
5190 | | |
5191 | | /* The spec says that the Name field must be given. |
5192 | | * |
5193 | | * If it's not, let's follow the behaviour of our get_name() |
5194 | | * implementation above and never return %NULL. |
5195 | | */ |
5196 | 0 | if (result == NULL) |
5197 | 0 | result = g_strdup (_("Unnamed")); |
5198 | |
|
5199 | 0 | return result; |
5200 | 0 | } |
5201 | | |
5202 | | /** |
5203 | | * g_desktop_app_info_launch_action: |
5204 | | * @info: a [class@GioUnix.DesktopAppInfo] |
5205 | | * @action_name: the name of the action as from |
5206 | | * [method@GioUnix.DesktopAppInfo.list_actions] |
5207 | | * @launch_context: (nullable): a [class@Gio.AppLaunchContext] |
5208 | | * |
5209 | | * Activates the named application action. |
5210 | | * |
5211 | | * You may only call this function on action names that were |
5212 | | * returned from [method@GioUnix.DesktopAppInfo.list_actions]. |
5213 | | * |
5214 | | * Note that if the main entry of the desktop file indicates that the |
5215 | | * application supports startup notification, and @launch_context is |
5216 | | * non-`NULL`, then startup notification will be used when activating the |
5217 | | * action (and as such, invocation of the action on the receiving side |
5218 | | * must signal the end of startup notification when it is completed). |
5219 | | * This is the expected behaviour of applications declaring additional |
5220 | | * actions, as per the |
5221 | | * [desktop file specification](https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s11.html). |
5222 | | * |
5223 | | * As with [method@Gio.AppInfo.launch] there is no way to detect failures that |
5224 | | * occur while using this function. |
5225 | | * |
5226 | | * Since: 2.38 |
5227 | | */ |
5228 | | void |
5229 | | g_desktop_app_info_launch_action (GDesktopAppInfo *info, |
5230 | | const gchar *action_name, |
5231 | | GAppLaunchContext *launch_context) |
5232 | 0 | { |
5233 | 0 | GDBusConnection *session_bus; |
5234 | |
|
5235 | 0 | g_return_if_fail (G_IS_DESKTOP_APP_INFO (info)); |
5236 | 0 | g_return_if_fail (action_name != NULL); |
5237 | 0 | g_return_if_fail (app_info_has_action (info, action_name)); |
5238 | | |
5239 | 0 | session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); |
5240 | |
|
5241 | 0 | if (session_bus && info->app_id) |
5242 | 0 | { |
5243 | 0 | gchar *object_path; |
5244 | |
|
5245 | 0 | object_path = object_path_from_appid (info->app_id); |
5246 | 0 | g_dbus_connection_call (session_bus, info->app_id, object_path, |
5247 | 0 | "org.freedesktop.Application", "ActivateAction", |
5248 | 0 | g_variant_new ("(sav@a{sv})", action_name, NULL, |
5249 | 0 | g_desktop_app_info_make_platform_data (info, NULL, launch_context)), |
5250 | 0 | NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); |
5251 | 0 | g_free (object_path); |
5252 | 0 | } |
5253 | 0 | else |
5254 | 0 | { |
5255 | 0 | gchar *group_name; |
5256 | 0 | gchar *exec_line; |
5257 | |
|
5258 | 0 | group_name = g_strdup_printf ("Desktop Action %s", action_name); |
5259 | 0 | exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL); |
5260 | 0 | g_free (group_name); |
5261 | |
|
5262 | 0 | if (exec_line) |
5263 | 0 | g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context, |
5264 | 0 | _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, |
5265 | 0 | -1, -1, -1, NULL); |
5266 | |
|
5267 | 0 | g_free (exec_line); |
5268 | 0 | } |
5269 | |
|
5270 | 0 | if (session_bus != NULL) |
5271 | 0 | { |
5272 | 0 | g_dbus_connection_flush (session_bus, NULL, NULL, NULL); |
5273 | 0 | g_object_unref (session_bus); |
5274 | 0 | } |
5275 | 0 | } |
5276 | | /* Epilogue {{{1 */ |
5277 | | |
5278 | | /* vim:set foldmethod=marker: */ |