aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-folder-commands.c
blob: aee1d459e3b9de37897b0d0e80cfd954e0b52490 (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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-shell-folder-commands.h
 *
 * Copyright (C) 2001  Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU 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 General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

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

#include "e-shell-folder-commands.h"

#include "e-util/e-request.h"

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

#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-util.h>
#include <libgnomeui/gnome-dialog.h>
#include <libgnomeui/gnome-stock.h>

#include <gtk/gtklabel.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkentry.h>

#include "e-shell-constants.h"
#include "e-shell-folder-creation-dialog.h"
#include "e-shell-folder-selection-dialog.h"
#include "e-shell-utils.h"


/* Utility functions.  */

static const char *
get_folder_name (EShell *shell,
         const char *path)
{
    EStorageSet *storage_set;
    EFolder *folder;

    storage_set = e_shell_get_storage_set (shell);
    folder = e_storage_set_get_folder (storage_set, path);

    return e_folder_get_name (folder);
}

static int
get_folder_unread (EShell *shell,
           const char *path)
{
    EStorageSet *storage_set;
    EFolder *folder;

    storage_set = e_shell_get_storage_set (shell);
    folder = e_storage_set_get_folder (storage_set, path);

    return e_folder_get_unread_count (folder);
}


/* The data passed to the signals handled during the execution of the folder
   commands.  */

enum _FolderCommand {
    FOLDER_COMMAND_COPY,
    FOLDER_COMMAND_MOVE
};
typedef enum _FolderCommand FolderCommand;

struct _FolderCommandData {
    EShell *shell;
    EShellView *shell_view;
    FolderCommand command;
    char *source_path;
    char *destination_path;
};
typedef struct _FolderCommandData FolderCommandData;

static FolderCommandData *
folder_command_data_new (EShell *shell,
             EShellView *shell_view,
             FolderCommand command,
             const char *source_path,
             const char *destination_path)
{
    FolderCommandData *new;

    new = g_new (FolderCommandData, 1);
    new->shell            = shell;
    new->shell_view       = shell_view;
    new->command          = command;
    new->source_path      = g_strdup (source_path);
    new->destination_path = g_strdup (destination_path);

    return new;
}

static void
folder_command_data_free (FolderCommandData *folder_command_data)
{
    g_free (folder_command_data->source_path);
    g_free (folder_command_data->destination_path);

    g_free (folder_command_data);
}


/* Callback for the storage result.  */

static void
xfer_result_callback (EStorageSet *storage_set,
              EStorageResult result,
              void *data)
{
    FolderCommandData *folder_command_data;

    folder_command_data = (FolderCommandData *) data;

    if (result != E_STORAGE_OK) {
        char *msg;

        if (folder_command_data->command == FOLDER_COMMAND_COPY)
            msg = g_strdup_printf (_("Cannot copy folder: %s"),
                           e_storage_result_to_string (result));
        else
            msg = g_strdup_printf (_("Cannot move folder: %s"),
                           e_storage_result_to_string (result));

        e_notice (GTK_WINDOW (folder_command_data->shell_view),
              GNOME_MESSAGE_BOX_ERROR, msg);

        g_free (msg);
    }

    folder_command_data_free (folder_command_data);
}


/* The signals for the folder selection dialog.  This used for the copy and
   move commands.  */

static void
folder_selection_dialog_folder_selected_callback (EShellFolderSelectionDialog *folder_selection_dialog,
                          const char *path,
                          void *data)
{
    FolderCommandData *folder_command_data;
    EStorageSet *storage_set;
    gboolean remove_source;

    folder_command_data = (FolderCommandData *) data;

    folder_command_data->destination_path = g_concat_dir_and_file (path,
                                       g_basename (folder_command_data->source_path));

    switch (folder_command_data->command) {
    case FOLDER_COMMAND_COPY:
        remove_source = FALSE;
        break;
    case FOLDER_COMMAND_MOVE:
        remove_source = TRUE;
        break;
    default:
        g_assert_not_reached ();
        return;
    }

    if (strcmp (folder_command_data->destination_path,
            folder_command_data->source_path) == 0) {
        const char *msg;

        if (remove_source)
            msg = _("Cannot move a folder over itself.");
        else
            msg = _("Cannot copy a folder over itself.");

        e_notice (GTK_WINDOW (folder_selection_dialog), GNOME_MESSAGE_BOX_ERROR, msg);
        return;
    }

    if (remove_source) {
        int source_len;

        source_len = strlen (folder_command_data->source_path);
        if (strncmp (folder_command_data->destination_path,
                 folder_command_data->source_path,
                 source_len) == 0) {
            e_notice (GTK_WINDOW (folder_selection_dialog), GNOME_MESSAGE_BOX_ERROR,
                  _("Cannot move a folder into one of its descendants."));
            return;
        }
    }

    storage_set = e_shell_get_storage_set (folder_command_data->shell);

    e_storage_set_async_xfer_folder (storage_set,
                     folder_command_data->source_path,
                     folder_command_data->destination_path,
                     remove_source,
                     xfer_result_callback,
                     folder_command_data);

    gtk_widget_destroy (GTK_WIDGET (folder_selection_dialog));
}

static void
folder_selection_dialog_cancelled_callback (EShellFolderSelectionDialog *folder_selection_dialog,
                        void *data)
{
    folder_command_data_free ((FolderCommandData *) data);
}

static void
connect_folder_selection_dialog_signals (EShellFolderSelectionDialog *folder_selection_dialog,
                     FolderCommandData *folder_command_data)
{
    g_assert (folder_command_data != NULL);

    gtk_signal_connect (GTK_OBJECT (folder_selection_dialog), "folder_selected",
                GTK_SIGNAL_FUNC (folder_selection_dialog_folder_selected_callback),
                folder_command_data);

    gtk_signal_connect (GTK_OBJECT (folder_selection_dialog), "cancelled",
                GTK_SIGNAL_FUNC (folder_selection_dialog_cancelled_callback),
                folder_command_data);
}


/* Create new folder.  */

void
e_shell_command_create_new_folder (EShell *shell,
                   EShellView *shell_view,
                   const char *parent_folder_path)
{
    g_return_if_fail (shell != NULL);
    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (shell_view != NULL || parent_folder_path != NULL);
    g_return_if_fail (shell_view != NULL && E_IS_SHELL_VIEW (shell_view));
    g_return_if_fail (parent_folder_path != NULL || g_path_is_absolute (parent_folder_path));

    if (parent_folder_path == NULL)
        parent_folder_path = e_shell_view_get_current_path (shell_view);

    /* FIXME: Should handle the result stuff.  */
    e_shell_show_folder_creation_dialog (shell, GTK_WINDOW (shell_view),
                         e_shell_view_get_current_path (shell_view),
                         NULL /* Default type. Take it from parent */,
                         NULL /* result_callback */,
                         NULL /* result_callback_data */);
}


/* Open folder in other window.   */

void
e_shell_command_open_folder_in_other_window (EShell *shell,
                         EShellView *shell_view,
                         const char *folder_path)
{
    EShellView *view;
    char *uri;

    g_return_if_fail (shell != NULL);
    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (shell_view != NULL && E_IS_SHELL_VIEW (shell_view));

    if (folder_path == NULL)
        folder_path = e_shell_view_get_current_path (shell_view);

    uri = g_strconcat (E_SHELL_URI_PREFIX, folder_path, NULL);
    view = e_shell_create_view (shell, uri, shell_view);
    g_free (uri);

    gtk_widget_show (GTK_WIDGET (view));
}


/* Copy folder.  */

void
e_shell_command_copy_folder (EShell *shell,
                 EShellView *shell_view,
                 const char *folder_path)
{
    GtkWidget *folder_selection_dialog;
    FolderCommandData *data;
    char *uri;
    char *caption;

    g_return_if_fail (shell != NULL);
    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (shell_view != NULL && E_IS_SHELL_VIEW (shell_view));
    g_return_if_fail (folder_path == NULL || g_path_is_absolute (folder_path));

    if (folder_path == NULL)
        folder_path = e_shell_view_get_current_path (shell_view);

    if (folder_path == NULL) {
        g_warning ("Called `e_shell_command_copy_folder()' without a valid displayed folder");
        return;
    }

    caption = g_strdup_printf (_("Specify a folder to copy folder \"%s\" into:"),
                   get_folder_name (shell, folder_path));

    uri = g_strconcat (E_SHELL_URI_PREFIX, folder_path, NULL);
    folder_selection_dialog = e_shell_folder_selection_dialog_new (shell, _("Copy folder"),
                                       caption, uri, NULL);

    g_free (caption);
    g_free (uri);

    data = folder_command_data_new (shell, shell_view, FOLDER_COMMAND_COPY, folder_path, NULL);
    connect_folder_selection_dialog_signals (E_SHELL_FOLDER_SELECTION_DIALOG (folder_selection_dialog),
                         data);

    gtk_widget_show (folder_selection_dialog);
}


/* Move folder.  */

void
e_shell_command_move_folder (EShell *shell,
                 EShellView *shell_view,
                 const char *folder_path)
{
    GtkWidget *folder_selection_dialog;
    FolderCommandData *data;
    char *uri;
    char *caption;

    g_return_if_fail (shell != NULL);
    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (shell_view != NULL);
    g_return_if_fail (E_IS_SHELL_VIEW (shell_view));
    g_return_if_fail (folder_path == NULL || g_path_is_absolute (folder_path));

    if (folder_path == NULL)
        folder_path = e_shell_view_get_current_path (shell_view);

    if (folder_path == NULL) {
        g_warning ("Called `e_shell_command_move_folder()' without a valid displayed folder");
        return;
    }

    caption = g_strdup_printf (_("Specify a folder to move folder \"%s\" into:"),
                   get_folder_name (shell, folder_path));

    uri = g_strconcat (E_SHELL_URI_PREFIX, folder_path, NULL);
    folder_selection_dialog = e_shell_folder_selection_dialog_new (shell, _("Move folder"),
                                       caption, uri, NULL);

    g_free (caption);
    g_free (uri);

    data = folder_command_data_new (shell, shell_view, FOLDER_COMMAND_MOVE, folder_path, NULL);
    connect_folder_selection_dialog_signals (E_SHELL_FOLDER_SELECTION_DIALOG (folder_selection_dialog),
                         data);

    gtk_widget_show (folder_selection_dialog);
}

static void
delete_cb (EStorageSet *storage_set,
       EStorageResult result,
       void *data)
{
    EShellView *shell_view;

    shell_view = E_SHELL_VIEW (data);

    if (result != E_STORAGE_OK)
        e_notice (GTK_WINDOW (shell_view), GNOME_MESSAGE_BOX_ERROR,
              _("Cannot delete folder:\n%s"), e_storage_result_to_string (result));
}

static int
delete_dialog (EShellView *shell_view, const char *utf8_folder)
{
    GnomeDialog *dialog;
    char *title;
    GtkWidget *question_label;
    char *question;
    char *folder_name;

    /* Popup a dialog asking if they are sure they want to delete
           the folder */
    folder_name = e_utf8_to_gtk_string (GTK_WIDGET (shell_view), 
                        (char *)utf8_folder);
    title = g_strdup_printf (_("Delete \"%s\""), folder_name);

    dialog = GNOME_DIALOG (gnome_dialog_new (title,
                         GNOME_STOCK_BUTTON_YES,
                         GNOME_STOCK_BUTTON_NO,
                         NULL));
    g_free (title);
    gnome_dialog_set_parent (dialog, GTK_WINDOW (shell_view));

    /* "Are you sure..." label */
    question = g_strdup_printf (_("Are you sure you want to remove the \"%s\" folder?"),
                    folder_name);
    question_label = gtk_label_new (question);  
    gtk_widget_show (question_label);

    gtk_box_pack_start (GTK_BOX (dialog->vbox), question_label, FALSE, TRUE, 2);
    g_free (folder_name);
    g_free (question);

    gnome_dialog_set_default (dialog, 1);

    return gnome_dialog_run_and_close (dialog);
}

void
e_shell_command_delete_folder (EShell *shell,
                   EShellView *shell_view,
                   const char *folder_path)
{
    EStorageSet *storage_set;

    g_return_if_fail (shell != NULL);
    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (shell_view != NULL || folder_path != NULL);
    g_return_if_fail (E_IS_SHELL_VIEW (shell_view));
    g_return_if_fail (folder_path != NULL || g_path_is_absolute (folder_path));

    storage_set = e_shell_get_storage_set (shell);

    if (folder_path == NULL)
        folder_path = e_shell_view_get_current_path (shell_view);

    if (delete_dialog (shell_view, get_folder_name (shell, folder_path)) == 0)
        e_storage_set_async_remove_folder (storage_set, folder_path, delete_cb, shell_view);
}


struct _RenameCallbackData {
    EShellView *shell_view;
    char *new_path;
};
typedef struct _RenameCallbackData RenameCallbackData;

static RenameCallbackData *
rename_callback_data_new (EShellView *shell_view,
              const char *new_path)
{
    RenameCallbackData *callback_data;

    callback_data = g_new (RenameCallbackData, 1);

    gtk_object_ref (GTK_OBJECT (shell_view));
    callback_data->shell_view = shell_view;

    callback_data->new_path = g_strdup (new_path);

    return callback_data;
}

static void
rename_callback_data_free (RenameCallbackData *callback_data)
{
    gtk_object_unref (GTK_OBJECT (callback_data->shell_view));
    g_free (callback_data->new_path);

    g_free (callback_data);
}

static void
rename_cb (EStorageSet *storage_set, EStorageResult result, void *data)
{
    RenameCallbackData *callback_data;

    callback_data = (RenameCallbackData *) data;

    if (result != E_STORAGE_OK) {
        e_notice (GTK_WINDOW (callback_data->shell_view), GNOME_MESSAGE_BOX_ERROR,
              _("Cannot rename folder:\n%s"), e_storage_result_to_string (result));
    } else {
        EFolder *folder;
        EShell *shell;
        EStorageSet *storage_set;

        shell = e_shell_view_get_shell (callback_data->shell_view);
        storage_set = e_shell_get_storage_set (shell);
        folder = e_storage_set_get_folder (storage_set, callback_data->new_path);

        if (folder != NULL)
            e_folder_set_name (folder, g_basename (callback_data->new_path));
    }

    rename_callback_data_free (callback_data);
}

void
e_shell_command_rename_folder (EShell *shell,
                   EShellView *shell_view,
                   const char *folder_path)
{
    EStorageSet *storage_set;
    EFolder *folder;
    RenameCallbackData *callback_data;
    const char *old_name;
    char *old_name_locale;
    char *prompt;
    char *new_name;
    char *old_base_path;
    char *new_path;

    g_return_if_fail (shell != NULL);
    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (shell_view != NULL);
    g_return_if_fail (E_IS_SHELL_VIEW (shell_view));

    storage_set = e_shell_get_storage_set (shell);

    if (folder_path == NULL)
        folder_path = e_shell_view_get_current_path (shell_view);

    folder = e_storage_set_get_folder (storage_set, folder_path);
    g_return_if_fail (folder != NULL);

    old_name = e_folder_get_name (folder);
    old_name_locale = e_utf8_to_locale_string (old_name);
    prompt = g_strdup_printf (_("Rename the \"%s\" folder to:"), old_name_locale);
    g_free (old_name_locale);

    while (1) {
        const char *reason;

        new_name = e_request_string (shell_view != NULL ? GTK_WINDOW (shell_view) : NULL,
                         _("Rename folder"), prompt, old_name);

        if (new_name == NULL)
            return;

        if (e_shell_folder_name_is_valid (new_name, &reason))
            break;

        e_notice (shell_view != NULL ? GTK_WINDOW (shell_view) : NULL,
              GNOME_MESSAGE_BOX_ERROR,
              _("The specified folder name is not valid: %s"), reason);
    }

    g_free (prompt);

    if (strcmp (old_name, new_name) == 0) {
        g_free (new_name);
        return;
    }

    old_base_path = g_dirname (folder_path);
    new_path = g_concat_dir_and_file (old_base_path, new_name);

    callback_data = rename_callback_data_new (shell_view, new_path);
    e_storage_set_async_xfer_folder (storage_set, folder_path, new_path, TRUE, rename_cb, callback_data);

    g_free (old_base_path);
    g_free (new_path);
    g_free (new_name);
}


void
e_shell_command_add_to_shortcut_bar (EShell *shell,
                     EShellView *shell_view,
                     const char *folder_path)
{
    EShortcuts *shortcuts;
    EStorageSet *storage_set;
    EFolder *folder;
    int group_num;
    char *uri;
    int unread_count;

    g_return_if_fail (shell != NULL);
    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (shell_view != NULL);
    g_return_if_fail (E_IS_SHELL_VIEW (shell_view));
    g_return_if_fail (folder_path == NULL || g_path_is_absolute (folder_path));

    shortcuts = e_shell_get_shortcuts (shell);
    group_num = e_shell_view_get_current_shortcuts_group_num (shell_view);

    if (folder_path == NULL)
        uri = g_strdup (e_shell_view_get_current_uri (shell_view));
    else
        uri = g_strconcat (E_SHELL_URI_PREFIX, folder_path, NULL);

    unread_count = get_folder_unread (shell, e_shell_view_get_current_path (shell_view));

    storage_set = e_shell_get_storage_set (shell);
    folder = e_storage_set_get_folder (storage_set, e_shell_view_get_current_path (shell_view));

    e_shortcuts_add_shortcut (shortcuts, group_num, -1, uri, NULL, unread_count, e_folder_get_type_string (folder));

    g_free (uri);
}