weather-summary.c 40.9 KB
Newer Older
1
/*  Copyright (c) 2003-2013 Xfce Development Team
2
 *
3 4 5 6
 * 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.
7
 *
8 9 10 11
 * 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.
12
 *
13 14 15 16
 * 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.
17 18 19
 */

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

23
#include <libxfce4ui/libxfce4ui.h>
24

Nick Schermer's avatar
Nick Schermer committed
25 26 27 28 29 30
#include "weather-parsers.h"
#include "weather-data.h"
#include "weather.h"
#include "weather-summary.h"
#include "weather-translate.h"
#include "weather-icon.h"
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static gboolean
lnk_clicked(GtkTextTag *tag,
            GObject *obj,
            GdkEvent *event,
            GtkTextIter *iter,
            GtkWidget *textview);


#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);

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
 * TRANSLATORS: This format string belongs to the macro used for
 * printing the "Label: Value Unit" lines on the details tab, e.g.
 * "Temperature: 10 °C" or "Latitude: 95.7°".
 * The %s stand for:
 *   - label
 *   - ": " if label is not empty, else empty
 *   - value
 *   - space if unit is not degree "°" (but this is not °C or °F!)
 *   - unit
 * Usually, you should leave this unchanged, BUT...
 * RTL TRANSLATORS: In case you did not translate the measurement
 * unit, use LRM (left-to-right mark) etc. to align it properly with
 * its numeric value.
 */
Harald Judt's avatar
Harald Judt committed
66 67 68 69 70 71 72 73 74 75
#define APPEND_TEXT_ITEM(text, item)                        \
    rawvalue = get_data(conditions, data->units, item,      \
                        FALSE, data->night_time);           \
    unit = get_unit(data->units, item);                     \
    value = g_strdup_printf(_("\t%s%s%s%s%s\n"),            \
                            text, text ? ": " : "",         \
                            rawvalue,                       \
                            strcmp(unit, "°") ? " " : "",   \
                            unit);                          \
    g_free(rawvalue);                                       \
76 77 78 79 80 81 82 83 84
    APPEND_TEXT_ITEM_REAL(value);

#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);                            \
Harald Judt's avatar
Harald Judt committed
85 86
    g_object_set_data_full(G_OBJECT(lnk_tag), "url",                    \
                           g_strdup(url), g_free);                      \
87 88
    g_signal_connect(G_OBJECT(lnk_tag), "event",                        \
                     G_CALLBACK(lnk_clicked), NULL);
89

Harald Judt's avatar
Harald Judt committed
90 91 92 93 94 95 96 97 98 99 100 101
#define ATTACH_DAYTIME_HEADER(title, pos)               \
    if (data->forecast_layout == FC_LAYOUT_CALENDAR)    \
        gtk_table_attach_defaults                       \
            (GTK_TABLE(table),                          \
             add_forecast_header(title, 90.0, &darkbg), \
             0, 1, pos, pos+1);                         \
    else                                                \
        gtk_table_attach_defaults                       \
            (GTK_TABLE(table),                          \
             add_forecast_header(title, 0.0, &darkbg),  \
             pos, pos+1, 0, 1);                         \

102
#define APPEND_TOOLTIP_ITEM(description, item)                  \
103 104
    value = get_data(fcdata, data->units, item,                 \
                     data->round, data->night_time);            \
105 106 107 108 109 110 111
    if (strcmp(value, "")) {                                    \
        unit = get_unit(data->units, item);                     \
        g_string_append_printf(text, description, value,        \
                               strcmp(unit, "°") ? " " : "",    \
                               unit);                           \
    } else                                                      \
        g_string_append_printf(text, description, "-", "", ""); \
112 113
    g_free(value);

114

115 116 117 118 119 120
static gboolean
lnk_clicked(GtkTextTag *tag,
            GObject *obj,
            GdkEvent *event,
            GtkTextIter *iter,
            GtkWidget *textview)
121
{
122 123
    const gchar *url;
    gchar *str;
124

125 126 127 128 129 130 131 132 133 134
    if (event->type == GDK_BUTTON_RELEASE) {
        url = g_object_get_data(G_OBJECT(tag), "url");
        str = g_strdup_printf("exo-open --launch WebBrowser %s", url);
        g_spawn_command_line_async(str, NULL);
        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);
    return FALSE;
135 136
}

137

138
static gboolean
139
icon_clicked (GtkWidget *widget,
140
              GdkEventButton *event,
141
              gpointer user_data)
142
{
143
    return lnk_clicked(user_data, NULL, (GdkEvent *) (event), NULL, NULL);
144 145
}

146 147 148 149

static gboolean
view_motion_notify(GtkWidget *widget,
                   GdkEventMotion *event,
Harald Judt's avatar
Harald Judt committed
150
                   summary_details *sum)
151
{
Harald Judt's avatar
Harald Judt committed
152 153 154 155 156
    GtkTextIter iter;
    GtkTextTag *tag;
    GSList *tags;
    GSList *cur;
    gint bx, by;
157

Harald Judt's avatar
Harald Judt committed
158
    if (event->x != -1 && event->y != -1) {
Harald Judt's avatar
Harald Judt committed
159
        gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(sum->text_view),
160 161
                                              GTK_TEXT_WINDOW_WIDGET,
                                              event->x, event->y, &bx, &by);
Harald Judt's avatar
Harald Judt committed
162
        gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(sum->text_view),
163 164 165
                                           &iter, bx, by);
        tags = gtk_text_iter_get_tags(&iter);
        for (cur = tags; cur != NULL; cur = cur->next) {
Harald Judt's avatar
Harald Judt committed
166
            tag = cur->data;
167
            if (g_object_get_data(G_OBJECT(tag), "url")) {
Harald Judt's avatar
Harald Judt committed
168 169 170 171
                gdk_window_set_cursor
                    (gtk_text_view_get_window(GTK_TEXT_VIEW(sum->text_view),
                                              GTK_TEXT_WINDOW_TEXT),
                     sum->hand_cursor);
172 173 174
                return FALSE;
            }
        }
175
    }
Harald Judt's avatar
Harald Judt committed
176 177 178 179 180
    if (!sum->on_icon)
        gdk_window_set_cursor
            (gtk_text_view_get_window(GTK_TEXT_VIEW(sum->text_view),
                                      GTK_TEXT_WINDOW_TEXT),
             sum->text_cursor);
181
    return FALSE;
182 183
}

184 185 186 187

static gboolean
icon_motion_notify(GtkWidget *widget,
                   GdkEventMotion *event,
Harald Judt's avatar
Harald Judt committed
188
                   summary_details *sum)
189
{
Harald Judt's avatar
Harald Judt committed
190 191 192 193 194
    sum->on_icon = TRUE;
    gdk_window_set_cursor
        (gtk_text_view_get_window(GTK_TEXT_VIEW(sum->text_view),
                                  GTK_TEXT_WINDOW_TEXT),
         sum->hand_cursor);
195
    return FALSE;
196 197
}

198 199 200 201

static gboolean
view_leave_notify(GtkWidget *widget,
                  GdkEventMotion *event,
Harald Judt's avatar
Harald Judt committed
202
                  summary_details *sum)
203
{
Harald Judt's avatar
Harald Judt committed
204 205 206 207 208
    sum->on_icon = FALSE;
    gdk_window_set_cursor
        (gtk_text_view_get_window(GTK_TEXT_VIEW(sum->text_view),
                                  GTK_TEXT_WINDOW_TEXT),
         sum->text_cursor);
209
    return FALSE;
210 211
}

212 213 214

static void
view_scrolled_cb(GtkAdjustment *adj,
Harald Judt's avatar
Harald Judt committed
215
                 summary_details *sum)
216
{
Harald Judt's avatar
Harald Judt committed
217 218
    gint x, y, x1, y1;

Harald Judt's avatar
Harald Judt committed
219
    if (sum->icon_ebox) {
220 221 222 223 224 225 226 227 228 229 230
        /* TRANSLATORS: DO NOT TRANSLATE THIS STRING. This string is
           not visible to the user but controls the alignment of the
           met.no image on the details tab in the summary window,
           which is needed for RTL languages because the text is on
           the right side.
           If you are a RTL translator (hebrew, arabic), set this
           string to "RTL" to align the image to the left.
           If not, leave this string untouched or untranslated.
           Whatever you do, it should not have the value "RTL".
           If you know of a better way to determine LTR/RTL that makes
           this tweak unnecessary, please tell the developer.
231
        */
232
        if (!strcmp(_("LTR"), "RTL"))
233
            x1 = -30;
234 235
        else
            x1 = sum->text_view->allocation.width - 191 - 15;
Harald Judt's avatar
Harald Judt committed
236 237
        y1 = sum->text_view->requisition.height - 60 - 15;
        gtk_text_view_buffer_to_window_coords(GTK_TEXT_VIEW(sum->text_view),
238 239
                                              GTK_TEXT_WINDOW_TEXT,
                                              x1, y1, &x, &y);
Harald Judt's avatar
Harald Judt committed
240 241
        gtk_text_view_move_child(GTK_TEXT_VIEW(sum->text_view),
                                 sum->icon_ebox, x, y);
242
    }
243 244
}

245 246 247 248 249

static void
view_size_allocate_cb(GtkWidget *widget,
                      GtkAllocation *allocation,
                      gpointer data)
250
{
Harald Judt's avatar
Harald Judt committed
251
    view_scrolled_cb(NULL, data);
252
}
253

254 255 256

static gchar *
get_logo_path(void)
257
{
258 259 260 261 262 263 264
    gchar *cache_dir, *logo_path;

    cache_dir = get_cache_directory();
    logo_path = g_strconcat(cache_dir, G_DIR_SEPARATOR_S,
                            "weather_logo.gif", NULL);
    g_free(cache_dir);
    return logo_path;
265 266
}

267

268
static void
269 270 271
logo_fetched(SoupSession *session,
             SoupMessage *msg,
             gpointer user_data)
272
{
273
    if (msg && msg->response_body && msg->response_body->length > 0) {
274 275 276
        gchar *path = get_logo_path();
        GError *error = NULL;
        GdkPixbuf *pixbuf = NULL;
277 278
        if (!g_file_set_contents(path, msg->response_body->data,
                                 msg->response_body->length, &error)) {
279 280 281
            g_warning(_("Error downloading met.no logo image to %s, "
                        "reason: %s\n"), path,
                      error ? error->message : _("unknown"));
282 283 284 285 286 287 288 289 290 291 292
            g_error_free(error);
            g_free(path);
            return;
        }
        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);
        }
    }
293 294
}

295 296

static GtkWidget *
297
weather_summary_get_logo(plugin_data *data)
298
{
299
    GtkWidget *image = gtk_image_new();
Harald Judt's avatar
Harald Judt committed
300
    GdkPixbuf *pixbuf;
301 302 303 304 305
    gchar *path = get_logo_path();

    pixbuf = gdk_pixbuf_new_from_file(path, NULL);
    g_free(path);
    if (pixbuf == NULL)
306 307
        weather_http_queue_request(data->session,
                                   "http://met.no/filestore/met.no-logo.gif",
308
                                   logo_fetched, image);
309 310 311 312 313
    else {
        gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
        g_object_unref(pixbuf);
    }
    return image;
314
}
315

316

317
static GtkWidget *
318
create_summary_tab(plugin_data *data)
319
{
320 321
    GtkTextBuffer *buffer;
    GtkTextIter iter;
322
    GtkTextTag *btag, *ltag_img, *ltag_metno, *ltag_wiki, *ltag_geonames;
Harald Judt's avatar
Harald Judt committed
323
    GtkWidget *view, *frame, *scrolled, *icon;
324 325 326 327
    GtkAdjustment *adj;
    GdkColor lnk_color;
    xml_time *conditions;
    const gchar *unit;
Harald Judt's avatar
Harald Judt committed
328
    gchar *value, *rawvalue, *wind;
329 330
    gchar *last_download, *next_download;
    gchar *interval_start, *interval_end, *point;
331
    gchar *sunrise, *sunset, *moonrise, *moonset;
Harald Judt's avatar
Harald Judt committed
332
    summary_details *sum;
333

Harald Judt's avatar
Harald Judt committed
334 335 336 337 338 339 340
    sum = g_slice_new0(summary_details);
    sum->on_icon = FALSE;
    sum->hand_cursor = gdk_cursor_new(GDK_HAND2);
    sum->text_cursor = gdk_cursor_new(GDK_XTERM);
    data->summary_details = sum;

    sum->text_view = view = gtk_text_view_new();
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
    gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
    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);

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

365 366
    /* TRANSLATORS: Please use as many \t as appropriate to align the
       date/time values as in the original. */
367 368 369 370 371 372 373 374 375 376 377 378
    APPEND_BTEXT(_("\nDownloads\n"));
    last_download = format_date(data->weather_update->last, NULL, TRUE);
    next_download = format_date(data->weather_update->next, NULL, TRUE);
    value = g_strdup_printf(_("\tWeather data:\n"
                              "\tLast:\t%s\n"
                              "\tNext:\t%s\n"
                              "\tCurrent failed attempts: %d\n\n"),
                            last_download,
                            next_download,
                            data->weather_update->attempt);
    g_free(last_download);
    g_free(next_download);
379 380
    APPEND_TEXT_ITEM_REAL(value);

381 382 383 384 385 386 387 388 389 390 391 392 393 394
    last_download = format_date(data->astro_update->last, NULL, TRUE);
    next_download = format_date(data->astro_update->next, NULL, TRUE);
    value = g_strdup_printf(_("\tAstronomical data:\n"
                              "\tLast:\t%s\n"
                              "\tNext:\t%s\n"
                              "\tCurrent failed attempts: %d\n"),
                            last_download,
                            next_download,
                            data->astro_update->attempt);
    g_free(last_download);
    g_free(next_download);
    APPEND_TEXT_ITEM_REAL(value);

    /* calculation times */
395
    APPEND_BTEXT(_("\nTimes Used for Calculations\n"));
396
    point = format_date(conditions->point, NULL, TRUE);
397
    value = g_strdup_printf
Harald Judt's avatar
Harald Judt committed
398
        (_("\tTemperatures, wind, atmosphere and cloud data calculated\n"
399
           "\tfor:\t\t%s\n"),
400
         point);
401
    g_free(point);
402 403
    APPEND_TEXT_ITEM_REAL(value);

404 405
    interval_start = format_date(conditions->start, NULL, TRUE);
    interval_end = format_date(conditions->end, NULL, TRUE);
406
    value = g_strdup_printf
Harald Judt's avatar
Harald Judt committed
407
        (_("\n\tPrecipitation and the weather symbol have been calculated\n"
408
           "\tusing the following time interval:\n"
409 410
           "\tStart:\t%s\n"
           "\tEnd:\t%s\n"),
411 412
         interval_start,
         interval_end);
413 414
    g_free(interval_end);
    g_free(interval_start);
415 416
    APPEND_TEXT_ITEM_REAL(value);

417
    /* sun and moon */
418
    APPEND_BTEXT(_("\nAstronomical Data\n"));
419 420 421 422 423 424 425 426
    if (data->astrodata) {
        if (data->astrodata->sun_never_rises) {
            value = g_strdup(_("\tSunrise:\t\tThe sun never rises today.\n"));
            APPEND_TEXT_ITEM_REAL(value);
        } else if (data->astrodata->sun_never_sets) {
            value = g_strdup(_("\tSunset:\t\tThe sun never sets today.\n"));
            APPEND_TEXT_ITEM_REAL(value);
        } else {
427
            sunrise = format_date(data->astrodata->sunrise, NULL, TRUE);
428
            value = g_strdup_printf(_("\tSunrise:\t\t%s\n"), sunrise);
429
            g_free(sunrise);
430 431
            APPEND_TEXT_ITEM_REAL(value);

432
            sunset = format_date(data->astrodata->sunset, NULL, TRUE);
433
            value = g_strdup_printf(_("\tSunset:\t\t%s\n\n"), sunset);
434
            g_free(sunset);
435 436 437 438 439 440 441 442 443 444 445 446
            APPEND_TEXT_ITEM_REAL(value);
        }

        if (data->astrodata->moon_phase)
            value = g_strdup_printf(_("\tMoon phase:\t%s\n"),
                                    translate_moon_phase
                                    (data->astrodata->moon_phase));
        else
            value = g_strdup(_("\tMoon phase:\tUnknown\n"));
        APPEND_TEXT_ITEM_REAL(value);

        if (data->astrodata->moon_never_rises) {
447 448
            value =
                g_strdup(_("\tMoonrise:\tThe moon never rises today.\n"));
449 450
            APPEND_TEXT_ITEM_REAL(value);
        } else if (data->astrodata->moon_never_sets) {
451 452
            value =
                g_strdup(_("\tMoonset:\tThe moon never sets today.\n"));
453 454
            APPEND_TEXT_ITEM_REAL(value);
        } else {
455
            moonrise = format_date(data->astrodata->moonrise, NULL, TRUE);
456
            value = g_strdup_printf(_("\tMoonrise:\t%s\n"), moonrise);
457
            g_free(moonrise);
458 459
            APPEND_TEXT_ITEM_REAL(value);

460
            moonset = format_date(data->astrodata->moonset, NULL, TRUE);
461
            value = g_strdup_printf(_("\tMoonset:\t%s\n"), moonset);
462
            g_free(moonset);
463 464 465 466 467 468 469 470
            APPEND_TEXT_ITEM_REAL(value);
        }
    } else {
        value = g_strdup(_("\tData not available, will use sane "
                           "default values for night and day.\n"));
        APPEND_TEXT_ITEM_REAL(value);
    }

Harald Judt's avatar
Harald Judt committed
471 472
    /* temperatures */
    APPEND_BTEXT(_("\nTemperatures\n"));
473
    APPEND_TEXT_ITEM(_("Temperature"), TEMPERATURE);
Harald Judt's avatar
Harald Judt committed
474
    APPEND_TEXT_ITEM(_("Dew point"), DEWPOINT);
475
    APPEND_TEXT_ITEM(_("Apparent temperature"), APPARENT_TEMPERATURE);
476 477 478

    /* wind */
    APPEND_BTEXT(_("\nWind\n"));
479 480 481 482
    wind = get_data(conditions, data->units, WIND_SPEED,
                    FALSE, data->night_time);
    rawvalue = get_data(conditions, data->units, WIND_BEAUFORT,
                        FALSE, data->night_time);
483 484
    value = g_strdup_printf(_("\tSpeed: %s %s (%s on the Beaufort scale)\n"),
                            wind, get_unit(data->units, WIND_SPEED),
485
                            rawvalue);
486 487 488 489
    g_free(rawvalue);
    g_free(wind);
    APPEND_TEXT_ITEM_REAL(value);

490 491
    rawvalue = get_data(conditions, data->units, WIND_DIRECTION,
                        FALSE, data->night_time);
492 493
    wind = translate_wind_direction(rawvalue);
    g_free(rawvalue);
494 495
    rawvalue = get_data(conditions, data->units, WIND_DIRECTION_DEG,
                        FALSE, data->night_time);
496 497 498

    /* wind direction */
    value = g_strdup_printf(_("\tDirection: %s (%s%s)\n"),
499
                            wind, rawvalue,
500
                            get_unit(data->units, WIND_DIRECTION_DEG));
501 502 503 504 505 506 507 508 509 510
    g_free(rawvalue);
    g_free(wind);
    APPEND_TEXT_ITEM_REAL(value);

    /* precipitation */
    APPEND_BTEXT(_("\nPrecipitations\n"));
    APPEND_TEXT_ITEM(_("Precipitations amount"), PRECIPITATIONS);

    /* atmosphere */
    APPEND_BTEXT(_("\nAtmosphere\n"));
511 512
    APPEND_TEXT_ITEM(_("Barometric pressure"), PRESSURE);
    APPEND_TEXT_ITEM(_("Relative humidity"), HUMIDITY);
513 514 515 516 517

    /* clouds */
    APPEND_BTEXT(_("\nClouds\n"));
    APPEND_TEXT_ITEM(_("Fog"), FOG);
    APPEND_TEXT_ITEM(_("Low clouds"), CLOUDS_LOW);
518
    APPEND_TEXT_ITEM(_("Middle clouds"), CLOUDS_MID);
519 520 521
    APPEND_TEXT_ITEM(_("High clouds"), CLOUDS_HIGH);
    APPEND_TEXT_ITEM(_("Cloudiness"), CLOUDINESS);

Harald Judt's avatar
Harald Judt committed
522 523
    /* credits */
    gdk_color_parse("#0000ff", &lnk_color);
524 525 526 527 528 529 530 531 532
    ltag_img = gtk_text_buffer_create_tag(buffer, "lnk0", "foreground-gdk",
                                          &lnk_color, NULL);
    ltag_metno = gtk_text_buffer_create_tag(buffer, "lnk1", "foreground-gdk",
                                            &lnk_color, NULL);
    ltag_wiki = gtk_text_buffer_create_tag(buffer, "lnk2", "foreground-gdk",
                                           &lnk_color, NULL);
    ltag_geonames = gtk_text_buffer_create_tag(buffer, "lnk3",
                                               "foreground-gdk",
                                               &lnk_color, NULL);
Harald Judt's avatar
Harald Judt committed
533 534
    APPEND_BTEXT(_("\nCredits\n"));
    APPEND_LINK_ITEM(_("\tEncyclopedic information partly taken from\n\t\t"),
535 536 537 538
                     _("Wikipedia"), "http://wikipedia.org", ltag_wiki);
    APPEND_LINK_ITEM(_("\n\tElevation and timezone data provided by\n\t\t"),
                     _("GeoNames"),
                     "http://geonames.org/", ltag_geonames);
Harald Judt's avatar
Harald Judt committed
539 540
    APPEND_LINK_ITEM(_("\n\tWeather and astronomical data from\n\t\t"),
                     _("The Norwegian Meteorological Institute"),
541 542
                     "http://met.no/", ltag_metno);
    g_object_set_data_full(G_OBJECT(ltag_img), "url",   /* url for image */
Harald Judt's avatar
Harald Judt committed
543
                           g_strdup("http://met.no"), g_free);
544 545

    g_signal_connect(G_OBJECT(view), "motion-notify-event",
Harald Judt's avatar
Harald Judt committed
546
                     G_CALLBACK(view_motion_notify), sum);
547
    g_signal_connect(G_OBJECT(view), "leave-notify-event",
Harald Judt's avatar
Harald Judt committed
548
                     G_CALLBACK(view_leave_notify), sum);
549

Harald Judt's avatar
Harald Judt committed
550
    icon = weather_summary_get_logo(data);
551

Harald Judt's avatar
Harald Judt committed
552 553
    if (icon) {
        sum->icon_ebox = gtk_event_box_new();
554 555
        gtk_event_box_set_visible_window(GTK_EVENT_BOX(sum->icon_ebox), FALSE);
        gtk_container_add(GTK_CONTAINER(sum->icon_ebox), icon);
556
        gtk_text_view_add_child_in_window(GTK_TEXT_VIEW(view),
Harald Judt's avatar
Harald Judt committed
557
                                          sum->icon_ebox,
558
                                          GTK_TEXT_WINDOW_TEXT, 0, 0);
Harald Judt's avatar
Harald Judt committed
559
        gtk_widget_show_all(sum->icon_ebox);
560 561 562
        adj =
            gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolled));
        g_signal_connect(G_OBJECT(adj), "value-changed",
Harald Judt's avatar
Harald Judt committed
563
                         G_CALLBACK(view_scrolled_cb), sum);
564
        g_signal_connect(G_OBJECT(view), "size_allocate",
Harald Judt's avatar
Harald Judt committed
565 566
                         G_CALLBACK(view_size_allocate_cb), sum);
        g_signal_connect(G_OBJECT(sum->icon_ebox), "button-release-event",
567
                         G_CALLBACK(icon_clicked), ltag_img);
Harald Judt's avatar
Harald Judt committed
568 569 570 571 572 573
        g_signal_connect(G_OBJECT(sum->icon_ebox), "enter-notify-event",
                         G_CALLBACK(icon_motion_notify), sum);
        g_signal_connect(G_OBJECT(sum->icon_ebox), "motion-notify-event",
                         G_CALLBACK(icon_motion_notify), sum);
        g_signal_connect(G_OBJECT(sum->icon_ebox), "leave-notify-event",
                         G_CALLBACK(view_leave_notify), sum);
574
    }
Harald Judt's avatar
Harald Judt committed
575

576
    return frame;
577
}
578

579

Harald Judt's avatar
Harald Judt committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
static gchar *
get_dayname(gint day)
{
    struct tm fcday_tm;
    time_t now_t = time(NULL), fcday_t;
    gint weekday;

    fcday_tm = *localtime(&now_t);
    fcday_t = time_calc_day(fcday_tm, day);
    weekday = localtime(&fcday_t)->tm_wday;
    switch (day) {
    case 0:
        return g_strdup_printf(_("Today"));
    case 1:
        return g_strdup_printf(_("Tomorrow"));
    default:
        return translate_day(weekday);
    }
}


601 602 603 604 605 606 607 608
static gchar *
forecast_cell_get_tooltip_text(plugin_data *data,
                               xml_time *fcdata)
{
    GString *text;
    gchar *result, *value;
    const gchar *unit;

609 610 611 612
    /* TRANSLATORS: Please use spaces as needed or desired to properly
       align the values; Monospace font is enforced with <tt> tags for
       alignment, and the text is enclosed in <small> tags because
       that looks much better and saves space.
Harald Judt's avatar
Harald Judt committed
613
    */
614
    text = g_string_new(_("<b>Times used for calculations</b>\n"));
615
    value = format_date(fcdata->start, NULL, TRUE);
616 617 618 619
    g_string_append_printf(text, _("<tt><small>"
                                   "Interval start:       %s"
                                   "</small></tt>\n"),
                           value);
620
    g_free(value);
621
    value = format_date(fcdata->end, NULL, TRUE);
622 623 624 625
    g_string_append_printf(text, _("<tt><small>"
                                   "Interval end:         %s"
                                   "</small></tt>\n"),
                           value);
626
    g_free(value);
627
    value = format_date(fcdata->point, NULL, TRUE);
628 629 630 631
    g_string_append_printf(text, _("<tt><small>"
                                   "Data calculated for:  %s"
                                   "</small></tt>\n\n"),
                           value);
632 633
    g_free(value);

Harald Judt's avatar
Harald Judt committed
634
    g_string_append(text, _("<b>Temperatures</b>\n"));
635 636 637 638 639 640 641
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
                          "Dew point:            %s%s%s"
                          "</small></tt>\n"),
                        DEWPOINT);
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
                          "Apparent temperature: %s%s%s"
                          "</small></tt>\n\n"),
642
                        APPARENT_TEMPERATURE);
Harald Judt's avatar
Harald Judt committed
643

644
    g_string_append(text, _("<b>Atmosphere</b>\n"));
645 646 647 648 649 650 651 652
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
                          "Barometric pressure:  %s%s%s"
                          "</small></tt>\n"),
                        PRESSURE);
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
                          "Relative humidity:    %s%s%s"
                          "</small></tt>\n\n"),
                        HUMIDITY);
653 654

    g_string_append(text, _("<b>Precipitations</b>\n"));
655 656 657 658
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
                          "Amount:        %s%s%s"
                          "</small></tt>\n\n"),
                        PRECIPITATIONS);
659 660

    g_string_append(text, _("<b>Clouds</b>\n"));
661 662 663
    /* TRANSLATORS: Clouds percentages are aligned to the right in the
       tooltip, the %5s are needed for that and are used both for
       rounded and unrounded values. */
664
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
665
                          "Fog:           %5s%s%s"
666 667
                          "</small></tt>\n"), FOG);
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
668
                          "Low clouds:    %5s%s%s"
669 670
                          "</small></tt>\n"), CLOUDS_LOW);
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
671
                          "Middle clouds: %5s%s%s"
672 673
                          "</small></tt>\n"), CLOUDS_MID);
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
674
                          "High clouds:   %5s%s%s"
675 676
                          "</small></tt>\n"), CLOUDS_HIGH);
    APPEND_TOOLTIP_ITEM(_("<tt><small>"
677
                          "Cloudiness:    %5s%s%s"
678
                          "</small></tt>"), CLOUDINESS);
679 680 681 682 683 684 685 686

    /* Free GString only and return its character data */
    result = text->str;
    g_string_free(text, FALSE);
    return result;
}


687
static GtkWidget *
Harald Judt's avatar
Harald Judt committed
688 689
wrap_forecast_cell(const GtkWidget *widget,
                   const GdkColor *color)
690
{
691
    GtkWidget *ebox;
Harald Judt's avatar
Harald Judt committed
692

693 694 695 696 697 698 699 700 701 702 703
    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;
}

704

705
static GtkWidget *
706 707 708
add_forecast_header(const gchar *text,
                    const gdouble angle,
                    const GdkColor *color)
709
{
Harald Judt's avatar
Harald Judt committed
710
    GtkWidget *label, *align;
711 712
    gchar *str;

713 714 715 716 717
    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);
718 719

    label = gtk_label_new(NULL);
Harald Judt's avatar
Harald Judt committed
720
    gtk_label_set_angle(GTK_LABEL(label), angle);
721 722 723
    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
724
    gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(label));
Harald Judt's avatar
Harald Judt committed
725
    return wrap_forecast_cell(align, color);
726 727
}

728

729
static GtkWidget *
730
add_forecast_cell(plugin_data *data,
731
                  GArray *daydata,
Harald Judt's avatar
Harald Judt committed
732 733
                  gint day,
                  gint daytime)
734
{
Harald Judt's avatar
Harald Judt committed
735
    GtkWidget *box, *label, *image;
736
    GdkPixbuf *icon;
737
    const GdkColor black = {0, 0x0000, 0x0000, 0x0000};
Harald Judt's avatar
Harald Judt committed
738 739 740 741 742
    gchar *wind_speed, *wind_direction, *value, *rawvalue;
    xml_time *fcdata;

    box = gtk_vbox_new(FALSE, 0);

743
    fcdata = make_forecast_data(data->weatherdata, daydata, day, daytime);
Harald Judt's avatar
Harald Judt committed
744 745 746 747 748 749 750 751 752
    if (fcdata == NULL)
        return box;

    if (fcdata->location == NULL) {
        xml_time_free(fcdata);
        return box;
    }

    /* symbol */
753 754
    rawvalue = get_data(fcdata, data->units, SYMBOL,
                        FALSE, data->night_time);
Harald Judt's avatar
Harald Judt committed
755 756 757 758 759 760 761 762
    icon = get_icon(data->icon_theme, rawvalue, 48, (daytime == NIGHT));
    g_free(rawvalue);
    image = gtk_image_new_from_pixbuf(icon);
    gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(image), TRUE, TRUE, 0);
    if (G_LIKELY(icon))
        g_object_unref(G_OBJECT(icon));

    /* symbol description */
763 764
    rawvalue = get_data(fcdata, data->units, SYMBOL,
                        FALSE, data->night_time);
Harald Judt's avatar
Harald Judt committed
765 766 767 768 769 770 771 772 773 774 775
    value = g_strdup_printf("%s",
                            translate_desc(rawvalue, (daytime == NIGHT)));
    g_free(rawvalue);
    label = gtk_label_new(NULL);
    gtk_label_set_markup(GTK_LABEL(label), value);
    if (!(day % 2))
        gtk_widget_modify_fg(GTK_WIDGET(label), GTK_STATE_NORMAL, &black);
    gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(label), TRUE, TRUE, 0);
    g_free(value);

    /* temperature */
776 777
    rawvalue = get_data(fcdata, data->units, TEMPERATURE,
                        data->round, data->night_time);
Harald Judt's avatar
Harald Judt committed
778 779 780 781 782 783 784 785 786 787
    value = g_strdup_printf("%s %s", rawvalue,
                            get_unit(data->units, TEMPERATURE));
    g_free(rawvalue);
    label = gtk_label_new(value);
    if (!(day % 2))
        gtk_widget_modify_fg(GTK_WIDGET(label), GTK_STATE_NORMAL, &black);
    gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(label), TRUE, TRUE, 0);
    g_free(value);

    /* wind direction and speed */
788 789
    rawvalue = get_data(fcdata, data->units, WIND_DIRECTION,
                        FALSE, data->night_time);
Harald Judt's avatar
Harald Judt committed
790
    wind_direction = translate_wind_direction(rawvalue);
791 792
    wind_speed = get_data(fcdata, data->units, WIND_SPEED,
                          data->round, data->night_time);
Harald Judt's avatar
Harald Judt committed
793 794 795 796 797 798 799 800 801 802 803 804 805
    value = g_strdup_printf("%s %s %s", wind_direction, wind_speed,
                            get_unit(data->units, WIND_SPEED));
    g_free(wind_speed);
    g_free(wind_direction);
    g_free(rawvalue);
    label = gtk_label_new(value);
    if (!(day % 2))
        gtk_widget_modify_fg(GTK_WIDGET(label), GTK_STATE_NORMAL, &black);
    gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
    g_free(value);

    gtk_widget_set_size_request(GTK_WIDGET(box), 150, -1);

806 807 808 809
    value = forecast_cell_get_tooltip_text(data, fcdata);
    gtk_widget_set_tooltip_markup(GTK_WIDGET(box), value);
    g_free(value);

Harald Judt's avatar
Harald Judt committed
810 811 812 813 814 815
    xml_time_free(fcdata);
    return box;
}


static GtkWidget *
816
make_forecast(plugin_data *data)
Harald Judt's avatar
Harald Judt committed
817 818 819
{
    GtkWidget *table, *ebox, *box, *align;
    GtkWidget *forecast_box;
820 821
    const GdkColor lightbg = {0, 0xeaea, 0xeaea, 0xeaea};
    const GdkColor darkbg = {0, 0x6666, 0x6666, 0x6666};
822
    GArray *daydata;
Harald Judt's avatar
Harald Judt committed
823
    gchar *dayname;
Harald Judt's avatar
Harald Judt committed
824 825 826 827 828 829 830
    gint i;
    daytime daytime;

    if (data->forecast_layout == FC_LAYOUT_CALENDAR)
        table = gtk_table_new(5, data->forecast_days + 1, FALSE);
    else
        table = gtk_table_new(data->forecast_days + 1, 5, FALSE);
831 832 833 834 835 836 837

    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
838
                              wrap_forecast_cell(box, &darkbg),
839 840 841
                              0, 1, 0, 1);

    /* daytime headers */
Harald Judt's avatar
Harald Judt committed
842 843 844 845
    ATTACH_DAYTIME_HEADER(_("Morning"), 1);
    ATTACH_DAYTIME_HEADER(_("Afternoon"), 2);
    ATTACH_DAYTIME_HEADER(_("Evening"), 3);
    ATTACH_DAYTIME_HEADER(_("Night"), 4);
846

847
    for (i = 0; i < data->forecast_days; i++) {
848
        /* forecast day headers */
Harald Judt's avatar
Harald Judt committed
849 850 851
        dayname = get_dayname(i);
        if (data->forecast_layout == FC_LAYOUT_CALENDAR)
            ebox = add_forecast_header(dayname, 0.0, &darkbg);
852
        else
Harald Judt's avatar
Harald Judt committed
853
            ebox = add_forecast_header(dayname, 90.0, &darkbg);
Harald Judt's avatar
Harald Judt committed
854
        g_free(dayname);
855

Harald Judt's avatar
Harald Judt committed
856 857 858 859 860 861
        if (data->forecast_layout == FC_LAYOUT_CALENDAR)
            gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(ebox),
                                      i+1, i+2, 0, 1);
        else
            gtk_table_attach_defaults(GTK_TABLE(table), GTK_WIDGET(ebox),
                                      0, 1, i+1, i+2);
862

863 864 865
        /* to speed up things, first get forecast data for all daytimes */
        daydata = get_point_data_for_day(data->weatherdata, i);

866
        /* get forecast data for each daytime */
867
        for (daytime = MORNING; daytime <= NIGHT; daytime++) {
868
            forecast_box = add_forecast_cell(data, daydata, i, daytime);
Harald Judt's avatar
Harald Judt committed
869 870 871
            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));
872
            if (i % 2)
Harald Judt's avatar
Harald Judt committed
873
                ebox = wrap_forecast_cell(align, NULL);
874
            else
Harald Judt's avatar
Harald Judt committed
875 876
                ebox = wrap_forecast_cell(align, &lightbg);

Harald Judt's avatar
Harald Judt committed
877 878 879 880 881 882 883 884
            if (data->forecast_layout == FC_LAYOUT_CALENDAR)
                gtk_table_attach_defaults(GTK_TABLE(table),
                                          GTK_WIDGET(ebox),
                                          i+1, i+2, 1+daytime, 2+daytime);
            else
                gtk_table_attach_defaults(GTK_TABLE(table),
                                          GTK_WIDGET(ebox),
                                          1+daytime, 2+daytime, i+1, i+2);
885
        }
886
        g_array_free(daydata, FALSE);
887 888
    }
    return table;
889 890
}

891

892
static GtkWidget *
893
create_forecast_tab(plugin_data *data)
894
{
895
    GtkWidget *ebox, *align, *hbox, *scrolled, *table;
896
    GdkWindow *window;
897
    GdkScreen *screen;
898
    GdkRectangle rect;
Harald Judt's avatar
Harald Judt committed
899 900
    gint monitor_num = 0, h_need, h_max, height;
    gint w_need, w_max, width;
901

902 903 904 905 906 907 908 909 910 911
    /* To avoid causing a GDK assertion, determine the monitor
     * geometry using the weather icon window, which has already been
     * realized in contrast to the summary window. Then calculate the
     * maximum height we may use, subtracting some sane value just to
     * be on the safe side. */
    window = GDK_WINDOW(gtk_widget_get_window(GTK_WIDGET(data->iconimage)));
    screen = GDK_SCREEN(gdk_window_get_screen(window));
    if (G_LIKELY(window && screen))
        monitor_num = gdk_screen_get_monitor_at_window(screen, window);
    gdk_screen_get_monitor_geometry(screen, monitor_num, &rect);
912

Harald Judt's avatar
Harald Judt committed
913 914 915
    /* calculate maximum width and height */
    h_max = rect.height - 250;
    w_max = rect.width - 50;
916

Harald Judt's avatar
Harald Judt committed
917 918 919 920 921 922 923 924
    /* calculate needed space using a good arbitrary value */
    if (data->forecast_layout == FC_LAYOUT_CALENDAR) {
        w_need = ((data->forecast_days < 8) ? data->forecast_days : 7) * 142;
        h_need = 500;
    } else {
        w_need = (rect.width <= 720) ? 650 : 700;
        h_need = data->forecast_days * 110;
    }
925 926 927 928

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

Harald Judt's avatar
Harald Judt committed
929
    /* generate the containing widgets */
930
    align = gtk_alignment_new(0.5, 0, 0.5, 0);
Harald Judt's avatar
Harald Judt committed
931 932 933 934
    if ((data->forecast_layout == FC_LAYOUT_CALENDAR &&
         w_need < w_max && data->forecast_days < 8) ||
        (data->forecast_layout == FC_LAYOUT_LIST && h_need < h_max)) {
        /* no scroll window needed, just align the contents */
935 936 937 938
        gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(table));
        gtk_container_set_border_width(GTK_CONTAINER(align), BORDER);
        return align;
    } else {
Harald Judt's avatar
Harald Judt committed
939
        /* contents too big, scroll window needed */
940 941 942 943 944 945 946
        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);

947 948
        gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled),
                                              align);
949
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
950 951
                                       GTK_POLICY_AUTOMATIC,
                                       GTK_POLICY_AUTOMATIC);
Harald Judt's avatar
Harald Judt committed
952 953 954 955 956 957

        /* set scroll window size */
        width = (w_need > w_max) ? w_max : w_need;
        height = (h_need > h_max) ? h_max : h_need;
        gtk_widget_set_size_request(GTK_WIDGET(scrolled), width, height);

958
        ebox = gtk_event_box_new();
959
        gtk_event_box_set_visible_window(GTK_EVENT_BOX(ebox), TRUE);
960 961
        gtk_container_add(GTK_CONTAINER(ebox), GTK_WIDGET(scrolled));
        return ebox;
962
    }
963
}
964

965

966
static void
967 968
summary_dialog_response(const GtkWidget *dlg,
                        const gint response,
969
                        GtkWidget *window)
970
{
971 972
    if (response == GTK_RESPONSE_ACCEPT)
        gtk_widget_destroy(window);
973 974
}

975

976 977 978 979 980 981 982 983 984 985 986 987
static void
cb_notebook_page_switched(GtkNotebook *notebook,
                          GtkNotebookPage *page,
                          guint page_num,
                          gpointer user_data)
{
    plugin_data *data = (plugin_data *) user_data;

    data->summary_remember_tab = page_num;
}


988 989 990 991 992
gboolean
update_summary_subtitle(plugin_data *data)
{
    time_t now_t;
    GTimeVal now;
993
    gchar *title, *date, *date_format;
994 995 996 997
    guint update_interval;
    gint64 now_ms;

    if (data->summary_update_timer) {
998
        g_source_remove(data->summary_update_timer);
999 1000 1001 1002 1003 1004 1005 1006
        data->summary_update_timer = 0;
    }

    if (G_UNLIKELY(data->location_name == NULL) ||
        G_UNLIKELY(data->summary_window == NULL))
        return FALSE;

    time(&now_t);