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