Coverage Report

Created: 2025-07-01 07:09

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