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