/
CommonUtils.pas
2652 lines (2531 loc) · 87.1 KB
/
CommonUtils.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Copyright (C) 2006-2008 Matteo Salvi of SalvadorSoftware
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 2
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
}
unit CommonUtils;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Forms, Dialogs, Menus,
ShellApi, VirtualTrees, ActiveX, ShlObj, TlHelp32, Registry, Controls, Consts,
CommCtrl, ComCtrls, XMLIntf, Main, XMLDoc, ComObj, jpeg, CommonClasses, Variants,
CoolTrayIcon, Clipbrd;
Type
TGetShortcutType = (gstPathExe,gstParameter,gstWorkingDir);
{ Icons, ImageList }
function SHSmallIcon(xpath : string; getopen : boolean): hicon;
function IconAdd(Sender: TBaseVirtualTree;ImageList: TImageList;Node: PVirtualNode): integer;
procedure ConvertTo32BitImageList(const ImageList: TImageList);
function ExtractAndCompareIcons(PathIcon1, PathIcon2: String): Boolean;
{ Processes, execution }
procedure ActionOnExe(Action: Integer);
function ProcessExists(exeFileName: string): Boolean;
procedure RunActionOnExe(NodeData: PTreeData);
procedure RunProcess(Sender: TBaseVirtualTree;NodeData: PTreeData;
RunIfNoInstancesRunning: Boolean);
procedure RunProcessAsUser(Sender: TBaseVirtualTree;NodeData: PTreeData;Username,
Password:WideString);
procedure SetDeleteASuiteWindowsStartup(Make:Boolean);
{ Files and folders }
function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
function BrowseForFolder(const Caption, InitialDir: String): String;
procedure CreateShortcutOnDesktop(FileName: WideString;TargetFilePath, Params, WorkingDir: String);
procedure DeleteFiles(PathDir, FileName: String);
procedure DeleteShortcutOnDesktop(FileName: WideString);
function FileFolderPageWebNotExists(Sender: TBaseVirtualTree;Node: PVirtualNode): Boolean;
function GetNumberSubFolders(FolderPath: String): Integer;
function GetParametersFromPath(Path: String): String;
function GetShortcutTarget(const LinkFileName:String;ShortcutType: TGetShortcutType):String;
procedure RunScanFolder(Sender: TBaseVirtualTree;ImageList: TImageList;PathExe: string;
ParentNode: PVirtualNode;Dialog: TForm);
{ Forms }
procedure CloseFormSensors;
function CreateDialogProgressBar(DialogMsg: String;NumberFolders: Integer): TForm;
procedure CreateFormSensors;
function IsFormOpen(const FormName : string): Boolean;
{ List, Menu, MRU }
procedure ActionsOnShutdownInsideTree(Sender: TBaseVirtualTree);
function AddNode(Sender: TBaseVirtualTree;Form: TComponentClass;Tipo: Integer): PTreeData;
procedure CheckList(Sender: TBaseVirtualTree;ImageList: TImageList);
function ClickOnButtonTree(Sender: TBaseVirtualTree): Boolean;
procedure DragDropFile(Sender: TBaseVirtualTree;ImageList: TImageList;
Node: pVirtualNode;PathTemp: string);
procedure DragDropText(Sender: TBaseVirtualTree;DataObject: IDataObject;
AttachMode: TVTNodeAttachMode);
function NodeDataXToNodeDataList(NodeX: PVirtualNode): PTreeData;
function GetNodeFromText(Tree: TBaseVirtualTree; Text: String; Cat: Boolean): PVirtualNode;
procedure GetProperty(List,Search: TBaseVirtualTree;IsNodeList: Boolean);
procedure RefreshList(Tree: TBaseVirtualTree;Trayicon: TCoolTrayicon;Save: Boolean);
procedure ShowTrayiconMenu(Tree: TBaseVirtualTree;Menu: TPopUpMenu);
procedure UpdateClassicMenu(Tree: TBaseVirtualTree;Menu: TPopUpMenu);
{ Load/Save FileXML }
function SaveFileXML(Tree: TBaseVirtualTree;Filename: String): Boolean;
//LoadFileXML is frmMain.FormCreate
{ MRU }
procedure AddMRU(Sender: TBaseVirtualTree;Trayicon: TCoolTrayIcon;Software: PVirtualNode;
DontAdd: Boolean);
procedure LoadMRUXML(XMLNode: IXMLNode;Tree: TBaseVirtualTree);
procedure SaveMRUXML(XMLNode: IXMLNode;Tree: TBaseVirtualTree);
procedure UpdateMRU(Sender: TBaseVirtualTree;PopupMenu: TMenuItem);
{ Options }
procedure LoadOptionsXML(XMLNode: IXMLNode);
procedure SaveOptionsXML(XMLNode: IXMLNode);
procedure UpdateOptions;
{ Conversions }
function BoolToStr(Bool: Boolean):String;
function StrToBool(Str: string): boolean;
function AbsoluteToRelative(PathFile: String): string;
function RelativeToAbsolute(PathFile: String): string;
procedure StrToStrings(Str, Sep: string; const List: TStrings);
function StringToWideString(Str: string): WideString;
{ Stats }
function GetCurrentUserName: string;
function GetComputerName: string;
function DiskFloatToString(Number: double;Units: Boolean): string;
function GetWindowsLanguage(TypeLocalInfo: LCTYPE): string;
function GetWindowsVersion: string;
function DiskFreeString(Drive: Char;Units: Boolean): string;
function DiskSizeString(Drive: Char;Units: Boolean): string;
function DiskUsedString(Drive: Char;Units: Boolean): string;
function DiskFreePercentual(Drive: Char): double;
{ HotKey }
function AddHotkey(Sender: TBaseVirtualTree;Node: PVirtualNode;HKId: integer): integer;
function GetHotKeyCode(KeyCode: Integer) : Integer;
function GetHotKeyMod(KeyMod: Integer) : Integer;
{ XML }
function ReadIntegerXML(XMLNode: IXMLNode;Default: Integer): Integer;
function ReadStringXML(XMLNode: IXMLNode;Default: String): String;
function ReadBooleanXML(XMLNode: IXMLNode;Default: Boolean): Boolean;
{ Scan File }
procedure LoadScanSettingsXML(XMLNode: IXMLNode);
procedure SaveScanSettingsXML(XMLNode: IXMLNode);
{ Autorun }
procedure AddAutorunInList(NodeData: PTreeData);
procedure RemoveAutorunFromList(NodeData: PTreeData);
{ Misc }
procedure EnableTimerCheckList(Sender: TObject);
function ExtractDirectoryName(const Filename: string): string;
function GetDateTime: String;
function GetEnvVarValue(VarName: string): string;
function GetFirstFreeIndex(ArrayWString: Array of WideString): Integer;
function IsFormatInClipBoard(format: Word): Boolean;
{ Windows Api }
function CreateProcessWithLogonW(
Username : LPCWSTR;
Domain : LPCWSTR;
Password : LPCWSTR;
LogonFlags : DWORD;
ApplicationName : LPCWSTR;
CommandLine : LPWSTR;
CreationFlags : DWORD;
Environment : Pointer;
CurrentDirectory : LPCWSTR;
const StartupInfo : TStartupInfoW;
var ProcessInfo : TProcessInformation ): BOOL; stdcall;
external 'advapi32.dll' name 'CreateProcessWithLogonW';
const
LOGON_WITH_PROFILE = $00000001;
type
TFileVersionInfo = record
CompanyName : string;
ProductName : string;
ProductVersion : string;
procedure FileVersionInfo(const sAppNamePath: TFileName);
end;
type
TScanFolderSettings = record
LastFolderPath : String;
SubFolders : Boolean;
FileTypes : TStringList;
ExcludeFiles : TStringList;
RetrieveInfo : Boolean;
end;
type
TListStats = record
SwCount : Integer;
SwGroupCount : Integer;
CatCount : Integer;
end;
type
TIterateSubtreeProcs = class //Class for IterateSubtree
procedure UpdateListItemCount(Sender: TBaseVirtualTree; Node: PVirtualNode;
Data: Pointer; var Abort: Boolean);
procedure PopulateClassicTrayMenu(Sender: TBaseVirtualTree; Node: PVirtualNode;
Data: Pointer; var Abort: Boolean);
end;
var
NewNode : Boolean;
VNDataCat, VNDataSoft, VNDataGroup : PTreeData; //Property
ScanSettings : TScanFolderSettings;
IterateSubtreeProcs : TIterateSubtreeProcs;
ListStats : TListStats; //Stats
const
MAX_PROFILE_PATH = 255;
implementation
uses Sensor, PropertySw, PropertyCat, PropertyGroup, Menu, Stats
{$ifdef ASuite}
,Card
{$endif};
function SHSmallIcon(xpath: string; getopen: boolean) : hicon;
var
fili : TSHFileInfo;
aka : Integer;
begin
aka := SHGFI_ICON or SHGFI_SMALLICON;
if getopen then
aka := aka or SHGFI_OPENICON;
SHGetFileInfo(Pchar(xpath), 0, fili, sizeof(TSHFileInfo), aka);
Result := fili.hIcon;
end;
function IconAdd(Sender: TBaseVirtualTree;ImageList: TImageList;Node: PVirtualNode): integer;
var
Icon : TIcon;
NodeData : PTreeData;
TempCache, TempIcon, TempExe, TempPath : String;
I : Integer;
begin
NodeData := Sender.GetNodeData(Node);
Icon := TIcon.Create;
//Three pathes
TempCache := NodeData.PathCache;
TempIcon := RelativeToAbsolute(NodeData.PathIcon);
TempExe := RelativeToAbsolute(NodeData.PathExe[0]);
if Not(FileExists(NodeData.PathCache)) then
NodeData.PathCache := '';
//Control file
if (FileExists(TempCache)) or (FileExists(TempIcon)) or
(FileExists(TempExe)) then
begin
try
//Priority icon->exe
if FileExists(TempIcon) then
TempPath := TempIcon
else
if FileExists(TempExe) then
TempPath := TempExe;
//Load path cache-icon if exists it
if (LauncherOptions.Cache) and FileExists(TempCache) then
begin
//Compare cache-icon and exe's icon
//If cache-icon is not equal to exe's icon, ASuite recreated it
if Not(StartUpTime) and (Not(ExtractAndCompareIcons(TempPath,TempCache)) or
(LowerCase(ExtractFileExt(TempPath)) = '.ico')) then
begin
DeleteFile(TempCache);
NodeData.PathCache := '';
end
else
TempPath := TempCache;
end;
//Check TempPath
if FileExists(TempPath) then
if LowerCase(ExtractFileExt(TempPath)) = '.ico' then
Icon.LoadFromFile(TempPath)
else
Icon.Handle := SHSmallIcon(TempPath, True)
else
ImageList.GetIcon(0,Icon);
//Add icon in ImageList
if (NodeData.ImageIndex = -1) or (NodeData.ImageIndex = 1) or
(NodeData.ImageIndex = 0) or (NodeData.ImageIndex = 13) then
Result := ImageList.AddIcon(Icon)
else begin
ImageList.ReplaceIcon(NodeData.ImageIndex, Icon);
Result := NodeData.ImageIndex;
end;
//Save file cache-icon
if ((NodeData.PathCache = '') or (Not FileExists(TempCache))) and (FileExists(TempPath)) then
if (LowerCase(ExtractFileExt(TempPath)) <> '.ico') and (LauncherOptions.Cache) then
begin
I := 0;
while (FileExists(PathCache + IntToStr(I) + '.ico')) do
Inc(I);
if Icon.HandleAllocated then
begin
Icon.SaveToFile(PathCache + IntToStr(I) + '.ico');
NodeData.PathCache := PathCache + IntToStr(I) +'.ico';
end;
end
else
NodeData.PathCache := '';
except
{$ifdef ASuite}
ShowMessageFmt(ArrayMessages[15],[NodeData.Name]);
{$else}
ShowMessageFmt(ArrayMessages[13],[NodeData.Name]);
{$endif}
Result := 0;
end;
end
else begin
//If node is category, set result to 1 (category icon)
//else to 0 (launcher icon)
if (NodeData.Tipo = 0) then
Result := 1
else
Result := 0;
end;
Icon.Free;
end;
procedure ConvertTo32BitImageList(const ImageList: TImageList);
const
Mask: array[Boolean] of Longint = (0, ILC_MASK);
var
TemporyImageList: TImageList;
begin
if Assigned(ImageList) then
begin
TemporyImageList := TImageList.Create(nil);
try
TemporyImageList.Assign(ImageList);
with ImageList do
begin
ImageList.Handle := ImageList_Create(Width, Height, ILC_COLOR32 or Mask[Masked], 0, AllocBy);
if not ImageList.HandleAllocated then
begin
raise EInvalidOperation.Create(SInvalidImageList);
end;
end;
ImageList.AddImages(TemporyImageList);
finally
TemporyImageList.Free;
end;
end;
end;
function ExtractAndCompareIcons(PathIcon1, PathIcon2: String): Boolean;
var
Icon1, Icon2 : TIcon;
MS1, MS2 : TMemoryStream;
begin
Result := False;
if (FileExists(PathIcon1)) and (FileExists(PathIcon2)) then
begin
//Extract Icon1
Icon1 := TIcon.Create;
MS1 := TMemoryStream.Create;
try
if LowerCase(ExtractFileExt(PathIcon1)) = '.ico' then
Icon1.LoadFromFile(PathIcon1)
else
Icon1.Handle := SHSmallIcon(PathIcon1,True);
Icon1.SaveToStream(MS1);
//Extract Icon2
Icon2 := TIcon.Create;
MS2 := TMemoryStream.Create;
try
if LowerCase(ExtractFileExt(PathIcon2)) = '.ico' then
Icon2.LoadFromFile(PathIcon2)
else
Icon2.Handle := SHSmallIcon(PathIcon2,True);
Icon2.SaveToStream(MS2);
//Compare memory size
if MS1.Size = MS2.Size then
Result := CompareMem(MS1.Memory, MS2.Memory, MS1.Size)
finally
MS2.Free;
Icon2.Free;
end;
finally
MS1.Free;
Icon1.Free;
end;
end;
end;
procedure ActionOnExe(Action: Integer);
begin
case Action of
2:
begin
//Hide frmMain
frmMain.Hide;
frmMain.CoolTrayIcon1.HideTaskbarIcon;
{$ifdef ASuite}
if IsFormOpen('frmCard') then
frmCard.Hide;
{$endif}
end;
3:
begin
//Close application
ShutdownTime := True;
{$ifdef ASuite}
if IsFormOpen('frmCard') then
frmCard.Close;
{$endif}
frmMain.Close;
end;
end;
end;
function ProcessExists(exeFileName: string): Boolean;
var
hSnapShot: THandle;
ProcInfo: TProcessEntry32;
begin
hSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
exeFileName := UpperCase(ExeFileName);
Result := False;
if (hSnapShot <> THandle(-1)) then
begin
ProcInfo.dwSize := SizeOf(ProcInfo);
//Check processes
if (Process32First(hSnapshot, ProcInfo)) then
begin
if (UpperCase(ExtractFileName(ProcInfo.szExeFile)) = ExeFileName) then
Result := True;
while (Process32Next(hSnapShot, ProcInfo)) do
if (UpperCase(ExtractFileName(ProcInfo.szExeFile)) = ExeFileName) then
Result := True;
end;
end;
CloseHandle(hSnapShot);
end;
procedure RunActionOnExe(NodeData: PTreeData);
var
ActionTemp : Integer;
begin
if NodeData.ActionOnExe = 0 then
begin
ActionTemp := LauncherOptions.ActionOnExe + 1;
ActionOnExe(ActionTemp);
end
else
ActionOnExe(NodeData.ActionOnExe);
end;
procedure RunProcess(Sender: TBaseVirtualTree;NodeData: PTreeData;RunIfNoInstancesRunning: Boolean);
var
WorkingDir, PathTemp, Parameters: String;
WindowState, ErrorCode, I: Integer;
begin
for I := 0 to High(NodeData.PathExe) do
begin
if NodeData.PathExe[I] = '' then
Continue;
PathTemp := RelativeToAbsolute(NodeData.PathExe[I]);
if (RunIfNoInstancesRunning) and (NodeData.Autorun = 2) then
if ProcessExists(ExtractFileName((NodeData.PathExe[I]))) then
Continue;
//Working directory
if NodeData.WorkingDir = '' then
WorkingDir := ExtractFileDir(PathTemp)
else
WorkingDir := NodeData.WorkingDir;
//Window state
case NodeData.WindowState of
1: WindowState := 7;
2: WindowState := 3;
else
WindowState := 10;
end;
//Parameters
if NodeData.Tipo = 2 then
begin
//Software Group
Parameters := GetParametersFromPath(PathTemp);
//Get Path
if Parameters <> '' then
begin
//Delete first "
Delete(PathTemp,1,1);
//Delete parameters from Path
PathTemp := StringReplace(PathTemp,'"' + Parameters,'',[rfIgnoreCase])
end;
end
else begin
//Software
Parameters := NodeData.Parameters;
end;
//Check variables in parameters to use RelativeToAbsolute
if (pos('$',Parameters) <> 0) then
Parameters := RelativeToAbsolute(Parameters);
//Execution
ErrorCode := ShellExecute(GetDesktopWindow, 'open', PChar(PathTemp),
PChar(Parameters),
PChar(RelativeToAbsolute(WorkingDir)), WindowState);
if ErrorCode <= 32 then
begin
//Show error message
{$IfDef ASuite}
ShowMessageFmt(ArrayMessages[9],[NodeData.Name]);
{$else}
ShowMessageFmt(ArrayMessages[6],[NodeData.Name]);
{$EndIf}
end;
end;
end;
procedure RunProcessAsUser(Sender: TBaseVirtualTree;NodeData: PTreeData;Username, Password:WideString);
var
WindowState, I : Integer;
StartupInfo : TStartupInfoW;
ProcInfo : TProcessInformation;
WorkingDir, PathTemp : WideString;
begin
for I := 0 to High(NodeData.PathExe) do
begin
if NodeData.PathExe[I] = '' then
Continue;
PathTemp := RelativeToAbsolute(NodeData.PathExe[I]);
//Working directory
if NodeData.WorkingDir = '' then
WorkingDir := StringToWideString(ExtractFileDir(PathTemp))
else
WorkingDir := NodeData.WorkingDir;
//Window state
case NodeData.WindowState of
1: WindowState := 7;
2: WindowState := 3;
else
WindowState := 10;
end;
//Execution
FillMemory(@StartupInfo, sizeof(StartupInfo), 0);
FillMemory(@ProcInfo, sizeof(ProcInfo), 0);
StartupInfo.cb := sizeof(TStartupInfoW);
StartupInfo.wShowWindow := WindowState;
if CreateProcessWithLogonW(LPCWSTR(Username),nil,LPCWSTR(Password),
LOGON_WITH_PROFILE,LPCWSTR(PathTemp),nil,
CREATE_UNICODE_ENVIRONMENT,nil,LPCWSTR(WorkingDir),
StartupInfo,ProcInfo) then
begin
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end
else
ShowMessage(SysErrorMessage(GetLastError));
end;
end;
procedure SetDeleteASuiteWindowsStartup(Make:Boolean);
var
Registry : TRegistry;
begin
Registry := TRegistry.Create;
try
with Registry do
begin
RootKey:=HKEY_LOCAL_MACHINE;
if OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run',False) then
begin
if (Make) then
begin
if Not(ValueExists(Launcher)) then
WriteString(Launcher,(Application.ExeName));
end
else
DeleteValue(Launcher)
end;
end
finally
Registry.Free;
end;
end;
function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): Integer; stdcall;
begin
//Set initial directory
if uMsg = BFFM_INITIALIZED then
SendMessage(hwnd, BFFM_SETSELECTION, 1, lpData);
Result := 0;
end;
function BrowseForFolder(const Caption, InitialDir: String): String;
var
BrowseInfo: TBrowseInfo;
Buffer: PChar;
ItemIDList: PItemIDList;
ShellMalloc: IMalloc;
Windows: Pointer;
Path: string;
begin
Result := '';
Path := InitialDir;
//Delete \ in last char. Example c:\xyz\ to c:\xyz
if (Length(Path) > 0) and (Path[Length(Path)] = '\') then
Delete(Path, Length(Path), 1);
FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
if (ShGetMalloc(ShellMalloc) = S_OK) and Assigned(ShellMalloc) then
begin
Buffer := ShellMalloc.Alloc(MAX_PATH);
try
//Set browseinfo's properties
with BrowseInfo do
begin
hwndOwner := Application.Handle;
pidlRoot := nil;
pszDisplayName := Buffer;
lpszTitle := PChar(Caption);
ulFlags := BIF_RETURNONLYFSDIRS;
lParam := Integer(PChar(Path));
lpfn := BrowseCallbackProc;
end;
//Show browse for folder
Windows := DisableTaskWindows(Application.Handle);
try
ItemIDList := ShBrowseForFolder(BrowseInfo);
finally
EnableTaskWindows(Windows);
end;
//Get directory
if Assigned(ItemIDList) then
begin
ShGetPathFromIDList(ItemIDList, Buffer);
ShellMalloc.Free(ItemIDList);
Result := Buffer;
end
else
Result := '';
finally
ShellMalloc.Free(Buffer);
end;
end;
end;
procedure CreateShortcutOnDesktop(FileName: WideString;TargetFilePath, Params, WorkingDir: String);
var
IObject : IUnknown;
ISLink : IShellLink;
IPFile : IPersistFile;
PIDL : PItemIDList;
InFolder : array[0..MAX_PATH] of Char;
TargetName : String;
begin
//Relative path to Absolute path
TargetName := RelativeToAbsolute(TargetFilePath);
if pos(':',TargetName) = 0 then
TargetName := ApplicationPath + TargetName;
IObject := CreateComObject(CLSID_ShellLink);
ISLink := IObject as IShellLink;
IPFile := IObject as IPersistFile;
//Create link
with ISLink do
begin
SetPath(pChar(TargetName));
SetArguments(pChar(Params));
if WorkingDir = '' then
SetWorkingDirectory(pChar(ExtractFilePath(TargetName)))
else
SetWorkingDirectory(pChar(RelativeToAbsolute(WorkingDir)));
end;
//DesktopPath
SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, PIDL);
SHGetPathFromIDList(PIDL, InFolder);
//Save link
IPFile.Save(PWChar(InFolder + FileName), false);
end;
procedure DeleteFiles(PathDir, FileName: String);
var
Search : TSearchRec;
begin
//Delete file with FileName in folder PathDir (path relative)
if FindFirst(ApplicationPath + PathDir + FileName, faAnyFile, Search) = 0 then
begin
repeat
DeleteFile(PathDir + Search.Name);
until
FindNext(Search) <> 0;
FindClose(Search);
end;
end;
procedure DeleteShortcutOnDesktop(FileName: WideString);
var
PIDL : PItemIDList;
DesktopPath : array[0..MAX_PATH] of Char;
begin
SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, PIDL);
SHGetPathFromIDList(PIDL, DesktopPath);
if (FileExists(DesktopPath + FileName)) then
DeleteFile(DesktopPath + FileName);
end;
function FileFolderPageWebNotExists(Sender: TBaseVirtualTree;Node: PVirtualNode): Boolean;
var
NodeData: PTreeData;
PathTemp : String;
I : Integer;
MaxIndex : Integer;
begin
Result := False;
NodeData := Sender.GetNodeData(Node);
case NodeData.Tipo of
0, 4: exit;
1: MaxIndex := 0;
2: MaxIndex := High(NodeData.PathExe);
else
MaxIndex := -1;
end;
for I := 0 to MaxIndex do
begin
PathTemp := RelativeToAbsolute(NodeData.PathExe[I]);
if ((NodeData.Tipo = 1) or (NodeData.Tipo = 2)) and
Not((FileExists(PathTemp)) or (DirectoryExists(PathTemp)) or
(pos('http://',PathTemp) = 1) or (pos('https://',PathTemp) = 1) or
(pos('ftp://',PathTemp) = 1) or (pos('www.',PathTemp) = 1) or
(pos('%',PathTemp) = 1)) then
begin
if ((NodeData.Tipo = 4) and (PathTemp = '')) or
(NodeData.Tipo = 1) then
Result := true;
end;
end;
end;
function GetNumberSubFolders(FolderPath: String): Integer;
var
SearchRec : TSearchRec;
begin
Result := 0;
//Count subfolders in FolderPath
if FindFirst(FolderPath + '*.*', faAnyFile, SearchRec) = 0 then
repeat
if ((SearchRec.Name <> '.') and (SearchRec.Name <> '..')) and
((SearchRec.Attr and faDirectory) = (faDirectory)) then
begin
//Increment result
Inc(Result);
Result := Result + GetNumberSubFolders(FolderPath + SearchRec.Name + '\');
end;
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
function GetParametersFromPath(Path: String): String;
var
J : Integer;
begin
Result := '';
if Pos('"',Path) = 1 then
begin
//Delete first "
Delete(Path,1,1);
J := Pos('"',Path);
//Get parameters
Result := Copy(Path,J + 1,Length(Path) - J);
end;
end;
function GetShortcutTarget(const LinkFileName:String;ShortcutType: TGetShortcutType):String;
var
psl : IShellLink;
ppf : IPersistFile;
WidePath : Array[0..260] of WideChar;
Info : Array[0..MAX_PATH] of Char;
wfs : TWin32FindData;
begin
if UpperCase(ExtractFileExt(LinkFileName)) <> '.LNK' Then
begin
Result := LinkFileName;
Exit;
end;
CoCreateInstance(CLSID_ShellLink,nil,CLSCTX_INPROC_SERVER,IShellLink,psl);
if psl.QueryInterface(IPersistFile, ppf) = 0 then
begin
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,PChar(LinkFileName),-1,@WidePath,MAX_PATH);
ppf.Load(WidePath, STGM_READ);
case ShortcutType of
gstPathExe : psl.GetPath(@info,MAX_PATH,wfs,SLGP_UNCPRIORITY);
gstParameter : psl.GetArguments(@info,MAX_PATH);
gstWorkingDir : psl.GetWorkingDirectory(@info,MAX_PATH);
end;
Result := info;
end
else
Result := LinkFileName;
end;
procedure RunScanFolder(Sender: TBaseVirtualTree;ImageList: TImageList;PathExe: string;
ParentNode: PVirtualNode;Dialog: TForm);
var
SearchRec : TSearchRec;
ChildNode : PVirtualNode;
ChildNodeData : PTreeData;
PathTemp, TempName : String;
FvI : TFileVersionInfo;
ProgressBar : TProgressBar;
begin
ProgressBar := TProgressBar(Dialog.FindComponent('Progress'));
Sender.BeginUpdate;
if FindFirst(PathExe + '*.*', faAnyFile, SearchRec) = 0 then
repeat
//Absolute path->relative path
PathTemp := AbsoluteToRelative(PathExe + SearchRec.Name);
//Add node in vstlist
if ((SearchRec.Name <> '.') and (SearchRec.Name <> '..')) and
(((SearchRec.Attr and faDirectory) = (faDirectory)) or ((ScanSettings.FileTypes.IndexOf(LowerCase(ExtractFileExt(SearchRec.Name))) <> -1))) then
begin
TempName:=SearchRec.Name;
Delete(TempName,pos(ExtractFileExt(SearchRec.Name),SearchRec.Name),Length(SearchRec.Name));
if (ScanSettings.ExcludeFiles.IndexOf(LowerCase(TempName)) = -1) then
begin
ChildNode := Sender.AddChild(ParentNode);
ChildNodeData := Sender.GetNodeData(ChildNode);
ChildNodeData.pNode := ChildNode;
ChildNodeData.Name := TempName;
if ((SearchRec.Attr and faDirectory) = (faDirectory)) and (ScanSettings.SubFolders) then
begin
ChildNodeData.Tipo := 0;
ChildNodeData.ImageIndex := 1;
//Dialog
ProgressBar.Position := ProgressBar.Position + 1;
Dialog.Update;
RunScanFolder(Sender, ImageList, PathExe + SearchRec.Name + '\',ChildNode,Dialog);
end
else
if (SearchRec.Attr <> faDirectory) and (ScanSettings.FileTypes.IndexOf(LowerCase(ExtractFileExt(SearchRec.Name))) <> -1) and
(ScanSettings.ExcludeFiles.IndexOf(LowerCase(TempName)) = -1) then
begin
ChildNodeData.PathExe[0] := PathTemp;
//Retrieve some software's info from EXE (name, developer and version)
if ScanSettings.RetrieveInfo then
begin
FvI.FileVersionInfo(PathTemp);
if (FvI.ProductName <> '') then
ChildNodeData.Name := FvI.ProductName;
{$IfDef ASuite}
if (FvI.CompanyName <> '') then
ChildNodeData.Developer := FvI.CompanyName;
if (FvI.ProductVersion <> '') then
ChildNodeData.Version := StringReplace(FvI.ProductVersion,', ','.',[rfReplaceAll]);
{$EndIf}
end;
ChildNodeData.Tipo := 1;
ChildNodeData.ImageIndex := IconAdd(Sender, ImageList, ChildNode);
end;
if (ChildNode.ChildCount = 0) And ((SearchRec.Attr and faDirectory) = (faDirectory)) then
Sender.DeleteNode(ChildNode);
end;
end;
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
Sender.EndUpdate;
end;
procedure CloseFormSensors;
var
I : Integer;
begin
//If assigned, close and free FormSensors
for I := 0 to 3 do
if Assigned(FormSensors[I]) then
begin
FormSensors[I].Close;
FormSensors[I] := nil;
end;
end;
function CreateDialogProgressBar(DialogMsg: String;NumberFolders: Integer): TForm;
var
ProgressBar : TProgressBar;
begin
//Create Dialog and ProgressBar (in Dialog)
Result := CreateMessageDialog(DialogMsg, mtInformation, []) ;
ProgressBar := TProgressBar.Create(Result) ;
Result.Caption := DialogMsg;
Result.Height := 100;
//Set some progressbar's property
with ProgressBar do
begin
Name := 'Progress';
Parent := Result;
Max := NumberFolders;
Step := 1;
Top := 50;
Left := 8;
Width := Result.ClientWidth - 16;
end;
Result.Show;
end;
procedure CreateFormSensors;
var
I : Integer;
begin
//Create FormSensors and set its height and width
for I := 0 to 3 do
begin
frmSensor := nil;
case I of
0: //Top
begin
if (LauncherOptions.SensorLeftClick[I] <> 0) or (LauncherOptions.SensorRightClick[I] <> 0) then
begin
Application.CreateForm(TfrmSensor,frmSensor);
frmSensor.Height := 1;
frmSensor.Width := GetDeviceCaps(GetDC(frmMain.Handle), HORZRES);
end
end;
1: //Left
begin
if (LauncherOptions.SensorLeftClick[I] <> 0) or (LauncherOptions.SensorRightClick[I] <> 0) then
begin
Application.CreateForm(TfrmSensor,frmSensor);
frmSensor.Height := GetDeviceCaps(GetDC(frmMain.Handle), VERTRES);
frmSensor.Width := 1;
end
end;
2: //Right
begin
if (LauncherOptions.SensorLeftClick[I] <> 0) or (LauncherOptions.SensorRightClick[I] <> 0) then
begin
Application.CreateForm(TfrmSensor,frmSensor);
frmSensor.Left := GetDeviceCaps(GetDC(frmMain.Handle), HORZRES) - 1;
frmSensor.Height := GetDeviceCaps(GetDC(frmMain.Handle), VERTRES);
frmSensor.Width := 1;
end
end;
3: //Bottom
begin
if (LauncherOptions.SensorLeftClick[I] <> 0) or (LauncherOptions.SensorRightClick[I] <> 0) then
begin
Application.CreateForm(TfrmSensor,frmSensor);
frmSensor.Top := GetDeviceCaps(GetDC(frmMain.Handle), VERTRES) - 1;
frmSensor.Height := 1;
frmSensor.Width := GetDeviceCaps(GetDC(frmMain.Handle), HORZRES);
end
end;
end;
//If assigned, show current frmSensor and set Side (to I)
//and FormSensors[I] (to current frmSensor)
if Assigned(frmSensor) then
begin
frmSensor.Show;
frmSensor.Side := I;
FormSensors[I] := frmSensor;
end;
end;
end;
function IsFormOpen(const FormName : string): Boolean;
var
I: Integer;
begin
Result := False;
for i := Screen.FormCount - 1 DownTo 0 do
if (Screen.Forms[i].Name = FormName) then
begin
Result := True;
Break;
end;
end;
procedure ActionsOnShutdownInsideTree(Sender: TBaseVirtualTree);
var
NodeParent : PVirtualNode;
procedure ProcessTreeItem(NodeChild: PVirtualNode);
var
NodeData : PTreeData;
begin
if (NodeChild = nil) then Exit;
NodeData := Sender.GetNodeData(NodeChild);
//Delete shortcut on shutdown
if (Nodedata.ShortcutDesktop) then
DeleteShortcutOnDesktop('\' + NodeData.Name + '.lnk');
NodeChild := NodeChild.FirstChild;
while Assigned(NodeChild) do
begin
ProcessTreeItem(NodeChild);
NodeChild := NodeChild.NextSibling;
end;
end;
begin
NodeParent := Sender.GetFirst;
while Assigned(NodeParent) do
begin
ProcessTreeItem(NodeParent);
NodeParent := NodeParent.NextSibling;
end;
end;
function AddNode(Sender: TBaseVirtualTree;Form: TComponentClass;Tipo: Integer): PTreeData;
var
ChildNode : PVirtualNode;
ChildNodeData, ParentNodeData : PTreeData;
frmProperty : TForm;
FolderPath : String;
begin
frmMain.tmCheckList.Enabled := false;
FolderPath := '';
//Create ChildNode
if Assigned(Sender.FocusedNode) then