forked from transmission-remote-gui/transgui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.pas
500 lines (426 loc) · 11.5 KB
/
utils.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
{*************************************************************************************
This file is part of Transmission Remote GUI.
Copyright (c) 2008-2010 by Yury Sidorov.
Transmission Remote GUI 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 2 of the License, or
(at your option) any later version.
Transmission Remote GUI 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 Transmission Remote GUI; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*************************************************************************************}
unit utils;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, Controls, Forms,
{$ifdef windows}
Windows, win32int, InterfaceBase
{$endif}
{$ifdef unix}
baseunix, unix, unixutil, process
{$endif}
;
type
{ TFileStreamUTF8 }
TFileStreamUTF8 = class(THandleStream)
private
FFileName: utf8string;
public
constructor Create(const AFileName: utf8string; Mode: Word);
constructor Create(const AFileName: utf8string; Mode: Word; Rights: Cardinal);
destructor Destroy; override;
property FileName: utf8string Read FFilename;
end;
function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle;
function FileCreateUTF8(Const FileName : string) : THandle;
function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle;
function GetTimeZoneDelta: TDateTime;
procedure ShowTaskbarButton;
procedure HideTaskbarButton;
function OpenURL(const URL: string; const Params: string = ''): boolean;
function UpdateGeoIp: integer;
function CompareFilePath(const p1, p2: string): integer;
procedure AppBusy;
procedure AppNormal;
procedure ForceAppNormal;
function ParamStrUTF8(Param: Integer): utf8string;
function ParamCount: integer;
function Base32Decode(const s: ansistring): ansistring;
{$ifdef mswindows}
procedure AllowSetForegroundWindow(dwProcessId: DWORD);
{$endif mswindows}
implementation
uses FileUtil;
{$ifdef windows}
function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle;
const
AccessMode: array[0..2] of Cardinal = (
GENERIC_READ,
GENERIC_WRITE,
GENERIC_READ or GENERIC_WRITE);
ShareMode: array[0..4] of Integer = (
0,
0,
FILE_SHARE_READ,
FILE_SHARE_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE);
begin
Result := CreateFileW(PWideChar(UTF8Decode(FileName)), dword(AccessMode[Mode and 3]),
dword(ShareMode[(Mode and $F0) shr 4]), nil, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
//if fail api return feInvalidHandle (INVALIDE_HANDLE=feInvalidHandle=-1)
end;
function FileCreateUTF8(Const FileName : string) : THandle;
begin
Result := CreateFileW(PWideChar(UTF8Decode(FileName)), GENERIC_READ or GENERIC_WRITE,
0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
end;
function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle;
begin
Result := CreateFileW(PWideChar(UTF8Decode(FileName)), GENERIC_READ or GENERIC_WRITE,
0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
end;
var
FParams: TStringList;
function ParamStrUTF8(Param: Integer): utf8string;
function SkipSpaces( P: PWideChar ): PWideChar;
begin
while True do
begin
while (P[0] <> #0) and (P[0] <= ' ') do Inc(P);
if (P[0] = '"') and (P[1] = '"') then Inc(P, 2) else Break;
end;
Result := P;
end;
function SkipParam(P: PWideChar): PWideChar;
begin
P := SkipSpaces( P );
while P[0] > ' ' do
if P[0] = '"' then
begin
Inc(P);
while (P[0] <> #0) and (P[0] <> '"') do
Inc(P);
if P[0] <> #0 then Inc(P);
end
else
Inc(P);
Result := P;
end;
var
P, P1: PWideChar;
s: widestring;
begin
if Win32Platform <> VER_PLATFORM_WIN32_NT then begin
Result:=FileUtil.ParamStrUTF8(Param);
exit;
end;
if FParams <> nil then begin
if Param >= FParams.Count then
Result:=''
else
Result:=FParams[Param];
exit;
end;
FParams:=TStringList.Create;
P := GetCommandLineW;
while True do begin
P := SkipSpaces( P );
P1 := P;
P := SkipParam(P);
if P = P1 then
break;
s := Copy( P1, 1, P - P1 );
if Length(s) >= 2 then
if (s[1] = '"') and (s[Length(s)] = '"') then
s:=Copy(s, 2, Length(s) - 2);
FParams.Add(UTF8Encode(s));
end;
end;
function ParamCount: integer;
begin
if Win32Platform <> VER_PLATFORM_WIN32_NT then
Result:=System.ParamCount
else begin
if FParams = nil then
ParamStrUTF8(0);
Result:=FParams.Count - 1;
end;
end;
{$else} // Non-Windows targets
function FileOpenUTF8(Const FileName : string; Mode : Integer) : THandle;
begin
Result:=FileOpen(FileName, Mode);
end;
function FileCreateUTF8(Const FileName : string) : THandle;
begin
Result:=FileCreate(FileName);
end;
function FileCreateUTF8(Const FileName : string; Rights: Cardinal) : THandle;
begin
Result:=FileCreate(FileName, Rights);
end;
function ParamStrUTF8(Param: Integer): utf8string;
begin
Result:=FileUtil.ParamStrUTF8(Param);
end;
function ParamCount: integer;
begin
Result:=System.ParamCount;
end;
{$endif windows}
{ TFileStreamUTF8 }
constructor TFileStreamUTF8.Create(const AFileName: utf8string; Mode: Word);
var
lHandle: THandle;
begin
FFileName:= AFileName;
if Mode = fmcreate then
lHandle:= FileCreateUTF8(AFileName)
else
lHandle:= FileOpenUTF8(AFileName, Mode);
If (THandle(lHandle)=feInvalidHandle) then
begin
if Mode = fmCreate then
raise EFCreateError.createfmt({SFCreateError}'Unable to create file "%s"', [AFileName])
else
raise EFOpenError.Createfmt({SFOpenError}'Unable to open file "%s"', [AFilename]);
end
else
inherited Create(lHandle);
end;
constructor TFileStreamUTF8.Create(const AFileName: utf8string; Mode: Word; Rights: Cardinal);
var
lHandle: THandle;
begin
FFileName:=AFileName;
if Mode=fmcreate then
lHandle:=FileCreateUTF8(AFileName,Rights)
else
lHandle:=FileOpenUTF8(AFileName,Mode);
if (THandle(lHandle)=feInvalidHandle) then
begin
if Mode=fmcreate then
raise EFCreateError.createfmt({SFCreateError}'Unable to create file "%s"',[AFileName])
else
raise EFOpenError.Createfmt({SFOpenError}'Unable to open file "%s"',[AFilename]);
end
else
inherited Create(lHandle);
end;
destructor TFileStreamUTF8.Destroy;
begin
FileClose(Handle);
end;
// ---------------------------------------------
function GetTimeZoneDelta: TDateTime;
{$ifdef windows}
var
t: TIME_ZONE_INFORMATION;
res: dword;
{$endif}
begin
Result:=0;
{$ifdef windows}
res:=GetTimeZoneInformation(t);
if res<> TIME_ZONE_ID_INVALID then begin
case res of
TIME_ZONE_ID_STANDARD:
Result:=-t.StandardBias;
TIME_ZONE_ID_DAYLIGHT:
Result:=-t.DaylightBias;
end;
Result:=(-t.Bias + Result)/MinsPerDay;
end;
{$endif}
{$ifdef unix}
Result:=Tzseconds/SecsPerDay;
{$endif}
end;
procedure ShowTaskbarButton;
begin
{$ifdef mswindows}
ShowWindow(TWin32WidgetSet(WidgetSet).AppHandle, SW_SHOW);
{$else}
Application.MainForm.Visible:=True;
{$endif mswindows}
end;
procedure HideTaskbarButton;
begin
{$ifdef mswindows}
ShowWindow(TWin32WidgetSet(WidgetSet).AppHandle, SW_HIDE);
{$else}
Application.MainForm.Visible:=False;
{$endif mswindows}
end;
{$ifdef unix}
type
TipoDeskMang = (dmUnknown, dmGnome, dmKde);
function DeterminaDesktopManager:TipoDeskMang;
begin
Result:=dmUnknown; //Desktop Manager sconosciuto
if (GetEnvironmentVariable('GNOME_DESKTOP_SESSION_ID') <> '') or
(GetEnvironmentVariable('DESKTOP_SESSION') = 'gnome') then begin
Result:=dmGnome;
end else begin
if (GetEnvironmentVariable('KDE_FULL_SESSION') <> '') or
(GetEnvironmentVariable('DESKTOP_SESSION') = 'kde') then begin
Result:=dmKde;
end;
end;
end;
function UnixOpenURL(const NomeFile: String):Integer;
var
WrkProcess: TProcess;
Comando, fn: String;
begin
Result:=-1;
Comando:='';
case DeterminaDesktopManager of
dmGnome:
Comando:='gnome-open';
dmKde:
if FileExists('/usr/bin/kioclient') then
Comando:='/usr/bin/kioclient exec'
else
Comando:='kfmclient exec';
end;
if Comando = '' then
exit;
fn:=NomeFile;
if Pos('://', fn) > 0 then
fn:=StringReplace(fn, '#', '%23', [rfReplaceAll]);
WrkProcess:=TProcess.Create(nil);
try
WrkProcess.Options:=[poNoConsole];
WrkProcess.CommandLine:=Comando + ' "' + fn + '"';
WrkProcess.Execute;
Result:=WrkProcess.ExitStatus;
finally
WrkProcess.Free;
end;
end;
{$endif unix}
function OpenURL(const URL, Params: string): boolean;
{$ifdef mswindows}
var
s, p: string;
{$endif mswindows}
begin
{$ifdef mswindows}
s:=UTF8Decode(URL);
p:=UTF8Decode(Params);
Result:=ShellExecute(0, 'open', PChar(s), PChar(p), nil, SW_SHOWNORMAL) > 32;
{$endif mswindows}
{$ifdef darwin}
Result:=fpSystem('Open "' + URL + '"') = 0;
{$else darwin}
{$ifdef unix}
Result:=UnixOpenURL(URL) = 0;
{$endif unix}
{$endif darwin}
end;
function UpdateGeoIp: integer;
begin
{$ifdef mswindows}
Result:=ShellExecute(0, nil, PChar('geoipupd.cmd'), nil, nil, 1);
{$endif mswindows}
{$ifdef darwin}
Result:=2;
{$else darwin}
{$ifdef unix}
Result:=2;
{$endif unix}
{$endif darwin}
end;
var
BusyCount: integer = 0;
procedure AppBusy;
begin
Inc(BusyCount);
Screen.Cursor:=crHourGlass;
end;
procedure AppNormal;
begin
Dec(BusyCount);
if BusyCount <= 0 then begin
BusyCount:=0;
Screen.Cursor:=crDefault;
end;
end;
procedure ForceAppNormal;
begin
BusyCount:=0;
AppNormal;
end;
{$ifdef mswindows}
procedure AllowSetForegroundWindow(dwProcessId: DWORD);
type
TAllowSetForegroundWindow = function(dwProcessId: DWORD): BOOL; stdcall;
var
_AllowSetForegroundWindow: TAllowSetForegroundWindow;
begin
_AllowSetForegroundWindow:=TAllowSetForegroundWindow(GetProcAddress(GetModuleHandle('user32.dll'), 'AllowSetForegroundWindow'));
if Assigned(_AllowSetForegroundWindow) then
_AllowSetForegroundWindow(dwProcessId);
end;
{$endif mswindows}
function CompareFilePath(const p1, p2: string): integer;
begin
{$ifdef windows}
Result:=AnsiCompareText(UTF8Decode(p1), UTF8Decode(p2));
{$else}
Result:=AnsiCompareStr(UTF8Decode(p1), UTF8Decode(p2));
{$endif windows}
end;
function Base32Decode(const s: ansistring): ansistring;
var
optr, len, bcnt: integer;
Output: PByteArray;
Input: PAnsiChar;
w: word;
c: ansichar;
b: byte;
begin
len:=Length(s);
Input:=PAnsiChar(s);
SetLength(Result, len);
Output:=PByteArray(PAnsiChar(Result));
optr:=0;
w:=0;
bcnt:=0;
while len > 0 do begin
repeat
c:=Input^;
if c = '=' then begin
len:=0;
break;
end;
if c in ['A'..'Z'] then
b:=Ord(c) - Ord('A')
else
if c in ['2'..'7'] then
b:=Ord(c) - 24
else
raise Exception.Create('Invalid base32 string.');
w:=(w shl 5) or b;
Inc(bcnt, 5);
Inc(Input);
Dec(len);
until (bcnt >= 8) or (len <= 0);
if bcnt < 8 then
Output^[optr]:=w shl (8 - bcnt)
else begin
Dec(bcnt, 8);
Output^[optr]:=w shr bcnt;
end;
Inc(optr);
end;
SetLength(Result, optr);
end;
end.