1 /*
2 * tray.c
3 * vim: expandtab:ts=4:sts=4:sw=4
4 *
5 * Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
6 *
7 * This file is part of Profanity.
8 *
9 * Profanity is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Profanity is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Profanity. If not, see <https://www.gnu.org/licenses/>.
21 *
22 * In addition, as a special exception, the copyright holders give permission to
23 * link the code of portions of this program with the OpenSSL library under
24 * certain conditions as described in each individual source file, and
25 * distribute linked combinations including the two.
26 *
27 * You must obey the GNU General Public License in all respects for all of the
28 * code used other than OpenSSL. If you modify file(s) with this exception, you
29 * may extend this exception to your version of the file(s), but you are not
30 * obligated to do so. If you do not wish to do so, delete this exception
31 * statement from your version. If you delete this exception statement from all
32 * source files in the program, then also delete it here.
33 *
34 */
35
36 #include "config.h"
37
38 #ifdef HAVE_GTK
39 #include <gtk/gtk.h>
40 #include <glib.h>
41 #include <glib/gstdio.h>
42 #include <stdlib.h>
43
44 #include "log.h"
45 #include "config/preferences.h"
46 #include "config/files.h"
47 #include "ui/tray.h"
48 #include "ui/window_list.h"
49
50 static gboolean gtk_ready = FALSE;
51 static GtkStatusIcon* prof_tray = NULL;
52 static GString* icon_filename = NULL;
53 static GString* icon_msg_filename = NULL;
54 static gint unread_messages;
55 static gboolean shutting_down;
56 static guint timer;
57
58 /*
59 * Get icons from installation share folder or (if defined) .locale user's folder
60 *
61 * As implementation, looking through all the entries in the .locale folder is chosen.
62 * While useless as now, it might be useful in case an association name-icon is created.
63 * As now, with 2 icons only, this is pretty useless, but it is not harming ;)
64 *
65 */
66 static void
_get_icons(void)67 _get_icons(void)
68 {
69 GString* icons_dir = NULL;
70
71 #ifdef ICONS_PATH
72
73 icons_dir = g_string_new(ICONS_PATH);
74 icon_filename = g_string_new(icons_dir->str);
75 icon_msg_filename = g_string_new(icons_dir->str);
76 g_string_append(icon_filename, "/proIcon.png");
77 g_string_append(icon_msg_filename, "/proIconMsg.png");
78 g_string_free(icons_dir, TRUE);
79
80 #endif /* ICONS_PATH */
81
82 gchar* icons_dir_s = files_get_config_path(DIR_ICONS);
83 icons_dir = g_string_new(icons_dir_s);
84 g_free(icons_dir_s);
85 GError* err = NULL;
86
87 if (!g_file_test(icons_dir->str, G_FILE_TEST_IS_DIR)) {
88 return;
89 }
90
91 GDir* dir = g_dir_open(icons_dir->str, 0, &err);
92 if (dir) {
93 GString* name = g_string_new(g_dir_read_name(dir));
94 while (name->len) {
95 if (g_strcmp0("proIcon.png", name->str) == 0) {
96 if (icon_filename) {
97 g_string_free(icon_filename, TRUE);
98 }
99 icon_filename = g_string_new(icons_dir->str);
100 g_string_append(icon_filename, "/proIcon.png");
101 } else if (g_strcmp0("proIconMsg.png", name->str) == 0) {
102 if (icon_msg_filename) {
103 g_string_free(icon_msg_filename, TRUE);
104 }
105 icon_msg_filename = g_string_new(icons_dir->str);
106 g_string_append(icon_msg_filename, "/proIconMsg.png");
107 }
108 g_string_free(name, TRUE);
109 name = g_string_new(g_dir_read_name(dir));
110 }
111 g_string_free(name, TRUE);
112 } else {
113 log_error("Unable to open dir: %s", err->message);
114 g_error_free(err);
115 }
116 g_dir_close(dir);
117 g_string_free(icons_dir, TRUE);
118 }
119
120 /*
121 * Callback for the timer
122 *
123 * This is the callback that the timer is calling in order to check if messages are there.
124 *
125 */
126 gboolean
_tray_change_icon(gpointer data)127 _tray_change_icon(gpointer data)
128 {
129 if (shutting_down) {
130 return FALSE;
131 }
132
133 unread_messages = wins_get_total_unread();
134
135 if (unread_messages) {
136 if (!prof_tray) {
137 prof_tray = gtk_status_icon_new_from_file(icon_msg_filename->str);
138 } else {
139 gtk_status_icon_set_from_file(prof_tray, icon_msg_filename->str);
140 }
141 } else {
142 if (prefs_get_boolean(PREF_TRAY_READ)) {
143 if (!prof_tray) {
144 prof_tray = gtk_status_icon_new_from_file(icon_filename->str);
145 } else {
146 gtk_status_icon_set_from_file(prof_tray, icon_filename->str);
147 }
148 } else {
149 g_clear_object(&prof_tray);
150 prof_tray = NULL;
151 }
152 }
153
154 return TRUE;
155 }
156
157 void
tray_init(void)158 tray_init(void)
159 {
160 _get_icons();
161 gtk_ready = gtk_init_check(0, NULL);
162 log_debug("Env is GTK-ready: %s", gtk_ready ? "true" : "false");
163 if (!gtk_ready) {
164 return;
165 }
166
167 if (prefs_get_boolean(PREF_TRAY)) {
168 log_debug("Building GTK icon");
169 tray_enable();
170 }
171
172 gtk_main_iteration_do(FALSE);
173 }
174
175 void
tray_update(void)176 tray_update(void)
177 {
178 if (gtk_ready) {
179 gtk_main_iteration_do(FALSE);
180 }
181 }
182
183 void
tray_shutdown(void)184 tray_shutdown(void)
185 {
186 if (gtk_ready && prefs_get_boolean(PREF_TRAY)) {
187 tray_disable();
188 }
189 g_string_free(icon_filename, TRUE);
190 g_string_free(icon_msg_filename, TRUE);
191 }
192
193 void
tray_set_timer(int interval)194 tray_set_timer(int interval)
195 {
196 g_source_remove(timer);
197 _tray_change_icon(NULL);
198 timer = g_timeout_add(interval * 1000, _tray_change_icon, NULL);
199 }
200
201 /*
202 * Create tray icon
203 *
204 * This will initialize the timer that will be called in order to change the icons
205 * and will search the icons in the defaults paths
206 */
207 void
tray_enable(void)208 tray_enable(void)
209 {
210 prof_tray = gtk_status_icon_new_from_file(icon_filename->str);
211 shutting_down = FALSE;
212 _tray_change_icon(NULL);
213 int interval = prefs_get_tray_timer() * 1000;
214 timer = g_timeout_add(interval, _tray_change_icon, NULL);
215 }
216
217 void
tray_disable(void)218 tray_disable(void)
219 {
220 shutting_down = TRUE;
221 g_source_remove(timer);
222 if (prof_tray) {
223 g_clear_object(&prof_tray);
224 prof_tray = NULL;
225 }
226 }
227
228 #endif
229