Included import of LDIF files.
[claws.git] / src / template_select.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
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 #include "defs.h"
21
22 #include <gtk/gtk.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <dirent.h>
25 #include <sys/stat.h>
26
27 #include "intl.h"
28 #include "manage_window.h"
29 #include "main.h"
30 #include "utils.h"
31 #include "alertpanel.h"
32 #include "template.h"
33
34 static struct Template_select {
35         GtkWidget *window;
36         GtkWidget *ok_btn;
37         GtkWidget *clist_tmpls;
38
39         GSList *tmpl_list;
40         void (*template_cb)(gchar *s, gpointer data);
41         gpointer save_data;
42 } tmplsl;
43
44 /* widget creating functions */
45 static void template_select_create_window(void);
46 static void template_select_setup_window(void);
47
48 /* callbacks */
49 static gint template_select_deleted_cb(GtkWidget *widget,
50                                        GdkEventAny *event,
51                                        gpointer data);
52 static void template_select_key_pressed_cb(GtkWidget *widget,
53                                            GdkEventKey *event,
54                                            gpointer data);
55 static void template_select_cancel_cb(void);
56 static void template_select_ok_cb(void);
57
58 /* Called from elsewhere */
59 void template_select (void (*template_apply_cb)(gchar *s, gpointer data), 
60                       gpointer data)
61 {
62         tmplsl.template_cb = template_apply_cb;
63         tmplsl.save_data = data;
64
65         tmplsl.tmpl_list = template_read_config();
66
67         inc_autocheck_timer_remove();
68
69         if(!tmplsl.window){
70                 template_select_create_window();
71         }
72         template_select_setup_window();
73         gtk_widget_show(tmplsl.window);
74 }
75
76 void template_select_create_window(void)
77 {
78         /* window structure ;) */
79         GtkWidget *window;
80         GtkWidget   *vbox1;
81         GtkWidget     *scroll1;
82         GtkWidget       *clist_tmpls;
83         GtkWidget     *confirm_area;
84         GtkWidget       *ok_btn;
85         GtkWidget       *cancel_btn;
86
87         gchar *title[] = {_("Registered templates")};
88
89         /* main window */
90         window = gtk_window_new(GTK_WINDOW_DIALOG);
91         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
92         gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
93         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
94         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
95         gtk_window_set_default_size(GTK_WINDOW(window), 200, 300);
96
97         /* vbox to handle template name and content */
98         vbox1 = gtk_vbox_new(FALSE, 6);
99         gtk_widget_show(vbox1);
100         gtk_container_add(GTK_CONTAINER(window), vbox1);
101
102         /* templates list */
103         scroll1 = gtk_scrolled_window_new(NULL, NULL);
104         gtk_widget_show(scroll1);
105         gtk_widget_set_usize(scroll1, -1, 150);
106         gtk_box_pack_start(GTK_BOX(vbox1), scroll1, TRUE, TRUE, 0);
107         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
108                                        GTK_POLICY_AUTOMATIC,
109                                        GTK_POLICY_AUTOMATIC);
110
111         clist_tmpls = gtk_clist_new_with_titles(1, title);
112         gtk_widget_show(clist_tmpls);
113         gtk_container_add(GTK_CONTAINER(scroll1), clist_tmpls);
114         gtk_clist_set_column_width(GTK_CLIST(clist_tmpls), 0, 80);
115         gtk_clist_set_selection_mode(GTK_CLIST(clist_tmpls), GTK_SELECTION_BROWSE);
116         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_tmpls)->column[0].button,
117                                GTK_CAN_FOCUS);
118
119         /* ok | cancel */
120         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
121                                 &cancel_btn, _("Cancel"), NULL, NULL);
122         gtk_widget_show(confirm_area);
123         gtk_box_pack_end(GTK_BOX(vbox1), confirm_area, FALSE, FALSE, 0);
124         gtk_widget_grab_default(ok_btn);
125
126         gtk_window_set_title(GTK_WINDOW(window), _("Templates"));
127
128         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
129                            GTK_SIGNAL_FUNC(template_select_deleted_cb), NULL);
130         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
131                            GTK_SIGNAL_FUNC(template_select_key_pressed_cb), NULL);
132         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
133                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
134         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
135                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
136         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
137                            GTK_SIGNAL_FUNC(template_select_ok_cb), NULL);
138         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
139                             GTK_SIGNAL_FUNC(template_select_cancel_cb), NULL);
140
141         tmplsl.window = window;
142         tmplsl.ok_btn = ok_btn;
143         tmplsl.clist_tmpls = clist_tmpls;
144 }
145
146 void template_select_setup_window(void)
147 {
148         GtkCList *clist = GTK_CLIST(tmplsl.clist_tmpls);
149         GSList *cur;
150         gint row;
151         Template *tmpl;
152         gchar *cond_str[1];
153
154         manage_window_set_transient(GTK_WINDOW(tmplsl.window));
155         gtk_widget_grab_focus(tmplsl.ok_btn);
156
157         gtk_clist_freeze(clist);
158         gtk_clist_clear(clist);
159
160         for (cur = tmplsl.tmpl_list; cur != NULL; cur = cur->next) {
161                 tmpl = cur->data;
162                 cond_str[0] = tmpl->name;
163                 row = gtk_clist_append(clist, cond_str);
164                 gtk_clist_set_row_data(clist, row, tmpl);
165         }
166
167         gtk_clist_thaw(clist);
168 }
169
170 static gint template_select_deleted_cb(GtkWidget *widget, GdkEventAny *event,
171                                        gpointer data)
172 {
173         template_select_cancel_cb();
174         return TRUE;
175 }
176
177 static void template_select_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
178                                            gpointer data)
179 {
180         if (event && event->keyval == GDK_Escape)
181                 template_select_cancel_cb();
182 }
183
184 static void template_select_ok_cb(void)
185 {
186         GtkCList *clist = GTK_CLIST(tmplsl.clist_tmpls);
187         gchar *s;
188         Template *tmpl;
189         gint row;
190
191         if (!clist->selection) return;
192         row = GPOINTER_TO_INT(clist->selection->data);
193         tmpl = gtk_clist_get_row_data(clist, row);
194         s = g_strdup(tmpl->value);
195
196         template_clear_config(tmplsl.tmpl_list);
197         gtk_widget_hide(tmplsl.window);
198         inc_autocheck_timer_set();
199         tmplsl.template_cb(s, tmplsl.save_data);
200 }
201
202 static void template_select_cancel_cb(void)
203 {
204         template_clear_config(tmplsl.tmpl_list);
205         gtk_widget_hide(tmplsl.window);
206         inc_autocheck_timer_set();
207         tmplsl.template_cb(NULL, tmplsl.save_data);
208 }