hythmbox-2.98/podcast/rb-podcast-search-miroguide.c

No issues found

Incomplete coverage

Tool Failure ID Location Function Message Data
clang-analyzer no-output-found rb-podcast-search-miroguide.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
clang-analyzer no-output-found rb-podcast-search-miroguide.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
  1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2  *
  3  *  Copyright (C) 2010 Jonathan Matthew <jonathan@d14n.org>
  4  *
  5  *  This program is free software; you can redistribute it and/or modify
  6  *  it under the terms of the GNU General Public License as published by
  7  *  the Free Software Foundation; either version 2 of the License, or
  8  *  (at your option) any later version.
  9  *
 10  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
 11  *  GStreamer plugins to be used and distributed together with GStreamer
 12  *  and Rhythmbox. This permission is above and beyond the permissions granted
 13  *  by the GPL license by which Rhythmbox is covered. If you modify this code
 14  *  you may extend this exception to your version of the code, but you are not
 15  *  obligated to do so. If you do not wish to do so, delete this exception
 16  *  statement from your version.
 17  *
 18  *  This program is distributed in the hope that it will be useful,
 19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 21  *  GNU General Public License for more details.
 22  *
 23  *  You should have received a copy of the GNU General Public License
 24  *  along with this program; if not, write to the Free Software
 25  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 26  *
 27  */
 28 
 29 #include "config.h"
 30 
 31 #include "rb-podcast-search.h"
 32 #include "rb-debug.h"
 33 
 34 #include <libsoup/soup.h>
 35 #include <libsoup/soup-gnome.h>
 36 #include <json-glib/json-glib.h>
 37 #include <totem-pl-parser.h>
 38 
 39 #define RB_TYPE_PODCAST_SEARCH_MIROGUIDE         (rb_podcast_search_miroguide_get_type ())
 40 #define RB_PODCAST_SEARCH_MIROGUIDE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_PODCAST_SEARCH_MIROGUIDE, RBPodcastSearchMiroGuide))
 41 #define RB_PODCAST_SEARCH_MIROGUIDE_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_PODCAST_SEARCH_MIROGUIDE, RBPodcastSearchMiroGuideClass))
 42 #define RB_IS_PODCAST_SEARCH_MIROGUIDE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_PODCAST_SEARCH_MIROGUIDE))
 43 #define RB_IS_PODCAST_SEARCH_MIROGUIDE_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_PODCAST_SEARCH_MIROGUIDE))
 44 #define RB_PODCAST_SEARCH_MIROGUIDE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_PODCAST_SEARCH_MIROGUIDE, RBPodcastSearchMiroGuideClass))
 45 
 46 typedef struct _RBPodcastSearchMiroGuide RBPodcastSearchMiroGuide;
 47 typedef struct _RBPodcastSearchMiroGuideClass RBPodcastSearchMiroGuideClass;
 48 
 49 struct _RBPodcastSearchMiroGuide
 50 {
 51 	RBPodcastSearch parent;
 52 
 53 	SoupSession *session;
 54 };
 55 
 56 struct _RBPodcastSearchMiroGuideClass
 57 {
 58 	RBPodcastSearchClass parent;
 59 };
 60 
 61 static void rb_podcast_search_miroguide_class_init (RBPodcastSearchMiroGuideClass *klass);
 62 static void rb_podcast_search_miroguide_init (RBPodcastSearchMiroGuide *search);
 63 
 64 G_DEFINE_TYPE (RBPodcastSearchMiroGuide, rb_podcast_search_miroguide, RB_TYPE_PODCAST_SEARCH);
 65 
 66 #define MIROGUIDE_SEARCH_URI	"http://www.miroguide.com/api/get_feeds"
 67 
 68 static void
 69 process_results (RBPodcastSearchMiroGuide *search, JsonParser *parser)
 70 {
 71 	JsonArray *results;
 72 	guint i;
 73 
 74 	results = json_node_get_array (json_parser_get_root (parser));
 75 
 76 	for (i = 0; i < json_array_get_length (results); i++) {
 77 		JsonObject *feed;
 78 		JsonArray *items;
 79 		RBPodcastChannel *channel;
 80 		int j;
 81 
 82 		feed = json_array_get_object_element (results, i);
 83 
 84 		channel = g_new0 (RBPodcastChannel, 1);
 85 		channel->url = g_strdup (json_object_get_string_member (feed, "url"));
 86 		channel->title = g_strdup (json_object_get_string_member (feed, "name"));
 87 		channel->author = g_strdup (json_object_get_string_member (feed, "publisher"));		/* hrm */
 88 		channel->img = g_strdup (json_object_get_string_member (feed, "thumbnail_url"));
 89 		channel->is_opml = FALSE;
 90 		rb_debug ("feed %d: url %s, name \"%s\"", i, channel->url, channel->title);
 91 
 92 		items = json_object_get_array_member (feed, "item");
 93 		for (j = 0; j < json_array_get_length (items); j++) {
 94 			JsonObject *episode = json_array_get_object_element (items, j);
 95 			RBPodcastItem *item;
 96 
 97 			item = g_new0 (RBPodcastItem, 1);
 98 			item->title = g_strdup (json_object_get_string_member (episode, "name"));
 99 			item->url = g_strdup (json_object_get_string_member (episode, "url"));
100 			item->description = g_strdup (json_object_get_string_member (episode, "description"));
101 			item->pub_date = totem_pl_parser_parse_date (json_object_get_string_member (episode, "date"), FALSE);
102 			item->filesize = json_object_get_int_member (episode, "size");
103 			rb_debug ("item %d: title \"%s\", url %s", j, item->title, item->url);
104 
105 			channel->posts = g_list_prepend (channel->posts, item);
106 		}
107 		channel->posts = g_list_reverse (channel->posts);
108 		rb_debug ("finished parsing items");
109 
110 		rb_podcast_search_result (RB_PODCAST_SEARCH (search), channel);
111 		rb_podcast_parse_channel_free (channel);
112 	}
113 }
114 
115 static void
116 search_response_cb (SoupSession *session, SoupMessage *msg, RBPodcastSearchMiroGuide *search)
117 {
118 	JsonParser *parser;
119 	int code;
120 
121 	g_object_get (msg, SOUP_MESSAGE_STATUS_CODE, &code, NULL);
122 	if (code != 200) {
123 		char *reason;
124 
125 		g_object_get (msg, SOUP_MESSAGE_REASON_PHRASE, &reason, NULL);
126 		rb_debug ("search request failed: %s", reason);
127 		g_free (reason);
128 		rb_podcast_search_finished (RB_PODCAST_SEARCH (search), FALSE);
129 		return;
130 	}
131 
132 	if (msg->response_body->data == NULL) {
133 		rb_debug ("no response data");
134 		rb_podcast_search_finished (RB_PODCAST_SEARCH (search), TRUE);
135 		return;
136 	}
137 
138 	parser = json_parser_new ();
139 	if (json_parser_load_from_data (parser, msg->response_body->data, msg->response_body->length, NULL)) {
140 		process_results (search, parser);
141 	} else {
142 		rb_debug ("unable to parse response data");
143 	}
144 
145 	g_object_unref (parser);
146 	rb_podcast_search_finished (RB_PODCAST_SEARCH (search), TRUE);
147 }
148 
149 static void
150 impl_start (RBPodcastSearch *bsearch, const char *text, int max_results)
151 {
152 	SoupURI *uri;
153 	SoupMessage *message;
154 	char *limit;
155 	RBPodcastSearchMiroGuide *search = RB_PODCAST_SEARCH_MIROGUIDE (bsearch);
156 
157 	search->session = soup_session_async_new_with_options (SOUP_SESSION_ADD_FEATURE_BY_TYPE,
158 							       SOUP_TYPE_GNOME_FEATURES_2_26,
159 							       NULL);
160 
161 	uri = soup_uri_new (MIROGUIDE_SEARCH_URI);
162 	limit = g_strdup_printf ("%d", max_results);
163 	soup_uri_set_query_from_fields (uri,
164 					"filter", "audio",
165 					"filter_value", "1",
166 					"filter", "name",
167 					"filter_value", text,
168 					"sort", "popular",	/* hmm */
169 					"limit", limit,
170 					"datatype", "json",
171 					NULL);
172 	g_free (limit);
173 
174 	message = soup_message_new_from_uri (SOUP_METHOD_GET, uri);
175 	soup_uri_free (uri);
176 
177 	soup_session_queue_message (search->session, message, (SoupSessionCallback) search_response_cb, search);
178 }
179 
180 static void
181 impl_cancel (RBPodcastSearch *bsearch)
182 {
183 	RBPodcastSearchMiroGuide *search = RB_PODCAST_SEARCH_MIROGUIDE (bsearch);
184 	if (search->session != NULL) {
185 		soup_session_abort (search->session);
186 	}
187 }
188 
189 static void
190 impl_dispose (GObject *object)
191 {
192 	RBPodcastSearchMiroGuide *search = RB_PODCAST_SEARCH_MIROGUIDE (object);
193 
194 	if (search->session != NULL) {
195 		soup_session_abort (search->session);
196 		g_object_unref (search->session);
197 		search->session = NULL;
198 	}
199 
200 	G_OBJECT_CLASS (rb_podcast_search_miroguide_parent_class)->dispose (object);
201 }
202 
203 static void
204 rb_podcast_search_miroguide_init (RBPodcastSearchMiroGuide *search)
205 {
206 	/* do nothing? */
207 }
208 
209 static void
210 rb_podcast_search_miroguide_class_init (RBPodcastSearchMiroGuideClass *klass)
211 {
212 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
213 	RBPodcastSearchClass *search_class = RB_PODCAST_SEARCH_CLASS (klass);
214 
215 	object_class->dispose = impl_dispose;
216 
217 	search_class->start = impl_start;
218 	search_class->cancel = impl_cancel;
219 }