-
Notifications
You must be signed in to change notification settings - Fork 8
/
UConfigDir.pas
289 lines (246 loc) · 6.89 KB
/
UConfigDir.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
unit UConfigDir;
interface
// All functions return a path with delimiter at the end
function GetConfigPath(const Filename: String; DoCopy: Boolean): String;
function GetConfigDir: String;
function GetApplicationDir: string;
function GetResourceDir: string;
function GetMusicDir: string;
function GetMovieDir: string;
function GetTempDir: String;
function GetRecordingDir: String;
implementation
uses System.SysUtils, System.IOUtils
{$IFDEF MACOS}, MacAPI.CoreFoundation, MacAPI.Foundation, MacAPI.ObjectiveC
{$ENDIF}
{$IFDEF MSWINDOWS}, Winapi.Windows {$ENDIF};
function GetApplicationDir: string;
begin
Result := ExtractFilePath(Paramstr(0));
end;
{$IFDEF PORTABLE}
function GetConfigPath(const Filename: String; DoCopy: Boolean): String;
begin
Result := GetConfigDir + Filename;
end;
function GetConfigDir: String;
begin
Result := GetApplicationDir;
end;
function GetResourceDir: string;
begin
Result := GetApplicationDir;
end;
function GetMusicDir: string;
begin
Result := GetApplicationDir;
end;
function GetMovieDir: string;
begin
Result := GetApplicationDir;
end;
function GetTempDir: String;
begin
Result := GetApplicationDir;
end;
{$ELSE}
var
FApplicationName: String = '';
{$IFDEF MACOS}
{$IFNDEF IOS}
function CFToDelphiString(const CFStr: CFStringRef): string;
var
Range: CFRange;
begin
Range.location := 0;
Range.length := CFStringGetLength(CFStr);
SetLength(Result, Range.length);
if Range.length = 0 then
Exit;
CFStringGetCharacters(CFStr, Range, PWideChar(Result));
end;
function NSToDelphiString(const NSStr: NSString): string; inline;
begin
Result := CFToDelphiString((NSStr as ILocalObject).GetObjectID);
end;
{$ENDIF}
{$ENDIF}
{$IFDEF MACOS}
var
FResourceDir: String = '';
{$ENDIF}
function GetResourceDir: string;
{$IFDEF MACOS}
var
NS: NSBundle;
AutoReleasePool: NSAutoreleasePool;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
Result := GetApplicationDir;
{$ENDIF}
{$IFDEF MACOS}
if FResourceDir = '' then
begin
AutoReleasePool := TNSAutoreleasePool.Create;
try
NS := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
FResourceDir := NSToDelphiString(NS.ResourcePath) + '/';
finally
AutoReleasePool.release;
end;
end;
Result := FResourceDir;
{$ENDIF}
end;
function GetConfigPath(const Filename: String; DoCopy: Boolean): String;
var
SourceDir, TargetDir: String;
SearchRec: TSearchRec;
DosError: Integer;
begin
Result := GetConfigDir + Filename;
if (DoCopy) and (not FileExists(Result)) then
begin
try
TargetDir := IncludeTrailingPathDelimiter(ExtractFileDir(Result));
if not DirectoryExists(TargetDir) then
ForceDirectories(TargetDir);
SourceDir := IncludeTrailingPathDelimiter
(GetResourceDir + ExtractFileDir(Filename));
DosError := FindFirst(GetResourceDir + Filename, faAnyFile, SearchRec);
while DosError = 0 do
begin
if (SearchRec.Attr <> faDirectory) and
(not FileExists(TargetDir + SearchRec.Name)) then
TFile.Copy(SourceDir + SearchRec.Name, TargetDir + SearchRec.Name);
DosError := FindNext(SearchRec);
end;
System.SysUtils.FindClose(SearchRec);
except
end;
end;
end;
// -- Platform dependend code --------------------------------------------------
{$IFDEF MACOS}
function CFStringGetValue(const CFStr: CFStringRef): string;
var
Range: CFRange;
begin
if CFStr = nil then
Exit('');
Range.location := 0;
Range.length := CFStringGetLength(CFStr);
SetLength(Result, Range.length);
CFStringGetCharacters(CFStr, Range, PChar(Result));
end;
function GetBundleIdentifier: String;
begin
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
Function GetFileInfo(out CompanyName, ProductName: String;
Filename: String = ''): Boolean;
Var
VersionInfoSize, VerInfoSize, GetInfoSizeJunk: LongWord;
VersionInfo, Translation, InfoPointer: Pointer;
VersionValue: String;
Begin
Result := False;
ProductName := '';
CompanyName := '';
if Filename = '' then
Filename := Paramstr(0);
VerInfoSize := GetFileVersionInfoSize(PChar(Filename), GetInfoSizeJunk);
If VerInfoSize > 0 Then
Begin
GetMem(VersionInfo, VerInfoSize);
Try
GetFileVersionInfo(PChar(Filename), 0, VerInfoSize, VersionInfo);
VerQueryValue(VersionInfo, '\\VarFileInfo\\Translation', Translation,
VerInfoSize);
VersionValue := '\\StringFileInfo\\' +
IntToHex((PLongInt(Translation)^ shl 16) or
(PLongInt(Translation)^ shr 16), 8) + '\\';
VersionInfoSize := 0;
VerQueryValue(VersionInfo, PWideChar(VersionValue + 'ProductName'),
InfoPointer, VersionInfoSize);
if VersionInfoSize > 0 then
ProductName := Trim(PWideChar(InfoPointer));
VerQueryValue(VersionInfo, PWideChar(VersionValue + 'CompanyName'),
InfoPointer, VersionInfoSize);
if VersionInfoSize > 0 then
CompanyName := Trim(PWideChar(InfoPointer));
Finally
FreeMem(VersionInfo);
End;
Result := (CompanyName <> '') and (ProductName <> '');
End;
End;
{$ENDIF}
procedure Prepare;
{$IFDEF MACOS}
var
BundleHandle: CFBundleRef;
{$ENDIF}
{$IFDEF MSWINDOWS}
var
Product, Company: String;
{$ENDIF}
begin
{$IFDEF MACOS}
BundleHandle := CFBundleGetMainBundle;
FApplicationName := '';
if BundleHandle <> nil then
FApplicationName := CFStringGetValue(CFBundleGetIdentifier(BundleHandle));
{$ENDIF}
{$IFDEF MSWINDOWS}
if GetFileInfo(Company, Product) then
FApplicationName := Company + PathDelim + Product;
{$ENDIF}
if FApplicationName = '' then // Fallback
FApplicationName := ExtractFileName(ChangeFileExt(Paramstr(0), ''));
end;
function GetConfigDir: String;
begin
{$IFDEF MACOS}
{$IFDEF IOS}
Result := TPath.GetHomePath + PathDelim + 'Documents' + PathDelim;
{$ELSE}
Result := TPath.GetHomePath + PathDelim + 'Library/Application Support/' +
FApplicationName + PathDelim;
{$ENDIF}
{$ENDIF}
{$IFDEF MSWINDOWS}
Result := TPath.GetPublicPath + PathDelim + FApplicationName + PathDelim;
{$ENDIF}
{$IFDEF ANDROID}
Result := TPath.GetHomePath + PathDelim;
{$ENDIF}
if not DirectoryExists(Result) then
ForceDirectories(Result);
end;
function GetTempDir: String;
begin
Result := IncludeTrailingPathDelimiter(TPath.GetTempPath);
end;
function GetMusicDir: string;
begin
Result := IncludeTrailingPathDelimiter(TPath.GetMusicPath);
end;
function GetMovieDir: string;
begin
Result := IncludeTrailingPathDelimiter(TPath.GetMoviesPath);
end;
function GetRecordingDir: String;
begin
Result := IncludeTrailingPathDelimiter(TPath.Combine(TPath.GetMoviesPath,
'SAT2IP Viewer recordings'));
if not DirectoryExists(Result) then
ForceDirectories(Result);
end;
initialization
Prepare;
finalization
{$ENDIF}
end.