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
|
#include "stdhdr.h"
static void ProcessDroppedItems ( char **ppDroppedItems, int nCount, char **ppFiles );
static int CountDroppedFiles ( char **ppDroppedItems, int nCount );
static BOOL OnDropFiles ( HDROP hDrop, ThumbInfo *pThumb );
HRESULT STDMETHODCALLTYPE CDropTarget::QueryInterface(REFIID riid,LPVOID *ppvObj)
{
if ( IsEqualIID( riid, IID_IDropTarget ) )
{
*ppvObj = this;
this->AddRef();
return S_OK;
}
*ppvObj=NULL;
return ( E_NOINTERFACE );
}
ULONG STDMETHODCALLTYPE CDropTarget::AddRef( )
{
return ++this->refCount;
}
ULONG STDMETHODCALLTYPE CDropTarget::Release( )
{
int res = --this->refCount;
if (!res) delete this;
return res;
}
HRESULT STDMETHODCALLTYPE CDropTarget::DragOver( DWORD fKeyState, POINTL pt, DWORD *pdwEffect )
{
*pdwEffect = 0;
if( hwndCurDrag == NULL )
{
*pdwEffect = DROPEFFECT_NONE;
}
else
{
*pdwEffect |= DROPEFFECT_COPY;
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE CDropTarget::DragEnter( IDataObject *pData, DWORD fKeyState, POINTL pt, DWORD *pdwEffect)
{
HWND hwnd = NULL;
POINT shortPt;
FORMATETC feFile = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
FORMATETC feText = { CF_TEXT, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
ThumbInfo *pThumb;
if ( S_OK == pData->QueryGetData( &feFile ) ||
S_OK == pData->QueryGetData( &feText ) )
{
shortPt.x = pt.x;
shortPt.y = pt.y;
hwnd = WindowFromPoint( shortPt );
if ( pThumb = thumbList.FindThumb( hwnd ) )
{
hwndCurDrag = hwnd;
pThumb->ThumbSelect( TRUE );
}
}
return DragOver( fKeyState, pt, pdwEffect);
}
HRESULT STDMETHODCALLTYPE CDropTarget::DragLeave( )
{
ThumbInfo *pThumb = thumbList.FindThumb( hwndCurDrag );
if ( NULL != pThumb )
{
pThumb->ThumbDeselect( TRUE );
}
hwndCurDrag = NULL;
return S_OK;
}
HRESULT STDMETHODCALLTYPE CDropTarget::Drop( IDataObject *pData,DWORD fKeyState,POINTL pt,DWORD *pdwEffect)
{
FORMATETC fe = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg;
HDROP hDrop = NULL;
ThumbInfo *pThumb = NULL;
char *pText = NULL;
BOOL bFormatText = FALSE;
*pdwEffect = DROPEFFECT_NONE;
if( hwndCurDrag == NULL ) return( S_OK );
pThumb = (ThumbInfo*)GetWindowLong( hwndCurDrag, GWL_USERDATA );
if ( pThumb == NULL ) return( S_OK );
if ( S_OK != pData->GetData( &fe,&stg ) )
{
fe.cfFormat = CF_TEXT;
if ( S_OK != pData->GetData( &fe,&stg ) )
{
return( S_OK );
}
else
{
bFormatText = TRUE;
}
}
if ( !bFormatText )
{
hDrop = (HDROP)stg.hGlobal;
if ( hDrop != NULL )
{
OnDropFiles( hDrop, pThumb );
}
}
else
{
pText = (char*)GlobalLock( stg.hGlobal );
if ( pText != NULL )
{
SendMsgDialog( hwndCurDrag, pText );
GlobalUnlock( stg.hGlobal );
}
}
if( stg.pUnkForRelease != NULL )
{
stg.pUnkForRelease->Release( );
}
else
{
GlobalFree( stg.hGlobal );
}
DragLeave( );
return S_OK;
}
///////////////////////////////////////////////////////
// Send files processing
BOOL OnDropFiles( HDROP hDrop, ThumbInfo *pThumb )
{
BOOL bSuccess = FALSE;
UINT nFilesCount = 0;
UINT iItem = 0;
char **ppFiles = NULL;
char **ppDroppedItems = NULL;
UINT nDroppedItemsCount = 0;
char szFilename[ MAX_PATH ];
nDroppedItemsCount = DragQueryFile( hDrop, 0xFFFFFFFF, NULL, 0 );
ppDroppedItems = ( char** )malloc( sizeof(char*)*( nDroppedItemsCount + 1 ) );
if ( ppDroppedItems == NULL )
{
return( FALSE );
}
ppDroppedItems[ nDroppedItemsCount ] = NULL;
for( iItem = 0; iItem < nDroppedItemsCount; ++iItem )
{
DragQueryFileA( hDrop, iItem, szFilename, sizeof( szFilename ) );
ppDroppedItems[ iItem ] = _strdup( szFilename );
}
nFilesCount = CountDroppedFiles( ppDroppedItems, nDroppedItemsCount );
ppFiles = ( char** )malloc( sizeof( char *)* ( nFilesCount+1 ) );
if ( ppFiles == NULL )
{
return( FALSE );
}
ppFiles[ nFilesCount] = NULL;
ProcessDroppedItems( ppDroppedItems, nDroppedItemsCount, ppFiles );
bSuccess = (BOOL)CallService( MS_CLIST_CONTACTFILESDROPPED, (WPARAM)pThumb->hContact, (LPARAM)ppFiles );
// Cleanup
for( iItem = 0; ppDroppedItems[ iItem ]; ++iItem )
{
free( ppDroppedItems[ iItem ] );
}
free( ppDroppedItems );
for( iItem = 0; iItem < nFilesCount ; ++iItem )
{
free( ppFiles[ iItem ] );
}
free( ppFiles );
return( bSuccess );
}
static int CountFiles( char *szItem )
{
int nCount = 0;
WIN32_FIND_DATAA fd;
HANDLE hFind = FindFirstFileA( szItem, &fd );
if ( hFind != INVALID_HANDLE_VALUE )
{
do
{
if ( fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
{
// Skip parent directories
if ( ( 0 != strcmp( fd.cFileName, "." ) ) &&
( 0 != strcmp( fd.cFileName, ".." ) ) )
{
char szDirName[ MAX_PATH ];
strncpy( szDirName, szItem, MAX_PATH - 1 );
if ( NULL != strstr( szItem, "*.*" ) )
{
sprintf( szDirName + strlen( szDirName ) - 3, "%s\0", fd.cFileName );
}
++nCount;
strcat( szDirName, "\\*.*" );
nCount += CountFiles( szDirName );
}
}
else
{
++nCount;
}
}
while( FALSE != FindNextFileA( hFind, &fd ) );
}
return( nCount );
}
static void SaveFiles( char *szItem, char **ppFiles, int *pnCount )
{
WIN32_FIND_DATAA fd;
HANDLE hFind = FindFirstFileA( szItem, &fd );
if ( hFind != INVALID_HANDLE_VALUE )
{
do
{
if ( fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
{
// Skip parent directories
if ( ( 0 != strcmp( fd.cFileName, "." ) ) &&
( 0 != strcmp( fd.cFileName, ".." ) ) )
{
char szDirName[ MAX_PATH ];
strncpy( szDirName, szItem, MAX_PATH - 1 );
if ( NULL != strstr( szItem, "*.*" ) )
{
sprintf( szDirName + strlen( szDirName ) - 3, "%s\0", fd.cFileName );
}
ppFiles[ *pnCount ] = _strdup( szDirName );
++( *pnCount );
strcat( szDirName, "\\*.*" );
SaveFiles( szDirName, ppFiles, pnCount );
}
}
else
{
int nSize = sizeof(char) * ( strlen( szItem ) + strlen( fd.cFileName ) + sizeof( char ) );
char *szFile = (char*) malloc( nSize ) ;
strncpy( szFile, szItem, nSize - 1 );
if ( NULL != strstr( szFile, "*.*" ) )
{
szFile[ strlen( szFile ) - 3 ] = '\0';
strncat( szFile, fd.cFileName, MAX_PATH - 1 );
}
ppFiles[ *pnCount ] = szFile;
++( *pnCount );
}
}
while( FALSE != FindNextFileA( hFind, &fd ) );
}
}
static void ProcessDroppedItems( char **ppDroppedItems, int nCount, char **ppFiles )
{
int i;
int fileCount = 0;
for( i = 0; i < nCount; ++i )
{
SaveFiles( ppDroppedItems[ i ], ppFiles, &fileCount );
}
}
static int CountDroppedFiles( char **ppDroppedItems, int nCount )
{
int fileCount = 0;
int i;
for( i = 0; i < nCount; ++i )
{
fileCount += CountFiles( ppDroppedItems[ i ] );
}
return( fileCount );
}
///////////////////////////////////////////////////////////////////////////////
// Init/destroy
void InitFileDropping()
{
OleInitialize( NULL );
}
void FreeFileDropping(void)
{
OleUninitialize();
}
void RegisterFileDropping( HWND hwnd, CDropTarget* pdropTarget )
{
RegisterDragDrop( hwnd, (IDropTarget*)pdropTarget );
}
void UnregisterFileDropping( HWND hwnd )
{
RevokeDragDrop( hwnd );
}
|