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