2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Hiroyuki Yamamoto
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.
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.
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.
21 * General functions for accessing address book files.
44 #include "stock_pixmap.h"
45 #include "mainwindow.h"
46 #include "prefs_common.h"
48 #include "prefs_actions.h"
49 #include "manage_window.h"
52 #include "prefs_toolbar.h"
55 #define TOOLBAR_TAG_INDEX "toolbar"
56 #define TOOLBAR_TAG_ITEM "item"
57 #define TOOLBAR_TAG_SEPARATOR SEPARATOR
59 #define TOOLBAR_ICON_FILE "file"
60 #define TOOLBAR_ICON_TEXT "text"
61 #define TOOLBAR_ICON_ACTION "action"
63 gboolean toolbar_is_duplicate (gint action,
65 static void toolbar_parse_item (XMLFile *file,
68 static gint toolbar_ret_val_from_text (const gchar *text);
69 static gchar *toolbar_ret_text_from_val (gint val);
71 static void toolbar_set_default_main (void);
72 static void toolbar_set_default_compose (void);
76 * Text Database linked to enumarated values in toolbar.h
78 static ToolbarText toolbar_text [] = {
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") },
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") },
104 { "A_SYL_ACTIONS", N_("Sylpheed Actions Feature") },
105 { "A_SEPARATOR", ("") }
108 /* struct holds configuration files and a list of
109 * currently active toolbar items
110 * TOOLBAR_MAIN and TOOLBAR_COMPOSE give as an index
112 static ToolbarConfig toolbar_config[2] = {
113 { "toolbar_main.xml", NULL},
114 { "toolbar_compose.xml", NULL},
117 gint toolbar_ret_val_from_descr(const gchar *descr)
121 for (i = 0; i < N_ACTION_VAL; i++) {
122 if (g_strcasecmp(toolbar_text[i].descr, descr) == 0)
129 gchar *toolbar_ret_descr_from_val(gint val)
131 g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
133 return toolbar_text[val].descr;
136 static gint toolbar_ret_val_from_text(const gchar *text)
140 for (i = 0; i < N_ACTION_VAL; i++) {
141 if (g_strcasecmp(toolbar_text[i].index_str, text) == 0)
148 static gchar *toolbar_ret_text_from_val(gint val)
150 g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
152 return toolbar_text[val].index_str;
155 gboolean toolbar_is_duplicate(gint action, Toolbar source)
159 if ((action == A_SEPARATOR) || (action == A_SYL_ACTIONS))
162 for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
163 ToolbarItem *item = (ToolbarItem*) cur->data;
165 if (item->index == action)
171 GList *toolbar_get_action_items(Toolbar source)
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,
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);
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,
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);
199 static void toolbar_parse_item(XMLFile *file, Toolbar source)
203 ToolbarItem *item = NULL;
205 attr = xml_get_current_tag_attr(file);
206 item = g_new0(ToolbarItem, 1);
208 name = ((XMLAttr *)attr->data)->name;
209 value = ((XMLAttr *)attr->data)->value;
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);
218 attr = g_list_next(attr);
220 if (item->index != -1) {
222 if (!toolbar_is_duplicate(item->index, source))
223 toolbar_config[source].item_list = g_slist_append(toolbar_config[source].item_list,
228 static void toolbar_set_default_main(void)
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") }
253 for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
255 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
257 if (default_toolbar[i].action != A_SEPARATOR) {
259 gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
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);
266 toolbar_item->file = g_strdup(SEPARATOR);
267 toolbar_item->index = A_SEPARATOR;
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);
278 static void toolbar_set_default_compose(void)
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") }
301 for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
303 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
305 if (default_toolbar[i].action != A_SEPARATOR) {
307 gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
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);
314 toolbar_item->file = g_strdup(SEPARATOR);
315 toolbar_item->index = A_SEPARATOR;
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);
326 void toolbar_set_default(Toolbar source)
328 if (source == TOOLBAR_MAIN)
329 toolbar_set_default_main();
330 else if (source == TOOLBAR_COMPOSE)
331 toolbar_set_default_compose();
335 void toolbar_save_config_file(Toolbar source)
340 gchar *fileSpec = NULL;
342 debug_print("save Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
344 fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
345 pfile = prefs_write_open(fileSpec);
349 fprintf(fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
350 conv_get_current_charset_str());
352 fprintf(fp, "<%s>\n", TOOLBAR_TAG_INDEX);
354 for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
355 ToolbarItem *toolbar_item = (ToolbarItem*) cur->data;
357 if (g_strcasecmp(toolbar_item->file, SEPARATOR) != 0)
358 fprintf(fp, "\t<%s %s=\"%s\" %s=\"%s\" %s=\"%s\"/>\n",
360 TOOLBAR_ICON_FILE, toolbar_item->file,
361 TOOLBAR_ICON_TEXT, toolbar_item->text,
363 toolbar_ret_text_from_val(toolbar_item->index));
365 fprintf(fp, "\t<%s/>\n", TOOLBAR_TAG_SEPARATOR);
368 fprintf(fp, "</%s>\n", TOOLBAR_TAG_INDEX);
370 if (prefs_write_close (pfile) < 0 )
371 g_warning("failed to write toolbar configuration to file\n");
373 g_warning("failed to open toolbar configuration file for writing\n");
376 void toolbar_read_config_file(Toolbar source)
378 XMLFile *file = NULL;
379 gchar *fileSpec = NULL;
384 debug_print("read Toolbar Configuration from %s\n", toolbar_config[source].conf_file);
386 fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
387 file = xml_open_file(fileSpec);
390 toolbar_clear_list(source);
394 || (xml_get_dtd(file))
395 || (xml_parse_next_tag(file))
396 || (!xml_compare_tag(file, TOOLBAR_TAG_INDEX))) {
397 xml_close_file(file);
401 attr = xml_get_current_tag_attr(file);
408 if (xml_parse_next_tag(file))
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);
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);
424 xml_close_file(file);
427 if ((!file) || (g_slist_length(toolbar_config[source].item_list) == 0)) {
429 if (source == TOOLBAR_MAIN)
430 toolbar_set_default(TOOLBAR_MAIN);
431 else if (source == TOOLBAR_COMPOSE)
432 toolbar_set_default(TOOLBAR_COMPOSE);
434 g_warning("failed to write Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
438 toolbar_save_config_file(source);
443 * clears list of toolbar items read from configuration files
445 void toolbar_clear_list(Toolbar source)
447 while (toolbar_config[source].item_list != NULL) {
448 ToolbarItem *item = (ToolbarItem*) toolbar_config[source].item_list->data;
450 toolbar_config[source].item_list =
451 g_slist_remove(toolbar_config[source].item_list, item);
459 g_slist_free(toolbar_config[source].item_list);
464 * return list of Toolbar items
466 GSList *toolbar_get_list(Toolbar source)
470 if ((source == TOOLBAR_MAIN) || (source == TOOLBAR_COMPOSE))
471 list = toolbar_config[source].item_list;
476 void toolbar_set_list_item(ToolbarItem *t_item, Toolbar source)
478 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
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;
484 toolbar_config[source].item_list =
485 g_slist_append(toolbar_config[source].item_list,
489 void toolbar_action_execute(GtkWidget *widget,
495 gchar *action, *action_p;
496 gboolean found = FALSE;
499 for (cur = action_list; cur != NULL; cur = cur->next) {
500 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
502 if (widget == act->widget) {
504 for (lop = prefs_common.actions_list; lop != NULL; lop = lop->next) {
505 action = g_strdup((gchar*)lop->data);
507 action_p = strstr(action, ": ");
509 if (g_strcasecmp(act->name, action) == 0) {
523 actions_execute(data, i, widget, source);
525 g_warning ("Error: did not find Sylpheed Action to execute");
528 void toolbar_destroy_items(GSList *t_item_list)
532 while (t_item_list != NULL) {
533 t_item = (ToolbarItem*)t_item_list->data;
535 t_item_list = g_slist_remove(t_item_list, t_item);
537 g_free(t_item->file);
539 g_free(t_item->text);
542 g_slist_free(t_item_list);