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