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