b49d7cbc0c4a2b83b3f1399776e1bc9f7de56d62
[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                 
669                 /* make sure the selected msg in summaryview is 
670                    the one we are asked to delete
671                 */
672                 if (msgview->mainwin->summaryview->selected) {
673                         SummaryView *summaryview = msgview->mainwin->summaryview;
674                         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
675                         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, 
676                                                                        summaryview->selected);  
677                         if (msginfo->msgnum != msgview->msginfo->msgnum) {
678                                 alertpanel_error(_("Message already removed from folder."));
679                                 return;
680                         }
681                 }
682                 
683                 summary_delete(msgview->mainwin->summaryview);  
684
685                 if (msgview->mainwin->summaryview->selected) {
686                         SummaryView *summaryview = msgview->mainwin->summaryview;
687                         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
688                         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, 
689                                                                        summaryview->selected);
690                         messageview_show(msgview, msginfo, 
691                                          msgview->all_headers);
692                 } else {
693                         toolbar_clear_list(TOOLBAR_MSGVIEW);
694                         TOOLBAR_DESTROY_ITEMS(msgview->toolbar->item_list);     
695                         TOOLBAR_DESTROY_ACTIONS(msgview->toolbar->action_list);
696                         gtk_widget_destroy(msgview->window);
697                 }
698                 break;
699         case TOOLBAR_MAIN:
700                 mainwin = (MainWindow*)parent->data;
701                 summary_delete(mainwin->summaryview);
702                 break;
703         default: 
704                 debug_print("toolbar_delete: Not supported for this type of window\n");
705                 break;
706         }
707 }
708
709
710 /*
711  * Compose new message
712  */
713 void common_toolbar_compose_cb(GtkWidget  *widget,
714                                gpointer    data)
715 {
716         ToolbarParent *parent = (ToolbarParent*)data;
717         MessageView *msgview;
718
719         g_return_if_fail(parent != NULL);
720
721         switch (parent->type) {
722         case TOOLBAR_MSGVIEW:
723                 msgview = (MessageView*)parent->data;
724                 compose_new_with_folderitem(NULL, 
725                                             msgview->msginfo->folder);
726                 break;  
727         default:
728                 debug_print("toolbar_compose: Not supported for this type of window\n");
729         }
730 }
731
732
733 /*
734  * Reply Message
735  */
736 void common_toolbar_reply_cb(GtkWidget   *widget, 
737                              gpointer     data)
738 {
739         ToolbarParent *parent = (ToolbarParent*)data;
740         MessageView *msgview;
741
742         g_return_if_fail(parent != NULL);
743
744         switch (parent->type) {
745         case TOOLBAR_MSGVIEW:
746                 msgview = (MessageView*)parent->data;
747                 compose_reply(msgview->msginfo,
748                               prefs_common.reply_with_quote ? COMPOSE_REPLY_WITH_QUOTE 
749                               : COMPOSE_REPLY_WITHOUT_QUOTE,
750                               FALSE, FALSE, FALSE, NULL);
751                 break;
752         default:
753                 debug_print("toolbar_reply: Not supported for this type of window\n");
754         }
755 }
756
757
758 /*
759  * Reply message to Sender and All recipients
760  */
761 void common_toolbar_reply_to_all_cb(GtkWidget   *widget, 
762                                     gpointer     data)
763 {
764         ToolbarParent *parent = (ToolbarParent*)data;
765         MessageView *msgview;
766
767         g_return_if_fail(parent != NULL);
768
769         switch (parent->type) {
770         case TOOLBAR_MSGVIEW:
771                 msgview = (MessageView*)parent->data;
772                 compose_reply(msgview->msginfo,
773                               prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_ALL_WITH_QUOTE 
774                               : COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE,
775                               TRUE, FALSE, FALSE, NULL);
776                 break;
777         default:
778                 debug_print("toolbar_reply_to_all: Not supported for this type of window\n");
779         }
780 }
781
782
783 /*
784  * Reply to Mailing List
785  */
786 void common_toolbar_reply_to_list_cb(GtkWidget   *widget, 
787                                      gpointer     data)
788 {
789         ToolbarParent *parent = (ToolbarParent*)data;
790         MessageView *msgview;
791
792         g_return_if_fail(parent != NULL);
793
794         switch (parent->type) {
795         case TOOLBAR_MSGVIEW:
796                 msgview = (MessageView*)parent->data;
797                 compose_reply(msgview->msginfo,
798                               prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_LIST_WITH_QUOTE 
799                               : COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE,
800                               FALSE, TRUE, FALSE, NULL);
801                 break;
802         default:
803                 debug_print("toolbar_reply_to_list: Not supported for this type of window\n");
804         }
805 }
806
807
808 /*
809  * Reply to sender of message
810  */ 
811 void common_toolbar_reply_to_sender_cb(GtkWidget   *widget, 
812                                        gpointer     data)
813 {
814         ToolbarParent *parent = (ToolbarParent*)data;
815         MessageView *msgview;
816
817         g_return_if_fail(parent != NULL);
818
819         switch (parent->type) {
820         case TOOLBAR_MSGVIEW:
821                 msgview = (MessageView*)parent->data;
822                 compose_reply(msgview->msginfo,
823                               prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_SENDER_WITH_QUOTE 
824                               : COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE,
825                               FALSE, FALSE, FALSE, NULL);
826                 break;
827         default:
828                 debug_print("toolbar_reply_to_sender: Not supported for this type of window\n");
829         }
830 }
831
832
833 /*
834  * Forward current/selected(s) message(s)
835  */
836 void common_toolbar_forward_cb(GtkWidget        *widget,
837                                gpointer          data)
838 {
839         ToolbarParent *parent = (ToolbarParent*)data;
840         MessageView *msgview;
841
842         g_return_if_fail(parent != NULL);
843
844         switch (parent->type) {
845 /* 
846   REFERENCE FROM mainwindow.c
847   
848         case TOOLBAR_MAIN:
849                 if (prefs_common.forward_as_attachment)
850                         reply_cb(mainwin, COMPOSE_FORWARD_AS_ATTACH, NULL);
851                 else
852                         reply_cb(mainwin, COMPOSE_FORWARD, NULL);
853                 break;
854 */              
855         default:
856                 debug_print("toolbar_forward: Not supported for this type of window\n");
857         }
858 }
859
860
861 /*
862  * Goto Next Unread Message
863  */
864 void common_toolbar_next_unread_cb(GtkWidget    *widget,
865                                    gpointer      data)
866 {
867         ToolbarParent *parent = (ToolbarParent*)data;
868         MainWindow *mainwin;
869         MessageView *msgview;
870
871         g_return_if_fail(parent != NULL);
872
873         switch (parent->type) {
874         case TOOLBAR_MAIN:
875                 mainwin = (MainWindow*)parent->data;
876                 summary_select_next_unread(mainwin->summaryview);
877                 break;
878                 
879         case TOOLBAR_MSGVIEW:
880                 msgview = (MessageView*)parent->data;
881 /*
882  * TODO: Check if summaryview stay in the same place when this message view was created 
883  * if summary have other message select the next will be based on message selected in summaryview
884  */
885                 summary_select_next_unread(msgview->mainwin->summaryview);
886                 
887                 /** Now we need to update the messageview window */
888                 if (msgview->mainwin->summaryview->selected) {
889                         GtkCTree *ctree = GTK_CTREE(msgview->mainwin->summaryview->ctree);
890                         
891                         MsgInfo * msginfo = gtk_ctree_node_get_row_data(ctree, 
892                                                                         msgview->mainwin->summaryview->selected);
893                        
894                         messageview_show(msgview, msginfo, 
895                                          msgview->all_headers);
896                 } else {
897                         gtk_widget_destroy(msgview->window);
898                 }
899                 break;
900         default:
901                 debug_print("toolbar_next_unread: Not supported for this type of window\n");
902         }
903 }
904
905
906 /*
907  * Execute actions from toolbar
908  */
909 void common_toolbar_actions_execute_cb(GtkWidget *widget,
910                                        gpointer   data)
911 {
912         ToolbarParent *parent = (ToolbarParent*)data;
913         GSList *action_list;
914         MainWindow *mainwin;
915         Compose *compose;
916
917         g_return_if_fail(parent != NULL);
918
919         switch (parent->type) {
920         case TOOLBAR_MAIN:
921                 mainwin = (MainWindow*)parent->data;
922                 action_list = mainwin->toolbar->action_list;
923                 break;
924         case TOOLBAR_COMPOSE:
925                 compose = (Compose*)parent->data;
926                 action_list = compose->toolbar->action_list;
927                 break;
928                 /* case TOOLBAR_MSGVIEW: not supported yet */
929         default:
930                 debug_print("toolbar_actions: not supported for this window type\n");
931                 return;
932         }
933
934         toolbar_action_execute(widget, action_list, parent->data, parent->type);
935 }