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