1
|
1 /*****************************************************************************/
|
|
2 /* dnd.c - drag and drop functions */
|
255
|
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
1
|
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"
|
33
|
21 static const char cvsid[] = "$Id$";
|
1
|
22
|
48
|
23
|
|
24 static int
|
50
|
25 dnd_remote_file (char *url, gftp_window_data * wdata)
|
48
|
26 {
|
56
|
27 gftp_window_data * other_wdata, * fromwdata;
|
48
|
28 gftp_request * current_ftpdata;
|
|
29 gftp_file * newfle;
|
56
|
30 GList * templist;
|
|
31 char *pos;
|
48
|
32
|
56
|
33 if (wdata == &window1)
|
|
34 other_wdata = &window2;
|
|
35 else if (wdata == &window2)
|
|
36 other_wdata = &window1;
|
|
37 else
|
|
38 other_wdata = NULL;
|
|
39
|
48
|
40 newfle = g_malloc0 (sizeof (*newfle));
|
|
41 newfle->shown = 1;
|
|
42 if (url[strlen (url) - 1] == '/')
|
|
43 {
|
|
44 newfle->isdir = 1;
|
|
45 url[strlen (url) - 1] = '\0';
|
|
46 }
|
|
47
|
|
48 current_ftpdata = gftp_request_new ();
|
|
49 current_ftpdata->logging_function = ftp_log;
|
|
50
|
56
|
51 if (gftp_parse_url (current_ftpdata, url) != 0 ||
|
|
52 current_ftpdata->directory == NULL ||
|
|
53 (pos = strrchr (current_ftpdata->directory, '/')) == NULL)
|
48
|
54 {
|
67
|
55 gftp_request_destroy (current_ftpdata, 1);
|
48
|
56 free_fdata (newfle);
|
|
57 return (0);
|
|
58 }
|
|
59
|
56
|
60 *pos++ = '\0';
|
48
|
61 if (compare_request (current_ftpdata, wdata->request, 1))
|
50
|
62 {
|
67
|
63 gftp_request_destroy (current_ftpdata, 1);
|
50
|
64 return (0);
|
|
65 }
|
48
|
66
|
56
|
67 if (other_wdata != NULL &&
|
|
68 compare_request (current_ftpdata, other_wdata->request, 1))
|
48
|
69 {
|
65
|
70 if (other_wdata->request->password != NULL)
|
|
71 gftp_set_password (current_ftpdata, other_wdata->request->password);
|
|
72
|
56
|
73 fromwdata = other_wdata;
|
48
|
74 }
|
|
75 else
|
56
|
76 fromwdata = NULL;
|
|
77
|
|
78 *(pos - 1) = '/';
|
129
|
79 newfle->file = g_strdup (current_ftpdata->directory);
|
56
|
80 *(pos - 1) = '\0';
|
48
|
81
|
245
|
82 newfle->destfile = gftp_build_path (wdata->request->directory, pos, NULL);
|
56
|
83 templist = g_malloc0 (sizeof (*templist));
|
|
84 templist->data = newfle;
|
|
85 templist->next = NULL;
|
|
86 add_file_transfer (current_ftpdata, wdata->request, fromwdata,
|
|
87 wdata, templist, 1);
|
67
|
88 gftp_request_destroy (current_ftpdata, 1);
|
48
|
89
|
58
|
90 /* FIXME resume files and download entire directories */
|
|
91
|
48
|
92 return (1);
|
|
93 }
|
|
94
|
1
|
95
|
|
96 void
|
|
97 openurl_get_drag_data (GtkWidget * widget, GdkDragContext * context, gint x,
|
|
98 gint y, GtkSelectionData * selection_data, guint info,
|
|
99 guint32 clk_time, gpointer data)
|
|
100 {
|
|
101 if ((selection_data->length >= 0) && (selection_data->format == 8))
|
|
102 {
|
|
103 if (gftp_parse_url (current_wdata->request,
|
|
104 (char *) selection_data->data) == 0)
|
|
105 {
|
|
106 if (GFTP_IS_CONNECTED (current_wdata->request))
|
|
107 disconnect (current_wdata);
|
|
108
|
|
109 ftp_connect (current_wdata, current_wdata->request, 1);
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113
|
|
114
|
|
115 void
|
|
116 listbox_drag (GtkWidget * widget, GdkDragContext * context,
|
|
117 GtkSelectionData * selection_data, guint info, guint32 clk_time,
|
|
118 gpointer data)
|
|
119 {
|
|
120 GList * templist, * filelist;
|
303
|
121 char *tempstr, *str, *df;
|
1
|
122 gftp_window_data * wdata;
|
|
123 size_t totlen, oldlen;
|
|
124 gftp_file * tempfle;
|
|
125 int curpos;
|
|
126
|
|
127 totlen = 0;
|
|
128 str = NULL;
|
|
129 wdata = data;
|
|
130 if (!check_status (_("Drag-N-Drop"), wdata, 1, 0, 1, 1))
|
|
131 return;
|
|
132
|
|
133 filelist = wdata->files;
|
|
134 templist = GTK_CLIST (wdata->listbox)->selection;
|
|
135 curpos = 0;
|
|
136 while (templist != NULL)
|
|
137 {
|
|
138 templist = get_next_selection (templist, &filelist, &curpos);
|
|
139 tempfle = filelist->data;
|
|
140
|
|
141 if (strcmp (tempfle->file, "..") == 0)
|
|
142 continue;
|
|
143
|
|
144 oldlen = totlen;
|
303
|
145 df = gftp_build_path (wdata->request->directory, tempfle->file, NULL);
|
|
146
|
129
|
147 if (wdata->request->hostname == NULL ||
|
65
|
148 wdata->request->protonum == GFTP_LOCAL_NUM)
|
1
|
149 {
|
303
|
150 tempstr = g_strdup_printf ("%s://%s ",
|
|
151 wdata->request->url_prefix, df);
|
1
|
152 }
|
303
|
153 else if (wdata->request->username == NULL ||
|
|
154 *wdata->request->username == '\0')
|
1
|
155 {
|
303
|
156 tempstr = g_strdup_printf ("%s://%s:%d%s ",
|
129
|
157 wdata->request->url_prefix,
|
|
158 wdata->request->hostname,
|
303
|
159 wdata->request->port, df);
|
1
|
160 }
|
|
161 else
|
|
162 {
|
303
|
163 tempstr = g_strdup_printf ("%s://%s@%s:%d%s ",
|
129
|
164 wdata->request->url_prefix,
|
|
165 wdata->request->username,
|
|
166 wdata->request->hostname,
|
303
|
167 wdata->request->port, df);
|
1
|
168 }
|
56
|
169
|
303
|
170 g_free (df);
|
56
|
171
|
|
172 /* Note, I am allocating memory for this byte above. Note the extra space
|
|
173 at the end of the g_strdup_printf() format argument */
|
|
174 if (tempfle->isdir)
|
|
175 tempstr[strlen (tempstr) - 1] = '/';
|
|
176 else
|
|
177 tempstr[strlen (tempstr) - 1] = '\0';
|
|
178
|
1
|
179 totlen += strlen (tempstr);
|
|
180 if (str != NULL)
|
|
181 {
|
|
182 totlen++;
|
|
183 str = g_realloc (str, totlen + 1);
|
|
184 strcpy (str + oldlen, "\n");
|
|
185 strcpy (str + oldlen + 1, tempstr);
|
|
186 }
|
|
187 else
|
|
188 {
|
|
189 str = g_malloc (totlen + 1);
|
|
190 strcpy (str, tempstr);
|
|
191 }
|
|
192 g_free (tempstr);
|
|
193 }
|
|
194
|
|
195 if (str != NULL)
|
|
196 {
|
|
197 gtk_selection_data_set (selection_data, selection_data->target, 8,
|
|
198 (unsigned char *) str, strlen (str));
|
|
199 g_free (str);
|
|
200 }
|
|
201 }
|
|
202
|
|
203
|
|
204 void
|
|
205 listbox_get_drag_data (GtkWidget * widget, GdkDragContext * context, gint x,
|
|
206 gint y, GtkSelectionData * selection_data, guint info,
|
|
207 guint32 clk_time, gpointer data)
|
|
208 {
|
296
|
209 char *newpos, *oldpos, *tempstr;
|
1
|
210 gftp_window_data * wdata;
|
|
211 int finish_drag;
|
296
|
212 size_t len;
|
1
|
213
|
|
214 wdata = data;
|
|
215 if (!check_status (_("Drag-N-Drop"), wdata, 1, 0, 0, 1))
|
|
216 return;
|
|
217
|
|
218 finish_drag = 0;
|
|
219 if ((selection_data->length >= 0) && (selection_data->format == 8))
|
|
220 {
|
|
221 oldpos = (char *) selection_data->data;
|
50
|
222 while ((newpos = strchr (oldpos, '\n')) ||
|
|
223 (newpos = strchr (oldpos, '\0')))
|
1
|
224 {
|
296
|
225 len = newpos - oldpos;
|
|
226 if (oldpos[len - 1] == '\r')
|
|
227 len--;
|
|
228
|
|
229 if (len == 0)
|
|
230 break;
|
|
231
|
|
232 tempstr = g_malloc (len + 1);
|
|
233 memcpy (tempstr, oldpos, len);
|
|
234 tempstr[len] = '\0';
|
|
235
|
|
236 ftp_log (gftp_logging_misc, NULL, _("Received URL %s\n"), tempstr);
|
|
237
|
|
238 if (dnd_remote_file (tempstr, wdata))
|
1
|
239 finish_drag = 1;
|
296
|
240 g_free (tempstr);
|
|
241
|
1
|
242 if (*newpos == '\0')
|
|
243 break;
|
296
|
244
|
1
|
245 oldpos = newpos + 1;
|
|
246 }
|
|
247 }
|
|
248
|
|
249 gtk_drag_finish (context, finish_drag, FALSE, clk_time);
|
|
250 }
|