1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <shlobj.h>
#include "nsDataObjCollection.h"
#include "nsClipboard.h"
#include "IEnumFE.h"
#include <ole2.h>
// {25589C3E-1FAC-47b9-BF43-CAEA89B79533}
const IID IID_IDataObjCollection =
{0x25589c3e, 0x1fac, 0x47b9, {0xbf, 0x43, 0xca, 0xea, 0x89, 0xb7, 0x95, 0x33}};
/*
* Class nsDataObjCollection
*/
nsDataObjCollection::nsDataObjCollection()
: m_cRef(0)
{
}
nsDataObjCollection::~nsDataObjCollection()
{
mDataObjects.Clear();
}
// IUnknown interface methods - see iunknown.h for documentation
STDMETHODIMP nsDataObjCollection::QueryInterface(REFIID riid, void** ppv)
{
*ppv=nullptr;
if ( (IID_IUnknown == riid) || (IID_IDataObject == riid) ) {
*ppv = static_cast<IDataObject*>(this);
AddRef();
return NOERROR;
}
if ( IID_IDataObjCollection == riid ) {
*ppv = static_cast<nsIDataObjCollection*>(this);
AddRef();
return NOERROR;
}
//offer to operate asynchronously (required by nsDragService)
if (IID_IAsyncOperation == riid) {
*ppv = static_cast<IAsyncOperation*>(this);
AddRef();
return NOERROR;
}
return E_NOINTERFACE;
}
STDMETHODIMP_(ULONG) nsDataObjCollection::AddRef()
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) nsDataObjCollection::Release()
{
if (0 != --m_cRef)
return m_cRef;
delete this;
return 0;
}
// IDataObject methods
STDMETHODIMP nsDataObjCollection::GetData(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
{
static CLIPFORMAT fileDescriptorFlavorA =
::RegisterClipboardFormat(CFSTR_FILEDESCRIPTORA);
static CLIPFORMAT fileDescriptorFlavorW =
::RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW);
static CLIPFORMAT fileFlavor = ::RegisterClipboardFormat(CFSTR_FILECONTENTS);
switch (pFE->cfFormat) {
case CF_TEXT:
case CF_UNICODETEXT:
return GetText(pFE, pSTM);
case CF_HDROP:
return GetFile(pFE, pSTM);
default:
if (pFE->cfFormat == fileDescriptorFlavorA ||
pFE->cfFormat == fileDescriptorFlavorW) {
return GetFileDescriptors(pFE, pSTM);
}
if (pFE->cfFormat == fileFlavor) {
return GetFileContents(pFE, pSTM);
}
}
return GetFirstSupporting(pFE, pSTM);
}
STDMETHODIMP nsDataObjCollection::GetDataHere(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
{
return E_FAIL;
}
// Other objects querying to see if we support a particular format
STDMETHODIMP nsDataObjCollection::QueryGetData(LPFORMATETC pFE)
{
UINT format = nsClipboard::GetFormat(MULTI_MIME);
if (format == pFE->cfFormat) {
return S_OK;
}
for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
IDataObject * dataObj = mDataObjects.ElementAt(i);
if (S_OK == dataObj->QueryGetData(pFE)) {
return S_OK;
}
}
return DV_E_FORMATETC;
}
STDMETHODIMP nsDataObjCollection::SetData(LPFORMATETC pFE,
LPSTGMEDIUM pSTM,
BOOL fRelease)
{
// Set arbitrary data formats on the first object in the collection and let
// it handle the heavy lifting
if (mDataObjects.Length() == 0)
return E_FAIL;
return mDataObjects.ElementAt(0)->SetData(pFE, pSTM, fRelease);
}
// Registers a DataFlavor/FE pair
void nsDataObjCollection::AddDataFlavor(const char * aDataFlavor,
LPFORMATETC aFE)
{
// Add the FormatEtc to our list if it's not already there. We don't care
// about the internal aDataFlavor because nsDataObj handles that.
IEnumFORMATETC * ifEtc;
FORMATETC fEtc;
ULONG num;
if (S_OK != this->EnumFormatEtc(DATADIR_GET, &ifEtc))
return;
while (S_OK == ifEtc->Next(1, &fEtc, &num)) {
NS_ASSERTION(1 == num,
"Bit off more than we can chew in nsDataObjCollection::AddDataFlavor");
if (FormatsMatch(fEtc, *aFE)) {
ifEtc->Release();
return;
}
} // If we didn't find a matching format, add this one
ifEtc->Release();
m_enumFE->AddFormatEtc(aFE);
}
// We accept ownership of the nsDataObj which we free on destruction
void nsDataObjCollection::AddDataObject(IDataObject * aDataObj)
{
nsDataObj* dataObj = reinterpret_cast<nsDataObj*>(aDataObj);
mDataObjects.AppendElement(dataObj);
}
// Methods for getting data
HRESULT nsDataObjCollection::GetFile(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
{
STGMEDIUM workingmedium;
FORMATETC fe = *pFE;
HGLOBAL hGlobalMemory;
HRESULT hr;
// Make enough space for the header and the trailing null
uint32_t buffersize = sizeof(DROPFILES) + sizeof(char16_t);
uint32_t alloclen = 0;
char16_t* realbuffer;
nsAutoString filename;
hGlobalMemory = GlobalAlloc(GHND, buffersize);
for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
nsDataObj* dataObj = mDataObjects.ElementAt(i);
hr = dataObj->GetData(&fe, &workingmedium);
if (hr != S_OK) {
switch (hr) {
case DV_E_FORMATETC:
continue;
default:
return hr;
}
}
// Now we need to pull out the filename
char16_t* buffer = (char16_t*)GlobalLock(workingmedium.hGlobal);
if (buffer == nullptr)
return E_FAIL;
buffer += sizeof(DROPFILES)/sizeof(char16_t);
filename = buffer;
GlobalUnlock(workingmedium.hGlobal);
ReleaseStgMedium(&workingmedium);
// Now put the filename into our buffer
alloclen = (filename.Length() + 1) * sizeof(char16_t);
hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen, GHND);
if (hGlobalMemory == nullptr)
return E_FAIL;
realbuffer = (char16_t*)((char*)GlobalLock(hGlobalMemory) + buffersize);
if (!realbuffer)
return E_FAIL;
realbuffer--; // Overwrite the preceding null
memcpy(realbuffer, filename.get(), alloclen);
GlobalUnlock(hGlobalMemory);
buffersize += alloclen;
}
// We get the last null (on the double null terminator) for free since we used
// the zero memory flag when we allocated. All we need to do is fill the
// DROPFILES structure
DROPFILES* df = (DROPFILES*)GlobalLock(hGlobalMemory);
if (!df)
return E_FAIL;
df->pFiles = sizeof(DROPFILES); //Offset to start of file name string
df->fNC = 0;
df->pt.x = 0;
df->pt.y = 0;
df->fWide = TRUE; // utf-16 chars
GlobalUnlock(hGlobalMemory);
// Finally fill out the STGMEDIUM struct
pSTM->tymed = TYMED_HGLOBAL;
pSTM->pUnkForRelease = nullptr; // Caller gets to free the data
pSTM->hGlobal = hGlobalMemory;
return S_OK;
}
HRESULT nsDataObjCollection::GetText(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
{
STGMEDIUM workingmedium;
FORMATETC fe = *pFE;
HGLOBAL hGlobalMemory;
HRESULT hr;
uint32_t buffersize = 1;
uint32_t alloclen = 0;
hGlobalMemory = GlobalAlloc(GHND, buffersize);
if (pFE->cfFormat == CF_TEXT) {
nsAutoCString text;
for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
nsDataObj* dataObj = mDataObjects.ElementAt(i);
hr = dataObj->GetData(&fe, &workingmedium);
if (hr != S_OK) {
switch (hr) {
case DV_E_FORMATETC:
continue;
default:
return hr;
}
}
// Now we need to pull out the text
char* buffer = (char*)GlobalLock(workingmedium.hGlobal);
if (buffer == nullptr)
return E_FAIL;
text = buffer;
GlobalUnlock(workingmedium.hGlobal);
ReleaseStgMedium(&workingmedium);
// Now put the text into our buffer
alloclen = text.Length();
hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen,
GHND);
if (hGlobalMemory == nullptr)
return E_FAIL;
buffer = ((char*)GlobalLock(hGlobalMemory) + buffersize);
if (!buffer)
return E_FAIL;
buffer--; // Overwrite the preceding null
memcpy(buffer, text.get(), alloclen);
GlobalUnlock(hGlobalMemory);
buffersize += alloclen;
}
pSTM->tymed = TYMED_HGLOBAL;
pSTM->pUnkForRelease = nullptr; // Caller gets to free the data
pSTM->hGlobal = hGlobalMemory;
return S_OK;
}
if (pFE->cfFormat == CF_UNICODETEXT) {
buffersize = sizeof(char16_t);
nsAutoString text;
for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
nsDataObj* dataObj = mDataObjects.ElementAt(i);
hr = dataObj->GetData(&fe, &workingmedium);
if (hr != S_OK) {
switch (hr) {
case DV_E_FORMATETC:
continue;
default:
return hr;
}
}
// Now we need to pull out the text
char16_t* buffer = (char16_t*)GlobalLock(workingmedium.hGlobal);
if (buffer == nullptr)
return E_FAIL;
text = buffer;
GlobalUnlock(workingmedium.hGlobal);
ReleaseStgMedium(&workingmedium);
// Now put the text into our buffer
alloclen = text.Length() * sizeof(char16_t);
hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen,
GHND);
if (hGlobalMemory == nullptr)
return E_FAIL;
buffer = (char16_t*)((char*)GlobalLock(hGlobalMemory) + buffersize);
if (!buffer)
return E_FAIL;
buffer--; // Overwrite the preceding null
memcpy(buffer, text.get(), alloclen);
GlobalUnlock(hGlobalMemory);
buffersize += alloclen;
}
pSTM->tymed = TYMED_HGLOBAL;
pSTM->pUnkForRelease = nullptr; // Caller gets to free the data
pSTM->hGlobal = hGlobalMemory;
return S_OK;
}
return E_FAIL;
}
HRESULT nsDataObjCollection::GetFileDescriptors(LPFORMATETC pFE,
LPSTGMEDIUM pSTM)
{
STGMEDIUM workingmedium;
FORMATETC fe = *pFE;
HGLOBAL hGlobalMemory;
HRESULT hr;
uint32_t buffersize = sizeof(UINT);
uint32_t alloclen = sizeof(FILEDESCRIPTOR);
hGlobalMemory = GlobalAlloc(GHND, buffersize);
for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
nsDataObj* dataObj = mDataObjects.ElementAt(i);
hr = dataObj->GetData(&fe, &workingmedium);
if (hr != S_OK) {
switch (hr) {
case DV_E_FORMATETC:
continue;
default:
return hr;
}
}
// Now we need to pull out the filedescriptor
FILEDESCRIPTOR* buffer =
(FILEDESCRIPTOR*)((char*)GlobalLock(workingmedium.hGlobal) + sizeof(UINT));
if (buffer == nullptr)
return E_FAIL;
hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen, GHND);
if (hGlobalMemory == nullptr)
return E_FAIL;
FILEGROUPDESCRIPTOR* realbuffer =
(FILEGROUPDESCRIPTOR*)GlobalLock(hGlobalMemory);
if (!realbuffer)
return E_FAIL;
FILEDESCRIPTOR* copyloc = (FILEDESCRIPTOR*)((char*)realbuffer + buffersize);
memcpy(copyloc, buffer, alloclen);
realbuffer->cItems++;
GlobalUnlock(hGlobalMemory);
GlobalUnlock(workingmedium.hGlobal);
ReleaseStgMedium(&workingmedium);
buffersize += alloclen;
}
pSTM->tymed = TYMED_HGLOBAL;
pSTM->pUnkForRelease = nullptr; // Caller gets to free the data
pSTM->hGlobal = hGlobalMemory;
return S_OK;
}
HRESULT nsDataObjCollection::GetFileContents(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
{
ULONG num = 0;
ULONG numwanted = (pFE->lindex == -1) ? 0 : pFE->lindex;
FORMATETC fEtc = *pFE;
fEtc.lindex = -1; // We're lying to the data object so it thinks it's alone
// The key for this data type is to figure out which data object the index
// corresponds to and then just pass it along
for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
nsDataObj* dataObj = mDataObjects.ElementAt(i);
if (dataObj->QueryGetData(&fEtc) != S_OK)
continue;
if (num == numwanted)
return dataObj->GetData(pFE, pSTM);
num++;
}
return DV_E_LINDEX;
}
HRESULT nsDataObjCollection::GetFirstSupporting(LPFORMATETC pFE,
LPSTGMEDIUM pSTM)
{
// There is no way to pass more than one of this, so just find the first data
// object that supports it and pass it along
for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
if (mDataObjects.ElementAt(i)->QueryGetData(pFE) == S_OK)
return mDataObjects.ElementAt(i)->GetData(pFE, pSTM);
}
return DV_E_FORMATETC;
}
|