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