Blame libgweather/weather-wx.c

rpm-build ca2b01
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
rpm-build ca2b01
/* weather-wx.c - Weather server functions (WX Radar)
rpm-build ca2b01
 *
rpm-build ca2b01
 * This program is free software; you can redistribute it and/or
rpm-build ca2b01
 * modify it under the terms of the GNU General Public License as
rpm-build ca2b01
 * published by the Free Software Foundation; either version 2 of the
rpm-build ca2b01
 * License, or (at your option) any later version.
rpm-build ca2b01
 *
rpm-build ca2b01
 * This program is distributed in the hope that it will be useful, but
rpm-build ca2b01
 * WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build ca2b01
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rpm-build ca2b01
 * General Public License for more details.
rpm-build ca2b01
 *
rpm-build ca2b01
 * You should have received a copy of the GNU General Public License
rpm-build ca2b01
 * along with this program; if not, see
rpm-build ca2b01
 * <http://www.gnu.org/licenses/>.
rpm-build ca2b01
 */
rpm-build ca2b01
rpm-build ca2b01
#ifdef HAVE_CONFIG_H
rpm-build ca2b01
#include <config.h>
rpm-build ca2b01
#endif
rpm-build ca2b01
rpm-build ca2b01
#include "gweather-private.h"
rpm-build ca2b01
rpm-build ca2b01
static void
rpm-build ca2b01
wx_finish (SoupSession *session, SoupMessage *msg, gpointer data)
rpm-build ca2b01
{
rpm-build ca2b01
    GWeatherInfo *info = (GWeatherInfo *)data;
rpm-build ca2b01
    GWeatherInfoPrivate *priv;
rpm-build ca2b01
    GdkPixbufAnimation *animation;
rpm-build ca2b01
rpm-build ca2b01
    g_return_if_fail (info != NULL);
rpm-build ca2b01
    priv = info->priv;
rpm-build ca2b01
rpm-build ca2b01
    if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
rpm-build ca2b01
	if (msg->status_code != SOUP_STATUS_CANCELLED)
rpm-build ca2b01
	    g_warning ("Failed to get radar map image: %d %s.\n",
rpm-build ca2b01
		       msg->status_code, msg->reason_phrase);
rpm-build ca2b01
	g_object_unref (priv->radar_loader);
rpm-build ca2b01
	_gweather_info_request_done (info, msg);
rpm-build ca2b01
	return;
rpm-build ca2b01
    }
rpm-build ca2b01
rpm-build ca2b01
    gdk_pixbuf_loader_close (priv->radar_loader, NULL);
rpm-build ca2b01
    animation = gdk_pixbuf_loader_get_animation (priv->radar_loader);
rpm-build ca2b01
    if (animation != NULL) {
rpm-build ca2b01
	if (priv->radar)
rpm-build ca2b01
	    g_object_unref (priv->radar);
rpm-build ca2b01
	priv->radar = animation;
rpm-build ca2b01
	g_object_ref (priv->radar);
rpm-build ca2b01
    }
rpm-build ca2b01
    g_object_unref (priv->radar_loader);
rpm-build ca2b01
rpm-build ca2b01
    _gweather_info_request_done (info, msg);
rpm-build ca2b01
}
rpm-build ca2b01
rpm-build ca2b01
static void
rpm-build ca2b01
wx_got_chunk (SoupMessage *msg, SoupBuffer *chunk, gpointer data)
rpm-build ca2b01
{
rpm-build ca2b01
    GWeatherInfo *info = (GWeatherInfo *)data;
rpm-build ca2b01
    GError *error = NULL;
rpm-build ca2b01
rpm-build ca2b01
    g_return_if_fail (info != NULL);
rpm-build ca2b01
rpm-build ca2b01
    gdk_pixbuf_loader_write (info->priv->radar_loader, (guchar *)chunk->data,
rpm-build ca2b01
			     chunk->length, &error);
rpm-build ca2b01
    if (error) {
rpm-build ca2b01
	g_print ("%s \n", error->message);
rpm-build ca2b01
	g_error_free (error);
rpm-build ca2b01
    }
rpm-build ca2b01
}
rpm-build ca2b01
rpm-build ca2b01
/* Get radar map and into newly allocated pixmap */
rpm-build ca2b01
void
rpm-build ca2b01
wx_start_open (GWeatherInfo *info)
rpm-build ca2b01
{
rpm-build ca2b01
    gchar *url;
rpm-build ca2b01
    SoupMessage *msg;
rpm-build ca2b01
    WeatherLocation *loc;
rpm-build ca2b01
    GWeatherInfoPrivate *priv;
rpm-build ca2b01
rpm-build ca2b01
    g_return_if_fail (info != NULL);
rpm-build ca2b01
    priv = info->priv;
rpm-build ca2b01
rpm-build ca2b01
    priv->radar = NULL;
rpm-build ca2b01
    priv->radar_loader = gdk_pixbuf_loader_new ();
rpm-build ca2b01
    loc = &priv->location;
rpm-build ca2b01
rpm-build ca2b01
    if (priv->radar_url)
rpm-build ca2b01
	url = g_strdup (priv->radar_url);
rpm-build ca2b01
    else {
rpm-build ca2b01
	if (loc->radar[0] == '-')
rpm-build ca2b01
	    return;
rpm-build ca2b01
	url = g_strdup_printf ("http://image.weather.com/web/radar/us_%s_closeradar_medium_usen.jpg", loc->radar);
rpm-build ca2b01
    }
rpm-build ca2b01
 
rpm-build ca2b01
    msg = soup_message_new ("GET", url);
rpm-build ca2b01
    if (!msg) {
rpm-build ca2b01
	g_warning ("Invalid radar URL: %s\n", url);
rpm-build ca2b01
	g_free (url);
rpm-build ca2b01
	return;
rpm-build ca2b01
    }
rpm-build ca2b01
rpm-build ca2b01
    g_signal_connect (msg, "got-chunk", G_CALLBACK (wx_got_chunk), info);
rpm-build ca2b01
    soup_message_body_set_accumulate (msg->response_body, FALSE);
rpm-build ca2b01
    _gweather_info_begin_request (info, msg);
rpm-build ca2b01
    soup_session_queue_message (priv->session, msg, wx_finish, info);
rpm-build ca2b01
rpm-build ca2b01
    g_free (url);
rpm-build ca2b01
}