[gnome-control-center/wip/applications: 3/4] application: Drop libflatpak dependency



commit 3d9727fbbb346e52e91a5e52f5c6d7037f62e7d0
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jan 2 12:39:15 2019 -0500

    application: Drop libflatpak dependency
    
    We can get by for now with some screenscraping,
    turning the flatpak dependency into a runtime thing.

 panels/applications/cc-applications-panel.c | 19 +-----
 panels/applications/meson.build             |  4 +-
 panels/applications/utils.c                 | 97 +++++++++++++++++++++--------
 panels/applications/utils.h                 | 21 +++----
 4 files changed, 84 insertions(+), 57 deletions(-)
---
diff --git a/panels/applications/cc-applications-panel.c b/panels/applications/cc-applications-panel.c
index 896a6bc2b..f6be91734 100644
--- a/panels/applications/cc-applications-panel.c
+++ b/panels/applications/cc-applications-panel.c
@@ -36,8 +36,6 @@
 #include "search.h"
 #include "utils.h"
 
-#include <flatpak/flatpak.h>
-
 enum {
   PROP_0,
   PROP_PARAMETERS
@@ -521,26 +519,15 @@ add_static_permissions (CcApplicationsPanel *self,
                         GAppInfo            *info,
                         const gchar         *app_id)
 {
-  g_autoptr(FlatpakInstalledRef) ref = NULL;
-  g_autoptr(GBytes) bytes = NULL;
-  g_autoptr(GError) error = NULL;
   g_autoptr(GKeyFile) keyfile = NULL;
   gchar **strv;
   gchar *str;
   gint added = 0;
   g_autofree gchar *text = NULL;
   
-  ref = find_flatpak_ref (app_id);
-  bytes = flatpak_installed_ref_load_metadata (ref, NULL, NULL);
-  keyfile = g_key_file_new ();
-  if (!g_key_file_load_from_data (keyfile,
-                                  g_bytes_get_data (bytes, NULL),
-                                  g_bytes_get_size (bytes),
-                                  0, &error))
-    {
-      g_warning ("%s", error->message);
-      return FALSE;
-    }
+  keyfile = get_flatpak_metadata (app_id);
+  if (keyfile == NULL)
+    return FALSE;
 
   strv = g_key_file_get_string_list (keyfile, "Context", "sockets", NULL, NULL);
   if (strv && g_strv_contains ((const gchar * const*)strv, "system-bus"))
diff --git a/panels/applications/meson.build b/panels/applications/meson.build
index 75b9157c4..49796a322 100644
--- a/panels/applications/meson.build
+++ b/panels/applications/meson.build
@@ -38,14 +38,12 @@ sources += gnome.compile_resources(
         export : true
 )
 
-deps = common_deps + [ dependency('flatpak', version: '>= 1.0.0') ]
-
 cflags += '-DGNOMELOCALEDIR="@0@"'.format(control_center_localedir)
 
 panels_libs += static_library(
            cappletname,
               sources : sources,
   include_directories : [ top_inc, common_inc ],
-         dependencies : deps,
+         dependencies : common_deps,
                c_args : cflags
 )
diff --git a/panels/applications/utils.c b/panels/applications/utils.c
index f21e02e1d..430bf7820 100644
--- a/panels/applications/utils.c
+++ b/panels/applications/utils.c
@@ -24,7 +24,6 @@
 
 #include <config.h>
 #include <glib/gi18n.h>
-#include <flatpak/flatpak.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
@@ -120,41 +119,85 @@ container_remove_all (GtkContainer *container)
   g_list_free (children);
 }
 
-FlatpakInstalledRef *
-find_flatpak_ref (const gchar *app_id)
+static gchar *
+get_output_of (const gchar **argv)
 {
-  g_autoptr(FlatpakInstallation) inst = NULL;
-  g_autoptr(GPtrArray) array = NULL;
-  FlatpakInstalledRef *ref;
-  gint i;
-
-  inst = flatpak_installation_new_user (NULL, NULL);
-  ref = flatpak_installation_get_current_installed_app (inst, app_id, NULL, NULL);
-  if (ref)
-    return ref;
-
-  array = flatpak_get_system_installations (NULL, NULL);
-  for (i = 0; i < array->len; i++)
+  g_autofree gchar *output = NULL;
+  int status;
+
+  if (!g_spawn_sync (NULL,
+                     (gchar**) argv,
+                     NULL,
+                     G_SPAWN_SEARCH_PATH,
+                     NULL, NULL,
+                     &output, NULL,
+                     &status, NULL))
+    return NULL;
+
+  if (!g_spawn_check_exit_status (status, NULL))
+    return NULL;
+
+  return g_steal_pointer (&output);
+}
+
+GKeyFile *
+get_flatpak_metadata (const gchar *app_id)
+{
+  const gchar *argv[5] = { "flatpak", "info", "-m", "app", NULL };
+  g_autofree gchar *data = NULL;
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GKeyFile) keyfile = NULL;
+
+  argv[3] = app_id;
+
+  data = get_output_of (argv);
+  if (data == NULL)
+    return NULL;
+
+  keyfile = g_key_file_new ();
+  if (!g_key_file_load_from_data (keyfile, data, -1, 0, &error))
     {
-      FlatpakInstallation *si = g_ptr_array_index (array, i);
-      ref = flatpak_installation_get_current_installed_app (si, app_id, NULL, NULL);
-      if (ref)
-        return ref;
+      g_warning ("%s", error->message);
+      return NULL;
     }
 
-  return NULL;
+  return g_steal_pointer (&keyfile);
 }
 
 guint64
 get_flatpak_app_size (const gchar *app_id)
 {
-  g_autoptr(FlatpakInstalledRef) ref = NULL;
-
-  ref = find_flatpak_ref (app_id);
-  if (ref)
-    return flatpak_installed_ref_get_installed_size (ref);
-
-  return 0;
+  const gchar *argv[5] = { "flatpak", "info", "-s", "app", NULL };
+  g_autofree gchar *data = NULL;
+  guint64 factor;
+  double val;
+
+  argv[3] = app_id;
+
+  data = get_output_of (argv);
+  if (data == NULL)
+    return 0;
+
+  data = g_strstrip (data);
+  
+  if (g_str_has_suffix (data, "kB") || g_str_has_suffix (data, "kb"))
+    factor = 1000;
+  else if (g_str_has_suffix (data, "MB") || g_str_has_suffix (data, "Mb"))
+    factor = 1000 * 1000;
+  else if (g_str_has_suffix (data, "GB") || g_str_has_suffix (data, "Gb"))
+    factor = 1000 * 1000 * 1000;
+  else if (g_str_has_suffix (data, "KiB") || g_str_has_suffix (data, "Kib"))
+    factor = 1024;
+  else if (g_str_has_suffix (data, "MiB") || g_str_has_suffix (data, "Mib"))
+    factor = 1024 * 1024;
+  else if (g_str_has_suffix (data, "GiB") || g_str_has_suffix (data, "Gib"))
+    factor = 1024 * 1024 * 1024;
+  else 
+    factor = 1;
+
+  val = g_ascii_strtod (data, NULL);
+
+  return (guint64)(val * factor);
 }
 
 char *
diff --git a/panels/applications/utils.h b/panels/applications/utils.h
index ad58dd188..557e455b6 100644
--- a/panels/applications/utils.h
+++ b/panels/applications/utils.h
@@ -22,24 +22,23 @@
 
 #include <gio/gio.h>
 #include <gtk/gtk.h>
-#include <flatpak/flatpak.h>
 
 G_BEGIN_DECLS
 
-void                 file_remove_async    (GFile               *file,
-                                           GAsyncReadyCallback  callback,
-                                           gpointer             data);
+void      file_remove_async    (GFile               *file,
+                                GAsyncReadyCallback  callback,
+                                gpointer             data);
 
-void                 file_size_async      (GFile               *file,
-                                           GAsyncReadyCallback  callback,
-                                           gpointer             data);
+void      file_size_async      (GFile               *file,
+                                GAsyncReadyCallback  callback,
+                                gpointer             data);
 
-void                 container_remove_all (GtkContainer        *container);
+void      container_remove_all (GtkContainer        *container);
 
-FlatpakInstalledRef* find_flatpak_ref     (const gchar         *app_id);
+GKeyFile* get_flatpak_metadata (const gchar         *app_id);
 
-guint64              get_flatpak_app_size (const gchar         *app_id);
+guint64   get_flatpak_app_size (const gchar         *app_id);
 
-gchar*               get_app_id           (GAppInfo            *info);
+gchar*    get_app_id           (GAppInfo            *info);
 
 G_END_DECLS


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