LocationProfiles.cpp 35 KB

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