Bookmarks.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "Bookmarks.h"
  6. #include "HierarchicalStorage.h"
  7. #include "TextsCore.h"
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. //---------------------------------------------------------------------------
  11. #define IS_NUMBER(STR) (StrToIntDef(STR, -123) != -123)
  12. //---------------------------------------------------------------------------
  13. __fastcall TBookmarks::TBookmarks(): TObject()
  14. {
  15. FBookmarkLists = new TStringList();
  16. FBookmarkLists->Sorted = true;
  17. FBookmarkLists->CaseSensitive = false;
  18. FBookmarkLists->Duplicates = dupError;
  19. }
  20. //---------------------------------------------------------------------------
  21. __fastcall TBookmarks::~TBookmarks()
  22. {
  23. Clear();
  24. SAFE_DESTROY(FBookmarkLists);
  25. }
  26. //---------------------------------------------------------------------------
  27. void __fastcall TBookmarks::Clear()
  28. {
  29. for (int i = 0; i < FBookmarkLists->Count; i++)
  30. {
  31. delete FBookmarkLists->Objects[i];
  32. }
  33. FBookmarkLists->Clear();
  34. }
  35. //---------------------------------------------------------------------------
  36. AnsiString TBookmarks::Keys[] = { "Local", "Remote", "Options" };
  37. //---------------------------------------------------------------------------
  38. void __fastcall TBookmarks::Load(THierarchicalStorage * Storage)
  39. {
  40. for (int i = 0; i <= 2; i++)
  41. {
  42. if (Storage->OpenSubKey(Keys[i], false))
  43. {
  44. TStrings * BookmarkKeys = new TStringList();
  45. try
  46. {
  47. Storage->GetSubKeyNames(BookmarkKeys);
  48. for (int Index = 0; Index < BookmarkKeys->Count; Index++)
  49. {
  50. AnsiString Key = BookmarkKeys->Strings[Index];
  51. if (Storage->OpenSubKey(Key, false))
  52. {
  53. TBookmarkList * BookmarkList = Bookmarks[Key];
  54. if (!BookmarkList)
  55. {
  56. BookmarkList = new TBookmarkList();
  57. FBookmarkLists->AddObject(Key, BookmarkList);
  58. }
  59. if (i < 2)
  60. {
  61. LoadLevel(Storage, "", (i == 0), BookmarkList);
  62. }
  63. else
  64. {
  65. BookmarkList->LoadOptions(Storage);
  66. }
  67. Storage->CloseSubKey();
  68. }
  69. }
  70. }
  71. __finally
  72. {
  73. delete BookmarkKeys;
  74. }
  75. Storage->CloseSubKey();
  76. }
  77. }
  78. ModifyAll(false);
  79. }
  80. //---------------------------------------------------------------------------
  81. void __fastcall TBookmarks::LoadLevel(THierarchicalStorage * Storage, const AnsiString Key,
  82. bool Local, TBookmarkList * BookmarkList)
  83. {
  84. TStrings * Names = new TStringList();
  85. try
  86. {
  87. Storage->GetValueNames(Names);
  88. AnsiString Name, Directory;
  89. for (int i = 0; i < Names->Count; i++)
  90. {
  91. Name = Names->Strings[i];
  92. Directory = Storage->ReadString(Name, "");
  93. TBookmark * Bookmark;
  94. if (IS_NUMBER(Name))
  95. {
  96. Name = Directory;
  97. }
  98. if (!Name.IsEmpty())
  99. {
  100. Bookmark = BookmarkList->FindByName(Key, Name);
  101. bool New;
  102. New = (Bookmark == NULL);
  103. if (New)
  104. {
  105. Bookmark = new TBookmark();
  106. Bookmark->Node = Key;
  107. Bookmark->Name = Name;
  108. }
  109. if (Local)
  110. {
  111. Bookmark->Local = Directory;
  112. }
  113. else
  114. {
  115. Bookmark->Remote = Directory;
  116. }
  117. if (New)
  118. {
  119. BookmarkList->Add(Bookmark);
  120. }
  121. }
  122. }
  123. Storage->GetSubKeyNames(Names);
  124. for (int i = 0; i < Names->Count; i++)
  125. {
  126. Name = Names->Strings[i];
  127. if (Storage->OpenSubKey(Name, false))
  128. {
  129. LoadLevel(Storage, Key + (Key.IsEmpty() ? "" : "/") + Name, Local, BookmarkList);
  130. Storage->CloseSubKey();
  131. }
  132. }
  133. }
  134. __finally
  135. {
  136. delete Names;
  137. }
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TBookmarks::Save(THierarchicalStorage * Storage, bool All)
  141. {
  142. for (int i = 0; i <= 2; i++)
  143. {
  144. if (Storage->OpenSubKey(Keys[i], true))
  145. {
  146. for (int Index = 0; Index < FBookmarkLists->Count; Index++)
  147. {
  148. TBookmarkList * BookmarkList = dynamic_cast<TBookmarkList *>(FBookmarkLists->Objects[Index]);
  149. if (All || BookmarkList->Modified)
  150. {
  151. AnsiString Key;
  152. Key = FBookmarkLists->Strings[Index];
  153. Storage->RecursiveDeleteSubKey(Key);
  154. if (Storage->OpenSubKey(Key, true))
  155. {
  156. if (i < 2)
  157. {
  158. for (int IndexB = 0; IndexB < BookmarkList->Count; IndexB++)
  159. {
  160. TBookmark * Bookmark = BookmarkList->Bookmarks[IndexB];
  161. AnsiString Directory = (i == 0) ? Bookmark->Local : Bookmark->Remote;
  162. if (!Bookmark->Node.IsEmpty())
  163. {
  164. if (Storage->OpenSubKey(Bookmark->Node, true))
  165. {
  166. Storage->WriteString(Bookmark->Name, Directory);
  167. Storage->CloseSubKey();
  168. }
  169. }
  170. else
  171. {
  172. Storage->WriteString(Bookmark->Name, Directory);
  173. }
  174. }
  175. }
  176. else
  177. {
  178. BookmarkList->SaveOptions(Storage);
  179. }
  180. Storage->CloseSubKey();
  181. }
  182. }
  183. }
  184. Storage->CloseSubKey();
  185. }
  186. }
  187. if (!All)
  188. {
  189. ModifyAll(false);
  190. }
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall TBookmarks::ModifyAll(bool Modify)
  194. {
  195. TBookmarkList * BookmarkList;
  196. for (int i = 0; i < FBookmarkLists->Count; i++)
  197. {
  198. BookmarkList = dynamic_cast<TBookmarkList *>(FBookmarkLists->Objects[i]);
  199. assert(BookmarkList);
  200. BookmarkList->Modified = Modify;
  201. }
  202. }
  203. //---------------------------------------------------------------------------
  204. TBookmarkList * __fastcall TBookmarks::GetBookmarks(AnsiString Index)
  205. {
  206. Index = SimpleMungeStr(Index);
  207. int I = FBookmarkLists->IndexOf(Index);
  208. if (I >= 0)
  209. {
  210. return dynamic_cast<TBookmarkList *>(FBookmarkLists->Objects[I]);
  211. }
  212. else
  213. {
  214. return NULL;
  215. }
  216. }
  217. //---------------------------------------------------------------------------
  218. void __fastcall TBookmarks::SetBookmarks(AnsiString Index, TBookmarkList * value)
  219. {
  220. Index = SimpleMungeStr(Index);
  221. int I = FBookmarkLists->IndexOf(Index);
  222. if (I >= 0)
  223. {
  224. TBookmarkList * BookmarkList;
  225. BookmarkList = dynamic_cast<TBookmarkList *>(FBookmarkLists->Objects[I]);
  226. BookmarkList->Assign(value);
  227. }
  228. else
  229. {
  230. TBookmarkList * BookmarkList = new TBookmarkList();
  231. BookmarkList->Assign(value);
  232. FBookmarkLists->AddObject(Index, BookmarkList);
  233. }
  234. }
  235. //---------------------------------------------------------------------------
  236. //---------------------------------------------------------------------------
  237. __fastcall TBookmarkList::TBookmarkList(): TPersistent()
  238. {
  239. FModified = false;
  240. FBookmarks = new TStringList();
  241. FBookmarks->CaseSensitive = false;
  242. FOpenedNodes = new TStringList();
  243. FOpenedNodes->CaseSensitive = false;
  244. FOpenedNodes->Sorted = true;
  245. }
  246. //---------------------------------------------------------------------------
  247. __fastcall TBookmarkList::~TBookmarkList()
  248. {
  249. Clear();
  250. SAFE_DESTROY(FBookmarks);
  251. SAFE_DESTROY(FOpenedNodes);
  252. }
  253. //---------------------------------------------------------------------------
  254. void __fastcall TBookmarkList::Clear()
  255. {
  256. for (int i = 0; i < FBookmarks->Count; i++)
  257. {
  258. delete FBookmarks->Objects[i];
  259. }
  260. FBookmarks->Clear();
  261. FOpenedNodes->Clear();
  262. }
  263. //---------------------------------------------------------------------------
  264. void __fastcall TBookmarkList::Assign(TPersistent * Source)
  265. {
  266. TBookmarkList * SourceList;
  267. SourceList = dynamic_cast<TBookmarkList *>(Source);
  268. if (SourceList)
  269. {
  270. Clear();
  271. for (int i = 0; i < SourceList->FBookmarks->Count; i++)
  272. {
  273. TBookmark * Bookmark = new TBookmark();
  274. Bookmark->Assign(dynamic_cast<TBookmark *>(SourceList->FBookmarks->Objects[i]));
  275. Add(Bookmark);
  276. }
  277. FOpenedNodes->Assign(SourceList->FOpenedNodes);
  278. Modified = SourceList->Modified;
  279. }
  280. else
  281. {
  282. TPersistent::Assign(Source);
  283. }
  284. }
  285. //---------------------------------------------------------------------------
  286. void __fastcall TBookmarkList::LoadOptions(THierarchicalStorage * Storage)
  287. {
  288. FOpenedNodes->CommaText = Storage->ReadString("OpenedNodes", "");
  289. }
  290. //---------------------------------------------------------------------------
  291. void __fastcall TBookmarkList::SaveOptions(THierarchicalStorage * Storage)
  292. {
  293. Storage->WriteString("OpenedNodes", FOpenedNodes->CommaText);
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TBookmarkList::Add(TBookmark * Bookmark)
  297. {
  298. Insert(Count, Bookmark);
  299. }
  300. //---------------------------------------------------------------------------
  301. void __fastcall TBookmarkList::InsertBefore(TBookmark * BeforeBookmark, TBookmark * Bookmark)
  302. {
  303. assert(BeforeBookmark);
  304. int I = FBookmarks->IndexOf(BeforeBookmark->Key);
  305. assert(I >= 0);
  306. Insert(I, Bookmark);
  307. }
  308. //---------------------------------------------------------------------------
  309. void __fastcall TBookmarkList::MoveTo(TBookmark * ToBookmark,
  310. TBookmark * Bookmark, bool Before)
  311. {
  312. assert(ToBookmark != NULL);
  313. int NewIndex = FBookmarks->IndexOf(ToBookmark->Key);
  314. assert(Bookmark != NULL);
  315. int OldIndex = FBookmarks->IndexOf(Bookmark->Key);
  316. if (Before && (NewIndex > OldIndex))
  317. {
  318. // otherwise item is moved after the item in the target index
  319. NewIndex--;
  320. }
  321. else if (!Before && (NewIndex < OldIndex))
  322. {
  323. NewIndex++;
  324. }
  325. FModified = true;
  326. FBookmarks->Move(OldIndex, NewIndex);
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TBookmarkList::Insert(int Index, TBookmark * Bookmark)
  330. {
  331. assert(Bookmark);
  332. assert(!Bookmark->FOwner);
  333. assert(!Bookmark->Name.IsEmpty());
  334. FModified = true;
  335. Bookmark->FOwner = this;
  336. if (FBookmarks->IndexOf(Bookmark->Key) >= 0)
  337. {
  338. throw Exception(FMTLOAD(DUPLICATE_BOOKMARK, (Bookmark->Name)));
  339. }
  340. FBookmarks->InsertObject(Index, Bookmark->Key, Bookmark);
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TBookmarkList::Delete(TBookmark * Bookmark)
  344. {
  345. assert(Bookmark);
  346. assert(Bookmark->FOwner == this);
  347. int I = IndexOf(Bookmark);
  348. assert(I >= 0);
  349. FModified = true;
  350. Bookmark->FOwner = NULL;
  351. FBookmarks->Delete(I);
  352. }
  353. //---------------------------------------------------------------------------
  354. int __fastcall TBookmarkList::IndexOf(TBookmark * Bookmark)
  355. {
  356. return FBookmarks->IndexOf(Bookmark->Key);
  357. }
  358. //---------------------------------------------------------------------------
  359. void __fastcall TBookmarkList::KeyChanged(int Index)
  360. {
  361. assert(Index < Count);
  362. TBookmark * Bookmark = dynamic_cast<TBookmark *>(FBookmarks->Objects[Index]);
  363. assert(FBookmarks->Strings[Index] != Bookmark->Key);
  364. if (FBookmarks->IndexOf(Bookmark->Key) >= 0)
  365. {
  366. throw Exception(FMTLOAD(DUPLICATE_BOOKMARK, (Bookmark->Name)));
  367. }
  368. FBookmarks->Strings[Index] = Bookmark->Key;
  369. }
  370. //---------------------------------------------------------------------------
  371. TBookmark * __fastcall TBookmarkList::FindByName(const AnsiString Node, const AnsiString Name)
  372. {
  373. int I = FBookmarks->IndexOf(TBookmark::BookmarkKey(Node, Name));
  374. TBookmark * Bookmark = I >= 0 ? dynamic_cast<TBookmark *>(FBookmarks->Objects[I]) : NULL;
  375. assert(!Bookmark || (Bookmark->Node == Node && Bookmark->Name == Name));
  376. return Bookmark;
  377. }
  378. //---------------------------------------------------------------------------
  379. int __fastcall TBookmarkList::GetCount()
  380. {
  381. return FBookmarks->Count;
  382. }
  383. //---------------------------------------------------------------------------
  384. TBookmark * __fastcall TBookmarkList::GetBookmarks(int Index)
  385. {
  386. TBookmark * Bookmark = dynamic_cast<TBookmark *>(FBookmarks->Objects[Index]);
  387. assert(Bookmark);
  388. return Bookmark;
  389. }
  390. //---------------------------------------------------------------------------
  391. bool __fastcall TBookmarkList::GetNodeOpened(AnsiString Index)
  392. {
  393. return (FOpenedNodes->IndexOf(Index) >= 0);
  394. }
  395. //---------------------------------------------------------------------------
  396. void __fastcall TBookmarkList::SetNodeOpened(AnsiString Index, bool value)
  397. {
  398. int I = FOpenedNodes->IndexOf(Index);
  399. if ((I >= 0) != value)
  400. {
  401. if (value)
  402. {
  403. FOpenedNodes->Add(Index);
  404. }
  405. else
  406. {
  407. FOpenedNodes->Delete(I);
  408. }
  409. FModified = true;
  410. }
  411. }
  412. //---------------------------------------------------------------------------
  413. //---------------------------------------------------------------------------
  414. __fastcall TBookmark::TBookmark()
  415. {
  416. FOwner = NULL;
  417. }
  418. //---------------------------------------------------------------------------
  419. void __fastcall TBookmark::Assign(TPersistent * Source)
  420. {
  421. TBookmark * SourceBookmark;
  422. SourceBookmark = dynamic_cast<TBookmark *>(Source);
  423. if (SourceBookmark)
  424. {
  425. Name = SourceBookmark->Name;
  426. Local = SourceBookmark->Local;
  427. Remote = SourceBookmark->Remote;
  428. Node = SourceBookmark->Node;
  429. }
  430. else
  431. {
  432. TPersistent::Assign(Source);
  433. }
  434. }
  435. //---------------------------------------------------------------------------
  436. void __fastcall TBookmark::SetName(const AnsiString value)
  437. {
  438. if (Name != value)
  439. {
  440. int OldIndex = FOwner ? FOwner->IndexOf(this) : -1;
  441. AnsiString OldName = FName;
  442. FName = value;
  443. try
  444. {
  445. Modify(OldIndex);
  446. }
  447. catch(...)
  448. {
  449. FName = OldName;
  450. throw;
  451. }
  452. }
  453. }
  454. //---------------------------------------------------------------------------
  455. void __fastcall TBookmark::SetLocal(const AnsiString value)
  456. {
  457. if (Local != value)
  458. {
  459. FLocal = value;
  460. Modify(-1);
  461. }
  462. }
  463. //---------------------------------------------------------------------------
  464. void __fastcall TBookmark::SetRemote(const AnsiString value)
  465. {
  466. if (Remote != value)
  467. {
  468. FRemote = value;
  469. Modify(-1);
  470. }
  471. }
  472. //---------------------------------------------------------------------------
  473. void __fastcall TBookmark::SetNode(const AnsiString value)
  474. {
  475. if (Node != value)
  476. {
  477. int OldIndex = FOwner ? FOwner->IndexOf(this) : -1;
  478. FNode = value;
  479. Modify(OldIndex);
  480. }
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TBookmark::Modify(int OldIndex)
  484. {
  485. if (FOwner)
  486. {
  487. FOwner->Modified = true;
  488. if (OldIndex >= 0)
  489. {
  490. FOwner->KeyChanged(OldIndex);
  491. }
  492. }
  493. }
  494. //---------------------------------------------------------------------------
  495. AnsiString __fastcall TBookmark::BookmarkKey(const AnsiString Node, const AnsiString Name)
  496. {
  497. return FORMAT("%s\1%s", (Node, Name));
  498. }
  499. //---------------------------------------------------------------------------
  500. AnsiString __fastcall TBookmark::GetKey()
  501. {
  502. return BookmarkKey(Node, Name);
  503. }