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