LocationProfiles.cpp 36 KB

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