Do ignore empty recipients when checking names
[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-ui-actions.h"
37 #include "modest-account-mgr-helpers.h"
38 #include "modest-dimming-rule.h"
39 #include "modest-debug.h"
40 #include "modest-tny-folder.h"
41 #include "modest-tny-account.h"
42 #include "modest-tny-msg.h"
43 #include "modest-tny-mime-part.h"
44 #include "modest-text-utils.h"
45 #include "widgets/modest-folder-view.h"
46 #include "modest-address-book.h"
47 #include <widgets/modest-attachments-view.h>
48 #include <modest-runtime.h>
49 #include <tny-simple-list.h>
50 #include <tny-merge-folder.h>
51 #include <widgets/modest-recpt-editor.h>
52 #include <gtkhtml/gtkhtml.h>
53 #include <modest-runtime.h>
54 #ifdef MODEST_TOOLKIT_HILDON2
55 #include <modest-header-window.h>
56 #include <modest-folder-window.h>
57 #endif
58
59
60 static gboolean _folder_is_any_of_type (TnyFolder *folder, TnyFolderType types[], guint ntypes);
61 static gboolean _invalid_msg_selected (ModestMainWindow *win, gboolean unique, ModestDimmingRule *rule);
62 static gboolean _invalid_attach_selected (ModestWindow *win, 
63                                           gboolean unique, gboolean for_view, gboolean for_remove,
64                                           ModestDimmingRule *rule);
65 static gboolean _purged_attach_selected (ModestWindow *win, gboolean all, ModestDimmingRule *rule);
66 static gboolean _clipboard_is_empty (ModestWindow *win);
67 static gboolean _invalid_clipboard_selected (ModestWindow *win, ModestDimmingRule *rule);
68 static gboolean _selected_folder_not_writeable (ModestMainWindow *win, gboolean for_paste);
69 static gboolean _selected_folder_not_moveable (ModestMainWindow *win);
70 static gboolean _selected_folder_not_renameable (ModestMainWindow *win);
71 static gboolean _selected_folder_not_deletable (ModestMainWindow *win);
72 static gboolean _selected_folder_is_any_of_type (ModestWindow *win, TnyFolderType types[], guint ntypes);
73 static gboolean _selected_folder_is_root_or_inbox (ModestMainWindow *win);
74 static gboolean _selected_folder_is_root (ModestMainWindow *win);
75 static gboolean _header_view_is_all_selected (ModestMainWindow *win);
76 static gboolean _selected_folder_is_empty (ModestMainWindow *win);
77 static gboolean _folder_view_has_focus (ModestWindow *win);
78 static gboolean _selected_folder_is_same_as_source (ModestWindow *win);
79 static gboolean _msg_download_in_progress (ModestWindow *win);
80 static gboolean _msg_download_completed (ModestMainWindow *win);
81 static gboolean _selected_msg_sent_in_progress (ModestWindow *win);
82 static gboolean _invalid_folder_for_purge (ModestWindow *win, ModestDimmingRule *rule);
83 static gboolean _transfer_mode_enabled (ModestWindow *win);
84 static gboolean _selected_folder_has_subfolder_with_same_name (ModestWindow *win);
85 static void fill_list_of_caches (gpointer key, gpointer value, gpointer userdata);
86 static gboolean _send_receive_in_progress (ModestWindow *win);
87 static gboolean _msgs_send_in_progress (void);
88 static gboolean _all_msgs_in_sending_status (ModestHeaderView *header_view) G_GNUC_UNUSED;
89 static gboolean _forbid_outgoing_xfers (ModestWindow *window);
90
91 static DimmedState *
92 _define_main_window_dimming_state (ModestMainWindow *window)
93 {
94         DimmedState *state = NULL;
95         GtkWidget *focused_widget = NULL;
96         GtkWidget *header_view = NULL;
97         GtkTreeModel *model = NULL;
98         TnyList *selected_headers = NULL;
99         TnyIterator *iter = NULL;
100         TnyHeader *header = NULL;
101         ModestCacheMgr *cache_mgr = NULL;
102         GHashTable *send_queue_cache = NULL;
103         ModestTnySendQueue *send_queue = NULL;
104         GSList *send_queues = NULL, *node = NULL;
105         ModestWindowMgr *mgr = NULL;
106         gboolean found = FALSE;
107         gchar *msg_uid = NULL;
108         TnyHeaderFlags flags;
109         gboolean all_deleted = TRUE;
110         gboolean all_seen = TRUE;
111         gboolean all_cached = TRUE;
112         gboolean all_has_attach = TRUE;
113         TnyFolder *folder = NULL;
114
115         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(window), NULL);
116
117         /* Init state */
118         state = g_slice_new0 (DimmedState);
119         state->n_selected = 0;
120         state->already_opened_msg = 0;
121         state->any_marked_as_deleted = FALSE;
122         state->all_marked_as_deleted = FALSE;
123         state->any_marked_as_seen = FALSE;
124         state->all_marked_as_seen = FALSE;
125         state->any_marked_as_cached = FALSE;
126         state->all_marked_as_cached = FALSE;
127         state->any_has_attachments = FALSE;
128         state->all_has_attachments = FALSE;
129         state->sent_in_progress = FALSE;
130         state->all_selected = FALSE;
131
132         /* Get focused widget */
133         focused_widget = gtk_window_get_focus (GTK_WINDOW (window));
134         if (MODEST_IS_FOLDER_VIEW (focused_widget)) {
135                 state->n_selected++;
136                 return state;
137         } else if (MODEST_IS_HEADER_VIEW (focused_widget)) {
138                 header_view = focused_widget;           
139         } else {
140                 header_view = modest_main_window_get_child_widget (window, MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
141         }
142         
143         /* Get header view and selected headers */
144         selected_headers = modest_header_view_get_selected_headers (MODEST_HEADER_VIEW(header_view));
145         if (!selected_headers) 
146                 return state;
147
148         /* Examine selected headers */
149         iter = tny_list_create_iterator (selected_headers);
150         while (!tny_iterator_is_done (iter)) {
151                 header = TNY_HEADER (tny_iterator_get_current (iter));
152                 flags = tny_header_get_flags (header);
153                 
154                 /* No selected */
155                 state->n_selected++;
156                 
157                 /* Already opened */
158                 mgr = modest_runtime_get_window_mgr ();
159                 if (modest_window_mgr_find_registered_header (mgr, header, NULL))
160                         state->already_opened_msg++;
161                 
162
163                 /* Mark as deleted */           
164                 all_deleted = all_deleted && (flags & TNY_HEADER_FLAG_DELETED);
165                 state->all_marked_as_deleted = all_deleted;
166                 if (!state->any_marked_as_deleted)
167                         state->any_marked_as_deleted = flags & TNY_HEADER_FLAG_DELETED;
168                 
169                 /* Mark as seen */
170                 all_seen = all_seen && (flags & TNY_HEADER_FLAG_SEEN);
171                 state->all_marked_as_seen = all_seen;
172                 if (!state->any_marked_as_seen)
173                         state->any_marked_as_seen = flags & TNY_HEADER_FLAG_SEEN;
174                 
175                 /* Mark as cached */
176                 all_cached = all_cached && (flags & TNY_HEADER_FLAG_CACHED);
177                 state->all_marked_as_cached = all_cached;
178                 if (!state->any_marked_as_cached)
179                         state->any_marked_as_cached = flags & TNY_HEADER_FLAG_CACHED;
180                 
181                 /* Mark has_attachments */
182                 all_has_attach = all_has_attach && (flags & TNY_HEADER_FLAG_ATTACHMENTS);
183                 state->all_has_attachments = all_has_attach;
184                 if (!state->any_has_attachments)
185                         state->any_has_attachments = flags & TNY_HEADER_FLAG_ATTACHMENTS;
186         
187                 /* sent in progress */
188                 folder = tny_header_get_folder (header);
189                 if (folder) {
190                         if (modest_tny_folder_guess_folder_type (folder) == TNY_FOLDER_TYPE_OUTBOX) {
191                                 msg_uid = modest_tny_send_queue_get_msg_id (header);
192                                 if (!state->sent_in_progress) {
193                                         cache_mgr = modest_runtime_get_cache_mgr ();
194                                         send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
195                                                                                        MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
196                         
197                                         g_hash_table_foreach (send_queue_cache, (GHFunc) fill_list_of_caches, &send_queues);
198                                         
199                                         for (node = send_queues; node != NULL && !found; node = g_slist_next (node)) {
200                                                 send_queue = MODEST_TNY_SEND_QUEUE (node->data);
201                                                 
202                                                 /* Check if msg uid is being processed inside send queue */
203                                                 found = modest_tny_send_queue_msg_is_being_sent (send_queue, msg_uid);          
204                                         }
205                                         state->sent_in_progress = found;
206                                 }
207                         }
208                         g_object_unref (folder);
209                 }
210                 tny_iterator_next (iter);
211                 g_object_unref (header);
212         }
213
214         /* check if all the headers are selected or not */
215         model = gtk_tree_view_get_model(GTK_TREE_VIEW(header_view));
216         if (model != NULL){
217                 gint count;
218                 count = gtk_tree_model_iter_n_children(model, NULL);
219                 if(state->n_selected == count)
220                         state->all_selected = TRUE;
221         }
222
223         /* Free */
224         g_free(msg_uid);
225         g_object_unref(selected_headers);
226         g_object_unref(iter);
227         g_slist_free (send_queues);
228         
229         return state;
230 }
231
232 static DimmedState *
233 _define_msg_view_window_dimming_state (ModestMsgViewWindow *window)
234 {
235         DimmedState *state = NULL;
236         TnyHeader *header = NULL;
237         ModestCacheMgr *cache_mgr = NULL;
238         GHashTable *send_queue_cache = NULL;
239         ModestTnySendQueue *send_queue = NULL;
240         GSList *send_queues = NULL, *node = NULL;
241         gboolean found = FALSE;
242         gchar *msg_uid = NULL;
243         TnyHeaderFlags flags;
244         gboolean all_deleted = TRUE;
245         gboolean all_seen = TRUE;
246         gboolean all_cached = TRUE;
247         gboolean all_has_attach = TRUE;
248                         
249         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(window), NULL);
250
251         /* Init state */
252         state = g_slice_new0 (DimmedState);
253         state->n_selected = 0;
254         state->already_opened_msg = 0;
255         state->any_marked_as_deleted = FALSE;
256         state->all_marked_as_deleted = FALSE;
257         state->any_marked_as_seen = FALSE;
258         state->all_marked_as_seen = FALSE;
259         state->any_marked_as_cached = FALSE;
260         state->all_marked_as_cached = FALSE;
261         state->any_has_attachments = FALSE;
262         state->all_has_attachments = FALSE;
263         state->sent_in_progress = FALSE;
264
265         header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW(window));
266         if (header == NULL)
267                 return state;
268         g_return_val_if_fail (TNY_IS_HEADER(header), state);
269         flags = tny_header_get_flags (header);
270
271         /* Selected */
272         state->n_selected++;
273
274         /* Mark as deleted */           
275         all_deleted = all_deleted && (flags & TNY_HEADER_FLAG_DELETED);
276         state->all_marked_as_deleted = all_deleted;
277         if (!state->any_marked_as_deleted)
278                 state->any_marked_as_deleted = flags & TNY_HEADER_FLAG_DELETED;
279         
280         /* Mark as seen */
281         all_seen = all_seen && (flags & TNY_HEADER_FLAG_SEEN);
282         state->all_marked_as_seen = all_seen;
283         if (!state->any_marked_as_seen)
284                 state->any_marked_as_seen = flags & TNY_HEADER_FLAG_SEEN;
285         
286         /* Mark as cached */
287         all_cached = all_cached && (flags & TNY_HEADER_FLAG_CACHED);
288         state->all_marked_as_cached = all_cached;
289         if (!state->any_marked_as_cached)
290                 state->any_marked_as_cached = flags & TNY_HEADER_FLAG_CACHED;
291         
292         /* Mark has_attachments */
293         all_has_attach = all_has_attach && (flags & TNY_HEADER_FLAG_ATTACHMENTS);
294         state->all_has_attachments = all_has_attach;
295         if (!state->any_has_attachments)
296                 state->any_has_attachments = (flags & TNY_HEADER_FLAG_ATTACHMENTS)?1:0;
297         
298         /* sent in progress */
299         msg_uid = modest_tny_send_queue_get_msg_id (header);
300         if (!state->sent_in_progress) {
301                 cache_mgr = modest_runtime_get_cache_mgr ();
302                 send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
303                                                                MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
304                 
305                 g_hash_table_foreach (send_queue_cache, (GHFunc) fill_list_of_caches, &send_queues);
306                 
307                 for (node = send_queues; node != NULL && !found; node = g_slist_next (node)) {
308                         send_queue = MODEST_TNY_SEND_QUEUE (node->data);
309                         
310                         /* Check if msg uid is being processed inside send queue */
311                         found = modest_tny_send_queue_msg_is_being_sent (send_queue, msg_uid);          
312                 }
313                 state->sent_in_progress = found;
314         }
315         
316         /* Free */
317         g_free(msg_uid);
318         g_object_unref (header);
319         g_slist_free (send_queues);
320
321         return state;
322 }
323
324    
325 DimmedState *
326 modest_ui_dimming_rules_define_dimming_state (ModestWindow *window)
327 {
328         DimmedState *state = NULL;
329
330         g_return_val_if_fail (MODEST_IS_WINDOW(window), NULL);
331         
332         if (MODEST_IS_MAIN_WINDOW (window)) 
333                 state = _define_main_window_dimming_state (MODEST_MAIN_WINDOW(window));
334         else if (MODEST_IS_MSG_VIEW_WINDOW (window)) {
335                 state = _define_msg_view_window_dimming_state (MODEST_MSG_VIEW_WINDOW(window));         
336         }
337         
338         return state;
339 }
340
341 gboolean 
342 modest_ui_dimming_rules_on_new_msg (ModestWindow *win, gpointer user_data)
343 {
344         ModestDimmingRule *rule = NULL;
345         gboolean dimmed = FALSE;
346
347         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
348         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
349         rule = MODEST_DIMMING_RULE (user_data);
350                 
351         /* Check dimmed rule */ 
352         if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
353 #ifndef MODEST_TOOLKIT_HILDON2
354                 dimmed = _msg_download_in_progress (win);
355                 if (dimmed)
356                         modest_dimming_rule_set_notification (rule, "");
357 #endif
358         } else if (MODEST_IS_MAIN_WINDOW(win)) {
359                 dimmed = !modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), 
360                                                           TRUE);        
361                 if (dimmed)
362                         modest_dimming_rule_set_notification (rule, _("mcen_nc_no_email_acnts_defined"));
363         }
364
365         return dimmed;
366 }
367
368 gboolean 
369 modest_ui_dimming_rules_on_new_folder (ModestWindow *win, gpointer user_data)
370 {
371         ModestDimmingRule *rule = NULL;
372         GtkWidget *folder_view = NULL;
373         TnyFolderStore *parent_folder = NULL;
374         gboolean dimmed = FALSE;
375
376         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
377         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
378         rule = MODEST_DIMMING_RULE (user_data);
379
380         dimmed = _transfer_mode_enabled (win);
381         if (dimmed) {
382                 modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_create_error"));
383                 return dimmed;
384         }
385
386         /* Get selected folder as parent of new folder to create */
387         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
388                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
389         if (!folder_view) /* folder view may not have been created yet */
390                 return TRUE;    
391
392         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
393         if (!parent_folder)
394                 return TRUE;
395         
396         if (TNY_IS_ACCOUNT (parent_folder)) {
397                 /* If it's the local account then do not dim */
398                 if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder))) {
399                         dimmed = FALSE;
400                 } else {
401                         ModestProtocolType protocol_type = modest_tny_account_get_protocol_type (TNY_ACCOUNT (parent_folder));
402                         if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
403                                 ModestProtocolRegistry *protocol_registry;
404
405                                 protocol_registry = modest_runtime_get_protocol_registry ();
406                                 /* If account does not support folders (pop) then dim */
407                                 dimmed = (!modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type,
408                                                                                            MODEST_PROTOCOL_REGISTRY_STORE_HAS_FOLDERS));
409                                 if (dimmed)
410                                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_create_error"));
411                         }
412                 }
413         } else {        
414                 TnyFolderType types[3];
415                                 
416                 types[0] = TNY_FOLDER_TYPE_DRAFTS; 
417                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
418                 types[2] = TNY_FOLDER_TYPE_SENT;
419
420                 /* Apply folder rules */        
421                 if (!dimmed) {
422                         dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win), FALSE);
423                         if (dimmed)
424                                 modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_create_error"));
425                 }
426                 if (!dimmed) {
427                         dimmed = _selected_folder_is_any_of_type (win, types, 3);
428                         if (dimmed)
429                                 modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_create_error"));
430                 }
431         }
432
433         /* if not the folder is selected then dim */
434         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
435                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
436         if (folder_view && !gtk_widget_is_focus (folder_view)) 
437                 dimmed = TRUE;
438
439         g_object_unref (parent_folder);
440
441         return dimmed;
442 }
443
444 gboolean 
445 modest_ui_dimming_rules_on_delete (ModestWindow *win, gpointer user_data)
446 {
447         ModestDimmingRule *rule = NULL;
448         GtkWidget *folder_view = NULL;
449         GtkWidget *header_view = NULL;  
450         gboolean dimmed = FALSE;
451
452         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
453         rule = MODEST_DIMMING_RULE (user_data);
454         
455         if (MODEST_IS_MAIN_WINDOW (win)) {
456                 /* Get the folder view */
457                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
458                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
459                 
460                 /* Get header view */
461                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
462                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
463
464                 if (header_view && gtk_widget_is_focus (header_view)) 
465                         dimmed = modest_ui_dimming_rules_on_delete_msg (win, rule);
466
467                 if (folder_view && gtk_widget_is_focus (folder_view)) 
468                         dimmed = modest_ui_dimming_rules_on_delete_folder (win, rule);
469
470                 if (header_view && folder_view &&
471                     !gtk_widget_is_focus (header_view) &&
472                     !gtk_widget_is_focus (folder_view)) {
473                         dimmed = TRUE;
474                         modest_dimming_rule_set_notification (rule, _CS("ckct_ib_nothing_to_delete"));
475                 }
476
477 #ifdef MODEST_TOOLKIT_HILDON2
478         } else if (MODEST_IS_FOLDER_WINDOW (win)) {
479                 dimmed = modest_ui_dimming_rules_on_folder_window_delete (win, user_data);
480         } else if (MODEST_IS_HEADER_WINDOW (win)) {
481
482                 if (!dimmed)
483                         dimmed = _transfer_mode_enabled (win);
484
485                 if (dimmed)
486                         modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
487
488                 if (!dimmed) {
489                         GtkWidget *header_view;
490                         TnyFolder *folder;
491
492                         header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (win)));
493                         folder = modest_header_view_get_folder (MODEST_HEADER_VIEW (header_view));
494                         if (folder) {
495                                 dimmed = (tny_folder_get_all_count (TNY_FOLDER (folder)) == 0) ||
496                                         modest_header_view_is_empty (MODEST_HEADER_VIEW (header_view));
497
498                                 if (!dimmed &&
499                                     (tny_folder_get_folder_type (TNY_FOLDER (folder)) == TNY_FOLDER_TYPE_OUTBOX)) {
500                                         dimmed = _all_msgs_in_sending_status (MODEST_HEADER_VIEW (header_view));;
501                                 }
502                                 g_object_unref (folder);
503                         }
504                 }
505
506 #endif
507         } else {
508                 dimmed = modest_ui_dimming_rules_on_delete_folder (win, rule);
509         }
510
511         return dimmed;
512 }
513
514
515
516 gboolean 
517 modest_ui_dimming_rules_on_delete_folder (ModestWindow *win, gpointer user_data)
518 {
519         ModestDimmingRule *rule = NULL;
520         TnyFolderType types[6];
521         gboolean dimmed = FALSE;
522
523         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
524         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
525         rule = MODEST_DIMMING_RULE (user_data);
526
527         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
528         types[1] = TNY_FOLDER_TYPE_OUTBOX;
529         types[2] = TNY_FOLDER_TYPE_SENT;
530         types[3] = TNY_FOLDER_TYPE_INBOX;
531         types[4] = TNY_FOLDER_TYPE_ROOT;
532         types[5] = TNY_FOLDER_TYPE_ARCHIVE;
533
534                 
535         /* Check dimmed rule */ 
536         dimmed = _selected_folder_not_deletable (MODEST_MAIN_WINDOW(win));
537         if (dimmed)
538                 modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_delete_error"));
539         if (!dimmed) {
540                 dimmed = _selected_folder_is_any_of_type (win, types, 6);
541                 if (dimmed)
542                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_delete_error"));
543         }
544         if (!dimmed) {
545                 dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
546                 if (dimmed)
547                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_delete_error"));
548         }
549         if (!dimmed) {
550                 dimmed = _transfer_mode_enabled (win);
551                 if (dimmed)
552                         modest_dimming_rule_set_notification (rule, _CS("ckct_ib_unable_to_delete"));
553         }
554
555         return dimmed;
556 }
557
558 gboolean
559 modest_ui_dimming_rules_on_sort (ModestWindow *win, gpointer user_data)
560 {
561         ModestDimmingRule *rule = NULL;
562         gboolean dimmed = FALSE;
563
564 #ifdef MODEST_TOOLKIT_HILDON2
565         if (MODEST_IS_HEADER_WINDOW (win)) {
566                 return FALSE;
567         }
568 #endif          
569
570         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
571         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
572         rule = MODEST_DIMMING_RULE (user_data);
573
574         /* Check dimmed rule */ 
575         dimmed = _selected_folder_is_root (MODEST_MAIN_WINDOW(win));
576
577         if (!dimmed)
578                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));
579
580         return dimmed;
581         
582 }
583
584 gboolean 
585 modest_ui_dimming_rules_on_rename_folder (ModestWindow *win, gpointer user_data)
586 {
587         ModestDimmingRule *rule = NULL;
588         TnyFolderType types[4];
589         gboolean dimmed = FALSE;
590
591         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
592         rule = MODEST_DIMMING_RULE (user_data);
593
594         types[0] = TNY_FOLDER_TYPE_DRAFTS;
595         types[1] = TNY_FOLDER_TYPE_OUTBOX;
596         types[2] = TNY_FOLDER_TYPE_SENT;
597         types[3] = TNY_FOLDER_TYPE_ARCHIVE;
598
599         /* Check dimmed rule */
600         if (MODEST_IS_MAIN_WINDOW (win)) {
601                 dimmed = _selected_folder_not_renameable (MODEST_MAIN_WINDOW(win));
602                 if (dimmed)
603                         modest_dimming_rule_set_notification (rule, "");
604                 if (!dimmed) {
605                         dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
606                         if (dimmed)
607                                 modest_dimming_rule_set_notification (rule, "");
608                 }
609         }
610
611 #ifdef MODEST_TOOLKIT_HILDON2
612         if (MODEST_IS_FOLDER_WINDOW (win)) {
613                 ModestFolderView *folder_view;
614                 folder_view = modest_folder_window_get_folder_view (MODEST_FOLDER_WINDOW (win));
615                 dimmed = !modest_folder_view_any_folder_fulfils_rules (folder_view,
616                                                                        MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE);
617         }
618 #endif
619
620         if (!dimmed) {
621                 dimmed = _selected_folder_is_any_of_type (win, types, 4);
622                 if (dimmed)
623                         modest_dimming_rule_set_notification (rule, "");
624         }
625
626         return dimmed;
627 }
628
629 gboolean 
630 modest_ui_dimming_rules_on_open_msg (ModestWindow *win, gpointer user_data)
631 {
632         ModestDimmingRule *rule = NULL;
633         gboolean dimmed = FALSE;
634         const DimmedState *state = NULL;
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         state = modest_window_get_dimming_state (win);          
640
641         /* Check dimmed rule */ 
642         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, user_data);      
643         if (!dimmed) {
644                 if (state)
645                         dimmed = state->any_marked_as_deleted;
646                 if (dimmed) {
647                         gchar *msg = modest_ui_actions_get_msg_already_deleted_error_msg (win);
648                         modest_dimming_rule_set_notification (rule, msg);
649                         g_free (msg);
650                 }
651         }
652         if (!dimmed) {
653                 dimmed = _selected_msg_sent_in_progress (win);
654                 if (dimmed)
655                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_open_while_sent"));
656         }
657
658         return dimmed;
659 }
660
661 gboolean 
662 modest_ui_dimming_rules_on_reply_msg (ModestWindow *win, gpointer user_data)
663 {
664         ModestDimmingRule *rule = NULL;
665         gboolean dimmed = FALSE;
666         TnyFolderType types[3];
667
668         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
669         rule = MODEST_DIMMING_RULE (user_data);
670
671         types[0] = TNY_FOLDER_TYPE_DRAFTS;
672         types[1] = TNY_FOLDER_TYPE_OUTBOX;
673         types[2] = TNY_FOLDER_TYPE_ROOT;
674
675         /* Check dimmed rule */
676         dimmed = _selected_folder_is_any_of_type (win, types, 3);
677         if (dimmed)
678                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_reply"));
679
680         /* main window dimming rules */
681         if (MODEST_IS_MAIN_WINDOW(win)) {
682
683                 if (!dimmed) {
684                         dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));
685                         if (dimmed)
686                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_nothing_to_reply"));
687                 }
688                 if (!dimmed) {
689                         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, rule);
690                 }
691         /* msg view window dimming rules */
692         } else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
693
694                 /* This could happen if we load the msg view window with a
695                    preview before loading the full message */
696                 TnyMsg *msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
697                 if (!msg) {
698                         dimmed = TRUE;
699                 } else {
700                         g_object_unref (msg);
701                 }
702
703                 if (!dimmed) {
704                         dimmed = _transfer_mode_enabled (win);
705                         if (dimmed)
706                                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
707                 }
708                 if (!dimmed) {
709                         dimmed = _msg_download_in_progress (win);
710                         if (dimmed)
711                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_reply"));
712                 }
713         }
714
715         return dimmed;
716 }
717
718
719 gboolean 
720 modest_ui_dimming_rules_on_contents_msg (ModestWindow *win, gpointer user_data)
721 {
722         ModestDimmingRule *rule = NULL;
723         gboolean dimmed = FALSE;
724
725         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
726         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
727         rule = MODEST_DIMMING_RULE (user_data);
728                 
729         /* Check dimmed rule */ 
730         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);     
731         if (!dimmed) {
732                 dimmed = _msg_download_completed (MODEST_MAIN_WINDOW(win));
733                 if (dimmed)
734                         modest_dimming_rule_set_notification (rule, "");
735         }
736
737         return dimmed;
738 }
739
740 gboolean 
741 modest_ui_dimming_rules_always_dimmed (ModestWindow *win, gpointer user_data)
742 {
743         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
744
745         return TRUE;
746 }
747
748 static gboolean
749 _message_already_sent (ModestMsgViewWindow *view_window)
750 {
751         TnyHeader *header;
752         TnyFolder *folder;
753         gboolean already_sent = FALSE;
754
755         header = modest_msg_view_window_get_header (view_window);
756         if (header) {
757                 folder = tny_header_get_folder (header);
758                 if (folder) {
759                         if (modest_tny_folder_guess_folder_type (folder) ==
760                             TNY_FOLDER_TYPE_OUTBOX) {
761                                 ModestTnySendQueueStatus status = 
762                                         modest_tny_all_send_queues_get_msg_status (header);
763                                 if (status == MODEST_TNY_SEND_QUEUE_UNKNOWN ||
764                                     status == MODEST_TNY_SEND_QUEUE_SENDING)
765                                         already_sent = TRUE;
766                         }
767                         g_object_unref (folder);
768                 }
769                 g_object_unref (header);
770         }
771         return already_sent;
772 }
773
774
775 gboolean 
776 modest_ui_dimming_rules_on_delete_msg (ModestWindow *win, gpointer user_data)
777 {
778         ModestDimmingRule *rule = NULL;
779         const DimmedState *state = NULL;
780         gboolean dimmed = FALSE;
781
782         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
783         rule = MODEST_DIMMING_RULE (user_data);
784         state = modest_window_get_dimming_state (win);
785
786         /* If we're in transfer mode then do not allow to delete messages */
787         dimmed = _transfer_mode_enabled (win);
788         if (dimmed) {
789                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
790                 return dimmed;
791         }
792
793         /* Check dimmed rule */ 
794         if (MODEST_IS_MAIN_WINDOW (win)) {
795                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));
796                 if (dimmed)
797                         modest_dimming_rule_set_notification (rule, _CS("ckct_ib_nothing_to_delete"));
798                 if (!dimmed) {
799                         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
800                 }
801                 if (!dimmed) {
802                         if (state)
803                                 dimmed = state->sent_in_progress;
804                         if (dimmed)
805                                 modest_dimming_rule_set_notification (rule, _CS("ckct_ib_unable_to_delete"));
806                 }
807                 if (!dimmed) {
808                         if (state)
809                                 dimmed = state->any_marked_as_deleted;
810                         if (dimmed) {
811                                 gchar *msg = modest_ui_actions_get_msg_already_deleted_error_msg (win);
812                                 modest_dimming_rule_set_notification (rule, msg);
813                                 g_free (msg);
814                         }
815                 }
816                 if (!dimmed) {
817                         if (state) {
818                                 dimmed = (state->already_opened_msg > 0) ? TRUE : FALSE;
819                                 if (dimmed) {
820                                         gchar *message = NULL;
821
822                                         message = g_strdup_printf(_("mcen_nc_unable_to_delete_n_messages"),
823                                                                   state->already_opened_msg);
824                                         modest_dimming_rule_set_notification (rule, message);
825                                         g_free(message);
826                                 }
827                         }
828                 }
829         }
830         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
831                 /* This could happen if we load the msg view window with a
832                    preview before loading the full message */
833                 TnyMsg *msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
834                 if (!msg) {
835                         dimmed = TRUE;
836                 } else {
837                         g_object_unref (msg);
838                 }
839
840                 if (!dimmed) {
841                         if (state)
842                                 dimmed = state->any_marked_as_deleted;
843                         if (dimmed) {
844                                 gchar *msg = modest_ui_actions_get_msg_already_deleted_error_msg (win);
845                                 modest_dimming_rule_set_notification (rule, msg);
846                                 g_free (msg);
847                         }
848                 }
849                 if (!dimmed) {
850                         if (state)
851                                 dimmed = state->sent_in_progress;
852                         if (dimmed)
853                                 modest_dimming_rule_set_notification (rule, _CS("ckct_ib_unable_to_delete"));
854                 }
855
856                 /* This could happen if we're viewing a message of the
857                    outbox that has been already sent */
858                 if (!dimmed)
859                         dimmed = _message_already_sent (MODEST_MSG_VIEW_WINDOW(win));
860
861                 /* The delete button should be dimmed when viewing an attachment,
862                  * but should be enabled when viewing a message from the list, 
863                  * or when viewing a search result.
864                  */
865                 if (!dimmed) {
866                         if (!modest_msg_view_window_is_search_result (MODEST_MSG_VIEW_WINDOW(win))) {
867                                 dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW(win));
868                                 if (dimmed) {
869                                         modest_dimming_rule_set_notification (rule, _CS("ckct_ib_unable_to_delete"));
870                                 }
871                         }
872                 }
873         }
874
875         return dimmed;
876 }
877
878 gboolean 
879 modest_ui_dimming_rules_on_details (ModestWindow *win, gpointer user_data)
880 {
881         ModestDimmingRule *rule = NULL;
882         GtkWidget *header_view = NULL;
883         gboolean dimmed = FALSE;
884
885         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
886         rule = MODEST_DIMMING_RULE (user_data);
887         
888         /* main window dimming rules */
889         if (MODEST_IS_MAIN_WINDOW(win)) {
890                                 
891                 /* Check dimmed rule */
892                 header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
893                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
894                 
895                 /* If the header view has the focus: */
896                 if (header_view && gtk_widget_is_focus (header_view)) {
897                         /* Check dimmed rule */ 
898                         if (!dimmed)
899                                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), TRUE, user_data);
900                 }
901                 else {
902                         /* If the folder view has the focus: */
903                         GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
904                                 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
905                         if (folder_view && gtk_widget_is_focus (folder_view)) {
906                                 TnyFolderStore *folder_store
907                                         = modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
908                                 if (folder_store) {
909                                         /* Only enable for folders, not accounts,
910                                          * though the UI spec is not clear about that.
911                                          * If we enable this for accounts then we must 
912                                          * add code to handle them in modest_ui_actions_on_details(). */
913                                         if (!TNY_IS_FOLDER(folder_store)) {
914                                                 dimmed = TRUE;
915                                                 modest_dimming_rule_set_notification (rule, "");
916                                         }
917
918                                         g_object_unref (folder_store);
919                                 } else {
920                                         dimmed = TRUE;
921                                         modest_dimming_rule_set_notification (rule, "");
922                                 }
923                                 if (!dimmed) {
924                                         dimmed = _msg_download_in_progress (win);
925                                         if (dimmed)
926                                                 modest_dimming_rule_set_notification (rule, "");
927                                 }
928
929                         } else {
930                                 return TRUE;
931                         }
932                 }
933
934         /* msg view window dimming rules */
935         } else {
936                 /* Check dimmed rule */ 
937                 if (MODEST_IS_MSG_VIEW_WINDOW (win))
938                         dimmed = _msg_download_in_progress (win);
939                 if (dimmed)
940                         modest_dimming_rule_set_notification (rule, "");
941         }
942
943         return dimmed;
944 }
945
946 gboolean
947 modest_ui_dimming_rules_on_fetch_images (ModestWindow *win, gpointer user_data)
948 {
949         gboolean dimmed = FALSE;
950
951         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), FALSE);
952
953         dimmed = !modest_msg_view_window_has_blocked_external_images (MODEST_MSG_VIEW_WINDOW (win));
954
955         return dimmed;
956 }
957
958
959 gboolean 
960 modest_ui_dimming_rules_on_mark_as_read_msg_in_view (ModestWindow *win, gpointer user_data)
961 {
962         ModestDimmingRule *rule = NULL;
963         TnyHeader *header;
964         TnyHeaderFlags flags;
965         gboolean dimmed = FALSE;
966
967
968         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
969         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
970         rule = MODEST_DIMMING_RULE (user_data);
971
972         header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
973         if (!header) {
974                 dimmed = TRUE;
975         }
976
977         /* If the viewer is showing a message sent as attachment */
978         if (!dimmed)
979                 dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW (win));
980
981         if (!dimmed) {
982                 flags = tny_header_get_flags (header);
983                 if (flags & TNY_HEADER_FLAG_SEEN)
984                         dimmed = TRUE;
985         }
986
987         if (header)
988                 g_object_unref (header);
989         return dimmed;
990 }
991
992
993 gboolean 
994 modest_ui_dimming_rules_on_mark_as_unread_msg_in_view (ModestWindow *win, gpointer user_data)
995 {
996         ModestDimmingRule *rule = NULL;
997         TnyHeader *header;
998         TnyHeaderFlags flags;
999         gboolean dimmed = FALSE;
1000         
1001
1002         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1003         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1004         rule = MODEST_DIMMING_RULE (user_data);
1005         
1006         header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
1007         if (!header) {
1008                 dimmed = TRUE;
1009         }
1010
1011         /* If the viewer is showing a message sent as attachment */
1012         if (!dimmed)
1013                 dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW (win));
1014
1015         if (!dimmed) {
1016                 flags = tny_header_get_flags (header);
1017                 if (!(flags & TNY_HEADER_FLAG_SEEN))
1018                         dimmed = TRUE;
1019         }
1020
1021         if (header)
1022                 g_object_unref (header);
1023         return dimmed;
1024 }
1025
1026 gboolean 
1027 modest_ui_dimming_rules_on_mark_as_read_msg (ModestWindow *win, gpointer user_data)
1028 {
1029         ModestDimmingRule *rule = NULL;
1030         TnyHeaderFlags flags;
1031         const DimmedState *state = NULL;
1032         gboolean dimmed = FALSE;
1033
1034         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1035         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1036         rule = MODEST_DIMMING_RULE (user_data);
1037         state = modest_window_get_dimming_state (win);          
1038         
1039         flags = TNY_HEADER_FLAG_SEEN; 
1040
1041         /* Check dimmed rule */ 
1042         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);     
1043         if (!dimmed) {
1044                 if (state)
1045                         dimmed = state->all_marked_as_seen;
1046                 if (dimmed)
1047                         modest_dimming_rule_set_notification (rule, "");
1048         }       
1049
1050         return dimmed;
1051 }
1052
1053 gboolean 
1054 modest_ui_dimming_rules_on_mark_as_unread_msg (ModestWindow *win, gpointer user_data)
1055 {
1056         ModestDimmingRule *rule = NULL;
1057         TnyHeaderFlags flags;
1058         const DimmedState *state = NULL;
1059         gboolean dimmed = FALSE;
1060
1061         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1062         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1063         rule = MODEST_DIMMING_RULE (user_data);
1064         state = modest_window_get_dimming_state (win);          
1065         
1066         flags = TNY_HEADER_FLAG_SEEN; 
1067
1068         /* Check dimmed rule */ 
1069         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
1070         if (!dimmed) {
1071                 if (state)
1072                         dimmed = !state->any_marked_as_seen;
1073                 if (dimmed)
1074                         modest_dimming_rule_set_notification (rule, "");
1075         }
1076
1077         return dimmed;
1078 }
1079
1080 gboolean 
1081 modest_ui_dimming_rules_on_move_to (ModestWindow *win, gpointer user_data)
1082 {
1083         ModestDimmingRule *rule = NULL;
1084         gboolean dimmed = FALSE;
1085
1086         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1087         rule = MODEST_DIMMING_RULE (user_data);
1088
1089         if (MODEST_IS_MAIN_WINDOW (win)) 
1090                 dimmed = modest_ui_dimming_rules_on_main_window_move_to (win, user_data);
1091 #ifdef MODEST_TOOLKIT_HILDON2
1092         else if (MODEST_IS_HEADER_WINDOW (win))
1093                 dimmed = modest_ui_dimming_rules_on_header_window_move_to (win, user_data);
1094         else if (MODEST_IS_FOLDER_WINDOW (win))
1095                 dimmed = modest_ui_dimming_rules_on_folder_window_move_to (win, user_data);
1096 #endif
1097         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) 
1098                  dimmed = modest_ui_dimming_rules_on_view_window_move_to (win, user_data);
1099
1100         return dimmed;
1101 }
1102
1103
1104 gboolean 
1105 modest_ui_dimming_rules_on_main_window_move_to (ModestWindow *win, gpointer user_data)
1106 {
1107         GtkWidget *folder_view = NULL;
1108         ModestDimmingRule *rule = NULL;
1109         const DimmedState *state = NULL;
1110         gboolean dimmed = FALSE;
1111         
1112         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), TRUE);
1113         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1114         rule = MODEST_DIMMING_RULE (user_data);
1115         state = modest_window_get_dimming_state (win);          
1116         
1117         /* Get the folder view */
1118         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1119                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1120
1121         if (folder_view) {
1122                 TnyFolderStore *selected = modest_folder_view_get_selected ((ModestFolderView *)folder_view);
1123                 if (selected) {
1124                         TnyAccount *account = NULL;
1125
1126                         if (TNY_IS_ACCOUNT (selected)) {
1127                                 account = g_object_ref (selected);
1128                         } else if (!TNY_IS_MERGE_FOLDER (selected)){
1129                                 account = tny_folder_get_account (TNY_FOLDER (selected));
1130                         }
1131                         if (account) {
1132                                 ModestProtocolType protocol_type;
1133
1134                                 protocol_type = modest_tny_account_get_protocol_type (account);
1135                                 dimmed  = modest_protocol_registry_protocol_type_has_tag
1136                                         (modest_runtime_get_protocol_registry (),
1137                                          protocol_type,
1138                                          MODEST_PROTOCOL_REGISTRY_STORE_FORBID_OUTGOING_XFERS);
1139
1140                                 g_object_unref (account);
1141                         }
1142                         g_object_unref (selected);
1143                 }
1144         }
1145
1146         /* Check diming rules for folders transfer  */
1147         if (!dimmed && folder_view && gtk_widget_is_focus (folder_view)) {
1148                 TnyFolderType types[5];
1149
1150                 types[0] = TNY_FOLDER_TYPE_DRAFTS; 
1151                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
1152                 types[2] = TNY_FOLDER_TYPE_SENT;
1153                 types[3] = TNY_FOLDER_TYPE_ROOT; 
1154                 types[4] = TNY_FOLDER_TYPE_INBOX; 
1155                 
1156                 /* Apply folder rules */        
1157                 dimmed = _selected_folder_not_moveable (MODEST_MAIN_WINDOW(win));
1158                 if (dimmed)
1159                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));           
1160                 if (!dimmed) {
1161                         dimmed = _selected_folder_is_any_of_type (win, types, 5);
1162                         if (dimmed)
1163                                 modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));
1164                 }
1165         }
1166         
1167         /* Check diming rules for messages transfer  */
1168         if (!dimmed) {
1169                 if (state) {
1170                         dimmed = (state->already_opened_msg > 0) ? TRUE : FALSE;
1171                         if (dimmed) {
1172                                 gchar *message = g_strdup_printf(_("emev_nc_unabletomove_items"),
1173                                                                  state->already_opened_msg);
1174                                 modest_dimming_rule_set_notification (rule, message);
1175                                 g_free(message);
1176                         }
1177                 }
1178         }
1179         if (!dimmed) {
1180                 if (!(folder_view && gtk_widget_is_focus (folder_view)))
1181                         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
1182
1183         }
1184         if (!dimmed) {
1185                 dimmed = _selected_msg_sent_in_progress (win);
1186                 if (dimmed)
1187                         modest_dimming_rule_set_notification (rule, _("sfil_ib_unable_to_move_selected_items"));
1188         }
1189
1190         return dimmed;
1191 }
1192
1193 static gboolean
1194 _forbid_outgoing_xfers (ModestWindow *window)
1195 {
1196         const gchar *account_name = NULL;
1197         TnyAccount *account = NULL;
1198         gboolean dimmed = FALSE;
1199
1200 #ifdef MODEST_TOOLKIT_HILDON2
1201         /* We cannot just get the active account because the active
1202            account of a header window that shows the headers of a
1203            local account is the ID of the remote account */
1204         if (MODEST_IS_HEADER_WINDOW (window)) {
1205                 ModestHeaderView *header_view;
1206                 TnyFolder *folder;
1207
1208                 header_view = modest_header_window_get_header_view ((ModestHeaderWindow *) window);
1209                 folder = modest_header_view_get_folder (header_view);
1210
1211                 if (folder) {
1212                         account = modest_tny_folder_get_account (folder);
1213                         g_object_unref (folder);
1214                 }
1215         }
1216 #endif
1217
1218         if (!account) {
1219                 account_name = modest_window_get_active_account (window);
1220                 account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store (),
1221                                                                        account_name,
1222                                                                        TNY_ACCOUNT_TYPE_STORE);
1223         }
1224
1225         if (account) {
1226                 ModestProtocolType protocol_type;
1227
1228                 protocol_type = modest_tny_account_get_protocol_type (account);
1229                 dimmed  = modest_protocol_registry_protocol_type_has_tag
1230                         (modest_runtime_get_protocol_registry (),
1231                          protocol_type,
1232                          MODEST_PROTOCOL_REGISTRY_STORE_FORBID_OUTGOING_XFERS);
1233
1234                 g_object_unref (account);
1235         }
1236         return dimmed;
1237 }
1238
1239 gboolean
1240 modest_ui_dimming_rules_on_view_window_move_to (ModestWindow *win, gpointer user_data)
1241 {
1242         ModestDimmingRule *rule = NULL;
1243         gboolean dimmed = FALSE;
1244
1245         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1246         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1247         rule = MODEST_DIMMING_RULE (user_data);
1248
1249         /* This could happen if we load the msg view window with a
1250            preview before loading the full message */
1251         TnyMsg *msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
1252         if (!msg) {
1253                 return TRUE;
1254         } else {
1255                 g_object_unref (msg);
1256         }
1257
1258         /* Check dimmed rule */
1259         dimmed = _transfer_mode_enabled (win);
1260         if (dimmed)
1261                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
1262
1263         if (!dimmed)
1264                 dimmed = _forbid_outgoing_xfers (win);
1265
1266         if (!dimmed) {
1267                 const DimmedState *state = modest_window_get_dimming_state (win);
1268                 if (state) {
1269                         dimmed = state->any_marked_as_deleted;
1270                         if (dimmed) {
1271                                 gchar *msg = modest_ui_actions_get_msg_already_deleted_error_msg (win);
1272                                 modest_dimming_rule_set_notification (rule, msg);
1273                                 g_free (msg);
1274                         }
1275                 }
1276         }
1277
1278         if (!dimmed) {
1279                 dimmed = _selected_msg_sent_in_progress (win);
1280                 if (dimmed)
1281                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_item"));
1282         }
1283
1284         /* This could happen if we're viewing a message of the outbox
1285            that has been already sent */
1286         if (!dimmed)
1287                 dimmed = _message_already_sent (MODEST_MSG_VIEW_WINDOW(win));
1288
1289         if (!dimmed) {
1290                 if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1291                         /* The move_to button should be dimmed when viewing an attachment,
1292                          * but should be enabled when viewing a message from the list, 
1293                          * or when viewing a search result.
1294                          */
1295                         if (!modest_msg_view_window_is_search_result (MODEST_MSG_VIEW_WINDOW(win))) {
1296                                 dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW (win));
1297                         }
1298                 }
1299                 if (dimmed) 
1300                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_item"));
1301         }
1302
1303         return dimmed;
1304 }
1305
1306 gboolean 
1307 modest_ui_dimming_rules_on_find_in_msg (ModestWindow *win, gpointer user_data)
1308 {
1309         ModestDimmingRule *rule = NULL;
1310         gboolean dimmed = FALSE;
1311
1312         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1313         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1314         rule = MODEST_DIMMING_RULE (user_data);
1315
1316         /* This could happen if we load the msg view window with a
1317            preview before loading the full message */
1318         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1319                 TnyMsg *msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
1320                 if (!msg) {
1321                         return TRUE;
1322                 } else {
1323                         g_object_unref (msg);
1324                 }
1325         }
1326
1327         /* Check dimmed rule */ 
1328         dimmed = _transfer_mode_enabled (win);
1329         if (dimmed)
1330                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));     
1331
1332         return dimmed;
1333 }
1334
1335 gboolean 
1336 modest_ui_dimming_rules_on_paste (ModestWindow *win, gpointer user_data)
1337 {
1338         ModestDimmingRule *rule = NULL;
1339         TnyFolderType types[3];
1340         gboolean dimmed = FALSE;
1341         
1342         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1343         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1344         rule = MODEST_DIMMING_RULE (user_data);
1345
1346         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
1347         types[1] = TNY_FOLDER_TYPE_OUTBOX;
1348         types[2] = TNY_FOLDER_TYPE_SENT;
1349         
1350         /* Check dimmed rule */ 
1351         dimmed = _clipboard_is_empty (win);
1352         if (dimmed)
1353                 modest_dimming_rule_set_notification (rule, 
1354                                                       _CS("ecoc_ib_edwin_nothing_to_paste"));
1355         if (!dimmed) {
1356                 dimmed = _selected_folder_is_any_of_type (win, types, 3);
1357                 if (dimmed)
1358                         modest_dimming_rule_set_notification (rule, 
1359                                                               _CS("ckct_ib_unable_to_paste_here"));
1360         }
1361         if (!dimmed) {
1362                 dimmed = !_folder_view_has_focus (win);
1363                 if (dimmed)
1364                         modest_dimming_rule_set_notification (rule,
1365                                         _CS("ckct_ib_unable_to_paste_here"));
1366         }
1367         if (!dimmed) {
1368                 dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win), TRUE);
1369                 if (dimmed) 
1370                         modest_dimming_rule_set_notification (rule, 
1371                                                               _CS("ckct_ib_unable_to_paste_here"));
1372         }
1373         if (!dimmed) {
1374                 dimmed = _selected_folder_is_same_as_source (win);
1375                 if (dimmed)
1376                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_copy_target_error"));
1377         }
1378         if (!dimmed) {
1379                 dimmed = _selected_folder_has_subfolder_with_same_name (win);
1380                 if (dimmed)
1381                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_copy_target_error"));
1382         }
1383         
1384         return dimmed;
1385 }
1386
1387
1388 gboolean 
1389 modest_ui_dimming_rules_on_select_all (ModestWindow *win, gpointer user_data)
1390 {
1391         ModestDimmingRule *rule = NULL;
1392         gboolean dimmed = FALSE;
1393         GtkWidget *focused = NULL;
1394
1395         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
1396         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1397         rule = MODEST_DIMMING_RULE (user_data);
1398
1399         focused = gtk_window_get_focus (GTK_WINDOW (win));
1400
1401         /* Main window dimming rules */ 
1402         if (MODEST_IS_MAIN_WINDOW (win))
1403                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));
1404
1405         if (!dimmed && MODEST_IS_MAIN_WINDOW (win))
1406                 dimmed = _header_view_is_all_selected (MODEST_MAIN_WINDOW(win));
1407
1408         if (!dimmed && GTK_IS_ENTRY (focused)) {
1409                 const gchar *current_text;
1410                 current_text = gtk_entry_get_text (GTK_ENTRY (focused));
1411                 dimmed = ((current_text == NULL) || (current_text[0] == '\0'));
1412         }
1413
1414         if (!dimmed && GTK_IS_TEXT_VIEW (focused)) {
1415                 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused));
1416                 dimmed = (gtk_text_buffer_get_char_count (buffer) < 1);
1417         }
1418
1419         if (dimmed && MODEST_IS_ATTACHMENTS_VIEW (focused))
1420                 dimmed = FALSE;
1421         return dimmed;
1422 }
1423
1424 gboolean 
1425 modest_ui_dimming_rules_on_view_attachments (ModestWindow *win, gpointer user_data)
1426 {
1427         ModestDimmingRule *rule = NULL;
1428         gboolean dimmed = FALSE;
1429
1430         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1431         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1432         rule = MODEST_DIMMING_RULE (user_data);
1433
1434         /* Check dimmed rule */ 
1435         dimmed = _invalid_attach_selected (win, TRUE, TRUE, FALSE, rule);                       
1436
1437         if (!dimmed) {
1438                 dimmed = _purged_attach_selected (win, FALSE, NULL);
1439                 if (dimmed) {
1440                         modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
1441                 }
1442         }
1443                 
1444         return dimmed;
1445 }
1446
1447 #ifdef MODEST_TOOLKIT_HILDON2
1448 static gboolean
1449 _not_valid_attachments (ModestWindow *win, gboolean save_not_remove)
1450 {
1451         gint n_attachments;
1452         TnyList *attachments;
1453         gboolean result = FALSE;
1454
1455         /* Get atachments */
1456         attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
1457         n_attachments = tny_list_get_length (attachments);
1458
1459         /* Check unique */              
1460         if (!result) {
1461                 result = n_attachments < 1;
1462         }
1463                 
1464         /* Check attached type (view operation not required) */
1465         if (!result)  {
1466                 gint n_valid = 0;
1467
1468                 TnyIterator *iter;
1469                 iter = tny_list_create_iterator (attachments);
1470                 while (!tny_iterator_is_done (iter)) {
1471                         gboolean is_valid = TRUE;
1472                         TnyMimePart *mime_part = TNY_MIME_PART (tny_iterator_get_current (iter));
1473                         TnyList *nested_list = tny_simple_list_new ();
1474                         tny_mime_part_get_parts (mime_part, nested_list);
1475
1476                         if (tny_mime_part_is_purged (mime_part)) {
1477                                 is_valid = FALSE;
1478                         }
1479                         
1480                         if (is_valid && modest_tny_mime_part_is_msg (mime_part)) {
1481                                 TnyMsg *window_msg;
1482                                 window_msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
1483                                 if (window_msg) {
1484                                         if (save_not_remove && (TnyMimePart *) window_msg != mime_part) {
1485                                                 is_valid = FALSE;
1486                                         }
1487                                         g_object_unref (window_msg);
1488                                 }
1489                                 if (is_valid && save_not_remove && tny_list_get_length (nested_list) > 0) {
1490                                         is_valid = FALSE;
1491                                 }
1492                         }
1493                         g_object_unref (nested_list);
1494                         g_object_unref (mime_part);
1495                         tny_iterator_next (iter);
1496
1497                         if (is_valid) 
1498                                 n_valid++;
1499                 }
1500                 g_object_unref (iter);
1501                 result = (n_valid == 0);
1502         }
1503         g_object_unref (attachments);
1504         return result;
1505
1506 }
1507 #endif
1508
1509 gboolean 
1510 modest_ui_dimming_rules_on_save_attachments (ModestWindow *win, gpointer user_data)
1511 {
1512         ModestDimmingRule *rule = NULL;
1513         gboolean dimmed = FALSE;
1514
1515         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1516         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1517         rule = MODEST_DIMMING_RULE (user_data);
1518
1519         /* Check dimmed rule */ 
1520
1521 #ifdef MODEST_TOOLKIT_HILDON2
1522         dimmed = _not_valid_attachments (win, TRUE);
1523 #else
1524         dimmed = _invalid_attach_selected (win, FALSE, FALSE, FALSE, rule);
1525
1526         if (!dimmed) {
1527                 dimmed = _purged_attach_selected (win, TRUE, NULL);
1528                 if (dimmed) {
1529                         modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
1530                 }
1531         }
1532 #endif
1533         return dimmed;
1534 }
1535
1536 gboolean 
1537 modest_ui_dimming_rules_on_remove_attachments (ModestWindow *win, gpointer user_data)
1538 {
1539         ModestDimmingRule *rule = NULL;
1540         const DimmedState *state = NULL;
1541         gboolean dimmed = FALSE;
1542
1543         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
1544         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1545         rule = MODEST_DIMMING_RULE (user_data);
1546         state = modest_window_get_dimming_state (win);          
1547
1548         /* Check in main window if there's only one message selected */
1549         if (MODEST_IS_MAIN_WINDOW (win)) {
1550                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW (win), TRUE, rule);
1551         }
1552
1553         /* Check in view window if there's any attachment selected */
1554         if (!dimmed && MODEST_IS_MSG_VIEW_WINDOW (win)) {
1555                 dimmed = _invalid_attach_selected (win, FALSE, FALSE, TRUE, NULL);
1556                 if (dimmed)
1557                         modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
1558         }
1559
1560         if (!dimmed) {
1561
1562                 dimmed = _selected_msg_sent_in_progress (win);
1563                 if (dimmed) {
1564                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
1565                 }
1566         }
1567
1568         /* cannot purge in editable drafts nor pop folders */
1569         if (!dimmed) {
1570                 dimmed = _invalid_folder_for_purge (win, rule);
1571                 if (dimmed)
1572                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
1573         }
1574
1575         /* Check if the selected message in main window has attachments */
1576         if (!dimmed && MODEST_IS_MAIN_WINDOW (win)) {
1577                 if (state)
1578                         dimmed = !(state->any_has_attachments);
1579                 if (dimmed)
1580                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
1581         }
1582
1583         /* Check if all attachments are already purged */
1584         if (!dimmed) {
1585                 dimmed = _purged_attach_selected (win, TRUE, rule);
1586         }
1587
1588         /* Check if the message is already downloaded */
1589         if (!dimmed && MODEST_IS_MAIN_WINDOW (win)) {
1590                 dimmed = !_msg_download_completed (MODEST_MAIN_WINDOW (win));
1591                 if (dimmed)
1592                         modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
1593         }
1594
1595         return dimmed;
1596 }
1597
1598 gboolean 
1599 modest_ui_dimming_rules_on_undo (ModestWindow *win, gpointer user_data)
1600 {
1601         ModestDimmingRule *rule = NULL;
1602         gboolean dimmed = FALSE;
1603         
1604         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1605         rule = MODEST_DIMMING_RULE (user_data);
1606
1607         /* Check dimmed rule */ 
1608         if (MODEST_IS_MAIN_WINDOW (win)) {
1609                 dimmed = _clipboard_is_empty (win); 
1610                 if (dimmed)
1611                         modest_dimming_rule_set_notification (rule, "");
1612         }
1613
1614         if (!dimmed && MODEST_IS_MSG_EDIT_WINDOW (win)) {
1615                 dimmed = !modest_msg_edit_window_can_undo (MODEST_MSG_EDIT_WINDOW (win));
1616         }
1617
1618         return dimmed;
1619 }
1620
1621 gboolean 
1622 modest_ui_dimming_rules_on_redo (ModestWindow *win, gpointer user_data)
1623 {
1624         ModestDimmingRule *rule = NULL;
1625         gboolean dimmed = FALSE;
1626         
1627         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1628         rule = MODEST_DIMMING_RULE (user_data);
1629
1630         /* Check dimmed rule */ 
1631         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
1632                 dimmed = !modest_msg_edit_window_can_redo (MODEST_MSG_EDIT_WINDOW (win));
1633         }
1634                                 
1635         return dimmed;  
1636 }
1637
1638 gboolean 
1639 modest_ui_dimming_rules_on_cut (ModestWindow *win, gpointer user_data)
1640 {
1641         ModestDimmingRule *rule = NULL;
1642         const DimmedState *state = NULL;
1643         gboolean dimmed = FALSE;
1644         
1645         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1646         rule = MODEST_DIMMING_RULE (user_data);
1647         state = modest_window_get_dimming_state (win);
1648
1649         /* Check common dimming rules */
1650         dimmed = _invalid_clipboard_selected (win, rule);
1651
1652         /* Check window specific dimming rules */
1653         if (MODEST_IS_MAIN_WINDOW (win)) {
1654                 /* Get focused widget */
1655                 GtkWidget *focused = gtk_window_get_focus (GTK_WINDOW (win));
1656                 
1657                 if (MODEST_IS_HEADER_VIEW (focused)) {
1658                         if (!dimmed) { 
1659                                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
1660                                 if (dimmed)
1661                                         modest_dimming_rule_set_notification (rule, "");
1662                         }
1663                         if (!dimmed) {
1664                                 dimmed = _selected_msg_sent_in_progress (win);
1665                                 if (dimmed)
1666                                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_cut_mess"));
1667                         }
1668                         if (!dimmed) {
1669                                 if (state)
1670                                         dimmed = (state->already_opened_msg > 0) ? TRUE : FALSE;
1671                                 if(dimmed)
1672                                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_cut_mess"));
1673                         }
1674                 }
1675                 else if (MODEST_IS_FOLDER_VIEW (focused)) {
1676                         TnyFolderType types[3];
1677                         
1678                         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
1679                         types[1] = TNY_FOLDER_TYPE_OUTBOX;
1680                         types[2] = TNY_FOLDER_TYPE_SENT;
1681                         
1682                         /* Apply folder rules */        
1683                         if (!dimmed) {
1684                                 dimmed = _selected_folder_not_deletable (MODEST_MAIN_WINDOW(win));
1685                                 if (dimmed)
1686                                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));
1687                         }
1688                         if (!dimmed) {
1689                                 dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
1690                                 if (dimmed)
1691                                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));
1692                         }
1693                         if (!dimmed) {
1694                                 dimmed = _selected_folder_is_any_of_type (win, types, 3);
1695                                 if (dimmed)
1696                                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));
1697                         }
1698                 }
1699         }
1700
1701         return dimmed;
1702 }
1703
1704 gboolean 
1705 modest_ui_dimming_rules_on_copy (ModestWindow *win, gpointer user_data)
1706 {
1707         ModestDimmingRule *rule = NULL;
1708         const DimmedState *state = NULL;
1709         gboolean dimmed = FALSE;
1710
1711         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1712         rule = MODEST_DIMMING_RULE (user_data);
1713         state = modest_window_get_dimming_state (win);
1714
1715         /* Check common dimming rules */
1716         dimmed = _invalid_clipboard_selected (win, rule);       
1717         
1718         /* Check window specific dimming rules */
1719         if (MODEST_IS_MAIN_WINDOW (win)) {
1720                 /* Get focused widget */
1721                 GtkWidget *focused = gtk_window_get_focus (GTK_WINDOW (win));
1722                 
1723                 if (MODEST_IS_HEADER_VIEW (focused)) {
1724                         if (!dimmed) {
1725                                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
1726                                 if (dimmed)
1727                                         modest_dimming_rule_set_notification (rule, "");
1728                         }               
1729                         if (!dimmed) {
1730                                 dimmed = _selected_msg_sent_in_progress (win);
1731                                 if (dimmed)
1732                                         modest_dimming_rule_set_notification (rule, _(""));
1733                         }
1734                         if (!dimmed) {
1735                                 if (state)
1736                                         dimmed = (state->already_opened_msg > 0) ? TRUE : FALSE;
1737                                 if(dimmed)
1738                                         modest_dimming_rule_set_notification (rule, _(""));
1739                         }
1740                 }
1741                 else if (MODEST_IS_FOLDER_VIEW (focused)) {
1742                         TnyFolderType types[3];
1743                         
1744                         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
1745                         types[1] = TNY_FOLDER_TYPE_OUTBOX;
1746                         types[2] = TNY_FOLDER_TYPE_SENT;
1747
1748                         if (!dimmed) {
1749                                 dimmed = _selected_folder_is_root (MODEST_MAIN_WINDOW(win));
1750                                 if (dimmed)
1751                                         modest_dimming_rule_set_notification (rule, _(""));
1752                         }
1753                         if (!dimmed) {
1754                                 dimmed = _selected_folder_is_any_of_type (win, types, 3);
1755                                 if (dimmed)
1756                                         modest_dimming_rule_set_notification (rule, _(""));
1757                         }
1758                 }
1759         }
1760                 
1761         return dimmed;
1762 }
1763
1764 gboolean 
1765 modest_ui_dimming_rules_on_set_style (ModestWindow *win, gpointer user_data)
1766 {
1767         ModestDimmingRule *rule = NULL;
1768         gboolean dimmed = FALSE;
1769         
1770         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1771         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1772         rule = MODEST_DIMMING_RULE (user_data);
1773
1774         /* Check common dimming rules */
1775         ModestMsgEditFormat format;
1776         format = modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (win));
1777
1778         dimmed = (format != MODEST_MSG_EDIT_FORMAT_HTML);
1779         if (dimmed)
1780                 modest_dimming_rule_set_notification (rule, _("mcen_ib_item_unavailable_plaintext"));
1781
1782         if (!dimmed) {
1783                 GtkWidget *body;
1784                 body = modest_msg_edit_window_get_child_widget (MODEST_MSG_EDIT_WINDOW (win),
1785                                                                 MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_BODY);
1786                 
1787                 dimmed = ((body == NULL)||(!gtk_widget_is_focus (body)));
1788                 if (dimmed)
1789                         modest_dimming_rule_set_notification (rule, _("mcen_ib_move_cursor_to_message"));
1790         }
1791                
1792         
1793         return dimmed;
1794 }
1795
1796 gboolean 
1797 modest_ui_dimming_rules_on_zoom (ModestWindow *win, gpointer user_data)
1798 {
1799         ModestDimmingRule *rule = NULL;
1800         gboolean dimmed = FALSE;
1801         
1802         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1803         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1804         rule = MODEST_DIMMING_RULE (user_data);
1805
1806         GtkWidget *body;
1807         body = modest_msg_edit_window_get_child_widget (MODEST_MSG_EDIT_WINDOW (win),
1808                                                         MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_BODY);
1809         
1810         dimmed = ((body == NULL)||(!gtk_widget_is_focus (body)));
1811         if (dimmed)
1812                 modest_dimming_rule_set_notification (rule, _("mcen_ib_move_cursor_to_message"));
1813         
1814         return dimmed;
1815 }
1816
1817 gboolean 
1818 modest_ui_dimming_rules_on_editor_paste (ModestWindow *win, gpointer user_data)
1819 {
1820         ModestDimmingRule *rule = NULL;
1821         gboolean dimmed = FALSE;
1822         GtkWidget *focused = NULL;
1823         
1824         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1825         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1826         rule = MODEST_DIMMING_RULE (user_data);
1827
1828         focused = gtk_window_get_focus (GTK_WINDOW (win));
1829
1830         dimmed = MODEST_IS_ATTACHMENTS_VIEW (focused);
1831         
1832         if (!dimmed) {
1833                 dimmed = GTK_IS_TOGGLE_BUTTON (focused);
1834         }
1835
1836         if (!dimmed) {
1837                 ModestEmailClipboard *e_clipboard = modest_runtime_get_email_clipboard ();
1838                 const gchar *clipboard_text = modest_msg_edit_window_get_clipboard_text (MODEST_MSG_EDIT_WINDOW (win));
1839
1840                 dimmed = modest_email_clipboard_cleared (e_clipboard) && 
1841                   ((clipboard_text == NULL) || (clipboard_text[0] == '\0'));
1842         }
1843         
1844         return dimmed;
1845 }
1846
1847 gboolean 
1848 modest_ui_dimming_rules_on_editor_remove_attachment (ModestWindow *win, gpointer user_data)
1849 {
1850         ModestDimmingRule *rule = NULL;
1851         gboolean dimmed = FALSE;
1852         TnyList *selected_attachments = NULL;
1853         gint n_att_selected = 0;
1854         GtkWidget *attachments_view;
1855         
1856         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1857         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1858         rule = MODEST_DIMMING_RULE (user_data);
1859
1860         attachments_view = modest_msg_edit_window_get_child_widget (
1861                                                                     MODEST_MSG_EDIT_WINDOW (win),
1862                                                                     MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_ATTACHMENTS);
1863         
1864 #ifdef MODEST_TOOLKIT_HILDON2
1865         selected_attachments = modest_attachments_view_get_attachments (
1866                                                                       MODEST_ATTACHMENTS_VIEW (attachments_view));
1867 #else
1868         selected_attachments = modest_attachments_view_get_selection (
1869                                                                       MODEST_ATTACHMENTS_VIEW (attachments_view));
1870 #endif
1871         n_att_selected = tny_list_get_length (selected_attachments);
1872         g_object_unref (selected_attachments);
1873         
1874         dimmed = (n_att_selected < 1);  
1875         
1876         return dimmed;
1877 }
1878
1879 gboolean 
1880 modest_ui_dimming_rules_on_send (ModestWindow *win, gpointer user_data)
1881 {
1882         ModestDimmingRule *rule = NULL;
1883         gboolean dimmed = FALSE;
1884         GtkWidget *body_field;
1885
1886         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1887         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1888         rule = MODEST_DIMMING_RULE (user_data);
1889
1890         body_field = modest_msg_edit_window_get_child_widget (MODEST_MSG_EDIT_WINDOW (win),
1891                                                               MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_BODY);
1892
1893         if (!dimmed) {
1894                 GtkWidget *to_field, *cc_field, *bcc_field;
1895                 GtkTextBuffer * to_buffer, *cc_buffer, *bcc_buffer;
1896                 cc_field = modest_msg_edit_window_get_child_widget (MODEST_MSG_EDIT_WINDOW (win),
1897                                                                     MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_CC);
1898                 to_field = modest_msg_edit_window_get_child_widget (MODEST_MSG_EDIT_WINDOW (win),
1899                                                                     MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_TO);
1900                 bcc_field = modest_msg_edit_window_get_child_widget (MODEST_MSG_EDIT_WINDOW (win),
1901                                                                      MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_BCC);
1902                 to_buffer = modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (to_field));
1903                 cc_buffer = modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (cc_field));
1904                 bcc_buffer = modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (bcc_field));
1905
1906                 dimmed = ((gtk_text_buffer_get_char_count (to_buffer) +
1907                            gtk_text_buffer_get_char_count (cc_buffer) +
1908                            gtk_text_buffer_get_char_count (bcc_buffer)) == 0);
1909                 if (dimmed)
1910                         modest_dimming_rule_set_notification (rule, _("mcen_ib_add_recipients_first"));
1911         }
1912         
1913         return dimmed;
1914 }
1915
1916 gboolean 
1917 modest_ui_dimming_rules_on_view_previous (ModestWindow *win, gpointer user_data)
1918 {
1919         ModestDimmingRule *rule = NULL;
1920         gboolean dimmed = FALSE;
1921         
1922         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1923         rule = MODEST_DIMMING_RULE (user_data);
1924
1925         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1926
1927         /* Check dimmed rule */
1928         dimmed = _transfer_mode_enabled (win);
1929         if (dimmed)
1930                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
1931         
1932         if (!dimmed) {
1933                 dimmed = modest_msg_view_window_first_message_selected (
1934                                 MODEST_MSG_VIEW_WINDOW(win));
1935                 modest_dimming_rule_set_notification (rule, NULL);
1936         }
1937
1938         return dimmed;
1939 }
1940
1941 gboolean 
1942 modest_ui_dimming_rules_on_view_next (ModestWindow *win, gpointer user_data)
1943 {
1944         ModestDimmingRule *rule = NULL;
1945         gboolean dimmed = FALSE;
1946
1947         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1948         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1949         rule = MODEST_DIMMING_RULE (user_data);
1950
1951         /* Check dimmed rule */ 
1952         dimmed = _transfer_mode_enabled (win);                  
1953         if (dimmed)
1954                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
1955         
1956         if (!dimmed) {
1957                 dimmed = modest_msg_view_window_last_message_selected (MODEST_MSG_VIEW_WINDOW (win));
1958                 modest_dimming_rule_set_notification (rule, NULL);
1959         }
1960
1961         return dimmed;
1962 }
1963
1964
1965 gboolean
1966 modest_ui_dimming_rules_on_tools_smtp_servers (ModestWindow *win, gpointer user_data)
1967 {
1968         gboolean dimmed;
1969         ModestAccountMgr *mgr;
1970
1971         mgr = modest_runtime_get_account_mgr();
1972         dimmed = !modest_account_mgr_has_accounts(mgr, TRUE);
1973
1974         /* Dimm it if we only have metaaccounts */
1975         if (!dimmed) {
1976                 ModestProtocolRegistry *reg = modest_runtime_get_protocol_registry ();
1977                 GSList *account_names = modest_account_mgr_account_names (mgr, TRUE);
1978
1979                 if (account_names) {
1980                         ModestProtocolType store_protocol;
1981                         gboolean found = FALSE;
1982                         GSList *iter = account_names;
1983                         const gchar *tag = MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS;
1984
1985                         while (iter && !found) {
1986                                 gchar* account_name;
1987
1988                                 account_name = (gchar *) iter->data;
1989                                 store_protocol = modest_account_mgr_get_store_protocol (mgr, account_name);
1990
1991                                 if (!modest_protocol_registry_protocol_type_has_tag (reg, store_protocol, tag))
1992                                         found = TRUE;
1993                                 else
1994                                         iter = g_slist_next (iter);
1995                         }
1996                         modest_account_mgr_free_account_names (account_names);
1997                         dimmed = !found;
1998                 }
1999         }
2000
2001         return dimmed;
2002 }
2003
2004 gboolean
2005 modest_ui_dimming_rules_on_cancel_sending (ModestWindow *win, gpointer user_data)
2006 {
2007         ModestDimmingRule *rule = NULL;
2008         TnyFolderType types[1];
2009         const DimmedState *state = NULL;
2010         gboolean dimmed = FALSE;
2011
2012         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2013         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
2014         rule = MODEST_DIMMING_RULE (user_data);
2015         state = modest_window_get_dimming_state (win);
2016
2017         types[0] = TNY_FOLDER_TYPE_OUTBOX; 
2018
2019         /* Check dimmed rules */        
2020         dimmed = !_selected_folder_is_any_of_type (win, types, 1);
2021         if (dimmed) 
2022                 modest_dimming_rule_set_notification (rule, "");        
2023         if (!dimmed) {
2024                 if (state)
2025                         dimmed = !state->sent_in_progress;
2026                 if (dimmed)
2027                         modest_dimming_rule_set_notification (rule, "");
2028         }
2029
2030         return dimmed;
2031 }
2032
2033 gboolean 
2034 modest_ui_dimming_rules_on_cancel_sending_all (ModestWindow *win, gpointer user_data)
2035 {
2036         /* We dim if no msg send is in progress (and then cancelling send all has no
2037          * effect */
2038         return !_msgs_send_in_progress ();
2039 }
2040
2041 gboolean 
2042 modest_ui_dimming_rules_on_send_receive (ModestWindow *win, gpointer user_data)
2043 {
2044         ModestDimmingRule *rule = NULL;
2045         gboolean dimmed = FALSE;
2046         ModestAccountMgr *mgr;
2047         const gchar* account_name;
2048
2049         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
2050         rule = MODEST_DIMMING_RULE (user_data);
2051         mgr = modest_runtime_get_account_mgr();
2052
2053         /* Check dimmed rule */
2054         account_name = modest_window_get_active_account (win);
2055
2056         if (account_name)
2057                 dimmed = modest_account_mgr_account_is_busy (mgr, account_name);
2058         else
2059                 dimmed = TRUE;
2060
2061         if (dimmed)
2062                 modest_dimming_rule_set_notification (rule, _("mcen_nc_no_email_acnts_defined"));
2063
2064         return dimmed;
2065 }
2066
2067 gboolean 
2068 modest_ui_dimming_rules_on_send_receive_all (ModestWindow *win, gpointer user_data)
2069 {
2070         ModestDimmingRule *rule = NULL;
2071         gboolean dimmed = FALSE;
2072
2073         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
2074         rule = MODEST_DIMMING_RULE (user_data);
2075  
2076         /* Check dimmed rule */ 
2077         GSList *account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr (), TRUE);
2078         if (g_slist_length (account_names) < 1)
2079                 dimmed = TRUE;
2080         if (dimmed)
2081                 modest_dimming_rule_set_notification (rule, _("mcen_nc_no_email_acnts_defined"));
2082
2083         modest_account_mgr_free_account_names (account_names);
2084
2085         if (!dimmed) {
2086                 dimmed = _send_receive_in_progress (win);
2087         }
2088
2089         return dimmed;
2090 }
2091
2092 #ifdef MODEST_TOOLKIT_HILDON2
2093 gboolean
2094 modest_ui_dimming_rules_on_add_to_contacts (ModestWindow *win, gpointer user_data)
2095 {
2096         GSList *recipients = NULL;
2097         gboolean has_recipients_to_add;
2098
2099         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
2100
2101         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2102                 TnyMsg *msg;
2103
2104                 msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
2105
2106                 /* Message is loaded asynchronously, so this could happen */
2107                 if (!msg) {
2108                         TnyHeader *header;
2109
2110                         header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
2111                         if (!header)
2112                                 return TRUE;
2113
2114                         recipients = modest_tny_msg_header_get_all_recipients_list (header);
2115                         g_object_unref (header);
2116                 } else {
2117                         recipients = modest_tny_msg_get_all_recipients_list (msg);
2118                         g_object_unref (msg);
2119                 }
2120         } else if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
2121                 /* Check if there are pending addresses to add */
2122                 return !modest_msg_edit_window_has_pending_addresses ((ModestMsgEditWindow *) win);
2123         }
2124
2125         has_recipients_to_add = FALSE;
2126
2127         if (recipients) {
2128                 GSList *node;
2129                 for (node = recipients; node != NULL; node = g_slist_next (node)) {
2130                         const gchar *recipient = (const gchar *) node->data;
2131                         if (modest_text_utils_validate_recipient (recipient, NULL)) {
2132                                 if (!modest_address_book_has_address (recipient)) {
2133                                         has_recipients_to_add = TRUE;
2134                                         break;
2135                                 }
2136                         }
2137                 }
2138                 g_slist_foreach (recipients, (GFunc) g_free, NULL);
2139                 g_slist_free (recipients);
2140         }
2141
2142         return !has_recipients_to_add;
2143 }
2144 #else
2145 gboolean
2146 modest_ui_dimming_rules_on_add_to_contacts (ModestWindow *win, gpointer user_data)
2147 {
2148         ModestDimmingRule *rule = NULL;
2149         gboolean dimmed = FALSE;
2150         GtkWidget *focused = NULL;
2151
2152         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
2153         rule = MODEST_DIMMING_RULE (user_data);
2154         focused = gtk_window_get_focus (GTK_WINDOW (win));
2155
2156         dimmed = !focused;
2157
2158         if (!dimmed) {
2159                 gchar *selection = NULL;
2160                 if (GTK_IS_TEXT_VIEW (focused)) {
2161                         GtkTextIter start, end;
2162                         GtkTextBuffer *buffer = NULL;
2163                         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused));
2164                         if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end)) {
2165                                 selection = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2166                         }
2167                 } else if (GTK_IS_LABEL (focused)) {
2168                         selection = modest_text_utils_label_get_selection (GTK_LABEL (focused));
2169                 } else {
2170                         gboolean do_check = TRUE;
2171                         GtkClipboard *clipboard;
2172                         if (GTK_IS_HTML (focused)) {
2173                                 const gchar *sel;
2174                                 int len = -1;
2175                                 sel = gtk_html_get_selection_html (GTK_HTML (focused), &len);
2176                                 do_check = !((sel == NULL) || (sel[0] == '\0'));
2177                         } else if (MODEST_IS_ATTACHMENTS_VIEW (focused)) {
2178                                 do_check = FALSE;
2179                         }
2180                         if (do_check) {
2181                                 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
2182                                 selection = gtk_clipboard_wait_for_text (clipboard);
2183                         } else {
2184                                 selection = NULL;
2185                         }
2186                 }
2187                 dimmed = !((selection != NULL) && (modest_text_utils_validate_recipient (selection, NULL)));
2188         }
2189
2190         return dimmed;
2191 }
2192 #endif
2193
2194 /* *********************** static utility functions ******************** */
2195
2196
2197 static gboolean
2198 _selected_folder_not_writeable (ModestMainWindow *win,
2199                                 gboolean for_paste)
2200 {
2201         GtkWidget *folder_view = NULL;
2202         TnyFolderStore *parent_folder = NULL;
2203         ModestEmailClipboard *clipboard = NULL;
2204         ModestTnyFolderRules rules;
2205         gboolean is_local_acc = FALSE;
2206         gboolean xfer_folders = FALSE;
2207         gboolean result = FALSE;
2208
2209         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2210
2211         /* Get folder view */
2212         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2213                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2214         /* If no folder view, always dimmed */
2215         if (!folder_view)
2216                 return TRUE;
2217         
2218         /* Get selected folder as parent of new folder to create */
2219         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2220         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
2221                 /* If it's the local account and its transfering folders, then do not dim */            
2222                 if (TNY_IS_ACCOUNT (parent_folder)) {
2223                         is_local_acc = modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder));
2224                         if (for_paste) {
2225                                 clipboard = modest_runtime_get_email_clipboard ();
2226                                 xfer_folders = modest_email_clipboard_folder_copied (clipboard);
2227                         }
2228                 }
2229
2230                 if (for_paste) 
2231                         result = !(is_local_acc && xfer_folders); 
2232                 else
2233                         result = !is_local_acc;
2234                 goto frees;             
2235         }
2236         
2237         /* Check dimmed rule */ 
2238         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
2239         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
2240
2241         /* free */
2242  frees:
2243         if (parent_folder != NULL)
2244                 g_object_unref (parent_folder);
2245
2246         return result;
2247 }
2248
2249 static gboolean
2250 _selected_folder_not_deletable (ModestMainWindow *win)
2251 {
2252         GtkWidget *folder_view = NULL;
2253         TnyFolderStore *parent_folder = NULL;
2254         ModestTnyFolderRules rules;
2255         gboolean result = FALSE;
2256
2257         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2258
2259         /* Get folder view */
2260         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2261                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2262         /* If no folder view, always dimmed */
2263         if (!folder_view)
2264                 return TRUE;
2265         
2266         /* Get selected folder as parent of new folder to create */
2267         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2268         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
2269                 result = TRUE;
2270                 goto frees;             
2271         }
2272         
2273         /* Check dimmed rule */ 
2274         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
2275         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
2276
2277         /* free */
2278  frees:
2279         if (parent_folder != NULL)
2280                 g_object_unref (parent_folder);
2281
2282         return result;
2283 }
2284
2285 static gboolean
2286 _selected_folder_not_moveable (ModestMainWindow *win)
2287 {
2288         GtkWidget *folder_view = NULL;
2289         TnyFolderStore *parent_folder = NULL;
2290         ModestTnyFolderRules rules;
2291         gboolean result = FALSE;
2292
2293         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2294
2295         /* Get folder view */
2296         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2297                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2298         /* If no folder view, always dimmed */
2299         if (!folder_view)
2300                 return TRUE;
2301         
2302         /* Get selected folder as parent of new folder to create */
2303         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2304         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
2305                 result = TRUE;
2306                 goto frees;             
2307         }
2308         
2309         /* Check dimmed rule */ 
2310         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
2311         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
2312
2313         /* free */
2314  frees:
2315         if (parent_folder != NULL)
2316                 g_object_unref (parent_folder);
2317
2318         return result;
2319 }
2320
2321 static gboolean
2322 _selected_folder_not_renameable (ModestMainWindow *win)
2323 {
2324         GtkWidget *folder_view = NULL;
2325         TnyFolderStore *parent_folder = NULL;
2326         ModestTnyFolderRules rules;
2327         gboolean result = FALSE;
2328
2329         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2330
2331         /* Get folder view */
2332         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2333                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2334         /* If no folder view, always dimmed */
2335         if (!folder_view)
2336                 return TRUE;
2337         
2338         /* Get selected folder as parent of new folder to create */
2339         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2340         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
2341                 result = TRUE;
2342                 goto frees;             
2343         }
2344         
2345         /* Check dimmed rule */ 
2346         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
2347         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
2348
2349         /* free */
2350  frees:
2351         if (parent_folder != NULL)
2352                 g_object_unref (parent_folder);
2353
2354         return result;
2355 }
2356
2357 static gboolean
2358 _selected_folder_is_root_or_inbox (ModestMainWindow *win)
2359 {
2360         TnyFolderType types[2];
2361         gboolean result = FALSE;
2362
2363         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2364
2365         types[0] = TNY_FOLDER_TYPE_ROOT; 
2366         types[1] = TNY_FOLDER_TYPE_INBOX; 
2367
2368         /* Check folder type */
2369         result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 2);
2370
2371         /* Check pop and MMC accounts */
2372         if (!result) {
2373                 result = _selected_folder_is_root (win);
2374         }
2375                 
2376         return result;
2377 }
2378
2379
2380 static gboolean
2381 _selected_folder_is_root (ModestMainWindow *win)
2382 {
2383         TnyFolderType types[1];
2384         gboolean result = FALSE;
2385
2386         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2387
2388         /* All accounts are root items: */
2389         GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2390                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2391         if (folder_view) {                                      
2392                 gboolean is_account = FALSE;
2393                 TnyFolderStore *folder_store = 
2394                         modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2395                 if (folder_store) {
2396                         is_account = TNY_IS_ACCOUNT (folder_store);
2397
2398                         g_object_unref (folder_store);
2399                         folder_store = NULL;
2400                 }
2401                 
2402                 if (is_account)
2403                         return TRUE;
2404         }
2405                 
2406         /* Try something more precise: */
2407         types[0] = TNY_FOLDER_TYPE_ROOT; 
2408
2409         /* Check folder type */
2410         result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 1);
2411                 
2412         return result;
2413 }
2414
2415 static gboolean
2416 _header_view_is_all_selected (ModestMainWindow *win)
2417 {
2418         const DimmedState *state = NULL;
2419
2420         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
2421
2422         state = modest_window_get_dimming_state (MODEST_WINDOW(win));
2423
2424         return (state) ? state->all_selected : TRUE;
2425 }
2426
2427 static gboolean
2428 _selected_folder_is_empty (ModestMainWindow *win)
2429 {
2430         GtkWidget *folder_view = NULL, *header_view = NULL;
2431         TnyFolderStore *folder = NULL;
2432         gboolean result = FALSE;
2433
2434         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2435
2436         /* Get folder view */
2437         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2438                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2439
2440         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2441                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
2442
2443         /* If no folder view, always dimmed */
2444         if (!folder_view || !header_view)
2445                 return TRUE;
2446         
2447         /* Get selected folder as parent of new folder to create */
2448         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2449         if (!(folder && TNY_IS_FOLDER(folder))) {
2450                 if (folder)
2451                         g_object_unref (folder);
2452                 return TRUE;
2453         }
2454         
2455         /* Check folder type */
2456         if (modest_header_view_is_empty (MODEST_HEADER_VIEW (header_view)) ||
2457             tny_folder_get_all_count (TNY_FOLDER (folder)) == 0)
2458                 result = TRUE;
2459
2460         /* free */
2461         g_object_unref (folder);
2462
2463         return result;
2464 }
2465
2466 static gboolean
2467 _folder_view_has_focus (ModestWindow *win)
2468 {
2469         GtkWidget *folder_view = NULL;
2470
2471         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2472
2473         /* Get folder view */
2474         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2475                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2476         if (!folder_view)
2477                 return FALSE;
2478         
2479         if (gtk_widget_is_focus(folder_view))
2480                 return TRUE;
2481
2482         return FALSE;
2483 }
2484
2485 static gboolean
2486 _selected_folder_is_same_as_source (ModestWindow *win)
2487 {
2488         ModestEmailClipboard *clipboard = NULL;
2489         GtkWidget *folder_view = NULL;
2490         TnyFolderStore *folder = NULL;
2491         gboolean result = FALSE;
2492
2493         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2494
2495         /* Get folder view */
2496         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2497                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2498         if (!folder_view)
2499                 return FALSE;
2500         
2501         /* Get selected folder as destination folder */
2502         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2503         if (!(folder && TNY_IS_FOLDER(folder))) {
2504                 result = FALSE;
2505                 goto frees;
2506         }
2507         
2508         /* Check clipboard is cleared */
2509         clipboard = modest_runtime_get_email_clipboard ();
2510         if (modest_email_clipboard_cleared (clipboard)) {
2511                 result = FALSE;
2512                 goto frees;
2513         }
2514                 
2515         /* Check source folder */
2516         result = modest_email_clipboard_check_source_folder (clipboard, TNY_FOLDER (folder));
2517         
2518         /* Free */
2519  frees:
2520         g_object_unref (folder);
2521         
2522         return result;
2523 }
2524
2525 static gboolean
2526 _selected_folder_is_any_of_type (ModestWindow *win,
2527                                  TnyFolderType types[], 
2528                                  guint ntypes)
2529 {
2530         GtkWidget *folder_view = NULL;
2531         TnyFolderStore *folder = NULL;
2532         TnyFolderType folder_type;
2533         guint i=0;
2534         gboolean result = FALSE;
2535
2536         /*Get current folder */
2537         if (MODEST_IS_MAIN_WINDOW(win)) {
2538
2539                 /* Get folder view */
2540                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2541                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2542                 /* If no folder view, always dimmed */
2543                 if (!folder_view)
2544                         return FALSE;
2545         
2546                 /* Get selected folder as parent of new folder to create */
2547                 folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2548
2549                 if (!(folder && TNY_IS_FOLDER(folder))) {
2550                         if (folder)
2551                                 g_object_unref (folder);
2552                         return FALSE;
2553                 }
2554                 
2555                 /* Check folder type */
2556                 result = _folder_is_any_of_type (TNY_FOLDER(folder), types, ntypes);
2557         }
2558         else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
2559                 folder_type = modest_msg_view_window_get_folder_type (MODEST_MSG_VIEW_WINDOW (win));
2560                 for (i=0; i < ntypes; i++) {
2561                         result = result || folder_type == types[i];
2562                 }
2563         }
2564
2565
2566         /* free */
2567         if (folder)
2568                 g_object_unref (folder);
2569
2570         return result;  
2571 }
2572
2573 static gboolean
2574 _folder_is_any_of_type (TnyFolder *folder,
2575                         TnyFolderType types[], 
2576                         guint ntypes)
2577 {
2578         TnyFolderType folder_type;
2579         gboolean result = FALSE;
2580         guint i;
2581
2582         g_return_val_if_fail (TNY_IS_FOLDER(folder), FALSE);
2583
2584         /* Get folder type */
2585         folder_type = modest_tny_folder_guess_folder_type (folder);
2586         if (folder_type == TNY_FOLDER_TYPE_INVALID)
2587                 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
2588         
2589         /* Check foler type */
2590         for (i=0; i < ntypes; i++) {
2591                 result = result || folder_type == types[i];
2592         }
2593
2594         return result;
2595 }
2596
2597 static gboolean
2598 _clipboard_is_empty (ModestWindow *win)
2599 {
2600         gboolean result = FALSE;
2601         
2602         if (MODEST_IS_MAIN_WINDOW (win)) {
2603                 ModestEmailClipboard *clipboard = NULL;
2604                 clipboard = modest_runtime_get_email_clipboard ();
2605                 if (modest_email_clipboard_cleared (clipboard)) 
2606                  result = TRUE;  
2607         }
2608
2609         return result;
2610 }
2611
2612 static gboolean
2613 _invalid_clipboard_selected (ModestWindow *win,
2614                              ModestDimmingRule *rule) 
2615 {
2616         gboolean result = FALSE;
2617         GtkWidget *focused = NULL;
2618
2619         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
2620
2621         /* Get focuesed widget */
2622         focused = gtk_window_get_focus (GTK_WINDOW (win));
2623
2624         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
2625                 gboolean has_selection = FALSE;
2626                 if (GTK_IS_TEXT_VIEW (focused)) {
2627                         GtkTextBuffer *buffer = NULL;
2628                         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused));
2629                         has_selection = modest_text_utils_buffer_selection_is_valid (buffer);
2630                 } else if (GTK_IS_EDITABLE (focused)) {
2631                         has_selection = gtk_editable_get_selection_bounds (GTK_EDITABLE (focused), NULL, NULL);
2632                 }
2633                 result = !has_selection;
2634         } else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2635                 if (focused) {
2636                         MODEST_DEBUG_BLOCK (g_debug ("FOCUSED %s", g_type_name (G_TYPE_FROM_INSTANCE (focused))););
2637                         if (GTK_IS_LABEL (focused) && 
2638                             !gtk_label_get_selection_bounds (GTK_LABEL (focused), NULL, NULL)) {
2639                                 result = TRUE;
2640                         } else if (GTK_IS_TEXT_VIEW (focused)) {
2641                                 GtkTextBuffer *buffer;
2642                                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused));
2643                                 result = !gtk_text_buffer_get_has_selection (buffer);
2644                         } else if (GTK_IS_HTML (focused)) {
2645                                 const gchar *sel;
2646                                 int len = -1;
2647                                 sel = gtk_html_get_selection_html (GTK_HTML (focused), &len);
2648                                 result = ((sel == NULL) || (sel[0] == '\0'));
2649                         } else if (MODEST_IS_ATTACHMENTS_VIEW (focused)) {
2650                                 result = TRUE;
2651                         } else {
2652                                 GtkClipboard *clipboard;
2653                                 gchar *selection;
2654
2655                                 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
2656                                 /* Get clipboard selection*/
2657                                 selection = gtk_clipboard_wait_for_text (clipboard);
2658                                 /* Check dimming */
2659                                 result = (selection == NULL);
2660                                 g_free (selection);
2661                         } 
2662                 } else {
2663                         result = TRUE;
2664                 }
2665                 
2666                 if (result)
2667                         modest_dimming_rule_set_notification (rule, "");
2668                 
2669         }               
2670         else if (MODEST_IS_MAIN_WINDOW (win)) {
2671                 const DimmedState *state = NULL;
2672
2673                 /* Check dimming */
2674                 state = modest_window_get_dimming_state (win);
2675                 if (state)
2676                         result = state->n_selected == 0;
2677                 if (result)
2678                         modest_dimming_rule_set_notification (rule, _("mcen_ib_no_message_selected"));                  
2679         }
2680         
2681         return result;
2682 }
2683
2684
2685 static gboolean
2686 _invalid_attach_selected (ModestWindow *win,
2687                           gboolean unique,
2688                           gboolean for_view,
2689                           gboolean for_remove,
2690                           ModestDimmingRule *rule) 
2691 {
2692         TnyList *attachments;
2693         gint n_selected;
2694         TnyHeaderFlags flags;
2695         gboolean nested_attachments = FALSE;
2696         gboolean selected_messages = FALSE;
2697         gboolean result = FALSE;
2698
2699         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
2700
2701         if (MODEST_IS_MAIN_WINDOW (win)) {
2702                 flags = TNY_HEADER_FLAG_ATTACHMENTS;
2703                 if (!result) {
2704                         const DimmedState *state = NULL;
2705                         state = modest_window_get_dimming_state (win);
2706                         result = !state->any_has_attachments;
2707                 }
2708         }
2709         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2710                 
2711                 /* Get selected atachments */
2712                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
2713                 n_selected = tny_list_get_length (attachments);
2714
2715                 /* Check unique */              
2716                 if (!result) {
2717                         if (unique) 
2718                                 result = n_selected != 1;
2719                         else
2720                                 
2721                                 result = n_selected < 1;
2722                 }
2723                 
2724                 /* Check attached type (view operation not required) */
2725                 if (!result && !for_view)  {
2726                         TnyIterator *iter;
2727                         iter = tny_list_create_iterator (attachments);
2728                         while (!tny_iterator_is_done (iter) && !result) {
2729 #ifdef MODEST_TOOLKIT_HILDON2
2730                                 gboolean not_selectable = FALSE;
2731 #endif
2732                                 TnyMimePart *mime_part = TNY_MIME_PART (tny_iterator_get_current (iter));
2733                                 TnyList *nested_list = tny_simple_list_new ();
2734                                 tny_mime_part_get_parts (mime_part, nested_list);
2735
2736                                 if (!for_remove && modest_tny_mime_part_is_msg (mime_part)) {
2737                                         TnyMsg *window_msg;
2738                                         window_msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
2739                                         if ((TnyMimePart *) window_msg != mime_part) {
2740                                                 selected_messages = TRUE;
2741 #ifdef MODEST_TOOLKIT_HILDON2
2742                                                 not_selectable = TRUE;
2743 #else
2744                                                 result = TRUE;
2745 #endif
2746                                         }
2747                                         g_object_unref (window_msg);
2748                                 }
2749                                 if (!for_remove && tny_list_get_length (nested_list) > 0) {
2750                                         nested_attachments = TRUE;
2751 #ifdef MODEST_TOOLKIT_HILDON2
2752                                         not_selectable = TRUE;
2753 #else
2754                                         result = TRUE;
2755 #endif
2756                                 }
2757 #ifdef MODEST_TOOLKIT_HILDON2
2758                                 if (not_selectable)
2759                                         n_selected --;
2760 #endif
2761                                 g_object_unref (nested_list);
2762                                 g_object_unref (mime_part);
2763                                 tny_iterator_next (iter);
2764                         }
2765                         g_object_unref (iter);
2766                 }
2767
2768                 /* No valid attachment available */
2769                 if (n_selected == 0)
2770                         result = TRUE;
2771                 
2772                 /* Set notifications */
2773                 if (result && rule != NULL) {
2774                         if (selected_messages) {
2775                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_save_attach_mail"));
2776                         } else if (nested_attachments) {
2777                                 modest_dimming_rule_set_notification (rule, _("FIXME:unable to save attachments with nested elements"));
2778                         } else if (n_selected == 0) {
2779                                 modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
2780                         } else if (unique) {
2781                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_display_more"));
2782                         }
2783                 }
2784                 
2785                 /* Free */
2786                 g_object_unref (attachments);
2787         }
2788
2789         return result;
2790 }
2791
2792 static gboolean
2793 _purged_attach_selected (ModestWindow *win, gboolean all, ModestDimmingRule *rule) 
2794 {
2795         TnyList *attachments = NULL;
2796         TnyIterator *iter;
2797         gint purged = 0;
2798         gint n_attachments = 0;
2799         gboolean result = FALSE;
2800
2801         /* This should check if _all_ the attachments are already purged. If only some
2802          * of them are purged, then it does not cause dim as there's a confirmation dialog
2803          * for removing only local attachments */
2804
2805         /* Get selected atachments */
2806         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2807                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
2808         } else if (MODEST_IS_MAIN_WINDOW (win)) {
2809                 /* If we're in main window, we won't know if there are already purged attachments */
2810                 return FALSE;
2811         }
2812
2813         if (attachments == NULL)
2814                 return FALSE;
2815
2816         if (tny_list_get_length (attachments) == 0) {
2817                 g_object_unref (attachments);
2818                 return FALSE;
2819         }
2820
2821         iter = tny_list_create_iterator (attachments);
2822         while (!tny_iterator_is_done (iter)) {
2823                 TnyMimePart *mime_part = TNY_MIME_PART (tny_iterator_get_current (iter));
2824                 if (tny_mime_part_is_purged (mime_part)) {
2825                         purged++;
2826                 }
2827                 n_attachments++;
2828                 g_object_unref (mime_part);
2829                 tny_iterator_next (iter);
2830         }
2831         g_object_unref (iter);
2832                 
2833         /* Free */
2834         g_object_unref (attachments);
2835
2836         if (all)
2837                 result = (purged == n_attachments);
2838         else
2839                 result = (purged > 0);
2840
2841         /* This string no longer exists, refer to NB#75415 for more info
2842         if (result && (rule != NULL))
2843                 modest_dimming_rule_set_notification (rule, _("mail_ib_attachment_already_purged"));
2844         */
2845
2846         return result;
2847 }
2848
2849 static gboolean
2850 _invalid_msg_selected (ModestMainWindow *win,
2851                        gboolean unique,
2852                        ModestDimmingRule *rule) 
2853 {
2854         GtkWidget *folder_view = NULL;
2855         const DimmedState *state = NULL;
2856         gboolean result = FALSE;
2857         gint n_selected = 0;
2858
2859         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2860         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (rule), FALSE);
2861
2862         state = modest_window_get_dimming_state (MODEST_WINDOW(win));
2863         if (state)
2864                 n_selected = state->n_selected;
2865
2866         /* Get folder view to check focus */
2867         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
2868                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2869
2870         /* Check dimmed rule (TODO: check focus on widgets */   
2871         if (!result) {
2872                 result = ((n_selected == 0 ) ||
2873                           (gtk_widget_is_focus (folder_view)));
2874                 if (result)
2875                         modest_dimming_rule_set_notification (rule, _("mcen_ib_no_message_selected"));
2876         }
2877         if (!result && unique) {
2878                 result = n_selected > 1;
2879                 if (result)
2880                         modest_dimming_rule_set_notification (rule, _("mcen_ib_select_one_message"));
2881         }
2882
2883         return result;
2884 }
2885
2886
2887 static gboolean
2888 _msg_download_in_progress (ModestWindow *win)
2889 {
2890         gboolean result = FALSE;
2891
2892         g_return_val_if_fail (MODEST_IS_WINDOW (win), FALSE);
2893
2894         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2895                 result = modest_msg_view_window_toolbar_on_transfer_mode (MODEST_MSG_VIEW_WINDOW(win));
2896         }
2897         else if (MODEST_IS_MAIN_WINDOW (win)) {
2898                 result = modest_main_window_transfer_mode_enabled (MODEST_MAIN_WINDOW(win));
2899         }
2900
2901         return result;
2902 }
2903
2904 static gboolean
2905 _msg_download_completed (ModestMainWindow *win)
2906 {
2907         const DimmedState *state = modest_window_get_dimming_state (MODEST_WINDOW(win));
2908         return (state) ? state->any_marked_as_cached : TRUE;
2909 }
2910
2911 static void 
2912 fill_list_of_caches (gpointer key, gpointer value, gpointer userdata)
2913 {
2914         GSList **send_queues = (GSList **) userdata;
2915         *send_queues = g_slist_prepend (*send_queues, value);
2916 }
2917
2918 static gboolean
2919 _selected_msg_sent_in_progress (ModestWindow *win)
2920 {
2921         const DimmedState *state = modest_window_get_dimming_state (win);
2922         return (state) ? state->sent_in_progress : TRUE;
2923 }
2924
2925
2926 static gboolean
2927 _invalid_folder_for_purge (ModestWindow *win, 
2928                            ModestDimmingRule *rule)
2929 {
2930         TnyFolder *folder = NULL;
2931         TnyAccount *account = NULL;
2932         gboolean result = FALSE;
2933
2934         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2935
2936                 /* Get folder and account of message */
2937                 TnyMsg *msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
2938                 g_return_val_if_fail(msg != NULL, TRUE);                        
2939                 folder = tny_msg_get_folder (msg);      
2940                 g_object_unref (msg);
2941                 if (folder == NULL) {
2942                         result = TRUE;
2943                         goto frees;
2944                 }
2945         } else if (MODEST_IS_MAIN_WINDOW (win)) {
2946                 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
2947                                                                               MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2948                 if (!folder_view)
2949                         return FALSE;
2950                 folder = (TnyFolder *) modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
2951                 /* Could be a folder store */
2952                 if (folder == NULL || ! TNY_IS_FOLDER (folder))
2953                         goto frees;             
2954         } else {
2955                 g_return_val_if_reached (FALSE);
2956         }
2957         account = modest_tny_folder_get_account (folder);
2958         if (account == NULL) goto frees;                        
2959                 
2960         /* Check account */
2961         if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (account))) {
2962                 TnyFolderType types[2];
2963                 types[0] = TNY_FOLDER_TYPE_DRAFTS;
2964                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
2965                 
2966                 if (_selected_folder_is_any_of_type (win, types, 2)) {
2967                         result = TRUE;
2968                 }
2969         } else {
2970                 ModestProtocolType protocol_type = modest_tny_account_get_protocol_type (TNY_ACCOUNT (account));
2971                 /* If it's a remote folder then dim */
2972                 if (modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
2973                                                                     protocol_type,
2974                                                                     MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS)) {
2975                         result = TRUE;
2976                 }
2977         }
2978         
2979 frees:
2980         if (folder != NULL)
2981                 g_object_unref (folder);
2982         if (account != NULL)
2983                 g_object_unref (account);
2984         
2985         return result;
2986 }
2987
2988 static gboolean
2989 _transfer_mode_enabled (ModestWindow *win)
2990 {
2991         gboolean result = FALSE;
2992
2993         /* Check dimming */
2994         if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
2995                 result = modest_msg_view_window_transfer_mode_enabled (MODEST_MSG_VIEW_WINDOW (win));
2996         } else if (MODEST_IS_MAIN_WINDOW(win)) {
2997                 result = modest_main_window_transfer_mode_enabled (MODEST_MAIN_WINDOW (win));
2998 #ifdef MODEST_TOOLKIT_HILDON2
2999         } else if (MODEST_IS_FOLDER_WINDOW (win)) {
3000                 result = modest_folder_window_transfer_mode_enabled (MODEST_FOLDER_WINDOW (win));
3001         } else if (MODEST_IS_HEADER_WINDOW (win)) {
3002                 result = modest_header_window_transfer_mode_enabled (MODEST_HEADER_WINDOW (win));
3003 #endif
3004         } else {
3005                 g_warning("_transfer_mode_enabled called with wrong window type");
3006         }
3007
3008         return result;
3009 }
3010
3011 static gboolean
3012 _selected_folder_has_subfolder_with_same_name (ModestWindow *win)
3013 {
3014         GtkWidget *folder_view = NULL;
3015         TnyFolderStore *folder = NULL;
3016         ModestEmailClipboard *clipboard = NULL;
3017         const gchar *folder_name = NULL;
3018         gboolean result = FALSE;
3019
3020         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), TRUE);
3021
3022         /*Get current parent folder */
3023         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
3024                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
3025         /* If no folder view, always dimmed */
3026         if (!folder_view) return FALSE;
3027         
3028         /* Get selected folder as parent of new folder to create */
3029         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));     
3030         if (!(folder && TNY_IS_FOLDER(folder))) goto frees;
3031         
3032         /* get modest clipboard and source folder */
3033         clipboard = modest_runtime_get_email_clipboard ();
3034         folder_name = modest_email_clipboard_get_folder_name (clipboard);
3035         if (folder_name == NULL) goto frees;
3036
3037         /* Check source subfolders names */
3038         result = modest_tny_folder_has_subfolder_with_name (folder, folder_name,
3039                                                             TRUE);
3040                 
3041         /* Free */
3042  frees:
3043         if (folder != NULL) 
3044                 g_object_unref (folder);
3045
3046
3047         return result;
3048 }
3049
3050 static gboolean
3051 _all_msgs_in_sending_status (ModestHeaderView *header_view)
3052 {
3053         GtkTreeModel *model;
3054         GtkTreeIter iter;
3055         gboolean all_sending = TRUE;
3056
3057         model = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
3058         if (gtk_tree_model_get_iter_first (model, &iter)) {
3059                 do {
3060                         TnyHeader *header;
3061
3062                         gtk_tree_model_get (model, &iter,
3063                                             TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN,
3064                                             &header,
3065                                             -1);
3066
3067                         if (header) {
3068                                 if (modest_tny_all_send_queues_get_msg_status (header) !=
3069                                     MODEST_TNY_SEND_QUEUE_SENDING)
3070                                         all_sending = FALSE;
3071                                 g_object_unref (header);
3072                         }
3073
3074                 } while (all_sending && gtk_tree_model_iter_next (model, &iter));
3075         }
3076         return all_sending;
3077 }
3078
3079 gboolean 
3080 modest_ui_dimming_rules_on_save_to_drafts (ModestWindow *win, 
3081                                            gpointer user_data)
3082 {
3083         ModestDimmingRule *rule = NULL;
3084
3085         g_return_val_if_fail (MODEST_MSG_EDIT_WINDOW (win), FALSE);
3086         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
3087         rule = MODEST_DIMMING_RULE (user_data);
3088
3089         /* Check dimmed rule */ 
3090         return !modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (win));
3091 }
3092
3093 gboolean
3094 modest_ui_dimming_rules_on_insert_image (ModestWindow *win,
3095                                          gpointer user_data)
3096 {
3097         g_return_val_if_fail (MODEST_MSG_EDIT_WINDOW (win), FALSE);
3098
3099         ModestMsgEditFormat format =
3100           modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (win));
3101
3102         return (format != MODEST_MSG_EDIT_FORMAT_HTML);
3103 }
3104
3105 static gboolean 
3106 _send_receive_in_progress (ModestWindow *win)
3107 {
3108         ModestMailOperationQueue *queue;
3109         GSList *op_list, *node;
3110         gboolean found_send_receive;
3111
3112         queue = modest_runtime_get_mail_operation_queue ();
3113         op_list = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (win));
3114
3115         found_send_receive = FALSE;
3116         for (node = op_list; node != NULL; node = g_slist_next (node)) {
3117                 ModestMailOperation *op;
3118
3119                 op = (ModestMailOperation *) node->data;
3120                 if (modest_mail_operation_get_type_operation (op) == MODEST_MAIL_OPERATION_TYPE_SEND_AND_RECEIVE) {
3121                         found_send_receive = TRUE;
3122                         break;
3123                 }
3124         }
3125
3126         if (op_list) {
3127                 g_slist_foreach (op_list, (GFunc) g_object_unref, NULL);
3128                 g_slist_free (op_list);
3129         }
3130
3131         return found_send_receive;
3132 }
3133
3134 static gboolean
3135 _msgs_send_in_progress (void)
3136 {
3137         ModestCacheMgr *cache_mgr;
3138         GHashTable *send_queue_cache;
3139         ModestTnySendQueue *send_queue;
3140         GSList *send_queues = NULL, *node = NULL;
3141         gboolean found = FALSE;
3142
3143         cache_mgr = modest_runtime_get_cache_mgr ();
3144         send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
3145                                                        MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
3146
3147         g_hash_table_foreach (send_queue_cache, (GHFunc) fill_list_of_caches, &send_queues);
3148
3149         for (node = send_queues; node != NULL && !found; node = g_slist_next (node)) {
3150                 send_queue = MODEST_TNY_SEND_QUEUE (node->data);
3151
3152                 /* Check if msg uid is being processed inside send queue */
3153                 if (modest_tny_send_queue_sending_in_progress (send_queue)) {
3154                         found = TRUE;
3155                         break;
3156                 }
3157         }
3158
3159         g_slist_free (send_queues);
3160
3161         return found;
3162 }
3163
3164 /*****************************************************************************/
3165 /********************** HILDON2 only dimming rules ***************************/
3166 /*****************************************************************************/
3167
3168 #ifdef MODEST_TOOLKIT_HILDON2
3169 gboolean 
3170 modest_ui_dimming_rules_on_header_window_move_to (ModestWindow *win, gpointer user_data)
3171 {
3172         ModestDimmingRule *rule = NULL;
3173         gboolean dimmed = FALSE;
3174
3175         g_return_val_if_fail (MODEST_IS_HEADER_WINDOW(win), FALSE);
3176         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
3177         rule = MODEST_DIMMING_RULE (user_data);
3178
3179         /* Check dimmed rule */ 
3180         dimmed = _transfer_mode_enabled (win);
3181         if (dimmed)
3182                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
3183
3184         if (!dimmed)
3185                 dimmed = _forbid_outgoing_xfers (win);
3186
3187         if (!dimmed) {
3188                 GtkWidget *header_view;
3189                 TnyFolder *folder;
3190
3191                 header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (win)));
3192                 folder = modest_header_view_get_folder (MODEST_HEADER_VIEW (header_view));
3193                 if (folder) {
3194                         dimmed = (tny_folder_get_all_count (TNY_FOLDER (folder)) == 0) ||
3195                                 modest_header_view_is_empty (MODEST_HEADER_VIEW (header_view));
3196
3197                         if (!dimmed &&
3198                             (tny_folder_get_folder_type (TNY_FOLDER (folder)) == TNY_FOLDER_TYPE_OUTBOX)) {
3199                                 dimmed = _all_msgs_in_sending_status (MODEST_HEADER_VIEW (header_view));;
3200                         }
3201                         g_object_unref (folder);
3202                 }
3203         }
3204
3205         return dimmed;
3206 }
3207
3208 gboolean 
3209 modest_ui_dimming_rules_on_folder_window_move_to (ModestWindow *win, gpointer user_data)
3210 {
3211         ModestDimmingRule *rule = NULL;
3212         gboolean dimmed = FALSE;
3213
3214         g_return_val_if_fail (MODEST_IS_FOLDER_WINDOW(win), FALSE);
3215         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
3216         rule = MODEST_DIMMING_RULE (user_data);
3217
3218         /* Check dimmed rule */ 
3219         dimmed = _transfer_mode_enabled (win);
3220         if (dimmed)
3221                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
3222
3223         if (!dimmed)
3224                 dimmed = _forbid_outgoing_xfers (win);
3225
3226         if (!dimmed && MODEST_IS_FOLDER_WINDOW (win)) {
3227                 ModestFolderView *folder_view;
3228                 folder_view = modest_folder_window_get_folder_view (MODEST_FOLDER_WINDOW (win));
3229                 dimmed = !modest_folder_view_any_folder_fulfils_rules (folder_view,
3230                                                                        MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE);
3231         }
3232
3233         if (!dimmed) {
3234                 dimmed = _transfer_mode_enabled (win);
3235                 if (dimmed)
3236                         modest_dimming_rule_set_notification (rule, "");
3237  &n