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