[gnome-software] plugins: Add a desktopdb plugin to map installed package names to application filenames



commit bfdbaf1dd168bb7029fdcfe1fceae3a6c4033abd
Author: Richard Hughes <richard hughsie com>
Date:   Thu Mar 7 10:52:47 2013 +0000

    plugins: Add a desktopdb plugin to map installed package names to application filenames
    
    This is only required if AppStream data is not available, and obviously only
    works with installed packages.

 src/gs-main.c                     |    1 +
 src/plugins/Makefile.am           |    6 ++
 src/plugins/README                |    7 ++
 src/plugins/gs-plugin-desktopdb.c |  141 +++++++++++++++++++++++++++++++++++++
 4 files changed, 155 insertions(+), 0 deletions(-)
---
diff --git a/src/gs-main.c b/src/gs-main.c
index 692e1b3..43097e6 100644
--- a/src/gs-main.c
+++ b/src/gs-main.c
@@ -1626,6 +1626,7 @@ main (int argc, char **argv)
        gs_plugin_loader_set_enabled (priv->plugin_loader, "hardcoded-popular", TRUE);
        gs_plugin_loader_set_enabled (priv->plugin_loader, "hardcoded-ratings", TRUE);
        gs_plugin_loader_set_enabled (priv->plugin_loader, "packagekit", TRUE);
+       gs_plugin_loader_set_enabled (priv->plugin_loader, "desktopdb", TRUE);
 
        /* wait */
        status = g_application_run (G_APPLICATION (priv->application), argc, argv);
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index 25476d8..e00b7bd 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -18,6 +18,7 @@ AM_CPPFLAGS =                                         \
 
 plugindir = $(libdir)/gs-plugins
 plugin_LTLIBRARIES =                                   \
+       libgs_plugin_desktopdb.la                       \
        libgs_plugin_dummy.la                           \
        libgs_plugin_hardcoded-kind.la                  \
        libgs_plugin_hardcoded-popular.la               \
@@ -49,4 +50,9 @@ libgs_plugin_packagekit_la_LIBADD = $(GS_PLUGIN_LIBS) $(PACKAGEKIT_LIBS)
 libgs_plugin_packagekit_la_LDFLAGS = -module -avoid-version
 libgs_plugin_packagekit_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(WARNINGFLAGS_C)
 
+libgs_plugin_desktopdb_la_SOURCES = gs-plugin-desktopdb.c
+libgs_plugin_desktopdb_la_LIBADD = $(GS_PLUGIN_LIBS) $(PACKAGEKIT_LIBS)
+libgs_plugin_desktopdb_la_LDFLAGS = -module -avoid-version
+libgs_plugin_desktopdb_la_CFLAGS = $(GS_PLUGIN_CFLAGS) $(WARNINGFLAGS_C)
+
 -include $(top_srcdir)/git.mk
diff --git a/src/plugins/README b/src/plugins/README
index 79220a5..b7de2c7 100644
--- a/src/plugins/README
+++ b/src/plugins/README
@@ -88,3 +88,10 @@ Refines:     <nothing>
 Sets:          {package-id}
                {package-name}
                {package-summary}
+
+== desktopdb ==
+Overview:      Uses the PackageKit desktop.db cache to map package names to
+               desktop names.
+Methods:       <nothing>
+Requires:      <nothing>
+Refines:       {package-name}->{datadir-desktop-filename}
diff --git a/src/plugins/gs-plugin-desktopdb.c b/src/plugins/gs-plugin-desktopdb.c
new file mode 100644
index 0000000..662dd74
--- /dev/null
+++ b/src/plugins/gs-plugin-desktopdb.c
@@ -0,0 +1,141 @@
+/* -*- 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>
+
+#define I_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE
+#include <packagekit-glib2/packagekit.h>
+
+#include <gs-plugin.h>
+
+struct GsPluginPrivate {
+       PkDesktop               *desktop;
+       gboolean                 loaded;
+};
+
+/**
+ * gs_plugin_get_name:
+ */
+const gchar *
+gs_plugin_get_name (void)
+{
+       return "desktopdb";
+}
+
+/**
+ * gs_plugin_initialize:
+ */
+void
+gs_plugin_initialize (GsPlugin *plugin)
+{
+       /* create private area */
+       plugin->priv = GS_PLUGIN_GET_PRIVATE (GsPluginPrivate);
+       plugin->priv->desktop = pk_desktop_new ();
+}
+
+/**
+ * gs_plugin_get_priority:
+ */
+gdouble
+gs_plugin_get_priority (GsPlugin *plugin)
+{
+       return 5.0f;
+}
+
+/**
+ * gs_plugin_destroy:
+ */
+void
+gs_plugin_destroy (GsPlugin *plugin)
+{
+       g_object_unref (plugin->priv->desktop);
+}
+
+/**
+ * gs_plugin_desktopdb_set_metadata:
+ */
+static void
+gs_plugin_desktopdb_set_metadata (GsPlugin *plugin,
+                                 GsApp *app,
+                                 const gchar *pkg_name)
+{
+       const gchar *desktop_file;
+       GError *error = NULL;
+       GPtrArray *files = NULL;
+
+       /* try to get the list of desktop files for this package */
+       files = pk_desktop_get_shown_for_package (plugin->priv->desktop,
+                                                 pkg_name,
+                                                 &error);
+       if (files == NULL) {
+               g_warning ("failed to get files for %s: %s",
+                          pkg_name, error->message);
+               g_error_free (error);
+               goto out;
+       }
+       if (files->len == 0) {
+               g_debug ("not an application %s", pkg_name);
+               goto out;
+       }
+
+       /* add just the first desktop file */
+       desktop_file = g_ptr_array_index (files, 0);
+       gs_app_set_metadata (app,
+                            "datadir-desktop-filename",
+                            desktop_file);
+out:
+       if (files != NULL)
+               g_ptr_array_unref (files);
+}
+
+/**
+ * gs_plugin_refine:
+ */
+gboolean
+gs_plugin_refine (GsPlugin *plugin, GList *list, GError **error)
+{
+       GsApp *app;
+       GList *l;
+       const gchar *tmp;
+       gboolean ret = TRUE;
+
+       /* not loaded yet */
+       if (!plugin->priv->loaded) {
+               ret = pk_desktop_open_database (plugin->priv->desktop, error);
+               if (!ret)
+                       goto out;
+       }
+
+       /* can we convert a package to an application */
+       for (l = list; l != NULL; l = l->next) {
+               app = GS_APP (l->data);
+               if (gs_app_get_kind (app) != GS_APP_KIND_PACKAGE)
+                       continue;
+               if (gs_app_get_metadata_item (app, "datadir-desktop-filename") != NULL)
+                       continue;
+               tmp = gs_app_get_metadata_item (app, "package-name");
+               if (tmp == NULL)
+                       continue;
+               gs_plugin_desktopdb_set_metadata (plugin, app, tmp);
+       }
+out:
+       return ret;
+}


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