Merging upstream version 0.5.9.
[debian/lxpanel.git] / src / plugins / dclock.c
1 /**
2 * Copyright (c) 2006 LxDE Developers, see the file AUTHORS for details.
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
19 #include <time.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib/gi18n.h>
26
27 #include "panel.h"
28 #include "misc.h"
29 #include "plugin.h"
30
31 #include "dbg.h"
32
33 #define DEFAULT_TIP_FORMAT "%A %x"
34 #define DEFAULT_CLOCK_FORMAT "%R"
35
36 /* Private context for digital clock plugin. */
37 typedef struct {
38 Plugin * plugin; /* Back pointer to Plugin */
39 GtkWidget * clock_label; /* Label containing clock value */
40 GtkWidget * clock_icon; /* Icon when icon_only */
41 GtkWidget * calendar_window; /* Calendar window, if it is being displayed */
42 char * clock_format; /* Format string for clock value */
43 char * tooltip_format; /* Format string for tooltip value */
44 char * action; /* Command to execute on a click */
45 gboolean bold; /* True if bold font */
46 gboolean icon_only; /* True if icon only (no clock value) */
47 gboolean center_text;
48 guint timer; /* Timer for periodic update */
49 enum {
50 AWAITING_FIRST_CHANGE, /* Experimenting to determine interval, waiting for first change */
51 AWAITING_SECOND_CHANGE, /* Experimenting to determine interval, waiting for second change */
52 ONE_SECOND_INTERVAL, /* Determined that one second interval is necessary */
53 ONE_MINUTE_INTERVAL /* Determined that one minute interval is sufficient */
54 } expiration_interval;
55 int experiment_count; /* Count of experiments that have been done to determine interval */
56 char * prev_clock_value; /* Previous value of clock */
57 char * prev_tooltip_value; /* Previous value of tooltip */
58 } DClockPlugin;
59
60 static void dclock_popup_map(GtkWidget * widget, DClockPlugin * dc);
61 static GtkWidget * dclock_create_calendar(DClockPlugin * dc);
62 static gboolean dclock_button_press_event(GtkWidget * widget, GdkEventButton * evt, Plugin * plugin);
63 static void dclock_timer_set(DClockPlugin * dc);
64 static gboolean dclock_update_display(DClockPlugin * dc);
65 static int dclock_constructor(Plugin * p, char ** fp);
66 static void dclock_destructor(Plugin * p);
67 static void dclock_apply_configuration(Plugin * p);
68 static void dclock_configure(Plugin * p, GtkWindow * parent);
69 static void dclock_save_configuration(Plugin * p, FILE * fp);
70 static void dclock_panel_configuration_changed(Plugin * p);
71
72 /* Handler for "map" signal on popup window. */
73 static void dclock_popup_map(GtkWidget * widget, DClockPlugin * dc)
74 {
75 plugin_adjust_popup_position(widget, dc->plugin);
76 }
77
78 /* Display a window containing the standard calendar widget. */
79 static GtkWidget * dclock_create_calendar(DClockPlugin * dc)
80 {
81 /* Create a new window. */
82 GtkWidget * win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
83 gtk_window_set_default_size(GTK_WINDOW(win), 180, 180);
84 gtk_window_set_decorated(GTK_WINDOW(win), FALSE);
85 gtk_window_set_resizable(GTK_WINDOW(win), FALSE);
86 gtk_container_set_border_width(GTK_CONTAINER(win), 5);
87 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(win), TRUE);
88 gtk_window_set_skip_pager_hint(GTK_WINDOW(win), TRUE);
89 gtk_window_stick(GTK_WINDOW(win));
90
91 /* Create a vertical box as a child of the window. */
92 GtkWidget * box = gtk_vbox_new(FALSE, 0);
93 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(box));
94
95 /* Create a standard calendar widget as a child of the vertical box. */
96 GtkWidget * calendar = gtk_calendar_new();
97 gtk_calendar_display_options(
98 GTK_CALENDAR(calendar),
99 GTK_CALENDAR_SHOW_WEEK_NUMBERS | GTK_CALENDAR_SHOW_DAY_NAMES | GTK_CALENDAR_SHOW_HEADING);
100 gtk_box_pack_start_defaults(GTK_BOX(box), calendar);
101
102 /* Connect signals. */
103 g_signal_connect(G_OBJECT(win), "map", G_CALLBACK(dclock_popup_map), dc);
104
105 /* Return the widget. */
106 return win;
107 }
108
109 /* Handler for "button-press-event" event from main widget. */
110 static gboolean dclock_button_press_event(GtkWidget * widget, GdkEventButton * evt, Plugin * plugin)
111 {
112 DClockPlugin * dc = (DClockPlugin *) plugin->priv;
113
114 /* Standard right-click handling. */
115 if (plugin_button_press_event(widget, evt, plugin))
116 return TRUE;
117
118 /* If an action is set, execute it. */
119 if (dc->action != NULL)
120 g_spawn_command_line_async(dc->action, NULL);
121
122 /* If no action is set, toggle the presentation of the calendar. */
123 else
124 {
125 if (dc->calendar_window == NULL)
126 {
127 dc->calendar_window = dclock_create_calendar(dc);
128 gtk_widget_show_all(dc->calendar_window);
129 }
130 else
131 {
132 gtk_widget_destroy(dc->calendar_window);
133 dc->calendar_window = NULL;
134 }
135 }
136 return TRUE;
137 }
138
139 /* Set the timer. */
140 static void dclock_timer_set(DClockPlugin * dc)
141 {
142 int milliseconds = 1000;
143
144 /* Get current time to millisecond resolution. */
145 struct timeval current_time;
146 if (gettimeofday(&current_time, NULL) >= 0)
147 {
148 /* Compute number of milliseconds until next second boundary. */
149 milliseconds = 1000 - (current_time.tv_usec / 1000);
150
151 /* If the expiration interval is the minute boundary,
152 * add number of milliseconds after that until next minute boundary. */
153 if (dc->expiration_interval == ONE_MINUTE_INTERVAL)
154 {
155 time_t seconds = 60 - (current_time.tv_sec - (current_time.tv_sec / 60) * 60);
156 milliseconds += seconds * 1000;
157 }
158 }
159
160 /* Be defensive, and set the timer. */
161 if (milliseconds <= 0)
162 milliseconds = 1000;
163 dc->timer = g_timeout_add(milliseconds, (GSourceFunc) dclock_update_display, (gpointer) dc);
164 }
165
166 /* Periodic timer callback.
167 * Also used during initialization and configuration change to do a redraw. */
168 static gboolean dclock_update_display(DClockPlugin * dc)
169 {
170 /* Determine the current time. */
171 time_t now;
172 time(&now);
173 struct tm * current_time = localtime(&now);
174
175 /* Determine the content of the clock label and tooltip. */
176 char clock_value[64];
177 char tooltip_value[64];
178 clock_value[0] = '\0';
179 if (dc->clock_format != NULL)
180 strftime(clock_value, sizeof(clock_value), dc->clock_format, current_time);
181 tooltip_value[0] = '\0';
182 if (dc->tooltip_format != NULL)
183 strftime(tooltip_value, sizeof(tooltip_value), dc->tooltip_format, current_time);
184
185 /* When we write the clock value, it causes the panel to do a full relayout.
186 * Since this function may be called too often while the timing experiment is underway,
187 * we take the trouble to check if the string actually changed first. */
188 if (( ! dc->icon_only)
189 && ((dc->prev_clock_value == NULL) || (strcmp(dc->prev_clock_value, clock_value) != 0)))
190 {
191 /* Convert "\n" escapes in the user's format string to newline characters. */
192 char * newlines_converted = NULL;
193 if (strstr(clock_value, "\\n") != NULL)
194 {
195 newlines_converted = g_strdup(clock_value); /* Just to get enough space for the converted result */
196 char * p;
197 char * q;
198 for (p = clock_value, q = newlines_converted; *p != '\0'; p += 1)
199 {
200 if ((p[0] == '\\') && (p[1] == 'n'))
201 {
202 *q++ = '\n';
203 p += 1;
204 }
205 else
206 *q++ = *p;
207 }
208 *q = '\0';
209 }
210
211 gchar * utf8 = g_locale_to_utf8(((newlines_converted != NULL) ? newlines_converted : clock_value), -1, NULL, NULL, NULL);
212 if (utf8 != NULL)
213 {
214 panel_draw_label_text(dc->plugin->panel, dc->clock_label, utf8, dc->bold, TRUE);
215 g_free(utf8);
216 }
217 g_free(newlines_converted);
218 }
219
220 /* Determine the content of the tooltip. */
221 gchar * utf8 = g_locale_to_utf8(tooltip_value, -1, NULL, NULL, NULL);
222 if (utf8 != NULL)
223 {
224 gtk_widget_set_tooltip_text(dc->plugin->pwid, utf8);
225 g_free(utf8);
226 }
227
228 /* Conduct an experiment to see how often the value changes.
229 * Use this to decide whether we update the value every second or every minute.
230 * We need to account for the possibility that the experiment is being run when we cross a minute boundary. */
231 if (dc->expiration_interval < ONE_SECOND_INTERVAL)
232 {
233 if (dc->prev_clock_value == NULL)
234 {
235 /* Initiate the experiment. */
236 dc->prev_clock_value = g_strdup(clock_value);
237 dc->prev_tooltip_value = g_strdup(tooltip_value);
238 }
239 else
240 {
241 if (((dc->icon_only) || (strcmp(dc->prev_clock_value, clock_value) == 0))
242 && (strcmp(dc->prev_tooltip_value, tooltip_value) == 0))
243 {
244 dc->experiment_count += 1;
245 if (dc->experiment_count > 3)
246 {
247 /* No change within 3 seconds. Assume change no more often than once per minute. */
248 dc->expiration_interval = ONE_MINUTE_INTERVAL;
249 g_free(dc->prev_clock_value);
250 g_free(dc->prev_tooltip_value);
251 dc->prev_clock_value = NULL;
252 dc->prev_tooltip_value = NULL;
253 }
254 }
255 else if (dc->expiration_interval == AWAITING_FIRST_CHANGE)
256 {
257 /* We have a change at the beginning of the experiment, but we do not know when the next change might occur.
258 * Continue the experiment for 3 more seconds. */
259 dc->expiration_interval = AWAITING_SECOND_CHANGE;
260 dc->experiment_count = 0;
261 g_free(dc->prev_clock_value);
262 g_free(dc->prev_tooltip_value);
263 dc->prev_clock_value = g_strdup(clock_value);
264 dc->prev_tooltip_value = g_strdup(tooltip_value);
265 }
266 else
267 {
268 /* We have a second change. End the experiment. */
269 dc->expiration_interval = ((dc->experiment_count > 3) ? ONE_MINUTE_INTERVAL : ONE_SECOND_INTERVAL);
270 g_free(dc->prev_clock_value);
271 g_free(dc->prev_tooltip_value);
272 dc->prev_clock_value = NULL;
273 dc->prev_tooltip_value = NULL;
274 }
275 }
276 }
277
278 /* Reset the timer and return. */
279 dclock_timer_set(dc);
280 return FALSE;
281 }
282
283 /* Plugin constructor. */
284 static int dclock_constructor(Plugin * p, char ** fp)
285 {
286 /* Allocate and initialize plugin context and set into Plugin private data pointer. */
287 DClockPlugin * dc = g_new0(DClockPlugin, 1);
288 p->priv = dc;
289 dc->plugin = p;
290
291 /* Load parameters from the configuration file. */
292 line s;
293 s.len = 256;
294 if (fp != NULL)
295 {
296 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END)
297 {
298 if (s.type == LINE_NONE)
299 {
300 ERR( "dclock: illegal token %s\n", s.str);
301 return 0;
302 }
303 if (s.type == LINE_VAR)
304 {
305 if (g_ascii_strcasecmp(s.t[0], "ClockFmt") == 0)
306 dc->clock_format = g_strdup(s.t[1]);
307 else if (g_ascii_strcasecmp(s.t[0], "TooltipFmt") == 0)
308 dc->tooltip_format = g_strdup(s.t[1]);
309 else if (g_ascii_strcasecmp(s.t[0], "Action") == 0)
310 dc->action = g_strdup(s.t[1]);
311 else if (g_ascii_strcasecmp(s.t[0], "BoldFont") == 0)
312 dc->bold = str2num(bool_pair, s.t[1], 0);
313 else if (g_ascii_strcasecmp(s.t[0], "IconOnly") == 0)
314 dc->icon_only = str2num(bool_pair, s.t[1], 0);
315 else if (g_ascii_strcasecmp(s.t[0], "CenterText") == 0)
316 dc->center_text = str2num(bool_pair, s.t[1], 0);
317 else
318 ERR( "dclock: unknown var %s\n", s.t[0]);
319 }
320 else
321 {
322 ERR( "dclock: illegal in this context %s\n", s.str);
323 return 0;
324 }
325 }
326 }
327
328 /* Allocate top level widget and set into Plugin widget pointer. */
329 p->pwid = gtk_event_box_new();
330
331 /* Allocate a horizontal box as the child of the top level. */
332 GtkWidget * hbox = gtk_hbox_new(TRUE, 0);
333 gtk_container_add(GTK_CONTAINER(p->pwid), hbox);
334 gtk_widget_show(hbox);
335
336 /* Create a label and an image as children of the horizontal box.
337 * Only one of these is visible at a time, controlled by user preference. */
338 dc->clock_label = gtk_label_new(NULL);
339 gtk_misc_set_alignment(GTK_MISC(dc->clock_label), 0.5, 0.5);
340 gtk_misc_set_padding(GTK_MISC(dc->clock_label), 4, 0);
341 gtk_container_add(GTK_CONTAINER(hbox), dc->clock_label);
342 dc->clock_icon = gtk_image_new();
343 gtk_container_add(GTK_CONTAINER(hbox), dc->clock_icon);
344
345 /* Connect signals. */
346 g_signal_connect(G_OBJECT (p->pwid), "button_press_event", G_CALLBACK(dclock_button_press_event), (gpointer) p);
347
348 /* Initialize the clock display. */
349 if (dc->clock_format == NULL)
350 dc->clock_format = g_strdup(DEFAULT_CLOCK_FORMAT);
351 if (dc->tooltip_format == NULL)
352 dc->tooltip_format = g_strdup(DEFAULT_TIP_FORMAT);
353 dclock_apply_configuration(p);
354
355 /* Show the widget and return. */
356 gtk_widget_show(p->pwid);
357 return 1;
358 }
359
360 /* Plugin destructor. */
361 static void dclock_destructor(Plugin * p)
362 {
363 DClockPlugin * dc = (DClockPlugin *) p->priv;
364
365 /* Remove the timer. */
366 if (dc->timer != 0)
367 g_source_remove(dc->timer);
368
369 /* Ensure that the calendar is dismissed. */
370 if (dc->calendar_window != NULL)
371 gtk_widget_destroy(dc->calendar_window);
372
373 /* Deallocate all memory. */
374 g_free(dc->clock_format);
375 g_free(dc->tooltip_format);
376 g_free(dc->action);
377 g_free(dc->prev_clock_value);
378 g_free(dc->prev_tooltip_value);
379 g_free(dc);
380 }
381
382 /* Callback when the configuration dialog has recorded a configuration change. */
383 static void dclock_apply_configuration(Plugin * p)
384 {
385 DClockPlugin * dc = (DClockPlugin *) p->priv;
386
387 /* Set up the icon or the label as the displayable widget. */
388 if (dc->icon_only)
389 {
390 panel_image_set_from_file(p->panel, dc->clock_icon, PACKAGE_DATA_DIR "/lxpanel/images/clock.png");
391 gtk_widget_show(dc->clock_icon);
392 gtk_widget_hide(dc->clock_label);
393 }
394 else
395 {
396 gtk_widget_show(dc->clock_label);
397 gtk_widget_hide(dc->clock_icon);
398 }
399
400 if (dc->center_text)
401 {
402 gtk_label_set_justify(GTK_LABEL(dc->clock_label), GTK_JUSTIFY_CENTER);
403 }
404 else
405 {
406 gtk_label_set_justify(GTK_LABEL(dc->clock_label), GTK_JUSTIFY_LEFT);
407 }
408
409 /* Rerun the experiment to determine update interval and update the display. */
410 g_free(dc->prev_clock_value);
411 g_free(dc->prev_tooltip_value);
412 dc->expiration_interval = AWAITING_FIRST_CHANGE;
413 dc->experiment_count = 0;
414 dc->prev_clock_value = NULL;
415 dc->prev_tooltip_value = NULL;
416 dclock_update_display(dc);
417
418 /* Hide the calendar. */
419 if (dc->calendar_window != NULL)
420 {
421 gtk_widget_destroy(dc->calendar_window);
422 dc->calendar_window = NULL;
423 }
424 }
425
426 /* Callback when the configuration dialog is to be shown. */
427 static void dclock_configure(Plugin * p, GtkWindow * parent)
428 {
429 DClockPlugin * dc = (DClockPlugin *) p->priv;
430 GtkWidget * dlg = create_generic_config_dlg(
431 _(p->class->name),
432 GTK_WIDGET(parent),
433 (GSourceFunc) dclock_apply_configuration, (gpointer) p,
434 _("Clock Format"), &dc->clock_format, CONF_TYPE_STR,
435 _("Tooltip Format"), &dc->tooltip_format, CONF_TYPE_STR,
436 _("Format codes: man 3 strftime; \\n for line break"), NULL, CONF_TYPE_TRIM,
437 _("Action when clicked (default: display calendar)"), &dc->action, CONF_TYPE_STR,
438 _("Bold font"), &dc->bold, CONF_TYPE_BOOL,
439 _("Tooltip only"), &dc->icon_only, CONF_TYPE_BOOL,
440 _("Center text"), &dc->center_text, CONF_TYPE_BOOL,
441 NULL);
442 gtk_window_present(GTK_WINDOW(dlg));
443 }
444
445 /* Callback when the configuration is to be saved. */
446 static void dclock_save_configuration(Plugin * p, FILE * fp)
447 {
448 DClockPlugin * dc = (DClockPlugin *) p->priv;
449 lxpanel_put_str(fp, "ClockFmt", dc->clock_format);
450 lxpanel_put_str(fp, "TooltipFmt", dc->tooltip_format);
451 lxpanel_put_str(fp, "Action", dc->action);
452 lxpanel_put_int(fp, "BoldFont", dc->bold);
453 lxpanel_put_int(fp, "IconOnly", dc->icon_only);
454 lxpanel_put_int(fp, "CenterText", dc->center_text);
455 }
456
457 /* Callback when panel configuration changes. */
458 static void dclock_panel_configuration_changed(Plugin * p)
459 {
460 dclock_apply_configuration(p);
461 }
462
463 /* Plugin descriptor. */
464 PluginClass dclock_plugin_class = {
465
466 PLUGINCLASS_VERSIONING,
467
468 type : "dclock",
469 name : N_("Digital Clock"),
470 version: "1.0",
471 description : N_("Display digital clock and tooltip"),
472
473 constructor : dclock_constructor,
474 destructor : dclock_destructor,
475 config : dclock_configure,
476 save : dclock_save_configuration,
477 panel_configuration_changed : dclock_panel_configuration_changed
478 };