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