083ff5402b0f2da042357e3d056a3288b466dd5d
[claws.git] / src / plugins / notification / notification_indicator.c
1 /* Notification plugin for Claws-Mail
2  * Copyright (C) 2005-2009 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 #ifdef HAVE_CONFIG_H
19 #  include "config.h"
20 #  include "claws-features.h"
21 #endif
22
23 #ifdef NOTIFICATION_INDICATOR
24
25 #include "notification_indicator.h"
26 #include "notification_prefs.h"
27 #include "notification_core.h"
28
29 #include "folder.h"
30 #include "common/utils.h"
31
32 #include <messaging-menu.h>
33 #include <unity.h>
34
35 #define CLAWS_DESKTOP_FILE "claws-mail.desktop"
36
37 static MessagingMenuApp *mmapp = NULL;
38 static gboolean mmapp_registered = FALSE;
39 static UnityLauncherEntry *launcher = NULL;
40 static gulong mainwin_state_changed_signal_id = 0;
41
42 static void show_claws_mail(MessagingMenuApp *mmapp, const gchar *id, gpointer data);
43
44 void notification_indicator_setup(void)
45 {
46   if(!mmapp) {
47     mmapp = messaging_menu_app_new(CLAWS_DESKTOP_FILE);
48   }
49   if(notify_config.indicator_enabled && !mmapp_registered) {
50     messaging_menu_app_register(MESSAGING_MENU_APP(mmapp));
51     g_signal_connect(mmapp, "activate-source", G_CALLBACK(show_claws_mail), NULL);
52     mmapp_registered = TRUE;
53   }
54   if(!launcher) {
55     launcher = unity_launcher_entry_get_for_desktop_id(CLAWS_DESKTOP_FILE);
56   }
57 }
58
59 void notification_indicator_destroy(void)
60 {
61   if(!launcher) {
62     unity_launcher_entry_set_count_visible(launcher, FALSE);
63   }
64   if(mmapp_registered) {
65     messaging_menu_app_unregister(mmapp);
66     mmapp_registered = FALSE;
67   }
68   if(mainwin_state_changed_signal_id != 0) {
69     MainWindow *mainwin;
70     if((mainwin = mainwindow_get_mainwindow()) != NULL)
71       g_signal_handler_disconnect(mainwin->window, mainwin_state_changed_signal_id);
72     mainwin_state_changed_signal_id = 0;
73   }
74 }
75
76 static void show_claws_mail(MessagingMenuApp *mmapp, const gchar *id, gpointer data)
77 {
78   MainWindow *mainwin;
79
80   if((mainwin = mainwindow_get_mainwindow()) == NULL)
81     return;
82
83   notification_show_mainwindow(mainwin);
84   if(data) {
85     Folder *folder = data;
86     FolderItem *item = folder->inbox;
87
88     gchar *path = folder_item_get_identifier(item);
89     mainwindow_jump_to(path, FALSE);
90     g_free(path);
91   }
92 }
93
94 static gboolean mainwin_state_event(GtkWidget *widget, GdkEventWindowState *event, gpointer user_data)
95 {
96   if(notify_config.indicator_hide_minimized) {
97     MainWindow *mainwin;
98
99     if((mainwin = mainwindow_get_mainwindow()) == NULL)
100       return FALSE;
101
102     if((event->changed_mask & GDK_WINDOW_STATE_ICONIFIED) && (event->new_window_state & GDK_WINDOW_STATE_ICONIFIED)) {
103       gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
104     }
105     else if((event->changed_mask & GDK_WINDOW_STATE_ICONIFIED) && !(event->new_window_state & GDK_WINDOW_STATE_ICONIFIED)) {
106       gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
107     }
108   }
109   return FALSE;
110 }
111
112 void notification_update_indicator(void)
113 {
114   GList *cur_mb;
115   gint total_message_count;
116
117   if(!mainwin_state_changed_signal_id) {
118     MainWindow *mainwin;
119
120     if((mainwin = mainwindow_get_mainwindow()) != NULL)
121       mainwin_state_changed_signal_id = g_signal_connect(G_OBJECT(mainwin->window), "window-state-event", G_CALLBACK(mainwin_state_event), NULL);
122   }
123
124   if(!notify_config.indicator_enabled)
125     return;
126
127   total_message_count = 0;
128   /* check accounts for new/unread counts */
129   for(cur_mb = folder_get_list(); cur_mb; cur_mb = cur_mb->next) {
130     Folder *folder = cur_mb->data;
131     NotificationMsgCount count;
132
133     if(!folder->name) {
134       debug_print("Notification plugin: Warning: Ignoring unnamed mailbox in indicator applet\n");
135       continue;
136     }
137     gchar *id = folder->name;
138     notification_core_get_msg_count_of_foldername(folder->name, &count);
139
140     total_message_count += count.unread_msgs;
141
142     if(count.new_msgs > 0) {
143       gchar *strcount = g_strdup_printf("%d / %d", count.new_msgs, count.unread_msgs);
144
145       if(messaging_menu_app_has_source(MESSAGING_MENU_APP(mmapp), id))
146         messaging_menu_app_set_source_string(MESSAGING_MENU_APP(mmapp), id, strcount);
147       else
148         messaging_menu_app_append_source_with_string(MESSAGING_MENU_APP(mmapp), id, NULL, id, strcount);
149
150       g_free(strcount);
151       messaging_menu_app_draw_attention(MESSAGING_MENU_APP(mmapp), id);
152     }
153     else {
154       if(messaging_menu_app_has_source(MESSAGING_MENU_APP(mmapp), id)) {
155         messaging_menu_app_remove_attention(MESSAGING_MENU_APP(mmapp), id);
156         messaging_menu_app_remove_source(MESSAGING_MENU_APP(mmapp), id);
157       }
158     }
159   }
160
161   unity_launcher_entry_set_count(launcher, total_message_count);
162   unity_launcher_entry_set_count_visible(launcher, total_message_count > 0);
163 }
164
165 #endif /* NOTIFICATION_INDICATOR */