-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.Utils.pas
More file actions
111 lines (96 loc) · 2.73 KB
/
Copy pathCommon.Utils.pas
File metadata and controls
111 lines (96 loc) · 2.73 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
unit Common.Utils;
interface
uses
System.Classes,
System.SysUtils,
System.UITypes, System.IOUtils
{$IFDEF MSWINDOWS}
, Winapi.Windows, Winapi.ShellAPI
{$ENDIF}
{$IFDEF ANDROID}
, Androidapi.Helpers, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net, Androidapi.JNI.JavaTypes, Androidapi.JNI.Support
{$ENDIF}
{$IFDEF IOS}
, iOSapi.Foundation, FMX.Helpers.iOS, Macapi.Helpers
{$ENDIF}
{$IFDEF MACOS}
{$IFNDEF IOS}
, Macapi.AppKit, Macapi.Foundation, Macapi.Helpers
{$ENDIF}
{$ENDIF};
function GetInitiales( aName: string ): string;
function HtmlColorToAlphaColor( const HtmlColor: string ): TAlphaColor;
function SecondsToDateTime( const aNbSeconds: Integer ): TDateTime;
function OpenDocument(const AFileName: string): Boolean;
implementation
function GetInitiales( aName: string ): string;
begin
var LFirst := aName[ 1 ];
var LPos := pos( ' ', aName );
if ( LPos > 0 ) and ( LPos < Length( aName ) ) then
begin
repeat
Inc( LPos );
until ( aName[ lpos ] <> ' ' );
var LSecond := aName[ LPos ];
Exit( UpperCase( LFirst + LSecond ) );
end
else if ( Length( aName ) > 1 ) then
begin
Exit( aName.Substring( 0, 2 ).ToUpper );
end
else
begin
Exit( aName );
end;
end;
function HtmlColorToAlphaColor( const HtmlColor: string ): TAlphaColor;
var
Hex: string;
begin
Hex := HtmlColor;
if ( Length( Hex ) > 0 ) and ( Hex[ 1 ] = '#' ) then
Delete( Hex, 1, 1 );
// On préfixe FF pour l'alpha (opaque)
Result := TAlphaColor( StrToInt( '$' + Hex ) );
end;
function SecondsToDateTime( const aNbSeconds: Integer ): TDateTime;
var
Lh, Lm, Ls: Integer;
begin
Lh := aNbSeconds div 3600;
Lm := ( aNbSeconds mod 3600 ) div 60;
Ls := aNbSeconds mod 60;
if not ( TryEncodeTime( Lh, Lm, Ls, 0, Result ) ) then
begin
Result := 0;
end;
end;
function OpenDocument(const AFileName: string): Boolean;
begin
Result := True;
{$IFDEF MSWINDOWS}
ShellExecute(0, 'open', PChar(AFileName), nil, nil, SW_SHOWNORMAL);
{$ENDIF}
{$IFDEF ANDROID}
// Nécessite un FileProvider (Android 7+) pour un fichier local en content://
var Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setDataAndType(
TAndroidHelper.JFileToJURI(TJFile.JavaClass.init(StringToJString(AFileName))),
StringToJString('application/pdf'));
Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
TAndroidHelper.Activity.startActivity(Intent);
{$ENDIF}
{$IFDEF IOS}
var Url := TNSUrl.Wrap(TNSUrl.OCClass.fileURLWithPath(StrToNSStr(AFileName)));
SharedApplication.openURL(Url);
{$ENDIF}
{$IFDEF MACOS}
{$IFNDEF IOS}
TNSWorkspace.Wrap(TNSWorkspace.OCClass.sharedWorkspace).openFile(StrToNSStr(AFileName));
{$ENDIF}
{$ENDIF}
end;
end.