[tracker/wip/removable-device-completed: 6/11] libtracker-miner: Make TrackerStorage a global singleton



commit 119f6bd4cad0532e5ab1470504a44854d2e24c77
Author: Sam Thursfield <sam thursfield codethink co uk>
Date:   Wed Apr 25 11:10:02 2012 +0100

    libtracker-miner: Make TrackerStorage a global singleton
    
    Mounts are system-wide, there's no reason to have multiple instances
    dotted around in different parts of the code.
    
    tracker_storage_new() is now deprecated. Use tracker_storage_get()
    instead.

 .../libtracker-miner/libtracker-miner-sections.txt |    1 +
 src/libtracker-miner/tracker-storage.c             |   27 ++++++++++-
 src/libtracker-miner/tracker-storage.h             |    2 +
 src/miners/fs/tracker-miner-files.c                |   43 ++++++++++--------
 src/tracker-extract/tracker-controller.c           |    6 +--
 src/tracker-extract/tracker-media-art.c            |   47 ++++----------------
 src/tracker-writeback/tracker-writeback.c          |    6 +--
 7 files changed, 62 insertions(+), 70 deletions(-)
---
diff --git a/docs/reference/libtracker-miner/libtracker-miner-sections.txt b/docs/reference/libtracker-miner/libtracker-miner-sections.txt
index 8187902..06d9b89 100644
--- a/docs/reference/libtracker-miner/libtracker-miner-sections.txt
+++ b/docs/reference/libtracker-miner/libtracker-miner-sections.txt
@@ -238,6 +238,7 @@ tracker_password_provider_get_type
 TrackerStorage
 TrackerStorageClass
 TrackerStorageType
+tracker_storage_get
 tracker_storage_get_device_roots
 tracker_storage_get_device_uuids
 tracker_storage_get_mount_point_for_uuid
diff --git a/src/libtracker-miner/tracker-storage.c b/src/libtracker-miner/tracker-storage.c
index bbf771d..277fb38 100644
--- a/src/libtracker-miner/tracker-storage.c
+++ b/src/libtracker-miner/tracker-storage.c
@@ -97,6 +97,8 @@ enum {
 
 static guint signals[LAST_SIGNAL] = {0};
 
+static TrackerStorage *global_instance = NULL;
+
 G_DEFINE_TYPE (TrackerStorage, tracker_storage, G_TYPE_OBJECT);
 
 static void
@@ -842,13 +844,35 @@ mount_pre_removed_cb (GVolumeMonitor *monitor,
 }
 
 /**
+ * tracker_storage_get:
+ *
+ * Returns the global instance of #TrackerStorage. You should not
+ * call g_object_unref() on this object.
+ *
+ * Returns: (transfer none): The #TrackerStorage singleton object.
+ *
+ * Since: 0.14.2
+ **/
+TrackerStorage *
+tracker_storage_get (void)
+{
+	if (global_instance == NULL) {
+		global_instance = g_object_new (TRACKER_TYPE_STORAGE, NULL);
+	}
+
+	return global_instance;
+}
+
+/**
  * tracker_storage_new:
  *
- * Creates a new instance of #TrackerStorage.
+ * Creates a new instance of #TrackerStorage. You should use
+ * tracker_storage_get() instead to use the global instance.
  *
  * Returns: The newly created #TrackerStorage.
  *
  * Since: 0.8
+ * Deprecated: 0.14.2: Use tracker_storage_get() instead.
  **/
 TrackerStorage *
 tracker_storage_new (void)
@@ -1104,4 +1128,3 @@ tracker_storage_get_uuid_for_file (TrackerStorage *storage,
 
 	return info->uuid;
 }
-
diff --git a/src/libtracker-miner/tracker-storage.h b/src/libtracker-miner/tracker-storage.h
index f3101c0..17f6596 100644
--- a/src/libtracker-miner/tracker-storage.h
+++ b/src/libtracker-miner/tracker-storage.h
@@ -99,6 +99,8 @@ struct _TrackerStorageClass {
 };
 
 GType              tracker_storage_get_type                 (void) G_GNUC_CONST;
+TrackerStorage *   tracker_storage_get                      (void);
+G_GNUC_DEPRECATED_FOR(tracker_storage_get)
 TrackerStorage *   tracker_storage_new                      (void);
 GSList *           tracker_storage_get_device_roots         (TrackerStorage     *storage,
                                                              TrackerStorageType  type,
diff --git a/src/miners/fs/tracker-miner-files.c b/src/miners/fs/tracker-miner-files.c
index 6503bf9..b4a1b07 100644
--- a/src/miners/fs/tracker-miner-files.c
+++ b/src/miners/fs/tracker-miner-files.c
@@ -69,7 +69,6 @@ struct ProcessFileData {
 
 struct TrackerMinerFilesPrivate {
 	TrackerConfig *config;
-	TrackerStorage *storage;
 
 	GVolumeMonitor *volume_monitor;
 
@@ -248,16 +247,17 @@ static void
 tracker_miner_files_init (TrackerMinerFiles *mf)
 {
 	TrackerMinerFilesPrivate *priv;
+	TrackerStorage *storage;
 
 	priv = mf->private = TRACKER_MINER_FILES_GET_PRIVATE (mf);
 
-	priv->storage = tracker_storage_new ();
+	storage = tracker_storage_get ();
 
-	g_signal_connect (priv->storage, "mount-point-added",
+	g_signal_connect (storage, "mount-point-added",
 	                  G_CALLBACK (mount_point_added_cb),
 	                  mf);
 
-	g_signal_connect (priv->storage, "mount-point-removed",
+	g_signal_connect (storage, "mount-point-removed",
 	                  G_CALLBACK (mount_point_removed_cb),
 	                  mf);
 
@@ -299,6 +299,7 @@ miner_files_initable_init (GInitable     *initable,
 	TrackerMinerFiles *mf;
 	TrackerMinerFS *fs;
 	TrackerIndexingTree *indexing_tree;
+	TrackerStorage *storage;
 	TrackerDirectoryFlags flags;
 	GError *inner_error = NULL;
 	GSList *mounts = NULL;
@@ -308,6 +309,8 @@ miner_files_initable_init (GInitable     *initable,
 	mf = TRACKER_MINER_FILES (initable);
 	fs = TRACKER_MINER_FS (initable);
 	indexing_tree = tracker_miner_fs_get_indexing_tree (fs);
+	storage = tracker_storage_get ();
+
 	tracker_indexing_tree_set_filter_hidden (indexing_tree, TRUE);
 
 	miner_files_update_filters (mf);
@@ -350,7 +353,7 @@ miner_files_initable_init (GInitable     *initable,
 	mf->private->index_removable_devices = tracker_config_get_index_removable_devices (mf->private->config);
 	if (mf->private->index_removable_devices) {
 		/* Get list of roots for removable devices (excluding optical) */
-		mounts = tracker_storage_get_device_roots (mf->private->storage,
+		mounts = tracker_storage_get_device_roots (storage,
 		                                           TRACKER_STORAGE_REMOVABLE,
 		                                           TRUE);
 	}
@@ -362,7 +365,7 @@ miner_files_initable_init (GInitable     *initable,
 	                                    FALSE);
 	if (mf->private->index_optical_discs) {
 		/* Get list of roots for removable+optical devices */
-		m = tracker_storage_get_device_roots (mf->private->storage,
+		m = tracker_storage_get_device_roots (storage,
 		                                      TRACKER_STORAGE_OPTICAL | TRACKER_STORAGE_REMOVABLE,
 		                                      TRUE);
 		mounts = g_slist_concat (mounts, m);
@@ -629,10 +632,6 @@ miner_files_finalize (GObject *object)
 	}
 #endif /* defined(HAVE_UPOWER) || defined(HAVE_HAL) */
 
-	if (priv->storage) {
-		g_object_unref (priv->storage);
-	}
-
 	if (priv->volume_monitor) {
 		g_signal_handlers_disconnect_by_func (priv->volume_monitor,
 		                                      mount_pre_unmount_cb,
@@ -892,6 +891,7 @@ init_mount_points (TrackerMinerFiles *miner_files)
 {
 	TrackerMiner *miner = TRACKER_MINER (miner_files);
 	TrackerMinerFilesPrivate *priv;
+	TrackerStorage *storage;
 	GHashTable *volumes;
 	GHashTableIter iter;
 	gpointer key, value;
@@ -914,6 +914,8 @@ init_mount_points (TrackerMinerFiles *miner_files)
 
 	priv = TRACKER_MINER_FILES_GET_PRIVATE (miner);
 
+	storage = tracker_storage_get ();
+
 	volumes = g_hash_table_new_full (g_str_hash, g_str_equal,
 	                                 (GDestroyNotify) g_free,
 	                                 NULL);
@@ -944,7 +946,7 @@ init_mount_points (TrackerMinerFiles *miner_files)
 	g_object_unref (cursor);
 
 	/* Then, get all currently mounted non-REMOVABLE volumes, according to GIO */
-	uuids = tracker_storage_get_device_uuids (priv->storage, 0, TRUE);
+	uuids = tracker_storage_get_device_uuids (storage, 0, TRUE);
 	for (u = uuids; u; u = u->next) {
 		const gchar *uuid;
 		gchar *non_removable_device_urn;
@@ -964,7 +966,7 @@ init_mount_points (TrackerMinerFiles *miner_files)
 
 	/* Then, get all currently mounted REMOVABLE volumes, according to GIO */
 	if (priv->index_removable_devices) {
-		uuids = tracker_storage_get_device_uuids (priv->storage, TRACKER_STORAGE_REMOVABLE, FALSE);
+		uuids = tracker_storage_get_device_uuids (storage, TRACKER_STORAGE_REMOVABLE, FALSE);
 		for (u = uuids; u; u = u->next) {
 			const gchar *uuid;
 			gchar *removable_device_urn;
@@ -1002,8 +1004,8 @@ init_mount_points (TrackerMinerFiles *miner_files)
 				const gchar *uuid;
 
 				uuid = urn + strlen (TRACKER_DATASOURCE_URN_PREFIX);
-				mount_point = tracker_storage_get_mount_point_for_uuid (priv->storage, uuid);
-				type = tracker_storage_get_type_for_uuid (priv->storage, uuid);
+				mount_point = tracker_storage_get_mount_point_for_uuid (storage, uuid);
+				type = tracker_storage_get_type_for_uuid (storage, uuid);
 			}
 
 			if (urn) {
@@ -1767,6 +1769,7 @@ static gboolean
 index_volumes_changed_idle (gpointer user_data)
 {
 	TrackerMinerFiles *mf = user_data;
+	TrackerStorage *storage;
 	GSList *mounts_removed = NULL;
 	GSList *mounts_added = NULL;
 	gboolean new_index_removable_devices;
@@ -1774,6 +1777,8 @@ index_volumes_changed_idle (gpointer user_data)
 
 	g_message ("Volume-related configuration changed, updating...");
 
+	storage = tracker_storage_get ();
+
 	/* Read new config values. Note that if removable devices is FALSE,
 	 * optical discs will also always be FALSE. */
 	new_index_removable_devices = tracker_config_get_index_removable_devices (mf->private->config);
@@ -1787,7 +1792,7 @@ index_volumes_changed_idle (gpointer user_data)
 
 		/* Get list of roots for currently mounted removable devices
 		 * (excluding optical) */
-		m = tracker_storage_get_device_roots (mf->private->storage,
+		m = tracker_storage_get_device_roots (storage,
 		                                      TRACKER_STORAGE_REMOVABLE,
 		                                      TRUE);
 		/* Set new config value */
@@ -1817,7 +1822,7 @@ index_volumes_changed_idle (gpointer user_data)
 		GSList *m;
 
 		/* Get list of roots for removable devices (excluding optical) */
-		m = tracker_storage_get_device_roots (mf->private->storage,
+		m = tracker_storage_get_device_roots (storage,
 		                                      TRACKER_STORAGE_REMOVABLE | TRACKER_STORAGE_OPTICAL,
 		                                      TRUE);
 
@@ -1936,16 +1941,14 @@ miner_files_add_to_datasource (TrackerMinerFiles    *mf,
                                GFile                *file,
                                TrackerSparqlBuilder *sparql)
 {
-	TrackerMinerFilesPrivate *priv;
 	const gchar *removable_device_uuid;
 	gchar *removable_device_urn, *uri;
 	const gchar *urn;
 	gboolean is_iri;
 
-	priv = TRACKER_MINER_FILES_GET_PRIVATE (mf);
 	uri = g_file_get_uri (file);
 
-	removable_device_uuid = tracker_storage_get_uuid_for_file (priv->storage, file);
+	removable_device_uuid = tracker_storage_get_uuid_for_file (tracker_storage_get (), file);
 
 	if (removable_device_uuid) {
 		removable_device_urn = g_strdup_printf (TRACKER_DATASOURCE_URN_PREFIX "%s",
@@ -2950,7 +2953,7 @@ miner_files_add_removable_or_optical_directory (TrackerMinerFiles *mf,
 
 	/* UUID may be NULL, and if so, get it */
 	if (!uuid) {
-		uuid = tracker_storage_get_uuid_for_file (mf->private->storage,
+		uuid = tracker_storage_get_uuid_for_file (tracker_storage_get (),
 		                                          mount_point_file);
 		if (!uuid) {
 			g_critical ("Couldn't get UUID for mount point '%s'",
diff --git a/src/tracker-extract/tracker-controller.c b/src/tracker-extract/tracker-controller.c
index 2d01246..c507036 100644
--- a/src/tracker-extract/tracker-controller.c
+++ b/src/tracker-extract/tracker-controller.c
@@ -46,7 +46,6 @@ struct TrackerControllerPrivate {
 	GMainContext *context;
 	GMainLoop *main_loop;
 
-	TrackerStorage *storage;
 	TrackerExtract *extractor;
 
 	GDBusConnection *connection;
@@ -167,8 +166,6 @@ tracker_controller_finalize (GObject *object)
 		g_object_unref (priv->extractor);
 	}
 
-	g_object_unref (priv->storage);
-
 	g_main_loop_unref (priv->main_loop);
 	g_main_context_unref (priv->context);
 
@@ -407,8 +404,7 @@ tracker_controller_init (TrackerController *controller)
 	priv->context = g_main_context_new ();
 	priv->main_loop = g_main_loop_new (priv->context, FALSE);
 
-	priv->storage = tracker_storage_new ();
-	g_signal_connect (priv->storage, "mount-point-removed",
+	g_signal_connect (tracker_storage_get (), "mount-point-removed",
 	                  G_CALLBACK (mount_point_removed_cb), controller);
 
 #if GLIB_CHECK_VERSION (2,31,0)
diff --git a/src/tracker-extract/tracker-media-art.c b/src/tracker-extract/tracker-media-art.c
index 7aa5f44..275c6a4 100644
--- a/src/tracker-extract/tracker-media-art.c
+++ b/src/tracker-extract/tracker-media-art.c
@@ -56,7 +56,6 @@ static const gchar *media_art_type_name[TRACKER_MEDIA_ART_TYPE_COUNT] = {
 };
 
 typedef struct {
-	TrackerStorage *storage;
 	gchar *art_path;
 	gchar *local_uri;
 } GetFileInfo;
@@ -77,7 +76,6 @@ typedef enum {
 
 static gboolean initialized = FALSE;
 static gboolean disable_requests;
-static TrackerStorage *media_art_storage;
 static GHashTable *media_art_cache;
 static GDBusConnection *connection;
 
@@ -777,8 +775,7 @@ media_art_set (const unsigned char *buffer,
 }
 
 static void
-albumart_request_download (TrackerStorage      *storage,
-                           TrackerMediaArtType  type,
+albumart_request_download (TrackerMediaArtType  type,
                            const gchar         *album,
                            const gchar         *artist,
                            const gchar         *local_uri,
@@ -797,8 +794,6 @@ albumart_request_download (TrackerStorage      *storage,
 
 		info = g_slice_new (GetFileInfo);
 
-		info->storage = storage ? g_object_ref (storage) : NULL;
-
 		info->local_uri = g_strdup (local_uri);
 		info->art_path = g_strdup (art_path);
 
@@ -822,24 +817,16 @@ albumart_request_download (TrackerStorage      *storage,
 }
 
 static void
-media_art_copy_to_local (TrackerStorage *storage,
-                         const gchar    *filename,
+media_art_copy_to_local (const gchar    *filename,
                          const gchar    *local_uri)
 {
 	GSList *roots, *l;
 	gboolean on_removable_device = FALSE;
 	guint flen;
 
-	/* Determining if we are on a removable device */
-	if (!storage) {
-		/* This is usually because we are running on the
-		 * command line, so we don't error here with
-		 * g_return_if_fail().
-		 */
-		return;
-	}
-
-	roots = tracker_storage_get_device_roots (storage, TRACKER_STORAGE_REMOVABLE, FALSE);
+	roots = tracker_storage_get_device_roots (tracker_storage_get (),
+	                                          TRACKER_STORAGE_REMOVABLE,
+	                                          FALSE);
 	flen = strlen (filename);
 
 	for (l = roots; l; l = l->next) {
@@ -917,21 +904,14 @@ albumart_queue_cb (GObject      *source_object,
 		g_variant_unref (v);
 	}
 
-	if (info->storage && info->art_path &&
-	    g_file_test (info->art_path, G_FILE_TEST_EXISTS)) {
+	if (info->art_path && g_file_test (info->art_path, G_FILE_TEST_EXISTS)) {
 
-		media_art_copy_to_local (info->storage,
-		                         info->art_path,
-		                         info->local_uri);
+		media_art_copy_to_local (info->art_path, info->local_uri);
 	}
 
 	g_free (info->art_path);
 	g_free (info->local_uri);
 
-	if (info->storage) {
-		g_object_unref (info->storage);
-	}
-
 	g_slice_free (GetFileInfo, info);
 }
 
@@ -944,8 +924,6 @@ tracker_media_art_init (void)
 
 	tracker_media_art_plugin_init ();
 
-	media_art_storage = tracker_storage_new ();
-
 	/* Cache to know if we have already handled uris */
 	media_art_cache = g_hash_table_new_full (g_str_hash,
 	                                         g_str_equal,
@@ -980,10 +958,6 @@ tracker_media_art_shutdown (void)
 		g_hash_table_unref (media_art_cache);
 	}
 
-	if (media_art_storage) {
-		g_object_unref (media_art_storage);
-	}
-
 	tracker_media_art_plugin_shutdown ();
 
 	initialized = FALSE;
@@ -1087,8 +1061,7 @@ tracker_media_art_process (const unsigned char *buffer,
 				 * media-art to the media-art
 				 * downloaders
 				 */
-				albumart_request_download (media_art_storage,
-				                           type,
+				albumart_request_download (type,
 				                           artist,
 				                           title,
 				                           local_art_uri,
@@ -1116,9 +1089,7 @@ tracker_media_art_process (const unsigned char *buffer,
 		 * situation might have changed
 		 */
 		if (g_file_test (art_path, G_FILE_TEST_EXISTS)) {
-			media_art_copy_to_local (media_art_storage,
-			                         art_path,
-			                         local_art_uri);
+			media_art_copy_to_local (art_path, local_art_uri);
 		}
 	}
 
diff --git a/src/tracker-writeback/tracker-writeback.c b/src/tracker-writeback/tracker-writeback.c
index c527197..a4046f1 100644
--- a/src/tracker-writeback/tracker-writeback.c
+++ b/src/tracker-writeback/tracker-writeback.c
@@ -51,8 +51,6 @@ typedef struct {
 	GMainContext *context;
 	GMainLoop *main_loop;
 
-	TrackerStorage *storage;
-
 	GDBusConnection *d_connection;
 	GDBusNodeInfo *introspection_data;
 	guint registration_id;
@@ -146,7 +144,6 @@ tracker_controller_finalize (GObject *object)
 
 	tracker_controller_dbus_stop (controller);
 
-	g_object_unref (priv->storage);
 	g_hash_table_unref (priv->modules);
 
 	g_main_loop_unref (priv->main_loop);
@@ -405,8 +402,7 @@ tracker_controller_init (TrackerController *controller)
 	priv->context = g_main_context_new ();
 	priv->main_loop = g_main_loop_new (priv->context, FALSE);
 
-	priv->storage = tracker_storage_new ();
-	g_signal_connect (priv->storage, "mount-point-removed",
+	g_signal_connect (tracker_storage_get (), "mount-point-removed",
 	                  G_CALLBACK (mount_point_removed_cb), controller);
 
 #if GLIB_CHECK_VERSION (2,31,0)



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