Clean att_remover warnings
[claws.git] / src / plugins / att_remover / att_remover.c
1 /*
2  * att_remover -- for Claws Mail
3  *
4  * Copyright (C) 2005 Colin Leroy <colin@colino.net>
5  *
6  * Sylpheed is a GTK+ based, lightweight, and fast e-mail client
7  * Copyright (C) 1999-2005 Hiroyuki Yamamoto and the Claws Mail Team
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #include "claws-features.h"
27 #endif
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31
32 #include <string.h>
33
34 #include <gdk/gdkkeysyms.h>
35 #include <gtk/gtk.h>
36
37 #include "mainwindow.h"
38 #include "summaryview.h"
39 #include "folder.h"
40 #include "version.h"
41 #include "summaryview.h"
42 #include "procmime.h"
43 #include "alertpanel.h"
44 #include "inc.h"
45 #include "menu.h"
46 #include "claws.h"
47 #include "plugin.h"
48 #include "prefs_common.h"
49 #include "defs.h"
50 #include "prefs_gtk.h"
51
52 #define PREFS_BLOCK_NAME "AttRemover"
53
54 static struct _AttRemover {
55         GtkWidget       *window;
56         MsgInfo         *msginfo;
57         GtkTreeModel    *model;
58         gint            win_width;
59         gint            win_height;
60 } AttRemoverData;
61
62 typedef struct _AttRemover AttRemover;
63
64 static PrefParam prefs[] = {
65         {"win_width", "-1", &AttRemoverData.win_width, P_INT, NULL,
66         NULL, NULL},
67         {"win_height", "-1", &AttRemoverData.win_height, P_INT, NULL, 
68          NULL, NULL},
69         {0,0,0,0}
70 };
71
72 enum {
73         ATT_REMOVER_INFO,
74         ATT_REMOVER_TOGGLE,
75         N_ATT_REMOVER_COLUMNS
76 };
77
78 static MimeInfo *find_first_text_part(MimeInfo *partinfo)
79 {
80         while (partinfo && partinfo->type != MIMETYPE_TEXT) {
81                 partinfo = procmime_mimeinfo_next(partinfo);
82         }
83         
84         return partinfo;
85 }
86
87 static gboolean key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
88                                 AttRemover *attremover)
89 {
90         if (event && event->keyval == GDK_Escape)
91                 gtk_widget_destroy(attremover->window);
92
93         return FALSE;
94 }
95
96 static void cancelled_event_cb(GtkWidget *widget, AttRemover *attremover)
97 {
98         gtk_widget_destroy(attremover->window);
99 }
100
101 static void size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation)
102 {
103         cm_return_if_fail(allocation != NULL);
104
105         AttRemoverData.win_width = allocation->width;
106         AttRemoverData.win_height = allocation->height;
107 }
108
109 static gint save_new_message(MsgInfo *oldmsg, MsgInfo *newmsg, MimeInfo *info,
110                                 gboolean has_attachment)
111 {
112         MsgInfo *finalmsg;
113         FolderItem *item = oldmsg->folder;
114         MsgFlags flags = {0, 0};
115         gint msgnum = -1;
116         
117         finalmsg = procmsg_msginfo_new_from_mimeinfo(newmsg, info);
118         if (!finalmsg) {
119                 procmsg_msginfo_free(newmsg);
120                 return -1;
121         }
122
123         debug_print("finalmsg %s\n", finalmsg->plaintext_file);
124                 
125         flags.tmp_flags = oldmsg->flags.tmp_flags;
126         flags.perm_flags = oldmsg->flags.perm_flags;
127         
128         if (!has_attachment)
129                 flags.tmp_flags &= ~MSG_HAS_ATTACHMENT;
130
131         oldmsg->flags.perm_flags &= ~MSG_LOCKED;
132         folder_item_remove_msg(item, oldmsg->msgnum);
133         msgnum = folder_item_add_msg(item, finalmsg->plaintext_file, 
134                         &flags, TRUE);
135         finalmsg->msgnum = msgnum;
136         procmsg_msginfo_free(newmsg);
137         procmsg_msginfo_free(finalmsg);
138                 
139         newmsg = folder_item_get_msginfo(item, msgnum);
140         if (newmsg && item) {
141                 procmsg_msginfo_unset_flags(newmsg, ~0, ~0);
142                 procmsg_msginfo_set_flags(newmsg, flags.perm_flags, flags.tmp_flags);
143                 procmsg_msginfo_free(newmsg);
144         }
145         
146         return msgnum;
147 }
148
149 #define MIMEINFO_NOT_ATTACHMENT(m) \
150         ((m)->type == MIMETYPE_MESSAGE || (m)->type == MIMETYPE_MULTIPART)
151
152 static void remove_attachments_cb(GtkWidget *widget, AttRemover *attremover)
153 {
154         MainWindow *mainwin = mainwindow_get_mainwindow();
155         SummaryView *summaryview = mainwin->summaryview;
156         GtkTreeModel *model = attremover->model;
157         GtkTreeIter iter;
158         MsgInfo *newmsg;
159         MimeInfo *info, *parent, *last, *partinfo;
160         GNode *child;
161         gint att_all = 0, att_removed = 0, msgnum;
162         gboolean to_removal, iter_valid=TRUE;
163         
164         newmsg = procmsg_msginfo_copy(attremover->msginfo);
165         info = procmime_scan_message(newmsg);
166         
167         last = partinfo = find_first_text_part(info);
168         partinfo = procmime_mimeinfo_next(partinfo);
169         if (!partinfo || !gtk_tree_model_get_iter_first(model, &iter)) {
170                 gtk_widget_destroy(attremover->window);
171                 procmsg_msginfo_free(newmsg);
172                 return;
173         }
174
175         main_window_cursor_wait(mainwin);
176         gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
177         folder_item_update_freeze();
178         inc_lock();
179         
180         while (partinfo && iter_valid) {
181                 if (MIMEINFO_NOT_ATTACHMENT(partinfo)) {
182                         last = partinfo;
183                         partinfo = procmime_mimeinfo_next(partinfo);
184                         continue;
185                 }
186                         
187                 att_all++;
188                 gtk_tree_model_get(model, &iter, ATT_REMOVER_TOGGLE,
189                                    &to_removal, -1);
190                 if (!to_removal) {
191                         last = partinfo;
192                         partinfo = procmime_mimeinfo_next(partinfo);
193                         iter_valid = gtk_tree_model_iter_next(model, &iter);
194                         continue;
195                 }
196
197                 parent = partinfo;
198                 partinfo = procmime_mimeinfo_next(partinfo);
199                 iter_valid = gtk_tree_model_iter_next(model, &iter);
200
201                 g_node_destroy(parent->node);
202                 att_removed++;
203         }
204
205         partinfo = last;
206         while (partinfo) {
207                 if (!(parent = procmime_mimeinfo_parent(partinfo)))
208                         break;
209                 
210                 /* multipart/{alternative,mixed,related} make sense
211                    only when they have at least 2 nodes, remove last
212                    one and move it one level up if otherwise  */
213                 if (MIMEINFO_NOT_ATTACHMENT(partinfo) &&
214                         g_node_n_children(partinfo->node) < 2)
215                 {
216                         gint pos = g_node_child_position(parent->node, partinfo->node);         
217                         g_node_unlink(partinfo->node);
218                         
219                         if ((child = g_node_first_child(partinfo->node))) {
220                                 g_node_unlink(child);
221                                 g_node_insert(parent->node, pos, child);
222                         }
223                         g_node_destroy(partinfo->node);
224                         
225                         child = g_node_last_child(parent->node);
226                         partinfo = child ? child->data : parent;
227                         continue;
228                 }
229
230                 if (partinfo->node->prev) {
231                         partinfo = (MimeInfo *) partinfo->node->prev->data;
232                         if (partinfo->node->children) {
233                                 child = g_node_last_child(partinfo->node);
234                                 partinfo = (MimeInfo *) child->data;                    
235                         }
236                 } else if (partinfo->node->parent)
237                         partinfo = (MimeInfo *) partinfo->node->parent->data;           
238         }
239
240         msgnum = save_new_message(attremover->msginfo, newmsg, info,
241                          (att_all - att_removed > 0));
242                          
243         inc_unlock();
244         folder_item_update_thaw();
245         gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
246         main_window_cursor_normal(mainwin);
247
248         if (msgnum > 0)
249                 summary_select_by_msgnum(summaryview, msgnum);
250
251         gtk_widget_destroy(attremover->window);
252 }
253
254 static void remove_toggled_cb(GtkCellRendererToggle *cell, gchar *path_str,
255                                 gpointer data)
256 {
257         GtkTreeModel *model = (GtkTreeModel *)data;
258         GtkTreeIter  iter;
259         GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
260         gboolean fixed;
261                                
262         gtk_tree_model_get_iter (model, &iter, path);
263         gtk_tree_model_get (model, &iter, ATT_REMOVER_TOGGLE, &fixed, -1);
264                                      
265         fixed ^= 1;
266         gtk_list_store_set (GTK_LIST_STORE (model), &iter, ATT_REMOVER_TOGGLE, fixed, -1);
267                                              
268         gtk_tree_path_free (path);
269 }
270
271 static void fill_attachment_store(GtkTreeView *list_view, MimeInfo *partinfo)
272 {
273         const gchar *name;
274         gchar *label, *content_type;
275         GtkTreeIter iter;
276         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
277                                         (GTK_TREE_VIEW(list_view)));
278
279         partinfo = find_first_text_part(partinfo);
280         partinfo = procmime_mimeinfo_next(partinfo);
281         if (!partinfo)
282                 return;
283                 
284         while (partinfo) {
285                 if (MIMEINFO_NOT_ATTACHMENT(partinfo)) {
286                         partinfo = procmime_mimeinfo_next(partinfo);
287                         continue;
288                 }
289                         
290                 content_type = procmime_get_content_type_str(
291                                         partinfo->type, partinfo->subtype);
292                 
293                 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
294                 if (!name)
295                         name = procmime_mimeinfo_get_parameter(partinfo, "name");
296                 if (!name)
297                         name = _("unknown");
298                 
299                 label = g_strconcat(_("<b>Type: </b>"), content_type, "   ",
300                         _("<b>Size: </b>"), to_human_readable((goffset)partinfo->length),
301                         "\n", _("<b>Filename: </b>"), name, NULL);
302
303                 gtk_list_store_append(list_store, &iter);
304                 gtk_list_store_set(list_store, &iter,
305                                    ATT_REMOVER_INFO, label,
306                                    ATT_REMOVER_TOGGLE, TRUE,
307                                    -1);
308                 g_free(label);
309                 g_free(content_type);
310                 partinfo = procmime_mimeinfo_next(partinfo);
311         }
312 }
313
314 static void remove_attachments_dialog(AttRemover *attremover)
315 {
316         GtkWidget *window;
317         GtkWidget *vbox;
318         GtkTreeView *list_view;
319         GtkTreeModel *model;
320         GtkTreeViewColumn *column;
321         GtkCellRenderer *renderer;
322         GtkWidget *scrollwin;
323         GtkWidget *hbbox;
324         GtkWidget *ok_btn;
325         GtkWidget *cancel_btn;
326         MimeInfo *info = procmime_scan_message(attremover->msginfo);
327
328         static GdkGeometry geometry;
329
330         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "AttRemover");
331         gtk_container_set_border_width( GTK_CONTAINER(window), VBOX_BORDER);
332         gtk_window_set_title(GTK_WINDOW(window), _("Remove attachments"));
333         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
334         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
335
336         g_signal_connect(G_OBJECT(window), "delete_event",
337                           G_CALLBACK(cancelled_event_cb), attremover);
338         g_signal_connect(G_OBJECT(window), "key_press_event",
339                           G_CALLBACK(key_pressed_cb), attremover);
340         g_signal_connect(G_OBJECT(window), "size_allocate",
341                          G_CALLBACK(size_allocate_cb), NULL);
342
343         vbox = gtk_vbox_new(FALSE, VBOX_BORDER);
344         gtk_container_add(GTK_CONTAINER(window), vbox);
345
346         model = GTK_TREE_MODEL(gtk_list_store_new(N_ATT_REMOVER_COLUMNS,
347                                   G_TYPE_STRING,
348                                   G_TYPE_BOOLEAN,
349                                   -1));
350         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
351         g_object_unref(model);  
352         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
353         
354         renderer = gtk_cell_renderer_toggle_new();
355         g_signal_connect(renderer, "toggled", G_CALLBACK(remove_toggled_cb), model);
356         column = gtk_tree_view_column_new_with_attributes
357                 (_("Remove"),
358                 renderer,
359                 "active", ATT_REMOVER_TOGGLE,
360                 NULL);
361         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
362
363         renderer = gtk_cell_renderer_text_new();
364         column = gtk_tree_view_column_new_with_attributes
365                 (_("Attachment"),
366                  renderer,
367                  "markup", ATT_REMOVER_INFO,
368                  NULL);
369         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
370         fill_attachment_store(list_view, info);
371
372         scrollwin = gtk_scrolled_window_new(NULL, NULL);
373         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrollwin),
374                                 GTK_SHADOW_ETCHED_OUT);
375         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
376                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
377         gtk_container_add(GTK_CONTAINER(scrollwin), GTK_WIDGET(list_view));
378         gtk_container_set_border_width(GTK_CONTAINER(scrollwin), 4);
379         gtk_box_pack_start(GTK_BOX(vbox), scrollwin, TRUE, TRUE, 0); 
380
381         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
382                                       &ok_btn, GTK_STOCK_OK,
383                                       NULL, NULL);
384         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
385         gtk_container_set_border_width(GTK_CONTAINER(hbbox), HSPACING_NARROW);
386         gtk_widget_grab_default(ok_btn);
387
388         g_signal_connect(G_OBJECT(ok_btn), "clicked",
389                          G_CALLBACK(remove_attachments_cb), attremover);
390         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
391                          G_CALLBACK(cancelled_event_cb), attremover);
392                          
393         if (!geometry.min_height) {
394                 geometry.min_width = 450;
395                 geometry.min_height = 350;
396         }
397
398         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
399                                       GDK_HINT_MIN_SIZE);
400         gtk_widget_set_size_request(window, attremover->win_width,
401                                         attremover->win_height);
402
403         attremover->window = window;
404         attremover->model  = model;
405
406         gtk_widget_show_all(window);
407         gtk_widget_grab_focus(ok_btn);
408 }
409
410 static void remove_attachments(GSList *msglist)
411 {
412         MainWindow *mainwin = mainwindow_get_mainwindow();
413         SummaryView *summaryview = mainwin->summaryview;
414         GSList *cur;
415         gint msgnum = -1;
416         
417         if (alertpanel_full(_("Destroy attachments"),
418                   _("Do you really want to remove all attachments from "
419                   "the selected messages?\n\n"
420                   "The deleted data will be unrecoverable."), 
421                   GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL,
422                   FALSE, NULL, ALERT_QUESTION, G_ALERTALTERNATE) != G_ALERTALTERNATE)
423                 return;
424
425         main_window_cursor_wait(summaryview->mainwin);
426         gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
427         folder_item_update_freeze();
428         inc_lock();
429
430         for (cur = msglist; cur; cur = cur->next) {
431                 MsgInfo *msginfo = (MsgInfo *)cur->data;
432                 MsgInfo *newmsg = NULL;
433                 MimeInfo *info = NULL;
434                 MimeInfo *partinfo = NULL;
435
436                 if (!msginfo)
437                         continue;
438                 
439                 newmsg = procmsg_msginfo_copy(msginfo);
440                 info = procmime_scan_message(newmsg);
441         
442                 if ( !(partinfo = find_first_text_part(info)) ) {
443                         procmsg_msginfo_free(newmsg);
444                         continue;
445                 }
446                 partinfo->node->next = NULL;
447                 partinfo->node->children = NULL;
448                 info->node->children->data = partinfo;
449
450                 msgnum = save_new_message(msginfo, newmsg, info, FALSE);                
451         }
452
453         inc_unlock();
454         folder_item_update_thaw();
455         gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
456         main_window_cursor_normal(summaryview->mainwin);
457
458         if (msgnum > 0) {
459                 summary_select_by_msgnum(summaryview, msgnum);
460         }
461 }
462
463 static void remove_attachments_ui(GtkAction *action, gpointer data)
464 {
465         MainWindow *mainwin = mainwindow_get_mainwindow();
466         GSList *msglist = summary_get_selected_msg_list(mainwin->summaryview);
467         MimeInfo *info = NULL, *partinfo = NULL;
468         
469         if (summary_is_locked(mainwin->summaryview) || !msglist) 
470                 return;
471
472         if (g_slist_length(msglist) == 1) {
473                 info = procmime_scan_message(msglist->data);
474         
475                 partinfo = find_first_text_part(info);
476                 partinfo = procmime_mimeinfo_next(partinfo);
477                 
478                 if (!partinfo) {
479                         alertpanel_notice(_("This message doesn't have any attachments."));
480                         g_slist_free(msglist);
481                         return;
482                 }
483                 
484                 AttRemoverData.msginfo = msglist->data;
485                 remove_attachments_dialog(&AttRemoverData);
486         } else
487                 remove_attachments(msglist);
488
489         g_slist_free(msglist);
490 }
491
492 static GtkActionEntry remove_att_main_menu[] = {{
493         "Message/RemoveAtt",
494         NULL, N_("Remove attachments..."), NULL, NULL, G_CALLBACK(remove_attachments_ui)
495 }};
496
497 static guint context_menu_id = 0;
498 static guint main_menu_id = 0;
499
500 gint plugin_init(gchar **error)
501 {
502         MainWindow *mainwin = mainwindow_get_mainwindow();
503         gchar *rcpath;
504         
505         if( !check_plugin_version(MAKE_NUMERIC_VERSION(3,6,1,27),
506                                 VERSION_NUMERIC, _("AttRemover"), error) )
507                 return -1;
508
509         gtk_action_group_add_actions(mainwin->action_group, remove_att_main_menu,
510                         1, (gpointer)mainwin);
511         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menu/Message", "RemoveAtt", 
512                           "Message/RemoveAtt", GTK_UI_MANAGER_MENUITEM,
513                           main_menu_id)
514         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "RemoveAtt", 
515                           "Message/RemoveAtt", GTK_UI_MANAGER_MENUITEM,
516                           context_menu_id)
517
518         prefs_set_default(prefs);
519         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
520         prefs_read_config(prefs, PREFS_BLOCK_NAME, rcpath, NULL);
521         g_free(rcpath);
522
523         return 0;
524 }
525
526 gboolean plugin_done(void)
527 {
528         MainWindow *mainwin = mainwindow_get_mainwindow();
529         PrefFile *pref_file;
530         gchar *rc_file_path;
531
532         rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
533                                    COMMON_RC, NULL);
534         pref_file = prefs_write_open(rc_file_path);
535         g_free(rc_file_path);
536         
537         if (!pref_file || prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0)
538                 return TRUE;
539         
540         if (prefs_write_param(prefs, pref_file->fp) < 0) {
541                 g_warning("failed to write AttRemover Plugin configuration\n");
542                 prefs_file_close_revert(pref_file);
543                 return TRUE;
544         }
545
546         if (fprintf(pref_file->fp, "\n") < 0) {
547                 FILE_OP_ERROR(rc_file_path, "fprintf");
548                 prefs_file_close_revert(pref_file);
549         } else
550                 prefs_file_close(pref_file);
551
552         if (mainwin == NULL)
553                 return TRUE;
554
555         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/RemoveAtt", main_menu_id);
556         main_menu_id = 0;
557
558         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/RemoveAtt", context_menu_id);
559         context_menu_id = 0;
560
561         return TRUE;
562 }
563
564 const gchar *plugin_name(void)
565 {
566         return _("AttRemover");
567 }
568
569 const gchar *plugin_desc(void)
570 {
571         return _("This plugin removes attachments from mails.\n\n"
572                  "Warning: this operation will be completely "
573                  "un-cancellable and the deleted attachments will "
574                  "be lost forever, and ever, and ever.");
575 }
576
577 const gchar *plugin_type(void)
578 {
579         return "GTK2";
580 }
581
582 const gchar *plugin_licence(void)
583 {
584                 return "GPL3+";
585 }
586
587 const gchar *plugin_version(void)
588 {
589         return VERSION;
590 }
591
592 struct PluginFeature *plugin_provides(void)
593 {
594         static struct PluginFeature features[] = 
595                 { {PLUGIN_UTILITY, N_("Attachment handling")},
596                   {PLUGIN_NOTHING, NULL}};
597         return features;
598 }