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