[gnome-software] plugins: Add a datadir-filename plugin to map application id's to the correct desktop filename



commit a67b61d813e5404990bca989c4d0ab2d1b23ca36
Author: Richard Hughes <richard hughsie com>
Date:   Thu Mar 7 17:57:21 2013 +0000

    plugins: Add a datadir-filename plugin to map application id's to the correct desktop filename
    
    This is only required if AppStream data is not available.

 src/gs-main.c                            |    1 +
 src/gs-self-test.c                       |    9 ++
 src/plugins/Makefile.am                  |    6 ++
 src/plugins/README                       |    9 ++
 src/plugins/gs-plugin-datadir-filename.c |  138 ++++++++++++++++++++++++++++++
 5 files changed, 163 insertions(+), 0 deletions(-)
---
diff --git a/src/gs-main.c b/src/gs-main.c
index 3837ec5..4f77f93 100644
--- a/src/gs-main.c
+++ b/src/gs-main.c
@@ -1628,6 +1628,7 @@ main (int argc, char **argv)
        gs_plugin_loader_set_enabled (priv->plugin_loader, "packagekit", TRUE);
        gs_plugin_loader_set_enabled (priv->plugin_loader, "desktopdb", TRUE);
        gs_plugin_loader_set_enabled (priv->plugin_loader, "datadir-apps", TRUE);
+       gs_plugin_loader_set_enabled (priv->plugin_loader, "datadir-filename", TRUE);
 
        /* wait */
        status = g_application_run (G_APPLICATION (priv->application), argc, argv);
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index af00f01..62ab096 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -69,6 +69,10 @@ gs_plugin_loader_func (void)
        g_assert (ret);
        ret = gs_plugin_loader_set_enabled (loader, "hardcoded-ratings", TRUE);
        g_assert (ret);
+       ret = gs_plugin_loader_set_enabled (loader, "datadir-filename", TRUE);
+       g_assert (ret);
+       ret = gs_plugin_loader_set_enabled (loader, "datadir-apps", TRUE);
+       g_assert (ret);
        ret = gs_plugin_loader_set_enabled (loader, "notgoingtoexist", TRUE);
        g_assert (!ret);
 
@@ -79,6 +83,11 @@ gs_plugin_loader_func (void)
        app = g_list_nth_data (list, 0);
        g_assert_cmpstr (gs_app_get_id (app), ==, "gnome-boxes");
        g_assert_cmpstr (gs_app_get_name (app), ==, "Boxes");
+
+       app = g_list_nth_data (list, 1);
+       g_assert_cmpstr (gs_app_get_id (app), ==, "gedit");
+       g_assert_cmpstr (gs_app_get_summary (app), ==, "Edit text files");
+
        g_list_free_full (list, (GDestroyNotify) g_object_unref);
 
        /* get updates */
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index 429f651..79f3723 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -19,6 +19,7 @@ AM_CPPFLAGS =                                         \
 plugindir = $(libdir)/gs-plugins
 plugin_LTLIBRARIES =                                   \
        libgs_plugin_datadir_apps.la                    \
+       libgs_plugin_datadir_filename.la                \
        libgs_plugin_desktopdb.la                       \
        libgs_plugin_dummy.la                           \
        libgs_plugin_hardcoded-kind.la                  \
@@ -61,4 +62,9 @@ libgs_plugin_datadir_apps_la_LIBADD = $(GS_PLUGIN_LIBS) $(GTK_LIBS)
 libgs_plugin_datadir_apps_la_LDFLAGS = -module -avoid-version
 libgs_plugin_datadir_apps_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(WARNINGFLAGS_C)
 
+libgs_plugin_datadir_filename_la_SOURCES = gs-plugin-datadir-filename.c
+libgs_plugin_datadir_filename_la_LIBADD = $(GS_PLUGIN_LIBS) $(GTK_LIBS)
+libgs_plugin_datadir_filename_la_LDFLAGS = -module -avoid-version
+libgs_plugin_datadir_filename_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(WARNINGFLAGS_C)
+
 -include $(top_srcdir)/git.mk
diff --git a/src/plugins/README b/src/plugins/README
index 76391cb..24af80c 100644
--- a/src/plugins/README
+++ b/src/plugins/README
@@ -106,3 +106,12 @@ Refines:   {datadir-desktop-filename}->[name]
                {datadir-desktop-filename}->[pixbuf]
                {datadir-desktop-filename}->[id]
                {datadir-desktop-filename}->[kind]
+
+== datadir-filename ==
+Overview:      Uses the existance of a files in /usr/share/applications
+               named $id.desktop to set the correct desktop filename.
+               This is useful if we just have a bare [id] from something like
+               AddPopular and want to get the details using 'datadir-apps'
+Methods:       <nothing>
+Requires:      <nothing>
+Refines:       [id]->{datadir-desktop-filename}
diff --git a/src/plugins/gs-plugin-datadir-filename.c b/src/plugins/gs-plugin-datadir-filename.c
new file mode 100644
index 0000000..3bbaf09
--- /dev/null
+++ b/src/plugins/gs-plugin-datadir-filename.c
@@ -0,0 +1,138 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <config.h>
+
+#include <gs-plugin.h>
+
+struct GsPluginPrivate {
+       GHashTable              *cache;
+};
+
+/**
+ * gs_plugin_get_name:
+ */
+const gchar *
+gs_plugin_get_name (void)
+{
+       return "datadir-filename";
+}
+
+/**
+ * gs_plugin_initialize:
+ */
+void
+gs_plugin_initialize (GsPlugin *plugin)
+{
+       /* create private area */
+       plugin->priv = GS_PLUGIN_GET_PRIVATE (GsPluginPrivate);
+       plugin->priv->cache = g_hash_table_new_full (g_str_hash,
+                                                    g_str_equal,
+                                                    g_free,
+                                                    g_free);
+}
+
+/**
+ * gs_plugin_get_priority:
+ */
+gdouble
+gs_plugin_get_priority (GsPlugin *plugin)
+{
+       return 1.1f;
+}
+
+/**
+ * gs_plugin_destroy:
+ */
+void
+gs_plugin_destroy (GsPlugin *plugin)
+{
+       g_hash_table_unref (plugin->priv->cache);
+}
+
+/**
+ * gs_plugin_datadir_filename_find:
+ */
+static const gchar *
+gs_plugin_datadir_filename_find (GsPlugin *plugin,
+                                GsApp *app)
+{
+       const gchar *id;
+       const gchar *path_tmp = NULL;
+       gboolean ret;
+       gchar *path = NULL;
+
+       /* try and get from cache */
+       id = gs_app_get_id (app);
+       ret = g_hash_table_lookup_extended (plugin->priv->cache,
+                                           id,
+                                           NULL,
+                                           (gpointer *) &path_tmp);
+       if (ret) {
+               g_debug ("found existing %s", id);
+               goto out;
+       }
+
+       /* find if the file exists */
+       path = g_strdup_printf ("/usr/share/applications/%s.desktop",
+                               gs_app_get_id (app));
+       if (g_file_test (path, G_FILE_TEST_EXISTS)) {
+               path_tmp = g_strdup (path);
+               g_hash_table_insert (plugin->priv->cache,
+                                    g_strdup (id),
+                                    (gpointer) path_tmp);
+       } else {
+               /* add an empty key to the cache to avoid stat'ing again */
+               g_hash_table_insert (plugin->priv->cache,
+                                    g_strdup (id),
+                                    NULL);
+       }
+out:
+       g_free (path);
+       return path_tmp;
+}
+
+/**
+ * gs_plugin_refine:
+ */
+gboolean
+gs_plugin_refine (GsPlugin *plugin, GList *list, GError **error)
+{
+       const gchar *tmp;
+       GList *l;
+       GsApp *app;
+
+       for (l = list; l != NULL; l = l->next) {
+               app = GS_APP (l->data);
+               if (gs_app_get_name (app) != NULL)
+                       continue;
+               tmp = gs_app_get_metadata_item (app, "datadir-desktop-filename");
+               if (tmp != NULL)
+                       continue;
+               tmp = gs_plugin_datadir_filename_find (plugin, app);
+               if (tmp != NULL) {
+                       gs_app_set_metadata (app,
+                                            "datadir-desktop-filename",
+                                            tmp);
+               }
+       }
+       return TRUE;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]