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