Debian Codegraph @

weather/src/applet-notifications.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <glib/gstdio.h>
23
24 #include "applet-struct.h"
25 #include "applet-load-icons.h"
26 #include "applet-read-data.h"
27 #include "applet-notifications.h"
28
29
30 CD_APPLET_ON_CLICK_BEGIN
31 if (pClickedIcon == myIcon) // en mode dock, on peut recevoir le clic sur l'icone principale si elle n'a pas de sous-dock, autrement dit si la connexion s'est mal passe, ce qui nous permet d'afficher le message d'erreur.
32 {
33 cd_weather_show_current_conditions_dialog (myApplet);
34 }
35 else if (pClickedIcon != NULL) // clic sur une des sous-icones.
36 {
37 cd_weather_show_forecast_dialog (myApplet, pClickedIcon);
38 }
39 CD_APPLET_ON_CLICK_END
40
41 static int _get_num_day_from_icon (GldiModuleInstance *myApplet, Icon *pIcon)
42 {
43 /// TODO: determiner le jour exact...
44 return (pIcon == myIcon ? -1 : pIcon->fOrder/2); // la 1ere icone est le plus souvent celle d'aujourd'hui, toutefois cela peut ne pas etre vrai, surtout la nuit autour du changement de jour.
45 }
46 static inline void _go_to_site (GldiModuleInstance *myApplet, int iNumDay)
47 {
48 //~ gchar *cURI;
49 //~ if (iNumDay == -1)
50 //~ cURI = g_strdup_printf ("http://www.weather.com/weather/hourbyhour/graph/%s", myConfig.cLocationCode);
51 //~ else if (iNumDay == 0)
52 //~ cURI = g_strdup_printf ("http://www.weather.com/weather/today/%s", myConfig.cLocationCode);
53 //~ else if (iNumDay == 1)
54 //~ cURI = g_strdup_printf ("http://www.weather.com/weather/tomorrow/%s", myConfig.cLocationCode);
55 //~ else
56 //~ cURI = g_strdup_printf ("http://www.weather.com/weather/wxdetail%d/%s", iNumDay, myConfig.cLocationCode); // ?dayNum=%d
57 //~ cairo_dock_fm_launch_uri (cURI);
58 //~ g_free (cURI);
59 g_print("url: %s\n", myData.wdata.cLink);
60 if (myData.wdata.cLink != NULL)
61 cairo_dock_fm_launch_uri ((const gchar *)myData.wdata.cLink);
62 }
63
64 static inline void _reload (GldiModuleInstance *myApplet)
65 {
66 if (gldi_task_is_running (myData.pTask))
67 {
68 gldi_dialog_show_temporary_with_icon (D_("Data are being retrieved, please wait a moment."),
69 myIcon,
70 myContainer,
71 3000,
72 "same icon");
73 }
74 else
75 {
76 gldi_task_stop (myData.pTask); // not blocking since the task is not running.
77
78 myData.bBusy = TRUE;
79 CD_APPLET_ANIMATE_MY_ICON ("busy", 999);
80 cairo_dock_mark_icon_as_clicked (myIcon); // prevent hovering the icon to overwrite the animation with another one.
81 gldi_task_launch (myData.pTask);
82 }
83 }
84
85
86 static void _cd_weather_reload (GtkMenuItem *menu_item, GldiModuleInstance *myApplet)
87 {
88 CD_APPLET_ENTER;
89 _reload (myApplet);
90 CD_APPLET_LEAVE ();
91 }
92 static void _cd_weather_show_site (GtkMenuItem *menu_item, GldiModuleInstance *myApplet)
93 {
94 CD_APPLET_ENTER;
95 _go_to_site (myApplet, myData.iClickedDay);
96 CD_APPLET_LEAVE ();
97 }
98 static void _cd_weather_show_cc (GtkMenuItem *menu_item, GldiModuleInstance *myApplet)
99 {
100 CD_APPLET_ENTER;
101 cd_weather_show_current_conditions_dialog (myApplet);
102 CD_APPLET_LEAVE ();
103 }
104 CD_APPLET_ON_BUILD_MENU_BEGIN
105 if (pClickedIcon == myIcon)
106 {
107 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Show current conditions (middle-click)"), GLDI_ICON_NAME_DIALOG_INFO, _cd_weather_show_cc, CD_APPLET_MY_MENU);
108 }
109 if (pClickedIcon != NULL)
110 {
111 myData.iClickedDay = _get_num_day_from_icon (myApplet, pClickedIcon);
112 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Open weather.com (double-click)"), GLDI_ICON_NAME_JUMP_TO, _cd_weather_show_site, CD_APPLET_MY_MENU);
113 }
114 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Reload now"), GLDI_ICON_NAME_REFRESH, _cd_weather_reload, CD_APPLET_MY_MENU);
115 CD_APPLET_ON_BUILD_MENU_END
116
117
118 CD_APPLET_ON_MIDDLE_CLICK_BEGIN
119 if (pClickedIcon == myIcon)
120 {
121 cd_weather_show_current_conditions_dialog (myApplet);
122 }
123 CD_APPLET_ON_MIDDLE_CLICK_END
124
125
126 CD_APPLET_ON_DOUBLE_CLICK_BEGIN
127 if (pClickedIcon != NULL)
128 {
129 gldi_dialogs_remove_on_icon (pClickedIcon);
130 int iNumDay = _get_num_day_from_icon (myApplet, pClickedIcon);
131 _go_to_site (myApplet, iNumDay);
132 }
133 CD_APPLET_ON_DOUBLE_CLICK_END
134
135
136 void cd_weather_show_forecast_dialog (GldiModuleInstance *myApplet, Icon *pIcon)
137 {
138 // remove any other forecast dialog.
139 if (myDock != NULL)
140 g_list_foreach (myIcon->pSubDock->icons, (GFunc) gldi_dialogs_remove_on_icon, NULL);
141 else
142 gldi_dialogs_remove_on_icon (myIcon);
143
144 // if we never got any result, show an error message. If we lost the connection, but could get some data beforehand, we'll just present the old data, since they are not likely to change very often.
145 if (myData.wdata.cCity == NULL)
146 {
147 gldi_dialog_show_temporary_with_icon (D_("No data available\n is your connection alive?"),
148 (myDock ? pIcon : myIcon),
149 (myDock ? CAIRO_CONTAINER (myIcon->pSubDock) : myContainer),
150 myConfig.cDialogDuration,
151 "same icon");
152 return ;
153 }
154
155 // present the day's forecast.
156 int iNumDay = ((int) pIcon->fOrder) / 2;
157 g_return_if_fail (iNumDay < myConfig.iNbDays);
158
159 Day *day = &myData.wdata.days[iNumDay];
160 gldi_dialog_show_temporary_with_icon_printf ("%s (%s) : %s\n %s : %s%s -> %s%s",
161 (myDock ? pIcon : myIcon),
162 (myDock ? CAIRO_CONTAINER (myIcon->pSubDock) : myContainer),
163 myConfig.cDialogDuration,
164 "same icon",
165 day->cName, day->cDate, day->cWeatherDescription,
166 D_("Temperature"), _display (day->cTempMin), myData.wdata.units.cTemp, _display (day->cTempMax), myData.wdata.units.cTemp);
167 }
168
169 void cd_weather_show_current_conditions_dialog (GldiModuleInstance *myApplet)
170 {
171 gldi_dialogs_remove_on_icon (myIcon);
172
173 // if an error occured, the current conditions are no more valid.
174 if (gldi_task_is_running (myData.pTask)) // current conditions are outdated.
175 {
176 gldi_dialog_show_temporary_with_icon (D_("Data are being fetched, please re-try in a few seconds."),
177 myIcon,
178 myContainer,
179 3000,
180 "same icon");
181 return;
182 }
183
184 if (myData.bErrorRetrievingData)
185 {
186 gldi_dialog_show_temporary_with_icon (D_("No data available\nRetrying now..."),
187 myIcon,
188 myContainer,
189 3000,
190 myIcon->cFileName);
191 _reload (myApplet);
192 return ;
193 }
194
195 // show a dialog with the current conditions.
196 CurrentContitions *cc = &myData.wdata.currentConditions;
197 gldi_dialog_show_temporary_with_icon_printf ("%s:\n %s : %s%s\n %s : %s%s\n %s : %s - %s : %s%s\n %s : %s %s %s",
198 myIcon, myContainer, myConfig.cDialogDuration, myIcon->cFileName,
199 cc->cDataAcquisitionDate,
200 D_("Temperature"), _display (cc->now.cTempMax), myData.wdata.units.cTemp,
201 D_("Wind"), _display (cc->cWindSpeed), myData.wdata.units.cSpeed,
202 D_("Humidity"), _display (cc->cHumidity), D_("Pressure"), _display (cc->cPressure), myData.wdata.units.cPressure,
203 D_("Sunrise"), _display (cc->cSunRise), D_("Sunset"), _display (cc->cSunSet));
204 }