[gnome-todo] task-list-view: add a sorting function



commit ef4eea04cdc0423390ca08663c9caf34c56bd404
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Thu Feb 11 18:26:59 2016 -0200

    task-list-view: add a sorting function
    
    Reflecting the public API on header functions, add an
    equivalent functionality on sorting function to allow
    plugins and third-party consumers of the API sort the
    tasklist differently.

 src/gtd-task-list-view.c |   79 ++++++++++++++++++++++++++++++++++++++++++++++
 src/gtd-task-list-view.h |   23 ++++++++++++-
 2 files changed, 100 insertions(+), 2 deletions(-)
---
diff --git a/src/gtd-task-list-view.c b/src/gtd-task-list-view.c
index 3f10bde..75176ce 100644
--- a/src/gtd-task-list-view.c
+++ b/src/gtd-task-list-view.c
@@ -58,8 +58,13 @@ typedef struct
   /* action */
   GActionGroup          *action_group;
 
+  /* Custom header function data */
   GtdTaskListViewHeaderFunc header_func;
   gpointer                  header_user_data;
+
+  /* Custom sorting function data */
+  GtdTaskListViewSortFunc sort_func;
+  gpointer                sort_user_data;
 } GtdTaskListViewPrivate;
 
 struct _GtdTaskListView
@@ -173,6 +178,37 @@ internal_header_func (GtdTaskRow      *row,
                            view->priv->header_user_data);
 }
 
+static gint
+internal_sort_func (GtdTaskRow      *row1,
+                    GtdTaskRow      *row2,
+                    GtdTaskListView *view)
+{
+  GtdTask *row1_task;
+  GtdTask *row2_task;
+
+  if (!view->priv->sort_func)
+    return 0;
+
+  if (gtd_task_row_get_new_task_mode (row1))
+    return 1;
+  else if (gtd_task_row_get_new_task_mode (row2))
+    return -1;
+
+  row1_task = row2_task = NULL;
+
+  if (row1)
+    row1_task = gtd_task_row_get_task (row1);
+
+  if (row2)
+    row2_task = gtd_task_row_get_task (row2);
+
+  return view->priv->sort_func (GTK_LIST_BOX_ROW (row1),
+                                row1_task,
+                                GTK_LIST_BOX_ROW (row2),
+                                row2_task,
+                                view->priv->header_user_data);
+}
+
 static void
 update_font_color (GtdTaskListView *view)
 {
@@ -1276,3 +1312,46 @@ gtd_task_list_view_set_header_func (GtdTaskListView           *view,
                                     NULL);
     }
 }
+
+/**
+ * gtd_task_list_view_set_sort_func:
+ * @view: a #GtdTaskListView
+ * @func: (closure user_data) (scope call) (nullable): the sort function
+ * @user_data: data passed to @func
+ *
+ * Sets @func as the sorting function of @view.
+ *
+ * Do not unref nor free any of the passed data.
+ */
+void
+gtd_task_list_view_set_sort_func (GtdTaskListView         *view,
+                                  GtdTaskListViewSortFunc  func,
+                                  gpointer                 user_data)
+{
+  GtdTaskListViewPrivate *priv;
+
+  g_return_if_fail (GTD_IS_TASK_LIST_VIEW (view));
+
+  priv = gtd_task_list_view_get_instance_private (view);
+
+  if (func)
+    {
+      priv->sort_func = func;
+      priv->header_user_data = user_data;
+
+      gtk_list_box_set_sort_func (priv->listbox,
+                                  (GtkListBoxSortFunc) internal_sort_func,
+                                  view,
+                                  NULL);
+    }
+  else
+    {
+      priv->sort_func = NULL;
+      priv->sort_user_data = NULL;
+
+      gtk_list_box_set_sort_func (priv->listbox,
+                                  (GtkListBoxSortFunc) gtd_task_list_view__listbox_sort_func,
+                                  NULL,
+                                  NULL);
+    }
+}
diff --git a/src/gtd-task-list-view.h b/src/gtd-task-list-view.h
index 4a4b039..e04ab37 100644
--- a/src/gtd-task-list-view.h
+++ b/src/gtd-task-list-view.h
@@ -37,8 +37,7 @@ G_DECLARE_FINAL_TYPE (GtdTaskListView, gtd_task_list_view, GTD, TASK_LIST_VIEW,
  * @before_task: the #GtdTask that @before represents
  * @user_data: (closure): user data
  *
- * Will be called when the primary or secondary action of @notification
- * is executed.
+ * The header function called on every task.
  */
 typedef void (*GtdTaskListViewHeaderFunc) (GtkListBoxRow        *row,
                                            GtdTask              *row_task,
@@ -46,6 +45,22 @@ typedef void (*GtdTaskListViewHeaderFunc) (GtkListBoxRow        *row,
                                            GtdTask              *before_task,
                                            gpointer              user_data);
 
+/**
+ * GtdTaskListViewSortFunc:
+ * @row: the current #GtkListBoxRow
+ * @row_task: the #GtdTask that @row represents
+ * @before: the #GtkListBoxRow before @row
+ * @before_task: the #GtdTask that @before represents
+ * @user_data: (closure): user data
+ *
+ * The sorting function called on every task.
+ */
+typedef gint (*GtdTaskListViewSortFunc)   (GtkListBoxRow        *row,
+                                           GtdTask              *row_task,
+                                           GtkListBoxRow        *before,
+                                           GtdTask              *before_task,
+                                           gpointer              user_data);
+
 GtkWidget*                gtd_task_list_view_new                (void);
 
 GList*                    gtd_task_list_view_get_list           (GtdTaskListView        *view);
@@ -77,6 +92,10 @@ void                      gtd_task_list_view_set_header_func    (GtdTaskListView
                                                                  GtdTaskListViewHeaderFunc  func,
                                                                  gpointer                   user_data);
 
+void                      gtd_task_list_view_set_sort_func      (GtdTaskListView           *view,
+                                                                 GtdTaskListViewSortFunc    func,
+                                                                 gpointer                   user_data);
+
 G_END_DECLS
 
 #endif /* GTD_TASK_LIST_VIEW_H */


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