[gnome-builder/wip/libide] libide: add IdeProgress progress abstraction
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/wip/libide] libide: add IdeProgress progress abstraction
- Date: Tue, 24 Feb 2015 00:37:02 +0000 (UTC)
commit 725889b952e9f4dc1c5671e4d7c0a81b1fd021c0
Author: Christian Hergert <christian hergert me>
Date: Mon Feb 23 16:31:07 2015 -0800
libide: add IdeProgress progress abstraction
This is a bit more convenient than using GFileProgressCallback everywhere
since you can attach to various signals on the resulting progress object.
libide/Makefile.am | 2 +
libide/ide-progress.c | 241 +++++++++++++++++++++++++++++++++++++++++++++++++
libide/ide-progress.h | 42 +++++++++
libide/ide-types.h | 2 +
libide/ide.h | 1 +
5 files changed, 288 insertions(+), 0 deletions(-)
---
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 7e79851..7552361 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -89,6 +89,8 @@ libide_1_0_la_public_sources = \
libide/ide-object.h \
libide/ide-process.c \
libide/ide-process.h \
+ libide/ide-progress.c \
+ libide/ide-progress.h \
libide/ide-project-file.c \
libide/ide-project-file.h \
libide/ide-project-files.c \
diff --git a/libide/ide-progress.c b/libide/ide-progress.c
new file mode 100644
index 0000000..6c68cad
--- /dev/null
+++ b/libide/ide-progress.c
@@ -0,0 +1,241 @@
+/* ide-progress.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "ide-progress"
+
+#include <glib/gi18n.h>
+
+#include "ide-progress.h"
+
+struct _IdeProgress
+{
+ IdeObject parent_instance;
+
+ gchar *message;
+ gdouble fraction;
+ guint completed : 1;
+};
+
+G_DEFINE_TYPE (IdeProgress, ide_progress, IDE_TYPE_OBJECT)
+
+enum {
+ PROP_0,
+ PROP_COMPLETED,
+ PROP_FRACTION,
+ PROP_MESSAGE,
+ LAST_PROP
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+
+gboolean
+ide_progress_get_completed (IdeProgress *self)
+{
+ g_return_val_if_fail (IDE_IS_PROGRESS (self), FALSE);
+
+ return self->completed;
+}
+
+void
+ide_progress_set_completed (IdeProgress *self,
+ gboolean completed)
+{
+ g_return_if_fail (IDE_IS_PROGRESS (self));
+
+ if (self->completed != completed)
+ {
+ self->completed = completed;
+ g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_COMPLETED]);
+ }
+}
+
+gdouble
+ide_progress_get_fraction (IdeProgress *self)
+{
+ g_return_val_if_fail (IDE_IS_PROGRESS (self), 0.0);
+
+ return self->fraction;
+}
+
+void
+ide_progress_set_fraction (IdeProgress *self,
+ gdouble fraction)
+{
+ g_return_if_fail (IDE_IS_PROGRESS (self));
+ g_return_if_fail (fraction >= 0.0);
+ g_return_if_fail (fraction <= 1.0);
+
+ if (self->fraction != fraction)
+ {
+ self->fraction = fraction;
+ if (fraction == 1.0)
+ ide_progress_set_completed (self, TRUE);
+ g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_FRACTION]);
+ }
+}
+
+const gchar *
+ide_progress_get_message (IdeProgress *self)
+{
+ g_return_val_if_fail (IDE_IS_PROGRESS (self), NULL);
+
+ return self->message;
+}
+
+void
+ide_progress_set_message (IdeProgress *self,
+ const gchar *message)
+{
+ g_return_if_fail (IDE_IS_PROGRESS (self));
+
+ if (self->message != message)
+ {
+ g_free (self->message);
+ self->message = g_strdup (message);
+ g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_MESSAGE]);
+ }
+}
+
+/**
+ * ide_progress_file_progress_callback:
+ *
+ * This function is a #GFileProgressCallback helper that will update the
+ * #IdeProgress:fraction property. @user_data must be an #IdeProgress.
+ *
+ * Remember to make sure to unref the #IdeProgress instance with
+ * g_object_unref() during the #GDestroyNotify.
+ */
+void
+ide_progress_file_progress_callback (goffset current_num_bytes,
+ goffset total_num_bytes,
+ gpointer user_data)
+{
+ IdeProgress *self = user_data;
+ gdouble fraction = 0.0;
+
+ g_return_if_fail (IDE_IS_PROGRESS (self));
+
+ if (total_num_bytes)
+ fraction = (gdouble)current_num_bytes / (gdouble)total_num_bytes;
+
+ ide_progress_set_fraction (self, fraction);
+}
+
+static void
+ide_progress_finalize (GObject *object)
+{
+ IdeProgress *self = (IdeProgress *)object;
+
+ g_clear_pointer (&self->message, g_free);
+
+ G_OBJECT_CLASS (ide_progress_parent_class)->finalize (object);
+}
+
+static void
+ide_progress_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ IdeProgress *self = IDE_PROGRESS (object);
+
+ switch (prop_id)
+ {
+ case PROP_COMPLETED:
+ g_value_set_boolean (value, ide_progress_get_completed (self));
+ break;
+
+ case PROP_FRACTION:
+ g_value_set_double (value, ide_progress_get_fraction (self));
+ break;
+
+ case PROP_MESSAGE:
+ g_value_set_string (value, ide_progress_get_message (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_progress_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ IdeProgress *self = IDE_PROGRESS (object);
+
+ switch (prop_id)
+ {
+ case PROP_FRACTION:
+ ide_progress_set_fraction (self, g_value_get_double (value));
+ break;
+
+ case PROP_MESSAGE:
+ ide_progress_set_message (self, g_value_get_string (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_progress_class_init (IdeProgressClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = ide_progress_finalize;
+ object_class->get_property = ide_progress_get_property;
+ object_class->set_property = ide_progress_set_property;
+
+ gParamSpecs [PROP_COMPLETED] =
+ g_param_spec_boolean ("completed",
+ _("Completed"),
+ _("If the progress has completed."),
+ FALSE,
+ (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_COMPLETED,
+ gParamSpecs [PROP_COMPLETED]);
+
+ gParamSpecs [PROP_FRACTION] =
+ g_param_spec_double ("fraction",
+ _("Fraction"),
+ _("The fraction of the progress."),
+ 0.0,
+ 1.0,
+ 0.0,
+ (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_FRACTION,
+ gParamSpecs [PROP_FRACTION]);
+
+ gParamSpecs [PROP_MESSAGE] =
+ g_param_spec_string ("message",
+ _("Message"),
+ _("A short message for the progress."),
+ NULL,
+ (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_MESSAGE,
+ gParamSpecs [PROP_MESSAGE]);
+}
+
+static void
+ide_progress_init (IdeProgress *self)
+{
+}
diff --git a/libide/ide-progress.h b/libide/ide-progress.h
new file mode 100644
index 0000000..70853d4
--- /dev/null
+++ b/libide/ide-progress.h
@@ -0,0 +1,42 @@
+/* ide-progress.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_PROGRESS_H
+#define IDE_PROGRESS_H
+
+#include "ide-object.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_PROGRESS (ide_progress_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeProgress, ide_progress, IDE, PROGRESS, IdeObject)
+
+gdouble ide_progress_get_fraction (IdeProgress *self);
+const gchar *ide_progress_get_message (IdeProgress *self);
+void ide_progress_set_fraction (IdeProgress *self,
+ gdouble fraction);
+void ide_progress_set_message (IdeProgress *self,
+ const gchar *message);
+void ide_progress_file_progress_callback (goffset current_num_bytes,
+ goffset total_num_bytes,
+ gpointer user_data);
+
+G_END_DECLS
+
+#endif /* IDE_PROGRESS_H */
diff --git a/libide/ide-types.h b/libide/ide-types.h
index 297d432..828d6ae 100644
--- a/libide/ide-types.h
+++ b/libide/ide-types.h
@@ -81,6 +81,8 @@ typedef struct _IdeObject IdeObject;
typedef struct _IdeProcess IdeProcess;
typedef struct _IdeProcessInterface IdeProcessInterface;
+typedef struct _IdeProgress IdeProgress;
+
typedef struct _IdeProject IdeProject;
typedef struct _IdeProjectItem IdeProjectItem;
diff --git a/libide/ide.h b/libide/ide.h
index 8e884f5..3b20443 100644
--- a/libide/ide.h
+++ b/libide/ide.h
@@ -50,6 +50,7 @@ G_BEGIN_DECLS
#include "ide-language.h"
#include "ide-object.h"
#include "ide-process.h"
+#include "ide-progress.h"
#include "ide-project.h"
#include "ide-project-file.h"
#include "ide-project-files.h"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]