bookcollection.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. unit bookCollection;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, Sysutils, Book;
  6. type
  7. { TBookCollection }
  8. TBookCollection = class(TObject)
  9. private
  10. mList : TFPList;
  11. function Get(Index: Integer): TBook;
  12. public
  13. procedure StoreData(path: String);
  14. procedure LoadData(path: String; parent: TComponent);
  15. procedure AddBook(book: TBook);
  16. property Books[Index: Integer]:TBook read Get;
  17. procedure Remove(book: TBook);
  18. function Count: Integer;
  19. procedure Clear;
  20. procedure SwapBooks(Source, Dest: Integer);
  21. procedure SortByTitle;
  22. procedure SortByAuthor;
  23. constructor Create;
  24. destructor Destroy; override;
  25. end;
  26. implementation
  27. uses
  28. unitCoverWorker, LazUTF8;
  29. { TBookCollection }
  30. procedure TBookCollection.Clear;
  31. var
  32. i : Integer;
  33. book : TBook;
  34. begin
  35. CoverWorkerStop;
  36. for i := mList.Count - 1 downto 0 do
  37. begin
  38. book := TBook(mList[i]);
  39. // Explicitly free the cover control to avoid orphaned images
  40. if Assigned(book) and Assigned(book.Cover) then
  41. book.Cover.Free;
  42. CoverWorkerRemoveBook(book);
  43. CoverWorkerUnregisterBook(book);
  44. book.Free; // free the book itself
  45. end;
  46. mList.Clear;
  47. end;
  48. function TBookCollection.Get(Index: Integer): TBook;
  49. begin
  50. Result := TBook(mList.Items[index]);
  51. End;
  52. procedure TBookCollection.AddBook(Book: TBook);
  53. begin
  54. mList.Add(book);
  55. End;
  56. procedure TBookCollection.Remove(Book: TBook);
  57. begin
  58. if book <> nil then
  59. begin
  60. CoverWorkerRemoveBook(book);
  61. CoverWorkerUnregisterBook(book);
  62. end;
  63. mList.Remove(book);
  64. end;
  65. function TBookCollection.Count: Integer;
  66. begin
  67. result:=mList.Count;
  68. end;
  69. procedure TBookCollection.SwapBooks(Source, Dest: Integer);
  70. begin
  71. mList.Move(Source,Dest);
  72. end;
  73. function CmpText(const A, B: String): Integer;
  74. var sA, sB: String;
  75. begin
  76. sA := UTF8LowerCase(Trim(A));
  77. sB := UTF8LowerCase(Trim(B));
  78. if sA < sB then Exit(-1)
  79. else if sA > sB then Exit(1)
  80. else Exit(0);
  81. end;
  82. function CompareByTitle(Item1, Item2: Pointer): Integer;
  83. begin
  84. Result := CmpText(TBook(Item1).Title, TBook(Item2).Title);
  85. end;
  86. function CompareByAuthor(Item1, Item2: Pointer): Integer;
  87. begin
  88. Result := CmpText(TBook(Item1).Authors, TBook(Item2).Authors);
  89. end;
  90. procedure TBookCollection.SortByTitle;
  91. begin
  92. if Assigned(mList) then
  93. mList.Sort(@CompareByTitle);
  94. end;
  95. procedure TBookCollection.SortByAuthor;
  96. begin
  97. if Assigned(mList) then
  98. mList.Sort(@CompareByAuthor);
  99. end;
  100. constructor TBookCollection.Create;
  101. begin
  102. mList:=TFPList.Create;
  103. end;
  104. destructor TBookCollection.Destroy;
  105. var i:Integer;
  106. book:TBook;
  107. begin
  108. CoverWorkerStop;
  109. for i:=0 to mList.Count-1 do
  110. begin
  111. book := TBook(mList.Items[i]);
  112. if Assigned(book) and Assigned(book.Cover) then
  113. book.Cover.Free;
  114. CoverWorkerRemoveBook(book);
  115. CoverWorkerUnregisterBook(book);
  116. FreeAndNil(book);
  117. end;
  118. FreeAndNil(mList);
  119. end;
  120. procedure TBookCollection.StoreData(Path: String);
  121. var
  122. tfOut: TextFile;
  123. i:integer;
  124. temp:TBook;
  125. begin
  126. // Set the name of the file that will be created
  127. AssignFile(tfOut, path);
  128. try
  129. // Create the file, write some text and close it.
  130. rewrite(tfOut);
  131. for i:=0 to mList.Count-1 do
  132. begin
  133. temp:= TBook(mList[i]);
  134. writeln(tfOut, temp.Title);
  135. WriteLn(tfOut, temp.Authors);
  136. WriteLn(tfOut, temp.ISBN);
  137. writeLn(tfOut, temp.FilePath);
  138. writeLn(tfOut, temp.ImagePath);
  139. writeLn(tfOut, '**********************');
  140. end;
  141. CloseFile(tfOut);
  142. except
  143. // If there was an error the reason can be found here
  144. on E: EInOutError do
  145. writeln('File handling error occurred. Details: ', E.ClassName, '/', E.Message);
  146. end;
  147. end;
  148. procedure TBookCollection.LoadData(Path: String; Parent: TComponent);
  149. var tempBook:TBook;
  150. title,filepath,imagepath:String;
  151. authors,isbn:String;
  152. dataFile:TextFile;
  153. begin
  154. AssignFile(dataFile, path);
  155. try
  156. Reset(dataFile);
  157. while not EOF(dataFile) do
  158. begin
  159. readln(dataFile, title);
  160. readln(datafile, authors);
  161. readln(datafile, isbn);
  162. readln(datafile, filepath);
  163. readln(datafile, imagepath);
  164. readln(datafile);
  165. tempBook:=TBook.Create(parent);
  166. tempBook.Title:=title;
  167. tempBook.Authors:=authors;
  168. tempBook.ISBN:=isbn;
  169. tempBook.FilePath:=filepath;
  170. tempBook.ImagePath:=imagepath;
  171. mList.Add(tempBook);
  172. end;
  173. finally
  174. CloseFile(dataFile);
  175. end;
  176. end;
  177. end.