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