1
|
1 /*****************************************************************************/
|
|
2 /* dnd.c - drag and drop functions */
|
|
3 /* Copyright (C) 1998-2002 Brian Masney <masneyb@gftp.org> */
|
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program is distributed in the hope that it will be useful, */
|
|
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 #include "gftp-gtk.h"
|
|
21
|
|
22 static int dnd_remote_file ( char *url,
|
|
23 GList ** transfers,
|
|
24 gftp_window_data * wdata );
|
|
25
|
|
26 void
|
|
27 openurl_get_drag_data (GtkWidget * widget, GdkDragContext * context, gint x,
|
|
28 gint y, GtkSelectionData * selection_data, guint info,
|
|
29 guint32 clk_time, gpointer data)
|
|
30 {
|
|
31 if ((selection_data->length >= 0) && (selection_data->format == 8))
|
|
32 {
|
|
33 if (gftp_parse_url (current_wdata->request,
|
|
34 (char *) selection_data->data) == 0)
|
|
35 {
|
|
36 if (GFTP_IS_CONNECTED (current_wdata->request))
|
|
37 disconnect (current_wdata);
|
|
38
|
|
39 ftp_connect (current_wdata, current_wdata->request, 1);
|
|
40 }
|
|
41 else
|
|
42 {
|
|
43 ftp_log (gftp_logging_misc, NULL, _("Could not parse URL %s\n"),
|
|
44 selection_data->data);
|
|
45 }
|
|
46 }
|
|
47 }
|
|
48
|
|
49
|
|
50 void
|
|
51 listbox_drag (GtkWidget * widget, GdkDragContext * context,
|
|
52 GtkSelectionData * selection_data, guint info, guint32 clk_time,
|
|
53 gpointer data)
|
|
54 {
|
|
55 GList * templist, * filelist;
|
|
56 char *tempstr, *str, *pos;
|
|
57 gftp_window_data * wdata;
|
|
58 size_t totlen, oldlen;
|
|
59 gftp_file * tempfle;
|
|
60 int curpos;
|
|
61
|
|
62 totlen = 0;
|
|
63 str = NULL;
|
|
64 wdata = data;
|
|
65 if (!check_status (_("Drag-N-Drop"), wdata, 1, 0, 1, 1))
|
|
66 return;
|
|
67
|
|
68 filelist = wdata->files;
|
|
69 templist = GTK_CLIST (wdata->listbox)->selection;
|
|
70 curpos = 0;
|
|
71 while (templist != NULL)
|
|
72 {
|
|
73 templist = get_next_selection (templist, &filelist, &curpos);
|
|
74 tempfle = filelist->data;
|
|
75
|
|
76 if (strcmp (tempfle->file, "..") == 0)
|
|
77 continue;
|
|
78
|
|
79 oldlen = totlen;
|
|
80 if (GFTP_GET_HOSTNAME (wdata->request) == NULL)
|
|
81 {
|
|
82 tempstr = g_strdup_printf ("%s://%s/%s%c",
|
|
83 GFTP_GET_URL_PREFIX (wdata->request),
|
|
84 GFTP_GET_DIRECTORY (wdata->request),
|
|
85 tempfle->file, tempfle->isdir ? '/' : '\0');
|
|
86 }
|
|
87 else if (GFTP_GET_USERNAME (wdata->request) == NULL
|
|
88 || *GFTP_GET_USERNAME (wdata->request) == '\0')
|
|
89 {
|
|
90 tempstr = g_strdup_printf ("%s://%s:%d%s/%s%c",
|
|
91 GFTP_GET_URL_PREFIX (wdata->request),
|
|
92 GFTP_GET_HOSTNAME (wdata->request),
|
|
93 GFTP_GET_PORT (wdata->request),
|
|
94 GFTP_GET_DIRECTORY (wdata->request),
|
|
95 tempfle->file, tempfle->isdir ? '/' : '\0');
|
|
96 }
|
|
97 else
|
|
98 {
|
|
99 tempstr = g_strdup_printf ("%s://%s@%s:%d%s/%s%c",
|
|
100 GFTP_GET_URL_PREFIX (wdata->request),
|
|
101 GFTP_GET_USERNAME (wdata->request),
|
|
102 GFTP_GET_HOSTNAME (wdata->request),
|
|
103 GFTP_GET_PORT (wdata->request),
|
|
104 GFTP_GET_DIRECTORY (wdata->request),
|
|
105 tempfle->file, tempfle->isdir ? '/' : '\0');
|
|
106 }
|
|
107 if ((pos = strchr (tempstr, ':')) != NULL)
|
|
108 pos += 2;
|
|
109 else
|
|
110 pos = tempstr;
|
|
111 remove_double_slashes (pos);
|
|
112 totlen += strlen (tempstr);
|
|
113 if (str != NULL)
|
|
114 {
|
|
115 totlen++;
|
|
116 str = g_realloc (str, totlen + 1);
|
|
117 strcpy (str + oldlen, "\n");
|
|
118 strcpy (str + oldlen + 1, tempstr);
|
|
119 }
|
|
120 else
|
|
121 {
|
|
122 str = g_malloc (totlen + 1);
|
|
123 strcpy (str, tempstr);
|
|
124 }
|
|
125 g_free (tempstr);
|
|
126 }
|
|
127
|
|
128 if (str != NULL)
|
|
129 {
|
|
130 gtk_selection_data_set (selection_data, selection_data->target, 8,
|
|
131 (unsigned char *) str, strlen (str));
|
|
132 g_free (str);
|
|
133 }
|
|
134 }
|
|
135
|
|
136
|
|
137 void
|
|
138 listbox_get_drag_data (GtkWidget * widget, GdkDragContext * context, gint x,
|
|
139 gint y, GtkSelectionData * selection_data, guint info,
|
|
140 guint32 clk_time, gpointer data)
|
|
141 {
|
|
142 GList * new_file_transfers, * templist;
|
|
143 char *newpos, *oldpos, tempchar;
|
|
144 gftp_window_data * wdata;
|
|
145 gftp_transfer * tdata;
|
|
146 int finish_drag;
|
|
147
|
|
148 wdata = data;
|
|
149 if (!check_status (_("Drag-N-Drop"), wdata, 1, 0, 0, 1))
|
|
150 return;
|
|
151
|
|
152 new_file_transfers = NULL;
|
|
153 finish_drag = 0;
|
|
154 if ((selection_data->length >= 0) && (selection_data->format == 8))
|
|
155 {
|
|
156 oldpos = (char *) selection_data->data;
|
|
157 while ((newpos = strchr (oldpos, '\n'))
|
|
158 || (newpos = strchr (oldpos, '\0')))
|
|
159 {
|
|
160 tempchar = *newpos;
|
|
161 *newpos = '\0';
|
|
162 ftp_log (gftp_logging_misc, NULL, _("Received URL %s\n"), oldpos);
|
|
163 if (dnd_remote_file (oldpos, &new_file_transfers, wdata))
|
|
164 finish_drag = 1;
|
|
165
|
|
166 if (*newpos == '\0')
|
|
167 break;
|
|
168 oldpos = newpos + 1;
|
|
169 }
|
|
170 }
|
|
171
|
|
172 if (finish_drag)
|
|
173 {
|
|
174 for (templist = new_file_transfers; templist != NULL;
|
|
175 templist = templist->next)
|
|
176 {
|
|
177 tdata = templist->data;
|
|
178 if (tdata->files != NULL)
|
|
179 add_file_transfer (tdata->fromreq, tdata->toreq, tdata->fromwdata,
|
|
180 tdata->towdata, tdata->files, 0);
|
|
181 }
|
|
182 }
|
|
183 gtk_drag_finish (context, finish_drag, FALSE, clk_time);
|
|
184 }
|
|
185
|
|
186
|
|
187 static int
|
|
188 dnd_remote_file (char *url, GList ** transfers, gftp_window_data * wdata)
|
|
189 {
|
|
190 gftp_request * current_ftpdata;
|
|
191 gftp_window_data * fromwdata;
|
|
192 gftp_transfer * tdata;
|
|
193 gftp_file * newfle;
|
|
194 GList * templist;
|
|
195 char *str, *pos;
|
|
196 int i;
|
|
197
|
|
198 newfle = g_malloc0 (sizeof (*newfle));
|
|
199 newfle->shown = 1;
|
|
200 if (url[strlen (url) - 1] == '/')
|
|
201 {
|
|
202 newfle->isdir = 1;
|
|
203 url[strlen (url) - 1] = '\0';
|
|
204 }
|
|
205
|
|
206 current_ftpdata = gftp_request_new ();
|
|
207 current_ftpdata->logging_function = ftp_log;
|
|
208
|
|
209 if (gftp_parse_url (current_ftpdata, url) != 0)
|
|
210 {
|
|
211 ftp_log (gftp_logging_misc, NULL,
|
|
212 _("Drag-N-Drop: Ignoring url %s: Not a valid url\n"), url);
|
|
213 gftp_request_destroy (current_ftpdata);
|
|
214 free_fdata (newfle);
|
|
215 return (0);
|
|
216 }
|
|
217
|
|
218 if ((str = GFTP_GET_DIRECTORY (current_ftpdata)) != NULL)
|
|
219 {
|
|
220 if ((pos = strrchr (str, '/')) == NULL)
|
|
221 pos = str;
|
|
222 else pos++;
|
|
223 *(pos - 1) = '\0';
|
|
224 i = 1;
|
|
225 }
|
|
226 else
|
|
227 {
|
|
228 pos = str = GFTP_GET_DIRECTORY (current_ftpdata);
|
|
229 i = 0;
|
|
230 }
|
|
231
|
|
232 if (compare_request (current_ftpdata, wdata->request, 1))
|
|
233 return (0);
|
|
234
|
|
235 if (i)
|
|
236 {
|
|
237 *(pos - 1) = '/';
|
|
238 newfle->file = g_malloc (strlen (str) + 1);
|
|
239 strcpy (newfle->file, str);
|
|
240 *(pos - 1) = '\0';
|
|
241 }
|
|
242 else
|
|
243 {
|
|
244 newfle->file = g_malloc (strlen (str) + 1);
|
|
245 strcpy (newfle->file, str);
|
|
246 }
|
|
247
|
|
248 newfle->destfile = g_strconcat (GFTP_GET_DIRECTORY (wdata->request),
|
|
249 "/", pos, NULL);
|
|
250 newfle->ascii = gftp_get_file_transfer_mode (newfle->file,
|
|
251 wdata->request->data_type) == GFTP_TYPE_ASCII;
|
|
252
|
|
253 tdata = NULL;
|
|
254 templist = *transfers;
|
|
255 while (templist != NULL)
|
|
256 {
|
|
257 tdata = templist->data;
|
|
258 if (compare_request (tdata->fromreq, current_ftpdata, 1))
|
|
259 break;
|
|
260 templist = templist->next;
|
|
261 }
|
|
262
|
|
263 if (tdata == NULL)
|
|
264 {
|
|
265 tdata = g_malloc0 (sizeof (*tdata));
|
|
266 tdata->towdata = wdata == &window1 ? &window1 : &window2;
|
|
267 fromwdata = wdata == &window1 ? &window2 : &window1;
|
7
|
268 if (fromwdata->request != NULL &&
|
|
269 compare_request (fromwdata->request, current_ftpdata, 1))
|
1
|
270 {
|
7
|
271 if (fromwdata->request->password != NULL)
|
|
272 gftp_set_password (current_ftpdata, fromwdata->request->password);
|
1
|
273 tdata->fromwdata = fromwdata;
|
|
274 }
|
|
275 tdata->fromreq = current_ftpdata;
|
|
276 tdata->toreq = gftp_request_new ();
|
|
277 tdata->toreq->logging_function = ftp_log;
|
|
278 tdata->toreq = copy_request (wdata->request);
|
|
279 *transfers = g_list_append (*transfers, tdata);
|
|
280 }
|
|
281 else
|
|
282 gftp_request_destroy (current_ftpdata);
|
|
283
|
|
284 if (newfle->isdir)
|
|
285 {
|
|
286 /* FIXME - need to fix this
|
|
287 add_entire_directory (tdata, newfle,
|
|
288 GFTP_GET_DIRECTORY (tdata->fromhdata->ftpdata),
|
|
289 GFTP_GET_DIRECTORY (tdata->tohdata->ftpdata),
|
|
290 tdata->fromhdata->ftpdata);
|
|
291 */
|
|
292 }
|
|
293 else
|
|
294 {
|
|
295 tdata->files = g_list_append (tdata->files, newfle);
|
|
296 if (tdata->curfle == NULL)
|
|
297 tdata->curfle = tdata->files;
|
|
298 }
|
|
299 return (1);
|
|
300 }
|
|
301
|