LocationProfiles.cpp 36 KB

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