| 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 |
#include <glib/gstdio.h> |
| 22 |
|
| 23 |
#include "applet-struct.h" |
| 24 |
#include "applet-read-data.h" |
| 25 |
#include "applet-config.h" |
| 26 |
|
| 27 |
GList *s_pLocationsList = NULL; |
| 28 |
|
| 29 |
CD_APPLET_GET_CONFIG_BEGIN |
| 30 |
//\_________________ On recupere toutes les valeurs de notre fichier de conf. |
| 31 |
myConfig.cLocationCode = CD_CONFIG_GET_STRING_WITH_DEFAULT ("Configuration", "location code", "FRXX0076"); |
| 32 |
myConfig.bISUnits = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "IS units", TRUE); |
| 33 |
myConfig.bCurrentConditions = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "display cc", TRUE); |
| 34 |
myConfig.iNbDays = MIN (CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "nb days", WEATHER_NB_DAYS_MAX), WEATHER_NB_DAYS_MAX); |
| 35 |
myConfig.bDisplayTemperature = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "display temperature", TRUE); |
| 36 |
myConfig.cDialogDuration = 1000 * CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "dialog duration", 7); |
| 37 |
|
| 38 |
myConfig.cThemePath = CD_CONFIG_GET_THEME_PATH ("Configuration", "theme", "themes", "Classic"); |
| 39 |
|
| 40 |
myConfig.bDesklet3D = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "3D desket", FALSE); |
| 41 |
|
| 42 |
myConfig.cRenderer = CD_CONFIG_GET_STRING ("Configuration", "renderer"); |
| 43 |
|
| 44 |
gchar *cName = CD_CONFIG_GET_STRING ("Icon", "name"); |
| 45 |
myConfig.bSetName = (cName == NULL); |
| 46 |
g_free (cName); |
| 47 |
CD_APPLET_GET_CONFIG_END |
| 48 |
|
| 49 |
|
| 50 |
CD_APPLET_RESET_CONFIG_BEGIN |
| 51 |
g_free (myConfig.cLocationCode); |
| 52 |
g_free (myConfig.cRenderer); |
| 53 |
g_free (myConfig.cThemePath); |
| 54 |
CD_APPLET_RESET_CONFIG_END |
| 55 |
|
| 56 |
|
| 57 |
void cd_weather_reset_all_datas (GldiModuleInstance *myApplet) |
| 58 |
{ |
| 59 |
gldi_task_discard (myData.pTask); |
| 60 |
gldi_task_discard (myData.pGetLocationTask); |
| 61 |
|
| 62 |
cd_weather_reset_data (myApplet); |
| 63 |
|
| 64 |
#ifdef CD_WEATHER_HAS_CODE_LOCATION |
| 65 |
cd_weather_free_location_list (); |
| 66 |
#endif |
| 67 |
|
| 68 |
CD_APPLET_DELETE_MY_ICONS_LIST; |
| 69 |
|
| 70 |
memset (myDataPtr, 0, sizeof (AppletData)); |
| 71 |
} |
| 72 |
|
| 73 |
CD_APPLET_RESET_DATA_BEGIN |
| 74 |
cd_weather_reset_all_datas (myApplet); |
| 75 |
CD_APPLET_RESET_DATA_END |
| 76 |
|
| 77 |
|
| 78 |
///////////////////// |
| 79 |
/// CUSTOM WIDGET /// |
| 80 |
///////////////////// |
| 81 |
#ifdef CD_WEATHER_HAS_CODE_LOCATION |
| 82 |
void cd_weather_free_location_list (void) |
| 83 |
{ |
| 84 |
if (s_pLocationsList == NULL) |
| 85 |
return ; |
| 86 |
g_list_foreach (s_pLocationsList, (GFunc) g_free, NULL); |
| 87 |
g_list_free (s_pLocationsList); |
| 88 |
s_pLocationsList = NULL; |
| 89 |
} |
| 90 |
|
| 91 |
static void _cd_weather_location_choosed (GtkMenuItem *pMenuItem, gchar *cLocationCode) |
| 92 |
{ |
| 93 |
g_return_if_fail (cLocationCode != NULL); |
| 94 |
|
| 95 |
//\____________________ On met a jour le panneau de conf. |
| 96 |
GldiModuleInstance *myApplet = g_object_get_data (G_OBJECT (pMenuItem), "cd-applet"); |
| 97 |
GtkWidget *pCodeEntry = myData.pCodeEntry; |
| 98 |
if (pCodeEntry) |
| 99 |
gtk_entry_set_text (GTK_ENTRY (pCodeEntry), cLocationCode); |
| 100 |
cd_weather_free_location_list (); |
| 101 |
} |
| 102 |
static void _on_got_location_data (const gchar *cLocationData, GldiModuleInstance *myApplet) |
| 103 |
{ |
| 104 |
GError *erreur = NULL; |
| 105 |
cd_weather_free_location_list (); |
| 106 |
|
| 107 |
GtkWidget *pCodeEntry = myData.pCodeEntry; |
| 108 |
if (!pCodeEntry) |
| 109 |
{ |
| 110 |
cd_debug ("request took too long, discard results"); |
| 111 |
gldi_task_discard (myData.pGetLocationTask); |
| 112 |
myData.pGetLocationTask = NULL; |
| 113 |
return; |
| 114 |
} |
| 115 |
cairo_dock_set_status_message (NULL, ""); |
| 116 |
|
| 117 |
s_pLocationsList = cd_weather_parse_location_data (cLocationData, &erreur); |
| 118 |
if (erreur != NULL) |
| 119 |
{ |
| 120 |
gchar *cIconPath = g_strdup_printf ("%s/broken.png", MY_APPLET_SHARE_DATA_DIR); |
| 121 |
gldi_dialog_show_temporary_with_icon (D_("I couldn't get the info\n Is connexion alive ?"), |
| 122 |
myIcon, |
| 123 |
myContainer, |
| 124 |
0, |
| 125 |
cIconPath); |
| 126 |
g_free (cIconPath); |
| 127 |
cairo_dock_set_status_message (NULL, D_("Couldn't get the location code (is connection alive?)")); |
| 128 |
|
| 129 |
g_error_free (erreur); |
| 130 |
erreur = NULL; // on ne garde pas trace de l'erreur, c'est deja fait au (re)chargement. |
| 131 |
} |
| 132 |
else if (s_pLocationsList == NULL) |
| 133 |
{ |
| 134 |
gchar *cIconPath = g_strdup_printf ("%s/broken.png", MY_APPLET_SHARE_DATA_DIR); |
| 135 |
gldi_dialog_show_temporary_with_icon (D_("I couldn't find this location"), |
| 136 |
myIcon, |
| 137 |
myContainer, |
| 138 |
0, |
| 139 |
cIconPath); |
| 140 |
g_free (cIconPath); |
| 141 |
cairo_dock_set_status_message (NULL, "Location not available"); |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
GtkWidget *pMenu = gtk_menu_new (); // inside the config window => use a normal GTK menu |
| 146 |
GString *sOneLocation = g_string_new (""); |
| 147 |
GtkWidget *pMenuItem; |
| 148 |
gchar *cLocationName, *cLocationCode; |
| 149 |
GList *list; |
| 150 |
for (list = s_pLocationsList; list != NULL; list = list->next) |
| 151 |
{ |
| 152 |
cLocationCode = list->data; |
| 153 |
list = list->next; |
| 154 |
cLocationName = list->data; |
| 155 |
|
| 156 |
g_string_printf (sOneLocation, "%s : %s", cLocationName, cLocationCode); |
| 157 |
pMenuItem = gtk_menu_item_new_with_label (sOneLocation->str); |
| 158 |
gtk_menu_shell_append (GTK_MENU_SHELL (pMenu), pMenuItem); |
| 159 |
g_object_set_data (G_OBJECT (pMenuItem), "cd-applet", myApplet); |
| 160 |
g_signal_connect (G_OBJECT (pMenuItem), "activate", G_CALLBACK(_cd_weather_location_choosed), cLocationCode); |
| 161 |
} |
| 162 |
g_string_free (sOneLocation, TRUE); |
| 163 |
|
| 164 |
gtk_widget_show_all (pMenu); |
| 165 |
|
| 166 |
gtk_menu_popup_at_widget (GTK_MENU (pMenu), |
| 167 |
pCodeEntry, |
| 168 |
GDK_GRAVITY_SOUTH, |
| 169 |
GDK_GRAVITY_NORTH, |
| 170 |
NULL); |
| 171 |
} |
| 172 |
|
| 173 |
gldi_task_discard (myData.pGetLocationTask); |
| 174 |
myData.pGetLocationTask = NULL; |
| 175 |
} |
| 176 |
|
| 177 |
static void _cd_weather_search_for_location (GtkEntry *pEntry, GldiModuleInstance *myApplet) |
| 178 |
{ |
| 179 |
const gchar *cLocationName = gtk_entry_get_text (pEntry); |
| 180 |
if (cLocationName == NULL || *cLocationName == '\0') |
| 181 |
return; |
| 182 |
|
| 183 |
cairo_dock_set_status_message_printf (NULL, D_("Searching the location code...")); |
| 184 |
|
| 185 |
if (myData.pGetLocationTask != NULL) |
| 186 |
{ |
| 187 |
gldi_task_discard (myData.pGetLocationTask); |
| 188 |
myData.pGetLocationTask = NULL; |
| 189 |
} |
| 190 |
gchar *cURL = g_strdup_printf (CD_WEATHER_BASE_URL"/search/search?where=%s", cLocationName); |
| 191 |
myData.pGetLocationTask = cairo_dock_get_url_data_async (cURL, (GFunc) _on_got_location_data, myApplet); |
| 192 |
g_free (cURL); |
| 193 |
} |
| 194 |
static void _on_destroyed_code_entry (GtkWidget *pEntry, GldiModuleInstance *myApplet) |
| 195 |
{ |
| 196 |
myData.pCodeEntry = NULL; |
| 197 |
} |
| 198 |
void cd_weather_load_custom_widget (GldiModuleInstance *myApplet, GKeyFile* pKeyFile, GSList *pWidgetList) |
| 199 |
{ |
| 200 |
if (!myApplet) // if called when the applet is not started |
| 201 |
return; |
| 202 |
cd_debug ("%s (%s)", __func__, myIcon->cName); |
| 203 |
//\____________ On recupere le widget. |
| 204 |
CairoDockGroupKeyWidget *pGroupKeyWidget = cairo_dock_gui_find_group_key_widget_in_list (pWidgetList, "Configuration", "location code"); |
| 205 |
GtkWidget *pCodeEntry = cairo_dock_gui_get_first_widget (pGroupKeyWidget); |
| 206 |
myData.pCodeEntry = pCodeEntry; |
| 207 |
g_return_if_fail (pCodeEntry != NULL); |
| 208 |
g_signal_connect (myData.pCodeEntry, "delete", G_CALLBACK (_on_destroyed_code_entry), myApplet); /// TODO: remove it on stop, to handle the case our applet is deactivated while the config window is opened. |
| 209 |
|
| 210 |
GtkWidget *pWidgetBox = gtk_widget_get_parent (pCodeEntry); |
| 211 |
|
| 212 |
GtkWidget *pLabel = gtk_label_new (D_("Search for your location :")); |
| 213 |
gtk_box_pack_start (GTK_BOX (pWidgetBox), pLabel, FALSE, FALSE, 0); |
| 214 |
|
| 215 |
GtkWidget *pLocationEntry = gtk_entry_new (); |
| 216 |
gtk_widget_set_tooltip_text (pLocationEntry, D_("Enter the name of your location and press Enter to choose amongst results.")); |
| 217 |
if (myData.wdata.cCity != NULL && myData.wdata.cCountry != NULL) |
| 218 |
gtk_entry_set_text (GTK_ENTRY (pLocationEntry), g_strdup_printf("%s, %s", myData.wdata.cCity, myData.wdata.cCountry)); |
| 219 |
gtk_box_pack_start (GTK_BOX (pWidgetBox), pLocationEntry, FALSE, FALSE, 0); |
| 220 |
g_signal_connect (pLocationEntry, "activate", G_CALLBACK (_cd_weather_search_for_location), myApplet); |
| 221 |
} |
| 222 |
#endif |