1 /* Notification plugin for Claws Mail
2  * Copyright (C) 2005-2007 Holger Berndt and the Claws Mail Team.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 /* This module is of course inspired by the trayicon plugin which is
19  * shipped with Claws Mail, copyrighted by the Claws Mail Team. */
20 
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #  include "claws-features.h"
24 #endif
25 
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 
29 #ifdef NOTIFICATION_TRAYICON
30 
31 #include "notification_trayicon.h"
32 #include "notification_prefs.h"
33 #include "notification_core.h"
34 #include "notification_hotkeys.h"
35 #include "notification_pixbuf.h"
36 #include "notification_foldercheck.h"
37 
38 #include "main.h"
39 #include "account.h"
40 #include "mainwindow.h"
41 #include "prefs_common.h"
42 #include "alertpanel.h"
43 #include "gtk/menu.h"
44 #ifndef USE_ALT_ADDRBOOK
45     #include "addressbook.h"
46     #include "addrindex.h"
47 #else
48     #include "addressbook-dbus.h"
49 #endif
50 
51 #include "gtk/manage_window.h"
52 #include "common/utils.h"
53 #include "gtk/gtkutils.h"
54 #include "inc.h"
55 
56 static void notification_trayicon_account_list_reset(const gchar *,
57 														gpointer,
58 														gboolean);
59 
60 static GdkPixbuf* notification_trayicon_create(void);
61 
62 static void notification_trayicon_on_popup_menu(GtkStatusIcon*,guint,
63 						guint,gpointer);
64 static gboolean notification_trayicon_on_size_changed(GtkStatusIcon*,
65 						      gint, gpointer);
66 
67 static void trayicon_get_all_cb(GtkAction*, gpointer);
68 static void trayicon_get_from_account_cb(GtkAction*, gpointer);
69 static void trayicon_compose_cb(GtkAction*, gpointer);
70 static void trayicon_compose_acc_cb(GtkMenuItem*, gpointer);
71 static void trayicon_addressbook_cb(GtkAction*, gpointer);
72 static void trayicon_exit_cb(GtkAction*,gpointer);
73 static void trayicon_toggle_offline_cb(GtkAction*,gpointer);
74 #ifdef HAVE_LIBNOTIFY
75 static void trayicon_toggle_notify_cb(GtkAction*,gpointer);
76 #endif
77 
78 #ifdef HAVE_LIBNOTIFY
79 #include <libnotify/notify.h>
80 
81 #ifndef NOTIFY_CHECK_VERSION
82 # define NOTIFY_CHECK_VERSION(a,b,c) 0
83 #endif
84 
85 typedef struct {
86   gint count;
87   gint num_mail;
88   gint num_news;
89   gint num_calendar;
90   gint num_rss;
91   gchar *msg_path;
92   NotifyNotification *notification;
93   GError *error;
94 } NotificationTrayiconPopup;
95 
96 static NotificationTrayiconPopup popup;
97 
98 static gboolean notification_trayicon_popup_add_msg(MsgInfo*,
99 						    NotificationFolderType);
100 static gboolean notification_trayicon_popup_create(MsgInfo*,
101 						   NotificationFolderType);
102 static void popup_timeout_fun(NotifyNotification*, gpointer);
103 static void notification_trayicon_popup_free_func(gpointer);
104 static void notification_trayicon_popup_default_action_cb(NotifyNotification*,
105 							  const char*,void*);
106 static gchar* notification_trayicon_popup_assemble_summary(void);
107 static gchar* notification_trayicon_popup_assemble_body(MsgInfo*);
108 static void   notification_trayicon_popup_count_msgs(NotificationFolderType);
109 
110 G_LOCK_DEFINE_STATIC(trayicon_popup);
111 
112 #endif
113 
114 static GtkStatusIcon *trayicon;
115 static gboolean updating_menu = FALSE;
116 static GtkWidget *traymenu_popup;
117 static GtkWidget *focused_widget = NULL;
118 
119 static GtkActionEntry trayicon_popup_menu_entries[] = {
120 	{"SysTrayiconPopup", NULL, "SysTrayiconPopup", NULL, NULL, NULL },
121 	{"SysTrayiconPopup/GetMail", NULL, N_("_Get Mail"), NULL, NULL, G_CALLBACK(trayicon_get_all_cb) },
122 	{"SysTrayiconPopup/GetMailAcc", NULL, N_("_Get Mail from account"), NULL, NULL, NULL },
123 	{"SysTrayiconPopup/---", NULL, "---", NULL, NULL, NULL },
124 	{"SysTrayiconPopup/Email", NULL, N_("_Email"), NULL, NULL, G_CALLBACK(trayicon_compose_cb) },
125 	{"SysTrayiconPopup/EmailAcc", NULL, N_("E_mail from account"), NULL, NULL, NULL },
126 	{"SysTrayiconPopup/OpenAB", NULL, N_("Open A_ddressbook"), NULL, NULL, G_CALLBACK(trayicon_addressbook_cb) },
127 	{"SysTrayiconPopup/Exit", NULL, N_("E_xit Claws Mail"), NULL, NULL, G_CALLBACK(trayicon_exit_cb) },
128 };
129 
130 static GtkToggleActionEntry trayicon_popup_toggle_menu_entries[] =
131 {
132 	{"SysTrayiconPopup/ToggleOffline", NULL, N_("_Work Offline"), NULL, NULL, G_CALLBACK(trayicon_toggle_offline_cb), FALSE },
133 #ifdef HAVE_LIBNOTIFY
134 	{"SysTrayiconPopup/ShowBubbles", NULL, N_("Show Trayicon Notifications"), NULL, NULL, G_CALLBACK(trayicon_toggle_notify_cb), FALSE },
135 #endif
136 };
137 
138 
notification_trayicon_msg(MsgInfo * msginfo)139 void notification_trayicon_msg(MsgInfo *msginfo)
140 {
141 #ifndef HAVE_LIBNOTIFY
142   return;
143 
144 #else
145   FolderType ftype;
146   NotificationFolderType nftype;
147   gchar *uistr;
148 
149   nftype = F_TYPE_MAIL;
150 
151   if(!msginfo || !notify_config.trayicon_enabled ||
152      !notify_config.trayicon_popup_enabled ||
153      !MSG_IS_NEW(msginfo->flags))
154     return;
155 
156   if(notify_config.trayicon_folder_specific) {
157     guint id;
158     GSList *list;
159     gchar *identifier;
160     gboolean found = FALSE;
161 
162     if(!(msginfo->folder))
163       return;
164 
165     identifier = folder_item_get_identifier(msginfo->folder);
166 
167     id =
168       notification_register_folder_specific_list
169       (TRAYICON_SPECIFIC_FOLDER_ID_STR);
170     list = notification_foldercheck_get_list(id);
171     for(; (list != NULL) && !found; list = g_slist_next(list)) {
172       gchar *list_identifier;
173       FolderItem *list_item = (FolderItem*) list->data;
174 
175       list_identifier = folder_item_get_identifier(list_item);
176       if(!g_strcmp0(list_identifier, identifier))
177 	found = TRUE;
178 
179       g_free(list_identifier);
180     }
181     g_free(identifier);
182 
183     if(!found)
184       return;
185   } /* folder specific */
186 
187   ftype = msginfo->folder->folder->klass->type;
188 
189   G_LOCK(trayicon_popup);
190   /* Check out which type to notify about */
191   switch(ftype) {
192   case F_MH:
193   case F_MBOX:
194   case F_MAILDIR:
195   case F_IMAP:
196     nftype = F_TYPE_MAIL;
197     break;
198   case F_NEWS:
199     nftype = F_TYPE_NEWS;
200     break;
201   case F_UNKNOWN:
202     if((uistr = msginfo->folder->folder->klass->uistr) == NULL) {
203       G_UNLOCK(trayicon_popup);
204       return;
205     }
206     else if(!strcmp(uistr, "vCalendar"))
207       nftype = F_TYPE_CALENDAR;
208     else if(!strcmp(uistr, "RSSyl"))
209       nftype = F_TYPE_RSS;
210     else {
211       debug_print("Notification Plugin: Unknown folder type %d\n",ftype);
212       G_UNLOCK(trayicon_popup);
213       return;
214     }
215     break;
216   default:
217     debug_print("Notification Plugin: Unknown folder type %d\n",ftype);
218     G_UNLOCK(trayicon_popup);
219     return;
220   }
221 
222 
223   notification_trayicon_popup_add_msg(msginfo, nftype);
224 
225   G_UNLOCK(trayicon_popup);
226 
227 #endif /* HAVE_LIBNOTIFY */
228 }
229 
notification_trayicon_destroy(void)230 void notification_trayicon_destroy(void)
231 {
232   if(trayicon) {
233     gtk_status_icon_set_visible(trayicon, FALSE);
234     g_object_unref(trayicon);
235     trayicon = NULL;
236   }
237 }
238 
notification_update_trayicon()239 void notification_update_trayicon()
240 {
241   gchar *buf;
242   static GdkPixbuf *old_icon = NULL;
243   GdkPixbuf *new_icon;
244   gint offset;
245   NotificationMsgCount count;
246   GSList *list;
247 
248   if(!notify_config.trayicon_enabled)
249     return;
250 
251   if(notify_config.trayicon_folder_specific) {
252     guint id;
253     id =
254       notification_register_folder_specific_list
255       (TRAYICON_SPECIFIC_FOLDER_ID_STR);
256     list = notification_foldercheck_get_list(id);
257   }
258   else
259     list = NULL;
260 
261   notification_core_get_msg_count(list, &count);
262 
263   if(!trayicon) {
264 
265 #ifdef NOTIFICATION_HOTKEYS
266     notification_hotkeys_update_bindings();
267 #endif
268 
269     old_icon = notification_trayicon_create();
270     if(!trayicon) {
271       debug_print("Notification plugin: Could not create trayicon\n");
272       return;
273     }
274   }
275 
276   /* Tooltip */
277   buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"),
278 			count.new_msgs, count.unread_msgs,
279 			count.total_msgs);
280   gtk_status_icon_set_tooltip_text(trayicon, buf);
281 
282   g_free(buf);
283 
284   /* Pixmap */
285   (prefs_common_get_prefs()->work_offline) ? (offset = 1) : (offset = 0);
286 
287   if((count.new_msgs > 0) && (count.unreadmarked_msgs > 0))
288     new_icon =
289       notification_pixbuf_get(NOTIFICATION_TRAYICON_NEWMARKEDMAIL+offset);
290   else if(count.new_msgs > 0)
291     new_icon =
292       notification_pixbuf_get(NOTIFICATION_TRAYICON_NEWMAIL+offset);
293   else if(count.unreadmarked_msgs > 0)
294     new_icon =
295       notification_pixbuf_get(NOTIFICATION_TRAYICON_UNREADMARKEDMAIL+offset);
296   else if(count.unread_msgs > 0)
297     new_icon =
298       notification_pixbuf_get(NOTIFICATION_TRAYICON_UNREADMAIL+offset);
299   else
300     new_icon =
301       notification_pixbuf_get(NOTIFICATION_TRAYICON_NOMAIL+offset);
302 
303   if(new_icon != old_icon) {
304     gtk_status_icon_set_from_pixbuf(trayicon, new_icon);
305     old_icon = new_icon;
306   }
307 }
308 
notification_trayicon_main_window_close(gpointer source,gpointer data)309 gboolean notification_trayicon_main_window_close(gpointer source, gpointer data)
310 {
311   if(!notify_config.trayicon_enabled)
312     return FALSE;
313 
314   if(source) {
315     gboolean *close_allowed = (gboolean*)source;
316 
317     if(notify_config.trayicon_close_to_tray) {
318       MainWindow *mainwin = mainwindow_get_mainwindow();
319 
320       *close_allowed = FALSE;
321       if(mainwin && gtk_widget_get_visible(GTK_WIDGET(mainwin->window))) {
322 	focused_widget = gtk_window_get_focus(GTK_WINDOW(mainwin->window));
323 	main_window_hide(mainwin);
324       }
325     }
326   }
327   return FALSE;
328 }
329 
notification_trayicon_main_window_got_iconified(gpointer source,gpointer data)330 gboolean notification_trayicon_main_window_got_iconified(gpointer source,
331 							 gpointer data)
332 {
333   MainWindow *mainwin = mainwindow_get_mainwindow();
334 
335   if(!notify_config.trayicon_enabled)
336     return FALSE;
337 
338   if(notify_config.trayicon_hide_when_iconified &&
339      mainwin && gtk_widget_get_visible(GTK_WIDGET(mainwin->window))
340      && !gtk_window_get_skip_taskbar_hint(GTK_WINDOW(mainwin->window))) {
341     gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
342   }
343   return FALSE;
344 }
345 
notification_trayicon_account_list_reset(const gchar * menuname,gpointer callback,gboolean receive)346 static void notification_trayicon_account_list_reset(const gchar *menuname,
347 													gpointer callback,
348 													gboolean receive)
349 {
350     GList *cur_ac;
351 	GtkWidget *menu, *submenu;
352     GtkWidget *menuitem;
353     PrefsAccount *ac_prefs;
354 
355     GList *account_list = account_get_list();
356 
357 	menu = gtk_ui_manager_get_widget(gtkut_ui_manager(), menuname);
358 	gtk_widget_show(menu);
359 
360 	gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), NULL);
361 	submenu = gtk_menu_new();
362 
363 	for(cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
364 		ac_prefs = (PrefsAccount *)cur_ac->data;
365 
366 		/* accounts list for receiving: skip SMTP-only accounts */
367 		if (receive && ac_prefs->protocol == A_NONE)
368 			continue;
369 
370 		menuitem = gtk_menu_item_new_with_label
371 						(ac_prefs->account_name ? ac_prefs->account_name
372 						: _("Untitled"));
373 		gtk_widget_show(menuitem);
374 		gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
375 		g_signal_connect(G_OBJECT(menuitem), "activate",
376 				G_CALLBACK(callback),
377 				ac_prefs);
378 	}
379 	gtk_widget_show(submenu);
380 	gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), submenu);
381 }
382 
notification_trayicon_account_list_changed(gpointer source,gpointer data)383 gboolean notification_trayicon_account_list_changed(gpointer source,
384 						    gpointer data)
385 {
386 	if (notify_config.trayicon_enabled) {
387 	  	notification_trayicon_account_list_reset("/Menus/SysTrayiconPopup/GetMailAcc",
388 												(gpointer)trayicon_get_from_account_cb, TRUE);
389 		notification_trayicon_account_list_reset("/Menus/SysTrayiconPopup/EmailAcc",
390 												(gpointer)trayicon_compose_acc_cb, FALSE);
391 	}
392 	return FALSE;
393 }
394 
notification_trayicon_create(void)395 static GdkPixbuf* notification_trayicon_create(void)
396 {
397   GdkPixbuf *trayicon_nomail;
398 	GtkActionGroup *action_group;
399 
400   trayicon_nomail = notification_pixbuf_get(NOTIFICATION_TRAYICON_NOMAIL);
401 
402   notification_trayicon_destroy();
403 
404   trayicon = gtk_status_icon_new_from_pixbuf(trayicon_nomail);
405 
406   g_signal_connect(G_OBJECT(trayicon), "activate",
407 		   G_CALLBACK(notification_trayicon_on_activate), NULL);
408   g_signal_connect(G_OBJECT(trayicon), "popup-menu",
409 		   G_CALLBACK(notification_trayicon_on_popup_menu), NULL);
410   g_signal_connect(G_OBJECT(trayicon), "size-changed",
411 		   G_CALLBACK(notification_trayicon_on_size_changed), NULL);
412 
413   /* Popup-Menu */
414 	action_group = cm_menu_create_action_group("SysTrayiconPopup", trayicon_popup_menu_entries,
415 																						 G_N_ELEMENTS(trayicon_popup_menu_entries), NULL);
416 	gtk_action_group_add_toggle_actions(action_group, trayicon_popup_toggle_menu_entries,
417 																			G_N_ELEMENTS(trayicon_popup_toggle_menu_entries), NULL);
418 
419 	MENUITEM_ADDUI("/Menus", "SysTrayiconPopup", "SysTrayiconPopup", GTK_UI_MANAGER_MENU)
420 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "GetMail", "SysTrayiconPopup/GetMail", GTK_UI_MANAGER_MENUITEM)
421 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "GetMailAcc", "SysTrayiconPopup/GetMailAcc", GTK_UI_MANAGER_MENU)
422 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "Separator1", "SysTrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
423 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "Email", "SysTrayiconPopup/Email", GTK_UI_MANAGER_MENUITEM)
424 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "EmailAcc", "SysTrayiconPopup/EmailAcc", GTK_UI_MANAGER_MENU)
425 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "Separator2", "SysTrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
426 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "OpenAB", "SysTrayiconPopup/OpenAB", GTK_UI_MANAGER_MENUITEM)
427 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "Separator3", "SysTrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
428 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "ToggleOffline", "SysTrayiconPopup/ToggleOffline", GTK_UI_MANAGER_MENUITEM)
429 #ifdef HAVE_LIBNOTIFY
430 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "ShowBubbles", "SysTrayiconPopup/ShowBubbles", GTK_UI_MANAGER_MENUITEM)
431 #endif
432 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "Separator4", "SysTrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
433 	MENUITEM_ADDUI("/Menus/SysTrayiconPopup", "Exit", "SysTrayiconPopup/Exit", GTK_UI_MANAGER_MENUITEM)
434 
435 	traymenu_popup = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
436 				gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/SysTrayiconPopup")));
437 
438 
439   return trayicon_nomail;
440 }
441 
notification_trayicon_on_activate(GtkStatusIcon * status_icon,gpointer user_data)442 void notification_trayicon_on_activate(GtkStatusIcon *status_icon, gpointer user_data)
443 {
444   MainWindow *mainwin = mainwindow_get_mainwindow();
445 
446   if(mainwin && gtk_widget_get_visible(GTK_WIDGET(mainwin->window)) == TRUE)
447     focused_widget = gtk_window_get_focus(GTK_WINDOW(mainwin->window));
448 
449   notification_toggle_hide_show_window();
450 
451   if(mainwin && gtk_widget_get_visible(GTK_WIDGET(mainwin->window)) == TRUE)
452     gtk_window_set_focus(GTK_WINDOW(mainwin->window), focused_widget);
453 }
454 
notification_trayicon_on_popup_menu(GtkStatusIcon * status_icon,guint button,guint activate_time,gpointer user_data)455 static void notification_trayicon_on_popup_menu(GtkStatusIcon *status_icon,
456 						guint button, guint activate_time,
457 						gpointer user_data)
458 {
459   MainWindow *mainwin = mainwindow_get_mainwindow();
460 
461   if(!mainwin)
462     return;
463 
464   /* tell callbacks to skip any event */
465   updating_menu = TRUE;
466   /* initialize checkitems according to current states */
467 	cm_toggle_menu_set_active("SysTrayiconPopup/ToggleOffline", prefs_common_get_prefs()->work_offline);
468 #ifdef HAVE_LIBNOTIFY
469 	cm_toggle_menu_set_active("SysTrayiconPopup/ShowBubbles", notify_config.trayicon_popup_enabled);
470 #endif
471 	cm_menu_set_sensitive("SysTrayiconPopup/GetMail", mainwin->lock_count == 0);
472 	cm_menu_set_sensitive("SysTrayiconPopup/GetMailAcc", mainwin->lock_count == 0);
473 	cm_menu_set_sensitive("SysTrayiconPopup/Exit", mainwin->lock_count == 0);
474 
475   updating_menu = FALSE;
476 
477   gtk_menu_popup(GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
478 		 button, activate_time);
479 }
480 
notification_trayicon_on_size_changed(GtkStatusIcon * icon,gint size,gpointer user_data)481 static gboolean notification_trayicon_on_size_changed(GtkStatusIcon *icon,
482 						      gint size,
483 						      gpointer user_data)
484 {
485   notification_update_msg_counts(NULL);
486   return FALSE;
487 }
488 
489 /* popup menu callbacks */
trayicon_get_all_cb(GtkAction * action,gpointer data)490 static void trayicon_get_all_cb(GtkAction *action, gpointer data)
491 {
492   MainWindow *mainwin = mainwindow_get_mainwindow();
493   inc_all_account_mail_cb(mainwin, 0, NULL);
494 }
495 
trayicon_get_from_account_cb(GtkAction * action,gpointer data)496 static void trayicon_get_from_account_cb(GtkAction *action, gpointer data)
497 {
498   MainWindow *mainwin = mainwindow_get_mainwindow();
499   PrefsAccount *account = (PrefsAccount *)data;
500   inc_account_mail(mainwin, account);
501 }
502 
trayicon_compose_cb(GtkAction * action,gpointer data)503 static void trayicon_compose_cb(GtkAction *action, gpointer data)
504 {
505   MainWindow *mainwin = mainwindow_get_mainwindow();
506   compose_mail_cb(mainwin, 0, NULL);
507 }
508 
trayicon_compose_acc_cb(GtkMenuItem * menuitem,gpointer data)509 static void trayicon_compose_acc_cb(GtkMenuItem *menuitem, gpointer data)
510 {
511   compose_new((PrefsAccount *)data, NULL, NULL);
512 }
513 
trayicon_addressbook_cb(GtkAction * action,gpointer data)514 static void trayicon_addressbook_cb(GtkAction *action, gpointer data)
515 {
516 #ifndef USE_ALT_ADDRBOOK
517     addressbook_open(NULL);
518 #else
519     GError* error = NULL;
520 
521     addressbook_dbus_open(FALSE, &error);
522     if (error) {
523 	g_warning("%s", error->message);
524 	g_error_free(error);
525     }
526 #endif
527 }
528 
trayicon_toggle_offline_cb(GtkAction * action,gpointer data)529 static void trayicon_toggle_offline_cb(GtkAction *action, gpointer data)
530 {
531   /* toggle offline mode if menu checkitem has been clicked */
532   if(!updating_menu) {
533     MainWindow *mainwin = mainwindow_get_mainwindow();
534     main_window_toggle_work_offline(mainwin, !prefs_common_get_prefs()->work_offline, TRUE);
535   }
536 }
537 
538 #ifdef HAVE_LIBNOTIFY
trayicon_toggle_notify_cb(GtkAction * action,gpointer data)539 static void trayicon_toggle_notify_cb(GtkAction *action, gpointer data)
540 {
541   if(!updating_menu) {
542     notify_config.trayicon_popup_enabled = !notify_config.trayicon_popup_enabled;
543   }
544 }
545 #endif
546 
app_exit_cb(MainWindow * mainwin,guint action,GtkWidget * widget)547 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
548 {
549   if(prefs_common_get_prefs()->confirm_on_exit) {
550     if(alertpanel(_("Exit"), _("Exit Claws Mail?"),
551 		  GTK_STOCK_CANCEL, GTK_STOCK_OK,
552 		  NULL, ALERTFOCUS_FIRST) != G_ALERTALTERNATE) {
553       return;
554     }
555     manage_window_focus_in(mainwin->window, NULL, NULL);
556   }
557 
558   if (prefs_common_get_prefs()->clean_on_exit) {
559     if (!main_window_empty_trash(mainwin, prefs_common_get_prefs()->ask_on_clean, TRUE))
560       return;
561   }
562 
563   app_will_exit(NULL, mainwin);
564 }
565 
trayicon_exit_cb(GtkAction * action,gpointer data)566 static void trayicon_exit_cb(GtkAction *action, gpointer data)
567 {
568   MainWindow *mainwin = mainwindow_get_mainwindow();
569 
570   if(mainwin->lock_count == 0) {
571     app_exit_cb(mainwin, 0, NULL);
572   }
573 }
574 
575 #ifdef HAVE_LIBNOTIFY
notification_trayicon_popup_add_msg(MsgInfo * msginfo,NotificationFolderType nftype)576 static gboolean notification_trayicon_popup_add_msg(MsgInfo *msginfo,
577 						    NotificationFolderType nftype)
578 {
579   gchar *summary;
580   gchar *utf8_str;
581   gboolean retval;
582   GdkPixbuf *pixbuf;
583 
584   g_return_val_if_fail(msginfo, FALSE);
585 
586   if(!popup.notification)
587     return notification_trayicon_popup_create(msginfo,nftype);
588 
589   /* Count messages */
590   notification_trayicon_popup_count_msgs(nftype);
591 
592   if(popup.msg_path) {
593     g_free(popup.msg_path);
594     popup.msg_path = NULL;
595   }
596 
597   summary  = notification_trayicon_popup_assemble_summary();
598   utf8_str = notification_trayicon_popup_assemble_body(msginfo);
599 
600   /* make sure we show a logo on many msg arrival */
601   pixbuf = notification_pixbuf_get(NOTIFICATION_CM_LOGO_64x64);
602   if(pixbuf)
603     notify_notification_set_icon_from_pixbuf(popup.notification, pixbuf);
604 
605   retval = notify_notification_update(popup.notification, summary,
606 				      utf8_str, NULL);
607   g_free(summary);
608   g_free(utf8_str);
609   if(!retval) {
610     debug_print("Notification Plugin: Failed to update notification.\n");
611     return FALSE;
612   }
613   /* Show the popup */
614   notify_notification_set_hint_string(popup.notification, "desktop-entry", "claws-mail");
615   if(!notify_notification_show(popup.notification, &(popup.error))) {
616     debug_print("Notification Plugin: Failed to send updated notification: "
617 		"%s\n",	popup.error->message);
618     g_clear_error(&(popup.error));
619     return FALSE;
620   }
621 
622   debug_print("Notification Plugin: Popup successfully modified "
623 	      "with libnotify.\n");
624 
625   return TRUE;
626 }
627 
notification_trayicon_popup_create(MsgInfo * msginfo,NotificationFolderType nftype)628 static gboolean notification_trayicon_popup_create(MsgInfo *msginfo,
629 						   NotificationFolderType nftype)
630 {
631   gchar *summary = NULL;
632   gchar *utf8_str = NULL;
633   GdkPixbuf *pixbuf;
634   GList *caps = NULL;
635   gboolean support_actions = FALSE;
636 
637   /* init libnotify if necessary */
638   if(!notify_is_initted()) {
639     if(!notify_init("claws-mail")) {
640       debug_print("Notification Plugin: Failed to initialize libnotify. "
641 		  "No popups will be shown.\n");
642       return FALSE;
643     }
644   }
645 
646   /* Count messages */
647   notification_trayicon_popup_count_msgs(nftype);
648 
649   summary  = notification_trayicon_popup_assemble_summary();
650   utf8_str = notification_trayicon_popup_assemble_body(msginfo);
651 
652 #if NOTIFY_CHECK_VERSION(0, 7, 0)
653   popup.notification = notify_notification_new(summary, utf8_str, NULL);
654 #else
655   popup.notification = notify_notification_new(summary, utf8_str, NULL, NULL);
656   notify_notification_attach_to_status_icon(popup.notification, trayicon);
657 #endif
658 
659   g_free(summary);
660   g_free(utf8_str);
661 
662   caps = notify_get_server_caps();
663     if(caps != NULL) {
664       GList *c;
665       for(c = caps; c != NULL; c = c->next) {
666 	if(strcmp((char*)c->data, "actions") == 0 ) {
667 	  support_actions = TRUE;
668 	  break;
669         }
670       }
671 
672     g_list_foreach(caps, (GFunc)g_free, NULL);
673     g_list_free(caps);
674   }
675 
676   /* Default action */
677   if (support_actions)
678     notify_notification_add_action(popup.notification,
679 				   "default", _("Present main window"),
680 				   (NotifyActionCallback)
681 				   notification_trayicon_popup_default_action_cb,
682 				   GINT_TO_POINTER(nftype),
683 				   notification_trayicon_popup_free_func);
684 
685   if(popup.notification == NULL) {
686     debug_print("Notification Plugin: Failed to create a new notification.\n");
687     return FALSE;
688   }
689 
690   /* Icon */
691   pixbuf = NULL;
692 #ifndef USE_ALT_ADDRBOOK
693   if(msginfo && msginfo->from) {
694     gchar *icon_path;
695     icon_path = addrindex_get_picture_file(msginfo->from);
696     if(is_file_exist(icon_path)) {
697       GError *error = NULL;
698       gint w, h;
699 
700       gdk_pixbuf_get_file_info(icon_path, &w, &h);
701       if((w > 64) || (h > 64))
702 	pixbuf = gdk_pixbuf_new_from_file_at_scale(icon_path,
703 						   64, 64, TRUE, &error);
704       else
705 	pixbuf = gdk_pixbuf_new_from_file(icon_path, &error);
706 
707       if(!pixbuf) {
708 	debug_print("Could not load picture file: %s\n",
709 		    error ? error->message : "no details");
710 	g_error_free(error);
711       }
712     }
713     else
714       debug_print("Picture path does not exist: %s\n",icon_path);
715     g_free(icon_path);
716   }
717 #endif
718   if(!pixbuf)
719     pixbuf = g_object_ref(notification_pixbuf_get(NOTIFICATION_CM_LOGO_64x64));
720 
721   if(pixbuf) {
722     notify_notification_set_icon_from_pixbuf(popup.notification, pixbuf);
723     g_object_unref(pixbuf);
724   }
725   else /* This is not fatal */
726     debug_print("Notification plugin: Icon could not be loaded.\n");
727 
728   /* timeout */
729   notify_notification_set_timeout(popup.notification, notify_config.trayicon_popup_timeout);
730 
731   /* Category */
732   notify_notification_set_category(popup.notification, "email.arrived");
733 
734   /* get notified on bubble close */
735   g_signal_connect(G_OBJECT(popup.notification), "closed", G_CALLBACK(popup_timeout_fun), NULL);
736 
737   /* Show the popup */
738   notify_notification_set_hint_string(popup.notification, "desktop-entry", "claws-mail");
739   if(!notify_notification_show(popup.notification, &(popup.error))) {
740     debug_print("Notification Plugin: Failed to send notification: %s\n",
741 		popup.error->message);
742     g_clear_error(&(popup.error));
743     g_object_unref(G_OBJECT(popup.notification));
744     popup.notification = NULL;
745     return FALSE;
746   }
747 
748   /* Store path to message */
749   if(nftype == F_TYPE_MAIL) {
750     if(msginfo && msginfo->folder) {
751       gchar *ident;
752       ident = folder_item_get_identifier(msginfo->folder);
753       popup.msg_path = g_strdup_printf("%s%s%u", ident,G_DIR_SEPARATOR_S,
754 				       msginfo->msgnum);
755       g_free(ident);
756     }
757     else
758       popup.msg_path = NULL;
759   }
760 
761   debug_print("Notification Plugin: Popup created with libnotify.\n");
762 
763   return TRUE;
764 }
765 
popup_timeout_fun(NotifyNotification * nn,gpointer data)766 static void popup_timeout_fun(NotifyNotification *nn, gpointer data)
767 {
768   G_LOCK(trayicon_popup);
769 
770   g_object_unref(G_OBJECT(popup.notification));
771 
772   popup.notification = NULL;
773   g_clear_error(&(popup.error));
774 
775   popup.count = 0;
776   popup.num_mail = 0;
777   popup.num_news = 0;
778   popup.num_calendar = 0;
779   popup.num_rss = 0;
780 
781   if(popup.msg_path) {
782     g_free(popup.msg_path);
783     popup.msg_path = NULL;
784   }
785 
786   G_UNLOCK(trayicon_popup);
787 }
788 
notification_trayicon_popup_free_func(gpointer data)789 static void notification_trayicon_popup_free_func(gpointer data)
790 {
791   if(popup.msg_path) {
792     g_free(popup.msg_path);
793     popup.msg_path = NULL;
794   }
795 
796   debug_print("Freed notification data\n");
797 }
798 
notification_trayicon_popup_default_action_cb(NotifyNotification * notification,const char * action,void * user_data)799 static void notification_trayicon_popup_default_action_cb(NotifyNotification
800 							  *notification,
801 							  const char *action,
802 							  void *user_data)
803 {
804   if(strcmp("default", action))
805     return;
806 
807   MainWindow *mainwin;
808   mainwin = mainwindow_get_mainwindow();
809   if(mainwin) {
810     NotificationFolderType nftype;
811 
812     /* Let mainwindow pop up */
813     notification_show_mainwindow(mainwin);
814     /* If there is only one new mail message, jump to this message */
815     nftype = (NotificationFolderType)GPOINTER_TO_INT(user_data);
816     if((popup.count == 1) && (nftype == F_TYPE_MAIL)) {
817       gchar *select_str;
818       G_LOCK(trayicon_popup);
819       select_str = g_strdup(popup.msg_path);
820       G_UNLOCK(trayicon_popup);
821       debug_print("Notification plugin: Select message %s\n", select_str);
822       mainwindow_jump_to(select_str, FALSE);
823       g_free(select_str);
824     }
825   }
826 }
827 
notification_trayicon_popup_count_msgs(NotificationFolderType nftype)828 static void notification_trayicon_popup_count_msgs(NotificationFolderType nftype)
829 {
830   switch(nftype) {
831   case F_TYPE_MAIL:
832     popup.num_mail++;
833     break;
834   case F_TYPE_NEWS:
835     popup.num_news++;
836     break;
837   case F_TYPE_CALENDAR:
838     popup.num_calendar++;
839     break;
840   case F_TYPE_RSS:
841     popup.num_rss++;
842     break;
843   default:
844     debug_print("Notification plugin: Unknown folder type\n");
845     return;
846   }
847   popup.count++;
848 }
849 
850 /* The returned value has to be freed by the caller */
notification_trayicon_popup_assemble_summary(void)851 static gchar* notification_trayicon_popup_assemble_summary(void)
852 {
853   gchar *summary = NULL;
854 
855   if(popup.count == 1) {
856     if(popup.num_mail)
857       summary = g_strdup(_("New mail message"));
858     else if(popup.num_news)
859       summary = g_strdup(_("New news post"));
860     else if(popup.num_calendar)
861       summary = g_strdup(_("New calendar message"));
862     else
863       summary = g_strdup(_("New article in RSS feed"));
864   } /* One new message */
865   else {
866     summary = g_strdup(_("New messages arrived"));
867   } /* Many new messages */
868 
869   return summary;
870 }
871 
872 /* The returned value has to be freed by the caller */
notification_trayicon_popup_assemble_body(MsgInfo * msginfo)873 static gchar* notification_trayicon_popup_assemble_body(MsgInfo *msginfo)
874 {
875   gchar *utf8_str;
876 
877   if(popup.count == 1) {
878     if(popup.num_mail || popup.num_news) {
879       gchar *from;
880       gchar *subj;
881       gchar *text;
882 	  gchar *foldname = NULL;
883 
884       from = notification_libnotify_sanitize_str(msginfo->from ?
885 						 msginfo->from :
886 						 _("(No From)"));
887       subj = notification_libnotify_sanitize_str(msginfo->subject ?
888 						 msginfo->subject :
889 						 _("(No Subject)"));
890   	if (notify_config.trayicon_display_folder_name) {
891         foldname = notification_libnotify_sanitize_str(msginfo->folder->path);
892         text = g_strconcat(from,"\n\n", subj, "\n\n", foldname, NULL);
893 	}
894     else
895         text = g_strconcat(from, "\n\n",subj, NULL);
896 
897 
898       /* Make sure text is valid UTF8 */
899       utf8_str = notification_validate_utf8_str(text);
900       g_free(text);
901 
902       if(from) g_free(from);
903       if(subj) g_free(subj);
904 	  if(foldname) g_free(foldname);
905     }
906     else if(popup.num_calendar) {
907       utf8_str = g_strdup(_("A new calendar message arrived"));
908     }
909     else {
910       utf8_str = g_strdup(_("A new article in a RSS feed arrived"));
911     }
912   } /* One message */
913 
914   else {
915     gchar *msg;
916     gchar *tmp;
917     gboolean str_empty = TRUE;
918 
919     utf8_str = g_strdup("");
920 
921     if(popup.num_mail) {
922       msg = g_strdup_printf(ngettext("%d new mail message arrived",
923 		      		     "%d new mail messages arrived",
924 		            popup.num_mail),
925 			    popup.num_mail);
926       tmp = g_strdup_printf("%s%s%s",utf8_str,"",msg);
927       g_free(msg);
928       g_free(utf8_str);
929       utf8_str = tmp;
930       str_empty = FALSE;
931     }
932     if(popup.num_news) {
933       msg = g_strdup_printf(ngettext("%d new news post arrived",
934 		      		     "%d new news posts arrived",
935 		            popup.num_news),
936 			    popup.num_news);
937       tmp = g_strdup_printf("%s%s%s",utf8_str,str_empty?"":"\n",msg);
938       g_free(msg);
939       g_free(utf8_str);
940       utf8_str = tmp;
941       str_empty = FALSE;
942     }
943     if(popup.num_calendar) {
944       msg = g_strdup_printf(ngettext("%d new calendar message arrived",
945 		      		     "%d new calendar messages arrived",
946 		            popup.num_calendar),
947 			    popup.num_calendar);
948       tmp = g_strdup_printf("%s%s%s",utf8_str,str_empty?"":"\n",msg);
949       g_free(msg);
950       g_free(utf8_str);
951       utf8_str = tmp;
952       str_empty = FALSE;
953     }
954     if(popup.num_rss) {
955       msg = g_strdup_printf(ngettext("%d new article in RSS feeds arrived",
956 		      		     "%d new articles in RSS feeds arrived",
957 		            popup.num_rss),
958 			    popup.num_rss);
959       tmp = g_strdup_printf("%s%s%s",utf8_str,str_empty?"":"\n",msg);
960       g_free(msg);
961       g_free(utf8_str);
962       utf8_str = tmp;
963       str_empty = FALSE;
964     }
965   } /* Many messages */
966 
967   return utf8_str;
968 }
969 
970 #endif /* HAVE_LIBNOTIFY */
971 
notification_trayicon_is_available(void)972 gboolean notification_trayicon_is_available(void)
973 {
974 	gboolean is_available;
975 	is_available = FALSE;
976 
977 	if(trayicon) {
978 		if(gtk_status_icon_is_embedded(trayicon) &&
979 			 gtk_status_icon_get_visible(trayicon))
980 			is_available = TRUE;
981 	}
982 
983 	return is_available;
984 }
985 
986 #endif /* NOTIFICATION_TRAYICON */
987