LocationProfiles.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <CoreMain.h>
  5. #include <Configuration.h>
  6. #include <RemoteFiles.h>
  7. #include <VCLCommon.h>
  8. #include <TextsWin.h>
  9. #include <HelpWin.h>
  10. #include <Common.h>
  11. #include "LocationProfiles.h"
  12. #include "WinConfiguration.h"
  13. #include "Custom.h"
  14. #include <Math.hpp>
  15. //---------------------------------------------------------------------
  16. #pragma link "IEComboBox"
  17. #pragma link "PngImageList"
  18. #ifndef NO_RESOURCES
  19. #pragma resource "*.dfm"
  20. #endif
  21. //---------------------------------------------------------------------
  22. bool __fastcall LocationProfilesDialog(TOpenDirectoryMode Mode,
  23. TOperationSide Side, UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  24. TStrings * LocalDirectories, TStrings * RemoteDirectories, TTerminal * Terminal)
  25. {
  26. bool Result;
  27. TLocationProfilesDialog * Dialog = new TLocationProfilesDialog(Application);
  28. try
  29. {
  30. Dialog->LocalDirectory = LocalDirectory;
  31. Dialog->RemoteDirectory = RemoteDirectory;
  32. Dialog->OperationSide = Side;
  33. Dialog->Terminal = Terminal;
  34. Dialog->RemoteDirectories = RemoteDirectories;
  35. Dialog->LocalDirectories = LocalDirectories;
  36. Dialog->Mode = Mode;
  37. Result = Dialog->Execute();
  38. if (Result)
  39. {
  40. LocalDirectory = Dialog->LocalDirectory;
  41. RemoteDirectory = Dialog->RemoteDirectory;
  42. }
  43. }
  44. __finally
  45. {
  46. delete Dialog;
  47. }
  48. return Result;
  49. }
  50. //---------------------------------------------------------------------------
  51. //---------------------------------------------------------------------------
  52. void __fastcall BookmarkNameValidateName(const UnicodeString Name)
  53. {
  54. if (Name.IsEmpty() || IsNumber(Name))
  55. {
  56. throw Exception(FMTLOAD(BOOKMARK_INVALID_NAME, (Name)));
  57. }
  58. }
  59. //---------------------------------------------------------------------------
  60. void __fastcall BookmarkFolderValidateName(const UnicodeString Name,
  61. bool AllowEmpty)
  62. {
  63. if ((!AllowEmpty && Name.IsEmpty()) || Name.Pos(L"\\"))
  64. {
  65. throw Exception(FMTLOAD(BOOKMARK_FOLDER_INVALID_NAME, (Name)));
  66. }
  67. }
  68. //---------------------------------------------------------------------------
  69. //---------------------------------------------------------------------------
  70. class TBookmarkNameDialog : public TCustomDialog
  71. {
  72. public:
  73. __fastcall TBookmarkNameDialog(TStrings * PeerBookmarks, bool AllowShared);
  74. bool __fastcall Execute(UnicodeString & Name, bool & Shared);
  75. protected:
  76. virtual void __fastcall DoValidate();
  77. private:
  78. TComboBox * NameCombo;
  79. TCheckBox * SharedCheck;
  80. };
  81. //---------------------------------------------------------------------
  82. __fastcall TBookmarkNameDialog::TBookmarkNameDialog(TStrings * PeerBookmarks,
  83. bool AllowShared) :
  84. TCustomDialog(HELP_LOCATION_PROFILE_ADD)
  85. {
  86. Caption = LoadStr(ADD_BOOKMARK_CAPTION);
  87. NameCombo = new TComboBox(this);
  88. NameCombo->AutoComplete = false;
  89. NameCombo->DropDownCount = Max(NameCombo->DropDownCount, 16);
  90. AddComboBox(NameCombo, CreateLabel(LoadStr(ADD_BOOKMARK_PROMPT)));
  91. NameCombo->Items = PeerBookmarks;
  92. if (AllowShared)
  93. {
  94. SharedCheck = new TCheckBox(this);
  95. SharedCheck->Caption = LoadStr(ADD_BOOKMARK_SHARED);
  96. AddButtonControl(SharedCheck);
  97. }
  98. else
  99. {
  100. SharedCheck = NULL;
  101. }
  102. }
  103. //---------------------------------------------------------------------
  104. void __fastcall TBookmarkNameDialog::DoValidate()
  105. {
  106. if (NameCombo->Text.IsEmpty() || IsNumber(NameCombo->Text))
  107. {
  108. throw Exception(FMTLOAD(BOOKMARK_INVALID_NAME, (NameCombo->Text)));
  109. }
  110. TCustomDialog::DoValidate();
  111. }
  112. //---------------------------------------------------------------------
  113. bool __fastcall TBookmarkNameDialog::Execute(UnicodeString & Name, bool & Shared)
  114. {
  115. NameCombo->Text = Name;
  116. if (SharedCheck != NULL)
  117. {
  118. SharedCheck->Checked = Shared;
  119. }
  120. bool Result = TCustomDialog::Execute();
  121. if (Result)
  122. {
  123. Name = NameCombo->Text;
  124. if (SharedCheck != NULL)
  125. {
  126. Shared = SharedCheck->Checked;
  127. }
  128. }
  129. return Result;
  130. }
  131. //---------------------------------------------------------------------------
  132. //---------------------------------------------------------------------------
  133. class TBookmarkFolderDialog : public TCustomDialog
  134. {
  135. public:
  136. __fastcall TBookmarkFolderDialog(TStrings * Folders);
  137. bool __fastcall Execute(UnicodeString & Name);
  138. protected:
  139. virtual void __fastcall DoValidate();
  140. private:
  141. TComboBox * NameCombo;
  142. };
  143. //---------------------------------------------------------------------
  144. __fastcall TBookmarkFolderDialog::TBookmarkFolderDialog(TStrings * Folders) :
  145. TCustomDialog(HELP_LOCATION_PROFILE_MOVE)
  146. {
  147. Caption = LoadStr(MOVE_BOOKMARK_CAPTION);
  148. NameCombo = new TComboBox(this);
  149. NameCombo->AutoComplete = false;
  150. AddComboBox(NameCombo, CreateLabel(LoadStr(MOVE_BOOKMARK_PROMPT)));
  151. NameCombo->Items = Folders;
  152. }
  153. //---------------------------------------------------------------------
  154. void __fastcall TBookmarkFolderDialog::DoValidate()
  155. {
  156. BookmarkFolderValidateName(NameCombo->Text, true);
  157. TCustomDialog::DoValidate();
  158. }
  159. //---------------------------------------------------------------------
  160. bool __fastcall TBookmarkFolderDialog::Execute(UnicodeString & Name)
  161. {
  162. NameCombo->Text = Name;
  163. bool Result = TCustomDialog::Execute();
  164. if (Result)
  165. {
  166. Name = NameCombo->Text;
  167. }
  168. return Result;
  169. }
  170. //---------------------------------------------------------------------
  171. //---------------------------------------------------------------------
  172. __fastcall TLocationProfilesDialog::TLocationProfilesDialog(TComponent * AOwner):
  173. TForm(AOwner)
  174. {
  175. FOperationSide = osLocal;
  176. FBookmarkDragSource = NULL;
  177. FTerminal = NULL;
  178. FSessionBookmarkList = new TBookmarkList();
  179. FSharedBookmarkList = new TBookmarkList();
  180. FChanging = false;
  181. FSessionFolders = new TStringList;
  182. FSessionFolders->CaseSensitive = false;
  183. FSessionFolders->Sorted = true;
  184. FSessionFolders->Duplicates = dupIgnore;
  185. FSharedFolders = new TStringList;
  186. FSharedFolders->CaseSensitive = false;
  187. FSharedFolders->Sorted = true;
  188. FSharedFolders->Duplicates = dupIgnore;
  189. FSessionScrollOnDragOver = new TTreeViewScrollOnDragOver(SessionProfilesView, true);
  190. FSharedScrollOnDragOver = new TTreeViewScrollOnDragOver(SharedProfilesView, true);
  191. UseSystemSettings(this);
  192. }
  193. //---------------------------------------------------------------------
  194. __fastcall TLocationProfilesDialog::~TLocationProfilesDialog()
  195. {
  196. SAFE_DESTROY(FSharedScrollOnDragOver);
  197. SAFE_DESTROY(FSessionScrollOnDragOver);
  198. SAFE_DESTROY(FSharedBookmarkList);
  199. SAFE_DESTROY(FSessionBookmarkList);
  200. SAFE_DESTROY(FSharedFolders);
  201. SAFE_DESTROY(FSessionFolders);
  202. }
  203. //---------------------------------------------------------------------------
  204. void __fastcall TLocationProfilesDialog::SetLocalDirectory(UnicodeString value)
  205. {
  206. if (LocalDirectory != value)
  207. {
  208. LocalDirectoryEdit->Text = value;
  209. FindProfile();
  210. UpdateControls();
  211. }
  212. }
  213. //---------------------------------------------------------------------------
  214. UnicodeString __fastcall TLocationProfilesDialog::GetLocalDirectory()
  215. {
  216. return ExcludeTrailingBackslash(LocalDirectoryEdit->Text);
  217. }
  218. //---------------------------------------------------------------------------
  219. void __fastcall TLocationProfilesDialog::SetRemoteDirectory(UnicodeString value)
  220. {
  221. if (RemoteDirectory != value)
  222. {
  223. RemoteDirectoryEdit->Text = value;
  224. FindProfile();
  225. UpdateControls();
  226. }
  227. }
  228. //---------------------------------------------------------------------------
  229. UnicodeString __fastcall TLocationProfilesDialog::GetRemoteDirectory()
  230. {
  231. return UnixExcludeTrailingBackslash(RemoteDirectoryEdit->Text);
  232. }
  233. //---------------------------------------------------------------------------
  234. void __fastcall TLocationProfilesDialog::SetRemoteDirectories(TStrings * value)
  235. {
  236. RemoteDirectoryEdit->Items = value;
  237. }
  238. //---------------------------------------------------------------------------
  239. TStrings * __fastcall TLocationProfilesDialog::GetRemoteDirectories()
  240. {
  241. return RemoteDirectoryEdit->Items;
  242. }
  243. //---------------------------------------------------------------------------
  244. void __fastcall TLocationProfilesDialog::SetLocalDirectories(TStrings * value)
  245. {
  246. LocalDirectoryEdit->Items = value;
  247. }
  248. //---------------------------------------------------------------------------
  249. TStrings * __fastcall TLocationProfilesDialog::GetLocalDirectories()
  250. {
  251. return LocalDirectoryEdit->Items;
  252. }
  253. //---------------------------------------------------------------------------
  254. bool __fastcall TLocationProfilesDialog::ProfileMatch(TTreeNode * Node)
  255. {
  256. bool Result = false;
  257. if (Node->Data)
  258. {
  259. TBookmark * Bookmark = (TBookmark *)Node->Data;
  260. Result =
  261. (Bookmark->Local == LocalDirectory) &&
  262. (Bookmark->Remote == RemoteDirectory);
  263. }
  264. return Result;
  265. }
  266. //---------------------------------------------------------------------------
  267. void __fastcall TLocationProfilesDialog::FindProfile(TTreeView * ProfilesView)
  268. {
  269. if ((ProfilesView->Selected == NULL) ||
  270. !ProfileMatch(ProfilesView->Selected))
  271. {
  272. TTreeNode * Match = NULL;
  273. for (int Index = 0; Index < ProfilesView->Items->Count; Index++)
  274. {
  275. TTreeNode * Node = ProfilesView->Items->Item[Index];
  276. if (ProfileMatch(Node))
  277. {
  278. Match = Node;
  279. break;
  280. }
  281. }
  282. if (Match)
  283. {
  284. ProfilesView->Selected = Match;
  285. Match->MakeVisible();
  286. }
  287. else
  288. {
  289. ProfilesView->Selected = NULL;
  290. }
  291. }
  292. }
  293. //---------------------------------------------------------------------------
  294. void __fastcall TLocationProfilesDialog::FindProfile()
  295. {
  296. FindProfile(SessionProfilesView);
  297. FindProfile(SharedProfilesView);
  298. }
  299. //---------------------------------------------------------------------------
  300. void __fastcall TLocationProfilesDialog::ControlChange(TObject * /*Sender*/)
  301. {
  302. UpdateControls();
  303. }
  304. //---------------------------------------------------------------------------
  305. void __fastcall TLocationProfilesDialog::UpdateProfilesControls(
  306. TTreeView * ProfilesView,
  307. TButton * AddBookmarkButton, TButton * RemoveBookmarkButton,
  308. TButton * RenameBookmarkButton, TButton * BookmarkMoveToButton,
  309. TButton * ShortCutBookmarkButton,
  310. TButton * UpBookmarkButton, TButton * DownBookmarkButton)
  311. {
  312. EnableControl(AddBookmarkButton,
  313. !LocalDirectory.IsEmpty() || !RemoteDirectory.IsEmpty());
  314. EnableControl(RemoveBookmarkButton, ProfilesView->Selected);
  315. EnableControl(RenameBookmarkButton, ProfilesView->Selected);
  316. EnableControl(BookmarkMoveToButton, ProfilesView->Selected && ProfilesView->Selected->Data);
  317. if (ShortCutBookmarkButton != NULL)
  318. {
  319. EnableControl(ShortCutBookmarkButton, ProfilesView->Selected && ProfilesView->Selected->Data);
  320. }
  321. EnableControl(UpBookmarkButton, ProfilesView->Selected &&
  322. ProfilesView->Selected->Data && ProfilesView->Selected->getPrevSibling() &&
  323. ProfilesView->Selected->getPrevSibling()->Data);
  324. EnableControl(DownBookmarkButton, ProfilesView->Selected &&
  325. ProfilesView->Selected->Data && ProfilesView->Selected->getNextSibling() &&
  326. ProfilesView->Selected->getNextSibling()->Data);
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TLocationProfilesDialog::UpdateControls()
  330. {
  331. EnableControl(OKBtn, !LocalDirectory.IsEmpty() || !RemoteDirectory.IsEmpty());
  332. UpdateProfilesControls(SessionProfilesView,
  333. AddSessionBookmarkButton, RemoveSessionBookmarkButton,
  334. RenameSessionBookmarkButton, SessionBookmarkMoveToButton, NULL,
  335. UpSessionBookmarkButton, DownSessionBookmarkButton);
  336. UpdateProfilesControls(SharedProfilesView,
  337. AddSharedBookmarkButton, RemoveSharedBookmarkButton,
  338. RenameSharedBookmarkButton, SharedBookmarkMoveToButton,
  339. ShortCutSharedBookmarkButton,
  340. UpSharedBookmarkButton, DownSharedBookmarkButton);
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TLocationProfilesDialog::LoadBookmarks(
  344. TTreeView * ProfilesView, TStringList * Folders, TBookmarkList * BookmarkList,
  345. TBookmarkList * Source)
  346. {
  347. if (Source != NULL)
  348. {
  349. BookmarkList->Assign(Source);
  350. }
  351. else
  352. {
  353. BookmarkList->Clear();
  354. }
  355. Configuration->Usage->SetMax(L"MaxBookmarks", BookmarkList->Count);
  356. assert(BookmarkList != NULL);
  357. Folders->Clear();
  358. for (int Index = 0; Index < BookmarkList->Count; Index++)
  359. {
  360. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  361. if (!Bookmark->Node.IsEmpty())
  362. {
  363. Folders->Add(Bookmark->Node);
  364. }
  365. }
  366. // WORKAROUND
  367. // TTreeNodes::Clear is noop, when tree does not have a handle yet.
  368. // (what happens here for a tree view on an inactive page)
  369. ProfilesView->HandleNeeded();
  370. ProfilesView->Items->Clear();
  371. for (int Index = 0; Index < Folders->Count; Index++)
  372. {
  373. Folders->Objects[Index] = ProfilesView->Items->Add(NULL, Folders->Strings[Index]);
  374. }
  375. for (int Index = 0; Index < BookmarkList->Count; Index++)
  376. {
  377. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  378. TTreeNode * Parent = NULL;
  379. if (!Bookmark->Node.IsEmpty())
  380. {
  381. assert(Folders->IndexOf(Bookmark->Node) >= 0);
  382. Parent = dynamic_cast<TTreeNode *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  383. }
  384. ProfilesView->Items->AddChildObject(Parent, BookmarkText(Bookmark), Bookmark);
  385. if ((Parent != NULL) && (Parent->Count == 1))
  386. {
  387. // only now, when folder node has its first child, we can eventually expand it
  388. Parent->Expanded = BookmarkList->NodeOpened[Parent->Text];
  389. }
  390. }
  391. }
  392. //---------------------------------------------------------------------------
  393. bool __fastcall TLocationProfilesDialog::Execute()
  394. {
  395. bool Result;
  396. PageControl->ActivePage = GetProfilesSheet();
  397. Result = (ShowModal() == DefaultResult(this));
  398. if (Terminal)
  399. {
  400. WinConfiguration->Bookmarks[FSessionKey] = FSessionBookmarkList;
  401. WinConfiguration->SharedBookmarks = FSharedBookmarkList;
  402. WinConfiguration->UseSharedBookmarks = (PageControl->ActivePage == SharedProfilesSheet);
  403. }
  404. return Result;
  405. }
  406. //---------------------------------------------------------------------------
  407. TTabSheet * TLocationProfilesDialog::GetProfilesSheet()
  408. {
  409. return WinConfiguration->UseSharedBookmarks ? SharedProfilesSheet : SessionProfilesSheet;
  410. }
  411. //---------------------------------------------------------------------------
  412. template<class T>
  413. typename T * GetProfilesObject(TObject * Sender, T * SessionObject, T * SharedObject)
  414. {
  415. TControl * Control = dynamic_cast<TControl *>(Sender);
  416. assert(Control != NULL);
  417. switch (abs(Control->Tag))
  418. {
  419. case 1: return SessionObject;
  420. case 2: return SharedObject;
  421. default: assert(false); return NULL;
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. TBookmarkList * TLocationProfilesDialog::GetBookmarkList(TObject * Sender)
  426. {
  427. return GetProfilesObject(Sender, FSessionBookmarkList, FSharedBookmarkList);
  428. }
  429. //---------------------------------------------------------------------------
  430. TStringList * TLocationProfilesDialog::GetFolders(TObject * Sender)
  431. {
  432. #ifdef _DEBUG
  433. assert(FSessionProfilesViewHandle == SessionProfilesView->Handle);
  434. assert(FSharedProfilesViewHandle == SharedProfilesView->Handle);
  435. #endif
  436. return GetProfilesObject(Sender, FSessionFolders, FSharedFolders);
  437. }
  438. //---------------------------------------------------------------------------
  439. TTreeView * TLocationProfilesDialog::GetProfilesView(TObject * Sender)
  440. {
  441. return GetProfilesObject(Sender, SessionProfilesView, SharedProfilesView);
  442. }
  443. //---------------------------------------------------------------------------
  444. TTreeViewScrollOnDragOver * TLocationProfilesDialog::GetScrollOnDragOver(TObject * Sender)
  445. {
  446. return GetProfilesObject(Sender, FSessionScrollOnDragOver, FSharedScrollOnDragOver);
  447. }
  448. //---------------------------------------------------------------------------
  449. bool __fastcall TLocationProfilesDialog::AddAsBookmark(TObject * Sender, bool Initial)
  450. {
  451. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  452. TTreeView * ProfilesView = GetProfilesView(Sender);
  453. assert(!LocalDirectory.IsEmpty() || !RemoteDirectory.IsEmpty());
  454. bool Result;
  455. UnicodeString BookmarkName;
  456. if ((OperationSide == osLocal && !LocalDirectory.IsEmpty()) ||
  457. RemoteDirectory.IsEmpty())
  458. {
  459. BookmarkName = LocalDirectory;
  460. }
  461. else
  462. {
  463. BookmarkName = RemoteDirectory;
  464. }
  465. TTreeNode * Selected = ProfilesView->Selected;
  466. TBookmark * SelectedBookmark = NULL;
  467. UnicodeString SelectedNode;
  468. if (Selected != NULL)
  469. {
  470. assert(!Initial);
  471. SelectedBookmark = (TBookmark *)Selected->Data;
  472. if (SelectedBookmark != NULL)
  473. {
  474. SelectedNode = SelectedBookmark->Node;
  475. }
  476. else
  477. {
  478. SelectedNode = Selected->Text;
  479. }
  480. }
  481. TStrings * PeerBookmarks = new TStringList();
  482. try
  483. {
  484. for (int Index = 0; Index < BookmarkList->Count; Index++)
  485. {
  486. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  487. if (Bookmark->Node == SelectedNode)
  488. {
  489. PeerBookmarks->Add(Bookmark->Name);
  490. }
  491. }
  492. TBookmarkNameDialog * Dialog = new TBookmarkNameDialog(PeerBookmarks, Initial);
  493. try
  494. {
  495. bool Shared = WinConfiguration->UseSharedBookmarks;
  496. Result = Dialog->Execute(BookmarkName, Shared);
  497. if (Result)
  498. {
  499. if (Initial)
  500. {
  501. WinConfiguration->UseSharedBookmarks = Shared;
  502. BookmarkList = GetBookmarkList(GetProfilesSheet());
  503. ProfilesView = GetProfilesView(GetProfilesSheet());
  504. }
  505. TBookmark * Bookmark = BookmarkList->FindByName(SelectedNode, BookmarkName);
  506. if (Bookmark != NULL)
  507. {
  508. Bookmark->Local = LocalDirectory;
  509. Bookmark->Remote = RemoteDirectory;
  510. for (int Index = 0; Index < ProfilesView->Items->Count; Index++)
  511. {
  512. TTreeNode * Node = ProfilesView->Items->Item[Index];
  513. if (Node->Data == Bookmark)
  514. {
  515. Selected = Node;
  516. break;
  517. }
  518. }
  519. }
  520. else
  521. {
  522. Bookmark = new TBookmark();
  523. Bookmark->Name = BookmarkName;
  524. Bookmark->Local = LocalDirectory;
  525. Bookmark->Remote = RemoteDirectory;
  526. if (SelectedBookmark != NULL)
  527. {
  528. Bookmark->Node = SelectedBookmark->Node;
  529. BookmarkList->InsertBefore(SelectedBookmark, Bookmark);
  530. Selected = ProfilesView->Items->InsertObject(Selected, BookmarkText(Bookmark), Bookmark);
  531. }
  532. else if ((Selected != NULL) && (SelectedBookmark == NULL))
  533. {
  534. // must be a folder
  535. assert(!Selected->Parent); // more than one level of folders is not supported
  536. Bookmark->Node = Selected->Text;
  537. BookmarkList->Add(Bookmark);
  538. Selected = ProfilesView->Items->AddChildObject(Selected, BookmarkText(Bookmark), Bookmark);
  539. }
  540. else
  541. {
  542. BookmarkList->Add(Bookmark);
  543. Selected = ProfilesView->Items->AddObject(NULL, BookmarkText(Bookmark), Bookmark);
  544. }
  545. }
  546. ProfilesView->Selected = Selected;
  547. }
  548. }
  549. __finally
  550. {
  551. delete Dialog;
  552. }
  553. }
  554. __finally
  555. {
  556. delete PeerBookmarks;
  557. }
  558. UpdateControls();
  559. return Result;
  560. }
  561. //---------------------------------------------------------------------------
  562. void __fastcall TLocationProfilesDialog::AddBookmarkButtonClick(TObject * Sender)
  563. {
  564. AddAsBookmark(Sender, false);
  565. }
  566. //---------------------------------------------------------------------------
  567. void __fastcall TLocationProfilesDialog::RemoveBookmark(TObject * Sender)
  568. {
  569. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  570. TTreeView * ProfilesView = GetProfilesView(Sender);
  571. TStringList * Folders = GetFolders(Sender);
  572. assert(ProfilesView->Selected);
  573. TTreeNode * Node = ProfilesView->Selected;
  574. if (Node->Data)
  575. {
  576. BookmarkList->Delete((TBookmark *)Node->Data);
  577. TTreeNode * ParentNode = Node->Parent;
  578. Node->Delete();
  579. if (ParentNode && !ParentNode->Count)
  580. {
  581. assert(Folders->IndexOfObject(ParentNode) >= 0);
  582. Folders->Delete(Folders->IndexOfObject(ParentNode));
  583. ParentNode->Delete();
  584. }
  585. }
  586. else
  587. {
  588. UnicodeString Message = MainInstructions(LoadStr(DELETE_BOOKMARK_FOLDER));
  589. if (MessageDialog(Message, qtConfirmation,
  590. qaYes | qaNo, HELP_LOCATION_PROFILE_DELETE) == qaYes)
  591. {
  592. assert(Node->Count);
  593. for (int i = 0; i < Node->Count; i++)
  594. {
  595. BookmarkList->Delete((TBookmark *)Node->Item[i]->Data);
  596. }
  597. assert(Folders->IndexOfObject(Node) >= 0);
  598. Folders->Delete(Folders->IndexOfObject(Node));
  599. Node->Delete();
  600. }
  601. }
  602. UpdateControls();
  603. }
  604. //---------------------------------------------------------------------------
  605. void __fastcall TLocationProfilesDialog::RemoveBookmarkButtonClick(TObject * Sender)
  606. {
  607. RemoveBookmark(Sender);
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TLocationProfilesDialog::BookmarkMove(TObject * Sender,
  611. TTreeNode * Source, TTreeNode * Dest)
  612. {
  613. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  614. TTreeView * ProfilesView = GetProfilesView(Sender);
  615. TStringList * Folders = GetFolders(Sender);
  616. assert(Source && Source->Data);
  617. TBookmark * Bookmark = (TBookmark *)Source->Data;
  618. TTreeNode * PrevFolderNode = Source->Parent;
  619. if (!Dest || !Dest->Data)
  620. {
  621. Bookmark->Node = Dest ? Dest->Text : UnicodeString();
  622. BookmarkList->MoveTo(BookmarkList->Bookmarks[BookmarkList->Count - 1],
  623. Bookmark, false);
  624. ProfilesView->Selected->MoveTo(Dest, naAddChild);
  625. }
  626. else
  627. {
  628. TBookmark * DestBookmark = (TBookmark *)Dest->Data;
  629. Bookmark->Node = DestBookmark->Node;
  630. BookmarkList->MoveTo(DestBookmark, Bookmark,
  631. Source->AbsoluteIndex > Dest->AbsoluteIndex);
  632. if (Source->AbsoluteIndex > Dest->AbsoluteIndex)
  633. {
  634. Source->MoveTo(Dest, naInsert);
  635. }
  636. else if (Dest->getNextSibling() != NULL)
  637. {
  638. Source->MoveTo(Dest->getNextSibling(), naInsert);
  639. }
  640. else
  641. {
  642. Source->MoveTo(Dest, naAdd);
  643. }
  644. }
  645. if (PrevFolderNode && !PrevFolderNode->Count)
  646. {
  647. assert(Folders->IndexOfObject(PrevFolderNode) >= 0);
  648. Folders->Delete(Folders->IndexOfObject(PrevFolderNode));
  649. PrevFolderNode->Delete();
  650. }
  651. Source->MakeVisible();
  652. UpdateControls();
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TLocationProfilesDialog::BookmarkButtonClick(TObject * Sender)
  656. {
  657. TControl * Control = dynamic_cast<TControl *>(Sender);
  658. TTreeNode * Node = GetProfilesView(Sender)->Selected;
  659. assert(Node);
  660. assert(Node->Data);
  661. TTreeNode * TargetNode;
  662. if (Control->Tag < 0)
  663. {
  664. TargetNode = Node->getPrevSibling();
  665. assert(TargetNode);
  666. }
  667. else
  668. {
  669. TargetNode = Node->getNextSibling();
  670. }
  671. BookmarkMove(Sender, Node, TargetNode ? TargetNode : Node->Parent);
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TLocationProfilesDialog::ProfilesViewStartDrag(
  675. TObject * Sender, TDragObject *& /*DragObject*/)
  676. {
  677. TTreeView * ProfilesView = GetProfilesView(Sender);
  678. if (!ProfilesView->Selected->Data)
  679. {
  680. Abort();
  681. }
  682. FBookmarkDragSource = ProfilesView->Selected;
  683. GetScrollOnDragOver(Sender)->StartDrag();
  684. }
  685. //---------------------------------------------------------------------------
  686. void __fastcall TLocationProfilesDialog::ProfilesViewDragOver(
  687. TObject * Sender, TObject * Source, int X, int Y,
  688. TDragState /*State*/, bool & Accept)
  689. {
  690. TTreeView * ProfilesView = GetProfilesView(Sender);
  691. if (Source == ProfilesView)
  692. {
  693. Accept = (ProfilesView->DropTarget != NULL) &&
  694. (FBookmarkDragSource != ProfilesView->DropTarget);
  695. GetScrollOnDragOver(Sender)->DragOver(TPoint(X, Y));
  696. }
  697. }
  698. //---------------------------------------------------------------------------
  699. void __fastcall TLocationProfilesDialog::ProfilesViewDragDrop(
  700. TObject * Sender, TObject * Source, int /*X*/, int /*Y*/)
  701. {
  702. TTreeView * ProfilesView = GetProfilesView(Sender);
  703. if ((Source == ProfilesView) && (ProfilesView->DropTarget != NULL) &&
  704. (FBookmarkDragSource != ProfilesView->DropTarget))
  705. {
  706. assert(FBookmarkDragSource);
  707. TTreeNode * Target = ProfilesView->DropTarget;
  708. BookmarkMove(Sender, FBookmarkDragSource, Target);
  709. FBookmarkDragSource = NULL;
  710. }
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TLocationProfilesDialog::ProfilesViewDblClick(TObject * Sender)
  714. {
  715. TTreeView * ProfilesView = GetProfilesView(Sender);
  716. TPoint P = ProfilesView->ScreenToClient(Mouse->CursorPos);
  717. TTreeNode * Node = ProfilesView->GetNodeAt(P.x, P.y);
  718. if (OKBtn->Enabled && Node && Node->Data && Node->Selected)
  719. {
  720. ModalResult = DefaultResult(this);
  721. }
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TLocationProfilesDialog::FormShow(TObject * /*Sender*/)
  725. {
  726. if (Terminal)
  727. {
  728. // cache session key, in case terminal is closed while the window is open
  729. FSessionKey = Terminal->SessionData->SessionKey;
  730. // WORKAROUND
  731. // Has to load this only now (not in Execute before ShowModal),
  732. // when the trees are finally (re)created,
  733. // otherwise the references in *Folders would be invalid already
  734. LoadBookmarks(SessionProfilesView, FSessionFolders, FSessionBookmarkList, WinConfiguration->Bookmarks[FSessionKey]);
  735. LoadBookmarks(SharedProfilesView, FSharedFolders, FSharedBookmarkList, WinConfiguration->SharedBookmarks);
  736. #ifdef _DEBUG
  737. FSessionProfilesViewHandle = SessionProfilesView->Handle;
  738. FSharedProfilesViewHandle = SharedProfilesView->Handle;
  739. #endif
  740. }
  741. if (Mode == odAddBookmark)
  742. {
  743. AddAsBookmark(GetProfilesSheet(), true);
  744. }
  745. InstallPathWordBreakProc(LocalDirectoryEdit);
  746. InstallPathWordBreakProc(RemoteDirectoryEdit);
  747. FindProfile();
  748. if (OperationSide == osLocal)
  749. {
  750. ActiveControl = LocalDirectoryEdit;
  751. }
  752. else
  753. {
  754. ActiveControl = RemoteDirectoryEdit;
  755. }
  756. UpdateControls();
  757. }
  758. //---------------------------------------------------------------------------
  759. void __fastcall TLocationProfilesDialog::ProfilesViewKeyDown(TObject * Sender,
  760. WORD & Key, TShiftState /*Shift*/)
  761. {
  762. TTreeView * ProfilesView = GetProfilesView(Sender);
  763. if (!ProfilesView->IsEditing())
  764. {
  765. if ((ProfilesView->Selected != NULL) && (Key == VK_DELETE))
  766. {
  767. RemoveBookmark(Sender);
  768. Key = 0;
  769. }
  770. else if ((ProfilesView->Selected != NULL) && (Key == VK_F2))
  771. {
  772. RenameBookmark(Sender);
  773. Key = 0;
  774. }
  775. }
  776. }
  777. //---------------------------------------------------------------------------
  778. void __fastcall TLocationProfilesDialog::DirectoryEditChange(TObject * /*Sender*/)
  779. {
  780. if (!FChanging)
  781. {
  782. FindProfile();
  783. UpdateControls();
  784. }
  785. }
  786. //---------------------------------------------------------------------------
  787. void __fastcall TLocationProfilesDialog::ProfilesViewChange(
  788. TObject * /*Sender*/, TTreeNode * Node)
  789. {
  790. if (Node && Node->Data)
  791. {
  792. assert(!FChanging);
  793. FChanging = true;
  794. try
  795. {
  796. LocalDirectoryEdit->Text = ((TBookmark *)Node->Data)->Local;
  797. RemoteDirectoryEdit->Text = ((TBookmark *)Node->Data)->Remote;
  798. }
  799. __finally
  800. {
  801. FChanging = false;
  802. }
  803. // try to locate the same profile in the other set
  804. FindProfile();
  805. }
  806. UpdateControls();
  807. }
  808. //---------------------------------------------------------------------------
  809. void __fastcall TLocationProfilesDialog::BookmarkMoveToButtonClick(TObject * Sender)
  810. {
  811. TTreeView * ProfilesView = GetProfilesView(Sender);
  812. TStringList * Folders = GetFolders(Sender);
  813. assert(ProfilesView->Selected->Data);
  814. TBookmark * Bookmark = (TBookmark *)ProfilesView->Selected->Data;
  815. TBookmarkFolderDialog * Dialog = new TBookmarkFolderDialog(Folders);
  816. try
  817. {
  818. UnicodeString NodeName = Bookmark->Node;
  819. if (Dialog->Execute(NodeName) &&
  820. (NodeName != Bookmark->Node))
  821. {
  822. TTreeNode * FolderNode;
  823. int I = Folders->IndexOf(NodeName);
  824. if (NodeName.IsEmpty())
  825. {
  826. FolderNode = NULL;
  827. }
  828. else if (I >= 0)
  829. {
  830. FolderNode = dynamic_cast<TTreeNode *>(Folders->Objects[I]);
  831. assert(FolderNode);
  832. }
  833. else
  834. {
  835. I = Folders->Add(NodeName);
  836. TTreeNode * NextNode;
  837. // duplicated in RenameButtonClick()
  838. if (I < Folders->Count-1)
  839. {
  840. NextNode = dynamic_cast<TTreeNode *>(Folders->Objects[I+1]);
  841. assert(NextNode);
  842. }
  843. else if (Folders->Count > 1)
  844. {
  845. NextNode = (dynamic_cast<TTreeNode *>(Folders->Objects[I-1]))->getNextSibling();
  846. }
  847. else
  848. {
  849. assert(ProfilesView->Items->Count);
  850. NextNode = ProfilesView->Items->Item[0];
  851. }
  852. FolderNode = ProfilesView->Items->Insert(NextNode, NodeName);
  853. assert(FolderNode);
  854. Folders->Objects[I] = FolderNode;
  855. }
  856. BookmarkMove(Sender, ProfilesView->Selected, FolderNode);
  857. }
  858. }
  859. __finally
  860. {
  861. delete Dialog;
  862. }
  863. }
  864. //---------------------------------------------------------------------------
  865. void __fastcall TLocationProfilesDialog::RenameBookmark(TObject * Sender)
  866. {
  867. TTreeView * ProfilesView = GetProfilesView(Sender);
  868. assert(ProfilesView->Selected != NULL);
  869. if (ProfilesView->Selected != NULL)
  870. {
  871. ProfilesView->SetFocus();
  872. ProfilesView->Selected->EditText();
  873. }
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TLocationProfilesDialog::RenameBookmarkButtonClick(TObject * Sender)
  877. {
  878. RenameBookmark(Sender);
  879. }
  880. //---------------------------------------------------------------------------
  881. void __fastcall TLocationProfilesDialog::ProfilesViewGetImageIndex(
  882. TObject * /*Sender*/, TTreeNode * Node)
  883. {
  884. Node->ImageIndex = Node->Data ? 0 : (Node->Expanded ? 1 : 2);
  885. }
  886. //---------------------------------------------------------------------------
  887. void __fastcall TLocationProfilesDialog::ProfilesViewGetSelectedIndex(
  888. TObject * /*Sender*/, TTreeNode * Node)
  889. {
  890. Node->SelectedIndex = Node->Data ? 0 : (Node->Expanded ? 1 : 2);
  891. }
  892. //---------------------------------------------------------------------------
  893. void __fastcall TLocationProfilesDialog::LocalDirectoryBrowseButtonClick(
  894. TObject * /*Sender*/)
  895. {
  896. UnicodeString Directory = LocalDirectoryEdit->Text;
  897. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  898. {
  899. LocalDirectoryEdit->Text = Directory;
  900. DirectoryEditChange(LocalDirectoryEdit);
  901. }
  902. }
  903. //---------------------------------------------------------------------------
  904. void __fastcall TLocationProfilesDialog::SwitchButtonClick(TObject * /*Sender*/)
  905. {
  906. WinConfiguration->UseLocationProfiles = false;
  907. }
  908. //---------------------------------------------------------------------------
  909. void __fastcall TLocationProfilesDialog::HelpButtonClick(TObject * /*Sender*/)
  910. {
  911. FormHelp(this);
  912. }
  913. //---------------------------------------------------------------------------
  914. void __fastcall TLocationProfilesDialog::ProfilesViewCollapsed(
  915. TObject * Sender, TTreeNode * Node)
  916. {
  917. assert(Node != NULL);
  918. assert(Node->Data == NULL);
  919. GetBookmarkList(Sender)->NodeOpened[Node->Text] = false;
  920. }
  921. //---------------------------------------------------------------------------
  922. void __fastcall TLocationProfilesDialog::ProfilesViewExpanded(
  923. TObject * Sender, TTreeNode * Node)
  924. {
  925. assert(Node != NULL);
  926. assert(Node->Data == NULL);
  927. GetBookmarkList(Sender)->NodeOpened[Node->Text] = true;
  928. }
  929. //---------------------------------------------------------------------------
  930. void __fastcall TLocationProfilesDialog::ProfilesViewEdited(
  931. TObject * Sender, TTreeNode * Node, UnicodeString & S)
  932. {
  933. TTreeView * ProfilesView = GetProfilesView(Sender);
  934. TStringList * Folders = GetFolders(Sender);
  935. if (Node->Data != NULL)
  936. {
  937. BookmarkNameValidateName(S);
  938. // raises exception in case of duplicate name??
  939. ((TBookmark *)Node->Data)->Name = S;
  940. }
  941. else
  942. {
  943. BookmarkFolderValidateName(S, false);
  944. if (S.IsEmpty())
  945. {
  946. throw Exception(FMTLOAD(BOOKMARK_FOLDER_INVALID_NAME, (S)));
  947. }
  948. if ((Folders->IndexOf(S) >= 0) && AnsiCompareText(S, Node->Text))
  949. {
  950. throw Exception(FMTLOAD(DUPLICATE_BOOKMARK_FOLDER, (S)));
  951. }
  952. assert(Node->Count);
  953. Folders->Delete(Folders->IndexOf(Node->Text));
  954. int I = Folders->AddObject(S, Node);
  955. TTreeNode * NextNode;
  956. // duplicated in MoveToButtonClick()
  957. if (I < Folders->Count-1)
  958. {
  959. NextNode = dynamic_cast<TTreeNode *>(Folders->Objects[I+1]);
  960. assert(NextNode);
  961. }
  962. else if (Folders->Count > 1)
  963. {
  964. NextNode = (dynamic_cast<TTreeNode *>(Folders->Objects[I-1]))->getNextSibling();
  965. }
  966. else
  967. {
  968. assert(ProfilesView->Items->Count);
  969. NextNode = ProfilesView->Items->Item[0];
  970. }
  971. if (NextNode != Node)
  972. {
  973. Node->MoveTo(NextNode, NextNode ? naInsert : naAddChild);
  974. }
  975. for (int i = 0; i < Node->Count; i++)
  976. {
  977. ((TBookmark *)Node->Item[i]->Data)->Node = S;
  978. }
  979. Node->MakeVisible();
  980. }
  981. }
  982. //---------------------------------------------------------------------------
  983. void __fastcall TLocationProfilesDialog::ProfilesViewEditing(
  984. TObject * /*Sender*/, TTreeNode * /*Node*/, bool & /*AllowEdit*/)
  985. {
  986. OKBtn->Default = false;
  987. CancelBtn->Cancel = false;
  988. }
  989. //---------------------------------------------------------------------------
  990. void __fastcall TLocationProfilesDialog::UpdateActions()
  991. {
  992. TForm::UpdateActions();
  993. if ((!OKBtn->Default || !CancelBtn->Cancel) &&
  994. !GetProfilesView(PageControl->ActivePage)->IsEditing())
  995. {
  996. OKBtn->Default = true;
  997. CancelBtn->Cancel = true;
  998. }
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TLocationProfilesDialog::ProfilesViewEndDrag(
  1002. TObject * Sender, TObject * /*Target*/, int /*X*/, int /*Y*/)
  1003. {
  1004. GetScrollOnDragOver(Sender)->EndDrag();
  1005. }
  1006. //---------------------------------------------------------------------------
  1007. void __fastcall TLocationProfilesDialog::ShortCutBookmarkButtonClick(
  1008. TObject * Sender)
  1009. {
  1010. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  1011. TTreeView * ProfilesView = GetProfilesView(Sender);
  1012. assert(ProfilesView->Selected != NULL);
  1013. TTreeNode * Node = ProfilesView->Selected;
  1014. assert(Node->Data != NULL);
  1015. TBookmark * Bookmark = static_cast<TBookmark *>(Node->Data);
  1016. TShortCuts ShortCuts;
  1017. WinConfiguration->CustomCommandList->ShortCuts(ShortCuts);
  1018. BookmarkList->ShortCuts(ShortCuts);
  1019. TShortCut ShortCut = Bookmark->ShortCut;
  1020. if (DoShortCutDialog(ShortCut, ShortCuts, HelpKeyword))
  1021. {
  1022. Bookmark->ShortCut = ShortCut;
  1023. Node->Text = BookmarkText(Bookmark);
  1024. UpdateControls();
  1025. }
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. UnicodeString __fastcall TLocationProfilesDialog::BookmarkText(TBookmark * Bookmark)
  1029. {
  1030. UnicodeString Result = Bookmark->Name;
  1031. if (!Result.IsEmpty() && (Bookmark->ShortCut != 0))
  1032. {
  1033. Result = FORMAT(L"%s (%s)", (Result, ShortCutToText(Bookmark->ShortCut)));
  1034. }
  1035. return Result;
  1036. }
  1037. //---------------------------------------------------------------------------