build-basic-autotools:bgo#688293 - build-basic-autotools: Fix invalid read
[anjuta.git] / libanjuta / anjuta-file-drop-entry.c
blob8e16b8580b8abf95d9d8557931c99278a95fdeaf
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
5 *
6 * anjuta is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * anjuta is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "anjuta-file-drop-entry.h"
22 enum
24 PROP_0,
26 PROP_RELATIVE_PATH
29 /* DND Targets */
30 static GtkTargetEntry dnd_target_entries[] =
33 "text/uri-list",
39 struct _AnjutaFileDropEntryPriv
41 gchar *relative_path;
44 G_DEFINE_TYPE (AnjutaFileDropEntry, anjuta_file_drop_entry, ANJUTA_TYPE_DROP_ENTRY);
46 static void
47 anjuta_file_drop_entry_init (AnjutaFileDropEntry *self)
49 self->priv = g_new0 (AnjutaFileDropEntryPriv, 1);
51 gtk_drag_dest_set (GTK_WIDGET (self),
52 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT,
53 dnd_target_entries,
54 G_N_ELEMENTS (dnd_target_entries),
55 GDK_ACTION_COPY | GDK_ACTION_MOVE);
58 static void
59 anjuta_file_drop_entry_finalize (GObject *object)
61 AnjutaFileDropEntry *self;
63 self = ANJUTA_FILE_DROP_ENTRY (object);
65 g_free (self->priv->relative_path);
66 g_free (self->priv);
68 G_OBJECT_CLASS (anjuta_file_drop_entry_parent_class)->finalize (object);
71 static void
72 anjuta_file_drop_entry_set_property (GObject *object, guint prop_id,
73 const GValue *value, GParamSpec *pspec)
75 AnjutaFileDropEntry *self;
77 g_return_if_fail (ANJUTA_IS_FILE_DROP_ENTRY (object));
79 self = ANJUTA_FILE_DROP_ENTRY (object);
81 switch (prop_id)
83 case PROP_RELATIVE_PATH:
84 g_free (self->priv->relative_path);
86 self->priv->relative_path = g_value_dup_string (value);
87 break;
88 default:
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
90 break;
94 static void
95 anjuta_file_drop_entry_get_property (GObject *object, guint prop_id,
96 GValue *value, GParamSpec *pspec)
98 AnjutaFileDropEntry *self;
100 g_return_if_fail (ANJUTA_IS_FILE_DROP_ENTRY (object));
102 self = ANJUTA_FILE_DROP_ENTRY (object);
104 switch (prop_id)
106 case PROP_RELATIVE_PATH:
107 g_value_set_string (value, self->priv->relative_path);
108 break;
109 default:
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 break;
115 static void
116 anjuta_file_drop_entry_drag_data_received (GtkWidget *widget,
117 GdkDragContext *context, gint x, gint y,
118 GtkSelectionData *data, guint target_type,
119 guint time)
121 AnjutaFileDropEntry *self;
122 gboolean success;
123 gchar **uri_list;
124 GFile *parent_file;
125 GFile *file;
126 gchar *path;
128 self = ANJUTA_FILE_DROP_ENTRY (widget);
129 success = FALSE;
131 if ((data != NULL) &&
132 (gtk_selection_data_get_length (data) >= 0))
134 if (target_type == 0)
136 uri_list = gtk_selection_data_get_uris (data);
137 parent_file = NULL;
139 if (self->priv->relative_path)
140 parent_file = g_file_new_for_path (self->priv->relative_path);
142 /* Take only the first file */
143 file = g_file_new_for_uri (uri_list[0]);
145 if (parent_file)
147 path = g_file_get_relative_path (parent_file, file);
149 g_object_unref (parent_file);
151 else
152 path = g_file_get_path (file);
154 if (path)
156 anjuta_entry_set_text (ANJUTA_ENTRY (self), path);
158 g_free (path);
161 success = TRUE;
163 g_object_unref (file);
164 g_strfreev (uri_list);
168 /* Do not delete source data */
169 gtk_drag_finish (context, success, FALSE, time);
172 static gboolean
173 anjuta_file_drop_entry_drag_drop (GtkWidget *widget, GdkDragContext *context,
174 gint x, gint y, guint time)
176 GdkAtom target_type;
178 target_type = gtk_drag_dest_find_target (widget, context, NULL);
180 g_print ("Drag drop target type: %p\n", GDK_ATOM_TO_POINTER (target_type));
182 if (target_type != GDK_NONE)
183 gtk_drag_get_data (widget, context, target_type, time);
184 else
185 gtk_drag_finish (context, FALSE, FALSE, time);
187 return TRUE;
190 static void
191 anjuta_file_drop_entry_class_init (AnjutaFileDropEntryClass *klass)
193 GObjectClass *object_class = G_OBJECT_CLASS (klass);
194 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
196 object_class->finalize = anjuta_file_drop_entry_finalize;
197 object_class->set_property = anjuta_file_drop_entry_set_property;
198 object_class->get_property = anjuta_file_drop_entry_get_property;
199 widget_class->drag_data_received = anjuta_file_drop_entry_drag_data_received;
200 widget_class->drag_drop = anjuta_file_drop_entry_drag_drop;
202 g_object_class_install_property (object_class,
203 PROP_RELATIVE_PATH,
204 g_param_spec_string ("relative-path",
205 "relative-path",
206 _("Path that dropped files should be relative to"),
208 0));
211 GtkWidget *
212 anjuta_file_drop_entry_new (void)
214 return g_object_new (ANJUTA_TYPE_FILE_DROP_ENTRY, NULL);
217 void
218 anjuta_file_drop_entry_set_relative_path (AnjutaFileDropEntry *self,
219 const gchar *path)
221 g_object_set (G_OBJECT (self), "relative-path", path, NULL);