[Xfce4-commits] r29853 - in thunar/branches/migration-to-gio: . thunar

Jannis Pohlmann jannis at xfce.org
Mon Apr 20 03:02:32 CEST 2009


Author: jannis
Date: 2009-04-20 01:02:32 +0000 (Mon, 20 Apr 2009)
New Revision: 29853

Added:
   thunar/branches/migration-to-gio/thunar/thunar-job.c
   thunar/branches/migration-to-gio/thunar/thunar-job.h
Modified:
   thunar/branches/migration-to-gio/ChangeLog
   thunar/branches/migration-to-gio/thunar/Makefile.am
   thunar/branches/migration-to-gio/thunar/thunar-enum-types.c
   thunar/branches/migration-to-gio/thunar/thunar-enum-types.h
   thunar/branches/migration-to-gio/thunar/thunar-marshal.list
Log:
	* thunar/Makefile.am, thunar/thunar-enum-types.{c,h},
	  thunar/thunar-job.{c,h}, thunar/thunar-marshal.list: Add abstract
	  class ThunarJob which is going to be an equivalent to ThunarVfsJob.
	  The main difference is that ThunarJob uses GIOScheduler to handle
	  the asynchronous execution of the job instead of managing worker
	  threads on its own. It's not finished yet though. Add
	  ThunarJobResponse enum type and new marshal functions for job
	  signals.

Modified: thunar/branches/migration-to-gio/ChangeLog
===================================================================
--- thunar/branches/migration-to-gio/ChangeLog	2009-04-19 21:24:17 UTC (rev 29852)
+++ thunar/branches/migration-to-gio/ChangeLog	2009-04-20 01:02:32 UTC (rev 29853)
@@ -1,3 +1,14 @@
+2009-04-20	Jannis Pohlmann <jannis at xfce.org>
+
+	* thunar/Makefile.am, thunar/thunar-enum-types.{c,h},
+	  thunar/thunar-job.{c,h}, thunar/thunar-marshal.list: Add abstract 
+	  class ThunarJob which is going to be an equivalent to ThunarVfsJob. 
+	  The main difference is that ThunarJob uses GIOScheduler to handle 
+	  the asynchronous execution of the job instead of managing worker 
+	  threads on its own. It's not finished yet though. Add 
+	  ThunarJobResponse enum type and new marshal functions for job
+	  signals.
+
 2009-04-19	Jannis Pohlmann <jannis at xfce.org>
 
 	* thunar/thunar-location-button.c: Use g_file_list_to_string() instead

Modified: thunar/branches/migration-to-gio/thunar/Makefile.am
===================================================================
--- thunar/branches/migration-to-gio/thunar/Makefile.am	2009-04-19 21:24:17 UTC (rev 29852)
+++ thunar/branches/migration-to-gio/thunar/Makefile.am	2009-04-20 01:02:32 UTC (rev 29853)
@@ -94,6 +94,8 @@
 	thunar-icon-renderer.h						\
 	thunar-icon-view.c						\
 	thunar-icon-view.h						\
+	thunar-job.c							\
+	thunar-job.h							\
 	thunar-launcher.c						\
 	thunar-launcher.h						\
 	thunar-launcher-ui.h						\

Modified: thunar/branches/migration-to-gio/thunar/thunar-enum-types.c
===================================================================
--- thunar/branches/migration-to-gio/thunar/thunar-enum-types.c	2009-04-19 21:24:17 UTC (rev 29852)
+++ thunar/branches/migration-to-gio/thunar/thunar-enum-types.c	2009-04-20 01:02:32 UTC (rev 29853)
@@ -1,6 +1,7 @@
 /* $Id$ */
 /*-
  * Copyright (c) 2006-2007 Benedikt Meurer <benny at xfce.org>
+ * Copyright (c) 2009 Jannis Pohlmann <jannis at xfce.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
@@ -271,4 +272,27 @@
 
 
 
+GType
+thunar_vfs_job_response_get_type (void)
+{
+	static GType type = G_TYPE_INVALID;
 
+  if (G_UNLIKELY (type == G_TYPE_INVALID))
+    {
+	    static const GFlagsValue values[] = 
+      {
+	      { THUNAR_JOB_RESPONSE_YES,     "THUNAR_JOB_RESPONSE_YES",     "yes"     },
+	      { THUNAR_JOB_RESPONSE_YES_ALL, "THUNAR_JOB_RESPONSE_YES_ALL", "yes-all" },
+	      { THUNAR_JOB_RESPONSE_NO,      "THUNAR_JOB_RESPONSE_NO",      "no"      },
+	      { THUNAR_JOB_RESPONSE_CANCEL,  "THUNAR_JOB_RESPONSE_CANCEL",  "cancel"  },
+	      { THUNAR_JOB_RESPONSE_NO_ALL,  "THUNAR_JOB_RESPONSE_NO_ALL",  "no-all"  },
+	      { THUNAR_JOB_RESPONSE_RETRY,   "THUNAR_JOB_RESPONSE_RETRY",   "retry"   },
+	      { 0,                           NULL,                          NULL      }
+	    };
+
+	    type = g_flags_register_static (I_("ThunarJobResponse"), values);
+    }
+
+	return type;
+}
+

Modified: thunar/branches/migration-to-gio/thunar/thunar-enum-types.h
===================================================================
--- thunar/branches/migration-to-gio/thunar/thunar-enum-types.h	2009-04-19 21:24:17 UTC (rev 29852)
+++ thunar/branches/migration-to-gio/thunar/thunar-enum-types.h	2009-04-20 01:02:32 UTC (rev 29853)
@@ -1,6 +1,7 @@
 /* $Id$ */
 /*-
  * Copyright (c) 2006-2007 Benedikt Meurer <benny at xfce.org>
+ * Copyright (c) 2009 Jannis Pohlmann <jannis at xfce.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
@@ -234,6 +235,30 @@
 GType          thunar_zoom_level_get_type     (void) G_GNUC_CONST G_GNUC_INTERNAL;
 ThunarIconSize thunar_zoom_level_to_icon_size (ThunarZoomLevel zoom_level) G_GNUC_CONST G_GNUC_INTERNAL;
 
+
+#define THUNAR_TYPE_JOB_RESPONSE (thunar_job_response_get_type ())
+
+/**
+ * ThunarJobResponse:
+ * @THUNAR_JOB_RESPONSE_YES     :
+ * @THUNAR_JOB_RESPONSE_YES_ALL :
+ * @THUNAR_JOB_RESPONSE_NO      :
+ * @THUNAR_JOB_RESPONSE_NO_ALL  :
+ * @THUNAR_JOB_RESPONSE_CANCEL  :
+ * @THUNAR_JOB_RESPONSE_RETRY   :
+ *
+ * Possible responses for the ThunarJob::ask signal.
+ **/
+typedef enum /*< flags >*/
+{
+  THUNAR_JOB_RESPONSE_YES     = 1 << 0,
+  THUNAR_JOB_RESPONSE_YES_ALL = 1 << 1,
+  THUNAR_JOB_RESPONSE_NO      = 1 << 2,
+  THUNAR_JOB_RESPONSE_CANCEL  = 1 << 3,
+  THUNAR_JOB_RESPONSE_NO_ALL  = 1 << 4,
+  THUNAR_JOB_RESPONSE_RETRY   = 1 << 5,
+} ThunarJobResponse;
+
 G_END_DECLS;
 
 #endif /* !__THUNAR_ENUM_TYPES_H__ */

Added: thunar/branches/migration-to-gio/thunar/thunar-job.c
===================================================================
--- thunar/branches/migration-to-gio/thunar/thunar-job.c	                        (rev 0)
+++ thunar/branches/migration-to-gio/thunar/thunar-job.c	2009-04-20 01:02:32 UTC (rev 29853)
@@ -0,0 +1,416 @@
+/* $Id$ */
+/*-
+ * Copyright (c) 2009 Jannis Pohlmann <jannis at xfce.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_MEMORY_H
+#include <memory.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include <thunar/thunar-enum-types.h>
+#include <thunar/thunar-job.h>
+#include <thunar/thunar-marshal.h>
+#include <thunar/thunar-private.h>
+
+
+
+#define THUNAR_JOB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), THUNAR_TYPE_JOB, ThunarJobPrivate))
+
+
+
+/* Signal identifiers */
+enum
+{
+  ASK,
+  ASK_REPLACE,
+  ERROR,
+  FINISHED,
+  INFO_MESSAGE,
+  INFOS_READY,
+  NEW_FILES,
+  PERCENT,
+  LAST_SIGNAL,
+};
+
+
+
+typedef struct _ThunarJobSyncSignalData  ThunarJobSyncSignalData;
+
+
+
+static void     thunar_job_class_init          (ThunarJobClass     *klass);
+static void     thunar_job_init                (ThunarJob          *job);
+static void     thunar_job_finalize            (GObject            *object);
+
+static gboolean _thunar_job_finish             (ThunarJob          *job,
+                                                GSimpleAsyncResult *result,
+                                                GError            **error);
+static void     _thunar_job_async_ready        (GObject            *object,
+                                                GAsyncResult       *result);
+static gboolean _thunar_job_scheduler_job_func (GIOSchedulerJob    *scheduler_job,
+                                                GCancellable       *cancellable,
+                                                gpointer            user_data);
+
+
+
+struct _ThunarJobPrivate
+{
+  GIOSchedulerJob *scheduler_job;
+  GCancellable    *cancellable;
+  guint            running : 1;
+};
+
+struct _ThunarJobSyncSignalData
+{
+  gpointer instance;
+  GQuark   signal_detail;
+  guint    signal_id;
+  va_list  var_args;
+};
+
+
+
+static GObjectClass *thunar_job_parent_class;
+static guint         job_signals[LAST_SIGNAL];
+
+
+
+GType
+thunar_job_get_type (void)
+{
+  static GType type = G_TYPE_INVALID;
+
+  if (G_UNLIKELY (type == G_TYPE_INVALID))
+    {
+      type = g_type_register_static_simple (G_TYPE_OBJECT,
+                                            "ThunarJob",
+                                            sizeof (ThunarJobClass),
+                                            (GClassInitFunc) thunar_job_class_init,
+                                            sizeof (ThunarJob),
+                                            (GInstanceInitFunc) thunar_job_init,
+                                            G_TYPE_FLAG_ABSTRACT);
+    }
+
+  return type;
+}
+
+
+
+static void
+thunar_job_class_init (ThunarJobClass *klass)
+{
+  GObjectClass *gobject_class;
+
+  /* add our private data for this class */
+  g_type_class_add_private (klass, sizeof (ThunarJobPrivate));
+
+  /* determine the parent class */
+  thunar_job_parent_class = g_type_class_peek_parent (klass);
+
+  gobject_class = G_OBJECT_CLASS (klass);
+  gobject_class->finalize = thunar_job_finalize;
+
+  klass->execute = NULL;
+
+  /**
+   * ThunarJob::error:
+   * @job   : a #ThunarJob.
+   * @error : a #GError describing the cause.
+   *
+   * Emitted whenever an error occurs while executing the @job.
+   **/
+  job_signals[ERROR] =
+    g_signal_new (I_("error"),
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_NO_HOOKS, 0, NULL, NULL,
+                  g_cclosure_marshal_VOID__POINTER,
+                  G_TYPE_NONE, 1, G_TYPE_POINTER);
+
+  /**
+   * ThunarJob::finished:
+   * @job : a #ThunarJob.
+   *
+   * This signal will be automatically emitted once the
+   * @job finishes its execution, no matter whether @job
+   * completed successfully or was cancelled by the
+   * user.
+   **/
+  job_signals[FINISHED] =
+    g_signal_new (I_("finished"),
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_NO_HOOKS, 0, NULL, NULL,
+                  g_cclosure_marshal_VOID__VOID,
+                  G_TYPE_NONE, 0);
+}
+
+
+
+static void
+thunar_job_init (ThunarJob *job)
+{
+  job->priv = THUNAR_JOB_GET_PRIVATE (job);
+  job->priv->cancellable = g_cancellable_new ();
+  job->priv->running = FALSE;
+  job->priv->scheduler_job = NULL;
+}
+
+
+
+static void
+thunar_job_finalize (GObject *object)
+{
+  ThunarJob *job = THUNAR_JOB (object);
+
+  g_object_unref (job->priv->cancellable);
+
+  (*G_OBJECT_CLASS (thunar_job_parent_class)->finalize) (object);
+}
+
+
+
+static gboolean
+_thunar_job_finish (ThunarJob          *job,
+                    GSimpleAsyncResult *result,
+                    GError            **error)
+{
+  _thunar_return_val_if_fail (THUNAR_IS_JOB (job), FALSE);
+  _thunar_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
+  _thunar_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  return !g_simple_async_result_propagate_error (result, error);
+}
+
+
+
+static void
+_thunar_job_async_ready (GObject      *object,
+                         GAsyncResult *result)
+{
+  ThunarJob *job = THUNAR_JOB (object);
+  GError    *error = NULL;
+
+  _thunar_return_if_fail (THUNAR_IS_JOB (job));
+  _thunar_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result));
+
+  if (!_thunar_job_finish (job, G_SIMPLE_ASYNC_RESULT (result), &error))
+    {
+      g_assert (error != NULL);
+      thunar_job_error (job, error);
+      g_error_free (error);
+    }
+
+  thunar_job_finished (job);
+}
+
+
+
+static gboolean
+_thunar_job_scheduler_job_func (GIOSchedulerJob *scheduler_job,
+                                GCancellable    *cancellable,
+                                gpointer         user_data)
+{
+  GSimpleAsyncResult *result = user_data;
+  ThunarJob          *job;
+  GError             *error = NULL;
+  gboolean            success;
+
+  job = g_simple_async_result_get_op_res_gpointer (result);
+  job->priv->scheduler_job = scheduler_job;
+
+  success = (*THUNAR_JOB_GET_CLASS (job)->execute) (job, &error);
+
+  /* TODO why was this necessary again? */
+  g_io_scheduler_job_send_to_mainloop (scheduler_job, 
+                                       (GSourceFunc) gtk_false, 
+                                       NULL, 
+                                       NULL);
+
+  if (!success)
+    {
+      g_simple_async_result_set_from_error (result, error);
+      g_error_free (error);
+    }
+
+  g_simple_async_result_complete_in_idle (result);
+
+  return FALSE;
+}
+
+
+
+/**
+ * thunar_job_launch:
+ * @job : a #ThunarJob.
+ *
+ * This functions schedules @job to be run as soon
+ * as possible, in a separate thread.
+ *
+ * Return value: a pointer to @job.
+ **/
+ThunarJob*
+thunar_job_launch (ThunarJob *job)
+{
+  GSimpleAsyncResult *result;
+
+  _thunar_return_val_if_fail (THUNAR_IS_JOB (job), NULL);
+  _thunar_return_val_if_fail (!job->priv->running, NULL);
+  _thunar_return_val_if_fail (THUNAR_JOB_GET_CLASS (job)->execute != NULL, NULL);
+
+  /* mark the job as running */
+  job->priv->running = TRUE;
+
+  result = g_simple_async_result_new (G_OBJECT (job),
+                                      (GAsyncReadyCallback) _thunar_job_async_ready,
+                                      NULL,
+                                      thunar_job_launch);
+
+  g_simple_async_result_set_op_res_gpointer (result,
+                                             g_object_ref (job),
+                                             (GDestroyNotify) g_object_unref);
+
+  g_io_scheduler_push_job (_thunar_job_scheduler_job_func,
+                           result,
+                           (GDestroyNotify) g_object_unref,
+                           G_PRIORITY_HIGH,
+                           job->priv->cancellable);
+
+  return job;
+}
+
+
+
+/**
+ * thunar_job_cancel:
+ * @job : a #ThunarJob.
+ *
+ * Attempts to cancel the operation currently
+ * performed by @job. Even after the cancellation
+ * of @job, it may still emit signals, so you
+ * must take care of disconnecting all handlers
+ * appropriately if you cannot handle signals
+ * after cancellation.
+ **/
+void
+thunar_job_cancel (ThunarJob *job)
+{
+  _thunar_return_if_fail (THUNAR_IS_JOB (job));
+  g_cancellable_cancel (job->priv->cancellable);
+}
+
+
+
+/**
+ * thunar_job_cancelled:
+ * @job : a #ThunarJob.
+ *
+ * Checks whether @job was previously cancelled
+ * by a call to thunar_job_cancel().
+ *
+ * Return value: %TRUE if @job is cancelled.
+ **/
+gboolean
+thunar_job_cancelled (const ThunarJob *job)
+{
+  _thunar_return_val_if_fail (THUNAR_IS_JOB (job), FALSE);
+  return g_cancellable_is_cancelled (job->priv->cancellable);
+}
+
+
+
+static gboolean
+_thunar_job_emit_valist_in_mainloop (gpointer user_data)
+{
+  ThunarJobSyncSignalData *data = user_data;
+
+  g_signal_emit_valist (data->instance, 
+                        data->signal_id, 
+                        data->signal_detail, 
+                        data->var_args);
+}
+
+
+
+void
+thunar_job_emit_valist (ThunarJob *job,
+                        guint      signal_id,
+                        GQuark     signal_detail,
+                        va_list    var_args)
+{
+  ThunarJobSyncSignalData data;
+
+  _thunar_return_if_fail (THUNAR_IS_JOB (job));
+  _thunar_return_if_fail (job->priv->scheduler_job != NULL);
+
+  data.instance = job;
+  data.signal_id = signal_id;
+  data.signal_detail = signal_detail;
+  
+  /* copy the variable argument list */
+  G_VA_COPY (data.var_args, var_args);
+
+  /* emit the signal in the main loop */
+  g_io_scheduler_job_send_to_mainloop (job->priv->scheduler_job,
+                                       _thunar_job_emit_valist_in_mainloop,
+                                       &data,
+                                       NULL);
+}
+
+
+
+void
+thunar_job_emit (ThunarJob *job,
+                 guint      signal_id,
+                 GQuark     signal_detail,
+                 ...)
+{
+  va_list var_args;
+
+  va_start (var_args, signal_detail);
+  thunar_job_emit_valist (job, signal_id, signal_detail, var_args);
+  va_end (var_args);
+}
+
+
+
+void
+thunar_job_error (ThunarJob *job,
+                  GError    *error)
+{
+  _thunar_return_if_fail (THUNAR_IS_JOB (job));
+  _thunar_return_if_fail (error != NULL);
+  _thunar_return_if_fail (g_utf8_validate (error->message, -1, NULL));
+
+  thunar_job_emit (job, job_signals[ERROR], 0, error);
+}
+
+
+
+void
+thunar_job_finished (ThunarJob *job)
+{
+  _thunar_return_if_fail (THUNAR_IS_JOB (job));
+
+  thunar_job_emit (job, job_signals[FINISHED], 0);
+}

Added: thunar/branches/migration-to-gio/thunar/thunar-job.h
===================================================================
--- thunar/branches/migration-to-gio/thunar/thunar-job.h	                        (rev 0)
+++ thunar/branches/migration-to-gio/thunar/thunar-job.h	2009-04-20 01:02:32 UTC (rev 29853)
@@ -0,0 +1,73 @@
+/* $Id$ */
+/*-
+ * Copyright (c) 2005-2007 Benedikt Meurer <benny at xfce.org>
+ * Copyright (c) 2009 Jannis Pohlmann <jannis at xfce.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __THUNAR_JOB_H__
+#define __THUNAR_JOB_H__
+
+#include <gio/gio.h>
+
+#include <thunar/thunar-enum-types.h>
+#include <thunar/thunar-file.h>
+
+G_BEGIN_DECLS
+
+typedef struct _ThunarJobPrivate ThunarJobPrivate;
+typedef struct _ThunarJobClass   ThunarJobClass;
+typedef struct _ThunarJob        ThunarJob;
+
+#define THUNAR_TYPE_JOB            (thunar_job_get_type ())
+#define THUNAR_JOB(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNAR_TYPE_JOB, ThunarJob))
+#define THUNAR_JOB_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), THUNAR_TYPE_JOB, ThunarJobClass))
+#define THUNAR_IS_JOB(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNAR_TYPE_JOB))
+#define THUNAR_IS_JOB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_TYPE_JOB))
+#define THUNAR_JOB_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_TYPE_JOB, ThunarJobClass))
+
+struct _ThunarJobClass
+{
+  /*< private >*/
+  GObjectClass __parent__;
+
+  /*< public >*/
+
+  /* virtual methods */
+  gboolean (*execute) (ThunarJob *job,
+                       GError   **error);
+};
+
+struct _ThunarJob
+{
+  /*< private >*/
+  GObject           __parent__;
+  ThunarJobPrivate *priv;
+};
+
+GType      thunar_job_get_type     (void) G_GNUC_CONST;
+ThunarJob *thunar_job_launch       (ThunarJob       *job);
+void       thunar_job_cancel       (ThunarJob       *job);
+gboolean   thunar_job_is_cancelled (const ThunarJob *job);
+
+void       thunar_job_error        (ThunarJob       *job,
+                                    GError          *error);
+void       thunar_job_finished     (ThunarJob       *job);
+
+G_END_DECLS
+
+#endif /* !__THUNAR_JOB_H__ */

Modified: thunar/branches/migration-to-gio/thunar/thunar-marshal.list
===================================================================
--- thunar/branches/migration-to-gio/thunar/thunar-marshal.list	2009-04-19 21:24:17 UTC (rev 29852)
+++ thunar/branches/migration-to-gio/thunar/thunar-marshal.list	2009-04-20 01:02:32 UTC (rev 29853)
@@ -1,4 +1,8 @@
+BOOLEAN:POINTER
 BOOLEAN:VOID
+FLAGS:BOXED,BOXED
+FLAGS:STRING,FLAGS
 VOID:BOXED,OBJECT
 VOID:BOXED,POINTER
 VOID:STRING,STRING
+VOID:UINT64,UINT,UINT,UINT




More information about the Xfce4-commits mailing list