~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to weather/src/applet-config.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
ImportĀ upstreamĀ versionĀ 2.0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program,
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by Fabrice Rey (for any bug report, please mail me to fabounet@users.berlios.de)
 
26
Fabrice Rey (fabounet@users.berlios.de)
 
27
 
 
28
******************************************************************************/
 
29
#include <string.h>
 
30
#include <glib/gstdio.h>
 
31
 
 
32
#include "applet-struct.h"
 
33
#include "applet-read-data.h"
 
34
#include "applet-config.h"
 
35
 
 
36
GList *s_pLocationsList = NULL;
 
37
 
 
38
CD_APPLET_GET_CONFIG_BEGIN
 
39
        //\_________________ On recupere toutes les valeurs de notre fichier de conf.
 
40
        myConfig.cLocationCode = CD_CONFIG_GET_STRING_WITH_DEFAULT ("Configuration", "location code", "FRXX0076");
 
41
        myConfig.bISUnits = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "IS units", TRUE);
 
42
        myConfig.bCurrentConditions = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "display cc", TRUE);
 
43
        myConfig.bDisplayNights = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "display nights", FALSE);
 
44
        myConfig.iNbDays = MIN (CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "nb days", WEATHER_NB_DAYS_MAX), WEATHER_NB_DAYS_MAX);
 
45
        myConfig.bDisplayTemperature = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "display temperature", FALSE);
 
46
        myConfig.cDialogDuration = 1000 * CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "dialog duration", 5);
 
47
        myConfig.iCheckInterval = 60 * MAX (CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "check interval", 15), 1);
 
48
        
 
49
        myConfig.cThemePath = CD_CONFIG_GET_THEME_PATH ("Configuration", "theme", "themes", "Classic");
 
50
        
 
51
        myConfig.bDesklet3D = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "3D desket", FALSE);
 
52
        
 
53
        myConfig.cRenderer = CD_CONFIG_GET_STRING ("Configuration", "renderer");
 
54
        
 
55
        gchar *cName = CD_CONFIG_GET_STRING ("Icon", "name");
 
56
        myConfig.bSetName = (cName == NULL);
 
57
        g_free (cName);
 
58
        ///myConfig.iDeskletRenderer = CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "current desklet", 0);
 
59
CD_APPLET_GET_CONFIG_END
 
60
 
 
61
 
 
62
static void _reset_units (Unit *pUnits)
 
63
{
 
64
        g_free (pUnits->cTemp);
 
65
        g_free (pUnits->cDistance);
 
66
        g_free (pUnits->cSpeed);
 
67
        g_free (pUnits->cPressure);
 
68
}
 
69
 
 
70
static void _reset_current_conditions (CurrentContitions *pCurrentContitions)
 
71
{
 
72
        g_free (pCurrentContitions->cSunRise);
 
73
        g_free (pCurrentContitions->cSunSet);
 
74
        g_free (pCurrentContitions->cDataAcquisitionDate);
 
75
        g_free (pCurrentContitions->cObservatory);
 
76
        g_free (pCurrentContitions->cTemp);
 
77
        g_free (pCurrentContitions->cFeeledTemp);
 
78
        g_free (pCurrentContitions->cWeatherDescription);
 
79
        g_free (pCurrentContitions->cIconNumber);
 
80
        g_free (pCurrentContitions->cWindSpeed);
 
81
        g_free (pCurrentContitions->cWindDirection);
 
82
        g_free (pCurrentContitions->cPressure);
 
83
        g_free (pCurrentContitions->cHumidity);
 
84
        g_free (pCurrentContitions->cMoonIconNumber);
 
85
}
 
86
 
 
87
static void _reset_current_one_day (Day *pDay)
 
88
{
 
89
        g_free (pDay->cName);
 
90
        g_free (pDay->cDate);
 
91
        g_free (pDay->cTempMax);
 
92
        g_free (pDay->cTempMin);
 
93
        g_free (pDay->cSunRise);
 
94
        g_free (pDay->cSunSet);
 
95
        int j;
 
96
        for (j = 0; j < 2; j ++)
 
97
        {
 
98
                g_free (pDay->part[j].cIconNumber);
 
99
                g_free (pDay->part[j].cWeatherDescription);
 
100
                g_free (pDay->part[j].cWindSpeed);
 
101
                g_free (pDay->part[j].cWindDirection);
 
102
                g_free (pDay->part[j].cHumidity);
 
103
                g_free (pDay->part[j].cPrecipitationProba);
 
104
        }
 
105
}
 
106
 
 
107
CD_APPLET_RESET_CONFIG_BEGIN
 
108
        g_free (myConfig.cLocationCode);
 
109
        g_free (myConfig.cRenderer);
 
110
        g_free (myConfig.cThemePath);
 
111
CD_APPLET_RESET_CONFIG_END
 
112
 
 
113
 
 
114
void cd_weather_reset_all_datas (CairoDockModuleInstance *myApplet)
 
115
{
 
116
        cairo_dock_free_task (myData.pTask);
 
117
        
 
118
        g_free (myData.cLon);
 
119
        g_free (myData.cLat);
 
120
        _reset_units (&myData.units);
 
121
        _reset_current_conditions (&myData.currentConditions);
 
122
        int i;
 
123
        for (i = 0; i < myConfig.iNbDays; i ++)
 
124
        {
 
125
                _reset_current_one_day (&myData.days[i]);
 
126
        }
 
127
        
 
128
        cd_weather_free_location_list ();
 
129
        
 
130
        CD_APPLET_DELETE_MY_ICONS_LIST;
 
131
        
 
132
        memset (myDataPtr, 0, sizeof (AppletData));
 
133
}
 
134
 
 
135
CD_APPLET_RESET_DATA_BEGIN
 
136
        cd_weather_reset_all_datas (myApplet);
 
137
CD_APPLET_RESET_DATA_END
 
138
 
 
139
 
 
140
  /////////////////////
 
141
 /// CUSTOM WIDGET ///
 
142
/////////////////////
 
143
void cd_weather_free_location_list (void)
 
144
{
 
145
        if (s_pLocationsList == NULL)
 
146
                return ;
 
147
        g_list_foreach (s_pLocationsList, (GFunc) g_free, NULL);
 
148
        g_list_free (s_pLocationsList);
 
149
        s_pLocationsList = NULL;
 
150
}
 
151
 
 
152
static void _cd_weather_location_choosed (GtkMenuItem *pMenuItem, gchar *cLocationCode)
 
153
{
 
154
        g_return_if_fail (cLocationCode != NULL);
 
155
        
 
156
        //\____________________ On met a jour le panneau de conf.
 
157
        GtkWidget *pCodeEntry = cairo_dock_get_widget_from_name ("Configuration", "location code");
 
158
        gtk_entry_set_text (GTK_ENTRY (pCodeEntry), cLocationCode);
 
159
        cd_weather_free_location_list ();
 
160
}
 
161
static void _cd_weather_search_for_location (GtkEntry *pEntry, CairoDockModuleInstance *myApplet)
 
162
{
 
163
        const gchar *cLocationName = gtk_entry_get_text (pEntry);
 
164
        if (cLocationName == NULL || *cLocationName == '\0')
 
165
                return;
 
166
        
 
167
        gchar *cFilePath = cd_weather_get_location_data (cLocationName);
 
168
        
 
169
        GError *erreur = NULL;
 
170
        cd_weather_free_location_list ();
 
171
        s_pLocationsList = cd_weather_parse_location_data (cFilePath, &erreur);
 
172
        if (erreur != NULL)
 
173
        {
 
174
                gchar *cIconPath = g_strdup_printf ("%s/broken.png", MY_APPLET_SHARE_DATA_DIR);
 
175
                cairo_dock_show_temporary_dialog_with_icon (D_("I couldn't get the info\n Is connexion alive ?"),
 
176
                        myIcon,
 
177
                        myContainer,
 
178
                        0,
 
179
                        cIconPath);
 
180
                g_free (cIconPath);
 
181
                
 
182
                g_error_free (erreur);
 
183
                erreur = NULL;  // on ne garde pas trace de l'erreur, c'est deja fait au (re)chargement.
 
184
        }
 
185
        else if (s_pLocationsList == NULL)
 
186
        {
 
187
                gchar *cIconPath = g_strdup_printf ("%s/broken.png", MY_APPLET_SHARE_DATA_DIR);
 
188
                cairo_dock_show_temporary_dialog_with_icon (D_("I couldn't find this location"),
 
189
                        myIcon,
 
190
                        myContainer,
 
191
                        0,
 
192
                        cIconPath);
 
193
                g_free (cIconPath);
 
194
                
 
195
                g_error_free (erreur);
 
196
                erreur = NULL;
 
197
        }
 
198
        else
 
199
        {
 
200
                GtkWidget *pMenu = gtk_menu_new ();
 
201
                GString *sOneLocation = g_string_new ("");
 
202
                GtkWidget *pMenuItem;
 
203
                gchar *cLocationName, *cLocationCode;
 
204
                GList *list, *data;
 
205
                for (list = s_pLocationsList; list != NULL; list = list->next)
 
206
                {
 
207
                        data = list;
 
208
                        cLocationCode = list->data;
 
209
                        list = list->next;
 
210
                        cLocationName = list->data;
 
211
                        
 
212
                        g_string_printf (sOneLocation, "%s : %s", cLocationName, cLocationCode);
 
213
                        pMenuItem = gtk_menu_item_new_with_label (sOneLocation->str);
 
214
                        gtk_menu_shell_append  (GTK_MENU_SHELL (pMenu), pMenuItem);
 
215
                        g_signal_connect (G_OBJECT (pMenuItem), "activate", G_CALLBACK(_cd_weather_location_choosed), cLocationCode);
 
216
                }
 
217
                g_string_free (sOneLocation, TRUE);
 
218
                
 
219
                gtk_widget_show_all (pMenu);
 
220
 
 
221
                gtk_menu_popup (GTK_MENU (pMenu),
 
222
                        NULL,
 
223
                        NULL,
 
224
                        NULL,
 
225
                        NULL,
 
226
                        1,
 
227
                        gtk_get_current_event_time ());
 
228
        }
 
229
        
 
230
        g_remove (cFilePath);
 
231
        g_free (cFilePath);
 
232
}
 
233
void cd_weather_load_custom_widget (CairoDockModuleInstance *myApplet, GKeyFile* pKeyFile)
 
234
{
 
235
        g_print ("%s (%s)\n", __func__, myIcon->acName);
 
236
        //\____________ On recupere le widget.
 
237
        GtkWidget *pCodeEntry = cairo_dock_get_widget_from_name ("Configuration", "location code");
 
238
        g_return_if_fail (pCodeEntry != NULL);
 
239
        
 
240
        GtkWidget *pWidgetBox = gtk_widget_get_parent (pCodeEntry);
 
241
        
 
242
        GtkWidget *pLabel = gtk_label_new (D_("Search for your location :"));
 
243
        gtk_box_pack_start (GTK_BOX (pWidgetBox), pLabel, FALSE, FALSE, 0);
 
244
        
 
245
        GtkWidget *pLocationEntry = gtk_entry_new ();
 
246
        gtk_widget_set_tooltip_text (pLocationEntry, D_("Enter the name of your location and press Enter to choose amongst results."));
 
247
        if (myData.cLocation != NULL)
 
248
                gtk_entry_set_text (GTK_ENTRY (pLocationEntry), myData.cLocation);
 
249
        gtk_box_pack_start (GTK_BOX (pWidgetBox), pLocationEntry, FALSE, FALSE, 0);
 
250
        g_signal_connect (pLocationEntry, "activate", G_CALLBACK (_cd_weather_search_for_location), myApplet);
 
251
}