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