1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/* timerapplet.c:
*
* Copyright (C) 2014 Stefano Karapetsas
*
* This file is part of MATE Applets.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Stefano Karapetsas <stefano@karapetsas.com>
*/
#include <config.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gtk/gtk.h>
#include <libnotify/notify.h>
#include <mate-panel-applet.h>
#include <mate-panel-applet-gsettings.h>
/* Applet constants */
#define APPLET_ICON "mate-panel-clock"
#define STEP 100
/* GSettings constants */
#define TIMER_SCHEMA "org.mate.panel.applet.timer"
#define NAME_KEY "name"
#define DURATION_KEY "duration"
#define SHOW_NOTIFICATION_KEY "show-notification"
#define SHOW_DIALOG_KEY "show-dialog"
#define GET_WIDGET(x) (GTK_WIDGET (gtk_builder_get_object (builder, (x))))
#define GET_DIALOG(x) (GTK_DIALOG (gtk_builder_get_object (builder, (x))))
#define GET_SPIN_BUTTON(x) (GTK_SPIN_BUTTON (gtk_builder_get_object (builder, (x))))
typedef struct
{
MatePanelApplet *applet;
GSettings *settings;
GtkActionGroup *action_group;
GtkLabel *label;
GtkImage *image;
GtkImage *pause_image;
GtkBox *box;
GtkSpinButton *hours;
GtkSpinButton *minutes;
GtkSpinButton *seconds;
gboolean active;
gboolean pause;
gint elapsed;
guint timeout_id;
} TimerApplet;
static void timer_start_callback (GtkAction *action, TimerApplet *applet);
static void timer_pause_callback (GtkAction *action, TimerApplet *applet);
static void timer_stop_callback (GtkAction *action, TimerApplet *applet);
static void timer_about_callback (GtkAction *action, TimerApplet *applet);
static void timer_reset_callback (GtkAction *action, TimerApplet *applet);
static void timer_preferences_callback (GtkAction *action, TimerApplet *applet);
static const GtkActionEntry applet_menu_actions [] = {
{ "Start", "media-playback-start", N_("_Start timer"), NULL, NULL, G_CALLBACK (timer_start_callback) },
{ "Pause", "media-playback-pause", N_("P_ause timer"), NULL, NULL, G_CALLBACK (timer_pause_callback) },
{ "Stop", "media-playback-stop", N_("S_top timer"), NULL, NULL, G_CALLBACK (timer_stop_callback) },
{ "Reset", "edit-undo", N_("R_eset"), NULL, NULL, G_CALLBACK (timer_reset_callback) },
{ "Preferences", "document-properties", N_("_Preferences"), NULL, NULL, G_CALLBACK (timer_preferences_callback) },
{ "About", "help-about", N_("_About"), NULL, NULL, G_CALLBACK (timer_about_callback) }
};
static char *ui = "<menuitem name='Item 1' action='Start' />"
"<menuitem name='Item 2' action='Pause' />"
"<menuitem name='Item 3' action='Stop' />"
"<menuitem name='Item 4' action='Reset' />"
"<menuitem name='Item 5' action='Preferences' />"
"<menuitem name='Item 6' action='About' />";
static void
timer_applet_destroy (MatePanelApplet *applet_widget, TimerApplet *applet)
{
g_assert (applet);
if (applet->timeout_id != 0)
{
g_source_remove(applet->timeout_id);
applet->timeout_id = 0;
}
g_object_unref (applet->settings);
notify_uninit ();
}
/* timer management */
static gboolean
timer_callback (TimerApplet *applet)
{
gboolean retval = TRUE;
gchar *label;
gchar *name;
gchar *tooltip;
gint hours, minutes, seconds, duration, remaining;
label = NULL;
tooltip = NULL;
name = g_settings_get_string (applet->settings, NAME_KEY);
if (!applet->active)
{
applet->pause = FALSE;
applet->elapsed = 0;
gtk_label_set_text (applet->label, name);
gtk_widget_set_tooltip_text (GTK_WIDGET (applet->label), "");
gtk_widget_hide (GTK_WIDGET (applet->pause_image));
}
else
{
if (!applet->pause)
applet->elapsed += STEP;
duration = g_settings_get_int (applet->settings, DURATION_KEY);
remaining = duration - (applet->elapsed / 1000);
if (remaining <= 0)
{
applet->active = FALSE;
applet->timeout_id = 0;
/* Translators: %s is a placeholder for the timer name, 'Timer' by default */
label = g_strdup_printf (_("Finished %s"), name);
gtk_label_set_text (applet->label, label);
gtk_widget_set_tooltip_text (GTK_WIDGET (applet->label), name);
gtk_widget_hide (GTK_WIDGET (applet->pause_image));
if (g_settings_get_boolean (applet->settings, SHOW_NOTIFICATION_KEY))
{
NotifyNotification *n;
n = notify_notification_new (name, _("Timer finished!"), APPLET_ICON);
notify_notification_set_timeout (n, 30000);
notify_notification_show (n, NULL);
g_object_unref (G_OBJECT (n));
}
if (g_settings_get_boolean (applet->settings, SHOW_DIALOG_KEY))
{
GtkWidget *dialog = gtk_message_dialog_new_with_markup (NULL,
GTK_DIALOG_MODAL,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"<b>%s</b>\n\n%s", name, _("Timer finished!"));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
/* stop further calls */
retval = FALSE;
}
else
{
hours = remaining / 60 / 60;
minutes = remaining / 60 % 60;
seconds = remaining % 60;
if (hours > 0)
label = g_strdup_printf ("%02d:%02d:%02d", hours, minutes, seconds);
else
label = g_strdup_printf ("%02d:%02d", minutes, seconds);
hours = duration / 60 / 60;
minutes = duration / 60 % 60;
seconds = duration % 60;
if (hours > 0)
tooltip = g_strdup_printf ("%s (%02d:%02d:%02d)", name, hours, minutes, seconds);
else
tooltip = g_strdup_printf ("%s (%02d:%02d)", name, minutes, seconds);
gtk_label_set_text (applet->label, label);
gtk_widget_set_tooltip_text (GTK_WIDGET (applet->label), tooltip);
gtk_widget_set_visible (GTK_WIDGET (applet->pause_image), applet->pause);
}
g_free (label);
g_free (tooltip);
}
/* update actions sensitiveness */
gtk_action_set_sensitive (gtk_action_group_get_action (applet->action_group, "Start"), !applet->active || applet->pause);
gtk_action_set_sensitive (gtk_action_group_get_action (applet->action_group, "Pause"), applet->active && !applet->pause);
gtk_action_set_sensitive (gtk_action_group_get_action (applet->action_group, "Stop"), applet->active);
gtk_action_set_sensitive (gtk_action_group_get_action (applet->action_group, "Reset"), !applet->active && !applet->pause && applet->elapsed);
gtk_action_set_sensitive (gtk_action_group_get_action (applet->action_group, "Preferences"), !applet->active && !applet->pause);
g_free (name);
return retval;
}
/* start action */
static void
timer_start_callback (GtkAction *action, TimerApplet *applet)
{
applet->active = TRUE;
if (applet->pause)
applet->pause = FALSE;
else
applet->elapsed = 0;
applet->timeout_id = g_timeout_add (STEP, (GSourceFunc) timer_callback, applet);
}
/* pause action */
static void
timer_pause_callback (GtkAction *action, TimerApplet *applet)
{
applet->pause = TRUE;
if (applet->timeout_id != 0)
{
g_source_remove(applet->timeout_id);
applet->timeout_id = 0;
}
timer_callback (applet);
}
/* stop action */
static void
timer_stop_callback (GtkAction *action, TimerApplet *applet)
{
applet->active = FALSE;
if (applet->timeout_id != 0)
{
g_source_remove(applet->timeout_id);
applet->timeout_id = 0;
}
timer_callback (applet);
}
/* reset action */
static void
timer_reset_callback (GtkAction *action, TimerApplet *applet)
{
applet->active = FALSE;
applet->pause = FALSE;
applet->elapsed = 0;
timer_callback (applet);
}
/* Show the about dialog */
static void
timer_about_callback (GtkAction *action, TimerApplet *applet)
{
const char* authors[] = { "Stefano Karapetsas <stefano@karapetsas.com>", NULL };
gtk_show_about_dialog(NULL,
"title", _("About Timer Applet"),
"version", VERSION,
"copyright", _("Copyright \xc2\xa9 2014 Stefano Karapetsas\n"
"Copyright \xc2\xa9 2015-2020 MATE developers"),
"authors", authors,
"comments", _("Start a timer and receive a notification when it is finished"),
"translator-credits", _("translator-credits"),
"logo-icon-name", APPLET_ICON,
NULL);
}
/* calculate duration and save in GSettings */
static void
timer_spin_button_value_changed (GtkSpinButton *spinbutton, TimerApplet *applet)
{
gint duration;
duration = gtk_spin_button_get_value_as_int (applet->seconds);
duration += gtk_spin_button_get_value_as_int (applet->minutes) * 60;
duration += gtk_spin_button_get_value_as_int (applet->hours) * 3600;
g_settings_set_int (applet->settings, DURATION_KEY, duration);
}
/* Show the preferences dialog */
static void
timer_preferences_callback (GtkAction *action, TimerApplet *applet)
{
GtkBuilder *builder;
GtkDialog *dialog;
gint duration, hours, minutes, seconds;
builder = gtk_builder_new_from_resource ("/org/mate/mate-applets/timerapplet/timerapplet-preferences.ui");
dialog = GET_DIALOG ("preferences_dialog");
applet->hours = GET_SPIN_BUTTON ("hours_spinbutton");
applet->minutes = GET_SPIN_BUTTON ("minutes_spinbutton");
applet->seconds = GET_SPIN_BUTTON ("seconds_spinbutton");
duration = g_settings_get_int (applet->settings, DURATION_KEY);
hours = duration / 60 / 60;
minutes = duration / 60 % 60;
seconds = duration % 60;
gtk_spin_button_set_value (applet->hours, hours);
gtk_spin_button_set_value (applet->minutes, minutes);
gtk_spin_button_set_value (applet->seconds, seconds);
g_settings_bind (applet->settings, NAME_KEY, GET_WIDGET ("name_entry"), "text", G_SETTINGS_BIND_DEFAULT);
g_settings_bind (applet->settings, SHOW_NOTIFICATION_KEY, GET_WIDGET ("show_notification_popup_check"), "active", G_SETTINGS_BIND_DEFAULT);
g_settings_bind (applet->settings, SHOW_DIALOG_KEY, GET_WIDGET ("show_dialog_check"), "active", G_SETTINGS_BIND_DEFAULT);
/* signals */
gtk_builder_add_callback_symbols (builder,
"on_hours_spinbutton_value_changed", G_CALLBACK (timer_spin_button_value_changed),
"on_minutes_spinbutton_value_changed", G_CALLBACK (timer_spin_button_value_changed),
"on_seconds_spinbutton_value_changed", G_CALLBACK (timer_spin_button_value_changed),
NULL);
gtk_builder_connect_signals (builder, applet);
g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog);
g_object_unref (builder);
gtk_widget_show_all (GTK_WIDGET (dialog));
}
static gboolean
timer_applet_click (TimerApplet *applet)
{
if (!applet->active && !applet->pause && applet->elapsed)
timer_reset_callback (NULL, applet);
else if (applet->active && !applet->pause)
timer_pause_callback (NULL, applet);
else if (!applet->active || applet->pause)
timer_start_callback (NULL, applet);
return FALSE;
}
static void
timer_settings_changed (GSettings *settings, gchar *key, TimerApplet *applet)
{
timer_callback (applet);
}
static gboolean
timer_applet_fill (MatePanelApplet* applet_widget)
{
TimerApplet *applet;
g_set_application_name (_("Timer Applet"));
gtk_window_set_default_icon_name (APPLET_ICON);
if (!notify_is_initted ())
notify_init ("timer-applet");
mate_panel_applet_set_flags (applet_widget, MATE_PANEL_APPLET_EXPAND_MINOR);
mate_panel_applet_set_background_widget (MATE_PANEL_APPLET (applet_widget), GTK_WIDGET (applet_widget));
applet = g_malloc0(sizeof(TimerApplet));
applet->applet = applet_widget;
applet->settings = mate_panel_applet_settings_new (applet_widget,TIMER_SCHEMA);
applet->timeout_id = 0;
applet->active = FALSE;
applet->pause = FALSE;
applet->box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
applet->image = GTK_IMAGE (gtk_image_new_from_icon_name (APPLET_ICON, GTK_ICON_SIZE_BUTTON));
applet->pause_image = GTK_IMAGE (gtk_image_new_from_icon_name ("media-playback-pause", GTK_ICON_SIZE_BUTTON));
applet->label = GTK_LABEL (gtk_label_new (""));
/* we add the Gtk label into the applet */
gtk_box_pack_start (applet->box,
GTK_WIDGET (applet->image),
TRUE, TRUE, 0);
gtk_box_pack_start (applet->box,
GTK_WIDGET (applet->pause_image),
TRUE, TRUE, 0);
gtk_box_pack_start (applet->box,
GTK_WIDGET (applet->label),
TRUE, TRUE, 3);
gtk_container_add (GTK_CONTAINER (applet_widget),
GTK_WIDGET (applet->box));
gtk_widget_show_all (GTK_WIDGET (applet->applet));
gtk_widget_hide (GTK_WIDGET (applet->pause_image));
g_signal_connect(G_OBJECT (applet->applet), "destroy",
G_CALLBACK (timer_applet_destroy),
applet);
g_signal_connect_swapped(GTK_WIDGET (applet->applet), "button-release-event",
G_CALLBACK (timer_applet_click), applet);
/* set up context menu */
applet->action_group = gtk_action_group_new ("Timer Applet Actions");
gtk_action_group_set_translation_domain (applet->action_group, GETTEXT_PACKAGE);
gtk_action_group_add_actions (applet->action_group, applet_menu_actions,
G_N_ELEMENTS (applet_menu_actions), applet);
mate_panel_applet_setup_menu (applet->applet, ui, applet->action_group);
/* execute callback to set actions sensitiveness */
timer_callback (applet);
/* GSettings callback */
g_signal_connect (G_OBJECT (applet->settings), "changed",
G_CALLBACK (timer_settings_changed), applet);
return TRUE;
}
/* this function, called by mate-panel, will create the applet */
static gboolean
timer_factory (MatePanelApplet* applet, const char* iid, gpointer data)
{
gboolean retval = FALSE;
if (!g_strcmp0 (iid, "TimerApplet"))
retval = timer_applet_fill (applet);
return retval;
}
/* needed by mate-panel applet library */
MATE_PANEL_APPLET_OUT_PROCESS_FACTORY("TimerAppletFactory",
PANEL_TYPE_APPLET,
"Timer applet",
timer_factory,
NULL)
|