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