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