[gnome-panel] libpanel-applet-private: implement GpAppletManager



commit 668a4f87e1535647a4a378c4add29fbe45adb3f7
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Fri Oct 28 00:57:19 2016 +0300

    libpanel-applet-private: implement GpAppletManager

 gnome-panel/libpanel-applet-private/Makefile.am    |    5 +
 .../libpanel-applet-private/gp-applet-manager.c    |  230 +++++++++++++++++++-
 2 files changed, 229 insertions(+), 6 deletions(-)
---
diff --git a/gnome-panel/libpanel-applet-private/Makefile.am b/gnome-panel/libpanel-applet-private/Makefile.am
index 0fdca48..438ef39 100644
--- a/gnome-panel/libpanel-applet-private/Makefile.am
+++ b/gnome-panel/libpanel-applet-private/Makefile.am
@@ -5,6 +5,7 @@ noinst_LTLIBRARIES = \
        $(NULL)
 
 libpanel_applet_private_la_CFLAGS = \
+       -DAPPLETSDIR=\""$(libdir)/gnome-panel/applets"\" \
        -DDATADIR=\""$(datadir)"\" \
        -DPANEL_APPLETS_DIR=\"$(appletsdir)\" \
        -I$(srcdir) \
@@ -33,6 +34,10 @@ libpanel_applet_private_la_SOURCES = \
        panel-applet-frame-dbus.h \
        $(NULL)
 
+libpanel_applet_private_la_LIBADD = \
+       $(top_builddir)/libgnome-panel/libgnome-panel.la \
+       $(NULL)
+
 libpanel_applet_private_la_LDFLAGS = \
        $(WARN_LDFLAGS) \
        $(AM_LDFLAGS) \
diff --git a/gnome-panel/libpanel-applet-private/gp-applet-manager.c 
b/gnome-panel/libpanel-applet-private/gp-applet-manager.c
index 79f839f..bd49fa9 100644
--- a/gnome-panel/libpanel-applet-private/gp-applet-manager.c
+++ b/gnome-panel/libpanel-applet-private/gp-applet-manager.c
@@ -17,40 +17,163 @@
 
 #include "config.h"
 
+#include <string.h>
+
+#include "gp-applet-frame.h"
 #include "gp-applet-manager.h"
+#include "gp-module-private.h"
+#include "libgnome-panel/gp-applet-info-private.h"
 
 struct _GpAppletManager
 {
   PanelAppletsManager  parent;
+
+  GHashTable          *modules;
+  GHashTable          *infos;
 };
 
 G_DEFINE_TYPE (GpAppletManager, gp_applet_manager, PANEL_TYPE_APPLETS_MANAGER)
 
+static void
+get_applet_infos (GpAppletManager *manager,
+                  const gchar     *id,
+                  GpModule        *module)
+{
+  const gchar *const *applets;
+  guint i;
+
+  applets = gp_module_get_applets (module);
+
+  for (i = 0; applets[i] != NULL; i++)
+    {
+      GError *error;
+      GpAppletInfo *info;
+      gchar *iid;
+      PanelAppletInfo *applet_info;
+
+      error = NULL;
+      info = gp_module_get_applet_info (module, applets[i], &error);
+
+      if (info == NULL)
+        {
+          g_warning ("%s", error->message);
+          g_error_free (error);
+
+          continue;
+        }
+
+      iid = g_strdup_printf ("%s::%s", id, applets[i]);
+      applet_info = panel_applet_info_new (iid, info->name,
+                                           info->description,
+                                           info->icon);
+
+      g_hash_table_insert (manager->infos, g_strdup (iid), applet_info);
+      g_free (iid);
+    }
+}
+
+static void
+load_modules (GpAppletManager *manager)
+{
+  GDir *dir;
+  const gchar *name;
+
+  dir = g_dir_open (APPLETSDIR, 0, NULL);
+  if (!dir)
+    return;
+
+  while ((name = g_dir_read_name (dir)) != NULL)
+    {
+      gchar *path;
+      GpModule *module;
+      const gchar *id;
+
+      path = g_build_filename (APPLETSDIR, name, NULL);
+      module = gp_module_new_from_path (path);
+      g_free (path);
+
+      if (module == NULL)
+        continue;
+
+      id = gp_module_get_id (module);
+
+      g_hash_table_insert (manager->modules, g_strdup (id), module);
+      get_applet_infos (manager, id, module);
+    }
+
+  g_dir_close (dir);
+}
+
+static void
+applet_info_free (gpointer data)
+{
+  PanelAppletInfo *info;
+
+  info = (PanelAppletInfo *) data;
+
+  panel_applet_info_free (info);
+}
+
+static void
+gp_applet_manager_finalize (GObject *object)
+{
+  GpAppletManager *manager;
+
+  manager = GP_APPLET_MANAGER (object);
+
+  g_clear_pointer (&manager->modules, g_hash_table_destroy);
+  g_clear_pointer (&manager->infos, g_hash_table_destroy);
+
+  G_OBJECT_CLASS (gp_applet_manager_parent_class)->finalize (object);
+}
+
 static GList *
-gp_applet_manager_get_applets (PanelAppletsManager *amanager)
+gp_applet_manager_get_applets (PanelAppletsManager *manager)
 {
-  return NULL;
+  GpAppletManager *applet_manager;
+
+  applet_manager = GP_APPLET_MANAGER (manager);
+
+  return g_hash_table_get_values (applet_manager->infos);
 }
 
 static gboolean
 gp_applet_manager_factory_activate (PanelAppletsManager *manager,
                                     const gchar         *iid)
 {
-  return FALSE;
+  GpAppletManager *applet_manager;
+
+  applet_manager = GP_APPLET_MANAGER (manager);
+
+  if (!g_hash_table_lookup (applet_manager->infos, iid))
+    return FALSE;
+
+  return TRUE;
 }
 
 static gboolean
 gp_applet_manager_factory_deactivate (PanelAppletsManager *manager,
                                       const gchar         *iid)
 {
-  return FALSE;
+  GpAppletManager *applet_manager;
+
+  applet_manager = GP_APPLET_MANAGER (manager);
+
+  if (!g_hash_table_lookup (applet_manager->infos, iid))
+    return FALSE;
+
+  return TRUE;
 }
 
 static PanelAppletInfo *
 gp_applet_manager_get_applet_info (PanelAppletsManager *manager,
                                    const gchar         *iid)
 {
-  return NULL;
+  GpAppletManager *applet_manager;
+
+  applet_manager = GP_APPLET_MANAGER (manager);
+
+  return g_hash_table_lookup (applet_manager->infos, iid);
 }
 
 static gboolean
@@ -58,7 +181,91 @@ gp_applet_manager_load_applet (PanelAppletsManager        *manager,
                                const gchar                *iid,
                                PanelAppletFrameActivating *frame_act)
 {
-  return FALSE;
+  GpAppletManager *applet_manager;
+  const gchar *applet_id;
+  gchar *module_id;
+  GpModule *module;
+  gchar *settings_path;
+  gboolean locked_down;
+  PanelOrientation panel_orientation;
+  GtkOrientation orientation;
+  GtkPositionType position;
+  GError *error;
+  GpApplet *applet;
+  GpAppletFrame *frame;
+  PanelAppletFrame *applet_frame;
+  GtkWidget *widget;
+
+  g_return_val_if_fail (iid != NULL, FALSE);
+  g_return_val_if_fail (frame_act != NULL, FALSE);
+
+  applet_manager = GP_APPLET_MANAGER (manager);
+
+  applet_id = g_strrstr (iid, "::");
+  if (!applet_id)
+    return FALSE;
+
+  module_id = g_strndup (iid, strlen (iid) - strlen (applet_id));
+  module = g_hash_table_lookup (applet_manager->modules, module_id);
+  g_free (module_id);
+
+  if (!module)
+    return FALSE;
+
+  applet_id += 2;
+  settings_path = panel_applet_frame_activating_get_settings_path (frame_act);
+  locked_down = panel_applet_frame_activating_get_locked_down (frame_act);
+  panel_orientation = panel_applet_frame_activating_get_orientation (frame_act);
+
+  switch (panel_orientation)
+    {
+      case PANEL_ORIENTATION_BOTTOM:
+        orientation = GTK_ORIENTATION_HORIZONTAL;
+        position = GTK_POS_BOTTOM;
+        break;
+      case PANEL_ORIENTATION_LEFT:
+        orientation = GTK_ORIENTATION_VERTICAL;
+        position = GTK_POS_LEFT;
+        break;
+      case PANEL_ORIENTATION_RIGHT:
+        orientation = GTK_ORIENTATION_VERTICAL;
+        position = GTK_POS_RIGHT;
+        break;
+      case PANEL_ORIENTATION_TOP:
+      default:
+        orientation = GTK_ORIENTATION_HORIZONTAL;
+        position = GTK_POS_TOP;
+        break;
+    }
+
+  error = NULL;
+  applet = gp_module_applet_new (module, applet_id, settings_path, locked_down,
+                                 orientation, position, &error);
+
+  g_free (settings_path);
+
+  if (!applet)
+    {
+      g_warning ("%s", error->message);
+      g_error_free (error);
+
+      return FALSE;
+    }
+
+  frame = g_object_new (GP_TYPE_APPLET_FRAME, NULL);
+  gp_applet_frame_set_applet (frame, applet);
+
+  applet_frame = PANEL_APPLET_FRAME (frame);
+  _panel_applet_frame_set_iid (applet_frame, iid);
+
+  widget = GTK_WIDGET (applet);
+
+  gtk_container_add (GTK_CONTAINER (frame), widget);
+  gtk_widget_show (widget);
+
+  _panel_applet_frame_activated (applet_frame, frame_act, NULL);
+
+  return TRUE;
 }
 
 static GtkWidget *
@@ -72,10 +279,14 @@ gp_applet_manager_get_applet_widget (PanelAppletsManager *manager,
 static void
 gp_applet_manager_class_init (GpAppletManagerClass *manager_class)
 {
+  GObjectClass *object_class;
   PanelAppletsManagerClass *applets_manager_class;
 
+  object_class = G_OBJECT_CLASS (manager_class);
   applets_manager_class = PANEL_APPLETS_MANAGER_CLASS (manager_class);
 
+  object_class->finalize = gp_applet_manager_finalize;
+
   applets_manager_class->get_applets = gp_applet_manager_get_applets;
   applets_manager_class->factory_activate = gp_applet_manager_factory_activate;
   applets_manager_class->factory_deactivate = gp_applet_manager_factory_deactivate;
@@ -87,4 +298,11 @@ gp_applet_manager_class_init (GpAppletManagerClass *manager_class)
 static void
 gp_applet_manager_init (GpAppletManager *manager)
 {
+  manager->modules = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                            g_free, g_object_unref);
+
+  manager->infos = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                          g_free, applet_info_free);
+
+  load_modules (manager);
 }


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