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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/*
* Copyright (c) Linux community.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <gst/gst.h> // GST_STATE_* values
#include "log.h"
#include "support.h"
#include "utility.h"
#include "dconf.h"
#include "about.h"
#include "rec-window.h"
#include "rec-manager.h"
// Macro to support GTK 3
#define gtk_menu_append(menu, menu_item) gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item)
// NOTICE:
// Undef this variable to test tray-icon with GNOME 2.x style.
// #undef HAS_APP_INDICATOR
// Note: HAS_APP_INDICATOR is defined in configure.in.
#ifdef HAS_APP_INDICATOR
#include <libappindicator/app-indicator.h>
#define INSTALLED_INDICATOR_TYPE "Unity's AppIndicator class"
#else
#define INSTALLED_INDICATOR_TYPE "GNOME 2.x/3.x GtkStatusIcon class"
#endif
// Note: Test the systray icon with and without the "indicator-applet" package.
// Notice the differences in the icon and its right-click menu.
// Note: Uncomment this to show debug messages from this module
// #define DEBUG_SYSTRAY
#if defined(DEBUG_SYSTRAY) || defined(DEBUG_ALL)
#define LOG_SYSTRAY LOG_MSG
#else
#define LOG_SYSTRAY(x, ...)
#endif
// Systray icon
static GtkWidget *g_tray_icon = NULL;
// Menu on the icon
static GtkWidget *g_tray_menu = NULL;
static void systray_icon_create();
static void systray_icon_remove();
static void systray_set_icon(gchar *icon_name);
static void systray_set_menu_items(GtkWidget *menu, gint state);
void systray_module_init() {
;
}
void systray_module_exit() {
systray_icon_remove();
}
void systray_icon_setup() {
systray_icon_create();
}
gboolean systray_icon_is_installed() {
return G_IS_OBJECT(g_tray_icon);
}
const gchar *systray_get_indicator_type() {
return INSTALLED_INDICATOR_TYPE;
}
void systray_set_menu_items1(gint state) {
systray_set_menu_items(g_tray_menu, state);
}
void systray_set_menu_items2(gboolean show) {
if (!GTK_IS_MENU(g_tray_menu)) return;
// Update "Show/Hide Window" menu item
GtkWidget *menu_item = (GtkWidget*)g_object_get_data(G_OBJECT(g_tray_menu), "menu-item-show-window");
if (!(GTK_IS_MENU(g_tray_menu) && GTK_IS_MENU_ITEM(menu_item))) return;
LOG_SYSTRAY("systray_set_menu_items2, show:%d\n", show);
if (!show) {
// Tray menu.
// Translators: This belongs to the tray menu (menu on the system tray).
gtk_menu_item_set_label(GTK_MENU_ITEM(menu_item), _("Show window"));
} else {
// Tray menu.
// Translators: This belongs to the tray menu (menu on the system tray).
gtk_menu_item_set_label(GTK_MENU_ITEM(menu_item), _("Hide window"));
}
}
static void systray_popup_menu_cb(GtkWidget * widget, gpointer data) {
gchar *cmd = (gchar*)data;
LOG_SYSTRAY("%s: systray_popup_menu_cb: %s\n", INSTALLED_INDICATOR_TYPE, cmd);
if (!g_strcmp0(cmd, "start")) {
rec_manager_flip_recording();
} else if (!g_strcmp0(cmd, "continue")) {
rec_manager_flip_recording();
} else if (!g_strcmp0(cmd, "stop")) {
rec_manager_stop_recording();
} else if (!g_strcmp0(cmd, "pause")) {
rec_manager_pause_recording();
} else if (!g_strcmp0(cmd, "about")) {
about_this_app();
} else if (!g_strcmp0(cmd, "settings")) {
// Not implemented
} else if (!g_strcmp0(cmd, "quit")) {
win_close_button_cb(NULL, GINT_TO_POINTER(TRUE)/*force quit*/);
} else if (!g_strcmp0(cmd, "show-folder")) {
// Open file browser and display content of Audio-folder.
// Audio folder
gchar *audio_folder = get_audio_folder();
// Fire default file browser
GError *error = NULL;
open_tool_for_file(audio_folder, "nautilus", &error);
if (error) {
// Translator: This error message is shown in a MessageBox.
gchar *msg = g_strdup_printf(_("Cannot start file browser.\nPlease display %s manually."), audio_folder);
messagebox_error(msg, NULL);
g_free(msg);
g_error_free(error);
}
g_free(audio_folder);
} else if (!g_strcmp0(cmd, "show") || !g_strcmp0(cmd, "hide")) {
// Main window is visible?
gboolean visible = win_window_is_visible();
// Main window is minimized/iconified?
GdkWindowState state = win_get_window_state();
win_show_window(!visible || state == GDK_WINDOW_STATE_ICONIFIED);
}
}
static void systray_set_menu_items(GtkWidget *menu, gint state) {
// Update menu items to reflect state of recording
if (!GTK_IS_MENU(menu)) return;
LOG_SYSTRAY("systray_set_menu_items, state:%d\n", state);
// Get menu items
GtkWidget *item_start = (GtkWidget*)g_object_get_data(G_OBJECT(menu), "menu-item-start-recording");
GtkWidget *item_continue = (GtkWidget*)g_object_get_data(G_OBJECT(menu), "menu-item-continue-recording");
GtkWidget *item_stop = (GtkWidget*)g_object_get_data(G_OBJECT(menu), "menu-item-stop-recording");
GtkWidget *item_pause = (GtkWidget*)g_object_get_data(G_OBJECT(menu), "menu-item-pause-recording");
gchar *tray_icon = NULL;
if (state == GST_STATE_PLAYING) {
// Recording is on
gtk_widget_hide(item_start);
gtk_widget_hide(item_continue);
gtk_widget_show(item_stop);
gtk_widget_show(item_pause);
gtk_widget_set_sensitive(item_pause, TRUE);
tray_icon = "audio-recorder-on.png";
} else if (state == GST_STATE_PAUSED) {
// Paused
gtk_widget_hide(item_start);
gtk_widget_show(item_continue);
gtk_widget_show(item_stop);
gtk_widget_show(item_pause);
gtk_widget_set_sensitive(item_pause, FALSE);
tray_icon = "audio-recorder-paused.png";
} else {
// Stopped
gtk_widget_show(item_start);
gtk_widget_hide(item_continue);
gtk_widget_hide(item_stop);
gtk_widget_show(item_pause);
gtk_widget_set_sensitive(item_pause, FALSE);
tray_icon = "audio-recorder-off.png";
}
if (menu == g_tray_menu) {
// Set tray icon
systray_set_icon(tray_icon);
}
}
GtkWidget *systray_create_menu(gboolean for_tray_icon) {
GtkWidget *menu = gtk_menu_new();
GtkWidget *menu_item = NULL;
if (for_tray_icon) {
#ifdef HAS_APP_INDICATOR
// Show main window
// Window is visible?
gboolean visible = win_window_is_visible();
// Window is minimized/nomal?
GdkWindowState state = win_get_window_state();
if (visible && state != GDK_WINDOW_STATE_ICONIFIED ) {
// Tray menu.
menu_item = gtk_menu_item_new_with_label(_("Hide window"));
g_signal_connect((gpointer)menu_item, "activate", G_CALLBACK(systray_popup_menu_cb), "hide");
} else {
// Tray menu.
menu_item = gtk_menu_item_new_with_label(_("Show window"));
g_signal_connect((gpointer)menu_item, "activate", G_CALLBACK(systray_popup_menu_cb), "show");
}
gtk_widget_show(menu_item);
gtk_menu_append(GTK_MENU(menu), menu_item);
// Save this item
g_object_set_data(G_OBJECT(menu), "menu-item-show-window", menu_item);
// Separator line
menu_item = gtk_separator_menu_item_new();
gtk_widget_show(menu_item);
gtk_menu_append(GTK_MENU(menu), menu_item);
#endif
}
// Start recording.
// Translators: This belongs to the tray menu (menu on the system tray).
menu_item = gtk_menu_item_new_with_label(_("Start recording"));
gtk_widget_show(menu_item);
g_signal_connect((gpointer)menu_item, "activate", G_CALLBACK(systray_popup_menu_cb), "start");
gtk_menu_append(GTK_MENU(menu), menu_item);
// Save this item
g_object_set_data(G_OBJECT(menu), "menu-item-start-recording", menu_item);
// Continue recording.
// Translators: This belongs to the tray menu (menu on the system tray).
menu_item = gtk_menu_item_new_with_label(_("Continue recording"));
// Hide this
gtk_widget_hide(menu_item);
g_signal_connect((gpointer)menu_item, "activate", G_CALLBACK(systray_popup_menu_cb), "continue");
gtk_menu_append(GTK_MENU(menu), menu_item);
// Save this item
g_object_set_data(G_OBJECT(menu), "menu-item-continue-recording", menu_item);
// Stop recording.
// Translators: This belongs to the tray menu (menu on the system tray).
menu_item = gtk_menu_item_new_with_label(_("Stop recording"));
// Hide this
gtk_widget_hide(menu_item);
g_signal_connect((gpointer)menu_item, "activate", G_CALLBACK(systray_popup_menu_cb), "stop");
gtk_menu_append(GTK_MENU(menu), menu_item);
// Save this item
g_object_set_data(G_OBJECT(menu), "menu-item-stop-recording", menu_item);
// Pause recording.
// Translators: This belongs to the tray menu (menu on the system tray).
menu_item = gtk_menu_item_new_with_label(_("Pause recording"));
// Hide this
gtk_widget_hide(menu_item);
g_signal_connect((gpointer)menu_item, "activate", G_CALLBACK(systray_popup_menu_cb), "pause");
gtk_menu_append(GTK_MENU(menu), menu_item);
// Save this item
g_object_set_data(G_OBJECT(menu), "menu-item-pause-recording", menu_item);
// Separator line
menu_item = gtk_separator_menu_item_new();
gtk_widget_show(menu_item);
gtk_menu_append(GTK_MENU(menu), menu_item);
// Menu item "Show saved recordings".
// Open Audio-folder and show all saved recordings.
// Translators: This belongs to the tray menu (menu on the system tray)
menu_item = gtk_menu_item_new_with_label(_("Show saved recordings"));
g_signal_connect(G_OBJECT (menu_item), "activate", G_CALLBACK(systray_popup_menu_cb), "show-folder");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
gtk_widget_show(menu_item);
gboolean show_about_item = TRUE;
if (show_about_item) {
// Separator line
menu_item = gtk_separator_menu_item_new();
gtk_widget_show(menu_item);
gtk_menu_append(GTK_MENU(menu), menu_item);
menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_ABOUT, NULL);
g_signal_connect (G_OBJECT (menu_item), "activate", G_CALLBACK(systray_popup_menu_cb), "about");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
gtk_widget_show(menu_item);
}
// Separator line
menu_item = gtk_separator_menu_item_new();
gtk_widget_show(menu_item);
gtk_menu_append(GTK_MENU(menu), menu_item);
menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_QUIT, NULL);
g_signal_connect (G_OBJECT (menu_item), "activate", G_CALLBACK(systray_popup_menu_cb), "quit");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
gtk_widget_show (menu_item);
gtk_widget_show(menu);
// Get recording state
gint state = -1;
gint pending = -1;
rec_manager_get_state(&state, &pending);
// Settle menu items
systray_set_menu_items(menu, state);
return menu;
}
#ifdef HAS_APP_INDICATOR
// Modern tray icons and app indicators. Replaces the older GtkStatusIcon class.
// This is normally supported by Ubuntu's Unity Desktop.
// Ref: https://launchpad.net/indicator-application
static void systray_icon_create() {
// Show tray icon?
gboolean show_icon = FALSE;
conf_get_boolean_value("show-systray-icon", &show_icon);
if (!show_icon) {
systray_icon_remove();
return;
}
LOG_SYSTRAY("systray_icon_create (Unity's AppIndicator class).\n");
if (IS_APP_INDICATOR(g_tray_icon)) {
app_indicator_set_status(APP_INDICATOR(g_tray_icon), APP_INDICATOR_STATUS_ACTIVE);
return;
}
g_tray_icon = (GtkWidget*)app_indicator_new("audio-recorder application",
"audio-recorder-off", APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
g_assert(IS_APP_INDICATOR(g_tray_icon));
app_indicator_set_status(APP_INDICATOR(g_tray_icon), APP_INDICATOR_STATUS_ACTIVE);
// Attach a popup-menu
g_tray_menu = systray_create_menu(TRUE/*for_tray_icon*/);
// Get current recording state
gint state = -1;
gint pending = -1;
rec_manager_get_state(&state, &pending);
// Set initial icon color and menu state
systray_set_menu_items1(state);
app_indicator_set_menu(APP_INDICATOR(g_tray_icon), GTK_MENU(g_tray_menu));
}
static void systray_icon_remove() {
if (G_IS_OBJECT(g_tray_menu)) {
gtk_widget_destroy(GTK_WIDGET(g_tray_menu));
}
g_tray_menu = NULL;
if (IS_APP_INDICATOR(g_tray_icon)) {
LOG_SYSTRAY("systray_icon_remove.\n");
g_object_unref(G_OBJECT(g_tray_icon));
}
g_tray_icon = NULL;
}
static void systray_set_icon(gchar *icon_name) {
if (!IS_APP_INDICATOR(g_tray_icon)) return;
gchar *s = g_strdup(icon_name);
// Remove file extension ".png"
// Simply find the last '.' and put '\0' on it. This should work fine.
gchar *pos = g_strrstr(s, ".");
if (pos) {
*pos = '\0';
}
app_indicator_set_icon(APP_INDICATOR(g_tray_icon), s);
g_free(s);
}
#else
// Create GNOME 2.x type tray icon. Uses the older GtkStatusIcon class.
// This should not be used in Ubuntu's Unity Desktop.
// Ref: http://library.gnome.org/devel/gtk/2.11/GtkStatusIcon.html
static void systray_icon_remove() {
if (G_IS_OBJECT(g_tray_menu)) {
gtk_widget_destroy(GTK_WIDGET(g_tray_menu));
}
g_tray_menu = NULL;
if (G_IS_OBJECT(g_tray_icon)) {
LOG_SYSTRAY("systray_icon_remove.\n");
gtk_status_icon_set_visible(GTK_STATUS_ICON(g_tray_icon), FALSE);
g_object_unref(G_OBJECT(g_tray_icon));
}
g_tray_icon = NULL;
}
static void systray_set_icon(gchar *icon_name) {
if (!GTK_IS_STATUS_ICON(g_tray_icon)) return;
LOG_SYSTRAY("systray_set_icon. icon name:%s\n", icon_name);
gchar *s = g_strdup(icon_name);
// Remove file extension ".png"
// Simply find the last '.' and put '\0' on it. This should work fine.
gchar *pos = g_strrstr(s, ".");
if (pos) {
*pos = '\0';
}
// Set systray icon
gtk_status_icon_set_from_icon_name(GTK_STATUS_ICON(g_tray_icon), s);
g_free(s);
}
static void systray_icon_activate_cb(GtkStatusIcon *status_icon, gpointer data) {
if (win_window_is_visible()) {
// Hide
LOG_SYSTRAY("systray_icon_activate_cb. Hide window\n");
win_show_window(FALSE);
} else {
// Show
LOG_SYSTRAY("systray_icon_activate_cb. Show window\n");
win_show_window(TRUE);
}
}
static void systray_icon_popup_cb(GtkStatusIcon *status_icon, guint button,
guint activate_time, gpointer data) {
gtk_menu_popup(GTK_MENU(g_tray_menu), NULL, NULL, NULL, data, button, activate_time);
}
static void systray_icon_notify_cb(GObject *gobject, GParamSpec *arg1, gpointer data) {
if ( g_str_equal(arg1->name,"size") ) {
guint icon_size = gtk_status_icon_get_size(GTK_STATUS_ICON(g_tray_icon));
LOG_SYSTRAY("systray_icon_notify_cb, icon size:%d.\n", icon_size);
if (icon_size > 0) {}
}
if ( g_str_equal(arg1->name,"embedded") ) {
gboolean b_value = gtk_status_icon_is_embedded(GTK_STATUS_ICON(g_tray_icon));
LOG_SYSTRAY("systray_icon_notify_cb, embedded:%s.\n", b_value ? "True" : "False");
if (b_value) {}
}
if ( g_str_equal(arg1->name,"visible") ) {
gboolean b_value = gtk_status_icon_get_visible(GTK_STATUS_ICON(g_tray_icon));
LOG_SYSTRAY("systray_icon_notify_cb, visible:%s.\n", b_value ? "True" : "False");
if (b_value) {}
}
}
static void systray_icon_create() {
// Show tray icon?
gboolean show_icon = FALSE;
conf_get_boolean_value("show-systray-icon", &show_icon);
if (!show_icon) {
systray_icon_remove();
return;
}
LOG_SYSTRAY("systray_icon_create (GNOME 2.x GtkStatusIcon class).\n");
if (g_tray_icon) {
gtk_status_icon_set_visible(GTK_STATUS_ICON(g_tray_icon), TRUE);
return;
}
g_tray_icon = (GtkWidget*)gtk_status_icon_new_from_icon_name("audio-recorder-off");
// g_object_ref_sink(G_OBJECT(g_tray_icon));
// Setup the signals to track it
g_signal_connect(g_tray_icon, "activate", G_CALLBACK(systray_icon_activate_cb), NULL);
g_signal_connect(g_tray_icon, "popup-menu", G_CALLBACK(systray_icon_popup_cb), NULL);
g_signal_connect(g_tray_icon, "notify::embedded", G_CALLBACK(systray_icon_notify_cb), NULL);
g_signal_connect(g_tray_icon, "notify::visible", G_CALLBACK(systray_icon_notify_cb), NULL);
g_signal_connect(g_tray_icon, "notify::size", G_CALLBACK(systray_icon_notify_cb), NULL);
// Other possible events:
// "scroll-event",
// "button_press_event"
// Attach a popup-menu
g_tray_menu = systray_create_menu(TRUE/*for_tray_icon*/);
// Get current recording state
gint state = -1;
gint pending = -1;
rec_manager_get_state(&state, &pending);
// Set initial icon color and menu state
systray_set_menu_items1(state);
// Attach tooltip message
gchar *prog_name = get_program_name();
// gtk_status_icon_set_tooltip(GTK_STATUS_ICON(g_tray_icon), prog_name);
gtk_widget_set_tooltip_text(g_tray_icon, prog_name);
g_free(prog_name);
}
#endif
|