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