1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* weather-wx.c - Weather server functions (WX Radar)
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 #define MATEWEATHER_I_KNOW_THIS_IS_UNSTABLE
24 #include "weather.h"
25 #include "weather-priv.h"
26 
27 static void
wx_finish(SoupSession * session,SoupMessage * msg,gpointer data)28 wx_finish (SoupSession *session, SoupMessage *msg, gpointer data)
29 {
30     WeatherInfo *info = (WeatherInfo *)data;
31     GdkPixbufAnimation *animation;
32 
33     g_return_if_fail (info != NULL);
34 
35     if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
36 	g_warning ("Failed to get radar map image: %d %s.\n",
37 		   msg->status_code, msg->reason_phrase);
38 	g_object_unref (info->radar_loader);
39 	request_done (info, FALSE);
40 	return;
41     }
42 
43     gdk_pixbuf_loader_close (info->radar_loader, NULL);
44     animation = gdk_pixbuf_loader_get_animation (info->radar_loader);
45     if (animation != NULL) {
46 	if (info->radar)
47 	    g_object_unref (info->radar);
48 	info->radar = animation;
49 	g_object_ref (info->radar);
50     }
51     g_object_unref (info->radar_loader);
52 
53     request_done (info, TRUE);
54 }
55 
56 static void
wx_got_chunk(SoupMessage * msg,SoupBuffer * chunk,gpointer data)57 wx_got_chunk (SoupMessage *msg, SoupBuffer *chunk, gpointer data)
58 {
59     WeatherInfo *info = (WeatherInfo *)data;
60     GError *error = NULL;
61 
62     g_return_if_fail (info != NULL);
63 
64     gdk_pixbuf_loader_write (info->radar_loader, (guchar *)chunk->data,
65 			     chunk->length, &error);
66     if (error) {
67 	g_print ("%s \n", error->message);
68 	g_error_free (error);
69     }
70 }
71 
72 /* Get radar map and into newly allocated pixmap */
73 void
wx_start_open(WeatherInfo * info)74 wx_start_open (WeatherInfo *info)
75 {
76     gchar *url;
77     SoupMessage *msg;
78     WeatherLocation *loc;
79 
80     g_return_if_fail (info != NULL);
81     info->radar = NULL;
82     info->radar_loader = gdk_pixbuf_loader_new ();
83     loc = info->location;
84     g_return_if_fail (loc != NULL);
85 
86     if (info->radar_url)
87 	url = g_strdup (info->radar_url);
88     else {
89 	if (loc->radar[0] == '-')
90 	    return;
91 	url = g_strdup_printf ("http://image.weather.com/web/radar/us_%s_closeradar_medium_usen.jpg", loc->radar);
92     }
93 
94     msg = soup_message_new ("GET", url);
95     if (!msg) {
96 	g_warning ("Invalid radar URL: %s\n", url);
97 	g_free (url);
98 	return;
99     }
100 
101     g_signal_connect (msg, "got-chunk", G_CALLBACK (wx_got_chunk), info);
102     soup_message_body_set_accumulate (msg->response_body, FALSE);
103     soup_session_queue_message (info->session, msg, wx_finish, info);
104     g_free (url);
105 
106     info->requests_pending++;
107 }
108