Repository navigation
Expand file tree
/
Copy pathmodel.mmpConfigFile.pas
More file actions
262 lines (218 loc) · 8.7 KB
/
Copy pathmodel.mmpConfigFile.pas
File metadata and controls
262 lines (218 loc) · 8.7 KB
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
{ MMP: Minimalist Media Player
Copyright (C) 2021-2099 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.mmpConfigFile;
interface
uses
system.classes, system.ioUtils,
bazFuncDefs,
mmpConsts;
type
IConfigFile = interface
function getAsBoolean (const aName: string): boolean;
procedure setAsBoolean (const aName: string; const aValue: boolean);
function getAsDeleteMethod (const aName: string): TDeleteMethod;
procedure setAsDeleteMethod (const aName: string; const aDeleteMethod: TDeleteMethod);
function getAsInteger (const aName: string): integer;
procedure setAsInteger (const aName: string; const aInteger: integer);
function getAsMediaType (const aName: string): TMediaType;
procedure setAsMediaType (const aName: string; const aMediaType: TMediaType);
function getValue (const aName: string): string;
procedure setValue (const aName: string; const aValue: string);
function deleteConfig (const aName: string): boolean;
function initConfigFile (const aFilePath: string): boolean;
function toHex (const aInteger: integer): string;
property asBoolean [const aName: string]: boolean read getAsBoolean write setAsBoolean;
property asDeleteMethod [const aName: string]: TDeleteMethod read getAsDeleteMethod write setAsDeleteMethod;
property asInteger [const aName: string]: integer read getAsInteger write setAsInteger;
property asMediaType [const aName: string]: TMediaType read getAsMediaType write setAsMediaType;
property value [const aName: string]: string read getValue write setValue; default;
end;
function CF: IConfigFile;
function mmpConfigClearWindow: TVoid;
function mmpConfigMaxWindow: boolean;
function mmpConfigWindowHeight: integer;
implementation
uses
system.math, system.sysUtils,
bazCmd,
_debugWindow;
type
TConfigFile = class(TInterfacedObject, IConfigFile)
strict private
FFileContents: TStringList;
FFilePath: string;
FLastWriteTime: TDateTime;
private
function checkForManualEdits: boolean;
function saveConfigFile: boolean;
public
constructor Create;
destructor Destroy; override;
function getAsBoolean (const aName: string): boolean;
procedure setAsBoolean (const aName: string; const aValue: boolean);
function getAsDeleteMethod (const aName: string): TDeleteMethod;
procedure setAsDeleteMethod (const aName: string; const aDeleteMethod: TDeleteMethod);
function getAsInteger (const aName: string): integer;
procedure setAsInteger (const aName: string; const aInteger: integer);
function getAsMediaType (const aName: string): TMediaType;
procedure setAsMediaType (const aName: string; const aMediaType: TMediaType);
function getValue (const aName: string): string;
procedure setValue (const aName: string; const aValue: string);
function deleteConfig (const aName: string): boolean;
function initConfigFile (const aFilePath: string): boolean;
function toHex (const aInteger: integer): string;
end;
function CF: IConfigFile;
{$J+} const gCF: IConfigFile = NIL; {$J-}
begin
case gCF = NIL of TRUE: gCF := TConfigFile.create; end;
result := gCF;
end;
function mmpConfigClearWindow: TVoid;
begin
CF[CONF_WINDOW_HEIGHT] := '-1';
CF[CONF_WINDOW_LEFT] := '-1';
CF[CONF_WINDOW_TOP] := '-1';
end;
function mmpConfigMaxWindow: boolean;
begin
result := mmpConfigWindowHeight = -1;
end;
function mmpConfigWindowHeight: integer;
begin
case CF[CONF_WINDOW_HEIGHT] = EMPTY of TRUE: result := -1;
FALSE: result := CF.asInteger[CONF_WINDOW_HEIGHT]; end;
case result = -1 of TRUE: EXIT; end;
result := max(UI_DEFAULT_AUDIO_HEIGHT + 1, CF.asInteger[CONF_WINDOW_HEIGHT]); // prevent the audio player from setting the window too short
end;
{ TConfigFile }
constructor TConfigFile.Create;
begin
inherited;
FFileContents := TStringList.create;
FFileContents.defaultEncoding := TEncoding.UTF8;
FFileContents.writeBOM := FALSE;
FFileContents.caseSensitive := FALSE;
end;
function TConfigFile.checkForManualEdits: boolean;
begin
result := FALSE;
case fileExists(FFilePath) of FALSE: EXIT; end;
var vLastWriteTime := TFile.getLastWriteTime(FFilePath);
mmp.cmd(vLastWriteTime > FLastWriteTime, procedure begin try FFileContents.loadFromFile(FFilePath); except end;end);
FLastWriteTime := vLastWriteTime;
result := TRUE;
end;
function TConfigFile.deleteConfig(const aName: string): boolean;
begin
checkForManualEdits;
var vIx := FFileContents.indexOfName(aName);
case vIx <> -1 of TRUE: begin
FFileContents.delete(vIx);
saveConfigFile; end;end;
result := TRUE;
end;
destructor TConfigFile.Destroy;
begin
case FFileContents = NIL of FALSE: FFileContents.free; end;
inherited;
end;
function TConfigFile.getAsBoolean(const aName: string): boolean;
begin
checkForManualEdits;
result := lowerCase(FFileContents.values[aName]) = 'yes';
end;
function TConfigFile.getAsDeleteMethod(const aName: string): TDeleteMethod;
begin
checkForManualEdits;
var vDeleteMethod := lowercase(FFileContents.values[aName]);
result := dmRecycle;
case vDeleteMethod = 'recycle' of TRUE: result := dmRecycle; end;
case vDeleteMethod = 'delete' of TRUE: result := dmStandard; end;
case vDeleteMethod = 'shred' of TRUE: result := dmShred; end;
end;
function TConfigFile.getAsInteger(const aName: string): integer;
begin
checkForManualEdits;
try result := strToIntDef(FFileContents.values[aName], 0); except result := 0; end;
end;
function TConfigFile.getAsMediaType(const aName: string): TMediaType;
begin
checkForManualEdits;
var vPlaylistFormat := lowercase(FFileContents.values[aName]);
result := mtUnk;
case vPlaylistFormat = 'audio' of TRUE: result := mtAudio; end;
case vPlaylistFormat = 'video' of TRUE: result := mtVideo; end;
case vPlaylistFormat = 'image' of TRUE: result := mtImage; end;
case vPlaylistFormat = 'audiovideo' of TRUE: result := mtAudioVideo; end;
end;
function TConfigFile.getValue(const aName: string): string;
begin
checkForManualEdits;
result := FFileContents.values[aName]; // don't trim!
end;
function TConfigFile.initConfigFile(const aFilePath: string): boolean;
begin
FFilePath := aFilePath;
result := TRUE;
end;
function TConfigFile.saveConfigFile: boolean;
begin
checkForManualEdits;
try
FFileContents.saveToFile(FFilePath);
except // trap rapid-fire write errors, e.g. when user holds down ctrl-B
end;
result := TRUE;
end;
procedure TConfigFile.setAsBoolean(const aName: string; const aValue: boolean);
begin
case aValue of TRUE: setValue(aName, 'yes');
FALSE: setValue(aName, 'no'); end;
end;
procedure TConfigFile.setAsDeleteMethod(const aName: string; const aDeleteMethod: TDeleteMethod);
begin
case aDeleteMethod of
dmRecycle: setValue(aName, 'recycle');
dmStandard: setValue(aName, 'delete');
dmShred: setValue(aName, 'shred');
end;
end;
procedure TConfigFile.setAsInteger(const aName: string; const aInteger: integer);
begin
setValue(aName, intToStr(aInteger));
end;
procedure TConfigFile.setAsMediaType(const aName: string; const aMediaType: TMediaType);
begin
case aMediaType of
mtUnk: setValue(aName, 'all');
mtAudio: setValue(aName, 'audio');
mtVideo: setValue(aName, 'video');
mtImage: setValue(aName, 'image');
mtAudioVideo: setValue(aName, 'audiovideo');
end;
end;
procedure TConfigFile.setValue(const aName: string; const aValue: string);
begin
checkForManualEdits;
FFileContents.values[aName] := aValue;
saveConfigFile;
end;
function TConfigFile.toHex(const aInteger: integer): string;
begin
result := '$' + intToHex(aInteger);
end;
end.