[gnome-panel] libgnome-panel: add gp_module_get_module_info



commit 8cfc517223ef5ec0c542fc65b682ed1ef4f58d27
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Mon Jul 31 15:49:52 2017 +0300

    libgnome-panel: add gp_module_get_module_info

 gnome-panel/libpanel-applet-private/gp-module.c |   93 ++++++++++++-----------
 libgnome-panel/gp-module.h                      |   50 +++++++------
 modules/clock/clock-module.c                    |   33 ++++----
 modules/fish/fish-module.c                      |   33 ++++----
 modules/notification-area/na-module.c           |   33 ++++----
 modules/separator/separator-module.c            |   33 ++++----
 modules/status-notifier/sn-module.c             |   33 ++++----
 modules/wncklet/wncklet-module.c                |   41 +++++-----
 8 files changed, 177 insertions(+), 172 deletions(-)
---
diff --git a/gnome-panel/libpanel-applet-private/gp-module.c b/gnome-panel/libpanel-applet-private/gp-module.c
index ef99adb..564d1da 100644
--- a/gnome-panel/libpanel-applet-private/gp-module.c
+++ b/gnome-panel/libpanel-applet-private/gp-module.c
@@ -31,8 +31,9 @@
 #include "libgnome-panel/gp-applet-info-private.h"
 #include "libgnome-panel/gp-module-info-private.h"
 
-typedef guint32 (* GetAbiVersionFunc) (void);
-typedef void    (* GetVTableFunc)     (GpModuleVTable *vtable);
+typedef guint32        (* GetAbiVersionFunc) (void);
+typedef GpModuleInfo * (* GetModuleInfoFunc) (void);
+typedef void           (* GetVTableFunc)     (GpModuleVTable *vtable);
 
 struct _GpModule
 {
@@ -41,9 +42,9 @@ struct _GpModule
   gchar          *path;
   GModule        *library;
 
-  GpModuleVTable  vtable;
-
   GpModuleInfo   *info;
+
+  GpModuleVTable  vtable;
   GHashTable     *applets;
 };
 
@@ -169,34 +170,6 @@ applet_info_free (gpointer data)
   gp_applet_info_free (info);
 }
 
-static gboolean
-load_module_info (GpModule *module)
-{
-  GpModuleInfo *info;
-
-  module->info = info = module->vtable.get_module_info ();
-
-  if (info == NULL)
-    {
-      g_warning ("Failed to get 'GpModuleInfo' from module '%s'", module->path);
-      return FALSE;
-    }
-
-  if (info->id == NULL || *info->id == '\0')
-    {
-      g_warning ("Module '%s' does not have valid id", module->path);
-      return FALSE;
-    }
-
-  if (info->applets == NULL || info->applets[0] == NULL)
-    {
-      g_warning ("Module '%s' does not have valid applets", module->path);
-      return FALSE;
-    }
-
-  return TRUE;
-}
-
 static void
 gp_module_finalize (GObject *object)
 {
@@ -213,7 +186,7 @@ gp_module_finalize (GObject *object)
     }
 
   g_clear_pointer (&module->info, gp_module_info_free);
-  g_clear_pointer (&module->info, g_hash_table_destroy);
+  g_clear_pointer (&module->applets, g_hash_table_destroy);
 
   G_OBJECT_CLASS (gp_module_parent_class)->finalize (object);
 }
@@ -248,6 +221,7 @@ gp_module_new_from_path (const gchar *path)
   GModuleFlags flags;
   const gchar *symbol;
   GetAbiVersionFunc abi_version_func;
+  GetModuleInfoFunc module_info_func;
   GetVTableFunc vtable_func;
 
   g_return_val_if_fail (path != NULL && *path != '\0', NULL);
@@ -293,8 +267,8 @@ gp_module_new_from_path (const gchar *path)
       return NULL;
     }
 
-  symbol = "gp_module_get_vtable";
-  if (!g_module_symbol (module->library, symbol, (gpointer) &vtable_func))
+  symbol = "gp_module_get_module_info";
+  if (!g_module_symbol (module->library, symbol, (gpointer) &module_info_func))
     {
       g_warning ("Failed to get '%s' for module '%s': %s",
                  symbol, path, g_module_error ());
@@ -303,7 +277,7 @@ gp_module_new_from_path (const gchar *path)
       return NULL;
     }
 
-  if (vtable_func == NULL)
+  if (module_info_func == NULL)
     {
       g_warning ("Invalid '%s' in module '%s'", symbol, path);
 
@@ -311,14 +285,45 @@ gp_module_new_from_path (const gchar *path)
       return NULL;
     }
 
-  vtable_func (&module->vtable);
+  module->info = module_info_func ();
+  if (module->info == NULL)
+    {
+      g_warning ("Failed to get 'GpModuleInfo' from module '%s'", module->path);
+      return NULL;
+    }
+
+  if (module->info->id == NULL || *module->info->id == '\0')
+    {
+      g_warning ("Module '%s' does not have valid id", module->path);
+      return NULL;
+    }
+
+  if (module->info->applets == NULL || module->info->applets[0] == NULL)
+    {
+      g_warning ("Module '%s' does not have valid applets", module->path);
+      return NULL;
+    }
+
+  symbol = "gp_module_get_vtable";
+  if (!g_module_symbol (module->library, symbol, (gpointer) &vtable_func))
+    {
+      g_warning ("Failed to get '%s' for module '%s': %s",
+                 symbol, path, g_module_error ());
+
+      g_object_unref (module);
+      return NULL;
+    }
 
-  if (!load_module_info (module))
+  if (vtable_func == NULL)
     {
+      g_warning ("Invalid '%s' in module '%s'", symbol, path);
+
       g_object_unref (module);
       return NULL;
     }
 
+  vtable_func (&module->vtable);
+
   return module;
 }
 
@@ -405,9 +410,9 @@ gp_module_applet_new (GpModule         *module,
 
   if (!match_backend (info))
     {
-      g_set_error (error, GP_MODULE_ERROR, GP_MODULE_ERROR_MISSING_APPLET_INFO,
-                   "Module '%s' did not return required info about applet '%s'",
-                   module->info->id, applet);
+      g_set_error (error, GP_MODULE_ERROR, GP_MODULE_ERROR_MISSING_APPLET_TYPE,
+                   "Applet '%s' from module '%s' does not work with current backend '%s'",
+                   applet, module->info->id, get_current_backend ());
 
       return NULL;
     }
@@ -415,9 +420,9 @@ gp_module_applet_new (GpModule         *module,
   type = module->vtable.get_applet_type (applet);
   if (type == G_TYPE_NONE)
     {
-      g_set_error (error, GP_MODULE_ERROR, GP_MODULE_ERROR_MISSING_APPLET_TYPE,
-                   "Applet '%s' from module '%s' does not work with current backend '%s'",
-                   applet, module->info->id, get_current_backend ());
+      g_set_error (error, GP_MODULE_ERROR, GP_MODULE_ERROR_MISSING_APPLET_INFO,
+                   "Module '%s' did not return required info about applet '%s'",
+                   module->info->id, applet);
 
       return NULL;
     }
diff --git a/libgnome-panel/gp-module.h b/libgnome-panel/gp-module.h
index 9bc0cad..d7f0376 100644
--- a/libgnome-panel/gp-module.h
+++ b/libgnome-panel/gp-module.h
@@ -33,22 +33,6 @@ G_BEGIN_DECLS
  * A module with one or more applets.
  *
  * |[<!-- language="C" -->
- * static GpModuleInfo *
- * example_get_module_info (void)
- * {
- *   GpModuleInfo *info;
- *
- *   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
- *   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
- *
- *   info = gp_module_info_new ("org.example.example",
- *                              PACKAGE_VERSION, GETTEXT_PACKAGE);
- *
- *   gp_module_info_set_applets (info, "example1", "example2", NULL);
- *
- *   return info;
- * }
- *
  * static GpAppletInfo *
  * example_get_applet_info (const gchar *applet)
  * {
@@ -128,11 +112,26 @@ G_BEGIN_DECLS
  *   return GP_MODULE_ABI_VERSION;
  * }
  *
+ * GpModuleInfo *
+ * gp_module_get_module_info (void)
+ * {
+ *   GpModuleInfo *info;
+ *
+ *   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+ *   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+ *
+ *   info = gp_module_info_new ("org.example.example",
+ *                              PACKAGE_VERSION, GETTEXT_PACKAGE);
+ *
+ *   gp_module_info_set_applets (info, "example1", "example2", NULL);
+ *
+ *   return info;
+ * }
+ *
  * void
  * gp_module_get_vtable (GpModuleVTable *vtable)
  * {
  *   *vtable = (GpModuleVTable) {
- *     example_get_module_info,
  *     example_get_applet_info,
  *     example_get_applet_type,
  *     example_get_applet_from_iid, // or NULL if not needed
@@ -151,7 +150,6 @@ G_BEGIN_DECLS
 
 /**
  * GpModuleVTable:
- * @get_module_info: (transfer full): returns a #GpModuleInfo
  * @get_applet_info: (transfer full): returns a #GpAppletInfo.
  * @get_applet_type: returns a #GType.
  * @get_applet_from_iid: Compatibility function.
@@ -162,8 +160,6 @@ G_BEGIN_DECLS
 typedef struct _GpModuleVTable GpModuleVTable;
 struct _GpModuleVTable
 {
-  GpModuleInfo * (* get_module_info)     (void);
-
   GpAppletInfo * (* get_applet_info)     (const gchar    *applet);
 
   GType          (* get_applet_type)     (const gchar    *applet);
@@ -182,7 +178,17 @@ struct _GpModuleVTable
  *
  * Returns: the module ABI version.
  */
-guint32 gp_module_get_abi_version (void);
+guint32       gp_module_get_abi_version (void);
+
+/**
+ * gp_module_get_module_info:
+ *
+ * Required API for GNOME Panel modules to implement. This function must
+ * return a newly created #GpModuleInfo.
+ *
+ * Returns: a #GpModuleInfo.
+ */
+GpModuleInfo *gp_module_get_module_info (void);
 
 /**
  * gp_module_get_vtable:
@@ -190,7 +196,7 @@ guint32 gp_module_get_abi_version (void);
  *
  * Required API for GNOME Panel modules to implement.
  */
-void    gp_module_get_vtable      (GpModuleVTable *vtable);
+void          gp_module_get_vtable      (GpModuleVTable *vtable);
 
 G_END_DECLS
 
diff --git a/modules/clock/clock-module.c b/modules/clock/clock-module.c
index 8bf1162..f8883d4 100644
--- a/modules/clock/clock-module.c
+++ b/modules/clock/clock-module.c
@@ -22,22 +22,6 @@
 
 #include "clock-applet.h"
 
-static GpModuleInfo *
-clock_get_module_info (void)
-{
-  GpModuleInfo *info;
-
-  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
-  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-
-  info = gp_module_info_new ("org.gnome.gnome-panel.clock",
-                             PACKAGE_VERSION, GETTEXT_PACKAGE);
-
-  gp_module_info_set_applets (info, "clock", NULL);
-
-  return info;
-}
-
 static GpAppletInfo *
 clock_get_applet_info (const gchar *applet)
 {
@@ -74,11 +58,26 @@ gp_module_get_abi_version (void)
   return GP_MODULE_ABI_VERSION;
 }
 
+GpModuleInfo *
+gp_module_get_module_info (void)
+{
+  GpModuleInfo *info;
+
+  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+
+  info = gp_module_info_new ("org.gnome.gnome-panel.clock",
+                             PACKAGE_VERSION, GETTEXT_PACKAGE);
+
+  gp_module_info_set_applets (info, "clock", NULL);
+
+  return info;
+}
+
 void
 gp_module_get_vtable (GpModuleVTable *vtable)
 {
   *vtable = (GpModuleVTable) {
-    clock_get_module_info,
     clock_get_applet_info,
     clock_get_applet_type,
     clock_get_applet_from_iid,
diff --git a/modules/fish/fish-module.c b/modules/fish/fish-module.c
index 13304eb..c04dcb3 100644
--- a/modules/fish/fish-module.c
+++ b/modules/fish/fish-module.c
@@ -22,22 +22,6 @@
 
 #include "fish-applet.h"
 
-static GpModuleInfo *
-fish_get_module_info (void)
-{
-  GpModuleInfo *info;
-
-  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
-  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-
-  info = gp_module_info_new ("org.gnome.gnome-panel.fish",
-                             PACKAGE_VERSION, GETTEXT_PACKAGE);
-
-  gp_module_info_set_applets (info, "fish", NULL);
-
-  return info;
-}
-
 static GpAppletInfo *
 fish_get_applet_info (const gchar *applet)
 {
@@ -74,11 +58,26 @@ gp_module_get_abi_version (void)
   return GP_MODULE_ABI_VERSION;
 }
 
+GpModuleInfo *
+gp_module_get_module_info (void)
+{
+  GpModuleInfo *info;
+
+  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+
+  info = gp_module_info_new ("org.gnome.gnome-panel.fish",
+                             PACKAGE_VERSION, GETTEXT_PACKAGE);
+
+  gp_module_info_set_applets (info, "fish", NULL);
+
+  return info;
+}
+
 void
 gp_module_get_vtable (GpModuleVTable *vtable)
 {
   *vtable = (GpModuleVTable) {
-    fish_get_module_info,
     fish_get_applet_info,
     fish_get_applet_type,
     fish_get_applet_from_iid,
diff --git a/modules/notification-area/na-module.c b/modules/notification-area/na-module.c
index ef460e7..66226eb 100644
--- a/modules/notification-area/na-module.c
+++ b/modules/notification-area/na-module.c
@@ -22,22 +22,6 @@
 
 #include "na-applet.h"
 
-static GpModuleInfo *
-na_get_module_info (void)
-{
-  GpModuleInfo *info;
-
-  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
-  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-
-  info = gp_module_info_new ("org.gnome.gnome-panel.notification-area",
-                             PACKAGE_VERSION, GETTEXT_PACKAGE);
-
-  gp_module_info_set_applets (info, "notification-area", NULL);
-
-  return info;
-}
-
 static GpAppletInfo *
 na_get_applet_info (const gchar *applet)
 {
@@ -74,11 +58,26 @@ gp_module_get_abi_version (void)
   return GP_MODULE_ABI_VERSION;
 }
 
+GpModuleInfo *
+gp_module_get_module_info (void)
+{
+  GpModuleInfo *info;
+
+  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+
+  info = gp_module_info_new ("org.gnome.gnome-panel.notification-area",
+                             PACKAGE_VERSION, GETTEXT_PACKAGE);
+
+  gp_module_info_set_applets (info, "notification-area", NULL);
+
+  return info;
+}
+
 void
 gp_module_get_vtable (GpModuleVTable *vtable)
 {
   *vtable = (GpModuleVTable) {
-    na_get_module_info,
     na_get_applet_info,
     na_get_applet_type,
     na_get_applet_from_iid,
diff --git a/modules/separator/separator-module.c b/modules/separator/separator-module.c
index 847c178..e500455 100644
--- a/modules/separator/separator-module.c
+++ b/modules/separator/separator-module.c
@@ -22,22 +22,6 @@
 
 #include "separator-applet.h"
 
-static GpModuleInfo *
-separator_get_module_info (void)
-{
-  GpModuleInfo *info;
-
-  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
-  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-
-  info = gp_module_info_new ("org.gnome.gnome-panel.separator",
-                             PACKAGE_VERSION, GETTEXT_PACKAGE);
-
-  gp_module_info_set_applets (info, "separator", NULL);
-
-  return info;
-}
-
 static GpAppletInfo *
 separator_get_applet_info (const gchar *applet)
 {
@@ -68,11 +52,26 @@ gp_module_get_abi_version (void)
   return GP_MODULE_ABI_VERSION;
 }
 
+GpModuleInfo *
+gp_module_get_module_info (void)
+{
+  GpModuleInfo *info;
+
+  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+
+  info = gp_module_info_new ("org.gnome.gnome-panel.separator",
+                             PACKAGE_VERSION, GETTEXT_PACKAGE);
+
+  gp_module_info_set_applets (info, "separator", NULL);
+
+  return info;
+}
+
 void
 gp_module_get_vtable (GpModuleVTable *vtable)
 {
   *vtable = (GpModuleVTable) {
-    separator_get_module_info,
     separator_get_applet_info,
     separator_get_applet_type,
     separator_get_applet_from_iid,
diff --git a/modules/status-notifier/sn-module.c b/modules/status-notifier/sn-module.c
index 8904315..cb5a65d 100644
--- a/modules/status-notifier/sn-module.c
+++ b/modules/status-notifier/sn-module.c
@@ -22,22 +22,6 @@
 
 #include "sn-applet.h"
 
-static GpModuleInfo *
-sn_get_module_info (void)
-{
-  GpModuleInfo *info;
-
-  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
-  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-
-  info = gp_module_info_new ("org.gnome.gnome-panel.status-notifier",
-                             PACKAGE_VERSION, GETTEXT_PACKAGE);
-
-  gp_module_info_set_applets (info, "status-notifier", NULL);
-
-  return info;
-}
-
 static GpAppletInfo *
 sn_get_applet_info (const gchar *applet)
 {
@@ -72,11 +56,26 @@ gp_module_get_abi_version (void)
   return GP_MODULE_ABI_VERSION;
 }
 
+GpModuleInfo *
+gp_module_get_module_info (void)
+{
+  GpModuleInfo *info;
+
+  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+
+  info = gp_module_info_new ("org.gnome.gnome-panel.status-notifier",
+                             PACKAGE_VERSION, GETTEXT_PACKAGE);
+
+  gp_module_info_set_applets (info, "status-notifier", NULL);
+
+  return info;
+}
+
 void
 gp_module_get_vtable (GpModuleVTable *vtable)
 {
   *vtable = (GpModuleVTable) {
-    sn_get_module_info,
     sn_get_applet_info,
     sn_get_applet_type,
     sn_get_applet_from_iid,
diff --git a/modules/wncklet/wncklet-module.c b/modules/wncklet/wncklet-module.c
index 0b6e8e7..2593b6f 100644
--- a/modules/wncklet/wncklet-module.c
+++ b/modules/wncklet/wncklet-module.c
@@ -26,26 +26,6 @@
 #include "window-menu.h"
 #include "workspace-switcher.h"
 
-static GpModuleInfo *
-wncklet_get_module_info (void)
-{
-  GpModuleInfo *info;
-
-  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
-  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-
-  wnck_set_client_type (WNCK_CLIENT_TYPE_PAGER);
-
-  info = gp_module_info_new ("org.gnome.gnome-panel.wncklet",
-                             PACKAGE_VERSION, GETTEXT_PACKAGE);
-
-  gp_module_info_set_applets (info, "show-desktop", "window-list",
-                              "window-menu", "workspace-switcher",
-                              NULL);
-
-  return info;
-}
-
 static GpAppletInfo *
 wncklet_get_applet_info (const gchar *applet)
 {
@@ -140,11 +120,30 @@ gp_module_get_abi_version (void)
   return GP_MODULE_ABI_VERSION;
 }
 
+GpModuleInfo *
+gp_module_get_module_info (void)
+{
+  GpModuleInfo *info;
+
+  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+
+  wnck_set_client_type (WNCK_CLIENT_TYPE_PAGER);
+
+  info = gp_module_info_new ("org.gnome.gnome-panel.wncklet",
+                             PACKAGE_VERSION, GETTEXT_PACKAGE);
+
+  gp_module_info_set_applets (info, "show-desktop", "window-list",
+                              "window-menu", "workspace-switcher",
+                              NULL);
+
+  return info;
+}
+
 void
 gp_module_get_vtable (GpModuleVTable *vtable)
 {
   *vtable = (GpModuleVTable) {
-    wncklet_get_module_info,
     wncklet_get_applet_info,
     wncklet_get_applet_type,
     wncklet_get_applet_from_iid,


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