[tracker] tracker-writeback: Fix crash when not possible to get D-Bus connection



commit bfa81902ffd7f71518fb49ce19cdbbf68d593fab
Author: Philip Van Hoof <philip codeminded be>
Date:   Tue Apr 12 11:48:55 2011 +0200

    tracker-writeback: Fix crash when not possible to get D-Bus connection
    
    Fixes NB#245998.

 src/tracker-writeback/tracker-main.c               |    9 ++-
 src/tracker-writeback/tracker-writeback-consumer.c |  101 +++++++++++++-------
 src/tracker-writeback/tracker-writeback-consumer.h |    8 +-
 3 files changed, 79 insertions(+), 39 deletions(-)
---
diff --git a/src/tracker-writeback/tracker-main.c b/src/tracker-writeback/tracker-main.c
index f5f66df..a42226d 100644
--- a/src/tracker-writeback/tracker-main.c
+++ b/src/tracker-writeback/tracker-main.c
@@ -205,7 +205,14 @@ main (int   argc,
 	sanity_check_option_values (config);
 
 
-	consumer = tracker_writeback_consumer_new ();
+	consumer = tracker_writeback_consumer_new (&error);
+
+	if (error) {
+		g_critical ("Error creating consumer: %s", error->message);
+		g_error_free (error);
+
+		return EXIT_FAILURE;
+	}
 
 	/* Create dispatcher thread data here, GType
 	 * initialization for boxed types don't seem
diff --git a/src/tracker-writeback/tracker-writeback-consumer.c b/src/tracker-writeback/tracker-writeback-consumer.c
index 26ad87a..8c27e2e 100644
--- a/src/tracker-writeback/tracker-writeback-consumer.c
+++ b/src/tracker-writeback/tracker-writeback-consumer.c
@@ -65,11 +65,21 @@ enum {
 };
 
 static void tracker_writeback_consumer_finalize    (GObject       *object);
-static void tracker_writeback_consumer_constructed (GObject       *object);
 static gboolean process_queue_cb                   (gpointer       user_data);
+static gboolean writeback_consumer_initable_init   (GInitable     *initable,
+                                                    GCancellable  *cancellable,
+                                                    GError       **error);
 
+static void
+writeback_consumer_initable_iface_init (GInitableIface *iface)
+{
+	iface->init = writeback_consumer_initable_init;
+}
+
+G_DEFINE_TYPE_WITH_CODE (TrackerWritebackConsumer, tracker_writeback_consumer, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
+                                                writeback_consumer_initable_iface_init));
 
-G_DEFINE_TYPE (TrackerWritebackConsumer, tracker_writeback_consumer, G_TYPE_OBJECT)
 
 static void
 tracker_writeback_consumer_class_init (TrackerWritebackConsumerClass *klass)
@@ -77,7 +87,6 @@ tracker_writeback_consumer_class_init (TrackerWritebackConsumerClass *klass)
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
 	object_class->finalize = tracker_writeback_consumer_finalize;
-	object_class->constructed = tracker_writeback_consumer_constructed;
 
 	g_type_class_add_private (object_class, sizeof (TrackerWritebackConsumerPrivate));
 }
@@ -85,18 +94,25 @@ tracker_writeback_consumer_class_init (TrackerWritebackConsumerClass *klass)
 static void
 tracker_writeback_consumer_init (TrackerWritebackConsumer *consumer)
 {
+}
+
+static gboolean
+writeback_consumer_initable_init (GInitable     *initable,
+                                  GCancellable  *cancellable,
+                                  GError       **error)
+{
 	TrackerWritebackConsumerPrivate *priv;
-	GError *error = NULL;
+	GError *new_error = NULL;
+	GList *modules;
 
-	priv = TRACKER_WRITEBACK_CONSUMER_GET_PRIVATE (consumer);
+	priv = TRACKER_WRITEBACK_CONSUMER_GET_PRIVATE (initable);
 
-	priv->connection = tracker_sparql_connection_get (NULL, &error);
+	priv->connection = tracker_sparql_connection_get (NULL, &new_error);
 
 	if (!priv->connection) {
-		g_printerr ("%s: %s\n",
-		            _("Could not establish a connection to Tracker"),
-		            error ? error->message : _("No error given"));
-		g_clear_error (&error);
+		g_propagate_error (error, new_error);
+
+		return FALSE;
 	}
 
 	priv->modules = g_hash_table_new_full (g_str_hash,
@@ -107,6 +123,24 @@ tracker_writeback_consumer_init (TrackerWritebackConsumer *consumer)
 
 	priv->manager = tracker_writeback_get_miner_manager ();
 	priv->state = STATE_IDLE;
+
+	modules = tracker_writeback_modules_list ();
+
+	while (modules) {
+		TrackerWritebackModule *module;
+		const gchar *path;
+
+		path = modules->data;
+		module = tracker_writeback_module_get (path);
+
+		if (module) {
+			g_hash_table_insert (priv->modules, g_strdup (path), module);
+		}
+
+		modules = modules->next;
+	}
+
+	return TRUE;
 }
 
 static void
@@ -120,39 +154,38 @@ tracker_writeback_consumer_finalize (GObject *object)
 		g_object_unref (priv->connection);
 	}
 
-	g_object_unref (priv->manager);
+	if (priv->manager) {
+		g_object_unref (priv->manager);
+	}
+
+	if (priv->modules) {
+		g_hash_table_unref (priv->modules);
+	}
+
+	if (priv->process_queue) {
+		g_queue_free (priv->process_queue);
+	}
 
 	G_OBJECT_CLASS (tracker_writeback_consumer_parent_class)->finalize (object);
 }
 
-static void
-tracker_writeback_consumer_constructed (GObject *object)
+TrackerWritebackConsumer *
+tracker_writeback_consumer_new (GError **error)
 {
-	TrackerWritebackConsumerPrivate *priv;
-	GList *modules;
-
-	priv = TRACKER_WRITEBACK_CONSUMER_GET_PRIVATE (object);
-	modules = tracker_writeback_modules_list ();
+	GError *internal_error = NULL;
+	TrackerWritebackConsumer *ret;
 
-	while (modules) {
-		TrackerWritebackModule *module;
-		const gchar *path;
-
-		path = modules->data;
-		module = tracker_writeback_module_get (path);
-
-		if (module) {
-			g_hash_table_insert (priv->modules, g_strdup (path), module);
-		}
+	ret =  g_initable_new (TRACKER_TYPE_WRITEBACK_CONSUMER,
+	                       NULL,
+	                       &internal_error,
+	                       NULL);
 
-		modules = modules->next;
+	if (internal_error) {
+		g_propagate_error (error, internal_error);
+		return NULL;
 	}
-}
 
-TrackerWritebackConsumer *
-tracker_writeback_consumer_new (void)
-{
-	return g_object_new (TRACKER_TYPE_WRITEBACK_CONSUMER, NULL);
+	return ret;
 }
 
 static gboolean
diff --git a/src/tracker-writeback/tracker-writeback-consumer.h b/src/tracker-writeback/tracker-writeback-consumer.h
index bb875a1..198177b 100644
--- a/src/tracker-writeback/tracker-writeback-consumer.h
+++ b/src/tracker-writeback/tracker-writeback-consumer.h
@@ -46,11 +46,11 @@ struct TrackerWritebackConsumerClass {
 };
 
 GType                      tracker_writeback_consumer_get_type (void) G_GNUC_CONST;
-TrackerWritebackConsumer * tracker_writeback_consumer_new      (void);
+TrackerWritebackConsumer * tracker_writeback_consumer_new      (GError **error);
 
-void tracker_writeback_consumer_add_subject (TrackerWritebackConsumer *consumer,
-                                             gint                      subject,
-                                             GArray                   *rdf_types);
+void tracker_writeback_consumer_add_subject (TrackerWritebackConsumer   *consumer,
+                                             gint                        subject,
+                                             GArray                     *rdf_types);
 
 G_END_DECLS
 



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