*** empty log message ***
[claws.git] / src / toolbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2003 Hiroyuki Yamamoto and the Sylpheed-Claws team
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 "mainwindow.h"
40 #include "summaryview.h"
41 #include "compose.h"
42 #include "utils.h"
43 #include "xml.h"
44 #include "mgutils.h"
45 #include "prefs_gtk.h"
46 #include "codeconv.h"
47 #include "stock_pixmap.h"
48 #include "manage_window.h"
49 #include "gtkutils.h"
50 #include "toolbar.h"
51 #include "menu.h"
52 #include "inc.h"
53 #include "action.h"
54 #include "prefs_actions.h"
55 #include "prefs_common.h"
56 #include "prefs_toolbar.h"
57 #include "alertpanel.h"
58
59 /* elements */
60 #define TOOLBAR_TAG_INDEX        "toolbar"
61 #define TOOLBAR_TAG_ITEM         "item"
62 #define TOOLBAR_TAG_SEPARATOR    "separator"
63
64 #define TOOLBAR_ICON_FILE   "file"    
65 #define TOOLBAR_ICON_TEXT   "text"     
66 #define TOOLBAR_ICON_ACTION "action"    
67
68 gboolean      toolbar_is_duplicate              (gint           action,
69                                                  ToolbarType    source);
70 static void   toolbar_parse_item                (XMLFile        *file,
71                                                  ToolbarType    source);
72
73 static gint   toolbar_ret_val_from_text         (const gchar    *text);
74 static gchar *toolbar_ret_text_from_val         (gint           val);
75
76 static void   toolbar_set_default_main          (void);
77 static void   toolbar_set_default_compose       (void);
78 static void   toolbar_set_default_msgview       (void);
79
80 static void     toolbar_style                   (ToolbarType     type, 
81                                                  guint           action, 
82                                                  gpointer        data);
83
84 static GtkWidget *get_window_widget             (ToolbarType     type, 
85                                                  gpointer        data);
86 static MainWindow *get_mainwin                  (gpointer data);
87 static void activate_compose_button             (Toolbar        *toolbar,
88                                                  ToolbarStyle    style,
89                                                  ComposeButtonType type);
90
91 /* toolbar callbacks */
92 static void toolbar_reply                       (gpointer        data, 
93                                                  guint           action);
94 static void toolbar_delete_cb                   (GtkWidget      *widget,
95                                                  gpointer        data);
96
97 static void toolbar_compose_cb                  (GtkWidget      *widget,
98                                                  gpointer        data);
99
100 static void toolbar_reply_cb                    (GtkWidget      *widget,
101                                                  gpointer        data);
102
103 static void toolbar_reply_to_all_cb             (GtkWidget      *widget,
104                                                  gpointer        data);
105
106 static void toolbar_reply_to_list_cb            (GtkWidget      *widget,
107                                                  gpointer        data);
108
109 static void toolbar_reply_to_sender_cb          (GtkWidget      *widget,
110                                                  gpointer        data);
111
112 static void toolbar_forward_cb                  (GtkWidget      *widget,
113                                                  gpointer        data);
114
115 static void toolbar_next_unread_cb              (GtkWidget      *widget,
116                                                  gpointer        data);
117
118 static void toolbar_ignore_thread_cb            (GtkWidget      *widget,
119                                                  gpointer        data);
120
121 static void toolbar_print_cb                    (GtkWidget      *widget,
122                                                  gpointer        data);
123
124 static void toolbar_actions_execute_cb          (GtkWidget      *widget,
125                                                  gpointer        data);
126
127
128 static void toolbar_send_cb                     (GtkWidget      *widget,
129                                                  gpointer        data);
130 static void toolbar_send_later_cb               (GtkWidget      *widget,
131                                                  gpointer        data);
132 static void toolbar_draft_cb                    (GtkWidget      *widget,
133                                                  gpointer        data);
134 static void toolbar_insert_cb                   (GtkWidget      *widget,
135                                                  gpointer        data);
136 static void toolbar_attach_cb                   (GtkWidget      *widget,
137                                                  gpointer        data);
138 static void toolbar_sig_cb                      (GtkWidget      *widget,
139                                                  gpointer        data);
140 static void toolbar_ext_editor_cb               (GtkWidget      *widget,
141                                                  gpointer        data);
142 static void toolbar_linewrap_cb                 (GtkWidget      *widget,
143                                                  gpointer        data);
144 static void toolbar_addrbook_cb                 (GtkWidget      *widget, 
145                                                  gpointer        data);
146
147 static void toolbar_popup_cb                    (gpointer        data, 
148                                                  guint           action, 
149                                                  GtkWidget      *widget);
150 struct {
151         gchar *index_str;
152         const gchar *descr;
153 } toolbar_text [] = {
154         { "A_RECEIVE_ALL",   N_("Receive Mail on all Accounts")         },
155         { "A_RECEIVE_CUR",   N_("Receive Mail on current Account")      },
156         { "A_SEND_QUEUED",   N_("Send Queued Message(s)")               },
157         { "A_COMPOSE_EMAIL", N_("Compose Email")                        },
158         { "A_COMPOSE_NEWS",  N_("Compose News")                         },
159         { "A_REPLY_MESSAGE", N_("Reply to Message")                     },
160         { "A_REPLY_SENDER",  N_("Reply to Sender")                      },
161         { "A_REPLY_ALL",     N_("Reply to All")                         },
162         { "A_REPLY_ML",      N_("Reply to Mailing-list")                },
163         { "A_FORWARD",       N_("Forward Message")                      }, 
164         { "A_DELETE",        N_("Delete Message")                       },
165         { "A_EXECUTE",       N_("Execute")                              },
166         { "A_GOTO_NEXT",     N_("Goto Next Message")                    },
167         { "A_IGNORE_THREAD", N_("Ignore thread")                        },
168         { "A_PRINT",         N_("Print")                                },
169
170         { "A_SEND",          N_("Send Message")                         },
171         { "A_SENDL",         N_("Put into queue folder and send later") },
172         { "A_DRAFT",         N_("Save to draft folder")                 },
173         { "A_INSERT",        N_("Insert file")                          },   
174         { "A_ATTACH",        N_("Attach file")                          },
175         { "A_SIG",           N_("Insert signature")                     },
176         { "A_EXTEDITOR",     N_("Edit with external editor")            },
177         { "A_LINEWRAP",      N_("Wrap all long lines")                  }, 
178         { "A_ADDRBOOK",      N_("Address book")                         },
179
180         { "A_SYL_ACTIONS",   N_("Sylpheed Actions Feature")             }, 
181         { "A_SEPARATOR",     "Separator"                                }
182 };
183
184 /* struct holds configuration files and a list of
185  * currently active toolbar items 
186  * TOOLBAR_MAIN, TOOLBAR_COMPOSE and TOOLBAR_MSGVIEW
187  * give us an index
188  */
189 struct {
190         const gchar  *conf_file;
191         GSList       *item_list;
192 } toolbar_config[3] = {
193         { "toolbar_main.xml",    NULL},
194         { "toolbar_compose.xml", NULL}, 
195         { "toolbar_msgview.xml", NULL}
196 };
197
198 static GtkItemFactoryEntry reply_popup_entries[] =
199 {
200         {N_("/Reply with _quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_WITH_QUOTE, NULL},
201         {N_("/_Reply without quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_WITHOUT_QUOTE, NULL}
202 };
203 static GtkItemFactoryEntry replyall_popup_entries[] =
204 {
205         {N_("/Reply to all with _quote"), "<shift>A", toolbar_popup_cb, COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL},
206         {N_("/_Reply to all without quote"), "a", toolbar_popup_cb, COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL}
207 };
208 static GtkItemFactoryEntry replylist_popup_entries[] =
209 {
210         {N_("/Reply to list with _quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_TO_LIST_WITH_QUOTE, NULL},
211         {N_("/_Reply to list without quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE, NULL}
212 };
213 static GtkItemFactoryEntry replysender_popup_entries[] =
214 {
215         {N_("/Reply to sender with _quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_TO_SENDER_WITH_QUOTE, NULL},
216         {N_("/_Reply to sender without quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE, NULL}
217 };
218 static GtkItemFactoryEntry fwd_popup_entries[] =
219 {
220         {N_("/_Forward message (inline style)"), "f", toolbar_popup_cb, COMPOSE_FORWARD_INLINE, NULL},
221         {N_("/Forward message as _attachment"), "<shift>F", toolbar_popup_cb, COMPOSE_FORWARD_AS_ATTACH, NULL}
222 };
223
224
225 gint toolbar_ret_val_from_descr(const gchar *descr)
226 {
227         gint i;
228
229         for (i = 0; i < N_ACTION_VAL; i++) {
230                 if (g_strcasecmp(gettext(toolbar_text[i].descr), descr) == 0)
231                                 return i;
232         }
233         
234         return -1;
235 }
236
237 gchar *toolbar_ret_descr_from_val(gint val)
238 {
239         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
240
241         return gettext(toolbar_text[val].descr);
242 }
243
244 static gint toolbar_ret_val_from_text(const gchar *text)
245 {
246         gint i;
247         
248         for (i = 0; i < N_ACTION_VAL; i++) {
249                 if (g_strcasecmp(toolbar_text[i].index_str, text) == 0)
250                                 return i;
251         }
252         
253         return -1;
254 }
255
256 static gchar *toolbar_ret_text_from_val(gint val)
257 {
258         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
259
260         return toolbar_text[val].index_str;
261 }
262
263 gboolean toolbar_is_duplicate(gint action, ToolbarType source)
264 {
265         GSList *cur;
266
267         if ((action == A_SEPARATOR) || (action == A_SYL_ACTIONS)) 
268                 return FALSE;
269
270         for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
271                 ToolbarItem *item = (ToolbarItem*) cur->data;
272                 
273                 if (item->index == action)
274                         return TRUE;
275         }
276         return FALSE;
277 }
278
279 /* depending on toolbar type this function 
280    returns a list of available toolbar events being 
281    displayed by prefs_toolbar
282 */
283 GList *toolbar_get_action_items(ToolbarType source)
284 {
285         GList *items = NULL;
286         gint i = 0;
287         
288         if (source == TOOLBAR_MAIN) {
289                 gint main_items[]   = { A_RECEIVE_ALL,   A_RECEIVE_CUR,   A_SEND_QUEUED,
290                                         A_COMPOSE_EMAIL, A_REPLY_MESSAGE, A_REPLY_SENDER, 
291                                         A_REPLY_ALL,     A_REPLY_ML,      A_FORWARD, 
292                                         A_DELETE,        A_EXECUTE,       A_GOTO_NEXT, 
293                                         A_IGNORE_THREAD, A_PRINT,  
294                                         A_ADDRBOOK,      A_SYL_ACTIONS };
295
296                 for (i = 0; i < sizeof main_items / sizeof main_items[0]; i++)  {
297                         items = g_list_append(items, gettext(toolbar_text[main_items[i]].descr));
298                         if (main_items[i] == A_PRINT) {
299                                 g_print("$$$ descr %s, trans %s\n",
300                                         toolbar_text[main_items[i]].descr,
301                                         gettext(toolbar_text[main_items[i]].descr));
302                         }
303                 }       
304         }
305         else if (source == TOOLBAR_COMPOSE) {
306                 gint comp_items[] =   { A_SEND,          A_SENDL,        A_DRAFT,
307                                         A_INSERT,        A_ATTACH,       A_SIG,
308                                         A_EXTEDITOR,     A_LINEWRAP,     A_ADDRBOOK,
309                                         A_SYL_ACTIONS };        
310
311                 for (i = 0; i < sizeof comp_items / sizeof comp_items[0]; i++) 
312                         items = g_list_append(items, gettext(toolbar_text[comp_items[i]].descr));
313         }
314         else if (source == TOOLBAR_MSGVIEW) {
315                 gint msgv_items[] =   { A_COMPOSE_EMAIL, A_REPLY_MESSAGE, A_REPLY_SENDER,
316                                         A_REPLY_ALL,     A_REPLY_ML,      A_FORWARD,
317                                         A_DELETE,        A_GOTO_NEXT,     A_ADDRBOOK,
318                                         A_SYL_ACTIONS };        
319
320                 for (i = 0; i < sizeof msgv_items / sizeof msgv_items[0]; i++) 
321                         items = g_list_append(items, gettext(toolbar_text[msgv_items[i]].descr));
322         }
323
324         return items;
325 }
326
327 static void toolbar_parse_item(XMLFile *file, ToolbarType source)
328 {
329         GList *attr;
330         gchar *name, *value;
331         ToolbarItem *item = NULL;
332
333         attr = xml_get_current_tag_attr(file);
334         item = g_new0(ToolbarItem, 1);
335         while( attr ) {
336                 name = ((XMLAttr *)attr->data)->name;
337                 value = ((XMLAttr *)attr->data)->value;
338                 
339                 if (g_strcasecmp(name, TOOLBAR_ICON_FILE) == 0) 
340                         item->file = g_strdup (value);
341                 else if (g_strcasecmp(name, TOOLBAR_ICON_TEXT) == 0)
342                         item->text = g_strdup (value);
343                 else if (g_strcasecmp(name, TOOLBAR_ICON_ACTION) == 0)
344                         item->index = toolbar_ret_val_from_text(value);
345
346                 attr = g_list_next(attr);
347         }
348         if (item->index != -1) {
349                 
350                 if (!toolbar_is_duplicate(item->index, source)) 
351                         toolbar_config[source].item_list = g_slist_append(toolbar_config[source].item_list,
352                                                                          item);
353         }
354 }
355
356 static void toolbar_set_default_main(void) 
357 {
358         struct {
359                 gint action;
360                 gint icon;
361                 gchar *text;
362         } default_toolbar[] = {
363                 { A_RECEIVE_CUR,   STOCK_PIXMAP_MAIL_RECEIVE,         _("Get")     },
364                 { A_RECEIVE_ALL,   STOCK_PIXMAP_MAIL_RECEIVE_ALL,     _("Get All") },
365                 { A_SEPARATOR,     0,                                 ("")         }, 
366                 { A_SEND_QUEUED,   STOCK_PIXMAP_MAIL_SEND_QUEUE,      _("Send")    },
367                 { A_COMPOSE_EMAIL, STOCK_PIXMAP_MAIL_COMPOSE,         _("Email")   },
368                 { A_SEPARATOR,     0,                                 ("")         },
369                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
370                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
371                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
372                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
373                 { A_SEPARATOR,     0,                                 ("")         },
374                 { A_DELETE,        STOCK_PIXMAP_CLOSE,                _("Delete")  },
375                 { A_EXECUTE,       STOCK_PIXMAP_EXEC,                 _("Execute") },
376                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    }
377         };
378         
379         gint i;
380         
381         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
382                 
383                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
384                 
385                 if (default_toolbar[i].action != A_SEPARATOR) {
386                         
387                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
388                         
389                         toolbar_item->file  = g_strdup(file);
390                         toolbar_item->index = default_toolbar[i].action;
391                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
392                 } else {
393
394                         toolbar_item->file  = g_strdup(TOOLBAR_TAG_SEPARATOR);
395                         toolbar_item->index = A_SEPARATOR;
396                 }
397                 
398                 if (toolbar_item->index != -1) {
399                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_MAIN)) 
400                                 toolbar_config[TOOLBAR_MAIN].item_list = 
401                                         g_slist_append(toolbar_config[TOOLBAR_MAIN].item_list, toolbar_item);
402                 }       
403         }
404 }
405
406 static void toolbar_set_default_compose(void)
407 {
408         struct {
409                 gint action;
410                 gint icon;
411                 gchar *text;
412         } default_toolbar[] = {
413                 { A_SEND,      STOCK_PIXMAP_MAIL_SEND,         _("Send")       },
414                 { A_SENDL,     STOCK_PIXMAP_MAIL_SEND_QUEUE,   _("Send later") },
415                 { A_DRAFT,     STOCK_PIXMAP_MAIL,              _("Draft")      },
416                 { A_SEPARATOR, 0,                               ("")           }, 
417                 { A_INSERT,    STOCK_PIXMAP_INSERT_FILE,       _("Insert")     },
418                 { A_ATTACH,    STOCK_PIXMAP_MAIL_ATTACH,       _("Attach")     },
419                 { A_SIG,       STOCK_PIXMAP_MAIL_SIGN,         _("Signature")  },
420                 { A_SEPARATOR, 0,                               ("")           },
421                 { A_EXTEDITOR, STOCK_PIXMAP_EDIT_EXTERN,       _("Editor")     },
422                 { A_LINEWRAP,  STOCK_PIXMAP_LINEWRAP,          _("Linewrap")   },
423                 { A_SEPARATOR, 0,                               ("")           },
424                 { A_ADDRBOOK,  STOCK_PIXMAP_ADDRESS_BOOK,      _("Address")    }
425         };
426         
427         gint i;
428
429         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
430                 
431                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
432                 
433                 if (default_toolbar[i].action != A_SEPARATOR) {
434                         
435                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
436                         
437                         toolbar_item->file  = g_strdup(file);
438                         toolbar_item->index = default_toolbar[i].action;
439                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
440                 } else {
441
442                         toolbar_item->file  = g_strdup(TOOLBAR_TAG_SEPARATOR);
443                         toolbar_item->index = A_SEPARATOR;
444                 }
445                 
446                 if (toolbar_item->index != -1) {
447                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_COMPOSE)) 
448                                 toolbar_config[TOOLBAR_COMPOSE].item_list = 
449                                         g_slist_append(toolbar_config[TOOLBAR_COMPOSE].item_list, toolbar_item);
450                 }       
451         }
452 }
453
454 static void toolbar_set_default_msgview(void)
455 {
456         struct {
457                 gint action;
458                 gint icon;
459                 gchar *text;
460         } default_toolbar[] = {
461                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
462                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
463                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
464                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
465                 { A_SEPARATOR,     0,                                 ("")         },
466                 { A_DELETE,        STOCK_PIXMAP_CLOSE,                _("Delete")  },
467                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    }
468         };
469         
470         gint i;
471
472         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
473                 
474                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
475                 
476                 if (default_toolbar[i].action != A_SEPARATOR) {
477                         
478                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
479                         
480                         toolbar_item->file  = g_strdup(file);
481                         toolbar_item->index = default_toolbar[i].action;
482                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
483                 } else {
484
485                         toolbar_item->file  = g_strdup(TOOLBAR_TAG_SEPARATOR);
486                         toolbar_item->index = A_SEPARATOR;
487                 }
488                 
489                 if (toolbar_item->index != -1) {
490                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_MSGVIEW)) 
491                                 toolbar_config[TOOLBAR_MSGVIEW].item_list = 
492                                         g_slist_append(toolbar_config[TOOLBAR_MSGVIEW].item_list, toolbar_item);
493                 }       
494         }
495 }
496
497 void toolbar_set_default(ToolbarType source)
498 {
499         if (source == TOOLBAR_MAIN)
500                 toolbar_set_default_main();
501         else if  (source == TOOLBAR_COMPOSE)
502                 toolbar_set_default_compose();
503         else if  (source == TOOLBAR_MSGVIEW)
504                 toolbar_set_default_msgview();
505 }
506
507 void toolbar_save_config_file(ToolbarType source)
508 {
509         GSList *cur;
510         FILE *fp;
511         PrefFile *pfile;
512         gchar *fileSpec = NULL;
513
514         debug_print("save Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
515
516         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
517         pfile = prefs_write_open(fileSpec);
518         g_free( fileSpec );
519         if( pfile ) {
520                 fp = pfile->fp;
521                 fprintf(fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
522                         conv_get_current_charset_str());
523
524                 fprintf(fp, "<%s>\n", TOOLBAR_TAG_INDEX);
525
526                 for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
527                         ToolbarItem *toolbar_item = (ToolbarItem*) cur->data;
528                         
529                         if (toolbar_item->index != A_SEPARATOR) 
530                                 fprintf(fp, "\t<%s %s=\"%s\" %s=\"%s\" %s=\"%s\"/>\n",
531                                         TOOLBAR_TAG_ITEM, 
532                                         TOOLBAR_ICON_FILE, toolbar_item->file,
533                                         TOOLBAR_ICON_TEXT, toolbar_item->text,
534                                         TOOLBAR_ICON_ACTION, 
535                                         toolbar_ret_text_from_val(toolbar_item->index));
536                         else 
537                                 fprintf(fp, "\t<%s/>\n", TOOLBAR_TAG_SEPARATOR); 
538                 }
539
540                 fprintf(fp, "</%s>\n", TOOLBAR_TAG_INDEX);      
541         
542                 if (prefs_file_close (pfile) < 0 ) 
543                         g_warning("failed to write toolbar configuration to file\n");
544         } else
545                 g_warning("failed to open toolbar configuration file for writing\n");
546 }
547
548 void toolbar_read_config_file(ToolbarType source)
549 {
550         XMLFile *file   = NULL;
551         gchar *fileSpec = NULL;
552         GList *attr;
553         gboolean retVal;
554         jmp_buf    jumper;
555
556         debug_print("read Toolbar Configuration from %s\n", toolbar_config[source].conf_file);
557
558         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
559         file = xml_open_file(fileSpec);
560         g_free(fileSpec);
561
562         toolbar_clear_list(source);
563
564         if (file) {
565                 if ((setjmp(jumper))
566                 || (xml_get_dtd(file))
567                 || (xml_parse_next_tag(file))
568                 || (!xml_compare_tag(file, TOOLBAR_TAG_INDEX))) {
569                         xml_close_file(file);
570                         return;
571                 }
572
573                 attr = xml_get_current_tag_attr(file);
574                 
575                 retVal = TRUE;
576                 for (;;) {
577                         if (!file->level) 
578                                 break;
579                         /* Get item tag */
580                         if (xml_parse_next_tag(file)) 
581                                 longjmp(jumper, 1);
582
583                         /* Get next tag (icon, icon_text or icon_action) */
584                         if (xml_compare_tag(file, TOOLBAR_TAG_ITEM)) {
585                                 toolbar_parse_item(file, source);
586                         } else if (xml_compare_tag(file, TOOLBAR_TAG_SEPARATOR)) {
587                                 ToolbarItem *item = g_new0(ToolbarItem, 1);
588                         
589                                 item->file   = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
590                                 item->index  = A_SEPARATOR;
591                                 toolbar_config[source].item_list = 
592                                         g_slist_append(toolbar_config[source].item_list, item);
593                         }
594
595                 }
596                 xml_close_file(file);
597         }
598
599         if ((!file) || (g_slist_length(toolbar_config[source].item_list) == 0)) {
600
601                 if (source == TOOLBAR_MAIN) 
602                         toolbar_set_default(TOOLBAR_MAIN);
603                 else if (source == TOOLBAR_COMPOSE) 
604                         toolbar_set_default(TOOLBAR_COMPOSE);
605                 else if (source == TOOLBAR_MSGVIEW) 
606                         toolbar_set_default(TOOLBAR_MSGVIEW);
607                 else {          
608                         g_warning("failed to write Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
609                         return;
610                 }
611
612                 toolbar_save_config_file(source);
613         }
614 }
615
616 /*
617  * clears list of toolbar items read from configuration files
618  */
619 void toolbar_clear_list(ToolbarType source)
620 {
621         while (toolbar_config[source].item_list != NULL) {
622                 ToolbarItem *item = (ToolbarItem*) toolbar_config[source].item_list->data;
623                 
624                 toolbar_config[source].item_list = 
625                         g_slist_remove(toolbar_config[source].item_list, item);
626
627                 if (item->file)
628                         g_free(item->file);
629                 if (item->text)
630                         g_free(item->text);
631                 g_free(item);   
632         }
633         g_slist_free(toolbar_config[source].item_list);
634 }
635
636
637 /* 
638  * return list of Toolbar items
639  */
640 GSList *toolbar_get_list(ToolbarType source)
641 {
642         GSList *list = NULL;
643
644         if ((source == TOOLBAR_MAIN) || (source == TOOLBAR_COMPOSE) || (source == TOOLBAR_MSGVIEW))
645                 list = toolbar_config[source].item_list;
646
647         return list;
648 }
649
650 void toolbar_set_list_item(ToolbarItem *t_item, ToolbarType source)
651 {
652         ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
653
654         toolbar_item->file  = g_strdup(t_item->file);
655         toolbar_item->text  = g_strdup(t_item->text);
656         toolbar_item->index = t_item->index;
657         
658         toolbar_config[source].item_list = 
659                 g_slist_append(toolbar_config[source].item_list,
660                                toolbar_item);
661 }
662
663 void toolbar_action_execute(GtkWidget    *widget,
664                             GSList       *action_list, 
665                             gpointer     data,
666                             gint         source) 
667 {
668         GSList *cur, *lop;
669         gchar *action, *action_p;
670         gboolean found = FALSE;
671         gint i = 0;
672
673         for (cur = action_list; cur != NULL;  cur = cur->next) {
674                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
675
676                 if (widget == act->widget) {
677                         
678                         for (lop = prefs_common.actions_list; lop != NULL; lop = lop->next) {
679                                 action = g_strdup((gchar*)lop->data);
680
681                                 action_p = strstr(action, ": ");
682                                 action_p[0] = 0x00;
683                                 if (g_strcasecmp(act->name, action) == 0) {
684                                         found = TRUE;
685                                         g_free(action);
686                                         break;
687                                 } else 
688                                         i++;
689                                 g_free(action);
690                         }
691                         if (found) 
692                                 break;
693                 }
694         }
695
696         if (found) 
697                 actions_execute(data, i, widget, source);
698         else
699                 g_warning ("Error: did not find Sylpheed Action to execute");
700 }
701
702 static void activate_compose_button (Toolbar           *toolbar,
703                                      ToolbarStyle      style,
704                                      ComposeButtonType type)
705 {
706         if ((!toolbar->compose_mail_btn) || (!toolbar->compose_news_btn))
707                 return;
708
709         gtk_widget_hide(type == COMPOSEBUTTON_NEWS ? toolbar->compose_mail_btn 
710                         : toolbar->compose_news_btn);
711         gtk_widget_show(type == COMPOSEBUTTON_NEWS ? toolbar->compose_news_btn
712                         : toolbar->compose_mail_btn);
713         toolbar->compose_btn_type = type;       
714 }
715
716 void toolbar_set_compose_button(Toolbar            *toolbar, 
717                                 ComposeButtonType  compose_btn_type)
718 {
719         if (toolbar->compose_btn_type != compose_btn_type)
720                 activate_compose_button(toolbar, 
721                                         prefs_common.toolbar_style,
722                                         compose_btn_type);
723 }
724
725 void toolbar_toggle(guint action, gpointer data)
726 {
727         MainWindow *mainwin = (MainWindow*)data;
728         GList *list;
729         GList *cur;
730
731         g_return_if_fail(mainwin != NULL);
732
733         toolbar_style(TOOLBAR_MAIN, action, mainwin);
734
735         list = compose_get_compose_list();
736         for (cur = list; cur != NULL; cur = cur->next) {
737                 toolbar_style(TOOLBAR_COMPOSE, action, cur->data);
738         }
739         list = messageview_get_msgview_list();
740         for (cur = list; cur != NULL; cur = cur->next) {
741                 toolbar_style(TOOLBAR_MSGVIEW, action, cur->data);
742         }
743         
744 }
745
746 void toolbar_set_style(GtkWidget *toolbar_wid, GtkWidget *handlebox_wid, guint action)
747 {
748         switch ((ToolbarStyle)action) {
749         case TOOLBAR_NONE:
750                 gtk_widget_hide(handlebox_wid);
751                 break;
752         case TOOLBAR_ICON:
753                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
754                                       GTK_TOOLBAR_ICONS);
755                 break;
756         case TOOLBAR_TEXT:
757                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
758                                       GTK_TOOLBAR_TEXT);
759                 break;
760         case TOOLBAR_BOTH:
761                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
762                                       GTK_TOOLBAR_BOTH);
763                 break;
764         default:
765                 return;
766         }
767
768         prefs_common.toolbar_style = (ToolbarStyle)action;
769         if (prefs_common.toolbar_style != TOOLBAR_NONE) {
770                 gtk_widget_show(handlebox_wid);
771                 gtk_widget_queue_resize(handlebox_wid);
772         }
773 }
774 /*
775  * Change the style of toolbar
776  */
777 static void toolbar_style(ToolbarType type, guint action, gpointer data)
778 {
779         GtkWidget  *handlebox_wid;
780         GtkWidget  *toolbar_wid;
781         MainWindow *mainwin = (MainWindow*)data;
782         Compose    *compose = (Compose*)data;
783         MessageView *msgview = (MessageView*)data;
784         
785         g_return_if_fail(data != NULL);
786         
787         switch (type) {
788         case TOOLBAR_MAIN:
789                 handlebox_wid = mainwin->handlebox;
790                 toolbar_wid = mainwin->toolbar->toolbar;
791                 break;
792         case TOOLBAR_COMPOSE:
793                 handlebox_wid = compose->handlebox;
794                 toolbar_wid = compose->toolbar->toolbar;
795                 break;
796         case TOOLBAR_MSGVIEW: 
797                 handlebox_wid = msgview->handlebox;
798                 toolbar_wid = msgview->toolbar->toolbar;
799                 break;
800         default:
801
802                 return;
803         }
804         toolbar_set_style(toolbar_wid, handlebox_wid, action);
805 }
806
807 /* Toolbar handling */
808 static void toolbar_inc_cb(GtkWidget    *widget,
809                            gpointer      data)
810 {
811         ToolbarItem *toolbar_item = (ToolbarItem*)data;
812         MainWindow *mainwin;
813
814         g_return_if_fail(toolbar_item != NULL);
815
816         switch (toolbar_item->type) {
817         case TOOLBAR_MAIN:
818                 mainwin = (MainWindow*)toolbar_item->parent;    
819                 inc_mail_cb(mainwin, 0, NULL);
820                 break;
821         default:
822                 break;
823         }
824 }
825
826 static void toolbar_inc_all_cb(GtkWidget        *widget,
827                                gpointer          data)
828 {
829         ToolbarItem *toolbar_item = (ToolbarItem*)data;
830         MainWindow *mainwin;
831
832         g_return_if_fail(toolbar_item != NULL);
833
834         switch (toolbar_item->type) {
835         case TOOLBAR_MAIN:
836                 mainwin = (MainWindow*)toolbar_item->parent;
837                 inc_all_account_mail_cb(mainwin, 0, NULL);
838                 break;
839         default:
840                 break;
841         }
842 }
843
844 static void toolbar_send_queued_cb(GtkWidget *widget,gpointer data)
845 {
846         ToolbarItem *toolbar_item = (ToolbarItem*)data;
847         MainWindow *mainwin;
848
849         g_return_if_fail(toolbar_item != NULL);
850
851         switch (toolbar_item->type) {
852         case TOOLBAR_MAIN:
853                 mainwin = (MainWindow*)toolbar_item->parent;
854                 send_queue_cb(mainwin, 0, NULL);
855                 break;
856         default:
857                 break;
858         }
859 }
860
861 static void toolbar_exec_cb(GtkWidget   *widget,
862                             gpointer     data)
863 {
864         MainWindow *mainwin = get_mainwin(data);
865
866         g_return_if_fail(mainwin != NULL);
867         summary_execute(mainwin->summaryview);
868 }
869
870
871
872 /* popup callback functions */
873 static void toolbar_reply_popup_cb(GtkWidget       *widget, 
874                                    GdkEventButton  *event, 
875                                    gpointer         data)
876 {
877         Toolbar *toolbar_data = (Toolbar*)data;
878         
879         if (!event) return;
880         
881         if (event->button == 3) {
882                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
883                 gtk_menu_popup(GTK_MENU(toolbar_data->reply_popup), NULL, NULL,
884                        menu_button_position, widget,
885                        event->button, event->time);
886         }
887 }
888
889 static void toolbar_reply_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
890 {
891         ToolbarItem *toolbar_item = (ToolbarItem*)data;
892         GtkWidget *window;
893         GtkWidget *reply_btn;
894         MainWindow *mainwin;
895         MessageView *msgview;
896
897         g_return_if_fail(toolbar_item != NULL);
898
899         switch(toolbar_item->type) {
900         case TOOLBAR_MAIN:
901                 mainwin   = (MainWindow*)toolbar_item->parent;
902                 reply_btn = mainwin->toolbar->reply_btn;
903                 window    = mainwin->window;
904                 break;
905         case TOOLBAR_MSGVIEW:
906                 msgview   = (MessageView*)toolbar_item->parent;
907                 reply_btn = msgview->toolbar->reply_btn;
908                 window    = msgview->window;
909                 break;
910         default:
911                 return;
912         }
913
914         gtk_button_set_relief(GTK_BUTTON(reply_btn), GTK_RELIEF_NONE);
915         manage_window_focus_in(window, NULL, NULL);
916 }
917
918 static void toolbar_reply_to_all_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
919 {
920         Toolbar *toolbar_data = (Toolbar*)data;
921         
922         if (!event) return;
923         
924         if (event->button == 3) {
925                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
926                 gtk_menu_popup(GTK_MENU(toolbar_data->replyall_popup), NULL, NULL,
927                        menu_button_position, widget,
928                        event->button, event->time);
929         }
930 }
931
932 static void toolbar_reply_to_all_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
933 {
934         ToolbarItem *toolbar_item = (ToolbarItem*)data;
935         GtkWidget *window;
936         GtkWidget *replyall_btn;
937         MainWindow *mainwin;
938         MessageView *msgview;
939
940         g_return_if_fail(toolbar_item != NULL);
941
942         switch(toolbar_item->type) {
943         case TOOLBAR_MAIN:
944                 mainwin      = (MainWindow*)toolbar_item->parent; 
945                 replyall_btn = mainwin->toolbar->replyall_btn;
946                 window       = mainwin->window;
947                 break;
948         case TOOLBAR_MSGVIEW:
949                 msgview      = (MessageView*)toolbar_item->parent;
950                 replyall_btn = msgview->toolbar->replyall_btn;
951                 window       = msgview->window;
952                 break;
953         default:
954                 return;
955         }
956
957         gtk_button_set_relief(GTK_BUTTON(replyall_btn), GTK_RELIEF_NONE);
958         manage_window_focus_in(window, NULL, NULL);
959 }
960
961 static void toolbar_reply_to_list_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
962 {
963         Toolbar *toolbar_data = (Toolbar*)data;
964
965         if (event->button == 3) {
966                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
967                 gtk_menu_popup(GTK_MENU(toolbar_data->replylist_popup), NULL, NULL,
968                        menu_button_position, widget,
969                        event->button, event->time);
970         }
971 }
972
973 static void toolbar_reply_to_list_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
974 {
975         ToolbarItem *toolbar_item = (ToolbarItem*)data;
976         gpointer parent = toolbar_item->parent;
977         GtkWidget *window;
978         GtkWidget *replylist_btn;
979
980
981         g_return_if_fail(toolbar_item != NULL);
982
983         switch(toolbar_item->type) {
984         case TOOLBAR_MAIN:
985                 replylist_btn = ((MainWindow*)parent)->toolbar->replylist_btn;
986                 window        = ((MainWindow*)parent)->window;
987                 break;
988         case TOOLBAR_MSGVIEW:
989                 replylist_btn = ((MessageView*)parent)->toolbar->replylist_btn;
990                 window        = ((MessageView*)parent)->window;
991                 break;
992         default:
993                 return;
994         }
995
996         gtk_button_set_relief(GTK_BUTTON(replylist_btn), GTK_RELIEF_NONE);
997         manage_window_focus_in(window, NULL, NULL);
998 }
999
1000 static void toolbar_reply_to_sender_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
1001 {
1002         Toolbar *toolbar_data = (Toolbar*)data;
1003
1004         if (event->button == 3) {
1005                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
1006                 gtk_menu_popup(GTK_MENU(toolbar_data->replysender_popup), NULL, NULL,
1007                        menu_button_position, widget,
1008                        event->button, event->time);
1009         }
1010 }
1011
1012 static void toolbar_reply_to_sender_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
1013 {
1014         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1015         gpointer parent = toolbar_item->parent;
1016         GtkWidget *window;
1017         GtkWidget *replysender_btn;
1018
1019         g_return_if_fail(toolbar_item != NULL);
1020
1021         switch(toolbar_item->type) {
1022         case TOOLBAR_MAIN:
1023                 replysender_btn = ((MainWindow*)parent)->toolbar->replysender_btn;
1024                 window          = ((MainWindow*)parent)->window;
1025                 break;
1026         case TOOLBAR_MSGVIEW:
1027                 replysender_btn = ((MessageView*)parent)->toolbar->replysender_btn;
1028                 window          = ((MessageView*)parent)->window;
1029                 break;
1030         default:
1031                 return;
1032         }
1033
1034         gtk_button_set_relief(GTK_BUTTON(replysender_btn), GTK_RELIEF_NONE);
1035         manage_window_focus_in(window, NULL, NULL);
1036 }
1037
1038 static void toolbar_forward_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
1039 {
1040         Toolbar *toolbar_data = (Toolbar*)data;
1041
1042         if (event->button == 3) {
1043                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
1044                 gtk_menu_popup(GTK_MENU(toolbar_data->fwd_popup), NULL, NULL,
1045                                menu_button_position, widget,
1046                                event->button, event->time);
1047         }
1048 }
1049
1050 static void toolbar_forward_popup_closed_cb (GtkMenuShell *menu_shell, 
1051                                              gpointer     data)
1052 {
1053         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1054         gpointer parent = toolbar_item->parent;
1055         GtkWidget *window;
1056         GtkWidget *fwd_btn;
1057
1058         g_return_if_fail(toolbar_item != NULL);
1059
1060         switch(toolbar_item->type) {
1061         case TOOLBAR_MAIN:
1062                 fwd_btn = ((MainWindow*)parent)->toolbar->fwd_btn;
1063                 window  = ((MainWindow*)parent)->window;
1064                 break;
1065         case TOOLBAR_MSGVIEW:
1066                 fwd_btn = ((MessageView*)parent)->toolbar->fwd_btn;
1067                 window  = ((MessageView*)parent)->window;
1068                 break;
1069         default:
1070                 return;
1071         }
1072
1073         gtk_button_set_relief(GTK_BUTTON(fwd_btn), GTK_RELIEF_NONE);
1074         manage_window_focus_in(window, NULL, NULL);
1075 }
1076
1077 /*
1078  * Delete current/selected(s) message(s)
1079  */
1080 static void toolbar_delete_cb(GtkWidget *widget, gpointer data)
1081 {
1082         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1083         MainWindow *mainwin;
1084         MessageView *msgview;
1085
1086         g_return_if_fail(toolbar_item != NULL);
1087         
1088         switch (toolbar_item->type) {
1089         case TOOLBAR_MSGVIEW:
1090                 msgview = (MessageView*)toolbar_item->parent;
1091                 messageview_delete(msgview);
1092                 break;
1093         case TOOLBAR_MAIN:
1094                 mainwin = (MainWindow*)toolbar_item->parent;
1095                 summary_delete(mainwin->summaryview);
1096                 break;
1097         default: 
1098                 debug_print("toolbar event not supported\n");
1099                 break;
1100         }
1101 }
1102
1103
1104 /*
1105  * Compose new message
1106  */
1107 static void toolbar_compose_cb(GtkWidget *widget, gpointer data)
1108 {
1109         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1110         MainWindow *mainwin;
1111         MessageView *msgview;
1112
1113         g_return_if_fail(toolbar_item != NULL);
1114
1115         switch (toolbar_item->type) {
1116         case TOOLBAR_MAIN:
1117                 mainwin = (MainWindow*)toolbar_item->parent;
1118                 if (mainwin->toolbar->compose_btn_type == COMPOSEBUTTON_NEWS) 
1119                         compose_news_cb(mainwin, 0, NULL);
1120                 else
1121                         compose_mail_cb(mainwin, 0, NULL);
1122                 break;
1123         case TOOLBAR_MSGVIEW:
1124                 msgview = (MessageView*)toolbar_item->parent;
1125                 compose_new_with_folderitem(NULL, 
1126                                             msgview->msginfo->folder);
1127                 break;  
1128         default:
1129                 debug_print("toolbar event not supported\n");
1130         }
1131 }
1132
1133 static void toolbar_popup_cb(gpointer data, guint action, GtkWidget *widget)
1134 {
1135         toolbar_reply(data, action);
1136 }
1137
1138
1139 /*
1140  * Reply Message
1141  */
1142 static void toolbar_reply_cb(GtkWidget *widget, gpointer data)
1143 {
1144         toolbar_reply(data, prefs_common.reply_with_quote ? 
1145                       COMPOSE_REPLY_WITH_QUOTE : COMPOSE_REPLY_WITHOUT_QUOTE);
1146 }
1147
1148
1149 /*
1150  * Reply message to Sender and All recipients
1151  */
1152 static void toolbar_reply_to_all_cb(GtkWidget *widget, gpointer data)
1153 {
1154         toolbar_reply(data,
1155                       prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_ALL_WITH_QUOTE 
1156                       : COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE);
1157 }
1158
1159
1160 /*
1161  * Reply to Mailing List
1162  */
1163 static void toolbar_reply_to_list_cb(GtkWidget *widget, gpointer data)
1164 {
1165         toolbar_reply(data, 
1166                       prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_LIST_WITH_QUOTE 
1167                       : COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE);
1168 }
1169
1170
1171 /*
1172  * Reply to sender of message
1173  */ 
1174 static void toolbar_reply_to_sender_cb(GtkWidget *widget, gpointer data)
1175 {
1176         toolbar_reply(data, 
1177                       prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_SENDER_WITH_QUOTE 
1178                       : COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE);
1179 }
1180
1181 /*
1182  * Open addressbook
1183  */ 
1184 static void toolbar_addrbook_cb(GtkWidget *widget, gpointer data)
1185 {
1186         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1187         Compose *compose;
1188
1189         g_return_if_fail(toolbar_item != NULL);
1190
1191         switch (toolbar_item->type) {
1192         case TOOLBAR_MAIN:
1193         case TOOLBAR_MSGVIEW:
1194                 compose = NULL;
1195                 break;
1196         case TOOLBAR_COMPOSE:
1197                 compose = (Compose *)toolbar_item->parent;
1198                 break;
1199         default:
1200                 return;
1201         }
1202         addressbook_open(compose);
1203 }
1204
1205
1206 /*
1207  * Forward current/selected(s) message(s)
1208  */
1209 static void toolbar_forward_cb(GtkWidget *widget, gpointer data)
1210 {
1211         toolbar_reply(data, COMPOSE_FORWARD);
1212 }
1213
1214
1215 /*
1216  * Goto Next Unread Message
1217  */
1218 static void toolbar_next_unread_cb(GtkWidget *widget, gpointer data)
1219 {
1220         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1221         MainWindow *mainwin;
1222         MessageView *msgview;
1223
1224         g_return_if_fail(toolbar_item != NULL);
1225
1226         switch (toolbar_item->type) {
1227         case TOOLBAR_MAIN:
1228                 mainwin = (MainWindow*)toolbar_item->parent;
1229                 summary_select_next_unread(mainwin->summaryview);
1230                 break;
1231                 
1232         case TOOLBAR_MSGVIEW:
1233                 msgview = (MessageView*)toolbar_item->parent;
1234                 summary_select_next_unread(msgview->mainwin->summaryview);
1235                 
1236                 /* Now we need to update the messageview window */
1237                 if (msgview->mainwin->summaryview->selected) {
1238                         GtkCTree *ctree = GTK_CTREE(msgview->mainwin->summaryview->ctree);
1239                         
1240                         MsgInfo * msginfo = gtk_ctree_node_get_row_data(ctree, 
1241                                                                         msgview->mainwin->summaryview->selected);
1242                        
1243                         messageview_show(msgview, msginfo, 
1244                                          msgview->all_headers);
1245                 } else {
1246                         gtk_widget_destroy(msgview->window);
1247                 }
1248                 break;
1249         default:
1250                 debug_print("toolbar event not supported\n");
1251         }
1252 }
1253
1254 static void toolbar_ignore_thread_cb(GtkWidget *widget, gpointer data)
1255 {
1256         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1257         MainWindow *mainwin;
1258
1259         g_return_if_fail(toolbar_item != NULL);
1260
1261         switch (toolbar_item->type) {
1262         case TOOLBAR_MAIN:
1263                 mainwin = (MainWindow *) toolbar_item->parent;
1264                 summary_toggle_ignore_thread(mainwin->summaryview);
1265                 break;
1266         case TOOLBAR_MSGVIEW:
1267                 /* TODO: see toolbar_next_unread_cb() if you need
1268                  * this in the message view */
1269                 break;
1270         default:
1271                 debug_print("toolbar event not supported\n");
1272                 break;
1273         }
1274 }
1275
1276 static void toolbar_print_cb(GtkWidget *widget, gpointer data)
1277 {
1278         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1279         MainWindow *mainwin;
1280
1281         g_return_if_fail(toolbar_item != NULL);
1282
1283         switch (toolbar_item->type) {
1284         case TOOLBAR_MAIN:
1285                 mainwin = (MainWindow *) toolbar_item->parent;
1286                 summary_print(mainwin->summaryview);
1287                 break;
1288         case TOOLBAR_MSGVIEW:
1289                 /* TODO: see toolbar_next_unread_cb() if you need
1290                  * this in the message view */
1291                 break;
1292         default:
1293                 debug_print("toolbar event not supported\n");
1294                 break;
1295         }
1296 }
1297
1298 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
1299 {
1300         compose_toolbar_cb(A_SEND, data);
1301 }
1302
1303 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
1304 {
1305         compose_toolbar_cb(A_SENDL, data);
1306 }
1307
1308 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
1309 {
1310         compose_toolbar_cb(A_DRAFT, data);
1311 }
1312
1313 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
1314 {
1315         compose_toolbar_cb(A_INSERT, data);
1316 }
1317
1318 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
1319 {
1320         compose_toolbar_cb(A_ATTACH, data);
1321 }
1322
1323 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
1324 {
1325         compose_toolbar_cb(A_SIG, data);
1326 }
1327
1328 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
1329 {
1330         compose_toolbar_cb(A_EXTEDITOR, data);
1331 }
1332
1333 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
1334 {
1335         compose_toolbar_cb(A_LINEWRAP, data);
1336 }
1337
1338 /*
1339  * Execute actions from toolbar
1340  */
1341 static void toolbar_actions_execute_cb(GtkWidget *widget, gpointer data)
1342 {
1343         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1344         GSList *action_list;
1345         MainWindow *mainwin;
1346         Compose *compose;
1347         MessageView *msgview;
1348         gpointer parent = toolbar_item->parent;
1349
1350         g_return_if_fail(toolbar_item != NULL);
1351
1352         switch (toolbar_item->type) {
1353         case TOOLBAR_MAIN:
1354                 mainwin = (MainWindow*)parent;
1355                 action_list = mainwin->toolbar->action_list;
1356                 break;
1357         case TOOLBAR_COMPOSE:
1358                 compose = (Compose*)parent;
1359                 action_list = compose->toolbar->action_list;
1360                 break;
1361         case TOOLBAR_MSGVIEW:
1362                 msgview = (MessageView*)parent;
1363                 action_list = msgview->toolbar->action_list;
1364                 break;
1365         default:
1366                 debug_print("toolbar event not supported\n");
1367                 return;
1368         }
1369         toolbar_action_execute(widget, action_list, parent, toolbar_item->type);        
1370 }
1371
1372 static MainWindow *get_mainwin(gpointer data)
1373 {
1374         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1375         MainWindow *mainwin = NULL;
1376         MessageView *msgview;
1377
1378         g_return_val_if_fail(toolbar_item != NULL, NULL);
1379
1380         switch(toolbar_item->type) {
1381         case TOOLBAR_MAIN:
1382                 mainwin = (MainWindow*)toolbar_item->parent;
1383                 break;
1384         case TOOLBAR_MSGVIEW:
1385                 msgview = (MessageView*)toolbar_item->parent;
1386                 mainwin = (MainWindow*)msgview->mainwin;
1387                 break;
1388         default:
1389                 break;
1390         }
1391
1392         return mainwin;
1393 }
1394
1395 static GtkWidget *get_window_widget(ToolbarType type, gpointer data)
1396 {
1397         MainWindow *mainwin;
1398         MessageView *msgview;
1399
1400         switch (type) {
1401         case TOOLBAR_MAIN:
1402                 mainwin = (MainWindow*)data;
1403                 return mainwin->window;
1404                 break;
1405         case TOOLBAR_MSGVIEW:
1406                 msgview = (MessageView*)data;
1407                 return msgview->vbox;
1408                 break;
1409         default:
1410                 break;
1411         }
1412         return NULL;
1413 }
1414
1415 static void toolbar_buttons_cb(GtkWidget   *widget, 
1416                                ToolbarItem *item)
1417 {
1418         gint num_items;
1419         gint i;
1420         struct {
1421                 gint   index;
1422                 void (*func)(GtkWidget *widget, gpointer data);
1423         } callbacks[] = {
1424                 { A_RECEIVE_ALL,        toolbar_inc_all_cb              },
1425                 { A_RECEIVE_CUR,        toolbar_inc_cb                  },
1426                 { A_SEND_QUEUED,        toolbar_send_queued_cb          },
1427                 { A_COMPOSE_EMAIL,      toolbar_compose_cb              },
1428                 { A_COMPOSE_NEWS,       toolbar_compose_cb              },
1429                 { A_REPLY_MESSAGE,      toolbar_reply_cb                },
1430                 { A_REPLY_SENDER,       toolbar_reply_to_sender_cb      },
1431                 { A_REPLY_ALL,          toolbar_reply_to_all_cb         },
1432                 { A_REPLY_ML,           toolbar_reply_to_list_cb        },
1433                 { A_FORWARD,            toolbar_forward_cb              },
1434                 { A_DELETE,             toolbar_delete_cb               },
1435                 { A_EXECUTE,            toolbar_exec_cb                 },
1436                 { A_GOTO_NEXT,          toolbar_next_unread_cb          },
1437                 { A_IGNORE_THREAD,      toolbar_ignore_thread_cb        },
1438                 { A_PRINT,              toolbar_print_cb                },
1439
1440                 { A_SEND,               toolbar_send_cb                 },
1441                 { A_SENDL,              toolbar_send_later_cb           },
1442                 { A_DRAFT,              toolbar_draft_cb                },
1443                 { A_INSERT,             toolbar_insert_cb               },
1444                 { A_ATTACH,             toolbar_attach_cb               },
1445                 { A_SIG,                toolbar_sig_cb                  },
1446                 { A_EXTEDITOR,          toolbar_ext_editor_cb           },
1447                 { A_LINEWRAP,           toolbar_linewrap_cb             },
1448                 { A_ADDRBOOK,           toolbar_addrbook_cb             },
1449
1450                 { A_SYL_ACTIONS,        toolbar_actions_execute_cb      }
1451         };
1452
1453         num_items = sizeof(callbacks)/sizeof(callbacks[0]);
1454
1455         for (i = 0; i < num_items; i++) {
1456                 if (callbacks[i].index == item->index) {
1457                         callbacks[i].func(widget, item);
1458                         return;
1459                 }
1460         }
1461 }
1462
1463 /**
1464  * Create a new toolbar with specified type
1465  * if a callback list is passed it will be used before the 
1466  * common callback list
1467  **/
1468 Toolbar *toolbar_create(ToolbarType      type, 
1469                         GtkWidget       *container,
1470                         gpointer         data)
1471 {
1472         ToolbarItem *toolbar_item;
1473
1474         GtkWidget *toolbar;
1475         GtkWidget *icon_wid = NULL;
1476         GtkWidget *icon_news;
1477         GtkWidget *item;
1478         GtkWidget *item_news;
1479         GtkWidget *window_wid;
1480
1481         guint n_menu_entries;
1482         GtkWidget *reply_popup;
1483         GtkWidget *replyall_popup;
1484         GtkWidget *replylist_popup;
1485         GtkWidget *replysender_popup;
1486         GtkWidget *fwd_popup;
1487
1488         GtkTooltips *toolbar_tips;
1489         ToolbarSylpheedActions *action_item;
1490         GSList *cur;
1491         GSList *toolbar_list;
1492         Toolbar *toolbar_data;
1493
1494         
1495         toolbar_tips = gtk_tooltips_new();
1496         
1497         toolbar_read_config_file(type);
1498         toolbar_list = toolbar_get_list(type);
1499
1500         toolbar_data = g_new0(Toolbar, 1); 
1501
1502         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
1503                                   GTK_TOOLBAR_BOTH);
1504         gtk_container_add(GTK_CONTAINER(container), toolbar);
1505         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
1506         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
1507         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
1508                                     GTK_TOOLBAR_SPACE_LINE);
1509         
1510         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
1511
1512                 if (g_strcasecmp(((ToolbarItem*)cur->data)->file, TOOLBAR_TAG_SEPARATOR) == 0) {
1513                         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
1514                         continue;
1515                 }
1516                 
1517                 toolbar_item = g_new0(ToolbarItem, 1); 
1518                 toolbar_item->index = ((ToolbarItem*)cur->data)->index;
1519                 toolbar_item->file = g_strdup(((ToolbarItem*)cur->data)->file);
1520                 toolbar_item->text = g_strdup(((ToolbarItem*)cur->data)->text);
1521                 toolbar_item->parent = data;
1522                 toolbar_item->type = type;
1523
1524                 /* collect toolbar items in list to keep track */
1525                 toolbar_data->item_list = 
1526                         g_slist_append(toolbar_data->item_list, 
1527                                        toolbar_item);
1528
1529                 icon_wid = stock_pixmap_widget(container, stock_pixmap_get_icon(toolbar_item->file));
1530                 item  = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1531                                                 toolbar_item->text,
1532                                                 (""),
1533                                                 (""),
1534                                                 icon_wid, toolbar_buttons_cb, 
1535                                                 toolbar_item);
1536                 
1537                 switch (toolbar_item->index) {
1538
1539                 case A_RECEIVE_ALL:
1540                         toolbar_data->getall_btn = item;
1541                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1542                                              toolbar_data->getall_btn, 
1543                                            _("Receive Mail on all Accounts"), NULL);
1544                         break;
1545                 case A_RECEIVE_CUR:
1546                         toolbar_data->get_btn = item;
1547                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1548                                              toolbar_data->get_btn,
1549                                            _("Receive Mail on current Account"), NULL);
1550                         break;
1551                 case A_SEND_QUEUED:
1552                         toolbar_data->send_btn = item; 
1553                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1554                                              toolbar_data->send_btn,
1555                                            _("Send Queued Message(s)"), NULL);
1556                         break;
1557                 case A_COMPOSE_EMAIL:
1558                         icon_news = stock_pixmap_widget(container, STOCK_PIXMAP_NEWS_COMPOSE);
1559                         item_news = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1560                                                             _("News"),
1561                                                             (""),
1562                                                             (""),
1563                                                             icon_news, toolbar_buttons_cb, 
1564                                                             toolbar_item);
1565                         toolbar_data->compose_mail_btn = item; 
1566                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1567                                              toolbar_data->compose_mail_btn,
1568                                            _("Compose Email"), NULL);
1569                         toolbar_data->compose_news_btn = item_news;
1570                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1571                                              toolbar_data->compose_news_btn,
1572                                            _("Compose News"), NULL);
1573                         break;
1574                 case A_REPLY_MESSAGE:
1575                         toolbar_data->reply_btn = item;
1576                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1577                                              toolbar_data->reply_btn,
1578                                            _("Reply to Message"), NULL);
1579                         gtk_signal_connect(GTK_OBJECT(toolbar_data->reply_btn), 
1580                                            "button_press_event",
1581                                            GTK_SIGNAL_FUNC(toolbar_reply_popup_cb),
1582                                            toolbar_data);
1583                         n_menu_entries = sizeof(reply_popup_entries) /
1584                                 sizeof(reply_popup_entries[0]);
1585
1586                         window_wid = get_window_widget(type, data);
1587                         reply_popup = popupmenu_create(window_wid,
1588                                                        reply_popup_entries, n_menu_entries,
1589                                                        "<ReplyPopup>", (gpointer)toolbar_item);
1590
1591                         gtk_signal_connect(GTK_OBJECT(reply_popup), "selection_done",
1592                                            GTK_SIGNAL_FUNC(toolbar_reply_popup_closed_cb), toolbar_item);
1593                         toolbar_data->reply_popup = reply_popup;
1594                         break;
1595                 case A_REPLY_SENDER:
1596                         toolbar_data->replysender_btn = item;
1597                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1598                                              toolbar_data->replysender_btn,
1599                                            _("Reply to Sender"), NULL);
1600                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replysender_btn), 
1601                                            "button_press_event",
1602                                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_cb),
1603                                            toolbar_data);
1604                         n_menu_entries = sizeof(replysender_popup_entries) /
1605                                 sizeof(replysender_popup_entries[0]);
1606
1607                         window_wid = get_window_widget(type, data);
1608                         replysender_popup = popupmenu_create(window_wid, 
1609                                                              replysender_popup_entries, n_menu_entries,
1610                                                              "<ReplySenderPopup>", (gpointer)toolbar_item);
1611
1612                         gtk_signal_connect(GTK_OBJECT(replysender_popup), "selection_done",
1613                                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_closed_cb), toolbar_item);
1614                         toolbar_data->replysender_popup = replysender_popup;
1615                         break;
1616                 case A_REPLY_ALL:
1617                         toolbar_data->replyall_btn = item;
1618                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1619                                              toolbar_data->replyall_btn,
1620                                            _("Reply to All"), NULL);
1621                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replyall_btn), 
1622                                            "button_press_event",
1623                                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_cb),
1624                                            toolbar_data);
1625                         n_menu_entries = sizeof(replyall_popup_entries) /
1626                                 sizeof(replyall_popup_entries[0]);
1627
1628                         window_wid = get_window_widget(type, data);     
1629                         replyall_popup = popupmenu_create(window_wid, 
1630                                                           replyall_popup_entries, n_menu_entries,
1631                                                           "<ReplyAllPopup>", (gpointer)toolbar_item);
1632         
1633                         gtk_signal_connect(GTK_OBJECT(replyall_popup), "selection_done",
1634                                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_closed_cb), toolbar_item);
1635                         toolbar_data->replyall_popup = replyall_popup;
1636                         break;
1637                 case A_REPLY_ML:
1638                         toolbar_data->replylist_btn = item;
1639                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1640                                              toolbar_data->replylist_btn,
1641                                            _("Reply to Mailing-list"), NULL);
1642                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replylist_btn), 
1643                                            "button_press_event",
1644                                            GTK_SIGNAL_FUNC(toolbar_reply_to_list_popup_cb),
1645                                            toolbar_data);
1646                         n_menu_entries = sizeof(replylist_popup_entries) /
1647                                 sizeof(replylist_popup_entries[0]);
1648
1649                         window_wid = get_window_widget(type, data);
1650                         replylist_popup = popupmenu_create(window_wid, 
1651                                                            replylist_popup_entries, n_menu_entries,
1652                                                            "<ReplyMlPopup>", (gpointer)toolbar_item);
1653                 
1654                         gtk_signal_connect(GTK_OBJECT(replylist_popup), "selection_done",
1655                                            GTK_SIGNAL_FUNC(toolbar_reply_to_list_popup_closed_cb), toolbar_item);
1656                         toolbar_data->replylist_popup = replylist_popup;
1657                         break;
1658                 case A_FORWARD:
1659                         toolbar_data->fwd_btn = item;
1660                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1661                                              toolbar_data->fwd_btn,
1662                                              _("Forward Message"), NULL);
1663                         gtk_signal_connect(GTK_OBJECT(toolbar_data->fwd_btn), 
1664                                            "button_press_event",
1665                                            GTK_SIGNAL_FUNC(toolbar_forward_popup_cb),
1666                                            toolbar_data);
1667                         n_menu_entries = sizeof(fwd_popup_entries) /
1668                                 sizeof(fwd_popup_entries[0]);
1669
1670                         window_wid = get_window_widget(type, data);
1671                         fwd_popup = popupmenu_create(window_wid, 
1672                                                      fwd_popup_entries, n_menu_entries,
1673                                                      "<ForwardPopup>", (gpointer)toolbar_item);
1674
1675                         gtk_signal_connect(GTK_OBJECT(fwd_popup), "selection_done",
1676                                            GTK_SIGNAL_FUNC(toolbar_forward_popup_closed_cb), toolbar_item);
1677                         toolbar_data->fwd_popup = fwd_popup;
1678                         break;
1679                 case A_DELETE:
1680                         toolbar_data->delete_btn = item;
1681                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1682                                              toolbar_data->delete_btn,
1683                                              _("Delete Message"), NULL);
1684                         break;
1685                 case A_EXECUTE:
1686                         toolbar_data->exec_btn = item;
1687                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1688                                              toolbar_data->exec_btn,
1689                                            _("Execute"), NULL);
1690                         break;
1691                 case A_GOTO_NEXT:
1692                         toolbar_data->next_btn = item;
1693                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1694                                              toolbar_data->next_btn,
1695                                              _("Goto Next Message"), NULL);
1696                         break;
1697                 
1698                 /* Compose Toolbar */
1699                 case A_SEND:
1700                         toolbar_data->send_btn = item;
1701                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1702                                              toolbar_data->send_btn, 
1703                                              _("Send Message"), NULL);
1704                         break;
1705                 case A_SENDL:
1706                         toolbar_data->sendl_btn = item;
1707                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1708                                              toolbar_data->sendl_btn,
1709                                              _("Put into queue folder and send later"), NULL);
1710                         break;
1711                 case A_DRAFT:
1712                         toolbar_data->draft_btn = item; 
1713                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1714                                              toolbar_data->draft_btn,
1715                                              _("Save to draft folder"), NULL);
1716                         break;
1717                 case A_INSERT:
1718                         toolbar_data->insert_btn = item; 
1719                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1720                                              toolbar_data->insert_btn,
1721                                              _("Insert file"), NULL);
1722                         break;
1723                 case A_ATTACH:
1724                         toolbar_data->attach_btn = item;
1725                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1726                                              toolbar_data->attach_btn,
1727                                              _("Attach file"), NULL);
1728                         break;
1729                 case A_SIG:
1730                         toolbar_data->sig_btn = item;
1731                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1732                                              toolbar_data->sig_btn,
1733                                              _("Insert signature"), NULL);
1734                         break;
1735                 case A_EXTEDITOR:
1736                         toolbar_data->exteditor_btn = item;
1737                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1738                                              toolbar_data->exteditor_btn,
1739                                              _("Edit with external editor"), NULL);
1740                         break;
1741                 case A_LINEWRAP:
1742                         toolbar_data->linewrap_btn = item;
1743                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1744                                              toolbar_data->linewrap_btn,
1745                                              _("Wrap all long lines"), NULL);
1746                         break;
1747                 case A_ADDRBOOK:
1748                         toolbar_data->addrbook_btn = item;
1749                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1750                                              toolbar_data->addrbook_btn,
1751                                              _("Address book"), NULL);
1752                         break;
1753
1754                 case A_SYL_ACTIONS:
1755                         action_item = g_new0(ToolbarSylpheedActions, 1);
1756                         action_item->widget = item;
1757                         action_item->name   = g_strdup(toolbar_item->text);
1758
1759                         toolbar_data->action_list = 
1760                                 g_slist_append(toolbar_data->action_list,
1761                                                action_item);
1762
1763                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1764                                              item,
1765                                              action_item->name, NULL);
1766
1767                         gtk_widget_show(item);
1768                         break;
1769                 default:
1770                         /* find and set the tool tip text */
1771                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips),
1772                                              item,
1773                                              toolbar_ret_descr_from_val
1774                                                 (toolbar_item->index),
1775                                              NULL);
1776                         break;
1777                 }
1778
1779         }
1780         toolbar_data->toolbar = toolbar;
1781         if (type == TOOLBAR_MAIN)
1782                 activate_compose_button(toolbar_data, 
1783                                         prefs_common.toolbar_style, 
1784                                         toolbar_data->compose_btn_type);
1785         
1786         gtk_widget_show_all(toolbar);
1787         
1788         return toolbar_data; 
1789 }
1790
1791 /**
1792  * Free toolbar structures
1793  */ 
1794 void toolbar_destroy(Toolbar * toolbar) {
1795
1796         TOOLBAR_DESTROY_ITEMS(toolbar->item_list);      
1797         TOOLBAR_DESTROY_ACTIONS(toolbar->action_list);
1798 }
1799
1800 void toolbar_update(ToolbarType type, gpointer data)
1801 {
1802         Toolbar *toolbar_data;
1803         GtkWidget *handlebox;
1804         MainWindow *mainwin = (MainWindow*)data;
1805         Compose    *compose = (Compose*)data;
1806         MessageView *msgview = (MessageView*)data;
1807
1808         switch(type) {
1809         case TOOLBAR_MAIN:
1810                 toolbar_data = mainwin->toolbar;
1811                 handlebox    = mainwin->handlebox;
1812                 break;
1813         case TOOLBAR_COMPOSE:
1814                 toolbar_data = compose->toolbar;
1815                 handlebox    = compose->handlebox;
1816                 break;
1817         case TOOLBAR_MSGVIEW:
1818                 toolbar_data = msgview->toolbar;
1819                 handlebox    = msgview->handlebox;
1820                 break;
1821         default:
1822                 return;
1823         }
1824
1825         gtk_container_remove(GTK_CONTAINER(handlebox), 
1826                              GTK_WIDGET(toolbar_data->toolbar));
1827
1828         toolbar_init(toolbar_data);
1829         toolbar_data = toolbar_create(type, handlebox, data);
1830         switch(type) {
1831         case TOOLBAR_MAIN:
1832                 mainwin->toolbar = toolbar_data;
1833                 break;
1834         case TOOLBAR_COMPOSE:
1835                 compose->toolbar = toolbar_data;
1836                 break;
1837         case TOOLBAR_MSGVIEW:
1838                 msgview->toolbar = toolbar_data;
1839                 break;
1840         }
1841
1842         toolbar_style(type, prefs_common.toolbar_style, data);
1843
1844         if (type == TOOLBAR_MAIN)
1845                 toolbar_main_set_sensitive((MainWindow*)data);
1846 }
1847
1848 void toolbar_main_set_sensitive(gpointer data)
1849 {
1850         SensitiveCond state;
1851         gboolean sensitive;
1852         MainWindow *mainwin = (MainWindow*)data;
1853         Toolbar *toolbar = mainwin->toolbar;
1854         GSList *cur;
1855         GSList *entry_list = NULL;
1856         
1857         typedef struct _Entry Entry;
1858         struct _Entry {
1859                 GtkWidget *widget;
1860                 SensitiveCond cond;
1861                 gboolean empty;
1862         };
1863
1864 #define SET_WIDGET_COND(w, c)     \
1865 { \
1866         Entry *e = g_new0(Entry, 1); \
1867         e->widget = w; \
1868         e->cond   = c; \
1869         entry_list = g_slist_append(entry_list, e); \
1870 }
1871
1872         SET_WIDGET_COND(toolbar->get_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1873         SET_WIDGET_COND(toolbar->getall_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1874         SET_WIDGET_COND(toolbar->compose_news_btn, M_HAVE_ACCOUNT);
1875         SET_WIDGET_COND(toolbar->reply_btn,
1876                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1877         SET_WIDGET_COND(toolbar->replyall_btn,
1878                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1879         SET_WIDGET_COND(toolbar->replylist_btn,
1880                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1881         SET_WIDGET_COND(toolbar->replysender_btn,
1882                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1883         SET_WIDGET_COND(toolbar->fwd_btn, M_HAVE_ACCOUNT|M_TARGET_EXIST);
1884
1885         SET_WIDGET_COND(toolbar->next_btn, M_MSG_EXIST);
1886         SET_WIDGET_COND(toolbar->delete_btn,
1887                         M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED);
1888         SET_WIDGET_COND(toolbar->exec_btn, M_DELAY_EXEC);
1889
1890         for (cur = toolbar->action_list; cur != NULL;  cur = cur->next) {
1891                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
1892                 
1893                 SET_WIDGET_COND(act->widget, M_TARGET_EXIST|M_UNLOCKED);
1894         }
1895
1896 #undef SET_WIDGET_COND
1897
1898         state = main_window_get_current_state(mainwin);
1899
1900         for (cur = entry_list; cur != NULL; cur = cur->next) {
1901                 Entry *e = (Entry*) cur->data;
1902
1903                 if (e->widget != NULL) {
1904                         sensitive = ((e->cond & state) == e->cond);
1905                         gtk_widget_set_sensitive(e->widget, sensitive); 
1906                 }
1907         }
1908         
1909         while (entry_list != NULL) {
1910                 Entry *e = (Entry*) entry_list->data;
1911
1912                 if (e)
1913                         g_free(e);
1914                 entry_list = g_slist_remove(entry_list, e);
1915         }
1916
1917         g_slist_free(entry_list);
1918
1919         activate_compose_button(toolbar, 
1920                                 prefs_common.toolbar_style,
1921                                 toolbar->compose_btn_type);
1922 }
1923
1924 void toolbar_comp_set_sensitive(gpointer data, gboolean sensitive)
1925 {
1926         Compose *compose = (Compose*)data;
1927         GSList *items = compose->toolbar->action_list;
1928
1929         if (compose->toolbar->send_btn)
1930                 gtk_widget_set_sensitive(compose->toolbar->send_btn, sensitive);
1931         if (compose->toolbar->sendl_btn)
1932                 gtk_widget_set_sensitive(compose->toolbar->sendl_btn, sensitive);
1933         if (compose->toolbar->draft_btn )
1934                 gtk_widget_set_sensitive(compose->toolbar->draft_btn , sensitive);
1935         if (compose->toolbar->insert_btn )
1936                 gtk_widget_set_sensitive(compose->toolbar->insert_btn , sensitive);
1937         if (compose->toolbar->attach_btn)
1938                 gtk_widget_set_sensitive(compose->toolbar->attach_btn, sensitive);
1939         if (compose->toolbar->sig_btn)
1940                 gtk_widget_set_sensitive(compose->toolbar->sig_btn, sensitive);
1941         if (compose->toolbar->exteditor_btn)
1942                 gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, sensitive);
1943         if (compose->toolbar->linewrap_btn)
1944                 gtk_widget_set_sensitive(compose->toolbar->linewrap_btn, sensitive);
1945         if (compose->toolbar->addrbook_btn)
1946                 gtk_widget_set_sensitive(compose->toolbar->addrbook_btn, sensitive);
1947         for (; items != NULL; items = g_slist_next(items)) {
1948                 ToolbarSylpheedActions *item = (ToolbarSylpheedActions *)items->data;
1949                 gtk_widget_set_sensitive(item->widget, sensitive);
1950         }
1951 }
1952
1953 /**
1954  * Initialize toolbar structure
1955  **/
1956 void toolbar_init(Toolbar * toolbar) {
1957
1958         toolbar->toolbar          = NULL;
1959         toolbar->get_btn          = NULL;
1960         toolbar->getall_btn       = NULL;
1961         toolbar->send_btn         = NULL;
1962         toolbar->compose_mail_btn = NULL;
1963         toolbar->compose_news_btn = NULL;
1964         toolbar->reply_btn        = NULL;
1965         toolbar->replysender_btn  = NULL;
1966         toolbar->replyall_btn     = NULL;
1967         toolbar->replylist_btn    = NULL;
1968         toolbar->fwd_btn          = NULL;
1969         toolbar->delete_btn       = NULL;
1970         toolbar->next_btn         = NULL;
1971         toolbar->exec_btn         = NULL;
1972
1973         /* compose buttons */ 
1974         toolbar->sendl_btn        = NULL;
1975         toolbar->draft_btn        = NULL;
1976         toolbar->insert_btn       = NULL;
1977         toolbar->attach_btn       = NULL;
1978         toolbar->sig_btn          = NULL;       
1979         toolbar->exteditor_btn    = NULL;       
1980         toolbar->linewrap_btn     = NULL;       
1981         toolbar->addrbook_btn     = NULL;       
1982
1983         toolbar_destroy(toolbar);
1984 }
1985
1986 /*
1987  */
1988 static void toolbar_reply(gpointer data, guint action)
1989 {
1990         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1991         MainWindow *mainwin;
1992         MessageView *msgview;
1993         GSList *msginfo_list = NULL;
1994         gchar *body;
1995
1996         g_return_if_fail(toolbar_item != NULL);
1997
1998         switch (toolbar_item->type) {
1999         case TOOLBAR_MAIN:
2000                 mainwin = (MainWindow*)toolbar_item->parent;
2001                 msginfo_list = summary_get_selection(mainwin->summaryview);
2002                 msgview = (MessageView*)mainwin->messageview;
2003                 break;
2004         case TOOLBAR_MSGVIEW:
2005                 msgview = (MessageView*)toolbar_item->parent;
2006                 msginfo_list = g_slist_append(msginfo_list, msgview->msginfo);
2007                 break;
2008         default:
2009                 return;
2010         }
2011
2012         g_return_if_fail(msgview != NULL);
2013         body = messageview_get_selection(msgview);
2014
2015         g_return_if_fail(msginfo_list != NULL);
2016         compose_reply_mode((ComposeMode)action, msginfo_list, body);
2017
2018         g_free(body);
2019         g_slist_free(msginfo_list);
2020
2021         /* TODO: update reply state ion summaryview */
2022 }
2023
2024
2025 /* exported functions */
2026
2027 void inc_mail_cb(gpointer data, guint action, GtkWidget *widget)
2028 {
2029         MainWindow *mainwin = (MainWindow*)data;
2030
2031         inc_mail(mainwin, prefs_common.newmail_notify_manu);
2032 }
2033
2034 void inc_all_account_mail_cb(gpointer data, guint action, GtkWidget *widget)
2035 {
2036         MainWindow *mainwin = (MainWindow*)data;
2037
2038         inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
2039 }
2040
2041 void send_queue_cb(gpointer data, guint action, GtkWidget *widget)
2042 {
2043         GList *list;
2044
2045         if (prefs_common.work_offline)
2046                 if (alertpanel(_("Offline warning"), 
2047                                _("You're working offline. Override?"),
2048                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
2049                 return;
2050
2051         for (list = folder_get_list(); list != NULL; list = list->next) {
2052                 Folder *folder = list->data;
2053
2054                 if (folder->queue) {
2055                         procmsg_send_queue(folder->queue, prefs_common.savemsg);
2056                         folder_item_scan(folder->queue);
2057                 }
2058         }
2059 }
2060
2061 void compose_mail_cb(gpointer data, guint action, GtkWidget *widget)
2062 {
2063         MainWindow *mainwin = (MainWindow*)data;
2064         PrefsAccount *ac = NULL;
2065         FolderItem *item = mainwin->summaryview->folder_item;   
2066         GList * list;
2067         GList * cur;
2068
2069         if (item) {
2070                 ac = account_find_from_item(item);
2071                 if (ac && ac->protocol != A_NNTP) {
2072                         compose_new_with_folderitem(ac, item);          /* CLAWS */
2073                         return;
2074                 }
2075         }
2076
2077         /*
2078          * CLAWS - use current account
2079          */
2080         if (cur_account && (cur_account->protocol != A_NNTP)) {
2081                 compose_new_with_folderitem(cur_account, item);
2082                 return;
2083         }
2084
2085         /*
2086          * CLAWS - just get the first one
2087          */
2088         list = account_get_list();
2089         for (cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2090                 ac = (PrefsAccount *) cur->data;
2091                 if (ac->protocol != A_NNTP) {
2092                         compose_new_with_folderitem(ac, item);
2093                         return;
2094                 }
2095         }
2096 }
2097
2098 void compose_news_cb(gpointer data, guint action, GtkWidget *widget)
2099 {
2100         MainWindow *mainwin = (MainWindow*)data;
2101         PrefsAccount * ac = NULL;
2102         GList * list;
2103         GList * cur;
2104
2105         if (mainwin->summaryview->folder_item) {
2106                 ac = mainwin->summaryview->folder_item->folder->account;
2107                 if (ac && ac->protocol == A_NNTP) {
2108                         compose_new(ac,
2109                                     mainwin->summaryview->folder_item->path,
2110                                     NULL);
2111                         return;
2112                 }
2113         }
2114
2115         list = account_get_list();
2116         for(cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2117                 ac = (PrefsAccount *) cur->data;
2118                 if (ac->protocol == A_NNTP) {
2119                         compose_new(ac, NULL, NULL);
2120                         return;
2121                 }
2122         }
2123 }