Adding upstream version 0.5.4.1.
[debian/lxpanel.git] / src / plugins / dclock.c
CommitLineData
6cc5e1a6
DB
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>
6cc5e1a6
DB
26
27#include "panel.h"
28#include "misc.h"
29#include "plugin.h"
6cc5e1a6
DB
30
31#include "dbg.h"
32
33#define DEFAULT_TIP_FORMAT "%A %x"
34#define DEFAULT_CLOCK_FORMAT "%R"
35
2ba86315 36/* Private context for digital clock plugin. */
6cc5e1a6 37typedef struct {
2ba86315
DB
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 guint timer; /* Timer for periodic update */
48 char * prev_output; /* Previous value of clock */
49} DClockPlugin;
50
51static GtkWidget * dclock_create_calendar(void);
52static gboolean dclock_button_press_event(GtkWidget * widget, GdkEventButton * evt, Plugin * plugin);
53static gboolean dclock_update_display(DClockPlugin * dc);
54static int dclock_constructor(Plugin * p, char ** fp);
55static void dclock_destructor(Plugin * p);
56static void dclock_apply_configuration(Plugin * p);
57static void dclock_configure(Plugin * p, GtkWindow * parent);
58static void dclock_save_configuration(Plugin * p, FILE * fp);
59static void dclock_panel_configuration_changed(Plugin * p);
60
61/* Display a window containing the standard calendar widget. */
62static GtkWidget * dclock_create_calendar(void)
6cc5e1a6 63{
2ba86315
DB
64 /* Create a new window. */
65 GtkWidget * win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
6cc5e1a6
DB
66 gtk_window_set_default_size(GTK_WINDOW(win), 180, 180);
67 gtk_window_set_decorated(GTK_WINDOW(win), FALSE);
2ba86315 68 gtk_window_set_resizable(GTK_WINDOW(win), FALSE);
6cc5e1a6
DB
69 gtk_container_set_border_width(GTK_CONTAINER(win), 5);
70 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(win), TRUE);
71 gtk_window_set_skip_pager_hint(GTK_WINDOW(win), TRUE);
6cc5e1a6 72 gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_MOUSE);
2ba86315
DB
73 gtk_window_stick(GTK_WINDOW(win));
74
75 /* Create a vertical box as a child of the window. */
76 GtkWidget * box = gtk_vbox_new(FALSE, 0);
7486d297 77 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(box));
6cc5e1a6 78
2ba86315
DB
79 /* Create a standard calendar widget as a child of the vertical box. */
80 GtkWidget * calendar = gtk_calendar_new();
81 gtk_calendar_display_options(
82 GTK_CALENDAR(calendar),
83 GTK_CALENDAR_SHOW_WEEK_NUMBERS | GTK_CALENDAR_SHOW_DAY_NAMES | GTK_CALENDAR_SHOW_HEADING);
84 gtk_box_pack_start_defaults(GTK_BOX(box), calendar);
6cc5e1a6 85
2ba86315
DB
86 /* Return the widget. */
87 return win;
6cc5e1a6
DB
88}
89
2ba86315
DB
90/* Handler for "button-press-event" event from main widget. */
91static gboolean dclock_button_press_event(GtkWidget * widget, GdkEventButton * evt, Plugin * plugin)
6cc5e1a6 92{
2ba86315 93 DClockPlugin * dc = (DClockPlugin *) plugin->priv;
6cc5e1a6 94
2ba86315
DB
95 /* Standard right-click handling. */
96 if (plugin_button_press_event(widget, evt, plugin))
6cc5e1a6 97 return TRUE;
6cc5e1a6 98
2ba86315
DB
99 /* If an action is set, execute it. */
100 if (dc->action != NULL)
101 g_spawn_command_line_async(dc->action, NULL);
102
103 /* If no action is set, toggle the presentation of the calendar. */
104 else
105 {
106 if (dc->calendar_window == NULL)
107 {
108 dc->calendar_window = dclock_create_calendar();
109 gtk_widget_show_all(dc->calendar_window);
110 }
111 else
112 {
113 gtk_widget_destroy(dc->calendar_window);
114 dc->calendar_window = NULL;
6cc5e1a6
DB
115 }
116 }
2ba86315 117 return TRUE;
6cc5e1a6
DB
118}
119
2ba86315
DB
120/* Periodic timer callback.
121 * Also used during initialization and configuration change to do a redraw. */
122static gboolean dclock_update_display(DClockPlugin * dc)
6cc5e1a6 123{
2ba86315 124 /* Determine the current time. */
6cc5e1a6 125 time_t now;
6cc5e1a6 126 time(&now);
2ba86315 127 struct tm * detail = localtime(&now);
6cc5e1a6 128
2ba86315
DB
129 /* Determine the content of the clock label. */
130 char output[64];
131 strftime(output, sizeof(output),
132 ((dc->clock_format != NULL) ? dc->clock_format : DEFAULT_CLOCK_FORMAT), detail);
6cc5e1a6 133
2ba86315
DB
134 /* When we write the clock value, it causes the panel to do a full relayout.
135 * Since this function is called once per second, we take the trouble to check if the string actually changed first. */
136 if (( ! dc->icon_only)
137 && ((dc->prev_output == NULL) || (strcmp(dc->prev_output, output) != 0)))
138 {
139 g_free(dc->prev_output);
140 dc->prev_output = g_strdup(output);
141
142 /* Convert "\n" escapes in the user's format string to newline characters. */
143 char * newlines_converted = NULL;
144 if (strstr(output, "\\n") != NULL)
145 {
146 newlines_converted = g_strdup(output); /* Just to get enough space for the converted result */
147 char * p;
148 char * q;
149 for (p = output, q = newlines_converted; *p != '\0'; p += 1)
150 {
151 if ((p[0] == '\\') && (p[1] == 'n'))
152 {
153 *q++ = '\n';
154 p += 1;
155 }
156 else
157 *q++ = *p;
6cc5e1a6 158 }
2ba86315
DB
159 *q = '\0';
160 }
161
162 gchar * utf8 = g_locale_to_utf8(((newlines_converted != NULL) ? newlines_converted : output), -1, NULL, NULL, NULL);
163 if (utf8 != NULL)
164 {
165 panel_draw_label_text(dc->plugin->panel, dc->clock_label, utf8, dc->bold, TRUE);
166 g_free(utf8);
167 }
168 g_free(newlines_converted);
6cc5e1a6
DB
169 }
170
2ba86315
DB
171 /* Determine the content of the tooltip. */
172 strftime(output, sizeof(output),
173 ((dc->tooltip_format != NULL) ? dc->tooltip_format : DEFAULT_TIP_FORMAT), detail);
174 gchar * utf8 = g_locale_to_utf8(output, -1, NULL, NULL, NULL);
175 if (utf8 != NULL)
176 {
177 gtk_widget_set_tooltip_text(dc->plugin->pwid, utf8);
178 g_free(utf8);
179 }
180 return TRUE;
6cc5e1a6
DB
181}
182
2ba86315
DB
183/* Plugin constructor. */
184static int dclock_constructor(Plugin * p, char ** fp)
6cc5e1a6 185{
2ba86315
DB
186 /* Allocate and initialize plugin context and set into Plugin private data pointer. */
187 DClockPlugin * dc = g_new0(DClockPlugin, 1);
6cc5e1a6 188 p->priv = dc;
2ba86315 189 dc->plugin = p;
6cc5e1a6 190
2ba86315
DB
191 /* Load parameters from the configuration file. */
192 line s;
6cc5e1a6 193 s.len = 256;
2ba86315 194 if (fp != NULL)
6cc5e1a6 195 {
2ba86315
DB
196 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END)
197 {
198 if (s.type == LINE_NONE)
199 {
6cc5e1a6 200 ERR( "dclock: illegal token %s\n", s.str);
2ba86315 201 return 0;
6cc5e1a6 202 }
2ba86315
DB
203 if (s.type == LINE_VAR)
204 {
205 if (g_ascii_strcasecmp(s.t[0], "ClockFmt") == 0)
206 dc->clock_format = g_strdup(s.t[1]);
207 else if (g_ascii_strcasecmp(s.t[0], "TooltipFmt") == 0)
208 dc->tooltip_format = g_strdup(s.t[1]);
209 else if (g_ascii_strcasecmp(s.t[0], "Action") == 0)
6cc5e1a6 210 dc->action = g_strdup(s.t[1]);
2ba86315 211 else if (g_ascii_strcasecmp(s.t[0], "BoldFont") == 0)
6cc5e1a6 212 dc->bold = str2num(bool_pair, s.t[1], 0);
2ba86315
DB
213 else if (g_ascii_strcasecmp(s.t[0], "IconOnly") == 0)
214 dc->icon_only = str2num(bool_pair, s.t[1], 0);
215 else
6cc5e1a6 216 ERR( "dclock: unknown var %s\n", s.t[0]);
2ba86315
DB
217 }
218 else
219 {
6cc5e1a6 220 ERR( "dclock: illegal in this context %s\n", s.str);
2ba86315 221 return 0;
6cc5e1a6
DB
222 }
223 }
224 }
6cc5e1a6 225
2ba86315
DB
226 /* Allocate top level widget and set into Plugin widget pointer. */
227 p->pwid = gtk_event_box_new();
6cc5e1a6 228
2ba86315
DB
229 /* Allocate a horizontal box as the child of the top level. */
230 GtkWidget * hbox = gtk_hbox_new(FALSE, 0);
231 gtk_container_add(GTK_CONTAINER(p->pwid), hbox);
232 gtk_widget_show(hbox);
6cc5e1a6 233
2ba86315
DB
234 /* Create a label and an image as children of the horizontal box.
235 * Only one of these is visible at a time, controlled by user preference. */
236 dc->clock_label = gtk_label_new(NULL);
237 gtk_misc_set_alignment(GTK_MISC(dc->clock_label), 0.5, 0.5);
238 gtk_misc_set_padding(GTK_MISC(dc->clock_label), 4, 0);
239 gtk_container_add(GTK_CONTAINER(hbox), dc->clock_label);
240 dc->clock_icon = gtk_image_new();
241 gtk_container_add(GTK_CONTAINER(hbox), dc->clock_icon);
6cc5e1a6 242
2ba86315
DB
243 /* Connect signals. */
244 g_signal_connect(G_OBJECT (p->pwid), "button_press_event", G_CALLBACK(dclock_button_press_event), (gpointer) p);
6cc5e1a6 245
2ba86315
DB
246 /* Initialize the clock display */
247 dclock_apply_configuration(p);
6cc5e1a6 248
2ba86315
DB
249 /* Start a timer to refresh the clock display. */
250 dc->timer = g_timeout_add(1000, (GSourceFunc) dclock_update_display, (gpointer) dc);
6cc5e1a6 251
2ba86315
DB
252 /* Show the widget and return. */
253 gtk_widget_show(p->pwid);
254 return 1;
255}
6cc5e1a6 256
2ba86315
DB
257/* Plugin destructor. */
258static void dclock_destructor(Plugin * p)
6cc5e1a6 259{
2ba86315 260 DClockPlugin * dc = (DClockPlugin *) p->priv;
6cc5e1a6 261
2ba86315
DB
262 /* Remove the timer. */
263 if (dc->timer != 0)
6cc5e1a6
DB
264 g_source_remove(dc->timer);
265
2ba86315
DB
266 /* Ensure that the calendar is dismissed. */
267 if (dc->calendar_window != NULL)
268 gtk_widget_destroy(dc->calendar_window);
7486d297 269
2ba86315
DB
270 /* Deallocate all memory. */
271 g_free(dc->clock_format);
272 g_free(dc->tooltip_format);
6cc5e1a6 273 g_free(dc->action);
2ba86315
DB
274 g_free(dc->prev_output);
275 g_free(dc);
6cc5e1a6
DB
276}
277
2ba86315
DB
278/* Callback when the configuration dialog has recorded a configuration change. */
279static void dclock_apply_configuration(Plugin * p)
6cc5e1a6 280{
2ba86315
DB
281 DClockPlugin * dc = (DClockPlugin *) p->priv;
282
283 /* Set up the icon or the label as the displayable widget. */
284 if (dc->icon_only)
285 {
286 panel_image_set_from_file(p->panel, dc->clock_icon, PACKAGE_DATA_DIR "/lxpanel/images/clock.png");
287 gtk_widget_show(dc->clock_icon);
288 gtk_widget_hide(dc->clock_label);
289 }
290 else
291 {
292 gtk_widget_show(dc->clock_label);
293 gtk_widget_hide(dc->clock_icon);
294 }
295
296 /* Update the display. */
297 g_free(dc->prev_output); /* Force the update of the clock display */
298 dc->prev_output = NULL;
299 dclock_update_display(dc);
6cc5e1a6
DB
300}
301
2ba86315
DB
302/* Callback when the configuration dialog is to be shown. */
303static void dclock_configure(Plugin * p, GtkWindow * parent)
6cc5e1a6 304{
2ba86315
DB
305 DClockPlugin * dc = (DClockPlugin *) p->priv;
306 GtkWidget * dlg = create_generic_config_dlg(
307 _(p->class->name),
308 GTK_WIDGET(parent),
309 (GSourceFunc) dclock_apply_configuration, (gpointer) p,
310 _("Clock Format"), &dc->clock_format, CONF_TYPE_STR,
311 _("Tooltip Format"), &dc->tooltip_format, CONF_TYPE_STR,
312 _("Format codes: man 3 strftime; \\n for line break"), NULL, CONF_TYPE_TRIM,
313 _("Action when clicked (default: display calendar)"), &dc->action, CONF_TYPE_STR,
314 _("Bold font"), &dc->bold, CONF_TYPE_BOOL,
315 _("Tooltip only"), &dc->icon_only, CONF_TYPE_BOOL,
316 NULL);
317 gtk_window_present(GTK_WINDOW(dlg));
6cc5e1a6
DB
318}
319
2ba86315
DB
320/* Callback when the configuration is to be saved. */
321static void dclock_save_configuration(Plugin * p, FILE * fp)
6cc5e1a6 322{
2ba86315
DB
323 DClockPlugin * dc = (DClockPlugin *) p->priv;
324 lxpanel_put_str(fp, "ClockFmt", dc->clock_format);
325 lxpanel_put_str(fp, "TooltipFmt", dc->tooltip_format);
326 lxpanel_put_str(fp, "Action", dc->action);
327 lxpanel_put_int(fp, "BoldFont", dc->bold);
328 lxpanel_put_int(fp, "IconOnly", dc->icon_only);
6cc5e1a6
DB
329}
330
2ba86315
DB
331/* Callback when panel configuration changes. */
332static void dclock_panel_configuration_changed(Plugin * p)
6cc5e1a6 333{
2ba86315 334 dclock_apply_configuration(p);
6cc5e1a6
DB
335}
336
2ba86315 337/* Plugin descriptor. */
6cc5e1a6 338PluginClass dclock_plugin_class = {
2ba86315
DB
339
340 PLUGINCLASS_VERSIONING,
6cc5e1a6
DB
341
342 type : "dclock",
343 name : N_("Digital Clock"),
344 version: "1.0",
2ba86315 345 description : N_("Display digital clock and tooltip"),
6cc5e1a6
DB
346
347 constructor : dclock_constructor,
348 destructor : dclock_destructor,
2ba86315
DB
349 config : dclock_configure,
350 save : dclock_save_configuration,
351 panel_configuration_changed : dclock_panel_configuration_changed
6cc5e1a6 352};