[tracker/miner-flickr-network-aware: 3/5] Flickr miner: Only download albums when specified



commit 47013b5ad94969665392384946f2e3b042e26bc5
Author: Adrien Bustany <abustany gnome org>
Date:   Mon May 31 10:57:31 2010 -0400

    Flickr miner: Only download albums when specified
    
    This commit takes advantage of the new NetworkProvider class available
    in libtracker-miner to allow the user to specify when he wants his
    albums to be indexed. This can be used for example to avoid downloading
    a large photoset over a slow/expensive connection.

 src/miners/flickr/Makefile.am               |    4 +-
 src/miners/flickr/tracker-config.vala       |   94 +++++++++++++++++++++++++++
 src/miners/flickr/tracker-miner-flickr.vala |   24 +++++++
 3 files changed, 121 insertions(+), 1 deletions(-)
---
diff --git a/src/miners/flickr/Makefile.am b/src/miners/flickr/Makefile.am
index b9b136c..f015387 100644
--- a/src/miners/flickr/Makefile.am
+++ b/src/miners/flickr/Makefile.am
@@ -28,7 +28,9 @@ libexec_PROGRAMS = tracker-miner-flickr
 
 tracker_miner_flickr_VALASOURCES =					\
 	$(top_srcdir)/src/libtracker-client/tracker-client.vapi		\
-	tracker-miner-flickr.vala
+	$(top_srcdir)/src/libtracker-common/libtracker-common.vapi	\
+	tracker-miner-flickr.vala					\
+	tracker-config.vala
 
 tracker_miner_flickr_SOURCES =						\
 	$(tracker_miner_flickr_VALASOURCES:.vala=.c)
diff --git a/src/miners/flickr/tracker-config.vala b/src/miners/flickr/tracker-config.vala
new file mode 100644
index 0000000..1623561
--- /dev/null
+++ b/src/miners/flickr/tracker-config.vala
@@ -0,0 +1,94 @@
+public class Tracker.Config : Tracker.ConfigFile {
+	public enum DownloadBehaviour {
+		GPRS, /* Download if connected via GPRS, EDGE, 3G or LAN */
+		EDGE, /* Download if connected via EDGE, 3G or LAN */
+		@3G,  /* Download if connected via 3G or LAN */
+		LAN   /* Download only if connected to a LAN */
+	}
+
+	/* GLib.KeyFile defines */
+	public static const string GROUP_GENERAL = "General";
+	public static const string GROUP_NETWORK = "Network";
+
+	private struct ConfigKey {
+		GLib.Type type;
+		string property;
+		string group;
+		string key;
+	}
+
+	const int TYPE_INT = typeof (int);
+
+	private const ConfigKey config_keys[] = {
+		{ typeof (int), "download-behaviour", GROUP_NETWORK, "DownloadBehaviour" }
+	};
+
+	/* Default values */
+	public static const uint DEFAULT_DOWNLOAD_BEHAVIOUR = (uint)DownloadBehaviour  3G;
+
+	private uint _download_behaviour = DEFAULT_DOWNLOAD_BEHAVIOUR;
+	public uint download_behaviour {
+		get {
+			return _download_behaviour;
+		}
+		set {
+			if (value < (uint)DownloadBehaviour.LAN || value > (uint)DownloadBehaviour.GPRS) {
+				return;
+			}
+			_download_behaviour = value;
+		}
+	}
+
+	construct {
+		domain = "tracker-miner-flickr";
+		base.constructed ();
+
+		load (true);
+	}
+
+	public override void changed () {
+		load (false);
+	}
+
+	public void load (bool use_defaults) {
+		if (use_defaults) {
+			create_with_defaults ();
+		}
+
+		if (!file_exists) {
+			base.save ();
+		}
+
+		foreach (ConfigKey key in config_keys) {
+			if (key.type == typeof (int)) {
+				KeyfileObject.load_int (this, key.property, key_file, key.group, key.key);
+			} else {
+				assert_not_reached ();
+			}
+		}
+	}
+
+	public new bool save () {
+		if (key_file == null) {
+			critical ("Could not save config, GKeyFile was NULL, has the config been loaded?");
+			return false;
+		}
+
+		KeyfileObject.save_int (this, "download-behaviour", key_file, GROUP_NETWORK, "DownloadBehaviour");
+
+		return base.save ();
+	}
+
+	private void create_with_defaults () {
+		try {
+			key_file.has_key (GROUP_NETWORK, "DownloadBehaviour");
+		} catch (Error e) {
+			if (!(e is KeyFileError.KEY_NOT_FOUND) && !(e is KeyFileError.GROUP_NOT_FOUND)) {
+				critical ("Could not load config default: %s", e.message);
+			} else {
+				key_file.set_integer (GROUP_NETWORK, "DownloadBehaviour",
+				                      DownloadBehaviour  3G);
+			}
+		}
+	}
+}
diff --git a/src/miners/flickr/tracker-miner-flickr.vala b/src/miners/flickr/tracker-miner-flickr.vala
index 2d1ef7e..1f234d9 100644
--- a/src/miners/flickr/tracker-miner-flickr.vala
+++ b/src/miners/flickr/tracker-miner-flickr.vala
@@ -79,12 +79,16 @@ public class MinerFlickr : Tracker.MinerWeb {
 	/* Used to form some urls */
 	private string user_id;
 
+	private Config config;
+
 	construct {
 		name = MINER_NAME;
 		associated = false;
 		status = "Not authenticated";
 		progress = 0.0;
 
+		config = new Config ();
+
 		rest = new Rest.Proxy (FLICKR_REST_URL, false);
 
 		tracker_client = new Tracker.Client (0, -1);
@@ -125,6 +129,26 @@ public class MinerFlickr : Tracker.MinerWeb {
 	}
 
 	private bool pull_timeout_cb () {
+		NetworkProviderStatus status = NetworkProvider.get ().get_status ();
+		if (status == NetworkProviderStatus.DISCONNECTED) {
+			return true;
+		}
+
+		if (status == NetworkProviderStatus.GPRS &&
+		    (uint)config.download_behaviour > (uint)Config.DownloadBehaviour.GPRS) {
+			return true;
+		}
+
+		if (status == NetworkProviderStatus.EDGE &&
+		    (uint)config.download_behaviour > (uint)Config.DownloadBehaviour.EDGE) {
+			return true;
+		}
+
+		if (status == NetworkProviderStatus  3G &&
+		    (uint)config.download_behaviour > (uint)Config DownloadBehaviour  3G) {
+			return true;
+		}
+
 		init_pull ();
 		return true;
 	}



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