aboutsummaryrefslogtreecommitdiffstats
path: root/mail/e-mail-folder-create-dialog.c
blob: bca14de1d9894c1de23140051cf9eb511a188621 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * e-mail-folder-create-dialog.c
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "e-mail-folder-create-dialog.h"

#include <config.h>
#include <glib/gi18n-lib.h>

#include "em-vfolder-editor-rule.h"
#include "mail-vfolder-ui.h"

#define E_MAIL_FOLDER_CREATE_DIALOG_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_FOLDER_CREATE_DIALOG, EMailFolderCreateDialogPrivate))

typedef struct _AsyncContext AsyncContext;

struct _EMailFolderCreateDialogPrivate {
    EMailUISession *session;
    GtkWidget *name_entry;
};

struct _AsyncContext {
    EMailFolderCreateDialog *dialog;
    EActivity *activity;
};

enum {
    PROP_0,
    PROP_SESSION
};

enum {
    FOLDER_CREATED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE (
    EMailFolderCreateDialog,
    e_mail_folder_create_dialog,
    EM_TYPE_FOLDER_SELECTOR)

static void
async_context_free (AsyncContext *async_context)
{
    g_clear_object (&async_context->dialog);
    g_clear_object (&async_context->activity);

    g_slice_free (AsyncContext, async_context);
}

static void
mail_folder_create_dialog_create_folder_cb (GObject *source_object,
                                            GAsyncResult *result,
                                            gpointer user_data)
{
    EMailFolderCreateDialog *dialog;
    EActivity *activity;
    EAlertSink *alert_sink;
    GdkWindow *gdk_window;
    AsyncContext *async_context;
    GError *local_error = NULL;

    async_context = (AsyncContext *) user_data;

    dialog = async_context->dialog;
    activity = async_context->activity;
    alert_sink = e_activity_get_alert_sink (activity);

    /* Set the cursor back to normal. */
    gdk_window = gtk_widget_get_window (GTK_WIDGET (dialog));
    gdk_window_set_cursor (gdk_window, NULL);

    e_mail_store_create_folder_finish (
        CAMEL_STORE (source_object), result, &local_error);

    /* Ignore cancellations. */
    if (e_activity_handle_cancellation (activity, local_error)) {
        g_error_free (local_error);

    } else if (local_error != NULL) {
        e_alert_submit (
            alert_sink,
            "system:simple-error",
            local_error->message, NULL);
        g_error_free (local_error);

    } else {
        gtk_widget_destroy (GTK_WIDGET (dialog));
    }

    async_context_free (async_context);
}

static void
mail_folder_create_dialog_create_folder (EMailFolderCreateDialog *dialog)
{
    CamelStore *store = NULL;
    gchar *create_folder_name;
    gchar *parent_folder_name = NULL;
    const gchar *text;

    em_folder_selector_get_selected (
        EM_FOLDER_SELECTOR (dialog), &store, &parent_folder_name);

    g_return_if_fail (store != NULL);

    text = gtk_entry_get_text (GTK_ENTRY (dialog->priv->name_entry));

    if (parent_folder_name != NULL) {
        create_folder_name = g_strconcat (
            parent_folder_name, "/", text, NULL);
    } else {
        create_folder_name = g_strdup (text);
    }

    /* For the vfolder store, we just open the editor window. */
    if (CAMEL_IS_VEE_STORE (store)) {
        EMailUISession *session;
        EFilterRule *rule;

        session = e_mail_folder_create_dialog_get_session (dialog);
        rule = em_vfolder_editor_rule_new (E_MAIL_SESSION (session));
        e_filter_rule_set_name (rule, create_folder_name);
        vfolder_gui_add_rule (EM_VFOLDER_RULE (rule));

        gtk_widget_destroy (GTK_WIDGET (dialog));

    } else {
        AsyncContext *async_context;
        EActivity *activity;
        GCancellable *cancellable;
        GdkCursor *gdk_cursor;
        GdkWindow *gdk_window;

        /* Make the cursor appear busy. */
        gdk_cursor = gdk_cursor_new (GDK_WATCH);
        gdk_window = gtk_widget_get_window (GTK_WIDGET (dialog));
        gdk_window_set_cursor (gdk_window, gdk_cursor);
        g_object_unref (gdk_cursor);

        activity = em_folder_selector_new_activity (
            EM_FOLDER_SELECTOR (dialog));

        async_context = g_slice_new0 (AsyncContext);
        async_context->dialog = g_object_ref (dialog);
        async_context->activity = g_object_ref (activity);

        cancellable = e_activity_get_cancellable (activity);

        e_mail_store_create_folder (
            store, create_folder_name,
            G_PRIORITY_DEFAULT, cancellable,
            mail_folder_create_dialog_create_folder_cb,
            async_context);

        g_object_unref (activity);
    }

    g_free (create_folder_name);
    g_free (parent_folder_name);

    g_object_unref (store);
}

static gboolean
mail_folder_create_dialog_inputs_are_valid (EMailFolderCreateDialog *dialog)
{
    GtkEntry *entry;
    const gchar *original_text;
    gchar *stripped_text;
    gboolean folder_or_store_is_selected;
    gboolean inputs_are_valid;

    entry = GTK_ENTRY (dialog->priv->name_entry);

    original_text = gtk_entry_get_text (entry);
    stripped_text = e_util_strdup_strip (original_text);

    folder_or_store_is_selected =
        em_folder_selector_get_selected (
        EM_FOLDER_SELECTOR (dialog), NULL, NULL);

    inputs_are_valid =
        folder_or_store_is_selected &&
        (stripped_text != NULL) &&
        (strchr (stripped_text, '/') == NULL);

    g_free (stripped_text);

    return inputs_are_valid;
}

static void
mail_folder_create_dialog_entry_activate_cb (GtkEntry *entry,
                                             EMailFolderCreateDialog *dialog)
{
    if (mail_folder_create_dialog_inputs_are_valid (dialog))
        gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
}

static void
mail_folder_create_dialog_entry_changed_cb (GtkEntry *entry,
                                            EMailFolderCreateDialog *dialog)
{
    gtk_dialog_set_response_sensitive (
        GTK_DIALOG (dialog), GTK_RESPONSE_OK,
        mail_folder_create_dialog_inputs_are_valid (dialog));
}

static void
mail_folder_create_dialog_set_session (EMailFolderCreateDialog *dialog,
                                       EMailUISession *session)
{
    g_return_if_fail (E_IS_MAIL_UI_SESSION (session));
    g_return_if_fail (dialog->priv->session == NULL);

    dialog->priv->session = g_object_ref (session);
}

static void
mail_folder_create_dialog_set_property (GObject *object,
                                        guint property_id,
                                        const GValue *value,
                                        GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_SESSION:
            mail_folder_create_dialog_set_session (
                E_MAIL_FOLDER_CREATE_DIALOG (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_folder_create_dialog_get_property (GObject *object,
                                        guint property_id,
                                        GValue *value,
                                        GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_SESSION:
            g_value_set_object (
                value,
                e_mail_folder_create_dialog_get_session (
                E_MAIL_FOLDER_CREATE_DIALOG (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_folder_create_dialog_dispose (GObject *object)
{
    EMailFolderCreateDialogPrivate *priv;

    priv = E_MAIL_FOLDER_CREATE_DIALOG_GET_PRIVATE (object);

    g_clear_object (&priv->session);
    g_clear_object (&priv->name_entry);

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_mail_folder_create_dialog_parent_class)->
        dispose (object);
}

static void
mail_folder_create_dialog_constructed (GObject *object)
{
    EMailFolderCreateDialog *dialog;
    EMailAccountStore *account_store;
    EMailUISession *session;
    EMFolderTree *folder_tree;
    EMFolderTreeModel *model;
    GQueue queue = G_QUEUE_INIT;
    GtkWidget *container;
    GtkWidget *widget;
    GtkLabel *label;

    dialog = E_MAIL_FOLDER_CREATE_DIALOG (object);
    session = e_mail_folder_create_dialog_get_session (dialog);
    model = em_folder_selector_get_model (EM_FOLDER_SELECTOR (dialog));

    /* Populate the tree model before chaining up, since the
     * subclass will immediately try to restore the tree state. */

    account_store = e_mail_ui_session_get_account_store (session);
    e_mail_account_store_queue_enabled_services (account_store, &queue);

    while (!g_queue_is_empty (&queue)) {
        CamelService *service;
        CamelStoreFlags flags;

        service = g_queue_pop_head (&queue);
        g_warn_if_fail (CAMEL_IS_STORE (service));

        flags = CAMEL_STORE (service)->flags;
        if ((flags & CAMEL_STORE_CAN_EDIT_FOLDERS) == 0)
            continue;

        em_folder_tree_model_add_store (model, CAMEL_STORE (service));
    }

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_mail_folder_create_dialog_parent_class)->
        constructed (object);

    gtk_window_set_title (GTK_WINDOW (dialog), _("Create Folder"));

    em_folder_selector_set_caption (
        EM_FOLDER_SELECTOR (dialog),
        _("Specify where to create the folder:"));

    em_folder_selector_set_default_button_label (
        EM_FOLDER_SELECTOR (dialog), _("C_reate"));

    folder_tree = em_folder_selector_get_folder_tree (
        EM_FOLDER_SELECTOR (dialog));
    em_folder_tree_set_excluded (folder_tree, EMFT_EXCLUDE_NOINFERIORS);

    /* Add a folder name entry field to the dialog. */

    container = em_folder_selector_get_content_area (
        EM_FOLDER_SELECTOR (dialog));

    widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    container = widget;

    widget = gtk_label_new_with_mnemonic (_("Folder _name:"));
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    label = GTK_LABEL (widget);

    widget = gtk_entry_new ();
    gtk_label_set_mnemonic_widget (label, widget);
    gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
    dialog->priv->name_entry = g_object_ref (widget);
    gtk_widget_grab_focus (widget);
    gtk_widget_show (widget);

    g_signal_connect (
        widget, "activate",
        G_CALLBACK (mail_folder_create_dialog_entry_activate_cb),
        dialog);

    g_signal_connect (
        widget, "changed",
        G_CALLBACK (mail_folder_create_dialog_entry_changed_cb),
        dialog);
}

static void
mail_folder_create_dialog_response (GtkDialog *dialog,
                                    gint response_id)
{
    /* Do not chain up.  GtkDialog does not implement this method. */

    switch (response_id) {
        case GTK_RESPONSE_OK:
            mail_folder_create_dialog_create_folder (
                E_MAIL_FOLDER_CREATE_DIALOG (dialog));
            break;
        case GTK_RESPONSE_CANCEL:
            gtk_widget_destroy (GTK_WIDGET (dialog));
            break;
        default:
            break;
    }
}

static void
mail_folder_create_dialog_folder_selected (EMFolderSelector *selector,
                                           CamelStore *store,
                                           const gchar *folder_name)
{
    EMailFolderCreateDialog *dialog;

    /* Do not chain up.  This overrides the subclass behavior. */

    dialog = E_MAIL_FOLDER_CREATE_DIALOG (selector);

    gtk_dialog_set_response_sensitive (
        GTK_DIALOG (dialog), GTK_RESPONSE_OK,
        mail_folder_create_dialog_inputs_are_valid (dialog));
}

static void
e_mail_folder_create_dialog_class_init (EMailFolderCreateDialogClass *class)
{
    GObjectClass *object_class;
    GtkDialogClass *dialog_class;
    EMFolderSelectorClass *selector_class;

    g_type_class_add_private (
        class, sizeof (EMailFolderCreateDialogPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = mail_folder_create_dialog_set_property;
    object_class->get_property = mail_folder_create_dialog_get_property;
    object_class->dispose = mail_folder_create_dialog_dispose;
    object_class->constructed = mail_folder_create_dialog_constructed;

    dialog_class = GTK_DIALOG_CLASS (class);
    dialog_class->response = mail_folder_create_dialog_response;

    selector_class = EM_FOLDER_SELECTOR_CLASS (class);
    selector_class->folder_selected = mail_folder_create_dialog_folder_selected;

    g_object_class_install_property (
        object_class,
        PROP_SESSION,
        g_param_spec_object (
            "session",
            "Session",
            "An EMailUISession from which "
            "to list enabled mail stores",
            E_TYPE_MAIL_UI_SESSION,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));

    signals[FOLDER_CREATED] = g_signal_new (
        "folder-created",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (EMailFolderCreateDialogClass, folder_created),
        NULL, NULL, NULL,
        G_TYPE_NONE, 2,
        CAMEL_TYPE_STORE,
        G_TYPE_STRING);
}

static void
e_mail_folder_create_dialog_init (EMailFolderCreateDialog *dialog)
{
    dialog->priv = E_MAIL_FOLDER_CREATE_DIALOG_GET_PRIVATE (dialog);
}

GtkWidget *
e_mail_folder_create_dialog_new (GtkWindow *parent,
                                 EMailUISession *session)
{
    GtkWidget *dialog;
    EMFolderTreeModel *model;

    g_return_val_if_fail (E_IS_MAIL_UI_SESSION (session), NULL);

    /* XXX The folder tree model is a construct-only property in
     *     EMFolderSelector, so create an empty model here and then
     *     populate it during instance initialization.  Already too
     *     much logic here for a "new" function, but works for now. */

    model = em_folder_tree_model_new ();
    em_folder_tree_model_set_session (model, E_MAIL_SESSION (session));

    dialog = g_object_new (
        E_TYPE_MAIL_FOLDER_CREATE_DIALOG,
        "transient-for", parent,
        "model", model,
        "session", session, NULL);

    g_object_unref (model);

    return dialog;
}

EMailUISession *
e_mail_folder_create_dialog_get_session (EMailFolderCreateDialog *dialog)
{
    g_return_val_if_fail (E_IS_MAIL_FOLDER_CREATE_DIALOG (dialog), NULL);

    return dialog->priv->session;
}