appinfo: avoid overriding the system defaults when adding support
[platform/upstream/glib.git] / gio / gdesktopappinfo.c
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 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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Author: Alexander Larsson <alexl@redhat.com>
22  */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/wait.h>
30
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h>
33 #endif
34
35 #undef G_DISABLE_DEPRECATED
36
37 #include "gcontenttypeprivate.h"
38 #include "gdesktopappinfo.h"
39 #include "gfile.h"
40 #include "gioerror.h"
41 #include "gthemedicon.h"
42 #include "gfileicon.h"
43 #include <glib/gstdio.h>
44 #include "glibintl.h"
45 #include "giomodule-priv.h"
46 #include "gappinfo.h"
47
48
49 /**
50  * SECTION:gdesktopappinfo
51  * @title: GDesktopAppInfo
52  * @short_description: Application information from desktop files
53  * @include: gio/gdesktopappinfo.h
54  *
55  * #GDesktopAppInfo is an implementation of #GAppInfo based on
56  * desktop files.
57  *
58  * Note that <filename>&lt;gio/gdesktopappinfo.h&gt;</filename> belongs to
59  * the UNIX-specific GIO interfaces, thus you have to use the
60  * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
61  */
62
63 #define DEFAULT_APPLICATIONS_GROUP  "Default Applications"
64 #define ADDED_ASSOCIATIONS_GROUP    "Added Associations"
65 #define REMOVED_ASSOCIATIONS_GROUP  "Removed Associations"
66 #define MIME_CACHE_GROUP            "MIME Cache"
67 #define FULL_NAME_KEY               "X-GNOME-FullName"
68
69 static void     g_desktop_app_info_iface_init         (GAppInfoIface    *iface);
70 static GList *  get_all_desktop_entries_for_mime_type (const char       *base_mime_type,
71                                                        const char      **except,
72                                                        gboolean          include_fallback,
73                                                        char            **explicit_default);
74 static void     mime_info_cache_reload                (const char       *dir);
75 static gboolean g_desktop_app_info_ensure_saved       (GDesktopAppInfo  *info,
76                                                        GError          **error);
77
78 /**
79  * GDesktopAppInfo:
80  * 
81  * Information about an installed application from a desktop file.
82  */
83 struct _GDesktopAppInfo
84 {
85   GObject parent_instance;
86
87   char *desktop_id;
88   char *filename;
89
90   char *name;
91   /* FIXME: what about GenericName ? */
92   char *fullname;
93   char *comment;
94   char *icon_name;
95   GIcon *icon;
96   char **only_show_in;
97   char **not_show_in;
98   char *try_exec;
99   char *exec;
100   char *binary;
101   char *path;
102
103   guint nodisplay       : 1;
104   guint hidden          : 1;
105   guint terminal        : 1;
106   guint startup_notify  : 1;
107   guint no_fuse         : 1;
108   /* FIXME: what about StartupWMClass ? */
109 };
110
111 typedef enum {
112   UPDATE_MIME_NONE = 1 << 0,
113   UPDATE_MIME_SET_DEFAULT = 1 << 1,
114   UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
115   UPDATE_MIME_REMOVE = 1 << 3,
116   UPDATE_MIME_SET_LAST_USED = 1 << 4,
117 } UpdateMimeFlags;
118
119 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
120                          G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
121                                                 g_desktop_app_info_iface_init))
122
123 static gpointer
124 search_path_init (gpointer data)
125 {
126   char **args = NULL;
127   const char * const *data_dirs;
128   const char *user_data_dir;
129   int i, length, j;
130
131   data_dirs = g_get_system_data_dirs ();
132   length = g_strv_length ((char **) data_dirs);
133   
134   args = g_new (char *, length + 2);
135   
136   j = 0;
137   user_data_dir = g_get_user_data_dir ();
138   args[j++] = g_build_filename (user_data_dir, "applications", NULL);
139   for (i = 0; i < length; i++)
140     args[j++] = g_build_filename (data_dirs[i],
141                                   "applications", NULL);
142   args[j++] = NULL;
143   
144   return args;
145 }
146   
147 static const char * const *
148 get_applications_search_path (void)
149 {
150   static GOnce once_init = G_ONCE_INIT;
151   return g_once (&once_init, search_path_init, NULL);
152 }
153
154 static void
155 g_desktop_app_info_finalize (GObject *object)
156 {
157   GDesktopAppInfo *info;
158
159   info = G_DESKTOP_APP_INFO (object);
160
161   g_free (info->desktop_id);
162   g_free (info->filename);
163   g_free (info->name);
164   g_free (info->fullname);
165   g_free (info->comment);
166   g_free (info->icon_name);
167   if (info->icon)
168     g_object_unref (info->icon);
169   g_strfreev (info->only_show_in);
170   g_strfreev (info->not_show_in);
171   g_free (info->try_exec);
172   g_free (info->exec);
173   g_free (info->binary);
174   g_free (info->path);
175   
176   G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
177 }
178
179 static void
180 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
181 {
182   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
183   
184   gobject_class->finalize = g_desktop_app_info_finalize;
185 }
186
187 static void
188 g_desktop_app_info_init (GDesktopAppInfo *local)
189 {
190 }
191
192 static char *
193 binary_from_exec (const char *exec)
194 {
195   const char *p, *start;
196   
197   p = exec;
198   while (*p == ' ')
199     p++;
200   start = p;
201   while (*p != ' ' && *p != 0)
202     p++;
203   
204   return g_strndup (start, p - start);
205   
206 }
207
208 /**
209  * g_desktop_app_info_new_from_keyfile:
210  * @key_file: an opened #GKeyFile
211  * 
212  * Creates a new #GDesktopAppInfo.
213  *
214  * Returns: a new #GDesktopAppInfo or %NULL on error.
215  *
216  * Since: 2.18
217  **/
218 GDesktopAppInfo *
219 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
220 {
221   GDesktopAppInfo *info;
222   char *start_group;
223   char *type;
224   char *try_exec;
225   
226   start_group = g_key_file_get_start_group (key_file);
227   if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
228     {
229       g_free (start_group);
230       return NULL;
231     }
232   g_free (start_group);
233
234   type = g_key_file_get_string (key_file,
235                                 G_KEY_FILE_DESKTOP_GROUP,
236                                 G_KEY_FILE_DESKTOP_KEY_TYPE,
237                                 NULL);
238   if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
239     {
240       g_free (type);
241       return NULL;
242     }
243   g_free (type);
244
245   try_exec = g_key_file_get_string (key_file,
246                                     G_KEY_FILE_DESKTOP_GROUP,
247                                     G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
248                                     NULL);
249   if (try_exec && try_exec[0] != '\0')
250     {
251       char *t;
252       t = g_find_program_in_path (try_exec);
253       if (t == NULL)
254         {
255           g_free (try_exec);
256           return NULL;
257         }
258       g_free (t);
259     }
260
261   info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
262   info->filename = NULL;
263
264   info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
265   info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
266   info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
267   info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
268   info->icon_name =  g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
269   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);
270   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);
271   info->try_exec = try_exec;
272   info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
273   info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
274   info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
275   info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
276   info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
277   info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
278   
279   info->icon = NULL;
280   if (info->icon_name)
281     {
282       if (g_path_is_absolute (info->icon_name))
283         {
284           GFile *file;
285           
286           file = g_file_new_for_path (info->icon_name);
287           info->icon = g_file_icon_new (file);
288           g_object_unref (file);
289         }
290       else
291         {
292           char *p;
293
294           /* Work around a common mistake in desktop files */    
295           if ((p = strrchr (info->icon_name, '.')) != NULL &&
296               (strcmp (p, ".png") == 0 ||
297                strcmp (p, ".xpm") == 0 ||
298                strcmp (p, ".svg") == 0)) 
299             *p = 0;
300
301           info->icon = g_themed_icon_new (info->icon_name);
302         }
303     }
304   
305   if (info->exec)
306     info->binary = binary_from_exec (info->exec);
307   
308   if (info->path && info->path[0] == '\0')
309     {
310       g_free (info->path);
311       info->path = NULL;
312     }
313
314   return info;
315 }
316
317 /**
318  * g_desktop_app_info_new_from_filename:
319  * @filename: the path of a desktop file, in the GLib filename encoding
320  * 
321  * Creates a new #GDesktopAppInfo.
322  *
323  * Returns: a new #GDesktopAppInfo or %NULL on error.
324  **/
325 GDesktopAppInfo *
326 g_desktop_app_info_new_from_filename (const char *filename)
327 {
328   GKeyFile *key_file;
329   GDesktopAppInfo *info = NULL;
330
331   key_file = g_key_file_new ();
332   
333   if (g_key_file_load_from_file (key_file,
334                                  filename,
335                                  G_KEY_FILE_NONE,
336                                  NULL))
337     {
338       info = g_desktop_app_info_new_from_keyfile (key_file);
339       if (info)
340         info->filename = g_strdup (filename);
341     }  
342
343   g_key_file_free (key_file);
344
345   return info;
346 }
347
348 /**
349  * g_desktop_app_info_new:
350  * @desktop_id: the desktop file id
351  * 
352  * Creates a new #GDesktopAppInfo based on a desktop file id. 
353  *
354  * A desktop file id is the basename of the desktop file, including the 
355  * .desktop extension. GIO is looking for a desktop file with this name 
356  * in the <filename>applications</filename> subdirectories of the XDG data
357  * directories (i.e. the directories specified in the 
358  * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment 
359  * variables). GIO also supports the prefix-to-subdirectory mapping that is
360  * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink> 
361  * (i.e. a desktop id of kde-foo.desktop will match
362  * <filename>/usr/share/applications/kde/foo.desktop</filename>).
363  * 
364  * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
365  */
366 GDesktopAppInfo *
367 g_desktop_app_info_new (const char *desktop_id)
368 {
369   GDesktopAppInfo *appinfo;
370   const char * const *dirs;
371   char *basename;
372   int i;
373
374   dirs = get_applications_search_path ();
375
376   basename = g_strdup (desktop_id);
377   
378   for (i = 0; dirs[i] != NULL; i++)
379     {
380       char *filename;
381       char *p;
382
383       filename = g_build_filename (dirs[i], desktop_id, NULL);
384       appinfo = g_desktop_app_info_new_from_filename (filename);
385       g_free (filename);
386       if (appinfo != NULL)
387         goto found;
388
389       p = basename;
390       while ((p = strchr (p, '-')) != NULL)
391         {
392           *p = '/';
393           
394           filename = g_build_filename (dirs[i], basename, NULL);
395           appinfo = g_desktop_app_info_new_from_filename (filename);
396           g_free (filename);
397           if (appinfo != NULL)
398             goto found;
399           *p = '-';
400           p++;
401         }
402     }
403   
404   g_free (basename);
405   return NULL;
406
407  found:
408   g_free (basename);
409   
410   appinfo->desktop_id = g_strdup (desktop_id);
411
412   if (g_desktop_app_info_get_is_hidden (appinfo))
413     {
414       g_object_unref (appinfo);
415       appinfo = NULL;
416     }
417   
418   return appinfo;
419 }
420
421 static GAppInfo *
422 g_desktop_app_info_dup (GAppInfo *appinfo)
423 {
424   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
425   GDesktopAppInfo *new_info;
426   
427   new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
428
429   new_info->filename = g_strdup (info->filename);
430   new_info->desktop_id = g_strdup (info->desktop_id);
431   
432   new_info->name = g_strdup (info->name);
433   new_info->fullname = g_strdup (info->fullname);
434   new_info->comment = g_strdup (info->comment);
435   new_info->nodisplay = info->nodisplay;
436   new_info->icon_name = g_strdup (info->icon_name);
437   if (info->icon)
438     new_info->icon = g_object_ref (info->icon);
439   new_info->only_show_in = g_strdupv (info->only_show_in);
440   new_info->not_show_in = g_strdupv (info->not_show_in);
441   new_info->try_exec = g_strdup (info->try_exec);
442   new_info->exec = g_strdup (info->exec);
443   new_info->binary = g_strdup (info->binary);
444   new_info->path = g_strdup (info->path);
445   new_info->hidden = info->hidden;
446   new_info->terminal = info->terminal;
447   new_info->startup_notify = info->startup_notify;
448   
449   return G_APP_INFO (new_info);
450 }
451
452 static gboolean
453 g_desktop_app_info_equal (GAppInfo *appinfo1,
454                           GAppInfo *appinfo2)
455 {
456   GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
457   GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
458
459   if (info1->desktop_id == NULL ||
460       info2->desktop_id == NULL)
461     return info1 == info2;
462
463   return strcmp (info1->desktop_id, info2->desktop_id) == 0;
464 }
465
466 static const char *
467 g_desktop_app_info_get_id (GAppInfo *appinfo)
468 {
469   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
470
471   return info->desktop_id;
472 }
473
474 static const char *
475 g_desktop_app_info_get_name (GAppInfo *appinfo)
476 {
477   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
478
479   if (info->name == NULL)
480     return _("Unnamed");
481   return info->name;
482 }
483
484 static const char *
485 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
486 {
487   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
488
489   if (info->fullname == NULL)
490     return g_desktop_app_info_get_name (appinfo);
491   return info->fullname;
492 }
493
494 /**
495  * g_desktop_app_info_get_is_hidden:
496  * @info: a #GDesktopAppInfo.
497  *
498  * A desktop file is hidden if the Hidden key in it is
499  * set to True.
500  *
501  * Returns: %TRUE if hidden, %FALSE otherwise. 
502  **/
503 gboolean
504 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
505 {
506   return info->hidden;
507 }
508
509 /**
510  * g_desktop_app_info_get_filename:
511  * @info: a #GDesktopAppInfo
512  *
513  * When @info was created from a known filename, return it.  In some
514  * situations such as the #GDesktopAppInfo returned from
515  * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
516  *
517  * Returns: The full path to the file for @info, or %NULL if not known.
518  * Since: 2.24
519  */
520 const char *
521 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
522 {
523   return info->filename;
524 }
525
526 static const char *
527 g_desktop_app_info_get_description (GAppInfo *appinfo)
528 {
529   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
530   
531   return info->comment;
532 }
533
534 static const char *
535 g_desktop_app_info_get_executable (GAppInfo *appinfo)
536 {
537   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
538   
539   return info->binary;
540 }
541
542 static const char *
543 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
544 {
545   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
546   
547   return info->exec;
548 }
549
550 static GIcon *
551 g_desktop_app_info_get_icon (GAppInfo *appinfo)
552 {
553   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
554
555   return info->icon;
556 }
557
558 static char *
559 expand_macro_single (char macro, char *uri)
560 {
561   GFile *file;
562   char *result = NULL;
563   char *path, *name;
564
565   file = g_file_new_for_uri (uri);
566   path = g_file_get_path (file);
567   g_object_unref (file);
568   
569   switch (macro)
570     {
571     case 'u':
572     case 'U':   
573       result = g_shell_quote (uri);
574       break;
575     case 'f':
576     case 'F':
577       if (path)
578         result = g_shell_quote (path);
579       break;
580     case 'd':
581     case 'D':
582       if (path)
583         {
584           name = g_path_get_dirname (path);
585           result = g_shell_quote (name);
586           g_free (name);
587         }
588       break;
589     case 'n':
590     case 'N':
591       if (path)
592         {
593           name = g_path_get_basename (path);
594           result = g_shell_quote (name);
595           g_free (name);
596         }
597       break;
598     }
599
600   g_free (path);
601   
602   return result;
603 }
604
605 static void
606 expand_macro (char              macro, 
607               GString          *exec, 
608               GDesktopAppInfo  *info, 
609               GList           **uri_list)
610 {
611   GList *uris = *uri_list;
612   char *expanded;
613   gboolean force_file_uri;
614   char force_file_uri_macro;
615   char *uri;
616
617   g_return_if_fail (exec != NULL);
618
619   /* On %u and %U, pass POSIX file path pointing to the URI via
620    * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
621    * running or the URI doesn't have a POSIX file path via FUSE
622    * we'll just pass the URI.
623    */
624   force_file_uri_macro = macro;
625   force_file_uri = FALSE;
626   if (!info->no_fuse)
627     {
628       switch (macro)
629         {
630         case 'u':
631           force_file_uri_macro = 'f';
632           force_file_uri = TRUE;
633           break;
634         case 'U':
635           force_file_uri_macro = 'F';
636           force_file_uri = TRUE;
637           break;
638         default:
639           break;
640         }
641     }
642
643   switch (macro)
644     {
645     case 'u':
646     case 'f':
647     case 'd':
648     case 'n':
649       if (uris)
650         {
651           uri = uris->data;
652           if (!force_file_uri ||
653               /* Pass URI if it contains an anchor */
654               strchr (uri, '#') != NULL)
655             {
656               expanded = expand_macro_single (macro, uri);
657             }
658           else
659             {
660               expanded = expand_macro_single (force_file_uri_macro, uri);
661               if (expanded == NULL)
662                 expanded = expand_macro_single (macro, uri);
663             }
664
665           if (expanded)
666             {
667               g_string_append (exec, expanded);
668               g_free (expanded);
669             }
670           uris = uris->next;
671         }
672
673       break;
674
675     case 'U':   
676     case 'F':
677     case 'D':
678     case 'N':
679       while (uris)
680         {
681           uri = uris->data;
682           
683           if (!force_file_uri ||
684               /* Pass URI if it contains an anchor */
685               strchr (uri, '#') != NULL)
686             {
687               expanded = expand_macro_single (macro, uri);
688             }
689           else
690             {
691               expanded = expand_macro_single (force_file_uri_macro, uri);
692               if (expanded == NULL)
693                 expanded = expand_macro_single (macro, uri);
694             }
695
696           if (expanded)
697             {
698               g_string_append (exec, expanded);
699               g_free (expanded);
700             }
701           
702           uris = uris->next;
703           
704           if (uris != NULL && expanded)
705             g_string_append_c (exec, ' ');
706         }
707
708       break;
709
710     case 'i':
711       if (info->icon_name)
712         {
713           g_string_append (exec, "--icon ");
714           expanded = g_shell_quote (info->icon_name);
715           g_string_append (exec, expanded);
716           g_free (expanded);
717         }
718       break;
719
720     case 'c':
721       if (info->name) 
722         {
723           expanded = g_shell_quote (info->name);
724           g_string_append (exec, expanded);
725           g_free (expanded);
726         }
727       break;
728
729     case 'k':
730       if (info->filename) 
731         {
732           expanded = g_shell_quote (info->filename);
733           g_string_append (exec, expanded);
734           g_free (expanded);
735         }
736       break;
737
738     case 'm': /* deprecated */
739       break;
740
741     case '%':
742       g_string_append_c (exec, '%');
743       break;
744     }
745   
746   *uri_list = uris;
747 }
748
749 static gboolean
750 expand_application_parameters (GDesktopAppInfo   *info,
751                                GList            **uris,
752                                int               *argc,
753                                char            ***argv,
754                                GError           **error)
755 {
756   GList *uri_list = *uris;
757   const char *p = info->exec;
758   GString *expanded_exec;
759   gboolean res;
760
761   if (info->exec == NULL)
762     {
763       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
764                            _("Desktop file didn't specify Exec field"));
765       return FALSE;
766     }
767
768   expanded_exec = g_string_new (NULL);
769
770   while (*p)
771     {
772       if (p[0] == '%' && p[1] != '\0')
773         {
774           expand_macro (p[1], expanded_exec, info, uris);
775           p++;
776         }
777       else
778         g_string_append_c (expanded_exec, *p);
779
780       p++;
781     }
782
783   /* No file substitutions */
784   if (uri_list == *uris && uri_list != NULL)
785     {
786       /* If there is no macro default to %f. This is also what KDE does */
787       g_string_append_c (expanded_exec, ' ');
788       expand_macro ('f', expanded_exec, info, uris);
789     }
790
791   res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
792   g_string_free (expanded_exec, TRUE);
793   return res;
794 }
795
796 static gboolean
797 prepend_terminal_to_vector (int    *argc,
798                             char ***argv)
799 {
800 #ifndef G_OS_WIN32
801   char **real_argv;
802   int real_argc;
803   int i, j;
804   char **term_argv = NULL;
805   int term_argc = 0;
806   char *check;
807   char **the_argv;
808   
809   g_return_val_if_fail (argc != NULL, FALSE);
810   g_return_val_if_fail (argv != NULL, FALSE);
811         
812   /* sanity */
813   if(*argv == NULL)
814     *argc = 0;
815   
816   the_argv = *argv;
817
818   /* compute size if not given */
819   if (*argc < 0)
820     {
821       for (i = 0; the_argv[i] != NULL; i++)
822         ;
823       *argc = i;
824     }
825   
826   term_argc = 2;
827   term_argv = g_new0 (char *, 3);
828
829   check = g_find_program_in_path ("gnome-terminal");
830   if (check != NULL)
831     {
832       term_argv[0] = check;
833       /* Note that gnome-terminal takes -x and
834        * as -e in gnome-terminal is broken we use that. */
835       term_argv[1] = g_strdup ("-x");
836     }
837   else
838     {
839       if (check == NULL)
840         check = g_find_program_in_path ("nxterm");
841       if (check == NULL)
842         check = g_find_program_in_path ("color-xterm");
843       if (check == NULL)
844         check = g_find_program_in_path ("rxvt");
845       if (check == NULL)
846         check = g_find_program_in_path ("xterm");
847       if (check == NULL)
848         check = g_find_program_in_path ("dtterm");
849       if (check == NULL)
850         {
851           check = g_strdup ("xterm");
852           g_warning ("couldn't find a terminal, falling back to xterm");
853         }
854       term_argv[0] = check;
855       term_argv[1] = g_strdup ("-e");
856     }
857
858   real_argc = term_argc + *argc;
859   real_argv = g_new (char *, real_argc + 1);
860   
861   for (i = 0; i < term_argc; i++)
862     real_argv[i] = term_argv[i];
863   
864   for (j = 0; j < *argc; j++, i++)
865     real_argv[i] = (char *)the_argv[j];
866   
867   real_argv[i] = NULL;
868   
869   g_free (*argv);
870   *argv = real_argv;
871   *argc = real_argc;
872   
873   /* we use g_free here as we sucked all the inner strings
874    * out from it into real_argv */
875   g_free (term_argv);
876   return TRUE;
877 #else
878   return FALSE;
879 #endif /* G_OS_WIN32 */
880 }
881
882 static GList *
883 uri_list_segment_to_files (GList *start,
884                            GList *end)
885 {
886   GList *res;
887   GFile *file;
888
889   res = NULL;
890   while (start != NULL && start != end)
891     {
892       file = g_file_new_for_uri ((char *)start->data);
893       res = g_list_prepend (res, file);
894       start = start->next;
895     }
896
897   return g_list_reverse (res);
898 }
899
900 typedef struct
901 {
902   char *display;
903   char *sn_id;
904   char *desktop_file;
905 } ChildSetupData;
906
907 static void
908 child_setup (gpointer user_data)
909 {
910   ChildSetupData *data = user_data;
911
912   if (data->display)
913     g_setenv ("DISPLAY", data->display, TRUE);
914
915   if (data->sn_id)
916     g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
917
918   if (data->desktop_file)
919     {
920       gchar pid[20];
921
922       g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
923
924       g_snprintf (pid, 20, "%ld", (long)getpid ());
925       g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
926     }
927 }
928
929 static gboolean
930 g_desktop_app_info_launch_uris (GAppInfo           *appinfo,
931                                 GList              *uris,
932                                 GAppLaunchContext  *launch_context,
933                                 GError            **error)
934 {
935   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
936   gboolean completed = FALSE;
937   GList *old_uris;
938   GList *launched_files;
939   char **argv;
940   int argc;
941   ChildSetupData data;
942
943   g_return_val_if_fail (appinfo != NULL, FALSE);
944
945   argv = NULL;
946
947   do
948     {
949       old_uris = uris;
950       if (!expand_application_parameters (info, &uris,
951                                           &argc, &argv, error))
952         goto out;
953       
954       if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
955         {
956           g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
957                                _("Unable to find terminal required for application"));
958           goto out;
959         }
960
961       data.display = NULL;
962       data.sn_id = NULL;
963       data.desktop_file = info->filename;
964
965       if (launch_context)
966         {
967           launched_files = uri_list_segment_to_files (old_uris, uris);
968
969           data.display = g_app_launch_context_get_display (launch_context,
970                                                            appinfo,
971                                                            launched_files);
972
973           if (info->startup_notify)
974             data.sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
975                                                                      appinfo,
976                                                                      launched_files);
977           g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
978           g_list_free (launched_files);
979         }
980
981       if (!g_spawn_async (info->path,
982                           argv,
983                           NULL,
984                           G_SPAWN_SEARCH_PATH,
985                           child_setup,
986                           &data,
987                           NULL,
988                           error))
989         {
990           if (data.sn_id)
991             g_app_launch_context_launch_failed (launch_context, data.sn_id);
992
993           g_free (data.sn_id);
994           g_free (data.display);
995
996           goto out;
997         }
998
999       g_free (data.sn_id);
1000       g_free (data.display);
1001
1002       g_strfreev (argv);
1003       argv = NULL;
1004     }
1005   while (uris != NULL);
1006
1007   completed = TRUE;
1008
1009  out:
1010   g_strfreev (argv);
1011
1012   return completed;
1013 }
1014
1015 static gboolean
1016 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1017 {
1018   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1019  
1020   return info->exec && 
1021     ((strstr (info->exec, "%u") != NULL) ||
1022      (strstr (info->exec, "%U") != NULL));
1023 }
1024
1025 static gboolean
1026 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1027 {
1028   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1029  
1030   return info->exec && 
1031     ((strstr (info->exec, "%f") != NULL) ||
1032      (strstr (info->exec, "%F") != NULL));
1033 }
1034
1035 static gboolean
1036 g_desktop_app_info_launch (GAppInfo           *appinfo,
1037                            GList              *files,
1038                            GAppLaunchContext  *launch_context,
1039                            GError            **error)
1040 {
1041   GList *uris;
1042   char *uri;
1043   gboolean res;
1044
1045   uris = NULL;
1046   while (files)
1047     {
1048       uri = g_file_get_uri (files->data);
1049       uris = g_list_prepend (uris, uri);
1050       files = files->next;
1051     }
1052   
1053   uris = g_list_reverse (uris);
1054   
1055   res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1056   
1057   g_list_foreach  (uris, (GFunc)g_free, NULL);
1058   g_list_free (uris);
1059   
1060   return res;
1061 }
1062
1063 G_LOCK_DEFINE_STATIC (g_desktop_env);
1064 static gchar *g_desktop_env = NULL;
1065
1066 /**
1067  * g_desktop_app_info_set_desktop_env:
1068  * @desktop_env: a string specifying what desktop this is
1069  *
1070  * Sets the name of the desktop that the application is running in.
1071  * This is used by g_app_info_should_show() to evaluate the
1072  * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1073  * desktop entry fields.
1074  *
1075  * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop 
1076  * Menu specification</ulink> recognizes the following:
1077  * <simplelist>
1078  *   <member>GNOME</member>
1079  *   <member>KDE</member>
1080  *   <member>ROX</member>
1081  *   <member>XFCE</member>
1082  *   <member>Old</member> 
1083  * </simplelist>
1084  *
1085  * Should be called only once; subsequent calls are ignored.
1086  */
1087 void
1088 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1089 {
1090   G_LOCK (g_desktop_env);
1091   if (!g_desktop_env)
1092     g_desktop_env = g_strdup (desktop_env);
1093   G_UNLOCK (g_desktop_env);
1094 }
1095
1096 static gboolean
1097 g_desktop_app_info_should_show (GAppInfo *appinfo)
1098 {
1099   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1100   gboolean found;
1101   const gchar *desktop_env;
1102   int i;
1103
1104   if (info->nodisplay)
1105     return FALSE;
1106
1107   G_LOCK (g_desktop_env);
1108   desktop_env = g_desktop_env;
1109   G_UNLOCK (g_desktop_env);
1110
1111   if (info->only_show_in)
1112     {
1113       if (desktop_env == NULL)
1114         return FALSE;
1115       
1116       found = FALSE;
1117       for (i = 0; info->only_show_in[i] != NULL; i++)
1118         {
1119           if (strcmp (info->only_show_in[i], desktop_env) == 0)
1120             {
1121               found = TRUE;
1122               break;
1123             }
1124         }
1125       if (!found)
1126         return FALSE;
1127     }
1128
1129   if (info->not_show_in && desktop_env)
1130     {
1131       for (i = 0; info->not_show_in[i] != NULL; i++)
1132         {
1133           if (strcmp (info->not_show_in[i], desktop_env) == 0)
1134             return FALSE;
1135         }
1136     }
1137   
1138   return TRUE;
1139 }
1140
1141 typedef enum {
1142   APP_DIR,
1143   MIMETYPE_DIR
1144 } DirType;
1145
1146 static char *
1147 ensure_dir (DirType   type,
1148             GError  **error)
1149 {
1150   char *path, *display_name;
1151   int errsv;
1152
1153   if (type == APP_DIR)
1154     path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1155   else
1156     path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1157
1158   errno = 0;
1159   if (g_mkdir_with_parents (path, 0700) == 0)
1160     return path;
1161
1162   errsv = errno;
1163   display_name = g_filename_display_name (path);
1164   if (type == APP_DIR)
1165     g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1166                  _("Can't create user application configuration folder %s: %s"),
1167                  display_name, g_strerror (errsv));
1168   else
1169     g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1170                  _("Can't create user MIME configuration folder %s: %s"),
1171                  display_name, g_strerror (errsv));
1172
1173   g_free (display_name);
1174   g_free (path);
1175
1176   return NULL;
1177 }
1178
1179 static gboolean
1180 update_mimeapps_list (const char  *desktop_id, 
1181                       const char  *content_type,
1182                       UpdateMimeFlags flags,
1183                       GError     **error)
1184 {
1185   char *dirname, *filename, *string;
1186   GKeyFile *key_file;
1187   gboolean load_succeeded, res, explicit_default;
1188   char **old_list, **list;
1189   GList *system_list;
1190   gsize length, data_size;
1191   char *data;
1192   int i, j, k;
1193   char **content_types;
1194
1195   /* Don't add both at start and end */
1196   g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1197               (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1198
1199   dirname = ensure_dir (APP_DIR, error);
1200   if (!dirname)
1201     return FALSE;
1202
1203   filename = g_build_filename (dirname, "mimeapps.list", NULL);
1204   g_free (dirname);
1205
1206   key_file = g_key_file_new ();
1207   load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1208   if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1209     {
1210       g_key_file_free (key_file);
1211       key_file = g_key_file_new ();
1212     }
1213
1214   if (content_type)
1215     {
1216       content_types = g_new (char *, 2);
1217       content_types[0] = g_strdup (content_type);
1218       content_types[1] = NULL;
1219     }
1220   else
1221     {
1222       content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1223     }
1224
1225   explicit_default = FALSE;
1226
1227   for (k = 0; content_types && content_types[k]; k++)
1228     {
1229       /* set as default, if requested so */
1230       string = g_key_file_get_string (key_file,
1231                                       DEFAULT_APPLICATIONS_GROUP,
1232                                       content_types[k],
1233                                       NULL);
1234
1235       if (g_strcmp0 (string, desktop_id) != 0 &&
1236           (flags & UPDATE_MIME_SET_DEFAULT))
1237         {
1238           g_free (string);
1239           string = g_strdup (desktop_id);
1240
1241           /* add in the non-default list too, if it's not already there */
1242           flags |= UPDATE_MIME_SET_NON_DEFAULT;
1243         }
1244
1245       if (string == NULL || desktop_id == NULL)
1246         g_key_file_remove_key (key_file,
1247                                DEFAULT_APPLICATIONS_GROUP,
1248                                content_types[k],
1249                                NULL);
1250       else
1251         {
1252           g_key_file_set_string (key_file,
1253                                  DEFAULT_APPLICATIONS_GROUP,
1254                                  content_types[k],
1255                                  string);
1256
1257           explicit_default = TRUE;
1258         }
1259
1260       g_free (string);
1261     }
1262
1263   if (content_type)
1264     {
1265       /* reuse the list from above */
1266     }
1267   else
1268     {
1269       g_strfreev (content_types);
1270       content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1271     }
1272   
1273   for (k = 0; content_types && content_types[k]; k++)
1274     { 
1275       /* Add to the right place in the list */
1276   
1277       length = 0;
1278       old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1279                                              content_types[k], &length, NULL);
1280
1281       list = g_new (char *, 1 + length + 1);
1282
1283       i = 0;
1284
1285       /* if we're adding a last-used hint, just put the application in front of the list */
1286       if (flags & UPDATE_MIME_SET_LAST_USED)
1287         {
1288           /* avoid adding this again as non-default later */
1289           if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1290             flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1291
1292           list[i++] = g_strdup (desktop_id);
1293         }
1294
1295       if (old_list)
1296         {
1297           for (j = 0; old_list[j] != NULL; j++)
1298             {
1299               if (g_strcmp0 (old_list[j], desktop_id) != 0)
1300                 {
1301                   /* rewrite other entries if they're different from the new one */
1302                   list[i++] = g_strdup (old_list[j]);
1303                 }
1304               else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1305                 {
1306                   /* we encountered an old entry which is equal to the one we're adding as non-default,
1307                    * don't change its position in the list.
1308                    */
1309                   flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1310                   list[i++] = g_strdup (old_list[j]);
1311                 }
1312             }
1313         }
1314
1315       /* add it at the end of the list */
1316       if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1317         list[i++] = g_strdup (desktop_id);
1318
1319       list[i] = NULL;
1320   
1321       g_strfreev (old_list);
1322
1323       if (list[0] == NULL || desktop_id == NULL)
1324         g_key_file_remove_key (key_file,
1325                                ADDED_ASSOCIATIONS_GROUP,
1326                                content_types[k],
1327                                NULL);
1328       else
1329         {
1330           g_key_file_set_string_list (key_file,
1331                                       ADDED_ASSOCIATIONS_GROUP,
1332                                       content_types[k],
1333                                       (const char * const *)list, i);
1334
1335           /* if we had no explicit default set, we should add the system default to the
1336            * list, to avoid overriding it with applications from this list.
1337            */
1338           if (!explicit_default)
1339             {
1340               system_list = get_all_desktop_entries_for_mime_type (content_type, (const char **) list, FALSE, NULL);
1341
1342               if (system_list != NULL)
1343                 {
1344                   string = system_list->data;
1345
1346                   g_key_file_set_string (key_file,
1347                                          DEFAULT_APPLICATIONS_GROUP,
1348                                          content_types[k],
1349                                          string);
1350                 }
1351
1352               g_list_free_full (system_list, g_free);
1353             }
1354         }
1355    
1356       g_strfreev (list);
1357     }
1358   
1359   if (content_type)
1360     {
1361       /* reuse the list from above */
1362     }
1363   else
1364     {
1365       g_strfreev (content_types);
1366       content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1367     }
1368
1369   for (k = 0; content_types && content_types[k]; k++) 
1370     {
1371       /* Remove from removed associations group (unless remove) */
1372   
1373       length = 0;
1374       old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1375                                              content_types[k], &length, NULL);
1376
1377       list = g_new (char *, 1 + length + 1);
1378
1379       i = 0;
1380       if (flags & UPDATE_MIME_REMOVE)
1381         list[i++] = g_strdup (desktop_id);
1382       if (old_list)
1383         {
1384           for (j = 0; old_list[j] != NULL; j++)
1385             {
1386               if (g_strcmp0 (old_list[j], desktop_id) != 0)
1387                 list[i++] = g_strdup (old_list[j]);
1388             }
1389         }
1390       list[i] = NULL;
1391   
1392       g_strfreev (old_list);
1393
1394       if (list[0] == NULL || desktop_id == NULL)
1395         g_key_file_remove_key (key_file,
1396                                REMOVED_ASSOCIATIONS_GROUP,
1397                                content_types[k],
1398                                NULL);
1399       else
1400         g_key_file_set_string_list (key_file,
1401                                     REMOVED_ASSOCIATIONS_GROUP,
1402                                     content_types[k],
1403                                     (const char * const *)list, i);
1404
1405       g_strfreev (list);
1406     }
1407
1408   g_strfreev (content_types);  
1409
1410   data = g_key_file_to_data (key_file, &data_size, error);
1411   g_key_file_free (key_file);
1412   
1413   res = g_file_set_contents (filename, data, data_size, error);
1414
1415   mime_info_cache_reload (NULL);
1416                           
1417   g_free (filename);
1418   g_free (data);
1419   
1420   return res;
1421 }
1422
1423 static gboolean
1424 g_desktop_app_info_set_as_last_used_for_type (GAppInfo    *appinfo,
1425                                               const char  *content_type,
1426                                               GError     **error)
1427 {
1428   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1429
1430   if (!g_desktop_app_info_ensure_saved (info, error))
1431     return FALSE;
1432
1433   /* both add support for the content type and set as last used */
1434   return update_mimeapps_list (info->desktop_id, content_type,
1435                                UPDATE_MIME_SET_NON_DEFAULT |
1436                                UPDATE_MIME_SET_LAST_USED,
1437                                error);
1438 }
1439
1440 static gboolean
1441 g_desktop_app_info_set_as_default_for_type (GAppInfo    *appinfo,
1442                                             const char  *content_type,
1443                                             GError     **error)
1444 {
1445   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1446
1447   if (!g_desktop_app_info_ensure_saved (info, error))
1448     return FALSE;  
1449   
1450   return update_mimeapps_list (info->desktop_id, content_type,
1451                                UPDATE_MIME_SET_DEFAULT,
1452                                error);
1453 }
1454
1455 static void
1456 update_program_done (GPid     pid,
1457                      gint     status,
1458                      gpointer data)
1459 {
1460   /* Did the application exit correctly */
1461   if (WIFEXITED (status) &&
1462       WEXITSTATUS (status) == 0)
1463     {
1464       /* Here we could clean out any caches in use */
1465     }
1466 }
1467
1468 static void
1469 run_update_command (char *command,
1470                     char *subdir)
1471 {
1472         char *argv[3] = {
1473                 NULL,
1474                 NULL,
1475                 NULL,
1476         };
1477         GPid pid = 0;
1478         GError *error = NULL;
1479
1480         argv[0] = command;
1481         argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1482
1483         if (g_spawn_async ("/", argv,
1484                            NULL,       /* envp */
1485                            G_SPAWN_SEARCH_PATH |
1486                            G_SPAWN_STDOUT_TO_DEV_NULL |
1487                            G_SPAWN_STDERR_TO_DEV_NULL |
1488                            G_SPAWN_DO_NOT_REAP_CHILD,
1489                            NULL, NULL, /* No setup function */
1490                            &pid,
1491                            &error)) 
1492           g_child_watch_add (pid, update_program_done, NULL);
1493         else
1494           {
1495             /* If we get an error at this point, it's quite likely the user doesn't
1496              * have an installed copy of either 'update-mime-database' or
1497              * 'update-desktop-database'.  I don't think we want to popup an error
1498              * dialog at this point, so we just do a g_warning to give the user a
1499              * chance of debugging it.
1500              */
1501             g_warning ("%s", error->message);
1502           }
1503         
1504         g_free (argv[1]);
1505 }
1506
1507 static gboolean
1508 g_desktop_app_info_set_as_default_for_extension (GAppInfo    *appinfo,
1509                                                  const char  *extension,
1510                                                  GError     **error)
1511 {
1512   char *filename, *basename, *mimetype;
1513   char *dirname;
1514   gboolean res;
1515
1516   if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1517     return FALSE;  
1518   
1519   dirname = ensure_dir (MIMETYPE_DIR, error);
1520   if (!dirname)
1521     return FALSE;
1522   
1523   basename = g_strdup_printf ("user-extension-%s.xml", extension);
1524   filename = g_build_filename (dirname, basename, NULL);
1525   g_free (basename);
1526   g_free (dirname);
1527
1528   mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1529   
1530   if (!g_file_test (filename, G_FILE_TEST_EXISTS)) 
1531     {
1532       char *contents;
1533
1534       contents =
1535         g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1536                          "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1537                          " <mime-type type=\"%s\">\n"
1538                          "  <comment>%s document</comment>\n"
1539                          "  <glob pattern=\"*.%s\"/>\n"
1540                          " </mime-type>\n"
1541                          "</mime-info>\n", mimetype, extension, extension);
1542
1543       g_file_set_contents (filename, contents, -1, NULL);
1544       g_free (contents);
1545
1546       run_update_command ("update-mime-database", "mime");
1547     }
1548   g_free (filename);
1549   
1550   res = g_desktop_app_info_set_as_default_for_type (appinfo,
1551                                                     mimetype,
1552                                                     error);
1553
1554   g_free (mimetype);
1555   
1556   return res;
1557 }
1558
1559 static gboolean
1560 g_desktop_app_info_add_supports_type (GAppInfo    *appinfo,
1561                                       const char  *content_type,
1562                                       GError     **error)
1563 {
1564   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1565
1566   if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1567     return FALSE;  
1568   
1569   return update_mimeapps_list (info->desktop_id, content_type,
1570                                UPDATE_MIME_SET_NON_DEFAULT,
1571                                error);
1572 }
1573
1574 static gboolean
1575 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1576 {
1577   return TRUE;
1578 }
1579
1580 static gboolean
1581 g_desktop_app_info_remove_supports_type (GAppInfo    *appinfo,
1582                                          const char  *content_type,
1583                                          GError     **error)
1584 {
1585   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1586
1587   if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1588     return FALSE;
1589   
1590   return update_mimeapps_list (info->desktop_id, content_type,
1591                                UPDATE_MIME_REMOVE,
1592                                error);
1593 }
1594
1595 static gboolean
1596 g_desktop_app_info_ensure_saved (GDesktopAppInfo  *info,
1597                                  GError          **error)
1598 {
1599   GKeyFile *key_file;
1600   char *dirname;
1601   char *filename;
1602   char *data, *desktop_id;
1603   gsize data_size;
1604   int fd;
1605   gboolean res;
1606   
1607   if (info->filename != NULL)
1608     return TRUE;
1609
1610   /* This is only used for object created with
1611    * g_app_info_create_from_commandline. All other
1612    * object should have a filename
1613    */
1614   
1615   dirname = ensure_dir (APP_DIR, error);
1616   if (!dirname)
1617     return FALSE;
1618   
1619   key_file = g_key_file_new ();
1620
1621   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1622                          "Encoding", "UTF-8");
1623   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1624                          G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1625   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1626                          G_KEY_FILE_DESKTOP_KEY_TYPE,
1627                          G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1628   if (info->terminal) 
1629     g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1630                             G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1631
1632   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1633                          G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1634
1635   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1636                          G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1637
1638   if (info->fullname != NULL)
1639     g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1640                            FULL_NAME_KEY, info->fullname);
1641
1642   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1643                          G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1644   
1645   g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1646                           G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1647
1648   data = g_key_file_to_data (key_file, &data_size, NULL);
1649   g_key_file_free (key_file);
1650
1651   desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1652   filename = g_build_filename (dirname, desktop_id, NULL);
1653   g_free (desktop_id);
1654   g_free (dirname);
1655   
1656   fd = g_mkstemp (filename);
1657   if (fd == -1)
1658     {
1659       char *display_name;
1660
1661       display_name = g_filename_display_name (filename);
1662       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1663                    _("Can't create user desktop file %s"), display_name);
1664       g_free (display_name);
1665       g_free (filename);
1666       g_free (data);
1667       return FALSE;
1668     }
1669
1670   desktop_id = g_path_get_basename (filename);
1671
1672   close (fd);
1673   
1674   res = g_file_set_contents (filename, data, data_size, error);
1675   if (!res)
1676     {
1677       g_free (desktop_id);
1678       g_free (filename);
1679       return FALSE;
1680     }
1681
1682   info->filename = filename;
1683   info->desktop_id = desktop_id;
1684   
1685   run_update_command ("update-desktop-database", "applications");
1686   
1687   return TRUE;
1688 }
1689
1690 static gboolean
1691 g_desktop_app_info_can_delete (GAppInfo *appinfo)
1692 {
1693   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1694
1695   if (info->filename)
1696     {
1697       if (strstr (info->filename, "/userapp-"))
1698         return g_access (info->filename, W_OK) == 0;
1699     }
1700
1701   return FALSE;
1702 }
1703
1704 static gboolean
1705 g_desktop_app_info_delete (GAppInfo *appinfo)
1706 {
1707   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1708   
1709   if (info->filename)
1710     { 
1711       if (g_remove (info->filename) == 0)
1712         {
1713           update_mimeapps_list (info->desktop_id, NULL,
1714                                 UPDATE_MIME_NONE,
1715                                 NULL);
1716
1717           g_free (info->filename);
1718           info->filename = NULL;
1719           g_free (info->desktop_id);
1720           info->desktop_id = NULL;
1721
1722           return TRUE;
1723         }
1724     }
1725
1726   return FALSE;
1727 }
1728
1729 /**
1730  * g_app_info_create_from_commandline:
1731  * @commandline: the commandline to use
1732  * @application_name: (allow-none): the application name, or %NULL to use @commandline
1733  * @flags: flags that can specify details of the created #GAppInfo
1734  * @error: a #GError location to store the error occuring, %NULL to ignore.
1735  *
1736  * Creates a new #GAppInfo from the given information.
1737  *
1738  * Returns: (transfer full): new #GAppInfo for given command.
1739  **/
1740 GAppInfo *
1741 g_app_info_create_from_commandline (const char           *commandline,
1742                                     const char           *application_name,
1743                                     GAppInfoCreateFlags   flags,
1744                                     GError              **error)
1745 {
1746   char **split;
1747   char *basename;
1748   GDesktopAppInfo *info;
1749
1750   g_return_val_if_fail (commandline, NULL);
1751
1752   info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1753
1754   info->filename = NULL;
1755   info->desktop_id = NULL;
1756   
1757   info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1758   info->startup_notify = flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION;
1759   info->hidden = FALSE;
1760   if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1761     info->exec = g_strconcat (commandline, " %u", NULL);
1762   else
1763     info->exec = g_strconcat (commandline, " %f", NULL);
1764   info->nodisplay = TRUE;
1765   info->binary = binary_from_exec (info->exec);
1766   
1767   if (application_name)
1768     info->name = g_strdup (application_name);
1769   else
1770     {
1771       /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1772       split = g_strsplit (commandline, " ", 2);
1773       basename = split[0] ? g_path_get_basename (split[0]) : NULL;
1774       g_strfreev (split);
1775       info->name = basename;
1776       if (info->name == NULL)
1777         info->name = g_strdup ("custom");
1778     }
1779   info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1780   
1781   return G_APP_INFO (info);
1782 }
1783
1784 static void
1785 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1786 {
1787   iface->dup = g_desktop_app_info_dup;
1788   iface->equal = g_desktop_app_info_equal;
1789   iface->get_id = g_desktop_app_info_get_id;
1790   iface->get_name = g_desktop_app_info_get_name;
1791   iface->get_description = g_desktop_app_info_get_description;
1792   iface->get_executable = g_desktop_app_info_get_executable;
1793   iface->get_icon = g_desktop_app_info_get_icon;
1794   iface->launch = g_desktop_app_info_launch;
1795   iface->supports_uris = g_desktop_app_info_supports_uris;
1796   iface->supports_files = g_desktop_app_info_supports_files;
1797   iface->launch_uris = g_desktop_app_info_launch_uris;
1798   iface->should_show = g_desktop_app_info_should_show;
1799   iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1800   iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1801   iface->add_supports_type = g_desktop_app_info_add_supports_type;
1802   iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1803   iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1804   iface->can_delete = g_desktop_app_info_can_delete;
1805   iface->do_delete = g_desktop_app_info_delete;
1806   iface->get_commandline = g_desktop_app_info_get_commandline;
1807   iface->get_display_name = g_desktop_app_info_get_display_name;
1808   iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
1809 }
1810
1811 static gboolean
1812 app_info_in_list (GAppInfo *info, 
1813                   GList    *list)
1814 {
1815   while (list != NULL)
1816     {
1817       if (g_app_info_equal (info, list->data))
1818         return TRUE;
1819       list = list->next;
1820     }
1821   return FALSE;
1822 }
1823
1824 /**
1825  * g_app_info_get_recommended_for_type:
1826  * @content_type: the content type to find a #GAppInfo for
1827  * 
1828  * Gets a list of recommended #GAppInfos for a given content type, i.e.
1829  * those applications which claim to support the given content type exactly,
1830  * and not by MIME type subclassing.
1831  * Note that the first application of the list is the last used one, i.e.
1832  * the last one for which #g_app_info_set_as_last_used_for_type has been
1833  * called.
1834  *
1835  * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
1836  *     for given @content_type or %NULL on error.
1837  *
1838  * Since: 2.28
1839  **/
1840 GList *
1841 g_app_info_get_recommended_for_type (const gchar *content_type)
1842 {
1843   GList *desktop_entries, *l;
1844   GList *infos;
1845   GDesktopAppInfo *info;
1846
1847   g_return_val_if_fail (content_type != NULL, NULL);
1848
1849   desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
1850
1851   infos = NULL;
1852   for (l = desktop_entries; l != NULL; l = l->next)
1853     {
1854       char *desktop_entry = l->data;
1855
1856       info = g_desktop_app_info_new (desktop_entry);
1857       if (info)
1858         {
1859           if (app_info_in_list (G_APP_INFO (info), infos))
1860             g_object_unref (info);
1861           else
1862             infos = g_list_prepend (infos, info);
1863         }
1864       g_free (desktop_entry);
1865     }
1866
1867   g_list_free (desktop_entries);
1868
1869   return g_list_reverse (infos);
1870 }
1871
1872 /**
1873  * g_app_info_get_fallback_for_type:
1874  * @content_type: the content type to find a #GAppInfo for
1875  * 
1876  * Gets a list of fallback #GAppInfos for a given content type, i.e.
1877  * those applications which claim to support the given content type
1878  * by MIME type subclassing and not directly.
1879  *
1880  * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
1881  *     for given @content_type or %NULL on error.
1882  *
1883  * Since: 2.28
1884  **/
1885 GList *
1886 g_app_info_get_fallback_for_type (const gchar *content_type)
1887 {
1888   GList *desktop_entries, *l;
1889   GList *infos, *recommended_infos;
1890   GDesktopAppInfo *info;
1891
1892   g_return_val_if_fail (content_type != NULL, NULL);
1893
1894   desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
1895   recommended_infos = g_app_info_get_recommended_for_type (content_type);
1896
1897   infos = NULL;
1898   for (l = desktop_entries; l != NULL; l = l->next)
1899     {
1900       char *desktop_entry = l->data;
1901
1902       info = g_desktop_app_info_new (desktop_entry);
1903       if (info)
1904         {
1905           if (app_info_in_list (G_APP_INFO (info), infos) ||
1906               app_info_in_list (G_APP_INFO (info), recommended_infos))
1907             g_object_unref (info);
1908           else
1909             infos = g_list_prepend (infos, info);
1910         }
1911       g_free (desktop_entry);
1912     }
1913
1914   g_list_free (desktop_entries);
1915   g_list_free_full (recommended_infos, g_object_unref);
1916
1917   return g_list_reverse (infos);
1918 }
1919
1920 /**
1921  * g_app_info_get_all_for_type:
1922  * @content_type: the content type to find a #GAppInfo for
1923  * 
1924  * Gets a list of all #GAppInfos for a given content type.
1925  *
1926  * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
1927  *     for given @content_type or %NULL on error.
1928  **/
1929 GList *
1930 g_app_info_get_all_for_type (const char *content_type)
1931 {
1932   GList *desktop_entries, *l;
1933   GList *infos;
1934   char *user_default = NULL;
1935   GDesktopAppInfo *info;
1936
1937   g_return_val_if_fail (content_type != NULL, NULL);
1938   
1939   desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
1940   infos = NULL;
1941
1942   /* put the user default in front of the list, for compatibility */
1943   if (user_default != NULL)
1944     {
1945       info = g_desktop_app_info_new (user_default);
1946
1947       if (info != NULL)
1948         infos = g_list_prepend (infos, info);
1949     }
1950
1951   g_free (user_default);
1952
1953   for (l = desktop_entries; l != NULL; l = l->next)
1954     {
1955       char *desktop_entry = l->data;
1956
1957       info = g_desktop_app_info_new (desktop_entry);
1958       if (info)
1959         {
1960           if (app_info_in_list (G_APP_INFO (info), infos))
1961             g_object_unref (info);
1962           else
1963             infos = g_list_prepend (infos, info);
1964         }
1965       g_free (desktop_entry);
1966     }
1967
1968   g_list_free (desktop_entries);
1969   
1970   return g_list_reverse (infos);
1971 }
1972
1973 /**
1974  * g_app_info_reset_type_associations:
1975  * @content_type: a content type 
1976  *
1977  * Removes all changes to the type associations done by
1978  * g_app_info_set_as_default_for_type(), 
1979  * g_app_info_set_as_default_for_extension(), 
1980  * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
1981  *
1982  * Since: 2.20
1983  */
1984 void
1985 g_app_info_reset_type_associations (const char *content_type)
1986 {
1987   update_mimeapps_list (NULL, content_type,
1988                         UPDATE_MIME_NONE,
1989                         NULL);
1990 }
1991
1992 /**
1993  * g_app_info_get_default_for_type:
1994  * @content_type: the content type to find a #GAppInfo for
1995  * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1996  *     support URIs
1997  * 
1998  * Gets the #GAppInfo that corresponds to a given content type.
1999  *
2000  * Returns: #GAppInfo for given @content_type or %NULL on error.
2001  **/
2002 GAppInfo *
2003 g_app_info_get_default_for_type (const char *content_type,
2004                                  gboolean    must_support_uris)
2005 {
2006   GList *desktop_entries, *l;
2007   char *user_default = NULL;
2008   GAppInfo *info;
2009
2010   g_return_val_if_fail (content_type != NULL, NULL);
2011   
2012   desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2013
2014   info = NULL;
2015
2016   if (user_default != NULL)
2017     {
2018       info = (GAppInfo *) g_desktop_app_info_new (user_default);
2019
2020       if (info)
2021         {
2022           if (must_support_uris && !g_app_info_supports_uris (info))
2023             {
2024               g_object_unref (info);
2025               info = NULL;
2026             }
2027         }
2028     }
2029
2030   g_free (user_default);
2031
2032   if (info != NULL)
2033     {
2034       g_list_free_full (desktop_entries, g_free);
2035       return info;
2036     }
2037
2038   /* pick the first from the other list that matches our URI
2039    * requirements.
2040    */
2041   for (l = desktop_entries; l != NULL; l = l->next)
2042     {
2043       char *desktop_entry = l->data;
2044
2045       info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2046       if (info)
2047         {
2048           if (must_support_uris && !g_app_info_supports_uris (info))
2049             {
2050               g_object_unref (info);
2051               info = NULL;
2052             }
2053           else
2054             break;
2055         }
2056     }
2057   
2058   g_list_free_full (desktop_entries, g_free);
2059
2060   return info;
2061 }
2062
2063 /**
2064  * g_app_info_get_default_for_uri_scheme:
2065  * @uri_scheme: a string containing a URI scheme.
2066  *
2067  * Gets the default application for launching applications 
2068  * using this URI scheme. A URI scheme is the initial part 
2069  * of the URI, up to but not including the ':', e.g. "http", 
2070  * "ftp" or "sip".
2071  * 
2072  * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2073  **/
2074 GAppInfo *
2075 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2076 {
2077   GAppInfo *app_info;
2078   char *content_type, *scheme_down;
2079
2080   scheme_down = g_ascii_strdown (uri_scheme, -1);
2081   content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2082   g_free (scheme_down);
2083   app_info = g_app_info_get_default_for_type (content_type, FALSE);
2084   g_free (content_type);
2085
2086   return app_info;
2087 }
2088
2089 static void
2090 get_apps_from_dir (GHashTable *apps, 
2091                    const char *dirname, 
2092                    const char *prefix)
2093 {
2094   GDir *dir;
2095   const char *basename;
2096   char *filename, *subprefix, *desktop_id;
2097   gboolean hidden;
2098   GDesktopAppInfo *appinfo;
2099   
2100   dir = g_dir_open (dirname, 0, NULL);
2101   if (dir)
2102     {
2103       while ((basename = g_dir_read_name (dir)) != NULL)
2104         {
2105           filename = g_build_filename (dirname, basename, NULL);
2106           if (g_str_has_suffix (basename, ".desktop"))
2107             {
2108               desktop_id = g_strconcat (prefix, basename, NULL);
2109
2110               /* Use _extended so we catch NULLs too (hidden) */
2111               if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2112                 {
2113                   appinfo = g_desktop_app_info_new_from_filename (filename);
2114                   hidden = FALSE;
2115
2116                   if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2117                     {
2118                       g_object_unref (appinfo);
2119                       appinfo = NULL;
2120                       hidden = TRUE;
2121                     }
2122                                       
2123                   if (appinfo || hidden)
2124                     {
2125                       g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2126
2127                       if (appinfo)
2128                         {
2129                           /* Reuse instead of strdup here */
2130                           appinfo->desktop_id = desktop_id;
2131                           desktop_id = NULL;
2132                         }
2133                     }
2134                 }
2135               g_free (desktop_id);
2136             }
2137           else
2138             {
2139               if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2140                 {
2141                   subprefix = g_strconcat (prefix, basename, "-", NULL);
2142                   get_apps_from_dir (apps, filename, subprefix);
2143                   g_free (subprefix);
2144                 }
2145             }
2146           g_free (filename);
2147         }
2148       g_dir_close (dir);
2149     }
2150 }
2151
2152
2153 /**
2154  * g_app_info_get_all:
2155  *
2156  * Gets a list of all of the applications currently registered 
2157  * on this system.
2158  * 
2159  * For desktop files, this includes applications that have 
2160  * <literal>NoDisplay=true</literal> set or are excluded from 
2161  * display by means of <literal>OnlyShowIn</literal> or
2162  * <literal>NotShowIn</literal>. See g_app_info_should_show().
2163  * The returned list does not include applications which have
2164  * the <literal>Hidden</literal> key set. 
2165  * 
2166  * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2167  **/
2168 GList *
2169 g_app_info_get_all (void)
2170 {
2171   const char * const *dirs;
2172   GHashTable *apps;
2173   GHashTableIter iter;
2174   gpointer value;
2175   int i;
2176   GList *infos;
2177
2178   dirs = get_applications_search_path ();
2179
2180   apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2181                                 g_free, NULL);
2182
2183   
2184   for (i = 0; dirs[i] != NULL; i++)
2185     get_apps_from_dir (apps, dirs[i], "");
2186
2187
2188   infos = NULL;
2189   g_hash_table_iter_init (&iter, apps);
2190   while (g_hash_table_iter_next (&iter, NULL, &value))
2191     {
2192       if (value)
2193         infos = g_list_prepend (infos, value);
2194     }
2195
2196   g_hash_table_destroy (apps);
2197
2198   return g_list_reverse (infos);
2199 }
2200
2201 /* Cacheing of mimeinfo.cache and defaults.list files */
2202
2203 typedef struct {
2204   char *path;
2205   GHashTable *mime_info_cache_map;
2206   GHashTable *defaults_list_map;
2207   GHashTable *mimeapps_list_added_map;
2208   GHashTable *mimeapps_list_removed_map;
2209   GHashTable *mimeapps_list_defaults_map;
2210   time_t mime_info_cache_timestamp;
2211   time_t defaults_list_timestamp;
2212   time_t mimeapps_list_timestamp;
2213 } MimeInfoCacheDir;
2214
2215 typedef struct {
2216   GList *dirs;                       /* mimeinfo.cache and defaults.list */
2217   GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2218   time_t last_stat_time;
2219   guint should_ping_mime_monitor : 1;
2220 } MimeInfoCache;
2221
2222 static MimeInfoCache *mime_info_cache = NULL;
2223 G_LOCK_DEFINE_STATIC (mime_info_cache);
2224
2225 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir  *dir,
2226                                                      const char        *mime_type,
2227                                                      char             **new_desktop_file_ids);
2228
2229 static MimeInfoCache * mime_info_cache_new (void);
2230
2231 static void
2232 destroy_info_cache_value (gpointer  key, 
2233                           GList    *value, 
2234                           gpointer  data)
2235 {
2236   g_list_foreach (value, (GFunc)g_free, NULL);
2237   g_list_free (value);
2238 }
2239
2240 static void
2241 destroy_info_cache_map (GHashTable *info_cache_map)
2242 {
2243   g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2244   g_hash_table_destroy (info_cache_map);
2245 }
2246
2247 static gboolean
2248 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2249                                  const char       *cache_file,
2250                                  time_t           *timestamp)
2251 {
2252   struct stat buf;
2253   char *filename;
2254   
2255   filename = g_build_filename (dir->path, cache_file, NULL);
2256   
2257   if (g_stat (filename, &buf) < 0)
2258     {
2259       g_free (filename);
2260       return TRUE;
2261     }
2262   g_free (filename);
2263
2264   if (buf.st_mtime != *timestamp) 
2265     return TRUE;
2266   
2267   return FALSE;
2268 }
2269
2270 /* Call with lock held */
2271 static gboolean
2272 remove_all (gpointer  key,
2273             gpointer  value,
2274             gpointer  user_data)
2275 {
2276   return TRUE;
2277 }
2278
2279
2280 static void
2281 mime_info_cache_blow_global_cache (void)
2282 {
2283   g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2284                                remove_all, NULL);
2285 }
2286
2287 static void
2288 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2289 {
2290   GError *load_error;
2291   GKeyFile *key_file;
2292   gchar *filename, **mime_types;
2293   int i;
2294   struct stat buf;
2295   
2296   load_error = NULL;
2297   mime_types = NULL;
2298   
2299   if (dir->mime_info_cache_map != NULL &&
2300       !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2301                                         &dir->mime_info_cache_timestamp))
2302     return;
2303   
2304   if (dir->mime_info_cache_map != NULL)
2305     destroy_info_cache_map (dir->mime_info_cache_map);
2306   
2307   dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2308                                                     (GDestroyNotify) g_free,
2309                                                     NULL);
2310   
2311   key_file = g_key_file_new ();
2312   
2313   filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2314   
2315   if (g_stat (filename, &buf) < 0)
2316     goto error;
2317   
2318   if (dir->mime_info_cache_timestamp > 0) 
2319     mime_info_cache->should_ping_mime_monitor = TRUE;
2320   
2321   dir->mime_info_cache_timestamp = buf.st_mtime;
2322   
2323   g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2324   
2325   g_free (filename);
2326   filename = NULL;
2327   
2328   if (load_error != NULL)
2329     goto error;
2330   
2331   mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2332                                     NULL, &load_error);
2333   
2334   if (load_error != NULL)
2335     goto error;
2336   
2337   for (i = 0; mime_types[i] != NULL; i++)
2338     {
2339       gchar **desktop_file_ids;
2340       char *unaliased_type;
2341       desktop_file_ids = g_key_file_get_string_list (key_file,
2342                                                      MIME_CACHE_GROUP,
2343                                                      mime_types[i],
2344                                                      NULL,
2345                                                      NULL);
2346       
2347       if (desktop_file_ids == NULL)
2348         continue;
2349
2350       unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2351       mime_info_cache_dir_add_desktop_entries (dir,
2352                                                unaliased_type,
2353                                                desktop_file_ids);
2354       g_free (unaliased_type);
2355     
2356       g_strfreev (desktop_file_ids);
2357     }
2358   
2359   g_strfreev (mime_types);
2360   g_key_file_free (key_file);
2361   
2362   return;
2363  error:
2364   g_free (filename);
2365   g_key_file_free (key_file);
2366   
2367   if (mime_types != NULL)
2368     g_strfreev (mime_types);
2369   
2370   if (load_error)
2371     g_error_free (load_error);
2372 }
2373
2374 static void
2375 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2376 {
2377   GKeyFile *key_file;
2378   GError *load_error;
2379   gchar *filename, **mime_types;
2380   char *unaliased_type;
2381   char **desktop_file_ids;
2382   int i;
2383   struct stat buf;
2384
2385   load_error = NULL;
2386   mime_types = NULL;
2387
2388   if (dir->defaults_list_map != NULL &&
2389       !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2390                                         &dir->defaults_list_timestamp))
2391     return;
2392   
2393   if (dir->defaults_list_map != NULL)
2394     g_hash_table_destroy (dir->defaults_list_map);
2395   dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2396                                                   g_free, (GDestroyNotify)g_strfreev);
2397   
2398
2399   key_file = g_key_file_new ();
2400   
2401   filename = g_build_filename (dir->path, "defaults.list", NULL);
2402   if (g_stat (filename, &buf) < 0)
2403     goto error;
2404
2405   if (dir->defaults_list_timestamp > 0) 
2406     mime_info_cache->should_ping_mime_monitor = TRUE;
2407
2408   dir->defaults_list_timestamp = buf.st_mtime;
2409
2410   g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2411   g_free (filename);
2412   filename = NULL;
2413
2414   if (load_error != NULL)
2415     goto error;
2416
2417   mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2418                                     NULL, NULL);
2419   if (mime_types != NULL)
2420     {
2421       for (i = 0; mime_types[i] != NULL; i++)
2422         {
2423           desktop_file_ids = g_key_file_get_string_list (key_file,
2424                                                          DEFAULT_APPLICATIONS_GROUP,
2425                                                          mime_types[i],
2426                                                          NULL,
2427                                                          NULL);
2428           if (desktop_file_ids == NULL)
2429             continue;
2430           
2431           unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2432           g_hash_table_replace (dir->defaults_list_map,
2433                                 unaliased_type,
2434                                 desktop_file_ids);
2435         }
2436       
2437       g_strfreev (mime_types);
2438     }
2439
2440   g_key_file_free (key_file);
2441   return;
2442   
2443  error:
2444   g_free (filename);
2445   g_key_file_free (key_file);
2446   
2447   if (mime_types != NULL)
2448     g_strfreev (mime_types);
2449   
2450   if (load_error)
2451     g_error_free (load_error);
2452 }
2453
2454 static void
2455 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2456 {
2457   GKeyFile *key_file;
2458   GError *load_error;
2459   gchar *filename, **mime_types;
2460   char *unaliased_type;
2461   char **desktop_file_ids;
2462   char *desktop_id;
2463   int i;
2464   struct stat buf;
2465
2466   load_error = NULL;
2467   mime_types = NULL;
2468
2469   if (dir->mimeapps_list_added_map != NULL &&
2470       !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2471                                         &dir->mimeapps_list_timestamp))
2472     return;
2473   
2474   if (dir->mimeapps_list_added_map != NULL)
2475     g_hash_table_destroy (dir->mimeapps_list_added_map);
2476   dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2477                                                         g_free, (GDestroyNotify)g_strfreev);
2478   
2479   if (dir->mimeapps_list_removed_map != NULL)
2480     g_hash_table_destroy (dir->mimeapps_list_removed_map);
2481   dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2482                                                           g_free, (GDestroyNotify)g_strfreev);
2483
2484   if (dir->mimeapps_list_defaults_map != NULL)
2485     g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2486   dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2487                                                            g_free, g_free);
2488
2489   key_file = g_key_file_new ();
2490   
2491   filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2492   if (g_stat (filename, &buf) < 0)
2493     goto error;
2494
2495   if (dir->mimeapps_list_timestamp > 0) 
2496     mime_info_cache->should_ping_mime_monitor = TRUE;
2497
2498   dir->mimeapps_list_timestamp = buf.st_mtime;
2499
2500   g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2501   g_free (filename);
2502   filename = NULL;
2503
2504   if (load_error != NULL)
2505     goto error;
2506
2507   mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2508                                     NULL, NULL);
2509   if (mime_types != NULL)
2510     {
2511       for (i = 0; mime_types[i] != NULL; i++)
2512         {
2513           desktop_file_ids = g_key_file_get_string_list (key_file,
2514                                                          ADDED_ASSOCIATIONS_GROUP,
2515                                                          mime_types[i],
2516                                                          NULL,
2517                                                          NULL);
2518           if (desktop_file_ids == NULL)
2519             continue;
2520           
2521           unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2522           g_hash_table_replace (dir->mimeapps_list_added_map,
2523                                 unaliased_type,
2524                                 desktop_file_ids);
2525         }
2526       
2527       g_strfreev (mime_types);
2528     }
2529
2530   mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2531                                     NULL, NULL);
2532   if (mime_types != NULL)
2533     {
2534       for (i = 0; mime_types[i] != NULL; i++)
2535         {
2536           desktop_file_ids = g_key_file_get_string_list (key_file,
2537                                                          REMOVED_ASSOCIATIONS_GROUP,
2538                                                          mime_types[i],
2539                                                          NULL,
2540                                                          NULL);
2541           if (desktop_file_ids == NULL)
2542             continue;
2543           
2544           unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2545           g_hash_table_replace (dir->mimeapps_list_removed_map,
2546                                 unaliased_type,
2547                                 desktop_file_ids);
2548         }
2549       
2550       g_strfreev (mime_types);
2551     }
2552
2553   mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2554                                     NULL, NULL);
2555   if (mime_types != NULL)
2556     {
2557       for (i = 0; mime_types[i] != NULL; i++)
2558         {
2559           desktop_id = g_key_file_get_string (key_file,
2560                                               DEFAULT_APPLICATIONS_GROUP,
2561                                               mime_types[i],
2562                                               NULL);
2563           if (desktop_id == NULL)
2564             continue;
2565
2566           unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2567           g_hash_table_replace (dir->mimeapps_list_defaults_map,
2568                                 unaliased_type,
2569                                 desktop_id);
2570         }
2571
2572       g_strfreev (mime_types);
2573     }
2574
2575   g_key_file_free (key_file);
2576   return;
2577   
2578  error:
2579   g_free (filename);
2580   g_key_file_free (key_file);
2581   
2582   if (mime_types != NULL)
2583     g_strfreev (mime_types);
2584   
2585   if (load_error)
2586     g_error_free (load_error);
2587 }
2588
2589 static MimeInfoCacheDir *
2590 mime_info_cache_dir_new (const char *path)
2591 {
2592   MimeInfoCacheDir *dir;
2593
2594   dir = g_new0 (MimeInfoCacheDir, 1);
2595   dir->path = g_strdup (path);
2596   
2597   return dir;
2598 }
2599
2600 static void
2601 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2602 {
2603   if (dir == NULL)
2604     return;
2605   
2606   if (dir->mime_info_cache_map != NULL)
2607     {
2608       destroy_info_cache_map (dir->mime_info_cache_map);
2609       dir->mime_info_cache_map = NULL;
2610       
2611   }
2612   
2613   if (dir->defaults_list_map != NULL)
2614     {
2615       g_hash_table_destroy (dir->defaults_list_map);
2616       dir->defaults_list_map = NULL;
2617     }
2618   
2619   if (dir->mimeapps_list_added_map != NULL)
2620     {
2621       g_hash_table_destroy (dir->mimeapps_list_added_map);
2622       dir->mimeapps_list_added_map = NULL;
2623     }
2624   
2625   if (dir->mimeapps_list_removed_map != NULL)
2626     {
2627       g_hash_table_destroy (dir->mimeapps_list_removed_map);
2628       dir->mimeapps_list_removed_map = NULL;
2629     }
2630
2631   if (dir->mimeapps_list_defaults_map != NULL)
2632     {
2633       g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2634       dir->mimeapps_list_defaults_map = NULL;
2635     }
2636
2637   g_free (dir);
2638 }
2639
2640 static void
2641 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir  *dir,
2642                                          const char        *mime_type,
2643                                          char             **new_desktop_file_ids)
2644 {
2645   GList *desktop_file_ids;
2646   int i;
2647   
2648   desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2649                                           mime_type);
2650   
2651   for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2652     {
2653       if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
2654         desktop_file_ids = g_list_append (desktop_file_ids,
2655                                           g_strdup (new_desktop_file_ids[i]));
2656     }
2657   
2658   g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2659 }
2660
2661 static void
2662 mime_info_cache_init_dir_lists (void)
2663 {
2664   const char * const *dirs;
2665   int i;
2666   
2667   mime_info_cache = mime_info_cache_new ();
2668   
2669   dirs = get_applications_search_path ();
2670   
2671   for (i = 0; dirs[i] != NULL; i++)
2672     {
2673       MimeInfoCacheDir *dir;
2674       
2675       dir = mime_info_cache_dir_new (dirs[i]);
2676       
2677       if (dir != NULL)
2678         {
2679           mime_info_cache_dir_init (dir);
2680           mime_info_cache_dir_init_defaults_list (dir);
2681           mime_info_cache_dir_init_mimeapps_list (dir);
2682           
2683           mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2684         }
2685     }
2686 }
2687
2688 static void
2689 mime_info_cache_update_dir_lists (void)
2690 {
2691   GList *tmp;
2692   
2693   tmp = mime_info_cache->dirs;
2694   
2695   while (tmp != NULL)
2696     {
2697       MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2698
2699       /* No need to do this if we had file monitors... */
2700       mime_info_cache_blow_global_cache ();
2701       mime_info_cache_dir_init (dir);
2702       mime_info_cache_dir_init_defaults_list (dir);
2703       mime_info_cache_dir_init_mimeapps_list (dir);
2704       
2705       tmp = tmp->next;
2706     }
2707 }
2708
2709 static void
2710 mime_info_cache_init (void)
2711 {
2712   G_LOCK (mime_info_cache);
2713   if (mime_info_cache == NULL)
2714     mime_info_cache_init_dir_lists ();
2715   else
2716     {
2717       time_t now;
2718       
2719       time (&now);
2720       if (now >= mime_info_cache->last_stat_time + 10)
2721         {
2722           mime_info_cache_update_dir_lists ();
2723           mime_info_cache->last_stat_time = now;
2724         }
2725     }
2726   
2727   if (mime_info_cache->should_ping_mime_monitor)
2728     {
2729       /* g_idle_add (emit_mime_changed, NULL); */
2730       mime_info_cache->should_ping_mime_monitor = FALSE;
2731     }
2732   
2733   G_UNLOCK (mime_info_cache);
2734 }
2735
2736 static MimeInfoCache *
2737 mime_info_cache_new (void)
2738 {
2739   MimeInfoCache *cache;
2740   
2741   cache = g_new0 (MimeInfoCache, 1);
2742   
2743   cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2744                                                         (GDestroyNotify) g_free,
2745                                                         (GDestroyNotify) g_free);
2746   return cache;
2747 }
2748
2749 static void
2750 mime_info_cache_free (MimeInfoCache *cache)
2751 {
2752   if (cache == NULL)
2753     return;
2754   
2755   g_list_foreach (cache->dirs,
2756                   (GFunc) mime_info_cache_dir_free,
2757                   NULL);
2758   g_list_free (cache->dirs);
2759   g_hash_table_destroy (cache->global_defaults_cache);
2760   g_free (cache);
2761 }
2762
2763 /**
2764  * mime_info_cache_reload:
2765  * @dir: directory path which needs reloading.
2766  * 
2767  * Reload the mime information for the @dir.
2768  */
2769 static void
2770 mime_info_cache_reload (const char *dir)
2771 {
2772   /* FIXME: just reload the dir that needs reloading,
2773    * don't blow the whole cache
2774    */
2775   if (mime_info_cache != NULL)
2776     {
2777       G_LOCK (mime_info_cache);
2778       mime_info_cache_free (mime_info_cache);
2779       mime_info_cache = NULL;
2780       G_UNLOCK (mime_info_cache);
2781     }
2782 }
2783
2784 static GList *
2785 append_desktop_entry (GList      *list, 
2786                       const char *desktop_entry,
2787                       GList      *removed_entries)
2788 {
2789   /* Add if not already in list, and valid */
2790   if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2791       !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2792     list = g_list_prepend (list, g_strdup (desktop_entry));
2793   
2794   return list;
2795 }
2796
2797 /**
2798  * get_all_desktop_entries_for_mime_type:
2799  * @mime_type: a mime type.
2800  * @except: NULL or a strv list
2801  *
2802  * Returns all the desktop ids for @mime_type. The desktop files
2803  * are listed in an order so that default applications are listed before
2804  * non-default ones, and handlers for inherited mimetypes are listed
2805  * after the base ones.
2806  *
2807  * Optionally doesn't list the desktop ids given in the @except 
2808  *
2809  * Return value: a #GList containing the desktop ids which claim
2810  *    to handle @mime_type.
2811  */
2812 static GList *
2813 get_all_desktop_entries_for_mime_type (const char  *base_mime_type,
2814                                        const char **except,
2815                                        gboolean     include_fallback,
2816                                        char       **explicit_default)
2817 {
2818   GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2819   MimeInfoCacheDir *dir;
2820   char *mime_type, *default_entry = NULL;
2821   const char *entry;
2822   char **mime_types;
2823   char **default_entries;
2824   char **removed_associations;
2825   int i, j, k;
2826   GPtrArray *array;
2827   char **anc;
2828   
2829   mime_info_cache_init ();
2830
2831   if (include_fallback)
2832     {
2833       /* collect all ancestors */
2834       mime_types = _g_unix_content_type_get_parents (base_mime_type);
2835       array = g_ptr_array_new ();
2836       for (i = 0; mime_types[i]; i++)
2837         g_ptr_array_add (array, mime_types[i]);
2838       g_free (mime_types);
2839       for (i = 0; i < array->len; i++)
2840         {
2841           anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
2842           for (j = 0; anc[j]; j++)
2843             {
2844               for (k = 0; k < array->len; k++)
2845                 {
2846                   if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
2847                     break;
2848                 }
2849               if (k == array->len) /* not found */
2850                 g_ptr_array_add (array, g_strdup (anc[j]));
2851             }
2852           g_strfreev (anc);
2853         }
2854       g_ptr_array_add (array, NULL);
2855       mime_types = (char **)g_ptr_array_free (array, FALSE);
2856     }
2857   else
2858     {
2859       mime_types = g_malloc0 (2 * sizeof (gchar *));
2860       mime_types[0] = g_strdup (base_mime_type);
2861       mime_types[1] = NULL;
2862     }
2863
2864   G_LOCK (mime_info_cache);
2865   
2866   removed_entries = NULL;
2867   desktop_entries = NULL;
2868
2869   for (i = 0; except != NULL && except[i] != NULL; i++)
2870     removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
2871   
2872   for (i = 0; mime_types[i] != NULL; i++)
2873     {
2874       mime_type = mime_types[i];
2875
2876       /* Go through all apps listed in user and system dirs */
2877       for (dir_list = mime_info_cache->dirs;
2878            dir_list != NULL;
2879            dir_list = dir_list->next)
2880         {
2881           dir = dir_list->data;
2882
2883           /* Pick the explicit default application */
2884           entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
2885
2886           if (entry != NULL)
2887             {
2888               /* Save the default entry if it's the first one we encounter */
2889               if (default_entry == NULL)
2890                 default_entry = g_strdup (entry);
2891             }
2892
2893           /* Then added associations from mimeapps.list */
2894           default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2895           for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2896             desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2897
2898           /* Then removed associations from mimeapps.list */
2899           removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2900           for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2901             removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2902
2903           /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2904           default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2905           for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2906             desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2907         }
2908
2909       /* Go through all entries that support the mimetype */
2910       for (dir_list = mime_info_cache->dirs;
2911            dir_list != NULL;
2912            dir_list = dir_list->next) 
2913         {
2914           dir = dir_list->data;
2915         
2916           list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2917           for (tmp = list; tmp != NULL; tmp = tmp->next)
2918             desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2919         }
2920     }
2921   
2922   G_UNLOCK (mime_info_cache);
2923
2924   g_strfreev (mime_types);
2925
2926   if (explicit_default != NULL)
2927     *explicit_default = default_entry;
2928   else
2929     g_free (default_entry);
2930
2931   g_list_foreach (removed_entries, (GFunc)g_free, NULL);
2932   g_list_free (removed_entries);
2933
2934   desktop_entries = g_list_reverse (desktop_entries);
2935   
2936   return desktop_entries;
2937 }
2938
2939 /* GDesktopAppInfoLookup interface: */
2940
2941 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
2942 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
2943
2944 static void
2945 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
2946 {
2947 }
2948
2949 /**
2950  * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2951  * @lookup: a #GDesktopAppInfoLookup
2952  * @uri_scheme: a string containing a URI scheme.
2953  *
2954  * Gets the default application for launching applications 
2955  * using this URI scheme for a particular GDesktopAppInfoLookup
2956  * implementation.
2957  *
2958  * The GDesktopAppInfoLookup interface and this function is used
2959  * to implement g_app_info_get_default_for_uri_scheme() backends
2960  * in a GIO module. There is no reason for applications to use it
2961  * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2962  * 
2963  * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2964  *
2965  * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
2966  */
2967 GAppInfo *
2968 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2969                                                       const char            *uri_scheme)
2970 {
2971   GDesktopAppInfoLookupIface *iface;
2972   
2973   g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2974
2975   iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2976
2977   return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2978 }