-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxtSOUtils.pas
512 lines (443 loc) · 13.3 KB
/
xtSOUtils.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
unit xtSOUtils;
interface
uses System.Types
{$IFDEF MACOS}
{$IFDEF IOS}
, iOSapi.Foundation, Macapi.Helpers, FMX.Helpers.iOS
{$ELSE}
, Macapi.CocoaTypes, Macapi.Helpers
{$ENDIF}
{$ENDIF}
{$IFDEF MSWINDOWS}
, FMX.Platform.Win, WINDOWS, ShellAPI, PsAPI
{$ENDIF}
{$IFDEF ANDROID}
, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Os
{$ENDIF}
,FMX.Types;
type
{$IFDEF MSWINDOWS}
TWinVersion = (wvUnknown, wvWin95, wvWin98, wvWin98SE, wvWinNT, wvWinME,
wvWin2000, wvWinXP, wvWinVista, wvWin7, wvWin8, wvWin10);
{$ELSE}
HWND = type System.UIntPtr; // NativeUInt;
{$ENDIF}
TSOVersion = record
SOType: String;
VersionDescription: String;
VerisonName: String;
VersionClass: Integer; // 1 = WinNT Class, 0 = oldWinClass
MajorNumber: Integer;
MinorNumber: Integer;
Supported: Boolean;
{$IFDEF MSWINDOWS}
WinVer: TWinVersion;
{$ENDIF}
end;
TSynapticaSOUtils = class
public
class function GetAppVersionStr: string;
class function getSOVersion : TSOVersion;
class function executeFile(FileName: String; Handle: TWindowHandle = nil;
executeAsAdmin: Boolean = false;
ForceRunWithDefaultApp: Boolean = True): Boolean;
class function RunAsAdmin(hWnd: HWND; filename: string; Parameters: string): Boolean;
class function UnzipFile(ArcFileName,PathDest : String): Boolean;
end;
Var
{$IFDEF MSWINDOWS}
winVer: TWinVersion;
{$ENDIF}
SOVersion: TSOVersion;
InPlay: Boolean;
implementation
uses
System.Classes, System.SysUtils, IdGlobalProtocols, System.IOUtils,
System.zip, idUri
{$IFDEF MSWINDOWS}
, TlHelp32, Winapi.Messages,
System.UITypes, idStack, System.Win.Registry
{$ENDIF}
{$IFDEF LINUX}
, idStack
, uLinuxExecCommand
{$ENDIF}
{$IFDEF MACOS}
, Macapi.CoreFoundation,
{$IFNDEF IOS}
Macapi.CoreGraphics,
Macapi.ImageIO,
Macapi.AppKit,
Macapi.QuartzCore,
Macapi.OpenGL,
Macapi.KeyCodes,
{$ENDIF}
Posix.StdLib,
Posix.Unistd,
idStack
{$ENDIF}
{$IFDEF ANDROID}
, Androidapi.JNI.media, FMX.Helpers.Android, Androidapi.JNI.JavaTypes,
Androidapi.JNIBridge,
Androidapi.Helpers, Androidapi.JNI.Net, Androidapi.JNI,
Androidapi.Input, Androidapi.NativeActivity, QAndroid.Shell, fedeWrapper
{$ENDIF}
;
{ TSynapticaSOUtils }
class function TSynapticaSOUtils.executeFile(FileName: String;
Handle: TWindowHandle; executeAsAdmin,
ForceRunWithDefaultApp: Boolean): Boolean;
{$IFDEF ANDROID}
var
Intent: JIntent;
idMimeTable: TIdMimeTable;
PM : JPackageManager;
lst : JList;
itr : Jiterator;
ri : JResolveInfo;
ts: TStringList;
{$ENDIF}
{$IFDEF IOS}
var
NSU: NSUrl;
{$ENDIF}
{$IFDEF MSWINDOWS}
procedure WindowsExec(fname : String;runAsAdministrator : Boolean);
var
go: Boolean;
Begin
if winVer = wvUnknown then
Begin
SOVersion := getSOVersion;
End;
if runAsAdministrator then
Begin
if ((winVER in [wvWin7,wvWinVista,wvWin8])or(winVER > wvWin8)) then
go := RunAsAdmin(FmxHandleToHWND(Handle),fname,'/DIR="' + ExtractFileDir(fname) + '" /LOG="' + ExtractFileDir(fname) + '\UPGP.LOG"')
else
ShellExecute(0,'open',PChar(fname),PChar('/DIR="' + ExtractFileDir(fname) + '" /LOG="' + ExtractFileDir(fname) + '\UPGP.LOG"'),PChar( ExtractFileDir(fname)),sw_shownormal);
End
Else
ShellExecute(0, 'open', pWideChar(ExtractFileName(fname.Replace('\\','\'))), '',
pWideChar(ExtractFileDir(fname).Replace('\\','\')), SW_SHOW);
End;
{$ENDIF}
begin
{$IFDEF LINUX}
ExecPCommand(FileName);
{$ENDIF}
{$IFDEF MACOS}
(* ***** SHELLEXECUTE EQUIVALENT FOR MACOSX ****** *)
(* - E' L'EQUIVALENTE DI LANCIARE UN COMANDO DA SHELL
- IL PARAMETRO -N (APRE IL FILE ANCHE SE E' GIA' APERTO)
*)
_system(PAnsiChar('open ' + '"' + AnsiString(FileName) + '" -F'));
{$ENDIF}
{$IFDEF MSWINDOWS}
if FileName.ToLower.StartsWith('http') then
Begin
ShellExecute(0, 'open', PChar(FileName), '',
'', SW_SHOW);
End
Else
Begin
WindowsExec(FileName,executeAsAdmin);
End;
{$ENDIF}
{$IFDEF ANDROID}
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
(*
intent = new Intent(context, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
*)
(*
**************************************
CERCO DI PORTARE REMODE IN PRIMO PIANO
*)
try
Intent := TJIntent.JavaClass.init(SharedActivityContext,SharedActivityContext.getClass);
intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(TJIntent.JavaClass.ACTION_MAIN);
intent.addCategory(TJIntent.JavaClass.CATEGORY_LAUNCHER);
SharedActivity.startActivity(Intent);
Except
On E:Exception do
Begin
End;
End;
InPlay := True;
if (ExtractFileExt(FileName).ToLower = '.html')and(not(FileName.toLower.StartsWith('http://'))) then
Begin
{ frmWebBrowser := TfrmWebBrowser.Create(nil);
ts := TStringList.Create;
try
ts.LoadFromFile(FileName);
frmWebBrowser.WebBrowser1.LoadFromStrings(ts.Text,'http://localhost:9999');
//frmWebBrowser.WebBrowser1.URL := FileName;
//frmWebBrowser.WebBrowser1.Navigate;
frmWebBrowser.Show;
finally
ts.Free;
End;
}
End
else if FileName.toLower.StartsWith('http://')or(FileName.toLower.StartsWith('https://')) then
Begin
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW, TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(FileName))));
End
Else
Begin
try
idMimeTable := TidMimeTable.Create;
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
if not FileName.ToLower.StartsWith('file://') then
FileName := 'file://' + FileName;
Intent.setDataAndType(StrToJURI(FileName), StringToJString(idMimeTable.GetFileMIMEType(FileName)));
Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
if ForceRunWithDefaultApp then
Begin
PM := SharedActivity.getPackageManager;
lst := PM.queryIntentActivities(Intent,0);
itr := lst.iterator;
if lst.size >0 then
Begin
ri := TJResolveInfo.Wrap((itr.next as ILocalObject).getObjectId);
Intent.setPackage(ri.activityInfo.applicationInfo.packageName);
End;
End;
Finally
try idMimeTable.Free; except end;
End;
End;
try
SharedActivity.startActivity(Intent);
exit(true);
except
(* on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end; *)
end;
{$ENDIF}
{$IFDEF IOS}
// iOS doesn't like spaces, so URL encode is important.
NSU := StrToNSUrl(TIdURI.URLEncode(Filename));
if SharedApplication.canOpenURL(NSU) then
exit(SharedApplication.openUrl(NSU))
else
begin
// if DisplayError then
// ShowMessage('Error: Opening "' + URL + '" not supported.');
exit(false);
end;
{$ENDIF}
end;
class function TSynapticaSOUtils.GetAppVersionStr: string;
{$IFDEF MACOS}
var
CFStr: CFStringRef;
Range: CFRange;
begin
CFStr := CFBundleGetValueForInfoDictionaryKey(
CFBundleGetMainBundle, kCFBundleVersionKey);
Range.location := 0;
Range.length := CFStringGetLength(CFStr);
SetLength(Result, Range.length);
CFStringGetCharacters(CFStr, Range, PChar(Result));
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
var
Exe: string;
Size, Handle: DWORD;
Buffer: TBytes;
FixedPtr: PVSFixedFileInfo;
begin
Exe := ParamStr(0);
Size := GetFileVersionInfoSize(PChar(Exe), Handle);
if Size = 0 then
RaiseLastOSError;
SetLength(Buffer, Size);
if not GetFileVersionInfo(PChar(Exe), Handle, Size, Buffer) then
RaiseLastOSError;
if not VerQueryValue(Buffer, '\', Pointer(FixedPtr), Size) then
RaiseLastOSError;
Result := Format('%d.%d.%d.%d',
[LongRec(FixedPtr.dwFileVersionMS).Hi, //major
LongRec(FixedPtr.dwFileVersionMS).Lo, //minor
LongRec(FixedPtr.dwFileVersionLS).Hi, //release
LongRec(FixedPtr.dwFileVersionLS).Lo]) //build
end;
{$ENDIF}
{$IFDEF ANDROID}
{code}
var
PackageManager: JPackageManager;
PackageInfo: JPackageInfo;
begin
PackageManager := SharedActivityContext.getPackageManager;
PackageInfo := PackageManager.getPackageInfo
(SharedActivityContext.getPackageName, 0);
result := JStringToString(PackageInfo.versionName);
End;
{$ENDIF}
{$IFDEF LINUX64}
var
RS : TResourceStream;
TS : TStringList;
begin
RS := nil;
ts := nil;
Result := '';
try
ts := TStringList.Create;
RS := TResourceStream.Create(HInstance, 'versione', RT_RCDATA);
ts.LoadFromStream(RS);
Result := ts.Text;
Finally
if RS <> nil then
try
RS.Free;
Except
End;
if TS <> nil then
try
TS.Free;
Except
End;
End;
// result := '';
// raise Exception.Create('not implemented');
End;
{$ENDIF}
// codeverge.com/embarcadero.delphi.firemonkey/ios-and-android-get-application/1060702#sthash.WwHh1av9.dpuf
(*
{$IFDEF IOS} Result := TNSString.Wrap(CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle, kCFBundleVersionKey)).UTF8String; {$ENDIF} - See more at: http://codeverge.com/embarcadero.delphi.firemonkey/ios-and-android-get-application/1060702#sthash.fo31DA7J.dpuf
*)
class function TSynapticaSOUtils.getSOVersion: TSOVersion;
{$IFDEF MSWINDOWS}
type
TIntSOVer = record
WinVer : TWinVersion;
MajorVersion : Integer;
MinorVersion : Integer;
VersionClass : Integer; // 1 = WinNT Class, 0 = oldWinClass
end;
function GetWinVersion: TIntSOVer;
var
osVerInfo: TOSVersionInfo;
majorVersion, minorVersion: Integer;
begin
Result.WinVer := wvUnknown;
osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo) ;
if GetVersionEx(osVerInfo) then
begin
minorVersion := osVerInfo.dwMinorVersion;
majorVersion := osVerInfo.dwMajorVersion;
Result.MajorVersion := majorVersion;
Result.MinorVersion := minorVersion;
case osVerInfo.dwPlatformId of
VER_PLATFORM_WIN32_NT:
begin
Result.VersionClass := 1;
if majorVersion <= 4 then
Result.WinVer := wvWinNT
else if (majorVersion = 5) and (minorVersion = 0) then
Result.WinVer := wvWin2000
else if (majorVersion = 5) and (minorVersion = 1) then
Result.WinVer := wvWinXP
else if (majorVersion = 6) then
Result.WinVer := wvWinVista
else if (majorVersion = 7) then
Result.WinVer := wvWin7;
end;
VER_PLATFORM_WIN32_WINDOWS:
begin
Result.VersionClass := 0;
if (majorVersion = 4) and (minorVersion = 0) then
Result.WinVer := wvWin95
else if (majorVersion = 4) and (minorVersion = 10) then
begin
if osVerInfo.szCSDVersion[1] = 'A' then
Result.WinVer := wvWin98SE
else
Result.WinVer := wvWin98;
end
else if (majorVersion = 4) and (minorVersion = 90) then
Result.WinVer := wvWinME
else
Result.WinVer := wvUnknown;
end;
end;
end;
end;
var
wVer: TIntSOVer;
{$ENDIF}
Begin
{$IFDEF MSWINDOWS}
wVer := GetWinVersion;
Result.SOType := 'MSWINDOWS';
Result.VersionDescription := '';
Result.VerisonName := '';
Result.VersionClass := wVer.VersionClass;
Result.MajorNumber := wVer.MajorVersion;
Result.MinorNumber := wVer.MinorVersion;
Result.winVer := wVer.winVer;
Result.Supported := True;
winVer := wVer.WinVer;
{$ELSE}
raise Exception.Create('not implemented');
Result.Supported := False;
{$ENDIF}
End;
class function TSynapticaSOUtils.RunAsAdmin(hWnd: HWND; filename,
Parameters: string): Boolean;
{$IFDEF MSWINDOWS}
var
sei: TShellExecuteInfo;
begin
ZeroMemory(@sei, SizeOf(sei));
sei.cbSize := SizeOf(TShellExecuteInfo);
sei.Wnd := hwnd;
sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
sei.lpVerb := PChar('runas');
sei.lpFile := PChar(Filename); // PAnsiChar;
if parameters <> '' then
sei.lpParameters := PChar(parameters); // PAnsiChar;
sei.nShow := SW_SHOWNORMAL; //Integer;
Result := ShellExecuteEx(@sei);
end;
{$ELSE}
Begin
result := False;
End;
{$ENDIF}
class function TSynapticaSOUtils.UnzipFile(ArcFileName,
PathDest: String): Boolean;
var zf:TZipFile;
begin
zf:= TZipFile.Create;
Try
Try
Result := False;
If ArcFileName <> '' then
Begin
if zf.IsValid(ArcFileName) then
zf.ExtractZipFile(ArcFileName,PathDest);
Result := zf.FileCount > 0;
End;
Except
Result := False;
End;
Finally
zf.free;
End;
End;
end.