dd2b75e899b0897df21161445197092ba386d1bb
[claws.git] / src / selective_download.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <gtk/gtkoptionmenu.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "intl.h"
34 #include "main.h"
35 #include "prefs.h"
36 #include "prefs_matcher.h"
37 #include "prefs_filtering.h"
38 #include "prefs_account.h"
39 #include "account.h"
40 #include "mainwindow.h"
41 #include "manage_window.h"
42 #include "menu.h"
43 #include "stock_pixmap.h"
44 #include "inc.h"
45 #include "utils.h"
46 #include "gtkutils.h"
47 #include "alertpanel.h"
48 #include "folder.h"
49 #include "inc.h"
50 #include "filtering.h"
51 #include "matcher_parser.h"
52 #include "selective_download.h"
53 #include "procheader.h"
54
55 static struct _SDView {
56         MainWindow *mainwin;
57
58         GtkWidget *window;
59         GtkWidget *clist;
60         GtkWidget *preview_btn;
61         GtkWidget *remove_btn;
62         GtkWidget *download_btn;
63         GtkWidget *ac_button;
64         GtkWidget *ac_label;
65         GtkWidget *ac_menu;
66         GtkWidget *preview_popup;
67         GtkWidget *msgs_label;
68         GtkWidget *show_old_chkbtn;
69
70 }selective;
71
72 static GdkPixmap *checkboxonxpm;
73 static GdkBitmap *checkboxonxpmmask;
74 static GdkPixmap *checkboxoffxpm;
75 static GdkBitmap *checkboxoffxpmmask;
76 static GdkPixmap *markxpm;
77 static GdkBitmap *markxpmmask;
78 static GdkPixmap *deletedxpm;
79 static GdkBitmap *deletedxpmmask;
80 static GdkPixmap *continuexpm;
81 static GdkBitmap *continuexpmmask;
82
83
84 /* local functions */
85 static void sd_clear_msglist();
86 static void sd_remove_header_files();
87 static void sd_toggle_btn();
88 static SD_State sd_header_filter (MsgInfo *msginfo);
89 static MsgInfo *sd_get_msginfo_from_file (const gchar *filename);
90
91 static void sd_clist_set_pixmap(HeaderItems *items, gint row);
92 static void sd_clist_get_items();
93 static void sd_clist_set_items();
94 static void sd_update_msg_num(PrefsAccount *acc);
95
96 enum {
97         PREVIEW_NEW,
98         PREVIEW_ALL,
99         REMOVE,
100         DOWNLOAD,
101         DONE,
102         CHECKBTN,
103 };
104
105 /* callbacks */
106 static void sd_action_cb(GtkWidget *widget, guint action);
107
108 static void sd_select_row_cb (GtkCList *clist, gint row, gint column,
109                               GdkEvent *event, gpointer user_data);
110 static void sd_key_pressed (GtkWidget *widget,
111                             GdkEventKey *event,
112                             gpointer data);
113 /* account menu */
114 static void sd_ac_label_pressed(GtkWidget *widget, 
115                                 GdkEventButton *event,
116                                 gpointer data);
117
118 static void sd_ac_menu_popup_closed(GtkMenuShell *menu_shell);
119 static void sd_ac_menu_cb(GtkMenuItem *menuitem, gpointer data);
120 static void sd_ac_menu_set();
121
122 /* preview popup */
123 static void sd_preview_popup_closed(GtkMenuShell *menu_shell);
124 static void sd_preview_popup_cb(GtkWidget *widget, GdkEventButton *event);
125 static GtkItemFactoryEntry preview_popup_entries[] =
126 {
127         {N_("/Preview _new Messages"), NULL, sd_action_cb, PREVIEW_NEW, NULL},
128         {N_("/Preview _all Messages"), NULL, sd_action_cb, PREVIEW_ALL, NULL}
129 };
130
131 /* create dialog */
132 static void sd_window_create (MainWindow *mainwin);
133
134 void selective_download(MainWindow *mainwin)
135 {
136         summary_write_cache(mainwin->summaryview);
137
138         sd_remove_header_files();
139         
140         stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_CHECKBOX_OFF,
141                          &checkboxoffxpm, &checkboxoffxpmmask);
142         stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_CHECKBOX_ON,
143                          &checkboxonxpm, &checkboxonxpmmask);
144         stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_DELETED,
145                          &deletedxpm, &deletedxpmmask);
146         stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_CONTINUE,
147                          &continuexpm, &continuexpmmask);
148         stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_MARK,
149                          &markxpm, &markxpmmask);
150         inc_lock();
151
152         if (!selective.window)
153             sd_window_create(mainwin);
154
155         manage_window_set_transient(GTK_WINDOW(selective.window));
156         gtk_widget_show(selective.window);
157         sd_clear_msglist();
158         sd_ac_menu_set();
159         gtk_clist_clear(GTK_CLIST(selective.clist));
160
161
162 }
163
164 static void sd_clear_msglist()
165 {
166         PrefsAccount *acc = cur_account;
167         while (acc->msg_list != NULL) {
168                 HeaderItems *item = (HeaderItems*)acc->msg_list->data;
169                 acc->msg_list = g_slist_remove(acc->msg_list, item);
170         }       
171         g_slist_free(acc->msg_list);
172         sd_update_msg_num(acc);
173 }
174
175 /* sd_remove_header_files()
176  * 
177  * - removes any stale header files in HEADER_CACHE_DIR 
178  *
179  */
180 static void sd_remove_header_files()
181 {
182         gchar *path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
183         
184         remove_all_files(path);
185         g_free(path);
186 }
187
188 /* sd_toggle_btn()
189  *
190  * - checks whether at least on email is selected 
191  *   if so, untoggle remove / download button
192  */
193 static void sd_toggle_btn()
194 {
195         PrefsAccount *acc = cur_account;
196         GSList *cur;
197         
198         for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
199                 HeaderItems *items = (HeaderItems*)cur->data;
200                 
201                 if (items->state == SD_CHECKED) {
202                         gtk_widget_set_sensitive (selective.remove_btn, TRUE);
203                         gtk_widget_set_sensitive (selective.download_btn, TRUE);
204                         return;
205                 }
206         }
207
208         gtk_widget_set_sensitive (selective.remove_btn, FALSE);
209 }
210
211 /* sd_header_filter(MsgInfo *msginfo)
212  *
213  * - parse header line and look for any applying filtering rules
214  * - if message matches other MATCHACTION --> return
215  *
216  */
217 SD_State sd_header_filter(MsgInfo *msginfo)
218 {
219         GSList *rules;
220
221         /* parse header line for line */
222         for (rules = global_processing; rules != NULL; rules = rules->next) { 
223
224                 FilteringProp *prop = (FilteringProp*) rules->data; 
225
226                 if ( matcherlist_match(prop->matchers, msginfo) ) {
227                         if (prop->action->type == MATCHACTION_DELETE_ON_SERVER) {
228                                 debug_print(_("action matched\n"));
229                                 return SD_CHECKED;
230                         }
231                         else {
232                                 debug_print(_("action not matched\n"));
233                                 return SD_UNCHECKED;
234                         }
235                 }
236         }
237         return SD_UNCHECKED;
238 }
239
240 /* sd_get_msginfo_from_file(const gchar *filename)
241  *
242  * - retrieve msginfo from saved header files
243  */
244 MsgInfo *sd_get_msginfo_from_file(const gchar *filename)
245 {
246         MsgInfo *msginfo  = g_new0(MsgInfo, 1);
247         MsgInfo *msg;
248         MsgFlags msgflags = { 0, 0 };
249
250         msg  = procheader_parse(filename, msgflags, TRUE, FALSE);
251
252         if (!msg) {
253                 msginfo->subject = _("(No Subject)");
254                 msginfo->from    = _("(No Sender)");
255                 msginfo->date    = _("(No Date)");
256         } else {
257                 g_memmove(&msginfo, &msg, sizeof(msginfo)); 
258
259                 if (msginfo->date_t) {
260                         gchar date_modified[80];
261                         procheader_date_get_localtime(date_modified,
262                                                       sizeof(date_modified),
263                                                       msginfo->date_t);
264                         msginfo->date = date_modified;
265                 }
266         } 
267         return msginfo;
268 }
269
270 static void sd_clist_set_pixmap(HeaderItems *items, gint row)
271 {
272         
273         switch (items->state) {
274         case SD_REMOVED:
275                 gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
276                                       row, 0,
277                                      deletedxpm, deletedxpmmask);
278                 break;
279         case SD_CHECKED:
280                 gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
281                                       row, 0,
282                                       checkboxonxpm, checkboxonxpmmask);
283                 break;
284         case SD_DOWNLOADED:
285                 gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
286                                       row, 0,
287                                       markxpm, markxpmmask);
288                 break;          
289         default:
290                 gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
291                                       row, 0,
292                                       checkboxoffxpm, checkboxoffxpmmask);
293                 break;
294         }
295 }
296
297 static void sd_clist_set_items()
298 {
299         PrefsAccount *acc = cur_account;
300         GSList *cur;
301         gboolean show_old = 
302                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selective.show_old_chkbtn));
303         
304         gtk_clist_clear(GTK_CLIST(selective.clist));
305         gtk_clist_freeze(GTK_CLIST(selective.clist));
306
307         for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
308                 HeaderItems *items = (HeaderItems*)cur->data;
309                 gchar *row[5];
310                 gint row_num;
311                 
312                 row[0] = _("");
313                 row[1] = items->from;
314                 row[2] = items->subject;
315                 row[3] = items->date;
316                 row[4] = g_strdup_printf("%i KB", items->size/1024);
317                 
318                 switch (items->state) {
319                 case SD_REMOVED:
320                 case SD_DOWNLOADED:
321                         items->del_by_old_session = TRUE;
322                 default:
323                         break;
324                 }
325                 if (show_old) {
326                         if (items->received) {
327                                 row_num = gtk_clist_append(GTK_CLIST(selective.clist), row);
328                                 sd_clist_set_pixmap(items, row_num);
329                         }
330                 }
331                 else {
332                         row_num = gtk_clist_append(GTK_CLIST(selective.clist), row);
333                         sd_clist_set_pixmap(items, row_num);
334                 }
335                 g_free(row[4]);
336         }
337
338         gtk_clist_thaw(GTK_CLIST(selective.clist));
339         sd_toggle_btn();
340 }
341
342 /* sd_update_msg_num(PrefsAccount *acc)
343  * - keep track of msgs still on server
344  * - count UNCHECKED items as well as downloaded but not removed
345  */
346 static void sd_update_msg_num(PrefsAccount *acc)
347 {
348         GSList *cur;
349         gint msg_num = g_slist_length(acc->msg_list);
350         gchar *text;
351
352         for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
353                 HeaderItems *items = (HeaderItems*) cur->data;
354
355                 if (items->state != SD_UNCHECKED)
356                         msg_num--;
357                 if (items->state == SD_DOWNLOADED)
358                         if (!acc->sd_rmmail_on_download)
359                                 msg_num++;
360         }
361
362         text = g_strdup_printf("%i Messages", msg_num);
363         gtk_label_set_text(GTK_LABEL(selective.msgs_label), text);
364
365         g_free(text);
366 }
367
368 /* sd_clist_get_items()
369  *
370  * - get items for clist from Files
371  */
372 static void sd_clist_get_items()
373 {
374         GSList *cur;
375         PrefsAccount *acc = cur_account;
376         gchar *path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
377         
378         for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
379
380                 HeaderItems *items = (HeaderItems*) cur->data;
381                 gchar *filename = g_strdup_printf("%s%i", path, items->index);
382                 MsgInfo *msginfo  = sd_get_msginfo_from_file(filename);
383                 
384                 items->from    = msginfo->from;
385                 items->subject = msginfo->subject;
386                 strncpy2(items->date, msginfo->date, sizeof(items->date));
387                 
388                 msginfo->folder = folder_get_default_processing();
389
390                 /* move msg file to drop folder */
391                 if ((msginfo->msgnum = folder_item_add_msg(msginfo->folder, 
392                                                            filename, TRUE)) < 0) {
393                         unlink(filename);
394                         return;
395                 }
396                 
397                 if (acc->sd_filter_on_recv)
398                         items->state = sd_header_filter(msginfo);
399
400                 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
401                 g_free(filename);
402                 g_free(msginfo);
403         }
404
405         g_free(path);
406 }
407
408 static gint sd_deleted_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
409 {
410         sd_remove_header_files();
411         sd_clear_msglist();
412         gtk_widget_destroy(selective.window);   
413         selective.window = NULL;
414         inc_unlock();
415         return TRUE;
416 }
417
418 /* --- Callbacks -- */
419 static void sd_action_cb(GtkWidget *widget, guint action)
420 {
421         PrefsAccount *acc = cur_account;
422         
423         switch(action) {
424         case PREVIEW_NEW:
425         case PREVIEW_ALL:
426                 if ( (acc->protocol != A_APOP) &&
427                      (acc->protocol != A_POP3) ) {
428                         alertpanel_error(
429                                 _("Selected Account \"%s\" is not a POP Mail Server.\nPlease select a different Account"), acc->account_name);
430                         return;
431                 }
432                 sd_clear_msglist();
433                 if (action == PREVIEW_NEW) {
434                         gtk_widget_set_sensitive(selective.show_old_chkbtn, FALSE);
435                         inc_selective_download(selective.mainwin, acc, STYPE_PREVIEW_NEW);
436                 }
437                 else {
438                         gtk_widget_set_sensitive(selective.show_old_chkbtn, TRUE);
439                         inc_selective_download(selective.mainwin, acc, STYPE_PREVIEW_ALL);
440                 }
441                 
442                 gtk_clist_clear(GTK_CLIST(selective.clist));
443                 sd_clist_get_items();   
444                 sd_clist_set_items();
445                 break;
446         case REMOVE:
447                 inc_selective_download(selective.mainwin, acc, STYPE_DELETE);
448                 sd_clist_set_items();
449                 break;
450         case DOWNLOAD:
451                 inc_selective_download(selective.mainwin, acc, STYPE_DOWNLOAD);
452                 sd_clist_set_items();
453                 break;
454         case DONE:
455                 sd_remove_header_files();
456                 sd_clear_msglist();
457                 gtk_widget_hide(selective.window);
458                 inc_unlock();
459                 break;
460         case CHECKBTN:
461                 sd_clist_set_items();
462                 break;
463         default:
464                 break;
465         }
466         
467         sd_update_msg_num(acc);
468 }
469
470 /* Events */
471 static void sd_select_row_cb(GtkCList *clist, gint row, gint column,
472                              GdkEvent *event, gpointer user_data)
473 {
474         if ((row >= 0) && (column >= 0)) {
475                 PrefsAccount *acc  = cur_account;
476                 HeaderItems *items = (HeaderItems*) g_slist_nth_data (acc->msg_list, row);
477
478                 if (!items) return;
479                 if (!gtk_clist_get_selectable(GTK_CLIST(selective.clist), row)) return;
480
481                 if (items->state == SD_UNCHECKED) {
482                         items->state = SD_CHECKED;
483                         gtk_clist_set_pixmap (GTK_CLIST (selective.clist), row, 0,
484                                               checkboxonxpm, checkboxonxpmmask);
485                 } else if (items->state == SD_CHECKED) {
486                         items->state = SD_UNCHECKED;
487                         gtk_clist_set_pixmap (GTK_CLIST (selective.clist), row, 0,
488                                               checkboxoffxpm, checkboxoffxpmmask);
489                 }
490                 sd_toggle_btn();
491         }
492 }
493
494 static void sd_key_pressed(GtkWidget *widget,
495                            GdkEventKey *event,
496                            gpointer data)
497 {
498         if (event && event->keyval == GDK_Escape)
499                 sd_action_cb(widget, DONE);
500 }
501
502 /* account menu */
503 static void sd_ac_label_pressed(GtkWidget *widget, GdkEventButton *event,
504                                     gpointer data)
505 {
506         if (!event) return;
507
508         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
509         gtk_object_set_data(GTK_OBJECT(selective.ac_menu), "menu_button",
510                             widget);
511
512         gtk_menu_popup(GTK_MENU(selective.ac_menu), NULL, NULL,
513                        menu_button_position, widget,
514                        event->button, event->time);
515 }
516
517 static void sd_ac_menu_popup_closed(GtkMenuShell *menu_shell)
518 {
519         GtkWidget *button;
520
521         button = gtk_object_get_data(GTK_OBJECT(menu_shell), "menu_button");
522         if (!button) return;
523         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
524         gtk_clist_clear(GTK_CLIST(selective.clist));
525         sd_clear_msglist();
526         sd_toggle_btn();
527         gtk_object_remove_data(GTK_OBJECT(selective.ac_menu), "menu_button");
528 }
529
530 static void sd_ac_menu_cb(GtkMenuItem *menuitem, gpointer data)
531 {
532         cur_account = (PrefsAccount *)data;
533         gtk_label_set_text(GTK_LABEL(selective.ac_label), cur_account->account_name);
534         gtk_widget_queue_resize(selective.ac_button);
535         main_window_reflect_prefs_all();
536 }
537
538 static void sd_ac_menu_set()
539 {
540         GList *cur_ac, *cur_item;
541         GtkWidget *menuitem;
542         PrefsAccount *ac_prefs;
543         GList *account_list = account_get_list();
544
545         /* destroy all previous menu item */
546         cur_item = GTK_MENU_SHELL(selective.ac_menu)->children;
547         while (cur_item != NULL) {
548                 GList *next = cur_item->next;
549                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
550                 cur_item = next;
551         }
552
553         gtk_label_set_text(GTK_LABEL(selective.ac_label), cur_account->account_name);
554
555         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
556                 ac_prefs = (PrefsAccount *)cur_ac->data;
557                 
558                 menuitem = gtk_menu_item_new_with_label
559                         (ac_prefs->account_name
560                          ? ac_prefs->account_name : _("Untitled"));
561                 gtk_widget_show(menuitem);
562                 gtk_menu_append(GTK_MENU(selective.ac_menu), menuitem);
563                 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
564                                            GTK_SIGNAL_FUNC(sd_ac_menu_cb),
565                                            ac_prefs);
566         }
567 }
568
569 /* receive button popup */
570 static void sd_preview_popup_closed(GtkMenuShell *menu_shell)
571 {
572         gtk_button_set_relief(GTK_BUTTON(selective.preview_btn), GTK_RELIEF_NORMAL);
573         manage_window_focus_in(selective.window, NULL, NULL);
574 }
575
576 static void sd_preview_popup_cb(GtkWidget *widget, GdkEventButton *event)
577 {
578         if (!event) return;
579         
580         if (event->button == 1) {
581                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
582                 gtk_menu_popup(GTK_MENU(selective.preview_popup), NULL, NULL,
583                        menu_button_position, widget,
584                        event->button, event->time);
585         }
586 }
587
588 static void sd_window_create(MainWindow *mainwin)
589 {
590         GtkWidget *window;
591         GtkWidget *table;
592         GtkWidget *msgs_label;
593         GtkWidget *bottom_hbox;
594         GtkWidget *fixed_label;
595         GtkWidget *expand_label;
596         GtkWidget *ac_button;
597         GtkWidget *ac_label;
598         GtkWidget *ac_menu;
599         GtkWidget *show_old_chkbtn;
600         GtkWidget *scrolledwindow;
601         GtkWidget *clist;
602         GtkWidget *state_label;
603         GtkWidget *from_label;
604         GtkWidget *subject_label;
605         GtkWidget *size_label;
606         GtkWidget *date_label;
607         GtkWidget *toolbar_hbox;
608         GtkWidget *toolbar;
609         GtkWidget *tmp_toolbar_icon;
610         GtkWidget *preview_btn;
611         GtkWidget *preview_popup;
612         GtkWidget *remove_btn;
613         GtkWidget *download_btn;
614         GtkWidget *done_btn;
615         gint n_menu_entries;
616
617         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
618         gtk_object_set_data (GTK_OBJECT (window), "window", window);
619         gtk_window_set_title (GTK_WINDOW (window), _("Selective download"));
620         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
621         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
622         gtk_window_set_policy (GTK_WINDOW (window), TRUE, TRUE, TRUE);
623
624         table = gtk_table_new (2, 2, FALSE);
625         gtk_container_add (GTK_CONTAINER (window), table);
626
627         msgs_label = gtk_label_new (_("0 Messages"));
628         gtk_table_attach (GTK_TABLE (table), msgs_label, 1, 2, 1, 2,
629                           (GtkAttachOptions) (GTK_FILL),
630                           (GtkAttachOptions) (0), 0, 0);
631         gtk_misc_set_alignment (GTK_MISC (msgs_label), 0, 0.5);
632
633         bottom_hbox = gtk_hbox_new (FALSE, 0);
634         gtk_table_attach (GTK_TABLE (table), bottom_hbox, 0, 1, 1, 2,
635                           (GtkAttachOptions) (GTK_FILL),
636                           (GtkAttachOptions) (GTK_FILL), 0, 0);
637         
638         show_old_chkbtn = gtk_check_button_new_with_label("Show only old Messages"); 
639         gtk_box_pack_start(GTK_BOX(bottom_hbox), show_old_chkbtn, FALSE, FALSE, 0); 
640         
641         gtk_signal_connect(GTK_OBJECT(show_old_chkbtn), "toggled", 
642                            GTK_SIGNAL_FUNC(sd_action_cb), GUINT_TO_POINTER(CHECKBTN)); 
643         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(show_old_chkbtn), FALSE);
644         GTK_WIDGET_UNSET_FLAGS(show_old_chkbtn, GTK_CAN_FOCUS);                                                 
645                            
646         expand_label = gtk_label_new (_(" "));
647         gtk_box_pack_start (GTK_BOX (bottom_hbox), expand_label, TRUE, TRUE, 0);
648         
649         fixed_label = gtk_label_new (_(" contains "));
650         gtk_box_pack_end (GTK_BOX (bottom_hbox), fixed_label, FALSE, FALSE, 0);
651
652         ac_menu = gtk_menu_new();
653         gtk_signal_connect(GTK_OBJECT(ac_menu), "selection_done",
654                            GTK_SIGNAL_FUNC(sd_ac_menu_popup_closed), NULL);
655         ac_button = gtk_button_new();
656         gtk_button_set_relief(GTK_BUTTON(ac_button), GTK_RELIEF_NONE);
657         GTK_WIDGET_UNSET_FLAGS(ac_button, GTK_CAN_FOCUS);
658         gtk_widget_set_usize(ac_button, -1, 1);
659         gtk_box_pack_start(GTK_BOX(bottom_hbox), ac_button, FALSE, FALSE, 0);
660         gtk_signal_connect(GTK_OBJECT(ac_button), "button_press_event",
661                            GTK_SIGNAL_FUNC(sd_ac_label_pressed), GTK_OBJECT(ac_menu));
662
663         ac_label = gtk_label_new("");
664         gtk_container_add(GTK_CONTAINER(ac_button), ac_label);
665
666         scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
667         gtk_table_attach (GTK_TABLE (table), scrolledwindow, 0, 1, 0, 1,
668                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
669                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);
670         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow), 
671                                         GTK_POLICY_NEVER, 
672                                         GTK_POLICY_AUTOMATIC);
673         
674         clist = gtk_clist_new (5);
675         gtk_container_add (GTK_CONTAINER (scrolledwindow), clist);
676         gtk_container_set_border_width (GTK_CONTAINER (clist), 5);
677         gtk_clist_set_column_width (GTK_CLIST (clist), 0, 20);
678         gtk_clist_set_column_width (GTK_CLIST (clist), 1, 150);
679         gtk_clist_set_column_width (GTK_CLIST (clist), 2, 150);
680         gtk_clist_set_column_width (GTK_CLIST (clist), 3, 100);
681         gtk_clist_set_column_width (GTK_CLIST (clist), 4, 30);
682         gtk_clist_set_selection_mode (GTK_CLIST (clist), GTK_SELECTION_BROWSE);
683         gtk_clist_column_titles_show (GTK_CLIST (clist));
684         
685         state_label = gtk_label_new (_("#"));
686         gtk_clist_set_column_widget (GTK_CLIST (clist), 0, state_label);
687         
688         from_label = gtk_label_new (_("From"));
689         gtk_clist_set_column_widget (GTK_CLIST (clist), 1, from_label);
690         
691         subject_label = gtk_label_new (_("Subject"));
692         gtk_clist_set_column_widget (GTK_CLIST (clist), 2, subject_label);
693         
694         date_label = gtk_label_new (_("Date"));
695         gtk_clist_set_column_widget (GTK_CLIST (clist), 3, date_label);
696
697         size_label = gtk_label_new (_("Size"));
698         gtk_widget_ref (size_label);
699         gtk_clist_set_column_widget (GTK_CLIST (clist), 4, size_label);
700         
701         toolbar_hbox = gtk_hbox_new (FALSE, 0);
702
703         gtk_table_attach (GTK_TABLE (table), toolbar_hbox, 1, 2, 0, 1,
704                           (GtkAttachOptions) (GTK_FILL),
705                           (GtkAttachOptions) (GTK_FILL), 0, 0);
706         
707         toolbar = gtk_toolbar_new (GTK_ORIENTATION_VERTICAL, GTK_TOOLBAR_BOTH);
708
709         gtk_box_pack_end (GTK_BOX (toolbar_hbox), toolbar, FALSE, FALSE, 0);
710         gtk_toolbar_set_space_size (GTK_TOOLBAR (toolbar), 30);
711         gtk_toolbar_set_space_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_SPACE_LINE);
712         gtk_container_set_border_width (GTK_CONTAINER (toolbar), 5);
713         
714         tmp_toolbar_icon = stock_pixmap_widget(toolbar_hbox, STOCK_PIXMAP_MAIL_RECEIVE);
715         preview_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
716                                                   GTK_TOOLBAR_CHILD_BUTTON,
717                                                   NULL,
718                                                   _("Preview Mail"),
719                                                   _("preview old/new E-Mail on account"), NULL,
720                                                   tmp_toolbar_icon, NULL, NULL);
721
722         n_menu_entries = sizeof(preview_popup_entries)/sizeof(preview_popup_entries[0]);
723         preview_popup = popupmenu_create(preview_btn, preview_popup_entries, n_menu_entries,
724                                       "<SelectiveDownload>", window);
725
726         gtk_signal_connect(GTK_OBJECT(preview_popup), "selection_done",
727                            GTK_SIGNAL_FUNC(sd_preview_popup_closed), NULL);
728
729         gtk_toolbar_append_space (GTK_TOOLBAR (toolbar));
730         
731         tmp_toolbar_icon = stock_pixmap_widget(toolbar_hbox, STOCK_PIXMAP_CLOSE);
732         remove_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
733                                                  GTK_TOOLBAR_CHILD_BUTTON,
734                                                  NULL,
735                                                  _("Remove"),
736                                                  _("remove selected E-Mails"), NULL,
737                                                  tmp_toolbar_icon, NULL, NULL);
738
739         gtk_widget_set_sensitive (remove_btn, FALSE);
740
741         tmp_toolbar_icon = stock_pixmap_widget(toolbar_hbox, STOCK_PIXMAP_DOWN_ARROW);
742         download_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
743                                                  GTK_TOOLBAR_CHILD_BUTTON,
744                                                  NULL,
745                                                  _("Download"),
746                                                  _("Download selected E-Mails"), NULL,
747                                                  tmp_toolbar_icon, NULL, NULL);
748
749         gtk_widget_set_sensitive (download_btn, FALSE);
750
751         gtk_toolbar_append_space (GTK_TOOLBAR (toolbar));
752
753         tmp_toolbar_icon = stock_pixmap_widget (toolbar_hbox, STOCK_PIXMAP_COMPLETE);
754         done_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
755                                                GTK_TOOLBAR_CHILD_BUTTON,
756                                                NULL,
757                                                _("Done"),
758                                                _("Exit Dialog"), NULL,
759                                                tmp_toolbar_icon, NULL, NULL);
760         
761         gtk_signal_connect (GTK_OBJECT (window), "delete_event",
762                             GTK_SIGNAL_FUNC (gtk_widget_hide_on_delete),
763                             NULL);
764         gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
765                             GTK_SIGNAL_FUNC(sd_key_pressed),
766                             NULL);
767         MANAGE_WINDOW_SIGNALS_CONNECT(window);
768         gtk_signal_connect (GTK_OBJECT (preview_btn), "button_press_event",
769                             GTK_SIGNAL_FUNC (sd_preview_popup_cb),
770                             NULL);
771
772         gtk_signal_connect (GTK_OBJECT (remove_btn), "clicked",
773                             GTK_SIGNAL_FUNC (sd_action_cb),
774                             GUINT_TO_POINTER(REMOVE));
775         gtk_signal_connect (GTK_OBJECT (download_btn), "clicked",
776                             GTK_SIGNAL_FUNC (sd_action_cb),
777                             GUINT_TO_POINTER(DOWNLOAD));
778         gtk_signal_connect (GTK_OBJECT (done_btn), "clicked",
779                             GTK_SIGNAL_FUNC (sd_action_cb),
780                             GUINT_TO_POINTER(DONE));
781         gtk_signal_connect (GTK_OBJECT (clist), "select_row",
782                             GTK_SIGNAL_FUNC (sd_select_row_cb),
783                             NULL);
784
785
786         selective.mainwin         = mainwin;
787         selective.window          = window;
788         selective.clist           = clist;
789         selective.preview_btn     = preview_btn;
790         selective.remove_btn      = remove_btn;
791         selective.download_btn    = download_btn;
792         selective.ac_label        = ac_label;
793         selective.ac_button       = ac_button;
794         selective.ac_menu         = ac_menu;
795         selective.preview_popup   = preview_popup;
796         selective.msgs_label      = msgs_label;
797         selective.show_old_chkbtn = show_old_chkbtn;
798
799         gtk_widget_show_all(window);
800 }
801