Update translation.
[lxde/lxpanel.git] / src / plugins / dclock.c
1
2 #include <time.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <glib/gi18n.h>
9
10 #include "panel.h"
11 #include "misc.h"
12 #include "plugin.h"
13
14 //#define DEBUG
15 #include "dbg.h"
16
17
18 #define DEFAULT_TIP_FORMAT "%A %x"
19 #define DEFAULT_CLOCK_FORMAT "%R"
20
21 typedef struct {
22 GtkWidget *eb;
23 GtkWidget *main;
24 GtkWidget *clockw;
25 GtkTooltips *tip;
26 char *tfmt;
27 char *cfmt;
28 char *action;
29 short lastDay;
30 int timer;
31 } dclock;
32
33 //static dclock me;
34
35
36
37 static gboolean
38 clicked( GtkWidget *widget, gpointer dummy, dclock *dc)
39 {
40 ENTER2;
41 DBG2("%s\n", dc->action);
42 system (dc->action);
43 RET2(TRUE);
44 }
45
46
47
48 static gint
49 clock_update(gpointer data )
50 {
51 char output[64], str[64];
52 time_t now;
53 struct tm * detail;
54 dclock *dc;
55 gchar *utf8;
56
57 ENTER;
58 g_assert(data != NULL);
59 dc = (dclock *)data;
60
61 time(&now);
62 detail = localtime(&now);
63 strftime(output, sizeof(output), dc->cfmt, detail) ;
64 g_snprintf(str, 64, "<b>%s</b>", output);
65 gtk_label_set_markup (GTK_LABEL(dc->clockw), str) ;
66
67 if (detail->tm_mday != dc->lastDay) {
68 dc->lastDay = detail->tm_mday ;
69
70 strftime (output, sizeof(output), dc->tfmt, detail) ;
71 if ((utf8 = g_locale_to_utf8(output, -1, NULL, NULL, NULL))) {
72 gtk_tooltips_set_tip(dc->tip, dc->main, utf8, NULL) ;
73 g_free(utf8);
74 }
75 }
76 RET(TRUE);
77 }
78
79
80 static int
81 dclock_constructor(plugin *p)
82 {
83 line s;
84 dclock *dc;
85 char output [40] ;
86 time_t now ;
87 struct tm * detail ;
88
89 ENTER;
90 dc = g_new0(dclock, 1);
91 g_return_val_if_fail(dc != NULL, 0);
92 p->priv = dc;
93
94 s.len = 256;
95 dc->cfmt = dc->tfmt = dc->action = 0;
96 while (get_line(p->fp, &s) != LINE_BLOCK_END) {
97 if (s.type == LINE_NONE) {
98 ERR( "dclock: illegal token %s\n", s.str);
99 goto error;
100 }
101 if (s.type == LINE_VAR) {
102 if (!g_ascii_strcasecmp(s.t[0], "ClockFmt"))
103 dc->cfmt = g_strdup(s.t[1]);
104 else if (!g_ascii_strcasecmp(s.t[0], "TooltipFmt"))
105 dc->tfmt = g_strdup(s.t[1]);
106 else if (!g_ascii_strcasecmp(s.t[0], "Action"))
107 dc->action = g_strdup(s.t[1]);
108 else {
109 ERR( "dclock: unknown var %s\n", s.t[0]);
110 goto error;
111 }
112 } else {
113 ERR( "dclock: illegal in this context %s\n", s.str);
114 goto error;
115 }
116 }
117 if (!dc->cfmt)
118 dc->cfmt = g_strdup(DEFAULT_CLOCK_FORMAT);
119 if (!dc->tfmt)
120 dc->tfmt = g_strdup(DEFAULT_TIP_FORMAT);
121 dc->main = gtk_event_box_new();
122 //gtk_widget_add_events (dc->main, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
123 //button = gtk_button_new();
124 //gtk_container_add(GTK_CONTAINER(dc->main), button);
125 if (dc->action)
126 g_signal_connect (G_OBJECT (dc->main), "button_press_event",
127 G_CALLBACK (clicked), (gpointer) dc);
128 time(&now);
129 detail = localtime(&now);
130 strftime(output, sizeof(output), dc->cfmt, detail) ;
131 dc->clockw = gtk_label_new(output);
132 gtk_misc_set_alignment(GTK_MISC(dc->clockw), 0.5, 0.5);
133 gtk_misc_set_padding(GTK_MISC(dc->clockw), 4, 0);
134 //gtk_widget_show(dc->clockw);
135 gtk_container_add(GTK_CONTAINER(dc->main), dc->clockw);
136 gtk_widget_show_all(dc->main);
137 dc->tip = gtk_tooltips_new();
138 dc->timer = g_timeout_add(1000, (GSourceFunc) clock_update, (gpointer)dc);
139 gtk_container_add(GTK_CONTAINER(p->pwid), dc->main);
140 RET(1);
141
142 error:
143 g_free(dc->cfmt);
144 g_free(dc->tfmt);
145 g_free(dc->action);
146 g_free(dc);
147 RET(0);
148 }
149
150
151 static void
152 dclock_destructor(plugin *p)
153 {
154 dclock *dc = (dclock *)p->priv;
155
156 ENTER;
157 dc = (dclock *) p->priv;
158 if (dc->timer)
159 g_source_remove(dc->timer);
160 gtk_widget_destroy(dc->main);
161 g_free(dc->cfmt);
162 g_free(dc->tfmt);
163 g_free(dc->action);
164 g_free(dc);
165 RET();
166 }
167
168 plugin_class dclock_plugin_class = {
169 fname: NULL,
170 count: 0,
171
172 type : "dclock",
173 name : "Digital Clock",
174 version: "1.0",
175 description : N_("Display Digital clock and Tooltip"),
176
177 constructor : dclock_constructor,
178 destructor : dclock_destructor,
179 };