2007-07-31 Philip Van Hoof <pvanhoof@gnome.org>
[modest] / src / modest-ui-dimming-rules.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29  
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif /*HAVE_CONFIG_H*/
33
34 #include <string.h>
35 #include "modest-ui-dimming-rules.h"
36 #include "modest-dimming-rule.h"
37 #include "modest-tny-folder.h"
38 #include "modest-tny-account.h"
39 #include "modest-text-utils.h"
40 #include <widgets/modest-attachments-view.h>
41 #include <modest-runtime.h>
42 #include <tny-simple-list.h>
43
44
45 static gboolean _folder_is_any_of_type (TnyFolder *folder, TnyFolderType types[], guint ntypes);
46 static gboolean _invalid_msg_selected (ModestMainWindow *win, gboolean unique, ModestDimmingRule *rule);
47 static gboolean _invalid_attach_selected (ModestWindow *win, gboolean unique, gboolean for_view, ModestDimmingRule *rule);
48 static gboolean _purged_attach_selected (ModestWindow *win, gboolean all, ModestDimmingRule *rule);
49 static gboolean _clipboard_is_empty (ModestWindow *win);
50 static gboolean _invalid_clipboard_selected (ModestWindow *win, ModestDimmingRule *rule);
51 static gboolean _already_opened_msg (ModestWindow *win, guint *n_messages);
52 static gboolean _selected_msg_marked_as (ModestWindow *win, TnyHeaderFlags mask, gboolean opposite);
53 static gboolean _selected_folder_not_writeable (ModestMainWindow *win);
54 static gboolean _selected_folder_is_snd_level (ModestMainWindow *win);
55 static gboolean _selected_folder_is_any_of_type (ModestWindow *win, TnyFolderType types[], guint ntypes);
56 static gboolean _selected_folder_is_root_or_inbox (ModestMainWindow *win);
57 static gboolean _selected_folder_is_MMC_or_POP_root (ModestMainWindow *win);
58 static gboolean _selected_folder_is_root (ModestMainWindow *win);
59 static gboolean _selected_folder_is_empty (ModestMainWindow *win);
60 static gboolean _selected_folder_is_same_as_source (ModestWindow *win);
61 static gboolean _msg_download_in_progress (ModestMsgViewWindow *win);
62 static gboolean _msg_download_completed (ModestMainWindow *win);
63 static gboolean _selected_msg_sent_in_progress (ModestWindow *win);
64 static gboolean _sending_in_progress (ModestWindow *win);
65 static gboolean _marked_as_deleted (ModestWindow *win);
66 static gboolean _invalid_folder_for_purge (ModestWindow *win, ModestDimmingRule *rule);
67 static gboolean _transfer_mode_enabled (ModestWindow *win);
68
69
70 gboolean 
71 modest_ui_dimming_rules_on_new_msg (ModestWindow *win, gpointer user_data)
72 {
73         ModestDimmingRule *rule = NULL;
74         gboolean dimmed = FALSE;
75
76         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
77         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
78         rule = MODEST_DIMMING_RULE (user_data);
79                 
80         /* Check dimmed rule */ 
81         if (!dimmed) {
82                 if (MODEST_IS_MSG_VIEW_WINDOW (win))
83                         dimmed = _msg_download_in_progress (MODEST_MSG_VIEW_WINDOW(win));
84                 if (dimmed)
85                         modest_dimming_rule_set_notification (rule, "");
86         }
87
88         return dimmed;
89 }
90
91 gboolean 
92 modest_ui_dimming_rules_on_csm_new_folder (ModestWindow *win, gpointer user_data)
93 {
94         ModestDimmingRule *rule = NULL;
95         gboolean dimmed = FALSE;
96
97         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
98         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
99         rule = MODEST_DIMMING_RULE (user_data);
100
101         /* Check common new folder menu item dimming rules */   
102         dimmed = modest_ui_dimming_rules_on_new_folder (win, user_data);
103         
104         /* Check CSM specific dimming rules */
105         if (!dimmed) {
106                 dimmed = _selected_folder_is_snd_level (MODEST_MAIN_WINDOW(win));
107                 if (dimmed)
108                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_create_error"));
109         }
110         
111         return dimmed;
112 }
113
114 gboolean 
115 modest_ui_dimming_rules_on_new_folder (ModestWindow *win, gpointer user_data)
116 {
117         ModestDimmingRule *rule = NULL;
118         GtkWidget *folder_view = NULL;
119         TnyFolderStore *parent_folder = NULL;
120         gboolean dimmed = FALSE;
121
122         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
123         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
124         rule = MODEST_DIMMING_RULE (user_data);
125
126         /* Get selected folder as parent of new folder to create */
127         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
128                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
129         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
130         if (!parent_folder)
131                 return TRUE;
132         
133         if (TNY_IS_ACCOUNT (parent_folder)) {
134                 /* If it's the local account then do not dim */
135                 if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
136                         dimmed = FALSE;
137                 } else {
138                         const gchar *proto_str = tny_account_get_proto (TNY_ACCOUNT (parent_folder));
139                         /* If it's POP then dim */
140                         dimmed = (modest_protocol_info_get_transport_store_protocol (proto_str) == 
141                                   MODEST_PROTOCOL_STORE_POP) ? TRUE : FALSE;
142                         if (dimmed)
143                                 modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_create_error"));
144                 }
145         } else {
146                 /* TODO: the specs say that only one level of subfolder is allowed, is this true ? */
147                 
148                 TnyFolderType types[3];
149                                 
150                 types[0] = TNY_FOLDER_TYPE_DRAFTS; 
151                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
152                 types[2] = TNY_FOLDER_TYPE_SENT;
153
154                 /* Apply folder rules */        
155                 if (!dimmed) {
156                         dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
157                         if (dimmed)
158                                 modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_create_error"));
159                 }
160                 if (!dimmed) {
161                         dimmed = _selected_folder_is_any_of_type (win, types, 3);
162                         if (dimmed)
163                                 modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_create_error"));
164                 }
165         }
166         g_object_unref (parent_folder);
167
168         return dimmed;
169 }
170
171 gboolean 
172 modest_ui_dimming_rules_on_delete (ModestWindow *win, gpointer user_data)
173 {
174         ModestDimmingRule *rule = NULL;
175         GtkWidget *folder_view = NULL;
176         GtkWidget *header_view = NULL;  
177         gboolean dimmed = FALSE;
178
179         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
180         rule = MODEST_DIMMING_RULE (user_data);
181         
182         if (MODEST_IS_MAIN_WINDOW (win)) {
183                 /* Get the folder view */
184                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
185                                                                    MODEST_WIDGET_TYPE_FOLDER_VIEW);
186                 
187                 /* Get header view */
188                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
189                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
190
191                 if (header_view && gtk_widget_is_focus (header_view)) 
192                         dimmed = modest_ui_dimming_rules_on_delete_msg (win, rule);
193
194                 if (folder_view && gtk_widget_is_focus (folder_view)) 
195                         dimmed = modest_ui_dimming_rules_on_delete_folder (win, rule);
196
197                 if (header_view && folder_view &&
198                     !gtk_widget_is_focus (header_view) &&
199                     !gtk_widget_is_focus (folder_view)) {
200                         dimmed = TRUE;
201                         modest_dimming_rule_set_notification (rule, dgettext("hildon-common-strings", "ckct_ib_nothing_to_delete"));                    
202                 }
203         } else {
204                 dimmed = modest_ui_dimming_rules_on_delete_folder (win, rule);
205         }
206
207         return dimmed;
208 }
209
210
211
212 gboolean 
213 modest_ui_dimming_rules_on_delete_folder (ModestWindow *win, gpointer user_data)
214 {
215         ModestDimmingRule *rule = NULL;
216         TnyFolderType types[6];
217         gboolean dimmed = FALSE;
218
219         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
220         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
221         rule = MODEST_DIMMING_RULE (user_data);
222
223         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
224         types[1] = TNY_FOLDER_TYPE_OUTBOX;
225         types[2] = TNY_FOLDER_TYPE_SENT;
226         types[3] = TNY_FOLDER_TYPE_INBOX;
227         types[4] = TNY_FOLDER_TYPE_ROOT;
228         types[5] = TNY_FOLDER_TYPE_ARCHIVE;
229
230                 
231         /* Check dimmed rule */ 
232         if (!dimmed) {
233                 dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
234                 if (dimmed)
235                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_delete_error"));
236         }
237         if (!dimmed) {
238                 dimmed = _selected_folder_is_any_of_type (win, types, 6);
239                 if (dimmed)
240                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_delete_error"));
241         }
242         if (!dimmed) {
243                 dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
244                 if (dimmed)
245                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_delete_error"));
246         }
247         if (!dimmed) {
248                 dimmed = _selected_folder_is_MMC_or_POP_root (MODEST_MAIN_WINDOW(win));
249                 if (dimmed)
250                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_delete_error"));
251         }
252
253         return dimmed;
254 }
255
256 gboolean
257 modest_ui_dimming_rules_on_sort (ModestWindow *win, gpointer user_data)
258 {
259         ModestDimmingRule *rule = NULL;
260         gboolean dimmed = FALSE;
261
262         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
263         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
264         rule = MODEST_DIMMING_RULE (user_data);
265                 
266         /* Check dimmed rule */ 
267         if (!dimmed) {
268                 dimmed = _selected_folder_is_root (MODEST_MAIN_WINDOW(win));
269                 modest_dimming_rule_set_notification (rule, _CS("ckdg_ib_nothing_to_sort"));
270         }
271
272         if (!dimmed) {
273                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));
274                 modest_dimming_rule_set_notification (rule, _CS("ckdg_ib_nothing_to_sort"));
275         }
276
277         return dimmed;
278         
279 }
280
281 gboolean 
282 modest_ui_dimming_rules_on_rename_folder (ModestWindow *win, gpointer user_data)
283 {
284         ModestDimmingRule *rule = NULL;
285         TnyFolderType types[4];
286         gboolean dimmed = FALSE;
287
288         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
289         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
290         rule = MODEST_DIMMING_RULE (user_data);
291
292         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
293         types[1] = TNY_FOLDER_TYPE_OUTBOX;
294         types[2] = TNY_FOLDER_TYPE_SENT;
295         types[3] = TNY_FOLDER_TYPE_ARCHIVE;
296         
297         /* Check dimmed rule */ 
298         if (!dimmed) {
299                 dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
300                 if (dimmed)
301                         modest_dimming_rule_set_notification (rule, "");
302         }
303         if (!dimmed) {
304                 dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
305                 if (dimmed)
306                         modest_dimming_rule_set_notification (rule, "");
307         }
308         if (!dimmed) {
309                 dimmed = _selected_folder_is_any_of_type (win, types, 4);
310                 if (dimmed)
311                         modest_dimming_rule_set_notification (rule, "");
312         }
313
314         return dimmed;
315 }
316
317 gboolean 
318 modest_ui_dimming_rules_on_open_msg (ModestWindow *win, gpointer user_data)
319 {
320         ModestDimmingRule *rule = NULL;
321         gboolean dimmed = FALSE;
322
323         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
324         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
325         rule = MODEST_DIMMING_RULE (user_data);
326                 
327         /* Check dimmed rule */ 
328         if (!dimmed) {
329                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, user_data);
330         }
331         if (!dimmed) {
332                 dimmed = _marked_as_deleted (win);
333                 if (dimmed)
334                         modest_dimming_rule_set_notification (rule, _("mcen_ib_message_already_deleted"));
335         }
336         if (!dimmed) {
337                 dimmed = _selected_msg_sent_in_progress (win);
338                 if (dimmed)
339                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_open_while_sent"));
340         }
341
342         return dimmed;
343 }
344
345 gboolean 
346 modest_ui_dimming_rules_on_reply_msg (ModestWindow *win, gpointer user_data)
347 {
348         ModestDimmingRule *rule = NULL;
349         gboolean dimmed = FALSE;
350         TnyFolderType types[3];
351
352         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
353         rule = MODEST_DIMMING_RULE (user_data);
354
355         /* main window dimming rules */
356         if (MODEST_IS_MAIN_WINDOW(win)) {
357                 
358                 types[0] = TNY_FOLDER_TYPE_DRAFTS; 
359                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
360                 types[2] = TNY_FOLDER_TYPE_ROOT;
361                 
362                 /* Check dimmed rule */ 
363                 if (!dimmed) {
364                         dimmed = _selected_folder_is_any_of_type (win, types, 3);                       
365                         if (dimmed)
366                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_reply"));
367                 }
368                 if (!dimmed) {
369                         dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
370                         if (dimmed)
371                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_nothing_to_reply"));
372                 }
373                 if (!dimmed) {
374                         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, rule);
375                 }
376         /* msg view window dimming rules */
377         } else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
378                 
379                 /* Check dimmed rule */ 
380                 if (!dimmed) {
381                         dimmed = _transfer_mode_enabled (win);                  
382                         if (dimmed)
383                                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
384                 }
385                 if (!dimmed) {
386                         if (MODEST_IS_MSG_VIEW_WINDOW (win))
387                                 dimmed = _msg_download_in_progress (MODEST_MSG_VIEW_WINDOW (win));
388                         if (dimmed)
389                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_reply"));
390                 }
391         }
392         
393         return dimmed;
394 }
395
396
397 gboolean 
398 modest_ui_dimming_rules_on_contents_msg (ModestWindow *win, gpointer user_data)
399 {
400         ModestDimmingRule *rule = NULL;
401         gboolean dimmed = FALSE;
402
403         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
404         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
405         rule = MODEST_DIMMING_RULE (user_data);
406                 
407         /* Check dimmed rule */ 
408         if (!dimmed) {
409                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
410         }
411         if (!dimmed) {
412                 dimmed = _msg_download_completed (MODEST_MAIN_WINDOW(win));
413                 if (dimmed)
414                         modest_dimming_rule_set_notification (rule, "");
415         }
416
417         return dimmed;
418 }
419
420 gboolean 
421 modest_ui_dimming_rules_always_dimmed (ModestWindow *win, gpointer user_data)
422 {
423         gboolean dimmed = FALSE;
424
425         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
426                 
427         /* Check dimmed rule */ 
428         if (!dimmed)
429                 dimmed = TRUE;
430
431         return dimmed;
432 }
433
434 gboolean 
435 modest_ui_dimming_rules_on_delete_msg (ModestWindow *win, gpointer user_data)
436 {
437         ModestDimmingRule *rule = NULL;
438         guint n_messages = 0;
439         gboolean dimmed = FALSE;
440
441         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
442         rule = MODEST_DIMMING_RULE (user_data);
443         
444         /* Check dimmed rule */         
445         if (MODEST_IS_MAIN_WINDOW (win)) {
446                 if (!dimmed) {
447                         dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
448                         if (dimmed)
449                                 modest_dimming_rule_set_notification (rule, _CS("ckct_ib_nothing_to_delete"));
450                 }
451                 if (!dimmed) {
452                         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
453                 }
454                 if (!dimmed) {
455                         dimmed = _already_opened_msg (win, &n_messages);
456                         if (dimmed) {
457                                 gchar *num = g_strdup_printf ("%d", n_messages);
458                                 gchar *message = g_strdup_printf(_("mcen_nc_unable_to_delete_n_messages"), num);
459 /*                              modest_dimming_rule_set_notification (rule, _("mcen_nc_unable_to_delete_n_messages")); */
460                                 modest_dimming_rule_set_notification (rule, message);
461                                 g_free(message);
462                                 g_free(num);
463                         }
464                 }
465                 if (!dimmed) {
466                         dimmed = _marked_as_deleted (win);
467                         if (dimmed)
468                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_message_already_deleted"));
469                 }
470                 if (!dimmed) {
471                         dimmed = _selected_msg_sent_in_progress (win);
472                         if (dimmed)
473                                 modest_dimming_rule_set_notification (rule, _CS("ckct_ib_unable_to_delete"));
474                 }
475         } 
476         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
477                 if (!dimmed) {
478                         dimmed = _transfer_mode_enabled (win);                  
479                         if (dimmed)
480                                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
481                 }
482                 if (!dimmed) {
483                         dimmed = _marked_as_deleted (win);
484                         if (dimmed)
485                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_message_already_deleted"));
486                 }
487                 if (!dimmed) {
488                         dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW(win));
489                         if (dimmed) {
490                                 modest_dimming_rule_set_notification (rule, _CS("ckct_ib_unable_to_delete"));
491                         }
492                 }
493         }
494
495         return dimmed;
496 }
497
498 gboolean 
499 modest_ui_dimming_rules_on_details (ModestWindow *win, gpointer user_data)
500 {
501         ModestDimmingRule *rule = NULL;
502         GtkWidget *header_view = NULL;
503         gboolean dimmed = FALSE;
504
505         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
506         rule = MODEST_DIMMING_RULE (user_data);
507         
508         /* main window dimming rules */
509         if (MODEST_IS_MAIN_WINDOW(win)) {
510                                 
511                 /* Check dimmed rule */
512                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
513                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
514                 
515                 /* If the header view has the focus: */
516                 if (header_view && gtk_widget_is_focus (header_view)) {
517                         /* Check dimmed rule */ 
518                         if (!dimmed)
519                                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, user_data);
520                 }
521                 else {
522                         /* If the folder view has the focus: */
523                         GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
524                                 MODEST_WIDGET_TYPE_FOLDER_VIEW);
525                         if (folder_view && gtk_widget_is_focus (folder_view)) {
526                                 TnyFolderStore *folder_store
527                                         = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
528                                 if (folder_store) {
529                                         /* Only enable for folders, not accounts,
530                                          * though the UI spec is not clear about that.
531                                          * If we enable this for accounts then we must 
532                                          * add code to handle them in modest_ui_actions_on_details(). */
533                                         if (!TNY_IS_FOLDER(folder_store)) {
534                                                 dimmed = TRUE;
535                                                 modest_dimming_rule_set_notification (rule, "");
536                                         }
537
538                                         g_object_unref (folder_store);
539                                 }
540                         }
541
542                 }
543
544         /* msg view window dimming rules */
545         } else {
546
547                 /* Check dimmed rule */ 
548                 if (!dimmed) {
549                         if (MODEST_IS_MSG_VIEW_WINDOW (win))
550                                 dimmed = _msg_download_in_progress (MODEST_MSG_VIEW_WINDOW (win));
551                         if (dimmed)
552                                 modest_dimming_rule_set_notification (rule, "");
553                 }
554         }
555
556         return dimmed;
557 }
558
559
560 gboolean 
561 modest_ui_dimming_rules_on_mark_as_read_msg (ModestWindow *win, gpointer user_data)
562 {
563         ModestDimmingRule *rule = NULL;
564         TnyHeaderFlags flags;
565         gboolean dimmed = FALSE;
566
567         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
568         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
569         rule = MODEST_DIMMING_RULE (user_data);
570         
571         flags = TNY_HEADER_FLAG_SEEN; 
572
573         /* Check dimmed rule */ 
574         if (!dimmed) {
575                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
576         }
577         if (!dimmed) {
578                 dimmed = _selected_msg_marked_as (win, flags, FALSE);
579                 if (dimmed)
580                         modest_dimming_rule_set_notification (rule, "");
581         }       
582
583         return dimmed;
584 }
585
586 gboolean 
587 modest_ui_dimming_rules_on_mark_as_unread_msg (ModestWindow *win, gpointer user_data)
588 {
589         ModestDimmingRule *rule = NULL;
590         TnyHeaderFlags flags;
591         gboolean dimmed = FALSE;
592
593         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
594         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
595         rule = MODEST_DIMMING_RULE (user_data);
596         
597         flags = TNY_HEADER_FLAG_SEEN; 
598
599         /* Check dimmed rule */ 
600         if (!dimmed)
601                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
602         if (!dimmed) {
603                 dimmed = _selected_msg_marked_as (win, flags, TRUE);
604                 if (dimmed)
605                         modest_dimming_rule_set_notification (rule, "");
606         }
607
608         return dimmed;
609 }
610
611 gboolean 
612 modest_ui_dimming_rules_on_move_to (ModestWindow *win, gpointer user_data)
613 {
614         ModestDimmingRule *rule = NULL;
615         gboolean dimmed = FALSE;
616
617         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
618         rule = MODEST_DIMMING_RULE (user_data);
619
620         if (MODEST_IS_MAIN_WINDOW (win)) 
621                 dimmed = modest_ui_dimming_rules_on_main_window_move_to (win, user_data);
622         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) 
623                  dimmed = modest_ui_dimming_rules_on_view_window_move_to (win, user_data);
624
625         return dimmed;
626 }
627
628
629 gboolean 
630 modest_ui_dimming_rules_on_main_window_move_to (ModestWindow *win, gpointer user_data)
631 {
632         GtkWidget *folder_view = NULL;
633         GtkWidget *header_view = NULL;
634         ModestDimmingRule *rule = NULL;
635         guint n_messages = 0;
636         gboolean dimmed = FALSE;
637         
638         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), TRUE);
639         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
640         rule = MODEST_DIMMING_RULE (user_data);
641         
642         /* Get the folder view */
643         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
644                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
645
646         /* Get header view */
647         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
648                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
649         
650         /* Check diming rules for folders and messages transfer  */
651         if (!dimmed) {
652                 dimmed = _already_opened_msg (win, &n_messages);
653                 if (dimmed) {
654                         gchar *message = g_strdup_printf(_("emev_bd_unabletomove_items"), n_messages);
655                         modest_dimming_rule_set_notification (rule, message);
656                         g_free(message);
657                 }
658         }
659         if (!dimmed) {
660                 if (!gtk_widget_is_focus (folder_view))
661                         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
662                 
663         }
664         
665         return dimmed;
666 }
667
668 gboolean 
669 modest_ui_dimming_rules_on_view_window_move_to (ModestWindow *win, gpointer user_data)
670 {
671         ModestDimmingRule *rule = NULL;
672         gboolean dimmed = FALSE;
673
674         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
675         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
676         rule = MODEST_DIMMING_RULE (user_data);
677
678         /* Check dimmed rule */ 
679         if (!dimmed) {
680                 dimmed = _transfer_mode_enabled (win);                  
681                 if (dimmed)
682                         modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
683         }
684         if (!dimmed) {
685                 if (MODEST_IS_MSG_VIEW_WINDOW (win))
686                         dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW (win));
687                 if (dimmed) 
688                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_move_mail_attachment"));
689         }
690
691         return dimmed;
692 }
693
694 gboolean 
695 modest_ui_dimming_rules_on_find_msg (ModestWindow *win, gpointer user_data)
696 {
697         ModestDimmingRule *rule = NULL;
698         gboolean dimmed = FALSE;
699
700         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
701         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
702         rule = MODEST_DIMMING_RULE (user_data);
703
704         /* Check dimmed rule */ 
705         if (!dimmed) {
706                 dimmed = _transfer_mode_enabled (win);                  
707                 if (dimmed)
708                         modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
709         }
710
711         return dimmed;
712 }
713
714 gboolean 
715 modest_ui_dimming_rules_on_paste_msgs (ModestWindow *win, gpointer user_data)
716 {
717         ModestDimmingRule *rule = NULL;
718         TnyFolderType types[3];
719         gboolean dimmed = FALSE;
720         
721         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
722         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
723         rule = MODEST_DIMMING_RULE (user_data);
724
725         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
726         types[1] = TNY_FOLDER_TYPE_OUTBOX;
727         types[2] = TNY_FOLDER_TYPE_SENT;
728         
729         /* Check dimmed rule */ 
730         if (!dimmed) {
731                 dimmed = _clipboard_is_empty (win);
732                 if (dimmed)
733                         modest_dimming_rule_set_notification (rule, dgettext("hildon-common-strings", "ecoc_ib_edwin_nothing_to_paste"));
734         }
735         if (!dimmed) {
736                 dimmed = _selected_folder_is_any_of_type (win, types, 3);
737                 if (dimmed)
738                         modest_dimming_rule_set_notification (rule, dgettext("hildon-common-strings", "ckct_ib_unable_to_paste_here"));
739         }
740         if (!dimmed) {
741                 dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
742                 if (dimmed) {
743                         modest_dimming_rule_set_notification (rule, dgettext("hildon-common-strings", "ckct_ib_unable_to_paste_here"));
744                 }
745         }
746         if (!dimmed) {
747                 dimmed = _selected_folder_is_same_as_source (win);
748                 if (dimmed)
749                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_copy_samefolder"));
750         }
751
752         return dimmed;
753 }
754
755
756 gboolean 
757 modest_ui_dimming_rules_on_select_all (ModestWindow *win, gpointer user_data)
758 {
759         ModestDimmingRule *rule = NULL;
760         gboolean dimmed = FALSE;
761
762         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
763         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
764         rule = MODEST_DIMMING_RULE (user_data);
765
766         /* Check dimmed rule */ 
767         if (!dimmed) {
768                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
769                 if (dimmed)
770                         modest_dimming_rule_set_notification (rule, "");
771         }
772                 
773         return dimmed;
774 }
775
776 gboolean 
777 modest_ui_dimming_rules_on_view_attachments (ModestWindow *win, gpointer user_data)
778 {
779         ModestDimmingRule *rule = NULL;
780         gboolean dimmed = FALSE;
781
782         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
783         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
784         rule = MODEST_DIMMING_RULE (user_data);
785
786         /* Check dimmed rule */ 
787         if (!dimmed) 
788                 dimmed = _invalid_attach_selected (win, TRUE, TRUE, rule);                      
789
790         if (!dimmed) {
791                 dimmed = _purged_attach_selected (win, FALSE, NULL);
792                 if (dimmed) {
793                         modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
794                 }
795         }
796                 
797         return dimmed;
798 }
799
800 gboolean 
801 modest_ui_dimming_rules_on_save_attachments (ModestWindow *win, gpointer user_data)
802 {
803         ModestDimmingRule *rule = NULL;
804         gboolean dimmed = FALSE;
805
806         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
807         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
808         rule = MODEST_DIMMING_RULE (user_data);
809
810         /* Check dimmed rule */ 
811         if (!dimmed) 
812                 dimmed = _invalid_attach_selected (win, FALSE, FALSE, rule);
813
814         if (!dimmed) {
815                 dimmed = _purged_attach_selected (win, FALSE, NULL);
816                 if (dimmed) {
817                         modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
818                 }
819         }
820                 
821         return dimmed;
822 }
823
824 gboolean 
825 modest_ui_dimming_rules_on_remove_attachments (ModestWindow *win, gpointer user_data)
826 {
827         ModestDimmingRule *rule = NULL;
828         gboolean dimmed = FALSE;
829
830         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
831         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
832         rule = MODEST_DIMMING_RULE (user_data);
833
834         /* Check in main window if there's only one message selected */
835         if (!dimmed && MODEST_IS_MAIN_WINDOW (win)) {
836                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW (win), TRUE, rule);
837         }
838
839         /* Check in view window if there's any attachment selected */
840         if (!dimmed && MODEST_IS_MSG_VIEW_WINDOW (win)) {
841                 dimmed = _invalid_attach_selected (win, FALSE, FALSE, NULL);
842                 if (dimmed)
843                         modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
844         }
845
846         /* cannot purge in editable drafts nor pop folders */
847         if (!dimmed) {
848                 dimmed = _invalid_folder_for_purge (win, rule);
849         }
850
851         /* Check if the selected message in main window has attachments */
852         if (!dimmed && MODEST_IS_MAIN_WINDOW (win)) {
853                 dimmed = _selected_msg_marked_as (win, TNY_HEADER_FLAG_ATTACHMENTS, TRUE);
854                 if (dimmed)
855                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
856         }
857
858         /* Check if all attachments are already purged */
859         if (!dimmed) {
860                 dimmed = _purged_attach_selected (win, TRUE, rule);
861         }
862
863         /* Check if the message is already downloaded */
864         if (!dimmed && MODEST_IS_MAIN_WINDOW (win)) {
865                 dimmed = !_msg_download_completed (MODEST_MAIN_WINDOW (win));
866                 if (dimmed)
867                         modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
868         }
869
870
871         return dimmed;
872 }
873
874 gboolean 
875 modest_ui_dimming_rules_on_undo (ModestWindow *win, gpointer user_data)
876 {
877         ModestDimmingRule *rule = NULL;
878         gboolean dimmed = FALSE;
879         
880         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
881         rule = MODEST_DIMMING_RULE (user_data);
882
883         /* Check dimmed rule */ 
884         if (!dimmed) {
885                 dimmed = _clipboard_is_empty (win); 
886                 if (dimmed)
887                         modest_dimming_rule_set_notification (rule, "");
888         }
889                                 
890         return dimmed;  
891 }
892
893 gboolean 
894 modest_ui_dimming_rules_on_cut (ModestWindow *win, gpointer user_data)
895 {
896         ModestDimmingRule *rule = NULL;
897         gboolean dimmed = FALSE;
898         
899         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
900         rule = MODEST_DIMMING_RULE (user_data);
901
902         /* Check common dimming rules */
903         if (!dimmed) {
904                 dimmed = _invalid_clipboard_selected (win, rule);
905         }
906
907         /* Check window specific dimming rules */
908         if (MODEST_IS_MAIN_WINDOW (win)) {
909                 if (!dimmed) { 
910                         dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
911                         if (dimmed)
912                                 modest_dimming_rule_set_notification (rule, "");
913                 }
914                 if (!dimmed) {
915                         dimmed = _selected_msg_sent_in_progress (win);
916                         if (dimmed)
917                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_cut_mess"));
918                 }
919         }
920         
921         return dimmed;
922 }
923
924 gboolean 
925 modest_ui_dimming_rules_on_copy (ModestWindow *win, gpointer user_data)
926 {
927         ModestDimmingRule *rule = NULL;
928         gboolean dimmed = FALSE;
929         
930         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
931         rule = MODEST_DIMMING_RULE (user_data);
932
933         /* Check common dimming rules */
934         if (!dimmed) {
935                 dimmed = _invalid_clipboard_selected (win, rule);
936         }
937         
938         /* Check window specific dimming rules */
939         if (MODEST_IS_MAIN_WINDOW (win)) {
940                 if (!dimmed) {
941                         dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
942                         if (dimmed)
943                                 modest_dimming_rule_set_notification (rule, "");
944                 }               
945                 if (!dimmed) {
946                         dimmed = _selected_msg_sent_in_progress (win);
947                         if (dimmed)
948                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_cut_mess"));
949                 }
950         }
951                 
952         return dimmed;
953 }
954
955 gboolean 
956 modest_ui_dimming_rules_on_view_previous (ModestWindow *win, gpointer user_data)
957 {
958         ModestDimmingRule *rule = NULL;
959         gboolean dimmed = FALSE;
960         
961         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
962         rule = MODEST_DIMMING_RULE (user_data);
963
964         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
965
966         /* Check dimmed rule */ 
967         if (!dimmed) {
968                 dimmed = _transfer_mode_enabled (win);                  
969                 if (dimmed)
970                         modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
971         }
972         if (!dimmed) {
973                 if (MODEST_IS_MSG_VIEW_WINDOW (win))
974                         dimmed = modest_msg_view_window_first_message_selected (MODEST_MSG_VIEW_WINDOW(win));
975                 modest_dimming_rule_set_notification (rule, NULL);
976         }               
977
978         return dimmed;
979 }
980
981 gboolean 
982 modest_ui_dimming_rules_on_view_next (ModestWindow *win, gpointer user_data)
983 {
984         ModestDimmingRule *rule = NULL;
985         gboolean dimmed = FALSE;
986
987         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
988         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
989         rule = MODEST_DIMMING_RULE (user_data);
990
991         /* Check dimmed rule */ 
992         if (!dimmed) {
993                 dimmed = _transfer_mode_enabled (win);                  
994                 if (dimmed)
995                         modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
996         }
997         if (!dimmed) {
998                 if (MODEST_IS_MSG_VIEW_WINDOW (win))
999                         dimmed = modest_msg_view_window_last_message_selected (MODEST_MSG_VIEW_WINDOW (win));
1000                 modest_dimming_rule_set_notification (rule, NULL);
1001         }               
1002
1003         return dimmed;
1004 }
1005
1006
1007 gboolean 
1008 modest_ui_dimming_rules_on_tools_smtp_servers (ModestWindow *win, gpointer user_data)
1009 {
1010         const gboolean dimmed = 
1011                 !modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), 
1012                         TRUE);  
1013                 
1014         return dimmed;
1015 }
1016
1017 gboolean 
1018 modest_ui_dimming_rules_on_cancel_sending (ModestWindow *win, gpointer user_data)
1019 {
1020         ModestDimmingRule *rule = NULL;
1021         TnyFolderType types[1];
1022         guint n_messages = 0;
1023         gboolean dimmed = FALSE;
1024
1025         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1026         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1027         rule = MODEST_DIMMING_RULE (user_data);
1028
1029         types[0] = TNY_FOLDER_TYPE_OUTBOX; 
1030
1031         /* Check dimmed rules */        
1032         if (!dimmed) {
1033                 dimmed = _already_opened_msg (win, &n_messages);
1034                 if (dimmed) 
1035                         modest_dimming_rule_set_notification (rule, _("mcen_ib_message_unableto_cancel_send"));
1036         }
1037         if (!dimmed) {
1038                 dimmed = !_selected_folder_is_any_of_type (win, types, 1);
1039                 if (dimmed) 
1040                         modest_dimming_rule_set_notification (rule, "");
1041         }
1042         if (!dimmed) {
1043                 dimmed = !_sending_in_progress (win);
1044                 if (dimmed)
1045                         modest_dimming_rule_set_notification (rule, "");
1046         }
1047
1048         return dimmed;
1049 }
1050
1051 gboolean 
1052 modest_ui_dimming_rules_on_send_receive (ModestWindow *win, gpointer user_data)
1053 {
1054         ModestDimmingRule *rule = NULL;
1055         gboolean dimmed = FALSE;
1056         
1057         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1058         rule = MODEST_DIMMING_RULE (user_data);
1059  
1060         /* Check dimmed rule */ 
1061         if (!dimmed) {
1062                 dimmed = !modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), 
1063                                                           TRUE);        
1064                 if (dimmed)
1065                         modest_dimming_rule_set_notification (rule, _("mcen_nc_no_email_acnts_defined"));
1066         }
1067
1068         return dimmed;
1069 }
1070
1071 gboolean
1072 modest_ui_dimming_rules_on_add_to_contacts (ModestWindow *win, gpointer user_data)
1073 {
1074         ModestDimmingRule *rule = NULL;
1075         gboolean dimmed = FALSE;
1076
1077         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1078         rule = MODEST_DIMMING_RULE (user_data);
1079
1080         /* Check dimmed rule */
1081         if (!dimmed) {
1082                 GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
1083                 gchar *selection = NULL;
1084                 selection = gtk_clipboard_wait_for_text (clipboard);
1085
1086                 dimmed = !((selection != NULL) && (modest_text_utils_validate_recipient (selection, NULL)));
1087         }
1088
1089         return dimmed;
1090 }
1091
1092 /* *********************** static utility functions ******************** */
1093
1094 static gboolean 
1095 _marked_as_deleted (ModestWindow *win)
1096 {
1097         gboolean result = FALSE;
1098         TnyHeaderFlags flags;
1099
1100         flags = TNY_HEADER_FLAG_DELETED; 
1101
1102         /* Check dimmed rule */ 
1103         result = _selected_msg_marked_as (win, flags, FALSE);
1104         
1105         return result;
1106 }
1107
1108 static gboolean
1109 _selected_folder_not_writeable (ModestMainWindow *win)
1110 {
1111         GtkWidget *folder_view = NULL;
1112         TnyFolderStore *parent_folder = NULL;
1113         ModestTnyFolderRules rules;
1114         gboolean result = FALSE;
1115
1116         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1117
1118         /* Get folder view */
1119         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1120                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1121         /* If no folder view, always dimmed */
1122         if (!folder_view)
1123                 return TRUE;
1124         
1125         /* Get selected folder as parent of new folder to create */
1126         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1127         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
1128                 if (parent_folder)
1129                         g_object_unref (parent_folder);
1130                 return TRUE;
1131         }
1132         
1133         /* Check dimmed rule */ 
1134         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
1135         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
1136
1137         /* free */
1138         g_object_unref (parent_folder);
1139
1140         return result;
1141 }
1142
1143 static gboolean
1144 _selected_folder_is_root_or_inbox (ModestMainWindow *win)
1145 {
1146         TnyFolderType types[2];
1147         gboolean result = FALSE;
1148
1149         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1150
1151         types[0] = TNY_FOLDER_TYPE_ROOT; 
1152         types[1] = TNY_FOLDER_TYPE_INBOX; 
1153
1154         /* Check folder type */
1155         result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 2);
1156
1157         /* Check pop and MMC accounts */
1158         if (!result) {
1159                 result = _selected_folder_is_MMC_or_POP_root (win);
1160         }
1161                 
1162         return result;
1163 }
1164
1165
1166 static gboolean
1167 _selected_folder_is_root (ModestMainWindow *win)
1168 {
1169         TnyFolderType types[1];
1170         gboolean result = FALSE;
1171
1172         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1173
1174         /* All accounts are root items: */
1175         GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1176                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1177         if (folder_view) {                                      
1178                 gboolean is_account = FALSE;
1179                 TnyFolderStore *folder_store = 
1180                         modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1181                 if (folder_store) {
1182                         is_account = TNY_IS_ACCOUNT (folder_store);
1183                         g_object_unref (folder_store);
1184                         folder_store = NULL;
1185                 }
1186                 
1187                 if (is_account)
1188                         return TRUE;
1189         }
1190                 
1191         /* Try something more precise: */
1192         types[0] = TNY_FOLDER_TYPE_ROOT; 
1193
1194         /* Check folder type */
1195         result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 1);
1196                 
1197         /* Check pop and MMC accounts */
1198         if (!result) {
1199                 result = _selected_folder_is_MMC_or_POP_root (win);
1200         }
1201
1202         return result;
1203 }
1204
1205 static gboolean
1206 _selected_folder_is_MMC_or_POP_root (ModestMainWindow *win)
1207 {
1208         GtkWidget *folder_view = NULL;
1209         TnyFolderStore *parent_folder = NULL;
1210         gboolean result = FALSE;
1211
1212         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1213                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1214         /* Get selected folder as parent of new folder to create */
1215         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1216         if (!parent_folder)
1217                 return TRUE;
1218         
1219         if (TNY_IS_ACCOUNT (parent_folder)) {
1220                 /* If it's the local account then do not dim */
1221                 if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
1222                         result = FALSE;
1223                 } else {
1224                                 /* If it's the MMC root folder then dim it */
1225                         if (!strcmp (tny_account_get_id (TNY_ACCOUNT (parent_folder)), MODEST_MMC_ACCOUNT_ID)) {
1226                                         result = TRUE;
1227                         } else {
1228                                 const gchar *proto_str = tny_account_get_proto (TNY_ACCOUNT (parent_folder));
1229                                 /* If it's POP then dim */
1230                                 result = (modest_protocol_info_get_transport_store_protocol (proto_str) == 
1231                                                   MODEST_PROTOCOL_STORE_POP) ? TRUE : FALSE;
1232                         }
1233                 }
1234         }
1235         g_object_unref (parent_folder);
1236
1237         return result;
1238 }
1239
1240
1241 static gboolean
1242 _selected_folder_is_empty (ModestMainWindow *win)
1243 {
1244         GtkWidget *folder_view = NULL;
1245         TnyFolderStore *folder = NULL;
1246         gboolean result = FALSE;
1247
1248         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1249
1250         /* Get folder view */
1251         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1252                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1253         /* If no folder view, always dimmed */
1254         if (!folder_view)
1255                 return TRUE;
1256         
1257         /* Get selected folder as parent of new folder to create */
1258         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1259         if (!(folder && TNY_IS_FOLDER(folder))) {
1260                 if (folder)
1261                         g_object_unref (folder);
1262                 return TRUE;
1263         }
1264         
1265         /* Check folder type */
1266         result = tny_folder_get_all_count (TNY_FOLDER (folder)) == 0;
1267
1268         /* free */
1269         g_object_unref (folder);
1270
1271         return result;
1272 }
1273
1274 static gboolean
1275 _selected_folder_is_same_as_source (ModestWindow *win)
1276 {
1277         ModestEmailClipboard *clipboard = NULL;
1278         GtkWidget *folder_view = NULL;
1279         TnyFolderStore *folder = NULL;
1280         gboolean result = FALSE;
1281
1282         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1283
1284         /* Get folder view */
1285         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1286                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1287         if (!folder_view)
1288                 return FALSE;
1289         
1290         /* Get selected folder as destination folder */
1291         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1292         if (!(folder && TNY_IS_FOLDER(folder))) {
1293                 result = FALSE;
1294                 goto frees;
1295         }
1296         
1297         /* Check clipboard is cleared */
1298         clipboard = modest_runtime_get_email_clipboard ();
1299         if (modest_email_clipboard_cleared (clipboard)) {
1300                 result = FALSE;
1301                 goto frees;
1302         }
1303                 
1304         /* Check source folder */
1305         result = modest_email_clipboard_check_source_folder (clipboard, 
1306                                                              (const TnyFolder *) folder);
1307         
1308         /* Free */
1309  frees:
1310         g_object_unref (folder);
1311         
1312         return result;
1313 }
1314
1315 static gboolean
1316 _selected_folder_is_any_of_type (ModestWindow *win,
1317                                  TnyFolderType types[], 
1318                                  guint ntypes)
1319 {
1320         GtkWidget *folder_view = NULL;
1321         TnyFolderStore *folder = NULL;
1322         TnyFolderType folder_type;
1323         guint i=0;
1324         gboolean result = FALSE;
1325
1326         /*Get current folder */
1327         if (MODEST_IS_MAIN_WINDOW(win)) {
1328
1329                 /* Get folder view */
1330                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1331                                                                    MODEST_WIDGET_TYPE_FOLDER_VIEW);
1332                 /* If no folder view, always dimmed */
1333                 if (!folder_view)
1334                         return FALSE;
1335         
1336                 /* Get selected folder as parent of new folder to create */
1337                 folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1338
1339                 if (!(folder && TNY_IS_FOLDER(folder))) {
1340                         if (folder)
1341                                 g_object_unref (folder);
1342                         return FALSE;
1343                 }
1344                 
1345                 /* Check folder type */
1346                 result = _folder_is_any_of_type (TNY_FOLDER(folder), types, ntypes);
1347         }
1348         else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
1349                 folder_type = modest_msg_view_window_get_folder_type (MODEST_MSG_VIEW_WINDOW (win));
1350                 for (i=0; i < ntypes; i++) {
1351                         result = result || folder_type == types[i];
1352                 }
1353         }
1354
1355
1356         /* free */
1357         g_object_unref (folder);
1358
1359         return result;  
1360 }
1361
1362 static gboolean
1363 _folder_is_any_of_type (TnyFolder *folder,
1364                         TnyFolderType types[], 
1365                         guint ntypes)
1366 {
1367         TnyFolderType folder_type;
1368         gboolean result = FALSE;
1369         guint i;
1370
1371         g_return_val_if_fail (TNY_IS_FOLDER(folder), FALSE);
1372
1373         /* Get folder type */
1374         folder_type = modest_tny_folder_guess_folder_type (folder);
1375                 
1376         /* Check foler type */
1377         for (i=0; i < ntypes; i++) {
1378                 result = result || folder_type == types[i];
1379         }
1380
1381         return result;
1382 }
1383
1384 static gboolean
1385 _selected_folder_is_snd_level (ModestMainWindow *win)
1386 {
1387         GtkWidget *folder_view = NULL;
1388         GtkTreeSelection *sel = NULL;
1389         GtkTreeModel *model = NULL;
1390         GtkTreePath *path = NULL;
1391         GtkTreeIter iter;
1392         TnyFolderStore *folder = NULL;
1393         gboolean result = FALSE;
1394
1395         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), TRUE);
1396
1397         /*Get curent folder */
1398         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1399                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1400         /* If no folder view, always dimmed */
1401         if (!folder_view) 
1402                 goto frees;
1403
1404         /* Get selected folder as parent of new folder to create */
1405         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));     
1406         if (!(folder && TNY_IS_FOLDER(folder))) {
1407                 goto frees;
1408         }
1409         
1410         /* Check folder level */
1411         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(folder_view));
1412         if (!gtk_tree_selection_get_selected (sel, &model, &iter))
1413                 goto frees;
1414         path = gtk_tree_model_get_path (model, &iter);
1415         result = gtk_tree_path_get_depth (path) > 2;
1416         
1417  frees:
1418         if (folder != NULL)
1419                 g_object_unref (folder);
1420         if (path != NULL)
1421                 gtk_tree_path_free (path);
1422         
1423         return result;
1424
1425 }
1426
1427 static gboolean
1428 _clipboard_is_empty (ModestWindow *win)
1429 {
1430         gboolean result = FALSE;
1431         
1432         if (MODEST_IS_MAIN_WINDOW (win)) {
1433                 ModestEmailClipboard *clipboard = NULL;
1434                 clipboard = modest_runtime_get_email_clipboard ();
1435                 if (modest_email_clipboard_cleared (clipboard)) 
1436                  result = TRUE;  
1437         }
1438
1439         return result;
1440 }
1441
1442 static gboolean
1443 _invalid_clipboard_selected (ModestWindow *win,
1444                        ModestDimmingRule *rule) 
1445 {
1446         gboolean result = FALSE;
1447
1448         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1449                 GtkClipboard *clipboard = NULL;
1450                 gchar *selection = NULL;
1451                 GtkWidget *focused = NULL;
1452
1453                 /* Get clipboard selection*/
1454                 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
1455                 selection = gtk_clipboard_wait_for_text (clipboard);
1456
1457                 /* Get focuesed widget */
1458                 focused = gtk_window_get_focus (GTK_WINDOW (win));
1459
1460                 /* Check dimming */
1461                 result = ((selection == NULL) || 
1462                           (MODEST_IS_ATTACHMENTS_VIEW (focused)));
1463                 
1464                 if (result)
1465                         modest_dimming_rule_set_notification (rule, "");
1466         }               
1467         else if (MODEST_IS_MAIN_WINDOW (win)) {
1468                 GtkWidget *header_view = NULL;
1469
1470                 /* Get header view to check selected messages */
1471                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1472                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
1473         
1474                 /* Check dimming */
1475                 result = !modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view));            
1476                 if (result)
1477                         modest_dimming_rule_set_notification (rule, _("mcen_ib_no_message_selected"));                  
1478         }
1479         
1480         return result;
1481 }
1482
1483
1484 static gboolean
1485 _invalid_attach_selected (ModestWindow *win,
1486                           gboolean unique,
1487                           gboolean for_view,
1488                           ModestDimmingRule *rule) 
1489 {
1490         GList *attachments, *node;
1491         gint n_selected;
1492         TnyHeaderFlags flags;
1493         gboolean nested_attachments = FALSE;
1494         gboolean selected_messages = FALSE;
1495         gboolean result = FALSE;
1496
1497         if (MODEST_IS_MAIN_WINDOW (win)) {
1498                 flags = TNY_HEADER_FLAG_ATTACHMENTS;
1499                 if (!result)
1500                         result = _selected_msg_marked_as (win, flags, TRUE);            
1501         }
1502         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1503                 
1504                 /* Get selected atachments */
1505                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
1506                 n_selected = g_list_length (attachments);
1507
1508                 /* Check unique */              
1509                 if (!result) {
1510                         if (unique) 
1511                                 result = n_selected != 1;
1512                         else
1513                                 
1514                                 result = n_selected < 1;
1515                 }
1516                 
1517                 /* Check attached type (view operation not required) */
1518                 if (!result && !for_view)  {
1519                         for (node = attachments; node != NULL && !result; node = g_list_next (node)) {
1520                                 TnyMimePart *mime_part = TNY_MIME_PART (node->data);
1521                                 TnyList *nested_list = tny_simple_list_new ();
1522                                 if (TNY_IS_MSG (mime_part)) {
1523                                         selected_messages = TRUE;
1524                                         result = TRUE;
1525                                 }
1526                                 tny_mime_part_get_parts (mime_part, nested_list);
1527                                 if (tny_list_get_length (nested_list) > 0) {
1528                                 nested_attachments = TRUE;
1529                                 result = TRUE;
1530                                 }
1531                                 g_object_unref (nested_list);
1532                         }
1533                 }
1534                 
1535                 /* Set notifications */
1536                 if (result && rule != NULL) {
1537                         if (selected_messages) {
1538                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_save_attach_mail"));
1539                         } else if (nested_attachments) {
1540                                 modest_dimming_rule_set_notification (rule, _("FIXME:unable to save attachments with nested elements"));
1541                         } else if (n_selected == 0) {
1542                                 modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
1543                         } else if (unique) {
1544                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_display_more"));
1545                         }
1546                 }
1547                 
1548                 /* Free */
1549                 g_list_free (attachments);
1550         }
1551
1552         return result;
1553 }
1554
1555 static gboolean
1556 _purged_attach_selected (ModestWindow *win, gboolean all, ModestDimmingRule *rule) 
1557 {
1558         GList *attachments = NULL, *node;
1559         gint purged = 0;
1560         gint n_attachments = 0;
1561         gboolean result = FALSE;
1562
1563         /* This should check if _all_ the attachments are already purged. If only some
1564          * of them are purged, then it does not cause dim as there's a confirmation dialog
1565          * for removing only local attachments */
1566
1567         /* Get selected atachments */
1568         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1569                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
1570         } else if (MODEST_IS_MAIN_WINDOW (win)) {
1571                 /* If we're in main window, we won't know if there are already purged attachments */
1572                 return FALSE;
1573         }
1574
1575         if (attachments == NULL)
1576                 return FALSE;
1577
1578         for (node = attachments; node != NULL; node = g_list_next (node)) {
1579                 TnyMimePart *mime_part = TNY_MIME_PART (node->data);
1580                 if (tny_mime_part_is_purged (mime_part)) {
1581                         purged++;
1582                 }
1583                 n_attachments++;
1584         }
1585                 
1586         /* Free */
1587         g_list_free (attachments);
1588
1589         if (all)
1590                 result = (purged == n_attachments);
1591         else
1592                 result = (purged > 0);
1593
1594         if (result && (rule != NULL))
1595                 modest_dimming_rule_set_notification (rule, _("mail_ib_attachment_already_purged"));
1596
1597         return result;
1598 }
1599
1600 static gboolean
1601 _invalid_msg_selected (ModestMainWindow *win,
1602                        gboolean unique,
1603                        ModestDimmingRule *rule) 
1604 {
1605         GtkWidget *header_view = NULL;          
1606         GtkWidget *folder_view = NULL;
1607         gboolean selected_headers = FALSE;
1608         gboolean result = FALSE;
1609
1610         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1611         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (rule), FALSE);
1612                 
1613         /* Get header view to check selected messages */
1614         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1615                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1616         
1617         /* Get folder view to check focus */
1618         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
1619                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1620
1621         /* Get selected headers */
1622         selected_headers = modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view));
1623
1624         /* Check dimmed rule (TODO: check focus on widgets */   
1625         if (!result) {
1626                 result = ((!selected_headers) ||
1627                           (gtk_widget_is_focus (folder_view)));
1628                 if (result)
1629                         modest_dimming_rule_set_notification (rule, _("mcen_ib_no_message_selected"));
1630         }
1631         if (!result && unique) {
1632                 result = modest_header_view_count_selected_headers (MODEST_HEADER_VIEW(header_view)) > 1;
1633                 if (result)
1634                         modest_dimming_rule_set_notification (rule, _("mcen_ib_select_one_message"));
1635         }
1636
1637         return result;
1638 }
1639
1640 static gboolean
1641 _already_opened_msg (ModestWindow *win,
1642                      guint *n_messages)
1643 {
1644         //ModestWindow *window = NULL;
1645         ModestWindowMgr *mgr = NULL;
1646         GtkWidget *header_view = NULL;          
1647         TnyList *selected_headers = NULL;
1648         TnyIterator *iter = NULL;
1649         TnyHeader *header = NULL;
1650         gboolean found;
1651
1652         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1653                 
1654         /* Get header view to check selected messages */
1655         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1656                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1657
1658
1659         /* Check no selection */
1660         if (!modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view)))
1661             return FALSE;
1662             
1663         /* Get selected headers */
1664         selected_headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
1665         if (selected_headers == NULL) 
1666                 return FALSE;
1667
1668         *n_messages = tny_list_get_length (selected_headers);
1669
1670         /* Check dimmed rule (TODO: check focus on widgets */   
1671         mgr = modest_runtime_get_window_mgr ();
1672         iter = tny_list_create_iterator (selected_headers);
1673         found = FALSE;
1674         while (!tny_iterator_is_done (iter)) {
1675                 header = TNY_HEADER (tny_iterator_get_current (iter));
1676                 if (header) {
1677                         found = modest_window_mgr_find_registered_header (mgr,header, NULL);
1678                 
1679                         g_object_unref (header);
1680                 }
1681
1682                 tny_iterator_next (iter);
1683
1684                 if (found)
1685                         break;
1686         }
1687                 
1688         /* free */
1689         if (selected_headers != NULL) 
1690                 g_object_unref (selected_headers);
1691         if (iter != NULL)
1692                 g_object_unref (iter);
1693                 
1694         return found;
1695 }
1696
1697 static gboolean
1698 _selected_msg_marked_as (ModestWindow *win, 
1699                          TnyHeaderFlags mask, 
1700                          gboolean opposite)
1701 {
1702         ModestWindow *main_window = NULL;
1703         GtkWidget *header_view = NULL;
1704         TnyList *selected_headers = NULL;
1705         TnyIterator *iter = NULL;
1706         TnyHeader *header = NULL;
1707         TnyHeaderFlags flags = 0;
1708         gboolean result = FALSE;
1709
1710         if (MODEST_IS_MAIN_WINDOW (win))
1711                 main_window = win;
1712         else
1713                 main_window = 
1714                         modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());           
1715
1716         /* TODO: Javi, what about if the main window does not
1717            exist?. Adding some code to avoid CRITICALs */
1718         if (!main_window)
1719                 return FALSE;
1720
1721         /* Get header view to check selected messages */
1722         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(main_window),
1723                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1724
1725         /* Check no selection */
1726         if (!modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view)))
1727             return TRUE;
1728
1729         /* Get selected headers */
1730         selected_headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
1731         if (selected_headers == NULL) 
1732                 return TRUE;
1733         
1734         /* Call the function for each header */
1735         iter = tny_list_create_iterator (selected_headers);
1736         while (!tny_iterator_is_done (iter) && !result) {
1737                 header = TNY_HEADER (tny_iterator_get_current (iter));
1738                 if (header) {
1739                         flags = tny_header_get_flags (header);
1740                         if (opposite)
1741                                 result = (flags & mask) == 0; 
1742                         else
1743                                 result = (flags & mask) != 0; 
1744
1745                         g_object_unref (header);
1746                 }
1747
1748                 tny_iterator_next (iter);
1749         }
1750
1751         /* free */
1752         if (selected_headers != NULL) 
1753                 g_object_unref (selected_headers);
1754         if (iter != NULL)
1755                 g_object_unref (iter);
1756
1757         return result;
1758 }
1759
1760 static gboolean
1761 _msg_download_in_progress (ModestMsgViewWindow *win)
1762 {
1763         gboolean result = FALSE;
1764
1765         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), FALSE);
1766
1767         result = modest_msg_view_window_toolbar_on_transfer_mode (win);
1768
1769         return result;
1770 }
1771
1772 static gboolean
1773 _msg_download_completed (ModestMainWindow *win)
1774 {
1775         GtkWidget *header_view = NULL;
1776         TnyList *selected_headers = NULL;
1777         TnyIterator *iter = NULL;
1778         TnyHeader *header = NULL;
1779         TnyHeaderFlags flags = 0;
1780         gboolean result = FALSE;
1781
1782         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (win), FALSE);
1783
1784
1785         /* Get header view to check selected messages */
1786         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1787                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1788
1789         /* Check no selection */
1790         if (!modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view)))
1791             return TRUE;
1792
1793         /* Get selected headers */
1794         selected_headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
1795         if (selected_headers == NULL) 
1796                 return TRUE;
1797
1798         /* Check dimmed rule  */        
1799         result = TRUE;
1800         iter = tny_list_create_iterator (selected_headers);
1801         while (!tny_iterator_is_done (iter) && result) {
1802                 header = TNY_HEADER (tny_iterator_get_current (iter));
1803                 if (header) {
1804                         flags = tny_header_get_flags (header);
1805                         /* TODO: is this the right flag?, it seems that some
1806                            headers that have been previously downloaded do not
1807                            come with it */
1808                         result = (flags & TNY_HEADER_FLAG_CACHED);
1809
1810                         g_object_unref (header);
1811                 }
1812
1813                 tny_iterator_next (iter);
1814         }
1815
1816         g_object_unref (iter);
1817
1818         return result;
1819 }
1820
1821 static gboolean
1822 _selected_msg_sent_in_progress (ModestWindow *win)
1823 {
1824         ModestTnySendQueue *send_queue = NULL;
1825         GtkWidget *header_view = NULL;
1826         ModestTnyAccountStore *acc_store = NULL;
1827         TnyAccount *account = NULL;
1828         TnyList *header_list = NULL;
1829         TnyIterator *iter = NULL;
1830         TnyHeader *header = NULL;
1831         const gchar *account_name = NULL;
1832         gboolean result = FALSE;
1833         gchar *id = NULL;
1834         
1835         /* Get transport account */
1836         acc_store = modest_runtime_get_account_store();
1837         account_name = modest_window_get_active_account (win);
1838         
1839         /* If no account defined, this action must be always dimmed  */
1840         if (account_name == NULL) return FALSE;
1841         account = modest_tny_account_store_get_transport_account_for_open_connection (acc_store, account_name);
1842         if (!TNY_IS_TRANSPORT_ACCOUNT (account)) return FALSE;
1843
1844         /* Get send queue for current ransport account */
1845         send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(account));
1846         g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE (send_queue), FALSE);
1847
1848         if (MODEST_IS_MAIN_WINDOW(win)) {
1849                 
1850                 /* Get header view to check selected messages */
1851                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1852                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
1853                 
1854                 /* Check no selection */
1855                 if (!modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view)))
1856                     return FALSE;
1857
1858                 /* Get selected headers */
1859                 header_list = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
1860
1861                 /* Get message header */
1862                 if (!header_list) return FALSE;
1863                 iter = tny_list_create_iterator (header_list);
1864                 header = TNY_HEADER (tny_iterator_get_current (iter));
1865                 if (header) {
1866                         /* Get message id */
1867                         id = g_strdup(tny_header_get_message_id (header));
1868                         g_object_unref (header);
1869                 }
1870                 
1871         } else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
1872                 
1873                 /* Get message header */
1874                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW(win));
1875                 if (header) {
1876                         /* Get message id */
1877                         id = g_strdup(tny_header_get_message_id (header));
1878
1879                         g_object_unref (header);
1880                 }
1881         }
1882
1883         /* Check if msg id is being processed inside send queue */
1884         result = modest_tny_send_queue_msg_is_being_sent (send_queue, id);
1885
1886         /* Free */
1887         g_free(id);
1888         g_object_unref(header_list);
1889         g_object_unref(iter);
1890
1891         return result;
1892 }
1893
1894
1895 static gboolean
1896 _sending_in_progress (ModestWindow *win)
1897 {
1898         gboolean result = FALSE;
1899         
1900         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (win), FALSE);
1901
1902         /* Check if send operation is in progress */
1903         result = modest_main_window_send_receive_in_progress (MODEST_MAIN_WINDOW (win));
1904
1905         return result;
1906 }
1907
1908 static gboolean
1909 _invalid_folder_for_purge (ModestWindow *win, 
1910                            ModestDimmingRule *rule)
1911 {
1912         TnyMsg *msg = NULL;
1913         TnyFolder *folder = NULL;
1914         TnyAccount *account = NULL;
1915         gboolean result = FALSE;
1916
1917         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1918
1919                 /* Get folder and account of message */
1920                 msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
1921                 g_return_val_if_fail(msg != NULL, TRUE);                        
1922                 folder = tny_msg_get_folder (msg);      
1923                 if (folder == NULL) {
1924                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
1925                         goto frees;
1926                 }
1927                 g_object_unref (msg);
1928         } else if (MODEST_IS_MAIN_WINDOW (win)) {
1929                 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
1930                                                                               MODEST_WIDGET_TYPE_FOLDER_VIEW);
1931                 if (!folder_view)
1932                         return FALSE;
1933                 folder = TNY_FOLDER (modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view)));
1934                 if (folder == NULL || ! TNY_IS_FOLDER (folder))
1935                         goto frees;             
1936         } else {
1937                 g_return_val_if_reached (FALSE);
1938         }
1939         account = modest_tny_folder_get_account (folder);
1940         if (account == NULL) goto frees;                        
1941                 
1942         /* Check account */
1943         if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (account))) {
1944                 TnyFolderType types[2];
1945                 types[0] = TNY_FOLDER_TYPE_DRAFTS;
1946                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
1947                 
1948                 if (_selected_folder_is_any_of_type (win, types, 2)) {
1949                         result = TRUE;
1950                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_editable_msg"));
1951                 } else {
1952                         /* We're currently disabling purge in any local store */
1953                         result = TRUE;
1954                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
1955                 }
1956         } else {
1957                 const gchar *proto_str = tny_account_get_proto (TNY_ACCOUNT (account));
1958                 /* If it's POP then dim */
1959                 if (modest_protocol_info_get_transport_store_protocol (proto_str) == 
1960                     MODEST_PROTOCOL_STORE_POP) {
1961                         GList *attachments = NULL;
1962                         gint n_selected = 0;
1963                         result = TRUE;
1964                         
1965                         /* TODO: This check is here to prevent a gwarning, but this looks like a logic error.
1966                          * murrayc */
1967                         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1968                                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
1969                                 n_selected = g_list_length (attachments);
1970                                 g_list_free (attachments);
1971                         }
1972                         
1973                         modest_dimming_rule_set_notification (rule, 
1974                                                               ngettext ("mail_ib_unable_to_pure_attach_pop_mail_singular",
1975                                                                         "mail_ib_unable_to_pure_attach_pop_mail_plural", 
1976                                                                         n_selected));
1977                 }
1978         }
1979         
1980 frees:
1981         if (folder != NULL)
1982                 g_object_unref (folder);
1983         if (account != NULL)
1984                 g_object_unref (account);
1985         
1986         return result;
1987 }
1988
1989 static gboolean
1990 _transfer_mode_enabled (ModestWindow *win)
1991 {
1992         gboolean result = FALSE;
1993
1994         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), FALSE);
1995         
1996         /* Check dimming */
1997         result = modest_msg_view_window_transfer_mode_enabled (MODEST_MSG_VIEW_WINDOW (win));
1998
1999         return result;
2000 }