* Add new dimming rules for copy,cut and paste operations.
[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-text-utils.h"
39 #include <widgets/modest-attachments-view.h>
40 #include <modest-runtime.h>
41 #include <tny-simple-list.h>
42
43
44 static gboolean _folder_is_any_of_type (TnyFolder *folder, TnyFolderType types[], guint ntypes);
45 static gboolean _invalid_msg_selected (ModestMainWindow *win, gboolean unique, ModestDimmingRule *rule);
46 static gboolean _invalid_attach_selected (ModestWindow *win, gboolean unique, gboolean for_view, ModestDimmingRule *rule);
47 static gboolean _clipboard_is_empty (ModestWindow *win);
48 static gboolean _invalid_clipboard_selected (ModestWindow *win);
49 static gboolean _already_opened_msg (ModestWindow *win, guint *n_messages);
50 static gboolean _selected_msg_marked_as (ModestWindow *win, TnyHeaderFlags mask, gboolean opposite);
51 static gboolean _selected_folder_not_writeable (ModestMainWindow *win);
52 static gboolean _selected_folder_is_any_of_type (ModestWindow *win, TnyFolderType types[], guint ntypes);
53 static gboolean _selected_folder_is_root_or_inbox (ModestMainWindow *win);
54 static gboolean _selected_folder_is_MMC_or_POP_root (ModestMainWindow *win);
55 static gboolean _selected_folder_is_root (ModestMainWindow *win);
56 static gboolean _selected_folder_is_empty (ModestMainWindow *win);
57 static gboolean _selected_folder_is_same_as_source (ModestWindow *win);
58 static gboolean _msg_download_in_progress (ModestMsgViewWindow *win);
59 static gboolean _msg_download_completed (ModestMainWindow *win);
60 static gboolean _selected_msg_sent_in_progress (ModestWindow *win);
61 static gboolean _sending_in_progress (ModestWindow *win);
62 static gboolean _marked_as_deleted (ModestWindow *win);
63 static gboolean _invalid_attachment_for_purge (ModestWindow *win, ModestDimmingRule *rule);
64
65
66 gboolean 
67 modest_ui_dimming_rules_on_new_msg (ModestWindow *win, gpointer user_data)
68 {
69         ModestDimmingRule *rule = NULL;
70         gboolean dimmed = FALSE;
71
72         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
73         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
74         rule = MODEST_DIMMING_RULE (user_data);
75                 
76         /* Check dimmed rule */ 
77         if (!dimmed) {
78                 dimmed = _msg_download_in_progress (MODEST_MSG_VIEW_WINDOW(win));
79                 if (dimmed)
80                         modest_dimming_rule_set_notification (rule, "");
81         }
82
83         return dimmed;
84 }
85
86 gboolean 
87 modest_ui_dimming_rules_on_new_folder (ModestWindow *win, gpointer user_data)
88 {
89         ModestDimmingRule *rule = NULL;
90         GtkWidget *folder_view = NULL;
91         TnyFolderStore *parent_folder = NULL;
92         gboolean dimmed = FALSE;
93
94         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
95         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
96         rule = MODEST_DIMMING_RULE (user_data);
97
98         /* Get selected folder as parent of new folder to create */
99         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
100                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
101         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
102         if (!parent_folder)
103                 return TRUE;
104         
105         if (TNY_IS_ACCOUNT (parent_folder)) {
106                 /* If it's the local account then do not dim */
107                 if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
108                         dimmed = FALSE;
109                 } else {
110                         const gchar *proto_str = tny_account_get_proto (TNY_ACCOUNT (parent_folder));
111                         /* If it's POP then dim */
112                         dimmed = (modest_protocol_info_get_transport_store_protocol (proto_str) == 
113                                   MODEST_PROTOCOL_STORE_POP) ? TRUE : FALSE;
114                         if (dimmed)
115                                 modest_dimming_rule_set_notification (rule, "");
116                 }
117         } else {
118                 /* TODO: the specs say that only one level of subfolder is allowed, is this true ? */
119                 
120                 TnyFolderType types[3];
121                                 
122                 types[0] = TNY_FOLDER_TYPE_DRAFTS; 
123                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
124                 types[2] = TNY_FOLDER_TYPE_SENT;
125
126                 /* Apply folder rules */        
127                 if (!dimmed) {
128                         dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
129                         if (dimmed)
130                                 modest_dimming_rule_set_notification (rule, "");
131                 }
132                 if (!dimmed) {
133                         dimmed = _selected_folder_is_any_of_type (win, types, 3);
134                         if (dimmed)
135                                 modest_dimming_rule_set_notification (rule, "");
136                 }
137         }
138         g_object_unref (parent_folder);
139
140         return dimmed;
141 }
142
143 gboolean 
144 modest_ui_dimming_rules_on_delete_folder (ModestWindow *win, gpointer user_data)
145 {
146         ModestDimmingRule *rule = NULL;
147         TnyFolderType types[5];
148         gboolean dimmed = FALSE;
149
150         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
151         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
152         rule = MODEST_DIMMING_RULE (user_data);
153
154         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
155         types[1] = TNY_FOLDER_TYPE_OUTBOX;
156         types[2] = TNY_FOLDER_TYPE_SENT;
157         types[3] = TNY_FOLDER_TYPE_INBOX;
158         types[4] = TNY_FOLDER_TYPE_ROOT;
159
160                 
161         /* Check dimmed rule */ 
162         if (!dimmed) {
163                 dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
164                 if (dimmed)
165                         modest_dimming_rule_set_notification (rule, "");
166         }
167         if (!dimmed) {
168                 dimmed = _selected_folder_is_any_of_type (win, types, 5);
169                 if (dimmed)
170                         modest_dimming_rule_set_notification (rule, "");
171         }
172         if (!dimmed) {
173                 dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
174                 if (dimmed)
175                         modest_dimming_rule_set_notification (rule, "");
176         }
177         if (!dimmed) {
178                 dimmed = _selected_folder_is_MMC_or_POP_root (MODEST_MAIN_WINDOW(win));
179                 if (dimmed)
180                         modest_dimming_rule_set_notification (rule, "");
181         }
182
183         return dimmed;
184 }
185
186 gboolean
187 modest_ui_dimming_rules_on_sort (ModestWindow *win, gpointer user_data)
188 {
189         ModestDimmingRule *rule = NULL;
190         gboolean dimmed = FALSE;
191
192         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
193         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
194         rule = MODEST_DIMMING_RULE (user_data);
195                 
196         /* Check dimmed rule */ 
197         if (!dimmed) {
198                 dimmed = _selected_folder_is_root (MODEST_MAIN_WINDOW(win));
199                 if (dimmed)
200                         modest_dimming_rule_set_notification (rule, "");
201         }
202
203         return dimmed;
204         
205 }
206
207 gboolean 
208 modest_ui_dimming_rules_on_rename_folder (ModestWindow *win, gpointer user_data)
209 {
210         ModestDimmingRule *rule = NULL;
211         TnyFolderType types[3];
212         gboolean dimmed = FALSE;
213
214         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
215         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
216         rule = MODEST_DIMMING_RULE (user_data);
217
218         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
219         types[1] = TNY_FOLDER_TYPE_OUTBOX;
220         types[2] = TNY_FOLDER_TYPE_SENT;
221         
222         /* Check dimmed rule */ 
223         if (!dimmed) {
224                 dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
225                 if (dimmed)
226                         modest_dimming_rule_set_notification (rule, "");
227         }
228         if (!dimmed) {
229                 dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
230                 if (dimmed)
231                         modest_dimming_rule_set_notification (rule, "");
232         }
233         if (!dimmed) {
234                 dimmed = _selected_folder_is_any_of_type (win, types, 3);
235                 if (dimmed)
236                         modest_dimming_rule_set_notification (rule, "");
237         }
238
239         return dimmed;
240 }
241
242 gboolean 
243 modest_ui_dimming_rules_on_open_msg (ModestWindow *win, gpointer user_data)
244 {
245         ModestDimmingRule *rule = NULL;
246         gboolean dimmed = FALSE;
247
248         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
249         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
250         rule = MODEST_DIMMING_RULE (user_data);
251                 
252         /* Check dimmed rule */ 
253         if (!dimmed) {
254                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, user_data);
255         }
256         if (!dimmed) {
257                 dimmed = _selected_msg_sent_in_progress (win);
258                 if (dimmed)
259                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_open_while_sent"));
260         }
261
262         return dimmed;
263 }
264
265 gboolean 
266 modest_ui_dimming_rules_on_reply_msg (ModestWindow *win, gpointer user_data)
267 {
268         ModestDimmingRule *rule = NULL;
269         gboolean dimmed = FALSE;
270         TnyFolderType types[3];
271
272         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
273         rule = MODEST_DIMMING_RULE (user_data);
274
275         /* main window dimming rules */
276         if (MODEST_IS_MAIN_WINDOW(win)) {
277                 
278                 types[0] = TNY_FOLDER_TYPE_DRAFTS; 
279                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
280                 types[2] = TNY_FOLDER_TYPE_ROOT;
281                 
282                 /* Check dimmed rule */ 
283                 if (!dimmed) {
284                         dimmed = _selected_folder_is_any_of_type (win, types, 3);                       
285                         if (dimmed)
286                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_reply"));
287                 }
288                 if (!dimmed) {
289                         dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
290                         if (dimmed)
291                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_nothing_to_reply"));
292                 }
293                 if (!dimmed) {
294                         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, rule);
295                 }
296         /* msg view window dimming rules */
297         } else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
298                 
299                 /* Check dimmed rule */ 
300                 if (!dimmed) {
301                         dimmed = _msg_download_in_progress (MODEST_MSG_VIEW_WINDOW(win));
302                         if (dimmed)
303                                 modest_dimming_rule_set_notification (rule, "");
304                 }
305         }
306         
307         return dimmed;
308 }
309
310
311 gboolean 
312 modest_ui_dimming_rules_on_contents_msg (ModestWindow *win, gpointer user_data)
313 {
314         ModestDimmingRule *rule = NULL;
315         gboolean dimmed = FALSE;
316
317         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
318         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
319         rule = MODEST_DIMMING_RULE (user_data);
320                 
321         /* Check dimmed rule */ 
322         if (!dimmed) {
323                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
324         }
325         if (!dimmed) {
326                 dimmed = _msg_download_completed (MODEST_MAIN_WINDOW(win));
327                 if (dimmed)
328                         modest_dimming_rule_set_notification (rule, "");
329         }
330
331         return dimmed;
332 }
333
334 gboolean 
335 modest_ui_dimming_rules_always_dimmed (ModestWindow *win, gpointer user_data)
336 {
337         gboolean dimmed = FALSE;
338
339         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
340                 
341         /* Check dimmed rule */ 
342         if (!dimmed)
343                 dimmed = TRUE;
344
345         return dimmed;
346 }
347
348 gboolean 
349 modest_ui_dimming_rules_on_delete_msg (ModestWindow *win, gpointer user_data)
350 {
351         ModestDimmingRule *rule = NULL;
352         guint n_messages = 0;
353         gboolean dimmed = FALSE;
354
355         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
356         rule = MODEST_DIMMING_RULE (user_data);
357         
358         /* Check dimmed rule */         
359         if (MODEST_IS_MAIN_WINDOW (win)) {
360                 if (!dimmed) {
361                         dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
362                         if (dimmed)
363                                 modest_dimming_rule_set_notification (rule, _CS("ckct_ib_nothing_to_delete"));
364                 }
365                 if (!dimmed) {
366                         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
367                 }
368                 if (!dimmed) {
369                         dimmed = _already_opened_msg (win, &n_messages);
370                         if (dimmed) {
371                                 gchar *num = g_strdup_printf ("%d", n_messages);
372                                 gchar *message = g_strdup_printf(_("mcen_nc_unable_to_delete_n_messages"), n_messages);
373                                 modest_dimming_rule_set_notification (rule, message);
374                                 g_free(message);
375                                 g_free(num);
376                         }
377                 }
378                 if (!dimmed) {
379                         dimmed = _marked_as_deleted (win);
380                         if (dimmed)
381                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_message_unableto_delete"));
382                 }
383                 if (!dimmed) {
384                         dimmed = _selected_msg_sent_in_progress (win);
385                         if (dimmed)
386                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_message_unableto_delete"));
387                 }
388         } 
389         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
390                 if (!dimmed) {
391                         dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW(win));
392                         if (dimmed) {
393                                 gchar *message = g_strdup_printf(_("mcen_nc_unable_to_delete_n_messages"), "1");
394                                 modest_dimming_rule_set_notification (rule, message);
395                                 g_free(message);
396                         }
397                 }
398         }
399
400         return dimmed;
401 }
402
403 gboolean 
404 modest_ui_dimming_rules_on_details (ModestWindow *win, gpointer user_data)
405 {
406         ModestDimmingRule *rule = NULL;
407         GtkWidget *header_view = NULL;
408         gboolean dimmed = FALSE;
409
410         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
411         rule = MODEST_DIMMING_RULE (user_data);
412         
413         /* main window dimming rules */
414         if (MODEST_IS_MAIN_WINDOW(win)) {
415                                 
416                 /* Check dimmed rule */
417                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
418                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
419                 
420                 /* If the header view has the focus: */
421                 if (header_view && gtk_widget_is_focus (header_view)) {
422                         /* Check dimmed rule */ 
423                         if (!dimmed)
424                                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, user_data);
425                 }
426                 else {
427                         /* If the folder view has the focus: */
428                         GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
429                                 MODEST_WIDGET_TYPE_FOLDER_VIEW);
430                         if (folder_view && gtk_widget_is_focus (folder_view)) {
431                                 TnyFolderStore *folder_store
432                                         = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
433                                 if (folder_store) {
434                                         /* Only enable for folders, not accounts,
435                                          * though the UI spec is not clear about that.
436                                          * If we enable this for accounts then we must 
437                                          * add code to handle them in modest_ui_actions_on_details(). */
438                                         if (!TNY_IS_FOLDER(folder_store)) {
439                                                 dimmed = TRUE;
440                                                 modest_dimming_rule_set_notification (rule, "");
441                                         }
442
443                                         g_object_unref (folder_store);
444                                 }
445                         }
446
447                 }
448
449         /* msg view window dimming rules */
450         } else {
451
452                 /* Check dimmed rule */ 
453                 if (!dimmed) {
454                         dimmed = _msg_download_in_progress (MODEST_MSG_VIEW_WINDOW(win));
455                         if (dimmed)
456                                 modest_dimming_rule_set_notification (rule, "");
457                 }
458         }
459
460         return dimmed;
461 }
462
463
464 gboolean 
465 modest_ui_dimming_rules_on_mark_as_read_msg (ModestWindow *win, gpointer user_data)
466 {
467         ModestDimmingRule *rule = NULL;
468         TnyHeaderFlags flags;
469         gboolean dimmed = FALSE;
470
471         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
472         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
473         rule = MODEST_DIMMING_RULE (user_data);
474         
475         flags = TNY_HEADER_FLAG_SEEN; 
476
477         /* Check dimmed rule */ 
478         if (!dimmed) {
479                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
480         }
481         if (!dimmed) {
482                 dimmed = _selected_msg_marked_as (win, flags, FALSE);
483                 if (dimmed)
484                         modest_dimming_rule_set_notification (rule, "");
485         }       
486
487         return dimmed;
488 }
489
490 gboolean 
491 modest_ui_dimming_rules_on_mark_as_unread_msg (ModestWindow *win, gpointer user_data)
492 {
493         ModestDimmingRule *rule = NULL;
494         TnyHeaderFlags flags;
495         gboolean dimmed = FALSE;
496
497         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
498         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
499         rule = MODEST_DIMMING_RULE (user_data);
500         
501         flags = TNY_HEADER_FLAG_SEEN; 
502
503         /* Check dimmed rule */ 
504         if (!dimmed)
505                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
506         if (!dimmed) {
507                 dimmed = _selected_msg_marked_as (win, flags, TRUE);
508                 if (dimmed)
509                         modest_dimming_rule_set_notification (rule, "");
510         }
511
512         return dimmed;
513 }
514
515 gboolean 
516 modest_ui_dimming_rules_on_move_to (ModestWindow *win, gpointer user_data)
517 {
518         ModestDimmingRule *rule = NULL;
519         gboolean dimmed = FALSE;
520
521         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
522         rule = MODEST_DIMMING_RULE (user_data);
523
524         if (MODEST_IS_MAIN_WINDOW (win)) 
525                 dimmed = modest_ui_dimming_rules_on_main_window_move_to (win, user_data);
526         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) 
527                  dimmed = modest_ui_dimming_rules_on_view_window_move_to (win, user_data);
528
529         return dimmed;
530 }
531
532
533 gboolean 
534 modest_ui_dimming_rules_on_main_window_move_to (ModestWindow *win, gpointer user_data)
535 {
536         GtkWidget *folder_view = NULL;
537         GtkWidget *header_view = NULL;
538         ModestDimmingRule *rule = NULL;
539         const gchar *message = NULL;
540         gboolean dimmed = FALSE;
541         
542         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), TRUE);
543         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
544         rule = MODEST_DIMMING_RULE (user_data);
545         
546         /* Get the folder view */
547         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
548                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
549
550         /* Get header view */
551         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
552                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
553         
554         /* Check diming rules for folders and messages transfer  */
555         if (!dimmed) {
556                 dimmed = _selected_folder_not_writeable(MODEST_MAIN_WINDOW(win));
557                 if (dimmed) {
558                         if (gtk_widget_is_focus (folder_view))
559                                 message = _("mail_in_ui_folder_move_target_error");
560                         else
561                                 message = ngettext ("mail_in_ui_folder_move_target_error", "mail_in_ui_folder_move_targets_error",
562                                                     modest_header_view_count_selected_headers (MODEST_HEADER_VIEW(header_view)) > 1);                                   
563                 }
564         }               
565         if (!dimmed) {
566                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
567                 
568         }
569         
570         return dimmed;
571 }
572
573 gboolean 
574 modest_ui_dimming_rules_on_view_window_move_to (ModestWindow *win, gpointer user_data)
575 {
576         ModestDimmingRule *rule = NULL;
577         gboolean dimmed = FALSE;
578
579         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
580         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
581         rule = MODEST_DIMMING_RULE (user_data);
582
583         /* Check dimmed rule */ 
584         if (!dimmed) {
585                 dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW(win));
586                 if (dimmed) 
587                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_move_mail_attachment"));
588                 
589         }
590
591         return dimmed;
592 }
593
594 gboolean 
595 modest_ui_dimming_rules_on_paste_msgs (ModestWindow *win, gpointer user_data)
596 {
597         ModestDimmingRule *rule = NULL;
598         TnyFolderType types[3];
599         gboolean dimmed = FALSE;
600
601         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
602         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
603         rule = MODEST_DIMMING_RULE (user_data);
604
605         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
606         types[1] = TNY_FOLDER_TYPE_OUTBOX;
607         types[2] = TNY_FOLDER_TYPE_SENT;
608         
609         /* Check dimmed rule */ 
610         if (!dimmed) {
611                 dimmed = _clipboard_is_empty (win);
612                 if (dimmed)
613                         modest_dimming_rule_set_notification (rule, "");
614         }
615         if (!dimmed) {
616                 dimmed = _selected_folder_is_any_of_type (win, types, 3);
617                 if (dimmed)
618                         modest_dimming_rule_set_notification (rule, "");
619         }
620         if (!dimmed) {
621                 dimmed = _selected_folder_is_same_as_source (win);
622                 if (dimmed)
623                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_copy_samefolder"));
624         }
625
626         return dimmed;
627 }
628
629
630 gboolean 
631 modest_ui_dimming_rules_on_select_all (ModestWindow *win, gpointer user_data)
632 {
633         ModestDimmingRule *rule = NULL;
634         gboolean dimmed = FALSE;
635
636         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
637         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
638         rule = MODEST_DIMMING_RULE (user_data);
639
640         /* Check dimmed rule */ 
641         if (!dimmed) {
642                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
643                 if (dimmed)
644                         modest_dimming_rule_set_notification (rule, "");
645         }
646                 
647         return dimmed;
648 }
649
650 gboolean 
651 modest_ui_dimming_rules_on_view_attachments (ModestWindow *win, gpointer user_data)
652 {
653         ModestDimmingRule *rule = NULL;
654         gboolean dimmed = FALSE;
655
656         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
657         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
658         rule = MODEST_DIMMING_RULE (user_data);
659
660         /* Check dimmed rule */ 
661         if (!dimmed) 
662                 dimmed = _invalid_attach_selected (win, TRUE, TRUE, rule);                      
663                 
664         return dimmed;
665 }
666
667 gboolean 
668 modest_ui_dimming_rules_on_save_attachments (ModestWindow *win, gpointer user_data)
669 {
670         ModestDimmingRule *rule = NULL;
671         gboolean dimmed = FALSE;
672
673         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
674         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
675         rule = MODEST_DIMMING_RULE (user_data);
676
677         /* Check dimmed rule */ 
678         if (!dimmed) 
679                 dimmed = _invalid_attach_selected (win, FALSE, FALSE, rule);                    
680                 
681         return dimmed;
682 }
683
684 gboolean 
685 modest_ui_dimming_rules_on_remove_attachments (ModestWindow *win, gpointer user_data)
686 {
687         ModestDimmingRule *rule = NULL;
688         gboolean dimmed = FALSE;
689
690         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
691         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
692         rule = MODEST_DIMMING_RULE (user_data);
693
694         /* Check dimmed rule */ 
695         if (!dimmed) {
696                 dimmed = _invalid_attach_selected (win, TRUE, TRUE, rule);                      
697         }
698         if (!dimmed) {
699                 dimmed = _invalid_attachment_for_purge (win, rule);
700         }
701
702
703         return dimmed;
704 }
705
706 gboolean 
707 modest_ui_dimming_rules_on_undo (ModestWindow *win, gpointer user_data)
708 {
709         ModestDimmingRule *rule = NULL;
710         gboolean dimmed = FALSE;
711         
712         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
713         rule = MODEST_DIMMING_RULE (user_data);
714
715         /* Check dimmed rule */ 
716         if (!dimmed) {
717                 dimmed = _clipboard_is_empty (win); 
718                 if (dimmed)
719                         modest_dimming_rule_set_notification (rule, "");
720         }
721                                 
722         return dimmed;  
723 }
724
725 gboolean 
726 modest_ui_dimming_rules_on_cut (ModestWindow *win, gpointer user_data)
727 {
728         ModestDimmingRule *rule = NULL;
729         gboolean dimmed = FALSE;
730         
731         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
732         rule = MODEST_DIMMING_RULE (user_data);
733
734         /* Check common dimming rules */
735         if (!dimmed) {
736                 dimmed = _invalid_clipboard_selected (win);
737                 if (dimmed)
738                         modest_dimming_rule_set_notification (rule, "");
739         }
740
741         /* Check window specific dimming rules */
742         if (MODEST_IS_MAIN_WINDOW (win)) {
743                 if (!dimmed) {
744                         dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win));
745                         if (dimmed)
746                                 modest_dimming_rule_set_notification (rule, "");
747                 }
748                 if (!dimmed) { 
749                         dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
750                         if (dimmed)
751                                 modest_dimming_rule_set_notification (rule, "");
752                 }
753                 if (!dimmed) {
754                         dimmed = _selected_msg_sent_in_progress (win);
755                         if (dimmed)
756                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_cut_mess"));
757                 }
758         }
759         
760         return dimmed;
761 }
762
763 gboolean 
764 modest_ui_dimming_rules_on_copy (ModestWindow *win, gpointer user_data)
765 {
766         ModestDimmingRule *rule = NULL;
767         gboolean dimmed = FALSE;
768         
769         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
770         rule = MODEST_DIMMING_RULE (user_data);
771
772         /* Check common dimming rules */
773         if (!dimmed) {
774                 dimmed = _invalid_clipboard_selected (win);
775                 if (dimmed)
776                         modest_dimming_rule_set_notification (rule, "");
777         }
778         
779         /* Check window specific dimming rules */
780         if (MODEST_IS_MAIN_WINDOW (win)) {
781                 if (!dimmed) {
782                         dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
783                         if (dimmed)
784                                 modest_dimming_rule_set_notification (rule, "");
785                 }               
786                 if (!dimmed) {
787                         dimmed = _selected_msg_sent_in_progress (win);
788                         if (dimmed)
789                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_cut_mess"));
790                 }
791         }
792                 
793         return dimmed;
794 }
795
796 gboolean 
797 modest_ui_dimming_rules_on_view_previous (ModestWindow *win, gpointer user_data)
798 {
799         ModestDimmingRule *rule = NULL;
800         gboolean dimmed = FALSE;
801         
802         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
803         rule = MODEST_DIMMING_RULE (user_data);
804
805         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
806
807         /* Check dimmed rule */ 
808         if (!dimmed) {
809                 dimmed = modest_msg_view_window_first_message_selected (MODEST_MSG_VIEW_WINDOW(win));
810                 if (dimmed)
811                         modest_dimming_rule_set_notification (rule, "");
812         }               
813
814         return dimmed;
815 }
816
817 gboolean 
818 modest_ui_dimming_rules_on_view_next (ModestWindow *win, gpointer user_data)
819 {
820         ModestDimmingRule *rule = NULL;
821         gboolean dimmed = FALSE;
822
823         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
824         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
825         rule = MODEST_DIMMING_RULE (user_data);
826
827         /* Check dimmed rule */ 
828         if (!dimmed) {
829                 dimmed = modest_msg_view_window_last_message_selected (MODEST_MSG_VIEW_WINDOW(win));
830                 if (dimmed)
831                         modest_dimming_rule_set_notification (rule, "");
832         }               
833
834         return dimmed;
835 }
836
837
838 gboolean 
839 modest_ui_dimming_rules_on_tools_smtp_servers (ModestWindow *win, gpointer user_data)
840 {
841         const gboolean dimmed = 
842                 !modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), 
843                         TRUE);  
844                 
845         return dimmed;
846 }
847
848 gboolean 
849 modest_ui_dimming_rules_on_cancel_sending (ModestWindow *win, gpointer user_data)
850 {
851         gboolean dimmed = FALSE;
852
853         /* Check dimmed rule */ 
854         if (!dimmed) 
855                 dimmed = !_sending_in_progress (win);
856                 
857         return dimmed;
858 }
859
860 gboolean 
861 modest_ui_dimming_rules_on_send_receive (ModestWindow *win, gpointer user_data)
862 {
863         ModestDimmingRule *rule = NULL;
864         gboolean dimmed = FALSE;
865         
866         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
867         rule = MODEST_DIMMING_RULE (user_data);
868  
869         /* Check dimmed rule */ 
870         if (!dimmed) {
871                 dimmed = !modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), 
872                                                           TRUE);        
873                 if (dimmed)
874                         modest_dimming_rule_set_notification (rule, _("mcen_nc_no_email_acnts_defined"));
875         }
876
877         return dimmed;
878 }
879
880 gboolean
881 modest_ui_dimming_rules_on_add_to_contacts (ModestWindow *win, gpointer user_data)
882 {
883         ModestDimmingRule *rule = NULL;
884         gboolean dimmed = FALSE;
885
886         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
887         rule = MODEST_DIMMING_RULE (user_data);
888
889         /* Check dimmed rule */
890         if (!dimmed) {
891                 GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
892                 gchar *selection = NULL;
893                 selection = gtk_clipboard_wait_for_text (clipboard);
894
895                 dimmed = !((selection != NULL) && (modest_text_utils_validate_recipient (selection)));
896         }
897
898         return dimmed;
899 }
900
901 /* *********************** static utility functions ******************** */
902
903 static gboolean 
904 _marked_as_deleted (ModestWindow *win)
905 {
906         gboolean result = FALSE;
907         TnyHeaderFlags flags;
908
909         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
910         
911         flags = TNY_HEADER_FLAG_DELETED; 
912
913         /* Check dimmed rule */ 
914         result = _selected_msg_marked_as (win, flags, FALSE);
915         
916         return result;
917 }
918
919 static gboolean
920 _selected_folder_not_writeable (ModestMainWindow *win)
921 {
922         GtkWidget *folder_view = NULL;
923         TnyFolderStore *parent_folder = NULL;
924         ModestTnyFolderRules rules;
925         gboolean result = FALSE;
926
927         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
928
929         /* Get folder view */
930         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
931                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
932         /* If no folder view, always dimmed */
933         if (!folder_view)
934                 return TRUE;
935         
936         /* Get selected folder as parent of new folder to create */
937         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
938         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
939                 if (parent_folder)
940                         g_object_unref (parent_folder);
941                 return TRUE;
942         }
943         
944         /* Check dimmed rule */ 
945         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
946         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
947
948         /* free */
949         g_object_unref (parent_folder);
950
951         return result;
952 }
953
954 static gboolean
955 _selected_folder_is_root_or_inbox (ModestMainWindow *win)
956 {
957         TnyFolderType types[2];
958         gboolean result = FALSE;
959
960         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
961
962         types[0] = TNY_FOLDER_TYPE_ROOT; 
963         types[1] = TNY_FOLDER_TYPE_INBOX; 
964
965         /* Check folder type */
966         result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 2);
967
968         /* Check pop and MMC accounts */
969         if (!result) {
970                 result = _selected_folder_is_MMC_or_POP_root (win);
971         }
972                 
973         return result;
974 }
975
976
977 static gboolean
978 _selected_folder_is_root (ModestMainWindow *win)
979 {
980         TnyFolderType types[1];
981         gboolean result = FALSE;
982
983         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
984
985         types[0] = TNY_FOLDER_TYPE_ROOT; 
986
987         /* Check folder type */
988         result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 1);
989                 
990         /* Check pop and MMC accounts */
991         if (!result) {
992                 result = _selected_folder_is_MMC_or_POP_root (win);
993         }
994
995         return result;
996 }
997
998 static gboolean
999 _selected_folder_is_MMC_or_POP_root (ModestMainWindow *win)
1000 {
1001         GtkWidget *folder_view = NULL;
1002         TnyFolderStore *parent_folder = NULL;
1003         gboolean result = FALSE;
1004
1005         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1006                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1007         /* Get selected folder as parent of new folder to create */
1008         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1009         if (!parent_folder)
1010                 return TRUE;
1011         
1012         if (TNY_IS_ACCOUNT (parent_folder)) {
1013                 /* If it's the local account then do not dim */
1014                 if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
1015                         result = FALSE;
1016                 } else {
1017                                 /* If it's the MMC root folder then dim it */
1018                         if (!strcmp (tny_account_get_id (TNY_ACCOUNT (parent_folder)), MODEST_MMC_ACCOUNT_ID)) {
1019                                         result = TRUE;
1020                         } else {
1021                                 const gchar *proto_str = tny_account_get_proto (TNY_ACCOUNT (parent_folder));
1022                                 /* If it's POP then dim */
1023                                 result = (modest_protocol_info_get_transport_store_protocol (proto_str) == 
1024                                                   MODEST_PROTOCOL_STORE_POP) ? TRUE : FALSE;
1025                         }
1026                 }
1027         }
1028         g_object_unref (parent_folder);
1029
1030         return result;
1031 }
1032
1033
1034
1035
1036 static gboolean
1037 _selected_folder_is_empty (ModestMainWindow *win)
1038 {
1039         GtkWidget *folder_view = NULL;
1040         TnyFolderStore *folder = NULL;
1041         gboolean result = FALSE;
1042
1043         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1044
1045         /* Get folder view */
1046         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1047                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1048         /* If no folder view, always dimmed */
1049         if (!folder_view)
1050                 return TRUE;
1051         
1052         /* Get selected folder as parent of new folder to create */
1053         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1054         if (!(folder && TNY_IS_FOLDER(folder))) {
1055                 if (folder)
1056                         g_object_unref (folder);
1057                 return TRUE;
1058         }
1059         
1060         /* Check folder type */
1061         result = tny_folder_get_all_count (TNY_FOLDER (folder)) == 0;
1062
1063         /* free */
1064         g_object_unref (folder);
1065
1066         return result;
1067 }
1068
1069 static gboolean
1070 _selected_folder_is_same_as_source (ModestWindow *win)
1071 {
1072         ModestEmailClipboard *clipboard = NULL;
1073         GtkWidget *folder_view = NULL;
1074         TnyFolderStore *folder = NULL;
1075         gboolean result = FALSE;
1076
1077         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1078
1079         /* Get folder view */
1080         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1081                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1082         if (!folder_view)
1083                 return FALSE;
1084         
1085         /* Get selected folder as destination folder */
1086         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1087         if (!(folder && TNY_IS_FOLDER(folder))) {
1088                 result = FALSE;
1089                 goto frees;
1090         }
1091         
1092         /* Check clipboard is cleared */
1093         clipboard = modest_runtime_get_email_clipboard ();
1094         if (modest_email_clipboard_cleared (clipboard)) {
1095                 result = FALSE;
1096                 goto frees;
1097         }
1098                 
1099         /* Check source folder */
1100         result = modest_email_clipboard_check_source_folder (clipboard, 
1101                                                              (const TnyFolder *) folder);
1102         
1103         /* Free */
1104  frees:
1105         g_object_unref (folder);
1106         
1107         return result;
1108 }
1109
1110 static gboolean
1111 _selected_folder_is_any_of_type (ModestWindow *win,
1112                                  TnyFolderType types[], 
1113                                  guint ntypes)
1114 {
1115         GtkWidget *folder_view = NULL;
1116         TnyFolderStore *folder = NULL;
1117         TnyFolderType folder_type;
1118         guint i=0;
1119         gboolean result = FALSE;
1120
1121         /*Get curent folder */
1122         if (MODEST_IS_MAIN_WINDOW(win)) {
1123
1124                 /* Get folder view */
1125                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1126                                                                    MODEST_WIDGET_TYPE_FOLDER_VIEW);
1127                 /* If no folder view, always dimmed */
1128                 if (!folder_view)
1129                         return TRUE;
1130         
1131                 /* Get selected folder as parent of new folder to create */
1132                 folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
1133
1134                 if (!(folder && TNY_IS_FOLDER(folder))) {
1135                         if (folder)
1136                                 g_object_unref (folder);
1137                         return TRUE;
1138                 }
1139                 
1140                 /* Check folder type */
1141                 result = _folder_is_any_of_type (TNY_FOLDER(folder), types, ntypes);
1142         }
1143         else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
1144                 folder_type = modest_msg_view_window_get_folder_type (MODEST_MSG_VIEW_WINDOW (win));
1145                 for (i=0; i < ntypes; i++) {
1146                         result = result || folder_type == types[i];
1147                 }
1148         }
1149
1150
1151         /* free */
1152         g_object_unref (folder);
1153
1154         return result;  
1155 }
1156
1157 static gboolean
1158 _folder_is_any_of_type (TnyFolder *folder,
1159                         TnyFolderType types[], 
1160                         guint ntypes)
1161 {
1162         TnyFolderType folder_type;
1163         gboolean result = FALSE;
1164         guint i;
1165
1166         g_return_val_if_fail (TNY_IS_FOLDER(folder), FALSE);
1167
1168         /* Get folder type */
1169         if (modest_tny_folder_is_local_folder (folder))
1170                 folder_type = modest_tny_folder_get_local_folder_type (folder);         
1171         else 
1172                 folder_type = modest_tny_folder_guess_folder_type (folder);             
1173         
1174         /* Check foler type */
1175         for (i=0; i < ntypes; i++) {
1176                 result = result || folder_type == types[i];
1177         }
1178
1179         return result;
1180 }
1181
1182 static gboolean
1183 _clipboard_is_empty (ModestWindow *win)
1184 {
1185         gboolean result = FALSE;
1186         
1187         if (MODEST_IS_MAIN_WINDOW (win)) {
1188                 ModestEmailClipboard *clipboard = NULL;
1189                 clipboard = modest_runtime_get_email_clipboard ();
1190                 if (modest_email_clipboard_cleared (clipboard)) 
1191                  result = TRUE;  
1192         }
1193
1194         return result;
1195 }
1196
1197 static gboolean
1198 _invalid_clipboard_selected (ModestWindow *win)
1199 {
1200         gboolean result = FALSE;
1201
1202         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1203                 GtkClipboard *clipboard = NULL;
1204                 gchar *selection = NULL;
1205                 GtkWidget *focused = NULL;
1206
1207                 /* Get clipboard selection*/
1208                 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
1209                 selection = gtk_clipboard_wait_for_text (clipboard);
1210
1211                 /* Get focuesed widget */
1212                 focused = gtk_window_get_focus (GTK_WINDOW (win));
1213
1214                 /* Check dimming */
1215                 result = ((selection == NULL) || 
1216                           (MODEST_IS_ATTACHMENTS_VIEW (focused)));
1217         }               
1218         else if (MODEST_IS_MAIN_WINDOW (win)) {
1219                 GtkWidget *header_view = NULL;
1220
1221                 /* Get header view to check selected messages */
1222                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1223                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
1224         
1225                 /* Check dimming */
1226                 result = !modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view));            
1227         }
1228         
1229         return result;
1230 }
1231
1232
1233 static gboolean
1234 _invalid_attach_selected (ModestWindow *win,
1235                           gboolean unique,
1236                           gboolean for_view,
1237                           ModestDimmingRule *rule) 
1238 {
1239         GList *attachments, *node;
1240         gint n_selected;
1241         TnyHeaderFlags flags;
1242         gboolean nested_attachments = FALSE;
1243         gboolean selected_messages = FALSE;
1244         gboolean result = FALSE;
1245
1246         if (MODEST_IS_MAIN_WINDOW (win)) {
1247                 flags = TNY_HEADER_FLAG_ATTACHMENTS;
1248                 if (!result)
1249                         result = _selected_msg_marked_as (win, flags, TRUE);            
1250         }
1251         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1252                 
1253                 /* Get selected atachments */
1254                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
1255                 n_selected = g_list_length (attachments);
1256
1257                 /* Check unique */              
1258                 if (!result) {
1259                         if (unique) 
1260                                 result = n_selected != 1;
1261                         else
1262                                 
1263                                 result = n_selected < 1;
1264                 }
1265                 
1266                 /* Check attached type (view operation not required) */
1267                 if (!result && !for_view)  {
1268                         for (node = attachments; node != NULL && !result; node = g_list_next (node)) {
1269                                 TnyMimePart *mime_part = TNY_MIME_PART (node->data);
1270                                 TnyList *nested_list = tny_simple_list_new ();
1271                                 if (TNY_IS_MSG (mime_part)) {
1272                                         selected_messages = TRUE;
1273                                         result = TRUE;
1274                                 }
1275                                 tny_mime_part_get_parts (mime_part, nested_list);
1276                                 if (tny_list_get_length (nested_list) > 0) {
1277                                 nested_attachments = TRUE;
1278                                 result = TRUE;
1279                                 }
1280                                 g_object_unref (nested_list);
1281                         }
1282                 }
1283                 
1284                 /* Set notifications */
1285                 if (result && rule != NULL) {
1286                         if (selected_messages) {
1287                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_save_attach_mail"));
1288                         } else if (nested_attachments) {
1289                                 modest_dimming_rule_set_notification (rule, _("FIXME:unable to save attachments with nested elements"));
1290                         } else if (n_selected == 0) {
1291                                 modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
1292                         } else if (unique) {
1293                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_display_more"));
1294                         }
1295                 }
1296                 
1297                 /* Free */
1298                 g_list_free (attachments);
1299         }
1300
1301         return result;
1302 }
1303
1304 static gboolean
1305 _invalid_msg_selected (ModestMainWindow *win,
1306                        gboolean unique,
1307                        ModestDimmingRule *rule) 
1308 {
1309         GtkWidget *header_view = NULL;          
1310         GtkWidget *folder_view = NULL;
1311         gboolean selected_headers = FALSE;
1312         gboolean result = FALSE;
1313
1314         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1315         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (rule), FALSE);
1316                 
1317         /* Get header view to check selected messages */
1318         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1319                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1320         
1321         /* Get folder view to check focus */
1322         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
1323                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
1324
1325         /* Get selected headers */
1326         selected_headers = modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view));
1327
1328         /* Check dimmed rule (TODO: check focus on widgets */   
1329         if (!result) {
1330                 result = ((!selected_headers) ||
1331                           (gtk_widget_is_focus (folder_view)));
1332                 if (result)
1333                         modest_dimming_rule_set_notification (rule, _("mcen_ib_no_message_selected"));
1334         }
1335         if (!result && unique) {
1336                 result = modest_header_view_count_selected_headers (MODEST_HEADER_VIEW(header_view)) > 1;
1337                 if (result)
1338                         modest_dimming_rule_set_notification (rule, _("mcen_ib_select_one_message"));
1339         }
1340
1341         return result;
1342 }
1343
1344 static gboolean
1345 _already_opened_msg (ModestWindow *win,
1346                      guint *n_messages)
1347 {
1348         ModestWindow *window = NULL;
1349         ModestWindowMgr *mgr = NULL;
1350         GtkWidget *header_view = NULL;          
1351         TnyList *selected_headers = NULL;
1352         TnyIterator *iter = NULL;
1353         TnyHeader *header = NULL;
1354         gboolean result = TRUE;
1355
1356         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1357                 
1358         /* Get header view to check selected messages */
1359         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1360                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1361
1362
1363         /* Check no selection */
1364         if (!modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view)))
1365             return FALSE;
1366             
1367         /* Get selected headers */
1368         selected_headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
1369         if (selected_headers == NULL) 
1370                 return FALSE;
1371
1372         *n_messages = tny_list_get_length (selected_headers);
1373
1374         /* Check dimmed rule (TODO: check focus on widgets */   
1375         mgr = modest_runtime_get_window_mgr ();
1376         iter = tny_list_create_iterator (selected_headers);
1377         while (!tny_iterator_is_done (iter) && result) {
1378                 header = TNY_HEADER (tny_iterator_get_current (iter));
1379                 window = modest_window_mgr_find_window_by_header (mgr, header);
1380                 result = result && (window != NULL);
1381                         
1382                 g_object_unref (header);
1383                 tny_iterator_next (iter);
1384         }
1385         
1386         /* free */
1387         if (selected_headers != NULL) 
1388                 g_object_unref (selected_headers);
1389         if (iter != NULL)
1390                 g_object_unref (iter);
1391                 
1392         return result;
1393 }
1394
1395 static gboolean
1396 _selected_msg_marked_as (ModestWindow *win, 
1397                          TnyHeaderFlags mask, 
1398                          gboolean opposite)
1399 {
1400         GtkWidget *header_view = NULL;
1401         TnyList *selected_headers = NULL;
1402         TnyIterator *iter = NULL;
1403         TnyHeader *header = NULL;
1404         TnyHeaderFlags flags;
1405         gboolean result = FALSE;
1406
1407         /* Get header view to check selected messages */
1408         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1409                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1410
1411         /* Check no selection */
1412         if (!modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view)))
1413             return TRUE;
1414
1415         /* Get selected headers */
1416         selected_headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
1417         if (selected_headers == NULL) 
1418                 return TRUE;
1419         
1420         /* Call the function for each header */
1421         iter = tny_list_create_iterator (selected_headers);
1422         while (!tny_iterator_is_done (iter) && !result) {
1423                 header = TNY_HEADER (tny_iterator_get_current (iter));
1424
1425                 flags = tny_header_get_flags (header);
1426                 if (opposite)
1427                         result = (flags & mask) == 0; 
1428                 else
1429                         result = (flags & mask) != 0; 
1430
1431                 g_object_unref (header);
1432                 tny_iterator_next (iter);
1433         }
1434
1435         /* free */
1436         if (selected_headers != NULL) 
1437                 g_object_unref (selected_headers);
1438         if (iter != NULL)
1439                 g_object_unref (iter);
1440
1441         return result;
1442 }
1443
1444 static gboolean
1445 _msg_download_in_progress (ModestMsgViewWindow *win)
1446 {
1447         gboolean result = FALSE;
1448
1449         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), FALSE);
1450
1451         result = modest_msg_view_window_toolbar_on_transfer_mode (win);
1452
1453         return result;
1454 }
1455
1456 static gboolean
1457 _msg_download_completed (ModestMainWindow *win)
1458 {
1459         GtkWidget *header_view = NULL;
1460         TnyList *selected_headers = NULL;
1461         TnyIterator *iter = NULL;
1462         TnyHeader *header = NULL;
1463         TnyHeaderFlags flags;
1464         gboolean result = FALSE;
1465
1466         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (win), FALSE);
1467
1468
1469         /* Get header view to check selected messages */
1470         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1471                                                            MODEST_WIDGET_TYPE_HEADER_VIEW);
1472
1473         /* Check no selection */
1474         if (!modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view)))
1475             return TRUE;
1476
1477         /* Get selected headers */
1478         selected_headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
1479         if (selected_headers == NULL) 
1480                 return TRUE;
1481
1482         /* Check dimmed rule  */        
1483         result = TRUE;
1484         iter = tny_list_create_iterator (selected_headers);
1485         while (!tny_iterator_is_done (iter) && result) {
1486                 header = TNY_HEADER (tny_iterator_get_current (iter));
1487                         
1488                 flags = tny_header_get_flags (header);
1489                 /* TODO: is this the right flag?, it seems that some
1490                    headers that have been previously downloaded do not
1491                    come with it */
1492                 result = (flags & TNY_HEADER_FLAG_CACHED);
1493
1494                 g_object_unref (header);
1495                 tny_iterator_next (iter);
1496         }
1497
1498         g_object_unref (iter);
1499
1500         return result;
1501 }
1502
1503 static gboolean
1504 _selected_msg_sent_in_progress (ModestWindow *win)
1505 {
1506         ModestTnySendQueue *send_queue = NULL;
1507         GtkWidget *header_view = NULL;
1508         ModestTnyAccountStore *acc_store = NULL;
1509         TnyAccount *account = NULL;
1510         TnyList *header_list = NULL;
1511         TnyIterator *iter = NULL;
1512         TnyHeader *header = NULL;
1513         const gchar *account_name = NULL;
1514         gboolean result = FALSE;
1515         gchar *id = NULL;
1516         
1517         /* Get transport account */
1518         acc_store = modest_runtime_get_account_store();
1519         account_name = modest_window_get_active_account (win);
1520         
1521         /* If no account defined, this action must be always dimmed  */
1522         if (account_name == NULL) return FALSE;
1523         account = modest_tny_account_store_get_transport_account_for_open_connection (acc_store, account_name);
1524         if (!TNY_IS_TRANSPORT_ACCOUNT (account)) return FALSE;
1525
1526         /* Get send queue for current ransport account */
1527         send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(account));
1528         g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE (send_queue), FALSE);
1529
1530         if (MODEST_IS_MAIN_WINDOW(win)) {
1531                 
1532                 /* Get header view to check selected messages */
1533                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1534                                                                    MODEST_WIDGET_TYPE_HEADER_VIEW);
1535                 
1536                 /* Check no selection */
1537                 if (!modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view)))
1538                     return FALSE;
1539
1540                 /* Get selected headers */
1541                 header_list = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
1542
1543                 /* Get message header */
1544                 if (!header_list) return FALSE;
1545                 iter = tny_list_create_iterator (header_list);
1546                 header = TNY_HEADER (tny_iterator_get_current (iter));
1547
1548                 /* Get message id */
1549                 id = g_strdup(tny_header_get_message_id (header));
1550                 
1551         } else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
1552                 
1553                 /* Get message header */
1554                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW(win));
1555
1556                 /* Get message id */
1557                 id = g_strdup(tny_header_get_message_id (header));
1558         }
1559
1560         /* Check if msg id is being processed inside send queue */
1561         result = modest_tny_send_queue_msg_is_being_sent (send_queue, id);
1562
1563         /* Free */
1564         g_free(id);
1565         g_object_unref (header);
1566         g_object_unref(header_list);
1567         g_object_unref(iter);
1568
1569         return result;
1570 }
1571
1572
1573 static gboolean
1574 _sending_in_progress (ModestWindow *win)
1575 {
1576         ModestTnySendQueue *send_queue = NULL;
1577         ModestTnyAccountStore *acc_store = NULL;
1578         TnyAccount *account = NULL;
1579         const gchar *account_name = NULL;
1580         gboolean result = FALSE;
1581         
1582         /* Get transport account */
1583         acc_store = modest_runtime_get_account_store();
1584         account_name = modest_window_get_active_account (win);
1585
1586         /* If no account defined, this action must be always dimmed  */
1587         if (account_name == NULL) return FALSE;
1588         account = modest_tny_account_store_get_transport_account_for_open_connection (acc_store, account_name);
1589         if (!TNY_IS_TRANSPORT_ACCOUNT (account)) return FALSE;
1590
1591         /* Get send queue for current ransport account */
1592         send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(account));
1593         g_return_val_if_fail (MODEST_IS_TNY_SEND_QUEUE (send_queue), FALSE);
1594
1595         /* Check if send queue is perfimring any send operation */
1596         result = modest_tny_send_queue_sending_in_progress (send_queue);
1597
1598         return result;
1599 }
1600
1601 static gboolean
1602 _invalid_attachment_for_purge (ModestWindow *win, 
1603                                ModestDimmingRule *rule)
1604 {
1605         TnyMsg *msg = NULL;
1606         TnyFolder *folder = NULL;
1607         TnyAccount *account = NULL;
1608         gboolean result = TRUE;
1609
1610         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1611
1612                 /* Get folder and account of message */
1613                 msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
1614                 g_return_val_if_fail(msg != NULL, TRUE);                        
1615                 folder = tny_msg_get_folder (msg);              
1616                 if (folder == NULL) {
1617                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
1618                         goto frees;                     
1619                 }
1620                 account = modest_tny_folder_get_account (folder);
1621                 if (account == NULL) goto frees;                        
1622                 
1623                 /* Check account */
1624                 if (!modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (account))) {
1625                         const gchar *proto_str = tny_account_get_proto (TNY_ACCOUNT (account));
1626                         /* If it's POP then dim */
1627                         if (modest_protocol_info_get_transport_store_protocol (proto_str) == 
1628                             MODEST_PROTOCOL_STORE_POP) {
1629                                 GList *attachments;
1630                                 gint n_selected;
1631                                 result = TRUE;
1632                                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
1633                                 n_selected = g_list_length (attachments);
1634                                 g_list_free (attachments);
1635                                 
1636                                 modest_dimming_rule_set_notification (rule, 
1637                                                                       ngettext ("mail_ib_unable_to_pure_attach_pop_mail_singular",
1638                                                                                 "mail_ib_unable_to_pure_attach_pop_mail_plural", 
1639                                                                                 n_selected));
1640                         }
1641                 }
1642         }
1643
1644  frees:
1645         if (msg != NULL)
1646                 g_object_unref (msg);
1647         if (folder != NULL)
1648                 g_object_unref (folder);
1649         if (account != NULL)
1650                 g_object_unref (account);
1651         
1652         return result;
1653 }
1654