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