2012-10-03 [mones] 3.8.1cvs82
[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         jmp_buf    jumper;
709
710         debug_print("read Toolbar Configuration from %s\n", toolbar_config[source].conf_file);
711
712         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
713         file = xml_open_file(fileSpec);
714         g_free(fileSpec);
715
716         toolbar_clear_list(source);
717
718         if (file) {
719                 if ((setjmp(jumper))
720                 || (xml_get_dtd(file))
721                 || (xml_parse_next_tag(file))
722                 || (!xml_compare_tag(file, TOOLBAR_TAG_INDEX))) {
723                         xml_close_file(file);
724                         return;
725                 }
726
727                 for (;;) {
728                         if (!file->level) 
729                                 break;
730                         /* Get item tag */
731                         if (xml_parse_next_tag(file)) 
732                                 longjmp(jumper, 1);
733
734                         /* Get next tag (icon, icon_text or icon_action) */
735                         if (xml_compare_tag(file, TOOLBAR_TAG_ITEM)) {
736                                 toolbar_parse_item(file, source);
737                         } else if (xml_compare_tag(file, TOOLBAR_TAG_SEPARATOR)) {
738                                 ToolbarItem *item = g_new0(ToolbarItem, 1);
739                         
740                                 item->file   = g_strdup(TOOLBAR_TAG_SEPARATOR);
741                                 item->index  = A_SEPARATOR;
742                                 toolbar_config[source].item_list = 
743                                         g_slist_append(toolbar_config[source].item_list, item);
744                         }
745
746                 }
747                 xml_close_file(file);
748         }
749
750         if ((!file) || (g_slist_length(toolbar_config[source].item_list) == 0)) {
751
752                 if (source == TOOLBAR_MAIN) 
753                         toolbar_set_default(TOOLBAR_MAIN);
754                 else if (source == TOOLBAR_COMPOSE) 
755                         toolbar_set_default(TOOLBAR_COMPOSE);
756                 else if (source == TOOLBAR_MSGVIEW) 
757                         toolbar_set_default(TOOLBAR_MSGVIEW);
758                 else {          
759                         g_warning("failed to write Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
760                         return;
761                 }
762
763                 toolbar_save_config_file(source);
764         }
765 }
766
767 /*
768  * clears list of toolbar items read from configuration files
769  */
770 void toolbar_clear_list(ToolbarType source)
771 {
772         while (toolbar_config[source].item_list != NULL) {
773                 ToolbarItem *item = (ToolbarItem*) toolbar_config[source].item_list->data;
774                 
775                 toolbar_config[source].item_list = 
776                         g_slist_remove(toolbar_config[source].item_list, item);
777
778                 g_free(item->file);
779                 g_free(item->text);
780                 g_free(item);   
781         }
782         g_slist_free(toolbar_config[source].item_list);
783 }
784
785
786 /* 
787  * return list of Toolbar items
788  */
789 GSList *toolbar_get_list(ToolbarType source)
790 {
791         GSList *list = NULL;
792
793         if ((source == TOOLBAR_MAIN) || (source == TOOLBAR_COMPOSE) || (source == TOOLBAR_MSGVIEW))
794                 list = toolbar_config[source].item_list;
795
796         return list;
797 }
798
799 void toolbar_set_list_item(ToolbarItem *t_item, ToolbarType source)
800 {
801         ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
802
803         toolbar_item->file  = g_strdup(t_item->file);
804         toolbar_item->text  = g_strdup(t_item->text);
805         toolbar_item->index = t_item->index;
806         
807         toolbar_config[source].item_list = 
808                 g_slist_append(toolbar_config[source].item_list,
809                                toolbar_item);
810 }
811
812 static void toolbar_action_execute(GtkWidget    *widget,
813                             GSList       *action_list, 
814                             gpointer     data,
815                             gint         source) 
816 {
817         GSList *cur;
818         gint i = 0;
819
820         for (cur = action_list; cur != NULL;  cur = cur->next) {
821                 ToolbarClawsActions *act = (ToolbarClawsActions*)cur->data;
822
823                 if (widget == act->widget) {
824                         i = prefs_actions_find_by_name(act->name);
825
826                         if (i != -1) 
827                                 break;
828                 }
829         }
830
831         if (i != -1) 
832                 actions_execute(data, i, widget, source);
833         else
834                 g_warning ("Error: did not find Claws Action to execute");
835 }
836
837 gboolean toolbar_check_action_btns(ToolbarType type)
838 {
839         GSList *temp, *curr, *list = toolbar_config[type].item_list;
840         gboolean modified = FALSE;
841         
842         curr = list;
843         while (curr != NULL) {
844                 ToolbarItem *toolbar_item = (ToolbarItem *) curr->data;
845                 temp = curr;
846                 curr = curr->next;
847                 
848                 if (toolbar_item->index != A_CLAWS_ACTIONS)
849                         continue;
850
851                 if (prefs_actions_find_by_name(toolbar_item->text) == -1) {
852                         list = g_slist_delete_link(list, temp);
853                         g_free(toolbar_item->file);
854                         g_free(toolbar_item->text);
855                         g_free(toolbar_item);
856                         modified = TRUE;
857                 }
858         }
859         
860         return modified;
861 }
862
863 #if !(GTK_CHECK_VERSION(2,12,0))
864 #define CLAWS_SET_TOOL_ITEM_TIP(widget,tip) { \
865         gtk_tool_item_set_tooltip(GTK_TOOL_ITEM(widget), GTK_TOOLTIPS(toolbar_tips),    \
866                         tip, NULL);                                                             \
867 }
868 #else
869 #define CLAWS_SET_TOOL_ITEM_TIP(widget,tip) { \
870         gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), tip);                             \
871 }
872 #endif  
873
874 #if !(GTK_CHECK_VERSION(2,12,0))
875 #define CLAWS_SET_ARROW_TIP(widget,tip) { \
876         gtk_menu_tool_button_set_arrow_tooltip(GTK_MENU_TOOL_BUTTON(widget), GTK_TOOLTIPS(toolbar_tips),        \
877                         tip, NULL);                                                             \
878 }
879 #else
880 #define CLAWS_SET_ARROW_TIP(widget,tip) { \
881         gtk_menu_tool_button_set_arrow_tooltip_text(GTK_MENU_TOOL_BUTTON(widget), tip);                         \
882 }
883 #endif  
884
885 static void activate_compose_button (Toolbar           *toolbar,
886                                      ToolbarStyle      style,
887                                      ComposeButtonType type)
888 {
889 #if !(GTK_CHECK_VERSION(2,12,0))
890         GtkTooltips *toolbar_tips = toolbar->tooltips;
891 #endif
892         if ((!toolbar->compose_mail_btn))
893                 return;
894
895         if (type == COMPOSEBUTTON_NEWS) {
896                 gtk_tool_button_set_icon_widget(
897                         GTK_TOOL_BUTTON(toolbar->compose_mail_btn),
898                         toolbar->compose_news_icon);
899 #ifndef GENERIC_UMPC
900                 CLAWS_SET_TOOL_ITEM_TIP(GTK_TOOL_ITEM(toolbar->compose_mail_btn), _("Compose News message"));
901 #endif  
902                 gtk_widget_show(toolbar->compose_news_icon);
903         } else {
904                 gtk_tool_button_set_icon_widget(
905                         GTK_TOOL_BUTTON(toolbar->compose_mail_btn),
906                         toolbar->compose_mail_icon);
907 #ifndef GENERIC_UMPC
908                 CLAWS_SET_TOOL_ITEM_TIP(GTK_TOOL_ITEM(toolbar->compose_mail_btn), _("Compose Email"));
909 #endif  
910                 gtk_widget_show(toolbar->compose_mail_icon);
911         }
912         toolbar->compose_btn_type = type;
913 }
914
915 void toolbar_set_compose_button(Toolbar            *toolbar, 
916                                 ComposeButtonType  compose_btn_type)
917 {
918         if (toolbar->compose_btn_type != compose_btn_type)
919                 activate_compose_button(toolbar, 
920                                         prefs_common.toolbar_style,
921                                         compose_btn_type);
922 }
923
924 static void activate_learn_button (Toolbar           *toolbar,
925                                      ToolbarStyle      style,
926                                      LearnButtonType type)
927 {
928 #if !(GTK_CHECK_VERSION(2,12,0))
929         GtkTooltips *toolbar_tips = toolbar->tooltips;
930 #endif
931         if ((!toolbar->learn_spam_btn))
932                 return;
933
934         if (type == LEARN_SPAM) {
935                 gtk_tool_button_set_icon_widget(
936                         GTK_TOOL_BUTTON(toolbar->learn_spam_btn),
937                         toolbar->learn_spam_icon);
938                 gtk_tool_button_set_label(
939                         GTK_TOOL_BUTTON(toolbar->learn_spam_btn),
940                         _("Spam"));
941 #ifndef GENERIC_UMPC
942                 CLAWS_SET_TOOL_ITEM_TIP(GTK_TOOL_ITEM(toolbar->learn_spam_btn), _("Learn spam"));       
943 #endif
944                 gtk_widget_show(toolbar->learn_spam_icon);
945         } else {
946                 gtk_tool_button_set_icon_widget(
947                         GTK_TOOL_BUTTON(toolbar->learn_spam_btn),
948                         toolbar->learn_ham_icon);
949                 gtk_tool_button_set_label(
950                         GTK_TOOL_BUTTON(toolbar->learn_spam_btn),
951                         _("Ham"));
952 #ifndef GENERIC_UMPC
953                 CLAWS_SET_TOOL_ITEM_TIP(GTK_TOOL_ITEM(toolbar->learn_spam_btn), _("Learn ham"));
954 #endif  
955                 gtk_widget_show(toolbar->learn_ham_icon);
956         }
957         toolbar->learn_btn_type = type; 
958 }
959
960 void toolbar_set_learn_button(Toolbar            *toolbar, 
961                                 LearnButtonType  learn_btn_type)
962 {
963         if (toolbar->learn_btn_type != learn_btn_type)
964                 activate_learn_button(toolbar, 
965                                         prefs_common.toolbar_style,
966                                         learn_btn_type);
967 }
968
969 void toolbar_toggle(guint action, gpointer data)
970 {
971         MainWindow *mainwin = (MainWindow*)data;
972         GList *list;
973         GList *cur;
974
975         cm_return_if_fail(mainwin != NULL);
976
977         toolbar_style(TOOLBAR_MAIN, action, mainwin);
978
979         list = compose_get_compose_list();
980         for (cur = list; cur != NULL; cur = cur->next) {
981                 toolbar_style(TOOLBAR_COMPOSE, action, cur->data);
982         }
983         list = messageview_get_msgview_list();
984         for (cur = list; cur != NULL; cur = cur->next) {
985                 toolbar_style(TOOLBAR_MSGVIEW, action, cur->data);
986         }
987         
988 }
989
990 void toolbar_set_style(GtkWidget *toolbar_wid, GtkWidget *handlebox_wid, guint action)
991 {
992         switch ((ToolbarStyle)action) {
993         case TOOLBAR_NONE:
994                 gtk_widget_hide(handlebox_wid);
995                 break;
996         case TOOLBAR_ICON:
997                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
998                                       GTK_TOOLBAR_ICONS);
999                 break;
1000         case TOOLBAR_TEXT:
1001                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
1002                                       GTK_TOOLBAR_TEXT);
1003                 break;
1004         case TOOLBAR_BOTH:
1005                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
1006                                       GTK_TOOLBAR_BOTH);
1007                 break;
1008         case TOOLBAR_BOTH_HORIZ:
1009                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
1010                                       GTK_TOOLBAR_BOTH_HORIZ);
1011                 break;
1012         default:
1013                 return;
1014         }
1015
1016         prefs_common.toolbar_style = (ToolbarStyle)action;
1017         gtk_widget_set_size_request(handlebox_wid, 1, -1);
1018         
1019         if (prefs_common.toolbar_style != TOOLBAR_NONE) {
1020                 gtk_widget_show(handlebox_wid);
1021                 gtk_widget_queue_resize(handlebox_wid);
1022         }
1023 }
1024 /*
1025  * Change the style of toolbar
1026  */
1027 static void toolbar_style(ToolbarType type, guint action, gpointer data)
1028 {
1029         GtkWidget  *handlebox_wid;
1030         GtkWidget  *toolbar_wid;
1031         MainWindow *mainwin = (MainWindow*)data;
1032         Compose    *compose = (Compose*)data;
1033         MessageView *msgview = (MessageView*)data;
1034         
1035         cm_return_if_fail(data != NULL);
1036         
1037         switch (type) {
1038         case TOOLBAR_MAIN:
1039                 handlebox_wid = mainwin->handlebox;
1040                 toolbar_wid = mainwin->toolbar->toolbar;
1041                 break;
1042         case TOOLBAR_COMPOSE:
1043                 handlebox_wid = compose->handlebox;
1044                 toolbar_wid = compose->toolbar->toolbar;
1045                 break;
1046         case TOOLBAR_MSGVIEW: 
1047                 handlebox_wid = msgview->handlebox;
1048                 toolbar_wid = msgview->toolbar->toolbar;
1049                 break;
1050         default:
1051
1052                 return;
1053         }
1054         toolbar_set_style(toolbar_wid, handlebox_wid, action);
1055 }
1056
1057 /* Toolbar handling */
1058 static void toolbar_inc_cb(GtkWidget    *widget,
1059                            gpointer      data)
1060 {
1061         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1062         MainWindow *mainwin;
1063
1064         cm_return_if_fail(toolbar_item != NULL);
1065
1066         switch (toolbar_item->type) {
1067         case TOOLBAR_MAIN:
1068                 mainwin = (MainWindow*)toolbar_item->parent;    
1069                 inc_mail_cb(mainwin, 0, NULL);
1070                 break;
1071         default:
1072                 break;
1073         }
1074 }
1075
1076 static void toolbar_inc_all_cb(GtkWidget        *widget,
1077                                gpointer          data)
1078 {
1079         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1080         MainWindow *mainwin;
1081
1082         cm_return_if_fail(toolbar_item != NULL);
1083
1084         switch (toolbar_item->type) {
1085         case TOOLBAR_MAIN:
1086                 mainwin = (MainWindow*)toolbar_item->parent;
1087                 inc_all_account_mail_cb(mainwin, 0, NULL);
1088                 break;
1089         default:
1090                 break;
1091         }
1092 }
1093
1094 static void toolbar_send_queued_cb(GtkWidget *widget,gpointer data)
1095 {
1096         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1097         MainWindow *mainwin;
1098
1099         cm_return_if_fail(toolbar_item != NULL);
1100
1101         switch (toolbar_item->type) {
1102         case TOOLBAR_MAIN:
1103                 mainwin = (MainWindow*)toolbar_item->parent;
1104                 send_queue_cb(mainwin, 0, NULL);
1105                 break;
1106         default:
1107                 break;
1108         }
1109 }
1110
1111 static void toolbar_exec_cb(GtkWidget   *widget,
1112                             gpointer     data)
1113 {
1114         MainWindow *mainwin = get_mainwin(data);
1115
1116         cm_return_if_fail(mainwin != NULL);
1117         summary_execute(mainwin->summaryview);
1118 }
1119
1120 /*
1121  * Delete current/selected(s) message(s)
1122  */
1123 static void toolbar_trash_cb(GtkWidget *widget, gpointer data)
1124 {
1125         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1126         MainWindow *mainwin;
1127
1128         cm_return_if_fail(toolbar_item != NULL);
1129         cm_return_if_fail(toolbar_item->parent);
1130         
1131         switch (toolbar_item->type) {
1132         case TOOLBAR_MSGVIEW:
1133                 messageview_delete((MessageView *)toolbar_item->parent);
1134                 break;
1135         case TOOLBAR_MAIN:
1136                 mainwin = (MainWindow *)toolbar_item->parent;
1137                 summary_delete_trash(mainwin->summaryview);
1138                 break;
1139         default: 
1140                 debug_print("toolbar event not supported\n");
1141                 break;
1142         }
1143 }
1144
1145 /*
1146  * Delete current/selected(s) message(s)
1147  */
1148 static void toolbar_delete_cb(GtkWidget *widget, gpointer data)
1149 {
1150         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1151         MainWindow *mainwin;
1152
1153         cm_return_if_fail(toolbar_item != NULL);
1154         cm_return_if_fail(toolbar_item->parent);
1155         
1156         switch (toolbar_item->type) {
1157         case TOOLBAR_MSGVIEW:
1158                 messageview_delete((MessageView *)toolbar_item->parent);
1159                 break;
1160         case TOOLBAR_MAIN:
1161                 mainwin = (MainWindow *)toolbar_item->parent;
1162                 summary_delete(mainwin->summaryview);
1163                 break;
1164         default: 
1165                 debug_print("toolbar event not supported\n");
1166                 break;
1167         }
1168 }
1169
1170
1171 /*
1172  * Compose new message
1173  */
1174 static void toolbar_compose_cb(GtkWidget *widget, gpointer data)
1175 {
1176         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1177         MainWindow *mainwin;
1178         MessageView *msgview;
1179
1180         cm_return_if_fail(toolbar_item != NULL);
1181
1182         switch (toolbar_item->type) {
1183         case TOOLBAR_MAIN:
1184                 mainwin = (MainWindow*)toolbar_item->parent;
1185                 if (mainwin->toolbar->compose_btn_type == COMPOSEBUTTON_NEWS) 
1186                         compose_news_cb(mainwin, 0, NULL);
1187                 else
1188                         compose_mail_cb(mainwin, 0, NULL);
1189                 break;
1190         case TOOLBAR_MSGVIEW:
1191                 msgview = (MessageView*)toolbar_item->parent;
1192                 compose_new_with_folderitem(NULL, 
1193                                             msgview->msginfo->folder, NULL);
1194                 break;  
1195         default:
1196                 debug_print("toolbar event not supported\n");
1197         }
1198 }
1199
1200 static void toolbar_learn(gpointer data, guint as_spam)
1201 {
1202         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1203         MainWindow *mainwin;
1204         MessageView *msgview;
1205
1206         cm_return_if_fail(toolbar_item != NULL);
1207
1208         switch (toolbar_item->type) {
1209         case TOOLBAR_MAIN:
1210                 mainwin = (MainWindow*)toolbar_item->parent;
1211                 if (as_spam) 
1212                         mainwindow_learn(mainwin, TRUE);
1213                 else
1214                         mainwindow_learn(mainwin, FALSE);
1215                 break;
1216         case TOOLBAR_MSGVIEW:
1217                 msgview = (MessageView*)toolbar_item->parent;
1218                 if (as_spam) 
1219                         messageview_learn(msgview, TRUE);
1220                 else
1221                         messageview_learn(msgview, FALSE);
1222                 break;
1223         default:
1224                 debug_print("toolbar event not supported\n");
1225         }
1226 }
1227
1228 static void toolbar_learn_cb(GtkWidget *widget, gpointer data)
1229 {
1230         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1231         MainWindow *mainwin;
1232         MessageView *msgview;
1233
1234         cm_return_if_fail(toolbar_item != NULL);
1235
1236         switch (toolbar_item->type) {
1237         case TOOLBAR_MAIN:
1238                 mainwin = (MainWindow*)toolbar_item->parent;
1239                 if (mainwin->toolbar->learn_btn_type == LEARN_SPAM) 
1240                         mainwindow_learn(mainwin, TRUE);
1241                 else
1242                         mainwindow_learn(mainwin, FALSE);
1243                 break;
1244         case TOOLBAR_MSGVIEW:
1245                 msgview = (MessageView*)toolbar_item->parent;
1246                 if (msgview->toolbar->learn_btn_type == LEARN_SPAM) 
1247                         messageview_learn(msgview, TRUE);
1248                 else
1249                         messageview_learn(msgview, FALSE);
1250                 break;
1251         default:
1252                 debug_print("toolbar event not supported\n");
1253         }
1254 }
1255
1256
1257 /*
1258  * Reply Message
1259  */
1260 static void toolbar_reply_cb(GtkWidget *widget, gpointer data)
1261 {
1262         toolbar_reply(data, (prefs_common.reply_with_quote ? 
1263                       COMPOSE_REPLY_WITH_QUOTE : COMPOSE_REPLY_WITHOUT_QUOTE));
1264 }
1265
1266
1267 /*
1268  * Reply message to Sender and All recipients
1269  */
1270 static void toolbar_reply_to_all_cb(GtkWidget *widget, gpointer data)
1271 {
1272         toolbar_reply(data,
1273                       (prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_ALL_WITH_QUOTE 
1274                       : COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE));
1275 }
1276
1277
1278 /*
1279  * Reply to Mailing List
1280  */
1281 static void toolbar_reply_to_list_cb(GtkWidget *widget, gpointer data)
1282 {
1283         toolbar_reply(data, 
1284                       (prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_LIST_WITH_QUOTE 
1285                       : COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE));
1286 }
1287
1288
1289 /*
1290  * Reply to sender of message
1291  */ 
1292 static void toolbar_reply_to_sender_cb(GtkWidget *widget, gpointer data)
1293 {
1294         toolbar_reply(data, 
1295                       (prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_SENDER_WITH_QUOTE 
1296                       : COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE));
1297 }
1298
1299 /*
1300  * Open addressbook
1301  */ 
1302 static void toolbar_addrbook_cb(GtkWidget *widget, gpointer data)
1303 {
1304         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1305         Compose *compose;
1306
1307         cm_return_if_fail(toolbar_item != NULL);
1308
1309         switch (toolbar_item->type) {
1310         case TOOLBAR_MAIN:
1311         case TOOLBAR_MSGVIEW:
1312                 compose = NULL;
1313                 break;
1314         case TOOLBAR_COMPOSE:
1315                 compose = (Compose *)toolbar_item->parent;
1316                 break;
1317         default:
1318                 return;
1319         }
1320 #ifndef USE_NEW_ADDRBOOK
1321         addressbook_open(compose);
1322 #else
1323         GError* error = NULL;
1324         addressbook_connect_signals(compose);
1325         addressbook_dbus_open(TRUE, &error);
1326         if (error) {
1327                 g_warning("%s", error->message);
1328                 g_error_free(error);
1329         }
1330 #endif
1331 }
1332
1333
1334 /*
1335  * Forward current/selected(s) message(s)
1336  */
1337 static void toolbar_forward_cb(GtkWidget *widget, gpointer data)
1338 {
1339         toolbar_reply(data, (COMPOSE_FORWARD));
1340 }
1341
1342 /*
1343  * Goto Prev Unread Message
1344  */
1345 static void toolbar_prev_unread_cb(GtkWidget *widget, gpointer data)
1346 {
1347         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1348         MainWindow *mainwin;
1349         MessageView *msgview;
1350
1351         cm_return_if_fail(toolbar_item != NULL);
1352
1353         switch (toolbar_item->type) {
1354         case TOOLBAR_MAIN:
1355                 mainwin = (MainWindow*)toolbar_item->parent;
1356                 summary_select_prev_unread(mainwin->summaryview);
1357                 break;
1358                 
1359         case TOOLBAR_MSGVIEW:
1360                 msgview = (MessageView*)toolbar_item->parent;
1361                 msgview->updating = TRUE;
1362                 summary_select_prev_unread(msgview->mainwin->summaryview);
1363                 msgview->updating = FALSE;
1364
1365                 if (msgview->deferred_destroy) {
1366                         debug_print("messageview got away!\n");
1367                         messageview_destroy(msgview);
1368                         return;
1369                 }
1370                 
1371                 /* Now we need to update the messageview window */
1372                 if (msgview->mainwin->summaryview->selected) {
1373 #ifndef GENERIC_UMPC
1374                         MsgInfo * msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
1375                        
1376                         if (msginfo)
1377                                 messageview_show(msgview, msginfo, 
1378                                          msgview->all_headers);
1379 #endif
1380                 } else {
1381                         gtk_widget_destroy(msgview->window);
1382                 }
1383                 break;
1384         default:
1385                 debug_print("toolbar event not supported\n");
1386         }
1387 }
1388
1389 /*
1390  * Goto Next Unread Message
1391  */
1392 static void toolbar_next_unread_cb(GtkWidget *widget, gpointer data)
1393 {
1394         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1395         MainWindow *mainwin;
1396         MessageView *msgview;
1397
1398         cm_return_if_fail(toolbar_item != NULL);
1399
1400         switch (toolbar_item->type) {
1401         case TOOLBAR_MAIN:
1402                 mainwin = (MainWindow*)toolbar_item->parent;
1403                 summary_select_next_unread(mainwin->summaryview);
1404                 break;
1405                 
1406         case TOOLBAR_MSGVIEW:
1407                 msgview = (MessageView*)toolbar_item->parent;
1408                 msgview->updating = TRUE;
1409                 summary_select_next_unread(msgview->mainwin->summaryview);
1410                 msgview->updating = FALSE;
1411
1412                 if (msgview->deferred_destroy) {
1413                         debug_print("messageview got away!\n");
1414                         messageview_destroy(msgview);
1415                         return;
1416                 }
1417
1418                 /* Now we need to update the messageview window */
1419                 if (msgview->mainwin->summaryview->selected) {
1420 #ifndef GENERIC_UMPC
1421                         MsgInfo * msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
1422                         
1423                         if (msginfo)
1424                                 messageview_show(msgview, msginfo, 
1425                                          msgview->all_headers);
1426 #endif
1427                 } else {
1428                         gtk_widget_destroy(msgview->window);
1429                 }
1430                 break;
1431         default:
1432                 debug_print("toolbar event not supported\n");
1433         }
1434 }
1435
1436 static void toolbar_ignore_thread_cb(GtkWidget *widget, gpointer data)
1437 {
1438         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1439         MainWindow *mainwin;
1440
1441         cm_return_if_fail(toolbar_item != NULL);
1442
1443         switch (toolbar_item->type) {
1444         case TOOLBAR_MAIN:
1445                 mainwin = (MainWindow *) toolbar_item->parent;
1446                 summary_toggle_ignore_thread(mainwin->summaryview);
1447                 break;
1448         case TOOLBAR_MSGVIEW:
1449                 /* TODO: see toolbar_next_unread_cb() if you need
1450                  * this in the message view */
1451                 break;
1452         default:
1453                 debug_print("toolbar event not supported\n");
1454                 break;
1455         }
1456 }
1457
1458 static void toolbar_watch_thread_cb(GtkWidget *widget, gpointer data)
1459 {
1460         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1461         MainWindow *mainwin;
1462
1463         cm_return_if_fail(toolbar_item != NULL);
1464
1465         switch (toolbar_item->type) {
1466         case TOOLBAR_MAIN:
1467                 mainwin = (MainWindow *) toolbar_item->parent;
1468                 summary_toggle_watch_thread(mainwin->summaryview);
1469                 break;
1470         case TOOLBAR_MSGVIEW:
1471                 /* TODO: see toolbar_next_unread_cb() if you need
1472                  * this in the message view */
1473                 break;
1474         default:
1475                 debug_print("toolbar event not supported\n");
1476                 break;
1477         }
1478 }
1479
1480 static void toolbar_cancel_inc_cb(GtkWidget *widget, gpointer data)
1481 {
1482         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1483
1484         cm_return_if_fail(toolbar_item != NULL);
1485         inc_cancel_all();
1486         imap_cancel_all();
1487 }
1488
1489
1490 static void toolbar_print_cb(GtkWidget *widget, gpointer data)
1491 {
1492         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1493         MainWindow *mainwin;
1494
1495         cm_return_if_fail(toolbar_item != NULL);
1496
1497         switch (toolbar_item->type) {
1498         case TOOLBAR_MAIN:
1499                 mainwin = (MainWindow *) toolbar_item->parent;
1500                 summary_print(mainwin->summaryview);
1501                 break;
1502         case TOOLBAR_MSGVIEW:
1503                 /* TODO: see toolbar_next_unread_cb() if you need
1504                  * this in the message view */
1505                 break;
1506         default:
1507                 debug_print("toolbar event not supported\n");
1508                 break;
1509         }
1510 }
1511
1512 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
1513 {
1514         compose_toolbar_cb(A_SEND, data);
1515 }
1516
1517 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
1518 {
1519         compose_toolbar_cb(A_SENDL, data);
1520 }
1521
1522 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
1523 {
1524         compose_toolbar_cb(A_DRAFT, data);
1525 }
1526
1527 static void toolbar_close_cb(GtkWidget *widget, gpointer data)
1528 {
1529         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1530         MainWindow *mainwin;
1531         MessageView *messageview;
1532         Compose *compose;
1533
1534         cm_return_if_fail(toolbar_item != NULL);
1535
1536         switch (toolbar_item->type) {
1537         case TOOLBAR_MAIN:
1538                 mainwin = (MainWindow *) toolbar_item->parent;
1539                 app_will_exit(NULL, mainwin);
1540                 break;
1541         case TOOLBAR_MSGVIEW:
1542                 messageview = (MessageView *)toolbar_item->parent;
1543                 messageview_destroy(messageview);
1544                 break;
1545         case TOOLBAR_COMPOSE:
1546                 compose = (Compose *)toolbar_item->parent;
1547                 compose_close_toolbar(compose);
1548                 break;
1549         }
1550 }
1551
1552 static void toolbar_preferences_cb(GtkWidget *widget, gpointer data)
1553 {
1554         prefs_gtk_open();
1555 }
1556
1557 static void toolbar_open_mail_cb(GtkWidget *widget, gpointer data)
1558 {
1559         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1560         MainWindow *mainwin;
1561
1562         cm_return_if_fail(toolbar_item != NULL);
1563
1564         switch (toolbar_item->type) {
1565         case TOOLBAR_MAIN:
1566                 mainwin = (MainWindow *) toolbar_item->parent;
1567                 summary_open_row(NULL, mainwin->summaryview);
1568                 break;
1569         case TOOLBAR_MSGVIEW:
1570                 debug_print("toolbar event not supported\n");
1571                 break;
1572         case TOOLBAR_COMPOSE:
1573                 debug_print("toolbar event not supported\n");
1574                 break;
1575         }
1576 }
1577
1578 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
1579 {
1580         compose_toolbar_cb(A_INSERT, data);
1581 }
1582
1583 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
1584 {
1585         compose_toolbar_cb(A_ATTACH, data);
1586 }
1587
1588 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
1589 {
1590         compose_toolbar_cb(A_SIG, data);
1591 }
1592
1593 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
1594 {
1595         compose_toolbar_cb(A_EXTEDITOR, data);
1596 }
1597
1598 static void toolbar_linewrap_current_cb(GtkWidget *widget, gpointer data)
1599 {
1600         compose_toolbar_cb(A_LINEWRAP_CURRENT, data);
1601 }
1602
1603 static void toolbar_linewrap_all_cb(GtkWidget *widget, gpointer data)
1604 {
1605         compose_toolbar_cb(A_LINEWRAP_ALL, data);
1606 }
1607
1608 #ifdef USE_ENCHANT
1609 static void toolbar_check_spelling_cb(GtkWidget *widget, gpointer data)
1610 {
1611         compose_toolbar_cb(A_CHECK_SPELLING, data);
1612 }
1613 #endif
1614 /*
1615  * Execute actions from toolbar
1616  */
1617 static void toolbar_actions_execute_cb(GtkWidget *widget, gpointer data)
1618 {
1619         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1620         GSList *action_list;
1621         MainWindow *mainwin;
1622         Compose *compose;
1623         MessageView *msgview;
1624         gpointer parent = toolbar_item->parent;
1625
1626         cm_return_if_fail(toolbar_item != NULL);
1627
1628         switch (toolbar_item->type) {
1629         case TOOLBAR_MAIN:
1630                 mainwin = (MainWindow*)parent;
1631                 action_list = mainwin->toolbar->action_list;
1632                 break;
1633         case TOOLBAR_COMPOSE:
1634                 compose = (Compose*)parent;
1635                 action_list = compose->toolbar->action_list;
1636                 break;
1637         case TOOLBAR_MSGVIEW:
1638                 msgview = (MessageView*)parent;
1639                 action_list = msgview->toolbar->action_list;
1640                 break;
1641         default:
1642                 debug_print("toolbar event not supported\n");
1643                 return;
1644         }
1645         toolbar_action_execute(widget, action_list, parent, toolbar_item->type);        
1646 }
1647
1648 static void toolbar_plugins_execute_cb(GtkWidget *widget, gpointer data)
1649 {
1650         ToolbarItem *toolbar_item = data;
1651         prefs_toolbar_execute_plugin_item(toolbar_item->parent, toolbar_item->type, toolbar_item->text);
1652 }
1653
1654 static MainWindow *get_mainwin(gpointer data)
1655 {
1656         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1657         MainWindow *mainwin = NULL;
1658         MessageView *msgview;
1659
1660         cm_return_val_if_fail(toolbar_item != NULL, NULL);
1661
1662         switch(toolbar_item->type) {
1663         case TOOLBAR_MAIN:
1664                 mainwin = (MainWindow*)toolbar_item->parent;
1665                 break;
1666         case TOOLBAR_MSGVIEW:
1667                 msgview = (MessageView*)toolbar_item->parent;
1668                 mainwin = (MainWindow*)msgview->mainwin;
1669                 break;
1670         default:
1671                 break;
1672         }
1673
1674         return mainwin;
1675 }
1676
1677 static void toolbar_go_folders_cb(GtkWidget *widget, gpointer data)
1678 {
1679         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1680         MainWindow *mainwin = NULL;
1681         switch(toolbar_item->type) {
1682         case TOOLBAR_MAIN:
1683                 mainwin = (MainWindow*)toolbar_item->parent;
1684                 break;
1685         default:
1686                 g_warning("wrong toolbar type\n");
1687                 return;
1688         }
1689
1690         if (!mainwin->in_folder) {
1691                 FolderItem *item = folderview_get_selected_item(mainwin->folderview);
1692                 if (item) {
1693                         folderview_select(mainwin->folderview, item);
1694                 }
1695         } else {
1696                 gtk_widget_grab_focus(mainwin->folderview->ctree);
1697                 mainwindow_exit_folder(mainwin);
1698         }
1699 }
1700
1701 static void toolbar_buttons_cb(GtkWidget   *widget, 
1702                                ToolbarItem *item)
1703 {
1704         gint num_items;
1705         gint i;
1706         struct {
1707                 gint   index;
1708                 void (*func)(GtkWidget *widget, gpointer data);
1709         } callbacks[] = {
1710                 { A_RECEIVE_ALL,        toolbar_inc_all_cb              },
1711                 { A_RECEIVE_CUR,        toolbar_inc_cb                  },
1712                 { A_SEND_QUEUED,        toolbar_send_queued_cb          },
1713                 { A_COMPOSE_EMAIL,      toolbar_compose_cb              },
1714                 { A_COMPOSE_NEWS,       toolbar_compose_cb              },
1715                 { A_REPLY_MESSAGE,      toolbar_reply_cb                },
1716                 { A_REPLY_SENDER,       toolbar_reply_to_sender_cb      },
1717                 { A_REPLY_ALL,          toolbar_reply_to_all_cb         },
1718                 { A_REPLY_ML,           toolbar_reply_to_list_cb        },
1719                 { A_FORWARD,            toolbar_forward_cb              },
1720                 { A_TRASH,              toolbar_trash_cb                },
1721                 { A_DELETE_REAL,        toolbar_delete_cb               },
1722                 { A_EXECUTE,            toolbar_exec_cb                 },
1723                 { A_GOTO_PREV,          toolbar_prev_unread_cb          },
1724                 { A_GOTO_NEXT,          toolbar_next_unread_cb          },
1725                 { A_IGNORE_THREAD,      toolbar_ignore_thread_cb        },
1726                 { A_WATCH_THREAD,       toolbar_watch_thread_cb         },
1727                 { A_PRINT,              toolbar_print_cb                },
1728                 { A_LEARN_SPAM,         toolbar_learn_cb                },
1729                 { A_GO_FOLDERS,         toolbar_go_folders_cb           },
1730
1731                 { A_SEND,               toolbar_send_cb                 },
1732                 { A_SENDL,              toolbar_send_later_cb           },
1733                 { A_DRAFT,              toolbar_draft_cb                },
1734                 { A_OPEN_MAIL,          toolbar_open_mail_cb            },
1735                 { A_CLOSE,              toolbar_close_cb                },
1736                 { A_PREFERENCES,        toolbar_preferences_cb          },
1737                 { A_INSERT,             toolbar_insert_cb               },
1738                 { A_ATTACH,             toolbar_attach_cb               },
1739                 { A_SIG,                toolbar_sig_cb                  },
1740                 { A_EXTEDITOR,          toolbar_ext_editor_cb           },
1741                 { A_LINEWRAP_CURRENT,   toolbar_linewrap_current_cb     },
1742                 { A_LINEWRAP_ALL,       toolbar_linewrap_all_cb         },
1743                 { A_ADDRBOOK,           toolbar_addrbook_cb             },
1744 #ifdef USE_ENCHANT
1745                 { A_CHECK_SPELLING,     toolbar_check_spelling_cb       },
1746 #endif
1747                 { A_CLAWS_ACTIONS,      toolbar_actions_execute_cb      },
1748                 { A_CANCEL_INC,         toolbar_cancel_inc_cb           },
1749                 { A_CLAWS_PLUGINS,  toolbar_plugins_execute_cb  },
1750         };
1751
1752         num_items = sizeof(callbacks)/sizeof(callbacks[0]);
1753
1754         for (i = 0; i < num_items; i++) {
1755                 if (callbacks[i].index == item->index) {
1756                         callbacks[i].func(widget, item);
1757                         return;
1758                 }
1759         }
1760 }
1761 #ifndef GENERIC_UMPC
1762 #define TOOLBAR_ITEM(item,icon,text,tooltip) {                                                          \
1763         item = GTK_WIDGET(gtk_tool_button_new(icon, text));                                             \
1764         gtkut_widget_set_can_focus(gtk_bin_get_child(GTK_BIN(item)), FALSE);                            \
1765         gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(item), FALSE);                                      \
1766         gtk_tool_item_set_is_important(GTK_TOOL_ITEM(item), TRUE);                                      \
1767         g_signal_connect (G_OBJECT(item), "clicked", G_CALLBACK(toolbar_buttons_cb), toolbar_item);     \
1768         gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(item), -1);                              \
1769         CLAWS_SET_TOOL_ITEM_TIP(GTK_TOOL_ITEM(item),                                                    \
1770                         tooltip);                                                                       \
1771 }
1772
1773 #define TOOLBAR_MENUITEM(item,icon,text,tooltip,menutip) {                                              \
1774         GtkWidget *child = NULL, *btn = NULL, *arr = NULL;                                              \
1775         GList *gchild = NULL;                                                                           \
1776         item = GTK_WIDGET(gtk_menu_tool_button_new(icon, text));                                        \
1777         gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(item), FALSE);                              \
1778         gtk_tool_item_set_is_important(GTK_TOOL_ITEM(item), TRUE);                                      \
1779         g_signal_connect (G_OBJECT(item), "clicked", G_CALLBACK(toolbar_buttons_cb), toolbar_item);     \
1780         gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(item), -1);                              \
1781         CLAWS_SET_TOOL_ITEM_TIP(GTK_TOOL_ITEM(item),                                                    \
1782                         tooltip);                                                                       \
1783         CLAWS_SET_ARROW_TIP(GTK_MENU_TOOL_BUTTON(item), menutip);                                       \
1784         child = gtk_bin_get_child(GTK_BIN(item));                                                       \
1785         gchild = gtk_container_get_children(                                                            \
1786                         GTK_CONTAINER(child));                                                          \
1787         btn = (GtkWidget *)gchild->data;                                                                \
1788         gtkut_widget_set_can_focus(btn, FALSE);                                                         \
1789         arr = (GtkWidget *)(gchild->next?gchild->next->data:NULL);                                      \
1790         gtkut_widget_set_can_focus(arr, FALSE);                                                         \
1791         g_list_free(gchild);                                                                            \
1792         gchild = gtk_container_get_children(GTK_CONTAINER(arr));                                        \
1793         gtk_widget_set_size_request(GTK_WIDGET(gchild->data), 9, -1);                                   \
1794         g_list_free(gchild);                                                                            \
1795 }
1796 #else
1797 #define TOOLBAR_ITEM(item,icon,text,tooltip) {                                                          \
1798         item = GTK_WIDGET(gtk_tool_button_new(icon, text));                                             \
1799         gtkut_widget_set_can_focus(gtk_bin_get_child(GTK_BIN(item)), FALSE);                            \
1800         gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(item), FALSE);                                      \
1801         gtk_tool_item_set_is_important(GTK_TOOL_ITEM(item), TRUE);                                      \
1802         g_signal_connect (G_OBJECT(item), "clicked", G_CALLBACK(toolbar_buttons_cb), toolbar_item);     \
1803         gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(item), -1);                              \
1804 }
1805
1806 #define TOOLBAR_MENUITEM(item,icon,text,tooltip,menutip) {                                              \
1807         GtkWidget *child = NULL, *btn = NULL, *arr = NULL;                                              \
1808         GList *gchild = NULL;                                                                           \
1809         item = GTK_WIDGET(gtk_menu_tool_button_new(icon, text));                                        \
1810         gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(item), FALSE);                              \
1811         gtk_tool_item_set_is_important(GTK_TOOL_ITEM(item), TRUE);                                      \
1812         g_signal_connect (G_OBJECT(item), "clicked", G_CALLBACK(toolbar_buttons_cb), toolbar_item);     \
1813         gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(item), -1);                              \
1814         child = gtk_bin_get_child(GTK_BIN(item));                                                       \
1815         gchild = gtk_container_get_children(                                                            \
1816                         GTK_CONTAINER(child));                                                          \
1817         btn = (GtkWidget *)gchild->data;                                                                \
1818         gtkut_widget_set_can_focus(btn, FALSE);                                                         \
1819         arr = (GtkWidget *)(gchild->next?gchild->next->data:NULL);                                      \
1820         gtkut_widget_set_can_focus(arr, FALSE);                                                         \
1821         g_list_free(gchild);                                                                            \
1822         gchild = gtk_container_get_children(GTK_CONTAINER(arr));                                        \
1823         gtk_widget_set_size_request(GTK_WIDGET(gchild->data), 9, -1);                                   \
1824         g_list_free(gchild);                                                                            \
1825 }
1826 #endif
1827
1828 #define ADD_MENU_ITEM(name,cb,data) {                                                   \
1829         item = gtk_menu_item_new_with_mnemonic(name);                                   \
1830         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);                              \
1831         g_signal_connect(G_OBJECT(item), "activate",                                    \
1832                          G_CALLBACK(cb),                                                \
1833                            toolbar_item);                                               \
1834         g_object_set_data(G_OBJECT(item), "int-value", GINT_TO_POINTER(data));          \
1835         gtk_widget_show(item);                                                          \
1836 }
1837
1838 static void toolbar_reply_menu_cb(GtkWidget *widget, gpointer data)
1839 {
1840         gpointer int_value = g_object_get_data(G_OBJECT(widget), "int-value");
1841         ToolbarItem *toolbar_item = (ToolbarItem *)data;
1842         
1843         toolbar_reply(toolbar_item, GPOINTER_TO_INT(int_value));
1844 }
1845
1846 static void toolbar_learn_menu_cb(GtkWidget *widget, gpointer data)
1847 {
1848         gpointer int_value = g_object_get_data(G_OBJECT(widget), "int-value");
1849         ToolbarItem *toolbar_item = (ToolbarItem *)data;
1850         
1851         toolbar_learn(toolbar_item, GPOINTER_TO_INT(int_value));
1852 }
1853
1854 /**
1855  * Create a new toolbar with specified type
1856  * if a callback list is passed it will be used before the 
1857  * common callback list
1858  **/
1859 Toolbar *toolbar_create(ToolbarType      type, 
1860                         GtkWidget       *container,
1861                         gpointer         data)
1862 {
1863         ToolbarItem *toolbar_item;
1864
1865         GtkWidget *toolbar;
1866         GtkWidget *icon_wid = NULL;
1867         GtkWidget *icon_news;
1868         GtkWidget *icon_ham;
1869         GtkWidget *item;
1870         ToolbarClawsActions *action_item;
1871         GSList *cur;
1872         GSList *toolbar_list;
1873         Toolbar *toolbar_data;
1874         GtkWidget *menu;
1875 #ifndef GENERIC_UMPC
1876 #if !(GTK_CHECK_VERSION(2,12,0))
1877         GtkTooltips *toolbar_tips = gtk_tooltips_new();
1878 #endif
1879 #endif  
1880         toolbar_read_config_file(type);
1881         toolbar_list = toolbar_get_list(type);
1882
1883         toolbar_data = g_new0(Toolbar, 1); 
1884
1885         toolbar = gtk_toolbar_new();
1886
1887 #if (GTK_CHECK_VERSION(2,16,0))
1888         gtk_orientable_set_orientation(GTK_ORIENTABLE(toolbar), GTK_ORIENTATION_HORIZONTAL);
1889 #else
1890         gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar), GTK_ORIENTATION_HORIZONTAL);
1891 #endif
1892         gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH);
1893         gtk_toolbar_set_show_arrow(GTK_TOOLBAR(toolbar), TRUE);
1894         
1895         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
1896
1897                 if (g_ascii_strcasecmp(((ToolbarItem*)cur->data)->file, TOOLBAR_TAG_SEPARATOR) == 0) {
1898                         gtk_toolbar_insert(GTK_TOOLBAR(toolbar), gtk_separator_tool_item_new(), -1);
1899                         continue;
1900                 }
1901                 
1902                 toolbar_item = g_new0(ToolbarItem, 1); 
1903                 toolbar_item->index = ((ToolbarItem*)cur->data)->index;
1904                 toolbar_item->file = g_strdup(((ToolbarItem*)cur->data)->file);
1905                 toolbar_item->text = g_strdup(((ToolbarItem*)cur->data)->text);
1906                 toolbar_item->parent = data;
1907                 toolbar_item->type = type;
1908
1909                 /* collect toolbar items in list to keep track */
1910                 toolbar_data->item_list = 
1911                         g_slist_append(toolbar_data->item_list, 
1912                                        toolbar_item);
1913                 icon_wid = stock_pixmap_widget(container, stock_pixmap_get_icon(toolbar_item->file));
1914                         
1915                 switch (toolbar_item->index) {
1916
1917                 case A_GO_FOLDERS:
1918                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Go to folder list"));
1919                         toolbar_data->folders_btn = item;
1920                         break;
1921                 case A_RECEIVE_ALL:
1922                         TOOLBAR_MENUITEM(item,icon_wid,toolbar_item->text,
1923                                 _("Receive Mail from all Accounts"),
1924                                 _("Receive Mail from selected Account"));
1925                         toolbar_data->getall_btn = item;
1926                         break;
1927                 case A_RECEIVE_CUR:
1928                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text, _("Receive Mail from current Account"));
1929                         toolbar_data->get_btn = item;
1930                         break;
1931                 case A_SEND_QUEUED:
1932                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Send Queued Messages"));
1933                         toolbar_data->send_btn = item; 
1934                         break;
1935                 case A_CLOSE:
1936                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Close window"));
1937                         toolbar_data->close_window_btn = item; 
1938                         break;
1939                 case A_PREFERENCES:
1940                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Open preferences"));
1941                         toolbar_data->preferences_btn = item; 
1942                         break;
1943                 case A_OPEN_MAIL:
1944                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Open email"));
1945                         toolbar_data->open_mail_btn = item; 
1946                         break;
1947                 case A_COMPOSE_EMAIL:
1948 #ifndef GENERIC_UMPC
1949                         TOOLBAR_MENUITEM(item,icon_wid,toolbar_item->text,
1950                                 _("Compose Email"),
1951                                 _("Compose with selected Account"));
1952                         toolbar_data->compose_mail_btn = item; 
1953                         toolbar_data->compose_mail_icon = icon_wid; 
1954                         g_object_ref(toolbar_data->compose_mail_icon);
1955
1956                         icon_news = stock_pixmap_widget(container, STOCK_PIXMAP_NEWS_COMPOSE);
1957                         toolbar_data->compose_news_icon = icon_news; 
1958                         g_object_ref(toolbar_data->compose_news_icon);
1959 #else
1960                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,
1961                                 _("Compose Email"));
1962                         toolbar_data->compose_mail_btn = item; 
1963                         toolbar_data->compose_mail_icon = icon_wid; 
1964
1965                         icon_news = stock_pixmap_widget(container, STOCK_PIXMAP_NEWS_COMPOSE);
1966                         toolbar_data->compose_news_icon = icon_news; 
1967 #endif
1968                         break;
1969                 case A_LEARN_SPAM:
1970                         TOOLBAR_MENUITEM(item,icon_wid,toolbar_item->text,
1971                                 _("Spam"),
1972                                 _("Learn as..."));
1973                         toolbar_data->learn_spam_btn = item; 
1974                         toolbar_data->learn_spam_icon = icon_wid; 
1975                         g_object_ref(toolbar_data->learn_spam_icon);
1976
1977                         icon_ham = stock_pixmap_widget(container, STOCK_PIXMAP_HAM_BTN);
1978                         toolbar_data->learn_ham_icon = icon_ham; 
1979                         g_object_ref(toolbar_data->learn_ham_icon);
1980
1981                         menu = gtk_menu_new();
1982                         ADD_MENU_ITEM(_("Learn as _Spam"), toolbar_learn_menu_cb, TRUE);
1983                         ADD_MENU_ITEM(_("Learn as _Ham"), toolbar_learn_menu_cb, FALSE);
1984                         gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(toolbar_data->learn_spam_btn), menu);
1985                         break;
1986                 case A_REPLY_MESSAGE:
1987 #ifndef GENERIC_UMPC
1988                         TOOLBAR_MENUITEM(item,icon_wid,toolbar_item->text,
1989                                 _("Reply to Message"),
1990                                 _("Reply to Message options"));
1991                         toolbar_data->reply_btn = item;
1992
1993                         menu = gtk_menu_new();
1994                         ADD_MENU_ITEM(_("_Reply with quote"), toolbar_reply_menu_cb, COMPOSE_REPLY_WITH_QUOTE);
1995                         ADD_MENU_ITEM(_("Reply without _quote"), toolbar_reply_menu_cb, COMPOSE_REPLY_WITHOUT_QUOTE);
1996                         gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(toolbar_data->reply_btn), menu);
1997 #else
1998                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,
1999                                 _("Reply to Message"));
2000                         toolbar_data->reply_btn = item;
2001 #endif
2002                         break;
2003                 case A_REPLY_SENDER:
2004 #ifndef GENERIC_UMPC
2005                         TOOLBAR_MENUITEM(item,icon_wid,toolbar_item->text,
2006                                 _("Reply to Sender"),
2007                                 _("Reply to Sender options"));
2008                         toolbar_data->replysender_btn = item;
2009
2010                         menu = gtk_menu_new();
2011                         ADD_MENU_ITEM(_("_Reply with quote"), toolbar_reply_menu_cb, COMPOSE_REPLY_TO_SENDER_WITH_QUOTE);
2012                         ADD_MENU_ITEM(_("Reply without _quote"), toolbar_reply_menu_cb, COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE);
2013                         gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(toolbar_data->replysender_btn), menu);
2014 #else
2015                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,
2016                                 _("Reply to Sender"));
2017                         toolbar_data->replysender_btn = item;
2018 #endif
2019                         break;
2020                 case A_REPLY_ALL:
2021 #ifndef GENERIC_UMPC
2022                         TOOLBAR_MENUITEM(item,icon_wid,toolbar_item->text,
2023                                 _("Reply to All"),
2024                                 _("Reply to All options"));
2025                         toolbar_data->replyall_btn = item;
2026
2027                         menu = gtk_menu_new();
2028                         ADD_MENU_ITEM(_("_Reply with quote"), toolbar_reply_menu_cb, COMPOSE_REPLY_TO_ALL_WITH_QUOTE);
2029                         ADD_MENU_ITEM(_("Reply without _quote"), toolbar_reply_menu_cb, COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE);
2030                         gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(toolbar_data->replyall_btn), menu);
2031 #else
2032                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,
2033                                 _("Reply to All"));
2034                         toolbar_data->replyall_btn = item;
2035 #endif
2036                         break;
2037                 case A_REPLY_ML:
2038 #ifndef GENERIC_UMPC
2039                         TOOLBAR_MENUITEM(item,icon_wid,toolbar_item->text,
2040                                 _("Reply to Mailing-list"),
2041                                 _("Reply to Mailing-list options"));
2042                         toolbar_data->replylist_btn = item;
2043
2044                         menu = gtk_menu_new();
2045                         ADD_MENU_ITEM(_("_Reply with quote"), toolbar_reply_menu_cb, COMPOSE_REPLY_TO_LIST_WITH_QUOTE);
2046                         ADD_MENU_ITEM(_("Reply without _quote"), toolbar_reply_menu_cb, COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE);
2047                         gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(toolbar_data->replylist_btn), menu);
2048 #else
2049                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,
2050                                 _("Reply to Mailing-list"));
2051                         toolbar_data->replylist_btn = item;
2052 #endif
2053                         break;
2054                 case A_FORWARD:
2055 #ifndef GENERIC_UMPC
2056                         TOOLBAR_MENUITEM(item,icon_wid,toolbar_item->text,
2057                                 _("Forward Message"),
2058                                 _("Forward Message options"));
2059                         toolbar_data->fwd_btn = item;
2060
2061                         menu = gtk_menu_new();
2062                         ADD_MENU_ITEM(_("_Forward"), toolbar_reply_menu_cb, COMPOSE_FORWARD_INLINE);
2063                         ADD_MENU_ITEM(_("For_ward as attachment"), toolbar_reply_menu_cb, COMPOSE_FORWARD_AS_ATTACH);
2064                         ADD_MENU_ITEM(_("Redirec_t"), toolbar_reply_menu_cb, COMPOSE_REDIRECT);
2065                         gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(toolbar_data->fwd_btn), menu);
2066 #else
2067                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,
2068                                 _("Forward Message"));
2069                         toolbar_data->fwd_btn = item;
2070 #endif
2071                         break;
2072                 case A_TRASH:
2073                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Trash Message"));
2074                         toolbar_data->trash_btn = item;
2075                         break;
2076                 case A_DELETE_REAL:
2077                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Delete Message"));
2078                         toolbar_data->delete_btn = item;
2079                         break;
2080                 case A_EXECUTE:
2081                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Execute"));
2082                         toolbar_data->exec_btn = item;
2083                         break;
2084                 case A_GOTO_PREV:
2085                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Go to Previous Unread Message"));
2086                         toolbar_data->prev_btn = item;
2087                         break;
2088                 case A_GOTO_NEXT:
2089                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Go to Next Unread Message"));
2090                         toolbar_data->next_btn = item;
2091                         break;
2092                 
2093                 /* Compose Toolbar */
2094                 case A_SEND:
2095                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Send Message"));
2096                         toolbar_data->send_btn = item;
2097                         break;
2098                 case A_SENDL:
2099                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Put into queue folder and send later"));
2100                         toolbar_data->sendl_btn = item;
2101                         break;
2102                 case A_DRAFT:
2103                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Save to draft folder"));
2104                         toolbar_data->draft_btn = item; 
2105                         break;
2106                 case A_INSERT:
2107                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Insert file"));
2108                         toolbar_data->insert_btn = item; 
2109                         break;
2110                 case A_ATTACH:
2111                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Attach file"));
2112                         toolbar_data->attach_btn = item;
2113                         break;
2114                 case A_SIG:
2115                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Insert signature"));
2116                         toolbar_data->sig_btn = item;
2117                         break;
2118                 case A_EXTEDITOR:
2119                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Edit with external editor"));
2120                         toolbar_data->exteditor_btn = item;
2121                         break;
2122                 case A_LINEWRAP_CURRENT:
2123                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Wrap long lines of current paragraph"));
2124                         toolbar_data->linewrap_current_btn = item;
2125                         break;
2126                 case A_LINEWRAP_ALL:
2127                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Wrap all long lines"));
2128                         toolbar_data->linewrap_all_btn = item;
2129                         break;
2130                 case A_ADDRBOOK:
2131                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Address book"));
2132                         toolbar_data->addrbook_btn = item;
2133                         break;
2134 #ifdef USE_ENCHANT
2135                 case A_CHECK_SPELLING:
2136                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Check spelling"));
2137                         toolbar_data->spellcheck_btn = item;
2138                         break;
2139 #endif
2140
2141                 case A_CLAWS_ACTIONS:
2142                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,toolbar_item->text);
2143                         action_item = g_new0(ToolbarClawsActions, 1);
2144                         action_item->widget = item;
2145                         action_item->name   = g_strdup(toolbar_item->text);
2146
2147                         toolbar_data->action_list = 
2148                                 g_slist_append(toolbar_data->action_list,
2149                                                action_item);
2150                         break;
2151                 case A_CANCEL_INC:
2152                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,_("Cancel receiving"));
2153                         toolbar_data->cancel_inc_btn = item;
2154                         break;
2155                 case A_CLAWS_PLUGINS:
2156                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text, toolbar_item->text);
2157                         break;
2158                 default:
2159                         TOOLBAR_ITEM(item,icon_wid,toolbar_item->text,
2160                                 toolbar_ret_descr_from_val(toolbar_item->index));
2161                         /* find and set the tool tip text */
2162                         break;
2163                 }
2164
2165         }
2166         toolbar_data->toolbar = toolbar;
2167 #ifndef GENERIC_UMPC
2168 #if !(GTK_CHECK_VERSION(2,12,0))
2169         toolbar_data->tooltips = toolbar_tips;
2170 #endif
2171 #endif
2172         gtk_widget_show_all(toolbar);
2173
2174         if (type == TOOLBAR_MAIN) {
2175 #ifdef GENERIC_UMPC
2176                 MainWindow *mainwin = mainwindow_get_mainwindow();
2177                 GtkWidget *progressbar = gtk_progress_bar_new();
2178                 item = GTK_WIDGET(gtk_tool_item_new());
2179                 gtk_container_add (GTK_CONTAINER (item), progressbar);
2180                 gtk_widget_show(item);
2181                 gtk_widget_set_size_request(progressbar, 84, -1);
2182                 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(item), -1);
2183                 mainwin->progressbar = progressbar;
2184 #endif
2185                 activate_compose_button(toolbar_data, 
2186                                         prefs_common.toolbar_style, 
2187                                         toolbar_data->compose_btn_type);
2188         }
2189         if (type != TOOLBAR_COMPOSE)
2190                 activate_learn_button(toolbar_data, prefs_common.toolbar_style,
2191                                 LEARN_SPAM);
2192         
2193 #ifndef MAEMO
2194         gtk_container_add(GTK_CONTAINER(container), toolbar);
2195         gtk_container_set_border_width(GTK_CONTAINER(container), 0);
2196 #else
2197         if ( GTK_IS_WINDOW(container) ) {
2198                 hildon_window_add_toolbar (HILDON_WINDOW(container), GTK_TOOLBAR(toolbar));
2199                 gtk_widget_show_all (container);
2200         } else {
2201                 gtk_container_add(GTK_CONTAINER(container), toolbar);
2202                 gtk_container_set_border_width(GTK_CONTAINER(container), 2);
2203         }
2204 #endif
2205         return toolbar_data; 
2206 }
2207
2208 /**
2209  * Free toolbar structures
2210  */ 
2211 void toolbar_destroy(Toolbar * toolbar) {
2212
2213         TOOLBAR_DESTROY_ITEMS(toolbar->item_list);      
2214         TOOLBAR_DESTROY_ACTIONS(toolbar->action_list);
2215 }
2216
2217 void toolbar_update(ToolbarType type, gpointer data)
2218 {
2219         Toolbar *toolbar_data;
2220         GtkWidget *handlebox;
2221         MainWindow *mainwin = (MainWindow*)data;
2222         Compose    *compose = (Compose*)data;
2223         MessageView *msgview = (MessageView*)data;
2224
2225 #ifndef GENERIC_UMPC
2226         switch(type) {
2227         case TOOLBAR_MAIN:
2228                 toolbar_data = mainwin->toolbar;
2229                 handlebox    = mainwin->handlebox;
2230                 break;
2231         case TOOLBAR_COMPOSE:
2232                 toolbar_data = compose->toolbar;
2233                 handlebox    = compose->handlebox;
2234                 break;
2235         case TOOLBAR_MSGVIEW:
2236                 toolbar_data = msgview->toolbar;
2237                 handlebox    = msgview->handlebox;
2238                 break;
2239         default:
2240                 return;
2241         }
2242
2243         gtk_container_remove(GTK_CONTAINER(handlebox), 
2244                              GTK_WIDGET(toolbar_data->toolbar));
2245
2246         toolbar_init(toolbar_data);
2247         toolbar_data = toolbar_create(type, handlebox, data);
2248 #else
2249         switch(type) {
2250         case TOOLBAR_MAIN:
2251                 toolbar_data = mainwin->toolbar;
2252                 handlebox    = mainwin->window;
2253                 break;
2254         case TOOLBAR_COMPOSE:
2255                 toolbar_data = compose->toolbar;
2256                 handlebox    = compose->window;
2257                 break;
2258         case TOOLBAR_MSGVIEW:
2259                 toolbar_data = msgview->toolbar;
2260                 handlebox    = msgview->window;
2261                 break;
2262         default:
2263                 return;
2264         }
2265 #ifdef MAEMO
2266         hildon_window_remove_toolbar(HILDON_WINDOW(handlebox), GTK_TOOLBAR(toolbar_data->toolbar));
2267 #endif
2268         toolbar_init(toolbar_data);
2269         toolbar_data = toolbar_create(type, handlebox, data);
2270 #endif
2271
2272         switch(type) {
2273         case TOOLBAR_MAIN:
2274                 mainwin->toolbar = toolbar_data;
2275                 break;
2276         case TOOLBAR_COMPOSE:
2277                 compose->toolbar = toolbar_data;
2278                 break;
2279         case TOOLBAR_MSGVIEW:
2280                 msgview->toolbar = toolbar_data;
2281                 break;
2282         }
2283
2284         toolbar_style(type, prefs_common.toolbar_style, data);
2285
2286         if (type == TOOLBAR_MAIN) {
2287                 toolbar_main_set_sensitive((MainWindow*)data);
2288                 account_set_menu_only_toolbar();
2289         }
2290 }
2291
2292 #if !GTK_CHECK_VERSION(2,14,0)
2293 /* Work around http://bugzilla.gnome.org/show_bug.cgi?id=56070 */
2294 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {            \
2295         gboolean in_btn1 = FALSE, in_btn2 = FALSE;              \
2296         if (GTK_IS_BUTTON(widget))                              \
2297                 in_btn1 = GTK_BUTTON(widget)->in_button;        \
2298         else if (GTK_IS_MENU_TOOL_BUTTON(widget)) {             \
2299                 GtkWidget *child = gtk_bin_get_child(           \
2300                         GTK_BIN(widget));                       \
2301                 GList *gchild = gtk_container_get_children(     \
2302                         GTK_CONTAINER(child));                  \
2303                 GtkWidget *btn = (GtkWidget *)gchild->data;     \
2304                 GtkWidget *arr = (GtkWidget *)                  \
2305                         (gchild->next?gchild->next->data:NULL); \
2306                 g_list_free(gchild);                            \
2307                 if (GTK_IS_BUTTON(btn))                         \
2308                         in_btn1 = GTK_BUTTON(btn)->in_button;   \
2309                 if (GTK_IS_BUTTON(arr))                         \
2310                         in_btn2 = GTK_BUTTON(arr)->in_button;   \
2311         }                                                       \
2312         else if (GTK_IS_TOOL_ITEM(widget)) {                    \
2313                 GtkWidget *child = gtk_bin_get_child(           \
2314                         GTK_BIN(widget));                       \
2315                 if (GTK_IS_BUTTON(child))                       \
2316                         in_btn1 = GTK_BUTTON(child)->in_button; \
2317         }                                                       \
2318         gtk_widget_set_sensitive(widget, sensitive);            \
2319         if (GTK_IS_BUTTON(widget))                              \
2320                 GTK_BUTTON(widget)->in_button = in_btn1;        \
2321         else if (GTK_IS_MENU_TOOL_BUTTON(widget)) {             \
2322                 GtkWidget *child = gtk_bin_get_child(           \
2323                         GTK_BIN(widget));                       \
2324                 GList *gchild = gtk_container_get_children(     \
2325                         GTK_CONTAINER(child));                  \
2326                 GtkWidget *btn = (GtkWidget *)gchild->data;     \
2327                 GtkWidget *arr = (GtkWidget *)                  \
2328                         (gchild->next?gchild->next->data:NULL); \
2329                 g_list_free(gchild);                            \
2330                 if (GTK_IS_BUTTON(btn))                         \
2331                         GTK_BUTTON(btn)->in_button = in_btn1;   \
2332                 if (GTK_IS_BUTTON(arr))                         \
2333                         GTK_BUTTON(arr)->in_button = in_btn2;   \
2334         }                                                       \
2335         else if (GTK_IS_TOOL_ITEM(widget)) {                    \
2336                 GtkWidget *child = gtk_bin_get_child(           \
2337                         GTK_BIN(widget));                       \
2338                 if (GTK_IS_BUTTON(child))                       \
2339                         GTK_BUTTON(child)->in_button = in_btn1; \
2340         }                                                       \
2341 }
2342 #else
2343 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {            \
2344         gtk_widget_set_sensitive(widget, sensitive);            \
2345 }
2346 #endif
2347
2348 void toolbar_main_set_sensitive(gpointer data)
2349 {
2350         SensitiveCond state;
2351         gboolean sensitive;
2352         MainWindow *mainwin = (MainWindow*)data;
2353         Toolbar *toolbar = mainwin->toolbar;
2354         GSList *cur;
2355         GSList *entry_list = NULL;
2356         
2357         typedef struct _Entry Entry;
2358         struct _Entry {
2359                 GtkWidget *widget;
2360                 SensitiveCond cond;
2361                 gboolean empty;
2362         };
2363
2364 #define SET_WIDGET_COND(w, c)     \
2365 { \
2366         Entry *e = g_new0(Entry, 1); \
2367         e->widget = w; \
2368         e->cond   = c; \
2369         entry_list = g_slist_append(entry_list, e); \
2370 }
2371
2372         
2373         if (toolbar->get_btn)
2374                 SET_WIDGET_COND(toolbar->get_btn, 
2375                         M_HAVE_ACCOUNT|M_UNLOCKED|M_HAVE_RETRIEVABLE_ACCOUNT);
2376
2377         if (toolbar->getall_btn) {
2378                 SET_WIDGET_COND(toolbar->getall_btn, 
2379                         M_HAVE_ACCOUNT|M_UNLOCKED|M_HAVE_ANY_RETRIEVABLE_ACCOUNT);
2380         }
2381         if (toolbar->send_btn) {
2382                 SET_WIDGET_COND(toolbar->send_btn,
2383                         M_HAVE_QUEUED_MAILS);
2384         }
2385         if (toolbar->compose_mail_btn) {
2386                 SET_WIDGET_COND(toolbar->compose_mail_btn, 
2387                         M_HAVE_ACCOUNT);
2388         }
2389         if (toolbar->close_window_btn) {
2390                 SET_WIDGET_COND(toolbar->close_window_btn, 
2391                         M_UNLOCKED);
2392         }
2393         if (toolbar->open_mail_btn) {
2394                 SET_WIDGET_COND(toolbar->open_mail_btn, 
2395                         M_TARGET_EXIST|M_SUMMARY_ISLIST);
2396         }
2397         if (toolbar->reply_btn) {
2398                 SET_WIDGET_COND(toolbar->reply_btn,
2399                         M_HAVE_ACCOUNT|M_TARGET_EXIST|M_SUMMARY_ISLIST);
2400         }
2401         if (toolbar->replyall_btn) {
2402                 SET_WIDGET_COND(toolbar->replyall_btn,
2403                         M_HAVE_ACCOUNT|M_TARGET_EXIST|M_SUMMARY_ISLIST);
2404         }
2405         if (toolbar->replylist_btn) {
2406                 SET_WIDGET_COND(toolbar->replylist_btn,
2407                         M_HAVE_ACCOUNT|M_TARGET_EXIST|M_SUMMARY_ISLIST);
2408         }
2409         if (toolbar->replysender_btn) {
2410                 SET_WIDGET_COND(toolbar->replysender_btn,
2411                         M_HAVE_ACCOUNT|M_TARGET_EXIST|M_SUMMARY_ISLIST);
2412         }
2413         if (toolbar->fwd_btn) {
2414                 SET_WIDGET_COND(toolbar->fwd_btn, 
2415                         M_HAVE_ACCOUNT|M_TARGET_EXIST|M_SUMMARY_ISLIST);
2416         }
2417
2418         if (prefs_common.next_unread_msg_dialog == NEXTUNREADMSGDIALOG_ASSUME_NO) {
2419                 SET_WIDGET_COND(toolbar->next_btn, M_MSG_EXIST|M_SUMMARY_ISLIST);
2420         } else {
2421                 SET_WIDGET_COND(toolbar->next_btn, 0);
2422         }
2423         
2424         if (toolbar->trash_btn)
2425                 SET_WIDGET_COND(toolbar->trash_btn,
2426                         M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS);
2427
2428         if (toolbar->delete_btn)
2429                 SET_WIDGET_COND(toolbar->delete_btn,
2430                         M_TARGET_EXIST|M_ALLOW_DELETE);
2431
2432         if (toolbar->exec_btn)
2433                 SET_WIDGET_COND(toolbar->exec_btn, 
2434                         M_DELAY_EXEC);
2435         
2436         if (toolbar->learn_spam_btn)
2437                 SET_WIDGET_COND(toolbar->learn_spam_btn, 
2438                         M_TARGET_EXIST|M_CAN_LEARN_SPAM|M_SUMMARY_ISLIST);
2439
2440         if (toolbar->cancel_inc_btn)
2441                 SET_WIDGET_COND(toolbar->cancel_inc_btn,
2442                                 M_INC_ACTIVE);
2443
2444         for (cur = toolbar->action_list; cur != NULL;  cur = cur->next) {
2445                 ToolbarClawsActions *act = (ToolbarClawsActions*)cur->data;
2446                 
2447                 SET_WIDGET_COND(act->widget, M_TARGET_EXIST|M_UNLOCKED);
2448         }
2449
2450 #undef SET_WIDGET_COND
2451
2452         state = main_window_get_current_state(mainwin);
2453
2454         for (cur = entry_list; cur != NULL; cur = cur->next) {
2455                 Entry *e = (Entry*) cur->data;
2456
2457                 if (e->widget != NULL) {
2458                         sensitive = ((e->cond & state) == e->cond);
2459                         GTK_BUTTON_SET_SENSITIVE(e->widget, sensitive); 
2460                 }
2461         }
2462         
2463         while (entry_list != NULL) {
2464                 Entry *e = (Entry*) entry_list->data;
2465
2466                 g_free(e);
2467                 entry_list = g_slist_remove(entry_list, e);
2468         }
2469
2470         g_slist_free(entry_list);
2471
2472         activate_compose_button(toolbar, 
2473                                 prefs_common.toolbar_style,
2474                                 toolbar->compose_btn_type);
2475         
2476 }
2477
2478 void toolbar_comp_set_sensitive(gpointer data, gboolean sensitive)
2479 {
2480         Compose *compose = (Compose*)data;
2481         GSList *items = compose->toolbar->action_list;
2482
2483         if (compose->toolbar->send_btn)
2484                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->send_btn, sensitive);
2485         if (compose->toolbar->sendl_btn)
2486                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->sendl_btn, sensitive);
2487         if (compose->toolbar->draft_btn )
2488                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->draft_btn , sensitive);
2489         if (compose->toolbar->insert_btn )
2490                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->insert_btn , sensitive);
2491         if (compose->toolbar->attach_btn)
2492                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->attach_btn, sensitive);
2493         if (compose->toolbar->sig_btn)
2494                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->sig_btn, sensitive);
2495         if (compose->toolbar->exteditor_btn)
2496                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->exteditor_btn, sensitive);
2497         if (compose->toolbar->linewrap_current_btn)
2498                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->linewrap_current_btn, sensitive);
2499         if (compose->toolbar->linewrap_all_btn)
2500                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->linewrap_all_btn, sensitive);
2501         if (compose->toolbar->addrbook_btn)
2502                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->addrbook_btn, sensitive);
2503 #ifdef USE_ENCHANT
2504         if (compose->toolbar->spellcheck_btn)
2505                 GTK_BUTTON_SET_SENSITIVE(compose->toolbar->spellcheck_btn, sensitive);
2506 #endif
2507         for (; items != NULL; items = g_slist_next(items)) {
2508                 ToolbarClawsActions *item = (ToolbarClawsActions *)items->data;
2509                 GTK_BUTTON_SET_SENSITIVE(item->widget, sensitive);
2510         }
2511 }
2512
2513 /**
2514  * Initialize toolbar structure
2515  **/
2516 static void toolbar_init(Toolbar * toolbar)
2517 {
2518
2519         toolbar->toolbar                = NULL;
2520         toolbar->folders_btn            = NULL;
2521         toolbar->get_btn                = NULL;
2522         toolbar->getall_btn             = NULL;
2523         toolbar->send_btn               = NULL;
2524         toolbar->compose_mail_btn       = NULL;
2525         toolbar->compose_mail_icon      = NULL;
2526         toolbar->compose_news_icon      = NULL;
2527         toolbar->reply_btn              = NULL;
2528         toolbar->replysender_btn        = NULL;
2529         toolbar->replyall_btn           = NULL;
2530         toolbar->replylist_btn          = NULL;
2531         toolbar->fwd_btn                = NULL;
2532         toolbar->trash_btn              = NULL;
2533         toolbar->delete_btn             = NULL;
2534         toolbar->prev_btn               = NULL;
2535         toolbar->next_btn               = NULL;
2536         toolbar->exec_btn               = NULL;
2537         toolbar->open_mail_btn          = NULL;
2538         toolbar->close_window_btn       = NULL;
2539         /* compose buttons */ 
2540         toolbar->sendl_btn              = NULL;
2541         toolbar->draft_btn              = NULL;
2542         toolbar->insert_btn             = NULL;
2543         toolbar->attach_btn             = NULL;
2544         toolbar->sig_btn                = NULL; 
2545         toolbar->exteditor_btn          = NULL; 
2546         toolbar->linewrap_current_btn   = NULL; 
2547         toolbar->linewrap_all_btn       = NULL; 
2548         toolbar->addrbook_btn           = NULL; 
2549 #ifdef USE_ENCHANT
2550         toolbar->spellcheck_btn         = NULL;
2551 #endif
2552
2553         toolbar_destroy(toolbar);
2554 }
2555
2556 /*
2557  */
2558 static void toolbar_reply(gpointer data, guint action)
2559 {
2560         ToolbarItem *toolbar_item = (ToolbarItem*)data;
2561         MainWindow *mainwin;
2562         MessageView *msgview;
2563         GSList *msginfo_list = NULL;
2564
2565         cm_return_if_fail(toolbar_item != NULL);
2566
2567         switch (toolbar_item->type) {
2568         case TOOLBAR_MAIN:
2569                 mainwin = (MainWindow*)toolbar_item->parent;
2570                 msginfo_list = summary_get_selection(mainwin->summaryview);
2571                 msgview = (MessageView*)mainwin->messageview;
2572                 break;
2573         case TOOLBAR_MSGVIEW:
2574                 msgview = (MessageView*)toolbar_item->parent;
2575                 cm_return_if_fail(msgview != NULL);     
2576                 msginfo_list = g_slist_append(msginfo_list, msgview->msginfo);
2577                 break;
2578         default:
2579                 return;
2580         }
2581
2582         cm_return_if_fail(msgview != NULL);
2583         cm_return_if_fail(msginfo_list != NULL);
2584         compose_reply_from_messageview(msgview, msginfo_list, action);
2585         g_slist_free(msginfo_list);
2586
2587         /* TODO: update reply state ion summaryview */
2588 }
2589
2590
2591 /* exported functions */
2592
2593 void inc_mail_cb(gpointer data, guint action, GtkWidget *widget)
2594 {
2595         MainWindow *mainwin = (MainWindow*)data;
2596
2597         inc_mail(mainwin, prefs_common.newmail_notify_manu);
2598 }
2599
2600 void inc_all_account_mail_cb(gpointer data, guint action, GtkWidget *widget)
2601 {
2602         MainWindow *mainwin = (MainWindow*)data;
2603
2604         inc_all_account_mail(mainwin, FALSE, prefs_common.newmail_notify_manu);
2605 }
2606
2607 void send_queue_cb(gpointer data, guint action, GtkWidget *widget)
2608 {
2609         GList *list;
2610         gboolean found;
2611         gboolean got_error = FALSE;
2612         gchar *errstr = NULL;
2613
2614         if (prefs_common.work_offline)
2615                 if (alertpanel(_("Offline warning"), 
2616                                _("You're working offline. Override?"),
2617                                GTK_STOCK_NO, GTK_STOCK_YES,
2618                                NULL) != G_ALERTALTERNATE)
2619                 return;
2620
2621         /* ask for confirmation before sending queued messages only
2622            in online mode and if there is at least one message queued
2623            in any of the folder queue
2624         */
2625         if (prefs_common.confirm_send_queued_messages) {
2626                 found = FALSE;
2627                 /* check if there's a queued message */
2628                 for (list = folder_get_list(); !found && list != NULL; list = list->next) {
2629                         Folder *folder = list->data;
2630
2631                         found = !procmsg_queue_is_empty(folder->queue);
2632                 }
2633                 /* if necessary, ask for confirmation before sending */
2634                 if (found && !prefs_common.work_offline) {
2635                         if (alertpanel(_("Send queued messages"), 
2636                                    _("Send all queued messages?"),
2637                                    GTK_STOCK_CANCEL, _("_Send"),
2638                                    NULL) != G_ALERTALTERNATE)
2639                                 return;
2640                 }
2641         }
2642
2643         for (list = folder_get_list(); list != NULL; list = list->next) {
2644                 Folder *folder = list->data;
2645
2646                 if (folder->queue) {
2647                         if (procmsg_send_queue(folder->queue, 
2648                                                prefs_common.savemsg,
2649                                                &errstr) < 0)
2650                                 got_error = TRUE;
2651                 }
2652         }
2653         if (got_error) {
2654                 if (!errstr)
2655                         alertpanel_error_log(_("Some errors occurred while "
2656                                            "sending queued messages."));
2657                 else {
2658                         alertpanel_error_log(_("Some errors occurred "
2659                                         "while sending queued messages:\n%s"), errstr);
2660                         g_free(errstr);
2661                 }
2662         }
2663 }
2664
2665 void compose_mail_cb(gpointer data, guint action, GtkWidget *widget)
2666 {
2667         MainWindow *mainwin = (MainWindow*)data;
2668         PrefsAccount *ac = NULL;
2669         FolderItem *item = mainwin->summaryview->folder_item;   
2670         GList * list;
2671         GList * cur;
2672         
2673         if (item) {
2674                 ac = account_find_from_item(item);
2675                 if (ac && ac->protocol != A_NNTP) {
2676                         compose_new_with_folderitem(ac, item, NULL);            /* CLAWS */
2677                         return;
2678                 }
2679         }
2680
2681         /*
2682          * CLAWS - use current account
2683          */
2684         if (cur_account && (cur_account->protocol != A_NNTP)) {
2685                 compose_new_with_folderitem(cur_account, item, NULL);
2686                 return;
2687         }
2688
2689         /*
2690          * CLAWS - just get the first one
2691          */
2692         list = account_get_list();
2693         for (cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2694                 ac = (PrefsAccount *) cur->data;
2695                 if (ac->protocol != A_NNTP) {
2696                         compose_new_with_folderitem(ac, item, NULL);
2697                         return;
2698                 }
2699         }
2700 }
2701
2702 void compose_news_cb(gpointer data, guint action, GtkWidget *widget)
2703 {
2704         MainWindow *mainwin = (MainWindow*)data;
2705         PrefsAccount * ac = NULL;
2706         GList * list;
2707         GList * cur;
2708
2709         if (mainwin->summaryview->folder_item) {
2710                 ac = mainwin->summaryview->folder_item->folder->account;
2711                 if (ac && ac->protocol == A_NNTP) {
2712                         compose_new_with_folderitem(ac,
2713                                     mainwin->summaryview->folder_item, NULL);
2714                         return;
2715                 }
2716         }
2717
2718         list = account_get_list();
2719         for(cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2720                 ac = (PrefsAccount *) cur->data;
2721                 if (ac->protocol == A_NNTP) {
2722                         compose_new_with_folderitem(ac,
2723                                     mainwin->summaryview->folder_item, NULL);
2724                         return;
2725                 }
2726         }
2727 }