Attachment remover: notify the user about what has been done when processing
[claws.git] / src / plugins / att_remover / att_remover.c
index c9a79b8bf7488313598f3dda34759c05467516ca..ad4cf8f6b637bc7c338483d9521b6a2459e10e91 100644 (file)
@@ -87,7 +87,7 @@ static MimeInfo *find_first_text_part(MimeInfo *partinfo)
 static gboolean key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
                                AttRemover *attremover)
 {
-       if (event && event->keyval == GDK_Escape)
+       if (event && event->keyval == GDK_KEY_Escape)
                gtk_widget_destroy(attremover->window);
 
        return FALSE;
@@ -116,7 +116,7 @@ static gint save_new_message(MsgInfo *oldmsg, MsgInfo *newmsg, MimeInfo *info,
        
        finalmsg = procmsg_msginfo_new_from_mimeinfo(newmsg, info);
        if (!finalmsg) {
-               procmsg_msginfo_free(newmsg);
+               procmsg_msginfo_free(&newmsg);
                return -1;
        }
 
@@ -129,18 +129,23 @@ static gint save_new_message(MsgInfo *oldmsg, MsgInfo *newmsg, MimeInfo *info,
                flags.tmp_flags &= ~MSG_HAS_ATTACHMENT;
 
        oldmsg->flags.perm_flags &= ~MSG_LOCKED;
+       msgnum = folder_item_add_msg(item, finalmsg->plaintext_file, &flags, TRUE);
+       if (msgnum < 0) {
+               g_warning("could not add message without attachments");
+               procmsg_msginfo_free(&newmsg);
+               procmsg_msginfo_free(&finalmsg);
+               return msgnum;
+       }
        folder_item_remove_msg(item, oldmsg->msgnum);
-       msgnum = folder_item_add_msg(item, finalmsg->plaintext_file, 
-                       &flags, TRUE);
        finalmsg->msgnum = msgnum;
-       procmsg_msginfo_free(newmsg);
-       procmsg_msginfo_free(finalmsg);
+       procmsg_msginfo_free(&newmsg);
+       procmsg_msginfo_free(&finalmsg);
                
        newmsg = folder_item_get_msginfo(item, msgnum);
        if (newmsg && item) {
                procmsg_msginfo_unset_flags(newmsg, ~0, ~0);
                procmsg_msginfo_set_flags(newmsg, flags.perm_flags, flags.tmp_flags);
-               procmsg_msginfo_free(newmsg);
+               procmsg_msginfo_free(&newmsg);
        }
        
        return msgnum;
@@ -168,12 +173,12 @@ static void remove_attachments_cb(GtkWidget *widget, AttRemover *attremover)
        partinfo = procmime_mimeinfo_next(partinfo);
        if (!partinfo || !gtk_tree_model_get_iter_first(model, &iter)) {
                gtk_widget_destroy(attremover->window);
-               procmsg_msginfo_free(newmsg);
+               procmsg_msginfo_free(&newmsg);
                return;
        }
 
        main_window_cursor_wait(mainwin);
-       gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
+       summary_freeze(summaryview);
        folder_item_update_freeze();
        inc_lock();
        
@@ -242,11 +247,11 @@ static void remove_attachments_cb(GtkWidget *widget, AttRemover *attremover)
                         
        inc_unlock();
        folder_item_update_thaw();
-       gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
+       summary_thaw(summaryview);
        main_window_cursor_normal(mainwin);
 
        if (msgnum > 0)
-               summary_select_by_msgnum(summaryview, msgnum);
+               summary_select_by_msgnum(summaryview, msgnum, TRUE);
 
        gtk_widget_destroy(attremover->window);
 }
@@ -296,9 +301,9 @@ static void fill_attachment_store(GtkTreeView *list_view, MimeInfo *partinfo)
                if (!name)
                        name = _("unknown");
                
-               label = g_strconcat(_("<b>Type: </b>"), content_type, "   ",
-                       _("<b>Size: </b>"), to_human_readable((goffset)partinfo->length),
-                       "\n", _("<b>Filename: </b>"), name, NULL);
+               label = g_strconcat("<b>",_("Type:"), "</b> ", content_type, "   <b>",
+                        _("Size:"), "</b> ", to_human_readable((goffset)partinfo->length),
+                       "\n", "<b>", _("Filename:"), "</b> ", name, NULL);
 
                gtk_list_store_append(list_store, &iter);
                gtk_list_store_set(list_store, &iter,
@@ -413,17 +418,19 @@ static void remove_attachments(GSList *msglist)
        SummaryView *summaryview = mainwin->summaryview;
        GSList *cur;
        gint msgnum = -1;
+       gint stripped_msgs = 0;
+       gint total_msgs = 0;
        
        if (alertpanel_full(_("Destroy attachments"),
                   _("Do you really want to remove all attachments from "
                   "the selected messages?\n\n"
                  "The deleted data will be unrecoverable."), 
-                 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL,
-                  FALSE, NULL, ALERT_QUESTION, G_ALERTALTERNATE) != G_ALERTALTERNATE)
+                 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_SECOND,
+                  FALSE, NULL, ALERT_QUESTION) != G_ALERTALTERNATE)
                return;
 
        main_window_cursor_wait(summaryview->mainwin);
-       gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
+       summary_freeze(summaryview);
        folder_item_update_freeze();
        inc_lock();
 
@@ -432,31 +439,48 @@ static void remove_attachments(GSList *msglist)
                MsgInfo *newmsg = NULL;
                MimeInfo *info = NULL;
                MimeInfo *partinfo = NULL;
+               MimeInfo *nextpartinfo = NULL;
 
                if (!msginfo)
                        continue;
-               
+               total_msgs++;                   /* count all processed messages */
+
                newmsg = procmsg_msginfo_copy(msginfo);
                info = procmime_scan_message(newmsg);
        
                if ( !(partinfo = find_first_text_part(info)) ) {
-                       procmsg_msginfo_free(newmsg);
+                       procmsg_msginfo_free(&newmsg);
                        continue;
                }
-               partinfo->node->next = NULL;
-               partinfo->node->children = NULL;
-               info->node->children->data = partinfo;
+               /* only strip attachments where there is at least one */
+               nextpartinfo = procmime_mimeinfo_next(partinfo);
+               if (nextpartinfo) {
+                       partinfo->node->next = NULL;
+                       partinfo->node->children = NULL;
+                       info->node->children->data = partinfo;
 
-               msgnum = save_new_message(msginfo, newmsg, info, FALSE);                
+                       msgnum = save_new_message(msginfo, newmsg, info, FALSE);
+
+                       stripped_msgs++;        /* count messages with removed attachment(s) */
+               }
        }
+       if (stripped_msgs == 0) {
+               alertpanel_notice(_("The selected messages don't have any attachments."));
+       } else {
+               if (stripped_msgs != total_msgs)
+                       alertpanel_notice(_("Attachments removed from %d of the %d selected messages."),
+                                                       stripped_msgs, total_msgs);
+               else
+                       alertpanel_notice(_("Attachments removed from all %d selected messages."), total_msgs);
+       }       
 
        inc_unlock();
        folder_item_update_thaw();
-       gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
+       summary_thaw(summaryview);
        main_window_cursor_normal(summaryview->mainwin);
 
        if (msgnum > 0) {
-               summary_select_by_msgnum(summaryview, msgnum);
+               summary_select_by_msgnum(summaryview, msgnum, TRUE);
        }
 }
 
@@ -477,12 +501,10 @@ static void remove_attachments_ui(GtkAction *action, gpointer data)
                
                if (!partinfo) {
                        alertpanel_notice(_("This message doesn't have any attachments."));
-                       g_slist_free(msglist);
-                       return;
+               } else {
+                       AttRemoverData.msginfo = msglist->data;
+                       remove_attachments_dialog(&AttRemoverData);
                }
-               
-               AttRemoverData.msginfo = msglist->data;
-               remove_attachments_dialog(&AttRemoverData);
        } else
                remove_attachments(msglist);