summaryrefslogtreecommitdiff
path: root/src/trg-rss-model.c
blob: 12b2d2b34ace034175645727dd29136de999fab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * transmission-remote-gtk - A GTK RPC client to Transmission
 * Copyright (C) 2011-2013  Alan Fitton

 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program 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 General Public License for more details.

 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <gtk/gtk.h>
#include <json-glib/json-glib.h>
#include <rss-glib/rss-glib.h>

#include "config.h"
#include "torrent.h"
#include "trg-client.h"
#include "trg-model.h"
#include "trg-rss-model.h"

enum {
	PROP_0, PROP_CLIENT
};

G_DEFINE_TYPE(TrgRssModel, trg_rss_model, GTK_TYPE_LIST_STORE)
#define TRG_RSS_MODEL_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRG_TYPE_RSS_MODEL, TrgRssModelPrivate))
typedef struct _TrgRssModelPrivate TrgRssModelPrivate;

struct _TrgRssModelPrivate {
	TrgClient *client;
	GHashTable *table;
};

typedef struct {
	TrgRssModel *model;
	gchar *feed_id;
	gchar *feed_url;
	GError *error;
	trg_response *response;
} feed_update;

static void feed_update_free(feed_update *update) {
	if (update->error)
		g_error_free(update->error);

	g_free(update->feed_id);
	g_free(update->feed_url);

	g_free(update);
}

static gboolean on_rss_receive(gpointer data) {
	trg_response *response = (trg_response *) data;
	feed_update *update = (feed_update*) response->cb_data;
	TrgRssModel *model = update->model;
	TrgRssModelPrivate *priv = TRG_RSS_MODEL_GET_PRIVATE(model);

	if (response->status == CURLE_OK) {
		RssParser* parser = rss_parser_new();
		GError *error = NULL;
		if (rss_parser_load_from_data(parser, response->raw,
				response->size, &error)) {
			RssDocument *doc = rss_parser_get_document(parser);
			GtkTreeIter iter;
			GList *list, *tmp;
			gchar *title;

			list = rss_document_get_items(doc);

			for (tmp = list; tmp != NULL; tmp = tmp->next) {
				RssItem *item = (RssItem*) tmp->data;
				const gchar *guid = rss_item_get_guid(item);
				if (g_hash_table_lookup(priv->table, guid) != (void*) 1) {
					gtk_list_store_append(GTK_LIST_STORE(model), &iter);
					gtk_list_store_set(GTK_LIST_STORE(model), &iter, RSSCOL_ID,
							guid, RSSCOL_TITLE, rss_item_get_title(item),
							RSSCOL_LINK, rss_item_get_link(item), RSSCOL_FEED,
							update->feed_id, RSSCOL_PUBDATE,
							rss_item_get_pub_date(item), -1);
					g_hash_table_insert(priv->table, g_strdup(guid), (void*) 1);
				}
			}

			g_list_free(list);
			g_object_unref(doc);
			g_object_unref(parser);
		} else {
			g_error_free(error);
			g_message("parse error?");
		}
	}

	trg_response_free(response);
	feed_update_free(update);

	return FALSE;
}

void trg_rss_model_update(TrgRssModel * model) {
	TrgRssModelPrivate *priv = TRG_RSS_MODEL_GET_PRIVATE(model);
	TrgPrefs *prefs = trg_client_get_prefs(priv->client);
	JsonArray *feeds = trg_prefs_get_rss(prefs);
	GList *li;

	if (!feeds)
		return;

	for (li = json_array_get_elements(feeds); li != NULL;
			li = g_list_next(li)) {
		JsonObject *feed = json_node_get_object((JsonNode *) li->data);
		const gchar *url = json_object_get_string_member(feed, "url");
		const gchar *id = json_object_get_string_member(feed, "id");

		if (!url || !id)
			continue;

		feed_update *update = g_new0(feed_update, 1);
		update->feed_url = g_strdup(url);
		update->feed_id = g_strdup(id);
		update->model = model;

		async_http_request(priv->client, update->feed_url, on_rss_receive,
				update);
	}

	/*trg_model_remove_removed(GTK_LIST_STORE(model),
	 RSSCOL_UPDATESERIAL, updateSerial);*/
}

static void trg_rss_model_set_property(GObject * object, guint prop_id,
		const GValue * value, GParamSpec * pspec G_GNUC_UNUSED) {
	TrgRssModelPrivate *priv = TRG_RSS_MODEL_GET_PRIVATE(object);

	switch (prop_id) {
	case PROP_CLIENT:
		priv->client = g_value_get_pointer(value);
		break;
	}
}

static void trg_rss_model_get_property(GObject * object, guint prop_id,
		GValue * value, GParamSpec * pspec G_GNUC_UNUSED) {
}

static GObject *trg_rss_model_constructor(GType type,
		guint n_construct_properties, GObjectConstructParam * construct_params) {
	GObject *obj = G_OBJECT_CLASS
	(trg_rss_model_parent_class)->constructor(type,
			n_construct_properties, construct_params);
	TrgRssModelPrivate *priv = TRG_RSS_MODEL_GET_PRIVATE(obj);

	priv->table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);

	return obj;
}

static void trg_rss_model_dispose(GObject * object) {
	TrgRssModelPrivate *priv = TRG_RSS_MODEL_GET_PRIVATE(object);
	g_hash_table_destroy(priv->table);
	G_OBJECT_CLASS(trg_rss_model_parent_class)->dispose(object);
}

static void trg_rss_model_class_init(TrgRssModelClass * klass) {
	GObjectClass *object_class = G_OBJECT_CLASS(klass);

	g_type_class_add_private(klass, sizeof(TrgRssModelPrivate));

	object_class->set_property = trg_rss_model_set_property;
	object_class->get_property = trg_rss_model_get_property;
	object_class->constructor = trg_rss_model_constructor;
	object_class->dispose = trg_rss_model_dispose;

	g_object_class_install_property(object_class, PROP_CLIENT,
			g_param_spec_pointer("client", "client", "client",
					G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
							| G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK
							| G_PARAM_STATIC_BLURB));
}

static void trg_rss_model_init(TrgRssModel * self) {
	GType column_types[RSSCOL_COLUMNS];

	column_types[RSSCOL_ID] = G_TYPE_STRING;
	column_types[RSSCOL_TITLE] = G_TYPE_STRING;
	column_types[RSSCOL_LINK] = G_TYPE_STRING;
	column_types[RSSCOL_FEED] = G_TYPE_STRING;
	column_types[RSSCOL_PUBDATE] = G_TYPE_STRING;

	gtk_list_store_set_column_types(GTK_LIST_STORE(self), RSSCOL_COLUMNS,
			column_types);
}

TrgRssModel *trg_rss_model_new(TrgClient *client) {
	return g_object_new(TRG_TYPE_RSS_MODEL, "client", client, NULL);
}