7824b2bb6a6bf238ea74dd1fabbcec6abf55f53d
[claws.git] / src / toolbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 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 /*
21  * General functions for accessing address book files.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <dirent.h>
34 #include <sys/stat.h>
35 #include <math.h>
36 #include <setjmp.h>
37
38 #include "intl.h"
39 #include "utils.h"
40 #include "xml.h"
41 #include "mgutils.h"
42 #include "prefs.h"
43 #include "codeconv.h"
44 #include "stock_pixmap.h"
45 #include "mainwindow.h"
46 #include "prefs_common.h"
47 #include "menu.h"
48 #include "prefs_actions.h"
49 #include "manage_window.h"
50
51 #include "toolbar.h"
52 #include "prefs_toolbar.h"
53
54 /* elements */
55 #define TOOLBAR_TAG_INDEX        "toolbar"
56 #define TOOLBAR_TAG_ITEM         "item"
57 #define TOOLBAR_TAG_SEPARATOR    SEPARATOR
58
59 jmp_buf    jumper;
60
61 GSList *toolbar_list;
62
63 MainWindow *mwin;
64
65 #define TOOLBAR_ICON_FILE   "file"    
66 #define TOOLBAR_ICON_TEXT   "text"     
67 #define TOOLBAR_ICON_ACTION "action"    
68
69 gboolean      toolbar_is_duplicate           (gint action);
70 static void   toolbar_parse_item             (XMLFile *file);
71 static gint   toolbar_ret_val_from_text      (gchar *text);
72
73
74 /* callback functions */
75 static void toolbar_inc_cb              (GtkWidget      *widget,
76                                          gpointer        data);
77
78 static void toolbar_inc_all_cb          (GtkWidget      *widget,
79                                          gpointer        data);
80
81 static void toolbar_send_cb             (GtkWidget      *widget,
82                                          gpointer        data);
83
84 static void toolbar_compose_cb          (GtkWidget      *widget,
85                                          gpointer        data);
86
87 static void toolbar_reply_cb            (GtkWidget      *widget,
88                                          gpointer        data);
89
90 static void toolbar_reply_to_all_cb     (GtkWidget      *widget,
91                                          gpointer        data);
92
93 static void toolbar_reply_to_sender_cb  (GtkWidget      *widget,
94                                          gpointer        data);
95
96 static void toolbar_forward_cb          (GtkWidget      *widget,
97                                          gpointer        data);
98
99 static void toolbar_delete_cb           (GtkWidget      *widget,
100                                          gpointer        data);
101
102 static void toolbar_exec_cb             (GtkWidget      *widget,
103                                          gpointer        data);
104
105 static void toolbar_next_unread_cb      (GtkWidget      *widget,
106                                          gpointer        data);
107
108 static void toolbar_actions_execute_cb  (GtkWidget      *widget,
109                                          gpointer        data);
110
111 static void toolbar_reply_popup_cb      (GtkWidget      *widget,
112                                          GdkEventButton *event,
113                                          gpointer        data);
114 static void toolbar_reply_popup_closed_cb(GtkMenuShell  *menu_shell,
115                                          gpointer        data);
116
117 static void toolbar_reply_to_all_popup_cb(GtkWidget     *widget,
118                                          GdkEventButton *event,
119                                          gpointer        data);
120
121 static void toolbar_reply_to_all_popup_closed_cb
122                                         (GtkMenuShell   *menu_shell,
123                                          gpointer        data);
124
125 static void toolbar_reply_to_sender_popup_cb(GtkWidget  *widget,
126                                          GdkEventButton *event,
127                                          gpointer        data);
128 static void toolbar_reply_to_sender_popup_closed_cb
129                                         (GtkMenuShell   *menu_shell,
130                                          gpointer        data);
131
132 static void toolbar_forward_popup_cb     (GtkWidget      *widget,
133                                          GdkEventButton    *event,
134                                          gpointer        data);
135
136 static void toolbar_forward_popup_closed_cb             
137                                         (GtkMenuShell   *menu_shell,
138                                          gpointer        data);
139
140 static ToolbarAction t_action[] = 
141 {
142         { "A_RECEIVE_ALL",   N_("Receive Mail on all Accounts"),    toolbar_inc_all_cb        },
143         { "A_RECEIVE_CUR",   N_("Receive Mail on current Account"), toolbar_inc_cb            },
144         { "A_SEND_QUEUED",   N_("Send Queued Message(s)"),          toolbar_send_cb           },
145         { "A_COMPOSE_EMAIL", N_("Compose Email"),                   toolbar_compose_cb        },
146         { "A_REPLY_MESSAGE", N_("Reply to Message"),                toolbar_reply_cb          },
147         { "A_REPLY_SENDER",  N_("Reply to Sender"),                 toolbar_reply_to_sender_cb},
148         { "A_REPLY_ALL",     N_("Reply to All"),                    toolbar_reply_to_all_cb   },
149         { "A_FORWARD",       N_("Forward Message"),                 toolbar_forward_cb        },
150         { "A_DELETE",        N_("Delete Message"),                  toolbar_delete_cb         },
151         { "A_EXECUTE",       N_("Execute"),                         toolbar_exec_cb           },
152         { "A_GOTO_NEXT",     N_("Goto Next Message"),               toolbar_next_unread_cb    },
153         { "A_SYL_ACTIONS",   N_("Sylpheed Actions Feature"),        toolbar_actions_execute_cb},
154
155         { "A_COMPOSE_NEWS",  N_("Compose News"),                    toolbar_compose_cb        },    
156         { "A_SEPARATOR",     SEPARATOR,                             NULL                      }
157 };
158
159 static GtkItemFactoryEntry reply_popup_entries[] =
160 {
161         {N_("/Reply with _quote"), NULL, reply_cb, COMPOSE_REPLY_WITH_QUOTE, NULL},
162         {N_("/_Reply without quote"), NULL, reply_cb, COMPOSE_REPLY_WITHOUT_QUOTE, NULL}
163 };
164 static GtkItemFactoryEntry replyall_popup_entries[] =
165 {
166         {N_("/Reply to all with _quote"), "<shift>A", reply_cb, COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL},
167         {N_("/_Reply to all without quote"), "a", reply_cb, COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL}
168 };
169 static GtkItemFactoryEntry replysender_popup_entries[] =
170 {
171         {N_("/Reply to sender with _quote"), NULL, reply_cb, COMPOSE_REPLY_TO_SENDER_WITH_QUOTE, NULL},
172         {N_("/_Reply to sender without quote"), NULL, reply_cb, COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE, NULL}
173 };
174 static GtkItemFactoryEntry fwd_popup_entries[] =
175 {
176         {N_("/_Forward message (inline style)"), "f", reply_cb, COMPOSE_FORWARD_INLINE, NULL},
177         {N_("/Forward message as _attachment"), "<shift>F", reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL}
178 };
179
180
181 void toolbar_actions_cb(GtkWidget *widget, ToolbarItem *toolbar_item)
182 {
183         if (toolbar_item->action < sizeof(t_action) / sizeof(t_action[0]))
184                 t_action[toolbar_item->action].func(widget, mwin);
185 }
186
187 gint toolbar_ret_val_from_descr(gchar *descr)
188 {
189         gint i;
190         
191         for (i = 0; i < sizeof(t_action) / sizeof(t_action[0]); i++) {
192                 if (g_strcasecmp(t_action[i].descr, descr) == 0)
193                                 return i;
194         }
195         
196         return -1;
197 }
198
199 gchar *toolbar_ret_descr_from_val(gint val)
200 {
201         g_return_val_if_fail(val >=0 && val <= sizeof(t_action) / sizeof(t_action[0]), NULL);
202
203         return t_action[val].descr;
204
205 }
206
207 static gint toolbar_ret_val_from_text(gchar *text)
208 {
209         gint i;
210         
211         for (i = 0; i < sizeof(t_action) / sizeof(t_action[0]); i++) {
212                 if (g_strcasecmp(t_action[i].action_text, text) == 0)
213                                 return i;
214         }
215         
216         return -1;
217 }
218
219 gchar *toolbar_ret_text_from_val(gint val)
220 {
221         g_return_val_if_fail(val >=0 && val <= sizeof(t_action) / sizeof(t_action[0]), NULL);
222
223         return t_action[val].action_text;
224 }
225
226 gboolean toolbar_is_duplicate(gint action)
227 {
228         GSList *cur;
229
230         if ((action == A_SEPARATOR) || (action == A_SYL_ACTIONS)) 
231                 return FALSE;
232
233         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
234                 ToolbarItem *item = (ToolbarItem*) cur->data;
235                 
236                 if (item->action == action)
237                         return TRUE;
238         }
239         return FALSE;
240 }
241
242 GList *toolbar_get_action_items(void)
243 {
244         GList *items = NULL;
245         gint i;
246         
247         for (i = 0; i < N_ACTION_VAL; i++) {
248                 items = g_list_append(items, t_action[i].descr);
249         }
250
251         return items;
252 }
253
254 static void toolbar_parse_item(XMLFile *file)
255 {
256         GList *attr;
257         gchar *name, *value;
258         ToolbarItem *item = NULL;
259
260         attr = xml_get_current_tag_attr(file);
261         item = g_new0(ToolbarItem, 1);
262         while( attr ) {
263                 name = ((XMLAttr *)attr->data)->name;
264                 value = ((XMLAttr *)attr->data)->value;
265                 
266                 if (g_strcasecmp(name, TOOLBAR_ICON_FILE) == 0) 
267                         item->file = g_strdup (value);
268                 else if (g_strcasecmp(name, TOOLBAR_ICON_TEXT) == 0)
269                         item->text = g_strdup (value);
270                 else if (g_strcasecmp(name, TOOLBAR_ICON_ACTION) == 0)
271                         item->action = toolbar_ret_val_from_text(value);
272
273                 attr = g_list_next(attr);
274         }
275         if (item->action != -1) {
276                 
277                 if ( !toolbar_is_duplicate(item->action)) 
278                         toolbar_list = g_slist_append(toolbar_list, item);
279         }
280 }
281
282 void toolbar_set_default_toolbar(void)
283 {
284         gint i;
285         /* create default toolbar */
286         struct _default_toolbar {
287                 gint action;
288                 gint icon;
289                 gchar *text;
290         } default_toolbar[] = {
291                 { A_RECEIVE_CUR,   STOCK_PIXMAP_MAIL_RECEIVE,         _("Get")     },
292                 { A_RECEIVE_ALL,   STOCK_PIXMAP_MAIL_RECEIVE_ALL,     _("Get All") },
293                 { A_SEPARATOR,     0,                                 ("")         }, 
294                 { A_SEND_QUEUED,   STOCK_PIXMAP_MAIL_SEND_QUEUE,      _("Send")    },
295                 { A_COMPOSE_EMAIL, STOCK_PIXMAP_MAIL_COMPOSE,         _("Email")   },
296                 { A_SEPARATOR,     0,                                 ("")         },
297                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
298                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
299                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
300                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
301                 { A_SEPARATOR,     0,                                 ("")         },
302                 { A_DELETE,        STOCK_PIXMAP_CLOSE,                _("Delete")  },
303                 { A_EXECUTE,       STOCK_PIXMAP_EXEC,                 _("Execute") },
304                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    },
305         };
306                 
307         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
308                 
309                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
310                 
311                 if (default_toolbar[i].action != A_SEPARATOR) {
312                         
313                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
314                         
315                         toolbar_item->file   = g_strdup(file);
316                         toolbar_item->action = default_toolbar[i].action;
317                         toolbar_item->text   = g_strdup(default_toolbar[i].text);
318                 } else {
319
320                         toolbar_item->file   = g_strdup(SEPARATOR);
321                         toolbar_item->action = A_SEPARATOR;
322                 }
323                 
324                 if (toolbar_item->action != -1) {
325                         
326                         if ( !toolbar_is_duplicate(toolbar_item->action)) 
327                                 toolbar_list = g_slist_append(toolbar_list, toolbar_item);
328                 }       
329         }
330 }
331
332 void toolbar_save_config_file()
333 {
334         GSList *cur;
335         FILE *fp;
336         PrefFile *pfile;
337         gchar *fileSpec = NULL;
338
339         debug_print("save Toolbar Configuration\n");
340
341         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, TOOLBAR_FILE, NULL );
342         pfile = prefs_write_open(fileSpec);
343         g_free( fileSpec );
344         if( pfile ) {
345                 fp = pfile->fp;
346                 fprintf(fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
347                         conv_get_current_charset_str());
348
349                 fprintf(fp, "<%s>\n", TOOLBAR_TAG_INDEX);
350
351                 for (cur = toolbar_list; cur != NULL; cur = cur->next) {
352                         ToolbarItem *toolbar_item = (ToolbarItem*) cur->data;
353                         
354                         if (g_strcasecmp(toolbar_item->file, SEPARATOR) != 0) 
355                                 fprintf(fp, "\t<%s %s=\"%s\" %s=\"%s\" %s=\"%s\"/>\n",
356                                         TOOLBAR_TAG_ITEM, 
357                                         TOOLBAR_ICON_FILE, toolbar_item->file,
358                                         TOOLBAR_ICON_TEXT, toolbar_item->text,
359                                         TOOLBAR_ICON_ACTION, 
360                                         toolbar_ret_text_from_val(toolbar_item->action));
361                         else 
362                                 fprintf(fp, "\t<%s/>\n", TOOLBAR_TAG_SEPARATOR); 
363                 }
364
365                 fprintf(fp, "</%s>\n", TOOLBAR_TAG_INDEX);      
366         
367                 if (prefs_write_close (pfile) < 0 ) 
368                         g_warning("failed to write toolbar configuration to file\n");
369         } else
370                 g_warning("failed to open toolbar configuration file for writing\n");
371 }
372
373 void toolbar_clear_list()
374 {
375         while (toolbar_list != NULL) {
376                 ToolbarItem *item = (ToolbarItem*) toolbar_list->data;
377                 
378                 toolbar_list = g_slist_remove(toolbar_list, item);
379                 if (item->file)
380                         g_free(item->file);
381                 if (item->text)
382                         g_free(item->text);
383                 g_free(item);   
384         }
385         g_slist_free(toolbar_list);
386 }
387
388 void toolbar_read_config_file(void)
389 {
390         XMLFile *file   = NULL;
391         gchar *fileSpec = NULL;
392         GList *attr;
393         gboolean retVal;
394
395         debug_print("read Toolbar Configuration\n");
396
397         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, TOOLBAR_FILE, NULL );
398         file = xml_open_file(fileSpec);
399         g_free(fileSpec);
400
401         toolbar_clear_list();
402
403         if (file) {
404                 if ((setjmp(jumper))
405                 || (xml_get_dtd(file))
406                 || (xml_parse_next_tag(file))
407                 || (!xml_compare_tag(file, TOOLBAR_TAG_INDEX))) {
408                         xml_close_file(file);
409                         return;
410                 }
411
412                 attr = xml_get_current_tag_attr(file);
413                 
414                 retVal = TRUE;
415                 for (;;) {
416                         if (!file->level) 
417                                 break;
418                         /* Get item tag */
419                         if (xml_parse_next_tag(file)) 
420                                 longjmp(jumper, 1);
421
422                         /* Get next tag (icon, icon_text or icon_action) */
423                         if (xml_compare_tag(file, TOOLBAR_TAG_ITEM)) {
424                                 toolbar_parse_item(file);
425                         } else if (xml_compare_tag(file, TOOLBAR_TAG_SEPARATOR)) {
426                                 ToolbarItem *item = g_new0(ToolbarItem, 1);
427                         
428                                 item->file   = g_strdup(SEPARATOR);
429                                 item->action = A_SEPARATOR;
430                                 toolbar_list = g_slist_append(toolbar_list, item);
431                         }
432
433                 }
434                 xml_close_file(file);
435         }
436         else {
437                 /* save default toolbar */
438                 toolbar_set_default_toolbar();
439                 toolbar_save_config_file();
440         }
441 }
442
443 static void toolbar_actions_execute_cb(GtkWidget *widget,
444                                        gpointer  data)
445 {
446         gint i = 0;
447         GSList *cur, *lop;
448         MainWindow *mainwin = (MainWindow*)data;
449         gchar *action, *action_p;
450         gboolean found = FALSE;
451
452         for (cur = mainwin->toolbar->syl_action; cur != NULL;  cur = cur->next) {
453                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
454
455                 if (widget == act->widget) {
456                         
457                         for (lop = prefs_common.actions_list; lop != NULL; lop = lop->next) {
458                                 action = g_strdup((gchar*)lop->data);
459
460                                 action_p = strstr(action, ": ");
461                                 action_p[0] = 0x00;
462                                 if (g_strcasecmp(act->name, action) == 0) {
463                                         found = TRUE;
464                                         g_free(action);
465                                         break;
466                                 } else 
467                                         i++;
468                                 g_free(action);
469                         }
470                         if (found) 
471                                 break;
472                 }
473         }
474
475         if (found)
476                 actions_execute(mwin, i, widget);
477         else
478                 g_warning ("Error: did not find Sylpheed Action to execute");
479 }
480
481 static void toolbar_inc_cb               (GtkWidget     *widget,
482                                           gpointer       data)
483 {
484         MainWindow *mainwin = (MainWindow *)data;
485
486         inc_mail_cb(mainwin, 0, NULL);
487 }
488
489 static void toolbar_inc_all_cb  (GtkWidget      *widget,
490                                  gpointer        data)
491 {
492         MainWindow *mainwin = (MainWindow *)data;
493
494         inc_all_account_mail_cb(mainwin, 0, NULL);
495 }
496
497 static void toolbar_send_cb     (GtkWidget      *widget,
498                                  gpointer        data)
499 {
500         MainWindow *mainwin = (MainWindow *)data;
501
502         send_queue_cb(mainwin, 0, NULL);
503 }
504
505 static void toolbar_compose_cb  (GtkWidget      *widget,
506                                  gpointer        data)
507 {
508         MainWindow *mainwin = (MainWindow *)data;
509
510         if (mainwin->toolbar->compose_btn_type == COMPOSEBUTTON_NEWS) 
511                 compose_news_cb(mainwin, 0, NULL);
512         else
513                 compose_mail_cb(mainwin, 0, NULL);
514 }
515
516 static void toolbar_reply_cb(GtkWidget *widget, gpointer data)
517 {
518         MainWindow *mainwin = (MainWindow *)data;
519
520         reply_cb(mainwin, 
521                  prefs_common.reply_with_quote ? COMPOSE_REPLY_WITH_QUOTE 
522                  : COMPOSE_REPLY_WITHOUT_QUOTE,
523                  NULL);
524 }
525
526 static void toolbar_reply_to_all_cb(GtkWidget *widget, gpointer data)
527 {
528         MainWindow *mainwin = (MainWindow *)data;
529
530         reply_cb(mainwin, 
531                  prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_ALL_WITH_QUOTE 
532                  : COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, 
533                  NULL);
534 }
535
536
537 static void toolbar_reply_to_sender_cb(GtkWidget *widget, gpointer data)
538 {
539         MainWindow *mainwin = (MainWindow *)data;
540
541         reply_cb(mainwin, 
542                  prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_SENDER_WITH_QUOTE 
543                  : COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE, 
544                  NULL);
545 }
546
547 static void toolbar_forward_cb  (GtkWidget      *widget,
548                                  gpointer        data)
549 {
550         MainWindow *mainwin = (MainWindow *)data;
551
552         if (prefs_common.forward_as_attachment)
553                 reply_cb(mainwin, COMPOSE_FORWARD_AS_ATTACH, NULL);
554         else
555                 reply_cb(mainwin, COMPOSE_FORWARD, NULL);
556 }
557
558 static void toolbar_delete_cb   (GtkWidget      *widget,
559                                  gpointer        data)
560 {
561         MainWindow *mainwin = (MainWindow *)data;
562
563         summary_delete(mainwin->summaryview);
564 }
565
566 static void toolbar_exec_cb     (GtkWidget      *widget,
567                                  gpointer        data)
568 {
569         MainWindow *mainwin = (MainWindow *)data;
570
571         summary_execute(mainwin->summaryview);
572 }
573
574 static void toolbar_next_unread_cb      (GtkWidget      *widget,
575                                          gpointer        data)
576 {
577         MainWindow *mainwin = (MainWindow *)data;
578
579         next_unread_cb(mainwin, 0, NULL);
580 }
581
582 /* popup callback functions */
583 static void toolbar_reply_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
584 {
585         MainWindow *mainwindow = (MainWindow *) data;
586         
587         if (!event) return;
588
589         if (event->button == 3) {
590                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
591                 gtk_menu_popup(GTK_MENU(mainwindow->toolbar->reply_popup), NULL, NULL,
592                        menu_button_position, widget,
593                        event->button, event->time);
594         }
595 }
596
597 static void toolbar_reply_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
598 {
599         MainWindow *mainwin = (MainWindow *)data;
600
601         gtk_button_set_relief(GTK_BUTTON(mainwin->toolbar->reply_btn), GTK_RELIEF_NONE);
602         manage_window_focus_in(mainwin->window, NULL, NULL);
603 }
604
605 static void toolbar_reply_to_all_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
606 {
607         MainWindow *mainwindow = (MainWindow *) data;
608         
609         if (!event) return;
610
611         if (event->button == 3) {
612                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
613                 gtk_menu_popup(GTK_MENU(mainwindow->toolbar->replyall_popup), NULL, NULL,
614                        menu_button_position, widget,
615                        event->button, event->time);
616         }
617 }
618
619 static void toolbar_reply_to_all_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
620 {
621         MainWindow *mainwin = (MainWindow *)data;
622
623         gtk_button_set_relief(GTK_BUTTON(mainwin->toolbar->replyall_btn), GTK_RELIEF_NONE);
624         manage_window_focus_in(mainwin->window, NULL, NULL);
625 }
626
627 static void toolbar_reply_to_sender_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
628 {
629         MainWindow *mainwindow = (MainWindow *) data;
630
631         if (!event) return;
632
633         if (event->button == 3) {
634                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
635                 gtk_menu_popup(GTK_MENU(mainwindow->toolbar->replysender_popup), NULL, NULL,
636                        menu_button_position, widget,
637                        event->button, event->time);
638         }
639 }
640
641 static void toolbar_reply_to_sender_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
642 {
643         MainWindow *mainwin = (MainWindow *)data;
644
645         gtk_button_set_relief(GTK_BUTTON(mainwin->toolbar->replysender_btn), GTK_RELIEF_NONE);
646         manage_window_focus_in(mainwin->window, NULL, NULL);
647 }
648
649 static void toolbar_forward_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
650 {
651         MainWindow *mainwindow = (MainWindow *) data;
652         
653         if (!event) return;
654
655         if (event->button == 3) {
656                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
657                 gtk_menu_popup(GTK_MENU(mainwindow->toolbar->fwd_popup), NULL, NULL,
658                        menu_button_position, widget,
659                        event->button, event->time);
660         }
661 }
662
663 static void toolbar_forward_popup_closed_cb (GtkMenuShell *menu_shell, 
664                                              gpointer     data)
665 {
666         MainWindow *mainwin = (MainWindow *)data;
667
668         gtk_button_set_relief(GTK_BUTTON(mainwin->toolbar->fwd_btn), GTK_RELIEF_NONE);
669         manage_window_focus_in(mainwin->window, NULL, NULL);
670 }
671
672 static void activate_compose_button (MainToolbar       *toolbar,
673                                      ToolbarStyle      style,
674                                      ComposeButtonType type)
675 {
676         if ((!toolbar->compose_mail_btn) || (!toolbar->compose_news_btn))
677                 return;
678
679         gtk_widget_hide(type == COMPOSEBUTTON_NEWS ? toolbar->compose_mail_btn 
680                         : toolbar->compose_news_btn);
681         gtk_widget_show(type == COMPOSEBUTTON_NEWS ? toolbar->compose_news_btn
682                         : toolbar->compose_mail_btn);
683         toolbar->compose_btn_type = type;       
684 }
685
686 void toolbar_set_compose_button (MainToolbar       *toolbar, 
687                                  ComposeButtonType compose_btn_type)
688 {
689         if (toolbar->compose_btn_type != compose_btn_type)
690                 activate_compose_button(toolbar, 
691                                         prefs_common.toolbar_style,
692                                         compose_btn_type);
693 }
694
695 void toolbar_set_sensitive(MainWindow *mainwin)
696 {
697         SensitiveCond state;
698         gboolean sensitive;
699         guint no_items = g_slist_length(toolbar_list);
700         MainToolbar *toolbar = mainwin->toolbar;
701         GSList *cur;
702         gint i = 0;
703         gint total = 0;
704
705         struct {
706                 GtkWidget *widget;
707                 SensitiveCond cond;
708                 gboolean empty;
709         } entry[no_items + 1];
710
711 #define SET_WIDGET_COND(w, c)     \
712 {                              \
713         entry[total].widget = w; \
714         entry[total].cond   = c; \
715         total++;                       \
716 }
717
718         SET_WIDGET_COND(toolbar->get_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
719         SET_WIDGET_COND(toolbar->getall_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
720         SET_WIDGET_COND(toolbar->compose_news_btn, M_HAVE_ACCOUNT);
721         SET_WIDGET_COND(toolbar->reply_btn,
722                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
723         SET_WIDGET_COND(toolbar->replyall_btn,
724                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
725         SET_WIDGET_COND(toolbar->replysender_btn,
726                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
727         SET_WIDGET_COND(toolbar->fwd_btn, M_HAVE_ACCOUNT|M_TARGET_EXIST);
728
729         SET_WIDGET_COND(toolbar->next_btn, M_MSG_EXIST);
730         SET_WIDGET_COND(toolbar->delete_btn,
731                         M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED);
732         SET_WIDGET_COND(toolbar->exec_btn, M_MSG_EXIST|M_EXEC|M_UNLOCKED);
733
734         for (cur = toolbar->syl_action; cur != NULL;  cur = cur->next) {
735                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
736                 
737                 SET_WIDGET_COND(act->widget, M_TARGET_EXIST|M_UNLOCKED);
738         }
739
740 #undef SET_WIDGET_COND
741
742         state = main_window_get_current_state(mainwin);
743
744         for (i = 0; i < total; i++) {
745                 
746                 if (entry[i].widget == NULL) continue;
747                 sensitive = ((entry[i].cond & state) == entry[i].cond);
748                 gtk_widget_set_sensitive(entry[i].widget, sensitive);
749         }
750
751         activate_compose_button(toolbar, 
752                         prefs_common.toolbar_style,
753                         toolbar->compose_btn_type);
754 }
755
756 void toolbar_popups_create(MainWindow *mainwin, GtkWidget *window)
757 {
758         guint n_menu_entries;
759         GtkWidget *reply_popup;
760         GtkWidget *replyall_popup;
761         GtkWidget *replysender_popup;
762         GtkWidget *fwd_popup;
763
764         if (mainwin->toolbar != NULL)
765                 g_free(mainwin->toolbar);
766
767         mainwin->toolbar = g_new0(MainToolbar, 1); 
768         
769         /* store mainwin localy */
770         mwin = mainwin;
771
772         n_menu_entries = sizeof(reply_popup_entries) /
773                         sizeof(reply_popup_entries[0]);
774         reply_popup = popupmenu_create(window, reply_popup_entries, n_menu_entries,
775                                       "<ReplyPopup>", mainwin);
776         gtk_signal_connect(GTK_OBJECT(reply_popup), "selection_done",
777                            GTK_SIGNAL_FUNC(toolbar_reply_popup_closed_cb), mainwin);
778         n_menu_entries = sizeof(replyall_popup_entries) /
779                         sizeof(replyall_popup_entries[0]);
780         replyall_popup = popupmenu_create(window, replyall_popup_entries, n_menu_entries,
781                                       "<ReplyAllPopup>", mainwin);
782         gtk_signal_connect(GTK_OBJECT(replyall_popup), "selection_done",
783                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_closed_cb), mainwin);
784         n_menu_entries = sizeof(replysender_popup_entries) /
785                         sizeof(replysender_popup_entries[0]);
786         replysender_popup = popupmenu_create(window, replysender_popup_entries, n_menu_entries,
787                                       "<ReplySenderPopup>", mainwin);
788         gtk_signal_connect(GTK_OBJECT(replysender_popup), "selection_done",
789                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_closed_cb), mainwin);
790         /* create the popup menu for the forward button */
791         n_menu_entries = sizeof(fwd_popup_entries) /
792                         sizeof(fwd_popup_entries[0]);
793         fwd_popup = popupmenu_create(window, fwd_popup_entries, n_menu_entries,
794                                       "<ForwardPopup>", mainwin);
795         gtk_signal_connect(GTK_OBJECT(fwd_popup), "selection_done",
796                            GTK_SIGNAL_FUNC(toolbar_forward_popup_closed_cb), mainwin);
797
798         mainwin->toolbar->reply_popup       = reply_popup;
799         mainwin->toolbar->replyall_popup    = replyall_popup;
800         mainwin->toolbar->replysender_popup = replysender_popup;
801         mainwin->toolbar->fwd_popup         = fwd_popup;
802 }
803
804 void toolbar_update(void)
805 {
806         gtk_container_remove(GTK_CONTAINER(mwin->handlebox), 
807                              GTK_WIDGET(mwin->toolbar->toolbar));
808         
809         mwin->toolbar->toolbar    = NULL;
810         mwin->toolbar->get_btn    = NULL;
811         mwin->toolbar->getall_btn = NULL;
812         mwin->toolbar->send_btn   = NULL;
813         mwin->toolbar->compose_mail_btn = NULL;
814         mwin->toolbar->compose_news_btn = NULL;
815         mwin->toolbar->reply_btn        = NULL; 
816         mwin->toolbar->replyall_btn     = NULL; 
817         mwin->toolbar->replysender_btn  = NULL; 
818         mwin->toolbar->fwd_btn    = NULL;       
819         mwin->toolbar->delete_btn = NULL;       
820         mwin->toolbar->next_btn   = NULL;       
821         mwin->toolbar->exec_btn   = NULL;
822
823         toolbar_destroy(mwin);
824         toolbar_create(mwin, mwin->handlebox);
825         toolbar_set_sensitive(mwin);
826
827 }
828
829 void toolbar_destroy(MainWindow *mainwin)
830 {
831         ToolbarSylpheedActions *syl_action;
832
833         toolbar_clear_list();
834         
835         while (mainwin->toolbar->syl_action != NULL) {
836                 syl_action = (ToolbarSylpheedActions*)mainwin->toolbar->syl_action->data;
837
838                 if (syl_action->name)
839                         g_free(syl_action->name);
840                 mainwin->toolbar->syl_action = g_slist_remove(mainwin->toolbar->syl_action, syl_action);
841         }
842
843         g_slist_free(mainwin->toolbar->syl_action);
844 }
845
846 void toolbar_create(MainWindow *mainwin,
847                     GtkWidget *container)
848 {
849         ToolbarItem *toolbar_item;
850
851         GtkWidget *toolbar;
852         GtkWidget *icon_wid  = NULL;
853         GtkWidget *icon_news = NULL;
854         GtkWidget *item_news = NULL;
855         GtkWidget *item;
856         ToolbarSylpheedActions *syl_action;
857         GSList *cur;
858
859         toolbar_destroy(mainwin);
860         toolbar_read_config_file();
861
862         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
863                                   GTK_TOOLBAR_BOTH);
864         gtk_container_add(GTK_CONTAINER(container), toolbar);
865         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
866         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
867         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
868                                     GTK_TOOLBAR_SPACE_LINE);
869         
870         mainwin->toolbar->exec_btn = NULL;
871
872         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
873                 toolbar_item  = (ToolbarItem*) cur->data;
874                 
875
876                 if (g_strcasecmp(toolbar_item->file, SEPARATOR) == 0) {
877                         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
878                         continue;
879                 }
880
881                 icon_wid = stock_pixmap_widget(container, stock_pixmap_get_icon(toolbar_item->file));
882                 item  = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
883                                                 toolbar_item->text,
884                                                 toolbar_ret_descr_from_val(toolbar_item->action),
885                                                 (""),
886                                                 icon_wid, toolbar_actions_cb, 
887                                                 toolbar_item);
888                 
889                 switch (toolbar_item->action) {
890                 case A_RECEIVE_ALL:
891                         mainwin->toolbar->getall_btn = item;
892                         break;
893                 case A_RECEIVE_CUR:
894                         mainwin->toolbar->get_btn = item;
895                         break;
896                 case A_SEND_QUEUED:
897                         mainwin->toolbar->send_btn = item; 
898                         break;
899                 case A_COMPOSE_EMAIL:
900                         icon_news = stock_pixmap_widget(container, STOCK_PIXMAP_NEWS_COMPOSE);
901                         item_news = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
902                                                             _("News"),
903                                                             toolbar_ret_descr_from_val(A_COMPOSE_NEWS),
904                                                             (""),
905                                                             icon_news, toolbar_actions_cb, 
906                                                             toolbar_item);
907                         mainwin->toolbar->compose_mail_btn = item; 
908                         mainwin->toolbar->compose_news_btn = item_news;
909                         break;
910                 case A_REPLY_MESSAGE:
911                         mainwin->toolbar->reply_btn = item;
912                         gtk_signal_connect(GTK_OBJECT(mainwin->toolbar->reply_btn), 
913                                            "button_press_event",
914                                            GTK_SIGNAL_FUNC(toolbar_reply_popup_cb),
915                                            mainwin);
916                         break;
917                 case A_REPLY_SENDER:
918                         mainwin->toolbar->replysender_btn = item;
919                         gtk_signal_connect(GTK_OBJECT(mainwin->toolbar->replysender_btn), 
920                                            "button_press_event",
921                                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_cb),
922                                            mainwin);
923                         break;
924                 case A_REPLY_ALL:
925                         mainwin->toolbar->replyall_btn = item;
926                         gtk_signal_connect(GTK_OBJECT(mainwin->toolbar->replyall_btn), 
927                                            "button_press_event",
928                                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_cb),
929                                            mainwin);
930                         break;
931                 case A_FORWARD:
932                         mainwin->toolbar->fwd_btn = item;
933                         gtk_signal_connect(GTK_OBJECT(mainwin->toolbar->fwd_btn), 
934                                            "button_press_event",
935                                            GTK_SIGNAL_FUNC(toolbar_forward_popup_cb),
936                                            mainwin);
937                         break;
938                 case A_DELETE:
939                         mainwin->toolbar->delete_btn = item;
940                         break;
941                 case A_EXECUTE:
942                         mainwin->toolbar->exec_btn = item;
943                         break;
944                 case A_GOTO_NEXT:
945                         mainwin->toolbar->next_btn = item;
946                         break;
947                 case A_SYL_ACTIONS:
948                         syl_action = g_new0(ToolbarSylpheedActions, 1);
949                         syl_action->widget = item;
950                         syl_action->name   = g_strdup(toolbar_item->text);
951
952                         mainwin->toolbar->syl_action = g_slist_append(mainwin->toolbar->syl_action,
953                                                                       syl_action);
954                         gtk_widget_show(item);
955                         break;
956                 default:
957                         break;
958                 }
959         }
960
961         /* we always create an exec button, if there isn't one yet */
962         if (!mainwin->toolbar->exec_btn) {
963                 toolbar_item = g_new0(ToolbarItem, 1);
964                 toolbar_item->action = A_EXECUTE;
965                 toolbar_item->file   = stock_pixmap_get_name(STOCK_PIXMAP_EXEC);
966                 toolbar_item->text   = toolbar_ret_text_from_val(A_EXECUTE);
967
968                 icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_EXEC);
969                 item = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
970                                                _("Execute"),
971                                                toolbar_ret_descr_from_val(A_EXECUTE),
972                                                (""),
973                                                icon_wid, toolbar_actions_cb,
974                                                toolbar_item);
975                 mainwin->toolbar->exec_btn = item;
976                 g_free(toolbar_item);
977         }
978         
979         mainwin->toolbar->toolbar = toolbar;
980         
981         activate_compose_button(mainwin->toolbar, 
982                                 prefs_common.toolbar_style, 
983                                 mainwin->toolbar->compose_btn_type);
984
985         gtk_widget_show_all(toolbar);
986 }