summaryrefslogtreecommitdiff
path: root/src/modules/clist/clcfiledrop.cpp
blob: c33bc2333414a21ebe9c11db440e95ac431f0fd8 (plain)
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
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2009 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "..\..\core\commonheaders.h"
#include "clc.h"
#include <shlobj.h>

struct CDropTarget : IDropTarget
{
	LONG refCount;
	IDropTargetHelper *pDropTargetHelper;

	ULONG STDMETHODCALLTYPE AddRef(void); 
	ULONG STDMETHODCALLTYPE Release(void);
	HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject);

	HRESULT STDMETHODCALLTYPE DragEnter(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect);
	HRESULT STDMETHODCALLTYPE DragOver(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect);
	HRESULT STDMETHODCALLTYPE DragLeave(void);
	HRESULT STDMETHODCALLTYPE Drop(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect);
}
static dropTarget;

static HWND hwndCurrentDrag = NULL;
static int originalSelection;

HRESULT CDropTarget::QueryInterface(REFIID riid, LPVOID * ppvObj)
{
	if (riid == IID_IDropTarget) {
		*ppvObj = this;
		AddRef();
		return S_OK;
	}
	*ppvObj = NULL;
	return E_NOINTERFACE;
}

ULONG CDropTarget::AddRef(void)
{
	return InterlockedIncrement(&refCount);
}

ULONG CDropTarget::Release(void)
{
	if (refCount == 1) {
		if (pDropTargetHelper)
			pDropTargetHelper->Release();
	}
	return InterlockedDecrement(&refCount);
}

static HANDLE HContactFromPoint(HWND hwnd, struct ClcData *dat, int x, int y, int *hitLine)
{
	DWORD hitFlags;
	ClcContact *contact;
	int hit = cli.pfnHitTest(hwnd, dat, x, y, &contact, NULL, &hitFlags);
	if (hit == -1 || !(hitFlags & (CLCHT_ONITEMLABEL | CLCHT_ONITEMICON)) || contact->type != CLCIT_CONTACT)
		return NULL;

	char *szProto = GetContactProto(contact->hContact);
	if (szProto == NULL)
		return NULL;

	DWORD protoCaps = CallProtoServiceInt(NULL,szProto, PS_GETCAPS, PFLAGNUM_1, 0);
	if ( !(protoCaps & PF1_FILESEND))
		return NULL;
	if (ID_STATUS_OFFLINE == db_get_w(contact->hContact, szProto, "Status", ID_STATUS_OFFLINE))
		return NULL;
	if (hitLine)
		*hitLine = hit;
	return contact->hContact;
}

HRESULT CDropTarget::DragOver(DWORD /*grfKeyState*/, POINTL pt, DWORD * pdwEffect)
{
	POINT shortPt;
	struct ClcData *dat;
	RECT clRect;
	int hit;
	HANDLE hContact;

	if (pDropTargetHelper && hwndCurrentDrag)
		pDropTargetHelper->DragOver((POINT*)&pt, *pdwEffect);

	*pdwEffect = 0;
	if (hwndCurrentDrag == NULL) {
		*pdwEffect = DROPEFFECT_NONE;
		return S_OK;
	}
	CallService(MS_CLIST_PAUSEAUTOHIDE, 0, 0);
	dat = (struct ClcData *) GetWindowLongPtr(hwndCurrentDrag, 0);
	shortPt.x = pt.x;
	shortPt.y = pt.y;
	ScreenToClient(hwndCurrentDrag, &shortPt);
	GetClientRect(hwndCurrentDrag, &clRect);

	if (shortPt.y < dat->dragAutoScrollHeight || shortPt.y >= clRect.bottom - dat->dragAutoScrollHeight) {
		*pdwEffect |= DROPEFFECT_SCROLL;
		cli.pfnScrollTo(hwndCurrentDrag, dat, dat->yScroll + (shortPt.y < dat->dragAutoScrollHeight ? -1 : 1) * dat->rowHeight * 2, 0);
	}
	hContact = HContactFromPoint(hwndCurrentDrag, dat, shortPt.x, shortPt.y, &hit);
	if (hContact == NULL) {
		hit = -1;
		*pdwEffect |= DROPEFFECT_NONE;
	}
	else
		*pdwEffect |= DROPEFFECT_COPY;

	if (dat->selection != hit) {
		dat->selection = hit;
		cli.pfnInvalidateRect(hwndCurrentDrag, NULL, FALSE);
		if (pDropTargetHelper) pDropTargetHelper->Show(FALSE);
		UpdateWindow(hwndCurrentDrag);
		if (pDropTargetHelper) pDropTargetHelper->Show(TRUE);
	}

	return S_OK;
}

HRESULT CDropTarget::DragEnter(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
{
	HWND hwnd;
	TCHAR szWindowClass[64];
	POINT shortPt;

	shortPt.x = pt.x;
	shortPt.y = pt.y;
	hwnd = WindowFromPoint(shortPt);
	GetClassName(hwnd, szWindowClass, SIZEOF(szWindowClass));
	if ( !lstrcmp(szWindowClass, CLISTCONTROL_CLASS)) {
		struct ClcData *dat;
		hwndCurrentDrag = hwnd;
		dat = (struct ClcData *) GetWindowLongPtr(hwndCurrentDrag, 0);
		originalSelection = dat->selection;
		dat->showSelAlways = 1;
	}
	if (pDropTargetHelper && hwndCurrentDrag)
		pDropTargetHelper->DragEnter(hwndCurrentDrag, pDataObj, (POINT*)&pt, *pdwEffect);
	return DragOver(grfKeyState, pt, pdwEffect);
}

HRESULT CDropTarget::DragLeave(void)
{
	if (hwndCurrentDrag) {
		struct ClcData *dat;
		if (pDropTargetHelper)
			pDropTargetHelper->DragLeave();
		dat = (struct ClcData *) GetWindowLongPtr(hwndCurrentDrag, 0);
		dat->showSelAlways = 0;
		dat->selection = originalSelection;
		cli.pfnInvalidateRect(hwndCurrentDrag, NULL, FALSE);
	}
	hwndCurrentDrag = NULL;
	return S_OK;
}

static void AddToFileList(TCHAR ***pppFiles, int *totalCount, const TCHAR *szFilename)
{
	*pppFiles = (TCHAR **) mir_realloc(*pppFiles, (++*totalCount + 1) * sizeof(TCHAR *));
	(*pppFiles)[*totalCount] = NULL;
	(*pppFiles)[*totalCount - 1] = mir_tstrdup(szFilename);
	if (GetFileAttributes(szFilename) & FILE_ATTRIBUTE_DIRECTORY) {
		WIN32_FIND_DATA fd;
		HANDLE hFind;
		TCHAR szPath[MAX_PATH];
		lstrcpy(szPath, szFilename);
		lstrcat(szPath, _T("\\*"));
		if (hFind = FindFirstFile(szPath, &fd)) {
			do {
				if ( !lstrcmp(fd.cFileName, _T(".")) || !lstrcmp(fd.cFileName, _T("..")))
					continue;
				lstrcpy(szPath, szFilename);
				lstrcat(szPath, _T("\\"));
				lstrcat(szPath, fd.cFileName);
				AddToFileList(pppFiles, totalCount, szPath);
			} while (FindNextFile(hFind, &fd));
			FindClose(hFind);
		}
	}
}

HRESULT CDropTarget::Drop(IDataObject * pDataObj, DWORD /*fKeyState*/, POINTL pt, DWORD * pdwEffect)
{
	FORMATETC fe = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
	STGMEDIUM stg;
	HDROP hDrop;
	POINT shortPt;
	struct ClcData *dat;
	HANDLE hContact;

	if (pDropTargetHelper && hwndCurrentDrag)
		pDropTargetHelper->Drop(pDataObj, (POINT*)&pt, *pdwEffect);

	*pdwEffect = DROPEFFECT_NONE;
	if (hwndCurrentDrag == NULL || S_OK != pDataObj->GetData(&fe, &stg))
		return S_OK;
	hDrop = (HDROP) stg.hGlobal;
	dat = (struct ClcData *) GetWindowLongPtr(hwndCurrentDrag, 0);

	shortPt.x = pt.x;
	shortPt.y = pt.y;
	ScreenToClient(hwndCurrentDrag, &shortPt);
	hContact = HContactFromPoint(hwndCurrentDrag, dat, shortPt.x, shortPt.y, NULL);
	if (hContact != NULL) {
		TCHAR **ppFiles = NULL;
		TCHAR szFilename[MAX_PATH];
		int fileCount, totalCount = 0, i;

		fileCount = DragQueryFile(hDrop, -1, NULL, 0);
		ppFiles = NULL;
		for (i=0; i < fileCount; i++) {
			DragQueryFile(hDrop, i, szFilename, SIZEOF(szFilename));
			AddToFileList(&ppFiles, &totalCount, szFilename);
		}

		if ( !CallService(MS_FILE_SENDSPECIFICFILEST, (WPARAM) hContact, (LPARAM) ppFiles))
			*pdwEffect = DROPEFFECT_COPY;

		for (i=0; ppFiles[i]; i++)
			mir_free(ppFiles[i]);
		mir_free(ppFiles);
	}

	if (stg.pUnkForRelease)
		stg.pUnkForRelease->Release();
	else
		GlobalFree(stg.hGlobal);

	DragLeave();
	return S_OK;
}

static VOID CALLBACK CreateDropTargetHelperTimerProc(HWND hwnd, UINT, UINT_PTR idEvent, DWORD)
{
	KillTimer(hwnd, idEvent);
	//This is a ludicrously slow function (~200ms) so we delay load it a bit.
	if (S_OK != CoCreateInstance(CLSID_DragDropHelper, NULL, CLSCTX_INPROC_SERVER, 
		IID_IDropTargetHelper, (LPVOID*)&dropTarget.pDropTargetHelper))
		dropTarget.pDropTargetHelper = NULL;
}

void InitFileDropping(void)
{
	// Disabled as this function loads tons of dlls for no apparenet reason
	// we will se what the reaction will be
//	SetTimer(NULL, 1, 1000, CreateDropTargetHelperTimerProc);
}

void fnRegisterFileDropping(HWND hwnd)
{
	RegisterDragDrop(hwnd, (IDropTarget *) & dropTarget);
}

void fnUnregisterFileDropping(HWND hwnd)
{
	RevokeDragDrop(hwnd);
}