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