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_LOCK_DEFINE_STATIC (g_desktop_env);
-static gchar *g_desktop_env = NULL;
-
/* DesktopFileDir implementation {{{1 */
typedef struct
GHashTable *app_names;
GHashTable *mime_tweaks;
GHashTable *memory_index;
+ GHashTable *memory_implementations;
} DesktopFileDir;
static DesktopFileDir *desktop_file_dirs;
return (const gchar **) result;
}
+static const gchar * const *
+get_current_desktops (const gchar *value)
+{
+ static gchar **result;
+
+ if (g_once_init_enter (&result))
+ {
+ gchar **tmp;
+
+ if (!value)
+ value = g_getenv ("XDG_CURRENT_DESKTOP");
+
+ if (!value)
+ value = "";
+
+ tmp = g_strsplit (value, ":", 0);
+
+ g_once_init_leave (&result, tmp);
+ }
+
+ return (const gchar **) result;
+}
+
/*< internal >
* add_to_table_if_appropriate:
* @apps: a string to GDesktopAppInfo hash table
goto no_add;
/* Don't add duplicates already in the list */
- for (j = 0; strv[j]; j++)
+ for (j = 0; j < strv_len; j++)
if (g_str_equal (to_add[i], strv[j]))
goto no_add;
gpointer app, path;
dir->memory_index = memory_index_new ();
+ dir->memory_implementations = memory_index_new ();
/* Nothing to search? */
if (dir->app_names == NULL)
!g_key_file_get_boolean (key_file, "Desktop Entry", "Hidden", NULL))
{
/* Index the interesting keys... */
+ gchar **implements;
gint i;
for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++)
g_free (raw);
}
+
+ /* Make note of the Implements= line */
+ implements = g_key_file_get_string_list (key_file, "Desktop Entry", "Implements", NULL, NULL);
+ for (i = 0; implements && implements[i]; i++)
+ memory_index_add_token (dir->memory_implementations, implements[i], 0, app);
+ g_strfreev (implements);
}
g_key_file_free (key_file);
}
}
+static void
+desktop_file_dir_unindexed_get_implementations (DesktopFileDir *dir,
+ GList **results,
+ const gchar *interface)
+{
+ MemoryIndexEntry *mie;
+
+ if (!dir->memory_index)
+ desktop_file_dir_unindexed_setup_search (dir);
+
+ for (mie = g_hash_table_lookup (dir->memory_implementations, interface); mie; mie = mie->next)
+ *results = g_list_prepend (*results, g_strdup (mie->app_name));
+}
+
/* DesktopFileDir "API" {{{2 */
/*< internal >
dir->mime_tweaks = NULL;
}
+ if (dir->memory_implementations)
+ {
+ g_hash_table_unref (dir->memory_implementations);
+ dir->memory_implementations = NULL;
+ }
+
dir->is_setup = FALSE;
}
desktop_file_dir_unindexed_search (dir, search_token);
}
+static void
+desktop_file_dir_get_implementations (DesktopFileDir *dir,
+ GList **results,
+ const gchar *interface)
+{
+ desktop_file_dir_unindexed_get_implementations (dir, results, interface);
+}
+
/* Lock/unlock and global setup API {{{2 */
static void
/**
* g_desktop_app_info_get_show_in:
* @info: a #GDesktopAppInfo
- * @desktop_env: a string specifying a desktop name
+ * @desktop_env: (nullable): a string specifying a desktop name
*
* Checks if the application info should be shown in menus that list available
* applications for a specific name of the desktop, based on the
* `OnlyShowIn` and `NotShowIn` keys.
*
- * If @desktop_env is %NULL, then the name of the desktop set with
- * g_desktop_app_info_set_desktop_env() is used.
+ * @desktop_env should typically be given as %NULL, in which case the
+ * `XDG_CURRENT_DESKTOP` environment variable is consulted. If you want
+ * to override the default mechanism then you may specify @desktop_env,
+ * but this is not recommended.
*
* Note that g_app_info_should_show() for @info will include this check (with
* %NULL for @desktop_env) as well as additional checks.
g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
const gchar *desktop_env)
{
- gboolean found;
- int i;
+ const gchar *specified_envs[] = { desktop_env, NULL };
+ const gchar * const *envs;
+ gint i;
g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
- if (!desktop_env) {
- G_LOCK (g_desktop_env);
- desktop_env = g_desktop_env;
- G_UNLOCK (g_desktop_env);
- }
+ if (desktop_env)
+ envs = specified_envs;
+ else
+ envs = get_current_desktops (NULL);
- if (info->only_show_in)
+ for (i = 0; envs[i]; i++)
{
- if (desktop_env == NULL)
- return FALSE;
+ gint j;
- found = FALSE;
- for (i = 0; info->only_show_in[i] != NULL; i++)
- {
- if (strcmp (info->only_show_in[i], desktop_env) == 0)
- {
- found = TRUE;
- break;
- }
- }
- if (!found)
- return FALSE;
- }
+ if (info->only_show_in)
+ for (j = 0; info->only_show_in[j]; j++)
+ if (g_str_equal (info->only_show_in[j], envs[i]))
+ return TRUE;
- if (info->not_show_in && desktop_env)
- {
- for (i = 0; info->not_show_in[i] != NULL; i++)
- {
- if (strcmp (info->not_show_in[i], desktop_env) == 0)
+ if (info->not_show_in)
+ for (j = 0; info->not_show_in[j]; j++)
+ if (g_str_equal (info->not_show_in[j], envs[i]))
return FALSE;
- }
}
- return TRUE;
+ return info->only_show_in == NULL;
}
/* Launching... {{{2 */
* `OnlyShowIn` and `NotShowIn`
* desktop entry fields.
*
- * The
- * [Desktop Menu specification](http://standards.freedesktop.org/menu-spec/latest/)
- * recognizes the following:
- * - GNOME
- * - KDE
- * - ROX
- * - XFCE
- * - LXDE
- * - Unity
- * - Old
- *
* Should be called only once; subsequent calls are ignored.
+ *
+ * Deprecated:2.42:do not use this API. Since 2.42 the value of the
+ * `XDG_CURRENT_DESKTOP` environment variable will be used.
*/
void
g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
{
- G_LOCK (g_desktop_env);
- if (!g_desktop_env)
- g_desktop_env = g_strdup (desktop_env);
- G_UNLOCK (g_desktop_env);
+ get_current_desktops (desktop_env);
}
static gboolean
/* "Get all" API {{{2 */
/**
+ * g_desktop_app_info_get_implementations:
+ * @interface: the name of the interface
+ *
+ * Gets all applications that implement @interface.
+ *
+ * An application implements an interface if that interface is listed in
+ * the Implements= line of the desktop file of the application.
+ *
+ * Since: 2.42
+ **/
+GList *
+g_desktop_app_info_get_implementations (const gchar *interface)
+{
+ GList *result = NULL;
+ GList **ptr;
+ gint i;
+
+ desktop_file_dirs_lock ();
+
+ for (i = 0; i < n_desktop_file_dirs; i++)
+ desktop_file_dir_get_implementations (&desktop_file_dirs[i], &result, interface);
+
+ desktop_file_dirs_unlock ();
+
+ ptr = &result;
+ while (*ptr)
+ {
+ gchar *name = (*ptr)->data;
+ GDesktopAppInfo *app;
+
+ app = g_desktop_app_info_new (name);
+ g_free (name);
+
+ if (app)
+ {
+ (*ptr)->data = app;
+ ptr = &(*ptr)->next;
+ }
+ else
+ *ptr = g_list_delete_link (*ptr, *ptr);
+ }
+
+ return result;
+}
+
+/**
* g_desktop_app_info_search:
* @search_string: the search string to use
*