* src/folder.[ch]
[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 "prefs_common.h"
47 #include "menu.h"
48 #include "prefs_actions.h"
49 #include "manage_window.h"
50
51 #include "toolbar.h"
52 #include "prefs_toolbar.h"
53
54 /* elements */
55 #define TOOLBAR_TAG_INDEX        "toolbar"
56 #define TOOLBAR_TAG_ITEM         "item"
57 #define TOOLBAR_TAG_SEPARATOR    SEPARATOR
58
59 #define TOOLBAR_ICON_FILE   "file"    
60 #define TOOLBAR_ICON_TEXT   "text"     
61 #define TOOLBAR_ICON_ACTION "action"    
62
63 gboolean      toolbar_is_duplicate           (gint            action,
64                                               Toolbar         source);
65 static void   toolbar_parse_item             (XMLFile        *file,
66                                               Toolbar        source);
67
68 static gint   toolbar_ret_val_from_text      (const gchar    *text);
69 static gchar *toolbar_ret_text_from_val      (gint            val);
70
71 static void   toolbar_set_default_main       (void);
72 static void   toolbar_set_default_compose    (void);
73
74
75 /* 
76  *  Text Database linked to enumarated values in toolbar.h 
77  */
78 static ToolbarText toolbar_text [] = {
79         
80         { "A_RECEIVE_ALL",   N_("Receive Mail on all Accounts")         },
81         { "A_RECEIVE_CUR",   N_("Receive Mail on current Account")      },
82         { "A_SEND_QUEUED",   N_("Send Queued Message(s)")               },
83         { "A_COMPOSE_EMAIL", N_("Compose Email")                        },
84         { "A_COMPOSE_NEWS",  N_("Compose News")                         },
85         { "A_REPLY_MESSAGE", N_("Reply to Message")                     },
86         { "A_REPLY_SENDER",  N_("Reply to Sender")                      },
87         { "A_REPLY_ALL",     N_("Reply to All")                         },
88         { "A_REPLY_ML",      N_("Reply to Mailing-list")                },
89         { "A_FORWARD",       N_("Forward Message")                      }, 
90         { "A_DELETE",        N_("Delete Message")                       },
91         { "A_EXECUTE",       N_("Execute")                              },
92         { "A_GOTO_NEXT",     N_("Goto Next Message")                    },
93
94         { "A_SEND",          N_("Send Message")                         },
95         { "A_SENDL",         N_("Put into queue folder and send later") },
96         { "A_DRAFT",         N_("Save to draft folder")                 },
97         { "A_INSERT",        N_("Insert file")                          },   
98         { "A_ATTACH",        N_("Attach file")                          },
99         { "A_SIG",           N_("Insert signature")                     },
100         { "A_EXTEDITOR",     N_("Edit with external editor")            },
101         { "A_LINEWRAP",      N_("Wrap all long lines")                  }, 
102         { "A_ADDRBOOK",      N_("Address book")                         },
103
104         { "A_SYL_ACTIONS",   N_("Sylpheed Actions Feature")             }, 
105         { "A_SEPARATOR",     ("")                                       }
106 };
107
108 /* struct holds configuration files and a list of
109  * currently active toolbar items 
110  * TOOLBAR_MAIN and TOOLBAR_COMPOSE give as an index
111  */
112 static ToolbarConfig toolbar_config[2] = {
113         { "toolbar_main.xml",    NULL},
114         { "toolbar_compose.xml", NULL}, 
115 };
116
117 gint toolbar_ret_val_from_descr(const gchar *descr)
118 {
119         gint i;
120
121         for (i = 0; i < N_ACTION_VAL; i++) {
122                 if (g_strcasecmp(toolbar_text[i].descr, descr) == 0)
123                                 return i;
124         }
125         
126         return -1;
127 }
128
129 gchar *toolbar_ret_descr_from_val(gint val)
130 {
131         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
132
133         return toolbar_text[val].descr;
134 }
135
136 static gint toolbar_ret_val_from_text(const gchar *text)
137 {
138         gint i;
139         
140         for (i = 0; i < N_ACTION_VAL; i++) {
141                 if (g_strcasecmp(toolbar_text[i].index_str, text) == 0)
142                                 return i;
143         }
144         
145         return -1;
146 }
147
148 static gchar *toolbar_ret_text_from_val(gint val)
149 {
150         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
151
152         return toolbar_text[val].index_str;
153 }
154
155 gboolean toolbar_is_duplicate(gint action, Toolbar source)
156 {
157         GSList *cur;
158
159         if ((action == A_SEPARATOR) || (action == A_SYL_ACTIONS)) 
160                 return FALSE;
161
162         for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
163                 ToolbarItem *item = (ToolbarItem*) cur->data;
164                 
165                 if (item->index == action)
166                         return TRUE;
167         }
168         return FALSE;
169 }
170
171 GList *toolbar_get_action_items(Toolbar source)
172 {
173         GList *items = NULL;
174         gint i = 0;
175         
176         if (source == TOOLBAR_MAIN) {
177                 gint main_items[13] = { A_RECEIVE_ALL,   A_RECEIVE_CUR,   A_SEND_QUEUED,
178                                         A_COMPOSE_EMAIL, A_REPLY_MESSAGE, A_REPLY_SENDER,  
179                                         A_REPLY_ALL,     A_REPLY_ML,      A_FORWARD,       
180                                         A_DELETE,        A_EXECUTE,       A_GOTO_NEXT,      
181                                         A_SYL_ACTIONS };
182
183                 for (i = 0; i < sizeof(main_items)/sizeof(main_items[0]); i++) 
184                         items = g_list_append(items, toolbar_text[main_items[i]].descr);
185         }
186         else if (source == TOOLBAR_COMPOSE) {
187                 gint comp_items[10] = { A_SEND,          A_SENDL,        A_DRAFT,
188                                         A_INSERT,        A_ATTACH,       A_SIG,
189                                         A_EXTEDITOR,     A_LINEWRAP,     A_ADDRBOOK,
190                                         A_SYL_ACTIONS };        
191
192                 for (i = 0; i < sizeof(comp_items)/sizeof(comp_items[0]); i++) 
193                         items = g_list_append(items, toolbar_text[comp_items[i]].descr);
194         }
195
196         return items;
197 }
198
199 static void toolbar_parse_item(XMLFile *file, Toolbar source)
200 {
201         GList *attr;
202         gchar *name, *value;
203         ToolbarItem *item = NULL;
204
205         attr = xml_get_current_tag_attr(file);
206         item = g_new0(ToolbarItem, 1);
207         while( attr ) {
208                 name = ((XMLAttr *)attr->data)->name;
209                 value = ((XMLAttr *)attr->data)->value;
210                 
211                 if (g_strcasecmp(name, TOOLBAR_ICON_FILE) == 0) 
212                         item->file = g_strdup (value);
213                 else if (g_strcasecmp(name, TOOLBAR_ICON_TEXT) == 0)
214                         item->text = g_strdup (value);
215                 else if (g_strcasecmp(name, TOOLBAR_ICON_ACTION) == 0)
216                         item->index = toolbar_ret_val_from_text(value);
217
218                 attr = g_list_next(attr);
219         }
220         if (item->index != -1) {
221                 
222                 if (!toolbar_is_duplicate(item->index, source)) 
223                         toolbar_config[source].item_list = g_slist_append(toolbar_config[source].item_list,
224                                                                          item);
225         }
226 }
227
228 static void toolbar_set_default_main(void) 
229 {
230         struct {
231                 gint action;
232                 gint icon;
233                 gchar *text;
234         } default_toolbar[] = {
235                 { A_RECEIVE_CUR,   STOCK_PIXMAP_MAIL_RECEIVE,         _("Get")     },
236                 { A_RECEIVE_ALL,   STOCK_PIXMAP_MAIL_RECEIVE_ALL,     _("Get All") },
237                 { A_SEPARATOR,     0,                                 ("")         }, 
238                 { A_SEND_QUEUED,   STOCK_PIXMAP_MAIL_SEND_QUEUE,      _("Send")    },
239                 { A_COMPOSE_EMAIL, STOCK_PIXMAP_MAIL_COMPOSE,         _("Email")   },
240                 { A_SEPARATOR,     0,                                 ("")         },
241                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
242                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
243                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
244                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
245                 { A_SEPARATOR,     0,                                 ("")         },
246                 { A_DELETE,        STOCK_PIXMAP_CLOSE,                _("Delete")  },
247                 { A_EXECUTE,       STOCK_PIXMAP_EXEC,                 _("Execute") },
248                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    }
249         };
250         
251         gint i;
252         
253         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
254                 
255                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
256                 
257                 if (default_toolbar[i].action != A_SEPARATOR) {
258                         
259                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
260                         
261                         toolbar_item->file  = g_strdup(file);
262                         toolbar_item->index = default_toolbar[i].action;
263                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
264                 } else {
265
266                         toolbar_item->file   = g_strdup(SEPARATOR);
267                         toolbar_item->index = A_SEPARATOR;
268                 }
269                 
270                 if (toolbar_item->index != -1) {
271                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_MAIN)) 
272                                 toolbar_config[TOOLBAR_MAIN].item_list = 
273                                         g_slist_append(toolbar_config[TOOLBAR_MAIN].item_list, toolbar_item);
274                 }       
275         }
276 }
277
278 static void toolbar_set_default_compose(void)
279 {
280         struct {
281                 gint action;
282                 gint icon;
283                 gchar *text;
284         } default_toolbar[] = {
285                 { A_SEND,      STOCK_PIXMAP_MAIL_SEND,         _("Send")       },
286                 { A_SENDL,     STOCK_PIXMAP_MAIL_SEND_QUEUE,   _("Send later") },
287                 { A_DRAFT,     STOCK_PIXMAP_MAIL,              _("Draft")      },
288                 { A_SEPARATOR, 0,                               ("")           }, 
289                 { A_INSERT,    STOCK_PIXMAP_INSERT_FILE,       _("Insert")     },
290                 { A_ATTACH,    STOCK_PIXMAP_MAIL_ATTACH,       _("Attach")     },
291                 { A_SIG,       STOCK_PIXMAP_MAIL_SIGN,         _("Signature")  },
292                 { A_SEPARATOR, 0,                               ("")           },
293                 { A_EXTEDITOR, STOCK_PIXMAP_EDIT_EXTERN,       _("Editor")     },
294                 { A_LINEWRAP,  STOCK_PIXMAP_LINEWRAP,          _("Linewrap")   },
295                 { A_SEPARATOR, 0,                               ("")           },
296                 { A_ADDRBOOK,  STOCK_PIXMAP_ADDRESS_BOOK,      _("Address")    }
297         };
298         
299         gint i;
300
301         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
302                 
303                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
304                 
305                 if (default_toolbar[i].action != A_SEPARATOR) {
306                         
307                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
308                         
309                         toolbar_item->file  = g_strdup(file);
310                         toolbar_item->index = default_toolbar[i].action;
311                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
312                 } else {
313
314                         toolbar_item->file   = g_strdup(SEPARATOR);
315                         toolbar_item->index = A_SEPARATOR;
316                 }
317                 
318                 if (toolbar_item->index != -1) {
319                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_COMPOSE)) 
320                                 toolbar_config[TOOLBAR_COMPOSE].item_list = 
321                                         g_slist_append(toolbar_config[TOOLBAR_COMPOSE].item_list, toolbar_item);
322                 }       
323         }
324 }
325
326 void toolbar_set_default(Toolbar source)
327 {
328         if (source == TOOLBAR_MAIN)
329                 toolbar_set_default_main();
330         else if  (source == TOOLBAR_COMPOSE)
331                 toolbar_set_default_compose();
332
333 }
334
335 void toolbar_save_config_file(Toolbar source)
336 {
337         GSList *cur;
338         FILE *fp;
339         PrefFile *pfile;
340         gchar *fileSpec = NULL;
341
342         debug_print("save Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
343
344         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
345         pfile = prefs_write_open(fileSpec);
346         g_free( fileSpec );
347         if( pfile ) {
348                 fp = pfile->fp;
349                 fprintf(fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
350                         conv_get_current_charset_str());
351
352                 fprintf(fp, "<%s>\n", TOOLBAR_TAG_INDEX);
353
354                 for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
355                         ToolbarItem *toolbar_item = (ToolbarItem*) cur->data;
356                         
357                         if (g_strcasecmp(toolbar_item->file, SEPARATOR) != 0) 
358                                 fprintf(fp, "\t<%s %s=\"%s\" %s=\"%s\" %s=\"%s\"/>\n",
359                                         TOOLBAR_TAG_ITEM, 
360                                         TOOLBAR_ICON_FILE, toolbar_item->file,
361                                         TOOLBAR_ICON_TEXT, toolbar_item->text,
362                                         TOOLBAR_ICON_ACTION, 
363                                         toolbar_ret_text_from_val(toolbar_item->index));
364                         else 
365                                 fprintf(fp, "\t<%s/>\n", TOOLBAR_TAG_SEPARATOR); 
366                 }
367
368                 fprintf(fp, "</%s>\n", TOOLBAR_TAG_INDEX);      
369         
370                 if (prefs_write_close (pfile) < 0 ) 
371                         g_warning("failed to write toolbar configuration to file\n");
372         } else
373                 g_warning("failed to open toolbar configuration file for writing\n");
374 }
375
376 void toolbar_read_config_file(Toolbar source)
377 {
378         XMLFile *file   = NULL;
379         gchar *fileSpec = NULL;
380         GList *attr;
381         gboolean retVal;
382         jmp_buf    jumper;
383
384         debug_print("read Toolbar Configuration from %s\n", toolbar_config[source].conf_file);
385
386         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
387         file = xml_open_file(fileSpec);
388         g_free(fileSpec);
389
390         toolbar_clear_list(source);
391
392         if (file) {
393                 if ((setjmp(jumper))
394                 || (xml_get_dtd(file))
395                 || (xml_parse_next_tag(file))
396                 || (!xml_compare_tag(file, TOOLBAR_TAG_INDEX))) {
397                         xml_close_file(file);
398                         return;
399                 }
400
401                 attr = xml_get_current_tag_attr(file);
402                 
403                 retVal = TRUE;
404                 for (;;) {
405                         if (!file->level) 
406                                 break;
407                         /* Get item tag */
408                         if (xml_parse_next_tag(file)) 
409                                 longjmp(jumper, 1);
410
411                         /* Get next tag (icon, icon_text or icon_action) */
412                         if (xml_compare_tag(file, TOOLBAR_TAG_ITEM)) {
413                                 toolbar_parse_item(file, source);
414                         } else if (xml_compare_tag(file, TOOLBAR_TAG_SEPARATOR)) {
415                                 ToolbarItem *item = g_new0(ToolbarItem, 1);
416                         
417                                 item->file   = g_strdup(SEPARATOR);
418                                 item->index  = A_SEPARATOR;
419                                 toolbar_config[source].item_list = 
420                                         g_slist_append(toolbar_config[source].item_list, item);
421                         }
422
423                 }
424                 xml_close_file(file);
425         }
426
427         if ((!file) || (g_slist_length(toolbar_config[source].item_list) == 0)) {
428
429                 if (source == TOOLBAR_MAIN) 
430                         toolbar_set_default(TOOLBAR_MAIN);
431                 else if (source == TOOLBAR_COMPOSE) 
432                         toolbar_set_default(TOOLBAR_COMPOSE);
433                 else {          
434                         g_warning("failed to write Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
435                         return;
436                 }
437
438                 toolbar_save_config_file(source);
439         }
440 }
441
442 /*
443  * clears list of toolbar items read from configuration files
444  */
445 void toolbar_clear_list(Toolbar source)
446 {
447         while (toolbar_config[source].item_list != NULL) {
448                 ToolbarItem *item = (ToolbarItem*) toolbar_config[source].item_list->data;
449                 
450                 toolbar_config[source].item_list = 
451                         g_slist_remove(toolbar_config[source].item_list, item);
452
453                 if (item->file)
454                         g_free(item->file);
455                 if (item->text)
456                         g_free(item->text);
457                 g_free(item);   
458         }
459         g_slist_free(toolbar_config[source].item_list);
460 }
461
462
463 /* 
464  * return list of Toolbar items
465  */
466 GSList *toolbar_get_list(Toolbar source)
467 {
468         GSList *list = NULL;
469
470         if ((source == TOOLBAR_MAIN) || (source == TOOLBAR_COMPOSE))
471                 list = toolbar_config[source].item_list;
472
473         return list;
474 }
475
476 void toolbar_set_list_item(ToolbarItem *t_item, Toolbar source)
477 {
478         ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
479
480         toolbar_item->file  = g_strdup(t_item->file);
481         toolbar_item->text  = g_strdup(t_item->text);
482         toolbar_item->index = t_item->index;
483         
484         toolbar_config[source].item_list = 
485                 g_slist_append(toolbar_config[source].item_list,
486                                toolbar_item);
487 }
488
489 void toolbar_action_execute(GtkWidget    *widget,
490                             GSList       *action_list, 
491                             gpointer     data,
492                             gint         source) 
493 {
494         GSList *cur, *lop;
495         gchar *action, *action_p;
496         gboolean found = FALSE;
497         gint i = 0;
498
499         for (cur = action_list; cur != NULL;  cur = cur->next) {
500                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
501
502                 if (widget == act->widget) {
503                         
504                         for (lop = prefs_common.actions_list; lop != NULL; lop = lop->next) {
505                                 action = g_strdup((gchar*)lop->data);
506
507                                 action_p = strstr(action, ": ");
508                                 action_p[0] = 0x00;
509                                 if (g_strcasecmp(act->name, action) == 0) {
510                                         found = TRUE;
511                                         g_free(action);
512                                         break;
513                                 } else 
514                                         i++;
515                                 g_free(action);
516                         }
517                         if (found) 
518                                 break;
519                 }
520         }
521
522         if (found) 
523                 actions_execute(data, i, widget, source);
524         else
525                 g_warning ("Error: did not find Sylpheed Action to execute");
526 }
527
528 void toolbar_destroy_items(GSList *t_item_list)
529 {
530         ToolbarItem *t_item;
531         
532         while (t_item_list != NULL) {
533                 t_item = (ToolbarItem*)t_item_list->data;
534
535                 t_item_list = g_slist_remove(t_item_list, t_item);      
536                 if (t_item->file)
537                         g_free(t_item->file);
538                 if (t_item->text)
539                         g_free(t_item->text);
540                 g_free(t_item);
541         }
542         g_slist_free(t_item_list);
543 }
544
545