aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-folder-selector.c
blob: 14169c94cce6c79a1cb000427a1d4a5a32aa4fed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2003 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <libgnome/gnome-i18n.h>

#include <gal/util/e-util.h>
#include <gal/widgets/e-gui-utils.h>

#include <gtk/gtkentry.h>
#include <gtk/gtkbox.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkstock.h>

#include <camel/camel-url.h>
#include <camel/camel-store.h>
#include <camel/camel-session.h>

#include "em-folder-tree.h"
#include "em-folder-selector.h"

#define d(x) x


extern CamelSession *session;


static void em_folder_selector_class_init (EMFolderSelectorClass *klass);
static void em_folder_selector_init (EMFolderSelector *emfs);
static void em_folder_selector_destroy (GtkObject *obj);
static void em_folder_selector_finalize (GObject *obj);


static GtkDialogClass *parent_class = NULL;


GType
em_folder_selector_get_type (void)
{
    static GType type = 0;
    
    if (!type) {
        static const GTypeInfo info = {
            sizeof (EMFolderSelectorClass),
            NULL, /* base_class_init */
            NULL, /* base_class_finalize */
            (GClassInitFunc) em_folder_selector_class_init,
            NULL, /* class_finalize */
            NULL, /* class_data */
            sizeof (EMFolderSelector),
            0,    /* n_preallocs */
            (GInstanceInitFunc) em_folder_selector_init,
        };
        
        type = g_type_register_static (GTK_TYPE_DIALOG, "EMFolderSelector", &info, 0);
    }
    
    return type;
}

static void
em_folder_selector_class_init (EMFolderSelectorClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (klass);
    
    parent_class = g_type_class_ref (GTK_TYPE_DIALOG);
    
    object_class->finalize = em_folder_selector_finalize;
    gtk_object_class->destroy = em_folder_selector_destroy;
}

static void
em_folder_selector_init (EMFolderSelector *emfs)
{
    emfs->selected_path = NULL;
    emfs->selected_uri = NULL;
}

static void
em_folder_selector_destroy (GtkObject *obj)
{
    GTK_OBJECT_CLASS (parent_class)->destroy (obj);
}

static void
em_folder_selector_finalize (GObject *obj)
{
    EMFolderSelector *emfs = (EMFolderSelector *) obj;
    
    g_free (emfs->selected_path);
    g_free (emfs->selected_uri);
    
    G_OBJECT_CLASS (parent_class)->finalize (obj);
}

static void
emfs_response (GtkWidget *dialog, int response, EMFolderSelector *emfs)
{
    EMFolderTreeModel *model;
    const char *path, *uri;
    EMFolderTree *emft;
    
    if (response != EM_FOLDER_SELECTOR_RESPONSE_NEW)
        return;
    
    model = em_folder_tree_get_model (emfs->emft);
    emft = (EMFolderTree *) em_folder_tree_new_with_model (model);
    dialog = em_folder_selector_create_new (emft, 0, _("Create New Folder"), _("Specify where to create the folder:"));
    gtk_window_set_transient_for ((GtkWindow *) dialog, (GtkWindow *) emfs);
    
    if (gtk_dialog_run ((GtkDialog *) dialog) == GTK_RESPONSE_OK) {
        uri = em_folder_selector_get_selected_uri ((EMFolderSelector *) dialog);
        path = em_folder_selector_get_selected_path ((EMFolderSelector *) dialog);
        
        if (em_folder_tree_create_folder (emfs->emft, path, uri))
            em_folder_tree_set_selected (emfs->emft, uri);
    }
    
    gtk_widget_destroy (dialog);
    
    g_signal_stop_emission_by_name (emfs, "response");
}

static void
emfs_create_name_changed (GtkEntry *entry, EMFolderSelector *emfs)
{
    const char *path, *text = NULL;
    gboolean active;
    
    if (emfs->name_entry->text_length > 0)
        text = gtk_entry_get_text (emfs->name_entry);
    
    path = em_folder_tree_get_selected_path (emfs->emft);
    
    active = text && path && !strchr (text, '/');
    
    gtk_dialog_set_response_sensitive ((GtkDialog *) emfs, GTK_RESPONSE_OK, active);
}

static void
folder_selected_cb (EMFolderTree *emft, const char *path, const char *uri, EMFolderSelector *emfs)
{
    if (emfs->name_entry)
        emfs_create_name_changed (emfs->name_entry, emfs);
    else
        gtk_dialog_set_response_sensitive (GTK_DIALOG (emfs), GTK_RESPONSE_OK, TRUE);
}

void
em_folder_selector_construct (EMFolderSelector *emfs, EMFolderTree *emft, guint32 flags, const char *title, const char *text)
{
    GtkWidget *scrolled_window;
    GtkWidget *label;
    
    gtk_window_set_default_size (GTK_WINDOW (emfs), 350, 300);
    gtk_window_set_modal (GTK_WINDOW (emfs), TRUE);
    gtk_window_set_title (GTK_WINDOW (emfs), title);
    gtk_container_set_border_width (GTK_CONTAINER (emfs), 6);
    
    gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (emfs)->vbox), 6);
    gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (emfs)->vbox), 6);
    
    emfs->flags = flags;
    if (flags & EM_FOLDER_SELECTOR_CAN_CREATE) {
        gtk_dialog_add_button (GTK_DIALOG (emfs), GTK_STOCK_NEW, EM_FOLDER_SELECTOR_RESPONSE_NEW);
        g_signal_connect (emfs, "response", G_CALLBACK (emfs_response), emfs);
    }
    
    gtk_dialog_add_buttons (GTK_DIALOG (emfs), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
    
    gtk_dialog_set_response_sensitive (GTK_DIALOG (emfs), GTK_RESPONSE_OK, FALSE);
    gtk_dialog_set_default_response (GTK_DIALOG (emfs), GTK_RESPONSE_OK);
    
    emfs->emft = emft;
    gtk_widget_show ((GtkWidget *) emft);
    
    g_signal_connect (emfs->emft, "folder-selected", G_CALLBACK (folder_selected_cb), emfs);
    
    scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_widget_show (scrolled_window);
    
    gtk_container_add (GTK_CONTAINER (scrolled_window), (GtkWidget *) emft);
        
    gtk_box_pack_end (GTK_BOX (GTK_DIALOG (emfs)->vbox), scrolled_window, TRUE, TRUE, 6);
    
    if (text != NULL) {
        label = gtk_label_new (text);
        gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT); 
        gtk_widget_show (label);
        
        gtk_box_pack_end (GTK_BOX (GTK_DIALOG (emfs)->vbox), label, FALSE, TRUE, 6);
    }
    
    GTK_WIDGET_SET_FLAGS ((GtkWidget *) emfs->emft, GTK_CAN_FOCUS);
    gtk_widget_grab_focus ((GtkWidget *) emfs->emft);
}

GtkWidget *
em_folder_selector_new (EMFolderTree *emft, guint32 flags, const char *title, const char *text)
{
    EMFolderSelector *emfs;
    
    emfs = g_object_new (em_folder_selector_get_type (), NULL);
    em_folder_selector_construct (emfs, emft, flags, title, text);
    
    return (GtkWidget *) emfs;
}


static void
emfs_create_name_activate (GtkEntry *entry, EMFolderSelector *emfs)
{
    g_signal_emit_by_name (emfs, "response", GTK_RESPONSE_OK);
}

GtkWidget *
em_folder_selector_create_new (EMFolderTree *emft, guint32 flags, const char *title, const char *text)
{
    EMFolderSelector *emfs;
    GtkWidget *hbox, *w;
    
    /* remove the CREATE flag if it is there since that's the
     * whole purpose of this dialog */
    flags &= ~EM_FOLDER_SELECTOR_CAN_CREATE;
    
    emfs = g_object_new (em_folder_selector_get_type (), NULL);
    em_folder_selector_construct (emfs, emft, flags, title, text);
    
    hbox = gtk_hbox_new (FALSE, 0);
    w = gtk_label_new_with_mnemonic (_("Folder _name:"));
    gtk_box_pack_start ((GtkBox *) hbox, w, FALSE, FALSE, 6);
    emfs->name_entry = (GtkEntry *) gtk_entry_new ();
    g_signal_connect (emfs->name_entry, "changed", G_CALLBACK (emfs_create_name_changed), emfs);
    g_signal_connect (emfs->name_entry, "activate", G_CALLBACK (emfs_create_name_activate), emfs);
    gtk_box_pack_start ((GtkBox *) hbox, (GtkWidget *) emfs->name_entry, TRUE, FALSE, 6);
    gtk_widget_show_all (hbox);
    
    gtk_box_pack_start ((GtkBox *) ((GtkDialog *) emfs)->vbox, hbox, FALSE, TRUE, 0);
    
    return (GtkWidget *) emfs;
}


void
em_folder_selector_set_selected (EMFolderSelector *emfs, const char *uri)
{
    em_folder_tree_set_selected (emfs->emft, uri);
}

void
em_folder_selector_set_selected_list (EMFolderSelector *emfs, GList *list)
{
    em_folder_tree_set_selected_list (emfs->emft, list);
}

const char *
em_folder_selector_get_selected_uri (EMFolderSelector *emfs)
{
    const char *uri, *name;
    
    if (!(uri = em_folder_tree_get_selected_uri (emfs->emft))) {
        d(printf ("no selected folder?\n"));
        return NULL;
    }
    
    if (uri && emfs->name_entry) {
        CamelProvider *provider;
        CamelException ex;
        CamelURL *url;
        char *newpath;
        
        camel_exception_init (&ex);
        provider = camel_session_get_provider (session, uri, &ex);
        camel_exception_clear (&ex);
        
        name = gtk_entry_get_text (emfs->name_entry);
        
        url = camel_url_new (uri, NULL);
        if (provider && (provider->url_flags & CAMEL_URL_FRAGMENT_IS_PATH)) {
            if (url->fragment)
                newpath = g_strdup_printf ("%s/%s", url->fragment, name);
            else
                newpath = g_strdup (name);
            
            camel_url_set_fragment (url, newpath);
        } else {
            newpath = g_strdup_printf ("%s/%s", url->path ? url->path : "", name);
            camel_url_set_path (url, newpath);
        }
        
        g_free (emfs->selected_path);
        emfs->selected_path = newpath;
        
        g_free (emfs->selected_uri);
        emfs->selected_uri = camel_url_to_string (url, 0);
        
        camel_url_free (url);
        uri = emfs->selected_uri;
    }
    
    return uri;
}

GList *
em_folder_selector_get_selected_uris (EMFolderSelector *emfs)
{
    return em_folder_tree_get_selected_uris (emfs->emft);
}

GList *
em_folder_selector_get_selected_paths (EMFolderSelector *emfs)
{
    return em_folder_tree_get_selected_paths (emfs->emft);
}

const char *
em_folder_selector_get_selected_path (EMFolderSelector *emfs)
{
    const char *path;
    
    if (emfs->selected_path) {
        /* already did the work in a previous call */
        return emfs->selected_path;
    }
    
    if (!(path = em_folder_tree_get_selected_path (emfs->emft))) {
        d(printf ("no selected folder?\n"));
        return NULL;
    }
    
    if (path && emfs->name_entry) {
        const char *name;
        char *newpath;
        
        name = gtk_entry_get_text (emfs->name_entry);
        if (strcmp (path, "/") != 0)
            newpath = g_strdup_printf ("%s/%s", path, name);
        else
            newpath = g_strdup_printf ("/%s", name);
        
        path = emfs->selected_path = newpath;
    }
    
    return path;
}