~ubuntu-branches/ubuntu/natty/xfce4-weather-plugin/natty-proposed

« back to all changes in this revision

Viewing changes to panel-plugin/plugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Emanuele Rocca
  • Date: 2004-07-11 12:09:48 UTC
  • Revision ID: james.westby@ubuntu.com-20040711120948-l35brbd021chm9lv
Tags: upstream-0.3.9
ImportĀ upstreamĀ versionĀ 0.3.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "plugin.h"
 
2
#include <sys/stat.h>
 
3
#include "debug_print.h"
 
4
 
 
5
gint IconSizeSmall = 0;
 
6
 
 
7
gchar *make_label(struct xml_weather *weatherdata, enum datas opt, enum units unit, gint size)
 
8
{
 
9
        gchar *str, *lbl, *txtsize;
 
10
 
 
11
        switch (opt)
 
12
        {
 
13
                case VIS:       lbl = "V"; break;
 
14
                case UV_INDEX:  lbl = "U"; break;
 
15
                case WIND_DIRECTION: lbl = "WD"; break;
 
16
                case BAR_D:     lbl = "P"; break;
 
17
                case BAR_R:     lbl = "P"; break;
 
18
                case FLIK:      lbl = "F"; break;
 
19
                case TEMP:      lbl = "T"; break;
 
20
                case DEWP:      lbl = "D"; break;
 
21
                case HMID:      lbl = "H"; break;
 
22
                case WIND_SPEED:lbl = "WS"; break;
 
23
                case WIND_GUST: lbl = "WG"; break;
 
24
        }
 
25
 
 
26
        switch (size)
 
27
        {
 
28
                case 2: txtsize = "medium"; break;
 
29
                case 3: txtsize = "medium"; break;
 
30
                default: txtsize="x-small"; break;
 
31
        }
 
32
 
 
33
       
 
34
 
 
35
        str = g_strdup_printf("<span size=\"%s\">%s: %s %s</span>", txtsize,
 
36
                        lbl, get_data(weatherdata, opt), get_unit(unit, opt));
 
37
 
 
38
        return str;
 
39
}
 
40
// -1 error 0 no update needed 1 success
 
41
gint update_weatherdata(struct xfceweather_data *data, gboolean force)
 
42
{
 
43
        xmlNode *cur_node = NULL;
 
44
        xmlDoc *doc = NULL;
 
45
        struct xml_weather *weather = NULL;
 
46
        gboolean ret;
 
47
        struct stat attrs; 
 
48
        /*gchar *fullfilename = xfce_resource_save_location(XFCE_RESOURCE_CACHE, 
 
49
                        filename, TRUE);*/
 
50
        gchar *fullfilename, *filename;
 
51
 
 
52
        if (!data->location_code)
 
53
                return -1;
 
54
 
 
55
        filename = g_strdup_printf("weather_%s_%c.xml", 
 
56
                        data->location_code, data->unit == METRIC ? 'm' : 'i');
 
57
 
 
58
        fullfilename = g_strdup_printf("%s%s%s", xfce_get_userdir(), G_DIR_SEPARATOR_S,
 
59
                        filename);
 
60
        g_free(filename);
 
61
 
 
62
        if (!fullfilename)
 
63
        {
 
64
               
 
65
                return -1;
 
66
        } 
 
67
 
 
68
        if (force || (stat(fullfilename, &attrs) == -1) || 
 
69
                        ((time(NULL) - attrs.st_mtime) > (UPDATE_TIME)))
 
70
        {
 
71
                gchar *url = g_strdup_printf("/weather/local/%s?cc=*&dayf=%d&unit=%c",
 
72
                                data->location_code, XML_WEATHER_DAYF_N,
 
73
                                data->unit == METRIC ? 'm' : 'i');
 
74
               
 
75
 
 
76
                ret = http_get_file(url, "xoap.weather.com", fullfilename);
 
77
                g_free(url);
 
78
 
 
79
                if (!ret)
 
80
                {
 
81
                        g_free(fullfilename);
 
82
                       
 
83
                        return -1;
 
84
                }
 
85
        }
 
86
        else if (data->weatherdata)
 
87
                return 0;
 
88
      
 
89
 
 
90
        doc = xmlParseFile(fullfilename);
 
91
        g_free(fullfilename);
 
92
 
 
93
        if (!doc)
 
94
                return -1;
 
95
 
 
96
       
 
97
 
 
98
        cur_node = xmlDocGetRootElement(doc);
 
99
 
 
100
        if (cur_node)
 
101
                weather = parse_weather(cur_node);
 
102
 
 
103
        xmlFreeDoc(doc);
 
104
 
 
105
        if (weather)
 
106
        {
 
107
               
 
108
                if (data->weatherdata)
 
109
                        xml_weather_free(data->weatherdata);
 
110
               
 
111
                data->weatherdata = weather;
 
112
                return 1;
 
113
        }
 
114
        else
 
115
                return -1;
 
116
}       
 
117
 
 
118
void update_plugin (struct xfceweather_data *data, gboolean force)
 
119
{
 
120
        int i;
 
121
        GdkPixbuf *icon = NULL;
 
122
        GtkIconSize iconsize;
 
123
       
 
124
 
 
125
       
 
126
        gtk_scrollbox_clear(GTK_SCROLLBOX(data->scrollbox));
 
127
        
 
128
        if (update_weatherdata(data, force) == -1)
 
129
        {
 
130
                GdkPixbuf *icon = get_icon(data->iconimage, "25", data->iconsize);
 
131
                gtk_image_set_from_pixbuf(GTK_IMAGE(data->iconimage), icon);
 
132
 
 
133
                if (data->weatherdata)
 
134
                {
 
135
                        xml_weather_free(data->weatherdata);
 
136
                        data->weatherdata = NULL;
 
137
                }
 
138
 
 
139
                add_tooltip(data->tooltipbox, "Cannot update weather data");
 
140
                
 
141
                return;
 
142
        }
 
143
 
 
144
       
 
145
 
 
146
       
 
147
 
 
148
        for (i = 0; i < data->labels->len; i++)
 
149
        {
 
150
                enum datas opt;
 
151
                gchar *str;
 
152
 
 
153
                opt = g_array_index(data->labels, enum datas, i);
 
154
               
 
155
                str = make_label(data->weatherdata, opt, data->unit,
 
156
                                data->size);
 
157
 
 
158
               
 
159
 
 
160
                gtk_scrollbox_set_label(GTK_SCROLLBOX(data->scrollbox), -1, 
 
161
                                str);
 
162
                g_free(str);
 
163
 
 
164
               
 
165
        }
 
166
 
 
167
        
 
168
 
 
169
        gtk_scrollbox_enablecb(GTK_SCROLLBOX(data->scrollbox), TRUE);
 
170
 
 
171
       
 
172
                
 
173
        icon = get_icon(data->iconimage, get_data(data->weatherdata, WICON), data->iconsize);
 
174
       
 
175
        gtk_image_set_from_pixbuf(GTK_IMAGE(data->iconimage), icon);
 
176
       
 
177
        add_tooltip(data->tooltipbox, get_data(data->weatherdata, TRANS));
 
178
}
 
179
 
 
180
GArray *labels_clear (GArray *array)
 
181
{       
 
182
        if (!array || array->len > 0)
 
183
        {
 
184
                if (array)
 
185
                        g_array_free(array, TRUE);
 
186
                
 
187
                array = g_array_new(FALSE, TRUE, sizeof(enum datas));
 
188
        }
 
189
 
 
190
        return array;
 
191
}
 
192
 
 
193
void xfceweather_read_config (Control *control, xmlNodePtr node)
 
194
{
 
195
        gchar *value;
 
196
        struct xfceweather_data *data = (struct xfceweather_data *)control->data;
 
197
 
 
198
       
 
199
 
 
200
        if (!node || !node->children)
 
201
                return;
 
202
 
 
203
        node = node->children;
 
204
 
 
205
        if (!xmlStrEqual (node->name, (const xmlChar *) XFCEWEATHER_ROOT))
 
206
                return;
 
207
 
 
208
        value = xmlGetProp (node, (const xmlChar *) "loc_code");
 
209
        
 
210
 
 
211
        if (value) 
 
212
        {
 
213
                if (data->location_code)
 
214
                        g_free(data->location_code);
 
215
                
 
216
                data->location_code = g_strdup(value);
 
217
                g_free(value); 
 
218
        }
 
219
 
 
220
        value = xmlGetProp (node, (const xmlChar *) "celsius");
 
221
 
 
222
        if (value) 
 
223
        {
 
224
                if (atoi(value) == 1)
 
225
                        data->unit = IMPERIAL;
 
226
                else
 
227
                        data->unit = METRIC;
 
228
                g_free(value);
 
229
        }
 
230
 
 
231
        data->labels = labels_clear(data->labels); 
 
232
        
 
233
        for (node = node->children; node; node = node->next) {
 
234
                if (node->type != XML_ELEMENT_NODE)
 
235
                        continue;
 
236
 
 
237
                if (NODE_IS_TYPE(node, "label_")) { // label_ because the values of the
 
238
                                                    // previous plugin are invalid in this version
 
239
                        value = DATA(node);
 
240
                        if (value) {
 
241
                                gint val = atoi(value); 
 
242
                                g_array_append_val(data->labels, val);
 
243
                                g_free(value);
 
244
                        }
 
245
                }
 
246
        }
 
247
 
 
248
       
 
249
 
 
250
        update_plugin(data, FALSE);
 
251
}
 
252
 
 
253
void xfceweather_write_config (Control *control,
 
254
                xmlNodePtr parent)
 
255
{
 
256
        xmlNodePtr root, node;
 
257
        gchar *value;
 
258
        int i = 0;
 
259
 
 
260
        struct xfceweather_data *data = (struct xfceweather_data *)control->data;
 
261
 
 
262
        root = xmlNewTextChild (parent, NULL, XFCEWEATHER_ROOT, NULL);
 
263
 
 
264
        value = g_strdup_printf("%d", data->unit == IMPERIAL ? 1 : 0);
 
265
        xmlSetProp (root, "celsius", value);
 
266
        g_free(value);
 
267
 
 
268
        if (data->location_code)
 
269
                xmlSetProp (root, "loc_code", data->location_code);
 
270
 
 
271
        for (i = 0; i < data->labels->len; i++) 
 
272
        {
 
273
                enum datas opt = g_array_index (data->labels, enum datas, i);
 
274
                value = g_strdup_printf("%d", opt);
 
275
 
 
276
                node = xmlNewTextChild(root, NULL, "label_", value);
 
277
                g_free(value);
 
278
        }
 
279
}
 
280
 
 
281
gboolean update_cb(struct xfceweather_data *data)
 
282
{
 
283
        update_plugin(data, FALSE);
 
284
 
 
285
        return TRUE;
 
286
}
 
287
 
 
288
void real_update_config(struct xfceweather_data *data, gboolean force)
 
289
{
 
290
        if (data->updatetimeout)
 
291
                g_source_remove(data->updatetimeout);
 
292
 
 
293
        update_plugin(data, force);
 
294
 
 
295
        data->updatetimeout = gtk_timeout_add(UPDATE_TIME * 1000, (GSourceFunc)update_cb, data);
 
296
}
 
297
 
 
298
 
 
299
void update_config(struct xfceweather_data *data)
 
300
{
 
301
        real_update_config(data, FALSE);
 
302
}
 
303
 
 
304
void close_summary(GtkWidget *widget, gpointer *user_data)
 
305
{
 
306
        struct xfceweather_data *data = (struct xfceweather_data *)user_data;
 
307
 
 
308
       
 
309
 
 
310
        data->summary_window = NULL;
 
311
       
 
312
}
 
313
 
 
314
gboolean cb_click(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
 
315
{
 
316
        struct xfceweather_data *data = (struct xfceweather_data *)user_data;
 
317
 
 
318
        if (event->button == 1)
 
319
        {
 
320
               
 
321
                if (data->summary_window != NULL)
 
322
                {
 
323
                       
 
324
                        gtk_window_present(GTK_WINDOW(data->summary_window));
 
325
                }
 
326
                else
 
327
                {
 
328
                        data->summary_window = create_summary_window(data->weatherdata,
 
329
                                        data->unit);
 
330
                        g_signal_connect(data->summary_window, "destroy",
 
331
                                        G_CALLBACK(close_summary), (gpointer)data);
 
332
                                        
 
333
                        gtk_widget_show_all(data->summary_window);
 
334
                }
 
335
        }
 
336
        else if (event->button == 2)
 
337
                real_update_config(data, TRUE);
 
338
 
 
339
        return FALSE;
 
340
}
 
341
 
 
342
 
 
343
void xfceweather_create_options(Control *control, GtkContainer *container,
 
344
                GtkWidget *done)
 
345
{
 
346
        struct xfceweather_data *data = (struct xfceweather_data *)control->data;
 
347
        struct xfceweather_dialog *dialog = create_config_dialog(data, container, done);
 
348
 
 
349
        set_callback_config_dialog(dialog, update_config);
 
350
}
 
351
       
 
352
 
 
353
gboolean xfceweather_create_control(Control *control)
 
354
{
 
355
        struct xfceweather_data *data = g_new0(struct xfceweather_data, 1);
 
356
        gchar *path;
 
357
        GtkWidget *box, *vbox, *vbox2;
 
358
        enum datas lbl;
 
359
 
 
360
        if (!IconSizeSmall)
 
361
                IconSizeSmall = gtk_icon_size_register("iconsize_small", 20, 20);
 
362
 
 
363
        path = g_strdup_printf("%s%s%s%s", THEMESDIR, G_DIR_SEPARATOR_S,
 
364
                        DEFAULT_W_THEME, G_DIR_SEPARATOR_S);
 
365
        register_icons(path);
 
366
        g_free(path);
 
367
 
 
368
        data->scrollbox = gtk_scrollbox_new();
 
369
       
 
370
        data->iconimage = gtk_image_new_from_pixbuf(get_icon(control->base, "25", IconSizeSmall));
 
371
        gtk_misc_set_alignment(GTK_MISC(data->iconimage), 0.5, 1);
 
372
       
 
373
 
 
374
        data->labels = g_array_new(FALSE, TRUE, sizeof(enum datas));
 
375
       
 
376
       
 
377
        vbox = gtk_vbox_new(FALSE, 0);
 
378
 
 
379
        gtk_box_pack_start(GTK_BOX(vbox), data->iconimage, TRUE, FALSE, 0);
 
380
        gtk_box_pack_start(GTK_BOX(vbox), data->scrollbox, TRUE, TRUE, 0); 
 
381
 
 
382
        data->tooltipbox = gtk_event_box_new();
 
383
        gtk_container_add(GTK_CONTAINER(data->tooltipbox), vbox);
 
384
        
 
385
        vbox2 = gtk_vbox_new(FALSE, 0);
 
386
        gtk_box_pack_start(GTK_BOX(vbox2), data->tooltipbox, FALSE, FALSE, 0);
 
387
 
 
388
        gtk_container_add(GTK_CONTAINER(control->base), vbox2);
 
389
        g_signal_connect(data->tooltipbox, "button-press-event", G_CALLBACK(cb_click), (gpointer *)data);
 
390
        
 
391
        gtk_widget_show_all(vbox2);
 
392
 
 
393
        lbl = FLIK;
 
394
        g_array_append_val(data->labels, lbl);
 
395
        lbl = TEMP;
 
396
        g_array_append_val(data->labels, lbl);
 
397
 
 
398
        control->data = data;
 
399
        control->with_popup = FALSE;
 
400
 
 
401
        gtk_scrollbox_set_label(GTK_SCROLLBOX(data->scrollbox), -1, "1");
 
402
        gtk_scrollbox_clear(GTK_SCROLLBOX(data->scrollbox));
 
403
 
 
404
        data->updatetimeout = gtk_timeout_add(UPDATE_TIME * 1000, (GSourceFunc)update_cb, data);
 
405
 
 
406
 
 
407
       
 
408
 
 
409
        return TRUE;
 
410
}
 
411
 
 
412
void xfceweather_free(Control *control)
 
413
{
 
414
        struct xfceweather_data *data = (struct xfceweather_data *)control->data;
 
415
 
 
416
        if (data->weatherdata)
 
417
                xml_weather_free(data->weatherdata);
 
418
 
 
419
        unregister_icons();
 
420
 
 
421
        g_free(data->location_code);
 
422
 
 
423
        free_get_data_buffer();
 
424
        
 
425
        g_array_free(data->labels, TRUE);
 
426
 
 
427
        xmlCleanupParser();
 
428
}
 
429
 
 
430
void xfceweather_attach_callback (Control *control, const gchar *signal, GCallback cb,
 
431
                gpointer data)
 
432
{
 
433
        struct xfceweather_data *datae = (struct xfceweather_data*) control->data;
 
434
 
 
435
        g_signal_connect(datae->tooltipbox, signal, cb, data);
 
436
}
 
437
 
 
438
void xfceweather_set_size(Control *control, gint size)
 
439
{
 
440
        struct xfceweather_data *data = (struct xfceweather_data *)control->data;
 
441
 
 
442
        data->size = size;
 
443
 
 
444
        switch (data->size)
 
445
        {
 
446
                case 0: data->iconsize = IconSizeSmall; break;
 
447
                case 1: data->iconsize = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
 
448
                case 2: data->iconsize = GTK_ICON_SIZE_DND; break;
 
449
                case 3: data->iconsize = GTK_ICON_SIZE_DIALOG; break; 
 
450
        }
 
451
 
 
452
        update_plugin(data, FALSE);
 
453
}
 
454
 
 
455
G_MODULE_EXPORT void
 
456
xfce_control_class_init (ControlClass * cc)
 
457
{
 
458
        cc->name = "weather";
 
459
        cc->caption = "Weather Update";
 
460
 
 
461
        cc->create_control = (CreateControlFunc) xfceweather_create_control;
 
462
        cc->attach_callback = xfceweather_attach_callback;
 
463
        cc->read_config = xfceweather_read_config;
 
464
        cc->write_config = xfceweather_write_config;
 
465
 
 
466
        cc->create_options = xfceweather_create_options;
 
467
        cc->free = xfceweather_free;
 
468
        cc->set_size = xfceweather_set_size;
 
469
}
 
470
 
 
471
XFCE_PLUGIN_CHECK_INIT