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