[tracker/rss-enclosures] tracker-miner-rss: initial support for enclosures downloads tracker-miner-rss: support for items' au



commit 4939f4655d2009a90986fd58a291b750da3edcbc
Author: Roberto Guido <bob4mail gmail com>
Date:   Mon Mar 29 21:58:30 2010 +0200

    tracker-miner-rss: initial support for enclosures downloads
    tracker-miner-rss: support for items' auto-expiration

 src/miners/rss/Makefile.am           |    6 +-
 src/miners/rss/tracker-miner-rss.c   |  166 ++++++++++++++++++++++++---
 src/miners/rss/wrap-feed-channel.c   |  211 ++++++++++++++++++++++++++++++++++
 src/miners/rss/wrap-feed-channel.h   |   57 +++++++++
 src/miners/rss/wrap-feed-enclosure.c |  127 ++++++++++++++++++++
 src/miners/rss/wrap-feed-enclosure.h |   49 ++++++++
 6 files changed, 597 insertions(+), 19 deletions(-)
---
diff --git a/src/miners/rss/Makefile.am b/src/miners/rss/Makefile.am
index ad8cc8a..e87e50a 100644
--- a/src/miners/rss/Makefile.am
+++ b/src/miners/rss/Makefile.am
@@ -19,7 +19,11 @@ libexec_PROGRAMS = tracker-miner-rss
 tracker_miner_rss_SOURCES =						\
 	tracker-main.c							\
 	tracker-miner-rss.h						\
-	tracker-miner-rss.c
+	tracker-miner-rss.c						\
+	wrap-feed-channel.h						\
+	wrap-feed-channel.c						\
+	wrap-feed-enclosure.h					\
+	wrap-feed-enclosure.c
 
 tracker_miner_rss_LDADD =						\
 	$(top_builddir)/src/libtracker-miner/libtracker-miner- TRACKER_API_VERSION@.la	\
diff --git a/src/miners/rss/tracker-miner-rss.c b/src/miners/rss/tracker-miner-rss.c
index 914e086..9736ca9 100644
--- a/src/miners/rss/tracker-miner-rss.c
+++ b/src/miners/rss/tracker-miner-rss.c
@@ -25,6 +25,8 @@
 #include <glib/gi18n.h>
 
 #include "tracker-miner-rss.h"
+#include "wrap-feed-channel.h"
+#include "wrap-feed-enclosure.h"
 
 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TRACKER_TYPE_MINER_RSS, TrackerMinerRSSPrivate))
 
@@ -159,7 +161,7 @@ verify_channel_update (GObject      *source,
 
 static void
 update_updated_interval (TrackerMinerRSS *miner,
-                         gchar           *uri,
+                         const gchar     *uri,
                          time_t          *now)
 {
 	TrackerSparqlBuilder *sparql;
@@ -195,6 +197,110 @@ update_updated_interval (TrackerMinerRSS *miner,
 }
 
 static void
+enclosure_downloaded_cb (SoupSession *session,
+                         SoupMessage *msg,
+                         gpointer     user_data)
+{
+	int status;
+	WrapFeedEnclosure *enclosure;
+
+	enclosure = user_data;
+	g_object_get (msg, "status-code", &status, NULL);
+
+	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);
+
+	g_object_unref (enclosure);
+}
+
+static void
+manage_enclosure (TrackerMinerRSS *miner,
+                  WrapFeedChannel *feed,
+                  FeedEnclosure   *enclosure)
+{
+	int size;
+	const gchar *path;
+	SoupMessage *msg;
+	TrackerMinerRSSPrivate *priv;
+	WrapFeedEnclosure *enc_wrap;
+
+	if (wrap_feed_channel_get_download_enclosures (feed) == FALSE) {
+		return;
+	}
+
+	size = wrap_feed_channel_get_enclosures_maxsize (feed);
+	if (size > 0 && (feed_enclosure_get_length (enclosure) * 1024) > size)
+		return;
+
+	path = wrap_feed_channel_get_enclosures_saving_path (feed);
+	if (path == NULL) {
+		/* TODO Provide a fallback? */
+		g_warning ("No saving folder set for enclosures.");
+		return;
+	}
+
+	enc_wrap = wrap_feed_enclosure_new (enclosure, feed);
+	msg = soup_message_new ("GET", feed_enclosure_get_url (enclosure));
+
+	priv = GET_PRIV (miner);
+	soup_session_queue_message (feeds_pool_get_session (priv->pool), msg, enclosure_downloaded_cb, enc_wrap);
+}
+
+static int
+queue_enclosures (TrackerMinerRSS      *miner,
+                  WrapFeedChannel      *channel,
+                  FeedItem             *item,
+                  TrackerSparqlBuilder *sparql)
+{
+	int num;
+	gchar *subject;
+	gchar *rsubject;
+	const GList *iter;
+	FeedEnclosure *enc;
+
+	for (iter = feed_item_get_enclosures (item), num = 0; iter; iter = iter->next) {
+		enc = iter->data;
+
+		rsubject = g_strdup_printf ("_:enclosurefile%d", num);
+		subject = g_strdup_printf ("_:enclosure%d", num);
+
+		tracker_sparql_builder_insert_open (sparql, rsubject);
+		tracker_sparql_builder_predicate (sparql, "a");
+		tracker_sparql_builder_object (sparql, "nfo:RemoteDataObject");
+
+		tracker_sparql_builder_predicate (sparql, "nfo:fileUrl");
+		tracker_sparql_builder_object_string (sparql, feed_enclosure_get_url (enc));
+
+		tracker_sparql_builder_predicate (sparql, "nfo:fileSize");
+		tracker_sparql_builder_object_int64 (sparql, (gint64) feed_enclosure_get_length (enc));
+
+		tracker_sparql_builder_predicate (sparql, "nfo:encoding");
+		tracker_sparql_builder_object_string (sparql, feed_enclosure_get_format (enc));
+
+		tracker_sparql_builder_insert_close (sparql);
+
+		tracker_sparql_builder_insert_open (sparql, subject);
+		tracker_sparql_builder_predicate (sparql, "a");
+		tracker_sparql_builder_object (sparql, "mfo:Enclosure");
+
+		tracker_sparql_builder_predicate (sparql, "mfo:remoteLink");
+		tracker_sparql_builder_object_iri (sparql, rsubject);
+
+		tracker_sparql_builder_insert_close (sparql);
+
+		g_free (rsubject);
+		g_free (subject);
+
+		manage_enclosure (miner, channel, enc);
+		num++;
+	}
+
+	return num;
+}
+
+static void
 change_status (FeedsPool   *pool,
                FeedChannel *feed,
                gpointer     user_data)
@@ -239,13 +345,16 @@ item_verify_reply_cb (GObject      *source_object,
                       GAsyncResult *res,
                       gpointer      user_data)
 {
+	int i;
+	int enclosures_num;
 	time_t t;
-	gchar *uri;
 	gchar **values;
+	gchar *enclosure_ref;
+	const gchar *uri;
 	const gchar *url;
+	const gchar *tmp_string;
 	gdouble latitude;
 	gdouble longitude;
-	const gchar *tmp_string;
 	const GPtrArray *response;
 	GError *error;
 	TrackerSparqlBuilder *sparql;
@@ -275,6 +384,7 @@ item_verify_reply_cb (GObject      *source_object,
 	url = get_message_url (item);
 
 	sparql = tracker_sparql_builder_new_update ();
+	enclosures_num = queue_enclosures (miner, WRAP_FEED_CHANNEL (feed), item, sparql);
 
 	has_geopoint = feed_item_get_geo_point (item, &latitude, &longitude);
 	tracker_sparql_builder_insert_open (sparql, url);
@@ -340,11 +450,17 @@ item_verify_reply_cb (GObject      *source_object,
 	tracker_sparql_builder_predicate (sparql, "nmo:isRead");
 	tracker_sparql_builder_object_boolean (sparql, FALSE);
 
-	feed = feed_item_get_parent (item);
-	uri = g_object_get_data (G_OBJECT (feed), "subject");
+	uri = wrap_feed_channel_get_subject (WRAP_FEED_CHANNEL (feed));
 	tracker_sparql_builder_predicate (sparql, "nmo:communicationChannel");
 	tracker_sparql_builder_object_iri (sparql, uri);
 
+	for (i = 0; i < enclosures_num; i++) {
+		tracker_sparql_builder_predicate (sparql, "mfo:enclosureList");
+		enclosure_ref = g_strdup_printf ("_:enclosure%d", i);
+		tracker_sparql_builder_object_iri (sparql, enclosure_ref);
+		g_free (enclosure_ref);
+	}
+
 	tracker_sparql_builder_insert_close (sparql);
 
 	tracker_miner_execute_update (TRACKER_MINER (miner),
@@ -388,7 +504,7 @@ feed_fetched (FeedsPool   *pool,
               GList       *items,
               gpointer     user_data)
 {
-	gchar *uri;
+	const gchar *uri;
 	time_t now;
 	GList *iter;
 	FeedItem *item;
@@ -408,7 +524,7 @@ feed_fetched (FeedsPool   *pool,
 		return;
 
 	now = time (NULL);
-	uri = g_object_get_data (G_OBJECT (feed), "subject");
+	uri = wrap_feed_channel_get_subject (WRAP_FEED_CHANNEL (feed));
 	update_updated_interval (miner, uri, &now);
 
 	for (iter = items; iter; iter = iter->next) {
@@ -424,12 +540,13 @@ feeds_retrieve_cb (GObject      *source_object,
 {
 	gint interval;
 	guint i;
+	int int_value;
 	gchar **values;
 	GList *channels;
 	const GPtrArray *response;
 	GError *error;
 	TrackerMinerRSSPrivate *priv;
-	FeedChannel *chan;
+	WrapFeedChannel *chan;
 
 	error = NULL;
 	response = tracker_miner_execute_sparql_finish (TRACKER_MINER (source_object),
@@ -447,12 +564,8 @@ feeds_retrieve_cb (GObject      *source_object,
 	for (i = 0; i < response->len; i++) {
 		values = g_ptr_array_index (response, i);
 
-		chan = feed_channel_new ();
-		g_object_set_data_full (G_OBJECT (chan),
-		                        "subject",
-		                        g_strdup (values [2]),
-		                        g_free);
-		feed_channel_set_source (chan, values [0]);
+		chan = wrap_feed_channel_new (TRACKER_MINER_RSS (source_object), values [0]);
+		feed_channel_set_source (FEED_CHANNEL (chan), values [0]);
 
 		/* TODO How to manage feeds with an update mfo:updateInterval == 0 ?
 		 * Here the interval is forced to be at least 1 minute, but perhaps those
@@ -461,7 +574,22 @@ feeds_retrieve_cb (GObject      *source_object,
 		interval = strtoull (values [1], NULL, 10);
 		if (interval <= 0)
 			interval = 1;
-		feed_channel_set_update_interval (chan, interval);
+		feed_channel_set_update_interval (FEED_CHANNEL (chan), interval);
+
+		if (strcmp (values [3], "") != 0) {
+			int_value = strtoull (values [3], NULL, 10);
+			if (int_value > 0)
+				wrap_feed_channel_set_feeds_expiry (chan, int_value);
+		}
+
+		if (strcmp (values [4], "") != 0)
+			wrap_feed_channel_set_download_enclosures (chan, strcmp (values [4], "1") == 0);
+
+		if (strcmp (values [5], "") != 0) {
+			int_value = strtoull (values [5], NULL, 10);
+			if (int_value > 0)
+				wrap_feed_channel_set_enclosures_maxsize (chan, int_value);
+		}
 
 		channels = g_list_prepend (channels, chan);
 	}
@@ -479,7 +607,10 @@ retrieve_and_schedule_feeds (TrackerMinerRSS *miner)
 	         "{ ?chanUrn a mfo:FeedChannel . "
 	         "?chanUrn mfo:feedSettings ?settings . "
 	         "?chanUrn nie:url ?chanUrl . "
-	         "?settings mfo:updateInterval ?interval }";
+	         "?settings mfo:updateInterval ?interval . "
+	         "OPTIONAL { ?settings mfo:expiryInterval ?expiry } . "
+	         "OPTIONAL { ?settings mfo:downloadFlag ?download } . "
+	         "OPTIONAL { ?settings mfo:maxSize ?msize } }";
 
 	tracker_miner_execute_sparql (TRACKER_MINER (miner),
 	                              sparql,
@@ -489,8 +620,7 @@ retrieve_and_schedule_feeds (TrackerMinerRSS *miner)
 
 }
 
-static
-const gchar *
+static const gchar *
 get_message_url (FeedItem *item)
 {
 	const gchar *url;
diff --git a/src/miners/rss/wrap-feed-channel.c b/src/miners/rss/wrap-feed-channel.c
new file mode 100644
index 0000000..bb16b66
--- /dev/null
+++ b/src/miners/rss/wrap-feed-channel.c
@@ -0,0 +1,211 @@
+/*
+ * Copyright (C) 2009, Roberto Guido <madbob users barberaware org>
+ *                     Michele Tameni <michele amdplanet it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include "wrap-feed-channel.h"
+#include "tracker-miner-rss.h"
+
+#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), WRAP_FEED_CHANNEL_TYPE, WrapFeedChannelPrivate))
+
+typedef struct _WrapFeedChannelPrivate	WrapFeedChannelPrivate;
+
+struct _WrapFeedChannelPrivate {
+	TrackerMinerRSS	*miner;
+
+	gchar *subject;
+
+	gint items_expiry_interval;
+	guint expiration_handler;
+
+	gboolean download_enclosures;
+	gint enclosures_maxsize;
+	gchar *enclosures_saving_path;
+};
+
+G_DEFINE_TYPE (WrapFeedChannel, wrap_feed_channel, FEED_CHANNEL_TYPE);
+
+static gboolean
+check_expired_items_cb (gpointer data)
+{
+	gchar *query;
+	gchar time_ago_str [100];
+	time_t time_ago_t;
+	struct tm time_ago_tm;
+	WrapFeedChannel *node;
+	WrapFeedChannelPrivate *priv;
+
+	node = data;
+	priv = GET_PRIV (node);
+
+	time_ago_t = time (NULL) - (priv->items_expiry_interval * 60);
+	localtime_r (&time_ago_t, &time_ago_tm);
+	strftime (time_ago_str, 100, "%Y-%m-%dT%H:%M:%SZ", &time_ago_tm);
+
+	query = g_strdup_printf ("DELETE {?i a rdfs:Resource} WHERE {?i nmo:communicationChannel <%s> . ?i mfo:downloadedTime ?t FILTER (?t < \"%s\")}",
+	                         priv->subject, time_ago_str);
+
+	tracker_miner_execute_update (TRACKER_MINER (priv->miner), query, NULL, NULL, NULL);
+	g_free (query);
+
+	return TRUE;
+}
+
+static void
+review_expiration_timer (WrapFeedChannel *node)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (node);
+
+	if (priv->expiration_handler != 0)
+		g_source_remove (priv->expiration_handler);
+
+	check_expired_items_cb (node);
+	priv->expiration_handler = g_timeout_add_seconds (priv->items_expiry_interval * 60, check_expired_items_cb, node);
+}
+
+static void
+wrap_feed_channel_finalize (GObject *obj)
+{
+	WrapFeedChannel *chan;
+	WrapFeedChannelPrivate *priv;
+
+	chan = WRAP_FEED_CHANNEL (obj);
+	priv = GET_PRIV (chan);
+
+	if (priv->subject != NULL)
+		g_free (priv->subject);
+
+	if (priv->enclosures_saving_path != NULL)
+		g_free (priv->enclosures_saving_path);
+}
+
+static void
+wrap_feed_channel_class_init (WrapFeedChannelClass *klass)
+{
+	GObjectClass *gobject_class;
+
+	gobject_class = G_OBJECT_CLASS (klass);
+	gobject_class->finalize = wrap_feed_channel_finalize;
+
+	g_type_class_add_private (klass, sizeof (WrapFeedChannelPrivate));
+}
+
+static void
+wrap_feed_channel_init (WrapFeedChannel *node)
+{
+}
+
+WrapFeedChannel*
+wrap_feed_channel_new (TrackerMinerRSS *miner,
+                       gchar           *subject)
+{
+	WrapFeedChannel *ret;
+	WrapFeedChannelPrivate *priv;
+
+	ret = g_object_new (WRAP_FEED_CHANNEL_TYPE, NULL);
+
+	priv = GET_PRIV (ret);
+	priv->miner = miner;
+	priv->subject = g_strdup (subject);
+	return ret;
+}
+
+const gchar*
+wrap_feed_channel_get_subject (WrapFeedChannel *feed)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (feed);
+	return (const gchar*) priv->subject;
+}
+
+void
+wrap_feed_channel_set_feeds_expiry (WrapFeedChannel *feed,
+                                    int              minutes)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (feed);
+
+	if (priv->items_expiry_interval != minutes) {
+		priv->items_expiry_interval = minutes;
+		review_expiration_timer (feed);
+	}
+}
+
+void
+wrap_feed_channel_set_download_enclosures (WrapFeedChannel *feed,
+                                           gboolean         download)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (feed);
+	priv->download_enclosures = download;
+}
+
+gboolean
+wrap_feed_channel_get_download_enclosures (WrapFeedChannel *feed)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (feed);
+	return priv->download_enclosures;
+}
+
+void
+wrap_feed_channel_set_enclosures_maxsize (WrapFeedChannel *feed,
+                                          int              kb)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (feed);
+	priv->enclosures_maxsize = kb;
+}
+
+int
+wrap_feed_channel_get_enclosures_maxsize (WrapFeedChannel *feed)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (feed);
+	return priv->enclosures_maxsize;
+}
+
+void
+wrap_feed_channel_set_enclosures_saving_path (WrapFeedChannel *feed,
+                                              gchar           *path)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (feed);
+
+	if (priv->enclosures_saving_path != NULL)
+		g_free (priv->enclosures_saving_path);
+	priv->enclosures_saving_path = g_strdup (path);
+}
+
+const gchar*
+wrap_feed_channel_get_enclosures_saving_path (WrapFeedChannel *feed)
+{
+	WrapFeedChannelPrivate *priv;
+
+	priv = GET_PRIV (feed);
+	return (const gchar*) priv->enclosures_saving_path;
+}
diff --git a/src/miners/rss/wrap-feed-channel.h b/src/miners/rss/wrap-feed-channel.h
new file mode 100644
index 0000000..f86ea4d
--- /dev/null
+++ b/src/miners/rss/wrap-feed-channel.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2009, Roberto Guido <madbob users barberaware org>
+ *                     Michele Tameni <michele amdplanet it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __WRAP_FEED_CHANNEL_H__
+#define __WRAP_FEED_CHANNEL_H__
+
+#include <libgrss.h>
+#include "tracker-miner-rss.h"
+
+#define WRAP_FEED_CHANNEL_TYPE         (wrap_feed_channel_get_type())
+#define WRAP_FEED_CHANNEL(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), WRAP_FEED_CHANNEL_TYPE, WrapFeedChannel))
+#define WRAP_FEED_CHANNEL_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), WRAP_FEED_CHANNEL_TYPE, WrapFeedChannelClass))
+#define IS_WRAP_FEED_CHANNEL(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), WRAP_FEED_CHANNEL_TYPE))
+#define IS_WRAP_FEED_CHANNEL_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c),  WRAP_FEED_CHANNEL_TYPE))
+#define WRAP_FEED_CHANNEL_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), WRAP_FEED_CHANNEL_TYPE, WrapFeedChannelClass))
+
+typedef struct _WrapFeedChannel		WrapFeedChannel;
+
+struct _WrapFeedChannel {
+	FeedChannel parent;
+};
+
+typedef struct {
+	FeedChannelClass parent;
+} WrapFeedChannelClass;
+
+GType            wrap_feed_channel_get_type                   (void) G_GNUC_CONST;
+
+WrapFeedChannel* wrap_feed_channel_new                        (TrackerMinerRSS *miner, gchar *subject);
+
+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);
+gboolean         wrap_feed_channel_get_download_enclosures    (WrapFeedChannel *feed);
+void             wrap_feed_channel_set_enclosures_maxsize     (WrapFeedChannel *feed, int kb);
+int              wrap_feed_channel_get_enclosures_maxsize     (WrapFeedChannel *feed);
+void             wrap_feed_channel_set_enclosures_saving_path (WrapFeedChannel *feed, gchar *path);
+const gchar*     wrap_feed_channel_get_enclosures_saving_path (WrapFeedChannel *feed);
+
+#endif /* __WRAP_FEED_CHANNEL_H__ */
diff --git a/src/miners/rss/wrap-feed-enclosure.c b/src/miners/rss/wrap-feed-enclosure.c
new file mode 100644
index 0000000..730c639
--- /dev/null
+++ b/src/miners/rss/wrap-feed-enclosure.c
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2009, Roberto Guido <madbob users barberaware org>
+ *                     Michele Tameni <michele amdplanet it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include <unistd.h>
+#include "wrap-feed-enclosure.h"
+
+#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), WRAP_FEED_ENCLOSURE_TYPE, WrapFeedEnclosurePrivate))
+
+typedef struct _WrapFeedEnclosurePrivate	WrapFeedEnclosurePrivate;
+
+struct _WrapFeedEnclosurePrivate {
+	FeedEnclosure *enclosure;
+	WrapFeedChannel *channel;
+};
+
+G_DEFINE_TYPE (WrapFeedEnclosure, wrap_feed_enclosure, G_TYPE_OBJECT);
+
+static void
+wrap_feed_enclosure_finalize (GObject *obj)
+{
+	WrapFeedEnclosure *enc;
+	WrapFeedEnclosurePrivate *priv;
+
+	enc = WRAP_FEED_ENCLOSURE (obj);
+	priv = GET_PRIV (enc);
+
+	g_object_unref (priv->enclosure);
+	g_object_unref (priv->channel);
+}
+
+static void
+wrap_feed_enclosure_class_init (WrapFeedEnclosureClass *klass)
+{
+	GObjectClass *gobject_class;
+
+	gobject_class = G_OBJECT_CLASS (klass);
+	gobject_class->finalize = wrap_feed_enclosure_finalize;
+
+	g_type_class_add_private (klass, sizeof (WrapFeedEnclosurePrivate));
+}
+
+static void
+wrap_feed_enclosure_init (WrapFeedEnclosure *node)
+{
+}
+
+WrapFeedEnclosure*
+wrap_feed_enclosure_new (FeedEnclosure   *enclosure,
+                         WrapFeedChannel *channel)
+{
+	WrapFeedEnclosure *ret;
+	WrapFeedEnclosurePrivate *priv;
+
+	ret = g_object_new (WRAP_FEED_ENCLOSURE_TYPE, NULL);
+
+	priv = GET_PRIV (ret);
+	priv->enclosure = enclosure;
+	g_object_ref (priv->enclosure);
+	priv->channel = channel;
+	g_object_ref (priv->channel);
+
+	return ret;
+}
+
+void
+wrap_feed_enclosure_save_data (WrapFeedEnclosure *enclosure,
+                               const gchar       *data,
+                               gsize              len)
+{
+	int modifier;
+	gchar *path;
+	gchar *name;
+	gchar *new_name;
+	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;
+
+	/* 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);
+
+		g_free (path);
+		g_free (name);
+
+		path = g_build_filename (folder, new_name, NULL);
+		name = new_name;
+	}
+
+	fd = fopen (path, "w+");
+	if (fd == NULL) {
+		g_warning ("Unable to open saving location for enclosure.");
+	}
+	else {
+		if (fwrite (data, len, 1, fd) != len)
+			g_warning ("Error while saving enclosure contents.");
+
+		fclose (fd);
+	}
+
+	g_free (path);
+	g_free (name);
+}
diff --git a/src/miners/rss/wrap-feed-enclosure.h b/src/miners/rss/wrap-feed-enclosure.h
new file mode 100644
index 0000000..6240074
--- /dev/null
+++ b/src/miners/rss/wrap-feed-enclosure.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2009, Roberto Guido <madbob users barberaware org>
+ *                     Michele Tameni <michele amdplanet it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __WRAP_FEED_ENCLOSURE_H__
+#define __WRAP_FEED_ENCLOSURE_H__
+
+#include <libgrss.h>
+#include "wrap-feed-channel.h"
+
+#define WRAP_FEED_ENCLOSURE_TYPE		(wrap_feed_enclosure_get_type())
+#define WRAP_FEED_ENCLOSURE(o)			(G_TYPE_CHECK_INSTANCE_CAST ((o), WRAP_FEED_ENCLOSURE_TYPE, WrapFeedEnclosure))
+#define WRAP_FEED_ENCLOSURE_CLASS(c)		(G_TYPE_CHECK_CLASS_CAST ((c), WRAP_FEED_ENCLOSURE_TYPE, WrapFeedEnclosureClass))
+#define IS_WRAP_FEED_ENCLOSURE(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), WRAP_FEED_ENCLOSURE_TYPE))
+#define IS_WRAP_FEED_ENCLOSURE_CLASS(c)		(G_TYPE_CHECK_CLASS_TYPE ((c),  WRAP_FEED_ENCLOSURE_TYPE))
+#define WRAP_FEED_ENCLOSURE_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), WRAP_FEED_ENCLOSURE_TYPE, WrapFeedEnclosureClass))
+
+typedef struct _WrapFeedEnclosure		WrapFeedEnclosure;
+
+struct _WrapFeedEnclosure {
+	GObject parent;
+};
+
+typedef struct {
+	GObjectClass parent;
+} WrapFeedEnclosureClass;
+
+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);
+
+#endif /* __WRAP_FEED_ENCLOSURE_H__ */



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