1 | /* $Id: VBoxDnDDropTarget.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDnDTarget.cpp - IDropTarget implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #define LOG_GROUP LOG_GROUP_GUEST_DND
|
---|
29 | #include <VBox/log.h>
|
---|
30 |
|
---|
31 | #include <iprt/win/windows.h>
|
---|
32 | #include <new> /* For bad_alloc. */
|
---|
33 | #include <iprt/win/shlobj.h> /* For DROPFILES and friends. */
|
---|
34 |
|
---|
35 | #include "VBoxTray.h"
|
---|
36 | #include "VBoxHelpers.h"
|
---|
37 | #include "VBoxDnD.h"
|
---|
38 |
|
---|
39 | #include "VBox/GuestHost/DragAndDrop.h"
|
---|
40 | #include "VBox/HostServices/DragAndDropSvc.h"
|
---|
41 |
|
---|
42 | #include <iprt/path.h>
|
---|
43 | #include <iprt/utf16.h>
|
---|
44 | #include <iprt/uri.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | VBoxDnDDropTarget::VBoxDnDDropTarget(VBoxDnDWnd *pParent)
|
---|
48 | : m_cRefs(1),
|
---|
49 | m_pWndParent(pParent),
|
---|
50 | m_dwCurEffect(0),
|
---|
51 | m_pvData(NULL),
|
---|
52 | m_cbData(0),
|
---|
53 | m_EvtDrop(NIL_RTSEMEVENT)
|
---|
54 | {
|
---|
55 | int rc = RTSemEventCreate(&m_EvtDrop);
|
---|
56 | LogFlowFunc(("rc=%Rrc\n", rc)); NOREF(rc);
|
---|
57 | }
|
---|
58 |
|
---|
59 | VBoxDnDDropTarget::~VBoxDnDDropTarget(void)
|
---|
60 | {
|
---|
61 | reset();
|
---|
62 |
|
---|
63 | int rc2 = RTSemEventDestroy(m_EvtDrop);
|
---|
64 | AssertRC(rc2);
|
---|
65 |
|
---|
66 | LogFlowFunc(("rc=%Rrc, mRefCount=%RI32\n", rc2, m_cRefs));
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * IUnknown methods.
|
---|
71 | */
|
---|
72 |
|
---|
73 | STDMETHODIMP_(ULONG) VBoxDnDDropTarget::AddRef(void)
|
---|
74 | {
|
---|
75 | return InterlockedIncrement(&m_cRefs);
|
---|
76 | }
|
---|
77 |
|
---|
78 | STDMETHODIMP_(ULONG) VBoxDnDDropTarget::Release(void)
|
---|
79 | {
|
---|
80 | LONG lCount = InterlockedDecrement(&m_cRefs);
|
---|
81 | if (lCount == 0)
|
---|
82 | {
|
---|
83 | delete this;
|
---|
84 | return 0;
|
---|
85 | }
|
---|
86 |
|
---|
87 | return lCount;
|
---|
88 | }
|
---|
89 |
|
---|
90 | STDMETHODIMP VBoxDnDDropTarget::QueryInterface(REFIID iid, void **ppvObject)
|
---|
91 | {
|
---|
92 | AssertPtrReturn(ppvObject, E_INVALIDARG);
|
---|
93 |
|
---|
94 | if ( iid == IID_IDropSource
|
---|
95 | || iid == IID_IUnknown)
|
---|
96 | {
|
---|
97 | AddRef();
|
---|
98 | *ppvObject = this;
|
---|
99 | return S_OK;
|
---|
100 | }
|
---|
101 |
|
---|
102 | *ppvObject = 0;
|
---|
103 | return E_NOINTERFACE;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Static helper function to dump supported formats of a data object.
|
---|
108 | *
|
---|
109 | * @param pDataObject Pointer to data object to dump formats for.
|
---|
110 | */
|
---|
111 | /* static */
|
---|
112 | void VBoxDnDDropTarget::DumpFormats(IDataObject *pDataObject)
|
---|
113 | {
|
---|
114 | AssertPtrReturnVoid(pDataObject);
|
---|
115 |
|
---|
116 | /* Enumerate supported source formats. This shouldn't happen too often
|
---|
117 | * on day to day use, but still keep it in here. */
|
---|
118 | IEnumFORMATETC *pEnumFormats;
|
---|
119 | HRESULT hr2 = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormats);
|
---|
120 | if (SUCCEEDED(hr2))
|
---|
121 | {
|
---|
122 | LogRel(("DnD: The following formats were offered to us:\n"));
|
---|
123 |
|
---|
124 | FORMATETC curFormatEtc;
|
---|
125 | while (pEnumFormats->Next(1, &curFormatEtc,
|
---|
126 | NULL /* pceltFetched */) == S_OK)
|
---|
127 | {
|
---|
128 | WCHAR wszCfName[128]; /* 128 chars should be enough, rest will be truncated. */
|
---|
129 | hr2 = GetClipboardFormatNameW(curFormatEtc.cfFormat, wszCfName,
|
---|
130 | sizeof(wszCfName) / sizeof(WCHAR));
|
---|
131 | LogRel(("\tcfFormat=%RI16 (%s), tyMed=%RI32, dwAspect=%RI32, strCustomName=%ls, hr=%Rhrc\n",
|
---|
132 | curFormatEtc.cfFormat,
|
---|
133 | VBoxDnDDataObject::ClipboardFormatToString(curFormatEtc.cfFormat),
|
---|
134 | curFormatEtc.tymed,
|
---|
135 | curFormatEtc.dwAspect,
|
---|
136 | wszCfName, hr2));
|
---|
137 | }
|
---|
138 |
|
---|
139 | pEnumFormats->Release();
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /*
|
---|
144 | * IDropTarget methods.
|
---|
145 | */
|
---|
146 |
|
---|
147 | STDMETHODIMP VBoxDnDDropTarget::DragEnter(IDataObject *pDataObject, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
|
---|
148 | {
|
---|
149 | RT_NOREF(pt);
|
---|
150 | AssertPtrReturn(pDataObject, E_INVALIDARG);
|
---|
151 | AssertPtrReturn(pdwEffect, E_INVALIDARG);
|
---|
152 |
|
---|
153 | LogFlowFunc(("pDataObject=0x%p, grfKeyState=0x%x, x=%ld, y=%ld, dwEffect=%RU32\n",
|
---|
154 | pDataObject, grfKeyState, pt.x, pt.y, *pdwEffect));
|
---|
155 |
|
---|
156 | reset();
|
---|
157 |
|
---|
158 | /** @todo At the moment we only support one DnD format at a time. */
|
---|
159 |
|
---|
160 | #ifdef DEBUG
|
---|
161 | VBoxDnDDropTarget::DumpFormats(pDataObject);
|
---|
162 | #endif
|
---|
163 |
|
---|
164 | /* Try different formats.
|
---|
165 | * CF_HDROP is the most common one, so start with this. */
|
---|
166 | FORMATETC fmtEtc = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
|
---|
167 | HRESULT hr = pDataObject->QueryGetData(&fmtEtc);
|
---|
168 | if (hr == S_OK)
|
---|
169 | {
|
---|
170 | m_strFormat = "text/uri-list";
|
---|
171 | }
|
---|
172 | else
|
---|
173 | {
|
---|
174 | LogFlowFunc(("CF_HDROP not wanted, hr=%Rhrc\n", hr));
|
---|
175 |
|
---|
176 | /* So we couldn't retrieve the data in CF_HDROP format; try with
|
---|
177 | * CF_UNICODETEXT + CF_TEXT formats now. Rest stays the same. */
|
---|
178 | fmtEtc.cfFormat = CF_UNICODETEXT;
|
---|
179 | hr = pDataObject->QueryGetData(&fmtEtc);
|
---|
180 | if (hr == S_OK)
|
---|
181 | {
|
---|
182 | m_strFormat = "text/plain;charset=utf-8";
|
---|
183 | }
|
---|
184 | else
|
---|
185 | {
|
---|
186 | LogFlowFunc(("CF_UNICODETEXT not wanted, hr=%Rhrc\n", hr));
|
---|
187 |
|
---|
188 | fmtEtc.cfFormat = CF_TEXT;
|
---|
189 | hr = pDataObject->QueryGetData(&fmtEtc);
|
---|
190 | if (hr == S_OK)
|
---|
191 | {
|
---|
192 | m_strFormat = "text/plain;charset=utf-8";
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | LogFlowFunc(("CF_TEXT not wanted, hr=%Rhrc\n", hr));
|
---|
197 | fmtEtc.cfFormat = 0; /* Set it to non-supported. */
|
---|
198 |
|
---|
199 | /* Clean up. */
|
---|
200 | reset();
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* Did we find a format that we support? */
|
---|
206 | if (fmtEtc.cfFormat)
|
---|
207 | {
|
---|
208 | LogFlowFunc(("Found supported format %RI16 (%s)\n",
|
---|
209 | fmtEtc.cfFormat, VBoxDnDDataObject::ClipboardFormatToString(fmtEtc.cfFormat)));
|
---|
210 |
|
---|
211 | /* Make a copy of the FORMATETC structure so that we later can
|
---|
212 | * use this for comparrison and stuff. */
|
---|
213 | /** @todo The DVTARGETDEVICE member only is a shallow copy for now! */
|
---|
214 | memcpy(&m_FormatEtc, &fmtEtc, sizeof(FORMATETC));
|
---|
215 |
|
---|
216 | /* Which drop effect we're going to use? */
|
---|
217 | /* Note: pt is not used since we don't need to differentiate within our
|
---|
218 | * proxy window. */
|
---|
219 | *pdwEffect = VBoxDnDDropTarget::GetDropEffect(grfKeyState, *pdwEffect);
|
---|
220 | }
|
---|
221 | else
|
---|
222 | {
|
---|
223 | /* No or incompatible data -- so no drop effect required. */
|
---|
224 | *pdwEffect = DROPEFFECT_NONE;
|
---|
225 |
|
---|
226 | switch (hr)
|
---|
227 | {
|
---|
228 | case ERROR_INVALID_FUNCTION:
|
---|
229 | {
|
---|
230 | LogRel(("DnD: Drag and drop format is not supported by VBoxTray\n"));
|
---|
231 | VBoxDnDDropTarget::DumpFormats(pDataObject);
|
---|
232 | break;
|
---|
233 | }
|
---|
234 |
|
---|
235 | default:
|
---|
236 | break;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | LogFlowFunc(("Returning mstrFormats=%s, cfFormat=%RI16, pdwEffect=%ld, hr=%Rhrc\n",
|
---|
241 | m_strFormat.c_str(), fmtEtc.cfFormat, *pdwEffect, hr));
|
---|
242 | return hr;
|
---|
243 | }
|
---|
244 |
|
---|
245 | STDMETHODIMP VBoxDnDDropTarget::DragOver(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
|
---|
246 | {
|
---|
247 | RT_NOREF(pt);
|
---|
248 | AssertPtrReturn(pdwEffect, E_INVALIDARG);
|
---|
249 |
|
---|
250 | #ifdef DEBUG_andy
|
---|
251 | LogFlowFunc(("cfFormat=%RI16, grfKeyState=0x%x, x=%ld, y=%ld\n",
|
---|
252 | m_FormatEtc.cfFormat, grfKeyState, pt.x, pt.y));
|
---|
253 | #endif
|
---|
254 |
|
---|
255 | if (m_FormatEtc.cfFormat)
|
---|
256 | {
|
---|
257 | /* Note: pt is not used since we don't need to differentiate within our
|
---|
258 | * proxy window. */
|
---|
259 | *pdwEffect = VBoxDnDDropTarget::GetDropEffect(grfKeyState, *pdwEffect);
|
---|
260 | }
|
---|
261 | else
|
---|
262 | {
|
---|
263 | *pdwEffect = DROPEFFECT_NONE;
|
---|
264 | }
|
---|
265 |
|
---|
266 | #ifdef DEBUG_andy
|
---|
267 | LogFlowFunc(("Returning *pdwEffect=%ld\n", *pdwEffect));
|
---|
268 | #endif
|
---|
269 | return S_OK;
|
---|
270 | }
|
---|
271 |
|
---|
272 | STDMETHODIMP VBoxDnDDropTarget::DragLeave(void)
|
---|
273 | {
|
---|
274 | #ifdef DEBUG_andy
|
---|
275 | LogFlowFunc(("cfFormat=%RI16\n", m_FormatEtc.cfFormat));
|
---|
276 | #endif
|
---|
277 |
|
---|
278 | if (m_pWndParent)
|
---|
279 | m_pWndParent->Hide();
|
---|
280 |
|
---|
281 | return S_OK;
|
---|
282 | }
|
---|
283 |
|
---|
284 | STDMETHODIMP VBoxDnDDropTarget::Drop(IDataObject *pDataObject, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
|
---|
285 | {
|
---|
286 | RT_NOREF(pt);
|
---|
287 | AssertPtrReturn(pDataObject, E_INVALIDARG);
|
---|
288 | AssertPtrReturn(pdwEffect, E_INVALIDARG);
|
---|
289 |
|
---|
290 | LogFlowFunc(("mFormatEtc.cfFormat=%RI16 (%s), pDataObject=0x%p, grfKeyState=0x%x, x=%ld, y=%ld\n",
|
---|
291 | m_FormatEtc.cfFormat, VBoxDnDDataObject::ClipboardFormatToString(m_FormatEtc.cfFormat),
|
---|
292 | pDataObject, grfKeyState, pt.x, pt.y));
|
---|
293 |
|
---|
294 | HRESULT hr = S_OK;
|
---|
295 |
|
---|
296 | if (m_FormatEtc.cfFormat) /* Did we get a supported format yet? */
|
---|
297 | {
|
---|
298 | /* Make sure the data object's data format is still valid. */
|
---|
299 | hr = pDataObject->QueryGetData(&m_FormatEtc);
|
---|
300 | AssertMsg(SUCCEEDED(hr),
|
---|
301 | ("Data format changed to invalid between DragEnter() and Drop(), cfFormat=%RI16 (%s), hr=%Rhrc\n",
|
---|
302 | m_FormatEtc.cfFormat, VBoxDnDDataObject::ClipboardFormatToString(m_FormatEtc.cfFormat), hr));
|
---|
303 | }
|
---|
304 |
|
---|
305 | int rc = VINF_SUCCESS;
|
---|
306 |
|
---|
307 | if (SUCCEEDED(hr))
|
---|
308 | {
|
---|
309 | STGMEDIUM stgMed;
|
---|
310 | hr = pDataObject->GetData(&m_FormatEtc, &stgMed);
|
---|
311 | if (SUCCEEDED(hr))
|
---|
312 | {
|
---|
313 | /*
|
---|
314 | * First stage: Prepare the access to the storage medium.
|
---|
315 | * For now we only support HGLOBAL stuff.
|
---|
316 | */
|
---|
317 | PVOID pvData = NULL; /** @todo Put this in an own union? */
|
---|
318 |
|
---|
319 | switch (m_FormatEtc.tymed)
|
---|
320 | {
|
---|
321 | case TYMED_HGLOBAL:
|
---|
322 | pvData = GlobalLock(stgMed.hGlobal);
|
---|
323 | if (!pvData)
|
---|
324 | {
|
---|
325 | LogFlowFunc(("Locking HGLOBAL storage failed with %Rrc\n",
|
---|
326 | RTErrConvertFromWin32(GetLastError())));
|
---|
327 | rc = VERR_INVALID_HANDLE;
|
---|
328 | hr = E_INVALIDARG; /* Set special hr for OLE. */
|
---|
329 | }
|
---|
330 | break;
|
---|
331 |
|
---|
332 | default:
|
---|
333 | AssertMsgFailed(("Storage medium type %RI32 supported\n",
|
---|
334 | m_FormatEtc.tymed));
|
---|
335 | rc = VERR_NOT_SUPPORTED;
|
---|
336 | hr = DV_E_TYMED; /* Set special hr for OLE. */
|
---|
337 | break;
|
---|
338 | }
|
---|
339 |
|
---|
340 | if (RT_SUCCESS(rc))
|
---|
341 | {
|
---|
342 | /*
|
---|
343 | * Second stage: Do the actual copying of the data object's data,
|
---|
344 | * based on the storage medium type.
|
---|
345 | */
|
---|
346 | switch (m_FormatEtc.cfFormat)
|
---|
347 | {
|
---|
348 | case CF_TEXT:
|
---|
349 | RT_FALL_THROUGH();
|
---|
350 | case CF_UNICODETEXT:
|
---|
351 | {
|
---|
352 | AssertPtr(pvData);
|
---|
353 | size_t cbSize = GlobalSize(pvData);
|
---|
354 |
|
---|
355 | LogRel(("DnD: Got %zu bytes of %s\n", cbSize,
|
---|
356 | m_FormatEtc.cfFormat == CF_TEXT
|
---|
357 | ? "ANSI text" : "Unicode text"));
|
---|
358 | if (cbSize)
|
---|
359 | {
|
---|
360 | char *pszText = NULL;
|
---|
361 |
|
---|
362 | rc = m_FormatEtc.cfFormat == CF_TEXT
|
---|
363 | /* ANSI codepage -> UTF-8 */
|
---|
364 | ? RTStrCurrentCPToUtf8(&pszText, (char *)pvData)
|
---|
365 | /* Unicode -> UTF-8 */
|
---|
366 | : RTUtf16ToUtf8((PCRTUTF16)pvData, &pszText);
|
---|
367 |
|
---|
368 | if (RT_SUCCESS(rc))
|
---|
369 | {
|
---|
370 | AssertPtr(pszText);
|
---|
371 |
|
---|
372 | size_t cbText = strlen(pszText) + 1; /* Include termination. */
|
---|
373 |
|
---|
374 | m_pvData = RTMemDup((void *)pszText, cbText);
|
---|
375 | m_cbData = cbText;
|
---|
376 |
|
---|
377 | RTStrFree(pszText);
|
---|
378 | pszText = NULL;
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | break;
|
---|
383 | }
|
---|
384 |
|
---|
385 | case CF_HDROP:
|
---|
386 | {
|
---|
387 | AssertPtr(pvData);
|
---|
388 |
|
---|
389 | /* Convert to a string list, separated by \r\n. */
|
---|
390 | DROPFILES *pDropFiles = (DROPFILES *)pvData;
|
---|
391 | AssertPtr(pDropFiles);
|
---|
392 |
|
---|
393 | /** @todo Replace / merge the following code with VBoxShClWinDropFilesToStringList(). */
|
---|
394 |
|
---|
395 | /* Do we need to do Unicode stuff? */
|
---|
396 | const bool fUnicode = RT_BOOL(pDropFiles->fWide);
|
---|
397 |
|
---|
398 | /* Get the offset of the file list. */
|
---|
399 | Assert(pDropFiles->pFiles >= sizeof(DROPFILES));
|
---|
400 |
|
---|
401 | /* Note: This is *not* pDropFiles->pFiles! DragQueryFile only
|
---|
402 | * will work with the plain storage medium pointer! */
|
---|
403 | HDROP hDrop = (HDROP)(pvData);
|
---|
404 |
|
---|
405 | /* First, get the file count. */
|
---|
406 | /** @todo Does this work on Windows 2000 / NT4? */
|
---|
407 | char *pszFiles = NULL;
|
---|
408 | size_t cchFiles = 0;
|
---|
409 | UINT cFiles = DragQueryFile(hDrop, UINT32_MAX /* iFile */, NULL /* lpszFile */, 0 /* cchFile */);
|
---|
410 |
|
---|
411 | LogRel(("DnD: Got %RU16 file(s), fUnicode=%RTbool\n", cFiles, fUnicode));
|
---|
412 |
|
---|
413 | for (UINT i = 0; i < cFiles; i++)
|
---|
414 | {
|
---|
415 | UINT cchFile = DragQueryFile(hDrop, i /* File index */, NULL /* Query size first */, 0 /* cchFile */);
|
---|
416 | Assert(cchFile);
|
---|
417 |
|
---|
418 | if (RT_FAILURE(rc))
|
---|
419 | break;
|
---|
420 |
|
---|
421 | char *pszFileUtf8 = NULL; /* UTF-8 version. */
|
---|
422 | UINT cchFileUtf8 = 0;
|
---|
423 | if (fUnicode)
|
---|
424 | {
|
---|
425 | /* Allocate enough space (including terminator). */
|
---|
426 | WCHAR *pwszFile = (WCHAR *)RTMemAlloc((cchFile + 1) * sizeof(WCHAR));
|
---|
427 | if (pwszFile)
|
---|
428 | {
|
---|
429 | const UINT cwcFileUtf16 = DragQueryFileW(hDrop, i /* File index */,
|
---|
430 | pwszFile, cchFile + 1 /* Include terminator */);
|
---|
431 |
|
---|
432 | AssertMsg(cwcFileUtf16 == cchFile, ("cchFileUtf16 (%RU16) does not match cchFile (%RU16)\n",
|
---|
433 | cwcFileUtf16, cchFile));
|
---|
434 | RT_NOREF(cwcFileUtf16);
|
---|
435 |
|
---|
436 | rc = RTUtf16ToUtf8(pwszFile, &pszFileUtf8);
|
---|
437 | if (RT_SUCCESS(rc))
|
---|
438 | {
|
---|
439 | cchFileUtf8 = (UINT)strlen(pszFileUtf8);
|
---|
440 | Assert(cchFileUtf8);
|
---|
441 | }
|
---|
442 |
|
---|
443 | RTMemFree(pwszFile);
|
---|
444 | }
|
---|
445 | else
|
---|
446 | rc = VERR_NO_MEMORY;
|
---|
447 | }
|
---|
448 | else /* ANSI */
|
---|
449 | {
|
---|
450 | /* Allocate enough space (including terminator). */
|
---|
451 | pszFileUtf8 = (char *)RTMemAlloc((cchFile + 1) * sizeof(char));
|
---|
452 | if (pszFileUtf8)
|
---|
453 | {
|
---|
454 | cchFileUtf8 = DragQueryFileA(hDrop, i /* File index */,
|
---|
455 | pszFileUtf8, cchFile + 1 /* Include terminator */);
|
---|
456 |
|
---|
457 | AssertMsg(cchFileUtf8 == cchFile, ("cchFileUtf8 (%RU16) does not match cchFile (%RU16)\n",
|
---|
458 | cchFileUtf8, cchFile));
|
---|
459 | }
|
---|
460 | else
|
---|
461 | rc = VERR_NO_MEMORY;
|
---|
462 | }
|
---|
463 |
|
---|
464 | if (RT_SUCCESS(rc))
|
---|
465 | {
|
---|
466 | LogFlowFunc(("\tFile: %s (cchFile=%RU16)\n", pszFileUtf8, cchFileUtf8));
|
---|
467 |
|
---|
468 | LogRel(("DnD: Adding guest file '%s'\n", pszFileUtf8));
|
---|
469 |
|
---|
470 | if (RT_SUCCESS(rc))
|
---|
471 | {
|
---|
472 | char *pszFileURI = RTUriFileCreate(pszFileUtf8);
|
---|
473 | if (pszFileURI)
|
---|
474 | {
|
---|
475 | const size_t cchFileURI = RTStrNLen(pszFileURI, RTPATH_MAX);
|
---|
476 | rc = RTStrAAppendExN(&pszFiles, 1 /* cPairs */, pszFileURI, cchFileURI);
|
---|
477 | if (RT_SUCCESS(rc))
|
---|
478 | cchFiles += cchFileURI;
|
---|
479 |
|
---|
480 | RTStrFree(pszFileURI);
|
---|
481 | }
|
---|
482 | else
|
---|
483 | rc = VERR_NO_MEMORY;
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | if (RT_FAILURE(rc))
|
---|
488 | LogRel(("DnD: Error handling file entry #%u, rc=%Rrc\n", i, rc));
|
---|
489 |
|
---|
490 | RTStrFree(pszFileUtf8);
|
---|
491 |
|
---|
492 | if (RT_SUCCESS(rc))
|
---|
493 | {
|
---|
494 | /* Add separation between filenames.
|
---|
495 | * Note: Also do this for the last element of the list. */
|
---|
496 | rc = RTStrAAppendExN(&pszFiles, 1 /* cPairs */, DND_PATH_SEPARATOR_STR, 2 /* Bytes */);
|
---|
497 | if (RT_SUCCESS(rc))
|
---|
498 | cchFiles += 2; /* Include \r\n */
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | if (RT_SUCCESS(rc))
|
---|
503 | {
|
---|
504 | cchFiles += 1; /* Add string termination. */
|
---|
505 |
|
---|
506 | const size_t cbFiles = cchFiles * sizeof(char);
|
---|
507 |
|
---|
508 | LogFlowFunc(("cFiles=%u, cchFiles=%zu, cbFiles=%zu, pszFiles=0x%p\n",
|
---|
509 | cFiles, cchFiles, cbFiles, pszFiles));
|
---|
510 |
|
---|
511 | m_pvData = pszFiles;
|
---|
512 | m_cbData = cbFiles;
|
---|
513 | }
|
---|
514 | else
|
---|
515 | {
|
---|
516 | RTStrFree(pszFiles);
|
---|
517 | pszFiles = NULL;
|
---|
518 | }
|
---|
519 |
|
---|
520 | LogFlowFunc(("Building CF_HDROP list rc=%Rrc, cFiles=%RU16, cchFiles=%RU32\n",
|
---|
521 | rc, cFiles, cchFiles));
|
---|
522 | break;
|
---|
523 | }
|
---|
524 |
|
---|
525 | default:
|
---|
526 | /* Note: Should not happen due to the checks done in DragEnter(). */
|
---|
527 | AssertMsgFailed(("Format of type %RI16 (%s) not supported\n",
|
---|
528 | m_FormatEtc.cfFormat, VBoxDnDDataObject::ClipboardFormatToString(m_FormatEtc.cfFormat)));
|
---|
529 | hr = DV_E_CLIPFORMAT; /* Set special hr for OLE. */
|
---|
530 | break;
|
---|
531 | }
|
---|
532 |
|
---|
533 | /*
|
---|
534 | * Third stage: Unlock + release access to the storage medium again.
|
---|
535 | */
|
---|
536 | switch (m_FormatEtc.tymed)
|
---|
537 | {
|
---|
538 | case TYMED_HGLOBAL:
|
---|
539 | GlobalUnlock(stgMed.hGlobal);
|
---|
540 | break;
|
---|
541 |
|
---|
542 | default:
|
---|
543 | AssertMsgFailed(("Really should not happen -- see init stage!\n"));
|
---|
544 | break;
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | /* Release storage medium again. */
|
---|
549 | ReleaseStgMedium(&stgMed);
|
---|
550 |
|
---|
551 | /* Signal waiters. */
|
---|
552 | m_rcDropped = rc;
|
---|
553 | RTSemEventSignal(m_EvtDrop);
|
---|
554 | }
|
---|
555 | }
|
---|
556 |
|
---|
557 | if (RT_SUCCESS(rc))
|
---|
558 | {
|
---|
559 | /* Note: pt is not used since we don't need to differentiate within our
|
---|
560 | * proxy window. */
|
---|
561 | *pdwEffect = VBoxDnDDropTarget::GetDropEffect(grfKeyState, *pdwEffect);
|
---|
562 | }
|
---|
563 | else
|
---|
564 | *pdwEffect = DROPEFFECT_NONE;
|
---|
565 |
|
---|
566 | if (m_pWndParent)
|
---|
567 | m_pWndParent->Hide();
|
---|
568 |
|
---|
569 | LogFlowFunc(("Returning with hr=%Rhrc (%Rrc), mFormatEtc.cfFormat=%RI16 (%s), *pdwEffect=%RI32\n",
|
---|
570 | hr, rc, m_FormatEtc.cfFormat, VBoxDnDDataObject::ClipboardFormatToString(m_FormatEtc.cfFormat),
|
---|
571 | *pdwEffect));
|
---|
572 |
|
---|
573 | return hr;
|
---|
574 | }
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * Static helper function to return a drop effect for a given key state and allowed effects.
|
---|
578 | *
|
---|
579 | * @returns Resolved drop effect.
|
---|
580 | * @param grfKeyState Key state to determine drop effect for.
|
---|
581 | * @param dwAllowedEffects Allowed drop effects to determine drop effect for.
|
---|
582 | */
|
---|
583 | /* static */
|
---|
584 | DWORD VBoxDnDDropTarget::GetDropEffect(DWORD grfKeyState, DWORD dwAllowedEffects)
|
---|
585 | {
|
---|
586 | DWORD dwEffect = DROPEFFECT_NONE;
|
---|
587 |
|
---|
588 | if(grfKeyState & MK_CONTROL)
|
---|
589 | dwEffect = dwAllowedEffects & DROPEFFECT_COPY;
|
---|
590 | else if(grfKeyState & MK_SHIFT)
|
---|
591 | dwEffect = dwAllowedEffects & DROPEFFECT_MOVE;
|
---|
592 |
|
---|
593 | /* If there still was no drop effect assigned, check for the handed-in
|
---|
594 | * allowed effects and assign one of them.
|
---|
595 | *
|
---|
596 | * Note: A move action has precendence over a copy action! */
|
---|
597 | if (dwEffect == DROPEFFECT_NONE)
|
---|
598 | {
|
---|
599 | if (dwAllowedEffects & DROPEFFECT_COPY)
|
---|
600 | dwEffect = DROPEFFECT_COPY;
|
---|
601 | if (dwAllowedEffects & DROPEFFECT_MOVE)
|
---|
602 | dwEffect = DROPEFFECT_MOVE;
|
---|
603 | }
|
---|
604 |
|
---|
605 | #ifdef DEBUG_andy
|
---|
606 | LogFlowFunc(("grfKeyState=0x%x, dwAllowedEffects=0x%x, dwEffect=0x%x\n",
|
---|
607 | grfKeyState, dwAllowedEffects, dwEffect));
|
---|
608 | #endif
|
---|
609 | return dwEffect;
|
---|
610 | }
|
---|
611 |
|
---|
612 | /**
|
---|
613 | * Resets a drop target object.
|
---|
614 | */
|
---|
615 | void VBoxDnDDropTarget::reset(void)
|
---|
616 | {
|
---|
617 | LogFlowFuncEnter();
|
---|
618 |
|
---|
619 | if (m_pvData)
|
---|
620 | {
|
---|
621 | RTMemFree(m_pvData);
|
---|
622 | m_pvData = NULL;
|
---|
623 | }
|
---|
624 |
|
---|
625 | m_cbData = 0;
|
---|
626 |
|
---|
627 | RT_ZERO(m_FormatEtc);
|
---|
628 | m_strFormat = "";
|
---|
629 | }
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * Returns the currently supported formats of a drop target.
|
---|
633 | *
|
---|
634 | * @returns Supported formats.
|
---|
635 | */
|
---|
636 | RTCString VBoxDnDDropTarget::Formats(void) const
|
---|
637 | {
|
---|
638 | return m_strFormat;
|
---|
639 | }
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * Waits for a drop event to happen.
|
---|
643 | *
|
---|
644 | * @returns VBox status code.
|
---|
645 | * @param msTimeout Timeout (in ms) to wait for drop event.
|
---|
646 | */
|
---|
647 | int VBoxDnDDropTarget::WaitForDrop(RTMSINTERVAL msTimeout)
|
---|
648 | {
|
---|
649 | LogFlowFunc(("msTimeout=%RU32\n", msTimeout));
|
---|
650 |
|
---|
651 | int rc = RTSemEventWait(m_EvtDrop, msTimeout);
|
---|
652 | if (RT_SUCCESS(rc))
|
---|
653 | rc = m_rcDropped;
|
---|
654 |
|
---|
655 | LogFlowFuncLeaveRC(rc);
|
---|
656 | return rc;
|
---|
657 | }
|
---|
658 |
|
---|