src/cmd/fsexam/src/fsexam-dnd.c
changeset 149 0014c9b031e9
equal deleted inserted replaced
148:91c620d9e52f 149:0014c9b031e9
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
       
    24  * Use is subject to license terms.
       
    25  */
       
    26 
       
    27 #ifdef HAVE_CONFIG_H
       
    28 #include <config.h>
       
    29 #endif
       
    30 
       
    31 #include <gtk/gtk.h>
       
    32 #include <glade/glade.h>
       
    33 #include <gconf/gconf-client.h>
       
    34 #include <libgnome/libgnome.h>
       
    35 #include <libgnomeui/libgnomeui.h>
       
    36 #include <libgnomevfs/gnome-vfs.h>
       
    37 #include <eel/eel-vfs-extensions.h>
       
    38 #include <eel/eel-string.h>
       
    39 #include <stdio.h>
       
    40 
       
    41 #include "fsexam-header.h"
       
    42 #include "fsexam-ui.h"
       
    43 #include "fsexam-dnd.h"
       
    44 
       
    45 enum {
       
    46   FSEXAM_ICON_DND_GNOME_ICON_LIST,
       
    47   FSEXAM_ICON_DND_URI_LIST,
       
    48   FSEXAM_ICON_DND_TEXT
       
    49 };
       
    50 
       
    51 static GtkTargetEntry drop_types[] = {
       
    52             {"text/uri-list", 0, FSEXAM_ICON_DND_URI_LIST},
       
    53             {"text/plain", 0, FSEXAM_ICON_DND_TEXT},
       
    54             {"x-special/gnome-icon-list", 0, FSEXAM_ICON_DND_GNOME_ICON_LIST}
       
    55 };
       
    56 
       
    57 static void
       
    58 drag_data_received_callback (GtkWidget *widget,
       
    59                              GdkDragContext *context,
       
    60                              gint x, 
       
    61                              gint y,
       
    62                              GtkSelectionData *data,
       
    63                              guint info,
       
    64                              guint32 time,
       
    65                              gpointer user_data)
       
    66 {
       
    67     gchar    *canonical_uri = NULL, *local_path, *p;
       
    68     gchar    **uris = NULL;
       
    69     gint     i;
       
    70 
       
    71     uris = g_uri_list_extract_uris ((gchar *) data->data);
       
    72 
       
    73     for (i = 0; uris[i] != NULL; i++) {
       
    74         gchar *uri;
       
    75 
       
    76         uri = gnome_vfs_make_uri_from_shell_arg (uris[i]);
       
    77         //TODO: is this valid?
       
    78         canonical_uri = gnome_vfs_make_uri_canonical (uri);
       
    79 
       
    80         g_free (uri);
       
    81 
       
    82         if (canonical_uri != NULL)
       
    83             break;
       
    84     }
       
    85 
       
    86     g_strfreev (uris);
       
    87 
       
    88     if (canonical_uri == NULL)
       
    89         return;
       
    90 
       
    91     local_path = gnome_vfs_get_local_path_from_uri (canonical_uri);
       
    92 
       
    93     if (local_path) {
       
    94         fsexam_change_dir (local_path);
       
    95     } else {
       
    96         if (eel_uri_is_desktop (canonical_uri) 
       
    97                 || eel_uri_is_trash (canonical_uri)) {
       
    98             gchar *uri = NULL;
       
    99             p = strstr(canonical_uri, ":");
       
   100             if (p != NULL) {
       
   101                 uri = g_strdup(p);
       
   102             }
       
   103 
       
   104             p = uri + 4; // to skip ":///"
       
   105 
       
   106             if (!strncmp (p, "home", 4) || !strncmp (p, "trash", 5) 
       
   107                     || !strncmp (p, "documents", 9)) {
       
   108                 if ((*p == 'h') || (*p == 'd'))
       
   109                     local_path = g_strdup_printf ("%s/%s", 
       
   110                                                    g_getenv ("HOME"), 
       
   111                                                    "Documents");
       
   112                 else
       
   113                     local_path = g_strdup_printf ("%s/%s", 
       
   114                                                    g_getenv ("HOME"), 
       
   115                                                    ".Trash");
       
   116 
       
   117                 fsexam_change_dir (local_path);
       
   118             }
       
   119 
       
   120             g_free (uri);
       
   121         }
       
   122     }
       
   123 
       
   124     g_free (local_path);
       
   125 
       
   126     gtk_drag_finish (context, TRUE, FALSE, time);
       
   127     g_free (canonical_uri);
       
   128 
       
   129     fsexam_statusbar_update("");
       
   130 
       
   131     return;
       
   132 }
       
   133 
       
   134 void
       
   135 fsexam_dnd_set (GtkWidget *widget)
       
   136 {
       
   137     gtk_drag_dest_set (widget,
       
   138                  GTK_DEST_DEFAULT_ALL,
       
   139                  drop_types, G_N_ELEMENTS (drop_types),
       
   140                  GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
       
   141     
       
   142     g_signal_connect (widget,
       
   143                 "drag_data_received",
       
   144                 G_CALLBACK (drag_data_received_callback),
       
   145                 NULL);
       
   146 
       
   147     return;
       
   148 }