summaryrefslogtreecommitdiff
path: root/src/modules/srfile/fileexistsdlg.cpp
blob: 23bc8b0877081edbaa6c341b8e2b57b72d03a262 (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
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
/*

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 <shlobj.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "file.h"

static void SetControlToUnixTime(HWND hwndDlg, UINT idCtrl, time_t unixTime)
{
	LARGE_INTEGER liFiletime;
	FILETIME filetime;
	SYSTEMTIME st;
	char szTime[64],szDate[64],szOutput[128];

	liFiletime.QuadPart=(BIGI(11644473600)+(__int64)unixTime)*10000000;
	filetime.dwHighDateTime=liFiletime.HighPart;
	filetime.dwLowDateTime=liFiletime.LowPart;
	FileTimeToSystemTime(&filetime,&st);
	GetTimeFormatA(LOCALE_USER_DEFAULT,0,&st,NULL,szTime,SIZEOF(szTime));
	GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&st,NULL,szDate,SIZEOF(szDate));
	mir_snprintf(szOutput, SIZEOF(szOutput), "%s %s",szDate,szTime);
	SetDlgItemTextA(hwndDlg,idCtrl,szOutput);
}

#define C_CONTEXTMENU  0
#define C_PROPERTIES   1
// not defined in VC++ 6.0 SE
#ifndef CMF_EXTENDEDVERBS
#define CMF_EXTENDEDVERBS 0x00000100
#endif
static void DoAnnoyingShellCommand(HWND hwnd,const TCHAR *szFilename,int cmd,POINT *ptCursor)
{
	IShellFolder *pDesktopFolder;
	if (SHGetDesktopFolder(&pDesktopFolder) == NOERROR) {
		ITEMIDLIST *pCurrentIdl;
		#if defined( _UNICODE )
			WCHAR* wszFilename = ( LPWSTR )szFilename;
		#else
			WCHAR wszFilename[MAX_PATH];
			MultiByteToWideChar(CP_ACP,0,szFilename,-1,wszFilename,SIZEOF(wszFilename));
		#endif
		if (pDesktopFolder->ParseDisplayName(NULL,NULL,wszFilename,NULL,&pCurrentIdl,NULL) == NOERROR) {
			if (pCurrentIdl->mkid.cb) {
				ITEMIDLIST *pidl,*pidlNext,*pidlFilename;
				IShellFolder *pFileFolder;

				for (pidl=pCurrentIdl;;) {
					pidlNext=(ITEMIDLIST*)((PBYTE)pidl+pidl->mkid.cb);
					if (pidlNext->mkid.cb == 0) {
						pidlFilename = (ITEMIDLIST*)CoTaskMemAlloc(pidl->mkid.cb+sizeof(pidl->mkid.cb));
						CopyMemory(pidlFilename,pidl,pidl->mkid.cb+sizeof(pidl->mkid.cb));
						pidl->mkid.cb=0;
						break;
					}
					pidl=pidlNext;
				}
				if (pDesktopFolder->BindToObject(pCurrentIdl,NULL,IID_IShellFolder,(void**)&pFileFolder) == NOERROR) {
					IContextMenu *pContextMenu;
					if (pFileFolder->GetUIObjectOf(NULL,1,(LPCITEMIDLIST*)&pidlFilename,IID_IContextMenu,NULL,(void**)&pContextMenu) == NOERROR) {
						switch(cmd) {
							case C_PROPERTIES:
							{	CMINVOKECOMMANDINFO ici={0};
								ici.cbSize=sizeof(ici);
								ici.hwnd=hwnd;
								ici.lpVerb="properties";
								ici.nShow=SW_SHOW;
								pContextMenu->InvokeCommand(&ici);
								break;
							}
							case C_CONTEXTMENU:
							{	HMENU hMenu;
								hMenu=CreatePopupMenu();
								if (SUCCEEDED(pContextMenu->QueryContextMenu(hMenu,0,1000,65535,(GetKeyState(VK_SHIFT)&0x8000?CMF_EXTENDEDVERBS:0)|CMF_NORMAL))) {
									int cmd;
									cmd=TrackPopupMenu(hMenu,TPM_RETURNCMD,ptCursor->x,ptCursor->y,0,hwnd,NULL);
									if (cmd) {
										CMINVOKECOMMANDINFO ici={0};
										ici.cbSize=sizeof(ici);
										ici.hwnd=hwnd;
										ici.lpVerb=MAKEINTRESOURCEA(cmd-1000);
										ici.nShow=SW_SHOW;
										pContextMenu->InvokeCommand(&ici);
									}
								}
								DestroyMenu(hMenu);
								break;
							}
						}
						pContextMenu->Release();
					}
					pFileFolder->Release();
				}
				CoTaskMemFree(pidlFilename);
			}
			CoTaskMemFree(pCurrentIdl);
		}
		pDesktopFolder->Release();
	}
}

static WNDPROC pfnIconWindowProc;
static LRESULT CALLBACK IconCtrlSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PROTOFILETRANSFERSTATUS* pft = (PROTOFILETRANSFERSTATUS*)GetWindowLongPtr(GetParent(hwnd),GWLP_USERDATA);

	switch(msg) {
		case WM_LBUTTONDBLCLK:
			ShellExecute(hwnd,NULL,pft->tszCurrentFile,NULL,NULL,SW_SHOW);
			break;
		case WM_RBUTTONUP:
		{	POINT pt;
			pt.x=(short)LOWORD(lParam); pt.y=(short)HIWORD(lParam);
			ClientToScreen( hwnd, &pt );
			DoAnnoyingShellCommand( hwnd, pft->tszCurrentFile, C_CONTEXTMENU, &pt );
			return 0;
		}
	}
	return CallWindowProc(pfnIconWindowProc,hwnd,msg,wParam,lParam);
}

struct loadiconsstartinfo {
	HWND hwndDlg;
	TCHAR *szFilename;
};
void __cdecl LoadIconsAndTypesThread(void* param)
{
	loadiconsstartinfo *info = ( loadiconsstartinfo* )param;
	SHFILEINFO fileInfo;

	if ( SHGetFileInfo( info->szFilename, 0, &fileInfo, sizeof(fileInfo),SHGFI_TYPENAME|SHGFI_ICON|SHGFI_LARGEICON)) {
		TCHAR *pszExtension,*pszFilename;
		TCHAR szExtension[64];
		TCHAR szIconFile[MAX_PATH];

		pszFilename = _tcsrchr(info->szFilename,'\\');
		if ( pszFilename == NULL )
			pszFilename = info->szFilename;

		pszExtension = _tcsrchr( pszFilename, '.' );
		if ( pszExtension )
			lstrcpyn( szExtension, pszExtension+1, SIZEOF( szExtension ));
		else {
			pszExtension = _T(".");
			szExtension[0]='\0';
		}
		CharUpper(szExtension);
		if ( fileInfo.szTypeName[0] == '\0' )
			mir_sntprintf( fileInfo.szTypeName, SIZEOF(fileInfo.szTypeName), TranslateT("%s File"),szExtension);
		SetDlgItemText(info->hwndDlg,IDC_EXISTINGTYPE,fileInfo.szTypeName);
		SetDlgItemText(info->hwndDlg,IDC_NEWTYPE,fileInfo.szTypeName);
		SendDlgItemMessage(info->hwndDlg,IDC_EXISTINGICON,STM_SETICON,(WPARAM)fileInfo.hIcon,0);
		szIconFile[0]='\0';
		if ( !lstrcmp( szExtension, _T("EXE"))) {
			SRFile_GetRegValue(HKEY_LOCAL_MACHINE,_T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Icons"),_T("2"),szIconFile,SIZEOF(szIconFile));
		}
		else {
			TCHAR szTypeName[MAX_PATH];
			if (SRFile_GetRegValue(HKEY_CLASSES_ROOT,pszExtension,NULL,szTypeName,SIZEOF(szTypeName))) {
				lstrcat(szTypeName,_T("\\DefaultIcon"));
				if (SRFile_GetRegValue(HKEY_CLASSES_ROOT,szTypeName,NULL,szIconFile,SIZEOF(szIconFile))) {
					if ( _tcsstr( szIconFile, _T("%1")))
						SRFile_GetRegValue(HKEY_LOCAL_MACHINE,_T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Icons"),_T("0"),szIconFile,SIZEOF(szIconFile));
					else szIconFile[0]='\0';
		}	}	}

		if ( szIconFile[0]) {
			int iconIndex;
			HICON hIcon;
			TCHAR *pszComma = _tcsrchr(szIconFile,',');
			if ( pszComma == NULL )
				iconIndex=0;
			else {
				iconIndex = _ttoi(pszComma+1); *pszComma='\0';
			}
			hIcon = ExtractIcon( hMirandaInst, szIconFile, iconIndex );
			if ( hIcon )
				fileInfo.hIcon = hIcon;
		}
		SendDlgItemMessage(info->hwndDlg,IDC_NEWICON,STM_SETICON,(WPARAM)fileInfo.hIcon,0);
	}
	mir_free(info->szFilename);
	mir_free(info);
}

INT_PTR CALLBACK DlgProcFileExists(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PROTOFILETRANSFERSTATUS *fts;

	fts=(PROTOFILETRANSFERSTATUS*)GetWindowLongPtr(hwndDlg,GWLP_USERDATA);
	switch(msg) {
	case WM_INITDIALOG:
	{
		TCHAR szSize[64];
		struct _stati64 statbuf;
		HWND hwndFocus;
		struct TDlgProcFileExistsParam *dat = (struct TDlgProcFileExistsParam *)lParam;

		SetPropA(hwndDlg,"Miranda.Preshutdown",HookEventMessage(ME_SYSTEM_PRESHUTDOWN,hwndDlg,M_PRESHUTDOWN));
		SetPropA(hwndDlg,"Miranda.ParentWnd",dat->hwndParent);

		TranslateDialogDefault(hwndDlg);
		fts=(PROTOFILETRANSFERSTATUS*)mir_alloc(sizeof(PROTOFILETRANSFERSTATUS));
		CopyProtoFileTransferStatus(fts,dat->fts);
		SetWindowLongPtr(hwndDlg,GWLP_USERDATA,(LONG_PTR)fts);
		SetDlgItemText(hwndDlg,IDC_FILENAME,fts->tszCurrentFile);
		SetControlToUnixTime(hwndDlg,IDC_NEWDATE,fts->currentFileTime);
		GetSensiblyFormattedSize(fts->currentFileSize,szSize,SIZEOF(szSize),0,1,NULL);
		SetDlgItemText(hwndDlg,IDC_NEWSIZE,szSize);

		pfnIconWindowProc=(WNDPROC)SetWindowLongPtr(GetDlgItem(hwndDlg,IDC_EXISTINGICON),GWLP_WNDPROC,(LONG_PTR)IconCtrlSubclassProc);

		hwndFocus=GetDlgItem(hwndDlg,IDC_RESUME);
		if ( _tstati64(fts->tszCurrentFile,&statbuf) == 0) {
			SetControlToUnixTime(hwndDlg,IDC_EXISTINGDATE,statbuf.st_mtime);
			GetSensiblyFormattedSize(statbuf.st_size,szSize,SIZEOF(szSize),0,1,NULL);
			SetDlgItemText(hwndDlg,IDC_EXISTINGSIZE,szSize);
			if (statbuf.st_size>(int)fts->currentFileSize) {
				EnableWindow(GetDlgItem(hwndDlg,IDC_RESUME),FALSE);
				hwndFocus=GetDlgItem(hwndDlg,IDC_OVERWRITE);
		}	}

		loadiconsstartinfo *lisi = ( loadiconsstartinfo* )mir_alloc(sizeof(loadiconsstartinfo));
		lisi->hwndDlg=hwndDlg;
		lisi->szFilename = mir_tstrdup(fts->tszCurrentFile);
		//can be a little slow, so why not?
		forkthread(LoadIconsAndTypesThread,0,lisi);			
		SetFocus(hwndFocus);
		SetWindowLongPtr(hwndFocus,GWL_STYLE,GetWindowLongPtr(hwndFocus,GWL_STYLE)|BS_DEFPUSHBUTTON);
		return FALSE;
	}
	case WM_COMMAND:
	{	
		PROTOFILERESUME pfr={0};
		switch(LOWORD(wParam)) {
		case IDC_OPENFILE:
			ShellExecute( hwndDlg, NULL, fts->tszCurrentFile, NULL, NULL, SW_SHOW );
			return FALSE;

		case IDC_OPENFOLDER:
		{	
			TCHAR szFile[MAX_PATH];
			lstrcpyn( szFile, fts->tszCurrentFile, SIZEOF(szFile));
			TCHAR* pszLastBackslash = _tcsrchr( szFile, '\\' );
			if ( pszLastBackslash )
				*pszLastBackslash = '\0';
			ShellExecute(hwndDlg,NULL,szFile,NULL,NULL,SW_SHOW);
			return FALSE;
		}
		case IDC_PROPERTIES:
			DoAnnoyingShellCommand(hwndDlg,fts->tszCurrentFile,C_PROPERTIES,NULL);
			return FALSE;
		case IDC_RESUME:
			pfr.action=FILERESUME_RESUME;
			break;
		case IDC_RESUMEALL:
			pfr.action=FILERESUME_RESUMEALL;
			break;
		case IDC_OVERWRITE:
			pfr.action=FILERESUME_OVERWRITE;
			break;
		case IDC_OVERWRITEALL:
			pfr.action=FILERESUME_OVERWRITEALL;
			break;

		case IDC_AUTORENAME:
			pfr.action = FILERESUME_RENAMEALL;
			break;

		case IDC_SAVEAS:
		{	
			OPENFILENAME ofn={0};
			TCHAR filter[512],*pfilter;
			TCHAR str[MAX_PATH];

			lstrcpyn( str, fts->tszCurrentFile, SIZEOF(str));
			ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
			ofn.hwndOwner = hwndDlg;
			ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
			_tcscpy( filter, TranslateT("All Files"));
			_tcscat( filter, _T(" (*)"));
			pfilter = filter + _tcslen(filter) + 1;
			_tcscpy( pfilter, _T("*"));
			pfilter = pfilter + _tcslen(pfilter) + 1;
			*pfilter='\0';
			ofn.lpstrFilter = filter;
			ofn.lpstrFile = str;
			ofn.nMaxFile = SIZEOF(str);
			ofn.nMaxFileTitle = MAX_PATH;
			if (!GetSaveFileName(&ofn))
				return FALSE;

			pfr.szFilename = mir_tstrdup(str);
			pfr.action = FILERESUME_RENAME;
			break;
		}
		case IDC_SKIP:
			pfr.action=FILERESUME_SKIP;
			break;
		case IDCANCEL:
			pfr.action=FILERESUME_CANCEL;
			break;
		default:
			return FALSE;
		}
		{	PROTOFILERESUME *pfrCopy;
			pfrCopy=(PROTOFILERESUME*)mir_alloc(sizeof(pfr));
			CopyMemory(pfrCopy,&pfr,sizeof(pfr));
			PostMessage((HWND)GetPropA(hwndDlg,"Miranda.ParentWnd"),M_FILEEXISTSDLGREPLY,(WPARAM)mir_tstrdup(fts->tszCurrentFile),(LPARAM)pfrCopy);
			DestroyWindow(hwndDlg);
		}
		break;
	}

	case WM_CLOSE:
		PostMessage(hwndDlg,WM_COMMAND,MAKEWPARAM(IDCANCEL,BN_CLICKED),(LPARAM)GetDlgItem(hwndDlg,IDCANCEL));
		break;

	case M_PRESHUTDOWN:
		PostMessage(hwndDlg,WM_CLOSE,0,0);
		break;

	case WM_DESTROY:
		UnhookEvent(GetPropA(hwndDlg,"Miranda.Preshutdown")); // GetProp() will return NULL if it couldnt find anything
		RemovePropA(hwndDlg,"Miranda.Preshutdown");
		RemovePropA(hwndDlg,"Miranda.ParentWnd");
		DestroyIcon((HICON)SendDlgItemMessage(hwndDlg,IDC_EXISTINGICON,STM_GETICON,0,0));
		DestroyIcon((HICON)SendDlgItemMessage(hwndDlg,IDC_NEWICON,STM_GETICON,0,0));
		FreeProtoFileTransferStatus(fts);
		mir_free(fts);
		break;
	}
	return FALSE;
}