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