-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.pas
646 lines (601 loc) · 21.2 KB
/
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
{
Project: FindDupFar http://code.google.com/p/findupfar/
Author: Alexey Suhinin http://x-alexey.narod.ru
License: GPL v.2
}
unit Main;
{$I PRJDefines.inc}
Interface
uses
// Qmemory,
{$IFDEF DEBUG}
DebugLog,
{$ENDIF}
{$IFDEF DIALOGS}
MainDialogs,
{$ENDIF}
Windows,
Classes,
SysUtils,
Plugin,
FarAPI,
PluginLng,
FileUtils,
FileSystemObjects;
const
PPIF_GROUP = 1;
type
TFarPlugin = class
private
{$IFDEF DIALOGS}
FMainDialog: TMainDialog;
{$ENDIF}
// FCurrentPanelItem: Integer;
FFileGroupList: TFileGroupList;
FFlagStop: Boolean;
FFlagPressBreak: Boolean;
// FFlagInProcess: Boolean;
// FFlagUseAnotherPanel: Boolean;
FIncludeFileMasks, FExcludeDirMasks, FSearchPaths: TStringList;
protected
function IsBreak: Boolean;
procedure FindDupFile(Cat: String);
function FindDupDir(Cat: String): TDirectoryObject;
function ProcessAllDisks: Integer;
public
constructor Create;
destructor Destroy; Override;
procedure GetOpenPluginInfo(var aInfo: TOpenPluginInfo);
function GetFindData(var aPanelItems: PPluginPanelItemArray;
var aItemsNumber: Integer; opMode: Integer): Integer;
procedure FreeFindData(aPanelItems: PPluginPanelItemArray;
aItemsNumber: integer);
function ProcessEvent(aEvent: Integer; aParam: pointer): Integer;
function ProcessKey(aKey: Integer; aControlState: UINT): Integer;
procedure ProcessRemoveKey;
end;
procedure GetPluginInfo(var aInfo: TPluginInfo); stdcall;
Implementation
constructor TFarPlugin.Create;
begin
inherited Create;
FFileGroupList:=Nil;
// FCurrentPanelItem:=-1;
// FFlagInProcess:=False;
// FFlagUseAnotherPanel:=False;
{
FMainDialog:=TMainDialog.Create;
FMainDialog.Show;
}
end;
destructor TFarPlugin.Destroy;
begin
if Assigned(FFileGroupList) then FFileGroupList.Free;
{
FMainDialog.Free;
}
inherited Destroy;
end;
function TFarPlugin.IsBreak: Boolean;
begin
Result:=FFlagStop;
if (not Result) and (FFlagPressBreak or CheckForEsc) then
begin
FFlagStop:=True;
Result:=True;
end;
end;
var
PanelModes: packed array[0..0] of TPanelMode =
((ColumnTypes: 'N';
ColumnWidths: '0';
ColumnTitles: nil;
FullScreen: integer(False);
DetailedStatus: integer(True);
AlignExtensions: integer(False);
CaseConversion: integer(False);
StatusColumnTypes: nil;
StatusColumnWidths: nil)
);
KeyBarTitles: TKeyBarTitles;
procedure TFarPlugin.GetOpenPluginInfo(var aInfo: TOpenPluginInfo);
begin
ZeroMemory(@aInfo, SizeOf(TOpenPluginInfo));
ZeroMemory(@KeyBarTitles, SizeOf(TKeyBarTitles));
KeyBarTitles.Titles[6]:=GetMsg(msgRemove);
KeyBarTitles.AltShiftTitles[2]:=GetMsg(msgMode);
with aInfo do
begin
StructSize:=SizeOf(TOpenPluginInfo);
Flags:=OPIF_USEHIGHLIGHTING or OPIF_ADDDOTS or OPIF_REALNAMES;
PanelModesArray:=@PanelModes;
PanelModesNumber:=1;
StartPanelMode:=$30;
StartSortMode:=SM_UNSORTED;
KeyBar:=@KeyBarTitles;
end;
end;
procedure TFarPlugin.FindDupFile(Cat: String);
var
hFindFile: THandle;
FindData: TWin32FindData;
I: Integer;
Found: Bool;
begin
if Assigned(FExcludeDirMasks) then
begin
I:=0;
while (I<FExcludeDirMasks.Count) and
(CmpName(PChar(FExcludeDirMasks.Strings[I]),
PChar(Cat),
False)=False) do Inc(I);
if I<>FExcludeDirMasks.Count then Exit;
end;
hFindFile:=FindFirstFile(PChar(Cat+'*'), FindData);
if hFindFile<>INVALID_HANDLE_VALUE then
begin
Found:=True;
while (Found<>False) and (not isBreak) do
begin
if (FindData.dwFileAttributes and faDirectory)<>0 then
begin
if (StrPas(@FindData.cFileName)<>'.') and
(StrPas(@FindData.cFileName)<>'..') then
FindDupFile(Cat+StrPas(@FindData.cFileName)+'\');
end
else
begin
if Assigned(FIncludeFileMasks) then
begin
I:=0;
while (I<FIncludeFileMasks.Count) and
(CmpName(PChar(FIncludeFileMasks.Strings[I]),
@FindData.cFileName,
False)=False) do Inc(I);
if I<>FIncludeFileMasks.Count then
begin
Move(FindData.cFileName,
FindData.cFileName[Length(Cat)],
StrLen(FindData.cFileName)+1);
Move(Cat[1],
FindData.cFileName,
Length(Cat));
FFileGroupList.Add(TFileObject.Create(FindData));
end;
end
else
begin
Move(FindData.cFileName,
FindData.cFileName[Length(Cat)],
StrLen(FindData.cFileName)+1);
Move(Cat[1],
FindData.cFileName,
Length(Cat));
FFileGroupList.Add(TFileObject.Create(FindData));
end;
end;
Found:=FindNextFile(hFindFile, FindData);
end;
Windows.FindClose(hFindFile);
end;
end;
function TFarPlugin.FindDupDir(Cat: String): TDirectoryObject;
var
hFindFile: THandle;
FindData: TWin32FindData;
I: Integer;
Found: Bool;
tmpDirectoryObject: TDirectoryObject;
begin
Result:=nil;
if Assigned(FExcludeDirMasks) then
begin
I:=0;
while (I<FExcludeDirMasks.Count) and
(CmpName(PChar(FExcludeDirMasks.Strings[I]),
PChar(Cat),
False)=False) do Inc(I);
if I<>FExcludeDirMasks.Count then Exit;
end;
{
Move(Cat[1], FindData.cFileName, Length(Cat));
FindData.cFileName[Length(Cat)]:=#0;
FindData.nFileSizeLow:=0;
FindData.dwFileAttributes:=GetFileAttributes(FindData.cFilename);
}
SetLastError(0);
hFindFile:=FindFirstFile(PChar(Cat+'*'), FindData);
if GetLastError=0 then
begin
if hFindFile<>INVALID_HANDLE_VALUE then
if ((FindData.dwFileAttributes and faDirectory)<>0) and (StrPas(@FindData.cFileName)='.') then
begin
Move(Cat[1], FindData.cFileName, Length(Cat));
FindData.cFileName[Length(Cat)]:=#0;
Result:=TDirectoryObject.Create(FindData);
Found:=FindNextFile(hFindFile, FindData);
while (Found<>False) and (not isBreak) do
begin
if (FindData.dwFileAttributes and faDirectory)<>0 then
begin
if (StrPas(@FindData.cFileName)<>'.') and
(StrPas(@FindData.cFileName)<>'..') then
begin
tmpDirectoryObject:=FindDupDir(Cat+StrPas(@FindData.cFileName)+'\');
if Assigned(tmpDirectoryObject) then
begin
// tmpDirectoryObject.SetFileSystemRecord(FindData);
Result.AddMember(tmpDirectoryObject);
end;
end;
end
else
begin
if Assigned(FIncludeFileMasks) then
begin
I:=0;
while (I<FIncludeFileMasks.Count) and
(CmpName(PChar(FIncludeFileMasks.Strings[I]),
@FindData.cFileName,
False)=False) do Inc(I);
if I<>FIncludeFileMasks.Count then
begin
Move(FindData.cFileName,
FindData.cFileName[Length(Cat)],
StrLen(FindData.cFileName)+1);
Move(Cat[1],
FindData.cFileName,
Length(Cat));
Result.AddMember(TFileObject.Create(FindData));
end;
end
else
begin
Move(FindData.cFileName,
FindData.cFileName[Length(Cat)],
StrLen(FindData.cFileName)+1);
Move(Cat[1],
FindData.cFileName,
Length(Cat));
Result.AddMember(TFileObject.Create(FindData));
end;
end;
Found:=FindNextFile(hFindFile, FindData);
end;
Windows.FindClose(hFindFile);
end;
FFileGroupList.Add(Result);
end
else
FreeAndNil(Result);
end;
function TFarPlugin.ProcessAllDisks: Integer;
var
Drivers: array[0..255] of Char;
Index: Byte;
RootDirectory: String;
I: LongInt;
begin
if FileExists(GetPluginPath+'exclude.txt') then
begin
FExcludeDirMasks:=TStringList.Create;
FExcludeDirMasks.LoadFromFile(GetPluginPath+'exclude.txt');
end
else
FExcludeDirMasks:=Nil;
if FileExists(GetPluginPath+'include.txt') then
begin
FIncludeFileMasks:=TStringList.Create;
FIncludeFileMasks.LoadFromFile(GetPluginPath+'include.txt');
if FIncludeFileMasks.Count<1 then FreeANDNil(FIncludeFileMasks);
end
else
FIncludeFileMasks:=Nil;
if FileExists(GetPluginPath+'SearchPaths.txt') then
begin
FSearchPaths:=TStringList.Create;
FSearchPaths.LoadFromFile(GetPluginPath+'SearchPaths.txt');
if FSearchPaths.Count>0 then
for I:=0 to FSearchPaths.Count-1 do
begin
if FileExists(GetPluginPath+'FindDupDir.flg') then
FindDupDir(PChar(FSearchPaths[I]))
else
FindDupFile(PChar(FSearchPaths[I]));
end;
FSearchPaths.Free;
end
else
begin
GetLogicalDriveStrings(SizeOf(Drivers), @Drivers);
Index:=0;
RootDirectory:=StrPas(Drivers);
while (Length(RootDirectory)>0) and (not isBreak) do
begin
if FileExists(GetPluginPath+'FindDupDir.flg') then
FindDupDir(RootDirectory)
else
FindDupFile(RootDirectory);
(*
case GetDriveType(PChar(@RootDirectory[1])) of
// DRIVE_REMOVABLE: Writeln(F, 'ñìåííûé)');
DRIVE_FIXED,
DRIVE_CDROM: begin
Scan(RootDirectory);
end;
DRIVE_REMOTE: Writeln(F, 'ñåòåâîé)');
DRIVE_CDROM: Writeln(F, 'CD-ROM)');
DRIVE_RAMDISK: Writeln(F, 'RAM)');
end;
*)
Index:=Index+Length(RootDirectory)+1;
RootDirectory:=StrPas(@Drivers[Index]);
end;
end;
// FindDupDir('C:\2\');
FIncludeFileMasks.Free;
FExcludeDirMasks.Free;
Result:=Integer(True);
end;
function TFarPlugin.GetFindData(var aPanelItems: PPluginPanelItemArray;
var aItemsNumber: integer; opMode: integer): Integer;
var
I, J, Index, Gr: LongInt;
hScreen: THandle;
begin
{$IFDEF UseExcept}
try
{$ENDIF UseExcept}
hScreen:=SaveScreen(0, 0, -1, -1);
try
Message(0, nil,
[Integer(msgPluginMenuString),
Integer(msgCreateFileList)],
0);
if FFileGroupList=nil then
begin
FFlagStop:=False;
FFlagPressBreak:=False;
FFileGroupList:=TFileGroupList.Create;
Result:=ProcessAllDisks;
end
else
begin
{$IFDEF FullRescan}
if FFlagStop then // åñëè ïðåäûäóùèé ïîèñê áûë ïðåðâàí ïîëüçîâàòåëåì,
begin // òî ïîëíîå ïåðåñêàíèðîâàíèå,
FFlagStop:=False;
FFlagPressBreak:=False;
FFileGroupList.Clear;
Result:=ProcessAllDisks;
end
else // èíà÷å ïðîâåðêà âàëèäíîñòè èíôîðìàöèè
begin
{$ENDIF FullRescan}
FFlagStop:=False;
FFlagPressBreak:=False;
{$IFDEF AUTO_VALIDATE}
{$IFDEF DEBUG}
AddDebugStringToLog('Enter to "FFileGroupList.Validate"', 2);
{$ENDIF}
FFileGroupList.Validate;
{$IFDEF DEBUG}
AddDebugStringToLog('Exit to "FFileGroupList.Validate"', -2);
{$ENDIF}
{$ELSE AUTO_VALIDATE}
{$IFDEF DEBUG}
AddDebugStringToLog('Enter to "FFileGroupList.Pack"', 2);
{$ENDIF}
FFileGroupList.Pack;
{$IFDEF DEBUG}
AddDebugStringToLog('Exit to "FFileGroupList.Pack"', -2);
{$ENDIF}
{$ENDIF AUTO_VALIDATE}
Result:=Integer(True);
{$IFDEF FullRescan}
end;
{$ENDIF FullRescan}
end;
// FFileGroupList.Pack;
{$IFDEF SORT_PANEL}
FFileGroupList.Sort;
{$ENDIF}
aItemsNumber:=FFileGroupList.GroupedFilesCount+FFileGroupList.GroupsCount;
GetMem(aPanelItems, SizeOf(TPluginPanelItem)*aItemsNumber);
ZeroMemory(aPanelItems, SizeOf(TPluginPanelItem)*aItemsNumber);
Index:=0;
Gr:=1;
for I:=0 to FFileGroupList.GroupsCount-1 do
begin
{
if FFileGroupList.Items[I].Count>1 then
begin
}
With aPanelItems^[Index] do
begin
{!!!}
UserData:=DWORD(FFileGroupList.Items[I]);
Flags:=PPIF_GROUP;
{!!!}
With FindData do
begin
dwFileAttributes:=FILE_ATTRIBUTE_DIRECTORY;
StrCopy(PChar(@cFileName[0]), PChar('['+StrPas(GetMsg(msgGroup))+' '
+IntToStr(Gr)+']'));
Inc(Gr);
Inc(Index);
end;
end;
for J:=0 to FFileGroupList.Items[I].Count-1 do
With aPanelItems^[Index] do
begin
{!!!}
UserData:=DWORD(FFileGroupList.Items[I].Items[J]);
{!!!}
FFileGroupList.Items[I].Items[J].GetFileSystemRecord(FindData);
Inc(Index);
end;
// end;
end;
finally
RestoreScreen(hScreen);
end;
{$IFDEF UseExcept}
except
Result:=Integer(False)
end;
{$ENDIF UseExcept}
end;
procedure TFarPlugin.FreeFindData(aPanelItems: PPluginPanelItemArray;
aItemsNumber: integer);
begin
FreeMem(aPanelItems);
end;
{.$DEFINE DEBUG_ANOTHERPANEL}
function TFarPlugin.ProcessEvent(aEvent: Integer; aParam: pointer): Integer;
{
var
PanelInfo: TPanelInfo;
}
// PanelItem: TPluginPanelItem;
{$IFDEF DEBUG_ANOTHERPANEL}
F: Text;
{$ENDIF DEBUG_ANOTHERPANEL}
begin
Result:=Integer(False);
case aEvent of
FE_BREAK: FFlagPressBreak:=True;
{
FE_REDRAW: begin
if not FFlagInProcess then
begin
FFlagInProcess:=True;
if (Control(THandle(self), FCTL_GETPANELINFO, @PanelInfo)<>0) and
(PanelInfo.CurrentItem>0) and
(PanelInfo.CurrentItem<=PanelInfo.ItemsNumber) and
(PanelInfo.SelectedItemsNumber>0) and
((PanelInfo.PanelItems^[PanelInfo.CurrentItem].Flags and PPIF_GROUP)=0) then
begin
if not TFileSystemObject(PanelInfo.PanelItems^
[PanelInfo.CurrentItem].UserData).IsValid then
begin
Control(THandle(self), FCTL_UPDATEPANEL, nil);
Control(THandle(self), FCTL_REDRAWPANEL, nil);
if not ((Control(THandle(self), FCTL_GETPANELINFO, @PanelInfo)<>0) and
(PanelInfo.SelectedItemsNumber>0)) then
begin
FFlagInProcess:=False;
Exit;
end;
end;
if FFlagUseAnotherPanel and PanelInfo.Focus then
begin
// ïîêàç ñîäåðæèìîãî êàòàëîãà ïîä êóðñîðîì íà ïðîòèâîïîëîæíîé ïàíåëè
with PanelInfo.PanelItems^[PanelInfo.CurrentItem] do
// if Flags and PPIF_GROUP) = 0 and
begin
if (FindData.dwFileAttributes and faDirectory) = 0
then
Control(THandle(self), FCTL_SETANOTHERPANELDIR,
PChar(ExtractFileDir(StrPas(@FindData.cFileName))))
else
Control(THandle(self), FCTL_SETANOTHERPANELDIR, @FindData.cFileName);
// Control(THandle(self), FCTL_UPDATEANOTHERPANEL, nil);
Control(THandle(self), FCTL_REDRAWANOTHERPANEL, nil);
end;
end;
end;
FFlagInProcess:=False;
end;
end;
}
end;
end;
function TFarPlugin.ProcessKey(aKey: Integer; aControlState: UINT): Integer;
begin
{ñêðûòèå: ñêðûâàåì òåêóùóþ ïîçèöèþ è ïðåðûâàåì îáðàáîòêó}
Result:=Integer(False);
if (aControlState=0) and (aKey=VK_F7) then
begin
ProcessRemoveKey;
Control(THandle(self), FCTL_UPDATEPANEL, nil);
Control(THandle(self), FCTL_REDRAWPANEL, nil);
Result:=Integer(True);
end;
{ïðè ïåðåìåùåíèè ôàéëà: ñêðûâàåì òåêóùóþ ïîçèöèþ è âîçâðàùàåì îáðàáîòêó FAR}
if (aControlState=0) and (aKey=VK_F6) then
begin
ProcessRemoveKey;
Result:=Integer(False);
end;
{ïðè óäàëåíèè ôàéëà: ñêðûâàåì òåêóùóþ ïîçèöèþ è âîçâðàùàåì îáðàáîòêó FAR}
if (aControlState=0) and (aKey=VK_F8) then
begin
ProcessRemoveKey;
Result:=Integer(False);
end;
if (aControlState=PKF_SHIFT or PKF_ALT) and (aKey=VK_F3) then
begin
FFileGroupList.ChangeMode;
Control(THandle(self), FCTL_UPDATEPANEL, nil);
Control(THandle(self), FCTL_REDRAWPANEL, nil);
Result:=Integer(True);
end;
{$IFDEF VALIDATE}
if (aControlState=PKF_CONTROL) and (aKey=Ord('R')) then
begin
FFileGroupList.Validate;
Control(THandle(self), FCTL_UPDATEPANEL, nil);
Control(THandle(self), FCTL_REDRAWPANEL, nil);
Result:=Integer(True);
end;
{$ENDIF VALIDATE}
{$IFDEF DIALOGS}
if (aControlState=PKF_SHIFT or PKF_ALT) and (aKey=VK_F1) then
begin
FMainDialog:=TMainDialog.Create;
FMainDialog.Show;
FMainDialog.Free;
Result:=Integer(True);
end;
{$ENDIF}
{
if (aControlState=(PKF_CONTROL or PKF_ALT)) and (aKey=Ord('Q')) then
begin
FFlagUseAnotherPanel:=not FFlagUseAnotherPanel;
Result:=Integer(True);
end;
}
end;
procedure TFarPlugin.ProcessRemoveKey;
var
PanelInfo: TPanelInfo;
I: LongInt;
begin
Control(THandle(self), FCTL_GETPANELINFO, @PanelInfo);
with PanelInfo do
for I:=0 to SelectedItemsNumber-1 do
begin
with SelectedItems^[I] do
if (Flags and PPIF_GROUP)<>0 then
TFileSystemObjectList(UserData).Flags:=TFileSystemObjectList(UserData).Flags+[gfDeleted]
else
TFileSystemObject(UserData).Flags:=TFileSystemObject(UserData).Flags+[flDeleted];
end;
// FFileGroupList.Pack;
end;
var
PluginStrings: array[0..0] of PChar;
procedure GetPluginInfo(var aInfo: TPluginInfo);
begin
ZeroMemory(@aInfo, SizeOf(TPluginInfo));
with aInfo do
begin
StructSize := SizeOf(TPluginInfo);
PluginStrings[0]:=GetMsg(msgPluginMenuString);
PluginMenuStrings:=@PluginStrings;
PluginMenuStringsNumber:=1;
CommandPrefix:=nil;
end;
end;
End.