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