-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathufrm_main.pas
1381 lines (1177 loc) · 38.9 KB
/
ufrm_main.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
unit Ufrm_Main;
{$mode delphi}
{$H+}
interface
uses
Classes,
SysUtils,
FileUtil,
Ipfilebroker,
IpHtml,
Forms,
Controls,
Graphics,
Dialogs,
StdCtrls,
ComCtrls,
Process,
LCLType,
LCLIntF,
LCLProc,
Menus,
ExtCtrls,
Clipbrd,
ActnList,
UTF8Process,
UStringSplitter,
UPersistency,
ULogger;
const
APP_VER = '0.1.2-alpha';
type
{ Tfrm_Main }
Tfrm_Main = class(TForm)
ac_ExcludeCBE: TAction;
ac_SuperUser: TAction;
ac_NewSearchWindow: TAction;
ac_Options: TAction;
al_Main: TActionList;
btn_Locate: TButton;
edt_SearchPattern: TEdit;
MenuItem3: TMenuItem;
MenuItem5: TMenuItem;
MenuItem9: TMenuItem;
mi_ExcludeCBE: TMenuItem;
mi_Delete: TMenuItem;
mi_TestBackdoorSeparator: TMenuItem;
mi_TestBackdoor: TMenuItem;
mi_TraySuperUser: TMenuItem;
mi_NewSearchWindow: TMenuItem;
MenuItem7: TMenuItem;
mi_SuperUser: TMenuItem;
tmr_Popup: TTimer;
lv_Files: TListView;
MenuItem4: TMenuItem;
mi_TrayNewSearchWindow: TMenuItem;
MenuItem6: TMenuItem;
mi_TrayOptions: TMenuItem;
MenuItem8: TMenuItem;
mi_TrayExit: TMenuItem;
mi_OpenWithSelectAdd: TMenuItem;
mi_OpenWith: TMenuItem;
mi_UpdateDB: TMenuItem;
mi_About: TMenuItem;
mi_Help: TMenuItem;
mi_Tools: TMenuItem;
mi_Options: TMenuItem;
mnu_Main: TMainMenu;
MenuItem1: TMenuItem;
mi_File: TMenuItem;
mi_Exit: TMenuItem;
mi_Properties: TMenuItem;
mi_CopyFullNameToClipboard: TMenuItem;
MenuItem2: TMenuItem;
mi_CopyPathToClipboard: TMenuItem;
mi_CopyNameOnlyToClipboard: TMenuItem;
mi_Open: TMenuItem;
mi_OpenPath: TMenuItem;
mnu_PopupFiles: TPopupMenu;
dlg_OpenWith: TOpenDialog;
pnl_Locate: TPanel;
mnu_PopupTray: TPopupMenu;
sb_Main: TStatusBar;
ti_Main: TTrayIcon;
procedure ac_ExcludeCBEExecute(Sender: TObject);
procedure ac_NewSearchWindowExecute(Sender: TObject);
procedure ac_OptionsExecute(Sender: TObject);
procedure ac_SuperUserExecute(Sender: TObject);
procedure btn_LocateClick(Sender: TObject);
procedure edt_SearchPatternKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormHide(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormShow(Sender: TObject);
procedure lv_FilesContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
procedure lv_FilesDblClick(Sender: TObject);
procedure lv_FilesDrawItem(Sender: TCustomListView; AItem: TListItem; ARect: TRect; AState: TOwnerDrawState);
procedure lv_FilesKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure lv_FilesMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure mi_DeleteClick(Sender: TObject);
procedure mi_TestBackdoorClick(Sender: TObject);
procedure mi_TrayOptionsClick(Sender: TObject);
procedure mi_TrayExitClick(Sender: TObject);
procedure mi_AboutClick(Sender: TObject);
procedure mi_OpenWithSelectAddClick(Sender: TObject);
procedure mi_UpdateDBClick(Sender: TObject);
procedure mi_ExitClick(Sender: TObject);
procedure mi_CopyFullNameToClipboardClick(Sender: TObject);
procedure mi_CopyNameOnlyToClipboardClick(Sender: TObject);
procedure mi_CopyPathToClipboardClick(Sender: TObject);
procedure mi_OpenClick(Sender: TObject);
procedure mi_OpenPathClick(Sender: TObject);
procedure mi_PropertiesClick(Sender: TObject);
procedure mnu_PopupFilesClose(Sender: TObject);
procedure mnu_PopupFilesPopup(Sender: TObject);
procedure ti_MainClick(Sender: TObject);
procedure tmr_PopupTimer(Sender: TObject);
procedure ti_MainDblClick(Sender: TObject);
private
{ private declarations }
FInitialW: Integer;
FInitialH: Integer;
FCanClose: Boolean;
FOperation: String;
FLastX: Integer;
FLastY: Integer;
FShowMenu: Boolean;
FLastCommandParams: String;
FLastCommandOutput: String;
FSuppressUpdateDBDialogs: Boolean;
procedure OpenWithAppClick(Sender: TObject);
procedure Reset;
procedure ResetLocate;
procedure LockUI(Lock: Boolean);
procedure SetOperation(Operation: String);
function GetOperation: String;
procedure SetStatus(Status: String);
function GetStatus: String;
procedure WarnUser(Msg: String);
function GetUpdateDBMenuPath: String;
function GetUpdateDBShortcut: String;
function GetUpdateDBDirections: String;
function GetSuperUserMenuPath: String;
function GetSuperUserDirections: String;
procedure ILog(Msg: String);
procedure WLog(Msg: String);
procedure ELog(Msg: String; Routine: String);
function ExecuteCommand(Directory: String; Command: String; Parameters: array of String; SuperUser: Boolean): Boolean; overload;
function ExecuteCommand(Directory: String; Command: String; Parameters: array of String; SuperUser: Boolean; var Output: String): Boolean; overload;
function TriggerCommand(Directory: String; Command: String; Parameters: array of String; SuperUser: Boolean): Boolean; overload;
function TriggerCommand(Directory: String; Command: String; Parameters: array of String; SuperUser: Boolean; TimeoutMS: Integer): Boolean; overload;
function OpenDocumentEx(FileName: String; SuperUser: Boolean): Boolean; overload;
function OpenDocumentEx(FileName: String; SuperUser: Boolean; var OSHandlerApp: String): Boolean; overload;
procedure LocateFiles(SearchPattern: String; JustUpdatedDB: Boolean);
procedure FilterList(List: TStringList; Keyword: String);
public
{ public declarations }
end;
const
OPEN_WITH_APP_MENU_ITEM_MASK = $10000;
const
TMO_NONE = 0;
TMO_MS_OPEN_DOC = 1000;
var
frm_Main: Tfrm_Main;
VisibleInstances: TList;
implementation
uses
ufrm_options;
{ misc routines }
function Get_Home_Dir: String;
begin
Result := ExpandFileName('~/');
end;
function Internal_Execute_Command(Process: TProcess; var OutputString: String; var ExitStatus: Integer): Integer;
const
READ_BYTES = 65536;
STEP_SLEEP_MS = 100;
var
NumBytes: Integer;
BytesRead: Integer;
begin
Result := -1;
try
try
Process.Options := [poUsePipes];
BytesRead := 0;
Process.Execute;
while Process.Running do
begin
Setlength(OutputString, BytesRead + READ_BYTES);
NumBytes := Process.Output.Read(OutputString[1 + BytesRead], READ_BYTES);
if NumBytes > 0 then
Inc(BytesRead, NumBytes)
else
Sleep(STEP_SLEEP_MS);
end;
repeat
Setlength(OutputString, BytesRead + READ_BYTES);
NumBytes := Process.Output.Read(OutputString[1 + BytesRead], READ_BYTES);
if NumBytes > 0 then
Inc(BytesRead, NumBytes);
until NumBytes <= 0;
Setlength(OutputString, BytesRead);
ExitStatus := Process.ExitStatus;
Result := 0;
except
on E: Exception do
begin
Result := 1;
Setlength(OutputString, BytesRead);
end;
end;
finally
Process.Free;
end;
end;
function Internal_Trigger_Command(Process: TProcess; var ExitStatus: Integer; TimeoutMS: Integer): Integer;
const
STEP_SLEEP_MS = 100;
var
T0: Integer;
begin
Result := -1;
try
try
Process.Options := [];
Process.Execute;
T0 := GetTickCount;
while Process.Running and (GetTickCount - T0 < TimeoutMS) do
Sleep(STEP_SLEEP_MS);
ExitStatus := Process.ExitStatus;
Result := 0;
except
on E: Exception do
begin
Result := 1;
end;
end;
finally
Process.Free;
end;
end;
function Caption_To_Message(Caption: String): String;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(Caption) do
if Caption[i] <> '&' then
Result := Result + Caption[i];
end;
{ Tfrm_Main }
{$R *.lfm}
procedure Tfrm_Main.FormCreate(Sender: TObject);
begin
// tray icon only for the main form;
if Self = frm_Main then // Application.MainForm is not yet set;
begin
ti_Main.Icon := Application.Icon;
ti_Main.Hint := Application.Title;
ti_Main.Visible := True;
end;
FInitialW := Width;
FInitialH := Height;
ac_SuperUser.Checked := Pers_Gen_Get_Super_User;
ac_ExcludeCBE.Checked := Pers_Gen_Get_Exclude_CBE;
Reset;
System.Randomize;
end;
procedure Tfrm_Main.FormDestroy(Sender: TObject);
begin
{}
end;
procedure Tfrm_Main.FormHide(Sender: TObject);
begin
VisibleInstances.Remove(Self);
end;
procedure Tfrm_Main.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
Shift := Shift;
if Key = VK_ESCAPE then
Close;
end;
procedure Tfrm_Main.FormShow(Sender: TObject);
begin
VisibleInstances.Add(Self);
if edt_SearchPattern.IsVisible then
edt_SearchPattern.SetFocus;
// let's just not risk it;
mi_TestBackdoor.ShortCut := scNone;
mi_TestBackdoor.Visible := False;
mi_TestBackdoorSeparator.Visible := False;
{$ifdef DEBUG}
mi_TestBackdoor.ShortCut := scCtrl + VK_F12;
mi_TestBackdoor.Visible := True;
mi_TestBackdoorSeparator.Visible := True;
{$endif}
end;
procedure Tfrm_Main.lv_FilesContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
var
HasSelectedFile: Boolean;
begin
MousePos := MousePos;
Handled := Handled;
HasSelectedFile := (lv_Files.Items.Count > 0) and (lv_Files.ItemIndex <> -1);
mi_Open.Enabled := HasSelectedFile;
mi_OpenPath.Enabled := HasSelectedFile;
mi_OpenWith.Enabled := HasSelectedFile;
mi_CopyFullNameToClipboard.Enabled := HasSelectedFile;
mi_CopyPathToClipboard.Enabled := HasSelectedFile;
mi_CopyNameOnlyToClipboard.Enabled := HasSelectedFile;
mi_Delete.Enabled := HasSelectedFile;
mi_Properties.Enabled := HasSelectedFile;
end;
procedure Tfrm_Main.lv_FilesDblClick(Sender: TObject);
begin
mi_Open.Click;
end;
procedure Tfrm_Main.lv_FilesDrawItem(Sender: TCustomListView; AItem: TListItem; ARect: TRect; AState: TOwnerDrawState);
const
COL_BRUSH_CURRENT_SEL_EVEN = $705030;
COL_BRUSH_CURRENT_SEL_ODD = $66482B;
COL_BRUSH_OTHER_SEL_EVEN = COL_BRUSH_CURRENT_SEL_EVEN;
COL_BRUSH_OTHER_SEL_ODD = COL_BRUSH_CURRENT_SEL_ODD;
COL_BRUSH_OTHER_UNSEL_EVEN = $FFFFFF;
COL_BRUSH_OTHER_UNSEL_ODD = $F8F8F8;
COL_FONT_OTHER_SEL = $FFFFFF;
COL_FONT_OTHER_UNSEL = $606060;
var
LV: TCustomListView;
C: TCanvas;
FileName: String;
Index: Integer;
begin
LV := Sender;
Index := AItem.Index;
// get filename & canvas;
FileName := LV.Items.Item[Index].Caption;
C := LV.Canvas;
// setup brush;
C.Brush.Style := bsSolid;
if odSelected in AState then
if Index mod 2 = 0 then
C.Brush.Color := COL_BRUSH_OTHER_SEL_EVEN
else
C.Brush.Color := COL_BRUSH_OTHER_SEL_ODD
else
if Index mod 2 = 0 then
C.Brush.Color := COL_BRUSH_OTHER_UNSEL_EVEN
else
C.Brush.Color := COL_BRUSH_OTHER_UNSEL_ODD;
// setup font;
C.Font.Assign(LV.Font);
if odSelected in AState then
C.Font.Color := COL_FONT_OTHER_SEL
else
C.Font.Color := COL_FONT_OTHER_UNSEL;
// clear item's area;
C.FillRect(ARect);
// render text;
C.Brush.Style := bsClear;
C.TextOut(ARect.Left + 2, ARect.Top + (10 - C.TextHeight('h')) div 2, FileName);
end;
procedure Tfrm_Main.lv_FilesKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
Shift := Shift;
if Key = VK_RETURN then
mi_Open.Click;
if Key = VK_DELETE then
mi_Delete.Click;
end;
procedure Tfrm_Main.lv_FilesMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
P: TPoint;
begin
Shift := Shift;
if Button = mbRight then
begin
P := lv_Files.ClientToScreen(Point(X, Y));
FLastX := P.X;
FLastY := P.Y;
FShowMenu := True;
end;
end;
procedure Tfrm_Main.mi_DeleteClick(Sender: TObject);
var
FileName: String;
DlgCaption: String;
begin
if lv_Files.Items.Count < 1 then
Exit;
if not Assigned(lv_Files.ItemFocused) then
Exit;
FileName := lv_Files.ItemFocused.Caption;
if FileExists(FileName) then
begin
DlgCaption := 'Delete';
if MessageDlg(DlgCaption, Format('Delete "%s"?' + #13#13 + 'Trash is not used ("rm -rf").', [FileName]), mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
Exit;
if ExecuteCommand('', 'rm', [FileName, '-rf'], mi_SuperUser.Checked) then
begin
MessageDlg( Format( 'Successfully removed "%s".',
[ FileName ] ),
mtInformation,
[mbOk],
0 );
// update db and refresh current search;
FSuppressUpdateDBDialogs := True;
mi_UpdateDB.Click;
FSuppressUpdateDBDialogs := False;
end
else
MessageDlg( Format( 'Could not remove "%s".' + #13#13 +
'Perhaps you don''t have sufficient permissions.' + #13#13 +
'%s' + #13#13 +
'But be warned, with great power comes great responsability.',
[ FileName, GetSuperUserDirections ] ),
mtWarning,
[mbOk],
0 );
end
else
MessageDlg( Format( '"%s" does not exist.' + #13#13 +
'You should update the "locate" database.' + #13#13 +
'%s',
[ FileName, GetUpdateDBDirections ] ),
mtWarning,
[mbOk],
0 );
end;
procedure Tfrm_Main.mi_TestBackdoorClick(Sender: TObject);
procedure Report_Test_Result(TestResultOk: Boolean);
const
STATUS_DESC: array [Boolean] of String = ('failed', 'succeeded');
begin
ShowMessageFmt('Operation %s.', [STATUS_DESC[TestResultOk]]);
end;
begin
//Report_Test_Result(ExecuteCommand('~/', 'bad_command', [], False)); // ok, should fail; // test bad commands;
//Report_Test_Result(ExecuteCommand('~/', 'bad_command', ['bad_param'], False)); // ok, should fail; // test bad commands with bad params;
//Report_Test_Result(TriggerCommand('~/', 'bad_command', [], False)); // ok, should fail; // test bad commands;
//Report_Test_Result(TriggerCommand('~/', 'bad_command', ['bad_param'], False)); // ok, should fail; // test bad commands with bad params;
//Report_Test_Result(ExecuteCommand('~/', 'nemo', ['~/blank space zzzzzz'], False)); // ok; // test files with blank spaces in path;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', '/usr/bin/kate', ['/etc/lighttpd/lighttpd.conf'], False)); // ok; // test open file with kate no sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', '/usr/bin/kate', ['/etc/lighttpd/lighttpd.conf'], True)); // NOK; // test open file with kate with sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', '/usr/bin/gedit', ['/etc/lighttpd/lighttpd.conf'], True)); // ok, sync while gedit runs; // test open file with gedit with sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', '/usr/bin/xdg-open', ['/etc/lighttpd/lighttpd.conf'], False)); // ok; // test open file with OS default no sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', '/usr/bin/xdg-open', ['/etc/lighttpd/lighttpd.conf'], True)); // ok; // test open file with OS default with sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', '/usr/bin/xdg-open', ['/etc/lighttpd/'], False)); // ok; // test open dir with OS default no sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', '/usr/bin/xdg-open', ['/etc/lighttpd/'], True)); // ok, but hangs; // test open dir with OS default with sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', 'nemo', ['/etc/lighttpd/'], False)); // ok; // test open file with nemo default no sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', 'nemo', ['/etc/lighttpd/'], True)); // ok, but doesn't return; // test open dir with nemo with sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', 'nautilus', ['/etc/lighttpd/'], False)); // ok, doesn't return; // test dir file with nautilus default no sudo;
//Report_Test_Result(ExecuteCommand('/etc/lighttpd/', 'nautilus', ['/etc/lighttpd/'], True)); // ok, doesn't return; // test open dir with nautilus with sudo;
//Report_Test_Result(TriggerCommand('/etc/lighttpd/', 'nemo', ['/etc/lighttpd/'], False, TMO_MS_OPEN_DOC)); // ok; // test open file with nemo default no sudo;
//Report_Test_Result(TriggerCommand('/etc/lighttpd/', 'nemo', ['/etc/lighttpd/'], True, TMO_MS_OPEN_DOC)); // ok, but WarnUsers failure; // test open dir with nemo with sudo;
//Report_Test_Result(TriggerCommand('/etc/lighttpd/', 'nautilus', ['/etc/lighttpd/'], False, TMO_MS_OPEN_DOC)); // ok, but WarnUsers failure; // test dir file with nautilus default no sudo;
//Report_Test_Result(TriggerCommand('/etc/lighttpd/', 'nautilus', ['/etc/lighttpd/'], True, TMO_MS_OPEN_DOC)); // ok, but WarnUsers failure; // test open dir with nautilus with sudo;
{
Conclusions:
- kate crashes if ran with sudo;
- nemo runs, but freezes locator if ran with sudo - internal process hangs?;
- nautilus runs, but freezes locator regardless if ran with sudo or not - internal process hangs?; to check if same behavior if using separate thread;
}
end;
procedure Tfrm_Main.mi_TrayOptionsClick(Sender: TObject);
begin
mi_Options.Click;
end;
procedure Tfrm_Main.mi_TrayExitClick(Sender: TObject);
begin
mi_Exit.Click;
end;
procedure Tfrm_Main.mi_AboutClick(Sender: TObject);
begin
MessageDlg( Application.Title + #13#13 +
' Purpose: Unix "locate" command front-end.' + #13 +
' Author: Alex Tuduran' + #13 +
' License: Completely free' + #13 +
' Version: ' + APP_VER,
mtInformation,
[mbOk],
0 );
end;
procedure Tfrm_Main.mi_OpenWithSelectAddClick(Sender: TObject);
var
NumApps: Integer;
FileName: String;
App: String;
function Open_With_App_Is_Registered(FileName: String): Boolean;
var
NumApps: Integer;
i: Integer;
begin
Result := False;
NumApps := Pers_OpenWith_Get_Num_Apps;
for i := 0 to NumApps - 1 do
if FileName = Pers_OpenWith_Get_App(i) then
begin
Result := True;
Break;
end;
end;
begin
if lv_Files.Items.Count < 1 then
Exit;
if not Assigned(lv_Files.ItemFocused) then
Exit;
dlg_OpenWith.FileName := '';
dlg_OpenWith.InitialDir := ExtractFilePath(dlg_OpenWith.FileName);
if dlg_OpenWith.Execute then
begin
App := dlg_OpenWith.FileName;
// register app;
if not Open_With_App_Is_Registered(App) then
begin
NumApps := Pers_OpenWith_Get_Num_Apps;
Pers_OpenWith_Set_Num_Apps(NumApps + 1);
Pers_OpenWith_Set_App(NumApps, App);
end;
// run app;
FileName := lv_Files.ItemFocused.Caption;
if not ExecuteCommand(Get_Home_Dir, App, [FileName], ac_SuperUser.Checked) then
WarnUser(Format('Could not open "%s" with "%s": External failure.', [FileName, App]));
end;
end;
procedure Tfrm_Main.mi_UpdateDBClick(Sender: TObject);
var
CmdStatus: Boolean;
DlgCaption: String;
begin
DlgCaption := 'Update locate database';
if not FSuppressUpdateDBDialogs then
if MessageDlg(DlgCaption, 'This is a potentially long operation lasting seconds to minutes.' + #13#13 + 'Continue?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
begin
MessageDlg(DlgCaption, 'Database was not updated.' + #13#13 + 'Operation was canceled by user.', mtWarning, [mbOk], 0);
Exit;
end;
CmdStatus := False;
try
SetOperation('Updating database...');
SetStatus('Busy');
LockUI(True);
Application.ProcessMessages;
CmdStatus := ExecuteCommand('', 'updatedb', [], True); // updatedb requires sudo;
Application.ProcessMessages;
if CmdStatus and (Length(edt_SearchPattern.Text) > 0) then
begin
SetOperation('Updating current search...');
SetStatus('Busy');
ResetLocate;
LocateFiles(edt_SearchPattern.Text, True);
end;
if not FSuppressUpdateDBDialogs then
if CmdStatus then
MessageDlg(DlgCaption, 'Database updated successfully.', mtInformation, [mbOk], 0)
else
MessageDlg(DlgCaption, 'Database could not be updated.' + #13#13 + 'Please open a terminal and manually run command "sudo updatedb".', mtWarning, [mbOk], 0);
finally
LockUI(False);
SetStatus('Idle');
SetOperation('');
end;
end;
procedure Tfrm_Main.mi_ExitClick(Sender: TObject);
begin
FCanClose := True;
Close;
end;
procedure Tfrm_Main.mi_CopyFullNameToClipboardClick(Sender: TObject);
begin
if lv_Files.Items.Count < 1 then
Exit;
if not Assigned(lv_Files.ItemFocused) then
Exit;
Clipboard.AsText := lv_Files.ItemFocused.Caption;
end;
procedure Tfrm_Main.mi_CopyNameOnlyToClipboardClick(Sender: TObject);
begin
if lv_Files.Items.Count < 1 then
Exit;
if not Assigned(lv_Files.ItemFocused) then
Exit;
Clipboard.AsText := ExtractFileName(lv_Files.ItemFocused.Caption);
end;
procedure Tfrm_Main.mi_CopyPathToClipboardClick(Sender: TObject);
begin
if lv_Files.Items.Count < 1 then
Exit;
if not Assigned(lv_Files.ItemFocused) then
Exit;
Clipboard.AsText := ExtractFilePath(lv_Files.ItemFocused.Caption);
end;
procedure Tfrm_Main.mi_OpenClick(Sender: TObject);
var
App: String;
FileName: String;
begin
if lv_Files.Items.Count < 1 then
Exit;
if not Assigned(lv_Files.ItemFocused) then
Exit;
App := '';
FileName := lv_Files.ItemFocused.Caption;
if not OpenDocumentEx(FileName, ac_SuperUser.Checked, App) then // with os default handler;
WarnUser(Format('Could not open "%s" (with OS default handler "%s"): External failure.', [FileName, App]));
end;
procedure Tfrm_Main.mi_OpenPathClick(Sender: TObject);
var
App: String;
Path: String;
begin
if lv_Files.Items.Count < 1 then
Exit;
if not Assigned(lv_Files.ItemFocused) then
Exit;
App := Pers_Gen_Get_Open_Path_App;
Path := ExtractFilePath(lv_Files.ItemFocused.Caption);
if (Length(App) < 1) or
(App = OPEN_PATH_OPT_OS_DEFAULT) then
begin
if not OpenDocumentEx(Path, ac_SuperUser.Checked) then // with os default handler;
WarnUser(Format('Could not open "%s" (with OS default handler "%s"): External failure.', [Path, App]));
end
else
begin
if (LowerCase(App) = 'nautilus') or
(LowerCase(App) = 'nemo') then
App := LowerCase(App);
if not TriggerCommand(Get_Home_Dir, App, [Path], ac_SuperUser.Checked, TMO_MS_OPEN_DOC) then
WarnUser(Format('Could not open "%s" with "%s": External failure.', [Path, App]));
end;
end;
procedure Tfrm_Main.mi_PropertiesClick(Sender: TObject);
const
FILE_PROPERTIES_MSG: array [0..17] of String =
(
'A great file.',
'Best file in the world.',
'An amazing file.',
'The super-file.',
'A file like no other.',
'Not a file you want to open.',
'You don''t screw up with this file.',
'A file to remember.',
'The mother of all files.',
'Can''t un-see one you''ve seen this file.',
'Monster-file.',
'A lousy-ass file.',
'Not worth showing properties for this file.',
'This file will keep you awake all night.',
'The file that will change it all.',
'You won''t believe how awesome this file is.',
'This file is for babies',
'Properties: Umm, agh, lots of properties..'
);
begin
MessageDlg(FILE_PROPERTIES_MSG[Random(Length(FILE_PROPERTIES_MSG))], mtInformation, [mbOK], 0);
end;
procedure Tfrm_Main.mnu_PopupFilesClose(Sender: TObject);
var
i: Integer;
begin
i := 0;
while i < mi_OpenWith.Count do
if mi_OpenWith.Items[i].Tag and OPEN_WITH_APP_MENU_ITEM_MASK = OPEN_WITH_APP_MENU_ITEM_MASK then
begin
mi_OpenWith.Items[i].Clear;
mi_OpenWith.Items[i].Free;
end
else
Inc(i);
end;
procedure Tfrm_Main.mnu_PopupFilesPopup(Sender: TObject);
var
NumApps: Integer;
Item: TMenuItem;
App: String;
i: Integer;
begin
NumApps := Pers_OpenWith_Get_Num_Apps;
if NumApps < 1 then
Exit;
for i := 0 to NumApps - 1 do
begin
App := Pers_OpenWith_Get_App(i);
if FileExists(App) then
begin
Item := TMenuItem.Create(nil);
Item.Caption := App;
Item.Tag := OPEN_WITH_APP_MENU_ITEM_MASK or i;
Item.OnClick := OpenWithAppClick;
mi_OpenWith.Add(Item);
end;
end;
end;
procedure Tfrm_Main.ti_MainClick(Sender: TObject);
begin
mi_TrayNewSearchWindow.Click;
end;
procedure Tfrm_Main.tmr_PopupTimer(Sender: TObject);
begin
if FShowMenu then
begin
FShowMenu := False;
mnu_PopupFiles.PopUp(FLastX, FLastY);
end;
end;
procedure Tfrm_Main.ti_MainDblClick(Sender: TObject);
begin
mi_TrayNewSearchWindow.Click;
end;
procedure Tfrm_Main.OpenWithAppClick(Sender: TObject);
var
Item: TMenuItem;
AppIndex: Integer;
App: String;
FileName: String;
begin
if lv_Files.Items.Count < 1 then
Exit;
if not Assigned(lv_Files.ItemFocused) then
Exit;
if not (Sender is TMenuItem) then
Exit;
Item := Sender as TMenuItem;
if not (Item.Tag and OPEN_WITH_APP_MENU_ITEM_MASK = OPEN_WITH_APP_MENU_ITEM_MASK) then
Exit;
AppIndex := Item.Tag and not OPEN_WITH_APP_MENU_ITEM_MASK;
if (AppIndex < 0) or (AppIndex > Pers_OpenWith_Get_Num_Apps - 1) then
Exit;
App := Pers_OpenWith_Get_App(AppIndex);
if not FileExists(App) then
Exit;
FileName := lv_Files.ItemFocused.Caption;
if not ExecuteCommand(Get_Home_Dir, App, [FileName], ac_SuperUser.Checked) then
WarnUser(Format('Could not open "%s" with "%s": External failure.', [FileName, App]));
end;
procedure Tfrm_Main.Reset;
begin
Width := FInitialW;
Height := FInitialH;
FCanClose := False;
FLastX := 0;
FLastY := 0;
FShowMenu := False;
ResetLocate;
edt_SearchPattern.Text := '';
lv_Files.Clear;
lv_Files.ItemIndex := -1;
sb_Main.Panels[0].Text := '';
sb_Main.Panels[1].Text := '';
LockUI(False);
SetStatus('Idle');
SetOperation('');
end;
procedure Tfrm_Main.ResetLocate;
begin
FLastCommandParams := '';
FLastCommandOutput := '';
end;
procedure Tfrm_Main.LockUI(Lock: Boolean);
begin
sb_Main.Enabled := not Lock;
edt_SearchPattern.Enabled := not Lock;
btn_Locate.Enabled := not Lock;
lv_Files.Enabled := not Lock;
Application.ProcessMessages;
end;
procedure Tfrm_Main.SetOperation(Operation: String);
begin
FOperation := Operation;
Caption := Application.Title;
if mi_SuperUser.Checked then
Caption := Format('%s (Super-user mode)', [Caption]);
if Length(Operation) > 0 then
Caption := Format('%s - [%s] ', [Caption, Operation]);
Application.ProcessMessages;
end;
function Tfrm_Main.GetOperation: String;
begin
Result := FOperation;
end;
procedure Tfrm_Main.SetStatus(Status: String);
begin
sb_Main.Panels[0].Text := Status;
Application.ProcessMessages;
end;
function Tfrm_Main.GetStatus: String;
begin
Result := sb_Main.Panels[0].Text;
end;
procedure Tfrm_Main.WarnUser(Msg: String);
begin
WLog(Msg);
if Pers_Gen_Get_Show_Op_Fail_Warns then
MessageDlg(Msg, mtWarning, [mbOk], 0);
end;
function Tfrm_Main.GetUpdateDBMenuPath: String;
begin
Result := Format( 'main menu -> %s -> %s',
[ Caption_To_Message(mi_Tools.Caption),
Caption_To_Message(mi_UpdateDB.Caption) ] );
end;
function Tfrm_Main.GetUpdateDBShortcut: String;
begin
Result := ShortCutToText(mi_UpdateDB.ShortCut);
end;
function Tfrm_Main.GetUpdateDBDirections: String;
begin
Result := Format('You can update the "locate" database from %s or by pressing %s.', [GetUpdateDBMenuPath, GetUpdateDBShortcut]);
end;
function Tfrm_Main.GetSuperUserMenuPath: String;
begin
Result := Format( 'main menu -> %s -> %s',
[ Caption_To_Message(mi_Tools.Caption),
Caption_To_Message(mi_SuperUser.Caption) ] );
end;
function Tfrm_Main.GetSuperUserDirections: String;
begin
Result := Format('Try impersonating super-user by checking %s.', [GetSuperUserMenuPath]);
end;
procedure Tfrm_Main.ILog(Msg: String);
begin
ULogger.ILog(Msg);
end;
procedure Tfrm_Main.WLog(Msg: String);
begin
ULogger.WLog(Msg);
end;
procedure Tfrm_Main.ELog(Msg: String; Routine: String);
begin
ULogger.ELog(Msg, Routine);
end;
function Tfrm_Main.ExecuteCommand(Directory: String; Command: String; Parameters: array of String; SuperUser: Boolean): Boolean;
var
CmdOutput: String;
begin
CmdOutput := '';
Result := ExecuteCommand(Directory, Command, Parameters, SuperUser, CmdOutput);
end;
function Tfrm_Main.ExecuteCommand(Directory: String; Command: String; Parameters: array of String; SuperUser: Boolean; var Output: String): Boolean;
var
Process: TProcess;
i: Integer;
ExitStatus: Integer;
Cmd: String;
T0: Integer;
T1: Integer;
begin
Process := TProcess.Create(nil);
if SuperUser then
begin
Process.Executable := 'sudo';
Process.Parameters.Add(Command);