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