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