summaryrefslogtreecommitdiffstats
path: root/vncviewer/FTDialog.cxx
blob: b27270da6f3b699a11971e984e76e75e92c9cb4e (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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* Copyright (C) 2005 TightVNC Team.  All Rights Reserved.
 *    
 * This 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 software 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 software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 *
 * TightVNC distribution homepage on the Web: http://www.tightvnc.com/
 *
 */

// -=- FTDialog.cxx

#include <vncviewer/FTDialog.h>

using namespace rfb;
using namespace rfb::win32;

FTDialog::FTDialog(HINSTANCE hInst, FileTransfer *pFT)
{
  m_pFileTransfer = pFT;
  m_hInstance = hInst;
  m_bDlgShown = false;

  m_pLocalLV = NULL;
  m_pRemoteLV = NULL;
  m_pProgress = NULL;

  m_hwndFTDialog = NULL;
  m_hwndLocalPath = NULL;
  m_hwndRemotePath = NULL;

  m_szLocalPath[0] = '\0';
  m_szRemotePath[0] = '\0';
  m_szLocalPathTmp[0] = '\0';
  m_szRemotePathTmp[0] = '\0';
}

FTDialog::~FTDialog()
{
  destroyFTDialog();
}

bool
FTDialog::createFTDialog(HWND hwndParent)
{
  if (m_hwndFTDialog != NULL) {
    ShowWindow(m_hwndFTDialog, SW_SHOW);
    m_bDlgShown = true;
    return true;
  }

  m_hwndFTDialog = CreateDialogParam(m_hInstance, 
                                     MAKEINTRESOURCE(IDD_FILETRANSFER_DLG),
                                     hwndParent, 
                                     (DLGPROC) FTDialogProc,
                                     (LONG) this);
  
  if (m_hwndFTDialog == NULL) return false;
  
  HWND hwndLocalList = GetDlgItem(m_hwndFTDialog, IDC_FTLOCALLIST);
  HWND hwndRemoteList = GetDlgItem(m_hwndFTDialog, IDC_FTREMOTELIST);

  if ((hwndLocalList == NULL) || (hwndRemoteList == NULL)) {
    destroyFTDialog();
    return false;
  }

  m_pLocalLV = new FTListView(hwndLocalList);
  m_pRemoteLV = new FTListView(hwndRemoteList);
  
  m_pProgress = new FTProgress(m_hwndFTDialog);
  
  if ((m_pLocalLV == NULL) || (m_pRemoteLV == NULL) || (m_pProgress == NULL)) {
    destroyFTDialog();
    return false;
  }

  initFTDialog();
  
  ShowWindow(m_hwndFTDialog, SW_SHOW);
  UpdateWindow(m_hwndFTDialog);
  m_bDlgShown = true;
  return true;
}

bool
FTDialog::initFTDialog()
{
  m_pLocalLV->initialize(m_hInstance);
  m_pRemoteLV->initialize(m_hInstance);

  m_pProgress->initialize(0,0);

  m_hwndLocalPath = GetDlgItem(m_hwndFTDialog, IDC_FTLOCALPATH);
  m_hwndRemotePath = GetDlgItem(m_hwndFTDialog, IDC_FTREMOTEPATH);

  setIcon(IDC_FTLOCALUP, IDI_FTUP);
  setIcon(IDC_FTREMOTEUP, IDI_FTUP);
  setIcon(IDC_FTLOCALRELOAD, IDI_FTRELOAD);
  setIcon(IDC_FTREMOTERELOAD, IDI_FTRELOAD);

  showLocalLVItems();
  showRemoteLVItems();

  return true;
}

bool
FTDialog::closeFTDialog()
{
  ShowWindow(m_hwndFTDialog, SW_HIDE);
  m_bDlgShown = false;
  return true;
}

void
FTDialog::destroyFTDialog()
{
  if (m_pLocalLV != NULL) {
    delete m_pLocalLV;
    m_pLocalLV = NULL;
  }

  if (m_pRemoteLV != NULL) {
    delete m_pRemoteLV;
    m_pRemoteLV = NULL;
  }

  if (m_pProgress != NULL) {
    delete m_pProgress;
    m_pProgress = NULL;
  }

  if (DestroyWindow(m_hwndFTDialog)) m_hwndFTDialog = NULL;
}

BOOL CALLBACK 
FTDialog::FTDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  FTDialog *_this = (FTDialog *) GetWindowLong(hwnd, GWL_USERDATA);
  switch (uMsg)
  {
  case WM_INITDIALOG:
    SetWindowLong(hwnd, GWL_USERDATA, (LONG) lParam);
    SetForegroundWindow(hwnd);
    return TRUE;
  case WM_COMMAND:
    {
      switch (LOWORD(wParam))
      {
      case IDC_FTLOCALPATH:
        switch (HIWORD (wParam))
        {
        case CBN_SETFOCUS:
          _this->refreshState();
          return FALSE;
        }
        break;
        case IDC_FTREMOTEPATH:
          switch (HIWORD (wParam))
          {
          case CBN_SETFOCUS:
            _this->refreshState();
            return FALSE;
          }
          break;
      case IDC_FTCLOSE:
        _this->closeFTDialog();
        return FALSE;
      case IDC_FTLOCALUP:
        _this->refreshState();
        _this->onLocalOneUpFolder();
        return FALSE;
      case IDC_FTREMOTEUP:
        _this->refreshState();
        _this->onRemoteOneUpFolder();
        return FALSE;
      case IDC_FTLOCALRELOAD:
        _this->refreshState();
        _this->onLocalReload();
        return FALSE;
      case IDC_FTREMOTERELOAD:
       _this->refreshState();
       _this->onRemoteReload();
        return FALSE;
      case IDC_FTUPLOAD:
        _this->onUpload();
        return FALSE;
      case IDC_FTDOWNLOAD:
        _this->onDownload();
        return FALSE;
      }
    }
    break;
    
  case WM_NOTIFY:
    switch (LOWORD(wParam))
    {
    case IDC_FTLOCALLIST:
      switch (((LPNMHDR) lParam)->code)
      {
      case NM_CLICK:
      case NM_SETFOCUS:
      case LVN_ITEMCHANGED:
        _this->refreshState();
        return FALSE;
      case LVN_GETDISPINFO:
        _this->m_pLocalLV->onGetDispInfo((NMLVDISPINFO *) lParam);
        return FALSE;
      case LVN_ITEMACTIVATE:
        _this->onLocalItemActivate((LPNMITEMACTIVATE) lParam);
        return FALSE;
      case NM_RCLICK:
        _this->onLocalRButton();
        return FALSE;
      }
      break;
    case IDC_FTREMOTELIST:
      switch (((LPNMHDR) lParam)->code)
      {
      case NM_CLICK:
      case NM_SETFOCUS:
      case LVN_ITEMCHANGED:
        _this->refreshState();
      case LVN_GETDISPINFO:
        _this->m_pRemoteLV->onGetDispInfo((NMLVDISPINFO *) lParam);
        return FALSE;
      case LVN_ITEMACTIVATE:
        _this->onRemoteItemActivate((LPNMITEMACTIVATE) lParam);
        return FALSE;
      case NM_RCLICK:
        _this->onRemoteRButton();
        return FALSE;
      }
      break;
    }
    break;
    case WM_CLOSE:
    case WM_DESTROY:
      _this->closeFTDialog();
      return FALSE;
  }
  return FALSE;
}

void 
FTDialog::onLocalItemActivate(LPNMITEMACTIVATE lpnmia)
{
  if (strlen(m_szLocalPath) == 0) {
   strcpy(m_szLocalPathTmp, m_pLocalLV->getActivateItemName(lpnmia)); 
  } else {
    sprintf(m_szLocalPathTmp, "%s\\%s", m_szLocalPath, m_pLocalLV->getActivateItemName(lpnmia));
  }
  showLocalLVItems();
}

void 
FTDialog::onRemoteItemActivate(LPNMITEMACTIVATE lpnmia)
{
  if (strlen(m_szRemotePath) == 0) {
   strcpy(m_szRemotePathTmp, m_pRemoteLV->getActivateItemName(lpnmia)); 
  } else {
    sprintf(m_szRemotePathTmp, "%s\\%s", m_szRemotePath, m_pRemoteLV->getActivateItemName(lpnmia));
  }
  showRemoteLVItems();
}

void 
FTDialog::onLocalReload()
{
  strcpy(m_szLocalPathTmp, m_szLocalPath);
  showLocalLVItems();
}

void 
FTDialog::onRemoteReload()
{
  strcpy(m_szRemotePathTmp, m_szRemotePath);
  showRemoteLVItems();
}

void
FTDialog::showLocalLVItems()
{
  FolderManager fm;
  FileInfo fileInfo;
  if (fm.getDirInfo(m_szLocalPathTmp, &fileInfo, 0)) {
    fileInfo.sort();
    m_pLocalLV->deleteAllItems();
    m_pLocalLV->addItems(&fileInfo);
    strcpy(m_szLocalPath, m_szLocalPathTmp);
    SetWindowText(m_hwndLocalPath, m_szLocalPath);
  }
}

void
FTDialog::showRemoteLVItems()
{
  m_pFileTransfer->requestFileList(m_szRemotePathTmp, FT_FLR_DEST_MAIN, 0);
}

void 
FTDialog::addRemoteLVItems(FileInfo *pFI)
{
  pFI->sort();
  m_pRemoteLV->deleteAllItems();
  m_pRemoteLV->addItems(pFI);
  strcpy(m_szRemotePath, m_szRemotePathTmp);
  SetWindowText(m_hwndRemotePath, m_szRemotePath);
  UpdateWindow(m_hwndFTDialog);
}

void 
FTDialog::onLocalOneUpFolder()
{
  strcpy(m_szLocalPathTmp, m_szLocalPath);
  makeOneUpFolder(m_szLocalPathTmp);
  showLocalLVItems();
}

void 
FTDialog::onRemoteOneUpFolder()
{
  strcpy(m_szRemotePathTmp, m_szRemotePath);
  makeOneUpFolder(m_szRemotePathTmp);
  showRemoteLVItems();
}

void 
FTDialog::reqFolderUnavailable()
{
  strcpy(m_szRemotePathTmp, m_szRemotePath);
}

int
FTDialog::makeOneUpFolder(char *pPath)
{
  if (strcmp(pPath, "") == 0) return strlen(pPath);
  for (int i=(strlen(pPath)-2); i>=0; i--) {
    if (pPath[i] == '\\') {
      pPath[i] = '\0';
      break;
    }
    if (i == 0) pPath[0] = '\0';
  }
  return strlen(pPath);
}

void 
FTDialog::onUpload()
{

}

void 
FTDialog::onDownload()
{

}

void 
FTDialog::onLocalRButton()
{
  showFTMenu(1, 1, 1, 1);
}

void 
FTDialog::onRemoteRButton()
{
  showFTMenu(1, 1, 1, 1);
}

void
FTDialog::showFTMenu(int copyBtnState, int renameBtnState, int deleteBtnState, int cancelBtnState)
{
  HMENU hMenu = LoadMenu(m_hInstance, MAKEINTRESOURCE(IDR_FTMENU));
  HMENU hFTMenu = GetSubMenu(hMenu, 0);

  SetMenuDefaultItem(hFTMenu, IDM_FTCOPY, FALSE);

  SetForegroundWindow(m_hwndFTDialog);

  POINT cursorPosition;
  GetCursorPos(&cursorPosition);
  TrackPopupMenu(hFTMenu, 0, cursorPosition.x, cursorPosition.y, 0, m_hwndFTDialog, 0);
}

void
FTDialog::setIcon(int dest, int idIcon)
{
  HANDLE hIcon = LoadImage(m_hInstance, MAKEINTRESOURCE(idIcon), IMAGE_ICON, 16, 16, LR_SHARED);
  SendMessage(GetDlgItem(m_hwndFTDialog, dest), BM_SETIMAGE, (WPARAM) IMAGE_ICON, (LPARAM) hIcon);
  DestroyIcon((HICON) hIcon);
}

void
FTDialog::refreshState()
{
  if (!m_bDlgShown) return;
  
  int locPathLen = strlen(m_szLocalPath);
  int remPathLen = strlen(m_szRemotePath);
  
  if (GetFocus() == m_pLocalLV->getWndHandle()) {
    if (strlen(m_szLocalPath) != 0) {
      int nCount = ListView_GetSelectedCount(m_pLocalLV->getWndHandle());
      if (nCount <= 0) {
        setButtonsState(0, 0, -1);
      } else {
        if (remPathLen == 0) {
          setButtonsState(0, 0, -1);
        } else {
          setButtonsState(1, 0, -1);
        }
      }
    } else {
      setButtonsState(0, 0, -1);
    }
  } else {
    if (GetFocus() == m_pRemoteLV->getWndHandle()) {
      if (strlen(m_szRemotePath) != 0) {
        int nCount = ListView_GetSelectedCount(m_pRemoteLV->getWndHandle());
        if (nCount <= 0) {
          setButtonsState(0, 0, -1);
        } else {
          if (locPathLen == 0) {
            setButtonsState(0, 0, -1);
          } else {
            setButtonsState(0, 1, -1);
          }
        }
      } else {
        setButtonsState(0, 0, -1);
      }
    } else {
      setButtonsState(0, 0, -1);
    }
  }
/*
  if (m_pFileTransfer->isTransferEnable()) {
    setAllButtonsState(-1, -1, -1, -1, 1);
  } else {
    setAllButtonsState(-1, -1, -1, -1, 0);
  }
*/
}

void
FTDialog::setButtonsState(int uploadBtnState, int downloadBtnState, int cancelBtnState)
{
  switch (uploadBtnState)
  {
  case 0: EnableWindow(GetDlgItem(m_hwndFTDialog, IDC_FTUPLOAD), FALSE); break;
  case 1: EnableWindow(GetDlgItem(m_hwndFTDialog, IDC_FTUPLOAD), TRUE); break;
  default: break;
  }

  switch (downloadBtnState)
  {
  case 0: EnableWindow(GetDlgItem(m_hwndFTDialog, IDC_FTDOWNLOAD), FALSE); break;
  case 1: EnableWindow(GetDlgItem(m_hwndFTDialog, IDC_FTDOWNLOAD), TRUE); break;
  default: break;
  }

  switch (cancelBtnState)
  {
  case 0: EnableWindow(GetDlgItem(m_hwndFTDialog, IDC_FTCANCEL), FALSE); break;
  case 1: EnableWindow(GetDlgItem(m_hwndFTDialog, IDC_FTCANCEL), TRUE); break;
  default: break;
  }
}