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