0.8.8claws26
[claws.git] / src / toolbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * General functions for accessing address book files.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <dirent.h>
34 #include <sys/stat.h>
35 #include <math.h>
36 #include <setjmp.h>
37
38 #include "intl.h"
39 #include "mainwindow.h"
40 #include "compose.h"
41 #include "utils.h"
42 #include "xml.h"
43 #include "mgutils.h"
44 #include "prefs_gtk.h"
45 #include "codeconv.h"
46 #include "stock_pixmap.h"
47 #include "manage_window.h"
48 #include "gtkutils.h"
49 #include "toolbar.h"
50 #include "menu.h"
51 #include "inc.h"
52 #include "prefs_actions.h"
53 #include "prefs_common.h"
54 #include "prefs_toolbar.h"
55 #include "alertpanel.h"
56
57 /* elements */
58 #define TOOLBAR_TAG_INDEX        "toolbar"
59 #define TOOLBAR_TAG_ITEM         "item"
60 #define TOOLBAR_TAG_SEPARATOR    "separator"
61
62 #define TOOLBAR_ICON_FILE   "file"    
63 #define TOOLBAR_ICON_TEXT   "text"     
64 #define TOOLBAR_ICON_ACTION "action"    
65
66 gboolean      toolbar_is_duplicate              (gint           action,
67                                                  ToolbarType    source);
68 static void   toolbar_parse_item                (XMLFile        *file,
69                                                  ToolbarType    source);
70
71 static gint   toolbar_ret_val_from_text         (const gchar    *text);
72 static gchar *toolbar_ret_text_from_val         (gint           val);
73
74 static void   toolbar_set_default_main          (void);
75 static void   toolbar_set_default_compose       (void);
76 static void   toolbar_set_default_msgview       (void);
77
78 static void     toolbar_style                   (ToolbarType     type, 
79                                                  guint           action, 
80                                                  gpointer        data);
81
82 static GtkWidget *get_window_widget             (ToolbarType     type, 
83                                                  gpointer        data);
84 static MainWindow *get_mainwin                  (gpointer data);
85 static void activate_compose_button             (Toolbar        *toolbar,
86                                                  ToolbarStyle    style,
87                                                  ComposeButtonType type);
88
89 /* toolbar callbacks */
90 static void toolbar_delete_cb                   (GtkWidget      *widget,
91                                                  gpointer        data);
92
93 static void toolbar_compose_cb                  (GtkWidget      *widget,
94                                                  gpointer        data);
95
96 static void toolbar_reply_cb                    (GtkWidget      *widget,
97                                                  gpointer        data);
98
99 static void toolbar_reply_to_all_cb             (GtkWidget      *widget,
100                                                  gpointer        data);
101
102 static void toolbar_reply_to_list_cb            (GtkWidget      *widget,
103                                                  gpointer        data);
104
105 static void toolbar_reply_to_sender_cb          (GtkWidget      *widget,
106                                                  gpointer        data);
107
108 static void toolbar_forward_cb                  (GtkWidget      *widget,
109                                                  gpointer        data);
110
111 static void toolbar_next_unread_cb              (GtkWidget      *widget,
112                                                  gpointer        data);
113
114 static void toolbar_actions_execute_cb          (GtkWidget      *widget,
115                                                  gpointer        data);
116
117
118 static void toolbar_send_cb                     (GtkWidget      *widget,
119                                                  gpointer        data);
120 static void toolbar_send_later_cb               (GtkWidget      *widget,
121                                                  gpointer        data);
122 static void toolbar_draft_cb                    (GtkWidget      *widget,
123                                                  gpointer        data);
124 static void toolbar_insert_cb                   (GtkWidget      *widget,
125                                                  gpointer        data);
126 static void toolbar_attach_cb                   (GtkWidget      *widget,
127                                                  gpointer        data);
128 static void toolbar_sig_cb                      (GtkWidget      *widget,
129                                                  gpointer        data);
130 static void toolbar_ext_editor_cb               (GtkWidget      *widget,
131                                                  gpointer        data);
132 static void toolbar_linewrap_cb                 (GtkWidget      *widget,
133                                                  gpointer        data);
134 static void toolbar_addrbook_cb                 (GtkWidget      *widget, 
135                                                  gpointer        data);
136
137 static void toolbar_popup_cb                    (gpointer        data, 
138                                                  guint           action, 
139                                                  GtkWidget      *widget);
140 struct {
141         gchar *index_str;
142         const gchar *descr;
143 } toolbar_text [] = {
144         { "A_RECEIVE_ALL",   N_("Receive Mail on all Accounts")         },
145         { "A_RECEIVE_CUR",   N_("Receive Mail on current Account")      },
146         { "A_SEND_QUEUED",   N_("Send Queued Message(s)")               },
147         { "A_COMPOSE_EMAIL", N_("Compose Email")                        },
148         { "A_COMPOSE_NEWS",  N_("Compose News")                         },
149         { "A_REPLY_MESSAGE", N_("Reply to Message")                     },
150         { "A_REPLY_SENDER",  N_("Reply to Sender")                      },
151         { "A_REPLY_ALL",     N_("Reply to All")                         },
152         { "A_REPLY_ML",      N_("Reply to Mailing-list")                },
153         { "A_FORWARD",       N_("Forward Message")                      }, 
154         { "A_DELETE",        N_("Delete Message")                       },
155         { "A_EXECUTE",       N_("Execute")                              },
156         { "A_GOTO_NEXT",     N_("Goto Next Message")                    },
157
158         { "A_SEND",          N_("Send Message")                         },
159         { "A_SENDL",         N_("Put into queue folder and send later") },
160         { "A_DRAFT",         N_("Save to draft folder")                 },
161         { "A_INSERT",        N_("Insert file")                          },   
162         { "A_ATTACH",        N_("Attach file")                          },
163         { "A_SIG",           N_("Insert signature")                     },
164         { "A_EXTEDITOR",     N_("Edit with external editor")            },
165         { "A_LINEWRAP",      N_("Wrap all long lines")                  }, 
166         { "A_ADDRBOOK",      N_("Address book")                         },
167
168         { "A_SYL_ACTIONS",   N_("Sylpheed Actions Feature")             }, 
169         { "A_SEPARATOR",     N_("Separator")                            }
170 };
171
172 /* struct holds configuration files and a list of
173  * currently active toolbar items 
174  * TOOLBAR_MAIN, TOOLBAR_COMPOSE and TOOLBAR_MSGVIEW
175  * give us an index
176  */
177 struct {
178         const gchar  *conf_file;
179         GSList       *item_list;
180 } toolbar_config[3] = {
181         { "toolbar_main.xml",    NULL},
182         { "toolbar_compose.xml", NULL}, 
183         { "toolbar_msgview.xml", NULL}
184 };
185
186 static GtkItemFactoryEntry reply_popup_entries[] =
187 {
188         {N_("/Reply with _quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_WITH_QUOTE, NULL},
189         {N_("/_Reply without quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_WITHOUT_QUOTE, NULL}
190 };
191 static GtkItemFactoryEntry replyall_popup_entries[] =
192 {
193         {N_("/Reply to all with _quote"), "<shift>A", toolbar_popup_cb, COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL},
194         {N_("/_Reply to all without quote"), "a", toolbar_popup_cb, COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL}
195 };
196 static GtkItemFactoryEntry replylist_popup_entries[] =
197 {
198         {N_("/Reply to list with _quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_TO_LIST_WITH_QUOTE, NULL},
199         {N_("/_Reply to list without quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE, NULL}
200 };
201 static GtkItemFactoryEntry replysender_popup_entries[] =
202 {
203         {N_("/Reply to sender with _quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_TO_SENDER_WITH_QUOTE, NULL},
204         {N_("/_Reply to sender without quote"), NULL, toolbar_popup_cb, COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE, NULL}
205 };
206 static GtkItemFactoryEntry fwd_popup_entries[] =
207 {
208         {N_("/_Forward message (inline style)"), "f", toolbar_popup_cb, COMPOSE_FORWARD_INLINE, NULL},
209         {N_("/Forward message as _attachment"), "<shift>F", toolbar_popup_cb, COMPOSE_FORWARD_AS_ATTACH, NULL}
210 };
211
212
213 gint toolbar_ret_val_from_descr(const gchar *descr)
214 {
215         gint i;
216
217         for (i = 0; i < N_ACTION_VAL; i++) {
218                 if (g_strcasecmp(gettext(toolbar_text[i].descr), descr) == 0)
219                                 return i;
220         }
221         
222         return -1;
223 }
224
225 gchar *toolbar_ret_descr_from_val(gint val)
226 {
227         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
228
229         return gettext(toolbar_text[val].descr);
230 }
231
232 static gint toolbar_ret_val_from_text(const gchar *text)
233 {
234         gint i;
235         
236         for (i = 0; i < N_ACTION_VAL; i++) {
237                 if (g_strcasecmp(toolbar_text[i].index_str, text) == 0)
238                                 return i;
239         }
240         
241         return -1;
242 }
243
244 static gchar *toolbar_ret_text_from_val(gint val)
245 {
246         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
247
248         return toolbar_text[val].index_str;
249 }
250
251 gboolean toolbar_is_duplicate(gint action, ToolbarType source)
252 {
253         GSList *cur;
254
255         if ((action == A_SEPARATOR) || (action == A_SYL_ACTIONS)) 
256                 return FALSE;
257
258         for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
259                 ToolbarItem *item = (ToolbarItem*) cur->data;
260                 
261                 if (item->index == action)
262                         return TRUE;
263         }
264         return FALSE;
265 }
266
267 /* depending on toolbar type this function 
268    returns a list of available toolbar events being 
269    displayed by prefs_toolbar
270 */
271 GList *toolbar_get_action_items(ToolbarType source)
272 {
273         GList *items = NULL;
274         gint i = 0;
275         
276         if (source == TOOLBAR_MAIN) {
277                 gint main_items[14] = { A_RECEIVE_ALL,   A_RECEIVE_CUR,   A_SEND_QUEUED,
278                                         A_COMPOSE_EMAIL, A_REPLY_MESSAGE, A_REPLY_SENDER,  
279                                         A_REPLY_ALL,     A_REPLY_ML,      A_FORWARD,       
280                                         A_DELETE,        A_EXECUTE,       A_GOTO_NEXT,      
281                                         A_ADDRBOOK,      A_SYL_ACTIONS };
282
283                 for (i = 0; i < sizeof(main_items)/sizeof(main_items[0]); i++) 
284                         items = g_list_append(items, gettext(toolbar_text[main_items[i]].descr));
285         }
286         else if (source == TOOLBAR_COMPOSE) {
287                 gint comp_items[10] = { A_SEND,          A_SENDL,        A_DRAFT,
288                                         A_INSERT,        A_ATTACH,       A_SIG,
289                                         A_EXTEDITOR,     A_LINEWRAP,     A_ADDRBOOK,
290                                         A_SYL_ACTIONS };        
291
292                 for (i = 0; i < sizeof(comp_items)/sizeof(comp_items[0]); i++) 
293                         items = g_list_append(items, gettext(toolbar_text[comp_items[i]].descr));
294         }
295         else if (source == TOOLBAR_MSGVIEW) {
296                 gint msgv_items[10] = { A_COMPOSE_EMAIL, A_REPLY_MESSAGE, A_REPLY_SENDER,
297                                         A_REPLY_ALL,     A_REPLY_ML,      A_FORWARD,
298                                         A_DELETE,        A_GOTO_NEXT,     A_ADDRBOOK,
299                                         A_SYL_ACTIONS };        
300
301                 for (i = 0; i < sizeof(msgv_items)/sizeof(msgv_items[0]); i++) 
302                         items = g_list_append(items, gettext(toolbar_text[msgv_items[i]].descr));
303         }
304
305         return items;
306 }
307
308 static void toolbar_parse_item(XMLFile *file, ToolbarType source)
309 {
310         GList *attr;
311         gchar *name, *value;
312         ToolbarItem *item = NULL;
313
314         attr = xml_get_current_tag_attr(file);
315         item = g_new0(ToolbarItem, 1);
316         while( attr ) {
317                 name = ((XMLAttr *)attr->data)->name;
318                 value = ((XMLAttr *)attr->data)->value;
319                 
320                 if (g_strcasecmp(name, TOOLBAR_ICON_FILE) == 0) 
321                         item->file = g_strdup (value);
322                 else if (g_strcasecmp(name, TOOLBAR_ICON_TEXT) == 0)
323                         item->text = g_strdup (value);
324                 else if (g_strcasecmp(name, TOOLBAR_ICON_ACTION) == 0)
325                         item->index = toolbar_ret_val_from_text(value);
326
327                 attr = g_list_next(attr);
328         }
329         if (item->index != -1) {
330                 
331                 if (!toolbar_is_duplicate(item->index, source)) 
332                         toolbar_config[source].item_list = g_slist_append(toolbar_config[source].item_list,
333                                                                          item);
334         }
335 }
336
337 static void toolbar_set_default_main(void) 
338 {
339         struct {
340                 gint action;
341                 gint icon;
342                 gchar *text;
343         } default_toolbar[] = {
344                 { A_RECEIVE_CUR,   STOCK_PIXMAP_MAIL_RECEIVE,         _("Get")     },
345                 { A_RECEIVE_ALL,   STOCK_PIXMAP_MAIL_RECEIVE_ALL,     _("Get All") },
346                 { A_SEPARATOR,     0,                                 ("")         }, 
347                 { A_SEND_QUEUED,   STOCK_PIXMAP_MAIL_SEND_QUEUE,      _("Send")    },
348                 { A_COMPOSE_EMAIL, STOCK_PIXMAP_MAIL_COMPOSE,         _("Email")   },
349                 { A_SEPARATOR,     0,                                 ("")         },
350                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
351                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
352                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
353                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
354                 { A_SEPARATOR,     0,                                 ("")         },
355                 { A_DELETE,        STOCK_PIXMAP_CLOSE,                _("Delete")  },
356                 { A_EXECUTE,       STOCK_PIXMAP_EXEC,                 _("Execute") },
357                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    }
358         };
359         
360         gint i;
361         
362         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
363                 
364                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
365                 
366                 if (default_toolbar[i].action != A_SEPARATOR) {
367                         
368                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
369                         
370                         toolbar_item->file  = g_strdup(file);
371                         toolbar_item->index = default_toolbar[i].action;
372                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
373                 } else {
374
375                         toolbar_item->file  = g_strdup(TOOLBAR_TAG_SEPARATOR);
376                         toolbar_item->index = A_SEPARATOR;
377                 }
378                 
379                 if (toolbar_item->index != -1) {
380                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_MAIN)) 
381                                 toolbar_config[TOOLBAR_MAIN].item_list = 
382                                         g_slist_append(toolbar_config[TOOLBAR_MAIN].item_list, toolbar_item);
383                 }       
384         }
385 }
386
387 static void toolbar_set_default_compose(void)
388 {
389         struct {
390                 gint action;
391                 gint icon;
392                 gchar *text;
393         } default_toolbar[] = {
394                 { A_SEND,      STOCK_PIXMAP_MAIL_SEND,         _("Send")       },
395                 { A_SENDL,     STOCK_PIXMAP_MAIL_SEND_QUEUE,   _("Send later") },
396                 { A_DRAFT,     STOCK_PIXMAP_MAIL,              _("Draft")      },
397                 { A_SEPARATOR, 0,                               ("")           }, 
398                 { A_INSERT,    STOCK_PIXMAP_INSERT_FILE,       _("Insert")     },
399                 { A_ATTACH,    STOCK_PIXMAP_MAIL_ATTACH,       _("Attach")     },
400                 { A_SIG,       STOCK_PIXMAP_MAIL_SIGN,         _("Signature")  },
401                 { A_SEPARATOR, 0,                               ("")           },
402                 { A_EXTEDITOR, STOCK_PIXMAP_EDIT_EXTERN,       _("Editor")     },
403                 { A_LINEWRAP,  STOCK_PIXMAP_LINEWRAP,          _("Linewrap")   },
404                 { A_SEPARATOR, 0,                               ("")           },
405                 { A_ADDRBOOK,  STOCK_PIXMAP_ADDRESS_BOOK,      _("Address")    }
406         };
407         
408         gint i;
409
410         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
411                 
412                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
413                 
414                 if (default_toolbar[i].action != A_SEPARATOR) {
415                         
416                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
417                         
418                         toolbar_item->file  = g_strdup(file);
419                         toolbar_item->index = default_toolbar[i].action;
420                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
421                 } else {
422
423                         toolbar_item->file  = g_strdup(TOOLBAR_TAG_SEPARATOR);
424                         toolbar_item->index = A_SEPARATOR;
425                 }
426                 
427                 if (toolbar_item->index != -1) {
428                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_COMPOSE)) 
429                                 toolbar_config[TOOLBAR_COMPOSE].item_list = 
430                                         g_slist_append(toolbar_config[TOOLBAR_COMPOSE].item_list, toolbar_item);
431                 }       
432         }
433 }
434
435 static void toolbar_set_default_msgview(void)
436 {
437         struct {
438                 gint action;
439                 gint icon;
440                 gchar *text;
441         } default_toolbar[] = {
442                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
443                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
444                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
445                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
446                 { A_SEPARATOR,     0,                                 ("")         },
447                 { A_DELETE,        STOCK_PIXMAP_CLOSE,                _("Delete")  },
448                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    }
449         };
450         
451         gint i;
452
453         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
454                 
455                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
456                 
457                 if (default_toolbar[i].action != A_SEPARATOR) {
458                         
459                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
460                         
461                         toolbar_item->file  = g_strdup(file);
462                         toolbar_item->index = default_toolbar[i].action;
463                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
464                 } else {
465
466                         toolbar_item->file  = g_strdup(TOOLBAR_TAG_SEPARATOR);
467                         toolbar_item->index = A_SEPARATOR;
468                 }
469                 
470                 if (toolbar_item->index != -1) {
471                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_MSGVIEW)) 
472                                 toolbar_config[TOOLBAR_MSGVIEW].item_list = 
473                                         g_slist_append(toolbar_config[TOOLBAR_MSGVIEW].item_list, toolbar_item);
474                 }       
475         }
476 }
477
478 void toolbar_set_default(ToolbarType source)
479 {
480         if (source == TOOLBAR_MAIN)
481                 toolbar_set_default_main();
482         else if  (source == TOOLBAR_COMPOSE)
483                 toolbar_set_default_compose();
484         else if  (source == TOOLBAR_MSGVIEW)
485                 toolbar_set_default_msgview();
486 }
487
488 void toolbar_save_config_file(ToolbarType source)
489 {
490         GSList *cur;
491         FILE *fp;
492         PrefFile *pfile;
493         gchar *fileSpec = NULL;
494
495         debug_print("save Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
496
497         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
498         pfile = prefs_write_open(fileSpec);
499         g_free( fileSpec );
500         if( pfile ) {
501                 fp = pfile->fp;
502                 fprintf(fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
503                         conv_get_current_charset_str());
504
505                 fprintf(fp, "<%s>\n", TOOLBAR_TAG_INDEX);
506
507                 for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
508                         ToolbarItem *toolbar_item = (ToolbarItem*) cur->data;
509                         
510                         if (toolbar_item->index != A_SEPARATOR) 
511                                 fprintf(fp, "\t<%s %s=\"%s\" %s=\"%s\" %s=\"%s\"/>\n",
512                                         TOOLBAR_TAG_ITEM, 
513                                         TOOLBAR_ICON_FILE, toolbar_item->file,
514                                         TOOLBAR_ICON_TEXT, toolbar_item->text,
515                                         TOOLBAR_ICON_ACTION, 
516                                         toolbar_ret_text_from_val(toolbar_item->index));
517                         else 
518                                 fprintf(fp, "\t<%s/>\n", TOOLBAR_TAG_SEPARATOR); 
519                 }
520
521                 fprintf(fp, "</%s>\n", TOOLBAR_TAG_INDEX);      
522         
523                 if (prefs_write_close (pfile) < 0 ) 
524                         g_warning("failed to write toolbar configuration to file\n");
525         } else
526                 g_warning("failed to open toolbar configuration file for writing\n");
527 }
528
529 void toolbar_read_config_file(ToolbarType source)
530 {
531         XMLFile *file   = NULL;
532         gchar *fileSpec = NULL;
533         GList *attr;
534         gboolean retVal;
535         jmp_buf    jumper;
536
537         debug_print("read Toolbar Configuration from %s\n", toolbar_config[source].conf_file);
538
539         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
540         file = xml_open_file(fileSpec);
541         g_free(fileSpec);
542
543         toolbar_clear_list(source);
544
545         if (file) {
546                 if ((setjmp(jumper))
547                 || (xml_get_dtd(file))
548                 || (xml_parse_next_tag(file))
549                 || (!xml_compare_tag(file, TOOLBAR_TAG_INDEX))) {
550                         xml_close_file(file);
551                         return;
552                 }
553
554                 attr = xml_get_current_tag_attr(file);
555                 
556                 retVal = TRUE;
557                 for (;;) {
558                         if (!file->level) 
559                                 break;
560                         /* Get item tag */
561                         if (xml_parse_next_tag(file)) 
562                                 longjmp(jumper, 1);
563
564                         /* Get next tag (icon, icon_text or icon_action) */
565                         if (xml_compare_tag(file, TOOLBAR_TAG_ITEM)) {
566                                 toolbar_parse_item(file, source);
567                         } else if (xml_compare_tag(file, TOOLBAR_TAG_SEPARATOR)) {
568                                 ToolbarItem *item = g_new0(ToolbarItem, 1);
569                         
570                                 item->file   = g_strdup(TOOLBAR_TAG_SEPARATOR);
571                                 item->index  = A_SEPARATOR;
572                                 toolbar_config[source].item_list = 
573                                         g_slist_append(toolbar_config[source].item_list, item);
574                         }
575
576                 }
577                 xml_close_file(file);
578         }
579
580         if ((!file) || (g_slist_length(toolbar_config[source].item_list) == 0)) {
581
582                 if (source == TOOLBAR_MAIN) 
583                         toolbar_set_default(TOOLBAR_MAIN);
584                 else if (source == TOOLBAR_COMPOSE) 
585                         toolbar_set_default(TOOLBAR_COMPOSE);
586                 else if (source == TOOLBAR_MSGVIEW) 
587                         toolbar_set_default(TOOLBAR_MSGVIEW);
588                 else {          
589                         g_warning("failed to write Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
590                         return;
591                 }
592
593                 toolbar_save_config_file(source);
594         }
595 }
596
597 /*
598  * clears list of toolbar items read from configuration files
599  */
600 void toolbar_clear_list(ToolbarType source)
601 {
602         while (toolbar_config[source].item_list != NULL) {
603                 ToolbarItem *item = (ToolbarItem*) toolbar_config[source].item_list->data;
604                 
605                 toolbar_config[source].item_list = 
606                         g_slist_remove(toolbar_config[source].item_list, item);
607
608                 if (item->file)
609                         g_free(item->file);
610                 if (item->text)
611                         g_free(item->text);
612                 g_free(item);   
613         }
614         g_slist_free(toolbar_config[source].item_list);
615 }
616
617
618 /* 
619  * return list of Toolbar items
620  */
621 GSList *toolbar_get_list(ToolbarType source)
622 {
623         GSList *list = NULL;
624
625         if ((source == TOOLBAR_MAIN) || (source == TOOLBAR_COMPOSE) || (source == TOOLBAR_MSGVIEW))
626                 list = toolbar_config[source].item_list;
627
628         return list;
629 }
630
631 void toolbar_set_list_item(ToolbarItem *t_item, ToolbarType source)
632 {
633         ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
634
635         toolbar_item->file  = g_strdup(t_item->file);
636         toolbar_item->text  = g_strdup(t_item->text);
637         toolbar_item->index = t_item->index;
638         
639         toolbar_config[source].item_list = 
640                 g_slist_append(toolbar_config[source].item_list,
641                                toolbar_item);
642 }
643
644 void toolbar_action_execute(GtkWidget    *widget,
645                             GSList       *action_list, 
646                             gpointer     data,
647                             gint         source) 
648 {
649         GSList *cur, *lop;
650         gchar *action, *action_p;
651         gboolean found = FALSE;
652         gint i = 0;
653
654         for (cur = action_list; cur != NULL;  cur = cur->next) {
655                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
656
657                 if (widget == act->widget) {
658                         
659                         for (lop = prefs_common.actions_list; lop != NULL; lop = lop->next) {
660                                 action = g_strdup((gchar*)lop->data);
661
662                                 action_p = strstr(action, ": ");
663                                 action_p[0] = 0x00;
664                                 if (g_strcasecmp(act->name, action) == 0) {
665                                         found = TRUE;
666                                         g_free(action);
667                                         break;
668                                 } else 
669                                         i++;
670                                 g_free(action);
671                         }
672                         if (found) 
673                                 break;
674                 }
675         }
676
677         if (found) 
678                 actions_execute(data, i, widget, source);
679         else
680                 g_warning ("Error: did not find Sylpheed Action to execute");
681 }
682
683 static void activate_compose_button (Toolbar           *toolbar,
684                                      ToolbarStyle      style,
685                                      ComposeButtonType type)
686 {
687         if ((!toolbar->compose_mail_btn) || (!toolbar->compose_news_btn))
688                 return;
689
690         gtk_widget_hide(type == COMPOSEBUTTON_NEWS ? toolbar->compose_mail_btn 
691                         : toolbar->compose_news_btn);
692         gtk_widget_show(type == COMPOSEBUTTON_NEWS ? toolbar->compose_news_btn
693                         : toolbar->compose_mail_btn);
694         toolbar->compose_btn_type = type;       
695 }
696
697 void toolbar_set_compose_button(Toolbar            *toolbar, 
698                                 ComposeButtonType  compose_btn_type)
699 {
700         if (toolbar->compose_btn_type != compose_btn_type)
701                 activate_compose_button(toolbar, 
702                                         prefs_common.toolbar_style,
703                                         compose_btn_type);
704 }
705
706 void toolbar_toggle(guint action, gpointer data)
707 {
708         MainWindow *mainwin = (MainWindow*)data;
709         GList *list;
710         GList *cur;
711
712         g_return_if_fail(mainwin != NULL);
713
714         toolbar_style(TOOLBAR_MAIN, action, mainwin);
715
716         list = compose_get_compose_list();
717         for (cur = list; cur != NULL; cur = cur->next) {
718                 toolbar_style(TOOLBAR_COMPOSE, action, cur->data);
719         }
720         list = messageview_get_msgview_list();
721         for (cur = list; cur != NULL; cur = cur->next) {
722                 toolbar_style(TOOLBAR_MSGVIEW, action, cur->data);
723         }
724         
725 }
726
727 void toolbar_set_style(GtkWidget *toolbar_wid, GtkWidget *handlebox_wid, guint action)
728 {
729         switch ((ToolbarStyle)action) {
730         case TOOLBAR_NONE:
731                 gtk_widget_hide(handlebox_wid);
732                 break;
733         case TOOLBAR_ICON:
734                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
735                                       GTK_TOOLBAR_ICONS);
736                 break;
737         case TOOLBAR_TEXT:
738                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
739                                       GTK_TOOLBAR_TEXT);
740                 break;
741         case TOOLBAR_BOTH:
742                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
743                                       GTK_TOOLBAR_BOTH);
744                 break;
745         default:
746                 return;
747         }
748
749         prefs_common.toolbar_style = (ToolbarStyle)action;
750         if (prefs_common.toolbar_style != TOOLBAR_NONE) {
751                 gtk_widget_show(handlebox_wid);
752                 gtk_widget_queue_resize(handlebox_wid);
753         }
754 }
755 /*
756  * Change the style of toolbar
757  */
758 static void toolbar_style(ToolbarType type, guint action, gpointer data)
759 {
760         GtkWidget  *handlebox_wid;
761         GtkWidget  *toolbar_wid;
762         MainWindow *mainwin = (MainWindow*)data;
763         Compose    *compose = (Compose*)data;
764         MessageView *msgview = (MessageView*)data;
765         
766         g_return_if_fail(data != NULL);
767         
768         switch (type) {
769         case TOOLBAR_MAIN:
770                 handlebox_wid = mainwin->handlebox;
771                 toolbar_wid = mainwin->toolbar->toolbar;
772                 break;
773         case TOOLBAR_COMPOSE:
774                 handlebox_wid = compose->handlebox;
775                 toolbar_wid = compose->toolbar->toolbar;
776                 break;
777         case TOOLBAR_MSGVIEW: 
778                 handlebox_wid = msgview->handlebox;
779                 toolbar_wid = msgview->toolbar->toolbar;
780                 break;
781         default:
782
783                 return;
784         }
785         toolbar_set_style(toolbar_wid, handlebox_wid, action);
786 }
787
788 /* Toolbar handling */
789 static void toolbar_inc_cb(GtkWidget    *widget,
790                            gpointer      data)
791 {
792         ToolbarItem *toolbar_item = (ToolbarItem*)data;
793         MainWindow *mainwin;
794
795         g_return_if_fail(toolbar_item != NULL);
796
797         switch (toolbar_item->type) {
798         case TOOLBAR_MAIN:
799                 mainwin = (MainWindow*)toolbar_item->parent;    
800                 inc_mail_cb(mainwin, 0, NULL);
801                 break;
802         default:
803                 break;
804         }
805 }
806
807 static void toolbar_inc_all_cb(GtkWidget        *widget,
808                                gpointer          data)
809 {
810         ToolbarItem *toolbar_item = (ToolbarItem*)data;
811         MainWindow *mainwin;
812
813         g_return_if_fail(toolbar_item != NULL);
814
815         switch (toolbar_item->type) {
816         case TOOLBAR_MAIN:
817                 mainwin = (MainWindow*)toolbar_item->parent;
818                 inc_all_account_mail_cb(mainwin, 0, NULL);
819                 break;
820         default:
821                 break;
822         }
823 }
824
825 static void toolbar_send_queued_cb(GtkWidget *widget,gpointer data)
826 {
827         ToolbarItem *toolbar_item = (ToolbarItem*)data;
828         MainWindow *mainwin;
829
830         g_return_if_fail(toolbar_item != NULL);
831
832         switch (toolbar_item->type) {
833         case TOOLBAR_MAIN:
834                 mainwin = (MainWindow*)toolbar_item->parent;
835                 send_queue_cb(mainwin, 0, NULL);
836                 break;
837         default:
838                 break;
839         }
840 }
841
842 static void toolbar_exec_cb(GtkWidget   *widget,
843                             gpointer     data)
844 {
845         MainWindow *mainwin = get_mainwin(data);
846
847         g_return_if_fail(mainwin != NULL);
848         summary_execute(mainwin->summaryview);
849 }
850
851
852
853 /* popup callback functions */
854 static void toolbar_reply_popup_cb(GtkWidget       *widget, 
855                                    GdkEventButton  *event, 
856                                    gpointer         data)
857 {
858         Toolbar *toolbar_data = (Toolbar*)data;
859         
860         if (!event) return;
861         
862         if (event->button == 3) {
863                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
864                 gtk_menu_popup(GTK_MENU(toolbar_data->reply_popup), NULL, NULL,
865                        menu_button_position, widget,
866                        event->button, event->time);
867         }
868 }
869
870 static void toolbar_reply_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
871 {
872         ToolbarItem *toolbar_item = (ToolbarItem*)data;
873         GtkWidget *window;
874         GtkWidget *reply_btn;
875         MainWindow *mainwin;
876         MessageView *msgview;
877
878         g_return_if_fail(toolbar_item != NULL);
879
880         switch(toolbar_item->type) {
881         case TOOLBAR_MAIN:
882                 mainwin   = (MainWindow*)toolbar_item->parent;
883                 reply_btn = mainwin->toolbar->reply_btn;
884                 window    = mainwin->window;
885                 break;
886         case TOOLBAR_MSGVIEW:
887                 msgview   = (MessageView*)toolbar_item->parent;
888                 reply_btn = msgview->toolbar->reply_btn;
889                 window    = msgview->window;
890                 break;
891         default:
892                 return;
893         }
894
895         gtk_button_set_relief(GTK_BUTTON(reply_btn), GTK_RELIEF_NONE);
896         manage_window_focus_in(window, NULL, NULL);
897 }
898
899 static void toolbar_reply_to_all_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
900 {
901         Toolbar *toolbar_data = (Toolbar*)data;
902         
903         if (!event) return;
904         
905         if (event->button == 3) {
906                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
907                 gtk_menu_popup(GTK_MENU(toolbar_data->replyall_popup), NULL, NULL,
908                        menu_button_position, widget,
909                        event->button, event->time);
910         }
911 }
912
913 static void toolbar_reply_to_all_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
914 {
915         ToolbarItem *toolbar_item = (ToolbarItem*)data;
916         GtkWidget *window;
917         GtkWidget *replyall_btn;
918         MainWindow *mainwin;
919         MessageView *msgview;
920
921         g_return_if_fail(toolbar_item != NULL);
922
923         switch(toolbar_item->type) {
924         case TOOLBAR_MAIN:
925                 mainwin      = (MainWindow*)toolbar_item->parent; 
926                 replyall_btn = mainwin->toolbar->replyall_btn;
927                 window       = mainwin->window;
928                 break;
929         case TOOLBAR_MSGVIEW:
930                 msgview      = (MessageView*)toolbar_item->parent;
931                 replyall_btn = msgview->toolbar->replyall_btn;
932                 window       = msgview->window;
933                 break;
934         default:
935                 return;
936         }
937
938         gtk_button_set_relief(GTK_BUTTON(replyall_btn), GTK_RELIEF_NONE);
939         manage_window_focus_in(window, NULL, NULL);
940 }
941
942 static void toolbar_reply_to_list_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
943 {
944         Toolbar *toolbar_data = (Toolbar*)data;
945
946         if (event->button == 3) {
947                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
948                 gtk_menu_popup(GTK_MENU(toolbar_data->replylist_popup), NULL, NULL,
949                        menu_button_position, widget,
950                        event->button, event->time);
951         }
952 }
953
954 static void toolbar_reply_to_list_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
955 {
956         ToolbarItem *toolbar_item = (ToolbarItem*)data;
957         gpointer parent = toolbar_item->parent;
958         GtkWidget *window;
959         GtkWidget *replylist_btn;
960
961
962         g_return_if_fail(toolbar_item != NULL);
963
964         switch(toolbar_item->type) {
965         case TOOLBAR_MAIN:
966                 replylist_btn = ((MainWindow*)parent)->toolbar->replylist_btn;
967                 window        = ((MainWindow*)parent)->window;
968                 break;
969         case TOOLBAR_MSGVIEW:
970                 replylist_btn = ((MessageView*)parent)->toolbar->replylist_btn;
971                 window        = ((MessageView*)parent)->window;
972                 break;
973         default:
974                 return;
975         }
976
977         gtk_button_set_relief(GTK_BUTTON(replylist_btn), GTK_RELIEF_NONE);
978         manage_window_focus_in(window, NULL, NULL);
979 }
980
981 static void toolbar_reply_to_sender_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
982 {
983         Toolbar *toolbar_data = (Toolbar*)data;
984
985         if (event->button == 3) {
986                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
987                 gtk_menu_popup(GTK_MENU(toolbar_data->replysender_popup), NULL, NULL,
988                        menu_button_position, widget,
989                        event->button, event->time);
990         }
991 }
992
993 static void toolbar_reply_to_sender_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
994 {
995         ToolbarItem *toolbar_item = (ToolbarItem*)data;
996         gpointer parent = toolbar_item->parent;
997         GtkWidget *window;
998         GtkWidget *replysender_btn;
999
1000         g_return_if_fail(toolbar_item != NULL);
1001
1002         switch(toolbar_item->type) {
1003         case TOOLBAR_MAIN:
1004                 replysender_btn = ((MainWindow*)parent)->toolbar->replysender_btn;
1005                 window          = ((MainWindow*)parent)->window;
1006                 break;
1007         case TOOLBAR_MSGVIEW:
1008                 replysender_btn = ((MessageView*)parent)->toolbar->replysender_btn;
1009                 window          = ((MessageView*)parent)->window;
1010                 break;
1011         default:
1012                 return;
1013         }
1014
1015         gtk_button_set_relief(GTK_BUTTON(replysender_btn), GTK_RELIEF_NONE);
1016         manage_window_focus_in(window, NULL, NULL);
1017 }
1018
1019 static void toolbar_forward_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
1020 {
1021         Toolbar *toolbar_data = (Toolbar*)data;
1022
1023         if (event->button == 3) {
1024                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
1025                 gtk_menu_popup(GTK_MENU(toolbar_data->fwd_popup), NULL, NULL,
1026                                menu_button_position, widget,
1027                                event->button, event->time);
1028         }
1029 }
1030
1031 static void toolbar_forward_popup_closed_cb (GtkMenuShell *menu_shell, 
1032                                              gpointer     data)
1033 {
1034         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1035         gpointer parent = toolbar_item->parent;
1036         GtkWidget *window;
1037         GtkWidget *fwd_btn;
1038
1039         g_return_if_fail(toolbar_item != NULL);
1040
1041         switch(toolbar_item->type) {
1042         case TOOLBAR_MAIN:
1043                 fwd_btn = ((MainWindow*)parent)->toolbar->fwd_btn;
1044                 window  = ((MainWindow*)parent)->window;
1045                 break;
1046         case TOOLBAR_MSGVIEW:
1047                 fwd_btn = ((MessageView*)parent)->toolbar->fwd_btn;
1048                 window  = ((MessageView*)parent)->window;
1049                 break;
1050         default:
1051                 return;
1052         }
1053
1054         gtk_button_set_relief(GTK_BUTTON(fwd_btn), GTK_RELIEF_NONE);
1055         manage_window_focus_in(window, NULL, NULL);
1056 }
1057
1058 /*
1059  * Delete current/selected(s) message(s)
1060  */
1061 static void toolbar_delete_cb(GtkWidget *widget, gpointer data)
1062 {
1063         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1064         MainWindow *mainwin;
1065         MessageView *msgview;
1066
1067         g_return_if_fail(toolbar_item != NULL);
1068         
1069         switch (toolbar_item->type) {
1070         case TOOLBAR_MSGVIEW:
1071                 msgview = (MessageView*)toolbar_item->parent;
1072                 /* make sure the selected msg in summaryview is 
1073                    the one we are asked to delete */
1074                 delete_msgview_cb(msgview, 0, widget);
1075                 break;
1076         case TOOLBAR_MAIN:
1077                 mainwin = (MainWindow*)toolbar_item->parent;
1078                 summary_delete(mainwin->summaryview);
1079                 break;
1080         default: 
1081                 debug_print("toolbar event not supported\n");
1082                 break;
1083         }
1084 }
1085
1086
1087 /*
1088  * Compose new message
1089  */
1090 static void toolbar_compose_cb(GtkWidget *widget, gpointer data)
1091 {
1092         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1093         MainWindow *mainwin;
1094         MessageView *msgview;
1095
1096         g_return_if_fail(toolbar_item != NULL);
1097
1098         switch (toolbar_item->type) {
1099         case TOOLBAR_MAIN:
1100                 mainwin = (MainWindow*)toolbar_item->parent;
1101                 if (mainwin->toolbar->compose_btn_type == COMPOSEBUTTON_NEWS) 
1102                         compose_news_cb(mainwin, 0, NULL);
1103                 else
1104                         compose_mail_cb(mainwin, 0, NULL);
1105                 break;
1106         case TOOLBAR_MSGVIEW:
1107                 msgview = (MessageView*)toolbar_item->parent;
1108                 compose_new_with_folderitem(NULL, 
1109                                             msgview->msginfo->folder);
1110                 break;  
1111         default:
1112                 debug_print("toolbar event not supported\n");
1113         }
1114 }
1115
1116 static void toolbar_popup_cb(gpointer data, guint action, GtkWidget *widget)
1117 {
1118         MainWindow *mainwin = get_mainwin(data);
1119
1120         g_return_if_fail(mainwin != NULL);
1121         reply_cb(mainwin, action, widget);
1122 }
1123
1124
1125 /*
1126  * Reply Message
1127  */
1128 static void toolbar_reply_cb(GtkWidget *widget, gpointer data)
1129 {
1130         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1131         MainWindow *mainwin;
1132         MessageView *msgview;
1133
1134         g_return_if_fail(toolbar_item != NULL);
1135
1136         switch (toolbar_item->type) {
1137         case TOOLBAR_MAIN:
1138                 mainwin = (MainWindow*)toolbar_item->parent;
1139                 if (prefs_common.default_reply_list)
1140                         reply_cb(mainwin, 
1141                                  prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_LIST_WITH_QUOTE 
1142                                  : COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE, 
1143                                  NULL);
1144                 else
1145                         reply_cb(mainwin, 
1146                                  prefs_common.reply_with_quote ? COMPOSE_REPLY_WITH_QUOTE 
1147                                  : COMPOSE_REPLY_WITHOUT_QUOTE,
1148                                  NULL);
1149                 break;
1150         case TOOLBAR_MSGVIEW:
1151                 msgview = (MessageView*)toolbar_item->parent;
1152                 compose_reply(msgview->msginfo,
1153                               prefs_common.reply_with_quote ? COMPOSE_REPLY_WITH_QUOTE 
1154                               : COMPOSE_REPLY_WITHOUT_QUOTE,
1155                               FALSE, FALSE, FALSE, NULL);
1156                 break;
1157         default:
1158                 debug_print("toolbar event not supported\n");
1159         }
1160 }
1161
1162
1163 /*
1164  * Reply message to Sender and All recipients
1165  */
1166 static void toolbar_reply_to_all_cb(GtkWidget *widget, gpointer data)
1167 {
1168         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1169         MainWindow *mainwin;
1170         MessageView *msgview;
1171
1172         g_return_if_fail(toolbar_item != NULL);
1173
1174         switch (toolbar_item->type) {
1175         case TOOLBAR_MAIN:
1176                 mainwin = (MainWindow*)toolbar_item->parent;
1177                 reply_cb(mainwin, 
1178                          prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_ALL_WITH_QUOTE 
1179                          : COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, 
1180                          NULL);
1181                 break;
1182         case TOOLBAR_MSGVIEW:
1183                 msgview = (MessageView*)toolbar_item->parent;
1184                 compose_reply(msgview->msginfo,
1185                               prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_ALL_WITH_QUOTE 
1186                               : COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE,
1187                               TRUE, FALSE, FALSE, NULL);
1188                 break;
1189         default:
1190                 debug_print("toolbar event not supported\n");
1191         }
1192 }
1193
1194
1195 /*
1196  * Reply to Mailing List
1197  */
1198 static void toolbar_reply_to_list_cb(GtkWidget *widget, gpointer data)
1199 {
1200         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1201         MainWindow *mainwin;
1202         MessageView *msgview;
1203
1204         g_return_if_fail(toolbar_item != NULL);
1205
1206         switch (toolbar_item->type) {
1207         case TOOLBAR_MAIN:
1208                 mainwin = (MainWindow*)toolbar_item->parent;
1209                 reply_cb(mainwin, 
1210                          prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_LIST_WITH_QUOTE 
1211                          : COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE, 
1212                          NULL);
1213                 break;
1214         case TOOLBAR_MSGVIEW:
1215                 msgview = (MessageView*)toolbar_item->parent;
1216                 compose_reply(msgview->msginfo,
1217                               prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_LIST_WITH_QUOTE 
1218                               : COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE,
1219                               FALSE, TRUE, FALSE, NULL);
1220                 break;
1221         default:
1222                 debug_print("toolbar event not supported\n");
1223         }
1224 }
1225
1226
1227 /*
1228  * Reply to sender of message
1229  */ 
1230 static void toolbar_reply_to_sender_cb(GtkWidget *widget, gpointer data)
1231 {
1232         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1233         MainWindow *mainwin;
1234         MessageView *msgview;
1235
1236         g_return_if_fail(toolbar_item != NULL);
1237
1238         switch (toolbar_item->type) {
1239         case TOOLBAR_MAIN:
1240                 mainwin = (MainWindow*)toolbar_item->parent;
1241                 reply_cb(mainwin, 
1242                          prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_SENDER_WITH_QUOTE 
1243                          : COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE, 
1244                          NULL);
1245                 break;
1246         case TOOLBAR_MSGVIEW:
1247                 msgview = (MessageView*)toolbar_item->parent;
1248                 compose_reply(msgview->msginfo,
1249                               prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_SENDER_WITH_QUOTE 
1250                               : COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE,
1251                               FALSE, FALSE, FALSE, NULL);
1252                 break;
1253         default:
1254                 debug_print("toolbar event not supported\n\n");
1255         }
1256 }
1257
1258 /*
1259  * Open addressbook
1260  */ 
1261 static void toolbar_addrbook_cb(GtkWidget *widget, gpointer data)
1262 {
1263         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1264         Compose *compose;
1265
1266         g_return_if_fail(toolbar_item != NULL);
1267
1268         switch (toolbar_item->type) {
1269         case TOOLBAR_MAIN:
1270         case TOOLBAR_MSGVIEW:
1271                 compose = NULL;
1272                 break;
1273         case TOOLBAR_COMPOSE:
1274                 compose = (Compose *)toolbar_item->parent;
1275                 break;
1276         default:
1277                 return;
1278         }
1279         addressbook_open(compose);
1280 }
1281
1282
1283 /*
1284  * Forward current/selected(s) message(s)
1285  */
1286 static void toolbar_forward_cb(GtkWidget *widget, gpointer data)
1287 {
1288         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1289         MainWindow *mainwin;
1290
1291         g_return_if_fail(toolbar_item != NULL);
1292
1293         switch (toolbar_item->type) {
1294         case TOOLBAR_MAIN:
1295                 mainwin = (MainWindow*)toolbar_item->parent;
1296                 if (prefs_common.forward_as_attachment)
1297                         reply_cb(mainwin, COMPOSE_FORWARD_AS_ATTACH, NULL);
1298                 else
1299                         reply_cb(mainwin, COMPOSE_FORWARD, NULL);
1300                 break;
1301                 
1302         default:
1303                 debug_print("toolbar event not supported\n");
1304         }
1305 }
1306
1307
1308 /*
1309  * Goto Next Unread Message
1310  */
1311 static void toolbar_next_unread_cb(GtkWidget *widget, gpointer data)
1312 {
1313         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1314         MainWindow *mainwin;
1315         MessageView *msgview;
1316
1317         g_return_if_fail(toolbar_item != NULL);
1318
1319         switch (toolbar_item->type) {
1320         case TOOLBAR_MAIN:
1321                 mainwin = (MainWindow*)toolbar_item->parent;
1322                 summary_select_next_unread(mainwin->summaryview);
1323                 break;
1324                 
1325         case TOOLBAR_MSGVIEW:
1326                 msgview = (MessageView*)toolbar_item->parent;
1327 /*
1328  * TODO: Check if summaryview stay in the same place when this message view was created 
1329  * if summary have other message select the next will be based on message selected in summaryview
1330  */
1331                 summary_select_next_unread(msgview->mainwin->summaryview);
1332                 
1333                 /** Now we need to update the messageview window */
1334                 if (msgview->mainwin->summaryview->selected) {
1335                         GtkCTree *ctree = GTK_CTREE(msgview->mainwin->summaryview->ctree);
1336                         
1337                         MsgInfo * msginfo = gtk_ctree_node_get_row_data(ctree, 
1338                                                                         msgview->mainwin->summaryview->selected);
1339                        
1340                         messageview_show(msgview, msginfo, 
1341                                          msgview->all_headers);
1342                 } else {
1343                         gtk_widget_destroy(msgview->window);
1344                 }
1345                 break;
1346         default:
1347                 debug_print("toolbar event not supported\n");
1348         }
1349 }
1350
1351 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
1352 {
1353         compose_toolbar_cb(A_SEND, data);
1354 }
1355
1356 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
1357 {
1358         compose_toolbar_cb(A_SENDL, data);
1359 }
1360
1361 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
1362 {
1363         compose_toolbar_cb(A_DRAFT, data);
1364 }
1365
1366 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
1367 {
1368         compose_toolbar_cb(A_INSERT, data);
1369 }
1370
1371 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
1372 {
1373         compose_toolbar_cb(A_ATTACH, data);
1374 }
1375
1376 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
1377 {
1378         compose_toolbar_cb(A_SIG, data);
1379 }
1380
1381 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
1382 {
1383         compose_toolbar_cb(A_EXTEDITOR, data);
1384 }
1385
1386 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
1387 {
1388         compose_toolbar_cb(A_LINEWRAP, data);
1389 }
1390
1391 /*
1392  * Execute actions from toolbar
1393  */
1394 static void toolbar_actions_execute_cb(GtkWidget *widget, gpointer data)
1395 {
1396         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1397         GSList *action_list;
1398         MainWindow *mainwin;
1399         Compose *compose;
1400         MessageView *msgview;
1401         gpointer parent = toolbar_item->parent;
1402
1403         g_return_if_fail(toolbar_item != NULL);
1404
1405         switch (toolbar_item->type) {
1406         case TOOLBAR_MAIN:
1407                 mainwin = (MainWindow*)parent;
1408                 action_list = mainwin->toolbar->action_list;
1409                 break;
1410         case TOOLBAR_COMPOSE:
1411                 compose = (Compose*)parent;
1412                 action_list = compose->toolbar->action_list;
1413                 break;
1414         case TOOLBAR_MSGVIEW:
1415                 msgview = (MessageView*)parent;
1416                 action_list = msgview->toolbar->action_list;
1417                 break;
1418         default:
1419                 debug_print("toolbar event not supported\n");
1420                 return;
1421         }
1422         toolbar_action_execute(widget, action_list, parent, toolbar_item->type);        
1423 }
1424
1425 static MainWindow *get_mainwin(gpointer data)
1426 {
1427         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1428         MainWindow *mainwin = NULL;
1429         MessageView *msgview;
1430
1431         g_return_val_if_fail(toolbar_item != NULL, NULL);
1432
1433         switch(toolbar_item->type) {
1434         case TOOLBAR_MAIN:
1435                 mainwin = (MainWindow*)toolbar_item->parent;
1436                 break;
1437         case TOOLBAR_MSGVIEW:
1438                 msgview = (MessageView*)toolbar_item->parent;
1439                 mainwin = (MainWindow*)msgview->mainwin;
1440                 break;
1441         default:
1442                 break;
1443         }
1444
1445         return mainwin;
1446 }
1447
1448 static GtkWidget *get_window_widget(ToolbarType type, gpointer data)
1449 {
1450         MainWindow *mainwin;
1451         MessageView *msgview;
1452
1453         switch (type) {
1454         case TOOLBAR_MAIN:
1455                 mainwin = (MainWindow*)data;
1456                 return mainwin->window;
1457                 break;
1458         case TOOLBAR_MSGVIEW:
1459                 msgview = (MessageView*)data;
1460                 return msgview->vbox;
1461                 break;
1462         default:
1463                 break;
1464         }
1465         return NULL;
1466 }
1467
1468 static void toolbar_buttons_cb(GtkWidget   *widget, 
1469                                       ToolbarItem *item)
1470 {
1471         gint num_items;
1472         gint i;
1473         struct {
1474                 gint   index;
1475                 void (*func)(GtkWidget *widget, gpointer data);
1476         } callbacks[] = {
1477                 { A_RECEIVE_ALL,        toolbar_inc_all_cb              },
1478                 { A_RECEIVE_CUR,        toolbar_inc_cb                  },
1479                 { A_SEND_QUEUED,        toolbar_send_queued_cb          },
1480                 { A_COMPOSE_EMAIL,      toolbar_compose_cb              },
1481                 { A_COMPOSE_NEWS,       toolbar_compose_cb              },
1482                 { A_REPLY_MESSAGE,      toolbar_reply_cb                },
1483                 { A_REPLY_SENDER,       toolbar_reply_to_sender_cb      },
1484                 { A_REPLY_ALL,          toolbar_reply_to_all_cb         },
1485                 { A_REPLY_ML,           toolbar_reply_to_list_cb        },
1486                 { A_FORWARD,            toolbar_forward_cb              },
1487                 { A_DELETE,             toolbar_delete_cb               },
1488                 { A_EXECUTE,            toolbar_exec_cb                 },
1489                 { A_GOTO_NEXT,          toolbar_next_unread_cb          },
1490
1491                 { A_SEND,               toolbar_send_cb                 },
1492                 { A_SENDL,              toolbar_send_later_cb           },
1493                 { A_DRAFT,              toolbar_draft_cb                },
1494                 { A_INSERT,             toolbar_insert_cb               },
1495                 { A_ATTACH,             toolbar_attach_cb               },
1496                 { A_SIG,                toolbar_sig_cb                  },
1497                 { A_EXTEDITOR,          toolbar_ext_editor_cb           },
1498                 { A_LINEWRAP,           toolbar_linewrap_cb             },
1499                 { A_ADDRBOOK,           toolbar_addrbook_cb             },
1500
1501                 { A_SYL_ACTIONS,        toolbar_actions_execute_cb      }
1502         };
1503
1504         num_items = sizeof(callbacks)/sizeof(callbacks[0]);
1505
1506         for (i = 0; i < num_items; i++) {
1507                 if (callbacks[i].index == item->index) {
1508                         callbacks[i].func(widget, item);
1509                         return;
1510                 }
1511         }
1512 }
1513
1514 /**
1515  * Create a new toolbar with specified type
1516  * if a callback list is passed it will be used before the 
1517  * common callback list
1518  **/
1519 Toolbar *toolbar_create(ToolbarType      type, 
1520                         GtkWidget       *container,
1521                         gpointer         data)
1522 {
1523         ToolbarItem *toolbar_item;
1524
1525         GtkWidget *toolbar;
1526         GtkWidget *icon_wid = NULL;
1527         GtkWidget *icon_news;
1528         GtkWidget *item;
1529         GtkWidget *item_news;
1530         GtkWidget *window_wid;
1531
1532         guint n_menu_entries;
1533         GtkWidget *reply_popup;
1534         GtkWidget *replyall_popup;
1535         GtkWidget *replylist_popup;
1536         GtkWidget *replysender_popup;
1537         GtkWidget *fwd_popup;
1538
1539         GtkTooltips *toolbar_tips;
1540         ToolbarSylpheedActions *action_item;
1541         GSList *cur;
1542         GSList *toolbar_list;
1543         Toolbar *toolbar_data;
1544
1545         
1546         toolbar_tips = gtk_tooltips_new();
1547         
1548         toolbar_read_config_file(type);
1549         toolbar_list = toolbar_get_list(type);
1550
1551         toolbar_data = g_new0(Toolbar, 1); 
1552
1553         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
1554                                   GTK_TOOLBAR_BOTH);
1555         gtk_container_add(GTK_CONTAINER(container), toolbar);
1556         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
1557         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
1558         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
1559                                     GTK_TOOLBAR_SPACE_LINE);
1560         
1561         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
1562
1563                 if (g_strcasecmp(((ToolbarItem*)cur->data)->file, TOOLBAR_TAG_SEPARATOR) == 0) {
1564                         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
1565                         continue;
1566                 }
1567                 
1568                 toolbar_item = g_new0(ToolbarItem, 1); 
1569                 toolbar_item->index = ((ToolbarItem*)cur->data)->index;
1570                 toolbar_item->file = g_strdup(((ToolbarItem*)cur->data)->file);
1571                 toolbar_item->text = g_strdup(((ToolbarItem*)cur->data)->text);
1572                 toolbar_item->parent = data;
1573                 toolbar_item->type = type;
1574
1575                 /* collect toolbar items in list to keep track */
1576                 toolbar_data->item_list = 
1577                         g_slist_append(toolbar_data->item_list, 
1578                                        toolbar_item);
1579
1580                 icon_wid = stock_pixmap_widget(container, stock_pixmap_get_icon(toolbar_item->file));
1581                 item  = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1582                                                 toolbar_item->text,
1583                                                 (""),
1584                                                 (""),
1585                                                 icon_wid, toolbar_buttons_cb, 
1586                                                 toolbar_item);
1587                 
1588                 switch (toolbar_item->index) {
1589
1590                 case A_RECEIVE_ALL:
1591                         toolbar_data->getall_btn = item;
1592                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1593                                              toolbar_data->getall_btn, 
1594                                            _("Receive Mail on all Accounts"), NULL);
1595                         break;
1596                 case A_RECEIVE_CUR:
1597                         toolbar_data->get_btn = item;
1598                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1599                                              toolbar_data->get_btn,
1600                                            _("Receive Mail on current Account"), NULL);
1601                         break;
1602                 case A_SEND_QUEUED:
1603                         toolbar_data->send_btn = item; 
1604                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1605                                              toolbar_data->send_btn,
1606                                            _("Send Queued Message(s)"), NULL);
1607                         break;
1608                 case A_COMPOSE_EMAIL:
1609                         icon_news = stock_pixmap_widget(container, STOCK_PIXMAP_NEWS_COMPOSE);
1610                         item_news = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1611                                                             _("News"),
1612                                                             (""),
1613                                                             (""),
1614                                                             icon_news, toolbar_buttons_cb, 
1615                                                             toolbar_item);
1616                         toolbar_data->compose_mail_btn = item; 
1617                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1618                                              toolbar_data->compose_mail_btn,
1619                                            _("Compose Email"), NULL);
1620                         toolbar_data->compose_news_btn = item_news;
1621                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1622                                              toolbar_data->compose_news_btn,
1623                                            _("Compose News"), NULL);
1624                         break;
1625                 case A_REPLY_MESSAGE:
1626                         toolbar_data->reply_btn = item;
1627                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1628                                              toolbar_data->reply_btn,
1629                                            _("Reply to Message"), NULL);
1630                         gtk_signal_connect(GTK_OBJECT(toolbar_data->reply_btn), 
1631                                            "button_press_event",
1632                                            GTK_SIGNAL_FUNC(toolbar_reply_popup_cb),
1633                                            toolbar_data);
1634                         n_menu_entries = sizeof(reply_popup_entries) /
1635                                 sizeof(reply_popup_entries[0]);
1636
1637                         window_wid = get_window_widget(type, data);
1638                         reply_popup = popupmenu_create(window_wid,
1639                                                        reply_popup_entries, n_menu_entries,
1640                                                        "<ReplyPopup>", (gpointer)toolbar_item);
1641
1642                         gtk_signal_connect(GTK_OBJECT(reply_popup), "selection_done",
1643                                            GTK_SIGNAL_FUNC(toolbar_reply_popup_closed_cb), toolbar_item);
1644                         toolbar_data->reply_popup = reply_popup;
1645                         break;
1646                 case A_REPLY_SENDER:
1647                         toolbar_data->replysender_btn = item;
1648                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1649                                              toolbar_data->replysender_btn,
1650                                            _("Reply to Sender"), NULL);
1651                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replysender_btn), 
1652                                            "button_press_event",
1653                                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_cb),
1654                                            toolbar_data);
1655                         n_menu_entries = sizeof(replysender_popup_entries) /
1656                                 sizeof(replysender_popup_entries[0]);
1657
1658                         window_wid = get_window_widget(type, data);
1659                         replysender_popup = popupmenu_create(window_wid, 
1660                                                              replysender_popup_entries, n_menu_entries,
1661                                                              "<ReplySenderPopup>", (gpointer)toolbar_item);
1662
1663                         gtk_signal_connect(GTK_OBJECT(replysender_popup), "selection_done",
1664                                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_closed_cb), toolbar_item);
1665                         toolbar_data->replysender_popup = replysender_popup;
1666                         break;
1667                 case A_REPLY_ALL:
1668                         toolbar_data->replyall_btn = item;
1669                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1670                                              toolbar_data->replyall_btn,
1671                                            _("Reply to All"), NULL);
1672                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replyall_btn), 
1673                                            "button_press_event",
1674                                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_cb),
1675                                            toolbar_data);
1676                         n_menu_entries = sizeof(replyall_popup_entries) /
1677                                 sizeof(replyall_popup_entries[0]);
1678
1679                         window_wid = get_window_widget(type, data);     
1680                         replyall_popup = popupmenu_create(window_wid, 
1681                                                           replyall_popup_entries, n_menu_entries,
1682                                                           "<ReplyAllPopup>", (gpointer)toolbar_item);
1683         
1684                         gtk_signal_connect(GTK_OBJECT(replyall_popup), "selection_done",
1685                                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_closed_cb), toolbar_item);
1686                         toolbar_data->replyall_popup = replyall_popup;
1687                         break;
1688                 case A_REPLY_ML:
1689                         toolbar_data->replylist_btn = item;
1690                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1691                                              toolbar_data->replylist_btn,
1692                                            _("Reply to Mailing-list"), NULL);
1693                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replylist_btn), 
1694                                            "button_press_event",
1695                                            GTK_SIGNAL_FUNC(toolbar_reply_to_list_popup_cb),
1696                                            toolbar_data);
1697                         n_menu_entries = sizeof(replylist_popup_entries) /
1698                                 sizeof(replylist_popup_entries[0]);
1699
1700                         window_wid = get_window_widget(type, data);
1701                         replylist_popup = popupmenu_create(window_wid, 
1702                                                            replylist_popup_entries, n_menu_entries,
1703                                                            "<ReplyMlPopup>", (gpointer)toolbar_item);
1704                 
1705                         gtk_signal_connect(GTK_OBJECT(replylist_popup), "selection_done",
1706                                            GTK_SIGNAL_FUNC(toolbar_reply_to_list_popup_closed_cb), toolbar_item);
1707                         toolbar_data->replylist_popup = replylist_popup;
1708                         break;
1709                 case A_FORWARD:
1710                         toolbar_data->fwd_btn = item;
1711                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1712                                              toolbar_data->fwd_btn,
1713                                              _("Forward Message"), NULL);
1714                         gtk_signal_connect(GTK_OBJECT(toolbar_data->fwd_btn), 
1715                                            "button_press_event",
1716                                            GTK_SIGNAL_FUNC(toolbar_forward_popup_cb),
1717                                            toolbar_data);
1718                         n_menu_entries = sizeof(fwd_popup_entries) /
1719                                 sizeof(fwd_popup_entries[0]);
1720
1721                         window_wid = get_window_widget(type, data);
1722                         fwd_popup = popupmenu_create(window_wid, 
1723                                                      fwd_popup_entries, n_menu_entries,
1724                                                      "<ForwardPopup>", (gpointer)toolbar_item);
1725
1726                         gtk_signal_connect(GTK_OBJECT(fwd_popup), "selection_done",
1727                                            GTK_SIGNAL_FUNC(toolbar_forward_popup_closed_cb), toolbar_item);
1728                         toolbar_data->fwd_popup = fwd_popup;
1729                         break;
1730                 case A_DELETE:
1731                         toolbar_data->delete_btn = item;
1732                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1733                                              toolbar_data->delete_btn,
1734                                              _("Delete Message"), NULL);
1735                         break;
1736                 case A_EXECUTE:
1737                         toolbar_data->exec_btn = item;
1738                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1739                                              toolbar_data->exec_btn,
1740                                            _("Execute"), NULL);
1741                         break;
1742                 case A_GOTO_NEXT:
1743                         toolbar_data->next_btn = item;
1744                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1745                                              toolbar_data->next_btn,
1746                                              _("Goto Next Message"), NULL);
1747                         break;
1748                 
1749                 /* Compose Toolbar */
1750                 case A_SEND:
1751                         toolbar_data->send_btn = item;
1752                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1753                                              toolbar_data->send_btn, 
1754                                              _("Send Message"), NULL);
1755                         break;
1756                 case A_SENDL:
1757                         toolbar_data->sendl_btn = item;
1758                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1759                                              toolbar_data->sendl_btn,
1760                                              _("Put into queue folder and send later"), NULL);
1761                         break;
1762                 case A_DRAFT:
1763                         toolbar_data->draft_btn = item; 
1764                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1765                                              toolbar_data->draft_btn,
1766                                              _("Save to draft folder"), NULL);
1767                         break;
1768                 case A_INSERT:
1769                         toolbar_data->insert_btn = item; 
1770                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1771                                              toolbar_data->insert_btn,
1772                                              _("Insert file"), NULL);
1773                         break;
1774                 case A_ATTACH:
1775                         toolbar_data->attach_btn = item;
1776                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1777                                              toolbar_data->attach_btn,
1778                                              _("Attach file"), NULL);
1779                         break;
1780                 case A_SIG:
1781                         toolbar_data->sig_btn = item;
1782                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1783                                              toolbar_data->sig_btn,
1784                                              _("Insert signature"), NULL);
1785                         break;
1786                 case A_EXTEDITOR:
1787                         toolbar_data->exteditor_btn = item;
1788                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1789                                              toolbar_data->exteditor_btn,
1790                                              _("Edit with external editor"), NULL);
1791                         break;
1792                 case A_LINEWRAP:
1793                         toolbar_data->linewrap_btn = item;
1794                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1795                                              toolbar_data->linewrap_btn,
1796                                              _("Wrap all long lines"), NULL);
1797                         break;
1798                 case A_ADDRBOOK:
1799                         toolbar_data->addrbook_btn = item;
1800                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1801                                              toolbar_data->addrbook_btn,
1802                                              _("Address book"), NULL);
1803                         break;
1804
1805                 case A_SYL_ACTIONS:
1806                         action_item = g_new0(ToolbarSylpheedActions, 1);
1807                         action_item->widget = item;
1808                         action_item->name   = g_strdup(toolbar_item->text);
1809
1810                         toolbar_data->action_list = 
1811                                 g_slist_append(toolbar_data->action_list,
1812                                                action_item);
1813
1814                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1815                                              item,
1816                                              action_item->name, NULL);
1817
1818                         gtk_widget_show(item);
1819                         break;
1820                 default:
1821                         break;
1822                 }
1823
1824         }
1825         toolbar_data->toolbar = toolbar;
1826         if (type == TOOLBAR_MAIN)
1827                 activate_compose_button(toolbar_data, 
1828                                         prefs_common.toolbar_style, 
1829                                         toolbar_data->compose_btn_type);
1830         
1831         gtk_widget_show_all(toolbar);
1832         
1833         return toolbar_data; 
1834 }
1835
1836 /**
1837  * Free toolbar structures
1838  */ 
1839 void toolbar_destroy(Toolbar * toolbar) {
1840
1841         TOOLBAR_DESTROY_ITEMS(toolbar->item_list);      
1842         TOOLBAR_DESTROY_ACTIONS(toolbar->action_list);
1843 }
1844
1845 void toolbar_update(ToolbarType type, gpointer data)
1846 {
1847         Toolbar *toolbar_data;
1848         GtkWidget *handlebox;
1849         MainWindow *mainwin = (MainWindow*)data;
1850         Compose    *compose = (Compose*)data;
1851         MessageView *msgview = (MessageView*)data;
1852
1853         switch(type) {
1854         case TOOLBAR_MAIN:
1855                 toolbar_data = mainwin->toolbar;
1856                 handlebox    = mainwin->handlebox;
1857                 break;
1858         case TOOLBAR_COMPOSE:
1859                 toolbar_data = compose->toolbar;
1860                 handlebox    = compose->handlebox;
1861                 break;
1862         case TOOLBAR_MSGVIEW:
1863                 toolbar_data = msgview->toolbar;
1864                 handlebox    = msgview->handlebox;
1865                 break;
1866         default:
1867                 return;
1868         }
1869
1870         gtk_container_remove(GTK_CONTAINER(handlebox), 
1871                              GTK_WIDGET(toolbar_data->toolbar));
1872
1873         toolbar_init(toolbar_data);
1874         toolbar_data = toolbar_create(type, handlebox, data);
1875         switch(type) {
1876         case TOOLBAR_MAIN:
1877                 mainwin->toolbar = toolbar_data;
1878                 break;
1879         case TOOLBAR_COMPOSE:
1880                 compose->toolbar = toolbar_data;
1881                 break;
1882         case TOOLBAR_MSGVIEW:
1883                 msgview->toolbar = toolbar_data;
1884                 break;
1885         }
1886
1887         toolbar_style(type, prefs_common.toolbar_style, data);
1888
1889         if (type == TOOLBAR_MAIN)
1890                 toolbar_main_set_sensitive((MainWindow*)data);
1891 }
1892
1893 void toolbar_main_set_sensitive(gpointer data)
1894 {
1895         SensitiveCond state;
1896         gboolean sensitive;
1897         MainWindow *mainwin = (MainWindow*)data;
1898         Toolbar *toolbar = mainwin->toolbar;
1899         GSList *cur;
1900         GSList *entry_list = NULL;
1901         
1902         typedef struct _Entry Entry;
1903         struct _Entry {
1904                 GtkWidget *widget;
1905                 SensitiveCond cond;
1906                 gboolean empty;
1907         };
1908
1909 #define SET_WIDGET_COND(w, c)     \
1910 { \
1911         Entry *e = g_new0(Entry, 1); \
1912         e->widget = w; \
1913         e->cond   = c; \
1914         entry_list = g_slist_append(entry_list, e); \
1915 }
1916
1917         SET_WIDGET_COND(toolbar->get_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1918         SET_WIDGET_COND(toolbar->getall_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1919         SET_WIDGET_COND(toolbar->compose_news_btn, M_HAVE_ACCOUNT);
1920         SET_WIDGET_COND(toolbar->reply_btn,
1921                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1922         SET_WIDGET_COND(toolbar->replyall_btn,
1923                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1924         SET_WIDGET_COND(toolbar->replylist_btn,
1925                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1926         SET_WIDGET_COND(toolbar->replysender_btn,
1927                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1928         SET_WIDGET_COND(toolbar->fwd_btn, M_HAVE_ACCOUNT|M_TARGET_EXIST);
1929
1930         SET_WIDGET_COND(toolbar->next_btn, M_MSG_EXIST);
1931         SET_WIDGET_COND(toolbar->delete_btn,
1932                         M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED);
1933         SET_WIDGET_COND(toolbar->exec_btn, M_DELAY_EXEC);
1934
1935         for (cur = toolbar->action_list; cur != NULL;  cur = cur->next) {
1936                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
1937                 
1938                 SET_WIDGET_COND(act->widget, M_TARGET_EXIST|M_UNLOCKED);
1939         }
1940
1941 #undef SET_WIDGET_COND
1942
1943         state = main_window_get_current_state(mainwin);
1944
1945         for (cur = entry_list; cur != NULL; cur = cur->next) {
1946                 Entry *e = (Entry*) cur->data;
1947
1948                 if (e->widget != NULL) {
1949                         sensitive = ((e->cond & state) == e->cond);
1950                         gtk_widget_set_sensitive(e->widget, sensitive); 
1951                 }
1952         }
1953         
1954         while (entry_list != NULL) {
1955                 Entry *e = (Entry*) entry_list->data;
1956
1957                 if (e)
1958                         g_free(e);
1959                 entry_list = g_slist_remove(entry_list, e);
1960         }
1961
1962         g_slist_free(entry_list);
1963
1964         activate_compose_button(toolbar, 
1965                                 prefs_common.toolbar_style,
1966                                 toolbar->compose_btn_type);
1967 }
1968
1969 void toolbar_comp_set_sensitive(gpointer data, gboolean sensitive)
1970 {
1971         Compose *compose = (Compose*)data;
1972         GSList *items = compose->toolbar->action_list;
1973
1974         if (compose->toolbar->send_btn)
1975                 gtk_widget_set_sensitive(compose->toolbar->send_btn, sensitive);
1976         if (compose->toolbar->sendl_btn)
1977                 gtk_widget_set_sensitive(compose->toolbar->sendl_btn, sensitive);
1978         if (compose->toolbar->draft_btn )
1979                 gtk_widget_set_sensitive(compose->toolbar->draft_btn , sensitive);
1980         if (compose->toolbar->insert_btn )
1981                 gtk_widget_set_sensitive(compose->toolbar->insert_btn , sensitive);
1982         if (compose->toolbar->attach_btn)
1983                 gtk_widget_set_sensitive(compose->toolbar->attach_btn, sensitive);
1984         if (compose->toolbar->sig_btn)
1985                 gtk_widget_set_sensitive(compose->toolbar->sig_btn, sensitive);
1986         if (compose->toolbar->exteditor_btn)
1987                 gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, sensitive);
1988         if (compose->toolbar->linewrap_btn)
1989                 gtk_widget_set_sensitive(compose->toolbar->linewrap_btn, sensitive);
1990         if (compose->toolbar->addrbook_btn)
1991                 gtk_widget_set_sensitive(compose->toolbar->addrbook_btn, sensitive);
1992         for (; items != NULL; items = g_slist_next(items)) {
1993                 ToolbarSylpheedActions *item = (ToolbarSylpheedActions *)items->data;
1994                 gtk_widget_set_sensitive(item->widget, sensitive);
1995         }
1996 }
1997
1998 /**
1999  * Initialize toolbar structure
2000  **/
2001 void toolbar_init(Toolbar * toolbar) {
2002
2003         toolbar->toolbar          = NULL;
2004         toolbar->get_btn          = NULL;
2005         toolbar->getall_btn       = NULL;
2006         toolbar->send_btn         = NULL;
2007         toolbar->compose_mail_btn = NULL;
2008         toolbar->compose_news_btn = NULL;
2009         toolbar->reply_btn        = NULL;
2010         toolbar->replysender_btn  = NULL;
2011         toolbar->replyall_btn     = NULL;
2012         toolbar->replylist_btn    = NULL;
2013         toolbar->fwd_btn          = NULL;
2014         toolbar->delete_btn       = NULL;
2015         toolbar->next_btn         = NULL;
2016         toolbar->exec_btn         = NULL;
2017
2018         /* compose buttons */ 
2019         toolbar->sendl_btn        = NULL;
2020         toolbar->draft_btn        = NULL;
2021         toolbar->insert_btn       = NULL;
2022         toolbar->attach_btn       = NULL;
2023         toolbar->sig_btn          = NULL;       
2024         toolbar->exteditor_btn    = NULL;       
2025         toolbar->linewrap_btn     = NULL;       
2026         toolbar->addrbook_btn     = NULL;       
2027
2028         toolbar_destroy(toolbar);
2029 }
2030
2031 /* exported functions */
2032
2033 void delete_msgview_cb(gpointer data, guint action, GtkWidget *widget)
2034 {
2035         MessageView *msgview = (MessageView*)data;
2036
2037         if (msgview->mainwin->summaryview->selected) {
2038                 SummaryView *summaryview = msgview->mainwin->summaryview;
2039                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2040                 MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, 
2041                                                                summaryview->selected);  
2042                 if (msginfo->msgnum != msgview->msginfo->msgnum) {
2043                         alertpanel_error(_("Message already removed from folder."));
2044                         return;
2045                 }
2046         }
2047         
2048         summary_delete(msgview->mainwin->summaryview);  
2049         
2050         if (msgview->mainwin->summaryview->selected) {
2051                 SummaryView *summaryview = msgview->mainwin->summaryview;
2052                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2053                 MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, 
2054                                                                summaryview->selected);
2055                 messageview_show(msgview, msginfo, 
2056                                  msgview->all_headers);
2057         } else {
2058                 toolbar_clear_list(TOOLBAR_MSGVIEW);
2059                 toolbar_destroy(msgview->toolbar);
2060                 gtk_widget_destroy(msgview->window);
2061         }       
2062 }
2063
2064 void reply_cb(gpointer data, guint action, GtkWidget *widget)
2065 {
2066         MainWindow *mainwin = (MainWindow*)data;
2067
2068         g_return_if_fail(mainwin != NULL);
2069
2070         summary_reply(mainwin->summaryview, (ComposeMode)action);
2071 }
2072
2073 void inc_mail_cb(gpointer data, guint action, GtkWidget *widget)
2074 {
2075         MainWindow *mainwin = (MainWindow*)data;
2076
2077         inc_mail(mainwin, prefs_common.newmail_notify_manu);
2078 }
2079
2080 void inc_all_account_mail_cb(gpointer data, guint action, GtkWidget *widget)
2081 {
2082         MainWindow *mainwin = (MainWindow*)data;
2083
2084         inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
2085 }
2086
2087 void send_queue_cb(gpointer data, guint action, GtkWidget *widget)
2088 {
2089         GList *list;
2090
2091         if (prefs_common.work_offline)
2092                 if (alertpanel(_("Offline warning"), 
2093                                _("You're working offline. Override?"),
2094                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
2095                 return;
2096
2097         for (list = folder_get_list(); list != NULL; list = list->next) {
2098                 Folder *folder = list->data;
2099
2100                 if (folder->queue) {
2101                         if (procmsg_send_queue
2102                                 (folder->queue, prefs_common.savemsg) < 0)
2103                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
2104                         folder_item_scan(folder->queue);
2105                         folder_update_item(folder->queue, TRUE);
2106                 }
2107         }
2108 }
2109
2110 void compose_mail_cb(gpointer data, guint action, GtkWidget *widget)
2111 {
2112         MainWindow *mainwin = (MainWindow*)data;
2113         PrefsAccount *ac = NULL;
2114         FolderItem *item = mainwin->summaryview->folder_item;   
2115         GList * list;
2116         GList * cur;
2117
2118         if (item) {
2119                 ac = account_find_from_item(item);
2120                 if (ac && ac->protocol != A_NNTP) {
2121                         compose_new_with_folderitem(ac, item);          /* CLAWS */
2122                         return;
2123                 }
2124         }
2125
2126         /*
2127          * CLAWS - use current account
2128          */
2129         if (cur_account && (cur_account->protocol != A_NNTP)) {
2130                 compose_new_with_folderitem(cur_account, item);
2131                 return;
2132         }
2133
2134         /*
2135          * CLAWS - just get the first one
2136          */
2137         list = account_get_list();
2138         for (cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2139                 ac = (PrefsAccount *) cur->data;
2140                 if (ac->protocol != A_NNTP) {
2141                         compose_new_with_folderitem(ac, item);
2142                         return;
2143                 }
2144         }
2145 }
2146
2147 void compose_news_cb(gpointer data, guint action, GtkWidget *widget)
2148 {
2149         MainWindow *mainwin = (MainWindow*)data;
2150         PrefsAccount * ac = NULL;
2151         GList * list;
2152         GList * cur;
2153
2154         if (mainwin->summaryview->folder_item) {
2155                 ac = mainwin->summaryview->folder_item->folder->account;
2156                 if (ac && ac->protocol == A_NNTP) {
2157                         compose_new(ac,
2158                                     mainwin->summaryview->folder_item->path,
2159                                     NULL);
2160                         return;
2161                 }
2162         }
2163
2164         list = account_get_list();
2165         for(cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2166                 ac = (PrefsAccount *) cur->data;
2167                 if (ac->protocol == A_NNTP) {
2168                         compose_new(ac, NULL, NULL);
2169                         return;
2170                 }
2171         }
2172 }