source: titan/plugins/weather/weather.h @ 15709

Last change on this file since 15709 was 15709, checked in by nit, 11 years ago

[titan] add write to stock/weather

File size: 13.7 KB
Line 
1#ifndef WEATHER_H
2#define WEATHER_H
3
4struct weather
5{
6        char* city;
7        char* date;
8        char* day0;
9        char* day0_low;
10        char* day0_high;
11        char* day0_condition;
12        char* day0_icon;
13        char* day0_temp;
14        char* day0_humidity;
15        char* day0_wind;
16        char* day1;
17        char* day1_low;
18        char* day1_high;
19        char* day1_condition;
20        char* day1_icon;
21        char* day2;
22        char* day2_low;
23        char* day2_high;
24        char* day2_condition;
25        char* day2_icon;
26        char* day3;
27        char* day3_low;
28        char* day3_high;
29        char* day3_condition;
30        char* day3_icon;
31};
32
33char* readweather(const char* filename, struct skin* weather, struct skin* listbox)
34{
35        debug(1000, "in");
36        FILE *fd = NULL;
37        char *fileline = NULL;
38        char *location = NULL;
39        struct skin* tmp = NULL;
40
41        if(weather == NULL || listbox == NULL) return NULL;
42
43        fileline = malloc(MINMALLOC);
44        if(fileline == NULL)
45        {
46                err("no memory");
47                return NULL;
48        }
49
50        fd = fopen(filename, "r");
51        if(fd == NULL)
52        {
53                perr("can't open %s", filename);
54                free(fileline);
55                return NULL;
56        }
57
58        while(fgets(fileline, MINMALLOC, fd) != NULL)
59        {
60                if(fileline[0] == '#' || fileline[0] == '\n')
61                        continue;
62                if(fileline[strlen(fileline) - 1] == '\n')
63                        fileline[strlen(fileline) - 1] = '\0';
64                if(fileline[strlen(fileline) - 1] == '\r')
65                        fileline[strlen(fileline) - 1] = '\0';
66
67                tmp = addlistbox(weather, listbox, tmp, 1);
68                if(tmp != NULL)
69                {
70                        changetext(tmp, fileline);
71                        changename(tmp, fileline);
72                        if(location == NULL)
73                                location = ostrcat(location, fileline, 1, 0);
74                }
75
76        }
77
78        free(fileline);
79        fclose(fd);
80        return location;
81}
82
83void freeweather(struct weather* node)
84{
85        if(node == NULL) return;
86
87        free(node->city); node->city = NULL;
88        free(node->date); node->date = NULL;
89
90        free(node->day0); node->day0 = NULL;
91        free(node->day0_low); node->day0_low = NULL;
92        free(node->day0_high); node->day0_high = NULL;
93        free(node->day0_condition); node->day0_condition = NULL;
94        free(node->day0_icon); node->day0_icon = NULL;
95        free(node->day0_temp); node->day0_temp = NULL;
96        free(node->day0_humidity); node->day0_humidity = NULL;
97        free(node->day0_wind); node->day0_wind = NULL;
98
99        free(node->day1); node->day1 = NULL;
100        free(node->day1_low); node->day1_low = NULL;
101        free(node->day1_high); node->day1_high = NULL;
102        free(node->day1_condition); node->day1_condition = NULL;
103        free(node->day1_icon); node->day1_icon = NULL;
104
105        free(node->day2); node->day2 = NULL;
106        free(node->day2_low); node->day2_low = NULL;
107        free(node->day2_high); node->day2_high = NULL;
108        free(node->day2_condition); node->day2_condition = NULL;
109        free(node->day2_icon); node->day2_icon = NULL;
110
111        free(node->day3); node->day3 = NULL;
112        free(node->day3_low); node->day3_low = NULL;
113        free(node->day3_high); node->day3_high = NULL;
114        free(node->day3_condition); node->day3_condition = NULL;
115        free(node->day3_icon); node->day3_icon = NULL;
116
117        free(node); node = NULL;
118}
119
120struct weather* getweather(char* location)
121{
122        struct weather* weather = NULL;
123        char* tmpstr = NULL, *tmpstr1 = NULL, *tmpstr2 = NULL;
124        char* tmpsearch = NULL;
125
126        tmpsearch = ostrcat("ig/api?weather=", location, 0, 0);
127        //TODO: implement auto language (from titan.cfg)
128        tmpsearch = ostrcat(tmpsearch, "&hl=de", 1, 0);
129        tmpsearch = stringreplacechar(tmpsearch, ' ', '+');
130
131        tmpstr = gethttp("www.google.com", tmpsearch, 80, NULL, NULL, NULL, 0);
132
133        free(tmpsearch); tmpsearch = NULL;
134
135        if(tmpstr != NULL)
136        {
137                weather = (struct weather*)malloc(sizeof(struct weather));
138                if(weather == NULL)
139                {
140                        err("no mem");
141                        free(tmpstr); tmpstr = NULL;
142                        return NULL;
143                }
144                memset(weather, 0, sizeof(struct weather));
145
146                tmpstr1 = tmpstr;
147                tmpstr2 = tmpstr;
148
149                tmpstr2 = strstr(tmpstr1, "<forecast_information>");
150                if(tmpstr2 != NULL)
151                {
152                        tmpstr1 = tmpstr2 + 5;
153                        weather->date = getxmlentry(tmpstr2, "forecast_date data=");
154                        weather->city = getxmlentry(tmpstr2, "postal_code data=");
155                }
156
157                tmpstr2 = strstr(tmpstr1, "<current_conditions>");
158                if(tmpstr2 != NULL)
159                {
160                        tmpstr1 = tmpstr2 + 5;
161                        weather->day0_temp = getxmlentry(tmpstr2, "temp_c data=");
162                        weather->day0_humidity = getxmlentry(tmpstr2, "humidity data=");
163                        weather->day0_wind = getxmlentry(tmpstr2, "wind_condition data=");
164                }
165               
166                tmpstr2 = strstr(tmpstr1, "<forecast_conditions>");
167                if(tmpstr2 != NULL)
168                {
169                        tmpstr1 = tmpstr2 + 5;
170                        weather->day0 = getxmlentry(tmpstr2, "day_of_week data=");
171                        weather->day0_low = getxmlentry(tmpstr2, "low data=");
172                        weather->day0_high = getxmlentry(tmpstr2, "high data=");
173                        weather->day0_condition = getxmlentry(tmpstr2, "condition data=");
174                        weather->day0_icon = getxmlentry(tmpstr2, "icon data=");
175
176                }
177
178                tmpstr2 = strstr(tmpstr1, "<forecast_conditions>");
179                if(tmpstr2 != NULL)
180                {
181                        tmpstr1 = tmpstr2 + 5;
182                        weather->day1 = getxmlentry(tmpstr2, "day_of_week data=");
183                        weather->day1_low = getxmlentry(tmpstr2, "low data=");
184                        weather->day1_high = getxmlentry(tmpstr2, "high data=");
185                        weather->day1_condition = getxmlentry(tmpstr2, "condition data=");
186                        weather->day1_icon = getxmlentry(tmpstr2, "icon data=");
187
188                }
189
190                tmpstr2 = strstr(tmpstr1, "<forecast_conditions>");
191                if(tmpstr2 != NULL)
192                {
193                        tmpstr1 = tmpstr2 + 5;
194                        weather->day2 = getxmlentry(tmpstr2, "day_of_week data=");
195                        weather->day2_low = getxmlentry(tmpstr2, "low data=");
196                        weather->day2_high = getxmlentry(tmpstr2, "high data=");
197                        weather->day2_condition = getxmlentry(tmpstr2, "condition data=");
198                        weather->day2_icon = getxmlentry(tmpstr2, "icon data=");
199
200                }
201
202                tmpstr2 = strstr(tmpstr1, "<forecast_conditions>");
203                if(tmpstr2 != NULL)
204                {
205                        tmpstr1 = tmpstr2 + 5;
206                        weather->day3 = getxmlentry(tmpstr2, "day_of_week data=");
207                        weather->day3_low = getxmlentry(tmpstr2, "low data=");
208                        weather->day3_high = getxmlentry(tmpstr2, "high data=");
209                        weather->day3_condition = getxmlentry(tmpstr2, "condition data=");
210                        weather->day3_icon = getxmlentry(tmpstr2, "icon data=");
211
212                }
213
214                free(tmpstr); tmpstr = NULL;
215        }
216
217        return weather;
218}
219
220void changeweatherpic(struct skin* node, char* icon)
221{
222        if(node == NULL) return;
223
224        if(icon == NULL)
225                changepic(node, NULL);
226        else if(strstr(icon, "/sunny.gif") != NULL)
227                changepic(node, "%pluginpath%/weather/skin/sunny.png");
228        else if(strstr(icon, "/mostly_sunny.gif") != NULL)
229                changepic(node, "%pluginpath%/weather/skin/mostly_sunny.png");
230  else if(strstr(icon, "/chance_of_rain.gif") != NULL)
231                changepic(node, "%pluginpath%/weather/skin/chance_of_rain.png");
232  else if(strstr(icon, "/partly_cloudy.gif") != NULL)
233                changepic(node, "%pluginpath%/weather/skin/partly_cloudy.png");
234  else if(strstr(icon, "/mostly_cloudy.gif") != NULL)
235                changepic(node, "%pluginpath%/weather/skin/cloudy.png");
236  else if(strstr(icon, "/chance_of_storm.gif") != NULL)
237                changepic(node, "%pluginpath%/weather/skin/chance_of_storm.png");
238  else if(strstr(icon, "/showers.gif") != NULL)
239                changepic(node, "%pluginpath%/weather/skin/showers.png");
240  else if(strstr(icon, "/rain.gif") != NULL)
241                changepic(node, "%pluginpath%/weather/skin/rain.png");
242  else if(strstr(icon, "/chance_of_snow.gif") != NULL)
243                changepic(node, "%pluginpath%/weather/skin/chance_of_snow.png");
244  else if(strstr(icon, "/cloudy.gif") != NULL)
245                changepic(node, "%pluginpath%/weather/skin/cloudy.png");
246  else if(strstr(icon, "/storm.gif") != NULL)
247                changepic(node, "%pluginpath%/weather/skin/storm.png");
248  else if(strstr(icon, "/thunderstorm.gif") != NULL)
249                changepic(node, "%pluginpath%/weather/skin/storm.png");
250  else if(strstr(icon, "/chance_of_tstorm.gif") != NULL)
251                changepic(node, "%pluginpath%/weather/skin/chance_of_storm.png");
252  else if(strstr(icon, "/sleet.gif") != NULL)
253                changepic(node, "%pluginpath%/weather/skin/snow.png");
254  else if(strstr(icon, "/snow.gif") != NULL)
255                changepic(node, "%pluginpath%/weather/skin/snow.png");
256  else if(strstr(icon, "/icy.gif") != NULL)
257                changepic(node, "%pluginpath%/weather/skin/icy.png");
258  else if(strstr(icon, "/dust.gif") != NULL)
259                changepic(node, "%pluginpath%/weather/skin/fog.png");
260  else if(strstr(icon, "/fog.gif") != NULL)
261                changepic(node, "%pluginpath%/weather/skin/fog.png");
262  else if(strstr(icon, "/smoke.gif") != NULL)
263                changepic(node, "%pluginpath%/weather/skin/fog.png");
264  else if(strstr(icon, "/haze.gif") != NULL)
265                changepic(node, "%pluginpath%/weather/skin/fog.png");
266  else if(strstr(icon, "/flurries.gif") != NULL)
267                changepic(node, "%pluginpath%/weather/skin/flurries.png");
268}
269
270void screenweather()
271{
272        int rcret = 0;
273        struct skin* weather = getscreen("weather");
274        struct skin* listbox = getscreennode(weather, "listbox");
275        struct skin* date = getscreennode(weather, "date");
276        struct skin* day0 = getscreennode(weather, "day0");
277        struct skin* day0_low = getscreennode(weather, "day0_low");
278        struct skin* day0_high = getscreennode(weather, "day0_high");
279        struct skin* day0_condition = getscreennode(weather, "day0_condition");
280        struct skin* day0_icon = getscreennode(weather, "day0_icon");
281        struct skin* day1 = getscreennode(weather, "day1");
282        struct skin* day1_low = getscreennode(weather, "day1_low");
283        struct skin* day1_high = getscreennode(weather, "day1_high");
284        struct skin* day1_condition = getscreennode(weather, "day1_condition");
285        struct skin* day1_icon = getscreennode(weather, "day1_icon");
286        struct skin* day2 = getscreennode(weather, "day2");
287        struct skin* day2_low = getscreennode(weather, "day2_low");
288        struct skin* day2_high = getscreennode(weather, "day2_high");
289        struct skin* day2_condition = getscreennode(weather, "day2_condition");
290        struct skin* day2_icon = getscreennode(weather, "day2_icon");
291        struct skin* day3 = getscreennode(weather, "day3");
292        struct skin* day3_low = getscreennode(weather, "day3_low");
293        struct skin* day3_high = getscreennode(weather, "day3_high");
294        struct skin* day3_condition = getscreennode(weather, "day3_condition");
295        struct skin* day3_icon = getscreennode(weather, "day3_icon");
296        struct weather* node = NULL;
297        char* tmpstr = NULL, *location = NULL;
298
299        location = readweather(getconfig("weatherfile", NULL), weather, listbox);
300        addscreenrc(weather, listbox);
301
302start:
303
304        tmpstr = ostrcat(_("Weather"), " - ", 0, 0);
305        tmpstr = ostrcat(tmpstr, location, 1, 0);
306        changetitle(weather, tmpstr);
307        free(tmpstr); tmpstr = NULL;
308
309        node = getweather(location);
310        free(location); location = NULL;
311
312        if(node != NULL)
313        {
314                changetext(date, node->date);
315
316                tmpstr = ostrcat(_("Day: "), node->day0, 0, 0);
317                changetext(day0, tmpstr);
318                free(tmpstr); tmpstr = NULL;
319
320                tmpstr = ostrcat(_("Lowest temp: "), node->day0_low, 0, 0);
321                changetext(day0_low, tmpstr);
322                free(tmpstr); tmpstr = NULL;
323
324                tmpstr = ostrcat(_("Highest temp: "), node->day0_high, 0, 0);
325                changetext(day0_high, tmpstr);
326                free(tmpstr); tmpstr = NULL;
327
328                tmpstr = ostrcat(_("Condition: "), node->day0_condition, 0, 0);
329                changetext(day0_condition, tmpstr);
330                free(tmpstr); tmpstr = NULL;
331
332                changeweatherpic(day0_icon, node->day0_icon);
333
334                tmpstr = ostrcat(_("Day: "), node->day1, 0, 0);
335                changetext(day1, tmpstr);
336                free(tmpstr); tmpstr = NULL;
337
338                tmpstr = ostrcat(_("Lowest temp: "), node->day1_low, 0, 0);
339                changetext(day1_low, tmpstr);
340                free(tmpstr); tmpstr = NULL;
341
342                tmpstr = ostrcat(_("Highest temp: "), node->day1_high, 0, 0);
343                changetext(day1_high, tmpstr);
344                free(tmpstr); tmpstr = NULL;
345
346                tmpstr = ostrcat(_("Condition: "), node->day1_condition, 0, 0);
347                changetext(day1_condition, tmpstr);
348                free(tmpstr); tmpstr = NULL;
349
350                changeweatherpic(day1_icon, node->day1_icon);
351
352                tmpstr = ostrcat(_("Day: "), node->day2, 0, 0);
353                changetext(day2, tmpstr);
354                free(tmpstr); tmpstr = NULL;
355
356                tmpstr = ostrcat(_("Lowest temp: "), node->day2_low, 0, 0);
357                changetext(day2_low, tmpstr);
358                free(tmpstr); tmpstr = NULL;
359
360                tmpstr = ostrcat(_("Highest temp: "), node->day2_high, 0, 0);
361                changetext(day2_high, tmpstr);
362                free(tmpstr); tmpstr = NULL;
363
364                tmpstr = ostrcat(_("Condition: "), node->day2_condition, 0, 0);
365                changetext(day2_condition, tmpstr);
366                free(tmpstr); tmpstr = NULL;
367
368                changeweatherpic(day2_icon, node->day2_icon);
369
370                tmpstr = ostrcat(_("Day: "), node->day3, 0, 0);
371                changetext(day3, tmpstr);
372                free(tmpstr); tmpstr = NULL;
373
374                tmpstr = ostrcat(_("Lowest temp: "), node->day3_low, 0, 0);
375                changetext(day3_low, tmpstr);
376                free(tmpstr); tmpstr = NULL;
377
378                tmpstr = ostrcat(_("Highest temp: "), node->day3_high, 0, 0);
379                changetext(day3_high, tmpstr);
380                free(tmpstr); tmpstr = NULL;
381
382                tmpstr = ostrcat(_("Condition: "), node->day3_condition, 0, 0);
383                changetext(day3_condition, tmpstr);
384                free(tmpstr); tmpstr = NULL;
385
386                changeweatherpic(day3_icon, node->day3_icon);
387        }
388
389        drawscreen(weather, 0);
390
391        while(1)
392        {
393                rcret = waitrc(weather, 0, 0);
394       
395                if(rcret == getrcconfigint("rcexit", NULL)) break;
396                if(rcret == getrcconfigint("rcok", NULL)) break;
397
398                if(rcret == getrcconfigint("rcred", NULL))
399    {
400                        free(location); location = NULL;
401                        location = textinput("Location", NULL);
402      if(location != NULL)
403      {
404                                struct skin* tmp = addlistbox(weather, listbox, NULL, 1);
405                                if(tmp != NULL)
406                                {
407                                        changetext(tmp, location);
408                                        changename(tmp, location);
409                                }
410      }
411      drawscreen(weather, 0);
412                        if(location == NULL)
413                                continue;
414                        free(location); location = NULL;
415    }
416
417                if(rcret == getrcconfigint("rcyellow", NULL))
418                {
419                        writeweather(getconfig("weatherfile", listbox);
420                        continue;
421                }
422
423                if(listbox->select != NULL && rcret == getrcconfigint("rcgreen", NULL))
424                {
425                        delscreennode(weather, listbox->select->name);
426                        drawscreen(weather, 0);
427                }
428
429                if(listbox->select != NULL)
430                {
431                        freeweather(node); node = NULL;
432                        location = ostrcat(location, listbox->select->text, 1, 0);
433                        goto start;
434                }
435        }
436
437        delownerrc(weather);
438        delmarkedscreennodes(weather, 1);
439        freeweather(node); node = NULL;
440        clearscreen(weather);
441}
442
443int writeweather(const char *filename, struct skin* listbox)
444{
445        debug(1000, "in");
446        FILE *fd = NULL;
447        struct skin* node = listbox;
448        int ret = 0;
449
450        fd = fopen(filename, "w");
451        if(fd == NULL)
452        {
453                perr("can't open %s", filename);
454                return 1;
455        }
456
457        while(node != NULL)
458        {
459                if(node->del == 1)
460                {
461                        ret = fprintf(fd, "%s\n", node->text);
462                        if(ret < 0)
463                        {
464                                perr("writting file %s", filename);
465                        }
466                }
467                node = node->next;
468        }
469
470        fclose(fd);
471        debug(1000, "out");
472        return 0;
473}
474
475#endif
Note: See TracBrowser for help on using the repository browser.