aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-folder-selection.c
blob: 4a736235295bb0c1906beb6fe7d6325a590d1459 (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
/* -*- 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 <gtk/gtkdialog.h>
#include <gtk/gtkstock.h>

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

#include "mail-component.h"
#include "mail-tools.h"


static void
folder_selected_cb (EMFolderTree *emft, const char *path, const char *uri, GtkDialog *dialog)
{
    if (!path || !strcmp (path, "/"))
        gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE);
    else
        gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, TRUE);
}

static GtkWidget *
create_dialog (GtkWindow *parent, EMFolderTree *emft, const char *title, const char *default_uri, gboolean allow_create)
{
    GtkWidget *dialog;
    
    dialog = gtk_dialog_new ();
    
    if (parent)
        gtk_window_set_transient_for ((GtkWindow *) dialog, parent);
    
    gtk_window_set_default_size (GTK_WINDOW (dialog), 350, 300);
    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
    gtk_window_set_title (GTK_WINDOW (dialog), title);
    gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
    gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), 6);
    gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 6);
    
    if (allow_create)
        gtk_dialog_add_buttons (GTK_DIALOG (dialog), GTK_STOCK_NEW, GTK_RESPONSE_APPLY, NULL);
    
    gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                GTK_STOCK_OK, GTK_RESPONSE_OK,
                NULL);
    
    if (default_uri == NULL)
        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
    else
        em_folder_tree_set_selected (emft, default_uri);
    
    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
    
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), GTK_WIDGET (emft), TRUE, TRUE, 6);
    
    g_signal_connect (emft, "folder-selected", G_CALLBACK (folder_selected_cb), dialog);
    
    return dialog;
}

CamelFolder *
em_folder_selection_run_dialog (GtkWindow *parent_window, const char *title, CamelFolder *default_folder)
{
    CamelFolder *selected_folder;
    EMFolderTreeModel *model;
    GtkWidget *dialog;
    GtkWidget *emft;
    const char *uri;
    int response;
    
    model = mail_component_get_tree_model (mail_component_peek ());
    emft = em_folder_tree_new_with_model (model);
    gtk_widget_show (emft);
    
    if (default_folder) {
        char *default_uri;
        
        default_uri = mail_tools_folder_to_url (default_folder);
        dialog = create_dialog (parent_window, (EMFolderTree *) emft, title, default_uri, FALSE);
        g_free (default_uri);
    } else {
        dialog = create_dialog (parent_window, (EMFolderTree *) emft, title, NULL, FALSE);
    }
    
    response = gtk_dialog_run (GTK_DIALOG (dialog));
    
    if (response != GTK_RESPONSE_OK) {
        gtk_widget_destroy (dialog);
        return NULL;
    }
    
    if (!(uri = em_folder_tree_get_selected_uri ((EMFolderTree *) emft))) {
        gtk_widget_destroy (dialog);
        return NULL;
    }
    
    selected_folder = mail_tool_uri_to_folder (uri, 0, NULL);
    gtk_widget_destroy (dialog);
    
    return selected_folder;
}

/* FIXME: This isn't the way to do it, but then neither is the above, really ... */
char *
em_folder_selection_run_dialog_uri (GtkWindow *parent_window, const char *title, const char *default_uri)
{
    EMFolderTreeModel *model;
    GtkWidget *dialog;
    GtkWidget *emft;
    const char *uri;
    int response;
    char *ret;
    
    model = mail_component_get_tree_model (mail_component_peek ());
    emft = em_folder_tree_new_with_model (model);
    gtk_widget_show (emft);
    
    if (default_uri)
        dialog = create_dialog (parent_window, (EMFolderTree *) emft, title, default_uri, FALSE);
    else
        dialog = create_dialog (parent_window, (EMFolderTree *) emft, title, NULL, FALSE);
    
    response = gtk_dialog_run (GTK_DIALOG (dialog));
    
    if (response != GTK_RESPONSE_OK) {
        gtk_widget_destroy (dialog);
        return NULL;
    }
    
    if (!(uri = em_folder_tree_get_selected_uri ((EMFolderTree *) emft))) {
        gtk_widget_destroy (dialog);
        return NULL;
    }
    
    ret = g_strdup (uri);
    gtk_widget_destroy (dialog);
    
    return ret;
}


struct _select_folder_data {
    void (*done) (const char *uri, void *data);
    void *data;
};

static void
emfs_response (GtkWidget *dialog, int response, struct _select_folder_data *d)
{
    EMFolderTree *emft = g_object_get_data ((GObject *) dialog, "emft");
    const char *uri;
    
    gtk_widget_hide (dialog);
    
    if (response == GTK_RESPONSE_OK) {
        uri = em_folder_tree_get_selected_uri (emft);
        d->done (uri, d->data);
    }
    
    gtk_widget_destroy (dialog);
}

void
em_select_folder (GtkWindow *parent_window, const char *title, const char *default_uri,
          void (*done) (const char *uri, void *user_data), void *user_data)
{
    struct _select_folder_data *d;
    EMFolderTreeModel *model;
    GtkWidget *dialog;
    GtkWidget *emft;
    
    model = mail_component_get_tree_model (mail_component_peek ());
    emft = em_folder_tree_new_with_model (model);
    gtk_widget_show (emft);
    
    if (default_uri)
        dialog = create_dialog (parent_window, (EMFolderTree *) emft, title, default_uri, FALSE);
    else
        dialog = create_dialog (parent_window, (EMFolderTree *) emft, title, NULL, FALSE);
    
    d = g_malloc0 (sizeof (*d));
    d->data = user_data;
    d->done = done;
    
    /* ugh, painful api ... */
    g_object_set_data ((GObject *) dialog, "emft", emft);
    g_object_set_data_full ((GObject *) dialog, "emfs_data", d, g_free);
    g_signal_connect (dialog, "response", G_CALLBACK (emfs_response), d);
    
    gtk_widget_show (dialog);
}