[tracker/pushplugin] Further refactoring, please squash me



commit da0654d85f147c9095f8da4c5f9c39b33f821915
Author: Philip Van Hoof <philip codeminded be>
Date:   Tue Sep 22 12:19:20 2009 +0200

    Further refactoring, please squash me

 src/plugins/evolution/Makefile.am                |    3 -
 src/plugins/evolution/tracker-evolution-plugin.c |  269 ++++++++++++++--------
 2 files changed, 170 insertions(+), 102 deletions(-)
---
diff --git a/src/plugins/evolution/Makefile.am b/src/plugins/evolution/Makefile.am
index 884f12c..15f0f61 100644
--- a/src/plugins/evolution/Makefile.am
+++ b/src/plugins/evolution/Makefile.am
@@ -42,14 +42,11 @@ liborg_freedesktop_Tracker_evolution_plugin_la_LIBADD = 		\
 	$(DBUS_LIBS)							\
 	$(GCOV_LIBS)
 
-dbus_sources = 								\
-	tracker-evolution-registrar-glue.h
 
 %-glue.h: %.xml
 	$(DBUSBINDINGTOOL) --mode=glib-server --output=$@ --prefix=$(subst -,_,$*) $^
 
 BUILT_SOURCES = 							\
-	$(dbus_sources)							\
 	$(eplugin_DATA)
 
 CLEANFILES = 								\
diff --git a/src/plugins/evolution/tracker-evolution-plugin.c b/src/plugins/evolution/tracker-evolution-plugin.c
index 94ec2ea..c7f1fd4 100644
--- a/src/plugins/evolution/tracker-evolution-plugin.c
+++ b/src/plugins/evolution/tracker-evolution-plugin.c
@@ -161,6 +161,9 @@ typedef struct {
 	GHashTable *registered_stores;
 	GList *registered_clients;
 	EAccountList *accounts;
+	TrackerClient *client;
+	DBusGProxy *dbus_proxy;
+	DBusGConnection *connection;
 } TrackerEvolutionPluginPrivate;
 
 typedef struct {
@@ -177,16 +180,18 @@ typedef struct {
 
 
 static GQueue *many_queue = NULL;
-static DBusGProxy *dbus_proxy = NULL;
 static TrackerEvolutionPlugin *manager = NULL;
 static GStaticRecMutex glock = G_STATIC_REC_MUTEX_INIT;
 static guint register_count = 0;
-static TrackerClient *client = NULL;
 
 /* Prototype declarations */
 static void register_account (TrackerEvolutionPlugin *self, EAccount *account);
 static void unregister_account (TrackerEvolutionPlugin *self, EAccount *account);
 int e_plugin_lib_enable (EPluginLib *ep, int enable);
+static void miner_started (TrackerMiner *miner);
+static void miner_stopped (TrackerMiner *miner);
+static void miner_paused (TrackerMiner *miner);
+static void miner_resumed (TrackerMiner *miner);
 
 /* First a bunch of helper functions. */
 #if 0
@@ -294,32 +299,44 @@ folder_registry_new (const gchar *account_uri,
 }
 
 static void
-on_replied (GError *error, gpointer user_data) { }
+on_replied (GError *error, gpointer user_data) 
+{ 
+	g_object_unref (user_data);
+}
 
 static void
-send_sparql_update (const gchar *sparql)
+send_sparql_update (TrackerEvolutionPlugin *self, const gchar *sparql)
 {
-	if (client) {
-		tracker_resources_batch_sparql_update_async (client, sparql,
-		                                             on_replied, NULL);
+	TrackerEvolutionPluginPrivate *priv = TRACKER_EVOLUTION_PLUGIN_GET_PRIVATE (self);
+
+	if (priv->client) {
+		tracker_resources_batch_sparql_update_async (priv->client, sparql,
+		                                             on_replied, 
+		                                             g_object_ref (self));
 	}
 }
 
 static void
-send_sparql_commit (gboolean update)
+send_sparql_commit (TrackerEvolutionPlugin *self, gboolean update)
 {
-	if (client) {
+	TrackerEvolutionPluginPrivate *priv = TRACKER_EVOLUTION_PLUGIN_GET_PRIVATE (self);
+
+	if (priv->client) {
 		if (update) {
 			gchar *update = g_strdup_printf ("DELETE { <" DATASOURCE_URN "> nie:contentLastModified ?d } "
 			                                 "WHERE { <" DATASOURCE_URN "> a nie:InformationElement ; nie:contentLastModified ?d } \n"
 			                                 "INSERT { <" DATASOURCE_URN "> nie:contentLastModified \"%s\" }",
-			                                 tracker_date_to_string (time(NULL)));
-			tracker_resources_batch_sparql_update_async (client, update, 
-			                                             on_replied, NULL);
+			                                 tracker_date_to_string (time (NULL)));
+
+			tracker_resources_batch_sparql_update_async (priv->client, update, 
+			                                             on_replied, 
+			                                             g_object_ref (self));
+
 			g_free (update);
 		}
 
-		tracker_resources_batch_commit_async (client, on_replied, NULL);
+		tracker_resources_batch_commit_async (priv->client, on_replied, 
+		                                      g_object_ref (self));
 	}
 }
 
@@ -721,7 +738,7 @@ on_folder_summary_changed (CamelFolder *folder,
 
 				tracker_sparql_builder_insert_close (sparql);
 
-				send_sparql_update (tracker_sparql_builder_get_result (sparql));
+				send_sparql_update (info->self, tracker_sparql_builder_get_result (sparql));
 
 				g_object_unref (sparql);
 
@@ -753,11 +770,11 @@ on_folder_summary_changed (CamelFolder *folder,
 
 		g_string_append_c (sparql, '}');
 
-		send_sparql_update (sparql->str);
+		send_sparql_update (info->self, sparql->str);
 		g_string_free (sparql, TRUE);
 	}
 
-	send_sparql_commit (TRUE);
+	send_sparql_commit (info->self, TRUE);
 
 	g_free (em_uri);
 }
@@ -789,6 +806,8 @@ many_idle_handler (gpointer user_data)
 	     queued_set != NULL && popped < QUEUED_SETS_PER_MAINLOOP; 
 	     queued_set  = g_queue_pop_head (many_queue), popped++) {
 
+		TrackerEvolutionPluginPrivate *priv = TRACKER_EVOLUTION_PLUGIN_GET_PRIVATE (queued_set->self);
+
 		/* During initial introduction the client-registrar might 
 		 * decide to crash, disconnect, stop listening. That 
 		 * would result in critical warnings so we start ignoring
@@ -797,8 +816,12 @@ many_idle_handler (gpointer user_data)
 		 * We nonetheless need to handle these items to clean up
 		 * the queue properly, of course. */
 		
-		if (client) {
-			send_sparql_update (tracker_sparql_builder_get_result (queued_set->sparql));
+		if (priv->client) {
+			const gchar *query;
+
+			query = tracker_sparql_builder_get_result (queued_set->sparql);
+
+			send_sparql_update (queued_set->self, query);
 		} else {
 			gint i;
 			
@@ -811,12 +834,12 @@ many_idle_handler (gpointer user_data)
 				queued_set_free (g_queue_pop_nth (many_queue, i));
 			}
 		}
-		
+
 		queued_set_free (queued_set);
 	}
 
 	if (!queued_set) {
-		send_sparql_commit (TRUE);
+		send_sparql_commit (user_data, TRUE);
 	}
 
 	return queued_set ? TRUE : FALSE;
@@ -825,18 +848,19 @@ many_idle_handler (gpointer user_data)
 static void
 many_idle_destroy (gpointer user_data)
 {
+	g_object_unref (user_data);
 	g_queue_free (many_queue);
 	many_queue = NULL;
 }
 
 static void
-start_many_handler (void)
+start_many_handler (TrackerEvolutionPlugin *self)
 {
 	/* We just slow it down to 'once per second' (for now, we can tweak this
 	 * afterward, of course, but once per second seems to work great) */
 	 g_idle_add_full (G_PRIORITY_LOW,
 	                            many_idle_handler,
-	                            NULL,
+	                            g_object_ref (self),
 	                            many_idle_destroy);
 }
 
@@ -1058,7 +1082,7 @@ introduce_walk_folders_in_folder (TrackerEvolutionPlugin *self,
 				                   queued_set);
 
 				if (start_handler) {
-					start_many_handler ();
+					start_many_handler (self);
 				}
 
 			} else if (sparql) {
@@ -1171,7 +1195,7 @@ introduce_store_deal_with_deleted (TrackerEvolutionPlugin *self,
 
 			g_string_append_c (sparql, '}');
 
-			send_sparql_update (sparql->str);
+			send_sparql_update (self, sparql->str);
 			g_string_free (sparql, TRUE);
 
 		}
@@ -1180,7 +1204,7 @@ introduce_store_deal_with_deleted (TrackerEvolutionPlugin *self,
 
 	}
 
-	send_sparql_commit (TRUE);
+	send_sparql_commit (self, TRUE);
 
 	sqlite3_finalize (stmt);
 	sqlite3_free (query);
@@ -1633,14 +1657,14 @@ register_client (TrackerEvolutionPlugin *self)
 	const gchar *query;
 	guint i;
 
-	if (!client) {
+	if (!priv->client) {
 		return;
 	}
 
 	query = "SELECT ?c "
 		"WHERE { <" DATASOURCE_URN "> nie:contentLastModified ?c }";
 
-	results = tracker_resources_sparql_query (client, query, &error);
+	results = tracker_resources_sparql_query (priv->client, query, &error);
 
 	if (error) {
 		g_warning ("%s\n", error->message);
@@ -1667,9 +1691,9 @@ register_client (TrackerEvolutionPlugin *self)
 
 	if (info->last_checkout < too_old) {
 
-		send_sparql_update ("DELETE { ?s a rdfs:Resource } "
-		                    "WHERE { ?s nie:dataSource <" DATASOURCE_URN "> }");
-		send_sparql_commit (FALSE);
+		send_sparql_update (self, "DELETE { ?s a rdfs:Resource } "
+		                          "WHERE { ?s nie:dataSource <" DATASOURCE_URN "> }");
+		send_sparql_commit (self, FALSE);
 
 		info->last_checkout = 0;
 	}
@@ -1934,14 +1958,9 @@ on_account_changed (EAccountList *list,
 static void
 disable_plugin (void) 
 {
-	if (client) {
-		tracker_disconnect (client);
-		client = NULL;
-	}
-
-	if (dbus_proxy) {
-		g_object_unref (dbus_proxy);
-		dbus_proxy = NULL;
+	if (manager) {
+		g_object_unref (manager);
+		manager = NULL;
 	}
 }
 
@@ -1969,7 +1988,7 @@ list_names_reply_cb (DBusGProxy     *proxy,
 
 	while (names[i] != NULL) {
 		if (g_strcmp0 (names[i], TRACKER_SERVICE) == 0) {
-			register_client (manager);
+			register_client (user_data);
 			break;
 		}
 		i++;
@@ -1986,19 +2005,21 @@ name_owner_changed_cb (DBusGProxy *proxy,
 		       gchar *new_owner, 
 		       gpointer user_data)
 {
+	TrackerEvolutionPluginPrivate *priv = TRACKER_EVOLUTION_PLUGIN_GET_PRIVATE (user_data);
+
 	if (g_strcmp0 (name, TRACKER_SERVICE) == 0) {
 		if (tracker_is_empty_string (new_owner) && !tracker_is_empty_string (old_owner)) {
-			if (client) {
-				tracker_disconnect (client);
-				client = NULL;
+			if (priv->client) {
+				tracker_disconnect (priv->client);
+				priv->client = NULL;
 			}
 		}
 
 		if (tracker_is_empty_string (old_owner) && !tracker_is_empty_string (new_owner)) {
-			if (!client) {
-				client = tracker_connect (FALSE, G_MAXINT);
+			if (!priv->client) {
+				priv->client = tracker_connect (FALSE, G_MAXINT);
 			}
-			register_client (manager);
+			register_client (user_data);
 		}
 	}
 }
@@ -2006,52 +2027,14 @@ name_owner_changed_cb (DBusGProxy *proxy,
 static void
 enable_plugin (void)
 {
-	DBusGConnection *connection;
-	GError *error = NULL;
-
-	if (client || dbus_proxy) {
-		disable_plugin ();
-	}
-
 	if (manager) {
 		g_object_unref (manager);
 	}
 
-	connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
-
-	if (error)
-		goto error_handler;
-
-	client = tracker_connect (FALSE, G_MAXINT);
-
 	manager = g_object_new (TRACKER_TYPE_EVOLUTION_PLUGIN, 
 	                        "name", "Emails", NULL);
 
-	dbus_proxy = dbus_g_proxy_new_for_name (connection, 
-	                                        DBUS_SERVICE_DBUS,
-	                                        DBUS_PATH_DBUS,
-	                                        DBUS_INTERFACE_DBUS);
-
-	dbus_g_proxy_add_signal (dbus_proxy, "NameOwnerChanged",
-	                         G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
-	                         G_TYPE_INVALID);
-
-	dbus_g_proxy_connect_signal (dbus_proxy, "NameOwnerChanged",
-	                             G_CALLBACK (name_owner_changed_cb),
-	                             NULL, NULL);
-
-	dbus_g_proxy_begin_call (dbus_proxy, "ListNames",
-	                         list_names_reply_cb, NULL, NULL,
-	                         G_TYPE_INVALID,
-	                         G_TYPE_INVALID);
-
-	error_handler:
-
-	if (error) {
-		g_warning ("Could not setup DBus, %s\n", error->message);
-		disable_plugin();
-		g_error_free (error);
-	}
+	g_signal_emit_by_name (manager, "started");
 }
 
 
@@ -2070,7 +2053,6 @@ e_plugin_lib_enable (EPluginLib *ep, int enabled)
 	return 0;
 }
 
-
 static void
 tracker_evolution_plugin_finalize (GObject *plugin)
 {
@@ -2092,19 +2074,36 @@ tracker_evolution_plugin_finalize (GObject *plugin)
 
 	g_object_unref (priv->accounts);
 
+	if (priv->client) {
+		tracker_disconnect (priv->client);
+		priv->client = NULL;
+	}
 	g_static_rec_mutex_unlock (priv->mutex);
 
+	if (priv->dbus_proxy) {
+		g_object_unref (priv->dbus_proxy);
+	}
+
+	if (priv->connection) {
+		dbus_g_connection_unref (priv->connection);
+	}
+
 	g_slice_free (GStaticRecMutex, priv->mutex);
 
 	G_OBJECT_CLASS (tracker_evolution_plugin_parent_class)->finalize (plugin);
 }
 
 
-
 static void
 tracker_evolution_plugin_class_init (TrackerEvolutionPluginClass *klass)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	TrackerMinerClass *miner_class = TRACKER_MINER_CLASS (klass);
+
+	miner_class->started = miner_started;
+	miner_class->stopped = miner_stopped;
+	miner_class->paused  = miner_paused;
+	miner_class->resumed = miner_resumed;
 
 	object_class->finalize = tracker_evolution_plugin_finalize;
 
@@ -2116,7 +2115,7 @@ tracker_evolution_plugin_init (TrackerEvolutionPlugin *plugin)
 {
 	TrackerEvolutionPluginPrivate *priv = TRACKER_EVOLUTION_PLUGIN_GET_PRIVATE (plugin);
 	EIterator *it;
-	gdouble length, step = 0;
+	GError *error = NULL;
 
 	priv->mutex = g_slice_new0 (GStaticRecMutex);
 	g_static_rec_mutex_init (priv->mutex);
@@ -2130,24 +2129,25 @@ tracker_evolution_plugin_init (TrackerEvolutionPlugin *plugin)
 
 	g_static_rec_mutex_unlock (priv->mutex);
 
-	priv->accounts = g_object_ref (mail_config_get_accounts ());
+	priv->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 
-	length = (gdouble) e_list_length (E_LIST (priv->accounts));
+	if (error) {
+		goto error_handler;
+	}
 
-	g_object_set (plugin, 
-	              "progress", 0.0, 
-	              "status", _("Initializing"),
-	              NULL);
+	priv->dbus_proxy = dbus_g_proxy_new_for_name (priv->connection, 
+	                                              DBUS_SERVICE_DBUS,
+	                                              DBUS_PATH_DBUS,
+	                                              DBUS_INTERFACE_DBUS);
 
-	for (it = e_list_get_iterator (E_LIST (priv->accounts)); e_iterator_is_valid (it); e_iterator_next (it)) {
+	dbus_g_proxy_add_signal (priv->dbus_proxy, "NameOwnerChanged",
+	                         G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
+	                         G_TYPE_INVALID);
 
-		g_object_set (plugin, 
-		              "progress", (((gdouble) (length * step)) / ((gdouble) 100)), 
-		              "status", _("Initializing"),
-		              NULL);
+	priv->accounts = g_object_ref (mail_config_get_accounts ());
 
+	for (it = e_list_get_iterator (E_LIST (priv->accounts)); e_iterator_is_valid (it); e_iterator_next (it)) {
 		register_account (plugin, (EAccount *) e_iterator_get (it));
-		step++;
 	}
 
 	g_object_unref (it);
@@ -2158,4 +2158,75 @@ tracker_evolution_plugin_init (TrackerEvolutionPlugin *plugin)
 	                  G_CALLBACK (on_account_removed), plugin);
 	g_signal_connect (priv->accounts, "account-changed", 
 	                  G_CALLBACK (on_account_changed), plugin);
+	error_handler:
+
+	if (error) {
+		g_warning ("Could not setup DBus for Tracker plugin, %s\n", error->message);
+		g_signal_emit_by_name (plugin, "error");
+		g_error_free (error);
+	}
+}
+
+static void
+dbus_connect_closure (gpointer data, GClosure *closure)
+{
+	g_object_unref (data);
+}
+
+
+static void
+miner_started (TrackerMiner *miner)
+{
+	TrackerEvolutionPluginPrivate *priv = TRACKER_EVOLUTION_PLUGIN_GET_PRIVATE (miner);
+
+	dbus_g_proxy_connect_signal (priv->dbus_proxy, "NameOwnerChanged",
+	                             G_CALLBACK (name_owner_changed_cb),
+	                             g_object_ref (miner),
+	                             dbus_connect_closure);
+
+	dbus_g_proxy_begin_call (priv->dbus_proxy, "ListNames",
+	                         list_names_reply_cb, 
+	                         g_object_ref (miner),
+	                         (GDestroyNotify) g_object_unref,
+	                         G_TYPE_INVALID,
+	                         G_TYPE_INVALID);
+
+	g_object_set (miner,  "progress", 0.0,  "status", _("Initializing"), NULL);
+}
+
+static void
+miner_stopped (TrackerMiner *miner)
+{
+	TrackerEvolutionPluginPrivate *priv = TRACKER_EVOLUTION_PLUGIN_GET_PRIVATE (miner);
+
+	g_object_set (miner,  "progress", 1.0,  "status", _("Idle"), NULL);
+
+	dbus_g_proxy_disconnect_signal (priv->dbus_proxy, "NameOwnerChanged",
+	                                G_CALLBACK (name_owner_changed_cb),
+	                                miner);
+}
+
+static void
+miner_paused (TrackerMiner *miner)
+{
+	TrackerEvolutionPluginPrivate *priv = TRACKER_EVOLUTION_PLUGIN_GET_PRIVATE (miner);
+
+	// Pause 
+
+	dbus_g_proxy_disconnect_signal (priv->dbus_proxy, "NameOwnerChanged",
+	                                G_CALLBACK (name_owner_changed_cb),
+	                                miner);
+}
+
+static void
+miner_resumed (TrackerMiner *miner)
+{
+	TrackerEvolutionPluginPrivate *priv = TRACKER_EVOLUTION_PLUGIN_GET_PRIVATE (miner);
+
+	// Resume
+
+	dbus_g_proxy_connect_signal (priv->dbus_proxy, "NameOwnerChanged",
+	                             G_CALLBACK (name_owner_changed_cb),
+	                             g_object_ref (miner),
+	                             dbus_connect_closure);
 }



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