[gnome-builder/wip/libide: 95/153] libide: implement basics of diagnostician



commit 30ffa5a8a2b7193a2c0112ea327ccb8adea71afd
Author: Christian Hergert <christian hergert me>
Date:   Wed Feb 11 19:13:01 2015 -0800

    libide: implement basics of diagnostician

 libide/ide-diagnostician.c |  132 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 130 insertions(+), 2 deletions(-)
---
diff --git a/libide/ide-diagnostician.c b/libide/ide-diagnostician.c
index 1da9474..b72ee81 100644
--- a/libide/ide-diagnostician.c
+++ b/libide/ide-diagnostician.c
@@ -16,13 +16,25 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <glib/gi18n.h>
+
+#include "ide-diagnostic-provider.h"
 #include "ide-diagnostician.h"
+#include "ide-diagnostics.h"
+#include "ide-file.h"
+#include "ide-private.h"
 
 typedef struct
 {
   GPtrArray *providers;
 } IdeDiagnosticianPrivate;
 
+typedef struct
+{
+  IdeDiagnostics *diagnostics;
+  guint           active;
+} DiagnoseState;
+
 G_DEFINE_TYPE_WITH_PRIVATE (IdeDiagnostician, ide_diagnostician,
                             IDE_TYPE_OBJECT)
 
@@ -33,18 +45,134 @@ enum {
 
 static GParamSpec *gParamSpecs [LAST_PROP];
 
+static void
+diagnose_state_free (gpointer data)
+{
+  DiagnoseState *state = data;
+
+  if (state)
+    {
+      g_clear_pointer (&state->diagnostics, ide_diagnostics_unref);
+      g_slice_free (DiagnoseState, state);
+    }
+}
+
 void
-_ide_diagnostician_add_provider (IdeDiagnostician      *diagnostician,
+_ide_diagnostician_add_provider (IdeDiagnostician      *self,
                                  IdeDiagnosticProvider *provider)
 {
+  IdeDiagnosticianPrivate *priv = ide_diagnostician_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_DIAGNOSTICIAN (self));
+  g_return_if_fail (IDE_IS_DIAGNOSTIC_PROVIDER (provider));
 
+  g_ptr_array_add (priv->providers, g_object_ref (provider));
 }
 
 void
-_ide_diagnostician_remove_provider (IdeDiagnostician      *diagnostician,
+_ide_diagnostician_remove_provider (IdeDiagnostician      *self,
                                     IdeDiagnosticProvider *provider)
 {
+  IdeDiagnosticianPrivate *priv = ide_diagnostician_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_DIAGNOSTICIAN (self));
+  g_return_if_fail (IDE_IS_DIAGNOSTIC_PROVIDER (provider));
+
+  g_ptr_array_remove (priv->providers, provider);
+}
+
+static void
+diagnose_cb (GObject      *object,
+             GAsyncResult *result,
+             gpointer      user_data)
+{
+  IdeDiagnosticProvider *provider = (IdeDiagnosticProvider *)object;
+  IdeDiagnostics *ret;
+  g_autoptr(GTask) task = user_data;
+  g_autoptr(GError) error = NULL;
+  DiagnoseState *state;
+
+  g_return_if_fail (IDE_IS_DIAGNOSTIC_PROVIDER (provider));
+  g_return_if_fail (G_IS_TASK (task));
+
+  state = g_task_get_task_data (task);
+
+  state->active--;
+
+  ret = ide_diagnostic_provider_diagnose_finish (provider, result, &error);
+
+  if (!ret)
+    {
+      g_info ("%s", error->message);
+      goto maybe_complete;
+    }
+
+  ide_diagnostics_merge (state->diagnostics, ret);
+  ide_diagnostics_unref (ret);
+
+maybe_complete:
+  if (!state->active)
+    {
+      g_task_return_pointer (task,
+                             ide_diagnostics_ref (state->diagnostics),
+                             (GDestroyNotify)ide_diagnostics_unref);
+    }
+}
+
+void
+ide_diagnostician_diagnose_async (IdeDiagnostician    *self,
+                                  IdeFile             *file,
+                                  GCancellable        *cancellable,
+                                  GAsyncReadyCallback  callback,
+                                  gpointer             user_data)
+{
+  IdeDiagnosticianPrivate *priv = ide_diagnostician_get_instance_private (self);
+  DiagnoseState *state;
+  g_autoptr(GTask) task = NULL;
+  gsize i;
+
+  g_return_if_fail (IDE_IS_DIAGNOSTICIAN (self));
+  g_return_if_fail (IDE_IS_FILE (file));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (self, cancellable, callback, user_data);
+
+  if (!priv->providers->len)
+    {
+      g_task_return_pointer (task,
+                             _ide_diagnostics_new (NULL),
+                             (GDestroyNotify)g_ptr_array_unref);
+      return;
+    }
+
+  state = g_slice_new0 (DiagnoseState);
+  state->active = priv->providers->len;
+  state->diagnostics = _ide_diagnostics_new (NULL);
+
+  g_task_set_task_data (task, state, diagnose_state_free);
+
+  for (i = 0; i < priv->providers->len; i++)
+    {
+      IdeDiagnosticProvider *provider = g_ptr_array_index (priv->providers, i);
+
+      ide_diagnostic_provider_diagnose_async (provider,
+                                              file,
+                                              cancellable,
+                                              diagnose_cb,
+                                              g_object_ref (task));
+    }
+}
+
+IdeDiagnostics *
+ide_diagnostician_diagnose_finish (IdeDiagnostician  *self,
+                                   GAsyncResult      *result,
+                                   GError           **error)
+{
+  GTask *task = (GTask *)result;
+
+  g_return_val_if_fail (G_IS_TASK (result), NULL);
 
+  return g_task_propagate_pointer (task, error);
 }
 
 static void


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