-
Notifications
You must be signed in to change notification settings - Fork 6
/
model.mmpPlaylist.pas
465 lines (399 loc) · 14.6 KB
/
model.mmpPlaylist.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
{ MMP: Minimalist Media Player
Copyright (C) 2021-2024 Baz Cuda
https://github.com/BazzaCuda/MinimalistMediaPlayerX
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
}
unit model.mmpPlaylist;
interface
uses
system.classes, system.generics.collections,
vcl.stdCtrls,
mmpNotify.notices, mmpNotify.notifier, mmpNotify.subscriber,
mmpConsts;
type
TSetOfMediaType = set of TMediaType;
IPlaylist = interface
['{EAA8F4FC-4F65-4080-B25F-5F5CA08309D9}']
function clear: boolean;
function copyToClipboard: string;
function count: integer;
function currentFolder: string;
function currentItem: string;
function currentIx: integer;
function deleteIx(const ix: integer = -1): boolean;
function fillPlaylist(const aFolder: string; const aSetOfMediaType: TSetOfMediaType = [mtAudio, mtVideo, mtImage]): boolean;
function find(const anItem: string): boolean;
function first: boolean;
function getNotifier: INotifier;
function hasItems: boolean;
function indexOf(const anItem: string): integer;
function insert(const anItem: string): boolean;
function isFirst: boolean;
function isLast: boolean;
function last: boolean;
function next(const aMediaType: TMediaType = mtUnk): boolean;
function notify(const aNotice: INotice): INotice;
function notifyEx(const aNotice: INotice; const aSetOfMediaType: TSetOfMediaType = [mtAudio, mtVideo, mtImage]): INotice;
function prev: boolean;
function replaceCurrentItem(const aNewItem: string): boolean;
function setIx(const ix: integer): integer;
function thisItem(const ix: integer): string;
function validIx(const ix: integer): boolean;
property notifier: INotifier read getNotifier;
end;
function newPlaylist: IPlaylist;
implementation
uses
winApi.windows,
system.regularExpressions, system.sysUtils,
vcl.clipbrd,
mmpFileUtils, mmpUtils,
model.mmpMediaTypes,
TListHelperClass,
_debugWindow;
type
TPlaylist = class(TInterfacedObject, IPlaylist)
strict private
FCurrentFolder: string;
FNotifier: INotifier;
FPlayIx: integer;
FPlaylist: TList<string>;
FSubscriber: ISubscriber;
private
function add(const anItem: string): boolean;
function displayItem: string;
function extractNumericPart(const aString: string): integer;
function formattedItem: string;
function getPlaylist(aListBox: TListBox): boolean;
function isSpecialImage: boolean;
function onNotify(const aNotice: INotice): INotice;
function sort: boolean;
protected
procedure setCurrentFolder(const aValue: string);
public
constructor create;
destructor Destroy; override;
function clear: boolean;
function copyToClipboard: string;
function count: integer;
function currentFolder: string;
function currentItem: string;
function currentIx: integer;
function deleteIx(const ix: integer = -1): boolean;
function fillPlaylist(const aFolder: string; const aSetOfMediaType: TSetOfMediaType = [mtAudio, mtVideo, mtImage]): boolean;
function find(const anItem: string): boolean;
function first: boolean;
function getNotifier: INotifier;
function hasItems: boolean;
function indexOf(const anItem: string): integer;
function insert(const anItem: string): boolean;
function isFirst: boolean;
function isLast: boolean;
function last: boolean;
function next(const aMediaType: TMediaType = mtUnk): boolean;
function notify(const aNotice: INotice): INotice;
function notifyEx(const aNotice: INotice; const aSetOfMediaType: TSetOfMediaType = [mtAudio, mtVideo, mtImage]): INotice;
function prev: boolean;
function replaceCurrentItem(const aNewItem: string): boolean;
function setIx(const ix: integer): integer;
function thisItem(const ix: integer): string;
function validIx(const ix: integer): boolean;
end;
function newPlaylist: IPlaylist;
begin
result := TPlaylist.create;
end;
{ TPlaylist }
function TPlaylist.add(const anItem: string): boolean;
begin
result := FPlayList.add(anItem) <> 0;
end;
function TPlaylist.clear: boolean;
begin
FPlaylist.clear;
FPlayIx := -1;
result := FPlaylist.count = 0;
end;
function TPlaylist.copyToClipboard: string;
begin
result := '';
clipboard.AsText := mmpFileNameWithoutExtension(currentItem);
result := 'Copied to clipboard';
end;
function TPlaylist.count: integer;
begin
result := FPlaylist.count;
end;
constructor TPlaylist.create;
begin
inherited;
FPlaylist := TList<string>.create;
FPLaylist.sort;
FSubscriber := appNotifier.subscribe(newSubscriber(onNotify));
end;
function TPlaylist.currentFolder: string;
begin
result := FCurrentFolder;
end;
function TPlaylist.currentItem: string;
begin
result := '';
case FPlayIx = -1 of TRUE: EXIT; end;
result := FPlaylist[FPlayIx];
end;
function TPlaylist.currentIx: integer;
begin
result := FPlayIx;
end;
function TPlaylist.deleteIx(const ix: integer = -1): boolean;
begin
result := FALSE;
case hasItems of FALSE: EXIT; end;
case ix = -1 of TRUE: begin
FPlaylist.delete(FPlayIx);
dec(FPlayIx); end;
FALSE: begin
case (ix < 0) or (ix > FPlaylist.count - 1) of TRUE: EXIT; end;
FPlaylist.delete(ix);
dec(FPlayIx); end;end;
case (FPlayIx < 0) and (FPlaylist.count > 0) of TRUE: FPlayIx := 0; end; // the item at index 0 was deleted so point to the new item[0]
result := TRUE;
end;
destructor TPlaylist.Destroy;
begin
appNotifier.unsubscribe(FSubscriber);
case FPlaylist <> NIL of TRUE: FPlaylist.free; end;
inherited;
end;
function TPlaylist.displayItem: string;
begin
result := format('[%d/%d] %s', [FPlayIx, count, extractFileName(currentItem)]);
end;
function TPlaylist.extractNumericPart(const aString: string): Integer;
var
vMatch: TMatch;
begin
// Use a regular expression to extract the numeric part from the string
vMatch := TRegEx.match(aString, '\d+');
if vMatch.success then
result := strToIntDef(vMatch.value, 0)
else
result := 0;
end;
function TPlaylist.fillPlaylist(const aFolder: string; const aSetOfMediaType: TSetOfMediaType = [mtAudio, mtVideo, mtImage]): boolean;
const
faFilesOnly = faAnyFile AND NOT faDirectory {AND NOT faHidden} AND NOT faSysFile;
var
vSR: TSearchRec;
function fileExtOK: boolean;
begin
var vMT := MT.mediaType(vSR.name);
result := (vMT <> mtUnk) and ((mtUnk in aSetOfMediaType) or (vMT in aSetOfMediaType));
end;
begin
result := FALSE;
clear;
case aFolder = '' of TRUE: EXIT; end;
case directoryExists(aFolder) of FALSE: EXIT; end;
FCurrentFolder := aFolder;
case findFirst(aFolder + '*.*', faFilesOnly, vSR) = 0 of TRUE:
repeat
case fileExtOK of TRUE: add(aFolder + vSR.Name); end;
until findNext(vSR) <> 0;
end;
system.sysUtils.findClose(vSR);
sort;
case hasItems of TRUE: FPlayIx := 0;
FALSE: FPlayIx := -1; end;
result := hasItems;
notifyApp(newNotice(evPLNewPlaylist));
end;
function TPlaylist.find(const anItem: string): boolean;
begin
FPlayIx := FPlaylist.indexOf(anItem);
result := FPlayIx <> -1;
end;
function TPlaylist.first: boolean;
begin
result := FALSE;
case hasItems of TRUE: FPlayIx := 0;
FALSE: FPlayIx := -1; end;
result := FPlayIx = 0;
end;
function TPlaylist.formattedItem: string;
begin
case hasItems of FALSE: EXIT; end;
result := format('[%d/%d] %s', [FPlayIx + 1, FPlaylist.count, extractFileName(currentItem)]);
end;
function TPlaylist.getNotifier: INotifier;
begin
case FNotifier = NIL of TRUE: FNotifier := newNotifier; end;
result := FNotifier;
end;
function TPlaylist.getPlaylist(aListBox: TListBox): boolean;
var i: integer;
begin
result := FALSE;
aListBox.items.beginUpdate; // prevent flicker when moving the window
try
aListBox.clear;
for i := 0 to FPlaylist.count - 1 do
aListBox.items.add(extractFileName(FPlaylist[i]));
finally
aListBox.items.endUpdate;
end;
result := aListBox.count > 0;
end;
function TPlaylist.hasItems: boolean;
begin
result := FPlaylist.count > 0;
end;
function TPlaylist.indexOf(const anItem: string): integer;
begin
result := FPlaylist.indexOf(anItem);
end;
function TPlaylist.insert(const anItem: string): boolean;
// insert at FPlayIx + 1, after the current item
begin
result := FALSE;
case isLast of TRUE: FPlaylist.add(anItem);
FALSE: FPlaylist.insert(FPlayIx, anItem); end;
result := FPlaylist.count > 0;
end;
function TPlaylist.isFirst: boolean;
begin
result := FPlayIx = 0;
end;
function TPlaylist.isLast: boolean;
begin
result := FPlayIx = FPlaylist.count - 1;
end;
function TPlaylist.isSpecialImage: boolean;
begin
result := FALSE;
var vExt := lowerCase(extractFileExt(currentItem)) + ' ';
result := '.avif .webp .png '.contains(vExt);
end;
function TPlaylist.last: boolean;
begin
result := FALSE;
case hasItems of TRUE: FPlayIx := FPlaylist.count - 1;
FALSE: FPlayIx := -1; end;
result := FPlayIx <> -1;
end;
function TPlaylist.next(const aMediaType: TMediaType = mtUnk): boolean;
function findNext: boolean;
var vMediaType: TMediaType;
begin
result := FALSE;
repeat
inc(FPlayIx);
vMediaType := MT.mediaType(currentItem);
case isLast and NOT (aMediaType in [mtUnk, vMediaType]) of TRUE: EXIT; end;
until (aMediaType in [mtUnk, vMediaType]) or isLast; // order of shortcut logic is important here
result := TRUE;
end;
begin
result := FALSE;
case hasItems of FALSE: EXIT; end;
case isLast of TRUE: EXIT; end;
case findNext of FALSE: EXIT; end;
result := TRUE;
end;
function TPlaylist.notify(const aNotice: INotice): INotice;
begin
result := onNotify(aNotice);
end;
function TPlaylist.onNotify(const aNotice: INotice): INotice;
begin
result := aNotice;
case aNotice = NIL of TRUE: EXIT; end;
case aNotice.event of
evPLCopyToClipboard: aNotice.text := copyToClipboard;
evPLDeleteIx: aNotice.tf := deleteIx(aNotice.integer);
evPLFillPlaylist: fillPlaylist(aNotice.text, [aNotice.mediaType]);
evPLFillListbox: getPlaylist(aNotice.component as TListBox);
evPLFind: aNotice.tf := find(aNotice.text);
evPLFirst: aNotice.tf := first;
evPLLast: aNotice.tf := last;
evPLNext: aNotice.tf := next(aNotice.mediaType);
evPLPrev: aNotice.tf := prev;
evPLReplaceCurrentItem: replaceCurrentItem(aNotice.text);
evPLReqCurrentFolder: aNotice.text := currentFolder;
evPLReqCurrentIx: aNotice.integer := FPlayIx;
evPLReqHasItems: aNotice.tf := hasItems;
evPLReqIsLast: aNotice.tf := isLast;
evPLReqIsSpecialImage: aNotice.tf := isSpecialImage;
evPLReqThisItem: aNotice.text := thisItem(aNotice.integer);
evPLReqCurrentItem: aNotice.text := currentItem;
evPLReqFormattedItem: aNotice.text := formattedItem; // return to sender
end;
result := aNotice;
end;
function TPlaylist.notifyEx(const aNotice: INotice; const aSetOfMediaType: TSetOfMediaType): INotice;
begin
result := aNotice;
case aNotice = NIL of TRUE: EXIT; end;
case aNotice.event of
evPLFillPlaylist: fillPlaylist(aNotice.text, aSetOfMediaType);
end;
end;
function TPlaylist.prev: boolean;
begin
result := FALSE;
case hasItems of FALSE: EXIT; end;
case isFirst of TRUE: EXIT; end;
dec(FPlayIx);
result := TRUE;
end;
function TPlaylist.replaceCurrentItem(const aNewItem: string): boolean;
begin
result := FALSE;
case hasItems of FALSE: EXIT; end;
FPlaylist[FPlayIx] := aNewItem;
result := TRUE;
end;
procedure TPlaylist.setCurrentFolder(const aValue: string);
begin
FCurrentFolder := aValue;
// FNotifier.notifySubscribers(newNotice(evPLCurrentFolder, aValue)); // CHECK THIS
// notifyApp(newNotice(evPLCurrentFolder, aValue));
end;
function TPlaylist.setIx(const ix: integer): integer;
begin
result := -1;
case validIx(ix) of TRUE: FPlayIx := ix;
FALSE: first; end;
result := FPlayIx;
end;
function TPlaylist.sort: boolean;
begin
result := FALSE;
FPlaylist.naturalSort;
result := TRUE;
end;
function TPlaylist.thisItem(const ix: integer): string;
begin
result := '';
case hasItems of FALSE: EXIT; end;
case (ix < 0) or (ix > FPlaylist.count - 1) of TRUE: EXIT; end;
result := FPlaylist[ix];
end;
function TPlaylist.validIx(const ix: integer): boolean;
begin
result := FALSE;
case hasItems of FALSE: EXIT; end;
case (ix < 0) or (ix > FPlaylist.count - 1) of TRUE: EXIT; end;
result := TRUE;
end;
end.