tracker-0.16.2/src/miners/fs/tracker-writeback.c

No issues found

  1 /*
  2  * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) any later version.
  8  *
  9  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public
 15  * License along with this library; if not, write to the
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  */
 19 
 20 #include "config.h"
 21 
 22 #include <gio/gio.h>
 23 
 24 #include "tracker-writeback.h"
 25 #include "tracker-writeback-listener.h"
 26 #include "tracker-writeback-dispatcher.h"
 27 
 28 /* Listener listens for the Writeback signal coming from the store, it performs
 29  * a few queries to get a set of changed values, and pushes the writeback task
 30  * to miner-fs's queues. */
 31 static TrackerWritebackListener *listener = NULL;
 32 
 33 /* That task in miner-fs's queue callsback to the dispatcher. The dispatcher
 34  * calls the external tracker-writeback process which does the actual write */
 35 static TrackerWritebackDispatcher *dispatcher = NULL;
 36 
 37 static gboolean enabled = FALSE;
 38 static TrackerMinerFiles *gminer_files = NULL;
 39 static TrackerConfig *gconfig = NULL;
 40 static guint gsig = 0;
 41 
 42 static void
 43 initialize_all (TrackerMinerFiles  *miner_files,
 44                 GError            **error)
 45 {
 46 	GError *internal_error = NULL;
 47 
 48 	listener = tracker_writeback_listener_new (miner_files, &internal_error);
 49 
 50 	if (!internal_error) {
 51 		dispatcher = tracker_writeback_dispatcher_new (miner_files, &internal_error);
 52 	}
 53 
 54 	if (internal_error) {
 55 		if (listener) {
 56 			g_object_unref (listener);
 57 			listener = NULL;
 58 		}
 59 		g_propagate_error (error, internal_error);
 60 	}
 61 	enabled = TRUE;
 62 }
 63 
 64 static void
 65 shutdown_writeback (void)
 66 {
 67 	if (listener) {
 68 		g_object_unref (listener);
 69 		listener = NULL;
 70 	}
 71 
 72 	if (dispatcher) {
 73 		g_object_unref (dispatcher);
 74 		dispatcher = NULL;
 75 	}
 76 	enabled = FALSE;
 77 }
 78 
 79 static void
 80 enable_writeback_cb (GObject    *object,
 81                      GParamSpec *pspec,
 82                      gpointer    user_data)
 83 {
 84 	if (enabled && !tracker_config_get_enable_monitors (gconfig)) {
 85 		shutdown_writeback ();
 86 	}
 87 
 88 	if (!enabled && tracker_config_get_enable_monitors (gconfig)) {
 89 		GError *error = NULL;
 90 
 91 		initialize_all (gminer_files, &error);
 92 
 93 		if (error) {
 94 			g_critical ("Can't reenable Writeback: '%s'", error->message);
 95 			g_error_free (error);
 96 		}
 97 	}
 98 }
 99 
100 void
101 tracker_writeback_init (TrackerMinerFiles  *miner_files,
102                         TrackerConfig      *config,
103                         GError            **error)
104 {
105 	GError *internal_error = NULL;
106 
107 	if (tracker_config_get_enable_monitors (config)) {
108 		initialize_all (miner_files, &internal_error);
109 	}
110 
111 	if (internal_error) {
112 		g_propagate_error (error, internal_error);
113 	} else {
114 		gminer_files = g_object_ref (miner_files);
115 		gconfig = g_object_ref (config);
116 		gsig = g_signal_connect (gconfig, "notify::enable-writeback",
117 		                         G_CALLBACK (enable_writeback_cb),
118 		                         NULL);
119 	}
120 }
121 
122 void
123 tracker_writeback_shutdown (void)
124 {
125 	shutdown_writeback ();
126 
127 	if (gconfig) {
128 		if (gsig) {
129 			g_signal_handler_disconnect (gconfig, gsig);
130 		}
131 		g_object_unref (gconfig);
132 	}
133 
134 	if (gminer_files) {
135 		g_object_unref (gminer_files);
136 	}
137 }