+static gboolean
+_selected_folder_is_snd_level (ModestMainWindow *win)
+{
+ GtkWidget *folder_view = NULL;
+ GtkTreeSelection *sel = NULL;
+ GtkTreeModel *model = NULL;
+ GtkTreePath *path = NULL;
+ GtkTreeIter iter;
+ TnyFolderStore *folder = NULL;
+ gboolean result = FALSE;
+
+ g_return_val_if_fail (MODEST_IS_MAIN_WINDOW(win), TRUE);
+
+ /*Get curent folder */
+ folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
+ MODEST_WIDGET_TYPE_FOLDER_VIEW);
+ /* If no folder view, always dimmed */
+ if (!folder_view)
+ goto frees;
+
+ /* Get selected folder as parent of new folder to create */
+ folder = modest_folder_view_get_selected (MODEST_FOLDER_VIEW(folder_view));
+ if (!(folder && TNY_IS_FOLDER(folder))) {
+ goto frees;
+ }
+
+ /* Check folder level */
+ sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(folder_view));
+ if (!gtk_tree_selection_get_selected (sel, &model, &iter))
+ goto frees;
+ path = gtk_tree_model_get_path (model, &iter);
+ result = gtk_tree_path_get_depth (path) > 2;
+
+ frees:
+ if (folder != NULL)
+ g_object_unref (folder);
+ if (path != NULL)
+ gtk_tree_path_free (path);
+
+ return result;
+
+}
+
+static gboolean
+_clipboard_is_empty (ModestWindow *win)
+{
+ gboolean result = FALSE;
+
+ if (MODEST_IS_MAIN_WINDOW (win)) {
+ ModestEmailClipboard *clipboard = NULL;
+ clipboard = modest_runtime_get_email_clipboard ();
+ if (modest_email_clipboard_cleared (clipboard))
+ result = TRUE;
+ }
+
+ return result;
+}
+
+static gboolean
+_invalid_clipboard_selected (ModestWindow *win,
+ ModestDimmingRule *rule)
+{
+ gboolean result = FALSE;
+
+ if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
+ GtkClipboard *clipboard = NULL;
+ gchar *selection = NULL;
+ GtkWidget *focused = NULL;
+
+ /* Get clipboard selection*/
+ clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
+ selection = gtk_clipboard_wait_for_text (clipboard);
+
+ /* Get focuesed widget */
+ focused = gtk_window_get_focus (GTK_WINDOW (win));
+
+ /* Check dimming */
+ result = ((selection == NULL) ||
+ (MODEST_IS_ATTACHMENTS_VIEW (focused)));
+
+ if (result)
+ modest_dimming_rule_set_notification (rule, "");
+ }
+ else if (MODEST_IS_MAIN_WINDOW (win)) {
+ GtkWidget *header_view = NULL;
+
+ /* Get header view to check selected messages */
+ header_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(win),
+ MODEST_WIDGET_TYPE_HEADER_VIEW);
+
+ /* Check dimming */
+ result = !modest_header_view_has_selected_headers (MODEST_HEADER_VIEW(header_view));
+ if (result)
+ modest_dimming_rule_set_notification (rule, _("mcen_ib_no_message_selected"));
+ }
+
+ return result;
+}
+
+
+static gboolean
+_invalid_attach_selected (ModestWindow *win,
+ gboolean unique,
+ gboolean for_view,
+ gboolean for_remove,
+ ModestDimmingRule *rule)
+{
+ GList *attachments, *node;
+ gint n_selected;
+ TnyHeaderFlags flags;
+ gboolean nested_attachments = FALSE;
+ gboolean selected_messages = FALSE;
+ gboolean result = FALSE;
+
+ if (MODEST_IS_MAIN_WINDOW (win)) {
+ flags = TNY_HEADER_FLAG_ATTACHMENTS;
+ if (!result)
+ result = _selected_msg_marked_as (MODEST_MAIN_WINDOW (win), flags, TRUE, FALSE);
+ }
+ else if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
+
+ /* Get selected atachments */
+ attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
+ n_selected = g_list_length (attachments);
+
+ /* Check unique */
+ if (!result) {
+ if (unique)
+ result = n_selected != 1;
+ else
+
+ result = n_selected < 1;
+ }
+
+ /* Check attached type (view operation not required) */
+ if (!result && !for_view) {
+ for (node = attachments; node != NULL && !result; node = g_list_next (node)) {
+ TnyMimePart *mime_part = TNY_MIME_PART (node->data);
+ TnyList *nested_list = tny_simple_list_new ();
+ if (!for_remove && TNY_IS_MSG (mime_part)) {
+ selected_messages = TRUE;
+ result = TRUE;
+ }
+ tny_mime_part_get_parts (mime_part, nested_list);
+ if (!for_remove && tny_list_get_length (nested_list) > 0) {
+ nested_attachments = TRUE;
+ result = TRUE;
+ }
+ g_object_unref (nested_list);
+ }
+ }
+
+ /* Set notifications */
+ if (result && rule != NULL) {
+ if (selected_messages) {
+ modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_save_attach_mail"));
+ } else if (nested_attachments) {
+ modest_dimming_rule_set_notification (rule, _("FIXME:unable to save attachments with nested elements"));
+ } else if (n_selected == 0) {
+ modest_dimming_rule_set_notification (rule, _("FIXME:no attachment selected"));
+ } else if (unique) {
+ modest_dimming_rule_set_notification (rule, _("mcen_ib_unable_to_display_more"));
+ }
+ }
+
+ /* Free */
+ g_list_free (attachments);
+ }
+
+ return result;
+}
+
+static gboolean
+_purged_attach_selected (ModestWindow *win, gboolean all, ModestDimmingRule *rule)
+{
+ GList *attachments = NULL, *node;
+ gint purged = 0;
+ gint n_attachments = 0;
+ gboolean result = FALSE;
+
+ /* This should check if _all_ the attachments are already purged. If only some
+ * of them are purged, then it does not cause dim as there's a confirmation dialog
+ * for removing only local attachments */
+
+ /* Get selected atachments */
+ if (MODEST_IS_MSG_VIEW_WINDOW (win)) {
+ attachments = modest_msg_view_window_get_attachments (MODEST_MSG_VIEW_WINDOW(win));
+ } else if (MODEST_IS_MAIN_WINDOW (win)) {
+ /* If we're in main window, we won't know if there are already purged attachments */
+ return FALSE;
+ }
+
+ if (attachments == NULL)
+ return FALSE;
+
+ for (node = attachments; node != NULL; node = g_list_next (node)) {
+ TnyMimePart *mime_part = TNY_MIME_PART (node->data);
+ if (tny_mime_part_is_purged (mime_part)) {
+ purged++;
+ }
+ n_attachments++;
+ }
+
+ /* Free */
+ g_list_free (attachments);
+
+ if (all)
+ result = (purged == n_attachments);
+ else
+ result = (purged > 0);