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