* Fixes NB#100523, dimm send&receive when there are no defined accounts
[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 the viewer is showing a message sent as attachment */
927         if (!dimmed)
928                 dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW (win));
929
930         if (!dimmed) {
931                 flags = tny_header_get_flags (header);
932                 if (flags & TNY_HEADER_FLAG_SEEN)
933                         dimmed = TRUE;
934         }
935
936         if (header)
937                 g_object_unref (header);
938         return dimmed;
939 }
940
941
942 gboolean 
943 modest_ui_dimming_rules_on_mark_as_unread_msg_in_view (ModestWindow *win, gpointer user_data)
944 {
945         ModestDimmingRule *rule = NULL;
946         TnyHeader *header;
947         TnyHeaderFlags flags;
948         gboolean dimmed = FALSE;
949         
950
951         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
952         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
953         rule = MODEST_DIMMING_RULE (user_data);
954         
955         header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
956         if (!header) {
957                 dimmed = TRUE;
958         }
959
960         /* If the viewer is showing a message sent as attachment */
961         if (!dimmed)
962                 dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW (win));
963
964         if (!dimmed) {
965                 flags = tny_header_get_flags (header);
966                 if (!(flags & TNY_HEADER_FLAG_SEEN))
967                         dimmed = TRUE;
968         }
969
970         if (header)
971                 g_object_unref (header);
972         return dimmed;
973 }
974
975 gboolean 
976 modest_ui_dimming_rules_on_mark_as_read_msg (ModestWindow *win, gpointer user_data)
977 {
978         ModestDimmingRule *rule = NULL;
979         TnyHeaderFlags flags;
980         const DimmedState *state = NULL;
981         gboolean dimmed = FALSE;
982
983         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
984         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
985         rule = MODEST_DIMMING_RULE (user_data);
986         state = modest_window_get_dimming_state (win);          
987         
988         flags = TNY_HEADER_FLAG_SEEN; 
989
990         /* Check dimmed rule */ 
991         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);     
992         if (!dimmed) {
993                 dimmed = state->all_marked_as_seen;
994                 if (dimmed)
995                         modest_dimming_rule_set_notification (rule, "");
996         }       
997
998         return dimmed;
999 }
1000
1001 gboolean 
1002 modest_ui_dimming_rules_on_mark_as_unread_msg (ModestWindow *win, gpointer user_data)
1003 {
1004         ModestDimmingRule *rule = NULL;
1005         TnyHeaderFlags flags;
1006         const DimmedState *state = NULL;
1007         gboolean dimmed = FALSE;
1008
1009         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1010         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1011         rule = MODEST_DIMMING_RULE (user_data);
1012         state = modest_window_get_dimming_state (win);          
1013         
1014         flags = TNY_HEADER_FLAG_SEEN; 
1015
1016         /* Check dimmed rule */ 
1017         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
1018         if (!dimmed) {
1019                 dimmed = !state->any_marked_as_seen;
1020                 if (dimmed)
1021                         modest_dimming_rule_set_notification (rule, "");
1022         }
1023
1024         return dimmed;
1025 }
1026
1027 gboolean 
1028 modest_ui_dimming_rules_on_move_to (ModestWindow *win, gpointer user_data)
1029 {
1030         ModestDimmingRule *rule = NULL;
1031         gboolean dimmed = FALSE;
1032
1033         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1034         rule = MODEST_DIMMING_RULE (user_data);
1035
1036         if (MODEST_IS_MAIN_WINDOW (win)) 
1037                 dimmed = modest_ui_dimming_rules_on_main_window_move_to (win, user_data);
1038 #ifdef MODEST_TOOLKIT_HILDON2
1039         else if (MODEST_IS_HEADER_WINDOW (win))
1040                 dimmed = modest_ui_dimming_rules_on_header_window_move_to (win, user_data);
1041 #endif
1042         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) 
1043                  dimmed = modest_ui_dimming_rules_on_view_window_move_to (win, user_data);
1044
1045         return dimmed;
1046 }
1047
1048
1049 gboolean 
1050 modest_ui_dimming_rules_on_main_window_move_to (ModestWindow *win, gpointer user_data)
1051 {
1052         GtkWidget *folder_view = NULL;
1053         ModestDimmingRule *rule = NULL;
1054         const DimmedState *state = NULL;
1055         gboolean dimmed = FALSE;
1056         
1057         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), TRUE);
1058         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1059         rule = MODEST_DIMMING_RULE (user_data);
1060         state = modest_window_get_dimming_state (win);          
1061         
1062         /* Get the folder view */
1063         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
1064                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1065
1066         
1067         /* Check diming rules for folders transfer  */
1068         if (folder_view && gtk_widget_is_focus (folder_view)) {
1069                 TnyFolderType types[5];
1070                 
1071                 types[0] = TNY_FOLDER_TYPE_DRAFTS; 
1072                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
1073                 types[2] = TNY_FOLDER_TYPE_SENT;
1074                 types[3] = TNY_FOLDER_TYPE_ROOT; 
1075                 types[4] = TNY_FOLDER_TYPE_INBOX; 
1076                 
1077                 /* Apply folder rules */        
1078                 dimmed = _selected_folder_not_moveable (MODEST_MAIN_WINDOW(win));
1079                 if (dimmed)
1080                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));           
1081                 if (!dimmed) {
1082                         dimmed = _selected_folder_is_any_of_type (win, types, 5);
1083                         if (dimmed)
1084                                 modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));
1085                 }
1086         }
1087         
1088         /* Check diming rules for messages transfer  */
1089         if (!dimmed) {
1090                 dimmed = (state->already_opened_msg > 0) ? TRUE : FALSE;
1091                 if (dimmed) {
1092                         gchar *message = g_strdup_printf(_("emev_nc_unabletomove_items"),
1093                                                          state->already_opened_msg);
1094                         modest_dimming_rule_set_notification (rule, message);
1095                         g_free(message);
1096                 }
1097         }
1098         if (!dimmed) {
1099                 if (!(folder_view && gtk_widget_is_focus (folder_view)))
1100                         dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW(win), FALSE, user_data);
1101                 
1102         }
1103         if (!dimmed) {
1104                 dimmed = _selected_msg_sent_in_progress (win);
1105                 if (dimmed)
1106                         modest_dimming_rule_set_notification (rule, _("sfil_ib_unable_to_move_selected_items"));
1107         }
1108         
1109         return dimmed;
1110 }
1111
1112 gboolean 
1113 modest_ui_dimming_rules_on_view_window_move_to (ModestWindow *win, gpointer user_data)
1114 {
1115         ModestDimmingRule *rule = NULL;
1116         gboolean dimmed = FALSE;
1117
1118         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1119         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1120         rule = MODEST_DIMMING_RULE (user_data);
1121
1122         /* Check dimmed rule */ 
1123         dimmed = _transfer_mode_enabled (win);                  
1124         if (dimmed)
1125                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));     
1126         if (!dimmed) {
1127                 dimmed = modest_window_get_dimming_state (win)->any_marked_as_deleted;
1128                 if (dimmed) {
1129                         gchar *msg = modest_ui_actions_get_msg_already_deleted_error_msg (win);
1130                         modest_dimming_rule_set_notification (rule, msg);
1131                         g_free (msg);
1132                 }
1133         }
1134         
1135         if (!dimmed) {
1136                 dimmed = _selected_msg_sent_in_progress (win);
1137                 if (dimmed)
1138                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_item"));
1139         }
1140
1141         /* This could happen if we're viewing a message of the outbox
1142            that has been already sent */
1143         if (!dimmed)
1144                 dimmed = _message_already_sent (MODEST_MSG_VIEW_WINDOW(win));
1145
1146         if (!dimmed) {
1147                 if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
1148                         /* The move_to button should be dimmed when viewing an attachment,
1149                          * but should be enabled when viewing a message from the list, 
1150                          * or when viewing a search result.
1151                          */
1152                         if (!modest_msg_view_window_is_search_result (MODEST_MSG_VIEW_WINDOW(win))) {
1153                                 dimmed = !modest_msg_view_window_has_headers_model (MODEST_MSG_VIEW_WINDOW (win));
1154                         }
1155                 }
1156                 
1157                 if (dimmed) 
1158                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_item"));
1159         }
1160
1161         return dimmed;
1162 }
1163
1164 #ifdef MODEST_TOOLKIT_HILDON2
1165 gboolean 
1166 modest_ui_dimming_rules_on_header_window_move_to (ModestWindow *win, gpointer user_data)
1167 {
1168         ModestDimmingRule *rule = NULL;
1169         gboolean dimmed = FALSE;
1170
1171         g_return_val_if_fail (MODEST_IS_HEADER_WINDOW(win), FALSE);
1172         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1173         rule = MODEST_DIMMING_RULE (user_data);
1174
1175         /* Check dimmed rule */ 
1176         dimmed = _transfer_mode_enabled (win);                  
1177         if (dimmed)
1178                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));     
1179
1180         if (!dimmed) {
1181                 GtkWidget *header_view;
1182                 TnyFolder *folder;
1183
1184                 header_view = GTK_WIDGET (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (win)));
1185                 folder = modest_header_view_get_folder (MODEST_HEADER_VIEW (header_view));
1186                 if (folder) {
1187                         dimmed = (tny_folder_get_all_count (TNY_FOLDER (folder)) == 0);
1188                         g_object_unref (folder);
1189                 }
1190         }
1191
1192         return dimmed;
1193 }
1194 #endif
1195
1196 gboolean 
1197 modest_ui_dimming_rules_on_find_msg (ModestWindow *win, gpointer user_data)
1198 {
1199         ModestDimmingRule *rule = NULL;
1200         gboolean dimmed = FALSE;
1201
1202         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1203         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1204         rule = MODEST_DIMMING_RULE (user_data);
1205
1206         /* Check dimmed rule */ 
1207         dimmed = _transfer_mode_enabled (win);                  
1208         if (dimmed)
1209                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));     
1210
1211         return dimmed;
1212 }
1213
1214 gboolean 
1215 modest_ui_dimming_rules_on_paste (ModestWindow *win, gpointer user_data)
1216 {
1217         ModestDimmingRule *rule = NULL;
1218         TnyFolderType types[3];
1219         gboolean dimmed = FALSE;
1220         
1221         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1222         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1223         rule = MODEST_DIMMING_RULE (user_data);
1224
1225         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
1226         types[1] = TNY_FOLDER_TYPE_OUTBOX;
1227         types[2] = TNY_FOLDER_TYPE_SENT;
1228         
1229         /* Check dimmed rule */ 
1230         dimmed = _clipboard_is_empty (win);
1231         if (dimmed)
1232                 modest_dimming_rule_set_notification (rule, 
1233                                                       _CS("ecoc_ib_edwin_nothing_to_paste"));
1234         if (!dimmed) {
1235                 dimmed = _selected_folder_is_any_of_type (win, types, 3);
1236                 if (dimmed)
1237                         modest_dimming_rule_set_notification (rule, 
1238                                                               _CS("ckct_ib_unable_to_paste_here"));
1239         }
1240         if (!dimmed) {
1241                 dimmed = !_folder_view_has_focus (win);
1242                 if (dimmed)
1243                         modest_dimming_rule_set_notification (rule,
1244                                         _CS("ckct_ib_unable_to_paste_here"));
1245         }
1246         if (!dimmed) {
1247                 dimmed = _selected_folder_not_writeable (MODEST_MAIN_WINDOW(win), TRUE);
1248                 if (dimmed) 
1249                         modest_dimming_rule_set_notification (rule, 
1250                                                               _CS("ckct_ib_unable_to_paste_here"));
1251         }
1252         if (!dimmed) {
1253                 dimmed = _selected_folder_is_same_as_source (win);
1254                 if (dimmed)
1255                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_copy_target_error"));
1256         }
1257         if (!dimmed) {
1258                 dimmed = _selected_folder_has_subfolder_with_same_name (win);
1259                 if (dimmed)
1260                         modest_dimming_rule_set_notification (rule, _("mail_in_ui_folder_copy_target_error"));
1261         }
1262         
1263         return dimmed;
1264 }
1265
1266
1267 gboolean 
1268 modest_ui_dimming_rules_on_select_all (ModestWindow *win, gpointer user_data)
1269 {
1270         ModestDimmingRule *rule = NULL;
1271         gboolean dimmed = FALSE;
1272         GtkWidget *focused = NULL;
1273
1274         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
1275         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1276         rule = MODEST_DIMMING_RULE (user_data);
1277
1278         focused = gtk_window_get_focus (GTK_WINDOW (win));
1279
1280         /* Main window dimming rules */ 
1281         if (MODEST_IS_MAIN_WINDOW (win))
1282                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));
1283
1284         if (!dimmed && MODEST_IS_MAIN_WINDOW (win))
1285                 dimmed = _header_view_is_all_selected (MODEST_MAIN_WINDOW(win));
1286
1287         if (!dimmed && GTK_IS_ENTRY (focused)) {
1288                 const gchar *current_text;
1289                 current_text = gtk_entry_get_text (GTK_ENTRY (focused));
1290                 dimmed = ((current_text == NULL) || (current_text[0] == '\0'));
1291         }
1292
1293         if (!dimmed && GTK_IS_TEXT_VIEW (focused)) {
1294                 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused));
1295                 dimmed = (gtk_text_buffer_get_char_count (buffer) < 1);
1296         }
1297
1298         if (dimmed && MODEST_IS_ATTACHMENTS_VIEW (focused))
1299                 dimmed = FALSE;
1300         return dimmed;
1301 }
1302
1303 gboolean 
1304 modest_ui_dimming_rules_on_view_attachments (ModestWindow *win, gpointer user_data)
1305 {
1306         ModestDimmingRule *rule = NULL;
1307         gboolean dimmed = FALSE;
1308
1309         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1310         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1311         rule = MODEST_DIMMING_RULE (user_data);
1312
1313         /* Check dimmed rule */ 
1314         dimmed = _invalid_attach_selected (win, TRUE, TRUE, FALSE, rule);                       
1315
1316         if (!dimmed) {
1317                 dimmed = _purged_attach_selected (win, FALSE, NULL);
1318                 if (dimmed) {
1319                         modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
1320                 }
1321         }
1322                 
1323         return dimmed;
1324 }
1325
1326 gboolean 
1327 modest_ui_dimming_rules_on_save_attachments (ModestWindow *win, gpointer user_data)
1328 {
1329         ModestDimmingRule *rule = NULL;
1330         gboolean dimmed = FALSE;
1331
1332         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1333         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1334         rule = MODEST_DIMMING_RULE (user_data);
1335
1336         /* Check dimmed rule */ 
1337         dimmed = _invalid_attach_selected (win, FALSE, FALSE, FALSE, rule);
1338
1339         if (!dimmed) {
1340                 dimmed = _purged_attach_selected (win, FALSE, NULL);
1341                 if (dimmed) {
1342                         modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
1343                 }
1344         }
1345                 
1346         return dimmed;
1347 }
1348
1349 gboolean 
1350 modest_ui_dimming_rules_on_remove_attachments (ModestWindow *win, gpointer user_data)
1351 {
1352         ModestDimmingRule *rule = NULL;
1353         const DimmedState *state = NULL;
1354         gboolean dimmed = FALSE;
1355
1356         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
1357         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1358         rule = MODEST_DIMMING_RULE (user_data);
1359         state = modest_window_get_dimming_state (win);          
1360
1361         /* Check in main window if there's only one message selected */
1362         if (MODEST_IS_MAIN_WINDOW (win)) {
1363                 dimmed = _invalid_msg_selected (MODEST_MAIN_WINDOW (win), TRUE, rule);
1364         }
1365
1366         /* Check in view window if there's any attachment selected */
1367         if (!dimmed && MODEST_IS_MSG_VIEW_WINDOW (win)) {
1368                 dimmed = _invalid_attach_selected (win, FALSE, FALSE, TRUE, NULL);
1369                 if (dimmed)
1370                         modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
1371         }
1372
1373 #ifndef MODEST_TOOLKIT_HILDON2
1374         /* Messages as attachments could not be removed */
1375         if (!dimmed && MODEST_IS_MSG_VIEW_WINDOW (win)) {
1376                 TnyList *attachments;
1377                 TnyIterator *iter;
1378                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW (win));
1379                 if (attachments) {
1380                         iter = tny_list_create_iterator (attachments);
1381                         while (!tny_iterator_is_done (iter) && !dimmed) {
1382                                 TnyMimePart *mime_part = (TnyMimePart *)
1383                                         tny_iterator_get_current (iter);
1384                                 if (modest_tny_mime_part_is_msg (mime_part))
1385                                         dimmed = TRUE;
1386                                 g_object_unref (mime_part);
1387                                 tny_iterator_next (iter);
1388                         }
1389                         g_object_unref (iter);
1390                         g_object_unref (attachments);
1391                 }
1392         }
1393 #endif
1394
1395         if (!dimmed) {
1396
1397                 dimmed = _selected_msg_sent_in_progress (win);
1398                 if (dimmed) {
1399                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
1400                 }
1401         }
1402
1403         /* cannot purge in editable drafts nor pop folders */
1404         if (!dimmed) {
1405                 dimmed = _invalid_folder_for_purge (win, rule);
1406                 if (dimmed)
1407                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
1408         }
1409
1410         /* Check if the selected message in main window has attachments */
1411         if (!dimmed && MODEST_IS_MAIN_WINDOW (win)) {
1412                 dimmed = !(state->any_has_attachments);
1413                 if (dimmed)
1414                         modest_dimming_rule_set_notification (rule, _("mail_ib_unable_to_purge_attachments"));
1415         }
1416
1417         /* Check if all attachments are already purged */
1418         if (!dimmed) {
1419                 dimmed = _purged_attach_selected (win, TRUE, rule);
1420         }
1421
1422         /* Check if the message is already downloaded */
1423         if (!dimmed && MODEST_IS_MAIN_WINDOW (win)) {
1424                 dimmed = !_msg_download_completed (MODEST_MAIN_WINDOW (win));
1425                 if (dimmed)
1426                         modest_dimming_rule_set_notification (rule, _("mail_ib_attach_not_local"));
1427         }
1428
1429         return dimmed;
1430 }
1431
1432 gboolean 
1433 modest_ui_dimming_rules_on_undo (ModestWindow *win, gpointer user_data)
1434 {
1435         ModestDimmingRule *rule = NULL;
1436         gboolean dimmed = FALSE;
1437         
1438         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1439         rule = MODEST_DIMMING_RULE (user_data);
1440
1441         /* Check dimmed rule */ 
1442         if (MODEST_IS_MAIN_WINDOW (win)) {
1443                 dimmed = _clipboard_is_empty (win); 
1444                 if (dimmed)
1445                         modest_dimming_rule_set_notification (rule, "");
1446         }
1447
1448         if (!dimmed && MODEST_IS_MSG_EDIT_WINDOW (win)) {
1449                 dimmed = !modest_msg_edit_window_can_undo (MODEST_MSG_EDIT_WINDOW (win));
1450         }
1451                                 
1452         return dimmed;  
1453 }
1454
1455 gboolean 
1456 modest_ui_dimming_rules_on_redo (ModestWindow *win, gpointer user_data)
1457 {
1458         ModestDimmingRule *rule = NULL;
1459         gboolean dimmed = FALSE;
1460         
1461         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1462         rule = MODEST_DIMMING_RULE (user_data);
1463
1464         /* Check dimmed rule */ 
1465         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
1466                 dimmed = !modest_msg_edit_window_can_redo (MODEST_MSG_EDIT_WINDOW (win));
1467         }
1468                                 
1469         return dimmed;  
1470 }
1471
1472 gboolean 
1473 modest_ui_dimming_rules_on_cut (ModestWindow *win, gpointer user_data)
1474 {
1475         ModestDimmingRule *rule = NULL;
1476         const DimmedState *state = NULL;
1477         gboolean dimmed = FALSE;
1478         
1479         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1480         rule = MODEST_DIMMING_RULE (user_data);
1481         state = modest_window_get_dimming_state (win);
1482
1483         /* Check common dimming rules */
1484         dimmed = _invalid_clipboard_selected (win, rule);
1485
1486         /* Check window specific dimming rules */
1487         if (MODEST_IS_MAIN_WINDOW (win)) {
1488                 /* Get focused widget */
1489                 GtkWidget *focused = gtk_window_get_focus (GTK_WINDOW (win));
1490                 
1491                 if (MODEST_IS_HEADER_VIEW (focused)) {
1492                         if (!dimmed) { 
1493                                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
1494                                 if (dimmed)
1495                                         modest_dimming_rule_set_notification (rule, "");
1496                         }
1497                         if (!dimmed) {
1498                                 dimmed = _selected_msg_sent_in_progress (win);
1499                                 if (dimmed)
1500                                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_cut_mess"));
1501                         }
1502                         if (!dimmed) {
1503                                 dimmed = (state->already_opened_msg > 0) ? TRUE : FALSE;
1504                                 if(dimmed)
1505                                         modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_cut_mess"));
1506                         }
1507                 }
1508                 else if (MODEST_IS_FOLDER_VIEW (focused)) {
1509                         TnyFolderType types[3];
1510                         
1511                         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
1512                         types[1] = TNY_FOLDER_TYPE_OUTBOX;
1513                         types[2] = TNY_FOLDER_TYPE_SENT;
1514                         
1515                         /* Apply folder rules */        
1516                         if (!dimmed) {
1517                                 dimmed = _selected_folder_not_deletable (MODEST_MAIN_WINDOW(win));
1518                                 if (dimmed)
1519                                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));
1520                         }
1521                         if (!dimmed) {
1522                                 dimmed = _selected_folder_is_root_or_inbox (MODEST_MAIN_WINDOW(win));
1523                                 if (dimmed)
1524                                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));
1525                         }
1526                         if (!dimmed) {
1527                                 dimmed = _selected_folder_is_any_of_type (win, types, 3);
1528                                 if (dimmed)
1529                                         modest_dimming_rule_set_notification (rule, _("emev_nc_unabletomove_items"));
1530                         }
1531                 }
1532         }
1533
1534         return dimmed;
1535 }
1536
1537 gboolean 
1538 modest_ui_dimming_rules_on_copy (ModestWindow *win, gpointer user_data)
1539 {
1540         ModestDimmingRule *rule = NULL;
1541         const DimmedState *state = NULL;
1542         gboolean dimmed = FALSE;
1543
1544         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1545         rule = MODEST_DIMMING_RULE (user_data);
1546         state = modest_window_get_dimming_state (win);
1547
1548         /* Check common dimming rules */
1549         dimmed = _invalid_clipboard_selected (win, rule);       
1550         
1551         /* Check window specific dimming rules */
1552         if (MODEST_IS_MAIN_WINDOW (win)) {
1553                 /* Get focused widget */
1554                 GtkWidget *focused = gtk_window_get_focus (GTK_WINDOW (win));
1555                 
1556                 if (MODEST_IS_HEADER_VIEW (focused)) {
1557                         if (!dimmed) {
1558                                 dimmed = _selected_folder_is_empty (MODEST_MAIN_WINDOW(win));                   
1559                                 if (dimmed)
1560                                         modest_dimming_rule_set_notification (rule, "");
1561                         }               
1562                         if (!dimmed) {
1563                                 dimmed = _selected_msg_sent_in_progress (win);
1564                                 if (dimmed)
1565                                         modest_dimming_rule_set_notification (rule, _(""));
1566                         }
1567                         if (!dimmed) {
1568                                 dimmed = (state->already_opened_msg > 0) ? TRUE : FALSE;
1569                                 if(dimmed)
1570                                         modest_dimming_rule_set_notification (rule, _(""));
1571                         }
1572                 }
1573                 else if (MODEST_IS_FOLDER_VIEW (focused)) {
1574                         TnyFolderType types[3];
1575                         
1576                         types[0] = TNY_FOLDER_TYPE_DRAFTS; 
1577                         types[1] = TNY_FOLDER_TYPE_OUTBOX;
1578                         types[2] = TNY_FOLDER_TYPE_SENT;
1579
1580                         if (!dimmed) {
1581                                 dimmed = _selected_folder_is_root (MODEST_MAIN_WINDOW(win));
1582                                 if (dimmed)
1583                                         modest_dimming_rule_set_notification (rule, _(""));
1584                         }
1585                         if (!dimmed) {
1586                                 dimmed = _selected_folder_is_any_of_type (win, types, 3);
1587                                 if (dimmed)
1588                                         modest_dimming_rule_set_notification (rule, _(""));
1589                         }
1590                 }
1591         }
1592                 
1593         return dimmed;
1594 }
1595
1596 gboolean 
1597 modest_ui_dimming_rules_on_set_style (ModestWindow *win, gpointer user_data)
1598 {
1599         ModestDimmingRule *rule = NULL;
1600         const DimmedState *state = NULL;
1601         gboolean dimmed = FALSE;
1602         
1603         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1604         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1605         rule = MODEST_DIMMING_RULE (user_data);
1606         state = modest_window_get_dimming_state (win);
1607
1608
1609         /* Check common dimming rules */
1610         ModestMsgEditFormat format;
1611         format = modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (win));
1612
1613         dimmed = (format != MODEST_MSG_EDIT_FORMAT_HTML);
1614         if (dimmed)
1615                 modest_dimming_rule_set_notification (rule, _("mcen_ib_item_unavailable_plaintext"));
1616
1617         if (!dimmed) {
1618                 GtkWidget *body;
1619                 body = modest_msg_edit_window_get_child_widget (MODEST_MSG_EDIT_WINDOW (win),
1620                                                                 MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_BODY);
1621                 
1622                 dimmed = ((body == NULL)||(!gtk_widget_is_focus (body)));
1623                 if (dimmed)
1624                         modest_dimming_rule_set_notification (rule, _("mcen_ib_move_cursor_to_message"));
1625         }
1626                
1627         
1628         return dimmed;
1629 }
1630
1631 gboolean 
1632 modest_ui_dimming_rules_on_zoom (ModestWindow *win, gpointer user_data)
1633 {
1634         ModestDimmingRule *rule = NULL;
1635         const DimmedState *state = NULL;
1636         gboolean dimmed = FALSE;
1637         
1638         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1639         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1640         rule = MODEST_DIMMING_RULE (user_data);
1641         state = modest_window_get_dimming_state (win);
1642
1643         GtkWidget *body;
1644         body = modest_msg_edit_window_get_child_widget (MODEST_MSG_EDIT_WINDOW (win),
1645                                                         MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_BODY);
1646         
1647         dimmed = ((body == NULL)||(!gtk_widget_is_focus (body)));
1648         if (dimmed)
1649                 modest_dimming_rule_set_notification (rule, _("mcen_ib_move_cursor_to_message"));
1650         
1651         return dimmed;
1652 }
1653
1654 gboolean 
1655 modest_ui_dimming_rules_on_editor_paste (ModestWindow *win, gpointer user_data)
1656 {
1657         ModestDimmingRule *rule = NULL;
1658         const DimmedState *state = NULL;
1659         gboolean dimmed = FALSE;
1660         GtkWidget *focused = NULL;
1661         
1662         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1663         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1664         rule = MODEST_DIMMING_RULE (user_data);
1665         state = modest_window_get_dimming_state (win);
1666
1667         focused = gtk_window_get_focus (GTK_WINDOW (win));
1668
1669         dimmed = MODEST_IS_ATTACHMENTS_VIEW (focused);
1670         
1671         if (!dimmed) {
1672                 dimmed = GTK_IS_TOGGLE_BUTTON (focused);
1673         }
1674
1675         if (!dimmed) {
1676                 ModestEmailClipboard *e_clipboard = modest_runtime_get_email_clipboard ();
1677                 const gchar *clipboard_text = modest_msg_edit_window_get_clipboard_text (MODEST_MSG_EDIT_WINDOW (win));
1678
1679                 dimmed = modest_email_clipboard_cleared (e_clipboard) && 
1680                   ((clipboard_text == NULL) || (clipboard_text[0] == '\0'));
1681         }
1682         
1683         return dimmed;
1684 }
1685
1686 gboolean 
1687 modest_ui_dimming_rules_on_editor_remove_attachment (ModestWindow *win, gpointer user_data)
1688 {
1689         ModestDimmingRule *rule = NULL;
1690         const DimmedState *state = NULL;
1691         gboolean dimmed = FALSE;
1692         TnyList *selected_attachments = NULL;
1693         gint n_att_selected = 0;
1694         GtkWidget *attachments_view;
1695         
1696         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1697         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1698         rule = MODEST_DIMMING_RULE (user_data);
1699         state = modest_window_get_dimming_state (win);
1700
1701         attachments_view = modest_msg_edit_window_get_child_widget (
1702                                                                     MODEST_MSG_EDIT_WINDOW (win),
1703                                                                     MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_ATTACHMENTS);
1704         
1705         selected_attachments = modest_attachments_view_get_selection (
1706                                                                       MODEST_ATTACHMENTS_VIEW (attachments_view));
1707         n_att_selected = tny_list_get_length (selected_attachments);
1708         g_object_unref (selected_attachments);
1709         
1710         dimmed = (n_att_selected < 1);  
1711         
1712         return dimmed;
1713 }
1714
1715 gboolean 
1716 modest_ui_dimming_rules_on_send (ModestWindow *win, gpointer user_data)
1717 {
1718         ModestDimmingRule *rule = NULL;
1719         const DimmedState *state = NULL;
1720         gboolean dimmed = FALSE;
1721         GtkWidget *subject_field, *body_field;
1722         GtkTextBuffer *body_buffer;
1723         const gchar *subject = NULL;
1724         
1725         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1726         g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (win), TRUE);
1727         rule = MODEST_DIMMING_RULE (user_data);
1728         state = modest_window_get_dimming_state (win);
1729
1730         body_field = modest_msg_edit_window_get_child_widget (
1731                                                               MODEST_MSG_EDIT_WINDOW (win),
1732                                                               MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_BODY);
1733         subject_field = modest_msg_edit_window_get_child_widget (
1734                                                                  MODEST_MSG_EDIT_WINDOW (win),
1735                                                                  MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_SUBJECT);
1736         body_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (body_field));
1737         subject = gtk_entry_get_text (GTK_ENTRY (subject_field));
1738
1739         dimmed = (subject == NULL || subject[0] == '\0');
1740         if (dimmed)
1741                 modest_dimming_rule_set_notification (rule, _("mcen_ib_subject_not_modified"));
1742                
1743         if (!dimmed) {
1744                 GtkWidget *to_field, *cc_field, *bcc_field;
1745                 GtkTextBuffer * to_buffer, *cc_buffer, *bcc_buffer;
1746                 cc_field = modest_msg_edit_window_get_child_widget (
1747                         MODEST_MSG_EDIT_WINDOW (win),
1748                         MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_CC);
1749                 to_field = modest_msg_edit_window_get_child_widget (
1750                         MODEST_MSG_EDIT_WINDOW (win),
1751                         MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_TO);
1752                 bcc_field = modest_msg_edit_window_get_child_widget (
1753                         MODEST_MSG_EDIT_WINDOW (win),
1754                         MODEST_MSG_EDIT_WINDOW_WIDGET_TYPE_BCC);
1755                 to_buffer = modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (to_field));
1756                 cc_buffer = modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (cc_field));
1757                 bcc_buffer = modest_recpt_editor_get_buffer (MODEST_RECPT_EDITOR (bcc_field));
1758
1759                 dimmed = ((gtk_text_buffer_get_char_count (to_buffer) +
1760                            gtk_text_buffer_get_char_count (cc_buffer) +
1761                            gtk_text_buffer_get_char_count (bcc_buffer)) == 0);
1762                 if (dimmed)
1763                         modest_dimming_rule_set_notification (rule, _("mcen_ib_add_recipients_first"));
1764         }
1765         
1766         return dimmed;
1767 }
1768
1769 gboolean 
1770 modest_ui_dimming_rules_on_view_previous (ModestWindow *win, gpointer user_data)
1771 {
1772         ModestDimmingRule *rule = NULL;
1773         gboolean dimmed = FALSE;
1774         
1775         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1776         rule = MODEST_DIMMING_RULE (user_data);
1777
1778         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1779
1780         /* Check dimmed rule */
1781         dimmed = _transfer_mode_enabled (win);
1782         if (dimmed)
1783                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
1784         
1785         if (!dimmed) {
1786                 dimmed = modest_msg_view_window_first_message_selected (
1787                                 MODEST_MSG_VIEW_WINDOW(win));
1788                 modest_dimming_rule_set_notification (rule, NULL);
1789         }
1790
1791         return dimmed;
1792 }
1793
1794 gboolean 
1795 modest_ui_dimming_rules_on_view_next (ModestWindow *win, gpointer user_data)
1796 {
1797         ModestDimmingRule *rule = NULL;
1798         gboolean dimmed = FALSE;
1799
1800         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW(win), FALSE);
1801         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1802         rule = MODEST_DIMMING_RULE (user_data);
1803
1804         /* Check dimmed rule */ 
1805         dimmed = _transfer_mode_enabled (win);                  
1806         if (dimmed)
1807                 modest_dimming_rule_set_notification (rule, _("mail_ib_notavailable_downloading"));
1808         
1809         if (!dimmed) {
1810                 dimmed = modest_msg_view_window_last_message_selected (MODEST_MSG_VIEW_WINDOW (win));
1811                 modest_dimming_rule_set_notification (rule, NULL);
1812         }               
1813
1814         return dimmed;
1815 }
1816
1817
1818 gboolean 
1819 modest_ui_dimming_rules_on_tools_smtp_servers (ModestWindow *win, gpointer user_data)
1820 {
1821         return !modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), 
1822                                                 TRUE);  
1823 }
1824
1825 gboolean 
1826 modest_ui_dimming_rules_on_cancel_sending (ModestWindow *win, gpointer user_data)
1827 {
1828         ModestDimmingRule *rule = NULL;
1829         TnyFolderType types[1];
1830         const DimmedState *state = NULL;
1831         gboolean dimmed = FALSE;
1832
1833         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
1834         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1835         rule = MODEST_DIMMING_RULE (user_data);
1836         state = modest_window_get_dimming_state (win);
1837
1838         types[0] = TNY_FOLDER_TYPE_OUTBOX; 
1839
1840         /* Check dimmed rules */        
1841         dimmed = !_selected_folder_is_any_of_type (win, types, 1);
1842         if (dimmed) 
1843                 modest_dimming_rule_set_notification (rule, "");        
1844         if (!dimmed) {
1845                 dimmed = !state->sent_in_progress;
1846                 if (dimmed)
1847                         modest_dimming_rule_set_notification (rule, "");
1848         }
1849
1850         return dimmed;
1851 }
1852
1853 gboolean 
1854 modest_ui_dimming_rules_on_cancel_sending_all (ModestWindow *win, gpointer user_data)
1855 {
1856         /* We dim if no msg send is in progress (and then cancelling send all has no
1857          * effect */
1858         return !_msgs_send_in_progress ();
1859 }
1860
1861 gboolean 
1862 modest_ui_dimming_rules_on_send_receive (ModestWindow *win, gpointer user_data)
1863 {
1864         ModestDimmingRule *rule = NULL;
1865         gboolean dimmed = FALSE;
1866         ModestAccountMgr *mgr;
1867         const gchar* account_name;
1868
1869         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1870         rule = MODEST_DIMMING_RULE (user_data);
1871         mgr = modest_runtime_get_account_mgr();
1872
1873         /* Check dimmed rule */
1874         account_name = modest_window_get_active_account (win);
1875
1876         if (account_name)
1877                 dimmed = modest_account_mgr_account_is_busy (mgr, account_name);
1878         else
1879                 dimmed = TRUE;
1880
1881         if (dimmed)
1882                 modest_dimming_rule_set_notification (rule, _("mcen_nc_no_email_acnts_defined"));
1883
1884         return dimmed;
1885 }
1886
1887 gboolean 
1888 modest_ui_dimming_rules_on_send_receive_all (ModestWindow *win, gpointer user_data)
1889 {
1890         ModestDimmingRule *rule = NULL;
1891         gboolean dimmed = FALSE;
1892
1893         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1894         rule = MODEST_DIMMING_RULE (user_data);
1895  
1896         /* Check dimmed rule */ 
1897         GSList *account_names = modest_account_mgr_account_names (modest_runtime_get_account_mgr (), TRUE);
1898         if (g_slist_length (account_names) < 1)
1899                 dimmed = TRUE;
1900         if (dimmed)
1901                 modest_dimming_rule_set_notification (rule, _("mcen_nc_no_email_acnts_defined"));
1902
1903         modest_account_mgr_free_account_names (account_names);
1904
1905         if (!dimmed) {
1906                 dimmed = _send_receive_in_progress (win);
1907         }
1908
1909         return dimmed;
1910 }
1911
1912 #ifdef MODEST_TOOLKIT_HILDON2
1913 gboolean
1914 modest_ui_dimming_rules_on_add_to_contacts (ModestWindow *win, gpointer user_data)
1915 {
1916         TnyMsg *msg;
1917         GSList *recipients, *node;
1918         gboolean has_recipients_to_add;
1919
1920         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1921         g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (win), FALSE);
1922
1923         msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
1924
1925         /* Message is loaded asynchronously, so this could happen */
1926         if (!msg) {
1927 #ifdef MODEST_TOOLKIT_HILDON2
1928                 TnyHeader *header;
1929
1930                 header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (win));
1931                 if (!header)
1932                         return TRUE;
1933
1934                 recipients = modest_tny_msg_header_get_all_recipients_list (header);
1935                 g_object_unref (header);
1936 #else
1937                 return TRUE;
1938 #endif
1939         } else {
1940                 recipients = modest_tny_msg_get_all_recipients_list (msg);
1941                 g_object_unref (msg);
1942         }
1943
1944         has_recipients_to_add = FALSE;
1945         for (node = recipients; node != NULL; node = g_slist_next (node)) {
1946                 if (!modest_address_book_has_address ((const gchar *) node->data)) {
1947                         has_recipients_to_add = TRUE;
1948                         break;
1949                 }
1950         }
1951
1952         g_slist_foreach (recipients, (GFunc) g_free, NULL);
1953         g_slist_free (recipients);
1954   
1955         return !has_recipients_to_add;
1956 }
1957 #else
1958 gboolean
1959 modest_ui_dimming_rules_on_add_to_contacts (ModestWindow *win, gpointer user_data)
1960 {
1961         ModestDimmingRule *rule = NULL;
1962         gboolean dimmed = FALSE;
1963         GtkWidget *focused = NULL;
1964
1965         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
1966         rule = MODEST_DIMMING_RULE (user_data);
1967         focused = gtk_window_get_focus (GTK_WINDOW (win));
1968
1969         dimmed = !focused;
1970
1971         if (!dimmed) {
1972                 gchar *selection = NULL;
1973                 if (GTK_IS_TEXT_VIEW (focused)) {
1974                         GtkTextIter start, end;
1975                         GtkTextBuffer *buffer = NULL;
1976                         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused));
1977                         if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end)) {
1978                                 selection = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1979                         }
1980                 } else if (GTK_IS_LABEL (focused)) {
1981                         selection = modest_text_utils_label_get_selection (GTK_LABEL (focused));
1982                 } else {
1983                         gboolean do_check = TRUE;
1984                         GtkClipboard *clipboard;
1985                         if (GTK_IS_HTML (focused)) {
1986                                 const gchar *sel;
1987                                 int len = -1;
1988                                 sel = gtk_html_get_selection_html (GTK_HTML (focused), &len);
1989                                 do_check = !((sel == NULL) || (sel[0] == '\0'));
1990                         } else if (MODEST_IS_ATTACHMENTS_VIEW (focused)) {
1991                                 do_check = FALSE;
1992                         }
1993                         if (do_check) {
1994                                 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
1995                                 selection = gtk_clipboard_wait_for_text (clipboard);
1996                         } else {
1997                                 selection = NULL;
1998                         }
1999                 }
2000                 dimmed = !((selection != NULL) && (modest_text_utils_validate_recipient (selection, NULL)));
2001         }
2002
2003         return dimmed;
2004 }
2005 #endif
2006
2007 /* *********************** static utility functions ******************** */
2008
2009
2010 static gboolean
2011 _selected_folder_not_writeable (ModestMainWindow *win,
2012                                 gboolean for_paste)
2013 {
2014         GtkWidget *folder_view = NULL;
2015         TnyFolderStore *parent_folder = NULL;
2016         ModestEmailClipboard *clipboard = NULL;
2017         ModestTnyFolderRules rules;
2018         gboolean is_local_acc = FALSE;
2019         gboolean xfer_folders = FALSE;
2020         gboolean result = FALSE;
2021
2022         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2023
2024         /* Get folder view */
2025         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2026                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2027         /* If no folder view, always dimmed */
2028         if (!folder_view)
2029                 return TRUE;
2030         
2031         /* Get selected folder as parent of new folder to create */
2032         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2033         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
2034                 /* If it's the local account and its transfering folders, then do not dim */            
2035                 if (TNY_IS_ACCOUNT (parent_folder)) {
2036                         is_local_acc = modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (parent_folder));
2037                         if (for_paste) {
2038                                 clipboard = modest_runtime_get_email_clipboard ();
2039                                 xfer_folders = modest_email_clipboard_folder_copied (clipboard);
2040                         }
2041                 }
2042
2043                 if (for_paste) 
2044                         result = !(is_local_acc && xfer_folders); 
2045                 else
2046                         result = !is_local_acc;
2047                 goto frees;             
2048         }
2049         
2050         /* Check dimmed rule */ 
2051         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
2052         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE;
2053
2054         /* free */
2055  frees:
2056         if (parent_folder != NULL)
2057                 g_object_unref (parent_folder);
2058
2059         return result;
2060 }
2061
2062 static gboolean
2063 _selected_folder_not_deletable (ModestMainWindow *win)
2064 {
2065         GtkWidget *folder_view = NULL;
2066         TnyFolderStore *parent_folder = NULL;
2067         ModestTnyFolderRules rules;
2068         gboolean result = FALSE;
2069
2070         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2071
2072         /* Get folder view */
2073         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2074                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2075         /* If no folder view, always dimmed */
2076         if (!folder_view)
2077                 return TRUE;
2078         
2079         /* Get selected folder as parent of new folder to create */
2080         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2081         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
2082                 result = TRUE;
2083                 goto frees;             
2084         }
2085         
2086         /* Check dimmed rule */ 
2087         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
2088         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE;
2089
2090         /* free */
2091  frees:
2092         if (parent_folder != NULL)
2093                 g_object_unref (parent_folder);
2094
2095         return result;
2096 }
2097
2098 static gboolean
2099 _selected_folder_not_moveable (ModestMainWindow *win)
2100 {
2101         GtkWidget *folder_view = NULL;
2102         TnyFolderStore *parent_folder = NULL;
2103         ModestTnyFolderRules rules;
2104         gboolean result = FALSE;
2105
2106         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2107
2108         /* Get folder view */
2109         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2110                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2111         /* If no folder view, always dimmed */
2112         if (!folder_view)
2113                 return TRUE;
2114         
2115         /* Get selected folder as parent of new folder to create */
2116         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2117         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
2118                 result = TRUE;
2119                 goto frees;             
2120         }
2121         
2122         /* Check dimmed rule */ 
2123         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
2124         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE;
2125
2126         /* free */
2127  frees:
2128         if (parent_folder != NULL)
2129                 g_object_unref (parent_folder);
2130
2131         return result;
2132 }
2133
2134 static gboolean
2135 _selected_folder_not_renameable (ModestMainWindow *win)
2136 {
2137         GtkWidget *folder_view = NULL;
2138         TnyFolderStore *parent_folder = NULL;
2139         ModestTnyFolderRules rules;
2140         gboolean result = FALSE;
2141
2142         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2143
2144         /* Get folder view */
2145         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2146                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2147         /* If no folder view, always dimmed */
2148         if (!folder_view)
2149                 return TRUE;
2150         
2151         /* Get selected folder as parent of new folder to create */
2152         parent_folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2153         if (!(parent_folder && TNY_IS_FOLDER(parent_folder))) {
2154                 result = TRUE;
2155                 goto frees;             
2156         }
2157         
2158         /* Check dimmed rule */ 
2159         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent_folder));
2160         result = rules & MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE;
2161
2162         /* free */
2163  frees:
2164         if (parent_folder != NULL)
2165                 g_object_unref (parent_folder);
2166
2167         return result;
2168 }
2169
2170 static gboolean
2171 _selected_folder_is_root_or_inbox (ModestMainWindow *win)
2172 {
2173         TnyFolderType types[2];
2174         gboolean result = FALSE;
2175
2176         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2177
2178         types[0] = TNY_FOLDER_TYPE_ROOT; 
2179         types[1] = TNY_FOLDER_TYPE_INBOX; 
2180
2181         /* Check folder type */
2182         result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 2);
2183
2184         /* Check pop and MMC accounts */
2185         if (!result) {
2186                 result = _selected_folder_is_root (win);
2187         }
2188                 
2189         return result;
2190 }
2191
2192
2193 static gboolean
2194 _selected_folder_is_root (ModestMainWindow *win)
2195 {
2196         TnyFolderType types[1];
2197         gboolean result = FALSE;
2198
2199         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2200
2201         /* All accounts are root items: */
2202         GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2203                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2204         if (folder_view) {                                      
2205                 gboolean is_account = FALSE;
2206                 TnyFolderStore *folder_store = 
2207                         modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2208                 if (folder_store) {
2209                         is_account = TNY_IS_ACCOUNT (folder_store);
2210
2211                         g_object_unref (folder_store);
2212                         folder_store = NULL;
2213                 }
2214                 
2215                 if (is_account)
2216                         return TRUE;
2217         }
2218                 
2219         /* Try something more precise: */
2220         types[0] = TNY_FOLDER_TYPE_ROOT; 
2221
2222         /* Check folder type */
2223         result = _selected_folder_is_any_of_type (MODEST_WINDOW(win), types, 1);
2224                 
2225         return result;
2226 }
2227
2228 static gboolean
2229 _header_view_is_all_selected (ModestMainWindow *win)
2230 {
2231         const DimmedState *state = NULL;
2232
2233         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
2234
2235         state = modest_window_get_dimming_state (MODEST_WINDOW(win));
2236
2237         return state->all_selected;
2238 }
2239
2240 static gboolean
2241 _selected_folder_is_empty (ModestMainWindow *win)
2242 {
2243         GtkWidget *folder_view = NULL, *header_view = NULL;
2244         TnyFolderStore *folder = NULL;
2245         gboolean result = FALSE;
2246
2247         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2248
2249         /* Get folder view */
2250         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2251                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2252
2253         header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2254                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
2255
2256         /* If no folder view, always dimmed */
2257         if (!folder_view || !header_view)
2258                 return TRUE;
2259         
2260         /* Get selected folder as parent of new folder to create */
2261         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2262         if (!(folder && TNY_IS_FOLDER(folder))) {
2263                 if (folder)
2264                         g_object_unref (folder);
2265                 return TRUE;
2266         }
2267         
2268         /* Check folder type */
2269         if (modest_header_view_is_empty (MODEST_HEADER_VIEW (header_view)) ||
2270             tny_folder_get_all_count (TNY_FOLDER (folder)) == 0)
2271                 result = TRUE;
2272
2273         /* free */
2274         g_object_unref (folder);
2275
2276         return result;
2277 }
2278
2279 static gboolean
2280 _folder_view_has_focus (ModestWindow *win)
2281 {
2282         GtkWidget *folder_view = NULL;
2283
2284         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2285
2286         /* Get folder view */
2287         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2288                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2289         if (!folder_view)
2290                 return FALSE;
2291         
2292         if (gtk_widget_is_focus(folder_view))
2293                 return TRUE;
2294
2295         return FALSE;
2296 }
2297
2298 static gboolean
2299 _selected_folder_is_same_as_source (ModestWindow *win)
2300 {
2301         ModestEmailClipboard *clipboard = NULL;
2302         GtkWidget *folder_view = NULL;
2303         TnyFolderStore *folder = NULL;
2304         gboolean result = FALSE;
2305
2306         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2307
2308         /* Get folder view */
2309         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2310                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2311         if (!folder_view)
2312                 return FALSE;
2313         
2314         /* Get selected folder as destination folder */
2315         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2316         if (!(folder && TNY_IS_FOLDER(folder))) {
2317                 result = FALSE;
2318                 goto frees;
2319         }
2320         
2321         /* Check clipboard is cleared */
2322         clipboard = modest_runtime_get_email_clipboard ();
2323         if (modest_email_clipboard_cleared (clipboard)) {
2324                 result = FALSE;
2325                 goto frees;
2326         }
2327                 
2328         /* Check source folder */
2329         result = modest_email_clipboard_check_source_folder (clipboard, TNY_FOLDER (folder));
2330         
2331         /* Free */
2332  frees:
2333         g_object_unref (folder);
2334         
2335         return result;
2336 }
2337
2338 static gboolean
2339 _selected_folder_is_any_of_type (ModestWindow *win,
2340                                  TnyFolderType types[], 
2341                                  guint ntypes)
2342 {
2343         GtkWidget *folder_view = NULL;
2344         TnyFolderStore *folder = NULL;
2345         TnyFolderType folder_type;
2346         guint i=0;
2347         gboolean result = FALSE;
2348
2349         /*Get current folder */
2350         if (MODEST_IS_MAIN_WINDOW(win)) {
2351
2352                 /* Get folder view */
2353                 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2354                                                                    MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2355                 /* If no folder view, always dimmed */
2356                 if (!folder_view)
2357                         return FALSE;
2358         
2359                 /* Get selected folder as parent of new folder to create */
2360                 folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
2361
2362                 if (!(folder && TNY_IS_FOLDER(folder))) {
2363                         if (folder)
2364                                 g_object_unref (folder);
2365                         return FALSE;
2366                 }
2367                 
2368                 /* Check folder type */
2369                 result = _folder_is_any_of_type (TNY_FOLDER(folder), types, ntypes);
2370         }
2371         else if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
2372                 folder_type = modest_msg_view_window_get_folder_type (MODEST_MSG_VIEW_WINDOW (win));
2373                 for (i=0; i < ntypes; i++) {
2374                         result = result || folder_type == types[i];
2375                 }
2376         }
2377
2378
2379         /* free */
2380         if (folder)
2381                 g_object_unref (folder);
2382
2383         return result;  
2384 }
2385
2386 static gboolean
2387 _folder_is_any_of_type (TnyFolder *folder,
2388                         TnyFolderType types[], 
2389                         guint ntypes)
2390 {
2391         TnyFolderType folder_type;
2392         gboolean result = FALSE;
2393         guint i;
2394
2395         g_return_val_if_fail (TNY_IS_FOLDER(folder), FALSE);
2396
2397         /* Get folder type */
2398         folder_type = modest_tny_folder_guess_folder_type (folder);
2399         if (folder_type == TNY_FOLDER_TYPE_INVALID)
2400                 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
2401         
2402         /* Check foler type */
2403         for (i=0; i < ntypes; i++) {
2404                 result = result || folder_type == types[i];
2405         }
2406
2407         return result;
2408 }
2409
2410 static gboolean
2411 _clipboard_is_empty (ModestWindow *win)
2412 {
2413         gboolean result = FALSE;
2414         
2415         if (MODEST_IS_MAIN_WINDOW (win)) {
2416                 ModestEmailClipboard *clipboard = NULL;
2417                 clipboard = modest_runtime_get_email_clipboard ();
2418                 if (modest_email_clipboard_cleared (clipboard)) 
2419                  result = TRUE;  
2420         }
2421
2422         return result;
2423 }
2424
2425 static gboolean
2426 _invalid_clipboard_selected (ModestWindow *win,
2427                              ModestDimmingRule *rule) 
2428 {
2429         const DimmedState *state = NULL;
2430         gboolean result = FALSE;
2431         GtkWidget *focused = NULL;
2432
2433         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
2434         state = modest_window_get_dimming_state (win);
2435         /* Get focuesed widget */
2436         focused = gtk_window_get_focus (GTK_WINDOW (win));
2437
2438         if (MODEST_IS_MSG_EDIT_WINDOW (win)) {
2439                 gboolean has_selection = FALSE;
2440                 if (GTK_IS_TEXT_VIEW (focused)) {
2441                         GtkTextBuffer *buffer = NULL;
2442                         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused));
2443                         has_selection = modest_text_utils_buffer_selection_is_valid (buffer);
2444                 } else if (GTK_IS_EDITABLE (focused)) {
2445                         has_selection = gtk_editable_get_selection_bounds (GTK_EDITABLE (focused), NULL, NULL);
2446                 }
2447                 result = !has_selection;
2448         } else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2449                 if (focused) {
2450                         MODEST_DEBUG_BLOCK (g_message ("FOCUSED %s", g_type_name (G_TYPE_FROM_INSTANCE (focused))););
2451                         if (GTK_IS_LABEL (focused) && 
2452                             !gtk_label_get_selection_bounds (GTK_LABEL (focused), NULL, NULL)) {
2453                                 result = TRUE;
2454                         } else if (GTK_IS_TEXT_VIEW (focused)) {
2455                                 GtkTextBuffer *buffer;
2456                                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (focused));
2457                                 result = !gtk_text_buffer_get_has_selection (buffer);
2458                         } else if (GTK_IS_HTML (focused)) {
2459                                 const gchar *sel;
2460                                 int len = -1;
2461                                 sel = gtk_html_get_selection_html (GTK_HTML (focused), &len);
2462                                 result = ((sel == NULL) || (sel[0] == '\0'));
2463                         } else if (MODEST_IS_ATTACHMENTS_VIEW (focused)) {
2464                                 result = TRUE;
2465                         } else {
2466                                 GtkClipboard *clipboard;
2467                                 gchar *selection;
2468
2469                                 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
2470                                 /* Get clipboard selection*/
2471                                 selection = gtk_clipboard_wait_for_text (clipboard);
2472                                 /* Check dimming */
2473                                 result = (selection == NULL);
2474                                 g_free (selection);
2475                         } 
2476                 } else {
2477                         result = TRUE;
2478                 }
2479                 
2480                 if (result)
2481                         modest_dimming_rule_set_notification (rule, "");
2482                 
2483         }               
2484         else if (MODEST_IS_MAIN_WINDOW (win)) {
2485                 /* Check dimming */
2486                 result = state->n_selected == 0;
2487                 if (result)
2488                         modest_dimming_rule_set_notification (rule, _("mcen_ib_no_message_selected"));                  
2489         }
2490         
2491         return result;
2492 }
2493
2494
2495 static gboolean
2496 _invalid_attach_selected (ModestWindow *win,
2497                           gboolean unique,
2498                           gboolean for_view,
2499                           gboolean for_remove,
2500                           ModestDimmingRule *rule) 
2501 {
2502         TnyList *attachments;
2503         gint n_selected;
2504         TnyHeaderFlags flags;
2505         gboolean nested_attachments = FALSE;
2506         gboolean selected_messages = FALSE;
2507         const DimmedState *state = NULL;
2508         gboolean result = FALSE;
2509
2510         g_return_val_if_fail (MODEST_IS_WINDOW(win), FALSE);
2511         state = modest_window_get_dimming_state (win);
2512
2513         if (MODEST_IS_MAIN_WINDOW (win)) {
2514                 flags = TNY_HEADER_FLAG_ATTACHMENTS;
2515                 if (!result)
2516                         result = !state->any_has_attachments;
2517         }
2518         else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2519                 
2520                 /* Get selected atachments */
2521                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
2522                 n_selected = tny_list_get_length (attachments);
2523
2524                 /* Check unique */              
2525                 if (!result) {
2526                         if (unique) 
2527                                 result = n_selected != 1;
2528                         else
2529                                 
2530                                 result = n_selected < 1;
2531                 }
2532                 
2533                 /* Check attached type (view operation not required) */
2534                 if (!result && !for_view)  {
2535                         TnyIterator *iter;
2536                         iter = tny_list_create_iterator (attachments);
2537                         while (!tny_iterator_is_done (iter) && !result) {
2538 #ifdef MODEST_TOOLKIT_HILDON2
2539                                 gboolean not_selectable = FALSE;
2540 #endif
2541                                 TnyMimePart *mime_part = TNY_MIME_PART (tny_iterator_get_current (iter));
2542                                 TnyList *nested_list = tny_simple_list_new ();
2543                                 tny_mime_part_get_parts (mime_part, nested_list);
2544
2545                                 if (!for_remove && modest_tny_mime_part_is_msg (mime_part)) {
2546                                         TnyMsg *window_msg;
2547                                         window_msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
2548                                         if ((TnyMimePart *) window_msg != mime_part) {
2549                                                 selected_messages = TRUE;
2550 #ifdef MODEST_TOOLKIT_HILDON2
2551                                                 not_selectable = TRUE;
2552 #else
2553                                                 result = TRUE;
2554 #endif
2555                                         }
2556                                         g_object_unref (window_msg);
2557                                 }
2558                                 if (!for_remove && tny_list_get_length (nested_list) > 0) {
2559                                         nested_attachments = TRUE;
2560 #ifdef MODEST_TOOLKIT_HILDON2
2561                                         not_selectable = TRUE;
2562 #else
2563                                         result = TRUE;
2564 #endif
2565                                 }
2566 #ifdef MODEST_TOOLKIT_HILDON2
2567                                 if (not_selectable)
2568                                         n_selected --;
2569 #endif
2570                                 g_object_unref (nested_list);
2571                                 g_object_unref (mime_part);
2572                                 tny_iterator_next (iter);
2573                         }
2574                         g_object_unref (iter);
2575                 }
2576
2577                 /* No valid attachment available */
2578                 if (n_selected == 0)
2579                         result = TRUE;
2580                 
2581                 /* Set notifications */
2582                 if (result && rule != NULL) {
2583                         if (selected_messages) {
2584                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_save_attach_mail"));
2585                         } else if (nested_attachments) {
2586                                 modest_dimming_rule_set_notification (rule, _("FIXME:unable to save attachments with nested elements"));
2587                         } else if (n_selected == 0) {
2588                                 modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
2589                         } else if (unique) {
2590                                 modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_display_more"));
2591                         }
2592                 }
2593                 
2594                 /* Free */
2595                 g_object_unref (attachments);
2596         }
2597
2598         return result;
2599 }
2600
2601 static gboolean
2602 _purged_attach_selected (ModestWindow *win, gboolean all, ModestDimmingRule *rule) 
2603 {
2604         TnyList *attachments = NULL;
2605         TnyIterator *iter;
2606         gint purged = 0;
2607         gint n_attachments = 0;
2608         gboolean result = FALSE;
2609
2610         /* This should check if _all_ the attachments are already purged. If only some
2611          * of them are purged, then it does not cause dim as there's a confirmation dialog
2612          * for removing only local attachments */
2613
2614         /* Get selected atachments */
2615         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2616                 attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
2617         } else if (MODEST_IS_MAIN_WINDOW (win)) {
2618                 /* If we're in main window, we won't know if there are already purged attachments */
2619                 return FALSE;
2620         }
2621
2622         if (attachments == NULL)
2623                 return FALSE;
2624
2625         if (tny_list_get_length (attachments) == 0) {
2626                 g_object_unref (attachments);
2627                 return FALSE;
2628         }
2629
2630         iter = tny_list_create_iterator (attachments);
2631         while (!tny_iterator_is_done (iter)) {
2632                 TnyMimePart *mime_part = TNY_MIME_PART (tny_iterator_get_current (iter));
2633                 if (tny_mime_part_is_purged (mime_part)) {
2634                         purged++;
2635                 }
2636                 n_attachments++;
2637                 g_object_unref (mime_part);
2638                 tny_iterator_next (iter);
2639         }
2640         g_object_unref (iter);
2641                 
2642         /* Free */
2643         g_object_unref (attachments);
2644
2645         if (all)
2646                 result = (purged == n_attachments);
2647         else
2648                 result = (purged > 0);
2649
2650         /* This string no longer exists, refer to NB#75415 for more info
2651         if (result && (rule != NULL))
2652                 modest_dimming_rule_set_notification (rule, _("mail_ib_attachment_already_purged"));
2653         */
2654
2655         return result;
2656 }
2657
2658 static gboolean
2659 _invalid_msg_selected (ModestMainWindow *win,
2660                        gboolean unique,
2661                        ModestDimmingRule *rule) 
2662 {
2663         GtkWidget *folder_view = NULL;
2664         const DimmedState *state = NULL;
2665         gboolean result = FALSE;
2666
2667         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), FALSE);
2668         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (rule), FALSE);
2669         state = modest_window_get_dimming_state (MODEST_WINDOW(win));
2670                 
2671         /* Get folder view to check focus */
2672         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
2673                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2674
2675         /* Check dimmed rule (TODO: check focus on widgets */   
2676         if (!result) {
2677                 result = ((state->n_selected == 0 ) ||
2678                           (gtk_widget_is_focus (folder_view)));
2679                 if (result)
2680                         modest_dimming_rule_set_notification (rule, _("mcen_ib_no_message_selected"));
2681         }
2682         if (!result && unique) {
2683                 result = state->n_selected > 1;
2684                 if (result)
2685                         modest_dimming_rule_set_notification (rule, _("mcen_ib_select_one_message"));
2686         }
2687
2688         return result;
2689 }
2690
2691
2692 static gboolean
2693 _msg_download_in_progress (ModestWindow *win)
2694 {
2695         gboolean result = FALSE;
2696
2697         g_return_val_if_fail (MODEST_IS_WINDOW (win), FALSE);
2698
2699         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2700                 result = modest_msg_view_window_toolbar_on_transfer_mode (MODEST_MSG_VIEW_WINDOW(win));
2701         }
2702         else if (MODEST_IS_MAIN_WINDOW (win)) {
2703                 result = modest_main_window_transfer_mode_enabled (MODEST_MAIN_WINDOW(win));
2704         }
2705
2706         return result;
2707 }
2708
2709 static gboolean
2710 _msg_download_completed (ModestMainWindow *win)
2711 {
2712         const DimmedState *state = modest_window_get_dimming_state (MODEST_WINDOW(win));
2713         return state->any_marked_as_cached;
2714 }
2715
2716 static void 
2717 fill_list_of_caches (gpointer key, gpointer value, gpointer userdata)
2718 {
2719         GSList **send_queues = (GSList **) userdata;
2720         *send_queues = g_slist_prepend (*send_queues, value);
2721 }
2722
2723 static gboolean
2724 _selected_msg_sent_in_progress (ModestWindow *win)
2725 {
2726         const DimmedState *state = modest_window_get_dimming_state (win);
2727         return state->sent_in_progress;
2728 }
2729
2730
2731 static gboolean
2732 _invalid_folder_for_purge (ModestWindow *win, 
2733                            ModestDimmingRule *rule)
2734 {
2735         TnyFolder *folder = NULL;
2736         TnyAccount *account = NULL;
2737         gboolean result = FALSE;
2738
2739         if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
2740
2741                 /* Get folder and account of message */
2742                 TnyMsg *msg = modest_msg_view_window_get_message (MODEST_MSG_VIEW_WINDOW (win));
2743                 g_return_val_if_fail(msg != NULL, TRUE);                        
2744                 folder = tny_msg_get_folder (msg);      
2745                 g_object_unref (msg);
2746                 if (folder == NULL) {
2747                         result = TRUE;
2748                         goto frees;
2749                 }
2750         } else if (MODEST_IS_MAIN_WINDOW (win)) {
2751                 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
2752                                                                               MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2753                 if (!folder_view)
2754                         return FALSE;
2755                 folder = (TnyFolder *) modest_folder_view_get_selected (MODEST_FOLDER_VIEW (folder_view));
2756                 /* Could be a folder store */
2757                 if (folder == NULL || ! TNY_IS_FOLDER (folder))
2758                         goto frees;             
2759         } else {
2760                 g_return_val_if_reached (FALSE);
2761         }
2762         account = modest_tny_folder_get_account (folder);
2763         if (account == NULL) goto frees;                        
2764                 
2765         /* Check account */
2766         if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (account))) {
2767                 TnyFolderType types[2];
2768                 types[0] = TNY_FOLDER_TYPE_DRAFTS;
2769                 types[1] = TNY_FOLDER_TYPE_OUTBOX;
2770                 
2771                 if (_selected_folder_is_any_of_type (win, types, 2)) {
2772                         result = TRUE;
2773                 }
2774         } else {
2775                 ModestProtocolType protocol_type = modest_tny_account_get_protocol_type (TNY_ACCOUNT (account));
2776                 /* If it's a remote folder then dim */
2777                 if (modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
2778                                                                     protocol_type,
2779                                                                     MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS)) {
2780                         result = TRUE;
2781                 }
2782         }
2783         
2784 frees:
2785         if (folder != NULL)
2786                 g_object_unref (folder);
2787         if (account != NULL)
2788                 g_object_unref (account);
2789         
2790         return result;
2791 }
2792
2793 static gboolean
2794 _transfer_mode_enabled (ModestWindow *win)
2795 {
2796         gboolean result = FALSE;
2797
2798         /* Check dimming */
2799         if (MODEST_IS_MSG_VIEW_WINDOW(win)) {
2800                 result = modest_msg_view_window_transfer_mode_enabled (MODEST_MSG_VIEW_WINDOW (win));
2801         } else if (MODEST_IS_MAIN_WINDOW(win)) {
2802                 result = modest_main_window_transfer_mode_enabled (MODEST_MAIN_WINDOW (win));
2803 #ifdef MODEST_TOOLKIT_HILDON2
2804         } else if (MODEST_IS_HEADER_WINDOW (win)) {
2805                 result = modest_header_window_transfer_mode_enabled (MODEST_HEADER_WINDOW (win));
2806 #endif
2807         } else {
2808                 g_warning("_transfer_mode_enabled called with wrong window type");
2809         }
2810
2811         return result;
2812 }
2813
2814 static gboolean
2815 _selected_folder_has_subfolder_with_same_name (ModestWindow *win)
2816 {
2817         GtkWidget *folder_view = NULL;
2818         TnyFolderStore *folder = NULL;
2819         ModestEmailClipboard *clipboard = NULL;
2820         const gchar *folder_name = NULL;
2821         gboolean result = FALSE;
2822
2823         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), TRUE);
2824
2825         /*Get current parent folder */
2826         folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
2827                                                            MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
2828         /* If no folder view, always dimmed */
2829         if (!folder_view) return FALSE;
2830         
2831         /* Get selected folder as parent of new folder to create */
2832         folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));     
2833         if (!(folder && TNY_IS_FOLDER(folder))) goto frees;
2834         
2835         /* get modest clipboard and source folder */
2836         clipboard = modest_runtime_get_email_clipboard ();
2837         folder_name = modest_email_clipboard_get_folder_name (clipboard);
2838         if (folder_name == NULL) goto frees;
2839
2840         /* Check source subfolders names */
2841         result = modest_tny_folder_has_subfolder_with_name (folder, folder_name,
2842                                                             TRUE);
2843                 
2844         /* Free */
2845  frees:
2846         if (folder != NULL) 
2847                 g_object_unref (folder);
2848
2849
2850         return result;
2851 }       
2852
2853 gboolean 
2854 modest_ui_dimming_rules_on_save_to_drafts (ModestWindow *win, 
2855                                            gpointer user_data)
2856 {
2857         ModestDimmingRule *rule = NULL;
2858
2859         g_return_val_if_fail (MODEST_MSG_EDIT_WINDOW (win), FALSE);
2860         g_return_val_if_fail (MODEST_IS_DIMMING_RULE (user_data), FALSE);
2861         rule = MODEST_DIMMING_RULE (user_data);
2862
2863         /* Check dimmed rule */ 
2864         return !modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (win));
2865 }
2866
2867 gboolean
2868 modest_ui_dimming_rules_on_insert_image (ModestWindow *win,
2869                                          gpointer user_data)
2870 {
2871         g_return_val_if_fail (MODEST_MSG_EDIT_WINDOW (win), FALSE);
2872
2873         ModestMsgEditFormat format =
2874           modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW (win));
2875
2876         return (format != MODEST_MSG_EDIT_FORMAT_HTML);
2877 }
2878
2879 static gboolean 
2880 _send_receive_in_progress (ModestWindow *win)
2881 {
2882         ModestMailOperationQueue *queue;
2883         GSList *op_list, *node;
2884         gboolean found_send_receive;
2885
2886         queue = modest_runtime_get_mail_operation_queue ();
2887         op_list = modest_mail_operation_queue_get_by_source (queue, G_OBJECT (win));
2888
2889         found_send_receive = FALSE;
2890         for (node = op_list; node != NULL; node = g_slist_next (node)) {
2891                 ModestMailOperation *op;
2892
2893                 op = (ModestMailOperation *) node->data;
2894                 if (modest_mail_operation_get_type_operation (op) == MODEST_MAIL_OPERATION_TYPE_SEND_AND_RECEIVE) {
2895                         found_send_receive = TRUE;
2896                         break;
2897                 }
2898         }
2899
2900         g_slist_foreach (op_list, (GFunc) g_object_unref, NULL);
2901         g_slist_free (op_list);
2902
2903         return found_send_receive;
2904 }
2905
2906 static gboolean
2907 _msgs_send_in_progress (void)
2908 {
2909         ModestCacheMgr *cache_mgr;
2910         GHashTable *send_queue_cache;
2911         ModestTnySendQueue *send_queue;
2912         GSList *send_queues = NULL, *node = NULL;
2913         gboolean found = FALSE;
2914
2915         cache_mgr = modest_runtime_get_cache_mgr ();
2916         send_queue_cache = modest_cache_mgr_get_cache (cache_mgr,
2917                                                        MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE);
2918
2919         g_hash_table_foreach (send_queue_cache, (GHFunc) fill_list_of_caches, &send_queues);
2920
2921         for (node = send_queues; node != NULL && !found; node = g_slist_next (node)) {
2922                 send_queue = MODEST_TNY_SEND_QUEUE (node->data);
2923
2924                 /* Check if msg uid is being processed inside send queue */
2925                 if (modest_tny_send_queue_sending_in_progress (send_queue)) {
2926                         found = TRUE;
2927                         break;
2928                 }
2929         }
2930
2931         g_slist_free (send_queues);
2932
2933         return found;
2934 }