forked from fmd-project-team/FMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuFavoritesManager.pas
1288 lines (1189 loc) · 36 KB
/
uFavoritesManager.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
File: uFavoritesManager.pas
License: GPLv2
This unit is a part of Free Manga Downloader
}
unit uFavoritesManager;
{$mode delphi}
interface
uses
Classes, SysUtils, fgl, Dialogs, IniFiles, lazutf8classes, LazFileUtils,
uBaseUnit, uData, uDownloadsManager, WebsiteModules,
FMDOptions, httpsendthread, FavoritesDB, BaseThread, SimpleException, VirtualTrees;
type
TFavoriteManager = class;
TFavoriteTask = class;
TfavoriteContainer = class;
{ TFavoriteThread }
TFavoriteThread = class(TBaseThread)
private
FMangaInformation: TMangaInformation;
protected
procedure SyncStatus;
procedure Execute; override;
public
WorkId: Cardinal;
Task: TFavoriteTask;
Container: TfavoriteContainer;
constructor Create;
destructor Destroy; override;
end;
TFavoriteThreads = TFPGList<TFavoriteThread>;
{ TFavoriteTask }
TFavoriteTask = class(TBaseThread)
private
FBtnCaption: String;
FPendingCount: Integer;
protected
procedure SyncStartChecking;
procedure SyncFinishChecking;
procedure SyncUpdateBtnCaption;
procedure Checkout;
procedure Execute; override;
public
CS_Threads: TRTLCriticalSection;
Manager: TFavoriteManager;
Threads: TFavoriteThreads;
procedure UpdateBtnCaption(Cap: String);
constructor Create;
destructor Destroy; override;
end;
{ TFavoriteContainer }
TFavoriteContainer = class
private
FEnabled: Boolean;
FModuleId: Integer;
FWebsite: String;
procedure SetEnabled(AValue: Boolean);
procedure SetWebsite(AValue: String);
public
Tag: Integer;
FavoriteInfo: TFavoriteInfo;
NewMangaInfo: TMangaInfo;
NewMangaInfoChaptersPos: TCardinalList;
Thread: TFavoriteThread;
Manager: TFavoriteManager;
Status: TFavoriteStatusType;
constructor Create;
destructor Destroy; override;
procedure SaveToDB(const AOrder: Integer = -1);
property ModuleId: Integer read FModuleId;
property Website: String read FWebsite write SetWebsite;
property Enabled: Boolean read FEnabled write SetEnabled;
end;
TFavoriteContainers = TFPGList<TFavoriteContainer>;
{ TFavoriteManager }
TFavoriteManager = class
private
CS_Favorites: TRTLCriticalSection;
FFavoritesDB: TFavoritesDB;
FSortColumn: Integer;
FSortDirection, FIsAuto, FIsRunning: Boolean;
function GetFavoritesCount: Integer; inline;
function GetEnabledFavoritesCount: Integer; inline;
function GetDisabledFavoritesCount: Integer; inline;
function GetFavorite(const Index: Integer): TFavoriteContainer;
function ConvertToDB: Boolean;
public
Items: TFavoriteContainers;
TaskThread: TFavoriteTask;
DLManager: TDownloadManager;
OnUpdateFavorite: procedure of object;
OnUpdateDownload: procedure of object;
constructor Create;
destructor Destroy; override;
//Check favorites
procedure CheckForNewChapter(FavoriteIndex: Integer = -1);
procedure StopChekForNewChapter(WaitFor: Boolean = True; FavoriteIndex: Integer = -1);
// Show notification form after checking completed
procedure ShowResult;
// Return true if a manga exist in favorites
function LocateManga(const ATitle, AWebsite: String): TFavoriteContainer;
function IsMangaExist(const ATitle, AWebsite: String): Boolean; inline;
function LocateMangaByLink(const AWebsite, ALink: String): TFavoriteContainer;
function IsMangaExistLink(const AWebsite, ALink: String): Boolean; inline;
// Add new manga to the list
procedure Add(const ATitle, ACurrentChapter, ADownloadedChapterList, AWebsite, ASaveTo, ALink: String);
// Merge manga information with a title that already exist in favorites
procedure AddMerge(const ATitle, ACurrentChapter, ADownloadedChapterList, AWebsite,
ASaveTo, ALink: String);
// Merge a favorites.ini with another favorites.ini
procedure MergeWith(const APath: String);
// Free then delete favorite without any check, use with caution
procedure FreeAndDelete(const Pos: Integer); overload;
procedure FreeAndDelete(const T: TFavoriteContainer); overload;
// Remove a manga from FFavorites
procedure Remove(const Pos: Integer; const isBackup: Boolean = True); overload;
procedure Remove(const T: TFavoriteContainer; const isBackup: Boolean = True); overload;
// Restore information from favorites.db
procedure Restore;
// Backup to favorites.db
procedure Backup;
// Add FFavorites downloadedchapterlist
procedure AddToDownloadedChaptersList(const AWebsite, ALink: String; const AValue: TStrings);
// sorting
procedure Sort(const AColumn: Integer);
// critical section
procedure Lock;
procedure LockRelease;
procedure SearchEnabledOnVT(Tree: TVirtualStringTree; Key: String);
procedure SearchDisabledOnVT(Tree: TVirtualStringTree; Key: String);
property Count: Integer read GetFavoritesCount;
property CountEnabled: Integer read GetEnabledFavoritesCount;
property CountDisabled: Integer read GetDisabledFavoritesCount;
property SortDirection: Boolean read FSortDirection write FSortDirection;
property SortColumn: Integer read FSortColumn write FSortColumn;
property isAuto: Boolean read FIsAuto write FIsAuto;
property isRunning: Boolean read FIsRunning write FIsRunning;
property Favorite[const Index: Integer]: TFavoriteContainer read GetFavorite; default;
end;
resourcestring
RS_DlgFavoritesCheckIsRunning = 'Favorites check is running!';
RS_DlgNewChapterCaption = '%d manga(s) have new chapter(s)';
RS_LblNewChapterFound = 'Found %d new chapter from %d manga(s):';
RS_FavoriteHasNewChapter = '%s <%s> has %d new chapter(s).';
RS_BtnDownload = '&Download';
RS_BtnAddToQueue = '&Add to queue';
RS_BtnCancel = '&Cancel';
RS_DlgCompletedMangaCaption = 'Found %d completed manga';
RS_LblMangaWillBeRemoved = 'Completed manga will be removed:';
RS_BtnRemove = '&Remove';
RS_BtnCheckFavorites = 'Check for new chapter';
implementation
uses
frmMain, frmNewChapter, FMDVars;
{ TFavoriteContainer }
procedure TFavoriteContainer.SetWebsite(AValue: String);
begin
if FWebsite = AValue then Exit;
FWebsite := AValue;
FavoriteInfo.Website := AValue;
FModuleId := Modules.LocateModule(FavoriteInfo.Website);
end;
procedure TFavoriteContainer.SetEnabled(AValue: Boolean);
begin
if FEnabled = AValue then Exit;
FEnabled := AValue;
end;
constructor TFavoriteContainer.Create;
begin
FModuleId := -1;
FEnabled := True;
Tag := 0;
end;
destructor TFavoriteContainer.Destroy;
begin
if Assigned(Thread) then
begin
Thread.Terminate;
Thread.WaitFor;
Thread := nil;
end;
if Assigned(NewMangaInfo) then
begin
NewMangaInfo.Free;
NewMangaInfoChaptersPos.Free;
end;
inherited Destroy;
end;
procedure TFavoriteContainer.SaveToDB(const AOrder: Integer);
var
i: Integer;
begin
if AOrder = -1 then
i := Manager.Items.IndexOf(Self)
else
i := AOrder;
with FavoriteInfo do
Manager.FFavoritesDB.Add(
i,
FEnabled,
Website,
Link,
Title,
CurrentChapter,
DownloadedChapterList,
SaveTo
);
end;
{ TFavoriteThread }
procedure TFavoriteThread.SyncStatus;
begin
if MainForm.pcMain.ActivePage = MainForm.tsFavorites then
MainForm.vtFavorites.Repaint;
end;
procedure TFavoriteThread.Execute;
var
DLChapters: TStringList;
i: Integer;
begin
if (Container.FavoriteInfo.Link) = '' then Exit;
Synchronize(SyncStatus);
with Container do
try
// get new manga info
FMangaInformation.isGetByUpdater := False;
//FMangaInformation.mangaInfo.title := FavoriteInfo.Title; // retrieve the original title so custom rename can remove them
FMangaInformation.GetInfoFromURL(FavoriteInfo.Website, FavoriteInfo.Link);
if not Terminated then
begin
NewMangaInfo := FMangaInformation.mangaInfo;
FMangaInformation.mangaInfo := nil;
NewMangaInfoChaptersPos := TCardinalList.Create;
// update current chapters count immedietly
FavoriteInfo.CurrentChapter := IntToStr(NewMangaInfo.chapterLinks.Count);
if NewMangaInfo.chapterLinks.Count > 0 then
begin
// tag 100 for transfer favorite, add all chapter to downloaded chapter list
if Container.Tag = 100 then
begin
FavoriteInfo.DownloadedChapterList := NewMangaInfo.chapterLinks.Text;
Container.Tag := 0;
end
else
try
DLChapters := TStringList.Create;
DLChapters.Sorted := False;
DLChapters.Text := FavoriteInfo.DownloadedChapterList;
DLChapters.Sorted := True;
for i := 0 to NewMangaInfo.chapterLinks.Count - 1 do
if DLChapters.IndexOf(NewMangaInfo.chapterLinks[i]) = -1 then
NewMangaInfoChaptersPos.Add(i);
finally
DLChapters.Free;
end;
end;
// free unneeded objects
if (NewMangaInfoChaptersPos.Count = 0) and
(NewMangaInfo.status <> MangaInfo_StatusCompleted) then
begin
FreeAndNil(NewMangaInfo);
FreeAndNil(NewMangaInfoChaptersPos);
end;
end;
except
on E: Exception do
ExceptionHandle(Self, E);
end;
end;
constructor TFavoriteThread.Create;
begin
inherited Create(True);
FMangaInformation := TMangaInformation.Create(Self);
end;
destructor TFavoriteThread.Destroy;
begin
if Terminated then
begin
Container.Status := STATUS_IDLE;
// free unused objects
if Assigned(Container.NewMangaInfo) then
begin
FreeAndNil(Container.NewMangaInfo);
FreeAndNil(Container.NewMangaInfoChaptersPos);
end;
end
else
Container.Status := STATUS_CHECKED;
Container.Thread := nil;
EnterCriticalsection(Task.CS_Threads);
try
Modules.DecActiveConnectionCount(Container.ModuleId);
Task.Threads.Remove(Self);
finally
LeaveCriticalsection(Task.CS_Threads);
end;
FMangaInformation.Free;
if not Terminated then
Synchronize(SyncStatus);
inherited Destroy;
end;
{ TFavoriteTask }
procedure TFavoriteTask.SyncStartChecking;
begin
with MainForm do begin
btCancelFavoritesCheck.Visible := True;
btFavoritesCheckNewChapter.Width :=
btFavoritesCheckNewChapter.Width - btCancelFavoritesCheck.Width - 6;
btFavoritesCheckNewChapter.Caption := RS_Checking;
rbFavoritesShowAll.Enabled := False;
rbFavoritesShowDisabled.Enabled := False;
rbFavoritesShowEnabled.Enabled := False;
end;
end;
procedure TFavoriteTask.SyncFinishChecking;
begin
with MainForm do
begin
btCancelFavoritesCheck.Visible := False;
btFavoritesCheckNewChapter.Width := btFavoritesCheckNewChapter.Width +
btCancelFavoritesCheck.Width + 6;
btFavoritesCheckNewChapter.Caption := RS_BtnCheckFavorites;
rbFavoritesShowAll.Enabled := True;
rbFavoritesShowDisabled.Enabled := True;
rbFavoritesShowEnabled.Enabled := True;
vtFavorites.Repaint;
if OptionAutoCheckFavInterval and (not tmCheckFavorites.Enabled) then
tmCheckFavorites.Enabled := True;
end;
end;
procedure TFavoriteTask.SyncUpdateBtnCaption;
begin
MainForm.btFavoritesCheckNewChapter.Caption := FBtnCaption;
end;
procedure TFavoriteTask.Checkout;
var
i: Integer;
begin
if Terminated then Exit;
if Manager.Items.Count = 0 then Exit;
FPendingCount := 0;
for i := 0 to Manager.Items.Count - 1 do
begin
if Terminated then Break;
with Manager.Items[i] do
if (Status = STATUS_CHECK) then
begin
if (Threads.Count < OptionMaxThreads) and
Modules.CanCreateConnection(ModuleId) then
begin
EnterCriticalsection(CS_Threads);
try
Modules.IncActiveConnectionCount(ModuleId);
Status := STATUS_CHECKING;
Thread := TFavoriteThread.Create;
Threads.Add(Thread);
Thread.Task := Self;
Thread.Container := Manager.Items[i];
Thread.WorkId := i;
Thread.Start;
finally
LeaveCriticalsection(CS_Threads);
end
end
else
Inc(FPendingCount);
end;
end;
end;
procedure TFavoriteTask.Execute;
var
cthread,
cmaxthreads: Integer;
begin
Manager.isRunning := True;
Synchronize(SyncStartChecking);
try
while not Terminated do
begin
cmaxthreads := OptionMaxThreads;
// if current thread count > max Threads allowed we wait until thread count decreased
while (not Terminated) and (Threads.Count >= cmaxthreads) do
Sleep(SOCKHEARTBEATRATE);
Checkout;
// if there is concurent connection limit applied and no more possible item to check
// we will wait until thread count decreased
// break wait if OptionMaxThreads changed
cthread := Threads.Count;
while (not Terminated) and (Threads.Count > 0) and (Threads.Count = cthread) and
(cmaxthreads = OptionMaxThreads) do
Sleep(SOCKHEARTBEATRATE);
// if there is no more item need to be checked, but thread count still > 0 we will wait for it
// we will also wait if there is new item pushed, so we will check it after it
while (not Terminated) and (FPendingCount = 0) and (Threads.Count > 0) do
Sleep(SOCKHEARTBEATRATE);
if FPendingCount = 0 then Break;
end;
while (not Terminated) and (Threads.Count > 0) do
Sleep(SOCKHEARTBEATRATE);
except
on E: Exception do
ExceptionHandle(Self, E);
end;
end;
procedure TFavoriteTask.UpdateBtnCaption(Cap: String);
begin
FBtnCaption := Cap;
Synchronize(SyncUpdateBtnCaption);
end;
constructor TFavoriteTask.Create;
begin
inherited Create(True);
InitCriticalSection(CS_Threads);
Threads := TFavoriteThreads.Create;
end;
destructor TFavoriteTask.Destroy;
var
i: Integer;
begin
// reset all status
EnterCriticalsection(Manager.CS_Favorites);
try
for i := 0 to Manager.Items.Count - 1 do
Manager.Items[i].Status := STATUS_IDLE;
finally
LeaveCriticalsection(Manager.CS_Favorites);
end;
// terminate all threads and wait
EnterCriticalsection(CS_Threads);
try
if Threads.Count > 0 then
for i := 0 to Threads.Count - 1 do
Threads[i].Terminate;
finally
LeaveCriticalsection(CS_Threads);
end;
while Threads.Count > 0 do
Sleep(32);
if (not Terminated) and (not isDlgCounter) then
Synchronize(Manager.ShowResult)
else
// free unused unit
begin
EnterCriticalsection(Manager.CS_Favorites);
try
for i := 0 to Manager.Items.Count - 1 do
with Manager.Items[i] do
begin
if Assigned(NewMangaInfo) then
FreeAndNil(NewMangaInfo);
if Assigned(NewMangaInfoChaptersPos) then
FreeAndNil(NewMangaInfoChaptersPos);
end;
finally
LeaveCriticalsection(Manager.CS_Favorites);
end;
end;
Threads.Free;
try
EnterCriticalsection(Manager.CS_Favorites);
Manager.isRunning := False;
Manager.TaskThread := nil;
finally
LeaveCriticalsection(Manager.CS_Favorites);
end;
// reset the ui
if not isExiting then
Synchronize(SyncFinishChecking);
inherited Destroy;
end;
{ TFavoriteManager }
function TFavoriteManager.GetFavoritesCount: Integer;
begin
Result := Items.Count;
end;
function TFavoriteManager.GetEnabledFavoritesCount: Integer;
var
i: Integer;
j: Integer;
begin
j := 0;
for i := 0 to Items.Count - 1 do
begin
if Items[i].FEnabled then j := j + 1;
end;
Result := j;
end;
function TFavoriteManager.GetDisabledFavoritesCount: Integer;
var
i: Integer;
j: Integer;
begin
j := 0;
for i := 0 to Items.Count - 1 do
begin
if not Items[i].FEnabled then j := j + 1;
end;
Result := j;
end;
procedure TFavoriteManager.SearchEnabledOnVT(Tree: TVirtualStringTree; Key: String);
var
s: String;
node, xnode: PVirtualNode;
v: Boolean;
begin
if Tree.TotalCount = 0 then
Exit;
s := AnsiUpperCase(Key);
Tree.BeginUpdate;
try
node := Tree.GetFirst();
if (s <> '') then
begin
while node <> nil do
begin
v := Pos(s, AnsiUpperCase(Tree.Text[node, 1])) <> 0;
if FavoriteManager[node^.Index].Enabled then
Tree.IsVisible[node] := v;
if v then
begin
xnode := node^.Parent;
while (xnode <> nil) and (xnode <> Tree.RootNode) do
begin
if not (vsVisible in xnode^.States) and (FavoriteManager[node^.Index].Enabled) then
Tree.IsVisible[xnode] := True;
xnode := xnode^.Parent;
end;
end;
node := Tree.GetNext(node);
end;
end
else
begin
while node <> nil do
begin
if (FavoriteManager[node^.Index].Enabled) then
Tree.IsVisible[node] := True
else
Tree.IsVisible[node] := False;
node := Tree.GetNext(node);
end;
end;
finally
Tree.EndUpdate;
end;
end;
procedure TFavoriteManager.SearchDisabledOnVT(Tree: TVirtualStringTree; Key: String);
var
s: String;
node, xnode: PVirtualNode;
v: Boolean;
begin
if Tree.TotalCount = 0 then
Exit;
s := AnsiUpperCase(Key);
Tree.BeginUpdate;
try
node := Tree.GetFirst();
if (s <> '') then
begin
while node <> nil do
begin
v := Pos(s, AnsiUpperCase(Tree.Text[node, 1])) <> 0;
if not FavoriteManager[node^.Index].Enabled then
Tree.IsVisible[node] := v;
if v then
begin
xnode := node^.Parent;
while (xnode <> nil) and (xnode <> Tree.RootNode) do
begin
if not ((vsVisible in xnode^.States) and (FavoriteManager[node^.Index].Enabled)) then
Tree.IsVisible[xnode] := True;
xnode := xnode^.Parent;
end;
end;
node := Tree.GetNext(node);
end;
end
else
begin
while node <> nil do
begin
if not (FavoriteManager[node^.Index].Enabled) then
Tree.IsVisible[node] := True
else
Tree.IsVisible[node] := False;
node := Tree.GetNext(node);
end;
end;
finally
Tree.EndUpdate;
end;
end;
function TFavoriteManager.GetFavorite(const Index: Integer): TFavoriteContainer;
begin
Result := Items[Index];
end;
function TFavoriteManager.ConvertToDB: Boolean;
var
i: Integer;
s: String;
begin
Result := False;
if not FileExistsUTF8(FAVORITES_FILE) then Exit;
with TIniFile.Create(FAVORITES_FILE) do
try
i := ReadInteger('general', 'NumberOfFavorites', 0);
if i > 0 then
begin
for i := 0 to i - 1 do
begin
s := IntToStr(i);
FFavoritesDB.Add(
i,
True,
ReadString(s, 'Website', ''),
RemoveHostFromURL(ReadString(s, 'Link', '')),
ReadString(s, 'Title', ''),
ReadString(s, 'CurrentChapter', ''),
GetParams(ReadString(s, 'DownloadedChapterList', '')),
ReadString(s, 'SaveTo', '')
);
end;
FFavoritesDB.Commit;
end;
Result := True;
finally
Free;
end;
if Result then
Result := DeleteFileUTF8(FAVORITES_FILE);
end;
constructor TFavoriteManager.Create;
begin
inherited Create;
ForceDirectoriesUTF8(WORK_FOLDER);
InitCriticalSection(CS_Favorites);
isRunning := False;
Items := TFavoriteContainers.Create;;
FFavoritesDB := TFavoritesDB.Create(FAVORITESDB_FILE);
FFavoritesDB.Open;
ConvertToDB;
end;
destructor TFavoriteManager.Destroy;
var
i: Integer;
begin
if Items.Count > 0 then
begin
StopChekForNewChapter;
for i := 0 to Items.Count - 1 do
Items[i].Free;
end;
Items.Free;
FFavoritesDB.Free;
DoneCriticalsection(CS_Favorites);
inherited Destroy;
end;
procedure TFavoriteManager.CheckForNewChapter(FavoriteIndex: Integer);
var
i: Integer;
begin
if isDlgCounter then Exit;
try
if FavoriteIndex > -1 then
begin
with Items[FavoriteIndex] do
if FEnabled and (Status = STATUS_IDLE) then
begin
Status := STATUS_CHECK;
if Assigned(TaskThread) then
TaskThread.FPendingCount := InterLockedIncrement(TaskThread.FPendingCount);
end;
end
else
if isRunning then
begin
if not isAuto then
MessageDlg('', RS_DlgFavoritesCheckIsRunning, mtInformation, [mbOK], 0);
end
else
begin
EnterCriticalsection(CS_Favorites);
try
for i := 0 to Items.Count - 1 do
with Items[i] do
if FEnabled and (Status = STATUS_IDLE) and (Trim(FavoriteInfo.Link) <> '') then
Status := STATUS_CHECK;
finally
LeaveCriticalsection(CS_Favorites);
end;
end;
if TaskThread = nil then
begin
TaskThread := TFavoriteTask.Create;
TaskThread.Manager := Self;
TaskThread.Start;
end;
except
on E: Exception do
ExceptionHandle(Self, E);
end;
end;
procedure TFavoriteManager.StopChekForNewChapter(WaitFor: Boolean; FavoriteIndex: Integer);
begin
if not isRunning then Exit;
if FavoriteIndex > -1 then
begin
with Items[FavoriteIndex] do begin
if Thread <> nil then
begin
Thread.Terminate;
if WaitFor then
Thread.WaitFor;
end;
if Status <> STATUS_IDLE then
Status := STATUS_IDLE;
end;
end
else
if Assigned(TaskThread) then
begin
TaskThread.Terminate;
if WaitFor then
TaskThread.WaitFor;
end;
end;
procedure TFavoriteManager.ShowResult;
var
i, j,
numOfNewChapters,
numOfMangaNewChapters,
numOfCompleted: Integer;
LNCResult: TNewChapterResult = ncrCancel;
newChapterListStr: String = '';
removeListStr: String = '';
newdl: LongInt;
begin
if isDlgCounter then Exit;
if (Self.DLManager = nil) and Assigned(DLManager) then
Self.DLManager := DLManager;
if Self.DLManager = nil then Exit;
EnterCriticalsection(CS_Favorites);
try
numOfNewChapters := 0;
numOfMangaNewChapters := 0;
numOfCompleted := 0;
try
// check for all favorites
for i := 0 to Items.Count - 1 do
with Items[i] do
if Assigned(NewMangaInfo) then
begin
// new chapters add to notification
if NewMangaInfoChaptersPos.Count > 0 then
begin
newChapterListStr += LineEnding + '- ' + Format(
RS_FavoriteHasNewChapter, [FavoriteInfo.Title, FavoriteInfo.Website,
NewMangaInfoChaptersPos.Count]);
Inc(numOfMangaNewChapters);
Inc(numOfNewChapters, NewMangaInfoChaptersPos.Count);
end
else
// completed series add to notification
if OptionAutoCheckFavRemoveCompletedManga and
(NewMangaInfo.status = MangaInfo_StatusCompleted) then
begin
removeListStr += LineEnding + Format('- %s <%s>',
[FavoriteInfo.Title, FavoriteInfo.Website]);
Inc(numOfCompleted);
end;
end;
// if there is completed mangas, show dialog
if numOfCompleted > 0 then
begin
with TNewChapter.Create(MainForm) do
try
Caption := Format(RS_DlgCompletedMangaCaption, [numOfCompleted]);
lbNotification.Caption := RS_LblMangaWillBeRemoved;
mmMemo.Lines.Text := Trim(removeListStr);
btDownload.Caption := RS_BtnRemove;
btCancel.Caption := RS_BtnCancel;
btDownload.Show;
btCancel.Show;
btQueue.Hide;
ShowModal;
LNCResult := FormResult;
finally
Free;
end;
//delete complete FFavorites
if LNCResult = ncrDownload then
begin
i := 0;
while i < Items.Count do
with Items[i] do
begin
if Assigned(NewMangaInfo) and
(NewMangaInfoChaptersPos.Count = 0) and
(NewMangaInfo.status = MangaInfo_StatusCompleted) then
FreeAndDelete(i)
else
Inc(i);
end;
end;
Backup;
end;
// if there is new chapters
if numOfNewChapters > 0 then
begin
if OptionAutoCheckFavDownload then
LNCResult := ncrDownload
else
with TNewChapter.Create(MainForm) do
try
Caption := Format(RS_DlgNewChapterCaption, [numOfNewChapters]);
lbNotification.Caption :=
Format(RS_LblNewChapterFound, [numOfNewChapters, numOfMangaNewChapters]);
mmMemo.Lines.Text := Trim(newChapterListStr);
btDownload.Caption := RS_BtnDownload;
btQueue.Caption := RS_BtnAddToQueue;
btCancel.Caption := RS_BtnCancel;
btDownload.Show;
btQueue.Show;
btCancel.Show;
ShowModal;
LNCResult := FormResult;
finally
Free;
end;
// generate download task
if LNCResult <> ncrCancel then
begin
while DLManager.isRunningBackup do
Sleep(100);
for i := 0 to Items.Count - 1 do
with Items[i] do
if Assigned(NewMangaInfo) and
(NewMangaInfoChaptersPos.Count > 0) then
try
EnterCriticalSection(DLManager.CS_Task);
newdl := DLManager.Items.Add(TTaskContainer.Create);
with DLManager.Items[newdl] do
begin
Manager := DLManager;
CurrentDownloadChapterPtr := 0;
Website := FavoriteInfo.Website;
DownloadInfo.Link := FavoriteInfo.Link;
DownloadInfo.Title := FavoriteInfo.Title;
DownloadInfo.SaveTo := FavoriteInfo.SaveTo;
DownloadInfo.dateTime := Now;
for j := 0 to NewMangaInfoChaptersPos.Count - 1 do
begin
ChapterLinks.Add(NewMangaInfo.chapterLinks[NewMangaInfoChaptersPos[j]]);
ChapterName.Add(CustomRename(
OptionChapterCustomRename,
FavoriteInfo.Website,
FavoriteInfo.Title,
NewMangaInfo.authors,
NewMangaInfo.artists,
NewMangaInfo.chapterName[NewMangaInfoChaptersPos[j]],
Format('%.4d', [NewMangaInfoChaptersPos[j] + 1]),
OptionChangeUnicodeCharacter,
OptionChangeUnicodeCharacterStr));
end;
if LNCResult = ncrDownload then
begin
DownloadInfo.Status := Format('[%d/%d] %s',[0,ChapterLinks.Count,RS_Waiting]);
Status := STATUS_WAIT;
end
else
begin
DownloadInfo.Status := Format('[%d/%d] %s',[0,ChapterLinks.Count,RS_Stopped]);
Status := STATUS_STOP;
end;
SaveToDB(newdl);
// add to downloaded chapter list
FavoriteInfo.downloadedChapterList := MergeCaseInsensitive([FavoriteInfo.DownloadedChapterList, chapterLinks.Text]);
// add to downloaded chapter list in downloadmanager
DLManager.DownloadedChapters.Chapters[FavoriteInfo.Website + FavoriteInfo.Link] := chapterLinks.Text;
end;
// free unused objects
FreeAndNil(NewMangaInfo);
FreeAndNil(NewMangaInfoChaptersPos);
finally
LeaveCriticalSection(DLManager.CS_Task);
end;
Backup;
if LNCResult = ncrDownload then
begin
DLManager.CheckAndActiveTask;
if OptionShowDownloadsTabOnNewTasks then
MainForm.pcMain.ActivePage := MainForm.tsDownload;
end;
if Assigned(OnUpdateDownload) then
OnUpdateDownload;
if Assigned(OnUpdateFavorite) then
OnUpdateFavorite;
end;
end;
except
on E: Exception do
ExceptionHandle(Self, E);
end;
// check again for unused objects and free them
for i := 0 to Items.Count - 1 do
with Items[i] do
if Assigned(NewMangaInfo) then
begin
FreeAndNil(NewMangaInfo);
FreeAndNil(NewMangaInfoChaptersPos);
end;
finally
LeaveCriticalsection(CS_Favorites);
end;
end;
function TFavoriteManager.LocateManga(const ATitle, AWebsite: String): TFavoriteContainer;
var
i: Integer;
begin
Result := nil;
if Items.Count <> 0 then
for i := 0 to Items.Count - 1 do
with Items[i].FavoriteInfo do