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