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