-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenRemoteDialog.cpp
443 lines (359 loc) · 14.2 KB
/
OpenRemoteDialog.cpp
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// OpenRemoteDialog.cpp : implementation file
//
#include "stdafx.h"
#include "cedtHeader.h"
#include "RemoteFile.h"
#include "FtpSettingsDialog.h"
#include "FtpPasswordDialog.h"
#include "OpenRemoteDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COpenRemoteDialog dialog
COpenRemoteDialog::COpenRemoteDialog(BOOL bOpenFileDialog, LPCTSTR lpszFileName, LPCTSTR lpszFilter, CWnd* pParent /*=NULL*/)
: CDialog(COpenRemoteDialog::IDD, pParent)
{
m_bOpenFileDialog = bOpenFileDialog;
if( lpszFileName ) m_szFileName = lpszFileName;
if( lpszFilter ) m_szFileFilter = lpszFilter;
m_nAccountCount = 0;
m_pFtpAccounts = NULL;
m_nCurrentFilterIndex = 0;
m_nCurrentFtpAccount = 0;
//{{AFX_DATA_INIT(COpenRemoteDialog)
//}}AFX_DATA_INIT
}
COpenRemoteDialog::~COpenRemoteDialog()
{
delete [] m_pFtpAccounts;
m_pFtpAccounts = NULL;
}
void COpenRemoteDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COpenRemoteDialog)
DDX_Control(pDX, IDC_FTP_SETTINGS, m_btnFtpSettings);
DDX_Control(pDX, IDOK, m_btnOK);
DDX_Control(pDX, IDC_FILE_TYPE, m_cmbFileType);
DDX_Control(pDX, IDC_FILE_NAME, m_edtFileName);
DDX_Control(pDX, IDC_PARENT_DIRECTORY, m_btnParentDirectory);
DDX_Control(pDX, IDC_CREATE_DIRECTORY, m_btnCreateDirectory);
DDX_Control(pDX, IDC_DIRECTORY, m_edtDirectory);
DDX_Control(pDX, IDC_FILE_LIST, m_lstFiles);
DDX_Control(pDX, IDC_FTP_ACCOUNT, m_cmbFtpAccount);
//}}AFX_DATA_MAP
}
void COpenRemoteDialog::SetFtpAccounts(INT nCount, CFtpAccount * pFtpAccounts)
{
m_nAccountCount = nCount;
delete [] m_pFtpAccounts;
m_pFtpAccounts = new CFtpAccount[nCount];
for( INT i = 0; i < nCount; i++ ) {
m_pFtpAccounts[i].CopyContents( pFtpAccounts[i] );
}
}
void COpenRemoteDialog::GetFtpAccounts(INT nCount, CFtpAccount * pFtpAccounts)
{
if( nCount > m_nAccountCount ) nCount = m_nAccountCount;
for( INT i = 0; i < nCount; i++ ) {
pFtpAccounts[i].CopyContents( m_pFtpAccounts[i] );
}
}
CString COpenRemoteDialog::GetPathName()
{
CString & szFileInfo = m_lstSelectedFileInfo.GetHead();
INT nFound = szFileInfo.Find('\n'); ASSERT(nFound >= 0);
return szFileInfo.Mid(0, nFound);
}
DWORD COpenRemoteDialog::GetFileSize()
{
CString & szFileInfo = m_lstSelectedFileInfo.GetHead();
INT nFound = szFileInfo.Find('\n'); ASSERT(nFound >= 0);
return atoi( szFileInfo.Mid(nFound+1) );
}
void COpenRemoteDialog::InitFtpAccounts()
{
INT nAccount = m_cmbFtpAccount.GetCurSel();
if( nAccount == CB_ERR ) nAccount = 0;
m_cmbFtpAccount.ResetContent();
for( INT i = 0; i < m_nAccountCount; i++ ) m_cmbFtpAccount.InsertString( i, m_pFtpAccounts[i].GetDisplayName() );
m_cmbFtpAccount.SetCurSel( nAccount );
}
void COpenRemoteDialog::InitFileFilters()
{
INT nFilter = m_cmbFileType.GetCurSel();
if( nFilter == CB_ERR ) nFilter = 0;
m_cmbFileType.ResetContent();
ParseFileFilter(m_arrFilterDescription, m_arrFilterExtensions, m_szFileFilter);
INT nSize = m_arrFilterDescription.GetSize();
for( INT i = 0; i < nSize; i++ ) m_cmbFileType.InsertString( i, m_arrFilterDescription.GetAt(i) );
m_cmbFileType.SetCurSel( nFilter );
}
void COpenRemoteDialog::ChangeDirectory(LPCTSTR lpszDirectory)
{
INT nAccount = m_cmbFtpAccount.GetCurSel();
CString szCurrentDirectory = m_pFtpAccounts[nAccount].m_szSubDirectory;
/* if( strcmp(lpszDirectory, "..") == 0 ) {
INT nFound = szCurrentDirectory.ReverseFind('/');
szCurrentDirectory = szCurrentDirectory.Mid(0, nFound);
if( szCurrentDirectory.GetLength() == 0 ) szCurrentDirectory = "/";
} else {
if( lpszDirectory[0] == '/' ) szCurrentDirectory = lpszDirectory;
else if( szCurrentDirectory.Compare("/") == 0 ) szCurrentDirectory += lpszDirectory;
else szCurrentDirectory += CString("/") + lpszDirectory;
} */
if( lpszDirectory[0] == '/' ) szCurrentDirectory = lpszDirectory;
else if( szCurrentDirectory.Compare("/") == 0 ) szCurrentDirectory += lpszDirectory;
else szCurrentDirectory += CString("/") + lpszDirectory;
m_pFtpAccounts[nAccount].m_szSubDirectory = szCurrentDirectory;
}
BOOL COpenRemoteDialog::RefreshFileList()
{
// get current ftp account
INT nAccount = m_cmbFtpAccount.GetCurSel();
if( nAccount == CB_ERR ) nAccount = 0;
CFtpAccount & rFtpAccount = m_pFtpAccounts[nAccount];
if( ! rFtpAccount.IsValid() ) return FALSE;
// if account password is not given, ask user
if( ! rFtpAccount.m_bSavePassword && ! rFtpAccount.m_bPasswordVerified ) {
CFtpPasswordDialog dlg; dlg.m_szAccountInfo = rFtpAccount.GetShortAccountName();
if( dlg.DoModal() != IDOK ) return FALSE;
rFtpAccount.m_szPassword = dlg.m_szPassword;
}
// get current file filter
INT nFilter = m_cmbFileType.GetCurSel();
if( nFilter == CB_ERR ) nFilter = 0;
CString szFilter = m_arrFilterExtensions.GetAt(nFilter);
if( ! szFilter.GetLength() ) szFilter = "*.*";
// get remote file list
CSortStringArray arrFileInfo;
if( ! GetRemoteFileList(arrFileInfo, rFtpAccount, szFilter) ) return FALSE;
// operation successful, password verified
if( ! rFtpAccount.m_bSavePassword ) rFtpAccount.m_bPasswordVerified = TRUE;
// set directory window text
CString szCurrentDirectory = rFtpAccount.m_szSubDirectory;
m_edtDirectory.SetWindowText( szCurrentDirectory );
// refresh file list
INT i, nSize, nItemCount = 0;
m_lstFiles.DeleteAllItems();
if( szCurrentDirectory.Compare("/") ) { // not a root directory
m_lstFiles.InsertItem(nItemCount, "..", REMOTE_ITEM_PARENT);
m_lstFiles.SetItemData(nItemCount, 0); nItemCount++;
m_btnParentDirectory.EnableWindow( TRUE );
} else { // root directory
m_btnParentDirectory.EnableWindow( FALSE );
}
arrFileInfo.Sort(TRUE); nSize = arrFileInfo.GetSize();
for( i = 0; i < nSize; i++ ) {
CString szFileInfo = arrFileInfo[i];
INT nFound = szFileInfo.Find('/', 2);
CString szName = szFileInfo.Mid(2, nFound - 2);
CString szInfo = szFileInfo.Mid(nFound + 1);
INT nImage = REMOTE_ITEM_FILE; DWORD dwSize = 0;
if( szFileInfo[0] == 'D' ) {
if( szInfo[0] == 'N' ) nImage = REMOTE_ITEM_DIRECTORY;
else nImage = REMOTE_ITEM_LINK;
} else dwSize = atoi( szInfo );
m_lstFiles.InsertItem(nItemCount, szName, nImage);
m_lstFiles.SetItemData(nItemCount, dwSize); nItemCount++;
}
return TRUE;
}
BEGIN_MESSAGE_MAP(COpenRemoteDialog, CDialog)
//{{AFX_MSG_MAP(COpenRemoteDialog)
ON_BN_CLICKED(IDC_FTP_SETTINGS, OnFtpSettings)
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_NOTIFY(NM_DBLCLK, IDC_FILE_LIST, OnDblclkFileList)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_FILE_LIST, OnItemchangedFileList)
ON_CBN_SELCHANGE(IDC_FILE_TYPE, OnSelchangeFileType)
ON_BN_CLICKED(IDC_CREATE_DIRECTORY, OnCreateDirectory)
ON_BN_CLICKED(IDC_PARENT_DIRECTORY, OnParentDirectory)
ON_CBN_SELCHANGE(IDC_FTP_ACCOUNT, OnSelchangeFtpAccount)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COpenRemoteDialog message handlers
BOOL COpenRemoteDialog::OnInitDialog()
{
CDialog::OnInitDialog();
if( m_bOpenFileDialog ) { // Open Remote
CString szText; m_lstFiles.ModifyStyle( LVS_SINGLESEL, 0, 0 );
szText.LoadString( IDS_CTRL_OPEN_REMOTE_OK ); m_btnOK.SetWindowText(szText);
szText.LoadString( IDS_DLG_OPEN_REMOTE ); SetWindowText(szText);
} else { // Save As Remote
CString szText; m_lstFiles.ModifyStyle( 0, LVS_SINGLESEL, 0 );
szText.LoadString( IDS_CTRL_SAVE_REMOTE_OK ); m_btnOK.SetWindowText(szText);
szText.LoadString( IDS_DLG_SAVE_REMOTE ); SetWindowText(szText);
}
m_edtFileName.SetWindowText( m_szFileName );
m_imgButtons.Create(IDB_REMOTE_OPEN, 16, 0, RGB(255, 0, 255));
m_btnParentDirectory.SetIcon(m_imgButtons.ExtractIcon(0));
m_btnCreateDirectory.SetIcon(m_imgButtons.ExtractIcon(1));
m_btnFtpSettings.SetIcon(m_imgButtons.ExtractIcon(2));
m_imgFileList.Create(IDB_REMOTE_ITEM, 16, 0, RGB(255, 0, 255));
m_lstFiles.SetImageList( & m_imgFileList, LVSIL_SMALL );
// disable open or save button
m_btnOK.EnableWindow(FALSE);
// initialize ftp account combo box
InitFtpAccounts();
m_cmbFtpAccount.SetCurSel( m_nCurrentFtpAccount );
OnSelchangeFtpAccount();
// initialize file type combo box
InitFileFilters();
m_cmbFileType.SetCurSel( m_nCurrentFilterIndex );
return TRUE;
}
void COpenRemoteDialog::OnSelchangeFtpAccount()
{
INT nAccount = m_cmbFtpAccount.GetCurSel();
if( nAccount == CB_ERR ) nAccount = 0;
m_nCurrentFtpAccount = nAccount;
// set directory window text
CFtpAccount & rFtpAccount = m_pFtpAccounts[nAccount];
CString szCurrentDirectory = rFtpAccount.m_szSubDirectory;
m_edtDirectory.SetWindowText( szCurrentDirectory );
}
void COpenRemoteDialog::OnSelchangeFileType()
{
INT nFilter = m_cmbFileType.GetCurSel();
if( nFilter == CB_ERR ) nFilter = 0;
m_nCurrentFilterIndex = nFilter;
m_btnOK.EnableWindow( RefreshFileList() );
}
void COpenRemoteDialog::OnCreateDirectory()
{
AfxMessageBox("Sorry, not available now");
}
void COpenRemoteDialog::OnParentDirectory()
{
ChangeDirectory( ".." );
m_btnOK.EnableWindow( RefreshFileList() );
}
void COpenRemoteDialog::OnFtpSettings()
{
CFtpSettingsDialog dlg;
dlg.SetFtpAccounts( m_nAccountCount, m_pFtpAccounts );
if( dlg.DoModal() == IDOK ) {
dlg.GetFtpAccounts( m_nAccountCount, m_pFtpAccounts );
InitFtpAccounts();
}
}
void COpenRemoteDialog::OnConnect()
{
m_btnOK.EnableWindow( RefreshFileList() );
}
void COpenRemoteDialog::OnItemchangedFileList(NMHDR* pNMHDR, LRESULT* pResult)
{
INT nCount = 0; CString szFileList;
LVITEM item; TCHAR szText[MAX_PATH];
POSITION pos = m_lstFiles.GetFirstSelectedItemPosition();
while( pos ) {
memset( & item, 0x00, sizeof(LVITEM) );
item.iItem = m_lstFiles.GetNextSelectedItem( pos );
item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
item.pszText = szText; item.cchTextMax = MAX_PATH;
m_lstFiles.GetItem( & item );
if( item.iImage == REMOTE_ITEM_FILE ) { // append only file items to the file name edit control
CString szQuoted; szQuoted.Format("\"%s\"", szText);
szFileList = szQuoted + " " + szFileList; nCount++;
}
}
if( nCount > 1 ) m_edtFileName.SetWindowText( szFileList );
else if( nCount == 1 ) m_edtFileName.SetWindowText( szText );
*pResult = 0;
}
void COpenRemoteDialog::OnDblclkFileList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
LVITEM item; TCHAR szText[MAX_PATH];
if( pNMListView->iItem >= 0 ) {
memset( & item, 0x00, sizeof(LVITEM) );
item.iItem = pNMListView->iItem;
item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
item.pszText = szText; item.cchTextMax = MAX_PATH;
m_lstFiles.GetItem( & item );
if( item.iImage == REMOTE_ITEM_PARENT ) {
// if it is a parent directory item then change directory
ChangeDirectory( ".." );
m_btnOK.EnableWindow( RefreshFileList() );
} else if( item.iImage == REMOTE_ITEM_LINK || item.iImage == REMOTE_ITEM_DIRECTORY ) {
// if it is a directory item then change directory
ChangeDirectory( szText );
m_btnOK.EnableWindow( RefreshFileList() );
} else if( item.iImage == REMOTE_ITEM_FILE ) {
// if it is a file item then close dialog box and open this file
OnOK();
}
}
*pResult = 0;
}
void COpenRemoteDialog::OnOK()
{
CString szFileName; m_edtFileName.GetWindowText( szFileName );
if( ! szFileName.GetLength() ) return;
CString szDirectory; m_edtDirectory.GetWindowText( szDirectory );
INT nLen = szDirectory.GetLength();
if( nLen == 0 || szDirectory.GetAt(nLen-1) != '/' ) szDirectory += "/";
LVITEM item; LVFINDINFO info; TCHAR szText[MAX_PATH];
m_lstSelectedFileInfo.RemoveAll(); CString szFileInfo;
POSITION pos = m_lstFiles.GetFirstSelectedItemPosition();
while( pos ) {
memset( & item, 0x00, sizeof(LVITEM) );
item.iItem = m_lstFiles.GetNextSelectedItem( pos );
item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
item.pszText = szText; item.cchTextMax = MAX_PATH;
m_lstFiles.GetItem( & item );
if( item.iImage == REMOTE_ITEM_FILE ) { // append only file items
szFileInfo.Format("%s\n%d", szDirectory + szText, item.lParam);
m_lstSelectedFileInfo.AddTail( szFileInfo );
}
}
if( m_lstSelectedFileInfo.GetCount() == 0 ) m_lstSelectedFileInfo.AddTail("");
if( m_lstSelectedFileInfo.GetCount() == 1 ) {
// check if the file name specified in the edit control exist in the list control
memset( & info, 0x00, sizeof(LVFINDINFO) );
info.flags = LVFI_STRING; info.psz = szFileName;
INT nItemFound = m_lstFiles.FindItem( & info );
if( nItemFound >= 0 ) { // if the file name specified is found in the list
memset( & item, 0x00, sizeof(LVITEM) );
item.iItem = nItemFound;
item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
item.pszText = szText; item.cchTextMax = MAX_PATH;
m_lstFiles.GetItem( & item );
}
if( m_bOpenFileDialog ) { // if it is open dialog then check if the file name specified exists
if( nItemFound < 0 ) { // if the file name specified is not found in the list
CString szMessage; szMessage.Format("%s\nThere is no such file!", szFileName);
AfxMessageBox(szMessage); return;
} else if( item.iImage == REMOTE_ITEM_PARENT || item.iImage == REMOTE_ITEM_LINK
|| item.iImage == REMOTE_ITEM_DIRECTORY ) { // if the file name specified is a directory
ChangeDirectory( szFileName );
m_btnOK.EnableWindow( RefreshFileList() ); return;
} else {
szFileInfo.Format("%s\n%d", szDirectory + szFileName, item.lParam);
POSITION pos = m_lstSelectedFileInfo.GetHeadPosition();
m_lstSelectedFileInfo.SetAt(pos, szFileInfo);
}
} else { // if it is save dialog then we could add file overwrite check here
if( nItemFound < 0 ) { // if the file name specified is not found in the list
szFileInfo.Format("%s\n%d", szDirectory + szFileName, 0);
POSITION pos = m_lstSelectedFileInfo.GetHeadPosition();
m_lstSelectedFileInfo.SetAt(pos, szFileInfo);
} else if( item.iImage == REMOTE_ITEM_PARENT || item.iImage == REMOTE_ITEM_LINK
|| item.iImage == REMOTE_ITEM_DIRECTORY ) { // if the file name specifed is a directory
ChangeDirectory( szFileName );
m_btnOK.EnableWindow( RefreshFileList() ); return;
} else {
CString szMessage; szMessage.Format("%s\nOverwrite the existing file?", szFileName);
if( AfxMessageBox(szMessage, MB_YESNO) != IDYES ) return;
szFileInfo.Format("%s\n%d", szDirectory + szFileName, item.lParam);
POSITION pos = m_lstSelectedFileInfo.GetHeadPosition();
m_lstSelectedFileInfo.SetAt(pos, szFileInfo);
}
}
}
CDialog::OnOK();
}