[nautilus/csoriano/tasks: 7/23] Add task-private utilities



commit 85ba0123abee043b82beccf419627145cc2c3c8c
Author: Ernestas Kulik <ernestask gnome org>
Date:   Mon Jul 3 14:56:20 2017 +0300

    Add task-private utilities

 src/meson.build             |  4 +-
 src/nautilus-task-private.c | 97 +++++++++++++++++++++++++++++++++++++++++++++
 src/nautilus-task-private.h | 10 +++++
 3 files changed, 110 insertions(+), 1 deletion(-)
---
diff --git a/src/meson.build b/src/meson.build
index f46da9e54..8144f83a5 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -271,7 +271,9 @@ libnautilus_sources = [
   'nautilus-task-manager.h',
   'tasks/nautilus-file-task.c',
   'tasks/nautilus-file-task.h',
-  'tasks/nautilus-file-task-private.h'
+  'tasks/nautilus-file-task-private.h',
+  'nautilus-task-private.c',
+  'nautilus-task-private.h'
 ]
 
 nautilus_deps = [
diff --git a/src/nautilus-task-private.c b/src/nautilus-task-private.c
new file mode 100644
index 000000000..62272fd50
--- /dev/null
+++ b/src/nautilus-task-private.c
@@ -0,0 +1,97 @@
+#include "nautilus-task-private.h"
+
+#include <glib-object.h>
+#include <gobject/gvaluecollector.h>
+
+typedef struct
+{
+    GValue instance_and_params[3];
+    guint signal_id;
+    int n_values;
+} EmissionData;
+
+static void
+emission_data_free (EmissionData *data)
+{
+    for (int i = 0; i < data->n_values; i++)
+    {
+        g_value_unset (&data->instance_and_params[i]);
+    }
+
+    g_free (data);
+}
+
+static gboolean
+emit_signal (gpointer data)
+{
+    EmissionData *emission_data;
+
+    emission_data = data;
+
+    g_signal_emitv (emission_data->instance_and_params,
+                    emission_data->signal_id,
+                    0, NULL);
+
+    g_clear_pointer (&emission_data, emission_data_free);
+
+    return FALSE;
+}
+
+void
+nautilus_emit_signal_in_main_context (gpointer instance,
+                                      guint    signal_id,
+                                      ...)
+{
+    va_list ap;
+    EmissionData *emission_data;
+    GSignalQuery query;
+    g_autofree gchar *error = NULL;
+
+    emission_data = g_new0 (EmissionData, 1);
+
+    va_start (ap, signal_id);
+
+    g_value_init (&emission_data->instance_and_params[0],
+                  G_TYPE_FROM_INSTANCE (instance));
+    g_value_set_instance (&emission_data->instance_and_params[0], instance);
+
+    emission_data->signal_id = signal_id;
+
+    g_signal_query (signal_id, &query);
+
+    if (query.signal_id == 0)
+    {
+        g_clear_pointer (&emission_data, emission_data_free);
+
+        va_end (ap);
+
+        return;
+    }
+
+    for (int i = 0; i < query.n_params; i++)
+    {
+        G_VALUE_COLLECT_INIT (&emission_data->instance_and_params[i + 1],
+                              query.param_types[i],
+                              ap, 0, &error);
+
+        if (error != NULL)
+        {
+            break;
+        }
+
+        emission_data->n_values++;
+    }
+
+    if (error != NULL)
+    {
+        g_clear_pointer (&emission_data, emission_data_free);
+
+        va_end (ap);
+
+        return;
+    }
+
+    g_main_context_invoke (NULL, emit_signal, emission_data);
+
+    va_end (ap);
+}
diff --git a/src/nautilus-task-private.h b/src/nautilus-task-private.h
new file mode 100644
index 000000000..f57b4d81e
--- /dev/null
+++ b/src/nautilus-task-private.h
@@ -0,0 +1,10 @@
+#ifndef NAUTILUS_TASK_PRIVATE_H
+#define NAUTILUS_TASK_PRIVATE_H
+
+#include <glib.h>
+
+void nautilus_emit_signal_in_main_context (gpointer instance,
+                                           guint    signal_id,
+                                           ...);
+
+#endif


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