[gnome-menus/wip/gobject: 5/15] Fold sorting into GMenuTreeFlags



commit 3749031178ee1e53869504ece5a4dcc7e979cc02
Author: Colin Walters <walters verbum org>
Date:   Sun Apr 17 07:10:01 2011 -0400

    Fold sorting into GMenuTreeFlags
    
    There's only two sorts right now, and so we can make one the default
    and select the other with the flags.
    
    Drop the ability to set the sort at runtime; this never was compatible
    with the current GMenuTree caching, and also I'm trying to move
    GMenuTree towards being immutable.

 libmenu/gmenu-tree.c |   68 ++++++++++----------------------------------
 libmenu/gmenu-tree.h |   18 +----------
 python/gmenu.c       |   77 ++------------------------------------------------
 3 files changed, 20 insertions(+), 143 deletions(-)
---
diff --git a/libmenu/gmenu-tree.c b/libmenu/gmenu-tree.c
index 1abefa0..5689bd4 100644
--- a/libmenu/gmenu-tree.c
+++ b/libmenu/gmenu-tree.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003, 2004 Red Hat, Inc.
+ * Copyright (C) 2003, 2004, 2011 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -51,7 +51,6 @@ struct GMenuTree
   char *canonical_path;
 
   GMenuTreeFlags flags;
-  GMenuTreeSortKey sort_key;
 
   GSList *menu_file_monitors;
 
@@ -618,8 +617,6 @@ gmenu_tree_lookup (const char     *menu_file,
 
   g_return_val_if_fail (menu_file != NULL, NULL);
 
-  flags &= GMENU_TREE_FLAGS_MASK;
-
   if (g_path_is_absolute (menu_file))
     retval = gmenu_tree_lookup_absolute (menu_file, flags);
   else
@@ -644,8 +641,6 @@ gmenu_tree_new (GMenuTreeType   type,
   tree->flags    = flags;
   tree->refcount = 1;
 
-  tree->sort_key = GMENU_TREE_SORT_NAME;
-
   if (tree->type == GMENU_TREE_BASENAME)
     {
       g_assert (canonical == FALSE);
@@ -867,31 +862,6 @@ gmenu_tree_get_directory_from_path (GMenuTree  *tree,
   return directory ? gmenu_tree_item_ref (directory) : NULL;
 }
 
-GMenuTreeSortKey
-gmenu_tree_get_sort_key (GMenuTree *tree)
-{
-  g_return_val_if_fail (tree != NULL, GMENU_TREE_SORT_NAME);
-  g_return_val_if_fail (tree->refcount > 0, GMENU_TREE_SORT_NAME);
-
-  return tree->sort_key;
-}
-
-void
-gmenu_tree_set_sort_key (GMenuTree        *tree,
-			 GMenuTreeSortKey  sort_key)
-{
-  g_return_if_fail (tree != NULL);
-  g_return_if_fail (tree->refcount > 0);
-  g_return_if_fail (sort_key >= GMENU_TREE_SORT_FIRST);
-  g_return_if_fail (sort_key <= GMENU_TREE_SORT_LAST);
-
-  if (sort_key == tree->sort_key)
-    return;
-
-  tree->sort_key = sort_key;
-  gmenu_tree_force_rebuild (tree);
-}
-
 void
 gmenu_tree_add_monitor (GMenuTree            *tree,
                        GMenuTreeChangedFunc   callback,
@@ -1530,7 +1500,7 @@ gmenu_tree_item_get_user_data (GMenuTreeItem *item)
 
 static inline const char *
 gmenu_tree_item_compare_get_name_helper (GMenuTreeItem    *item,
-					 GMenuTreeSortKey  sort_key)
+					 GMenuTreeFlags    flags)
 {
   const char *name;
 
@@ -1546,25 +1516,17 @@ gmenu_tree_item_compare_get_name_helper (GMenuTreeItem    *item,
       break;
 
     case GMENU_TREE_ITEM_ENTRY:
-      switch (sort_key)
-	{
-	case GMENU_TREE_SORT_NAME:
-	  name = desktop_entry_get_name (GMENU_TREE_ENTRY (item)->desktop_entry);
-	  break;
-	case GMENU_TREE_SORT_DISPLAY_NAME:
-	  name = g_app_info_get_display_name (G_APP_INFO (gmenu_tree_entry_get_app_info (GMENU_TREE_ENTRY (item))));
-	  break;
-	default:
-	  g_assert_not_reached ();
-	  break;
-	}
+      if (flags & GMENU_TREE_FLAGS_SORT_DISPLAY_NAME)
+	name = g_app_info_get_display_name (G_APP_INFO (gmenu_tree_entry_get_app_info (GMENU_TREE_ENTRY (item))));
+      else
+	name = desktop_entry_get_name (GMENU_TREE_ENTRY (item)->desktop_entry);
       break;
 
     case GMENU_TREE_ITEM_ALIAS:
       {
         GMenuTreeItem *dir;
         dir = GMENU_TREE_ITEM (GMENU_TREE_ALIAS (item)->directory);
-        name = gmenu_tree_item_compare_get_name_helper (dir, sort_key);
+        name = gmenu_tree_item_compare_get_name_helper (dir, flags);
       }
       break;
 
@@ -1581,16 +1543,16 @@ gmenu_tree_item_compare_get_name_helper (GMenuTreeItem    *item,
 static int
 gmenu_tree_item_compare (GMenuTreeItem *a,
 			 GMenuTreeItem *b,
-			 gpointer       sort_key_p)
+			 gpointer       flags_p)
 {
   const char       *name_a;
   const char       *name_b;
-  GMenuTreeSortKey  sort_key;
+  GMenuTreeFlags    flags;
 
-  sort_key = GPOINTER_TO_INT (sort_key_p);
+  flags = GPOINTER_TO_INT (flags_p);
 
-  name_a = gmenu_tree_item_compare_get_name_helper (a, sort_key);
-  name_b = gmenu_tree_item_compare_get_name_helper (b, sort_key);
+  name_a = gmenu_tree_item_compare_get_name_helper (a, flags);
+  name_b = gmenu_tree_item_compare_get_name_helper (b, flags);
 
   return g_utf8_collate (name_a, name_b);
 }
@@ -4069,7 +4031,7 @@ merge_subdirs (GMenuTree          *tree,
 
   subdirs = g_slist_sort_with_data (subdirs,
 				    (GCompareDataFunc) gmenu_tree_item_compare,
-				     GINT_TO_POINTER (GMENU_TREE_SORT_NAME));
+				    0);
 
   tmp = subdirs;
   while (tmp != NULL)
@@ -4114,7 +4076,7 @@ merge_entries (GMenuTree          *tree,
 
   entries = g_slist_sort_with_data (entries,
 				    (GCompareDataFunc) gmenu_tree_item_compare,
-				    GINT_TO_POINTER (tree->sort_key));
+				    GINT_TO_POINTER (tree->flags));
 
   tmp = entries;
   while (tmp != NULL)
@@ -4163,7 +4125,7 @@ merge_subdirs_and_entries (GMenuTree          *tree,
 
   items = g_slist_sort_with_data (items,
 				  (GCompareDataFunc) gmenu_tree_item_compare,
-				  GINT_TO_POINTER (tree->sort_key));
+				  GINT_TO_POINTER (tree->flags));
 
   tmp = items;
   while (tmp != NULL)
diff --git a/libmenu/gmenu-tree.h b/libmenu/gmenu-tree.h
index a3990ae..d1fcdef 100644
--- a/libmenu/gmenu-tree.h
+++ b/libmenu/gmenu-tree.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004 Red Hat, Inc.
+ * Copyright (C) 2004,2011 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -63,17 +63,9 @@ typedef enum
   GMENU_TREE_FLAGS_SHOW_EMPTY          = 1 << 1,
   GMENU_TREE_FLAGS_INCLUDE_NODISPLAY   = 1 << 2,
   GMENU_TREE_FLAGS_SHOW_ALL_SEPARATORS = 1 << 3,
-  GMENU_TREE_FLAGS_MASK                = 0x0f
+  GMENU_TREE_FLAGS_SORT_DISPLAY_NAME   = 1 << 4
 } GMenuTreeFlags;
 
-typedef enum
-{
-  #define GMENU_TREE_SORT_FIRST GMENU_TREE_SORT_NAME
-  GMENU_TREE_SORT_NAME = 0,
-  GMENU_TREE_SORT_DISPLAY_NAME
-  #define GMENU_TREE_SORT_LAST GMENU_TREE_SORT_DISPLAY_NAME
-} GMenuTreeSortKey;
-
 GMenuTree *gmenu_tree_lookup (const char     *menu_file,
 			      GMenuTreeFlags  flags);
 
@@ -90,12 +82,6 @@ GMenuTreeDirectory *gmenu_tree_get_root_directory      (GMenuTree  *tree);
 GMenuTreeDirectory *gmenu_tree_get_directory_from_path (GMenuTree  *tree,
 							const char *path);
 
-GMenuTreeSortKey     gmenu_tree_get_sort_key (GMenuTree        *tree);
-void                 gmenu_tree_set_sort_key (GMenuTree        *tree,
-					      GMenuTreeSortKey  sort_key);
-
-
-
 gpointer gmenu_tree_item_ref   (gpointer item);
 void     gmenu_tree_item_unref (gpointer item);
 
diff --git a/python/gmenu.c b/python/gmenu.c
index d66e63e..0c2b476 100644
--- a/python/gmenu.c
+++ b/python/gmenu.c
@@ -1522,56 +1522,6 @@ pygmenu_tree_get_directory_from_path (PyObject *self,
   return (PyObject *) retval;
 }
 
-static PyObject *
-pygmenu_tree_get_sort_key (PyObject *self,
-			   PyObject *args)
-{
-  PyGMenuTree *tree;
-  PyObject    *retval;
-
-  if (args != NULL)
-    {
-      if (!PyArg_ParseTuple (args, ":gmenu.Tree.get_sort_key"))
-	return NULL;
-    }
-
-  tree = (PyGMenuTree *) self;
-
-  switch (gmenu_tree_get_sort_key (tree->tree))
-    {
-    case GMENU_TREE_SORT_NAME:
-      retval = lookup_item_type_str ("SORT_NAME");
-      break;
-
-    case GMENU_TREE_SORT_DISPLAY_NAME:
-      retval = lookup_item_type_str ("SORT_DISPLAY_NAME");
-      break;
-
-    default:
-      g_assert_not_reached ();
-      break;
-    }
-
-  return (PyObject *) retval;
-}
-
-static PyObject *
-pygmenu_tree_set_sort_key (PyObject *self,
-			   PyObject *args)
-{
-  PyGMenuTree *tree;
-  int          sort_key;
-
-  if (!PyArg_ParseTuple (args, "i:gmenu.Tree.set_sort_key", &sort_key))
-    return NULL;
-
-  tree = (PyGMenuTree *) self;
-
-  gmenu_tree_set_sort_key (tree->tree, sort_key);
-
-  return Py_None;
-}
-
 static PyGMenuTreeCallback *
 pygmenu_tree_callback_new (PyObject *tree,
 			   PyObject *callback,
@@ -1737,7 +1687,7 @@ pygmenu_tree_getattro (PyGMenuTree *self,
 
       if (!strcmp (attr, "__members__"))
 	{
-	  return Py_BuildValue ("[sss]", "root", "menu_file", "sort_key");
+	  return Py_BuildValue ("[sss]", "root", "menu_file" );
 	}
       else if (!strcmp (attr, "root"))
 	{
@@ -1747,10 +1697,6 @@ pygmenu_tree_getattro (PyGMenuTree *self,
 	{
 	  return pygmenu_tree_get_menu_file ((PyObject *) self, NULL);
 	}
-      else if (!strcmp (attr, "sort_key"))
-	{
-	  return pygmenu_tree_get_sort_key ((PyObject *) self, NULL);
-	}
     }
 
   return PyObject_GenericGetAttr ((PyObject *) self, py_attr);
@@ -1771,20 +1717,7 @@ pygmenu_tree_setattro (PyGMenuTree *self,
 
       attr = PyString_AsString (py_attr);
 
-      if (!strcmp (attr, "sort_key"))
-	{
-	  if (PyInt_Check (py_value))
-	    {
-	      int sort_key;
-
-	      sort_key = PyInt_AsLong (py_value);
-	      if (sort_key < GMENU_TREE_SORT_FIRST || sort_key > GMENU_TREE_SORT_LAST)
-		return -1;
-	      gmenu_tree_set_sort_key (tree->tree, sort_key);
-
-	      return 0;
-	    }
-	}
+      (void) attr;
     }
 
   return -1;
@@ -1795,8 +1728,6 @@ static struct PyMethodDef pygmenu_tree_methods[] =
   { "get_menu_file",           pygmenu_tree_get_menu_file,           METH_VARARGS },
   { "get_root_directory",      pygmenu_tree_get_root_directory,      METH_VARARGS },
   { "get_directory_from_path", pygmenu_tree_get_directory_from_path, METH_VARARGS },
-  { "get_sort_key",            pygmenu_tree_get_sort_key,            METH_VARARGS },
-  { "set_sort_key",            pygmenu_tree_set_sort_key,            METH_VARARGS },
   { "add_monitor",             pygmenu_tree_add_monitor,             METH_VARARGS },
   { "remove_monitor",          pygmenu_tree_remove_monitor,          METH_VARARGS },
   { NULL,                      NULL,                                 0            }
@@ -1948,7 +1879,5 @@ initgmenu (void)
   PyModule_AddIntConstant (mod, "FLAGS_SHOW_EMPTY",          GMENU_TREE_FLAGS_SHOW_EMPTY);
   PyModule_AddIntConstant (mod, "FLAGS_INCLUDE_NODISPLAY",   GMENU_TREE_FLAGS_INCLUDE_NODISPLAY);
   PyModule_AddIntConstant (mod, "FLAGS_SHOW_ALL_SEPARATORS", GMENU_TREE_FLAGS_SHOW_ALL_SEPARATORS);
-
-  PyModule_AddIntConstant (mod, "SORT_NAME",         GMENU_TREE_SORT_NAME);
-  PyModule_AddIntConstant (mod, "SORT_DISPLAY_NAME", GMENU_TREE_SORT_DISPLAY_NAME);
+  PyModule_AddIntConstant (mod, "FLAGS_SORT_DISPLAY_NAME",   GMENU_TREE_FLAGS_SORT_DISPLAY_NAME);
 }



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