weather-summary.c 23.2 KB
Newer Older
Florian Rivoal's avatar
Florian Rivoal committed
1
/*  Copyright (c) 2003-2007 Xfce Development Team
2
 *
3 4 5
 *  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
6 7 8 9 10
 *  (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
11
 *  GNU Library General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU General Public License
14 15 16 17 18
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
19
#include <config.h>
20 21
#endif

22
#include <libxfce4ui/libxfce4ui.h>
23

Nick Schermer's avatar
Nick Schermer committed
24 25
#include "weather-parsers.h"
#include "weather-data.h"
Florian Rivoal's avatar
Florian Rivoal committed
26
#include "weather-http.h"
Nick Schermer's avatar
Nick Schermer committed
27
#include "weather.h"
28

Nick Schermer's avatar
Nick Schermer committed
29 30 31
#include "weather-summary.h"
#include "weather-translate.h"
#include "weather-icon.h"
32

33

34 35 36
static gboolean lnk_clicked (GtkTextTag *tag, GObject *obj,
			     GdkEvent *event, GtkTextIter *iter,
			     GtkWidget *textview);
37

38 39 40 41 42 43
#define BORDER                           8
#define APPEND_BTEXT(text)               gtk_text_buffer_insert_with_tags(GTK_TEXT_BUFFER(buffer),\
                                                                          &iter, text, -1, btag, NULL);
#define APPEND_TEXT_ITEM_REAL(text)      gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffer), \
                                                                &iter, text, -1);\
                                         g_free (value);
44 45
#define APPEND_TEXT_ITEM(text, item)     rawvalue = get_data(conditions, data->unit_system, item); \
                                         unit = get_unit(conditions, data->unit_system, item); \
46 47 48 49 50 51
                                         value = g_strdup_printf("\t%s%s%s%s%s\n", \
                                                                 text, text ? ": " : "", \
                                                                 rawvalue, \
                                                                 strcmp(unit, "°") ? " " : "", \
                                                                 unit); \
                                         g_free (rawvalue);\
52
                                         APPEND_TEXT_ITEM_REAL(value);
53 54 55 56 57 58 59 60 61
#define APPEND_LINK_ITEM(prefix, text, url, lnk_tag) \
					 gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffer), \
                                                                &iter, prefix, -1);\
					 gtk_text_buffer_insert_with_tags(GTK_TEXT_BUFFER(buffer), \
                                                                &iter, text, -1, lnk_tag, NULL);\
					 gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffer), \
                                                                &iter, "\n", -1);\
					 g_object_set_data_full(G_OBJECT(lnk_tag), "url", g_strdup(url), g_free); \
					 g_signal_connect(G_OBJECT(lnk_tag), "event", \
62
						G_CALLBACK(lnk_clicked), NULL);
63

64 65


66 67
static GtkTooltips *tooltips = NULL;

68 69 70 71 72 73
static gboolean lnk_clicked (GtkTextTag *tag, GObject *obj,
			     GdkEvent *event, GtkTextIter *iter,
			     GtkWidget *textview)
{
  if (event->type == GDK_BUTTON_RELEASE) {
    const gchar *url = g_object_get_data(G_OBJECT(tag), "url");
74
    gchar *str = g_strdup_printf("exo-open --launch WebBrowser %s", url);
75

76
    g_spawn_command_line_async(str, NULL);
77 78 79 80
    g_free(str);
  } else if (event->type == GDK_LEAVE_NOTIFY) {
     gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(obj),
				GTK_TEXT_WINDOW_TEXT), NULL);
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
  return FALSE;
}

static gboolean
icon_clicked (GtkWidget      *widget,
              GdkEventButton *event,
              gpointer        user_data)
{
  return lnk_clicked(user_data, NULL, (GdkEvent *)(event), NULL, NULL);
}

static GdkCursor *hand_cursor = NULL;
static GdkCursor *text_cursor = NULL;
static gboolean on_icon = FALSE;
static gboolean view_motion_notify(GtkWidget *widget,
				   GdkEventMotion *event,
				   GtkWidget *view)
{
  GtkTextBuffer *buffer;

  buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));

  if (event->x != -1 && event->y != -1) {
    gint bx, by;
    GtkTextIter iter;
    GSList *tags;
    GSList *cur;

110
    gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(view),
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
    						GTK_TEXT_WINDOW_WIDGET,
    						event->x, event->y, &bx, &by);
    gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(view),
    						&iter, bx, by);
    tags = gtk_text_iter_get_tags(&iter);
    for (cur = tags; cur != NULL; cur = cur->next) {
      GtkTextTag *tag = cur->data;
      if (g_object_get_data(G_OBJECT(tag), "url")) {
        gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(view),
				   GTK_TEXT_WINDOW_TEXT), hand_cursor);
        return FALSE;
      }
    }
  }
  if (!on_icon)
    gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(view),
			     GTK_TEXT_WINDOW_TEXT), text_cursor);
  return FALSE;
}

static gboolean icon_motion_notify(GtkWidget *widget,
				   GdkEventMotion *event,
				   GtkWidget *view)
{
  on_icon = TRUE;
  gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(view),
			     GTK_TEXT_WINDOW_TEXT), hand_cursor);
  return FALSE;
}

static gboolean view_leave_notify(GtkWidget *widget,
				   GdkEventMotion *event,
				   GtkWidget *view)
{
  on_icon = FALSE;
  gdk_window_set_cursor(gtk_text_view_get_window(GTK_TEXT_VIEW(view),
			     GTK_TEXT_WINDOW_TEXT), text_cursor);
  return FALSE;
}

static GtkWidget *weather_channel_evt = NULL;
static void view_scrolled_cb (GtkAdjustment *adj, GtkWidget *view)
{
  if (weather_channel_evt) {
    gint x, y, x1, y1;
156 157
    x1 = view->allocation.width - 73 - 5;
    y1 = view->requisition.height - 55 - 5;
158 159 160
    gtk_text_view_buffer_to_window_coords(
			GTK_TEXT_VIEW(view),
			GTK_TEXT_WINDOW_TEXT, x1, y1, &x, &y);
161
    gtk_text_view_move_child(GTK_TEXT_VIEW(view),
162 163 164 165 166 167 168 169 170 171
			weather_channel_evt, x, y);
  }
}

static void view_size_allocate_cb	(GtkWidget	*widget,
					 GtkAllocation	*allocation,
					 gpointer	 data)
{
  view_scrolled_cb(NULL, GTK_WIDGET(data));
}
172

173 174 175 176 177
static gchar *get_logo_path (void)
{
	gchar *dir = g_strconcat(g_get_user_cache_dir(), G_DIR_SEPARATOR_S,
				"xfce4", G_DIR_SEPARATOR_S, "weather-plugin", NULL);

178
	g_mkdir_with_parents(dir, 0755);
179 180 181 182 183 184 185 186 187
	g_free(dir);

	return g_strconcat(g_get_user_cache_dir(), G_DIR_SEPARATOR_S,
				"xfce4", G_DIR_SEPARATOR_S, "weather-plugin",
				G_DIR_SEPARATOR_S, "weather_logo.jpg", NULL);
}

static void
logo_fetched (gboolean  succeed,
Florian Rivoal's avatar
Florian Rivoal committed
188
           gchar     *result,
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	   size_t    len,
           gpointer  user_data)
{
	if (succeed && result) {
		gchar *path = get_logo_path();
		GError *error = NULL;
		GdkPixbuf *pixbuf = NULL;
		if (!g_file_set_contents(path, result, len, &error)) {
			printf("err %s\n", error?error->message:"?");
			g_error_free(error);
			g_free(result);
			g_free(path);
			return;
		}
		g_free(result);
		pixbuf = gdk_pixbuf_new_from_file(path, NULL);
		g_free(path);
		if (pixbuf) {
			gtk_image_set_from_pixbuf(GTK_IMAGE(user_data), pixbuf);
			g_object_unref(pixbuf);
		}
	}
}

static GtkWidget *weather_summary_get_logo(xfceweather_data *data)
{
	GtkWidget *image = gtk_image_new();
	GdkPixbuf *pixbuf = NULL;
	gchar *path = get_logo_path();
218

219 220 221
	pixbuf = gdk_pixbuf_new_from_file(path, NULL);
	g_free(path);
	if (pixbuf == NULL) {
222
		weather_http_receive_data ("xoap.weather.com", "/web/common/twc/logos/web_73x55.jpg",
223 224 225 226 227
			data->proxy_host, data->proxy_port, logo_fetched, image);
	} else {
		gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
		g_object_unref(pixbuf);
	}
228

229 230
	return image;
}
231

232
static GtkWidget *
233
create_summary_tab (xfceweather_data *data)
234
{
235
  GtkTextBuffer *buffer;
236
  GtkTextIter    iter;
Colin Leroy's avatar
Colin Leroy committed
237
  GtkTextTag    *btag, *ltag1;
238 239
  gchar         *value, *wind, *sun_val, *vis, *rawvalue;
  const gchar   *unit;
240
  GtkWidget     *view, *frame, *scrolled;
241 242 243
  GdkColor       lnk_color;
  GtkAdjustment *adj;
  GtkWidget     *weather_channel_icon;
244
  xml_time      *conditions;
245 246
  struct tm     *start, *end, *point_tm;
  char           interval_start[80], interval_end[80], point[80];
247 248 249

  view = gtk_text_view_new ();
  gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE);
250
  gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE);
Harald Judt's avatar
Harald Judt committed
251
  gtk_container_set_border_width (GTK_CONTAINER (view), BORDER);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  frame = gtk_frame_new (NULL);
  scrolled = gtk_scrolled_window_new (NULL, NULL);

  gtk_container_add (GTK_CONTAINER (scrolled), view);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  gtk_container_set_border_width (GTK_CONTAINER (frame), BORDER);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_container_add (GTK_CONTAINER (frame), scrolled);

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
  gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &iter, 0);
  btag =
    gtk_text_buffer_create_tag (buffer, NULL, "weight", PANGO_WEIGHT_BOLD,
                                NULL);

269 270 271
  gdk_color_parse("#0000ff", &lnk_color);
  ltag1 = gtk_text_buffer_create_tag(buffer, "lnk1", "foreground-gdk", &lnk_color, NULL);

272
  /* head */
Colin Leroy's avatar
Colin Leroy committed
273
  value = g_strdup_printf (_("Weather report for: %s.\n\n"), data->location_name);
274 275
  APPEND_BTEXT (value);
  g_free (value);
Colin Leroy's avatar
Colin Leroy committed
276

277
  conditions = get_current_conditions(data->weatherdata);
278
  APPEND_BTEXT(_("Coordinates\n"));
279 280 281 282
  APPEND_TEXT_ITEM (_("Altitude"), ALTITUDE);
  APPEND_TEXT_ITEM (_("Latitude"), LATITUDE);
  APPEND_TEXT_ITEM (_("Longitude"), LONGITUDE);

283
  APPEND_BTEXT(_("\nTime\n"));
284
  point_tm = localtime(&conditions->point);
285 286 287 288
  strftime (point, 80, "%c", point_tm);
  value = g_strdup_printf (_("\tPoint data applies to:\n\t%s\n"), point);
  APPEND_TEXT_ITEM_REAL (value);

289
  start = localtime(&conditions->start);
290
  strftime (interval_start, 80, "%c", start);
291
  end = localtime(&conditions->end);
292
  strftime (interval_end, 80, "%c", end);
293
  value = g_strdup_printf (_("\n\tInterval data applies to:\n\t%s\n\t%s\n"),
294 295
                           interval_start,
                           interval_end);
296 297
  APPEND_TEXT_ITEM_REAL (value);

298 299 300 301 302
  value = g_strdup_printf (_("\n\tInterval data provides icon, description and precipitation,"
                             "\n\twhile point data supplies the rest.\n"), point);
  APPEND_TEXT_ITEM_REAL (value);


303
  /* Temperature */
304
  APPEND_BTEXT (_("\nTemperature\n"));
Colin Leroy's avatar
Colin Leroy committed
305
  APPEND_TEXT_ITEM (_("Temperature"), TEMPERATURE);
306

307
  /* Wind */
308
  APPEND_BTEXT (_("\nWind\n"));
309 310
  rawvalue = get_data (conditions, data->unit_system, WIND_SPEED);
  wind = translate_wind_speed (conditions, rawvalue, data->unit_system);
311
  g_free (rawvalue);
312
  rawvalue = get_data (conditions, data->unit_system, WIND_BEAUFORT);
313 314
  value = g_strdup_printf (_("\t%s: %s (%s on the Beaufort scale)\n"), _("Speed"), wind, rawvalue);
  g_free (rawvalue);
315
  g_free (wind);
316
  APPEND_TEXT_ITEM_REAL (value);
317

318
  rawvalue = get_data (conditions, data->unit_system, WIND_DIRECTION);
319 320
  wind = translate_wind_direction (rawvalue);
  g_free (rawvalue);
321
  rawvalue = get_data (conditions, data->unit_system, WIND_DIRECTION_DEG);
Colin Leroy's avatar
Colin Leroy committed
322
  value = g_strdup_printf ("\t%s: %s (%s%s)\n", _("Direction"),
323
                           wind, rawvalue,
324
                           get_unit (conditions, data->unit_system, WIND_DIRECTION_DEG));
325
  g_free (rawvalue);
326
  g_free (wind);
327
  APPEND_TEXT_ITEM_REAL (value);
328

329 330 331 332
  /* Precipitation */
  APPEND_BTEXT (_("\nPrecipitations\n"));
  APPEND_TEXT_ITEM(_("Precipitations amount"), PRECIPITATIONS);

Harald Judt's avatar
Harald Judt committed
333 334
  /* Atmosphere */
  APPEND_BTEXT (_("\nAtmosphere\n"));
Colin Leroy's avatar
Colin Leroy committed
335 336
  APPEND_TEXT_ITEM (_("Pressure"), PRESSURE);
  APPEND_TEXT_ITEM (_("Humidity"), HUMIDITY);
Harald Judt's avatar
Harald Judt committed
337 338 339 340

  /* Clouds */
  APPEND_BTEXT (_("\nClouds\n"));
  APPEND_TEXT_ITEM (_("Fog"), FOG);
Colin Leroy's avatar
Colin Leroy committed
341 342 343
  APPEND_TEXT_ITEM (_("Low clouds"), CLOUDINESS_LOW);
  APPEND_TEXT_ITEM (_("Medium clouds"), CLOUDINESS_MED);
  APPEND_TEXT_ITEM (_("High clouds"), CLOUDINESS_HIGH);
344
  APPEND_TEXT_ITEM (_("Cloudiness"), CLOUDINESS_OVERALL);
345

Colin Leroy's avatar
Colin Leroy committed
346
  APPEND_BTEXT (_("\nData from The Norwegian Meteorological Institute\n"));
347
  APPEND_LINK_ITEM ("\t", _("Thanks to met.no"), "http://met.no/", ltag1);
348 349

  g_signal_connect(G_OBJECT(view), "motion-notify-event",
Harald Judt's avatar
Harald Judt committed
350
                   G_CALLBACK(view_motion_notify), view);
351
  g_signal_connect(G_OBJECT(view), "leave-notify-event",
Harald Judt's avatar
Harald Judt committed
352
                   G_CALLBACK(view_leave_notify), view);
Colin Leroy's avatar
Colin Leroy committed
353

354 355 356 357
  if (hand_cursor == NULL)
    hand_cursor = gdk_cursor_new(GDK_HAND2);
  if (text_cursor == NULL)
    text_cursor = gdk_cursor_new(GDK_XTERM);
358

359 360
  return frame;
}
361

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
GtkWidget *
add_forecast_cell(GtkWidget *widget, GdkColor *color) {
    GtkWidget *ebox;
    ebox = gtk_event_box_new();
    if (color == NULL)
        gtk_event_box_set_visible_window(GTK_EVENT_BOX(ebox), FALSE);
    else {
        gtk_event_box_set_visible_window(GTK_EVENT_BOX(ebox), TRUE);
        gtk_widget_modify_bg(GTK_WIDGET(ebox), GTK_STATE_NORMAL, color);
    }
    gtk_container_add(GTK_CONTAINER(ebox), GTK_WIDGET(widget));
    return ebox;
}

GtkWidget *
add_forecast_header(gchar *text, gdouble angle, GdkColor *color)
{
Harald Judt's avatar
Harald Judt committed
379
    GtkWidget *label, *align;
380 381
    gchar *str;

Harald Judt's avatar
Harald Judt committed
382 383 384 385 386
	if (angle)
		align = gtk_alignment_new(1, 1, 0, 1);
	else
		align = gtk_alignment_new(1, 1, 1, 0);
	gtk_container_set_border_width(GTK_CONTAINER(align), 4);
387 388

    label = gtk_label_new(NULL);
Harald Judt's avatar
Harald Judt committed
389
    gtk_label_set_angle(GTK_LABEL(label), angle);
390 391 392
    str = g_strdup_printf("<span foreground=\"white\"><b>%s</b></span>", text ? text : "");
    gtk_label_set_markup(GTK_LABEL(label), str);
    g_free(str);
Harald Judt's avatar
Harald Judt committed
393 394 395
    gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(label));

    return add_forecast_cell(align, color);
396 397
}

398
static GtkWidget *
399
make_forecast (xfceweather_data *data)
400
{
Harald Judt's avatar
Harald Judt committed
401 402
    GtkWidget *table, *ebox, *box, *align;
    GtkWidget *forecast_box, *label, *image;
403 404 405
    GdkPixbuf *icon;
    GdkColor lightbg = {0, 0xeaea, 0xeaea, 0xeaea};
    GdkColor darkbg = {0, 0x6666, 0x6666, 0x6666};
406
    gint i, weekday, daytime;
407
    gchar *dayname, *wind_speed, *value, *rawvalue;
408 409
    xml_time *fcdata;
    time_t now_t = time(NULL), fcday_t;
410
    struct tm fcday_tm;
411

412
    table = gtk_table_new(data->forecast_days + 1, 5, FALSE);
413 414 415 416 417 418
    gtk_table_set_row_spacings(GTK_TABLE(table), 0);
    gtk_table_set_col_spacings(GTK_TABLE(table), 0);

    /* empty upper left corner */
    box = gtk_vbox_new(FALSE, 0);
    gtk_table_attach_defaults(GTK_TABLE(table),
Harald Judt's avatar
Harald Judt committed
419
                              add_forecast_cell(box, &darkbg),
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
                              0, 1, 0, 1);

    /* daytime headers */
    gtk_table_attach_defaults(GTK_TABLE(table),
                              add_forecast_header(_("Morning"), 0.0, &darkbg),
                              1, 2, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(table),
                              add_forecast_header(_("Afternoon"), 0.0, &darkbg),
                              2, 3, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(table),
                              add_forecast_header(_("Evening"), 0.0, &darkbg),
                              3, 4, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(table),
                              add_forecast_header(_("Night"), 0.0, &darkbg),
                              4, 5, 0, 1);

436
    for (i = 0; i < data->forecast_days; i++) {
437
        /* Forecast day headers */
438 439
        fcday_tm = *localtime(&now_t);
        fcday_t = time_calc_day(fcday_tm, i);
440 441 442 443 444 445 446 447
        weekday = localtime(&fcday_t)->tm_wday;
        if (i == 0)
            dayname = _("Today");
        else if (i == 1)
            dayname = _("Tomorrow");
        else
            dayname = translate_day(weekday);

Harald Judt's avatar
Harald Judt committed
448
        ebox = add_forecast_header(dayname, 90.0, &darkbg);
449

Harald Judt's avatar
Harald Judt committed
450
        gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(ebox),
451 452 453 454 455
                                  0, 1, i+1, i+2);

        /* Get forecast data for each daytime */
        for (daytime = MORNING; daytime <= NIGHT; daytime++) {
            forecast_box = gtk_vbox_new(FALSE, 0);
Harald Judt's avatar
Harald Judt committed
456 457 458
            align = gtk_alignment_new(0.5, 0.5, 1, 1);
            gtk_container_set_border_width(GTK_CONTAINER(align), 4);
            gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(forecast_box));
459
            if (i % 2)
Harald Judt's avatar
Harald Judt committed
460
                ebox = add_forecast_cell(align, NULL);
461
            else
Harald Judt's avatar
Harald Judt committed
462
                ebox = add_forecast_cell(align, &lightbg);
463 464 465 466

            fcdata = make_forecast_data(data->weatherdata, i, daytime);
            if (fcdata != NULL) {
                if (fcdata->location != NULL) {
467
                    rawvalue = get_data(fcdata, data->unit_system, SYMBOL);
468 469
                    icon = get_icon(rawvalue, 48, (daytime == NIGHT));
                    g_free(rawvalue);
470 471 472 473 474 475
                    image = gtk_image_new_from_pixbuf(icon);
                    gtk_box_pack_start(GTK_BOX(forecast_box), GTK_WIDGET(image),
                                        TRUE, TRUE, 0);
                    if (G_LIKELY (icon))
                        g_object_unref (G_OBJECT (icon));

476
                    rawvalue = get_data(fcdata, data->unit_system, SYMBOL);
Harald Judt's avatar
Harald Judt committed
477
                    value = g_strdup_printf("%s",
478
                                            translate_desc(rawvalue,
479
                                                           (daytime == NIGHT)));
480
                    g_free(rawvalue);
481 482 483 484 485 486
                    label = gtk_label_new(NULL);
                    gtk_label_set_markup(GTK_LABEL(label), value);
                    gtk_box_pack_start(GTK_BOX(forecast_box), GTK_WIDGET(label),
                                        TRUE, TRUE, 0);
                    g_free(value);

487
                    rawvalue = get_data(fcdata, data->unit_system, TEMPERATURE);
Harald Judt's avatar
Harald Judt committed
488
                    value = g_strdup_printf("%s %s",
489
                                            rawvalue,
490
                                            get_unit(fcdata, data->unit_system, TEMPERATURE));
491
                    g_free(rawvalue);
492 493 494 495 496
                    label = gtk_label_new(value);
                    gtk_box_pack_start(GTK_BOX(forecast_box), GTK_WIDGET(label),
                                        TRUE, TRUE, 0);
                    g_free(value);

497 498
                    rawvalue = get_data(fcdata, data->unit_system, WIND_DIRECTION);
                    wind_speed = get_data(fcdata, data->unit_system, WIND_SPEED);
Harald Judt's avatar
Harald Judt committed
499
                    value = g_strdup_printf("%s %s %s",
500 501
                                            translate_wind_direction(rawvalue),
                                            wind_speed,
502
                                            get_unit(fcdata, data->unit_system, WIND_SPEED));
503 504
                    g_free(wind_speed);
                    g_free(rawvalue);
505 506 507 508 509 510 511
                    label = gtk_label_new(value);
                    gtk_box_pack_start(GTK_BOX(forecast_box), label, TRUE, TRUE, 0);
                    g_free(value);
                }
                xml_time_free(fcdata);
            }
            gtk_table_attach_defaults(GTK_TABLE(table),
Harald Judt's avatar
Harald Judt committed
512
                                      GTK_WIDGET(ebox),
513 514 515 516
                                      1+daytime, 2+daytime, i+1, i+2);
        }
    }
    return table;
517 518
}

519 520


521
static GtkWidget *
522
create_forecast_tab (xfceweather_data *data, GtkWidget *window)
523
{
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    GtkWidget   *align, *hbox, *scrolled, *table;
    GdkWindow   *win;
    GdkScreen   *screen;
    GdkRectangle rect;
    gint         monitor_num, height_needed, height_max;

    /* calculate maximum height we may use, subtracting some sane value for safety */
    screen = gtk_window_get_screen(GTK_WINDOW(window));
    win = GTK_WIDGET(window)->window;
    monitor_num = gdk_screen_get_monitor_at_window(GDK_SCREEN(screen), GDK_WINDOW(win));
    gdk_screen_get_monitor_geometry(GDK_SCREEN(screen), monitor_num, &rect);
    height_max = rect.height * 0.85 - 200;

    /* calculate height needed using a good arbitrary value */
    height_needed = data->forecast_days * 110;

    /* generate the forecast table */
    table = GTK_WIDGET(make_forecast(data));

    align = gtk_alignment_new(0.5, 0, 0.5, 0);
    if (height_needed < height_max) {
        gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(table));
        gtk_container_set_border_width(GTK_CONTAINER(align), BORDER);
        return align;
    } else {
        hbox = gtk_hbox_new(FALSE, 0);
        gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, FALSE, 0);
        gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(hbox));

        scrolled = gtk_scrolled_window_new (NULL, NULL);
        gtk_container_set_border_width(GTK_CONTAINER(scrolled), BORDER);

        gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), align);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
                                       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
        gtk_widget_set_size_request(GTK_WIDGET(scrolled), 550, height_max);
        return scrolled;
    }
562
}
563

564 565 566 567 568 569 570
static void
summary_dialog_response (GtkWidget          *dlg,
                         gint                response,
                         GtkWidget          *window)
{
	if (response == GTK_RESPONSE_ACCEPT)
		gtk_widget_destroy(window);
571
	else if (response == GTK_RESPONSE_HELP)
572 573 574
		g_spawn_command_line_async ("exo-open --launch WebBrowser " PLUGIN_WEBSITE, NULL);
}

575
GtkWidget *
576
create_summary_window (xfceweather_data *data)
577
{
Harald Judt's avatar
Harald Judt committed
578
  GtkWidget *window, *notebook, *vbox, *hbox, *label;
579
  gchar     *title, *rawvalue;
580
  GdkPixbuf *icon;
581
  xml_time  *conditions;
582

583 584 585
  window = xfce_titled_dialog_new_with_buttons (_("Weather Update"),
                                                NULL,
                                                GTK_DIALOG_NO_SEPARATOR,
586
                                                GTK_STOCK_ABOUT,
587 588
						GTK_RESPONSE_HELP,
                                                GTK_STOCK_CLOSE,
589
                                                GTK_RESPONSE_ACCEPT, NULL);
590 591
  if (data->location_name_short != NULL) {
    title = g_strdup_printf (_("Weather report for: %s"), data->location_name_short);
Harald Judt's avatar
Harald Judt committed
592 593 594
    xfce_titled_dialog_set_subtitle (XFCE_TITLED_DIALOG (window), title);
    g_free (title);
  }
595

596 597 598
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE,
                      0);
599

600
  conditions = get_current_conditions(data->weatherdata);
601

602
  rawvalue = get_data (conditions, data->unit_system, SYMBOL);
603 604
  icon = get_icon (rawvalue, 48, is_night_time());
  g_free (rawvalue);
605

606
  gtk_window_set_icon (GTK_WINDOW (window), icon);
607 608 609

  if (G_LIKELY (icon))
    g_object_unref (G_OBJECT (icon));
610

Harald Judt's avatar
Harald Judt committed
611 612 613 614 615 616 617 618 619 620 621
  if (data->location_name == NULL || data->weatherdata == NULL) {
    hbox = gtk_hbox_new (FALSE, 0);
    if (data->location_name == NULL)
      label = gtk_label_new(_("Please set a location in the plugin settings."));
    else
      label = gtk_label_new(_("Currently no data available."));
    gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET(label),
                        TRUE, TRUE, 0);

    gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET(hbox),
                        TRUE, TRUE, 0);
622
    gtk_window_set_default_size (GTK_WINDOW (window), 500, 400);
Harald Judt's avatar
Harald Judt committed
623 624 625 626
  } else {
    notebook = gtk_notebook_new ();
    gtk_container_set_border_width (GTK_CONTAINER (notebook), BORDER);
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
627
                              create_forecast_tab (data, window),
Harald Judt's avatar
Harald Judt committed
628 629 630 631 632 633 634
                              gtk_label_new (_("Forecast")));
    gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
                              create_summary_tab (data),
                              gtk_label_new (_("Details")));

    gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
  }
635

636
  g_signal_connect (G_OBJECT (window), "response",
637
                    G_CALLBACK (summary_dialog_response), window);
638

639
  return window;
640
}