[latexila/wip/build-tools-revamp] Build tools: implement the run operations (not finished)



commit dc667865cf651c165415ac6c81b04aec3378fa14
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu May 8 22:08:47 2014 +0200

    Build tools: implement the run operations (not finished)

 docs/reference/latexila-sections.txt  |    5 +-
 src/liblatexila/latexila-build-job.c  |   98 ++++++++++++++++++++++++++
 src/liblatexila/latexila-build-job.h  |   12 +++-
 src/liblatexila/latexila-build-tool.c |  124 ++++++++++++++++++++++++++++++---
 src/liblatexila/latexila-build-tool.h |   12 +++-
 src/liblatexila/latexila-build-view.c |    3 +
 6 files changed, 239 insertions(+), 15 deletions(-)
---
diff --git a/docs/reference/latexila-sections.txt b/docs/reference/latexila-sections.txt
index 8d1703a..71f0d12 100644
--- a/docs/reference/latexila-sections.txt
+++ b/docs/reference/latexila-sections.txt
@@ -67,7 +67,8 @@ latexila_build_tool_add_job
 latexila_build_tool_get_jobs
 latexila_build_tool_get_description
 latexila_build_tool_to_xml
-latexila_build_tool_run
+latexila_build_tool_run_async
+latexila_build_tool_run_finish
 <SUBSECTION Standard>
 LATEXILA_BUILD_TOOL
 LATEXILA_BUILD_TOOL_CLASS
@@ -87,6 +88,8 @@ LatexilaBuildJob
 latexila_build_job_new
 latexila_build_job_clone
 latexila_build_job_to_xml
+latexila_build_job_run_async
+latexila_build_job_run_finish
 <SUBSECTION Standard>
 LATEXILA_BUILD_JOB
 LATEXILA_BUILD_JOB_CLASS
diff --git a/src/liblatexila/latexila-build-job.c b/src/liblatexila/latexila-build-job.c
index f6d458b..7f9d3ca 100644
--- a/src/liblatexila/latexila-build-job.c
+++ b/src/liblatexila/latexila-build-job.c
@@ -26,12 +26,17 @@
  */
 
 #include "latexila-build-job.h"
+#include "latexila-build-view.h"
 #include "latexila-enum-types.h"
 
 struct _LatexilaBuildJobPrivate
 {
   gchar *command;
   LatexilaPostProcessorType post_processor_type;
+
+  LatexilaBuildView *build_view;
+  GtkTreeIter job_title;
+  GNode *build_messages;
 };
 
 enum
@@ -43,6 +48,30 @@ enum
 
 G_DEFINE_TYPE_WITH_PRIVATE (LatexilaBuildJob, latexila_build_job, G_TYPE_OBJECT)
 
+static gboolean
+free_build_msg (GNode    *node,
+                gpointer  user_data)
+{
+  latexila_build_msg_free (node->data);
+  return FALSE;
+}
+
+static void
+free_build_messages (GNode *build_messages)
+{
+  if (build_messages != NULL)
+    {
+      g_node_traverse (build_messages,
+                       G_POST_ORDER,
+                       G_TRAVERSE_ALL,
+                       -1,
+                       free_build_msg,
+                       NULL);
+
+      g_node_destroy (build_messages);
+    }
+}
+
 static void
 latexila_build_job_get_property (GObject    *object,
                                  guint       prop_id,
@@ -75,6 +104,9 @@ latexila_build_job_set_property (GObject      *object,
 {
   LatexilaBuildJob *build_job = LATEXILA_BUILD_JOB (object);
 
+  /* The build job can not be modified when it is running. */
+  g_return_if_fail (build_job->priv->build_view == NULL);
+
   switch (prop_id)
     {
     case PROP_COMMAND:
@@ -95,6 +127,9 @@ latexila_build_job_set_property (GObject      *object,
 static void
 latexila_build_job_dispose (GObject *object)
 {
+  LatexilaBuildJob *build_job = LATEXILA_BUILD_JOB (object);
+
+  g_clear_object (&build_job->priv->build_view);
 
   G_OBJECT_CLASS (latexila_build_job_parent_class)->dispose (object);
 }
@@ -105,6 +140,7 @@ latexila_build_job_finalize (GObject *object)
   LatexilaBuildJob *build_job = LATEXILA_BUILD_JOB (object);
 
   g_free (build_job->priv->command);
+  free_build_messages (build_job->priv->build_messages);
 
   G_OBJECT_CLASS (latexila_build_job_parent_class)->finalize (object);
 }
@@ -192,3 +228,65 @@ latexila_build_job_to_xml (LatexilaBuildJob *build_job)
                                   latexila_post_processor_get_name_from_type 
(build_job->priv->post_processor_type),
                                   build_job->priv->command != NULL ? build_job->priv->command : "");
 }
+
+/**
+ * latexila_build_job_run_async:
+ * @build_job: a build job.
+ * @file: a file.
+ * @build_view: a build view.
+ * @cancellable: a #GCancellable object.
+ * @callback: the callback to call when the operation is finished.
+ * @user_data: the data to pass to the callback function.
+ *
+ * Runs asynchronously the build job on a file with the messages displayed in a
+ * build view. When the operation is finished, @callback will be called. You can
+ * then call latexila_build_job_run_finish().
+ */
+void
+latexila_build_job_run_async (LatexilaBuildJob    *build_job,
+                              GFile               *file,
+                              LatexilaBuildView   *build_view,
+                              GCancellable        *cancellable,
+                              GAsyncReadyCallback  callback,
+                              gpointer             user_data)
+{
+  GTask *task;
+  LatexilaBuildMsg *msg;
+
+  g_return_if_fail (LATEXILA_IS_BUILD_JOB (build_job));
+  g_return_if_fail (G_IS_FILE (file));
+  g_return_if_fail (LATEXILA_IS_BUILD_VIEW (build_view));
+
+  g_clear_object (&build_job->priv->build_view);
+  build_job->priv->build_view = g_object_ref (build_view);
+
+  task = g_task_new (build_job, cancellable, callback, user_data);
+
+  free_build_messages (build_job->priv->build_messages);
+
+  msg = latexila_build_msg_new ();
+  msg->text = g_strdup ("build job!");
+
+  build_job->priv->build_messages = g_node_new (msg);
+
+  g_task_return_boolean (task, TRUE);
+  g_object_unref (task);
+}
+
+/**
+ * latexila_build_job_run_finish:
+ * @build_job: a build job.
+ * @result: a #GAsyncResult.
+ *
+ * Finishes the operation started with latexila_build_job_run_async().
+ *
+ * Returns: %TRUE if the build job has run successfully.
+ */
+gboolean
+latexila_build_job_run_finish (LatexilaBuildJob *build_job,
+                               GAsyncResult     *result)
+{
+  g_return_if_fail (g_task_is_valid (result, build_job));
+
+  return g_task_propagate_boolean (G_TASK (result), NULL);
+}
diff --git a/src/liblatexila/latexila-build-job.h b/src/liblatexila/latexila-build-job.h
index d09da3d..f4bef9b 100644
--- a/src/liblatexila/latexila-build-job.h
+++ b/src/liblatexila/latexila-build-job.h
@@ -20,7 +20,7 @@
 #ifndef __LATEXILA_BUILD_JOB_H__
 #define __LATEXILA_BUILD_JOB_H__
 
-#include <glib-object.h>
+#include <gio/gio.h>
 #include "latexila-types.h"
 #include "latexila-post-processor.h"
 
@@ -56,6 +56,16 @@ LatexilaBuildJob *  latexila_build_job_clone                      (LatexilaBuild
 
 gchar *             latexila_build_job_to_xml                     (LatexilaBuildJob *build_job);
 
+void                latexila_build_job_run_async                  (LatexilaBuildJob    *build_job,
+                                                                   GFile               *file,
+                                                                   LatexilaBuildView   *build_view,
+                                                                   GCancellable        *cancellable,
+                                                                   GAsyncReadyCallback  callback,
+                                                                   gpointer             user_data);
+
+gboolean            latexila_build_job_run_finish                 (LatexilaBuildJob *build_job,
+                                                                   GAsyncResult     *result);
+
 G_END_DECLS
 
 #endif /* __LATEXILA_BUILD_JOB_H__ */
diff --git a/src/liblatexila/latexila-build-tool.c b/src/liblatexila/latexila-build-tool.c
index 67b393a..b459f01 100644
--- a/src/liblatexila/latexila-build-tool.c
+++ b/src/liblatexila/latexila-build-tool.c
@@ -44,6 +44,13 @@ struct _LatexilaBuildToolPrivate
   /* A list of LatexilaBuildJob's. */
   GQueue *jobs;
 
+  /* Used for running the build tool. */
+  GTask *task;
+  GFile *file;
+  LatexilaBuildView *build_view;
+  GtkTreeIter main_title;
+  GList *current_job;
+
   guint enabled : 1;
 };
 
@@ -113,6 +120,9 @@ latexila_build_tool_set_property (GObject      *object,
 {
   LatexilaBuildTool *build_tool = LATEXILA_BUILD_TOOL (object);
 
+  /* The build tool can not be modified when it is running. */
+  g_return_if_fail (build_tool->priv->task == NULL);
+
   switch (prop_id)
     {
     case PROP_LABEL:
@@ -165,6 +175,9 @@ latexila_build_tool_dispose (GObject *object)
       build_tool->priv->jobs = NULL;
     }
 
+  g_clear_object (&build_tool->priv->file);
+  g_clear_object (&build_tool->priv->build_view);
+
   G_OBJECT_CLASS (latexila_build_tool_parent_class)->dispose (object);
 }
 
@@ -362,6 +375,9 @@ latexila_build_tool_add_job (LatexilaBuildTool *build_tool,
   g_return_if_fail (LATEXILA_IS_BUILD_TOOL (build_tool));
   g_return_if_fail (LATEXILA_IS_BUILD_JOB (build_job));
 
+  /* The build tool can not be modified when it is running. */
+  g_return_if_fail (build_tool->priv->task == NULL);
+
   g_queue_push_tail (build_tool->priv->jobs, build_job);
   g_object_ref (build_job);
 }
@@ -431,30 +447,118 @@ latexila_build_tool_to_xml (LatexilaBuildTool *tool)
   return g_string_free (contents, FALSE);
 }
 
+static void
+failed (LatexilaBuildTool *build_tool)
+{
+  latexila_build_view_set_title_state (build_tool->priv->build_view,
+                                       &build_tool->priv->main_title,
+                                       LATEXILA_BUILD_STATE_FAILED);
+
+  g_task_return_boolean (build_tool->priv->task, FALSE);
+}
+
+static void
+open_files (LatexilaBuildTool *build_tool)
+{
+  g_task_return_boolean (build_tool->priv->task, TRUE);
+}
+
+static void
+run_job_cb (LatexilaBuildJob  *build_job,
+            GAsyncResult      *result,
+            LatexilaBuildTool *build_tool)
+{
+  gboolean success;
+
+  success = latexila_build_job_run_finish (build_job, result);
+
+  if (success)
+    {
+      build_tool->priv->current_job = build_tool->priv->current_job->next;
+      run_job (build_tool);
+    }
+  else
+    {
+      failed (build_tool);
+    }
+}
+
+static void
+run_job (LatexilaBuildTool *build_tool)
+{
+  LatexilaBuildJob *build_job;
+
+  if (build_tool->priv->current_job == NULL)
+    {
+      open_files (build_tool);
+      return;
+    }
+
+  build_job = build_tool->priv->current_job->data;
+
+  latexila_build_job_run_async (build_job,
+                                build_tool->priv->file,
+                                build_tool->priv->build_view,
+                                g_task_get_cancellable (build_tool->priv->task),
+                                (GAsyncReadyCallback) run_job_cb,
+                                build_tool);
+}
+
 /**
- * latexila_build_tool_run:
+ * latexila_build_tool_run_async:
  * @build_tool: a build tool.
  * @file: a file.
  * @build_view: a build view.
+ * @cancellable: a #GCancellable object.
+ * @callback: the callback to call when the operation is finished.
+ * @user_data: the data to pass to the callback function.
  *
  * Run a build tool on a file with the messages displayed in a build view.
  */
 void
-latexila_build_tool_run (LatexilaBuildTool *build_tool,
-                         GFile             *file,
-                         LatexilaBuildView *build_view)
+latexila_build_tool_run_async (LatexilaBuildTool   *build_tool,
+                               GFile               *file,
+                               LatexilaBuildView   *build_view,
+                               GCancellable        *cancellable,
+                               GAsyncReadyCallback  callback,
+                               gpointer             user_data)
 {
   g_return_if_fail (LATEXILA_IS_BUILD_TOOL (build_tool));
   g_return_if_fail (G_IS_FILE (file));
   g_return_if_fail (LATEXILA_IS_BUILD_VIEW (build_view));
 
+  g_return_if_fail (build_tool->priv->task == NULL);
+
+  build_tool->priv->task = g_task_new (build_tool, cancellable, callback, user_data);
+
+  g_clear_object (&build_tool->priv->file);
+  build_tool->priv->file = g_object_ref (file);
+
+  g_clear_object (&build_tool->priv->build_view);
+  build_tool->priv->build_view = g_object_ref (build_view);
+
   latexila_build_view_clear (build_view);
 
-  latexila_build_view_add_main_title (build_view,
-                                      build_tool->priv->label,
-                                      LATEXILA_BUILD_STATE_RUNNING);
+  build_tool->priv->main_title = latexila_build_view_add_main_title (build_view,
+                                                                     build_tool->priv->label,
+                                                                     LATEXILA_BUILD_STATE_RUNNING);
+
+  build_tool->priv->current_job = build_tool->priv->jobs->head;
+  run_job (build_tool);
+}
+
+/**
+ * latexila_build_tool_run_finish:
+ * @build_tool: a build tool.
+ * @result: a #GAsyncResult.
+ *
+ * Finishes the operation started with latexila_build_tool_run_async().
+ */
+void
+latexila_build_tool_run_finish (LatexilaBuildTool *build_tool,
+                                GAsyncResult      *result)
+{
+  g_return_if_fail (g_task_is_valid (result, build_tool));
 
-  latexila_build_view_add_job_title (build_view,
-                                     "job",
-                                     LATEXILA_BUILD_STATE_SUCCEEDED);
+  g_task_propagate_boolean (G_TASK (result), NULL);
 }
diff --git a/src/liblatexila/latexila-build-tool.h b/src/liblatexila/latexila-build-tool.h
index 77c6443..32c5dc6 100644
--- a/src/liblatexila/latexila-build-tool.h
+++ b/src/liblatexila/latexila-build-tool.h
@@ -62,9 +62,15 @@ GList *               latexila_build_tool_get_jobs                  (LatexilaBui
 
 gchar *               latexila_build_tool_to_xml                    (LatexilaBuildTool *tool);
 
-void                  latexila_build_tool_run                       (LatexilaBuildTool *build_tool,
-                                                                     GFile             *file,
-                                                                     LatexilaBuildView *build_view);
+void                  latexila_build_tool_run_async                 (LatexilaBuildTool   *build_tool,
+                                                                     GFile               *file,
+                                                                     LatexilaBuildView   *build_view,
+                                                                     GCancellable        *cancellable,
+                                                                     GAsyncReadyCallback  callback,
+                                                                     gpointer             user_data);
+
+void                  latexila_build_tool_run_finish                (LatexilaBuildTool *build_tool,
+                                                                     GAsyncResult      *result);
 
 G_END_DECLS
 
diff --git a/src/liblatexila/latexila-build-view.c b/src/liblatexila/latexila-build-view.c
index 879a291..60c3a20 100644
--- a/src/liblatexila/latexila-build-view.c
+++ b/src/liblatexila/latexila-build-view.c
@@ -565,6 +565,9 @@ latexila_build_view_clear (LatexilaBuildView *build_view)
   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
 
   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (build_view));
+
+  build_view->priv->has_details = FALSE;
+  g_object_notify (G_OBJECT (build_view), "has-details");
 }
 
 static GtkTreeIter


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