Bookmarks.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. int I = FBookmarkLists->IndexOf(Index);
  207. if (I >= 0)
  208. {
  209. return dynamic_cast<TBookmarkList *>(FBookmarkLists->Objects[I]);
  210. }
  211. else
  212. {
  213. return NULL;
  214. }
  215. }
  216. //---------------------------------------------------------------------------
  217. void __fastcall TBookmarks::SetBookmarks(AnsiString Index, TBookmarkList * value)
  218. {
  219. int I = FBookmarkLists->IndexOf(Index);
  220. if (I >= 0)
  221. {
  222. TBookmarkList * BookmarkList;
  223. BookmarkList = dynamic_cast<TBookmarkList *>(FBookmarkLists->Objects[I]);
  224. BookmarkList->Assign(value);
  225. }
  226. else
  227. {
  228. TBookmarkList * BookmarkList = new TBookmarkList();
  229. BookmarkList->Assign(value);
  230. FBookmarkLists->AddObject(Index, BookmarkList);
  231. }
  232. }
  233. //---------------------------------------------------------------------------
  234. //---------------------------------------------------------------------------
  235. __fastcall TBookmarkList::TBookmarkList(): TPersistent()
  236. {
  237. FModified = false;
  238. FBookmarks = new TStringList();
  239. FBookmarks->CaseSensitive = false;
  240. FOpenedNodes = new TStringList();
  241. FOpenedNodes->CaseSensitive = false;
  242. FOpenedNodes->Sorted = true;
  243. }
  244. //---------------------------------------------------------------------------
  245. __fastcall TBookmarkList::~TBookmarkList()
  246. {
  247. Clear();
  248. SAFE_DESTROY(FBookmarks);
  249. SAFE_DESTROY(FOpenedNodes);
  250. }
  251. //---------------------------------------------------------------------------
  252. void __fastcall TBookmarkList::Clear()
  253. {
  254. for (int i = 0; i < FBookmarks->Count; i++)
  255. {
  256. delete FBookmarks->Objects[i];
  257. }
  258. FBookmarks->Clear();
  259. FOpenedNodes->Clear();
  260. }
  261. //---------------------------------------------------------------------------
  262. void __fastcall TBookmarkList::Assign(TPersistent * Source)
  263. {
  264. TBookmarkList * SourceList;
  265. SourceList = dynamic_cast<TBookmarkList *>(Source);
  266. if (SourceList)
  267. {
  268. Clear();
  269. for (int i = 0; i < SourceList->FBookmarks->Count; i++)
  270. {
  271. TBookmark * Bookmark = new TBookmark();
  272. Bookmark->Assign(dynamic_cast<TBookmark *>(SourceList->FBookmarks->Objects[i]));
  273. Add(Bookmark);
  274. }
  275. FOpenedNodes->Assign(SourceList->FOpenedNodes);
  276. Modified = SourceList->Modified;
  277. }
  278. else
  279. {
  280. TPersistent::Assign(Source);
  281. }
  282. }
  283. //---------------------------------------------------------------------------
  284. void __fastcall TBookmarkList::LoadOptions(THierarchicalStorage * Storage)
  285. {
  286. FOpenedNodes->CommaText = Storage->ReadString("OpenedNodes", "");
  287. }
  288. //---------------------------------------------------------------------------
  289. void __fastcall TBookmarkList::SaveOptions(THierarchicalStorage * Storage)
  290. {
  291. Storage->WriteString("OpenedNodes", FOpenedNodes->CommaText);
  292. }
  293. //---------------------------------------------------------------------------
  294. void __fastcall TBookmarkList::Add(TBookmark * Bookmark)
  295. {
  296. Insert(Count, Bookmark);
  297. }
  298. //---------------------------------------------------------------------------
  299. void __fastcall TBookmarkList::InsertBefore(TBookmark * BeforeBookmark, TBookmark * Bookmark)
  300. {
  301. assert(BeforeBookmark);
  302. int I = FBookmarks->IndexOf(BeforeBookmark->Key);
  303. assert(I >= 0);
  304. Insert(I, Bookmark);
  305. }
  306. //---------------------------------------------------------------------------
  307. void __fastcall TBookmarkList::MoveTo(TBookmark * ToBookmark,
  308. TBookmark * Bookmark, bool Before)
  309. {
  310. assert(ToBookmark != NULL);
  311. int NewIndex = FBookmarks->IndexOf(ToBookmark->Key);
  312. assert(Bookmark != NULL);
  313. int OldIndex = FBookmarks->IndexOf(Bookmark->Key);
  314. if (Before && (NewIndex > OldIndex))
  315. {
  316. // otherwise item is moved after the item in the target index
  317. NewIndex--;
  318. }
  319. else if (!Before && (NewIndex < OldIndex))
  320. {
  321. NewIndex++;
  322. }
  323. FModified = true;
  324. FBookmarks->Move(OldIndex, NewIndex);
  325. }
  326. //---------------------------------------------------------------------------
  327. void __fastcall TBookmarkList::Insert(int Index, TBookmark * Bookmark)
  328. {
  329. assert(Bookmark);
  330. assert(!Bookmark->FOwner);
  331. assert(!Bookmark->Name.IsEmpty());
  332. FModified = true;
  333. Bookmark->FOwner = this;
  334. if (FBookmarks->IndexOf(Bookmark->Key) >= 0)
  335. {
  336. throw Exception(FMTLOAD(DUPLICATE_BOOKMARK, (Bookmark->Name)));
  337. }
  338. FBookmarks->InsertObject(Index, Bookmark->Key, Bookmark);
  339. }
  340. //---------------------------------------------------------------------------
  341. void __fastcall TBookmarkList::Delete(TBookmark * Bookmark)
  342. {
  343. assert(Bookmark);
  344. assert(Bookmark->FOwner == this);
  345. int I = IndexOf(Bookmark);
  346. assert(I >= 0);
  347. FModified = true;
  348. Bookmark->FOwner = NULL;
  349. FBookmarks->Delete(I);
  350. }
  351. //---------------------------------------------------------------------------
  352. int __fastcall TBookmarkList::IndexOf(TBookmark * Bookmark)
  353. {
  354. return FBookmarks->IndexOf(Bookmark->Key);
  355. }
  356. //---------------------------------------------------------------------------
  357. void __fastcall TBookmarkList::KeyChanged(int Index)
  358. {
  359. assert(Index < Count);
  360. TBookmark * Bookmark = dynamic_cast<TBookmark *>(FBookmarks->Objects[Index]);
  361. assert(FBookmarks->Strings[Index] != Bookmark->Key);
  362. if (FBookmarks->IndexOf(Bookmark->Key) >= 0)
  363. {
  364. throw Exception(FMTLOAD(DUPLICATE_BOOKMARK, (Bookmark->Name)));
  365. }
  366. FBookmarks->Strings[Index] = Bookmark->Key;
  367. }
  368. //---------------------------------------------------------------------------
  369. TBookmark * __fastcall TBookmarkList::FindByName(const AnsiString Node, const AnsiString Name)
  370. {
  371. int I = FBookmarks->IndexOf(TBookmark::BookmarkKey(Node, Name));
  372. TBookmark * Bookmark = I >= 0 ? dynamic_cast<TBookmark *>(FBookmarks->Objects[I]) : NULL;
  373. assert(!Bookmark || (Bookmark->Node == Node && Bookmark->Name == Name));
  374. return Bookmark;
  375. }
  376. //---------------------------------------------------------------------------
  377. int __fastcall TBookmarkList::GetCount()
  378. {
  379. return FBookmarks->Count;
  380. }
  381. //---------------------------------------------------------------------------
  382. TBookmark * __fastcall TBookmarkList::GetBookmarks(int Index)
  383. {
  384. TBookmark * Bookmark = dynamic_cast<TBookmark *>(FBookmarks->Objects[Index]);
  385. assert(Bookmark);
  386. return Bookmark;
  387. }
  388. //---------------------------------------------------------------------------
  389. bool __fastcall TBookmarkList::GetNodeOpened(AnsiString Index)
  390. {
  391. return (FOpenedNodes->IndexOf(Index) >= 0);
  392. }
  393. //---------------------------------------------------------------------------
  394. void __fastcall TBookmarkList::SetNodeOpened(AnsiString Index, bool value)
  395. {
  396. int I = FOpenedNodes->IndexOf(Index);
  397. if ((I >= 0) != value)
  398. {
  399. if (value)
  400. {
  401. FOpenedNodes->Add(Index);
  402. }
  403. else
  404. {
  405. FOpenedNodes->Delete(I);
  406. }
  407. FModified = true;
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. //---------------------------------------------------------------------------
  412. __fastcall TBookmark::TBookmark()
  413. {
  414. FOwner = NULL;
  415. }
  416. //---------------------------------------------------------------------------
  417. void __fastcall TBookmark::Assign(TPersistent * Source)
  418. {
  419. TBookmark * SourceBookmark;
  420. SourceBookmark = dynamic_cast<TBookmark *>(Source);
  421. if (SourceBookmark)
  422. {
  423. Name = SourceBookmark->Name;
  424. Local = SourceBookmark->Local;
  425. Remote = SourceBookmark->Remote;
  426. Node = SourceBookmark->Node;
  427. }
  428. else
  429. {
  430. TPersistent::Assign(Source);
  431. }
  432. }
  433. //---------------------------------------------------------------------------
  434. void __fastcall TBookmark::SetName(const AnsiString value)
  435. {
  436. if (Name != value)
  437. {
  438. int OldIndex = FOwner ? FOwner->IndexOf(this) : -1;
  439. AnsiString OldName = FName;
  440. FName = value;
  441. try
  442. {
  443. Modify(OldIndex);
  444. }
  445. catch(...)
  446. {
  447. FName = OldName;
  448. throw;
  449. }
  450. }
  451. }
  452. //---------------------------------------------------------------------------
  453. void __fastcall TBookmark::SetLocal(const AnsiString value)
  454. {
  455. if (Local != value)
  456. {
  457. FLocal = value;
  458. Modify(-1);
  459. }
  460. }
  461. //---------------------------------------------------------------------------
  462. void __fastcall TBookmark::SetRemote(const AnsiString value)
  463. {
  464. if (Remote != value)
  465. {
  466. FRemote = value;
  467. Modify(-1);
  468. }
  469. }
  470. //---------------------------------------------------------------------------
  471. void __fastcall TBookmark::SetNode(const AnsiString value)
  472. {
  473. if (Node != value)
  474. {
  475. int OldIndex = FOwner ? FOwner->IndexOf(this) : -1;
  476. FNode = value;
  477. Modify(OldIndex);
  478. }
  479. }
  480. //---------------------------------------------------------------------------
  481. void __fastcall TBookmark::Modify(int OldIndex)
  482. {
  483. if (FOwner)
  484. {
  485. FOwner->Modified = true;
  486. if (OldIndex >= 0)
  487. {
  488. FOwner->KeyChanged(OldIndex);
  489. }
  490. }
  491. }
  492. //---------------------------------------------------------------------------
  493. AnsiString __fastcall TBookmark::BookmarkKey(const AnsiString Node, const AnsiString Name)
  494. {
  495. return FORMAT("%s\1%s", (Node, Name));
  496. }
  497. //---------------------------------------------------------------------------
  498. AnsiString __fastcall TBookmark::GetKey()
  499. {
  500. return BookmarkKey(Node, Name);
  501. }