1 /* Notification plugin for Claws Mail
2 * Copyright (C) 2005-2007 Holger Berndt
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # include "claws-features.h"
24 #include <glib/gi18n.h>
26 #include "common/claws.h"
27 #include "common/version.h"
28 #include "common/utils.h"
29 #include "common/defs.h"
31 #include "common/hooks.h"
33 #include "mainwindow.h"
35 #include "gtk/gtkutils.h"
37 #include "notification_plugin.h"
38 #include "notification_core.h"
39 #include "notification_prefs.h"
40 #include "notification_banner.h"
41 #include "notification_lcdproc.h"
42 #include "notification_trayicon.h"
43 #include "notification_indicator.h"
44 #include "notification_hotkeys.h"
45 #include "notification_foldercheck.h"
46 #include "notification_pixbuf.h"
50 # include <libnotify/notify.h>
53 /* delay in ms until the tray icon shall be embedded in the
54 system tray before it is assumed that the tray doesn't
55 work and the main window is shown again */
56 #define CM_NOTIFICATION_TRAYICON_SAFETY_NET_DELAY_MS 5000
58 static gboolean my_folder_item_update_hook(gpointer, gpointer);
59 static gboolean my_folder_update_hook(gpointer, gpointer);
60 static gboolean my_msginfo_update_hook(gpointer, gpointer);
61 static gboolean my_offline_switch_hook(gpointer, gpointer);
62 static gboolean my_main_window_close_hook(gpointer, gpointer);
63 static gboolean my_main_window_got_iconified_hook(gpointer, gpointer);
64 static gboolean my_account_list_changed_hook(gpointer, gpointer);
65 static gboolean my_update_theme_hook(gpointer, gpointer);
67 #ifdef NOTIFICATION_TRAYICON
68 static gboolean trayicon_startup_idle(gpointer);
71 static gulong hook_f_item;
73 static gulong hook_m_info;
74 static gulong hook_offline;
75 static gulong hook_mw_close;
76 static gulong hook_got_iconified;
77 static gulong hook_account;
78 static gulong hook_theme_changed;
80 #ifdef NOTIFICATION_BANNER
81 static GSList* banner_collected_msgs;
84 void notification_update_urgency_hint(void)
87 mainwin = mainwindow_get_mainwindow();
89 NotificationMsgCount count;
92 if(notify_config.urgency_hint_new || notify_config.urgency_hint_unread) {
93 notification_core_get_msg_count(NULL, &count);
94 if(notify_config.urgency_hint_new)
95 active = (active || (count.new_msgs > 0));
96 if(notify_config.urgency_hint_unread)
97 active = (active || (count.unread_msgs > 0));
99 gtk_window_set_urgency_hint(GTK_WINDOW(mainwin->window), active);
103 static gboolean my_account_list_changed_hook(gpointer source,
106 gboolean retVal = FALSE;
108 #ifdef NOTIFICATION_TRAYICON
109 notification_update_msg_counts(NULL);
110 retVal = notification_trayicon_account_list_changed(source, data);
116 static gboolean my_update_theme_hook(gpointer source, gpointer data)
118 notification_pixbuf_free_all();
119 #ifdef NOTIFICATION_TRAYICON
120 notification_update_trayicon();
125 static gboolean my_main_window_got_iconified_hook(gpointer source,
128 gboolean retVal = FALSE;
129 #ifdef NOTIFICATION_TRAYICON
130 notification_update_msg_counts(NULL);
131 retVal = notification_trayicon_main_window_got_iconified(source, data);
136 static gboolean my_main_window_close_hook(gpointer source, gpointer data)
138 gboolean retVal = FALSE;
139 #ifdef NOTIFICATION_TRAYICON
140 notification_update_msg_counts(NULL);
141 retVal = notification_trayicon_main_window_close(source, data);
146 static gboolean my_offline_switch_hook(gpointer source, gpointer data)
148 #ifdef NOTIFICATION_TRAYICON
149 notification_update_msg_counts(NULL);
154 static gboolean my_folder_item_update_hook(gpointer source, gpointer data)
156 FolderItemUpdateData *update_data = source;
160 g_return_val_if_fail(source != NULL, FALSE);
162 if (folder_has_parent_of_type(update_data->item, F_DRAFT))
165 #if defined(NOTIFICATION_LCDPROC) || defined(NOTIFICATION_TRAYICON) || defined(NOTIFICATION_INDICATOR)
166 notification_update_msg_counts(NULL);
168 if(notify_config.urgency_hint_new || notify_config.urgency_hint_unread)
169 notification_update_msg_counts(NULL);
172 /* Check if the folder types is to be notified about */
173 ftype = update_data->item->folder->klass->type;
174 uistr = update_data->item->folder->klass->uistr;
175 if(!notify_include_folder_type(ftype, uistr))
178 if(update_data->update_flags & F_ITEM_UPDATE_MSGCNT) {
179 #ifdef NOTIFICATION_BANNER
180 notification_update_banner();
182 #if defined(NOTIFICATION_POPUP) || defined(NOTIFICATION_COMMAND)
183 notification_new_unnotified_msgs(update_data);
189 static gboolean my_folder_update_hook(gpointer source, gpointer data)
191 FolderUpdateData *hookdata;
193 g_return_val_if_fail(source != NULL, FALSE);
196 #if defined(NOTIFICATION_LCDPROC) || defined(NOTIFICATION_TRAYICON)
197 if(hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
198 notification_update_msg_counts(hookdata->item);
200 notification_update_msg_counts(NULL);
208 static gboolean my_msginfo_update_hook(gpointer source, gpointer data)
210 return notification_notified_hash_msginfo_update((MsgInfoUpdate*)source);
213 gint plugin_init(gchar **error)
218 /* No be able to test against new-contacts */
219 if(!check_plugin_version(MAKE_NUMERIC_VERSION(3,8,1,46),
220 VERSION_NUMERIC, _("Notification"), error))
223 /* Check if threading is enabled */
224 if(!g_thread_supported()) {
225 *error = g_strdup(_("The Notification plugin needs threading support."));
229 hook_f_item = hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST,
230 my_folder_item_update_hook, NULL);
231 if(hook_f_item == 0) {
232 *error = g_strdup(_("Failed to register folder item update hook in the "
233 "Notification plugin"));
237 hook_f = hooks_register_hook(FOLDER_UPDATE_HOOKLIST,
238 my_folder_update_hook, NULL);
240 *error = g_strdup(_("Failed to register folder update hook in the "
241 "Notification plugin"));
242 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_f_item);
247 hook_m_info = hooks_register_hook(MSGINFO_UPDATE_HOOKLIST,
248 my_msginfo_update_hook, NULL);
249 if(hook_m_info == 0) {
250 *error = g_strdup(_("Failed to register msginfo update hook in the "
251 "Notification plugin"));
252 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_f_item);
253 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, hook_f);
257 hook_offline = hooks_register_hook(OFFLINE_SWITCH_HOOKLIST,
258 my_offline_switch_hook, NULL);
259 if(hook_offline == 0) {
260 *error = g_strdup(_("Failed to register offline switch hook in the "
261 "Notification plugin"));
262 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_f_item);
263 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, hook_f);
264 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST, hook_m_info);
268 hook_mw_close = hooks_register_hook(MAIN_WINDOW_CLOSE,
269 my_main_window_close_hook, NULL);
270 if(hook_mw_close == 0) {
271 *error = g_strdup(_("Failed to register main window close hook in the "
272 "Notification plugin"));
273 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_f_item);
274 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, hook_f);
275 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST, hook_m_info);
276 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, hook_offline);
280 hook_got_iconified = hooks_register_hook(MAIN_WINDOW_GOT_ICONIFIED,
281 my_main_window_got_iconified_hook,
283 if(hook_got_iconified == 0) {
284 *error = g_strdup(_("Failed to register got iconified hook in the "
285 "Notification plugin"));
286 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_f_item);
287 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, hook_f);
288 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST, hook_m_info);
289 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, hook_offline);
290 hooks_unregister_hook(MAIN_WINDOW_CLOSE, hook_mw_close);
294 hook_account = hooks_register_hook(ACCOUNT_LIST_CHANGED_HOOKLIST,
295 my_account_list_changed_hook, NULL);
296 if (hook_account == 0) {
297 *error = g_strdup(_("Failed to register account list changed hook in the "
298 "Notification plugin"));
299 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_f_item);
300 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, hook_f);
301 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST, hook_m_info);
302 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, hook_offline);
303 hooks_unregister_hook(MAIN_WINDOW_CLOSE, hook_mw_close);
304 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, hook_got_iconified);
308 hook_theme_changed = hooks_register_hook(THEME_CHANGED_HOOKLIST, my_update_theme_hook, NULL);
309 if(hook_theme_changed == 0) {
310 *error = g_strdup(_("Failed to register theme change hook in the "
311 "Notification plugin"));
312 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_f_item);
313 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, hook_f);
314 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST, hook_m_info);
315 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, hook_offline);
316 hooks_unregister_hook(MAIN_WINDOW_CLOSE, hook_mw_close);
317 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, hook_got_iconified);
318 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, hook_account);
323 prefs_set_default(notify_param);
324 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
325 prefs_read_config(notify_param, "NotificationPlugin", rcpath, NULL);
328 /* Folder specific stuff */
329 notification_foldercheck_read_array();
331 notification_notified_hash_startup_init();
335 #ifdef NOTIFICATION_INDICATOR
336 notification_indicator_setup();
338 #ifdef NOTIFICATION_BANNER
339 notification_update_banner();
341 #ifdef NOTIFICATION_LCDPROC
342 notification_lcdproc_connect();
344 #ifdef NOTIFICATION_TRAYICON
345 if(notify_config.trayicon_enabled &&
346 notify_config.trayicon_hide_at_startup && claws_is_starting()) {
347 MainWindow *mainwin = mainwindow_get_mainwindow();
349 g_timeout_add(CM_NOTIFICATION_TRAYICON_SAFETY_NET_DELAY_MS, trayicon_startup_idle,NULL);
350 if(mainwin && gtk_widget_get_visible(GTK_WIDGET(mainwin->window)))
351 main_window_hide(mainwin);
352 main_set_show_at_startup(FALSE);
355 my_account_list_changed_hook(NULL,NULL);
357 if(notify_config.urgency_hint_new || notify_config.urgency_hint_unread)
358 notification_update_msg_counts(NULL);
360 #ifdef NOTIFICATION_HOTKEYS
361 notification_hotkeys_update_bindings();
364 debug_print("Notification plugin loaded\n");
369 gboolean plugin_done(void)
371 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_f_item);
372 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, hook_f);
373 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST, hook_m_info);
374 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, hook_offline);
375 hooks_unregister_hook(MAIN_WINDOW_CLOSE, hook_mw_close);
376 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, hook_got_iconified);
377 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, hook_account);
378 hooks_unregister_hook(THEME_CHANGED_HOOKLIST, hook_theme_changed);
380 notify_save_config();
384 /* foldercheck cleanup */
385 notification_foldercheck_write_array();
386 notification_free_folder_specific_array();
388 #ifdef NOTIFICATION_BANNER
389 notification_collected_msgs_free(banner_collected_msgs);
390 banner_collected_msgs = NULL;
391 notification_banner_destroy();
393 #ifdef NOTIFICATION_LCDPROC
394 notification_lcdproc_disconnect();
396 #ifdef NOTIFICATION_TRAYICON
397 notification_trayicon_destroy();
399 #ifdef NOTIFICATION_INDICATOR
400 notification_indicator_destroy();
402 notification_core_free();
404 #ifdef HAVE_LIBNOTIFY
405 if(notify_is_initted())
409 #ifdef NOTIFICATION_HOTKEYS
410 notification_hotkeys_unbind_all();
413 notification_pixbuf_free_all();
415 debug_print("Notification plugin unloaded\n");
417 /* Returning FALSE here means that g_module_close() will not be called on the plugin.
418 * This is necessary, as needed libraries are not designed to be unloaded. */
422 const gchar *plugin_name(void)
424 return _("Notification");
427 const gchar *plugin_desc(void)
429 return _("This plugin provides various ways "
430 "to notify the user of new and unread email.\n"
431 "The plugin is extensively configurable in the "
432 "plugins section of the preferences dialog.\n\n"
433 "Feedback to <berndth@gmx.de> is welcome.");
436 const gchar *plugin_type(void)
441 const gchar *plugin_licence(void)
446 const gchar *plugin_version(void)
451 struct PluginFeature *plugin_provides(void)
453 static struct PluginFeature features[] =
454 { {PLUGIN_NOTIFIER, N_("Various tools")},
455 {PLUGIN_NOTHING, NULL}};
459 #ifdef NOTIFICATION_BANNER
460 void notification_update_banner(void)
462 notification_collected_msgs_free(banner_collected_msgs);
463 banner_collected_msgs = NULL;
465 if(notify_config.banner_show != NOTIFY_BANNER_SHOW_NEVER) {
467 GSList *folder_list = NULL;
469 if(notify_config.banner_folder_specific) {
470 id = notification_register_folder_specific_list
471 (BANNER_SPECIFIC_FOLDER_ID_STR);
472 folder_list = notification_foldercheck_get_list(id);
475 if(!(notify_config.banner_folder_specific && (folder_list == NULL)))
476 banner_collected_msgs =
477 notification_collect_msgs(notify_config.banner_include_unread,
478 notify_config.banner_folder_specific ?
479 folder_list : NULL, notify_config.banner_max_msgs);
482 notification_banner_show(banner_collected_msgs);
486 #ifdef NOTIFICATION_TRAYICON
487 static gboolean trayicon_startup_idle(gpointer data)
489 /* if the trayicon is not available,
490 simulate click on it to show mainwindow */
491 if(!notification_trayicon_is_available())
492 notification_trayicon_on_activate(NULL,data);