File: system-tray.c

package info (click to toggle)
balsa 2.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,576 kB
  • sloc: ansic: 99,871; xml: 4,934; makefile: 769; sh: 185; awk: 60; python: 34
file content (142 lines) | stat: -rw-r--r-- 3,710 bytes parent folder | download
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
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
 *
 * Copyright (C) 1997-2022 Stuart Parmenter and others,
 *                         See the file AUTHORS for a list.
 *
 * 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, 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, see <https://www.gnu.org/licenses/>.
 */

#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif                          /* HAVE_CONFIG_H */


#ifdef ENABLE_SYSTRAY

#include <glib/gi18n.h>
#include <libxapp/xapp-status-icon.h>
#include "files.h"
#include "system-tray.h"


static XAppStatusIcon *status_icon;
static gchar *icon_path[2] = {NULL, NULL};
static gboolean icon_attention;
static GtkMenu *icon_menu;
static libbalsa_systray_cb_t icon_activate_cb;
static gpointer icon_activate_cb_data;


static void systray_cb_internal(XAppStatusIcon *icon,
								guint           button,
								guint           time,
								gpointer        user_data);


void
libbalsa_systray_icon_init(GtkMenu *menu, libbalsa_systray_cb_t activate_cb, gpointer activate_cb_data)
{
	g_return_if_fail(icon_path[0] == NULL);

	icon_path[0] = libbalsa_pixmap_finder("balsa_icon.png");
	icon_path[1] = libbalsa_pixmap_finder("balsa_attention.png");
	icon_activate_cb = activate_cb;
	icon_activate_cb_data = activate_cb_data;
	if (menu != NULL) {
		icon_menu = g_object_ref(menu);
	}
}


void
libbalsa_systray_icon_enable(gboolean enable)
{
	g_return_if_fail(icon_path[0] != NULL);

	if (enable) {
		if (status_icon == NULL) {
			status_icon = xapp_status_icon_new();
			if (icon_menu != NULL) {
				xapp_status_icon_set_secondary_menu(status_icon, icon_menu);
			}
			if (icon_activate_cb != NULL) {
				g_signal_connect(status_icon, "activate", G_CALLBACK(systray_cb_internal), icon_activate_cb_data);
			}
			xapp_status_icon_set_visible(status_icon, TRUE);
			icon_attention = TRUE;
			libbalsa_systray_icon_attention(FALSE);
		}
	} else {
		if (status_icon != NULL) {
			g_object_unref(status_icon);
			status_icon = NULL;
		}
	}
}


void
libbalsa_systray_icon_attention(gboolean attention)
{
	g_return_if_fail(icon_path[0] != NULL);

	if ((status_icon != NULL) && (attention != icon_attention)) {
		icon_attention = attention;
		if (attention) {
			xapp_status_icon_set_icon_name(status_icon, icon_path[1]);
			xapp_status_icon_set_tooltip_text(status_icon, _("Balsa: you have new mail"));
		} else {
			xapp_status_icon_set_icon_name(status_icon, icon_path[0]);
			xapp_status_icon_set_tooltip_text(status_icon, _("Balsa"));
		}
	}
}


void
libbalsa_systray_icon_destroy(void)
{
	size_t n;

	if (icon_path[0] != NULL) {
		icon_activate_cb = NULL;
		if (icon_menu != NULL) {
			g_object_unref(icon_menu);
			icon_menu = NULL;
		}
		for (n = 0; n < G_N_ELEMENTS(icon_path); n++) {
			g_free(icon_path[n]);
			icon_path[n] = NULL;
		}
		if (status_icon != NULL) {
			g_object_unref(status_icon);
			status_icon = NULL;
		}
	}
}


static void
systray_cb_internal(XAppStatusIcon G_GNUC_UNUSED *icon, guint button, guint G_GNUC_UNUSED time, gpointer user_data)
{
	g_return_if_fail(status_icon != NULL);

	if ((button == 1) && (icon_activate_cb != NULL)) {
		icon_activate_cb(user_data);
	}
}


#endif /* ENABLE_SYSTRAY */