#include <crt_externs.h>
#endif
+#undef G_DISABLE_DEPRECATED
+
#include "gcontenttypeprivate.h"
#include "gdesktopappinfo.h"
#include "gfile.h"
#include "giomodule-priv.h"
#include "gappinfo.h"
-#include "gioalias.h"
/**
* SECTION:gdesktopappinfo
+ * @title: GDesktopAppInfo
* @short_description: Application information from desktop files
- * @include: gio/gdesktopappinfo.h
- *
+ * @include: gio/gdesktopappinfo.h
+ *
* #GDesktopAppInfo is an implementation of #GAppInfo based on
* desktop files.
- *
- * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
- * the UNIX-specific GIO interfaces, thus you have to use the
+ *
+ * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
+ * the UNIX-specific GIO interfaces, thus you have to use the
* <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
*/
-#define DEFAULT_APPLICATIONS_GROUP "Default Applications"
-#define ADDED_ASSOCIATIONS_GROUP "Added Associations"
-#define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
+#define DEFAULT_APPLICATIONS_GROUP "Default Applications"
+#define ADDED_ASSOCIATIONS_GROUP "Added Associations"
+#define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
#define MIME_CACHE_GROUP "MIME Cache"
+#define FULL_NAME_KEY "X-GNOME-FullName"
static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
- const char **except);
+ const char **except,
+ gboolean include_fallback,
+ char **explicit_default);
static void mime_info_cache_reload (const char *dir);
static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
GError **error);
char *name;
/* FIXME: what about GenericName ? */
+ char *fullname;
char *comment;
char *icon_name;
GIcon *icon;
/* FIXME: what about StartupWMClass ? */
};
+typedef enum {
+ UPDATE_MIME_NONE = 1 << 0,
+ UPDATE_MIME_SET_DEFAULT = 1 << 1,
+ UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
+ UPDATE_MIME_REMOVE = 1 << 3,
+ UPDATE_MIME_SET_LAST_USED = 1 << 4,
+} UpdateMimeFlags;
+
G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
g_desktop_app_info_iface_init))
g_free (info->desktop_id);
g_free (info->filename);
g_free (info->name);
+ g_free (info->fullname);
g_free (info->comment);
g_free (info->icon_name);
if (info->icon)
info->filename = NULL;
info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
+ info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
new_info->desktop_id = g_strdup (info->desktop_id);
new_info->name = g_strdup (info->name);
+ new_info->fullname = g_strdup (info->fullname);
new_info->comment = g_strdup (info->comment);
new_info->nodisplay = info->nodisplay;
new_info->icon_name = g_strdup (info->icon_name);
return info->name;
}
+static const char *
+g_desktop_app_info_get_display_name (GAppInfo *appinfo)
+{
+ GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+
+ if (info->fullname == NULL)
+ return g_desktop_app_info_get_name (appinfo);
+ return info->fullname;
+}
+
/**
* g_desktop_app_info_get_is_hidden:
* @info: a #GDesktopAppInfo.
return info->hidden;
}
+/**
+ * g_desktop_app_info_get_filename:
+ * @info: a #GDesktopAppInfo
+ *
+ * When @info was created from a known filename, return it. In some
+ * situations such as the #GDesktopAppInfo returned from
+ * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
+ *
+ * Returns: The full path to the file for @info, or %NULL if not known.
+ * Since: 2.24
+ */
+const char *
+g_desktop_app_info_get_filename (GDesktopAppInfo *info)
+{
+ return info->filename;
+}
+
static const char *
g_desktop_app_info_get_description (GAppInfo *appinfo)
{
if (info->icon_name)
{
g_string_append (exec, "--icon ");
- g_string_append (exec, info->icon_name);
+ expanded = g_shell_quote (info->icon_name);
+ g_string_append (exec, expanded);
+ g_free (expanded);
}
break;
case 'c':
if (info->name)
- g_string_append (exec, info->name);
+ {
+ expanded = g_shell_quote (info->name);
+ g_string_append (exec, expanded);
+ g_free (expanded);
+ }
break;
case 'k':
if (info->filename)
- g_string_append (exec, info->filename);
+ {
+ expanded = g_shell_quote (info->filename);
+ g_string_append (exec, expanded);
+ g_free (expanded);
+ }
break;
case 'm': /* deprecated */
{
GList *uri_list = *uris;
const char *p = info->exec;
- GString *expanded_exec = g_string_new (NULL);
+ GString *expanded_exec;
gboolean res;
-
+
if (info->exec == NULL)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Desktop file didn't specify Exec field"));
return FALSE;
}
-
+
+ expanded_exec = g_string_new (NULL);
+
while (*p)
{
if (p[0] == '%' && p[1] != '\0')
}
else
g_string_append_c (expanded_exec, *p);
-
+
p++;
}
-
+
/* No file substitutions */
if (uri_list == *uris && uri_list != NULL)
{
g_string_append_c (expanded_exec, ' ');
expand_macro ('f', expanded_exec, info, uris);
}
-
+
res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
g_string_free (expanded_exec, TRUE);
return res;
typedef struct
{
- const char *display;
- const char *sn_id;
+ char *display;
+ char *sn_id;
+ char *desktop_file;
} ChildSetupData;
static void
if (data->display)
g_setenv ("DISPLAY", data->display, TRUE);
- else
- g_unsetenv ("DISPLAY");
if (data->sn_id)
g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
- else
- g_unsetenv ("DESKTOP_STARTUP_ID");
+
+ if (data->desktop_file)
+ {
+ gchar pid[20];
+
+ g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
+
+ g_snprintf (pid, 20, "%ld", (long)getpid ());
+ g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
+ }
}
static gboolean
data.display = NULL;
data.sn_id = NULL;
+ data.desktop_file = info->filename;
if (launch_context)
{
static gboolean
update_mimeapps_list (const char *desktop_id,
- const char *content_type,
- gboolean add_as_default,
- gboolean add_non_default,
- gboolean remove,
+ const char *content_type,
+ UpdateMimeFlags flags,
GError **error)
{
- char *dirname, *filename;
+ char *dirname, *filename, *string;
GKeyFile *key_file;
- gboolean load_succeeded, res;
+ gboolean load_succeeded, res, explicit_default;
char **old_list, **list;
- GList *system_list, *l;
+ GList *system_list;
gsize length, data_size;
char *data;
int i, j, k;
char **content_types;
/* Don't add both at start and end */
- g_assert (!(add_as_default && add_non_default));
-
+ g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
+ (flags & UPDATE_MIME_SET_NON_DEFAULT)));
+
dirname = ensure_dir (APP_DIR, error);
if (!dirname)
return FALSE;
}
else
{
- content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
+ content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
}
+ explicit_default = FALSE;
+
+ for (k = 0; content_types && content_types[k]; k++)
+ {
+ /* set as default, if requested so */
+ string = g_key_file_get_string (key_file,
+ DEFAULT_APPLICATIONS_GROUP,
+ content_types[k],
+ NULL);
+
+ if (g_strcmp0 (string, desktop_id) != 0 &&
+ (flags & UPDATE_MIME_SET_DEFAULT))
+ {
+ g_free (string);
+ string = g_strdup (desktop_id);
+
+ /* add in the non-default list too, if it's not already there */
+ flags |= UPDATE_MIME_SET_NON_DEFAULT;
+ }
+
+ if (string == NULL || desktop_id == NULL)
+ g_key_file_remove_key (key_file,
+ DEFAULT_APPLICATIONS_GROUP,
+ content_types[k],
+ NULL);
+ else
+ {
+ g_key_file_set_string (key_file,
+ DEFAULT_APPLICATIONS_GROUP,
+ content_types[k],
+ string);
+
+ explicit_default = TRUE;
+ }
+
+ g_free (string);
+ }
+
+ if (content_type)
+ {
+ /* reuse the list from above */
+ }
+ else
+ {
+ g_strfreev (content_types);
+ content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
+ }
+
for (k = 0; content_types && content_types[k]; k++)
{
/* Add to the right place in the list */
list = g_new (char *, 1 + length + 1);
i = 0;
- if (add_as_default)
- list[i++] = g_strdup (desktop_id);
+
+ /* if we're adding a last-used hint, just put the application in front of the list */
+ if (flags & UPDATE_MIME_SET_LAST_USED)
+ {
+ /* avoid adding this again as non-default later */
+ if (flags & UPDATE_MIME_SET_NON_DEFAULT)
+ flags ^= UPDATE_MIME_SET_NON_DEFAULT;
+
+ list[i++] = g_strdup (desktop_id);
+ }
+
if (old_list)
{
for (j = 0; old_list[j] != NULL; j++)
{
if (g_strcmp0 (old_list[j], desktop_id) != 0)
- list[i++] = g_strdup (old_list[j]);
- else if (add_non_default)
+ {
+ /* rewrite other entries if they're different from the new one */
+ list[i++] = g_strdup (old_list[j]);
+ }
+ else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
{
- /* If adding as non-default, and it's already in,
- don't change order of desktop ids */
- add_non_default = FALSE;
+ /* we encountered an old entry which is equal to the one we're adding as non-default,
+ * don't change its position in the list.
+ */
+ flags ^= UPDATE_MIME_SET_NON_DEFAULT;
list[i++] = g_strdup (old_list[j]);
}
}
}
-
- if (add_non_default)
- {
- /* We're adding as non-default, and it wasn't already in the list,
- so we add at the end. But to avoid listing the app before the
- current system default (thus changing the default) we have to
- add the current list of (not yet listed) apps before it. */
- list[i] = NULL; /* Terminate current list so we can use it */
- system_list = get_all_desktop_entries_for_mime_type (content_type, (const char **)list);
-
- list = g_renew (char *, list, 1 + length + g_list_length (system_list) + 1);
-
- for (l = system_list; l != NULL; l = l->next)
- {
- list[i++] = l->data; /* no strdup, taking ownership */
- if (g_strcmp0 (l->data, desktop_id) == 0)
- add_non_default = FALSE;
- }
- g_list_free (system_list);
-
- if (add_non_default)
- list[i++] = g_strdup (desktop_id);
- }
-
+ /* add it at the end of the list */
+ if (flags & UPDATE_MIME_SET_NON_DEFAULT)
+ list[i++] = g_strdup (desktop_id);
+
list[i] = NULL;
g_strfreev (old_list);
content_types[k],
NULL);
else
- g_key_file_set_string_list (key_file,
- ADDED_ASSOCIATIONS_GROUP,
- content_types[k],
- (const char * const *)list, i);
+ {
+ g_key_file_set_string_list (key_file,
+ ADDED_ASSOCIATIONS_GROUP,
+ content_types[k],
+ (const char * const *)list, i);
+
+ /* if we had no explicit default set, we should add the system default to the
+ * list, to avoid overriding it with applications from this list.
+ */
+ if (!explicit_default)
+ {
+ system_list = get_all_desktop_entries_for_mime_type (content_type, (const char **) list, FALSE, NULL);
+
+ if (system_list != NULL)
+ {
+ string = system_list->data;
+
+ g_key_file_set_string (key_file,
+ DEFAULT_APPLICATIONS_GROUP,
+ content_types[k],
+ string);
+ }
+
+ g_list_free_full (system_list, g_free);
+ }
+ }
g_strfreev (list);
}
list = g_new (char *, 1 + length + 1);
i = 0;
- if (remove)
+ if (flags & UPDATE_MIME_REMOVE)
list[i++] = g_strdup (desktop_id);
if (old_list)
{
g_strfreev (list);
}
-
+
g_strfreev (content_types);
data = g_key_file_to_data (key_file, &data_size, error);
}
static gboolean
+g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
+ const char *content_type,
+ GError **error)
+{
+ GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+
+ if (!g_desktop_app_info_ensure_saved (info, error))
+ return FALSE;
+
+ /* both add support for the content type and set as last used */
+ return update_mimeapps_list (info->desktop_id, content_type,
+ UPDATE_MIME_SET_NON_DEFAULT |
+ UPDATE_MIME_SET_LAST_USED,
+ error);
+}
+
+static gboolean
g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
const char *content_type,
GError **error)
if (!g_desktop_app_info_ensure_saved (info, error))
return FALSE;
- return update_mimeapps_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
+ return update_mimeapps_list (info->desktop_id, content_type,
+ UPDATE_MIME_SET_DEFAULT,
+ error);
}
static void
if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
return FALSE;
- return update_mimeapps_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
+ return update_mimeapps_list (info->desktop_id, content_type,
+ UPDATE_MIME_SET_NON_DEFAULT,
+ error);
}
static gboolean
if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
return FALSE;
- return update_mimeapps_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
+ return update_mimeapps_list (info->desktop_id, content_type,
+ UPDATE_MIME_REMOVE,
+ error);
}
static gboolean
g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
+ if (info->fullname != NULL)
+ g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+ FULL_NAME_KEY, info->fullname);
+
g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
{
if (g_remove (info->filename) == 0)
{
- update_mimeapps_list (info->desktop_id, NULL, FALSE, FALSE, FALSE, NULL);
+ update_mimeapps_list (info->desktop_id, NULL,
+ UPDATE_MIME_NONE,
+ NULL);
g_free (info->filename);
info->filename = NULL;
/**
* g_app_info_create_from_commandline:
* @commandline: the commandline to use
- * @application_name: the application name, or %NULL to use @commandline
+ * @application_name: (allow-none): the application name, or %NULL to use @commandline
* @flags: flags that can specify details of the created #GAppInfo
* @error: a #GError location to store the error occuring, %NULL to ignore.
*
* Creates a new #GAppInfo from the given information.
*
- * Returns: new #GAppInfo for given command.
+ * Returns: (transfer full): new #GAppInfo for given command.
**/
GAppInfo *
g_app_info_create_from_commandline (const char *commandline,
char *basename;
GDesktopAppInfo *info;
+ g_return_val_if_fail (commandline, NULL);
+
info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
info->filename = NULL;
info->desktop_id = NULL;
info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
- info->startup_notify = FALSE;
+ info->startup_notify = flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION;
info->hidden = FALSE;
if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
info->exec = g_strconcat (commandline, " %u", NULL);
{
/* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
split = g_strsplit (commandline, " ", 2);
- basename = g_path_get_basename (split[0]);
+ basename = split[0] ? g_path_get_basename (split[0]) : NULL;
g_strfreev (split);
info->name = basename;
if (info->name == NULL)
iface->can_delete = g_desktop_app_info_can_delete;
iface->do_delete = g_desktop_app_info_delete;
iface->get_commandline = g_desktop_app_info_get_commandline;
+ iface->get_display_name = g_desktop_app_info_get_display_name;
+ iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
}
static gboolean
return FALSE;
}
+/**
+ * g_app_info_get_recommended_for_type:
+ * @content_type: the content type to find a #GAppInfo for
+ *
+ * Gets a list of recommended #GAppInfos for a given content type, i.e.
+ * those applications which claim to support the given content type exactly,
+ * and not by MIME type subclassing.
+ * Note that the first application of the list is the last used one, i.e.
+ * the last one for which #g_app_info_set_as_last_used_for_type has been
+ * called.
+ *
+ * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
+ * for given @content_type or %NULL on error.
+ *
+ * Since: 2.28
+ **/
+GList *
+g_app_info_get_recommended_for_type (const gchar *content_type)
+{
+ GList *desktop_entries, *l;
+ GList *infos;
+ GDesktopAppInfo *info;
+
+ g_return_val_if_fail (content_type != NULL, NULL);
+
+ desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
+
+ infos = NULL;
+ for (l = desktop_entries; l != NULL; l = l->next)
+ {
+ char *desktop_entry = l->data;
+
+ info = g_desktop_app_info_new (desktop_entry);
+ if (info)
+ {
+ if (app_info_in_list (G_APP_INFO (info), infos))
+ g_object_unref (info);
+ else
+ infos = g_list_prepend (infos, info);
+ }
+ g_free (desktop_entry);
+ }
+
+ g_list_free (desktop_entries);
+
+ return g_list_reverse (infos);
+}
+
+/**
+ * g_app_info_get_fallback_for_type:
+ * @content_type: the content type to find a #GAppInfo for
+ *
+ * Gets a list of fallback #GAppInfos for a given content type, i.e.
+ * those applications which claim to support the given content type
+ * by MIME type subclassing and not directly.
+ *
+ * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
+ * for given @content_type or %NULL on error.
+ *
+ * Since: 2.28
+ **/
+GList *
+g_app_info_get_fallback_for_type (const gchar *content_type)
+{
+ GList *desktop_entries, *l;
+ GList *infos, *recommended_infos;
+ GDesktopAppInfo *info;
+
+ g_return_val_if_fail (content_type != NULL, NULL);
+
+ desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
+ recommended_infos = g_app_info_get_recommended_for_type (content_type);
+
+ infos = NULL;
+ for (l = desktop_entries; l != NULL; l = l->next)
+ {
+ char *desktop_entry = l->data;
+
+ info = g_desktop_app_info_new (desktop_entry);
+ if (info)
+ {
+ if (app_info_in_list (G_APP_INFO (info), infos) ||
+ app_info_in_list (G_APP_INFO (info), recommended_infos))
+ g_object_unref (info);
+ else
+ infos = g_list_prepend (infos, info);
+ }
+ g_free (desktop_entry);
+ }
+
+ g_list_free (desktop_entries);
+ g_list_free_full (recommended_infos, g_object_unref);
+
+ return g_list_reverse (infos);
+}
/**
* g_app_info_get_all_for_type:
* @content_type: the content type to find a #GAppInfo for
*
- * Gets a list of all #GAppInfo s for a given content type.
+ * Gets a list of all #GAppInfos for a given content type.
*
- * Returns: #GList of #GAppInfo s for given @content_type
- * or %NULL on error.
+ * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
+ * for given @content_type or %NULL on error.
**/
GList *
g_app_info_get_all_for_type (const char *content_type)
{
GList *desktop_entries, *l;
GList *infos;
+ char *user_default = NULL;
GDesktopAppInfo *info;
g_return_val_if_fail (content_type != NULL, NULL);
- desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL);
-
+ desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
infos = NULL;
+
+ /* put the user default in front of the list, for compatibility */
+ if (user_default != NULL)
+ {
+ info = g_desktop_app_info_new (user_default);
+
+ if (info != NULL)
+ infos = g_list_prepend (infos, info);
+ }
+
+ g_free (user_default);
+
for (l = desktop_entries; l != NULL; l = l->next)
{
char *desktop_entry = l->data;
* Removes all changes to the type associations done by
* g_app_info_set_as_default_for_type(),
* g_app_info_set_as_default_for_extension(),
- * g_app_info_add_supports_type() of g_app_info_remove_supports_type().
+ * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
*
* Since: 2.20
*/
void
g_app_info_reset_type_associations (const char *content_type)
{
- update_mimeapps_list (NULL, content_type, FALSE, FALSE, FALSE, NULL);
+ update_mimeapps_list (NULL, content_type,
+ UPDATE_MIME_NONE,
+ NULL);
}
/**
* @must_support_uris: if %TRUE, the #GAppInfo is expected to
* support URIs
*
- * Gets the #GAppInfo that correspond to a given content type.
+ * Gets the #GAppInfo that corresponds to a given content type.
*
- * Returns: #GAppInfo for given @content_type or %NULL on error.
+ * Returns: (transfer full): #GAppInfo for given @content_type or
+ * %NULL on error.
**/
GAppInfo *
g_app_info_get_default_for_type (const char *content_type,
gboolean must_support_uris)
{
GList *desktop_entries, *l;
+ char *user_default = NULL;
GAppInfo *info;
g_return_val_if_fail (content_type != NULL, NULL);
- desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL);
+ desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
info = NULL;
+
+ if (user_default != NULL)
+ {
+ info = (GAppInfo *) g_desktop_app_info_new (user_default);
+
+ if (info)
+ {
+ if (must_support_uris && !g_app_info_supports_uris (info))
+ {
+ g_object_unref (info);
+ info = NULL;
+ }
+ }
+ }
+
+ g_free (user_default);
+
+ if (info != NULL)
+ {
+ g_list_free_full (desktop_entries, g_free);
+ return info;
+ }
+
+ /* pick the first from the other list that matches our URI
+ * requirements.
+ */
for (l = desktop_entries; l != NULL; l = l->next)
{
char *desktop_entry = l->data;
}
}
- g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
- g_list_free (desktop_entries);
-
+ g_list_free_full (desktop_entries, g_free);
+
return info;
}
* of the URI, up to but not including the ':', e.g. "http",
* "ftp" or "sip".
*
- * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
+ * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
**/
GAppInfo *
g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
{
- static gsize lookup = 0;
-
- if (g_once_init_enter (&lookup))
- {
- gsize setup_value = 1;
- GDesktopAppInfoLookup *lookup_instance;
- const char *use_this;
- GIOExtensionPoint *ep;
- GIOExtension *extension;
- GList *l;
+ GAppInfo *app_info;
+ char *content_type, *scheme_down;
- use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
-
- /* Ensure vfs in modules loaded */
- _g_io_modules_ensure_loaded ();
-
- ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
-
- lookup_instance = NULL;
- if (use_this)
- {
- extension = g_io_extension_point_get_extension_by_name (ep, use_this);
- if (extension)
- lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
- }
-
- if (lookup_instance == NULL)
- {
- for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
- {
- extension = l->data;
- lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
- if (lookup_instance != NULL)
- break;
- }
- }
-
- if (lookup_instance != NULL)
- setup_value = (gsize)lookup_instance;
-
- g_once_init_leave (&lookup, setup_value);
- }
-
- if (lookup == 1)
- return NULL;
+ scheme_down = g_ascii_strdown (uri_scheme, -1);
+ content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
+ g_free (scheme_down);
+ app_info = g_app_info_get_default_for_type (content_type, FALSE);
+ g_free (content_type);
- return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
- uri_scheme);
+ return app_info;
}
-
static void
get_apps_from_dir (GHashTable *apps,
const char *dirname,
if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
{
appinfo = g_desktop_app_info_new_from_filename (filename);
+ hidden = FALSE;
if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
{
* The returned list does not include applications which have
* the <literal>Hidden</literal> key set.
*
- * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
+ * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
**/
GList *
g_app_info_get_all (void)
GHashTable *defaults_list_map;
GHashTable *mimeapps_list_added_map;
GHashTable *mimeapps_list_removed_map;
+ GHashTable *mimeapps_list_defaults_map;
time_t mime_info_cache_timestamp;
time_t defaults_list_timestamp;
time_t mimeapps_list_timestamp;
gchar *filename, **mime_types;
char *unaliased_type;
char **desktop_file_ids;
+ char *desktop_id;
int i;
struct stat buf;
dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, (GDestroyNotify)g_strfreev);
+ if (dir->mimeapps_list_defaults_map != NULL)
+ g_hash_table_destroy (dir->mimeapps_list_defaults_map);
+ dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
+ g_free, g_free);
+
key_file = g_key_file_new ();
filename = g_build_filename (dir->path, "mimeapps.list", NULL);
g_strfreev (mime_types);
}
+ mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
+ NULL, NULL);
+ if (mime_types != NULL)
+ {
+ for (i = 0; mime_types[i] != NULL; i++)
+ {
+ desktop_id = g_key_file_get_string (key_file,
+ DEFAULT_APPLICATIONS_GROUP,
+ mime_types[i],
+ NULL);
+ if (desktop_id == NULL)
+ continue;
+
+ unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
+ g_hash_table_replace (dir->mimeapps_list_defaults_map,
+ unaliased_type,
+ desktop_id);
+ }
+
+ g_strfreev (mime_types);
+ }
+
g_key_file_free (key_file);
return;
g_hash_table_destroy (dir->mimeapps_list_removed_map);
dir->mimeapps_list_removed_map = NULL;
}
-
+
+ if (dir->mimeapps_list_defaults_map != NULL)
+ {
+ g_hash_table_destroy (dir->mimeapps_list_defaults_map);
+ dir->mimeapps_list_defaults_map = NULL;
+ }
+
g_free (dir);
}
for (i = 0; new_desktop_file_ids[i] != NULL; i++)
{
- if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
+ if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
desktop_file_ids = g_list_append (desktop_file_ids,
g_strdup (new_desktop_file_ids[i]));
}
* to handle @mime_type.
*/
static GList *
-get_all_desktop_entries_for_mime_type (const char *base_mime_type,
- const char **except)
+get_all_desktop_entries_for_mime_type (const char *base_mime_type,
+ const char **except,
+ gboolean include_fallback,
+ char **explicit_default)
{
GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
MimeInfoCacheDir *dir;
- char *mime_type;
+ char *mime_type, *default_entry = NULL;
+ const char *entry;
char **mime_types;
char **default_entries;
char **removed_associations;
mime_info_cache_init ();
- /* collect all ancestors */
- mime_types = _g_unix_content_type_get_parents (base_mime_type);
- array = g_ptr_array_new ();
- for (i = 0; mime_types[i]; i++)
- g_ptr_array_add (array, mime_types[i]);
- g_free (mime_types);
- for (i = 0; i < array->len; i++)
+ if (include_fallback)
{
- anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
- for (j = 0; anc[j]; j++)
- {
- for (k = 0; k < array->len; k++)
- {
- if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
- break;
- }
- if (k == array->len) /* not found */
- g_ptr_array_add (array, g_strdup (anc[j]));
- }
- g_strfreev (anc);
+ /* collect all ancestors */
+ mime_types = _g_unix_content_type_get_parents (base_mime_type);
+ array = g_ptr_array_new ();
+ for (i = 0; mime_types[i]; i++)
+ g_ptr_array_add (array, mime_types[i]);
+ g_free (mime_types);
+ for (i = 0; i < array->len; i++)
+ {
+ anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
+ for (j = 0; anc[j]; j++)
+ {
+ for (k = 0; k < array->len; k++)
+ {
+ if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
+ break;
+ }
+ if (k == array->len) /* not found */
+ g_ptr_array_add (array, g_strdup (anc[j]));
+ }
+ g_strfreev (anc);
+ }
+ g_ptr_array_add (array, NULL);
+ mime_types = (char **)g_ptr_array_free (array, FALSE);
+ }
+ else
+ {
+ mime_types = g_malloc0 (2 * sizeof (gchar *));
+ mime_types[0] = g_strdup (base_mime_type);
+ mime_types[1] = NULL;
}
- g_ptr_array_add (array, NULL);
- mime_types = (char **)g_ptr_array_free (array, FALSE);
G_LOCK (mime_info_cache);
{
mime_type = mime_types[i];
- /* Go through all apps listed as defaults */
+ /* Go through all apps listed in user and system dirs */
for (dir_list = mime_info_cache->dirs;
dir_list != NULL;
dir_list = dir_list->next)
{
dir = dir_list->data;
- /* First added associations from mimeapps.list */
+ /* Pick the explicit default application */
+ entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
+
+ if (entry != NULL)
+ {
+ /* Save the default entry if it's the first one we encounter */
+ if (default_entry == NULL)
+ default_entry = g_strdup (entry);
+ }
+
+ /* Then added associations from mimeapps.list */
default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
- desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
+ desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
/* Then removed associations from mimeapps.list */
removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
g_strfreev (mime_types);
+ if (explicit_default != NULL)
+ *explicit_default = default_entry;
+ else
+ g_free (default_entry);
+
g_list_foreach (removed_entries, (GFunc)g_free, NULL);
g_list_free (removed_entries);
-
+
desktop_entries = g_list_reverse (desktop_entries);
return desktop_entries;
/* GDesktopAppInfoLookup interface: */
-static void g_desktop_app_info_lookup_base_init (gpointer g_class);
-static void g_desktop_app_info_lookup_class_init (gpointer g_class,
- gpointer class_data);
-
-GType
-g_desktop_app_info_lookup_get_type (void)
-{
- static volatile gsize g_define_type_id__volatile = 0;
-
- if (g_once_init_enter (&g_define_type_id__volatile))
- {
- const GTypeInfo desktop_app_info_lookup_info =
- {
- sizeof (GDesktopAppInfoLookupIface), /* class_size */
- g_desktop_app_info_lookup_base_init, /* base_init */
- NULL, /* base_finalize */
- g_desktop_app_info_lookup_class_init,
- NULL, /* class_finalize */
- NULL, /* class_data */
- 0,
- 0, /* n_preallocs */
- NULL
- };
- GType g_define_type_id =
- g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
- &desktop_app_info_lookup_info, 0);
-
- g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
-
- g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
- }
-
- return g_define_type_id__volatile;
-}
-
-static void
-g_desktop_app_info_lookup_class_init (gpointer g_class,
- gpointer class_data)
-{
-}
+typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
+G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
static void
-g_desktop_app_info_lookup_base_init (gpointer g_class)
+g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
{
}
* in a GIO module. There is no reason for applications to use it
* directly. Applications should use g_app_info_get_default_for_uri_scheme().
*
- * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
+ * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
+ *
+ * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
*/
GAppInfo *
g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
}
-
-#define __G_DESKTOP_APP_INFO_C__
-#include "gioaliasdef.c"