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