LocationProfiles.cpp 36 KB

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