| 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 <string.h> |
| 21 |
|
| 22 |
#include "applet-struct.h" |
| 23 |
#include "applet-read-data.h" |
| 24 |
#include "applet-load-icons.h" |
| 25 |
|
| 26 |
const char *cMonthsWeeks[19] = { N_("Monday") , N_("Tuesday") , N_("Wednesday") , N_("Thursday") , N_("Friday") , N_("Saturday") , N_("Sunday") , N_("Jan") , N_("Feb") , N_("Mar") , N_("Apr") , N_("May") ,N_("Jun") , N_("Jui") , N_("Aug") , N_("Sep") , N_("Oct") , N_("Nov") , N_("Dec") }; // pour qu'ils soient listes dans le .pot. |
| 27 |
|
| 28 |
#define _add_icon(i, j)\ |
| 29 |
if (myData.wdata.days[i].cName != NULL)\ |
| 30 |
{\ |
| 31 |
pIcon = cairo_dock_create_dummy_launcher (g_strdup ((gchar *)myData.wdata.days[i].cName),\ |
| 32 |
g_strdup_printf ("%s/%s.png", myConfig.cThemePath, myData.wdata.days[i].cIconNumber),\ |
| 33 |
NULL,\ |
| 34 |
(myConfig.bDisplayTemperature ? g_strdup_printf ("%s/%s", _display (myData.wdata.days[i].cTempMin), _display (myData.wdata.days[i].cTempMax)) : NULL),\ |
| 35 |
2*i+j);\ |
| 36 |
if (! g_file_test (pIcon->cFileName, G_FILE_TEST_EXISTS))\ |
| 37 |
{\ |
| 38 |
g_free (pIcon->cFileName);\ |
| 39 |
pIcon->cFileName = g_strdup_printf ("%s/%s.svg", myConfig.cThemePath, myData.wdata.days[i].cIconNumber);\ |
| 40 |
}\ |
| 41 |
cairo_dock_listen_for_double_click (pIcon);\ |
| 42 |
pIconList = g_list_append (pIconList, pIcon);\ |
| 43 |
} |
| 44 |
|
| 45 |
static GList * _list_icons (GldiModuleInstance *myApplet) |
| 46 |
{ |
| 47 |
GList *pIconList = NULL; |
| 48 |
|
| 49 |
Icon *pIcon; |
| 50 |
int i; |
| 51 |
for (i = 0; i < myConfig.iNbDays; i ++) |
| 52 |
{ |
| 53 |
_add_icon (i, 0); |
| 54 |
} |
| 55 |
|
| 56 |
return pIconList; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
static void _weather_draw_current_conditions (GldiModuleInstance *myApplet) |
| 61 |
{ |
| 62 |
if (myConfig.bCurrentConditions || myData.bErrorRetrievingData) |
| 63 |
{ |
| 64 |
cd_message (" chargement de l'icone meteo (%x)", myApplet); |
| 65 |
if (myConfig.bDisplayTemperature && myData.wdata.currentConditions.now.cTempMax != NULL) |
| 66 |
{ |
| 67 |
CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%s%s", myData.wdata.currentConditions.now.cTempMax, myData.wdata.units.cTemp); |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL); |
| 72 |
} |
| 73 |
|
| 74 |
g_free (myIcon->cFileName); |
| 75 |
if (myData.bErrorRetrievingData) |
| 76 |
{ |
| 77 |
myIcon->cFileName = g_strdup_printf ("%s/na.png", myConfig.cThemePath); |
| 78 |
if (! g_file_test (myIcon->cFileName, G_FILE_TEST_EXISTS)) |
| 79 |
{ |
| 80 |
g_free (myIcon->cFileName); |
| 81 |
myIcon->cFileName = g_strdup_printf ("%s/na.svg", myConfig.cThemePath); |
| 82 |
if (! g_file_test (myIcon->cFileName, G_FILE_TEST_EXISTS)) |
| 83 |
{ |
| 84 |
g_free (myIcon->cFileName); |
| 85 |
myIcon->cFileName = g_strdup (MY_APPLET_SHARE_DATA_DIR"/broken.png"); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
myIcon->cFileName = g_strdup_printf ("%s/%s.png", myConfig.cThemePath, myData.wdata.currentConditions.now.cIconNumber); |
| 92 |
if (! g_file_test (myIcon->cFileName, G_FILE_TEST_EXISTS)) |
| 93 |
{ |
| 94 |
g_free (myIcon->cFileName); |
| 95 |
myIcon->cFileName = g_strdup_printf ("%s/%s.svg", myConfig.cThemePath, myData.wdata.currentConditions.now.cIconNumber); |
| 96 |
} |
| 97 |
} |
| 98 |
CD_APPLET_SET_IMAGE_ON_MY_ICON (myIcon->cFileName); |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
CD_APPLET_SET_DEFAULT_IMAGE_ON_MY_ICON_IF_NONE; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
gboolean cd_weather_update_from_data (CDSharedMemory *pSharedMemory) |
| 109 |
{ |
| 110 |
GldiModuleInstance *myApplet = pSharedMemory->pApplet; |
| 111 |
g_return_val_if_fail (myIcon != NULL, FALSE); // paranoia |
| 112 |
CD_APPLET_ENTER; |
| 113 |
|
| 114 |
if (myData.bBusy) |
| 115 |
{ |
| 116 |
myData.bBusy = FALSE; |
| 117 |
CD_APPLET_STOP_ANIMATING_MY_ICON; |
| 118 |
} |
| 119 |
|
| 120 |
//\_______________________ in case an error occured, keep the current data, and just redraw the main icon. |
| 121 |
if (pSharedMemory->bErrorInThread) |
| 122 |
{ |
| 123 |
if (!myData.bErrorRetrievingData) // no previous error, draw an emblem. |
| 124 |
{ |
| 125 |
myData.bErrorRetrievingData = TRUE; |
| 126 |
|
| 127 |
_weather_draw_current_conditions (myApplet); // draw the icon, in case we never drawn the icon before. |
| 128 |
|
| 129 |
// retry in 20s, in case it's just a temporary network loss, or a slow connection on startup. |
| 130 |
if (myData.pTask->iPeriod > 20) |
| 131 |
{ |
| 132 |
cd_message ("no data, will re-try in 20s"); |
| 133 |
gldi_task_change_frequency (myData.pTask, 20); |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
cd_weather_reset_weather_data (&pSharedMemory->wdata); // discard the results, since they are probably empty or incomplete. |
| 138 |
memset (&pSharedMemory->wdata, 0, sizeof (CDWeatherData)); |
| 139 |
|
| 140 |
CD_APPLET_LEAVE (TRUE); // don't recreate the icons, since data have not changed. |
| 141 |
} |
| 142 |
myData.bErrorRetrievingData = FALSE; |
| 143 |
|
| 144 |
//\_______________________ copy the shared memory into our data. |
| 145 |
// free the current saved data. |
| 146 |
cd_weather_reset_data (myApplet); |
| 147 |
|
| 148 |
// then save the new data. |
| 149 |
memcpy (&myData.wdata, &pSharedMemory->wdata, sizeof (CDWeatherData)); |
| 150 |
|
| 151 |
// and clear the shared memory. |
| 152 |
memset (&pSharedMemory->wdata, 0, sizeof (CDWeatherData)); |
| 153 |
|
| 154 |
//\_______________________ On etablit le nom de l'icone. |
| 155 |
if ((myIcon->cName == NULL || myData.bSetName) && myDock) |
| 156 |
{ |
| 157 |
myData.bSetName = (myData.wdata.cCity == NULL); // cas ou l'applet demarre avant l'etablissesment de la connexion. |
| 158 |
gchar *cLocation = g_strdup_printf("%s, %s", myData.wdata.cCity, myData.wdata.cCountry); |
| 159 |
CD_APPLET_SET_NAME_FOR_MY_ICON (myData.wdata.cCity != NULL ? cLocation : WEATHER_DEFAULT_NAME); |
| 160 |
g_free (cLocation); |
| 161 |
} |
| 162 |
|
| 163 |
//\_______________________ On cree la liste des icones de prevision. |
| 164 |
GList *pIconList = _list_icons (myApplet); // ne nous appartiendra plus, donc ne pas desallouer. |
| 165 |
|
| 166 |
//\_______________________ On efface l'ancienne liste. |
| 167 |
CD_APPLET_DELETE_MY_ICONS_LIST; |
| 168 |
|
| 169 |
//\_______________________ On charge la nouvelle liste. |
| 170 |
if (pIconList != NULL) |
| 171 |
{ |
| 172 |
///gpointer pConfig[2] = {GINT_TO_POINTER (myConfig.bDesklet3D), GINT_TO_POINTER (FALSE)}; |
| 173 |
///CD_APPLET_LOAD_MY_ICONS_LIST (pIconList, myConfig.cRenderer, "Caroussel", pConfig); |
| 174 |
gdouble white[4] = {1., 1., 1., .4}; |
| 175 |
gpointer pConfig[3] = {GINT_TO_POINTER (1), GINT_TO_POINTER (FALSE), white}; |
| 176 |
CD_APPLET_LOAD_MY_ICONS_LIST (pIconList, myConfig.cRenderer, "Panel", pConfig); |
| 177 |
} |
| 178 |
else if (myDock) // sinon on ne veut pas du sous-dock vide. |
| 179 |
{ |
| 180 |
gldi_object_unref (GLDI_OBJECT(myIcon->pSubDock)); |
| 181 |
myIcon->pSubDock = NULL; |
| 182 |
} |
| 183 |
if (myDesklet) |
| 184 |
CD_APPLET_ALLOW_NO_CLICKABLE_DESKLET; |
| 185 |
|
| 186 |
//\_______________________ On recharge l'icone principale. |
| 187 |
_weather_draw_current_conditions (myApplet); // ne lance pas le redraw. |
| 188 |
CD_APPLET_REDRAW_MY_ICON; |
| 189 |
|
| 190 |
if (myData.wdata.currentConditions.ttl <= 0) |
| 191 |
myData.wdata.currentConditions.ttl = 60; // 1h by default |
| 192 |
cd_message ("next fetching in %dmn", myData.wdata.currentConditions.ttl); |
| 193 |
gldi_task_change_frequency (myData.pTask, myData.wdata.currentConditions.ttl * 60); |
| 194 |
|
| 195 |
CD_APPLET_LEAVE (TRUE); |
| 196 |
} |