[gnome-software] plugins: Add a datadir-apps plugin to map application filenames to application data



commit 39c2a01d26b7c29cddc0cf96d768ea4b20a1115e
Author: Richard Hughes <richard hughsie com>
Date:   Thu Mar 7 10:54:57 2013 +0000

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

 src/gs-main.c                        |    1 +
 src/gs-self-test.c                   |   47 ++++++++++
 src/plugins/Makefile.am              |    6 ++
 src/plugins/README                   |   11 +++
 src/plugins/gs-plugin-datadir-apps.c |  162 ++++++++++++++++++++++++++++++++++
 5 files changed, 227 insertions(+), 0 deletions(-)
---
diff --git a/src/gs-main.c b/src/gs-main.c
index 43097e6..3837ec5 100644
--- a/src/gs-main.c
+++ b/src/gs-main.c
@@ -1627,6 +1627,7 @@ main (int argc, char **argv)
        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);
+       gs_plugin_loader_set_enabled (priv->plugin_loader, "datadir-apps", 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 3981037..9a34db7 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -47,6 +47,7 @@ gs_plugin_loader_func (void)
        gboolean ret;
        GError *error = NULL;
        GList *list;
+       GList *l;
        GsApp *app;
        GsPluginLoader *loader;
 
@@ -80,6 +81,52 @@ gs_plugin_loader_func (void)
        g_assert_cmpstr (gs_app_get_name (app), ==, "Boxes");
        g_list_free_full (list, (GDestroyNotify) g_object_unref);
 
+       /* get updates */
+       list = gs_plugin_loader_get_updates (loader, &error);
+       g_assert_no_error (error);
+       g_assert (list != NULL);
+       g_assert_cmpint (g_list_length (list), ==, 2);
+       app = g_list_nth_data (list, 0);
+       g_assert_cmpstr (gs_app_get_id (app), ==, 
"os-update:gnome-boxes-libs;0.0.1;i386;updates-testing,libvirt-glib-devel;0.0.1;noarch;fedora");
+       g_assert_cmpstr (gs_app_get_name (app), ==, "OS Update");
+       g_assert_cmpstr (gs_app_get_summary (app), ==, "This updates the system:\nDo not segfault when using 
newer versons of libvirt.\nFix several memory leaks.");
+       g_assert_cmpint (gs_app_get_kind (app), ==, GS_APP_KIND_OS_UPDATE);
+
+       app = g_list_nth_data (list, 1);
+       g_assert_cmpstr (gs_app_get_id (app), ==, "gnome-boxes");
+       g_assert_cmpstr (gs_app_get_name (app), ==, "Boxes");
+       g_assert_cmpstr (gs_app_get_summary (app), ==, "Do not segfault when using newer versons of 
libvirt.");
+       g_assert_cmpint (gs_app_get_kind (app), ==, GS_APP_KIND_NORMAL);
+       g_list_free_full (list, (GDestroyNotify) g_object_unref);
+
+       /* test packagekit */
+       gs_plugin_loader_set_enabled (loader, "dummy", FALSE);
+       ret = gs_plugin_loader_set_enabled (loader, "packagekit", TRUE);
+       g_assert (ret);
+       ret = gs_plugin_loader_set_enabled (loader, "desktopdb", TRUE);
+       g_assert (ret);
+       ret = gs_plugin_loader_set_enabled (loader, "datadir-apps", TRUE);
+       g_assert (ret);
+
+       list = gs_plugin_loader_get_installed (loader, &error);
+       g_assert_no_error (error);
+       g_assert (list != NULL);
+       g_assert_cmpint (g_list_length (list), >, 50);
+
+       /* find a specific app */
+       for (l = list; l != NULL; l = l->next) {
+               app = GS_APP (l->data);
+               if (g_strcmp0 (gs_app_get_id (app), "gnome-screenshot") == 0)
+                       break;
+       }
+       g_assert_cmpstr (gs_app_get_id (app), ==, "gnome-screenshot");
+       g_assert_cmpstr (gs_app_get_name (app), ==, "Screenshot");
+       g_assert_cmpstr (gs_app_get_summary (app), ==, "Save images of your screen or individual windows");
+       g_assert_cmpint (gs_app_get_state (app), ==, GS_APP_STATE_INSTALLED);
+       g_assert_cmpint (gs_app_get_kind (app), ==, GS_APP_KIND_SYSTEM);
+       g_assert (gs_app_get_pixbuf (app) != NULL);
+       g_list_free_full (list, (GDestroyNotify) g_object_unref);
+
        g_object_unref (loader);
 }
 
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index e00b7bd..429f651 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_datadir_apps.la                    \
        libgs_plugin_desktopdb.la                       \
        libgs_plugin_dummy.la                           \
        libgs_plugin_hardcoded-kind.la                  \
@@ -55,4 +56,9 @@ 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)
 
+libgs_plugin_datadir_apps_la_SOURCES = gs-plugin-datadir-apps.c
+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)
+
 -include $(top_srcdir)/git.mk
diff --git a/src/plugins/README b/src/plugins/README
index b7de2c7..76391cb 100644
--- a/src/plugins/README
+++ b/src/plugins/README
@@ -95,3 +95,14 @@ Overview:    Uses the PackageKit desktop.db cache to map package names to
 Methods:       <nothing>
 Requires:      <nothing>
 Refines:       {package-name}->{datadir-desktop-filename}
+
+== datadir-apps ==
+Overview:      Uses the files in /usr/share/applications to provide icons and
+               translations for installed applications.
+Methods:       <nothing>
+Requires:      <nothing>
+Refines:       {datadir-desktop-filename}->[name]
+               {datadir-desktop-filename}->[summary]
+               {datadir-desktop-filename}->[pixbuf]
+               {datadir-desktop-filename}->[id]
+               {datadir-desktop-filename}->[kind]
diff --git a/src/plugins/gs-plugin-datadir-apps.c b/src/plugins/gs-plugin-datadir-apps.c
new file mode 100644
index 0000000..8d1fdc0
--- /dev/null
+++ b/src/plugins/gs-plugin-datadir-apps.c
@@ -0,0 +1,162 @@
+/* -*- 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>
+
+/**
+ * gs_plugin_get_name:
+ */
+const gchar *
+gs_plugin_get_name (void)
+{
+       return "datadir-apps";
+}
+
+/**
+ * gs_plugin_get_priority:
+ */
+gdouble
+gs_plugin_get_priority (GsPlugin *plugin)
+{
+       return 1.0f;
+}
+
+/**
+ * gs_plugin_datadir_apps_extract_desktop_data:
+ */
+static gboolean
+gs_plugin_datadir_apps_extract_desktop_data (GsPlugin *plugin,
+                                            GsApp *app,
+                                            const gchar *desktop_file,
+                                            GError **error)
+{
+       const gchar *basename_tmp = NULL;
+       gboolean ret;
+       gchar *basename = NULL;
+       gchar *comment = NULL;
+       gchar *name = NULL;
+       gchar *icon = NULL;
+       GKeyFile *key_file;
+       GdkPixbuf *pixbuf = NULL;
+
+       /* load desktop file */
+       key_file = g_key_file_new ();
+       ret = g_key_file_load_from_file (key_file,
+                                        desktop_file,
+                                        G_KEY_FILE_NONE,
+                                        error);
+       if (!ret)
+               goto out;
+
+       /* get desktop name */
+       name = g_key_file_get_string (key_file,
+                                     G_KEY_FILE_DESKTOP_GROUP,
+                                     G_KEY_FILE_DESKTOP_KEY_NAME,
+                                     NULL);
+       if (name != NULL && name[0] != '\0')
+               gs_app_set_name (app, name);
+
+       /* get desktop summary */
+       comment = g_key_file_get_string (key_file,
+                                        G_KEY_FILE_DESKTOP_GROUP,
+                                        G_KEY_FILE_DESKTOP_KEY_COMMENT,
+                                        NULL);
+       if (comment != NULL && comment[0] != '\0')
+               gs_app_set_summary (app, comment);
+
+       /* get desktop icon */
+       icon = g_key_file_get_string (key_file,
+                                     G_KEY_FILE_DESKTOP_GROUP,
+                                     G_KEY_FILE_DESKTOP_KEY_ICON,
+                                     NULL);
+       if (icon == NULL)
+               icon = g_strdup (GTK_STOCK_MISSING_IMAGE);
+
+       /* set pixbuf */
+       if (icon[0] == '/') {
+               pixbuf = gdk_pixbuf_new_from_file_at_size (icon,
+                                                          plugin->pixbuf_size,
+                                                          plugin->pixbuf_size,
+                                                          NULL);
+       } else {
+               pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+                                                  icon,
+                                                  plugin->pixbuf_size,
+                                                  GTK_ICON_LOOKUP_USE_BUILTIN |
+                                                  GTK_ICON_LOOKUP_FORCE_SIZE,
+                                                  NULL);
+       }
+       if (pixbuf != NULL)
+               gs_app_set_pixbuf (app, pixbuf);
+
+       /* set new id */
+       basename = g_path_get_basename (desktop_file);
+       g_strdelimit (basename, ".", '\0');
+       basename_tmp = basename;
+       if (g_str_has_prefix (basename_tmp, "fedora-"))
+               basename_tmp += 7;
+       g_debug ("setting new id for %s to %s",
+                gs_app_get_id (app), basename_tmp);
+       gs_app_set_id (app, basename_tmp);
+
+       /* mark as an application */
+       gs_app_set_kind (app, GS_APP_KIND_NORMAL);
+out:
+       g_key_file_unref (key_file);
+       if (pixbuf != NULL)
+               g_object_unref (pixbuf);
+       g_free (basename);
+       g_free (icon);
+       g_free (name);
+       g_free (comment);
+       return ret;
+}
+
+/**
+ * gs_plugin_refine:
+ */
+gboolean
+gs_plugin_refine (GsPlugin *plugin, GList *list, GError **error)
+{
+       const gchar *tmp;
+       gboolean ret = TRUE;
+       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;
+               ret = gs_plugin_datadir_apps_extract_desktop_data (plugin,
+                                                                  app,
+                                                                  tmp,
+                                                                  error);
+               if (!ret)
+                       goto out;
+       }
+out:
+       return ret;
+}


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