Turn MilkTask into a proper GObject and use it directly in MilkTaskModel
authorTravis Reitter <treitter@torizo.(none)>
Sat, 24 Oct 2009 04:38:52 +0000 (21:38 -0700)
committerTravis Reitter <treitter@gmail.com>
Fri, 4 Dec 2009 06:01:13 +0000 (22:01 -0800)
src/Makefile.am
src/milk-main-window.c
src/milk-task-model.c
src/milk-task.c [new file with mode: 0644]
src/milk-task.h [new file with mode: 0644]

index ee21ad6..edcdf3e 100644 (file)
@@ -12,6 +12,8 @@ milk_SOURCES = \
        milk-main.h \
        milk-main-window.c \
        milk-main-window.h \
+       milk-task.c \
+       milk-task.h \
        milk-task-model.c \
        milk-task-model.h
 
index 91faeed..be74c98 100644 (file)
@@ -25,6 +25,7 @@
 #include <hildon/hildon.h>
 
 #include "milk-main-window.h"
+#include "milk-task.h"
 #include "milk-task-model.h"
 
 G_DEFINE_TYPE (MilkMainWindow, milk_main_window, HILDON_TYPE_WINDOW)
@@ -403,6 +404,26 @@ create_menu (gpointer user_data)
 }
 
 static void
+contact_column_render_func (GtkCellLayout   *cell_layout,
+                            GtkCellRenderer *renderer,
+                            GtkTreeModel    *model,
+                            GtkTreeIter     *iter,
+                            gpointer         user_data)
+{
+        MilkTask *task;
+        char *title;
+
+        gtk_tree_model_get (
+                        model, iter, MILK_TASK_MODEL_COLUMN_TASK, &task, -1);
+
+        g_object_get (task, "title", &title, NULL);
+        g_object_set (renderer, "text", title, NULL);
+
+        g_free (title);
+        g_object_unref (task);
+}
+
+static void
 milk_main_window_constructed (GObject* object)
 {
         MilkMainWindow *self = MILK_MAIN_WINDOW (object);
@@ -434,18 +455,21 @@ milk_main_window_constructed (GObject* object)
         model = GTK_TREE_MODEL (milk_task_model_new ());
         w = hildon_touch_selector_new ();
 
+
         renderer = gtk_cell_renderer_text_new ();
         g_object_set (renderer,
                         "ellipsize", PANGO_ELLIPSIZE_END,
                         NULL);
-        col = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (w),
-                        model, renderer,
-                        "text", MILK_TASK_MODEL_COLUMN_TITLE,
+
+        col = hildon_touch_selector_append_column (
+                        HILDON_TOUCH_SELECTOR (w), model, NULL, NULL);
+        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (col), renderer, TRUE);
+        gtk_cell_layout_set_cell_data_func (
+                        GTK_CELL_LAYOUT (col), renderer,
+                        (GtkTreeCellDataFunc) contact_column_render_func, self,
                         NULL);
         g_object_unref (model);
 
-        hildon_touch_selector_column_set_text_column (
-                        col, MILK_TASK_MODEL_COLUMN_TITLE);
         hildon_touch_selector_set_column_selection_mode (
                         HILDON_TOUCH_SELECTOR (w),
                         HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
index e5bdf5e..5550537 100644 (file)
@@ -25,6 +25,7 @@
 #include <hildon/hildon.h>
 
 #include "milk-task-model.h"
+#include "milk-task.h"
 
 static void
 milk_task_model_tree_model_init (GtkTreeModelIface *iface);
@@ -44,22 +45,6 @@ struct _MilkTaskModelPrivate
         GtkListStore *store;
 };
 
-/* FIXME: relocate this to its own class */
-typedef struct {
-        gint id;
-        gint priority;
-        char *title;
-} MilkTask;
-
-/* FIXME: don't hard-code this */
-static MilkTask tasks[] = {
-        { 0, 1, "Remember the milk"},
-        { 2, 1, "Make a Maemo 5 RTM client"},
-        { 6, 3, "Get a haircut"},
-        { 9, 2, "Keep it real"},
-        { 5, 3, "Invent smellovision"},
-};
-
 static GtkTreeModelFlags
 milk_task_model_get_flags (GtkTreeModel *model)
 {
@@ -77,14 +62,8 @@ milk_task_model_get_column_type (GtkTreeModel *model,
                                  gint          column)
 {
         switch (column) {
-                case MILK_TASK_MODEL_COLUMN_ID:
-                        return G_TYPE_INT;
-
-                case MILK_TASK_MODEL_COLUMN_PRIORITY:
-                        return G_TYPE_UINT;
-
-                case MILK_TASK_MODEL_COLUMN_TITLE:
-                        return G_TYPE_STRING;
+                case MILK_TASK_MODEL_COLUMN_TASK:
+                        return MILK_TYPE_TASK;
 
                 default:
                         g_warning (G_STRLOC ": invalid column: %d", column);
@@ -133,7 +112,6 @@ milk_task_model_get_value (GtkTreeModel *model,
 
         g_return_if_fail (MILK_IS_TASK_MODEL (model));
         g_return_if_fail (iter);
-        g_return_if_fail (column >= 0);
         g_return_if_fail (value);
 
         priv = MILK_TASK_MODEL_PRIVATE (model);
@@ -310,15 +288,15 @@ milk_task_model_constructed (GObject* object)
 
         /* Populate model */
         for (i = 0; i < G_N_ELEMENTS (tasks); i++) {
+                MilkTask *task;
+
+                task = milk_task_new (
+                                tasks[i].id, tasks[i].title, tasks[i].priority);
+
                 gtk_list_store_append (priv->store, &iter);
                 gtk_list_store_set (
                                 priv->store, &iter,
-                                MILK_TASK_MODEL_COLUMN_ID,
-                                tasks[i].id,
-                                MILK_TASK_MODEL_COLUMN_PRIORITY,
-                                tasks[i].priority,
-                                MILK_TASK_MODEL_COLUMN_TITLE,
-                                tasks[i].title,
+                                MILK_TASK_MODEL_COLUMN_TASK, task,
                                 -1);
         }
 }
@@ -345,8 +323,7 @@ milk_task_model_init (MilkTaskModel *self)
                         self, MILK_TYPE_TASK_MODEL, MilkTaskModelPrivate);
 
         priv->store = gtk_list_store_new (
-                        MILK_TASK_MODEL_N_COLUMNS, G_TYPE_INT, G_TYPE_UINT,
-                        G_TYPE_STRING);
+                        MILK_TASK_MODEL_N_COLUMNS, MILK_TYPE_TASK);
 }
 
 static void
@@ -369,6 +346,5 @@ milk_task_model_tree_model_init (GtkTreeModelIface *iface)
 MilkTaskModel*
 milk_task_model_new ()
 {
-        return g_object_new (MILK_TYPE_TASK_MODEL,
-                             NULL);
+        return g_object_new (MILK_TYPE_TASK_MODEL, NULL);
 }
diff --git a/src/milk-task.c b/src/milk-task.c
new file mode 100644 (file)
index 0000000..fffbeb2
--- /dev/null
@@ -0,0 +1,220 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Authors: Travis Reitter <treitter@gmail.com>
+ */
+
+#include <config.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+#include <hildon/hildon.h>
+
+#include "milk-task.h"
+
+G_DEFINE_TYPE (MilkTask, milk_task, G_TYPE_OBJECT);
+
+/* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
+#define MILK_TASK_PRIVATE(o) ((MILK_TASK ((o)))->priv)
+
+struct _MilkTaskPrivate
+{
+        char *id;
+        gint priority;
+        char *title;
+};
+
+enum {
+        PROP_ID = 1,
+        PROP_TITLE,
+        PROP_PRIORITY,
+};
+
+
+static void
+task_set_id (MilkTask   *task,
+             const char *id)
+{
+        MilkTaskPrivate *priv;
+
+        g_return_if_fail (MILK_IS_TASK (task));
+        g_return_if_fail (id && !g_str_equal (id, ""));
+
+        priv = MILK_TASK_PRIVATE (task);
+        g_free (priv->id);
+        priv->id = g_strdup (id);
+}
+
+void
+milk_task_set_title (MilkTask   *task,
+                     const char *title)
+{
+        MilkTaskPrivate *priv;
+
+        g_return_if_fail (MILK_IS_TASK (task));
+
+        priv = MILK_TASK_PRIVATE (task);
+        g_free (priv->title);
+        priv->title = g_strdup (title);
+}
+
+void
+milk_task_set_priority (MilkTask *task,
+                        gint      priority)
+{
+        MilkTaskPrivate *priv;
+
+        g_return_if_fail (MILK_IS_TASK (task));
+        /* FIXME: set constraints on the allowed priority values */
+
+        priv = MILK_TASK_PRIVATE (task);
+        priv->priority = priority;
+}
+
+static void
+milk_task_get_property (GObject    *object,
+                        guint       property_id,
+                        GValue     *value,
+                        GParamSpec *pspec)
+{
+        MilkTaskPrivate *priv = MILK_TASK_PRIVATE (object);
+
+        switch (property_id)
+        {
+                case PROP_ID:
+                        g_value_set_string (value, priv->id);
+                break;
+
+                case PROP_TITLE:
+                        g_value_set_string (value, priv->title);
+                break;
+
+                case PROP_PRIORITY:
+                        g_value_set_int (value, priv->priority);
+                break;
+
+                default:
+                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
+                                        pspec);
+        }
+}
+
+static void
+milk_task_set_property (GObject      *object,
+                        guint         property_id,
+                        const GValue *value,
+                        GParamSpec   *pspec)
+{
+        MilkTaskPrivate *priv;
+        MilkTask *task;
+
+        task = MILK_TASK (object);
+        priv = MILK_TASK_PRIVATE (task);
+
+        switch (property_id)
+        {
+                case PROP_ID:
+                        task_set_id (task, g_value_get_string (value));
+                break;
+
+                case PROP_TITLE:
+                        milk_task_set_title (task, g_value_get_string (value));
+                break;
+
+                case PROP_PRIORITY:
+                        milk_task_set_priority (task, g_value_get_int (value));
+                break;
+
+                default:
+                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
+                                        pspec);
+        }
+}
+
+static void
+milk_task_finalize (GObject *object)
+{
+        MilkTaskPrivate *priv = MILK_TASK_PRIVATE (object);
+
+        g_free (priv->id);
+        g_free (priv->title);
+}
+
+static void
+milk_task_class_init (MilkTaskClass *klass)
+{
+        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+        g_type_class_add_private (klass, sizeof (MilkTaskPrivate));
+
+        object_class->get_property = milk_task_get_property;
+        object_class->set_property = milk_task_set_property;
+        object_class->finalize = milk_task_finalize;
+
+        g_object_class_install_property
+                (object_class,
+                 PROP_ID,
+                 g_param_spec_string
+                         ("id",
+                          "Task ID",
+                          "A globally-unique identifier for the task.",
+                          "invalid-ID",
+                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+
+        g_object_class_install_property
+                (object_class,
+                 PROP_TITLE,
+                 g_param_spec_string
+                         ("title",
+                          "Task title",
+                          "The description of the task to be performed.",
+                          "Untitled Task",
+                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+        g_object_class_install_property
+                (object_class,
+                 PROP_PRIORITY,
+                 g_param_spec_int
+                         ("priority",
+                          "Task priority",
+                          "The user-valued priority of the task.",
+                          0, G_MAXINT, 0,
+                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+}
+
+static void
+milk_task_init (MilkTask *self)
+{
+        MilkTaskPrivate *priv;
+
+        self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (
+                        self, MILK_TYPE_TASK, MilkTaskPrivate);
+}
+
+MilkTask*
+milk_task_new (const char *id,
+               const char *title,
+               gint priority)
+{
+        return g_object_new (
+                        MILK_TYPE_TASK,
+                        "id", id,
+                        "title", title,
+                        "priority", priority,
+                        NULL);
+}
diff --git a/src/milk-task.h b/src/milk-task.h
new file mode 100644 (file)
index 0000000..36efa9c
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Authors: Travis Reitter <treitter@gmail.com>
+ */
+
+#ifndef _MILK_TASK_H
+#define _MILK_TASK_H
+
+G_BEGIN_DECLS
+
+#define MILK_TYPE_TASK milk_task_get_type()
+
+#define MILK_TASK(obj) \
+                (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+                MILK_TYPE_TASK, MilkTask))
+
+#define MILK_TASK_CLASS(klass) \
+                (G_TYPE_CHECK_CLASS_CAST ((klass), \
+                MILK_TYPE_TASK, MilkTaskClass))
+
+#define MILK_IS_TASK(obj) \
+                (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+                MILK_TYPE_TASK))
+
+#define MILK_IS_TASK_CLASS(klass) \
+                (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+                MILK_TYPE_TASK))
+
+#define MILK_TASK_GET_CLASS(obj) \
+                (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+                MILK_TYPE_TASK, MilkTaskClass))
+
+typedef struct _MilkTask MilkTask;
+typedef struct _MilkTaskClass MilkTaskClass;
+typedef struct _MilkTaskPrivate MilkTaskPrivate;
+
+struct _MilkTask
+{
+        GObject parent;
+        MilkTaskPrivate *priv;
+};
+
+struct _MilkTaskClass
+{
+        GObjectClass parent_class;
+};
+
+GType milk_task_get_type (void);
+
+
+MilkTask* milk_task_new (const char *id, const char *title, gint priority);
+
+void milk_task_set_title (MilkTask *task, const char *title);
+void milk_task_set_priority (MilkTask *task, gint priority);
+
+#endif /* _MILK_TASK_H */