1
0

LocationProfiles.cpp 36 KB

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