/
AIMP.VK.Plugin.DataStorage.pas
1483 lines (1325 loc) · 44.4 KB
/
AIMP.VK.Plugin.DataStorage.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
{************************************************}
{* *}
{* AIMP VK Plugin *}
{* *}
{* Artem Izmaylov *}
{* (C) 2016-2024 *}
{* www.aimp.ru *}
{* Mail: support@aimp.ru *}
{* *}
{************************************************}
unit AIMP.VK.Plugin.DataStorage;
{$I AIMP.VK.inc}
interface
uses
Winapi.Windows,
Winapi.ActiveX,
System.Classes,
System.Generics.Collections,
System.SysUtils,
System.Variants,
// API
apiCore,
apiFileManager,
apiMessages,
apiMusicLibrary,
apiObjects,
apiPlugin,
apiWrappers,
// ACL
ACL.Classes.StringList,
ACL.Hashes,
ACL.SQLite3,
ACL.Threading,
ACL.Utils.FileSystem,
ACL.Utils.Strings,
// VK
AIMP.VK.Classes,
AIMP.VK.Core;
const
VKDSFieldID = AIMPML_RESERVED_FIELD_ID;
VKDSFieldFileName = AIMPML_RESERVED_FIELD_FILENAME;
VKDSFieldAlbum = 'Album';
VKDSFieldArtist = 'Artist';
VKDSFieldCategory = 'Category';
VKDSFieldDuration = 'Duration';
VKDSFieldGenre = 'Genre';
VKDSFieldTitle = 'Title';
type
TAIMPVKDataStorageCache = class;
TAIMPVKCategory = (Unknown, Music, MusicFromPlaylist, MusicFromWall, MyFriends,
MyGroups, Recommended, Popular, Search, SearchByUser, SearchByGroup, MyNewsFeed);
{ EAIMPVKDataStorageError }
EAIMPVKDataStorageError = class(Exception);
{ IAIMPVKDataStorage }
IAIMPVKDataStorage = interface
['{3A8F4498-3EE8-48FF-ADFD-BA24D335A0CB}']
procedure EnumSystemPlaylists(AProc: TProc<TVKPlaylist>);
procedure EnumMyPlaylists(AProc: TProc<TVKPlaylist>);
procedure EnumMyFriends(AProc: TProc<TVKFriend>);
procedure EnumMyGroups(AProc: TProc<TVKGroup>);
procedure NotifyMyMusicChanged(AAlbumID: Integer = -1);
end;
{ TAIMPVKDataStorage }
TAIMPVKDataStorage = class(TAIMPPropertyList,
IAIMPMessageHook,
IAIMPMLDataProvider,
IAIMPMLExtensionDataStorage,
IAIMPVKDataStorage,
IVKServiceListener)
protected const
CacheFmtAlbums = 'alb-%d';
CacheFmtAudios = 'Id-%d';
CacheFmtAudiosInAlbum = 'Id-%d-%d';
MaxPlaylistCount = 500;
strict private
FCache: TAIMPVKDataStorageCache;
FManager: IAIMPMLDataStorageManager;
FService: TVKService;
// IAIMPMessageHook
procedure CoreMessage(Message: Cardinal; Param1: Integer; Param2: Pointer; var Result: HRESULT); stdcall;
protected
function AudioGetPlaylists(OwnerID: Integer; IgnoreCache: Boolean = False): TVKPlaylists;
procedure DoGetValueAsInt32(PropertyID: Integer; out Value: Integer; var Result: HRESULT); override;
function DoGetValueAsObject(PropertyID: Integer): IInterface; override;
public
constructor Create(AService: TVKService; ADataBase: TACLSQLiteBase);
destructor Destroy; override;
procedure EnumSystemPlaylists(AProc: TProc<TVKPlaylist>);
procedure EnumMyPlaylists(AProc: TProc<TVKPlaylist>);
procedure EnumMyFriends(AProc: TProc<TVKFriend>);
procedure EnumMyGroups(AProc: TProc<TVKGroup>);
// IAIMPMLDataProvider
function GetData(Fields: IAIMPObjectList; Filter: IAIMPMLDataFilter; out Data: IUnknown): HRESULT; stdcall;
// IAIMPMLExtensionDataStorage
function ConfigLoad(Config: IAIMPConfig; Section: IAIMPString): HRESULT; stdcall;
function ConfigSave(Config: IAIMPConfig; Section: IAIMPString): HRESULT; stdcall;
function GetFields(Schema: Integer; out List: IAIMPObjectList): HRESULT; stdcall;
function GetGroupingPresets(Schema: Integer; Presets: IAIMPMLGroupingPresets): HRESULT; stdcall;
procedure FlushCache(AReserved: Integer); stdcall;
procedure Finalize; stdcall;
procedure Initialize(AManager: IAIMPMLDataStorageManager); stdcall;
// IVKServiceListener
procedure IVKServiceListener.NotifyLogIn = NotifyAuthChanged;
procedure IVKServiceListener.NotifyLogOut = NotifyAuthChanged;
procedure NotifyAuthChanged;
// IAIMPVKDataStorage
procedure NotifyMyMusicChanged(AAlbumID: Integer);
property Cache: TAIMPVKDataStorageCache read FCache;
property Service: TVKService read FService;
end;
{ TAIMPVKDataStorageCache }
TAIMPVKDataStorageCache = class
public const
LifeTime = 7; // Days
strict private
FDB: TACLSQLiteBase;
public type
TOperator = (opBeginsWith, opEquals);
public
constructor Create(DB: TACLSQLiteBase);
procedure Flush; overload;
procedure Flush(const AQuery: string; AOperator: TOperator); overload;
procedure Flush(const AFormatString: string; const AArgs: array of const; AOperator: TOperator); overload;
function Load(const AQuery: string; AList: IVKListIO): Boolean;
procedure Save(const AQuery: string; AList: IVKListIO);
//
function Request<T: class>(const ASource: IVKListIO; const AQuery: string; AProc: TProc<T>; AForceRefresh: Boolean = False): T;
end;
{ TAIMPVKDataStorageCacheQueryBuilder }
TAIMPVKDataStorageCacheQueryBuilder = class
strict private const
sFieldData = 'd';
sFieldQuery = 'q';
sFieldTime = 't';
sTableCache = 'VKCache';
class function GetTimeStamp: Integer;
public
class function CreateTable: string;
class function Delete(const AQuery: string; AOperator: TAIMPVKDataStorageCache.TOperator): string;
class function DeleteAll: string;
class function DeleteObsolette: string;
class function Load(const AQuery: string): string;
class function Save(const AQuery: string): string;
end;
{ TAIMPVKDataProviderGroupingTree }
TAIMPVKDataProviderGroupingTree = class(TInterfacedObject,
IAIMPMLGroupingTreeDataProvider)
strict private
FStorage: TAIMPVKDataStorage;
public
constructor Create(AStorage: TAIMPVKDataStorage);
// IAIMPMLGroupingTreeDataProvider
function AppendFilter(Filter: IAIMPMLDataFilterGroup; Selection: IAIMPMLGroupingTreeSelection): HRESULT; stdcall;
function GetCapabilities: Cardinal; stdcall;
function GetData(Selection: IAIMPMLGroupingTreeSelection; out Data: IAIMPMLGroupingTreeDataProviderSelection): HRESULT; stdcall;
function GetFieldForAlphabeticIndex(out FieldName: IAIMPString): HRESULT; stdcall;
end;
{ TAIMPVKDataProviderGroupingTreeData }
PAIMPVKDataProviderGroupingTreeNode = ^TAIMPVKDataProviderGroupingTreeNode;
TAIMPVKDataProviderGroupingTreeNode = record
DisplayValue: string;
HasChildren: Boolean;
ImageIndex: Integer;
Value: string;
end;
TAIMPVKDataProviderGroupingTreeData = class(TList<TAIMPVKDataProviderGroupingTreeNode>)
public
function Add(const ACategory: TAIMPVKCategory;
AHasChildren: Boolean): PAIMPVKDataProviderGroupingTreeNode; overload;
function Add(const ACategory: TAIMPVKCategory;
AHasChildren: Boolean; const AValue: OleVariant): PAIMPVKDataProviderGroupingTreeNode; overload;
function Add(const ACategory: TAIMPVKCategory;
AHasChildren: Boolean; const AValue: OleVariant; const ADisplayValue: UnicodeString;
AImageIndex: Integer = AIMPML_FIELDIMAGE_NOTE): PAIMPVKDataProviderGroupingTreeNode; overload;
end;
{ TAIMPVKDataProviderGroupingTreeSelection }
TAIMPVKDataProviderGroupingTreeSelection = class(TInterfacedObject,
IAIMPMLGroupingTreeDataProviderSelection)
strict private
FData: TAIMPVKDataProviderGroupingTreeData;
FFieldName: IAIMPString;
FIterator: Integer;
FMode: TAIMPVKCategory;
FStorage: TAIMPVKDataStorage;
procedure PopulateFriends(AList: TAIMPVKDataProviderGroupingTreeData);
procedure PopulateGroups(AList: TAIMPVKDataProviderGroupingTreeData);
procedure PopulateIndex(AList: TAIMPVKDataProviderGroupingTreeData);
procedure PopulateMusicCategories(AList: TAIMPVKDataProviderGroupingTreeData; ID: Integer);
procedure PopulatePopularGenres(AList: TAIMPVKDataProviderGroupingTreeData);
procedure PopulateSearchCategories(AList: TAIMPVKDataProviderGroupingTreeData);
procedure PopulateRecommended(AList: TAIMPVKDataProviderGroupingTreeData);
protected
function PopulateData(const AData: string): TAIMPVKDataProviderGroupingTreeData;
public
constructor Create(AStorage: TAIMPVKDataStorage; const AValue: OleVariant);
destructor Destroy; override;
// IAIMPMLGroupingTreeDataProviderSelection
function GetDisplayValue(out S: IAIMPString): HRESULT; stdcall;
function GetFlags: Cardinal; stdcall;
function GetImageIndex(out Index: Integer): HRESULT; stdcall;
function GetValue(out FieldName: IAIMPString; out Value: OleVariant): HRESULT; stdcall;
function NextRecord: LongBool; stdcall;
end;
{ IAIMPVKDataProviderTable }
IAIMPVKDataProviderTable = interface(IAIMPMLDataProviderSelection)
['{CA3DF842-FA5A-4804-87F9-4C48235A713B}']
function GetValueAsString(AFieldIndex: Integer): string; overload;
function HasData: Boolean;
end;
{ TAIMPVKDataProviderTable }
TAIMPVKDataProviderTable = class(TInterfacedObjectEx,
IAIMPVKDataProviderTable,
IAIMPMLDataProviderSelection)
strict private type
TCachedRequestProc = reference to function: TVKAudios;
strict private
FFieldIndexAlbum: Integer;
FFieldIndexArtist: Integer;
FFieldIndexCategory: Integer;
FFieldIndexDuration: Integer;
FFieldIndexFileName: Integer;
FFieldIndexGenre: Integer;
FFieldIndexID: Integer;
FFieldIndexTitle: Integer;
FIgnoreCache: Boolean;
FMessageText: string;
FOwnerPlaylists: TVKPlaylists;
FStorage: TAIMPVKDataStorage;
FTempBuffer: UnicodeString;
function GetCache: TAIMPVKDataStorageCache;
function GetService: TVKService;
protected
FCategory: string;
FData: TVKAudios;
FIndex: Integer;
FOffset: Integer;
function CachedRequest(const AQueryID: string; AProc: TCachedRequestProc): TVKAudios;
function GetPlaylist(OwnerID, PlaylistID: Integer): string;
function GetAudios(ACategory: TAIMPVKCategory; const AData: string): TVKAudios;
function GetAudiosFromAlbum(const AOwnerAndAlbumIDPair: string): TVKAudios;
function GetAudiosFromID(ID: Integer): TVKAudios; overload;
function GetAudiosFromID(ID: Integer; APlaylistID: Integer): TVKAudios; overload;
function GetAudiosFromNews: TVKAudios;
function GetAudiosFromSearchQuery(ACategory: TAIMPVKCategory; const AData: string): TVKAudios;
function GetAudiosFromWall(const AOwnerAndPostIDPair: string): TVKAudios; overload;
function GetAudiosFromWall(ID: Integer): TVKAudios; overload;
function GetPopularAudios(AGenreId: Integer): TVKAudios;
function GetRecommended: TVKAudios;
function QueryInterface(const IID: TGUID; out Obj): HRESULT; override; stdcall;
public
constructor Create(AStorage: TAIMPVKDataStorage; ACategory: TAIMPVKCategory;
const AData: string; AFields: IAIMPObjectList; AOffset: Integer; AIgnoreCache: Boolean = False);
destructor Destroy; override;
// IAIMPMLDataProviderSelection
function GetValueAsFloat(AFieldIndex: Integer): Double; stdcall;
function GetValueAsInt32(AFieldIndex: Integer): Integer; stdcall;
function GetValueAsInt64(AFieldIndex: Integer): Int64; stdcall;
function GetValueAsString(AFieldIndex: Integer): string; overload;
function GetValueAsString(FieldIndex: Integer; out Length: Integer): PWideChar; overload; stdcall;
function HasData: Boolean;
function HasNextPage: LongBool; stdcall;
function NextRow: LongBool; stdcall;
//
property Cache: TAIMPVKDataStorageCache read GetCache;
property MessageText: string read FMessageText write FMessageText;
property Service: TVKService read GetService;
property Storage: TAIMPVKDataStorage read FStorage;
end;
{ TAIMPVKGroupingTreeValue }
TAIMPVKGroupingTreeValue = class
public
class function Decode(const AValue: string; out AData: string): TAIMPVKCategory;
class function Encode(const AValue: string; ACategory: TAIMPVKCategory): string;
end;
function GetCategory(AFilter: IAIMPMLDataFilter; out AData: UnicodeString): TAIMPVKCategory;
function GetCategoryName(ACategory: TAIMPVKCategory): UnicodeString;
implementation
uses
System.Character,
System.StrUtils,
System.Math,
// VK
AIMP.VK.Plugin,
AIMP.VK.Plugin.FileSystem;
type
TEnumDataFieldFiltersProc = reference to function (AFilter: IAIMPMLDataFieldFilter): Boolean;
function CreateField(const AName: string; AType: Integer; AFlags: Integer = 0): IAIMPMLDataField;
begin
CoreCreateObject(IAIMPMLDataField, Result);
CheckResult(Result.SetValueAsInt32(AIMPML_FIELD_PROPID_TYPE, AType));
CheckResult(Result.SetValueAsObject(AIMPML_FIELD_PROPID_NAME, MakeString(AName)));
CheckResult(Result.SetValueAsInt32(AIMPML_FIELD_PROPID_FLAGS, AIMPML_FIELDFLAG_FILTERING or AFlags));
end;
function EnumDataFieldFilters(const AFilter: IAIMPMLDataFilterGroup; const AProc: TEnumDataFieldFiltersProc): Boolean;
var
AFieldFilter: IAIMPMLDataFieldFilter;
AGroup: IAIMPMLDataFilterGroup;
I: Integer;
begin
Result := False;
for I := 0 to AFilter.GetChildCount - 1 do
begin
if Succeeded(AFilter.GetChild(I, IAIMPMLDataFilterGroup, AGroup)) then
Result := EnumDataFieldFilters(AGroup, AProc)
else
if Succeeded(AFilter.GetChild(I, IAIMPMLDataFieldFilter, AFieldFilter)) then
Result := AProc(AFieldFilter);
if Result then
Break;
end;
end;
function GetFieldIndex(AFields: IAIMPObjectList; const AFieldName: string): Integer;
var
AResult: Integer;
I: Integer;
S: IAIMPString;
begin
Result := -1;
for I := 0 to AFields.GetCount - 1 do
if Succeeded(AFields.GetObject(I, IAIMPString, S)) then
begin
if Succeeded(S.Compare2(PChar(AFieldName), Length(AFieldName), AResult, False)) and (AResult = 0) then
Exit(I);
end;
end;
function GetCategory(AFilter: IAIMPMLDataFilter; out AData: UnicodeString): TAIMPVKCategory;
function ExtractData(out AData: UnicodeString): Boolean;
var
AString: IAIMPString;
begin
Result := EnumDataFieldFilters(AFilter,
function (AFilter: IAIMPMLDataFieldFilter): Boolean
var
AField: IAIMPMLDataField;
begin
Result :=
Succeeded(AFilter.GetValueAsObject(AIMPML_FIELDFILTER_FIELD, IAIMPMLDataField, AField)) and
(PropListGetStr(AField, AIMPML_FIELD_PROPID_NAME) = VKDSFieldCategory) and
(PropListGetStr(AFilter, AIMPML_FIELDFILTER_VALUE1, AString));
end);
if Result then
AData := IAIMPStringToString(AString);
end;
var
AValue: string;
begin
Result := TAIMPVKCategory.Unknown;
if ExtractData(AValue) then
begin
Result := TAIMPVKGroupingTreeValue.Decode(AValue, AData);
if Result in [Search, SearchByUser, SearchByGroup] then
begin
AData := PropListGetStr(AFilter, AIMPML_FILTER_SEARCHSTRING);
AFilter.SetValueAsObject(AIMPML_FILTER_SEARCHSTRING, nil);
end;
end;
end;
function GetCategoryName(ACategory: TAIMPVKCategory): UnicodeString;
const
Map: array[TAIMPVKCategory] of UnicodeString = ('',
'Music', 'MusicFromPlaylist', 'Wall', 'MyFriends', 'MyGroups',
'MyRecommendations', 'Popular', 'Search', 'SearchByUserID',
'SearchByGroupID', 'MyNewsFeed'
);
begin
Result := LangLoadString('AIMPVKPlugin\' + Map[ACategory]);
end;
{ TAIMPVKDataStorage }
constructor TAIMPVKDataStorage.Create(AService: TVKService; ADataBase: TACLSQLiteBase);
begin
inherited Create;
FService := AService;
FCache := TAIMPVKDataStorageCache.Create(ADataBase);
end;
destructor TAIMPVKDataStorage.Destroy;
begin
FreeAndNil(FCache);
inherited Destroy;
end;
procedure TAIMPVKDataStorage.EnumSystemPlaylists(AProc: TProc<TVKPlaylist>);
//const
// SystemPlaylists: array[0..7] of Integer = (-21, -22, -23, -25, -26, -27, -30, -31);
begin
// with Cache.Request<TVKPlaylists>(TVKPlaylists.Create, 'systemplaylists',
// procedure (APlaylists: TVKPlaylists)
// begin
// for var ID in SystemPlaylists do
// APlaylists.Add(Service.AudioGetPlaylistByID(Service.UserID, ID));
// end) do
// try
// Enum(AProc);
// finally
// Free;
// end;
end;
procedure TAIMPVKDataStorage.EnumMyPlaylists(AProc: TProc<TVKPlaylist>);
begin
with AudioGetPlaylists(Service.UserID) do
try
Enum(AProc);
finally
Free;
end;
end;
procedure TAIMPVKDataStorage.EnumMyFriends(AProc: TProc<TVKFriend>);
begin
with Cache.Request<TVKFriends>(TVKFriends.Create, 'myfriends', Service.FriendsGet) do
try
Enum(AProc);
finally
Free;
end;
end;
procedure TAIMPVKDataStorage.EnumMyGroups(AProc: TProc<TVKGroup>);
begin
with Cache.Request<TVKGroups>(TVKGroups.Create, 'mygroups', Service.GroupsGet) do
try
Enum(AProc);
finally
Free;
end;
end;
function TAIMPVKDataStorage.GetData(Fields: IAIMPObjectList; Filter: IAIMPMLDataFilter; out Data: IUnknown): HRESULT;
var
AData: UnicodeString;
begin
try
Result := S_OK;
Data := TAIMPVKDataProviderTable.Create(Self, GetCategory(Filter, AData),
AData, Fields, PropListGetInt32(Filter, AIMPML_FILTER_OFFSET));
except
Result := E_FAIL;
end;
end;
function TAIMPVKDataStorage.ConfigLoad(Config: IAIMPConfig; Section: IAIMPString): HRESULT;
begin
Result := S_OK;
end;
function TAIMPVKDataStorage.ConfigSave(Config: IAIMPConfig; Section: IAIMPString): HRESULT;
begin
Result := S_OK;
end;
function TAIMPVKDataStorage.GetFields(Schema: Integer; out List: IAIMPObjectList): HRESULT;
var
AField: IAIMPMLDataField;
begin
CoreCreateObject(IAIMPObjectList, List);
case Schema of
AIMPML_FIELDS_SCHEMA_ALL:
begin
List.Add(CreateField(VKDSFieldID, AIMPML_FIELDTYPE_INT32, AIMPML_FIELDFLAG_INTERNAL));
List.Add(CreateField(VKDSFieldFileName, 0));
List.Add(CreateField(VKDSFieldArtist, AIMPML_FIELDTYPE_STRING));
List.Add(CreateField(VKDSFieldAlbum, AIMPML_FIELDTYPE_STRING));
List.Add(CreateField(VKDSFieldTitle, AIMPML_FIELDTYPE_STRING));
List.Add(CreateField(VKDSFieldGenre, AIMPML_FIELDTYPE_STRING));
List.Add(CreateField(VKDSFieldDuration, AIMPML_FIELDTYPE_DURATION));
AField := CreateField(VKDSFieldCategory, AIMPML_FIELDTYPE_STRING, AIMPML_FIELDFLAG_INTERNAL);
AField.SetValueAsInt32(AIMPML_FIELD_PROPID_IMAGE, 3);
List.Add(AField);
end;
AIMPML_FIELDS_SCHEMA_TABLE_VIEW_ALBUMTHUMBNAILS,
AIMPML_FIELDS_SCHEMA_TABLE_VIEW_DEFAULT,
AIMPML_FIELDS_SCHEMA_TABLE_VIEW_GROUPDETAILS:
begin
List.Add(MakeString(VKDSFieldTitle));
List.Add(MakeString(VKDSFieldArtist));
List.Add(MakeString(VKDSFieldGenre));
List.Add(MakeString(VKDSFieldDuration));
end;
end;
Result := S_OK;
end;
function TAIMPVKDataStorage.GetGroupingPresets(Schema: Integer; Presets: IAIMPMLGroupingPresets): HRESULT;
var
APreset: IAIMPMLGroupingPreset;
begin
Result := S_OK;
if Schema = AIMPML_GROUPINGPRESETS_SCHEMA_BUILTIN then
Presets.Add(MakeString('AIMP.VK.ML.GroupingPreset.Default'), nil, 0, TAIMPVKDataProviderGroupingTree.Create(Self), APreset);
end;
procedure TAIMPVKDataStorage.FlushCache(AReserved: Integer);
begin
FCache.Flush;
end;
procedure TAIMPVKDataStorage.Finalize;
var
AService: IAIMPServiceMessageDispatcher;
begin
if CoreGetService(IID_IAIMPServiceMessageDispatcher, AService) then
AService.Unhook(Self);
FService.Listeners.Remove(Self);
FManager := nil;
end;
procedure TAIMPVKDataStorage.Initialize(AManager: IAIMPMLDataStorageManager);
var
AService: IAIMPServiceMessageDispatcher;
begin
FManager := AManager;
FService.Listeners.Add(Self);
if CoreGetService(IID_IAIMPServiceMessageDispatcher, AService) then
AService.Hook(Self);
end;
procedure TAIMPVKDataStorage.NotifyAuthChanged;
begin
FlushCache(0);
FManager.Changed;
end;
procedure TAIMPVKDataStorage.NotifyMyMusicChanged(AAlbumID: Integer);
begin
FCache.Flush(CacheFmtAlbums, [FService.UserID], opBeginsWith);
if AAlbumID >= 0 then
FCache.Flush(CacheFmtAudiosInAlbum, [FService.UserID, AAlbumID], opEquals)
else
FCache.Flush(CacheFmtAudios, [FService.UserID], opBeginsWith);
FManager.Changed;
end;
function TAIMPVKDataStorage.AudioGetPlaylists(OwnerID: Integer; IgnoreCache: Boolean = False): TVKPlaylists;
begin
Result := Cache.Request<TVKPlaylists>(TVKPlaylists.Create, Format(CacheFmtAlbums, [OwnerID]),
procedure (Playlists: TVKPlaylists)
var
AMaxCount: Integer;
ATempResult: TVKPlaylists;
begin
AMaxCount := MaxPlaylistCount;
repeat
ATempResult := Service.AudioGetPlaylists(OwnerID, Playlists.Count, TVKService.MaxPlaylistGetCount);
try
if ATempResult.Count > 0 then
begin
ATempResult.OwnsObjects := False;
AMaxCount := Min(AMaxCount, ATempResult.MaxCount);
Playlists.AddRange(ATempResult);
end
else
Break;
finally
FreeAndNil(ATempResult);
end;
until Playlists.Count >= AMaxCount;
end,
IgnoreCache);
end;
procedure TAIMPVKDataStorage.DoGetValueAsInt32(PropertyID: Integer; out Value: Integer; var Result: HRESULT);
begin
if PropertyID = AIMPML_DATASTORAGE_PROPID_CAPABILITIES then
begin
Value := 0; // Supress all features
Result := S_OK;
end
else
inherited DoGetValueAsInt32(PropertyID, Value, Result);
end;
function TAIMPVKDataStorage.DoGetValueAsObject(PropertyID: Integer): IInterface;
begin
case PropertyID of
AIMPML_DATASTORAGE_PROPID_ID:
Result := MakeString(VKPluginIDBase + '.DataStorage');
AIMPML_DATASTORAGE_PROPID_CAPTION:
Result := LangLoadStringEx('AIMPVKPlugin\Caption');
else
Result := inherited DoGetValueAsObject(PropertyID);
end
end;
procedure TAIMPVKDataStorage.CoreMessage(Message: Cardinal; Param1: Integer; Param2: Pointer; var Result: HRESULT);
begin
case Message of
AIMP_MSG_EVENT_LANGUAGE:
if FManager <> nil then
FManager.Changed;
end;
end;
{ TAIMPVKDataStorageCache }
constructor TAIMPVKDataStorageCache.Create(DB: TACLSQLiteBase);
begin
inherited Create;
FDB := DB;
FDB.Exec(TAIMPVKDataStorageCacheQueryBuilder.CreateTable);
end;
procedure TAIMPVKDataStorageCache.Flush;
begin
FDB.Exec(TAIMPVKDataStorageCacheQueryBuilder.DeleteAll);
FDB.Compress;
end;
procedure TAIMPVKDataStorageCache.Flush(const AQuery: string; AOperator: TOperator);
begin
FDB.Exec(TAIMPVKDataStorageCacheQueryBuilder.Delete(AQuery, AOperator));
FDB.Compress;
end;
procedure TAIMPVKDataStorageCache.Flush(const AFormatString: string; const AArgs: array of const; AOperator: TOperator);
begin
Flush(Format(AFormatString, AArgs), AOperator);
FDB.Compress;
end;
function TAIMPVKDataStorageCache.Load(const AQuery: string; AList: IVKListIO): Boolean;
var
AData: TMemoryStream;
ATable: TACLSQLiteTable;
begin
Result := FDB.Exec(TAIMPVKDataStorageCacheQueryBuilder.Load(AQuery), ATable);
if Result then
try
AData := TMemoryStream.Create;
try
ATable.ReadBlob(0, AData);
AData.Position := 0;
AList.Load(AData);
finally
AData.Free;
end;
finally
ATable.Free;
end;
end;
procedure TAIMPVKDataStorageCache.Save(const AQuery: string; AList: IVKListIO);
var
AData: TMemoryStream;
begin
AData := TMemoryStream.Create;
try
AList.Save(AData);
AData.Position := 0;
FDB.Transaction(
procedure
begin
FDB.Exec(TAIMPVKDataStorageCacheQueryBuilder.DeleteObsolette);
FDB.ExecInsertBlob(TAIMPVKDataStorageCacheQueryBuilder.Save(AQuery), AData);
end);
finally
AData.Free;
end;
end;
function TAIMPVKDataStorageCache.Request<T>(const ASource: IVKListIO;
const AQuery: string; AProc: TProc<T>; AForceRefresh: Boolean = False): T;
begin
Result := T(ASource);
if AForceRefresh or not Load(AQuery, ASource) then
try
AProc(Result);
Save(AQuery, ASource);
except
ASource.Clear;
end;
end;
{ TAIMPVKDataStorageCacheQueryBuilder }
class function TAIMPVKDataStorageCacheQueryBuilder.CreateTable: string;
procedure AddField(S: TStringBuilder; const AName, AType: string; ALast: Boolean = False);
begin
S.Append(AName);
S.Append(' ');
S.Append(AType);
if not ALast then
S.Append(', ');
end;
var
S: TStringBuilder;
begin
S := TStringBuilder.Create;
try
S.Append('CREATE TABLE IF NOT EXISTS ');
S.Append(sTableCache);
S.Append('(');
AddField(S, sFieldQuery, 'TEXT PRIMARY KEY COLLATE UNICODE');
AddField(S, sFieldTime, 'INT');
AddField(S, sFieldData, 'BLOB', True);
S.Append(');');
Result := S.ToString;
finally
S.Free;
end;
end;
class function TAIMPVKDataStorageCacheQueryBuilder.Delete(
const AQuery: string; AOperator: TAIMPVKDataStorageCache.TOperator): string;
var
S: TStringBuilder;
begin
S := TStringBuilder.Create;
try
S.Append('DELETE FROM ');
S.Append(sTableCache);
S.Append(' WHERE ');
S.Append(sFieldQuery);
if AOperator = opEquals then
begin
S.Append(' = ');
S.Append(PrepareData(AQuery));
end
else
begin
S.Append(' LIKE "');
S.Append(AQuery);
S.Append('%"');
end;
S.Append(';');
Result := S.ToString;
finally
S.Free;
end;
end;
class function TAIMPVKDataStorageCacheQueryBuilder.DeleteAll: string;
var
S: TStringBuilder;
begin
S := TStringBuilder.Create;
try
S.Append('DELETE FROM ');
S.Append(sTableCache);
S.Append(';');
Result := S.ToString;
finally
S.Free;
end;
end;
class function TAIMPVKDataStorageCacheQueryBuilder.DeleteObsolette: string;
var
S: TStringBuilder;
begin
S := TStringBuilder.Create;
try
S.Append('DELETE FROM ');
S.Append(sTableCache);
S.Append(' WHERE ');
S.Append(sFieldTime);
S.Append(' < ');
S.Append(PrepareData(GetTimeStamp - TAIMPVKDataStorageCache.LifeTime));
S.Append(';');
Result := S.ToString;
finally
S.Free;
end;
end;
class function TAIMPVKDataStorageCacheQueryBuilder.Load(const AQuery: string): string;
var
S: TStringBuilder;
begin
S := TStringBuilder.Create;
try
S.Append('SELECT ');
S.Append(sFieldData);
S.Append(' FROM ');
S.Append(sTableCache);
S.Append(' WHERE ');
S.Append(sFieldQuery);
S.Append(' = ');
S.Append(PrepareData(AQuery));
// S.Append(PrepareData(GetTimeStamp - TAIMPVKDataStorageCache.LifeTime));
S.Append(' LIMIT 1;');
Result := S.ToString;
finally
S.Free;
end;
end;
class function TAIMPVKDataStorageCacheQueryBuilder.Save(const AQuery: string): string;
var
S: TStringBuilder;
begin
S := TStringBuilder.Create;
try
S.Append('REPLACE INTO ');
S.Append(sTableCache);
S.Append(' VALUES(');
S.Append(PrepareData(AQuery));
S.Append(', ');
S.Append(PrepareData(GetTimeStamp));
S.Append(', ?);');
Result := S.ToString;
finally
S.Free;
end;
end;
class function TAIMPVKDataStorageCacheQueryBuilder.GetTimeStamp: Integer;
begin
Result := Trunc(Now);
end;
{ TAIMPVKDataProviderGroupingTree }
constructor TAIMPVKDataProviderGroupingTree.Create(AStorage: TAIMPVKDataStorage);
begin
FStorage := AStorage;
end;
function TAIMPVKDataProviderGroupingTree.AppendFilter(
Filter: IAIMPMLDataFilterGroup; Selection: IAIMPMLGroupingTreeSelection): HRESULT;
var
AFieldName: IAIMPString;
AFilter: IAIMPMLDataFieldFilter;
AValue: OleVariant;
I: Integer;
begin
Filter.BeginUpdate;
try
Filter.SetValueAsInt32(AIMPML_FILTERGROUP_OPERATION, AIMPML_FILTERGROUP_OPERATION_AND);
for I := 0 to Selection.GetCount - 1 do
begin
if Succeeded(Selection.GetValue(I, AFieldName, AValue)) then
Filter.Add(AFieldName, AValue, Null, AIMPML_FIELDFILTER_OPERATION_EQUALS, AFilter);
end;
finally
Filter.EndUpdate;
end;
Result := S_OK;
end;
function TAIMPVKDataProviderGroupingTree.GetCapabilities: Cardinal;
begin
Result := AIMPML_GROUPINGTREEDATAPROVIDER_CAP_HIDEALLDATA;
end;
function TAIMPVKDataProviderGroupingTree.GetData(
Selection: IAIMPMLGroupingTreeSelection; out Data: IAIMPMLGroupingTreeDataProviderSelection): HRESULT;
var
AFieldName: IAIMPString;
AValue: OleVariant;
begin
if Succeeded(Selection.GetValue(0, AFieldName, AValue)) then
begin
if not WideSameText(IAIMPStringToString(AFieldName), VKDSFieldCategory) then
Exit(E_UNEXPECTED);
end
else
AValue := '';
try
Data := TAIMPVKDataProviderGroupingTreeSelection.Create(FStorage, AValue);
Result := S_OK;
except
Result := E_FAIL;
end;
end;
function TAIMPVKDataProviderGroupingTree.GetFieldForAlphabeticIndex(out FieldName: IAIMPString): HRESULT;
begin
Result := E_NOTIMPL;
end;
{ TAIMPVKDataProviderGroupingTreeData }
function TAIMPVKDataProviderGroupingTreeData.Add(
const ACategory: TAIMPVKCategory; AHasChildren: Boolean): PAIMPVKDataProviderGroupingTreeNode;
begin
Result := Add(ACategory, AHasChildren, '');
end;
function TAIMPVKDataProviderGroupingTreeData.Add(
const ACategory: TAIMPVKCategory; AHasChildren: Boolean; const AValue: OleVariant): PAIMPVKDataProviderGroupingTreeNode;
begin
Result := Add(ACategory, AHasChildren, AValue, GetCategoryName(ACategory));
end;
function TAIMPVKDataProviderGroupingTreeData.Add(const ACategory: TAIMPVKCategory;
AHasChildren: Boolean; const AValue: OleVariant; const ADisplayValue: UnicodeString;
AImageIndex: Integer = AIMPML_FIELDIMAGE_NOTE): PAIMPVKDataProviderGroupingTreeNode;
var
ANode: TAIMPVKDataProviderGroupingTreeNode;
begin
ANode.ImageIndex := AImageIndex;
ANode.HasChildren := AHasChildren;
ANode.DisplayValue := ADisplayValue;
ANode.Value := TAIMPVKGroupingTreeValue.Encode(AValue, ACategory);
Result := @List[inherited Add(ANode)];
end;
{ TAIMPVKDataProviderGroupingTreeSelection }
constructor TAIMPVKDataProviderGroupingTreeSelection.Create(AStorage: TAIMPVKDataStorage; const AValue: OleVariant);
var
AData: string;
begin
FStorage := AStorage;
FFieldName := MakeString(VKDSFieldCategory);
FMode := TAIMPVKGroupingTreeValue.Decode(AValue, AData);
FData := PopulateData(AData);
end;
destructor TAIMPVKDataProviderGroupingTreeSelection.Destroy;
begin
FreeAndNil(FData);
inherited Destroy;
end;
function TAIMPVKDataProviderGroupingTreeSelection.GetDisplayValue(out S: IAIMPString): HRESULT;
begin
try
S := MakeString(FData[FIterator].DisplayValue);
Result := S_OK;
except
Result := E_UNEXPECTED;
end;
end;
function TAIMPVKDataProviderGroupingTreeSelection.GetFlags: Cardinal;
begin
Result := AIMPML_GROUPINGTREENODE_FLAG_STANDALONE;
if FData[FIterator].HasChildren then
Result := Result or AIMPML_GROUPINGTREENODE_FLAG_HASCHILDREN;
end;
function TAIMPVKDataProviderGroupingTreeSelection.GetImageIndex(out Index: Integer): HRESULT; stdcall;
begin
try
Index := FData[FIterator].ImageIndex;
Result := S_OK;
except
Result := E_UNEXPECTED;
end;
end;
function TAIMPVKDataProviderGroupingTreeSelection.GetValue(out FieldName: IAIMPString; out Value: OleVariant): HRESULT;