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