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