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

1 by Didier Roche
Import upstream version 2.0.8.1
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.bDisplayNights = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "display nights", FALSE);
35
	myConfig.iNbDays = MIN (CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "nb days", WEATHER_NB_DAYS_MAX), WEATHER_NB_DAYS_MAX);
1.1.5 by Matthieu Baerts (matttbe)
Import upstream version 2.2.0~0beta4
36
	myConfig.bDisplayTemperature = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "display temperature", TRUE);
1.1.2 by Julien Lavergne
Import upstream version 2.0.9
37
	myConfig.cDialogDuration = 1000 * CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "dialog duration", 7);
1 by Didier Roche
Import upstream version 2.0.8.1
38
	myConfig.iCheckInterval = 60 * MAX (CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "check interval", 15), 1);
39
	
40
	myConfig.cThemePath = CD_CONFIG_GET_THEME_PATH ("Configuration", "theme", "themes", "Classic");
41
	
42
	myConfig.bDesklet3D = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "3D desket", FALSE);
43
	
44
	myConfig.cRenderer = CD_CONFIG_GET_STRING ("Configuration", "renderer");
45
	
46
	gchar *cName = CD_CONFIG_GET_STRING ("Icon", "name");
47
	myConfig.bSetName = (cName == NULL);
48
	g_free (cName);
49
CD_APPLET_GET_CONFIG_END
50
51
52
CD_APPLET_RESET_CONFIG_BEGIN
53
	g_free (myConfig.cLocationCode);
54
	g_free (myConfig.cRenderer);
55
	g_free (myConfig.cThemePath);
56
CD_APPLET_RESET_CONFIG_END
57
58
59
void cd_weather_reset_all_datas (CairoDockModuleInstance *myApplet)
60
{
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
61
	cairo_dock_discard_task (myData.pTask);
62
	cairo_dock_discard_task (myData.pGetLocationTask);
1 by Didier Roche
Import upstream version 2.0.8.1
63
	
1.1.3 by Matthieu Baerts (matttbe)
Import upstream version 2.1.3-6
64
	cd_weather_reset_data (myApplet);
1 by Didier Roche
Import upstream version 2.0.8.1
65
	
66
	cd_weather_free_location_list ();
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
void cd_weather_free_location_list (void)
82
{
83
	if (s_pLocationsList == NULL)
84
		return ;
85
	g_list_foreach (s_pLocationsList, (GFunc) g_free, NULL);
86
	g_list_free (s_pLocationsList);
87
	s_pLocationsList = NULL;
88
}
89
90
static void _cd_weather_location_choosed (GtkMenuItem *pMenuItem, gchar *cLocationCode)
91
{
92
	g_return_if_fail (cLocationCode != NULL);
93
	
94
	//\____________________ On met a jour le panneau de conf.
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
95
	CairoDockModuleInstance *myApplet = g_object_get_data (G_OBJECT (pMenuItem), "cd-applet");
1.1.22 by Matthieu Baerts
Import upstream version 3.0.99.beta1
96
	GtkWidget *pCodeEntry = myData.pCodeEntry;
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
97
	if (pCodeEntry)
98
		gtk_entry_set_text (GTK_ENTRY (pCodeEntry), cLocationCode);
1 by Didier Roche
Import upstream version 2.0.8.1
99
	cd_weather_free_location_list ();
100
}
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
101
static void _on_got_location_data (const gchar *cLocationData, CairoDockModuleInstance *myApplet)
1 by Didier Roche
Import upstream version 2.0.8.1
102
{
103
	GError *erreur = NULL;
104
	cd_weather_free_location_list ();
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
105
	
1.1.22 by Matthieu Baerts
Import upstream version 3.0.99.beta1
106
	GtkWidget *pCodeEntry = myData.pCodeEntry;
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
107
	if (!pCodeEntry)
108
	{
109
		cd_debug ("request took too long, discard results");
110
		cairo_dock_discard_task (myData.pGetLocationTask);
111
		myData.pGetLocationTask = NULL;
112
		return;
113
	}
114
	cairo_dock_set_status_message (NULL, "");
115
	
1.1.5 by Matthieu Baerts (matttbe)
Import upstream version 2.2.0~0beta4
116
	s_pLocationsList = cd_weather_parse_location_data (cLocationData, &erreur);
1 by Didier Roche
Import upstream version 2.0.8.1
117
	if (erreur != NULL)
118
	{
119
		gchar *cIconPath = g_strdup_printf ("%s/broken.png", MY_APPLET_SHARE_DATA_DIR);
120
		cairo_dock_show_temporary_dialog_with_icon (D_("I couldn't get the info\n Is connexion alive ?"),
121
			myIcon,
122
			myContainer,
123
			0,
124
			cIconPath);
125
		g_free (cIconPath);
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
126
		cairo_dock_set_status_message (NULL, D_("Couldn't get the location code (is connection alive?)"));
1 by Didier Roche
Import upstream version 2.0.8.1
127
		
128
		g_error_free (erreur);
129
		erreur = NULL;  // on ne garde pas trace de l'erreur, c'est deja fait au (re)chargement.
130
	}
131
	else if (s_pLocationsList == NULL)
132
	{
133
		gchar *cIconPath = g_strdup_printf ("%s/broken.png", MY_APPLET_SHARE_DATA_DIR);
134
		cairo_dock_show_temporary_dialog_with_icon (D_("I couldn't find this location"),
135
			myIcon,
136
			myContainer,
137
			0,
138
			cIconPath);
139
		g_free (cIconPath);
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
140
		cairo_dock_set_status_message (NULL, "Location not available");
1 by Didier Roche
Import upstream version 2.0.8.1
141
	}
142
	else
143
	{
144
		GtkWidget *pMenu = gtk_menu_new ();
145
		GString *sOneLocation = g_string_new ("");
146
		GtkWidget *pMenuItem;
147
		gchar *cLocationName, *cLocationCode;
148
		GList *list, *data;
149
		for (list = s_pLocationsList; list != NULL; list = list->next)
150
		{
151
			data = list;
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);
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
159
			g_object_set_data (G_OBJECT (pMenuItem), "cd-applet", myApplet);
1 by Didier Roche
Import upstream version 2.0.8.1
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 (GTK_MENU (pMenu),
167
			NULL,
168
			NULL,
169
			NULL,
170
			NULL,
171
			1,
172
			gtk_get_current_event_time ());
173
	}
174
	
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
175
	cairo_dock_discard_task (myData.pGetLocationTask);
176
	myData.pGetLocationTask = NULL;
177
}
178
#define CD_WEATHER_BASE_URL "http://xml.weather.com"
179
static void _cd_weather_search_for_location (GtkEntry *pEntry, CairoDockModuleInstance *myApplet)
180
{
181
	const gchar *cLocationName = gtk_entry_get_text (pEntry);
182
	if (cLocationName == NULL || *cLocationName == '\0')
183
		return;
184
	
185
	cairo_dock_set_status_message_printf (NULL, D_("Searching the location code..."));
186
	
187
	if (myData.pGetLocationTask != NULL)
188
	{
189
		cairo_dock_discard_task (myData.pGetLocationTask);
190
		myData.pGetLocationTask = NULL;
191
	}
1.1.18 by Matthieu Baerts (matttbe)
Import upstream version 3.0.0.0beta1
192
	gchar *cURL = g_strdup_printf (CD_WEATHER_BASE_URL"/search/search?where=%s", cLocationName);
193
	myData.pGetLocationTask = cairo_dock_get_url_data_async (cURL, (GFunc) _on_got_location_data, myApplet);
194
	g_free (cURL);
1 by Didier Roche
Import upstream version 2.0.8.1
195
}
1.1.22 by Matthieu Baerts
Import upstream version 3.0.99.beta1
196
static void _on_destroyed_code_entry (GtkWidget *pEntry, CairoDockModuleInstance *myApplet)
197
{
198
	myData.pCodeEntry = NULL;
199
}
200
void cd_weather_load_custom_widget (CairoDockModuleInstance *myApplet, GKeyFile* pKeyFile, GSList *pWidgetList)
1 by Didier Roche
Import upstream version 2.0.8.1
201
{
1.1.18 by Matthieu Baerts (matttbe)
Import upstream version 3.0.0.0beta1
202
	if (!myApplet)  // if called when the applet is not started
203
		return;
1.1.5 by Matthieu Baerts (matttbe)
Import upstream version 2.2.0~0beta4
204
	cd_debug ("%s (%s)\n", __func__, myIcon->cName);
1 by Didier Roche
Import upstream version 2.0.8.1
205
	//\____________ On recupere le widget.
1.1.22 by Matthieu Baerts
Import upstream version 3.0.99.beta1
206
	CairoDockGroupKeyWidget *pGroupKeyWidget = cairo_dock_gui_find_group_key_widget_in_list (pWidgetList, "Configuration", "location code");
207
	GtkWidget *pCodeEntry = cairo_dock_gui_get_first_widget (pGroupKeyWidget);
208
	myData.pCodeEntry = pCodeEntry;
1 by Didier Roche
Import upstream version 2.0.8.1
209
	g_return_if_fail (pCodeEntry != NULL);
1.1.22 by Matthieu Baerts
Import upstream version 3.0.99.beta1
210
	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.
1 by Didier Roche
Import upstream version 2.0.8.1
211
	
212
	GtkWidget *pWidgetBox = gtk_widget_get_parent (pCodeEntry);
213
	
214
	GtkWidget *pLabel = gtk_label_new (D_("Search for your location :"));
215
	gtk_box_pack_start (GTK_BOX (pWidgetBox), pLabel, FALSE, FALSE, 0);
216
	
217
	GtkWidget *pLocationEntry = gtk_entry_new ();
218
	gtk_widget_set_tooltip_text (pLocationEntry, D_("Enter the name of your location and press Enter to choose amongst results."));
1.1.9 by Matthieu Baerts (matttbe)
Import upstream version 2.3.0~1
219
	if (myData.wdata.cLocation != NULL)
220
		gtk_entry_set_text (GTK_ENTRY (pLocationEntry), myData.wdata.cLocation);
1 by Didier Roche
Import upstream version 2.0.8.1
221
	gtk_box_pack_start (GTK_BOX (pWidgetBox), pLocationEntry, FALSE, FALSE, 0);
222
	g_signal_connect (pLocationEntry, "activate", G_CALLBACK (_cd_weather_search_for_location), myApplet);
223
}