85328c26df9c06c1dd7db6ba01575d59e4bd60cf
[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
170                 acc->msg_list = g_slist_remove(acc->msg_list, item);
171                 g_free(item->from);
172                 g_free(item->subject);
173                 g_free(item->date);
174                 g_free(item);
175         }       
176         g_slist_free(acc->msg_list);
177         sd_update_msg_num(acc);
178 }
179
180 /* sd_remove_header_files()
181  * 
182  * - removes any stale header files in HEADER_CACHE_DIR 
183  *
184  */
185 static void sd_remove_header_files()
186 {
187         gchar *path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
188         
189         remove_all_files(path);
190         g_free(path);
191 }
192
193 /* sd_toggle_btn()
194  *
195  * - checks whether at least on email is selected 
196  *   if so, untoggle remove / download button
197  */
198 static void sd_toggle_btn()
199 {
200         PrefsAccount *acc = cur_account;
201         GSList *cur;
202         
203         for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
204                 HeaderItems *items = (HeaderItems*)cur->data;
205                 
206                 if (items->state == SD_CHECKED) {
207                         gtk_widget_set_sensitive (selective.remove_btn, TRUE);
208                         gtk_widget_set_sensitive (selective.download_btn, TRUE);
209                         return;
210                 }
211         }
212
213         gtk_widget_set_sensitive (selective.remove_btn, FALSE);
214 }
215
216 /* sd_header_filter(MsgInfo *msginfo)
217  *
218  * - parse header line and look for any applying filtering rules
219  * - if message matches other MATCHACTION --> return
220  *
221  */
222 SD_State sd_header_filter(MsgInfo *msginfo)
223 {
224         GSList *rules;
225
226         /* parse header line for line */
227         for (rules = global_processing; rules != NULL; rules = rules->next) { 
228
229                 FilteringProp *prop = (FilteringProp*) rules->data; 
230
231                 if ( matcherlist_match(prop->matchers, msginfo) ) {
232                         if (prop->action->type == MATCHACTION_DELETE_ON_SERVER) {
233                                 debug_print(_("action matched\n"));
234                                 return SD_CHECKED;
235                         }
236                         else {
237                                 debug_print(_("action not matched\n"));
238                                 return SD_UNCHECKED;
239                         }
240                 }
241         }
242         return SD_UNCHECKED;
243 }
244
245 /* sd_get_msginfo_from_file(const gchar *filename)
246  *
247  * - retrieve msginfo from saved header files
248  */
249 static MsgInfo *sd_get_msginfo_from_file(const gchar *filename)
250 {
251         MsgInfo  *msginfo;
252         MsgFlags  msgflags = { 0, 0 };
253         gchar     date[80];
254
255         msginfo = procheader_parse_file(filename, msgflags, TRUE, FALSE);
256
257         if (msginfo && msginfo->date_t) {
258                 procheader_date_get_localtime(date, sizeof date, msginfo->date_t);
259                 if (msginfo->date) g_free(msginfo->date);
260                 msginfo->date = g_strdup(date);
261         } 
262
263         if (!msginfo)
264                 msginfo = g_new0(MsgInfo, 1);
265
266         if (!msginfo->date) 
267                 msginfo->date = g_strdup(_("(No Date)"));
268         if (!msginfo->from)
269                 msginfo->from = g_strdup(_("(No Sender)"));
270         if (!msginfo->subject)
271                 msginfo->subject = g_strdup(_("(No Subject)"));
272                 
273         return msginfo;
274 }
275
276 static void sd_clist_set_pixmap(HeaderItems *items, gint row)
277 {
278         
279         switch (items->state) {
280         case SD_REMOVED:
281                 gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
282                                       row, 0,
283                                      deletedxpm, deletedxpmmask);
284                 break;
285         case SD_CHECKED:
286                 gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
287                                       row, 0,
288                                       checkboxonxpm, checkboxonxpmmask);
289                 break;
290         case SD_DOWNLOADED:
291                 gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
292                                       row, 0,
293                                       markxpm, markxpmmask);
294                 break;          
295         default:
296                 gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
297                                       row, 0,
298                                       checkboxoffxpm, checkboxoffxpmmask);
299                 break;
300         }
301 }
302
303 static void sd_clist_set_items()
304 {
305         PrefsAccount *acc = cur_account;
306         GSList *cur;
307         gboolean show_old = 
308                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selective.show_old_chkbtn));
309         
310         gtk_clist_clear(GTK_CLIST(selective.clist));
311         gtk_clist_freeze(GTK_CLIST(selective.clist));
312
313         for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
314                 HeaderItems *items = (HeaderItems*)cur->data;
315                 gchar *row[5];
316                 gint row_num;
317                 
318                 row[0] = _("");
319                 row[1] = items->from;
320                 row[2] = items->subject;
321                 row[3] = items->date;
322                 row[4] = g_strdup_printf("%i KB", items->size/1024);
323                 
324                 switch (items->state) {
325                 case SD_REMOVED:
326                 case SD_DOWNLOADED:
327                         items->del_by_old_session = TRUE;
328                 default:
329                         break;
330                 }
331                 if (show_old) {
332                         if (items->received) {
333                                 row_num = gtk_clist_append(GTK_CLIST(selective.clist), row);
334                                 sd_clist_set_pixmap(items, row_num);
335                         }
336                 }
337                 else {
338                         row_num = gtk_clist_append(GTK_CLIST(selective.clist), row);
339                         sd_clist_set_pixmap(items, row_num);
340                 }
341                 g_free(row[4]);
342         }
343
344         gtk_clist_thaw(GTK_CLIST(selective.clist));
345         sd_toggle_btn();
346 }
347
348 /* sd_update_msg_num(PrefsAccount *acc)
349  * - keep track of msgs still on server
350  * - count UNCHECKED items as well as downloaded but not removed
351  */
352 static void sd_update_msg_num(PrefsAccount *acc)
353 {
354         GSList *cur;
355         gint msg_num = g_slist_length(acc->msg_list);
356         gchar *text;
357
358         for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
359                 HeaderItems *items = (HeaderItems*) cur->data;
360
361                 if (items->state != SD_UNCHECKED)
362                         msg_num--;
363                 if (items->state == SD_DOWNLOADED)
364                         if (!acc->sd_rmmail_on_download)
365                                 msg_num++;
366         }
367
368         text = g_strdup_printf("%i Messages", msg_num);
369         gtk_label_set_text(GTK_LABEL(selective.msgs_label), text);
370
371         g_free(text);
372 }
373
374 /* sd_clist_get_items()
375  *
376  * - get items for clist from Files
377  */
378 static void sd_clist_get_items(void)
379 {
380         GSList *cur;
381         PrefsAccount *acc = cur_account;
382         gchar *path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
383         
384         for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
385
386                 HeaderItems *items = (HeaderItems*) cur->data;
387                 gchar *filename    = g_strdup_printf("%s%i", path, items->index);
388                 MsgInfo *msginfo   = sd_get_msginfo_from_file(filename);
389                 
390                 items->from        = g_strdup(msginfo->from);
391                 items->subject     = g_strdup(msginfo->subject);
392                 items->date        = g_strdup(msginfo->date);
393                 
394                 msginfo->folder = folder_get_default_processing();
395
396                 /* move msg file to drop folder */
397                 if ((msginfo->msgnum = folder_item_add_msg(msginfo->folder, 
398                                                            filename, TRUE)) < 0) {
399                         unlink(filename);
400                         return;
401                 }
402                 
403                 if (acc->sd_filter_on_recv)
404                         items->state = sd_header_filter(msginfo);
405
406                 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
407                 g_free(filename);
408                 procmsg_msginfo_free(msginfo);
409         }
410
411         g_free(path);
412 }
413
414 static gint sd_deleted_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
415 {
416         sd_remove_header_files();
417         sd_clear_msglist();
418         gtk_widget_destroy(selective.window);   
419         selective.window = NULL;
420         inc_unlock();
421         return TRUE;
422 }
423
424 /* --- Callbacks -- */
425 static void sd_action_cb(GtkWidget *widget, guint action)
426 {
427         PrefsAccount *acc = cur_account;
428         
429         switch(action) {
430         case PREVIEW_NEW:
431         case PREVIEW_ALL:
432                 if ( (acc->protocol != A_APOP) &&
433                      (acc->protocol != A_POP3) ) {
434                         alertpanel_error(
435                                 _("Selected Account \"%s\" is not a POP Mail Server.\nPlease select a different Account"), acc->account_name);
436                         return;
437                 }
438                 sd_clear_msglist();
439                 if (action == PREVIEW_NEW) {
440                         gtk_widget_set_sensitive(selective.show_old_chkbtn, FALSE);
441                         inc_selective_download(selective.mainwin, acc, STYPE_PREVIEW_NEW);
442                 }
443                 else {
444                         gtk_widget_set_sensitive(selective.show_old_chkbtn, TRUE);
445                         inc_selective_download(selective.mainwin, acc, STYPE_PREVIEW_ALL);
446                 }
447                 
448                 gtk_clist_clear(GTK_CLIST(selective.clist));
449                 sd_clist_get_items();   
450                 sd_clist_set_items();
451                 break;
452         case REMOVE:
453                 inc_selective_download(selective.mainwin, acc, STYPE_DELETE);
454                 sd_clist_set_items();
455                 break;
456         case DOWNLOAD:
457                 inc_selective_download(selective.mainwin, acc, STYPE_DOWNLOAD);
458                 sd_clist_set_items();
459                 break;
460         case DONE:
461                 sd_remove_header_files();
462                 sd_clear_msglist();
463                 gtk_widget_hide(selective.window);
464                 inc_unlock();
465                 break;
466         case CHECKBTN:
467                 sd_clist_set_items();
468                 break;
469         default:
470                 break;
471         }
472         
473         sd_update_msg_num(acc);
474 }
475
476 /* Events */
477 static void sd_select_row_cb(GtkCList *clist, gint row, gint column,
478                              GdkEvent *event, gpointer user_data)
479 {
480         if ((row >= 0) && (column >= 0)) {
481                 PrefsAccount *acc  = cur_account;
482                 HeaderItems *items = (HeaderItems*) g_slist_nth_data (acc->msg_list, row);
483
484                 if (!items) return;
485                 if (!gtk_clist_get_selectable(GTK_CLIST(selective.clist), row)) return;
486
487                 if (items->state == SD_UNCHECKED) {
488                         items->state = SD_CHECKED;
489                         gtk_clist_set_pixmap (GTK_CLIST (selective.clist), row, 0,
490                                               checkboxonxpm, checkboxonxpmmask);
491                 } else if (items->state == SD_CHECKED) {
492                         items->state = SD_UNCHECKED;
493                         gtk_clist_set_pixmap (GTK_CLIST (selective.clist), row, 0,
494                                               checkboxoffxpm, checkboxoffxpmmask);
495                 }
496                 sd_toggle_btn();
497         }
498 }
499
500 static void sd_key_pressed(GtkWidget *widget,
501                            GdkEventKey *event,
502                            gpointer data)
503 {
504         if (event && event->keyval == GDK_Escape)
505                 sd_action_cb(widget, DONE);
506 }
507
508 /* account menu */
509 static void sd_ac_label_pressed(GtkWidget *widget, GdkEventButton *event,
510                                     gpointer data)
511 {
512         if (!event) return;
513
514         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
515         gtk_object_set_data(GTK_OBJECT(selective.ac_menu), "menu_button",
516                             widget);
517
518         gtk_menu_popup(GTK_MENU(selective.ac_menu), NULL, NULL,
519                        menu_button_position, widget,
520                        event->button, event->time);
521 }
522
523 static void sd_ac_menu_popup_closed(GtkMenuShell *menu_shell)
524 {
525         GtkWidget *button;
526
527         button = gtk_object_get_data(GTK_OBJECT(menu_shell), "menu_button");
528         if (!button) return;
529         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
530         gtk_clist_clear(GTK_CLIST(selective.clist));
531         sd_clear_msglist();
532         sd_toggle_btn();
533         gtk_object_remove_data(GTK_OBJECT(selective.ac_menu), "menu_button");
534 }
535
536 static void sd_ac_menu_cb(GtkMenuItem *menuitem, gpointer data)
537 {
538         cur_account = (PrefsAccount *)data;
539         gtk_label_set_text(GTK_LABEL(selective.ac_label), cur_account->account_name);
540         gtk_widget_queue_resize(selective.ac_button);
541         main_window_reflect_prefs_all();
542 }
543
544 static void sd_ac_menu_set()
545 {
546         GList *cur_ac, *cur_item;
547         GtkWidget *menuitem;
548         PrefsAccount *ac_prefs;
549         GList *account_list = account_get_list();
550
551         /* destroy all previous menu item */
552         cur_item = GTK_MENU_SHELL(selective.ac_menu)->children;
553         while (cur_item != NULL) {
554                 GList *next = cur_item->next;
555                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
556                 cur_item = next;
557         }
558
559         gtk_label_set_text(GTK_LABEL(selective.ac_label), cur_account->account_name);
560
561         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
562                 ac_prefs = (PrefsAccount *)cur_ac->data;
563                 
564                 menuitem = gtk_menu_item_new_with_label
565                         (ac_prefs->account_name
566                          ? ac_prefs->account_name : _("Untitled"));
567                 gtk_widget_show(menuitem);
568                 gtk_menu_append(GTK_MENU(selective.ac_menu), menuitem);
569                 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
570                                            GTK_SIGNAL_FUNC(sd_ac_menu_cb),
571                                            ac_prefs);
572         }
573 }
574
575 /* receive button popup */
576 static void sd_preview_popup_closed(GtkMenuShell *menu_shell)
577 {
578         gtk_button_set_relief(GTK_BUTTON(selective.preview_btn), GTK_RELIEF_NORMAL);
579         manage_window_focus_in(selective.window, NULL, NULL);
580 }
581
582 static void sd_preview_popup_cb(GtkWidget *widget, GdkEventButton *event)
583 {
584         if (!event) return;
585         
586         if (event->button == 1) {
587                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
588                 gtk_menu_popup(GTK_MENU(selective.preview_popup), NULL, NULL,
589                        menu_button_position, widget,
590                        event->button, event->time);
591         }
592 }
593
594 static void sd_window_create(MainWindow *mainwin)
595 {
596         GtkWidget *window;
597         GtkWidget *table;
598         GtkWidget *msgs_label;
599         GtkWidget *bottom_hbox;
600         GtkWidget *fixed_label;
601         GtkWidget *expand_label;
602         GtkWidget *ac_button;
603         GtkWidget *ac_label;
604         GtkWidget *ac_menu;
605         GtkWidget *show_old_chkbtn;
606         GtkWidget *scrolledwindow;
607         GtkWidget *clist;
608         GtkWidget *state_label;
609         GtkWidget *from_label;
610         GtkWidget *subject_label;
611         GtkWidget *size_label;
612         GtkWidget *date_label;
613         GtkWidget *toolbar_hbox;
614         GtkWidget *toolbar;
615         GtkWidget *tmp_toolbar_icon;
616         GtkWidget *preview_btn;
617         GtkWidget *preview_popup;
618         GtkWidget *remove_btn;
619         GtkWidget *download_btn;
620         GtkWidget *done_btn;
621         gint n_menu_entries;
622
623         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
624         gtk_object_set_data (GTK_OBJECT (window), "window", window);
625         gtk_window_set_title (GTK_WINDOW (window), _("Selective download"));
626         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
627         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
628         gtk_window_set_policy (GTK_WINDOW (window), TRUE, TRUE, TRUE);
629
630         table = gtk_table_new (2, 2, FALSE);
631         gtk_container_add (GTK_CONTAINER (window), table);
632
633         msgs_label = gtk_label_new (_("0 Messages"));
634         gtk_table_attach (GTK_TABLE (table), msgs_label, 1, 2, 1, 2,
635                           (GtkAttachOptions) (GTK_FILL),
636                           (GtkAttachOptions) (0), 0, 0);
637         gtk_misc_set_alignment (GTK_MISC (msgs_label), 0, 0.5);
638
639         bottom_hbox = gtk_hbox_new (FALSE, 0);
640         gtk_table_attach (GTK_TABLE (table), bottom_hbox, 0, 1, 1, 2,
641                           (GtkAttachOptions) (GTK_FILL),
642                           (GtkAttachOptions) (GTK_FILL), 0, 0);
643         
644         show_old_chkbtn = gtk_check_button_new_with_label("Show only old Messages"); 
645         gtk_box_pack_start(GTK_BOX(bottom_hbox), show_old_chkbtn, FALSE, FALSE, 0); 
646         
647         gtk_signal_connect(GTK_OBJECT(show_old_chkbtn), "toggled", 
648                            GTK_SIGNAL_FUNC(sd_action_cb), GUINT_TO_POINTER(CHECKBTN)); 
649         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(show_old_chkbtn), FALSE);
650         GTK_WIDGET_UNSET_FLAGS(show_old_chkbtn, GTK_CAN_FOCUS);                                                 
651                            
652         expand_label = gtk_label_new (_(" "));
653         gtk_box_pack_start (GTK_BOX (bottom_hbox), expand_label, TRUE, TRUE, 0);
654         
655         fixed_label = gtk_label_new (_(" contains "));
656         gtk_box_pack_end (GTK_BOX (bottom_hbox), fixed_label, FALSE, FALSE, 0);
657
658         ac_menu = gtk_menu_new();
659         gtk_signal_connect(GTK_OBJECT(ac_menu), "selection_done",
660                            GTK_SIGNAL_FUNC(sd_ac_menu_popup_closed), NULL);
661         ac_button = gtk_button_new();
662         gtk_button_set_relief(GTK_BUTTON(ac_button), GTK_RELIEF_NONE);
663         GTK_WIDGET_UNSET_FLAGS(ac_button, GTK_CAN_FOCUS);
664         gtk_widget_set_usize(ac_button, -1, 1);
665         gtk_box_pack_start(GTK_BOX(bottom_hbox), ac_button, FALSE, FALSE, 0);
666         gtk_signal_connect(GTK_OBJECT(ac_button), "button_press_event",
667                            GTK_SIGNAL_FUNC(sd_ac_label_pressed), GTK_OBJECT(ac_menu));
668
669         ac_label = gtk_label_new("");
670         gtk_container_add(GTK_CONTAINER(ac_button), ac_label);
671
672         scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
673         gtk_table_attach (GTK_TABLE (table), scrolledwindow, 0, 1, 0, 1,
674                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
675                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);
676         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow), 
677                                         GTK_POLICY_NEVER, 
678                                         GTK_POLICY_AUTOMATIC);
679         
680         clist = gtk_clist_new (5);
681         gtk_container_add (GTK_CONTAINER (scrolledwindow), clist);
682         gtk_container_set_border_width (GTK_CONTAINER (clist), 5);
683         gtk_clist_set_column_width (GTK_CLIST (clist), 0, 20);
684         gtk_clist_set_column_width (GTK_CLIST (clist), 1, 150);
685         gtk_clist_set_column_width (GTK_CLIST (clist), 2, 150);
686         gtk_clist_set_column_width (GTK_CLIST (clist), 3, 100);
687         gtk_clist_set_column_width (GTK_CLIST (clist), 4, 30);
688         gtk_clist_set_selection_mode (GTK_CLIST (clist), GTK_SELECTION_BROWSE);
689         gtk_clist_column_titles_show (GTK_CLIST (clist));
690         
691         state_label = gtk_label_new (_("#"));
692         gtk_clist_set_column_widget (GTK_CLIST (clist), 0, state_label);
693         
694         from_label = gtk_label_new (_("From"));
695         gtk_clist_set_column_widget (GTK_CLIST (clist), 1, from_label);
696         
697         subject_label = gtk_label_new (_("Subject"));
698         gtk_clist_set_column_widget (GTK_CLIST (clist), 2, subject_label);
699         
700         date_label = gtk_label_new (_("Date"));
701         gtk_clist_set_column_widget (GTK_CLIST (clist), 3, date_label);
702
703         size_label = gtk_label_new (_("Size"));
704         gtk_widget_ref (size_label);
705         gtk_clist_set_column_widget (GTK_CLIST (clist), 4, size_label);
706         
707         toolbar_hbox = gtk_hbox_new (FALSE, 0);
708
709         gtk_table_attach (GTK_TABLE (table), toolbar_hbox, 1, 2, 0, 1,
710                           (GtkAttachOptions) (GTK_FILL),
711                           (GtkAttachOptions) (GTK_FILL), 0, 0);
712         
713         toolbar = gtk_toolbar_new (GTK_ORIENTATION_VERTICAL, GTK_TOOLBAR_BOTH);
714
715         gtk_box_pack_end (GTK_BOX (toolbar_hbox), toolbar, FALSE, FALSE, 0);
716         gtk_toolbar_set_space_size (GTK_TOOLBAR (toolbar), 30);
717         gtk_toolbar_set_space_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_SPACE_LINE);
718         gtk_container_set_border_width (GTK_CONTAINER (toolbar), 5);
719         
720         tmp_toolbar_icon = stock_pixmap_widget(toolbar_hbox, STOCK_PIXMAP_MAIL_RECEIVE);
721         preview_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
722                                                   GTK_TOOLBAR_CHILD_BUTTON,
723                                                   NULL,
724                                                   _("Preview Mail"),
725                                                   _("preview old/new E-Mail on account"), NULL,
726                                                   tmp_toolbar_icon, NULL, NULL);
727
728         n_menu_entries = sizeof(preview_popup_entries)/sizeof(preview_popup_entries[0]);
729         preview_popup = popupmenu_create(preview_btn, preview_popup_entries, n_menu_entries,
730                                       "<SelectiveDownload>", window);
731
732         gtk_signal_connect(GTK_OBJECT(preview_popup), "selection_done",
733                            GTK_SIGNAL_FUNC(sd_preview_popup_closed), NULL);
734
735         gtk_toolbar_append_space (GTK_TOOLBAR (toolbar));
736         
737         tmp_toolbar_icon = stock_pixmap_widget(toolbar_hbox, STOCK_PIXMAP_CLOSE);
738         remove_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
739                                                  GTK_TOOLBAR_CHILD_BUTTON,
740                                                  NULL,
741                                                  _("Remove"),
742                                                  _("remove selected E-Mails"), NULL,
743                                                  tmp_toolbar_icon, NULL, NULL);
744
745         gtk_widget_set_sensitive (remove_btn, FALSE);
746
747         tmp_toolbar_icon = stock_pixmap_widget(toolbar_hbox, STOCK_PIXMAP_DOWN_ARROW);
748         download_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
749                                                  GTK_TOOLBAR_CHILD_BUTTON,
750                                                  NULL,
751                                                  _("Download"),
752                                                  _("Download selected E-Mails"), NULL,
753                                                  tmp_toolbar_icon, NULL, NULL);
754
755         gtk_widget_set_sensitive (download_btn, FALSE);
756
757         gtk_toolbar_append_space (GTK_TOOLBAR (toolbar));
758
759         tmp_toolbar_icon = stock_pixmap_widget (toolbar_hbox, STOCK_PIXMAP_COMPLETE);
760         done_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
761                                                GTK_TOOLBAR_CHILD_BUTTON,
762                                                NULL,
763                                                _("Done"),
764                                                _("Exit Dialog"), NULL,
765                                                tmp_toolbar_icon, NULL, NULL);
766         
767         gtk_signal_connect (GTK_OBJECT (window), "delete_event",
768                             GTK_SIGNAL_FUNC (gtk_widget_hide_on_delete),
769                             NULL);
770         gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
771                             GTK_SIGNAL_FUNC(sd_key_pressed),
772                             NULL);
773         MANAGE_WINDOW_SIGNALS_CONNECT(window);
774         gtk_signal_connect (GTK_OBJECT (preview_btn), "button_press_event",
775                             GTK_SIGNAL_FUNC (sd_preview_popup_cb),
776                             NULL);
777
778         gtk_signal_connect (GTK_OBJECT (remove_btn), "clicked",
779                             GTK_SIGNAL_FUNC (sd_action_cb),
780                             GUINT_TO_POINTER(REMOVE));
781         gtk_signal_connect (GTK_OBJECT (download_btn), "clicked",
782                             GTK_SIGNAL_FUNC (sd_action_cb),
783                             GUINT_TO_POINTER(DOWNLOAD));
784         gtk_signal_connect (GTK_OBJECT (done_btn), "clicked",
785                             GTK_SIGNAL_FUNC (sd_action_cb),
786                             GUINT_TO_POINTER(DONE));
787         gtk_signal_connect (GTK_OBJECT (clist), "select_row",
788                             GTK_SIGNAL_FUNC (sd_select_row_cb),
789                             NULL);
790
791
792         selective.mainwin         = mainwin;
793         selective.window          = window;
794         selective.clist           = clist;
795         selective.preview_btn     = preview_btn;
796         selective.remove_btn      = remove_btn;
797         selective.download_btn    = download_btn;
798         selective.ac_label        = ac_label;
799         selective.ac_button       = ac_button;
800         selective.ac_menu         = ac_menu;
801         selective.preview_popup   = preview_popup;
802         selective.msgs_label      = msgs_label;
803         selective.show_old_chkbtn = show_old_chkbtn;
804
805         gtk_widget_show_all(window);
806 }
807