msgview stays open if msgs are deleted
[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 "utils.h"
40 #include "xml.h"
41 #include "mgutils.h"
42 #include "prefs.h"
43 #include "codeconv.h"
44 #include "stock_pixmap.h"
45 #include "mainwindow.h"
46 #include "messageview.h"
47 #include "prefs_common.h"
48 #include "menu.h"
49 #include "prefs_actions.h"
50 #include "manage_window.h"
51 #include "gtkutils.h"
52 #include "toolbar.h"
53 #include "prefs_toolbar.h"
54
55 /* elements */
56 #define TOOLBAR_TAG_INDEX        "toolbar"
57 #define TOOLBAR_TAG_ITEM         "item"
58 #define TOOLBAR_TAG_SEPARATOR    SEPARATOR
59
60 #define TOOLBAR_ICON_FILE   "file"    
61 #define TOOLBAR_ICON_TEXT   "text"     
62 #define TOOLBAR_ICON_ACTION "action"    
63
64 gboolean      toolbar_is_duplicate           (gint              action,
65                                               ToolbarType       source);
66 static void   toolbar_parse_item             (XMLFile           *file,
67                                               ToolbarType       source);
68
69 static gint   toolbar_ret_val_from_text      (const gchar       *text);
70 static gchar *toolbar_ret_text_from_val      (gint              val);
71
72 static void   toolbar_set_default_main       (void);
73 static void   toolbar_set_default_compose    (void);
74 static void   toolbar_set_default_msgview    (void);
75
76 static ToolbarType detect_window(gpointer data);
77
78 struct ToolbarText 
79 {
80         gchar *index_str;
81         const gchar *descr;
82 };
83 /* 
84  *  Text Database linked to enumarated values in toolbar.h 
85  */
86 static struct ToolbarText toolbar_text [] = {
87         
88         { "A_RECEIVE_ALL",   N_("Receive Mail on all Accounts")         },
89         { "A_RECEIVE_CUR",   N_("Receive Mail on current Account")      },
90         { "A_SEND_QUEUED",   N_("Send Queued Message(s)")               },
91         { "A_COMPOSE_EMAIL", N_("Compose Email")                        },
92         { "A_COMPOSE_NEWS",  N_("Compose News")                         },
93         { "A_REPLY_MESSAGE", N_("Reply to Message")                     },
94         { "A_REPLY_SENDER",  N_("Reply to Sender")                      },
95         { "A_REPLY_ALL",     N_("Reply to All")                         },
96         { "A_REPLY_ML",      N_("Reply to Mailing-list")                },
97         { "A_FORWARD",       N_("Forward Message")                      }, 
98         { "A_DELETE",        N_("Delete Message")                       },
99         { "A_EXECUTE",       N_("Execute")                              },
100         { "A_GOTO_NEXT",     N_("Goto Next Message")                    },
101
102         { "A_SEND",          N_("Send Message")                         },
103         { "A_SENDL",         N_("Put into queue folder and send later") },
104         { "A_DRAFT",         N_("Save to draft folder")                 },
105         { "A_INSERT",        N_("Insert file")                          },   
106         { "A_ATTACH",        N_("Attach file")                          },
107         { "A_SIG",           N_("Insert signature")                     },
108         { "A_EXTEDITOR",     N_("Edit with external editor")            },
109         { "A_LINEWRAP",      N_("Wrap all long lines")                  }, 
110         { "A_ADDRBOOK",      N_("Address book")                         },
111
112         { "A_SYL_ACTIONS",   N_("Sylpheed Actions Feature")             }, 
113         { "A_SEPARATOR",     ("")                                       }
114 };
115
116 /* struct holds configuration files and a list of
117  * currently active toolbar items 
118  * TOOLBAR_MAIN, TOOLBAR_COMPOSE and TOOLBAR_MSGVIEW
119  * give us an index
120  */
121 static ToolbarConfig toolbar_config[3] = {
122         { "toolbar_main.xml",    NULL},
123         { "toolbar_compose.xml", NULL}, 
124         { "toolbar_msgview.xml", NULL}
125 };
126
127 gint toolbar_ret_val_from_descr(const gchar *descr)
128 {
129         gint i;
130
131         for (i = 0; i < N_ACTION_VAL; i++) {
132                 if (g_strcasecmp(gettext(toolbar_text[i].descr), descr) == 0)
133                                 return i;
134         }
135         
136         return -1;
137 }
138
139 gchar *toolbar_ret_descr_from_val(gint val)
140 {
141         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
142
143         return gettext(toolbar_text[val].descr);
144 }
145
146 static gint toolbar_ret_val_from_text(const gchar *text)
147 {
148         gint i;
149         
150         for (i = 0; i < N_ACTION_VAL; i++) {
151                 if (g_strcasecmp(toolbar_text[i].index_str, text) == 0)
152                                 return i;
153         }
154         
155         return -1;
156 }
157
158 static gchar *toolbar_ret_text_from_val(gint val)
159 {
160         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
161
162         return toolbar_text[val].index_str;
163 }
164
165 gboolean toolbar_is_duplicate(gint action, ToolbarType source)
166 {
167         GSList *cur;
168
169         if ((action == A_SEPARATOR) || (action == A_SYL_ACTIONS)) 
170                 return FALSE;
171
172         for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
173                 ToolbarItem *item = (ToolbarItem*) cur->data;
174                 
175                 if (item->index == action)
176                         return TRUE;
177         }
178         return FALSE;
179 }
180
181 GList *toolbar_get_action_items(ToolbarType source)
182 {
183         GList *items = NULL;
184         gint i = 0;
185         
186         if (source == TOOLBAR_MAIN) {
187                 gint main_items[13] = { A_RECEIVE_ALL,   A_RECEIVE_CUR,   A_SEND_QUEUED,
188                                         A_COMPOSE_EMAIL, A_REPLY_MESSAGE, A_REPLY_SENDER,  
189                                         A_REPLY_ALL,     A_REPLY_ML,      A_FORWARD,       
190                                         A_DELETE,        A_EXECUTE,       A_GOTO_NEXT,      
191                                         A_SYL_ACTIONS };
192
193                 for (i = 0; i < sizeof(main_items)/sizeof(main_items[0]); i++) 
194                         items = g_list_append(items, gettext(toolbar_text[main_items[i]].descr));
195         }
196         else if (source == TOOLBAR_COMPOSE) {
197                 gint comp_items[10] = { A_SEND,          A_SENDL,        A_DRAFT,
198                                         A_INSERT,        A_ATTACH,       A_SIG,
199                                         A_EXTEDITOR,     A_LINEWRAP,     A_ADDRBOOK,
200                                         A_SYL_ACTIONS };        
201
202                 for (i = 0; i < sizeof(comp_items)/sizeof(comp_items[0]); i++) 
203                         items = g_list_append(items, gettext(toolbar_text[comp_items[i]].descr));
204         }
205         else if (source == TOOLBAR_MSGVIEW) {
206                 gint msgv_items[8/*9*/] = { A_COMPOSE_EMAIL, A_REPLY_MESSAGE, A_REPLY_SENDER,
207                                             A_REPLY_ALL,     A_REPLY_ML,      A_FORWARD,
208                                             A_DELETE,        A_GOTO_NEXT/*   A_SYL_ACTIONS*/ }; 
209
210                 for (i = 0; i < sizeof(msgv_items)/sizeof(msgv_items[0]); i++) 
211                         items = g_list_append(items, gettext(toolbar_text[msgv_items[i]].descr));
212         }
213
214         return items;
215 }
216
217 static void toolbar_parse_item(XMLFile *file, ToolbarType source)
218 {
219         GList *attr;
220         gchar *name, *value;
221         ToolbarItem *item = NULL;
222
223         attr = xml_get_current_tag_attr(file);
224         item = g_new0(ToolbarItem, 1);
225         while( attr ) {
226                 name = ((XMLAttr *)attr->data)->name;
227                 value = ((XMLAttr *)attr->data)->value;
228                 
229                 if (g_strcasecmp(name, TOOLBAR_ICON_FILE) == 0) 
230                         item->file = g_strdup (value);
231                 else if (g_strcasecmp(name, TOOLBAR_ICON_TEXT) == 0)
232                         item->text = g_strdup (value);
233                 else if (g_strcasecmp(name, TOOLBAR_ICON_ACTION) == 0)
234                         item->index = toolbar_ret_val_from_text(value);
235
236                 attr = g_list_next(attr);
237         }
238         if (item->index != -1) {
239                 
240                 if (!toolbar_is_duplicate(item->index, source)) 
241                         toolbar_config[source].item_list = g_slist_append(toolbar_config[source].item_list,
242                                                                          item);
243         }
244 }
245
246 static void toolbar_set_default_main(void) 
247 {
248         struct {
249                 gint action;
250                 gint icon;
251                 gchar *text;
252         } default_toolbar[] = {
253                 { A_RECEIVE_CUR,   STOCK_PIXMAP_MAIL_RECEIVE,         _("Get")     },
254                 { A_RECEIVE_ALL,   STOCK_PIXMAP_MAIL_RECEIVE_ALL,     _("Get All") },
255                 { A_SEPARATOR,     0,                                 ("")         }, 
256                 { A_SEND_QUEUED,   STOCK_PIXMAP_MAIL_SEND_QUEUE,      _("Send")    },
257                 { A_COMPOSE_EMAIL, STOCK_PIXMAP_MAIL_COMPOSE,         _("Email")   },
258                 { A_SEPARATOR,     0,                                 ("")         },
259                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
260                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
261                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
262                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
263                 { A_SEPARATOR,     0,                                 ("")         },
264                 { A_DELETE,        STOCK_PIXMAP_CLOSE,                _("Delete")  },
265                 { A_EXECUTE,       STOCK_PIXMAP_EXEC,                 _("Execute") },
266                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    }
267         };
268         
269         gint i;
270         
271         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
272                 
273                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
274                 
275                 if (default_toolbar[i].action != A_SEPARATOR) {
276                         
277                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
278                         
279                         toolbar_item->file  = g_strdup(file);
280                         toolbar_item->index = default_toolbar[i].action;
281                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
282                 } else {
283
284                         toolbar_item->file   = g_strdup(SEPARATOR);
285                         toolbar_item->index = A_SEPARATOR;
286                 }
287                 
288                 if (toolbar_item->index != -1) {
289                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_MAIN)) 
290                                 toolbar_config[TOOLBAR_MAIN].item_list = 
291                                         g_slist_append(toolbar_config[TOOLBAR_MAIN].item_list, toolbar_item);
292                 }       
293         }
294 }
295
296 static void toolbar_set_default_compose(void)
297 {
298         struct {
299                 gint action;
300                 gint icon;
301                 gchar *text;
302         } default_toolbar[] = {
303                 { A_SEND,      STOCK_PIXMAP_MAIL_SEND,         _("Send")       },
304                 { A_SENDL,     STOCK_PIXMAP_MAIL_SEND_QUEUE,   _("Send later") },
305                 { A_DRAFT,     STOCK_PIXMAP_MAIL,              _("Draft")      },
306                 { A_SEPARATOR, 0,                               ("")           }, 
307                 { A_INSERT,    STOCK_PIXMAP_INSERT_FILE,       _("Insert")     },
308                 { A_ATTACH,    STOCK_PIXMAP_MAIL_ATTACH,       _("Attach")     },
309                 { A_SIG,       STOCK_PIXMAP_MAIL_SIGN,         _("Signature")  },
310                 { A_SEPARATOR, 0,                               ("")           },
311                 { A_EXTEDITOR, STOCK_PIXMAP_EDIT_EXTERN,       _("Editor")     },
312                 { A_LINEWRAP,  STOCK_PIXMAP_LINEWRAP,          _("Linewrap")   },
313                 { A_SEPARATOR, 0,                               ("")           },
314                 { A_ADDRBOOK,  STOCK_PIXMAP_ADDRESS_BOOK,      _("Address")    }
315         };
316         
317         gint i;
318
319         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
320                 
321                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
322                 
323                 if (default_toolbar[i].action != A_SEPARATOR) {
324                         
325                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
326                         
327                         toolbar_item->file  = g_strdup(file);
328                         toolbar_item->index = default_toolbar[i].action;
329                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
330                 } else {
331
332                         toolbar_item->file   = g_strdup(SEPARATOR);
333                         toolbar_item->index = A_SEPARATOR;
334                 }
335                 
336                 if (toolbar_item->index != -1) {
337                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_COMPOSE)) 
338                                 toolbar_config[TOOLBAR_COMPOSE].item_list = 
339                                         g_slist_append(toolbar_config[TOOLBAR_COMPOSE].item_list, toolbar_item);
340                 }       
341         }
342 }
343
344 static void toolbar_set_default_msgview(void)
345 {
346         struct {
347                 gint action;
348                 gint icon;
349                 gchar *text;
350         } default_toolbar[] = {
351                 /*{ A_COMPOSE_EMAIL, STOCK_PIXMAP_MAIL_COMPOSE,         _("Email")   },
352                   { A_SEPARATOR,     0,                                 ("")         },*/
353                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
354                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
355                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
356                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
357                 { A_SEPARATOR,     0,                                 ("")         },
358                 { A_DELETE,        STOCK_PIXMAP_CLOSE,                _("Delete")  },
359                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    }
360         };
361         
362         gint i;
363
364         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
365                 
366                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
367                 
368                 if (default_toolbar[i].action != A_SEPARATOR) {
369                         
370                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
371                         
372                         toolbar_item->file  = g_strdup(file);
373                         toolbar_item->index = default_toolbar[i].action;
374                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
375                 } else {
376
377                         toolbar_item->file   = g_strdup(SEPARATOR);
378                         toolbar_item->index = A_SEPARATOR;
379                 }
380                 
381                 if (toolbar_item->index != -1) {
382                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_MSGVIEW)) 
383                                 toolbar_config[TOOLBAR_MSGVIEW].item_list = 
384                                         g_slist_append(toolbar_config[TOOLBAR_MSGVIEW].item_list, toolbar_item);
385                 }       
386         }
387 }
388
389 void toolbar_set_default(ToolbarType source)
390 {
391         if (source == TOOLBAR_MAIN)
392                 toolbar_set_default_main();
393         else if  (source == TOOLBAR_COMPOSE)
394                 toolbar_set_default_compose();
395         else if  (source == TOOLBAR_MSGVIEW)
396                 toolbar_set_default_msgview();
397
398 }
399
400 void toolbar_save_config_file(ToolbarType source)
401 {
402         GSList *cur;
403         FILE *fp;
404         PrefFile *pfile;
405         gchar *fileSpec = NULL;
406
407         debug_print("save Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
408
409         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
410         pfile = prefs_write_open(fileSpec);
411         g_free( fileSpec );
412         if( pfile ) {
413                 fp = pfile->fp;
414                 fprintf(fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
415                         conv_get_current_charset_str());
416
417                 fprintf(fp, "<%s>\n", TOOLBAR_TAG_INDEX);
418
419                 for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
420                         ToolbarItem *toolbar_item = (ToolbarItem*) cur->data;
421                         
422                         if (g_strcasecmp(toolbar_item->file, SEPARATOR) != 0) 
423                                 fprintf(fp, "\t<%s %s=\"%s\" %s=\"%s\" %s=\"%s\"/>\n",
424                                         TOOLBAR_TAG_ITEM, 
425                                         TOOLBAR_ICON_FILE, toolbar_item->file,
426                                         TOOLBAR_ICON_TEXT, toolbar_item->text,
427                                         TOOLBAR_ICON_ACTION, 
428                                         toolbar_ret_text_from_val(toolbar_item->index));
429                         else 
430                                 fprintf(fp, "\t<%s/>\n", TOOLBAR_TAG_SEPARATOR); 
431                 }
432
433                 fprintf(fp, "</%s>\n", TOOLBAR_TAG_INDEX);      
434         
435                 if (prefs_write_close (pfile) < 0 ) 
436                         g_warning("failed to write toolbar configuration to file\n");
437         } else
438                 g_warning("failed to open toolbar configuration file for writing\n");
439 }
440
441 void toolbar_read_config_file(ToolbarType source)
442 {
443         XMLFile *file   = NULL;
444         gchar *fileSpec = NULL;
445         GList *attr;
446         gboolean retVal;
447         jmp_buf    jumper;
448
449         debug_print("read Toolbar Configuration from %s\n", toolbar_config[source].conf_file);
450
451         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
452         file = xml_open_file(fileSpec);
453         g_free(fileSpec);
454
455         toolbar_clear_list(source);
456
457         if (file) {
458                 if ((setjmp(jumper))
459                 || (xml_get_dtd(file))
460                 || (xml_parse_next_tag(file))
461                 || (!xml_compare_tag(file, TOOLBAR_TAG_INDEX))) {
462                         xml_close_file(file);
463                         return;
464                 }
465
466                 attr = xml_get_current_tag_attr(file);
467                 
468                 retVal = TRUE;
469                 for (;;) {
470                         if (!file->level) 
471                                 break;
472                         /* Get item tag */
473                         if (xml_parse_next_tag(file)) 
474                                 longjmp(jumper, 1);
475
476                         /* Get next tag (icon, icon_text or icon_action) */
477                         if (xml_compare_tag(file, TOOLBAR_TAG_ITEM)) {
478                                 toolbar_parse_item(file, source);
479                         } else if (xml_compare_tag(file, TOOLBAR_TAG_SEPARATOR)) {
480                                 ToolbarItem *item = g_new0(ToolbarItem, 1);
481                         
482                                 item->file   = g_strdup(SEPARATOR);
483                                 item->index  = A_SEPARATOR;
484                                 toolbar_config[source].item_list = 
485                                         g_slist_append(toolbar_config[source].item_list, item);
486                         }
487
488                 }
489                 xml_close_file(file);
490         }
491
492         if ((!file) || (g_slist_length(toolbar_config[source].item_list) == 0)) {
493
494                 if (source == TOOLBAR_MAIN) 
495                         toolbar_set_default(TOOLBAR_MAIN);
496                 else if (source == TOOLBAR_COMPOSE) 
497                         toolbar_set_default(TOOLBAR_COMPOSE);
498                 else if (source == TOOLBAR_MSGVIEW) 
499                         toolbar_set_default(TOOLBAR_MSGVIEW);
500                 else {          
501                         g_warning("failed to write Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
502                         return;
503                 }
504
505                 toolbar_save_config_file(source);
506         }
507 }
508
509 /*
510  * clears list of toolbar items read from configuration files
511  */
512 void toolbar_clear_list(ToolbarType source)
513 {
514         while (toolbar_config[source].item_list != NULL) {
515                 ToolbarItem *item = (ToolbarItem*) toolbar_config[source].item_list->data;
516                 
517                 toolbar_config[source].item_list = 
518                         g_slist_remove(toolbar_config[source].item_list, item);
519
520                 if (item->file)
521                         g_free(item->file);
522                 if (item->text)
523                         g_free(item->text);
524                 g_free(item);   
525         }
526         g_slist_free(toolbar_config[source].item_list);
527 }
528
529
530 /* 
531  * return list of Toolbar items
532  */
533 GSList *toolbar_get_list(ToolbarType source)
534 {
535         GSList *list = NULL;
536
537         if ((source == TOOLBAR_MAIN) || (source == TOOLBAR_COMPOSE) || (source == TOOLBAR_MSGVIEW))
538                 list = toolbar_config[source].item_list;
539
540         return list;
541 }
542
543 void toolbar_set_list_item(ToolbarItem *t_item, ToolbarType source)
544 {
545         ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
546
547         toolbar_item->file  = g_strdup(t_item->file);
548         toolbar_item->text  = g_strdup(t_item->text);
549         toolbar_item->index = t_item->index;
550         
551         toolbar_config[source].item_list = 
552                 g_slist_append(toolbar_config[source].item_list,
553                                toolbar_item);
554 }
555
556 void toolbar_action_execute(GtkWidget    *widget,
557                             GSList       *action_list, 
558                             gpointer     data,
559                             gint         source) 
560 {
561         GSList *cur, *lop;
562         gchar *action, *action_p;
563         gboolean found = FALSE;
564         gint i = 0;
565
566         for (cur = action_list; cur != NULL;  cur = cur->next) {
567                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
568
569                 if (widget == act->widget) {
570                         
571                         for (lop = prefs_common.actions_list; lop != NULL; lop = lop->next) {
572                                 action = g_strdup((gchar*)lop->data);
573
574                                 action_p = strstr(action, ": ");
575                                 action_p[0] = 0x00;
576                                 if (g_strcasecmp(act->name, action) == 0) {
577                                         found = TRUE;
578                                         g_free(action);
579                                         break;
580                                 } else 
581                                         i++;
582                                 g_free(action);
583                         }
584                         if (found) 
585                                 break;
586                 }
587         }
588
589         if (found) 
590                 actions_execute(data, i, widget, source);
591         else
592                 g_warning ("Error: did not find Sylpheed Action to execute");
593 }
594
595 /*
596  * Change the style of toolbar
597  */
598 void common_toolbar_set_style(gpointer data, ToolbarType type)
599 {
600         GtkWidget  *handlebox_wid;
601         GtkWidget  *toolbar_wid;
602         MainWindow *mainwin;
603         Compose    *compose;
604         MessageView *msgview;
605         
606         g_return_if_fail(data != NULL);
607         
608         switch (type) {
609         case TOOLBAR_MAIN:
610                 mainwin = (MainWindow*)data;
611                 handlebox_wid = mainwin->handlebox;
612                 toolbar_wid = mainwin->toolbar->toolbar;
613                 break;
614         case TOOLBAR_COMPOSE:
615                 compose = (Compose*)data;
616                 handlebox_wid = compose->handlebox;
617                 toolbar_wid = compose->toolbar->toolbar;
618                 break;
619         case TOOLBAR_MSGVIEW: 
620                 msgview = (MessageView*)data;
621                 handlebox_wid = msgview->handlebox;
622                 toolbar_wid = msgview->toolbar->toolbar;
623                 break;
624         default:
625                 debug_print("toolbar_set_style: not supported for this type of window\n");
626                 return;
627         }
628         
629         switch (prefs_common.toolbar_style) {
630         case TOOLBAR_NONE:
631                 gtk_widget_hide(handlebox_wid);
632                 break;
633         case TOOLBAR_ICON:
634                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
635                                       GTK_TOOLBAR_ICONS);
636                 break;
637         case TOOLBAR_TEXT:
638                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
639                                       GTK_TOOLBAR_TEXT);
640                 break;
641         case TOOLBAR_BOTH:
642                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
643                                       GTK_TOOLBAR_BOTH);
644                 break;
645         }
646         
647         if (prefs_common.toolbar_style != TOOLBAR_NONE) {
648                 gtk_widget_show(handlebox_wid);
649                 gtk_widget_queue_resize(handlebox_wid);
650         }
651 }
652
653 /*
654  * Delete current/selected(s) message(s)
655  */
656 void common_toolbar_delete_cb(GtkWidget   *widget,
657                               gpointer     data)
658 {
659         ToolbarParent *parent = (ToolbarParent*)data;
660         MainWindow *mainwin;
661         MessageView *msgview;
662
663         g_return_if_fail(parent != NULL);
664         
665         switch (parent->type) {
666         case TOOLBAR_MSGVIEW:
667                 msgview = (MessageView*)parent->data;
668                 summary_delete(msgview->mainwin->summaryview);  
669                 /* do we really want to close the widget ? 
670                    I`d rather have it staying open and moving to next msg 
671                    in summaryview ... - oha */
672                 /* following code is already used somewhere else, perhaps wrap it in a function ? */
673                 if (msgview->mainwin->summaryview->selected) {
674                         GtkCTree *ctree = GTK_CTREE(msgview->mainwin->summaryview->ctree);
675                         MsgInfo * msginfo = gtk_ctree_node_get_row_data(ctree, 
676                                                                         msgview->mainwin->summaryview->selected);
677                         messageview_show(msgview, msginfo, 
678                                          msgview->all_headers);
679                 } else {
680                         toolbar_clear_list(TOOLBAR_MSGVIEW);
681                         TOOLBAR_DESTROY_ITEMS(msgview->toolbar->item_list);     
682                         TOOLBAR_DESTROY_ACTIONS(msgview->toolbar->action_list);
683                         gtk_widget_destroy(msgview->window);
684                 }
685                 break;
686         case TOOLBAR_MAIN:
687                 mainwin = (MainWindow*)parent->data;
688                 summary_delete(mainwin->summaryview);
689                 break;
690         default: 
691                 debug_print("toolbar_delete: Not supported for this type of window\n");
692                 break;
693         }
694 }
695
696
697 /*
698  * Compose new message
699  */
700 void common_toolbar_compose_cb(GtkWidget  *widget,
701                                gpointer    data)
702 {
703         ToolbarParent *parent = (ToolbarParent*)data;
704         MessageView *msgview;
705
706         g_return_if_fail(parent != NULL);
707
708         switch (parent->type) {
709         case TOOLBAR_MSGVIEW:
710                 msgview = (MessageView*)parent->data;
711                 compose_new_with_folderitem(NULL, 
712                                             msgview->msginfo->folder);
713                 break;  
714         default:
715                 debug_print("toolbar_compose: Not supported for this type of window\n");
716         }
717 }
718
719
720 /*
721  * Reply Message
722  */
723 void common_toolbar_reply_cb(GtkWidget   *widget, 
724                              gpointer     data)
725 {
726         ToolbarParent *parent = (ToolbarParent*)data;
727         MessageView *msgview;
728
729         g_return_if_fail(parent != NULL);
730
731         switch (parent->type) {
732         case TOOLBAR_MSGVIEW:
733                 msgview = (MessageView*)parent->data;
734                 compose_reply(msgview->msginfo,
735                               prefs_common.reply_with_quote ? COMPOSE_REPLY_WITH_QUOTE 
736                               : COMPOSE_REPLY_WITHOUT_QUOTE,
737                               FALSE, FALSE, FALSE, NULL);
738                 break;
739         default:
740                 debug_print("toolbar_reply: Not supported for this type of window\n");
741         }
742 }
743
744
745 /*
746  * Reply message to Sender and All recipients
747  */
748 void common_toolbar_reply_to_all_cb(GtkWidget   *widget, 
749                                     gpointer     data)
750 {
751         ToolbarParent *parent = (ToolbarParent*)data;
752         MessageView *msgview;
753
754         g_return_if_fail(parent != NULL);
755
756         switch (parent->type) {
757         case TOOLBAR_MSGVIEW:
758                 msgview = (MessageView*)parent->data;
759                 compose_reply(msgview->msginfo,
760                               prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_ALL_WITH_QUOTE 
761                               : COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE,
762                               TRUE, FALSE, FALSE, NULL);
763                 break;
764         default:
765                 debug_print("toolbar_reply_to_all: Not supported for this type of window\n");
766         }
767 }
768
769
770 /*
771  * Reply to Mailing List
772  */
773 void common_toolbar_reply_to_list_cb(GtkWidget   *widget, 
774                                      gpointer     data)
775 {
776         ToolbarParent *parent = (ToolbarParent*)data;
777         MessageView *msgview;
778
779         g_return_if_fail(parent != NULL);
780
781         switch (parent->type) {
782         case TOOLBAR_MSGVIEW:
783                 msgview = (MessageView*)parent->data;
784                 compose_reply(msgview->msginfo,
785                               prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_LIST_WITH_QUOTE 
786                               : COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE,
787                               FALSE, TRUE, FALSE, NULL);
788                 break;
789         default:
790                 debug_print("toolbar_reply_to_list: Not supported for this type of window\n");
791         }
792 }
793
794
795 /*
796  * Reply to sender of message
797  */ 
798 void common_toolbar_reply_to_sender_cb(GtkWidget   *widget, 
799                                        gpointer     data)
800 {
801         ToolbarParent *parent = (ToolbarParent*)data;
802         MessageView *msgview;
803
804         g_return_if_fail(parent != NULL);
805
806         switch (parent->type) {
807         case TOOLBAR_MSGVIEW:
808                 msgview = (MessageView*)parent->data;
809                 compose_reply(msgview->msginfo,
810                               prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_SENDER_WITH_QUOTE 
811                               : COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE,
812                               FALSE, FALSE, FALSE, NULL);
813                 break;
814         default:
815                 debug_print("toolbar_reply_to_sender: Not supported for this type of window\n");
816         }
817 }
818
819
820 /*
821  * Forward current/selected(s) message(s)
822  */
823 void common_toolbar_forward_cb(GtkWidget        *widget,
824                                gpointer          data)
825 {
826         ToolbarParent *parent = (ToolbarParent*)data;
827         MessageView *msgview;
828
829         g_return_if_fail(parent != NULL);
830
831         switch (parent->type) {
832 /* 
833   REFERENCE FROM mainwindow.c
834   
835         case TOOLBAR_MAIN:
836                 if (prefs_common.forward_as_attachment)
837                         reply_cb(mainwin, COMPOSE_FORWARD_AS_ATTACH, NULL);
838                 else
839                         reply_cb(mainwin, COMPOSE_FORWARD, NULL);
840                 break;
841 */              
842         default:
843                 debug_print("toolbar_forward: Not supported for this type of window\n");
844         }
845 }
846
847
848 /*
849  * Goto Next Unread Message
850  */
851 void common_toolbar_next_unread_cb(GtkWidget    *widget,
852                                    gpointer      data)
853 {
854         ToolbarParent *parent = (ToolbarParent*)data;
855         MainWindow *mainwin;
856         MessageView *msgview;
857
858         g_return_if_fail(parent != NULL);
859
860         switch (parent->type) {
861         case TOOLBAR_MAIN:
862                 mainwin = (MainWindow*)parent->data;
863                 summary_select_next_unread(mainwin->summaryview);
864                 break;
865                 
866         case TOOLBAR_MSGVIEW:
867                 msgview = (MessageView*)parent->data;
868 /*
869  * TODO: Check if summaryview stay in the same place when this message view was created 
870  * if summary have other message select the next will be based on message selected in summaryview
871  */
872                 summary_select_next_unread(msgview->mainwin->summaryview);
873                 
874                 /** Now we need to update the messageview window */
875                 if (msgview->mainwin->summaryview->selected) {
876                         GtkCTree *ctree = GTK_CTREE(msgview->mainwin->summaryview->ctree);
877                         
878                         MsgInfo * msginfo = gtk_ctree_node_get_row_data(ctree, 
879                                                                         msgview->mainwin->summaryview->selected);
880                        
881                         messageview_show(msgview, msginfo, 
882                                          msgview->all_headers);
883                 } else {
884                         gtk_widget_destroy(msgview->window);
885                 }
886                 break;
887         default:
888                 debug_print("toolbar_next_unread: Not supported for this type of window\n");
889         }
890 }
891
892
893 /*
894  * Execute actions from toolbar
895  */
896 void common_toolbar_actions_execute_cb(GtkWidget *widget,
897                                        gpointer   data)
898 {
899         ToolbarParent *parent = (ToolbarParent*)data;
900         GSList *action_list;
901         MainWindow *mainwin;
902         Compose *compose;
903
904         g_return_if_fail(parent != NULL);
905
906         switch (parent->type) {
907         case TOOLBAR_MAIN:
908                 mainwin = (MainWindow*)parent->data;
909                 action_list = mainwin->toolbar->action_list;
910                 break;
911         case TOOLBAR_COMPOSE:
912                 compose = (Compose*)parent->data;
913                 action_list = compose->toolbar->action_list;
914                 break;
915                 /* case TOOLBAR_MSGVIEW: not supported yet */
916         default:
917                 debug_print("toolbar_actions: not supported for this window type\n");
918                 return;
919         }
920
921         toolbar_action_execute(widget, action_list, parent->data, parent->type);
922 }