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

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

[titan] shown info in stock and weather title

File size: 12.8 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                        if(location == NULL)
72                                location = ostrcat(location, fileline, 1, 0);
73                }
74
75        }
76
77        free(fileline);
78        fclose(fd);
79        return location;
80}
81
82void freeweather(struct weather* node)
83{
84        if(node == NULL) return;
85
86        free(node->city); node->city = NULL;
87        free(node->date); node->date = NULL;
88
89        free(node->day0); node->day0 = NULL;
90        free(node->day0_low); node->day0_low = NULL;
91        free(node->day0_high); node->day0_high = NULL;
92        free(node->day0_condition); node->day0_condition = NULL;
93        free(node->day0_icon); node->day0_icon = NULL;
94        free(node->day0_temp); node->day0_temp = NULL;
95        free(node->day0_humidity); node->day0_humidity = NULL;
96        free(node->day0_wind); node->day0_wind = NULL;
97
98        free(node->day1); node->day1 = NULL;
99        free(node->day1_low); node->day1_low = NULL;
100        free(node->day1_high); node->day1_high = NULL;
101        free(node->day1_condition); node->day1_condition = NULL;
102        free(node->day1_icon); node->day1_icon = NULL;
103
104        free(node->day2); node->day2 = NULL;
105        free(node->day2_low); node->day2_low = NULL;
106        free(node->day2_high); node->day2_high = NULL;
107        free(node->day2_condition); node->day2_condition = NULL;
108        free(node->day2_icon); node->day2_icon = NULL;
109
110        free(node->day3); node->day3 = NULL;
111        free(node->day3_low); node->day3_low = NULL;
112        free(node->day3_high); node->day3_high = NULL;
113        free(node->day3_condition); node->day3_condition = NULL;
114        free(node->day3_icon); node->day3_icon = NULL;
115
116        free(node); node = NULL;
117}
118
119struct weather* getweather(char* location)
120{
121        struct weather* weather = NULL;
122        char* tmpstr = NULL, *tmpstr1 = NULL, *tmpstr2 = NULL;
123        char* tmpsearch = NULL;
124
125        tmpsearch = ostrcat("ig/api?weather=", location, 0, 0);
126        //TODO: implement auto language (from titan.cfg)
127        tmpsearch = ostrcat(tmpsearch, "&hl=de", 1, 0);
128        tmpsearch = stringreplacechar(tmpsearch, ' ', '+');
129
130        tmpstr = gethttp("www.google.com", tmpsearch, 80, NULL, NULL, NULL, 0);
131
132        free(tmpsearch); tmpsearch = NULL;
133
134        if(tmpstr != NULL)
135        {
136                weather = (struct weather*)malloc(sizeof(struct weather));
137                if(weather == NULL)
138                {
139                        err("no mem");
140                        free(tmpstr); tmpstr = NULL;
141                        return NULL;
142                }
143                memset(weather, 0, sizeof(struct weather));
144
145                tmpstr1 = tmpstr;
146                tmpstr2 = tmpstr;
147
148                tmpstr2 = strstr(tmpstr1, "<forecast_information>");
149                if(tmpstr2 != NULL)
150                {
151                        tmpstr1 = tmpstr2 + 5;
152                        weather->date = getxmlentry(tmpstr2, "forecast_date data=");
153                        weather->city = getxmlentry(tmpstr2, "postal_code data=");
154                }
155
156                tmpstr2 = strstr(tmpstr1, "<current_conditions>");
157                if(tmpstr2 != NULL)
158                {
159                        tmpstr1 = tmpstr2 + 5;
160                        weather->day0_temp = getxmlentry(tmpstr2, "temp_c data=");
161                        weather->day0_humidity = getxmlentry(tmpstr2, "humidity data=");
162                        weather->day0_wind = getxmlentry(tmpstr2, "wind_condition data=");
163                }
164               
165                tmpstr2 = strstr(tmpstr1, "<forecast_conditions>");
166                if(tmpstr2 != NULL)
167                {
168                        tmpstr1 = tmpstr2 + 5;
169                        weather->day0 = getxmlentry(tmpstr2, "day_of_week data=");
170                        weather->day0_low = getxmlentry(tmpstr2, "low data=");
171                        weather->day0_high = getxmlentry(tmpstr2, "high data=");
172                        weather->day0_condition = getxmlentry(tmpstr2, "condition data=");
173                        weather->day0_icon = getxmlentry(tmpstr2, "icon data=");
174
175                }
176
177                tmpstr2 = strstr(tmpstr1, "<forecast_conditions>");
178                if(tmpstr2 != NULL)
179                {
180                        tmpstr1 = tmpstr2 + 5;
181                        weather->day1 = getxmlentry(tmpstr2, "day_of_week data=");
182                        weather->day1_low = getxmlentry(tmpstr2, "low data=");
183                        weather->day1_high = getxmlentry(tmpstr2, "high data=");
184                        weather->day1_condition = getxmlentry(tmpstr2, "condition data=");
185                        weather->day1_icon = getxmlentry(tmpstr2, "icon data=");
186
187                }
188
189                tmpstr2 = strstr(tmpstr1, "<forecast_conditions>");
190                if(tmpstr2 != NULL)
191                {
192                        tmpstr1 = tmpstr2 + 5;
193                        weather->day2 = getxmlentry(tmpstr2, "day_of_week data=");
194                        weather->day2_low = getxmlentry(tmpstr2, "low data=");
195                        weather->day2_high = getxmlentry(tmpstr2, "high data=");
196                        weather->day2_condition = getxmlentry(tmpstr2, "condition data=");
197                        weather->day2_icon = getxmlentry(tmpstr2, "icon data=");
198
199                }
200
201                tmpstr2 = strstr(tmpstr1, "<forecast_conditions>");
202                if(tmpstr2 != NULL)
203                {
204                        tmpstr1 = tmpstr2 + 5;
205                        weather->day3 = getxmlentry(tmpstr2, "day_of_week data=");
206                        weather->day3_low = getxmlentry(tmpstr2, "low data=");
207                        weather->day3_high = getxmlentry(tmpstr2, "high data=");
208                        weather->day3_condition = getxmlentry(tmpstr2, "condition data=");
209                        weather->day3_icon = getxmlentry(tmpstr2, "icon data=");
210
211                }
212
213                free(tmpstr); tmpstr = NULL;
214        }
215
216        return weather;
217}
218
219void changeweatherpic(struct skin* node, char* icon)
220{
221        if(node == NULL) return;
222
223        if(icon == NULL)
224                changepic(node, NULL);
225        else if(strstr(icon, "/sunny.gif") != NULL)
226                changepic(node, "%pluginpath%/weather/skin/sunny.png");
227        else if(strstr(icon, "/mostly_sunny.gif") != NULL)
228                changepic(node, "%pluginpath%/weather/skin/mostly_sunny.png");
229  else if(strstr(icon, "/chance_of_rain.gif") != NULL)
230                changepic(node, "%pluginpath%/weather/skin/chance_of_rain.png");
231  else if(strstr(icon, "/partly_cloudy.gif") != NULL)
232                changepic(node, "%pluginpath%/weather/skin/partly_cloudy.png");
233  else if(strstr(icon, "/mostly_cloudy.gif") != NULL)
234                changepic(node, "%pluginpath%/weather/skin/cloudy.png");
235  else if(strstr(icon, "/chance_of_storm.gif") != NULL)
236                changepic(node, "%pluginpath%/weather/skin/chance_of_storm.png");
237  else if(strstr(icon, "/showers.gif") != NULL)
238                changepic(node, "%pluginpath%/weather/skin/showers.png");
239  else if(strstr(icon, "/rain.gif") != NULL)
240                changepic(node, "%pluginpath%/weather/skin/rain.png");
241  else if(strstr(icon, "/chance_of_snow.gif") != NULL)
242                changepic(node, "%pluginpath%/weather/skin/chance_of_snow.png");
243  else if(strstr(icon, "/cloudy.gif") != NULL)
244                changepic(node, "%pluginpath%/weather/skin/cloudy.png");
245  else if(strstr(icon, "/storm.gif") != NULL)
246                changepic(node, "%pluginpath%/weather/skin/storm.png");
247  else if(strstr(icon, "/thunderstorm.gif") != NULL)
248                changepic(node, "%pluginpath%/weather/skin/storm.png");
249  else if(strstr(icon, "/chance_of_tstorm.gif") != NULL)
250                changepic(node, "%pluginpath%/weather/skin/chance_of_storm.png");
251  else if(strstr(icon, "/sleet.gif") != NULL)
252                changepic(node, "%pluginpath%/weather/skin/snow.png");
253  else if(strstr(icon, "/snow.gif") != NULL)
254                changepic(node, "%pluginpath%/weather/skin/snow.png");
255  else if(strstr(icon, "/icy.gif") != NULL)
256                changepic(node, "%pluginpath%/weather/skin/icy.png");
257  else if(strstr(icon, "/dust.gif") != NULL)
258                changepic(node, "%pluginpath%/weather/skin/fog.png");
259  else if(strstr(icon, "/fog.gif") != NULL)
260                changepic(node, "%pluginpath%/weather/skin/fog.png");
261  else if(strstr(icon, "/smoke.gif") != NULL)
262                changepic(node, "%pluginpath%/weather/skin/fog.png");
263  else if(strstr(icon, "/haze.gif") != NULL)
264                changepic(node, "%pluginpath%/weather/skin/fog.png");
265  else if(strstr(icon, "/flurries.gif") != NULL)
266                changepic(node, "%pluginpath%/weather/skin/flurries.png");
267}
268
269void screenweather()
270{
271        int rcret = 0;
272        struct skin* weather = getscreen("weather");
273        struct skin* listbox = getscreennode(weather, "listbox");
274        struct skin* date = getscreennode(weather, "date");
275        struct skin* day0 = getscreennode(weather, "day0");
276        struct skin* day0_low = getscreennode(weather, "day0_low");
277        struct skin* day0_high = getscreennode(weather, "day0_high");
278        struct skin* day0_condition = getscreennode(weather, "day0_condition");
279        struct skin* day0_icon = getscreennode(weather, "day0_icon");
280        struct skin* day1 = getscreennode(weather, "day1");
281        struct skin* day1_low = getscreennode(weather, "day1_low");
282        struct skin* day1_high = getscreennode(weather, "day1_high");
283        struct skin* day1_condition = getscreennode(weather, "day1_condition");
284        struct skin* day1_icon = getscreennode(weather, "day1_icon");
285        struct skin* day2 = getscreennode(weather, "day2");
286        struct skin* day2_low = getscreennode(weather, "day2_low");
287        struct skin* day2_high = getscreennode(weather, "day2_high");
288        struct skin* day2_condition = getscreennode(weather, "day2_condition");
289        struct skin* day2_icon = getscreennode(weather, "day2_icon");
290        struct skin* day3 = getscreennode(weather, "day3");
291        struct skin* day3_low = getscreennode(weather, "day3_low");
292        struct skin* day3_high = getscreennode(weather, "day3_high");
293        struct skin* day3_condition = getscreennode(weather, "day3_condition");
294        struct skin* day3_icon = getscreennode(weather, "day3_icon");
295        struct weather* node = NULL;
296        char* tmpstr = NULL, *location = NULL;
297
298        location = readweather(getconfig("weatherfile", NULL), weather, listbox);
299
300start:
301
302        tmpstr = ostrcat(_("Weather"), " - ", 0, 0);
303        tmpstr = ostrcat(tmpstr, location, 1, 0);
304        changetitle(weather, tmpstr);
305        free(tmpstr); tmpstr = NULL;
306
307        node = getweather(location);
308        free(location); location = NULL;
309
310        if(node != NULL)
311        {
312                changetext(date, node->date);
313
314                tmpstr = ostrcat(_("Day: "), node->day0, 0, 0);
315                changetext(day0, tmpstr);
316                free(tmpstr); tmpstr = NULL;
317
318                tmpstr = ostrcat(_("Lowest temp: "), node->day0_low, 0, 0);
319                changetext(day0_low, tmpstr);
320                free(tmpstr); tmpstr = NULL;
321
322                tmpstr = ostrcat(_("Highest temp: "), node->day0_high, 0, 0);
323                changetext(day0_high, tmpstr);
324                free(tmpstr); tmpstr = NULL;
325
326                tmpstr = ostrcat(_("Condition: "), node->day0_condition, 0, 0);
327                changetext(day0_condition, tmpstr);
328                free(tmpstr); tmpstr = NULL;
329
330                changeweatherpic(day0_icon, node->day0_icon);
331
332                tmpstr = ostrcat(_("Day: "), node->day1, 0, 0);
333                changetext(day1, tmpstr);
334                free(tmpstr); tmpstr = NULL;
335
336                tmpstr = ostrcat(_("Lowest temp: "), node->day1_low, 0, 0);
337                changetext(day1_low, tmpstr);
338                free(tmpstr); tmpstr = NULL;
339
340                tmpstr = ostrcat(_("Highest temp: "), node->day1_high, 0, 0);
341                changetext(day1_high, tmpstr);
342                free(tmpstr); tmpstr = NULL;
343
344                tmpstr = ostrcat(_("Condition: "), node->day1_condition, 0, 0);
345                changetext(day1_condition, tmpstr);
346                free(tmpstr); tmpstr = NULL;
347
348                changeweatherpic(day1_icon, node->day1_icon);
349
350                tmpstr = ostrcat(_("Day: "), node->day2, 0, 0);
351                changetext(day2, tmpstr);
352                free(tmpstr); tmpstr = NULL;
353
354                tmpstr = ostrcat(_("Lowest temp: "), node->day2_low, 0, 0);
355                changetext(day2_low, tmpstr);
356                free(tmpstr); tmpstr = NULL;
357
358                tmpstr = ostrcat(_("Highest temp: "), node->day2_high, 0, 0);
359                changetext(day2_high, tmpstr);
360                free(tmpstr); tmpstr = NULL;
361
362                tmpstr = ostrcat(_("Condition: "), node->day2_condition, 0, 0);
363                changetext(day2_condition, tmpstr);
364                free(tmpstr); tmpstr = NULL;
365
366                changeweatherpic(day2_icon, node->day2_icon);
367
368                tmpstr = ostrcat(_("Day: "), node->day3, 0, 0);
369                changetext(day3, tmpstr);
370                free(tmpstr); tmpstr = NULL;
371
372                tmpstr = ostrcat(_("Lowest temp: "), node->day3_low, 0, 0);
373                changetext(day3_low, tmpstr);
374                free(tmpstr); tmpstr = NULL;
375
376                tmpstr = ostrcat(_("Highest temp: "), node->day3_high, 0, 0);
377                changetext(day3_high, tmpstr);
378                free(tmpstr); tmpstr = NULL;
379
380                tmpstr = ostrcat(_("Condition: "), node->day3_condition, 0, 0);
381                changetext(day3_condition, tmpstr);
382                free(tmpstr); tmpstr = NULL;
383
384                changeweatherpic(day3_icon, node->day3_icon);
385        }
386
387        addscreenrc(weather, listbox);
388        drawscreen(weather, 0);
389
390        while(1)
391        {
392                rcret = waitrc(weather, 0, 0);
393       
394                if(rcret == getrcconfigint("rcexit", NULL)) break;
395                if(rcret == getrcconfigint("rcok", NULL)) break;
396
397                if(rcret == getrcconfigint("rcred", NULL))
398    {
399                        free(location); location = NULL;
400                        location = textinput("Location", NULL);
401      if(location != NULL)
402      {
403                                freeweather(node); node = NULL;
404                                goto start;
405      }
406      drawscreen(weather, 0);
407      continue;
408    }
409
410                if(listbox->select != NULL)
411                {
412                        freeweather(node); node = NULL;
413                        location = ostrcat(location, listbox->select->text, 1, 0);
414                        goto start;
415                }
416        }
417
418        delownerrc(weather);
419        delmarkedscreennodes(weather, 1);
420        freeweather(node); node = NULL;
421        clearscreen(weather);
422}
423
424#endif
Note: See TracBrowser for help on using the repository browser.