aboutsummaryrefslogblamecommitdiffstats
path: root/shell/e-folder-dnd-bridge.c
blob: 3c15ba966c54d07d52ba7671cd54f09cf5c2b376 (plain) (tree)
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


















































































































































































































































































































































































































































                                                                                                                                  
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-folder-dnd-bridge.c - Utility functions for handling dnd to Evolution
 * folders using the ShellComponentDnd interface.
 *
 * Copyright (C) 2002 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 <ettore@ximian.com>
 */

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

#include "e-folder-dnd-bridge.h"

#include "Evolution.h"
#include "e-storage-set-view.h"

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

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


/* Callbacks for folder operations.  */

static void
folder_xfer_callback (EStorageSet *storage_set,
              EStorageResult result,
              void *data)
{
    GtkWindow *parent;

    if (result == E_STORAGE_OK)
        return;

    parent = GTK_WINDOW (data);
    e_notice (parent, GNOME_MESSAGE_BOX_ERROR, _("Cannot transfer folder:\n%s"),
          e_storage_result_to_string (result));
}


static GNOME_Evolution_ShellComponentDnd_ActionSet
convert_gdk_drag_action_set_to_corba (GdkDragAction action)
{
    GNOME_Evolution_ShellComponentDnd_Action retval;

    retval = GNOME_Evolution_ShellComponentDnd_ACTION_DEFAULT;

    if (action & GDK_ACTION_COPY)
        retval |= GNOME_Evolution_ShellComponentDnd_ACTION_COPY;
    if (action & GDK_ACTION_MOVE)
        retval |= GNOME_Evolution_ShellComponentDnd_ACTION_MOVE;
    if (action & GDK_ACTION_LINK)
        retval |= GNOME_Evolution_ShellComponentDnd_ACTION_LINK;
    if (action & GDK_ACTION_ASK)
        retval |= GNOME_Evolution_ShellComponentDnd_ACTION_ASK;

    return retval;
}

static GNOME_Evolution_ShellComponentDnd_ActionSet
convert_gdk_drag_action_to_corba (GdkDragAction action)
{
    if (action == GDK_ACTION_COPY)
        return GNOME_Evolution_ShellComponentDnd_ACTION_COPY;
    else if (action == GDK_ACTION_MOVE)
        return GNOME_Evolution_ShellComponentDnd_ACTION_MOVE;
    else if (action == GDK_ACTION_LINK)
        return GNOME_Evolution_ShellComponentDnd_ACTION_LINK;
    else if (action == GDK_ACTION_ASK)
        return GNOME_Evolution_ShellComponentDnd_ACTION_ASK;
    else {
        g_warning ("Unknown GdkDragAction %d", action);
        return GNOME_Evolution_ShellComponentDnd_ACTION_DEFAULT;
    }
}

static GdkDragAction
convert_corba_drag_action_to_gdk (GNOME_Evolution_ShellComponentDnd_ActionSet action)
{
    if (action == GNOME_Evolution_ShellComponentDnd_ACTION_COPY)
        return GDK_ACTION_COPY;
    else if (action == GNOME_Evolution_ShellComponentDnd_ACTION_MOVE)
        return GDK_ACTION_MOVE;
    else if (action == GNOME_Evolution_ShellComponentDnd_ACTION_LINK)
        return GDK_ACTION_LINK;
    else if (action == GNOME_Evolution_ShellComponentDnd_ACTION_ASK)
        return GDK_ACTION_ASK;
    else {
        g_warning ("unknown GNOME_Evolution_ShellComponentDnd_ActionSet %d", action);
        return GDK_ACTION_DEFAULT;
    }
}

static EvolutionShellComponentClient *
get_component_at_path (EStorageSet *storage_set,
               const char *path)
{
    EvolutionShellComponentClient *component_client;
    EFolderTypeRegistry *folder_type_registry;
    EFolder *folder;

    folder = e_storage_set_get_folder (storage_set, path);
    if (folder == NULL)
        return NULL;

    folder_type_registry = e_storage_set_get_folder_type_registry (storage_set);
    g_assert (folder_type_registry != NULL);

    component_client = e_folder_type_registry_get_handler_for_type (folder_type_registry,
                                    e_folder_get_type_string (folder));

    return component_client;
}

/* This will look for the targets in @drag_context, choose one that matches
   with the allowed types at @path, and return its name.  The EVOLUTION_PATH
   type always matches.  */
static const char *
find_matching_target_for_drag_context (EStorageSet *storage_set,
                       const char *path,
                       GdkDragContext *drag_context)
{
    EFolderTypeRegistry *folder_type_registry;
    EFolder *folder;
    GList *accepted_types;
    GList *p, *q;

    folder_type_registry = e_storage_set_get_folder_type_registry (storage_set);

    folder = e_storage_set_get_folder (storage_set, path);
    if (folder == NULL)
        return E_FOLDER_DND_PATH_TARGET_TYPE;

    accepted_types = e_folder_type_registry_get_accepted_dnd_types_for_type (folder_type_registry,
                                         e_folder_get_type_string (folder));

    /* FIXME?  We might make this more efficient.  Currently it takes `n *
       m' string compares, where `n' is the number of targets in the
       @drag_context, and `m' is the number of supported types in
       @folder.  */

    for (p = drag_context->targets; p != NULL; p = p->next) {
        char *possible_type;

        possible_type = gdk_atom_name ((GdkAtom) p->data);
        if (strcmp (possible_type, E_FOLDER_DND_PATH_TARGET_TYPE) == 0) {
            g_free (possible_type);
            return E_FOLDER_DND_PATH_TARGET_TYPE;
        }

        for (q = accepted_types; q != NULL; q = q->next) {
            const char *accepted_type;

            accepted_type = (const char *) q->data;
            if (strcmp (possible_type, accepted_type) == 0) {
                g_free (possible_type);
                return accepted_type;
            }
        }

        g_free (possible_type);
    }

    return NULL;
}

static gboolean
handle_evolution_path_drag_motion (EStorageSet *storage_set,
                   const char *path,
                   GdkDragContext *context,
                   unsigned int time)
{
    GdkModifierType modifiers;
    GdkDragAction action;

    gdk_window_get_pointer (NULL, NULL, NULL, &modifiers);

    if ((modifiers & GDK_CONTROL_MASK) != 0) {
        action = GDK_ACTION_COPY;
    } else {
        GtkWidget *source_widget;

        action = GDK_ACTION_MOVE;

        source_widget = gtk_drag_get_source_widget (context);
        if (source_widget != NULL
            && E_IS_STORAGE_SET_VIEW (source_widget)) {
            const char *source_path;

            source_path = e_storage_set_view_get_current_folder (E_STORAGE_SET_VIEW (source_widget));
            if (source_path != NULL) {
                EFolder *folder;
                int source_path_len;
                char *destination_path;

                folder = e_storage_set_get_folder (storage_set, source_path);
                if (folder != NULL && e_folder_get_is_stock (folder))
                    return FALSE;

                source_path_len = strlen (path);
                if (strcmp (path, source_path) == 0)
                    return FALSE;

                destination_path = g_strconcat (path, "/", g_basename (source_path), NULL);
                if (strncmp (destination_path, source_path, source_path_len) == 0) {
                    g_free (destination_path);
                    return FALSE;
                }

                g_free (destination_path);
            }
        }
    }

    gdk_drag_status (context, action, time);
    return TRUE;
}


gboolean
e_folder_dnd_bridge_motion  (GtkWidget *widget,
                 GdkDragContext *context,
                 unsigned int time,
                 EStorageSet *storage_set,
                 const char *path)
{
    EvolutionShellComponentClient *component_client;
    GNOME_Evolution_ShellComponentDnd_DestinationFolder destination_folder_interface;
    GNOME_Evolution_ShellComponentDnd_DestinationFolder_Context corba_context;
    GNOME_Evolution_ShellComponentDnd_Action suggested_action;
    CORBA_Environment ev;
    CORBA_boolean can_handle;
    EFolder *folder;
    const char *dnd_type;

    g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
    g_return_val_if_fail (context != NULL, FALSE);
    g_return_val_if_fail (E_IS_STORAGE_SET (storage_set), FALSE);
    g_return_val_if_fail (path != NULL, FALSE);

    dnd_type = find_matching_target_for_drag_context (storage_set, path, context);
    if (dnd_type == NULL)
        return FALSE;

    if (strcmp (dnd_type, E_FOLDER_DND_PATH_TARGET_TYPE) == 0)
        return handle_evolution_path_drag_motion (storage_set, path, context, time);

    component_client = get_component_at_path (storage_set, path);
    if (component_client == NULL)
        return FALSE;

    destination_folder_interface = evolution_shell_component_client_get_dnd_destination_interface (component_client);
    if (destination_folder_interface == NULL)
        return FALSE;

    CORBA_exception_init (&ev);

    corba_context.dndType = (char *) dnd_type; /* (Safe cast, as we don't actually free the corba_context.)  */
    corba_context.possibleActions = convert_gdk_drag_action_set_to_corba (context->actions);
    corba_context.suggestedAction = convert_gdk_drag_action_to_corba (context->suggested_action);

    folder = e_storage_set_get_folder (storage_set, path);
    
    can_handle = GNOME_Evolution_ShellComponentDnd_DestinationFolder_handleMotion (destination_folder_interface,
                                               e_folder_get_physical_uri (folder),
                                               e_folder_get_type_string (folder),
                                               &corba_context,
                                               &suggested_action,
                                               &ev);
    if (ev._major != CORBA_NO_EXCEPTION || ! can_handle) {
        CORBA_exception_free (&ev);
        return FALSE;
    }

    CORBA_exception_free (&ev);

    gdk_drag_status (context, convert_corba_drag_action_to_gdk (suggested_action), time);
    return TRUE;
}

static gboolean
handle_data_received_path (GdkDragContext *context,
               GtkSelectionData *selection_data,
               EStorageSet *storage_set,
               const char *path,
               GtkWindow *toplevel_window)
{
    const char *source_path;
    char *destination_path;
    gboolean handled;

    source_path = (const char *) selection_data->data;

    /* (Basic sanity checks.)  */
    if (source_path == NULL || source_path[0] != G_DIR_SEPARATOR || source_path[1] == '\0')
        return FALSE;

    destination_path = g_concat_dir_and_file (path, g_basename (source_path));

    switch (context->action) {
    case GDK_ACTION_MOVE:
        e_storage_set_async_xfer_folder (storage_set,
                         source_path,
                         destination_path,
                         TRUE,
                         folder_xfer_callback,
                         toplevel_window);
        handled = TRUE;
        break;
    case GDK_ACTION_COPY:
        e_storage_set_async_xfer_folder (storage_set,
                         source_path,
                         destination_path,
                         FALSE,
                         folder_xfer_callback,
                         toplevel_window);
        handled = TRUE;
        break;
    default:
        handled = FALSE;
        g_warning ("EStorageSetView: Unknown action %d", context->action);
    }

    g_free (destination_path);

    return handled;
}

static gboolean
handle_data_received_non_path (GdkDragContext *context,
                   GtkSelectionData *selection_data,
                   EStorageSet *storage_set,
                   const char *path,
                   const char *target_type)
{
    GNOME_Evolution_ShellComponentDnd_DestinationFolder destination_folder_interface;
    GNOME_Evolution_ShellComponentDnd_DestinationFolder_Context corba_context;
    GNOME_Evolution_ShellComponentDnd_Data corba_data;
    EvolutionShellComponentClient *component_client;
    EFolder *folder;
    CORBA_Environment ev;

    component_client = get_component_at_path (storage_set, path);
    if (component_client == NULL)
        return FALSE;

    destination_folder_interface = evolution_shell_component_client_get_dnd_destination_interface (component_client);
    if (destination_folder_interface == NULL)
        return FALSE;
        
    CORBA_exception_init (&ev);

    folder = e_storage_set_get_folder (storage_set, path);
    
    corba_context.dndType = (char *) target_type;
    corba_context.possibleActions = convert_gdk_drag_action_set_to_corba (context->actions);
    corba_context.suggestedAction = convert_gdk_drag_action_to_corba (context->suggested_action);

    corba_data.format = selection_data->format;
    corba_data.target = selection_data->target;

    corba_data.bytes._release = FALSE;

    if (selection_data->data == NULL) {
        /* If data is NULL the length is -1 and this would mess things
           up so we handle it separately.  */
        corba_data.bytes._length = 0;
        corba_data.bytes._buffer = NULL;
    } else {
        corba_data.bytes._length = selection_data->length;
        corba_data.bytes._buffer = selection_data->data;
    }

    /* pass off the data to the component's DestinationFolderInterface */
    return GNOME_Evolution_ShellComponentDnd_DestinationFolder_handleDrop (destination_folder_interface,
                                           e_folder_get_physical_uri (folder),
                                           e_folder_get_type_string (folder),
                                           &corba_context,
                                           convert_gdk_drag_action_to_corba (context->action),
                                           &corba_data,
                                           &ev);
}

void
e_folder_dnd_bridge_data_received (GtkWidget *widget,
                   GdkDragContext *context,
                   GtkSelectionData *selection_data,
                   unsigned int time,
                   EStorageSet *storage_set,
                   const char *path)
{
    char *target_type;
    gboolean handled;

    g_return_if_fail (GTK_IS_WIDGET (widget));
    g_return_if_fail (context != NULL);
    g_return_if_fail (E_IS_STORAGE_SET (storage_set));
    g_return_if_fail (path != NULL);

    if (selection_data->data == NULL && selection_data->length == -1)
        return;

    target_type = gdk_atom_name (selection_data->target);

    if (strcmp (target_type, E_FOLDER_DND_PATH_TARGET_TYPE) != 0) {
        handled = handle_data_received_non_path (context, selection_data, storage_set,
                             path, target_type);
    } else {
        GtkWindow *toplevel_window;

        toplevel_window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (widget)));
        handled = handle_data_received_path (context, selection_data, storage_set, path,
                             toplevel_window);
    }

    g_free (target_type);
    gtk_drag_finish (context, handled, FALSE, time);
}