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