[gnome-menus] Convert to GObject, drop static Python bindings



commit 21672a2d0ee94a9588fc15cf0cd4c5ffdfc818c1
Author: Colin Walters <walters verbum org>
Date:   Sun Apr 17 07:50:19 2011 -0400

    Convert to GObject, drop static Python bindings
    
    GMenuTree is now a GObject.  Drop the static Python bindings, since
    introspection gives us coverage of most of the API now.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=647968

 Makefile.am            |    6 +-
 configure.ac           |   37 +-
 libmenu/gmenu-tree.c   |  152 +++--
 libmenu/gmenu-tree.h   |   26 +-
 libmenu/menu-monitor.c |    9 -
 python/Makefile.am     |   20 -
 python/gmenu.c         | 1883 ------------------------------------------------
 util/gnome-menus-ls.py |    2 +-
 util/test-menu-spec.c  |    4 +-
 9 files changed, 122 insertions(+), 2017 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 565ab87..b1a66e6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,12 +1,10 @@
 SUBDIRS = libmenu desktop-directories layout util po
 
-if HAVE_PYTHON
-SUBDIRS += python simple-editor
-endif
+SUBDIRS += simple-editor
 
 ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
 
-DISTCHECK_CONFIGURE_FLAGS = --enable-introspection --enable-python
+DISTCHECK_CONFIGURE_FLAGS = --enable-introspection
 
 EXTRA_DIST = 			\
 	HACKING			\
diff --git a/configure.ac b/configure.ac
index e9361aa..65bdccb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -72,39 +72,8 @@ else
 fi
 AC_SUBST(DEBUG_CFLAGS)
 
-AC_ARG_ENABLE(python,
-              [AC_HELP_STRING([--enable-python],
-                              [build python bindings @<:@default=auto@:>@])],
-              [enable_deprecations=$enableval],
-              [enable_deprecations=auto])
-
-# Detect if we can build Python bindings (need python and python headers)
-if test "x$enable_python" = "xno" ; then
-  have_python=no
-else
-  AC_MSG_NOTICE([Checking to see if we can build Python bindings])
-  have_python=no
-  AM_PATH_PYTHON(2.3)
-
-  if test "x$PYTHON" = "x" ; then
-    AC_MSG_WARN([Python not found])
-  else
-    AM_CHECK_PYTHON_HEADERS(have_python_headers=yes, have_python_headers=no)
-    if test "x$have_python_headers" = "xyes" ; then
-      have_python=yes
-    fi
-  fi
-
-  if test "x$have_python" = "xno" ; then
-    if test "x$enable_python" = "xyes" ; then
-      AC_MSG_ERROR([Building python explicitly requested, but can't build python bindings])
-    else
-      AC_MSG_WARN([Couldn't find the Python headers, not building Python bindings])
-    fi
-  fi
-fi
-
-AM_CONDITIONAL(HAVE_PYTHON, test x$have_python = xyes)
+AM_PATH_PYTHON(2.3)
+AM_CONDITIONAL(HAVE_PYTHON, test x$PYTHON != xyes)
 
 # Because of the way Python implements polymorphism, we get the following warning:
 # "warning: dereferencing type-punned pointer will break strict-aliasing rules"
@@ -136,7 +105,6 @@ libmenu/libgnome-menu-uninstalled.pc
 layout/Makefile
 desktop-directories/Makefile
 util/Makefile
-python/Makefile
 simple-editor/Makefile
 simple-editor/GMenuSimpleEditor/Makefile
 po/Makefile.in
@@ -165,7 +133,6 @@ echo "
         Use *_DISABLE_DEPRECATED:     ${enable_deprecation_flags}
 
         Turn on debugging:            ${enable_debug}
-        Build python bindings:        ${have_python}
         Build introspection support:  ${found_introspection}
 
 "
diff --git a/libmenu/gmenu-tree.c b/libmenu/gmenu-tree.c
index 7d709f9..f8e01ea 100644
--- a/libmenu/gmenu-tree.c
+++ b/libmenu/gmenu-tree.c
@@ -29,9 +29,24 @@
 #include "menu-util.h"
 #include "canonicalize.h"
 
-struct GMenuTree
+enum {
+  PROP_0,
+
+  PROP_NAME,
+  PROP_FLAGS
+};
+
+/* Signals */
+enum
+{
+  LAST_SIGNAL
+};
+
+static guint gmenu_tree_signals [LAST_SIGNAL] = { 0 };
+
+struct _GMenuTree
 {
-  guint         refcount;
+  GObject       parent_instance;
 
   char *basename;
   char *canonical_path;
@@ -43,14 +58,11 @@ struct GMenuTree
   MenuLayoutNode *layout;
   GMenuTreeDirectory *root;
 
-  GSList *monitors;
-
-  gpointer       user_data;
-  GDestroyNotify dnotify;
-
   guint canonical : 1;
 };
 
+G_DEFINE_TYPE (GMenuTree, gmenu_tree, G_TYPE_OBJECT)
+
 typedef struct
 {
   GMenuTreeChangedFunc callback;
@@ -419,45 +431,61 @@ gmenu_tree_force_recanonicalize (GMenuTree *tree)
  * Returns: (transfer full): A new #GMenuTree instance
  */
 GMenuTree *
-gmenu_tree_new (const char     *menu_file,
+gmenu_tree_new (const char     *name,
 		GMenuTreeFlags  flags)
 {
-  GMenuTree *tree;
+  g_return_val_if_fail (name != NULL, NULL);
 
-  g_return_val_if_fail (menu_file != NULL, NULL);
-
-  tree = g_new0 (GMenuTree, 1);
-  tree->flags    = flags;
-  tree->refcount = 1;
-  tree->basename = g_strdup (menu_file);
-
-  return tree;
+  return g_object_new (GMENU_TYPE_TREE, "name", name, "flags", flags, NULL);
 }
 
-GMenuTree *
-gmenu_tree_ref (GMenuTree *tree)
+static void
+gmenu_tree_set_property(GObject         *object,
+			guint            prop_id,
+			const GValue    *value,
+			GParamSpec      *pspec)
 {
-  g_return_val_if_fail (tree != NULL, NULL);
-  g_return_val_if_fail (tree->refcount > 0, NULL);
+  GMenuTree *self = GMENU_TREE (object);
 
-  tree->refcount++;
+  switch (prop_id)
+    {
+    case PROP_NAME:
+      self->basename = g_value_dup_string (value);
+      break;
 
-  return tree;
+    case PROP_FLAGS:
+      self->flags = g_value_get_flags (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
 }
 
-void
-gmenu_tree_unref (GMenuTree *tree)
+static void
+gmenu_tree_get_property(GObject         *object,
+			guint            prop_id,
+			GValue          *value,
+			GParamSpec      *pspec)
 {
-  g_return_if_fail (tree != NULL);
-  g_return_if_fail (tree->refcount >= 1);
+  GMenuTree *self = GMENU_TREE (object);
 
-  if (--tree->refcount > 0)
-    return;
+  switch (prop_id)
+    {
+    case PROP_NAME:
+      g_value_set_string (value, self->basename);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
 
-  if (tree->dnotify)
-    tree->dnotify (tree->user_data);
-  tree->user_data = NULL;
-  tree->dnotify   = NULL;
+static void
+gmenu_tree_finalize (GObject *object)
+{
+  GMenuTree *tree = GMENU_TREE (object);
 
   gmenu_tree_force_recanonicalize (tree);
 
@@ -469,35 +497,49 @@ gmenu_tree_unref (GMenuTree *tree)
     g_free (tree->canonical_path);
   tree->canonical_path = NULL;
 
-  g_slist_foreach (tree->monitors, (GFunc) g_free, NULL);
-  g_slist_free (tree->monitors);
-  tree->monitors = NULL;
-
-  g_free (tree);
+  G_OBJECT_CLASS (gmenu_tree_parent_class)->finalize (object);
 }
 
-void
-gmenu_tree_set_user_data (GMenuTree       *tree,
-			  gpointer        user_data,
-			  GDestroyNotify  dnotify)
+static void
+gmenu_tree_init (GMenuTree *self)
 {
-  g_return_if_fail (tree != NULL);
-
-  if (tree->dnotify != NULL)
-    tree->dnotify (tree->user_data);
-
-  tree->dnotify   = dnotify;
-  tree->user_data = user_data;
 }
 
-gpointer
-gmenu_tree_get_user_data (GMenuTree *tree)
+static void
+gmenu_tree_class_init (GMenuTreeClass *klass)
 {
-  g_return_val_if_fail (tree != NULL, NULL);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->get_property = gmenu_tree_get_property;
+  gobject_class->set_property = gmenu_tree_set_property;
+  gobject_class->finalize = gmenu_tree_finalize;
+
+  /**
+   * GMenuTree:name
+   *
+   * The name of the menu file; must be a relative path.  See
+   * the Desktop Menu specification.
+   */
+  g_object_class_install_property (gobject_class,
+                                   PROP_NAME,
+                                   g_param_spec_string ("name", "", "",
+							NULL,
+                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+  /**
+   * GMenuTree:flags
+   *
+   * Flags controlling the content of the menu.
+   */
+  g_object_class_install_property (gobject_class,
+                                   PROP_FLAGS,
+                                   g_param_spec_flags ("flags", "", "",
+						       GMENU_TYPE_TREE_FLAGS,
+						       GMENU_TREE_FLAGS_NONE,
+						       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
-  return tree->user_data;
 }
 
+
 const char *
 gmenu_tree_get_menu_file (GMenuTree *tree)
 {
@@ -825,7 +867,7 @@ gmenu_tree_directory_get_tree (GMenuTreeDirectory *directory)
   root = (GMenuTreeDirectoryRoot *) directory;
 
   if (root->tree)
-    gmenu_tree_ref (root->tree);
+    g_object_ref (root->tree);
 
   return root->tree;
 }
diff --git a/libmenu/gmenu-tree.h b/libmenu/gmenu-tree.h
index 755fc6f..825b025 100644
--- a/libmenu/gmenu-tree.h
+++ b/libmenu/gmenu-tree.h
@@ -28,7 +28,23 @@
 
 G_BEGIN_DECLS
 
-typedef struct GMenuTree          GMenuTree;
+#define GMENU_TYPE_TREE         (gmenu_tree_get_type ())
+#define GMENU_TREE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GMENU_TYPE_TREE, GMenuTree))
+#define GMENU_TREE_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), GMENU_TYPE_TREE, GMenuTreeClass))
+#define GMENU_IS_TREE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GMENU_TYPE_TREE))
+#define GMENU_IS_TREE_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GMENU_TYPE_TREE))
+#define GMENU_TREE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_DESKTOP_APP_INFO, GMenuTreeClass))
+
+typedef struct _GMenuTree        GMenuTree;
+typedef struct _GMenuTreeClass   GMenuTreeClass;
+
+struct _GMenuTreeClass
+{
+  GObjectClass parent_class;
+};
+
+GType            gmenu_tree_get_type          (void) G_GNUC_CONST;
+
 typedef struct GMenuTreeItem      GMenuTreeItem;
 typedef struct GMenuTreeDirectory GMenuTreeDirectory;
 typedef struct GMenuTreeEntry     GMenuTreeEntry;
@@ -71,14 +87,6 @@ GType gmenu_tree_flags_get_type (void);
 GMenuTree *gmenu_tree_new (const char     *menu_file,
 			   GMenuTreeFlags  flags);
 
-GMenuTree *gmenu_tree_ref   (GMenuTree *tree);
-void       gmenu_tree_unref (GMenuTree *tree);
-
-void     gmenu_tree_set_user_data (GMenuTree       *tree,
-				   gpointer        user_data,
-				   GDestroyNotify  dnotify);
-gpointer gmenu_tree_get_user_data (GMenuTree       *tree);
-
 const char         *gmenu_tree_get_menu_file           (GMenuTree  *tree);
 GMenuTreeDirectory *gmenu_tree_get_root_directory      (GMenuTree  *tree);
 GMenuTreeDirectory *gmenu_tree_get_directory_from_path (GMenuTree  *tree,
diff --git a/libmenu/menu-monitor.c b/libmenu/menu-monitor.c
index 09d416d..8895b49 100644
--- a/libmenu/menu-monitor.c
+++ b/libmenu/menu-monitor.c
@@ -203,18 +203,9 @@ static MenuMonitor *
 register_monitor (const char *path,
 		  gboolean    is_directory)
 {
-  static gboolean  initted = FALSE;
   MenuMonitor     *retval;
   GFile           *file;
 
-  if (!initted)
-    {
-      /* This is the only place where we're using GObject and the app can't
-       * know we're using it, so we need to init the type system ourselves. */
-      g_type_init ();
-      initted = TRUE;
-    }
-
   retval = g_new0 (MenuMonitor, 1);
 
   retval->path         = g_strdup (path);
diff --git a/util/gnome-menus-ls.py b/util/gnome-menus-ls.py
index d752552..83660e0 100644
--- a/util/gnome-menus-ls.py
+++ b/util/gnome-menus-ls.py
@@ -23,7 +23,7 @@
 import optparse
 import sys
 
-import gmenu
+import gi.GMenu
 
 def print_entry(entry, path):
     if entry.get_is_excluded():
diff --git a/util/test-menu-spec.c b/util/test-menu-spec.c
index c26754d..2be39c7 100644
--- a/util/test-menu-spec.c
+++ b/util/test-menu-spec.c
@@ -188,6 +188,8 @@ main (int argc, char **argv)
   GMenuTreeDirectory *root;
   GMenuTreeFlags      flags;
 
+  g_type_init ();
+
 #if 0
   /* See comment when defining _() at the top of this file. */
   bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
@@ -238,7 +240,7 @@ main (int argc, char **argv)
 
     }
 
-  gmenu_tree_unref (tree);
+  g_object_unref (tree);
 
   return 0;
 }



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