[rhythmbox] add an interface for reporting task progress



commit 4ae376525d154b1df3fb99996127250f7199e961
Author: Jonathan Matthew <jonathan d14n org>
Date:   Tue May 14 21:06:34 2013 +1000

    add an interface for reporting task progress
    
    RBTaskProgress consists of a few properties that can be used to
    construct a list of tasks with progress bars for each.  Tasks
    can also be cancellable.
    
    RBTaskProgressSimple is a GObject that implements the interface.
    It can be used to represent tasks that aren't bound to the lifecycle
    of an existing object.

 bindings/gi/Makefile.am       |    4 +
 lib/Makefile.am               |    6 +-
 lib/rb-task-progress-simple.c |  214 +++++++++++++++++++++++++++++++++++++++++
 lib/rb-task-progress-simple.h |   64 ++++++++++++
 lib/rb-task-progress.c        |  118 ++++++++++++++++++++++
 lib/rb-task-progress.h        |   67 +++++++++++++
 6 files changed, 472 insertions(+), 1 deletions(-)
---
diff --git a/bindings/gi/Makefile.am b/bindings/gi/Makefile.am
index ed35fe9..324bec0 100644
--- a/bindings/gi/Makefile.am
+++ b/bindings/gi/Makefile.am
@@ -50,6 +50,10 @@ rb_introspection_sources = \
                lib/rb-stock-icons.c \
                lib/rb-string-value-map.h \
                lib/rb-string-value-map.c \
+               lib/rb-task-progress.h \
+               lib/rb-task-progress.c \
+               lib/rb-task-progress-simple.h \
+               lib/rb-task-progress-simple.c \
                lib/rb-util.h \
                lib/rb-util.c \
                metadata/rb-ext-db.h \
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 9c8d553..1fb0483 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -11,7 +11,9 @@ rbinclude_HEADERS =                                   \
        rb-list-model.h                                 \
        rb-stock-icons.h                                \
        rb-string-value-map.h                           \
-       rb-util.h
+       rb-util.h                                       \
+       rb-task-progress.h                              \
+       rb-task-progress-simple.h
 
 librb_la_SOURCES =                                     \
        $(rbinclude_HEADERS)                            \
@@ -39,6 +41,8 @@ librb_la_SOURCES =                                    \
        rb-async-copy.h                                 \
        rb-chunk-loader.c                               \
        rb-chunk-loader.h                               \
+       rb-task-progress.c                              \
+       rb-task-progress-simple.c                       \
        rb-list-model.c
 
 INCLUDES =                                             \
diff --git a/lib/rb-task-progress-simple.c b/lib/rb-task-progress-simple.c
new file mode 100644
index 0000000..5b17c41
--- /dev/null
+++ b/lib/rb-task-progress-simple.c
@@ -0,0 +1,214 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ *  Copyright (C) 2013 Jonathan Matthew <jonathan d14n org>
+ *
+ *  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.
+ *
+ *  The Rhythmbox authors hereby grant permission for non-GPL compatible
+ *  GStreamer plugins to be used and distributed together with GStreamer
+ *  and Rhythmbox. This permission is above and beyond the permissions granted
+ *  by the GPL license by which Rhythmbox is covered. If you modify this code
+ *  you may extend this exception to your version of the code, but you are not
+ *  obligated to do so. If you do not wish to do so, delete this exception
+ *  statement from your 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.
+ *
+ */
+
+#include <config.h>
+
+#include <lib/rb-task-progress-simple.h>
+
+enum {
+       CANCEL_TASK,
+       LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = {0};
+
+struct _RBTaskProgressSimplePrivate
+{
+       char *label;
+       char *detail;
+       double progress;
+       RBTaskOutcome outcome;
+       gboolean notify;
+       gboolean cancellable;
+};
+
+static void rb_task_progress_simple_class_init (RBTaskProgressSimpleClass *klass);
+static void rb_task_progress_simple_init (RBTaskProgressSimple *task);
+static void rb_task_progress_simple_task_progress_init (RBTaskProgressInterface *iface);
+
+
+/**
+ * SECTION:rb-task-progress-simple
+ * @short_description: implementation of RBTaskProgress interface
+ *
+ * This implementation of #RBTaskProgress can be used to represent
+ * tasks that aren't bound to the lifecycle of an object that can
+ * implement the interface directly.
+ */
+
+G_DEFINE_TYPE_EXTENDED (RBTaskProgressSimple,
+                       rb_task_progress_simple,
+                       G_TYPE_OBJECT,
+                       0,
+                       G_IMPLEMENT_INTERFACE (RB_TYPE_TASK_PROGRESS, 
rb_task_progress_simple_task_progress_init));
+
+enum {
+       PROP_0,
+       PROP_TASK_LABEL,
+       PROP_TASK_DETAIL,
+       PROP_TASK_PROGRESS,
+       PROP_TASK_OUTCOME,
+       PROP_TASK_NOTIFY,
+       PROP_TASK_CANCELLABLE
+};
+
+static void
+task_progress_cancel (RBTaskProgress *task)
+{
+       g_signal_emit (RB_TASK_PROGRESS_SIMPLE (task), signals[CANCEL_TASK], 0);
+}
+
+static void
+impl_set_property (GObject *object,
+                  guint prop_id,
+                  const GValue *value,
+                  GParamSpec *pspec)
+{
+       RBTaskProgressSimple *task = RB_TASK_PROGRESS_SIMPLE (object);
+       switch (prop_id) {
+       case PROP_TASK_LABEL:
+               g_free (task->priv->label);
+               task->priv->label = g_value_dup_string (value);
+               break;
+       case PROP_TASK_DETAIL:
+               g_free (task->priv->detail);
+               task->priv->detail = g_value_dup_string (value);
+               break;
+       case PROP_TASK_PROGRESS:
+               task->priv->progress = g_value_get_double (value);
+               break;
+       case PROP_TASK_OUTCOME:
+               task->priv->outcome = g_value_get_enum (value);
+               break;
+       case PROP_TASK_NOTIFY:
+               task->priv->notify = g_value_get_boolean (value);
+               break;
+       case PROP_TASK_CANCELLABLE:
+               task->priv->cancellable = g_value_get_boolean (value);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+impl_get_property (GObject *object,
+                  guint prop_id,
+                  GValue *value,
+                  GParamSpec *pspec)
+{
+       RBTaskProgressSimple *task = RB_TASK_PROGRESS_SIMPLE (object);
+       switch (prop_id) {
+       case PROP_TASK_LABEL:
+               g_value_set_string (value, task->priv->label);
+               break;
+       case PROP_TASK_DETAIL:
+               g_value_set_string (value, task->priv->detail);
+               break;
+       case PROP_TASK_PROGRESS:
+               g_value_set_double (value, task->priv->progress);
+               break;
+       case PROP_TASK_OUTCOME:
+               g_value_set_enum (value, task->priv->outcome);
+               break;
+       case PROP_TASK_NOTIFY:
+               g_value_set_boolean (value, task->priv->notify);
+               break;
+       case PROP_TASK_CANCELLABLE:
+               g_value_set_boolean (value, task->priv->cancellable);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+impl_finalize (GObject *object)
+{
+       RBTaskProgressSimple *task = RB_TASK_PROGRESS_SIMPLE (object);
+
+       g_free (task->priv->label);
+       g_free (task->priv->detail);
+
+       G_OBJECT_CLASS (rb_task_progress_simple_parent_class)->finalize (object);
+}
+
+static void
+rb_task_progress_simple_init (RBTaskProgressSimple *task)
+{
+       task->priv = G_TYPE_INSTANCE_GET_PRIVATE (task, RB_TYPE_TASK_PROGRESS_SIMPLE, 
RBTaskProgressSimplePrivate);
+}
+
+static void
+rb_task_progress_simple_task_progress_init (RBTaskProgressInterface *interface)
+{
+       interface->cancel = task_progress_cancel;
+}
+
+static void
+rb_task_progress_simple_class_init (RBTaskProgressSimpleClass *klass)
+{
+       GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+       g_type_class_add_private (klass, sizeof (RBTaskProgressSimplePrivate));
+
+       gobject_class->finalize = impl_finalize;
+       gobject_class->set_property = impl_set_property;
+       gobject_class->get_property = impl_get_property;
+
+       g_object_class_override_property (gobject_class, PROP_TASK_LABEL, "task-label");
+       g_object_class_override_property (gobject_class, PROP_TASK_DETAIL, "task-detail");
+       g_object_class_override_property (gobject_class, PROP_TASK_PROGRESS, "task-progress");
+       g_object_class_override_property (gobject_class, PROP_TASK_OUTCOME, "task-outcome");
+       g_object_class_override_property (gobject_class, PROP_TASK_NOTIFY, "task-notify");
+       g_object_class_override_property (gobject_class, PROP_TASK_CANCELLABLE, "task-cancellable");
+
+       signals[CANCEL_TASK] =
+               g_signal_new ("cancel-task",
+                             G_OBJECT_CLASS_TYPE (gobject_class),
+                             G_SIGNAL_RUN_LAST,
+                             0,
+                             NULL, NULL, NULL,
+                             G_TYPE_NONE,
+                             0);
+}
+
+/**
+ * rb_task_progress_simple_new:
+ *
+ * Creates a new simple task progress object.
+ *
+ * Return value: (transfer full): the task object
+ */
+RBTaskProgress *
+rb_task_progress_simple_new (void)
+{
+       return RB_TASK_PROGRESS (g_object_new (RB_TYPE_TASK_PROGRESS_SIMPLE, NULL));
+}
diff --git a/lib/rb-task-progress-simple.h b/lib/rb-task-progress-simple.h
new file mode 100644
index 0000000..4ac94c4
--- /dev/null
+++ b/lib/rb-task-progress-simple.h
@@ -0,0 +1,64 @@
+/*
+ *  Copyright (C) 2013 Jonathan Matthew  <jonathan d14n org>
+ *
+ *  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.
+ *
+ *  The Rhythmbox authors hereby grant permission for non-GPL compatible
+ *  GStreamer plugins to be used and distributed together with GStreamer
+ *  and Rhythmbox. This permission is above and beyond the permissions granted
+ *  by the GPL license by which Rhythmbox is covered. If you modify this code
+ *  you may extend this exception to your version of the code, but you are not
+ *  obligated to do so. If you do not wish to do so, delete this exception
+ *  statement from your 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.
+ *
+ */
+
+#ifndef RB_TASK_PROGRESS_SIMPLE_H
+#define RB_TASK_PROGRESS_SIMPLE_H
+
+#include <lib/rb-task-progress.h>
+
+G_BEGIN_DECLS
+
+#define RB_TYPE_TASK_PROGRESS_SIMPLE         (rb_task_progress_simple_get_type ())
+#define RB_TASK_PROGRESS_SIMPLE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_TASK_PROGRESS_SIMPLE, 
RBTaskProgressSimple))
+#define RB_TASK_PROGRESS_SIMPLE_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_TASK_PROGRESS_SIMPLE, 
RBTaskProgressSimpleClass))
+#define RB_IS_TASK_PROGRESS_SIMPLE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_TASK_PROGRESS_SIMPLE))
+#define RB_IS_TASK_PROGRESS_SIMPLE_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_TASK_PROGRESS_SIMPLE))
+#define RB_TASK_PROGRESS_SIMPLE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_TASK_PROGRESS_SIMPLE, 
RBTaskProgressSimpleClass))
+
+typedef struct _RBTaskProgressSimple RBTaskProgressSimple;
+typedef struct _RBTaskProgressSimpleClass RBTaskProgressSimpleClass;
+typedef struct _RBTaskProgressSimplePrivate RBTaskProgressSimplePrivate;
+
+struct _RBTaskProgressSimple
+{
+       GObject parent;
+
+       RBTaskProgressSimplePrivate *priv;
+};
+
+struct _RBTaskProgressSimpleClass
+{
+       GObjectClass parent;
+};
+
+GType          rb_task_progress_simple_get_type        (void);
+
+RBTaskProgress *rb_task_progress_simple_new            (void);
+
+G_END_DECLS
+
+#endif /* RB_TASK_PROGRESS_SIMPLE_H */
diff --git a/lib/rb-task-progress.c b/lib/rb-task-progress.c
new file mode 100644
index 0000000..753a338
--- /dev/null
+++ b/lib/rb-task-progress.c
@@ -0,0 +1,118 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ *  Copyright (C) 2013  Jonathan Matthew  <jonathan d14n org>
+ *
+ *  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.
+ *
+ *  The Rhythmbox authors hereby grant permission for non-GPL compatible
+ *  GStreamer plugins to be used and distributed together with GStreamer
+ *  and Rhythmbox. This permission is above and beyond the permissions granted
+ *  by the GPL license by which Rhythmbox is covered. If you modify this code
+ *  you may extend this exception to your version of the code, but you are not
+ *  obligated to do so. If you do not wish to do so, delete this exception
+ *  statement from your 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include <config.h>
+
+#include <lib/rb-task-progress.h>
+
+G_DEFINE_INTERFACE (RBTaskProgress, rb_task_progress, 0);
+
+/**
+ * SECTION:rb-task-progress
+ * @short_description: interface for objects that report task progress
+ *
+ */
+
+
+static void
+default_cancel (RBTaskProgress *progress)
+{
+       /* nothing */
+}
+
+static void
+rb_task_progress_default_init (RBTaskProgressInterface *interface)
+{
+       interface->cancel = default_cancel;
+
+       g_object_interface_install_property (interface,
+                                            g_param_spec_string ("task-label",
+                                                                 "task label",
+                                                                 "task label",
+                                                                 NULL,
+                                                                 G_PARAM_READWRITE));
+       g_object_interface_install_property (interface,
+                                            g_param_spec_string ("task-detail",
+                                                                 "task detail",
+                                                                 "task detail",
+                                                                 NULL,
+                                                                 G_PARAM_READWRITE));
+       g_object_interface_install_property (interface,
+                                            g_param_spec_double ("task-progress",
+                                                                 "task progress",
+                                                                 "task progress",
+                                                                 0.0, 1.0, 0.0,
+                                                                 G_PARAM_READWRITE));
+       g_object_interface_install_property (interface,
+                                            g_param_spec_enum ("task-outcome",
+                                                               "task outcome",
+                                                               "task outcome",
+                                                               RB_TASK_OUTCOME_TYPE,
+                                                               RB_TASK_OUTCOME_NONE,
+                                                               G_PARAM_READWRITE));
+       g_object_interface_install_property (interface,
+                                            g_param_spec_boolean ("task-notify",
+                                                                  "task notify",
+                                                                  "whether to notify on completion",
+                                                                  FALSE,
+                                                                  G_PARAM_READWRITE));
+       g_object_interface_install_property (interface,
+                                            g_param_spec_boolean ("task-cancellable",
+                                                                  "task cancellable",
+                                                                  "whether the task can be cancelled",
+                                                                  FALSE,
+                                                                  G_PARAM_READWRITE));
+}
+
+void
+rb_task_progress_cancel (RBTaskProgress *progress)
+{
+       RBTaskProgressInterface *iface = RB_TASK_PROGRESS_GET_IFACE (progress);
+       iface->cancel (progress);
+}
+
+#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
+
+GType
+rb_task_outcome_get_type (void)
+{
+       static GType etype = 0;
+
+       if (etype == 0) {
+               static const GEnumValue values[] = {
+                       ENUM_ENTRY (RB_TASK_OUTCOME_NONE, "none"),
+                       ENUM_ENTRY (RB_TASK_OUTCOME_COMPLETE, "complete"),
+                       ENUM_ENTRY (RB_TASK_OUTCOME_CANCELLED, "cancelled"),
+                       { 0, 0, 0 }
+               };
+
+               etype = g_enum_register_static ("RBTaskOutcome", values);
+       }
+
+       return etype;
+}
diff --git a/lib/rb-task-progress.h b/lib/rb-task-progress.h
new file mode 100644
index 0000000..59f99ad
--- /dev/null
+++ b/lib/rb-task-progress.h
@@ -0,0 +1,67 @@
+/*
+ *  Copyright (C) 2013 Jonathan Matthew  <jonathan d14n org>
+ *
+ *  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.
+ *
+ *  The Rhythmbox authors hereby grant permission for non-GPL compatible
+ *  GStreamer plugins to be used and distributed together with GStreamer
+ *  and Rhythmbox. This permission is above and beyond the permissions granted
+ *  by the GPL license by which Rhythmbox is covered. If you modify this code
+ *  you may extend this exception to your version of the code, but you are not
+ *  obligated to do so. If you do not wish to do so, delete this exception
+ *  statement from your 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.
+ *
+ */
+
+#ifndef RB_TASK_PROGRESS_H
+#define RB_TASK_PROGRESS_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define RB_TYPE_TASK_PROGRESS         (rb_task_progress_get_type ())
+#define RB_TASK_PROGRESS(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_TASK_PROGRESS, 
RBTaskProgress))
+#define RB_IS_TASK_PROGRESS(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_TASK_PROGRESS))
+#define RB_TASK_PROGRESS_GET_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), RB_TYPE_TASK_PROGRESS, 
RBTaskProgressInterface))
+
+typedef struct _RBTaskProgress RBTaskProgress;
+typedef struct _RBTaskProgressInterface RBTaskProgressInterface;
+
+typedef enum
+{
+       RB_TASK_OUTCOME_NONE,
+       RB_TASK_OUTCOME_COMPLETE,
+       RB_TASK_OUTCOME_CANCELLED
+} RBTaskOutcome;
+
+GType rb_task_outcome_get_type (void);
+#define RB_TASK_OUTCOME_TYPE (rb_task_outcome_get_type())
+
+struct _RBTaskProgressInterface
+{
+       GTypeInterface g_iface;
+
+       /* methods */
+       void    (*cancel)       (RBTaskProgress *progress);
+};
+
+GType          rb_task_progress_get_type       (void);
+
+void           rb_task_progress_cancel         (RBTaskProgress *progress);
+
+G_END_DECLS
+
+#endif /* RB_TASK_PROGRESS_H */


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