64c45f7310784672b42f20ef6fd468859cd0c936
[claws.git] / src / foldersel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkmain.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkctree.h>
27 #include <gtk/gtkwindow.h>
28 #include <gtk/gtkvbox.h>
29 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtkentry.h>
31 #include <gtk/gtkhbbox.h>
32 #include <gtk/gtksignal.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #include <errno.h>
40
41 #include "intl.h"
42 #include "main.h"
43 #include "utils.h"
44 #include "gtkutils.h"
45 #include "stock_pixmap.h"
46 #include "foldersel.h"
47 #include "alertpanel.h"
48 #include "manage_window.h"
49 #include "folder.h"
50
51 static GdkPixmap *folderxpm;
52 static GdkBitmap *folderxpmmask;
53 static GdkPixmap *folderopenxpm;
54 static GdkBitmap *folderopenxpmmask;
55
56 static GtkWidget *window;
57 static GtkWidget *ctree;
58 static GtkWidget *entry;
59 static GtkWidget *ok_button;
60 static GtkWidget *cancel_button;
61
62 static FolderItem *folder_item;
63
64 static gboolean cancelled;
65
66 static void foldersel_create    (void);
67 static void foldersel_init      (void);
68 static void foldersel_set_tree  (Folder         *cur_folder);
69 static void foldersel_selected  (GtkCList       *clist,
70                                  gint            row,
71                                  gint            column,
72                                  GdkEvent       *event,
73                                  gpointer        data);
74
75 static void foldersel_ok        (GtkButton      *button,
76                                  gpointer        data);
77 static void foldersel_cancel    (GtkButton      *button,
78                                  gpointer        data);
79 static void foldersel_activated (void);
80 static gint delete_event        (GtkWidget      *widget,
81                                  GdkEventAny    *event,
82                                  gpointer        data);
83 static void key_pressed         (GtkWidget      *widget,
84                                  GdkEventKey    *event,
85                                  gpointer        data);
86
87 FolderItem *foldersel_folder_sel(Folder *cur_folder,
88                                  const gchar *default_folder)
89 {
90         GtkCTreeNode *node;
91
92         if (!window) {
93                 foldersel_create();
94                 foldersel_init();
95         } else
96                 gtk_widget_show(window);
97         manage_window_set_transient(GTK_WINDOW(window));
98
99         foldersel_set_tree(cur_folder);
100
101         if (folder_item) {
102                 node = gtk_ctree_find_by_row_data
103                         (GTK_CTREE(ctree), NULL, folder_item);
104                 if (node) {
105                         gint row;
106
107                         row = gtkut_ctree_get_nth_from_node
108                                 (GTK_CTREE(ctree), node);
109                         gtk_clist_select_row(GTK_CLIST(ctree), row, -1);
110                         gtkut_clist_set_focus_row(GTK_CLIST(ctree), row);
111                         gtk_ctree_node_moveto(GTK_CTREE(ctree), node, -1,
112                                               0.5, 0);
113                 }
114         }
115         gtk_widget_grab_focus(ok_button);
116         gtk_widget_grab_focus(ctree);
117
118         cancelled = FALSE;
119
120         gtk_main();
121
122         gtk_widget_hide(window);
123         gtk_entry_set_text(GTK_ENTRY(entry), "");
124         gtk_clist_clear(GTK_CLIST(ctree));
125
126         if (!cancelled && folder_item && folder_item->path)
127                 return folder_item;
128         else
129                 return NULL;
130 }
131
132 static void foldersel_create(void)
133 {
134         GtkWidget *vbox;
135         GtkWidget *scrolledwin;
136         GtkWidget *confirm_area;
137
138         window = gtk_window_new(GTK_WINDOW_DIALOG);
139         gtk_window_set_title(GTK_WINDOW(window), _("Select folder"));
140         gtk_widget_set_usize(window, 300, 400);
141         gtk_container_set_border_width(GTK_CONTAINER(window), BORDER_WIDTH);
142         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
143         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
144         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, TRUE);
145         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
146                            GTK_SIGNAL_FUNC(delete_event), NULL);
147         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
148                            GTK_SIGNAL_FUNC(key_pressed), NULL);
149         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
150                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
151         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
152                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
153
154         vbox = gtk_vbox_new(FALSE, 4);
155         gtk_container_add(GTK_CONTAINER(window), vbox);
156
157         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
158         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
159                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
160         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
161
162         ctree = gtk_ctree_new(1, 0);
163         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
164                                             GTK_CLIST(ctree)->vadjustment);
165         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
166         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
167         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
168         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
169                                      GTK_CTREE_EXPANDER_SQUARE);
170         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
171         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[0].button,
172                                GTK_CAN_FOCUS);
173         /* gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
174                            GTK_SIGNAL_FUNC(foldersel_selected), NULL); */
175         gtk_signal_connect(GTK_OBJECT(ctree), "select_row",
176                            GTK_SIGNAL_FUNC(foldersel_selected), NULL);
177
178         entry = gtk_entry_new();
179         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
180         gtk_signal_connect(GTK_OBJECT(entry), "activate",
181                            GTK_SIGNAL_FUNC(foldersel_activated), NULL);
182
183         gtkut_button_set_create(&confirm_area,
184                                 &ok_button,     _("OK"),
185                                 &cancel_button, _("Cancel"),
186                                 NULL, NULL);
187
188         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
189         gtk_widget_grab_default(ok_button);
190
191         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
192                            GTK_SIGNAL_FUNC(foldersel_ok), NULL);
193         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
194                            GTK_SIGNAL_FUNC(foldersel_cancel), NULL);
195
196         gtk_widget_show_all(window);
197 }
198
199 static void foldersel_init(void)
200 {
201         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE,
202                          &folderxpm, &folderxpmmask);
203         stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN,
204                          &folderopenxpm, &folderopenxpmmask);
205 }
206
207 static gboolean foldersel_gnode_func(GtkCTree *ctree, guint depth,
208                                      GNode *gnode, GtkCTreeNode *cnode,
209                                      gpointer data)
210 {
211         FolderItem *item = FOLDER_ITEM(gnode->data);
212         gchar *name;
213
214         switch (item->stype) {
215         case F_INBOX:
216                 name = _("Inbox");
217                 break;
218         case F_OUTBOX:
219                 name = _("Outbox");
220                 break;
221         case F_QUEUE:
222                 name = _("Queue");
223                 break;
224         case F_TRASH:
225                 name = _("Trash");
226                 break;
227         case F_DRAFT:
228                 name = _("Draft");
229                 break;
230         default:
231                 name = item->name;
232
233                 if (!item->parent) {
234                         switch (item->folder->type) {
235                         case F_MBOX:
236                                 Xstrcat_a(name, name, " (MBOX)", ); break;
237                         case F_MH:
238                                 Xstrcat_a(name, name, " (MH)", ); break;
239                         case F_IMAP:
240                                 Xstrcat_a(name, name, " (IMAP4)", ); break;
241                         case F_NEWS:
242                                 Xstrcat_a(name, name, " (News)", ); break;
243                         default:
244                                 break;
245                         }
246                 }
247         }
248
249         gtk_ctree_node_set_row_data(ctree, cnode, item);
250         gtk_ctree_set_node_info(ctree, cnode, name,
251                                 FOLDER_SPACING,
252                                 folderxpm, folderxpmmask,
253                                 folderopenxpm, folderopenxpmmask,
254                                 FALSE, FALSE);
255
256         return TRUE;
257 }
258
259 static void foldersel_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
260                                   gpointer data)
261 {
262         if (GTK_CTREE_ROW(node)->children)
263                 gtk_ctree_expand(ctree, node);
264 }
265
266 #define SET_SPECIAL_FOLDER(item) \
267 { \
268         if (item) { \
269                 GtkCTreeNode *node_, *sibling; \
270  \
271                 node_ = gtk_ctree_find_by_row_data \
272                         (GTK_CTREE(ctree), node, item); \
273                 if (!node_) \
274                         g_warning("%s not found.\n", item->path); \
275                 else { \
276                         if (!prev) \
277                                 sibling = GTK_CTREE_ROW(node)->children; \
278                         else \
279                                 sibling = GTK_CTREE_ROW(prev)->sibling; \
280                         if (node_ != sibling) \
281                                 gtk_ctree_move(GTK_CTREE(ctree), \
282                                                node_, node, sibling); \
283                 } \
284  \
285                 prev = node_; \
286         } \
287 }
288
289 static void foldersel_set_tree(Folder *cur_folder)
290 {
291         Folder *folder;
292         GtkCTreeNode *node;
293         GList *list;
294
295         list = folder_get_list();
296
297         gtk_clist_freeze(GTK_CLIST(ctree));
298
299         for (; list != NULL; list = list->next) {
300                 GtkCTreeNode *prev = NULL;
301
302                 folder = FOLDER(list->data);
303                 g_return_if_fail(folder != NULL);
304
305                 if (folder->type == F_NEWS) continue;
306                 if (cur_folder) {
307                         if (cur_folder->type != folder->type) continue;
308                         if (cur_folder->type == F_IMAP) {
309                                 if (cur_folder->account != folder->account)
310                                         continue;
311                         }
312                 }
313
314                 node = gtk_ctree_insert_gnode(GTK_CTREE(ctree), NULL, NULL,
315                                               folder->node,
316                                               foldersel_gnode_func,
317                                               NULL);
318                 gtk_ctree_sort_recursive(GTK_CTREE(ctree), node);
319                 SET_SPECIAL_FOLDER(folder->inbox);
320                 SET_SPECIAL_FOLDER(folder->outbox);
321                 SET_SPECIAL_FOLDER(folder->draft);
322                 SET_SPECIAL_FOLDER(folder->queue);
323                 SET_SPECIAL_FOLDER(folder->trash);
324                 gtk_ctree_pre_recursive(GTK_CTREE(ctree), node,
325                                         foldersel_expand_func,
326                                         NULL);
327         }
328
329         gtk_clist_thaw(GTK_CLIST(ctree));
330 }
331
332 static void foldersel_selected(GtkCList *clist, gint row, gint column,
333                                GdkEvent *event, gpointer data)
334 {
335         FolderItem *item;
336         GdkEventButton *ev = (GdkEventButton *)event;
337
338         item = gtk_clist_get_row_data(clist, row);
339         if (item) gtk_entry_set_text(GTK_ENTRY(entry),
340                                      item->path ? item->path : "");
341
342         if (ev && GDK_2BUTTON_PRESS == ev->type)
343                 gtk_button_clicked(GTK_BUTTON(ok_button));
344 }
345
346 static void foldersel_ok(GtkButton *button, gpointer data)
347 {
348         GList *list;
349
350         list = GTK_CLIST(ctree)->selection;
351         if (list)
352                 folder_item = gtk_ctree_node_get_row_data
353                         (GTK_CTREE(ctree), GTK_CTREE_NODE(list->data));
354
355         gtk_main_quit();
356 }
357
358 static void foldersel_cancel(GtkButton *button, gpointer data)
359 {
360         cancelled = TRUE;
361         gtk_main_quit();
362 }
363
364 static void foldersel_activated(void)
365 {
366         gtk_button_clicked(GTK_BUTTON(ok_button));
367 }
368
369 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
370 {
371         foldersel_cancel(NULL, NULL);
372         return TRUE;
373 }
374
375 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
376 {
377         if (event && event->keyval == GDK_Escape)
378                 foldersel_cancel(NULL, NULL);
379 }