weather-summary.c 14.2 KB
Newer Older
1 2 3 4 5
/*  $Id$
 *
 *  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 22 23
#endif

#include <libxfcegui4/libxfcegui4.h>

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

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

32
#define BORDER 8
33 34

#define APPEND_BTEXT(text) gtk_text_buffer_insert_with_tags(GTK_TEXT_BUFFER(buffer),\
Nick Schermer's avatar
Nick Schermer committed
35
        &iter, text, -1, btag, NULL);
36 37 38 39

#define APPEND_TEXT_ITEM_REAL(text) gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffer), &iter, text, -1);

#define APPEND_TEXT_ITEM(text, item) value = g_strdup_printf("\t%s: %s %s\n",\
Nick Schermer's avatar
Nick Schermer committed
40 41
        text, get_data(data, item), get_unit(unit, item));\
        APPEND_TEXT_ITEM_REAL(value); g_free(value);
42

43 44
static GtkTooltips *tooltips = NULL;

45
static GtkWidget *
46
create_summary_tab (xml_weather * data, units unit)
47
{
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 110 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  GtkTextBuffer *buffer;
  GtkTextIter iter;
  GtkTextTag *btag;
  gchar *value, *date, *wind, *sun_val, *vis;
  GtkWidget *view, *frame, *scrolled;

  view = gtk_text_view_new ();
  gtk_text_view_set_editable (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);


  value =
    g_strdup_printf (_("Weather report for: %s.\n\n"), get_data (data, DNAM));
  APPEND_BTEXT (value);
  g_free (value);

  date = translate_lsup (get_data (data, LSUP));
  value =
    g_strdup_printf (_
                     ("Observation station located in %s\nLast update: %s.\n"),
                     get_data (data, OBST), date ? date : get_data (data,
                                                                    LSUP));
  APPEND_TEXT_ITEM_REAL (value);

  if (date)
    g_free (date);

  g_free (value);

  APPEND_BTEXT (_("\nTemperature\n"));
  APPEND_TEXT_ITEM (_("Temperature"), TEMP);
  APPEND_TEXT_ITEM (_("Windchill"), FLIK);

  /* special case for TRANS because of translate_desc */
  value = g_strdup_printf ("\t%s: %s\n",
                           _("Description"),
                           translate_desc (get_data (data, TRANS)));
  APPEND_TEXT_ITEM_REAL (value);
  g_free (value);

  APPEND_TEXT_ITEM (_("Dew point"), DEWP);

  APPEND_BTEXT (_("\nWind\n"));
  wind = translate_wind_speed (get_data (data, WIND_SPEED), unit);
  value = g_strdup_printf ("\t%s: %s\n", _("Speed"), wind);
  APPEND_TEXT_ITEM_REAL (value);
  g_free (wind);
  g_free (value);

  wind = translate_wind_direction (get_data (data, WIND_DIRECTION));
  value = g_strdup_printf ("\t%s: %s\n",
                           _("Direction"), wind ? wind : get_data (data,
                                                                   WIND_DIRECTION));
  if (wind)
    g_free (wind);
  APPEND_TEXT_ITEM_REAL (value);
  g_free (value);

  wind = translate_wind_speed (get_data (data, WIND_GUST), unit);
  value = g_strdup_printf ("\t%s: %s\n", _("Gusts"), wind);
  APPEND_TEXT_ITEM_REAL (value);
  g_free (wind);
  g_free (value);

  APPEND_BTEXT (_("\nUV\n"));
  APPEND_TEXT_ITEM (_("Index"), UV_INDEX);
  value = g_strdup_printf ("\t%s: %s\n",
                           _("Risk"),
                           translate_risk (get_data (data, UV_TRANS)));
  APPEND_TEXT_ITEM_REAL (value);
  g_free (value);

  APPEND_BTEXT (_("\nAtmospheric pressure\n"));
  APPEND_TEXT_ITEM (_("Pressure"), BAR_R);

  value = g_strdup_printf ("\t%s: %s\n",
                           _("State"),
                           translate_bard (get_data (data, BAR_D)));
  APPEND_TEXT_ITEM_REAL (value);
  g_free (value);

  APPEND_BTEXT (_("\nSun\n"));

  sun_val = translate_time (get_data (data, SUNR));
  value = g_strdup_printf ("\t%s: %s\n",
                           _("Rise"), sun_val ? sun_val : get_data (data,
                                                                    SUNR));
  if (sun_val)
    g_free (sun_val);
  APPEND_TEXT_ITEM_REAL (value);
  g_free (value);

  sun_val = translate_time (get_data (data, SUNS));
  value = g_strdup_printf ("\t%s: %s\n",
                           _("Set"), sun_val ? sun_val : get_data (data,
                                                                   SUNS));
  if (sun_val)
    g_free (sun_val);
  APPEND_TEXT_ITEM_REAL (value);
  g_free (value);

  APPEND_BTEXT (_("\nOther\n"));
  APPEND_TEXT_ITEM (_("Humidity"), HMID);
  vis = translate_visibility (get_data (data, VIS), unit);
  value = g_strdup_printf ("\t%s: %s\n", _("Visibility"), vis);
  APPEND_TEXT_ITEM_REAL (value);
  g_free (vis);
  g_free (value);

  return frame;
173 174
}

175
static GtkWidget *
176
make_forecast (xml_dayf * weatherdata, units unit)
177
{
178 179 180 181 182 183 184 185 186 187 188 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  GtkWidget *item_vbox, *temp_hbox, *icon_hbox, *label, *icon_d, *icon_n,
    *box_d, *box_n;
  GdkPixbuf *icon;
  gchar *str, *day, *wind;

  DBG ("this day %s", weatherdata->day);

  item_vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (item_vbox), 6);


  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);

  day = translate_day (get_data_f (weatherdata, WDAY));

  str =
    g_strdup_printf ("<b>%s</b>", day ? day : get_data_f (weatherdata, WDAY));
  if (day)
    g_free (day);
  gtk_label_set_markup (GTK_LABEL (label), str);
  g_free (str);
  gtk_box_pack_start (GTK_BOX (item_vbox), label, FALSE, FALSE, 0);

  icon_hbox = gtk_hbox_new (FALSE, 0);

  icon = get_icon (get_data_f (weatherdata, ICON_D), GTK_ICON_SIZE_DIALOG);
  icon_d = gtk_image_new_from_pixbuf (icon);
  g_object_unref (G_OBJECT (icon));
  box_d = gtk_event_box_new ();
  gtk_container_add (GTK_CONTAINER (box_d), icon_d);

  icon = get_icon (get_data_f (weatherdata, ICON_N), GTK_ICON_SIZE_DIALOG);
  icon_n = gtk_image_new_from_pixbuf (icon);
  g_object_unref (G_OBJECT (icon));
  box_n = gtk_event_box_new ();
  gtk_container_add (GTK_CONTAINER (box_n), icon_n);

  if (G_UNLIKELY (!tooltips))
    tooltips = gtk_tooltips_new ();

  str =
    g_strdup_printf (_("Day: %s"),
                     translate_desc (get_data_f (weatherdata, TRANS_D)));
  gtk_tooltips_set_tip (tooltips, box_d, str, NULL);
  g_free (str);
  str =
    g_strdup_printf (_("Night: %s"),
                     translate_desc (get_data_f (weatherdata, TRANS_N)));
  gtk_tooltips_set_tip (tooltips, box_n, str, NULL);
  g_free (str);

  gtk_box_pack_start (GTK_BOX (icon_hbox), box_d, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (icon_hbox), box_n, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (item_vbox), icon_hbox, FALSE, FALSE, 0);

  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  gtk_label_set_markup (GTK_LABEL (label), _("<b>Precipitation</b>"));
  gtk_box_pack_start (GTK_BOX (item_vbox), label, FALSE, FALSE, 0);

  temp_hbox = gtk_hbox_new (FALSE, 0);
  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  str = g_strdup_printf ("%s %%", get_data_f (weatherdata, PPCP_D));
  gtk_label_set_markup (GTK_LABEL (label), str);
  g_free (str);
  gtk_box_pack_start (GTK_BOX (temp_hbox), label, TRUE, TRUE, 0);
  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  str = g_strdup_printf ("%s %%", get_data_f (weatherdata, PPCP_N));
  gtk_label_set_markup (GTK_LABEL (label), str);
  g_free (str);
  gtk_box_pack_start (GTK_BOX (temp_hbox), label, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (item_vbox), temp_hbox, FALSE, FALSE, 0);


  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  gtk_label_set_markup (GTK_LABEL (label), _("<b>Temperature</b>"));
  gtk_box_pack_start (GTK_BOX (item_vbox), label, FALSE, FALSE, 0);


  temp_hbox = gtk_hbox_new (FALSE, 0);
  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  str = g_strdup_printf ("<span foreground=\"red\">%s</span> %s",
                         get_data_f (weatherdata, TEMP_MAX), get_unit (unit,
                                                                       TEMP));
  gtk_label_set_markup (GTK_LABEL (label), str);
  g_free (str);
  gtk_box_pack_start (GTK_BOX (temp_hbox), label, TRUE, TRUE, 0);
  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  str = g_strdup_printf ("<span foreground=\"blue\">%s</span> %s",
                         get_data_f (weatherdata, TEMP_MIN), get_unit (unit,
                                                                       TEMP));
  gtk_label_set_markup (GTK_LABEL (label), str);
  g_free (str);
  gtk_box_pack_start (GTK_BOX (temp_hbox), label, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (item_vbox), temp_hbox, FALSE, FALSE, 0);


  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  gtk_label_set_markup (GTK_LABEL (label), _("<b>Wind</b>"));
  gtk_box_pack_start (GTK_BOX (item_vbox), label, FALSE, FALSE, 0);

  temp_hbox = gtk_hbox_new (FALSE, 0);
  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);

  wind = translate_wind_direction (get_data_f (weatherdata, W_DIRECTION_D));
  gtk_label_set_markup (GTK_LABEL (label),
                        wind ? wind : get_data_f (weatherdata,
                                                  W_DIRECTION_D));
  if (wind)
    g_free (wind);

  gtk_box_pack_start (GTK_BOX (temp_hbox), label, TRUE, TRUE, 0);

  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);

  wind = translate_wind_direction (get_data_f (weatherdata, W_DIRECTION_N));
  gtk_label_set_markup (GTK_LABEL (label),
                        wind ? wind : get_data_f (weatherdata,
                                                  W_DIRECTION_N));
  if (wind)
    g_free (wind);
  gtk_box_pack_start (GTK_BOX (temp_hbox), label, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (item_vbox), temp_hbox, FALSE, FALSE, 0);

  /* speed */

  temp_hbox = gtk_hbox_new (FALSE, 2);
  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  str = g_strdup_printf ("%s %s", get_data_f (weatherdata, W_SPEED_D),
                         get_unit (unit, WIND_SPEED));
  gtk_label_set_markup (GTK_LABEL (label), str);
  g_free (str);
  gtk_box_pack_start (GTK_BOX (temp_hbox), label, TRUE, TRUE, 0);

  label = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  str = g_strdup_printf ("%s %s", get_data_f (weatherdata, W_SPEED_N),
                         get_unit (unit, WIND_SPEED));
  gtk_label_set_markup (GTK_LABEL (label), str);
  g_free (str);
  gtk_box_pack_start (GTK_BOX (temp_hbox), label, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (item_vbox), temp_hbox, FALSE, FALSE, 0);

  DBG ("Done");

  return item_vbox;
334 335
}

336
static GtkWidget *
337
create_forecast_tab (xml_weather * data, units unit)
338
{
339 340
  GtkWidget *widg = gtk_hbox_new (FALSE, 0);
  guint i;
341

342
  gtk_container_set_border_width (GTK_CONTAINER (widg), 6);
343

344
  if (data && data->dayf)
Nick Schermer's avatar
Nick Schermer committed
345
    {
346
      for (i = 0; i < XML_WEATHER_DAYF_N - 1; i++)
347
        {
348 349
          if (!data->dayf[i])
            break;
Nick Schermer's avatar
Nick Schermer committed
350

351
          DBG ("%s", data->dayf[i]->day);
Nick Schermer's avatar
Nick Schermer committed
352

353 354 355 356 357
          gtk_box_pack_start (GTK_BOX (widg),
                              make_forecast (data->dayf[i], unit), FALSE,
                              FALSE, 0);
          gtk_box_pack_start (GTK_BOX (widg), gtk_vseparator_new (), TRUE,
                              TRUE, 0);
358
        }
Nick Schermer's avatar
Nick Schermer committed
359

360 361 362 363
      if (data->dayf[i])
        gtk_box_pack_start (GTK_BOX (widg),
                            make_forecast (data->dayf[i], unit), FALSE, FALSE,
                            0);
Nick Schermer's avatar
Nick Schermer committed
364
    }
365

366
  return widg;
367
}
368 369

GtkWidget *
370
create_summary_window (xml_weather * data, units unit)
371
{
372 373 374
  GtkWidget *window, *notebook, *vbox;
  gchar *title;
  GdkPixbuf *icon;
375

376 377 378 379 380
  window = xfce_titled_dialog_new_with_buttons (_("Weather Update"),
                                                NULL,
                                                GTK_DIALOG_NO_SEPARATOR,
                                                GTK_STOCK_OK,
                                                GTK_RESPONSE_ACCEPT, NULL);
381

382 383
  title =
    g_strdup_printf (_("Weather report for: %s"), get_data (data, DNAM));
Nick Schermer's avatar
Nick Schermer committed
384

385 386
  xfce_titled_dialog_set_subtitle (XFCE_TITLED_DIALOG (window), title);
  g_free (title);
387

388 389 390
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE,
                      0);
391

392
  icon = get_icon (get_data (data, WICON), GTK_ICON_SIZE_DIALOG);
393

394 395
  if (!icon)
    icon = get_icon ("25", GTK_ICON_SIZE_DIALOG);
396

397 398
  gtk_window_set_icon (GTK_WINDOW (window), icon);
  g_object_unref (G_OBJECT (icon));
399

400 401 402 403 404 405 406 407
  notebook = gtk_notebook_new ();
  gtk_container_set_border_width (GTK_CONTAINER (notebook), BORDER);
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
                            create_summary_tab (data, unit),
                            gtk_label_new (_("Summary")));
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
                            create_forecast_tab (data, unit),
                            gtk_label_new (_("Forecast")));
408

409
  gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
410

411 412
  g_signal_connect (window, "response",
                    G_CALLBACK (gtk_widget_destroy), window);
413

414
  gtk_window_set_default_size (GTK_WINDOW (window), 500, 400);
415

416
  return window;
417
}