[tracker/rss-enclosures: 5/9] tracker-miner-rss: saving mfo:localLink for fetched enclosures



commit fa38dc1c41b8f40b855b02fc6eb8ee77548f6694
Author: Roberto Guido <bob4mail gmail com>
Date:   Mon May 3 02:17:31 2010 +0200

    tracker-miner-rss: saving mfo:localLink for fetched enclosures
    
    Signed-off-by: Michele Tameni <michele tameni it>

 src/miners/rss/tracker-main.c        |    2 +
 src/miners/rss/tracker-miner-rss.c   |   25 +++--
 src/miners/rss/wrap-feed-channel.c   |   17 +++-
 src/miners/rss/wrap-feed-channel.h   |    3 +-
 src/miners/rss/wrap-feed-enclosure.c |  167 ++++++++++++++++++++++++++++-----
 src/miners/rss/wrap-feed-enclosure.h |    4 +-
 6 files changed, 177 insertions(+), 41 deletions(-)
---
diff --git a/src/miners/rss/tracker-main.c b/src/miners/rss/tracker-main.c
index 8411aba..98f2c51 100644
--- a/src/miners/rss/tracker-main.c
+++ b/src/miners/rss/tracker-main.c
@@ -50,6 +50,7 @@ main (int argc, char **argv)
 
 	g_type_init ();
 	g_thread_init (NULL);
+	dbus_g_thread_init ();
 
 	setlocale (LC_ALL, "");
 
@@ -74,6 +75,7 @@ main (int argc, char **argv)
 	tracker_miner_start (TRACKER_MINER (miner));
 
 	loop = g_main_loop_new (NULL, FALSE);
+	g_print ("Running.\n");
 	g_main_loop_run (loop);
 
 	tracker_log_shutdown ();
diff --git a/src/miners/rss/tracker-miner-rss.c b/src/miners/rss/tracker-miner-rss.c
index 431ddd6..a309aad 100644
--- a/src/miners/rss/tracker-miner-rss.c
+++ b/src/miners/rss/tracker-miner-rss.c
@@ -130,7 +130,7 @@ tracker_miner_rss_init (TrackerMinerRSS *object)
 	GError *error = NULL;
 	TrackerMinerRSSPrivate *priv;
 
-	g_message ("Initializing...");
+	g_print ("Initializing...\n");
 
 	connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 
@@ -151,7 +151,7 @@ tracker_miner_rss_init (TrackerMinerRSS *object)
 	/*                                   "org.freedesktop.Tracker1.Resources.Class"); */
 
 	if (!proxy) {
-		g_message ("Could not create DBusGProxy for interface:'%s'",
+		g_message ("Could not create DBusGProxy for interface: '%s'",
 		           TRACKER_DBUS_INTERFACE_FEED);
 		return;
 	}
@@ -241,12 +241,15 @@ enclosure_downloaded_cb (SoupSession *session,
 	enclosure = user_data;
 	g_object_get (msg, "status-code", &status, NULL);
 
-	if (status < 200 || status > 299)
+	if (status < 200 || status > 299) {
 		g_warning ("Unable to download enclosure in %s.",
-                   feed_enclosure_get_url (FEED_ENCLOSURE (enclosure)));
-	else
-		wrap_feed_enclosure_save_data (enclosure, msg->response_body->data,
-                                       msg->response_body->length);
+			   feed_enclosure_get_url (FEED_ENCLOSURE (enclosure)));
+	}
+	else {
+		wrap_feed_enclosure_save_data (enclosure,
+					       g_memdup (msg->response_body->data, msg->response_body->length),
+					       msg->response_body->length);
+	}
 
 	g_object_unref (enclosure);
 }
@@ -280,9 +283,11 @@ manage_enclosure (TrackerMinerRSS *miner,
 	enc_wrap = wrap_feed_enclosure_new (enclosure, feed);
 	msg = soup_message_new ("GET", feed_enclosure_get_url (enclosure));
 
-	priv = GET_PRIV (miner);
+	g_message ("Downloading enclosures in %s", feed_enclosure_get_url (enclosure));
+
+	priv = TRACKER_MINER_RSS_GET_PRIVATE (miner);
 	soup_session_queue_message (feeds_pool_get_session (priv->pool), msg,
-                                enclosure_downloaded_cb, enc_wrap);
+				    enclosure_downloaded_cb, enc_wrap);
 }
 
 static int
@@ -672,7 +677,7 @@ retrieve_and_schedule_feeds (TrackerMinerRSS *miner)
 	         "{ ?chanUrn a mfo:FeedChannel . "
 	         "?chanUrn mfo:feedSettings ?settings . "
 	         "?chanUrn nie:url ?chanUrl . "
-	         "?settings mfo:updateInterval ?interval . "
+	         "OPTIONAL { ?settings mfo:updateInterval ?interval } . "
 	         "OPTIONAL { ?settings mfo:expiryInterval ?expiry } . "
 	         "OPTIONAL { ?settings mfo:downloadFlag ?download } . "
 	         "OPTIONAL { ?settings mfo:downloadPath ?path } . "
diff --git a/src/miners/rss/wrap-feed-channel.c b/src/miners/rss/wrap-feed-channel.c
index bb16b66..2bc0d8f 100644
--- a/src/miners/rss/wrap-feed-channel.c
+++ b/src/miners/rss/wrap-feed-channel.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Roberto Guido <madbob users barberaware org>
+ * Copyright (C) 2010, Roberto Guido <madbob users barberaware org>
  *                     Michele Tameni <michele amdplanet it>
  *
  * This library is free software; you can redistribute it and/or
@@ -76,8 +76,12 @@ review_expiration_timer (WrapFeedChannel *node)
 	if (priv->expiration_handler != 0)
 		g_source_remove (priv->expiration_handler);
 
+	if (priv->items_expiry_interval == 0)
+		return;
+
 	check_expired_items_cb (node);
-	priv->expiration_handler = g_timeout_add_seconds (priv->items_expiry_interval * 60, check_expired_items_cb, node);
+	priv->expiration_handler = g_timeout_add_seconds (priv->items_expiry_interval * 60,
+							  check_expired_items_cb, node);
 }
 
 static void
@@ -127,6 +131,15 @@ wrap_feed_channel_new (TrackerMinerRSS *miner,
 	return ret;
 }
 
+TrackerMinerRSS*
+wrap_feed_channel_get_referring_miner (WrapFeedChannel *feed)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (feed);
+	return priv->miner;
+}
+
 const gchar*
 wrap_feed_channel_get_subject (WrapFeedChannel *feed)
 {
diff --git a/src/miners/rss/wrap-feed-channel.h b/src/miners/rss/wrap-feed-channel.h
index f86ea4d..34b291e 100644
--- a/src/miners/rss/wrap-feed-channel.h
+++ b/src/miners/rss/wrap-feed-channel.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Roberto Guido <madbob users barberaware org>
+ * Copyright (C) 2010, Roberto Guido <madbob users barberaware org>
  *                     Michele Tameni <michele amdplanet it>
  *
  * This library is free software; you can redistribute it and/or
@@ -45,6 +45,7 @@ GType            wrap_feed_channel_get_type                   (void) G_GNUC_CONS
 
 WrapFeedChannel* wrap_feed_channel_new                        (TrackerMinerRSS *miner, gchar *subject);
 
+TrackerMinerRSS* wrap_feed_channel_get_referring_miner        (WrapFeedChannel *feed);
 const gchar*     wrap_feed_channel_get_subject                (WrapFeedChannel *feed);
 void             wrap_feed_channel_set_feeds_expiry           (WrapFeedChannel *feed, int minutes);
 void             wrap_feed_channel_set_download_enclosures    (WrapFeedChannel *feed, gboolean download);
diff --git a/src/miners/rss/wrap-feed-enclosure.c b/src/miners/rss/wrap-feed-enclosure.c
index e4739db..09318bc 100644
--- a/src/miners/rss/wrap-feed-enclosure.c
+++ b/src/miners/rss/wrap-feed-enclosure.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Roberto Guido <madbob users barberaware org>
+ * Copyright (C) 2010, Roberto Guido <madbob users barberaware org>
  *                     Michele Tameni <michele amdplanet it>
  *
  * This library is free software; you can redistribute it and/or
@@ -19,6 +19,10 @@
  */
 
 #include <unistd.h>
+#include <errno.h>
+
+#include <dbus/dbus-glib.h>
+
 #include "wrap-feed-enclosure.h"
 
 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), WRAP_FEED_ENCLOSURE_TYPE, WrapFeedEnclosurePrivate))
@@ -28,6 +32,11 @@ typedef struct _WrapFeedEnclosurePrivate	WrapFeedEnclosurePrivate;
 struct _WrapFeedEnclosurePrivate {
 	FeedEnclosure *enclosure;
 	WrapFeedChannel *channel;
+
+	gchar *save_path;
+
+	gchar *data;
+	gsize data_len;
 };
 
 G_DEFINE_TYPE (WrapFeedEnclosure, wrap_feed_enclosure, G_TYPE_OBJECT);
@@ -43,6 +52,11 @@ wrap_feed_enclosure_finalize (GObject *obj)
 
 	g_object_unref (priv->enclosure);
 	g_object_unref (priv->channel);
+
+	if (priv->save_path != NULL)
+		g_free (priv->save_path);
+	if (priv->data != NULL)
+		g_free (priv->data);
 }
 
 static void
@@ -79,47 +93,148 @@ wrap_feed_enclosure_new (FeedEnclosure   *enclosure,
 	return ret;
 }
 
-void
-wrap_feed_enclosure_save_data (WrapFeedEnclosure *enclosure,
-                               const gchar       *data,
-                               gsize              len)
+static const gchar*
+saving_path (WrapFeedEnclosure *enclosure)
 {
 	int modifier;
-	gchar *path;
 	gchar *name;
 	gchar *new_name;
+	gchar *path;
 	const gchar *folder;
-	FILE *fd;
 	WrapFeedEnclosurePrivate *priv;
 
 	priv = GET_PRIV (enclosure);
-	folder = wrap_feed_channel_get_enclosures_saving_path (priv->channel);
-	name = g_path_get_basename (feed_enclosure_get_url (priv->enclosure));
-	path = g_build_filename (folder, name, NULL);
 
-	modifier = 0;
+	if (priv->save_path == NULL) {
+		folder = wrap_feed_channel_get_enclosures_saving_path (priv->channel);
+		name = g_path_get_basename (feed_enclosure_get_url (priv->enclosure));
+		path = g_build_filename (folder, name, NULL);
 
-	/* This is to avoid overlapping existing files with the same name */
-	while (access (path, F_OK) == 0) {
-		modifier++;
-		new_name = g_strdup_printf ("%d_%s", modifier, name);
+		/* This is to avoid overlapping existing files with the same name */
 
-		g_free (path);
-		g_free (name);
+		modifier = 0;
 
-		path = g_build_filename (folder, new_name, NULL);
-		name = new_name;
+		while (access (path, F_OK) == 0) {
+			modifier++;
+			new_name = g_strdup_printf ("%d_%s", modifier, name);
+
+			g_free (path);
+			g_free (name);
+
+			path = g_build_filename (folder, new_name, NULL);
+			name = new_name;
+		}
+
+		g_free (name);
+		priv->save_path = path;
 	}
 
-	fd = fopen (path, "w+");
-	if (fd == NULL) {
-		g_warning ("Unable to open saving location for enclosure.");
+	return (const gchar*) priv->save_path;
+}
+
+static gchar*
+get_node_query (WrapFeedEnclosure *enclosure)
+{
+	gchar *query;
+	WrapFeedEnclosurePrivate *priv;
+
+	priv = GET_PRIV (enclosure);
+
+	query = g_strdup_printf ("INSERT {_:enclosure a nfo:FileDataObject; nie:url \"%s\" . ?i mfo:localLink _:enclosure} "
+				 "WHERE {?r nie:url \"%s\" . ?i mfo:remoteLink ?r}",
+				 saving_path (enclosure),
+				 feed_enclosure_get_url (priv->enclosure));
+
+	return query;
+}
+
+static gboolean
+notify_miner_fs (const gchar *path)
+{
+	gboolean ret;
+	const gchar **params;
+	GError *error;
+	DBusGConnection *connection;
+	DBusGProxy *proxy;
+
+	connection = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
+	proxy = dbus_g_proxy_new_for_name (connection,
+	                                   "org.freedesktop.Tracker1.Miner.Files",
+	                                   "/org/freedesktop/Tracker1/Miner/Files",
+	                                   "org.freedesktop.Tracker1.Miner");
+
+	params = g_new0 (const gchar*, 2);
+	params [0] = path;
+
+	error = NULL;
+	ret = dbus_g_proxy_call (proxy, "IgnoreNextUpdate", &error,
+				 G_TYPE_STRV, params, G_TYPE_INVALID, G_TYPE_INVALID);
+
+	if (ret == FALSE)
+		g_warning ("Unable to notify miner-fs about incoming enclosure saving: %s.", error->message);
+
+	g_free (params);
+	dbus_g_connection_unref (connection);
+	return ret;
+}
+
+static void
+enclosure_node_set (GObject      *source,
+                    GAsyncResult *result,
+                    gpointer      user_data)
+{
+	const gchar *path;
+	FILE *fd;
+	GError *error;
+	WrapFeedEnclosurePrivate *priv;
+	WrapFeedEnclosure *enclosure;
+
+	error = NULL;
+
+	tracker_miner_execute_update_finish (TRACKER_MINER (source), result, &error);
+	if (error != NULL) {
+		g_critical ("Could not save enclosure informations, %s", error->message);
+		g_error_free (error);
 	}
 	else {
-		fwrite (data, len, 1, fd);
-		fclose (fd);
+		enclosure = user_data;
+
+		priv = GET_PRIV (enclosure);
+		path = saving_path (enclosure);
+
+		if (notify_miner_fs (path) == FALSE)
+			return;
+
+		fd = fopen (path, "w+");
+		if (fd == NULL) {
+			g_warning ("Unable to open saving location for enclosure.");
+		}
+		else {
+			if (fwrite (priv->data, priv->data_len, 1, fd) != 1)
+				g_warning ("Error while writing enclosure contents on the filesystem: %s.", strerror (errno));
+			fclose (fd);
+		}
 	}
 
-	g_free (path);
-	g_free (name);
+	g_object_unref (enclosure);
+}
+
+void
+wrap_feed_enclosure_save_data (WrapFeedEnclosure *enclosure,
+                               gchar             *data,
+                               gsize              len)
+{
+	gchar *query;
+	WrapFeedEnclosurePrivate *priv;
+
+	priv = GET_PRIV (enclosure);
+	priv->data = data;
+	priv->data_len = len;
+
+	g_object_ref (enclosure);
+
+	query = get_node_query (enclosure);
+	tracker_miner_execute_update (TRACKER_MINER (wrap_feed_channel_get_referring_miner (priv->channel)),
+				      query, NULL, enclosure_node_set, enclosure);
+	g_free (query);
 }
diff --git a/src/miners/rss/wrap-feed-enclosure.h b/src/miners/rss/wrap-feed-enclosure.h
index 6240074..c37cf72 100644
--- a/src/miners/rss/wrap-feed-enclosure.h
+++ b/src/miners/rss/wrap-feed-enclosure.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Roberto Guido <madbob users barberaware org>
+ * Copyright (C) 2010, Roberto Guido <madbob users barberaware org>
  *                     Michele Tameni <michele amdplanet it>
  *
  * This library is free software; you can redistribute it and/or
@@ -44,6 +44,6 @@ typedef struct {
 GType              wrap_feed_enclosure_get_type           (void) G_GNUC_CONST;
 
 WrapFeedEnclosure* wrap_feed_enclosure_new                (FeedEnclosure *enclosure, WrapFeedChannel *channel);
-void               wrap_feed_enclosure_save_data          (WrapFeedEnclosure *enclosure, const gchar *data, gsize len);
+void               wrap_feed_enclosure_save_data          (WrapFeedEnclosure *enclosure, gchar *data, gsize len);
 
 #endif /* __WRAP_FEED_ENCLOSURE_H__ */



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