Adding upstream version 0.8.2.
[debian/lxpanel.git] / plugins / dclock.c
CommitLineData
0688b017
AG
1/*
2 * Copyright (C) 2006-2008 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
3 * 2006 Jim Huang <jserv.tw@gmail.com>
4 * 2008 Fred Chien <fred@lxde.org>
5 * 2009 Ying-Chun Liu (PaulLiu) <grandpaul@gmail.com>
6 * 2009-2010 Marty Jack <martyj19@comcast.net>
7 * 2010 Julien Lavergne <julien.lavergne@gmail.com>
8 * 2012 Michael Rawson <michaelrawson76@gmail.com>
9 * 2012-2014 Henry Gebhardt <hsggebhardt@gmail.com>
10 * 2012 Piotr Sipika <Piotr.Sipika@gmail.com>
11 * 2014-2015 Andriy Grytsenko <andrej@rep.kiev.ua>
12 *
13 * This file is a part of LXPanel project.
6cc5e1a6
DB
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software Foundation,
27 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
6b775dbb 30#include "plugin.h"
f7ecd6ce 31#include "misc.h"
6b775dbb
AG
32
33#include <libfm/fm-gtk.h>
34
6cc5e1a6
DB
35#include <time.h>
36#include <sys/time.h>
37#include <sys/types.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <glib/gi18n.h>
6cc5e1a6 42
6cc5e1a6
DB
43#include "dbg.h"
44
45#define DEFAULT_TIP_FORMAT "%A %x"
46#define DEFAULT_CLOCK_FORMAT "%R"
47
2ba86315 48/* Private context for digital clock plugin. */
6cc5e1a6 49typedef struct {
6b775dbb
AG
50 GtkWidget * plugin; /* Back pointer to plugin */
51 LXPanel * panel;
52 config_setting_t *settings;
2ba86315
DB
53 GtkWidget * clock_label; /* Label containing clock value */
54 GtkWidget * clock_icon; /* Icon when icon_only */
55 GtkWidget * calendar_window; /* Calendar window, if it is being displayed */
56 char * clock_format; /* Format string for clock value */
57 char * tooltip_format; /* Format string for tooltip value */
58 char * action; /* Command to execute on a click */
59 gboolean bold; /* True if bold font */
60 gboolean icon_only; /* True if icon only (no clock value) */
514580cf 61 gboolean center_text;
2ba86315 62 guint timer; /* Timer for periodic update */
4652f59b
DB
63 enum {
64 AWAITING_FIRST_CHANGE, /* Experimenting to determine interval, waiting for first change */
65 AWAITING_SECOND_CHANGE, /* Experimenting to determine interval, waiting for second change */
66 ONE_SECOND_INTERVAL, /* Determined that one second interval is necessary */
67 ONE_MINUTE_INTERVAL /* Determined that one minute interval is sufficient */
6b775dbb 68 } expiration_interval;
4652f59b
DB
69 int experiment_count; /* Count of experiments that have been done to determine interval */
70 char * prev_clock_value; /* Previous value of clock */
71 char * prev_tooltip_value; /* Previous value of tooltip */
2ba86315
DB
72} DClockPlugin;
73
2ba86315 74static gboolean dclock_update_display(DClockPlugin * dc);
6b775dbb
AG
75static void dclock_destructor(gpointer user_data);
76static gboolean dclock_apply_configuration(gpointer user_data);
2ba86315
DB
77
78/* Display a window containing the standard calendar widget. */
32a67dc7 79static GtkWidget * dclock_create_calendar(DClockPlugin * dc)
6cc5e1a6 80{
2ba86315
DB
81 /* Create a new window. */
82 GtkWidget * win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
f7ecd6ce
AG
83 gint x, y;
84
6cc5e1a6
DB
85 gtk_window_set_default_size(GTK_WINDOW(win), 180, 180);
86 gtk_window_set_decorated(GTK_WINDOW(win), FALSE);
2ba86315 87 gtk_window_set_resizable(GTK_WINDOW(win), FALSE);
6cc5e1a6
DB
88 gtk_container_set_border_width(GTK_CONTAINER(win), 5);
89 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(win), TRUE);
90 gtk_window_set_skip_pager_hint(GTK_WINDOW(win), TRUE);
6b775dbb 91 gtk_window_set_type_hint(GTK_WINDOW(win), GDK_WINDOW_TYPE_HINT_UTILITY);
2ba86315
DB
92 gtk_window_stick(GTK_WINDOW(win));
93
94 /* Create a vertical box as a child of the window. */
95 GtkWidget * box = gtk_vbox_new(FALSE, 0);
7486d297 96 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(box));
6cc5e1a6 97
2ba86315
DB
98 /* Create a standard calendar widget as a child of the vertical box. */
99 GtkWidget * calendar = gtk_calendar_new();
6b775dbb 100 gtk_calendar_set_display_options(
2ba86315
DB
101 GTK_CALENDAR(calendar),
102 GTK_CALENDAR_SHOW_WEEK_NUMBERS | GTK_CALENDAR_SHOW_DAY_NAMES | GTK_CALENDAR_SHOW_HEADING);
6b775dbb 103 gtk_box_pack_start(GTK_BOX(box), calendar, TRUE, TRUE, 0);
f7ecd6ce 104 gtk_widget_show_all(box);
6cc5e1a6 105
f7ecd6ce
AG
106 /* Preset the widget position right now to not move it across the screen */
107 lxpanel_plugin_popup_set_position_helper(dc->panel, dc->plugin, win, &x, &y);
108 gtk_window_move(GTK_WINDOW(win), x, y);
32a67dc7 109
2ba86315
DB
110 /* Return the widget. */
111 return win;
6cc5e1a6
DB
112}
113
2ba86315 114/* Handler for "button-press-event" event from main widget. */
6b775dbb 115static gboolean dclock_button_press_event(GtkWidget * widget, GdkEventButton * evt, LXPanel * panel)
6cc5e1a6 116{
6b775dbb 117 DClockPlugin * dc = lxpanel_plugin_get_data(widget);
6cc5e1a6 118
2ba86315
DB
119 /* If an action is set, execute it. */
120 if (dc->action != NULL)
6b775dbb 121 fm_launch_command_simple(NULL, NULL, 0, dc->action, NULL);
2ba86315
DB
122
123 /* If no action is set, toggle the presentation of the calendar. */
124 else
125 {
126 if (dc->calendar_window == NULL)
127 {
32a67dc7 128 dc->calendar_window = dclock_create_calendar(dc);
2ba86315
DB
129 gtk_widget_show_all(dc->calendar_window);
130 }
131 else
132 {
133 gtk_widget_destroy(dc->calendar_window);
134 dc->calendar_window = NULL;
6cc5e1a6
DB
135 }
136 }
2ba86315 137 return TRUE;
6cc5e1a6
DB
138}
139
4652f59b 140/* Set the timer. */
6b775dbb 141static void dclock_timer_set(DClockPlugin * dc, struct timeval *current_time)
4652f59b
DB
142{
143 int milliseconds = 1000;
144
145 /* Get current time to millisecond resolution. */
6b775dbb 146 if (gettimeofday(current_time, NULL) >= 0)
4652f59b
DB
147 {
148 /* Compute number of milliseconds until next second boundary. */
6b775dbb 149 milliseconds = 1000 - (current_time->tv_usec / 1000);
4652f59b
DB
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 {
6b775dbb 155 time_t seconds = 60 - (current_time->tv_sec - (current_time->tv_sec / 60) * 60);
4652f59b
DB
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
2ba86315
DB
166/* Periodic timer callback.
167 * Also used during initialization and configuration change to do a redraw. */
168static gboolean dclock_update_display(DClockPlugin * dc)
6cc5e1a6 169{
2ba86315 170 /* Determine the current time. */
6b775dbb
AG
171 struct timeval now;
172 struct tm * current_time;
173
174 if (g_source_is_destroyed(g_main_current_source()))
175 return FALSE;
176
177 dclock_timer_set(dc, &now);
178 current_time = localtime(&now.tv_sec);
4652f59b
DB
179
180 /* Determine the content of the clock label and tooltip. */
181 char clock_value[64];
182 char tooltip_value[64];
183 clock_value[0] = '\0';
184 if (dc->clock_format != NULL)
185 strftime(clock_value, sizeof(clock_value), dc->clock_format, current_time);
186 tooltip_value[0] = '\0';
187 if (dc->tooltip_format != NULL)
188 strftime(tooltip_value, sizeof(tooltip_value), dc->tooltip_format, current_time);
6cc5e1a6 189
2ba86315 190 /* When we write the clock value, it causes the panel to do a full relayout.
4652f59b
DB
191 * Since this function may be called too often while the timing experiment is underway,
192 * we take the trouble to check if the string actually changed first. */
2ba86315 193 if (( ! dc->icon_only)
4652f59b 194 && ((dc->prev_clock_value == NULL) || (strcmp(dc->prev_clock_value, clock_value) != 0)))
2ba86315 195 {
2ba86315
DB
196 /* Convert "\n" escapes in the user's format string to newline characters. */
197 char * newlines_converted = NULL;
4652f59b 198 if (strstr(clock_value, "\\n") != NULL)
2ba86315 199 {
4652f59b 200 newlines_converted = g_strdup(clock_value); /* Just to get enough space for the converted result */
2ba86315
DB
201 char * p;
202 char * q;
4652f59b 203 for (p = clock_value, q = newlines_converted; *p != '\0'; p += 1)
2ba86315
DB
204 {
205 if ((p[0] == '\\') && (p[1] == 'n'))
206 {
207 *q++ = '\n';
208 p += 1;
209 }
210 else
211 *q++ = *p;
6cc5e1a6 212 }
2ba86315
DB
213 *q = '\0';
214 }
215
4652f59b 216 gchar * utf8 = g_locale_to_utf8(((newlines_converted != NULL) ? newlines_converted : clock_value), -1, NULL, NULL, NULL);
2ba86315
DB
217 if (utf8 != NULL)
218 {
6b775dbb 219 lxpanel_draw_label_text(dc->panel, dc->clock_label, utf8, dc->bold, 1, TRUE);
2ba86315
DB
220 g_free(utf8);
221 }
222 g_free(newlines_converted);
6cc5e1a6
DB
223 }
224
2ba86315 225 /* Determine the content of the tooltip. */
4652f59b 226 gchar * utf8 = g_locale_to_utf8(tooltip_value, -1, NULL, NULL, NULL);
2ba86315
DB
227 if (utf8 != NULL)
228 {
6b775dbb 229 gtk_widget_set_tooltip_text(dc->plugin, utf8);
2ba86315
DB
230 g_free(utf8);
231 }
4652f59b
DB
232
233 /* Conduct an experiment to see how often the value changes.
234 * Use this to decide whether we update the value every second or every minute.
235 * We need to account for the possibility that the experiment is being run when we cross a minute boundary. */
236 if (dc->expiration_interval < ONE_SECOND_INTERVAL)
237 {
238 if (dc->prev_clock_value == NULL)
239 {
240 /* Initiate the experiment. */
241 dc->prev_clock_value = g_strdup(clock_value);
242 dc->prev_tooltip_value = g_strdup(tooltip_value);
243 }
244 else
245 {
246 if (((dc->icon_only) || (strcmp(dc->prev_clock_value, clock_value) == 0))
247 && (strcmp(dc->prev_tooltip_value, tooltip_value) == 0))
248 {
249 dc->experiment_count += 1;
250 if (dc->experiment_count > 3)
251 {
252 /* No change within 3 seconds. Assume change no more often than once per minute. */
253 dc->expiration_interval = ONE_MINUTE_INTERVAL;
254 g_free(dc->prev_clock_value);
255 g_free(dc->prev_tooltip_value);
256 dc->prev_clock_value = NULL;
257 dc->prev_tooltip_value = NULL;
258 }
259 }
260 else if (dc->expiration_interval == AWAITING_FIRST_CHANGE)
261 {
262 /* We have a change at the beginning of the experiment, but we do not know when the next change might occur.
263 * Continue the experiment for 3 more seconds. */
264 dc->expiration_interval = AWAITING_SECOND_CHANGE;
265 dc->experiment_count = 0;
266 g_free(dc->prev_clock_value);
267 g_free(dc->prev_tooltip_value);
268 dc->prev_clock_value = g_strdup(clock_value);
269 dc->prev_tooltip_value = g_strdup(tooltip_value);
270 }
271 else
272 {
273 /* We have a second change. End the experiment. */
274 dc->expiration_interval = ((dc->experiment_count > 3) ? ONE_MINUTE_INTERVAL : ONE_SECOND_INTERVAL);
275 g_free(dc->prev_clock_value);
276 g_free(dc->prev_tooltip_value);
277 dc->prev_clock_value = NULL;
278 dc->prev_tooltip_value = NULL;
279 }
280 }
281 }
282
283 /* Reset the timer and return. */
4652f59b 284 return FALSE;
6cc5e1a6
DB
285}
286
2ba86315 287/* Plugin constructor. */
6b775dbb 288static GtkWidget *dclock_constructor(LXPanel *panel, config_setting_t *settings)
6cc5e1a6 289{
2ba86315
DB
290 /* Allocate and initialize plugin context and set into Plugin private data pointer. */
291 DClockPlugin * dc = g_new0(DClockPlugin, 1);
6b775dbb
AG
292 GtkWidget * p;
293 const char *str;
294 int tmp_int;
6cc5e1a6 295
2ba86315 296 /* Load parameters from the configuration file. */
6b775dbb
AG
297 if (config_setting_lookup_string(settings, "ClockFmt", &str))
298 dc->clock_format = g_strdup(str);
299 if (config_setting_lookup_string(settings, "TooltipFmt", &str))
300 dc->tooltip_format = g_strdup(str);
301 if (config_setting_lookup_string(settings, "Action", &str))
302 dc->action = g_strdup(str);
303 if (config_setting_lookup_int(settings, "BoldFont", &tmp_int))
304 dc->bold = tmp_int != 0;
305 if (config_setting_lookup_int(settings, "IconOnly", &tmp_int))
306 dc->icon_only = tmp_int != 0;
307 if (config_setting_lookup_int(settings, "CenterText", &tmp_int))
308 dc->center_text = tmp_int != 0;
309
310 /* Save construction pointers */
311 dc->panel = panel;
312 dc->settings = settings;
6cc5e1a6 313
2ba86315 314 /* Allocate top level widget and set into Plugin widget pointer. */
6b775dbb
AG
315 dc->plugin = p = gtk_event_box_new();
316 lxpanel_plugin_set_data(p, dc, dclock_destructor);
6cc5e1a6 317
2ba86315 318 /* Allocate a horizontal box as the child of the top level. */
514580cf 319 GtkWidget * hbox = gtk_hbox_new(TRUE, 0);
6b775dbb 320 gtk_container_add(GTK_CONTAINER(p), hbox);
2ba86315 321 gtk_widget_show(hbox);
6cc5e1a6 322
2ba86315
DB
323 /* Create a label and an image as children of the horizontal box.
324 * Only one of these is visible at a time, controlled by user preference. */
325 dc->clock_label = gtk_label_new(NULL);
326 gtk_misc_set_alignment(GTK_MISC(dc->clock_label), 0.5, 0.5);
327 gtk_misc_set_padding(GTK_MISC(dc->clock_label), 4, 0);
328 gtk_container_add(GTK_CONTAINER(hbox), dc->clock_label);
f7ecd6ce 329 dc->clock_icon = lxpanel_image_new_for_icon(panel, "clock", -1, NULL);
2ba86315 330 gtk_container_add(GTK_CONTAINER(hbox), dc->clock_icon);
6cc5e1a6 331
4652f59b
DB
332 /* Initialize the clock display. */
333 if (dc->clock_format == NULL)
aaccad27 334 dc->clock_format = g_strdup(_(DEFAULT_CLOCK_FORMAT));
4652f59b 335 if (dc->tooltip_format == NULL)
aaccad27 336 dc->tooltip_format = g_strdup(_(DEFAULT_TIP_FORMAT));
2ba86315 337 dclock_apply_configuration(p);
6cc5e1a6 338
2ba86315 339 /* Show the widget and return. */
8713e384 340 dc->timer = g_idle_add((GSourceFunc)dclock_update_display, dc);
6b775dbb 341 return p;
2ba86315 342}
6cc5e1a6 343
2ba86315 344/* Plugin destructor. */
6b775dbb 345static void dclock_destructor(gpointer user_data)
6cc5e1a6 346{
6b775dbb 347 DClockPlugin * dc = user_data;
6cc5e1a6 348
2ba86315
DB
349 /* Remove the timer. */
350 if (dc->timer != 0)
6cc5e1a6
DB
351 g_source_remove(dc->timer);
352
2ba86315
DB
353 /* Ensure that the calendar is dismissed. */
354 if (dc->calendar_window != NULL)
355 gtk_widget_destroy(dc->calendar_window);
7486d297 356
2ba86315
DB
357 /* Deallocate all memory. */
358 g_free(dc->clock_format);
359 g_free(dc->tooltip_format);
6cc5e1a6 360 g_free(dc->action);
4652f59b
DB
361 g_free(dc->prev_clock_value);
362 g_free(dc->prev_tooltip_value);
2ba86315 363 g_free(dc);
6cc5e1a6
DB
364}
365
2ba86315 366/* Callback when the configuration dialog has recorded a configuration change. */
6b775dbb 367static gboolean dclock_apply_configuration(gpointer user_data)
6cc5e1a6 368{
6b775dbb
AG
369 GtkWidget * p = user_data;
370 DClockPlugin * dc = lxpanel_plugin_get_data(p);
6b775dbb
AG
371
372 /* stop the updater now */
373 if (dc->timer)
374 g_source_remove(dc->timer);
2ba86315
DB
375
376 /* Set up the icon or the label as the displayable widget. */
377 if (dc->icon_only)
378 {
2ba86315
DB
379 gtk_widget_show(dc->clock_icon);
380 gtk_widget_hide(dc->clock_label);
381 }
382 else
383 {
384 gtk_widget_show(dc->clock_label);
385 gtk_widget_hide(dc->clock_icon);
386 }
6b775dbb 387
514580cf
DB
388 if (dc->center_text)
389 {
6b775dbb 390 gtk_label_set_justify(GTK_LABEL(dc->clock_label), GTK_JUSTIFY_CENTER);
514580cf
DB
391 }
392 else
393 {
6b775dbb 394 gtk_label_set_justify(GTK_LABEL(dc->clock_label), GTK_JUSTIFY_LEFT);
514580cf 395 }
2ba86315 396
4652f59b
DB
397 /* Rerun the experiment to determine update interval and update the display. */
398 g_free(dc->prev_clock_value);
399 g_free(dc->prev_tooltip_value);
400 dc->expiration_interval = AWAITING_FIRST_CHANGE;
401 dc->experiment_count = 0;
402 dc->prev_clock_value = NULL;
403 dc->prev_tooltip_value = NULL;
8713e384 404 dc->timer = g_idle_add((GSourceFunc)dclock_update_display, dc);
32a67dc7
DB
405
406 /* Hide the calendar. */
407 if (dc->calendar_window != NULL)
408 {
409 gtk_widget_destroy(dc->calendar_window);
410 dc->calendar_window = NULL;
411 }
6b775dbb
AG
412
413 /* Save configuration */
414 config_group_set_string(dc->settings, "ClockFmt", dc->clock_format);
415 config_group_set_string(dc->settings, "TooltipFmt", dc->tooltip_format);
416 config_group_set_string(dc->settings, "Action", dc->action);
417 config_group_set_int(dc->settings, "BoldFont", dc->bold);
418 config_group_set_int(dc->settings, "IconOnly", dc->icon_only);
419 config_group_set_int(dc->settings, "CenterText", dc->center_text);
420 return FALSE;
6cc5e1a6
DB
421}
422
2ba86315 423/* Callback when the configuration dialog is to be shown. */
6b775dbb 424static GtkWidget *dclock_configure(LXPanel *panel, GtkWidget *p)
6cc5e1a6 425{
6b775dbb
AG
426 DClockPlugin * dc = lxpanel_plugin_get_data(p);
427 return lxpanel_generic_config_dlg(_("Digital Clock"), panel,
428 dclock_apply_configuration, p,
2ba86315
DB
429 _("Clock Format"), &dc->clock_format, CONF_TYPE_STR,
430 _("Tooltip Format"), &dc->tooltip_format, CONF_TYPE_STR,
6b775dbb 431 _("Format codes: man 3 strftime; %n for line break"), NULL, CONF_TYPE_TRIM,
2ba86315
DB
432 _("Action when clicked (default: display calendar)"), &dc->action, CONF_TYPE_STR,
433 _("Bold font"), &dc->bold, CONF_TYPE_BOOL,
434 _("Tooltip only"), &dc->icon_only, CONF_TYPE_BOOL,
514580cf 435 _("Center text"), &dc->center_text, CONF_TYPE_BOOL,
2ba86315 436 NULL);
6cc5e1a6
DB
437}
438
2ba86315 439/* Plugin descriptor. */
6b775dbb
AG
440LXPanelPluginInit lxpanel_static_plugin_dclock = {
441 .name = N_("Digital Clock"),
442 .description = N_("Display digital clock and tooltip"),
443
444 .new_instance = dclock_constructor,
445 .config = dclock_configure,
6b775dbb 446 .button_press_event = dclock_button_press_event
6cc5e1a6 447};