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