Fix compilation of "weather" plugin.
[lxde/lxpanel.git] / src / plugins / weather / weather.c
CommitLineData
f0a22461
PS
1/**
2 * Copyright (c) 2012-2013 Piotr Sipika; see the AUTHORS file for more.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * See the COPYRIGHT file for more information.
19 */
20
21#include "location.h"
22#include "weatherwidget.h"
23#include "yahooutil.h"
24#include "logutil.h"
25
01f6d691 26#include "../../private.h"
f0a22461
PS
27
28#include <glib.h>
29#include <glib/gi18n.h>
30#include <gtk/gtk.h>
31#include <stdlib.h>
32
8ac3d56f
PS
33/* External button-press handler from plugin.c */
34extern gboolean plugin_button_press_event(GtkWidget *widget, GdkEventButton *event, Plugin *plugin);
35
f0a22461
PS
36/* Need to maintain count for bookkeeping */
37static gint g_iCount = 0;
38
39typedef struct
40{
41 gint iMyId_;
42 GtkWidget * pWeather_;
43} WeatherPluginPrivate;
44
45/**
46 * Weather Plugin constructor
47 *
48 * @param pPlugin Pointer to the PluginClass wrapper instance.
49 * @param pcFP Pointer to the configuration file position for this plugin.
50 *
51 * @return 1 (TRUE) on success, 0 (FALSE) on failure.
52 */
53static int
54weather_constructor(Plugin * pPlugin, char ** pFP)
55{
56 WeatherPluginPrivate * pPriv = g_new0(WeatherPluginPrivate, 1);
57
58 /* There is one more now... */
59 ++g_iCount;
60
61 pPriv->iMyId_ = g_iCount;
62
63 if (g_iCount == 1)
64 {
65 initializeLogUtil("syslog");
66
67 setMaxLogLevel(LXW_ERROR);
68
69 initializeYahooUtil();
70 }
71
72 LXW_LOG(LXW_DEBUG, "weather_constructor()");
73
8ac3d56f 74 GtkWidget * pWidg = gtk_weather_new(FALSE);
f0a22461
PS
75
76 pPriv->pWeather_ = pWidg;
77
78 GtkWidget * pEventBox = gtk_event_box_new();
79
8ac3d56f
PS
80 /* Connect signals. */
81 g_signal_connect(pEventBox,
82 "button-press-event",
83 G_CALLBACK(plugin_button_press_event),
84 pPlugin);
f0a22461
PS
85
86 gtk_container_add(GTK_CONTAINER(pEventBox), pWidg);
8ac3d56f 87
f0a22461
PS
88 pPlugin->priv = pPriv;
89 pPlugin->pwid = pEventBox;
90
91 gtk_widget_set_has_window(pPlugin->pwid,FALSE);
92
93 gtk_widget_show_all(pPlugin->pwid);
94
95 /* use config, see lxpanel_get_line, below */
96 LocationInfo * pLocation = g_new0(LocationInfo, 1);
97
98 line l;
99
100 l.len = 256;
101
102 if (pFP)
103 {
104 while (lxpanel_get_line(pFP, &l) != LINE_BLOCK_END)
105 {
106 if (l.type == LINE_VAR)
107 {
108 if (!g_ascii_strcasecmp(l.t[0], "alias"))
109 {
110 pLocation->pcAlias_ = g_strndup(l.t[1], strlen(l.t[1]));
111 }
112 else if (!g_ascii_strcasecmp(l.t[0], "city"))
113 {
114 pLocation->pcCity_ = g_strndup(l.t[1], strlen(l.t[1]));
115 }
116 else if (!g_ascii_strcasecmp(l.t[0], "state"))
117 {
118 pLocation->pcState_ = g_strndup(l.t[1], strlen(l.t[1]));
119 }
120 else if (!g_ascii_strcasecmp(l.t[0], "country"))
121 {
122 pLocation->pcCountry_ = g_strndup(l.t[1], strlen(l.t[1]));
123 }
124 else if (!g_ascii_strcasecmp(l.t[0], "woeid"))
125 {
126 pLocation->pcWOEID_ = g_strndup(l.t[1], strlen(l.t[1]));
127 }
128 else if (!g_ascii_strcasecmp(l.t[0], "units"))
129 {
130 pLocation->cUnits_ = *(l.t[1]);
131 }
132 else if (!g_ascii_strcasecmp(l.t[0], "interval"))
133 {
134 pLocation->uiInterval_ = (guint)abs((gint)g_ascii_strtoll((l.t[1])?l.t[1]:"1", NULL, 10));
135 }
136 else if (!g_ascii_strcasecmp(l.t[0], "enabled"))
137 {
138 pLocation->bEnabled_ = (gint)g_ascii_strtoll((l.t[1])?l.t[1]:"0", NULL, 10);
139 }
140
141 }
142 else
143 {
144 LXW_LOG(LXW_ERROR, "Weather: illegal config line: %s", l.str);
145
146 return 0;
147 }
148
149 }
150
151 }
152
153 if (pLocation->pcAlias_ && pLocation->pcWOEID_)
154 {
155 GValue locationValue = G_VALUE_INIT;
156
157 g_value_init(&locationValue, G_TYPE_POINTER);
158
159 /* location is copied by the widget */
160 g_value_set_pointer(&locationValue, pLocation);
161
162 g_object_set_property(G_OBJECT(pWidg),
163 "location",
164 &locationValue);
165 }
166
167 freeLocation(pLocation);
168
169 return 1;
170}
171
172/**
173 * Weather Plugin destructor.
174 *
175 * @param pPlugin Pointer to the PluginClass wrapper instance.
176 */
177static void
178weather_destructor(Plugin * pPlugin)
179{
180 WeatherPluginPrivate * pPriv = (WeatherPluginPrivate *) pPlugin->priv;
181
182 LXW_LOG(LXW_DEBUG, "weather_destructor(%d): %d", pPriv->iMyId_, g_iCount);
183
184 g_free(pPriv);
185
186 --g_iCount;
187
188 if (g_iCount == 0)
189 {
190 cleanupYahooUtil();
191
192 cleanupLogUtil();
193 }
194}
195
196/**
197 * Weather Plugin configuration change callback.
198 *
199 * @param pPlugin Pointer to the PluginClass wrapper instance.
200 */
201static void
202weather_configuration_changed(Plugin * pPlugin)
203{
204 LXW_LOG(LXW_DEBUG, "weather_configuration_changed()");
205
206 if (pPlugin && pPlugin->panel)
207 {
208 LXW_LOG(LXW_DEBUG,
209 " orientation: %s, width: %d, height: %d, icon size: %d\n",
210 (pPlugin->panel->orientation == ORIENT_HORIZ)?"HORIZONTAL":
211 (pPlugin->panel->orientation == ORIENT_VERT)?"VERTICAL":"NONE",
212 pPlugin->panel->width,
213 pPlugin->panel->height,
214 pPlugin->panel->icon_size);
215 }
216
217}
218
219/**
220 * Weather Plugin configuration dialog callback.
221 *
222 * @param pPlugin Pointer to the PluginClass wrapper instance.
223 * @param pParent Pointer to the GtkWindow parent.
224 */
225static void
226weather_configure(Plugin * pPlugin, GtkWindow * pParent G_GNUC_UNUSED)
227{
228 LXW_LOG(LXW_DEBUG, "weather_configure()");
229
230 WeatherPluginPrivate * pPriv = (WeatherPluginPrivate *) pPlugin->priv;
231
232 gtk_weather_run_preferences_dialog(GTK_WIDGET(pPriv->pWeather_));
233}
234
235/**
236 * Weather Plugin callback to save configuration
237 *
238 * @param pPlugin Pointer to the PluginClass wrapper instance.
239 * @param pFile Pointer to the FILE object
240 */
241static void
242weather_save_configuration(Plugin * pPlugin, FILE *pFile)
243{
244 WeatherPluginPrivate * pPriv = (WeatherPluginPrivate *) pPlugin->priv;
245
246 LXW_LOG(LXW_DEBUG, "weather_save_configuration(%d)", pPriv->iMyId_);
247
248 GValue location = G_VALUE_INIT;
249
250 g_value_init(&location, G_TYPE_POINTER);
251
252 /* pwid is the WeatherWidget */
253 g_object_get_property(G_OBJECT(pPriv->pWeather_),
254 "location",
255 &location);
256
257 LocationInfo * pLocation = g_value_get_pointer(&location);
258
259 if (pLocation)
260 {
261 lxpanel_put_str(pFile, "alias", pLocation->pcAlias_);
262 lxpanel_put_str(pFile, "city", pLocation->pcCity_);
263 lxpanel_put_str(pFile, "state", pLocation->pcState_);
264 lxpanel_put_str(pFile, "country", pLocation->pcCountry_);
265 lxpanel_put_str(pFile, "woeid", pLocation->pcWOEID_);
266 lxpanel_put_line(pFile, "units=%c", pLocation->cUnits_);
267 lxpanel_put_int(pFile, "interval", pLocation->uiInterval_);
268 lxpanel_put_bool(pFile, "enabled", pLocation->bEnabled_);
269 }
270
271}
272
273/**
274 * Definition of the weather plugin class
275 *
276 *
277 */
278PluginClass weather_plugin_class =
279 {
280 // version info
281 PLUGINCLASS_VERSIONING,
282
283 // general info
6ae6e2d6
HG
284 .type = "weather",
285 .name = N_("Weather Plugin"),
286 .version = "0.0.1",
287 .description = N_("Show weather conditions for a location."),
f0a22461
PS
288
289 // system settings
6ae6e2d6
HG
290 .one_per_system = 0,
291 .expand_available = 0,
f0a22461
PS
292
293 // API functions
6ae6e2d6
HG
294 .constructor = weather_constructor,
295 .destructor = weather_destructor,
296 .config = weather_configure,
297 .save = weather_save_configuration,
298 .panel_configuration_changed = weather_configuration_changed
f0a22461 299 };