appinfo: add g_app_info_set_as_last_used_for_type()
[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;
1188   char **old_list, **list;
1189   gsize length, data_size;
1190   char *data;
1191   int i, j, k;
1192   char **content_types;
1193
1194   /* Don't add both at start and end */
1195   g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1196               (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1197
1198   dirname = ensure_dir (APP_DIR, error);
1199   if (!dirname)
1200     return FALSE;
1201
1202   filename = g_build_filename (dirname, "mimeapps.list", NULL);
1203   g_free (dirname);
1204
1205   key_file = g_key_file_new ();
1206   load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1207   if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1208     {
1209       g_key_file_free (key_file);
1210       key_file = g_key_file_new ();
1211     }
1212
1213   if (content_type)
1214     {
1215       content_types = g_new (char *, 2);
1216       content_types[0] = g_strdup (content_type);
1217       content_types[1] = NULL;
1218     }
1219   else
1220     {
1221       content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1222     }
1223
1224   for (k = 0; content_types && content_types[k]; k++)
1225     {
1226       /* set as default, if requested so */
1227       string = g_key_file_get_string (key_file,
1228                                       DEFAULT_APPLICATIONS_GROUP,
1229                                       content_types[k],
1230                                       NULL);
1231
1232       if (g_strcmp0 (string, desktop_id) != 0 &&
1233           (flags & UPDATE_MIME_SET_DEFAULT))
1234         {
1235           g_free (string);
1236           string = g_strdup (desktop_id);
1237
1238           /* add in the non-default list too, if it's not already there */
1239           flags |= UPDATE_MIME_SET_NON_DEFAULT;
1240         }
1241
1242       if (string == NULL || desktop_id == NULL)
1243         g_key_file_remove_key (key_file,
1244                                DEFAULT_APPLICATIONS_GROUP,
1245                                content_types[k],
1246                                NULL);
1247       else
1248         g_key_file_set_string (key_file,
1249                                DEFAULT_APPLICATIONS_GROUP,
1250                                content_types[k],
1251                                string);
1252
1253       g_free (string);
1254     }
1255
1256   if (content_type)
1257     {
1258       /* reuse the list from above */
1259     }
1260   else
1261     {
1262       g_strfreev (content_types);
1263       content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1264     }
1265   
1266   for (k = 0; content_types && content_types[k]; k++)
1267     { 
1268       /* Add to the right place in the list */
1269   
1270       length = 0;
1271       old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1272                                              content_types[k], &length, NULL);
1273
1274       list = g_new (char *, 1 + length + 1);
1275
1276       i = 0;
1277
1278       /* if we're adding a last-used hint, just put the application in front of the list */
1279       if (flags & UPDATE_MIME_SET_LAST_USED)
1280         {
1281           /* avoid adding this again as non-default later */
1282           if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1283             flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1284
1285           list[i++] = g_strdup (desktop_id);
1286         }
1287
1288       if (old_list)
1289         {
1290           for (j = 0; old_list[j] != NULL; j++)
1291             {
1292               if (g_strcmp0 (old_list[j], desktop_id) != 0)
1293                 {
1294                   /* rewrite other entries if they're different from the new one */
1295                   list[i++] = g_strdup (old_list[j]);
1296                 }
1297               else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1298                 {
1299                   /* we encountered an old entry which is equal to the one we're adding as non-default,
1300                    * don't change its position in the list.
1301                    */
1302                   flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1303                   list[i++] = g_strdup (old_list[j]);
1304                 }
1305             }
1306         }
1307
1308       /* add it at the end of the list */
1309       if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1310         list[i++] = g_strdup (desktop_id);
1311
1312       list[i] = NULL;
1313   
1314       g_strfreev (old_list);
1315
1316       if (list[0] == NULL || desktop_id == NULL)
1317         g_key_file_remove_key (key_file,
1318                                ADDED_ASSOCIATIONS_GROUP,
1319                                content_types[k],
1320                                NULL);
1321       else
1322         g_key_file_set_string_list (key_file,
1323                                     ADDED_ASSOCIATIONS_GROUP,
1324                                     content_types[k],
1325                                     (const char * const *)list, i);
1326    
1327       g_strfreev (list);
1328     }
1329   
1330   if (content_type)
1331     {
1332       /* reuse the list from above */
1333     }
1334   else
1335     {
1336       g_strfreev (content_types);
1337       content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1338     }
1339
1340   for (k = 0; content_types && content_types[k]; k++) 
1341     {
1342       /* Remove from removed associations group (unless remove) */
1343   
1344       length = 0;
1345       old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1346                                              content_types[k], &length, NULL);
1347
1348       list = g_new (char *, 1 + length + 1);
1349
1350       i = 0;
1351       if (flags & UPDATE_MIME_REMOVE)
1352         list[i++] = g_strdup (desktop_id);
1353       if (old_list)
1354         {
1355           for (j = 0; old_list[j] != NULL; j++)
1356             {
1357               if (g_strcmp0 (old_list[j], desktop_id) != 0)
1358                 list[i++] = g_strdup (old_list[j]);
1359             }
1360         }
1361       list[i] = NULL;
1362   
1363       g_strfreev (old_list);
1364
1365       if (list[0] == NULL || desktop_id == NULL)
1366         g_key_file_remove_key (key_file,
1367                                REMOVED_ASSOCIATIONS_GROUP,
1368                                content_types[k],
1369                                NULL);
1370       else
1371         g_key_file_set_string_list (key_file,
1372                                     REMOVED_ASSOCIATIONS_GROUP,
1373                                     content_types[k],
1374                                     (const char * const *)list, i);
1375
1376       g_strfreev (list);
1377     }
1378
1379   g_strfreev (content_types);  
1380
1381   data = g_key_file_to_data (key_file, &data_size, error);
1382   g_key_file_free (key_file);
1383   
1384   res = g_file_set_contents (filename, data, data_size, error);
1385
1386   mime_info_cache_reload (NULL);
1387                           
1388   g_free (filename);
1389   g_free (data);
1390   
1391   return res;
1392 }
1393
1394 static gboolean
1395 g_desktop_app_info_set_as_last_used_for_type (GAppInfo    *appinfo,
1396                                               const char  *content_type,
1397                                               GError     **error)
1398 {
1399   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1400
1401   if (!g_desktop_app_info_ensure_saved (info, error))
1402     return FALSE;
1403
1404   /* both add support for the content type and set as last used */
1405   return update_mimeapps_list (info->desktop_id, content_type,
1406                                UPDATE_MIME_SET_NON_DEFAULT |
1407                                UPDATE_MIME_SET_LAST_USED,
1408                                error);
1409 }
1410
1411 static gboolean
1412 g_desktop_app_info_set_as_default_for_type (GAppInfo    *appinfo,
1413                                             const char  *content_type,
1414                                             GError     **error)
1415 {
1416   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1417
1418   if (!g_desktop_app_info_ensure_saved (info, error))
1419     return FALSE;  
1420   
1421   return update_mimeapps_list (info->desktop_id, content_type,
1422                                UPDATE_MIME_SET_DEFAULT,
1423                                error);
1424 }
1425
1426 static void
1427 update_program_done (GPid     pid,
1428                      gint     status,
1429                      gpointer data)
1430 {
1431   /* Did the application exit correctly */
1432   if (WIFEXITED (status) &&
1433       WEXITSTATUS (status) == 0)
1434     {
1435       /* Here we could clean out any caches in use */
1436     }
1437 }
1438
1439 static void
1440 run_update_command (char *command,
1441                     char *subdir)
1442 {
1443         char *argv[3] = {
1444                 NULL,
1445                 NULL,
1446                 NULL,
1447         };
1448         GPid pid = 0;
1449         GError *error = NULL;
1450
1451         argv[0] = command;
1452         argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1453
1454         if (g_spawn_async ("/", argv,
1455                            NULL,       /* envp */
1456                            G_SPAWN_SEARCH_PATH |
1457                            G_SPAWN_STDOUT_TO_DEV_NULL |
1458                            G_SPAWN_STDERR_TO_DEV_NULL |
1459                            G_SPAWN_DO_NOT_REAP_CHILD,
1460                            NULL, NULL, /* No setup function */
1461                            &pid,
1462                            &error)) 
1463           g_child_watch_add (pid, update_program_done, NULL);
1464         else
1465           {
1466             /* If we get an error at this point, it's quite likely the user doesn't
1467              * have an installed copy of either 'update-mime-database' or
1468              * 'update-desktop-database'.  I don't think we want to popup an error
1469              * dialog at this point, so we just do a g_warning to give the user a
1470              * chance of debugging it.
1471              */
1472             g_warning ("%s", error->message);
1473           }
1474         
1475         g_free (argv[1]);
1476 }
1477
1478 static gboolean
1479 g_desktop_app_info_set_as_default_for_extension (GAppInfo    *appinfo,
1480                                                  const char  *extension,
1481                                                  GError     **error)
1482 {
1483   char *filename, *basename, *mimetype;
1484   char *dirname;
1485   gboolean res;
1486
1487   if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1488     return FALSE;  
1489   
1490   dirname = ensure_dir (MIMETYPE_DIR, error);
1491   if (!dirname)
1492     return FALSE;
1493   
1494   basename = g_strdup_printf ("user-extension-%s.xml", extension);
1495   filename = g_build_filename (dirname, basename, NULL);
1496   g_free (basename);
1497   g_free (dirname);
1498
1499   mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1500   
1501   if (!g_file_test (filename, G_FILE_TEST_EXISTS)) 
1502     {
1503       char *contents;
1504
1505       contents =
1506         g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1507                          "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1508                          " <mime-type type=\"%s\">\n"
1509                          "  <comment>%s document</comment>\n"
1510                          "  <glob pattern=\"*.%s\"/>\n"
1511                          " </mime-type>\n"
1512                          "</mime-info>\n", mimetype, extension, extension);
1513
1514       g_file_set_contents (filename, contents, -1, NULL);
1515       g_free (contents);
1516
1517       run_update_command ("update-mime-database", "mime");
1518     }
1519   g_free (filename);
1520   
1521   res = g_desktop_app_info_set_as_default_for_type (appinfo,
1522                                                     mimetype,
1523                                                     error);
1524
1525   g_free (mimetype);
1526   
1527   return res;
1528 }
1529
1530 static gboolean
1531 g_desktop_app_info_add_supports_type (GAppInfo    *appinfo,
1532                                       const char  *content_type,
1533                                       GError     **error)
1534 {
1535   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1536
1537   if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1538     return FALSE;  
1539   
1540   return update_mimeapps_list (info->desktop_id, content_type,
1541                                UPDATE_MIME_SET_NON_DEFAULT,
1542                                error);
1543 }
1544
1545 static gboolean
1546 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1547 {
1548   return TRUE;
1549 }
1550
1551 static gboolean
1552 g_desktop_app_info_remove_supports_type (GAppInfo    *appinfo,
1553                                          const char  *content_type,
1554                                          GError     **error)
1555 {
1556   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1557
1558   if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1559     return FALSE;
1560   
1561   return update_mimeapps_list (info->desktop_id, content_type,
1562                                UPDATE_MIME_REMOVE,
1563                                error);
1564 }
1565
1566 static gboolean
1567 g_desktop_app_info_ensure_saved (GDesktopAppInfo  *info,
1568                                  GError          **error)
1569 {
1570   GKeyFile *key_file;
1571   char *dirname;
1572   char *filename;
1573   char *data, *desktop_id;
1574   gsize data_size;
1575   int fd;
1576   gboolean res;
1577   
1578   if (info->filename != NULL)
1579     return TRUE;
1580
1581   /* This is only used for object created with
1582    * g_app_info_create_from_commandline. All other
1583    * object should have a filename
1584    */
1585   
1586   dirname = ensure_dir (APP_DIR, error);
1587   if (!dirname)
1588     return FALSE;
1589   
1590   key_file = g_key_file_new ();
1591
1592   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1593                          "Encoding", "UTF-8");
1594   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1595                          G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1596   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1597                          G_KEY_FILE_DESKTOP_KEY_TYPE,
1598                          G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1599   if (info->terminal) 
1600     g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1601                             G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1602
1603   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1604                          G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1605
1606   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1607                          G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1608
1609   if (info->fullname != NULL)
1610     g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1611                            FULL_NAME_KEY, info->fullname);
1612
1613   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1614                          G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1615   
1616   g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1617                           G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1618
1619   data = g_key_file_to_data (key_file, &data_size, NULL);
1620   g_key_file_free (key_file);
1621
1622   desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1623   filename = g_build_filename (dirname, desktop_id, NULL);
1624   g_free (desktop_id);
1625   g_free (dirname);
1626   
1627   fd = g_mkstemp (filename);
1628   if (fd == -1)
1629     {
1630       char *display_name;
1631
1632       display_name = g_filename_display_name (filename);
1633       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1634                    _("Can't create user desktop file %s"), display_name);
1635       g_free (display_name);
1636       g_free (filename);
1637       g_free (data);
1638       return FALSE;
1639     }
1640
1641   desktop_id = g_path_get_basename (filename);
1642
1643   close (fd);
1644   
1645   res = g_file_set_contents (filename, data, data_size, error);
1646   if (!res)
1647     {
1648       g_free (desktop_id);
1649       g_free (filename);
1650       return FALSE;
1651     }
1652
1653   info->filename = filename;
1654   info->desktop_id = desktop_id;
1655   
1656   run_update_command ("update-desktop-database", "applications");
1657   
1658   return TRUE;
1659 }
1660
1661 static gboolean
1662 g_desktop_app_info_can_delete (GAppInfo *appinfo)
1663 {
1664   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1665
1666   if (info->filename)
1667     {
1668       if (strstr (info->filename, "/userapp-"))
1669         return g_access (info->filename, W_OK) == 0;
1670     }
1671
1672   return FALSE;
1673 }
1674
1675 static gboolean
1676 g_desktop_app_info_delete (GAppInfo *appinfo)
1677 {
1678   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1679   
1680   if (info->filename)
1681     { 
1682       if (g_remove (info->filename) == 0)
1683         {
1684           update_mimeapps_list (info->desktop_id, NULL,
1685                                 UPDATE_MIME_NONE,
1686                                 NULL);
1687
1688           g_free (info->filename);
1689           info->filename = NULL;
1690           g_free (info->desktop_id);
1691           info->desktop_id = NULL;
1692
1693           return TRUE;
1694         }
1695     }
1696
1697   return FALSE;
1698 }
1699
1700 /**
1701  * g_app_info_create_from_commandline:
1702  * @commandline: the commandline to use
1703  * @application_name: (allow-none): the application name, or %NULL to use @commandline
1704  * @flags: flags that can specify details of the created #GAppInfo
1705  * @error: a #GError location to store the error occuring, %NULL to ignore.
1706  *
1707  * Creates a new #GAppInfo from the given information.
1708  *
1709  * Returns: (transfer full): new #GAppInfo for given command.
1710  **/
1711 GAppInfo *
1712 g_app_info_create_from_commandline (const char           *commandline,
1713                                     const char           *application_name,
1714                                     GAppInfoCreateFlags   flags,
1715                                     GError              **error)
1716 {
1717   char **split;
1718   char *basename;
1719   GDesktopAppInfo *info;
1720
1721   g_return_val_if_fail (commandline, NULL);
1722
1723   info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1724
1725   info->filename = NULL;
1726   info->desktop_id = NULL;
1727   
1728   info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1729   info->startup_notify = flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION;
1730   info->hidden = FALSE;
1731   if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1732     info->exec = g_strconcat (commandline, " %u", NULL);
1733   else
1734     info->exec = g_strconcat (commandline, " %f", NULL);
1735   info->nodisplay = TRUE;
1736   info->binary = binary_from_exec (info->exec);
1737   
1738   if (application_name)
1739     info->name = g_strdup (application_name);
1740   else
1741     {
1742       /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1743       split = g_strsplit (commandline, " ", 2);
1744       basename = split[0] ? g_path_get_basename (split[0]) : NULL;
1745       g_strfreev (split);
1746       info->name = basename;
1747       if (info->name == NULL)
1748         info->name = g_strdup ("custom");
1749     }
1750   info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1751   
1752   return G_APP_INFO (info);
1753 }
1754
1755 static void
1756 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1757 {
1758   iface->dup = g_desktop_app_info_dup;
1759   iface->equal = g_desktop_app_info_equal;
1760   iface->get_id = g_desktop_app_info_get_id;
1761   iface->get_name = g_desktop_app_info_get_name;
1762   iface->get_description = g_desktop_app_info_get_description;
1763   iface->get_executable = g_desktop_app_info_get_executable;
1764   iface->get_icon = g_desktop_app_info_get_icon;
1765   iface->launch = g_desktop_app_info_launch;
1766   iface->supports_uris = g_desktop_app_info_supports_uris;
1767   iface->supports_files = g_desktop_app_info_supports_files;
1768   iface->launch_uris = g_desktop_app_info_launch_uris;
1769   iface->should_show = g_desktop_app_info_should_show;
1770   iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1771   iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1772   iface->add_supports_type = g_desktop_app_info_add_supports_type;
1773   iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1774   iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1775   iface->can_delete = g_desktop_app_info_can_delete;
1776   iface->do_delete = g_desktop_app_info_delete;
1777   iface->get_commandline = g_desktop_app_info_get_commandline;
1778   iface->get_display_name = g_desktop_app_info_get_display_name;
1779   iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
1780 }
1781
1782 static gboolean
1783 app_info_in_list (GAppInfo *info, 
1784                   GList    *list)
1785 {
1786   while (list != NULL)
1787     {
1788       if (g_app_info_equal (info, list->data))
1789         return TRUE;
1790       list = list->next;
1791     }
1792   return FALSE;
1793 }
1794
1795 /**
1796  * g_app_info_get_recommended_for_type:
1797  * @content_type: the content type to find a #GAppInfo for
1798  * 
1799  * Gets a list of recommended #GAppInfos for a given content type, i.e.
1800  * those applications which claim to support the given content type exactly,
1801  * and not by MIME type subclassing.
1802  * Note that the first application of the list is the last used one, i.e.
1803  * the last one for which #g_app_info_set_as_last_used_for_type has been
1804  * called.
1805  *
1806  * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
1807  *     for given @content_type or %NULL on error.
1808  *
1809  * Since: 2.28
1810  **/
1811 GList *
1812 g_app_info_get_recommended_for_type (const gchar *content_type)
1813 {
1814   GList *desktop_entries, *l;
1815   GList *infos;
1816   GDesktopAppInfo *info;
1817
1818   g_return_val_if_fail (content_type != NULL, NULL);
1819
1820   desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
1821
1822   infos = NULL;
1823   for (l = desktop_entries; l != NULL; l = l->next)
1824     {
1825       char *desktop_entry = l->data;
1826
1827       info = g_desktop_app_info_new (desktop_entry);
1828       if (info)
1829         {
1830           if (app_info_in_list (G_APP_INFO (info), infos))
1831             g_object_unref (info);
1832           else
1833             infos = g_list_prepend (infos, info);
1834         }
1835       g_free (desktop_entry);
1836     }
1837
1838   g_list_free (desktop_entries);
1839
1840   return g_list_reverse (infos);
1841 }
1842
1843 /**
1844  * g_app_info_get_fallback_for_type:
1845  * @content_type: the content type to find a #GAppInfo for
1846  * 
1847  * Gets a list of fallback #GAppInfos for a given content type, i.e.
1848  * those applications which claim to support the given content type
1849  * by MIME type subclassing and not directly.
1850  *
1851  * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
1852  *     for given @content_type or %NULL on error.
1853  *
1854  * Since: 2.28
1855  **/
1856 GList *
1857 g_app_info_get_fallback_for_type (const gchar *content_type)
1858 {
1859   GList *desktop_entries, *l;
1860   GList *infos, *recommended_infos;
1861   GDesktopAppInfo *info;
1862
1863   g_return_val_if_fail (content_type != NULL, NULL);
1864
1865   desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
1866   recommended_infos = g_app_info_get_recommended_for_type (content_type);
1867
1868   infos = NULL;
1869   for (l = desktop_entries; l != NULL; l = l->next)
1870     {
1871       char *desktop_entry = l->data;
1872
1873       info = g_desktop_app_info_new (desktop_entry);
1874       if (info)
1875         {
1876           if (app_info_in_list (G_APP_INFO (info), infos) ||
1877               app_info_in_list (G_APP_INFO (info), recommended_infos))
1878             g_object_unref (info);
1879           else
1880             infos = g_list_prepend (infos, info);
1881         }
1882       g_free (desktop_entry);
1883     }
1884
1885   g_list_free (desktop_entries);
1886   g_list_free_full (recommended_infos, g_object_unref);
1887
1888   return g_list_reverse (infos);
1889 }
1890
1891 /**
1892  * g_app_info_get_all_for_type:
1893  * @content_type: the content type to find a #GAppInfo for
1894  * 
1895  * Gets a list of all #GAppInfos for a given content type.
1896  *
1897  * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
1898  *     for given @content_type or %NULL on error.
1899  **/
1900 GList *
1901 g_app_info_get_all_for_type (const char *content_type)
1902 {
1903   GList *desktop_entries, *l;
1904   GList *infos;
1905   char *user_default = NULL;
1906   GDesktopAppInfo *info;
1907
1908   g_return_val_if_fail (content_type != NULL, NULL);
1909   
1910   desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
1911   infos = NULL;
1912
1913   /* put the user default in front of the list, for compatibility */
1914   if (user_default != NULL)
1915     {
1916       info = g_desktop_app_info_new (user_default);
1917
1918       if (info != NULL)
1919         infos = g_list_prepend (infos, info);
1920     }
1921
1922   g_free (user_default);
1923
1924   for (l = desktop_entries; l != NULL; l = l->next)
1925     {
1926       char *desktop_entry = l->data;
1927
1928       info = g_desktop_app_info_new (desktop_entry);
1929       if (info)
1930         {
1931           if (app_info_in_list (G_APP_INFO (info), infos))
1932             g_object_unref (info);
1933           else
1934             infos = g_list_prepend (infos, info);
1935         }
1936       g_free (desktop_entry);
1937     }
1938
1939   g_list_free (desktop_entries);
1940   
1941   return g_list_reverse (infos);
1942 }
1943
1944 /**
1945  * g_app_info_reset_type_associations:
1946  * @content_type: a content type 
1947  *
1948  * Removes all changes to the type associations done by
1949  * g_app_info_set_as_default_for_type(), 
1950  * g_app_info_set_as_default_for_extension(), 
1951  * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
1952  *
1953  * Since: 2.20
1954  */
1955 void
1956 g_app_info_reset_type_associations (const char *content_type)
1957 {
1958   update_mimeapps_list (NULL, content_type,
1959                         UPDATE_MIME_NONE,
1960                         NULL);
1961 }
1962
1963 /**
1964  * g_app_info_get_default_for_type:
1965  * @content_type: the content type to find a #GAppInfo for
1966  * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1967  *     support URIs
1968  * 
1969  * Gets the #GAppInfo that corresponds to a given content type.
1970  *
1971  * Returns: #GAppInfo for given @content_type or %NULL on error.
1972  **/
1973 GAppInfo *
1974 g_app_info_get_default_for_type (const char *content_type,
1975                                  gboolean    must_support_uris)
1976 {
1977   GList *desktop_entries, *l;
1978   char *user_default = NULL;
1979   GAppInfo *info;
1980
1981   g_return_val_if_fail (content_type != NULL, NULL);
1982   
1983   desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
1984
1985   info = NULL;
1986
1987   if (user_default != NULL)
1988     {
1989       info = (GAppInfo *) g_desktop_app_info_new (user_default);
1990
1991       if (info)
1992         {
1993           if (must_support_uris && !g_app_info_supports_uris (info))
1994             {
1995               g_object_unref (info);
1996               info = NULL;
1997             }
1998         }
1999     }
2000
2001   g_free (user_default);
2002
2003   if (info != NULL)
2004     {
2005       g_list_free_full (desktop_entries, g_free);
2006       return info;
2007     }
2008
2009   /* pick the first from the other list that matches our URI
2010    * requirements.
2011    */
2012   for (l = desktop_entries; l != NULL; l = l->next)
2013     {
2014       char *desktop_entry = l->data;
2015
2016       info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2017       if (info)
2018         {
2019           if (must_support_uris && !g_app_info_supports_uris (info))
2020             {
2021               g_object_unref (info);
2022               info = NULL;
2023             }
2024           else
2025             break;
2026         }
2027     }
2028   
2029   g_list_free_full (desktop_entries, g_free);
2030
2031   return info;
2032 }
2033
2034 /**
2035  * g_app_info_get_default_for_uri_scheme:
2036  * @uri_scheme: a string containing a URI scheme.
2037  *
2038  * Gets the default application for launching applications 
2039  * using this URI scheme. A URI scheme is the initial part 
2040  * of the URI, up to but not including the ':', e.g. "http", 
2041  * "ftp" or "sip".
2042  * 
2043  * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2044  **/
2045 GAppInfo *
2046 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2047 {
2048   GAppInfo *app_info;
2049   char *content_type, *scheme_down;
2050
2051   scheme_down = g_ascii_strdown (uri_scheme, -1);
2052   content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2053   g_free (scheme_down);
2054   app_info = g_app_info_get_default_for_type (content_type, FALSE);
2055   g_free (content_type);
2056
2057   return app_info;
2058 }
2059
2060 static void
2061 get_apps_from_dir (GHashTable *apps, 
2062                    const char *dirname, 
2063                    const char *prefix)
2064 {
2065   GDir *dir;
2066   const char *basename;
2067   char *filename, *subprefix, *desktop_id;
2068   gboolean hidden;
2069   GDesktopAppInfo *appinfo;
2070   
2071   dir = g_dir_open (dirname, 0, NULL);
2072   if (dir)
2073     {
2074       while ((basename = g_dir_read_name (dir)) != NULL)
2075         {
2076           filename = g_build_filename (dirname, basename, NULL);
2077           if (g_str_has_suffix (basename, ".desktop"))
2078             {
2079               desktop_id = g_strconcat (prefix, basename, NULL);
2080
2081               /* Use _extended so we catch NULLs too (hidden) */
2082               if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2083                 {
2084                   appinfo = g_desktop_app_info_new_from_filename (filename);
2085                   hidden = FALSE;
2086
2087                   if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2088                     {
2089                       g_object_unref (appinfo);
2090                       appinfo = NULL;
2091                       hidden = TRUE;
2092                     }
2093                                       
2094                   if (appinfo || hidden)
2095                     {
2096                       g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2097
2098                       if (appinfo)
2099                         {
2100                           /* Reuse instead of strdup here */
2101                           appinfo->desktop_id = desktop_id;
2102                           desktop_id = NULL;
2103                         }
2104                     }
2105                 }
2106               g_free (desktop_id);
2107             }
2108           else
2109             {
2110               if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2111                 {
2112                   subprefix = g_strconcat (prefix, basename, "-", NULL);
2113                   get_apps_from_dir (apps, filename, subprefix);
2114                   g_free (subprefix);
2115                 }
2116             }
2117           g_free (filename);
2118         }
2119       g_dir_close (dir);
2120     }
2121 }
2122
2123
2124 /**
2125  * g_app_info_get_all:
2126  *
2127  * Gets a list of all of the applications currently registered 
2128  * on this system.
2129  * 
2130  * For desktop files, this includes applications that have 
2131  * <literal>NoDisplay=true</literal> set or are excluded from 
2132  * display by means of <literal>OnlyShowIn</literal> or
2133  * <literal>NotShowIn</literal>. See g_app_info_should_show().
2134  * The returned list does not include applications which have
2135  * the <literal>Hidden</literal> key set. 
2136  * 
2137  * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2138  **/
2139 GList *
2140 g_app_info_get_all (void)
2141 {
2142   const char * const *dirs;
2143   GHashTable *apps;
2144   GHashTableIter iter;
2145   gpointer value;
2146   int i;
2147   GList *infos;
2148
2149   dirs = get_applications_search_path ();
2150
2151   apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2152                                 g_free, NULL);
2153
2154   
2155   for (i = 0; dirs[i] != NULL; i++)
2156     get_apps_from_dir (apps, dirs[i], "");
2157
2158
2159   infos = NULL;
2160   g_hash_table_iter_init (&iter, apps);
2161   while (g_hash_table_iter_next (&iter, NULL, &value))
2162     {
2163       if (value)
2164         infos = g_list_prepend (infos, value);
2165     }
2166
2167   g_hash_table_destroy (apps);
2168
2169   return g_list_reverse (infos);
2170 }
2171
2172 /* Cacheing of mimeinfo.cache and defaults.list files */
2173
2174 typedef struct {
2175   char *path;
2176   GHashTable *mime_info_cache_map;
2177   GHashTable *defaults_list_map;
2178   GHashTable *mimeapps_list_added_map;
2179   GHashTable *mimeapps_list_removed_map;
2180   GHashTable *mimeapps_list_defaults_map;
2181   time_t mime_info_cache_timestamp;
2182   time_t defaults_list_timestamp;
2183   time_t mimeapps_list_timestamp;
2184 } MimeInfoCacheDir;
2185
2186 typedef struct {
2187   GList *dirs;                       /* mimeinfo.cache and defaults.list */
2188   GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2189   time_t last_stat_time;
2190   guint should_ping_mime_monitor : 1;
2191 } MimeInfoCache;
2192
2193 static MimeInfoCache *mime_info_cache = NULL;
2194 G_LOCK_DEFINE_STATIC (mime_info_cache);
2195
2196 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir  *dir,
2197                                                      const char        *mime_type,
2198                                                      char             **new_desktop_file_ids);
2199
2200 static MimeInfoCache * mime_info_cache_new (void);
2201
2202 static void
2203 destroy_info_cache_value (gpointer  key, 
2204                           GList    *value, 
2205                           gpointer  data)
2206 {
2207   g_list_foreach (value, (GFunc)g_free, NULL);
2208   g_list_free (value);
2209 }
2210
2211 static void
2212 destroy_info_cache_map (GHashTable *info_cache_map)
2213 {
2214   g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2215   g_hash_table_destroy (info_cache_map);
2216 }
2217
2218 static gboolean
2219 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2220                                  const char       *cache_file,
2221                                  time_t           *timestamp)
2222 {
2223   struct stat buf;
2224   char *filename;
2225   
2226   filename = g_build_filename (dir->path, cache_file, NULL);
2227   
2228   if (g_stat (filename, &buf) < 0)
2229     {
2230       g_free (filename);
2231       return TRUE;
2232     }
2233   g_free (filename);
2234
2235   if (buf.st_mtime != *timestamp) 
2236     return TRUE;
2237   
2238   return FALSE;
2239 }
2240
2241 /* Call with lock held */
2242 static gboolean
2243 remove_all (gpointer  key,
2244             gpointer  value,
2245             gpointer  user_data)
2246 {
2247   return TRUE;
2248 }
2249
2250
2251 static void
2252 mime_info_cache_blow_global_cache (void)
2253 {
2254   g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2255                                remove_all, NULL);
2256 }
2257
2258 static void
2259 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2260 {
2261   GError *load_error;
2262   GKeyFile *key_file;
2263   gchar *filename, **mime_types;
2264   int i;
2265   struct stat buf;
2266   
2267   load_error = NULL;
2268   mime_types = NULL;
2269   
2270   if (dir->mime_info_cache_map != NULL &&
2271       !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2272                                         &dir->mime_info_cache_timestamp))
2273     return;
2274   
2275   if (dir->mime_info_cache_map != NULL)
2276     destroy_info_cache_map (dir->mime_info_cache_map);
2277   
2278   dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2279                                                     (GDestroyNotify) g_free,
2280                                                     NULL);
2281   
2282   key_file = g_key_file_new ();
2283   
2284   filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2285   
2286   if (g_stat (filename, &buf) < 0)
2287     goto error;
2288   
2289   if (dir->mime_info_cache_timestamp > 0) 
2290     mime_info_cache->should_ping_mime_monitor = TRUE;
2291   
2292   dir->mime_info_cache_timestamp = buf.st_mtime;
2293   
2294   g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2295   
2296   g_free (filename);
2297   filename = NULL;
2298   
2299   if (load_error != NULL)
2300     goto error;
2301   
2302   mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2303                                     NULL, &load_error);
2304   
2305   if (load_error != NULL)
2306     goto error;
2307   
2308   for (i = 0; mime_types[i] != NULL; i++)
2309     {
2310       gchar **desktop_file_ids;
2311       char *unaliased_type;
2312       desktop_file_ids = g_key_file_get_string_list (key_file,
2313                                                      MIME_CACHE_GROUP,
2314                                                      mime_types[i],
2315                                                      NULL,
2316                                                      NULL);
2317       
2318       if (desktop_file_ids == NULL)
2319         continue;
2320
2321       unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2322       mime_info_cache_dir_add_desktop_entries (dir,
2323                                                unaliased_type,
2324                                                desktop_file_ids);
2325       g_free (unaliased_type);
2326     
2327       g_strfreev (desktop_file_ids);
2328     }
2329   
2330   g_strfreev (mime_types);
2331   g_key_file_free (key_file);
2332   
2333   return;
2334  error:
2335   g_free (filename);
2336   g_key_file_free (key_file);
2337   
2338   if (mime_types != NULL)
2339     g_strfreev (mime_types);
2340   
2341   if (load_error)
2342     g_error_free (load_error);
2343 }
2344
2345 static void
2346 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2347 {
2348   GKeyFile *key_file;
2349   GError *load_error;
2350   gchar *filename, **mime_types;
2351   char *unaliased_type;
2352   char **desktop_file_ids;
2353   int i;
2354   struct stat buf;
2355
2356   load_error = NULL;
2357   mime_types = NULL;
2358
2359   if (dir->defaults_list_map != NULL &&
2360       !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2361                                         &dir->defaults_list_timestamp))
2362     return;
2363   
2364   if (dir->defaults_list_map != NULL)
2365     g_hash_table_destroy (dir->defaults_list_map);
2366   dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2367                                                   g_free, (GDestroyNotify)g_strfreev);
2368   
2369
2370   key_file = g_key_file_new ();
2371   
2372   filename = g_build_filename (dir->path, "defaults.list", NULL);
2373   if (g_stat (filename, &buf) < 0)
2374     goto error;
2375
2376   if (dir->defaults_list_timestamp > 0) 
2377     mime_info_cache->should_ping_mime_monitor = TRUE;
2378
2379   dir->defaults_list_timestamp = buf.st_mtime;
2380
2381   g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2382   g_free (filename);
2383   filename = NULL;
2384
2385   if (load_error != NULL)
2386     goto error;
2387
2388   mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2389                                     NULL, NULL);
2390   if (mime_types != NULL)
2391     {
2392       for (i = 0; mime_types[i] != NULL; i++)
2393         {
2394           desktop_file_ids = g_key_file_get_string_list (key_file,
2395                                                          DEFAULT_APPLICATIONS_GROUP,
2396                                                          mime_types[i],
2397                                                          NULL,
2398                                                          NULL);
2399           if (desktop_file_ids == NULL)
2400             continue;
2401           
2402           unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2403           g_hash_table_replace (dir->defaults_list_map,
2404                                 unaliased_type,
2405                                 desktop_file_ids);
2406         }
2407       
2408       g_strfreev (mime_types);
2409     }
2410
2411   g_key_file_free (key_file);
2412   return;
2413   
2414  error:
2415   g_free (filename);
2416   g_key_file_free (key_file);
2417   
2418   if (mime_types != NULL)
2419     g_strfreev (mime_types);
2420   
2421   if (load_error)
2422     g_error_free (load_error);
2423 }
2424
2425 static void
2426 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2427 {
2428   GKeyFile *key_file;
2429   GError *load_error;
2430   gchar *filename, **mime_types;
2431   char *unaliased_type;
2432   char **desktop_file_ids;
2433   char *desktop_id;
2434   int i;
2435   struct stat buf;
2436
2437   load_error = NULL;
2438   mime_types = NULL;
2439
2440   if (dir->mimeapps_list_added_map != NULL &&
2441       !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2442                                         &dir->mimeapps_list_timestamp))
2443     return;
2444   
2445   if (dir->mimeapps_list_added_map != NULL)
2446     g_hash_table_destroy (dir->mimeapps_list_added_map);
2447   dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2448                                                         g_free, (GDestroyNotify)g_strfreev);
2449   
2450   if (dir->mimeapps_list_removed_map != NULL)
2451     g_hash_table_destroy (dir->mimeapps_list_removed_map);
2452   dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2453                                                           g_free, (GDestroyNotify)g_strfreev);
2454
2455   if (dir->mimeapps_list_defaults_map != NULL)
2456     g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2457   dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2458                                                            g_free, g_free);
2459
2460   key_file = g_key_file_new ();
2461   
2462   filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2463   if (g_stat (filename, &buf) < 0)
2464     goto error;
2465
2466   if (dir->mimeapps_list_timestamp > 0) 
2467     mime_info_cache->should_ping_mime_monitor = TRUE;
2468
2469   dir->mimeapps_list_timestamp = buf.st_mtime;
2470
2471   g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2472   g_free (filename);
2473   filename = NULL;
2474
2475   if (load_error != NULL)
2476     goto error;
2477
2478   mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2479                                     NULL, NULL);
2480   if (mime_types != NULL)
2481     {
2482       for (i = 0; mime_types[i] != NULL; i++)
2483         {
2484           desktop_file_ids = g_key_file_get_string_list (key_file,
2485                                                          ADDED_ASSOCIATIONS_GROUP,
2486                                                          mime_types[i],
2487                                                          NULL,
2488                                                          NULL);
2489           if (desktop_file_ids == NULL)
2490             continue;
2491           
2492           unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2493           g_hash_table_replace (dir->mimeapps_list_added_map,
2494                                 unaliased_type,
2495                                 desktop_file_ids);
2496         }
2497       
2498       g_strfreev (mime_types);
2499     }
2500
2501   mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2502                                     NULL, NULL);
2503   if (mime_types != NULL)
2504     {
2505       for (i = 0; mime_types[i] != NULL; i++)
2506         {
2507           desktop_file_ids = g_key_file_get_string_list (key_file,
2508                                                          REMOVED_ASSOCIATIONS_GROUP,
2509                                                          mime_types[i],
2510                                                          NULL,
2511                                                          NULL);
2512           if (desktop_file_ids == NULL)
2513             continue;
2514           
2515           unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2516           g_hash_table_replace (dir->mimeapps_list_removed_map,
2517                                 unaliased_type,
2518                                 desktop_file_ids);
2519         }
2520       
2521       g_strfreev (mime_types);
2522     }
2523
2524   mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2525                                     NULL, NULL);
2526   if (mime_types != NULL)
2527     {
2528       for (i = 0; mime_types[i] != NULL; i++)
2529         {
2530           desktop_id = g_key_file_get_string (key_file,
2531                                               DEFAULT_APPLICATIONS_GROUP,
2532                                               mime_types[i],
2533                                               NULL);
2534           if (desktop_id == NULL)
2535             continue;
2536
2537           unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2538           g_hash_table_replace (dir->mimeapps_list_defaults_map,
2539                                 unaliased_type,
2540                                 desktop_id);
2541         }
2542
2543       g_strfreev (mime_types);
2544     }
2545
2546   g_key_file_free (key_file);
2547   return;
2548   
2549  error:
2550   g_free (filename);
2551   g_key_file_free (key_file);
2552   
2553   if (mime_types != NULL)
2554     g_strfreev (mime_types);
2555   
2556   if (load_error)
2557     g_error_free (load_error);
2558 }
2559
2560 static MimeInfoCacheDir *
2561 mime_info_cache_dir_new (const char *path)
2562 {
2563   MimeInfoCacheDir *dir;
2564
2565   dir = g_new0 (MimeInfoCacheDir, 1);
2566   dir->path = g_strdup (path);
2567   
2568   return dir;
2569 }
2570
2571 static void
2572 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2573 {
2574   if (dir == NULL)
2575     return;
2576   
2577   if (dir->mime_info_cache_map != NULL)
2578     {
2579       destroy_info_cache_map (dir->mime_info_cache_map);
2580       dir->mime_info_cache_map = NULL;
2581       
2582   }
2583   
2584   if (dir->defaults_list_map != NULL)
2585     {
2586       g_hash_table_destroy (dir->defaults_list_map);
2587       dir->defaults_list_map = NULL;
2588     }
2589   
2590   if (dir->mimeapps_list_added_map != NULL)
2591     {
2592       g_hash_table_destroy (dir->mimeapps_list_added_map);
2593       dir->mimeapps_list_added_map = NULL;
2594     }
2595   
2596   if (dir->mimeapps_list_removed_map != NULL)
2597     {
2598       g_hash_table_destroy (dir->mimeapps_list_removed_map);
2599       dir->mimeapps_list_removed_map = NULL;
2600     }
2601
2602   if (dir->mimeapps_list_defaults_map != NULL)
2603     {
2604       g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2605       dir->mimeapps_list_defaults_map = NULL;
2606     }
2607
2608   g_free (dir);
2609 }
2610
2611 static void
2612 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir  *dir,
2613                                          const char        *mime_type,
2614                                          char             **new_desktop_file_ids)
2615 {
2616   GList *desktop_file_ids;
2617   int i;
2618   
2619   desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2620                                           mime_type);
2621   
2622   for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2623     {
2624       if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
2625         desktop_file_ids = g_list_append (desktop_file_ids,
2626                                           g_strdup (new_desktop_file_ids[i]));
2627     }
2628   
2629   g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2630 }
2631
2632 static void
2633 mime_info_cache_init_dir_lists (void)
2634 {
2635   const char * const *dirs;
2636   int i;
2637   
2638   mime_info_cache = mime_info_cache_new ();
2639   
2640   dirs = get_applications_search_path ();
2641   
2642   for (i = 0; dirs[i] != NULL; i++)
2643     {
2644       MimeInfoCacheDir *dir;
2645       
2646       dir = mime_info_cache_dir_new (dirs[i]);
2647       
2648       if (dir != NULL)
2649         {
2650           mime_info_cache_dir_init (dir);
2651           mime_info_cache_dir_init_defaults_list (dir);
2652           mime_info_cache_dir_init_mimeapps_list (dir);
2653           
2654           mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2655         }
2656     }
2657 }
2658
2659 static void
2660 mime_info_cache_update_dir_lists (void)
2661 {
2662   GList *tmp;
2663   
2664   tmp = mime_info_cache->dirs;
2665   
2666   while (tmp != NULL)
2667     {
2668       MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2669
2670       /* No need to do this if we had file monitors... */
2671       mime_info_cache_blow_global_cache ();
2672       mime_info_cache_dir_init (dir);
2673       mime_info_cache_dir_init_defaults_list (dir);
2674       mime_info_cache_dir_init_mimeapps_list (dir);
2675       
2676       tmp = tmp->next;
2677     }
2678 }
2679
2680 static void
2681 mime_info_cache_init (void)
2682 {
2683   G_LOCK (mime_info_cache);
2684   if (mime_info_cache == NULL)
2685     mime_info_cache_init_dir_lists ();
2686   else
2687     {
2688       time_t now;
2689       
2690       time (&now);
2691       if (now >= mime_info_cache->last_stat_time + 10)
2692         {
2693           mime_info_cache_update_dir_lists ();
2694           mime_info_cache->last_stat_time = now;
2695         }
2696     }
2697   
2698   if (mime_info_cache->should_ping_mime_monitor)
2699     {
2700       /* g_idle_add (emit_mime_changed, NULL); */
2701       mime_info_cache->should_ping_mime_monitor = FALSE;
2702     }
2703   
2704   G_UNLOCK (mime_info_cache);
2705 }
2706
2707 static MimeInfoCache *
2708 mime_info_cache_new (void)
2709 {
2710   MimeInfoCache *cache;
2711   
2712   cache = g_new0 (MimeInfoCache, 1);
2713   
2714   cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2715                                                         (GDestroyNotify) g_free,
2716                                                         (GDestroyNotify) g_free);
2717   return cache;
2718 }
2719
2720 static void
2721 mime_info_cache_free (MimeInfoCache *cache)
2722 {
2723   if (cache == NULL)
2724     return;
2725   
2726   g_list_foreach (cache->dirs,
2727                   (GFunc) mime_info_cache_dir_free,
2728                   NULL);
2729   g_list_free (cache->dirs);
2730   g_hash_table_destroy (cache->global_defaults_cache);
2731   g_free (cache);
2732 }
2733
2734 /**
2735  * mime_info_cache_reload:
2736  * @dir: directory path which needs reloading.
2737  * 
2738  * Reload the mime information for the @dir.
2739  */
2740 static void
2741 mime_info_cache_reload (const char *dir)
2742 {
2743   /* FIXME: just reload the dir that needs reloading,
2744    * don't blow the whole cache
2745    */
2746   if (mime_info_cache != NULL)
2747     {
2748       G_LOCK (mime_info_cache);
2749       mime_info_cache_free (mime_info_cache);
2750       mime_info_cache = NULL;
2751       G_UNLOCK (mime_info_cache);
2752     }
2753 }
2754
2755 static GList *
2756 append_desktop_entry (GList      *list, 
2757                       const char *desktop_entry,
2758                       GList      *removed_entries)
2759 {
2760   /* Add if not already in list, and valid */
2761   if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2762       !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2763     list = g_list_prepend (list, g_strdup (desktop_entry));
2764   
2765   return list;
2766 }
2767
2768 /**
2769  * get_all_desktop_entries_for_mime_type:
2770  * @mime_type: a mime type.
2771  * @except: NULL or a strv list
2772  *
2773  * Returns all the desktop ids for @mime_type. The desktop files
2774  * are listed in an order so that default applications are listed before
2775  * non-default ones, and handlers for inherited mimetypes are listed
2776  * after the base ones.
2777  *
2778  * Optionally doesn't list the desktop ids given in the @except 
2779  *
2780  * Return value: a #GList containing the desktop ids which claim
2781  *    to handle @mime_type.
2782  */
2783 static GList *
2784 get_all_desktop_entries_for_mime_type (const char  *base_mime_type,
2785                                        const char **except,
2786                                        gboolean     include_fallback,
2787                                        char       **explicit_default)
2788 {
2789   GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2790   MimeInfoCacheDir *dir;
2791   char *mime_type, *default_entry = NULL;
2792   const char *entry;
2793   char **mime_types;
2794   char **default_entries;
2795   char **removed_associations;
2796   int i, j, k;
2797   GPtrArray *array;
2798   char **anc;
2799   
2800   mime_info_cache_init ();
2801
2802   if (include_fallback)
2803     {
2804       /* collect all ancestors */
2805       mime_types = _g_unix_content_type_get_parents (base_mime_type);
2806       array = g_ptr_array_new ();
2807       for (i = 0; mime_types[i]; i++)
2808         g_ptr_array_add (array, mime_types[i]);
2809       g_free (mime_types);
2810       for (i = 0; i < array->len; i++)
2811         {
2812           anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
2813           for (j = 0; anc[j]; j++)
2814             {
2815               for (k = 0; k < array->len; k++)
2816                 {
2817                   if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
2818                     break;
2819                 }
2820               if (k == array->len) /* not found */
2821                 g_ptr_array_add (array, g_strdup (anc[j]));
2822             }
2823           g_strfreev (anc);
2824         }
2825       g_ptr_array_add (array, NULL);
2826       mime_types = (char **)g_ptr_array_free (array, FALSE);
2827     }
2828   else
2829     {
2830       mime_types = g_malloc0 (2 * sizeof (gchar *));
2831       mime_types[0] = g_strdup (base_mime_type);
2832       mime_types[1] = NULL;
2833     }
2834
2835   G_LOCK (mime_info_cache);
2836   
2837   removed_entries = NULL;
2838   desktop_entries = NULL;
2839
2840   for (i = 0; except != NULL && except[i] != NULL; i++)
2841     removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
2842   
2843   for (i = 0; mime_types[i] != NULL; i++)
2844     {
2845       mime_type = mime_types[i];
2846
2847       /* Go through all apps listed in user and system dirs */
2848       for (dir_list = mime_info_cache->dirs;
2849            dir_list != NULL;
2850            dir_list = dir_list->next)
2851         {
2852           dir = dir_list->data;
2853
2854           /* Pick the explicit default application */
2855           entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
2856
2857           if (entry != NULL)
2858             {
2859               /* Save the default entry if it's the first one we encounter */
2860               if (default_entry == NULL)
2861                 default_entry = g_strdup (entry);
2862             }
2863
2864           /* Then added associations from mimeapps.list */
2865           default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2866           for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2867             desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2868
2869           /* Then removed associations from mimeapps.list */
2870           removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2871           for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2872             removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2873
2874           /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2875           default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2876           for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2877             desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2878         }
2879
2880       /* Go through all entries that support the mimetype */
2881       for (dir_list = mime_info_cache->dirs;
2882            dir_list != NULL;
2883            dir_list = dir_list->next) 
2884         {
2885           dir = dir_list->data;
2886         
2887           list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2888           for (tmp = list; tmp != NULL; tmp = tmp->next)
2889             desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2890         }
2891     }
2892   
2893   G_UNLOCK (mime_info_cache);
2894
2895   g_strfreev (mime_types);
2896
2897   if (explicit_default != NULL)
2898     *explicit_default = default_entry;
2899   else
2900     g_free (default_entry);
2901
2902   g_list_foreach (removed_entries, (GFunc)g_free, NULL);
2903   g_list_free (removed_entries);
2904
2905   desktop_entries = g_list_reverse (desktop_entries);
2906   
2907   return desktop_entries;
2908 }
2909
2910 /* GDesktopAppInfoLookup interface: */
2911
2912 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
2913 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
2914
2915 static void
2916 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
2917 {
2918 }
2919
2920 /**
2921  * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2922  * @lookup: a #GDesktopAppInfoLookup
2923  * @uri_scheme: a string containing a URI scheme.
2924  *
2925  * Gets the default application for launching applications 
2926  * using this URI scheme for a particular GDesktopAppInfoLookup
2927  * implementation.
2928  *
2929  * The GDesktopAppInfoLookup interface and this function is used
2930  * to implement g_app_info_get_default_for_uri_scheme() backends
2931  * in a GIO module. There is no reason for applications to use it
2932  * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2933  * 
2934  * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2935  *
2936  * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
2937  */
2938 GAppInfo *
2939 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2940                                                       const char            *uri_scheme)
2941 {
2942   GDesktopAppInfoLookupIface *iface;
2943   
2944   g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2945
2946   iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2947
2948   return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2949 }