[gnome-software] Add a simple plugin to refine the hardcoded name to a package name



commit 04922b14d31f5e397bc60fa85da63fa86f7fd495
Author: Richard Hughes <richard hughsie com>
Date:   Fri Aug 23 13:26:00 2013 +0100

    Add a simple plugin to refine the hardcoded name to a package name
    
    This alows us to remove installed featured and picked applications without AppStream data.

 src/gs-main.c                             |    1 +
 src/gs-plugin-loader.c                    |    5 +-
 src/plugins/Makefile.am                   |    6 +
 src/plugins/README.md                     |    9 +
 src/plugins/gs-plugin-packagekit-refine.c |  228 +++++++++++++++++++++++++++++
 5 files changed, 247 insertions(+), 2 deletions(-)
---
diff --git a/src/gs-main.c b/src/gs-main.c
index 0b0565c..4710a91 100644
--- a/src/gs-main.c
+++ b/src/gs-main.c
@@ -510,6 +510,7 @@ main (int argc, char **argv)
        gs_plugin_loader_set_enabled (priv->plugin_loader, "hardcoded-screenshots", TRUE);
        gs_plugin_loader_set_enabled (priv->plugin_loader, "local-ratings", TRUE);
        gs_plugin_loader_set_enabled (priv->plugin_loader, "packagekit", TRUE);
+       gs_plugin_loader_set_enabled (priv->plugin_loader, "packagekit-refine", 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);
diff --git a/src/gs-plugin-loader.c b/src/gs-plugin-loader.c
index e02b18c..24a3038 100644
--- a/src/gs-plugin-loader.c
+++ b/src/gs-plugin-loader.c
@@ -852,8 +852,9 @@ gs_plugin_loader_run_action (GsPluginLoader *plugin_loader,
                        if (g_error_matches (error_local,
                                             GS_PLUGIN_ERROR,
                                             GS_PLUGIN_ERROR_NOT_SUPPORTED)) {
-                               g_debug ("not supported for plugin %s",
-                                        plugin->name);
+                               g_debug ("not supported for plugin %s: %s",
+                                        plugin->name,
+                                        error_local->message);
                                g_clear_error (&error_local);
                        } else {
                                g_propagate_error (error, error_local);
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index 3e907fa..167a4e3 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -37,6 +37,7 @@ plugin_LTLIBRARIES =                                  \
        libgs_plugin_hardcoded-ratings.la               \
        libgs_plugin_hardcoded-screenshots.la           \
        libgs_plugin_local-ratings.la                   \
+       libgs_plugin_packagekit-refine.la               \
        libgs_plugin_packagekit.la
 
 libgs_plugin_dummy_la_SOURCES = gs-plugin-dummy.c
@@ -84,6 +85,11 @@ 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_packagekit_refine_la_SOURCES = gs-plugin-packagekit-refine.c
+libgs_plugin_packagekit_refine_la_LIBADD = $(GS_PLUGIN_LIBS) $(PACKAGEKIT_LIBS)
+libgs_plugin_packagekit_refine_la_LDFLAGS = -module -avoid-version
+libgs_plugin_packagekit_refine_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
diff --git a/src/plugins/README.md b/src/plugins/README.md
index 87eb173..8b80a20 100644
--- a/src/plugins/README.md
+++ b/src/plugins/README.md
@@ -110,6 +110,15 @@ Requires:    | `{package-id}`
 Refines:     | `nothing`
 Refines:     | `{package-id}`, `{package-name}`, `{package-summary}`, `{update-name}`, `{update-details}`
 
+### packagekit-refine ###
+Uses the system PackageKit instance to return convert filenames to package-ids.
+
+Overview:    | <p>
+-------------|---
+Methods:     | `nothing`
+Requires:    | `{datadir-desktop-filename}`
+Refines:     | `{package-id}`, `[installed]`
+
 ### desktopdb ###
 Uses the PackageKit desktop.db cache to map package names to, desktop names.
 
diff --git a/src/plugins/gs-plugin-packagekit-refine.c b/src/plugins/gs-plugin-packagekit-refine.c
new file mode 100644
index 0000000..a1add54
--- /dev/null
+++ b/src/plugins/gs-plugin-packagekit-refine.c
@@ -0,0 +1,228 @@
+/* -*- 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 {
+       PkClient                *client;
+};
+
+/**
+ * gs_plugin_get_name:
+ */
+const gchar *
+gs_plugin_get_name (void)
+{
+       return "packagekit-refine";
+}
+
+/**
+ * gs_plugin_initialize:
+ */
+void
+gs_plugin_initialize (GsPlugin *plugin)
+{
+       /* create private area */
+       plugin->priv = GS_PLUGIN_GET_PRIVATE (GsPluginPrivate);
+       plugin->priv->client = pk_client_new ();
+       g_object_set (plugin->priv->client,
+                     "background", FALSE,
+                     NULL);
+}
+
+/**
+ * gs_plugin_get_priority:
+ */
+gdouble
+gs_plugin_get_priority (GsPlugin *plugin)
+{
+       return -150.0f;
+}
+
+/**
+ * gs_plugin_destroy:
+ */
+void
+gs_plugin_destroy (GsPlugin *plugin)
+{
+       g_object_unref (plugin->priv->client);
+}
+
+/**
+ * gs_plugin_packagekit_progress_cb:
+ **/
+static void
+gs_plugin_packagekit_progress_cb (PkProgress *progress,
+                                 PkProgressType type,
+                                 gpointer user_data)
+{
+       GsPluginStatus plugin_status = GS_PLUGIN_STATUS_UNKNOWN;
+       PkStatusEnum status;
+       GsPlugin *plugin = GS_PLUGIN (user_data);
+
+       if (type != PK_PROGRESS_TYPE_STATUS)
+               return;
+       g_object_get (progress,
+                     "status", &status,
+                     NULL);
+
+       /* set label */
+       switch (status) {
+       case PK_STATUS_ENUM_SETUP:
+       case PK_STATUS_ENUM_FINISHED:
+       case PK_STATUS_ENUM_UNKNOWN:
+               break;
+       case PK_STATUS_ENUM_WAIT:
+       case PK_STATUS_ENUM_WAITING_FOR_LOCK:
+       case PK_STATUS_ENUM_WAITING_FOR_AUTH:
+               plugin_status = GS_PLUGIN_STATUS_WAITING;
+               break;
+       case PK_STATUS_ENUM_LOADING_CACHE:
+       case PK_STATUS_ENUM_TEST_COMMIT:
+               plugin_status = GS_PLUGIN_STATUS_SETUP;
+               break;
+       case PK_STATUS_ENUM_DOWNLOAD:
+       case PK_STATUS_ENUM_DOWNLOAD_REPOSITORY:
+       case PK_STATUS_ENUM_DOWNLOAD_PACKAGELIST:
+       case PK_STATUS_ENUM_DOWNLOAD_FILELIST:
+       case PK_STATUS_ENUM_DOWNLOAD_CHANGELOG:
+       case PK_STATUS_ENUM_DOWNLOAD_GROUP:
+       case PK_STATUS_ENUM_DOWNLOAD_UPDATEINFO:
+               plugin_status = GS_PLUGIN_STATUS_DOWNLOADING;
+               break;
+       case PK_STATUS_ENUM_QUERY:
+       case PK_STATUS_ENUM_INFO:
+       case PK_STATUS_ENUM_DEP_RESOLVE:
+               plugin_status = GS_PLUGIN_STATUS_QUERYING;
+               break;
+       default:
+               g_warning ("no mapping for %s",
+                          pk_status_enum_to_string (status));
+               break;
+       }
+       if (plugin_status != GS_PLUGIN_STATUS_UNKNOWN)
+               gs_plugin_status_update (plugin, NULL, plugin_status);
+}
+
+/**
+ * gs_plugin_packagekit_refine_app:
+ */
+static gboolean
+gs_plugin_packagekit_refine_app (GsPlugin *plugin, GsApp *app, GCancellable *cancellable, GError **error)
+{
+       const gchar *filename;
+       const gchar *to_array[] = { NULL, NULL };
+       gboolean ret = TRUE;
+       GPtrArray *array = NULL;
+       GPtrArray *packages = NULL;
+       PkError *error_code = NULL;
+       PkPackage *package;
+       PkResults *results = NULL;
+
+       filename = gs_app_get_metadata_item (app, "datadir-desktop-filename");
+       if (filename == NULL) {
+               g_warning ("refining %s without filename not supported -- "
+                          "perhaps not installed and no AppStream data",
+                          gs_app_get_id (app));
+               gs_app_set_state (app, GS_APP_STATE_UNKNOWN);
+               goto out;
+       }
+       to_array[0] = filename;
+       results = pk_client_search_files (plugin->priv->client,
+                                         pk_bitfield_from_enums (PK_FILTER_ENUM_INSTALLED, -1),
+                                         (gchar **) to_array,
+                                         cancellable,
+                                         gs_plugin_packagekit_progress_cb, plugin,
+                                         error);
+       if (results == NULL) {
+               ret = FALSE;
+               goto out;
+       }
+
+       /* check error code */
+       error_code = pk_results_get_error_code (results);
+       if (error_code != NULL) {
+               ret = FALSE;
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_FAILED,
+                            "failed to search files: %s, %s",
+                            pk_error_enum_to_string (pk_error_get_code (error_code)),
+                            pk_error_get_details (error_code));
+               goto out;
+       }
+
+       /* get results */
+       packages = pk_results_get_package_array (results);
+       if (packages->len != 1) {
+               ret = FALSE;
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_FAILED,
+                            "failed to to find one package for: %s, %s [%i]",
+                            gs_app_get_id (app), filename, packages->len);
+               goto out;
+       }
+       package = g_ptr_array_index (packages, 0);
+       gs_app_set_metadata (app, "package-id", pk_package_get_id (package));
+       gs_app_set_state (app, GS_APP_STATE_INSTALLED);
+out:
+       if (packages != NULL)
+               g_ptr_array_unref (packages);
+       if (error_code != NULL)
+               g_object_unref (error_code);
+       if (array != NULL)
+               g_ptr_array_unref (array);
+       if (results != NULL)
+               g_object_unref (results);
+       return ret;
+}
+
+/**
+ * gs_plugin_refine:
+ */
+gboolean
+gs_plugin_refine (GsPlugin *plugin,
+                 GList *list,
+                 GCancellable *cancellable,
+                 GError **error)
+{
+       gboolean ret = TRUE;
+       GList *l;
+       GsApp *app;
+
+       /* add any missing ratings data */
+       for (l = list; l != NULL; l = l->next) {
+               app = GS_APP (l->data);
+               if (gs_app_get_metadata_item (app, "package-id") != NULL)
+                       continue;
+               ret = gs_plugin_packagekit_refine_app (plugin, app, cancellable, error);
+               if (!ret)
+                       goto out;
+       }
+out:
+       return ret;
+}


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