File: filebrowsertypes.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (457 lines) | stat: -rw-r--r-- 11,535 bytes parent folder | download
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
unit filebrowsertypes;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Contnrs;

type
  TStartDir = (sdProjectDir, sdLastOpened, sdCustomDir);

  TRootDir = (rdProjectDir, rdUserDir, rdRootDir, rdCustomDir);

  EFileEntry = Class(Exception);

  TEntryType = (etDirectory,etFile,etSymlink);
  TEntryTypes = Set of TEntryType;

  TReadEntryOption = (reoHidden,reoRecurse);
  TReadEntryOptions = Set of TReadEntryOption;

Const
  AllEntryTypes = [Low(TEntryType)..High(TEntryType)];

Type
  { TFileSystemEntry }

  TFileSystemEntry = Class(TObject)
  private
    FName: String;
    FParent: TFileSystemEntry;
  Protected
    function GetChildCount: Integer; virtual;
    function GetEntry(index : integer): TFileSystemEntry; virtual;
  Public
    Constructor Create(aParent : TFileSystemEntry; const aName : string); virtual;
    Procedure AddEntry(aEntry : TFileSystemEntry); virtual;
    Class function EntryType : TEntryType; virtual; abstract;
    function AbsolutePath : String;
    Procedure Clear; virtual;
    Procedure ReadEntries(aOptions : TReadEntryOptions); virtual;
    Function HasEntries(aShowHidden : Boolean; aTypes : TEntryTypes = AllEntryTypes) : Boolean; virtual;
    Property EntryCount : Integer Read GetChildCount;
    Property Entries [index : integer] : TFileSystemEntry Read GetEntry; default;
    Property Name : String Read FName;
    Property Parent : TFileSystemEntry Read FParent;
  end;
  TFileSystemEntryArray = Array of TFileSystemEntry;

  { TSymlinkEntry }

  TSymlinkEntry = Class(TFileSystemEntry)
  private
    FTarget: String;
  Public
    Constructor Create(aParent : TFileSystemEntry; const aName, aTarget : string); reintroduce;
    Class function EntryType : TEntryType; override;
    property Target : String Read FTarget;
  end;

  { TDirectoryEntry }

  TDirectoryEntry = Class(TFileSystemEntry)
  Private
    FEntries:TFPObjectList;
  Protected
    function GetChildCount: Integer; override;
    function GetEntry(index : integer): TFileSystemEntry; override;
  Public
    Constructor Create(aParent : TFileSystemEntry; const aName: string); override;
    Destructor Destroy; override;
    Procedure Clear; override;
    Procedure ReadEntries(aOptions : TReadEntryOptions); override;
    Class function EntryType : TEntryType; override;
    Class function HasEntries(aPath : String; aShowHidden : Boolean; aTypes : TEntryTypes = AllEntryTypes) : Boolean; virtual;
    Function HasEntries(aShowHidden : Boolean; aTypes : TEntryTypes = AllEntryTypes) : Boolean; override;
    Procedure AddEntry(aEntry : TFileSystemEntry); override;
  end;

  { TFileEntry }

  TFileEntry = Class(TFileSystemEntry)
    Class function EntryType : TEntryType; override;
  end;
  TFileEntryArray = Array of TFileEntry;

  TTreeDoneEvent = procedure (Sender : TThread; aTree : TDirectoryEntry) of object;
  TTreeErrorEvent = procedure (Sender : TThread; const aError : String) of object;

  { TTreeCreatorThread }

  TTreeCreatorThread = Class(TThread)
  Private
    FRootDir : String;
    FOptions : TReadEntryOptions;
    FOnDone : TTreeDoneEvent;
    FOnError : TTreeErrorEvent;
    FNode : TDirectoryEntry;
    FError : String;
  Protected
    procedure FillNode(N: TDirectoryEntry);
    procedure DoDone;
    procedure DoError;
  Public
    constructor Create(aRootDir: String; aOptions: TReadEntryOptions; aOnDone: TTreeDoneEvent; aOnError : TTreeErrorEvent);
    procedure execute; override;
  end;


const
  DefaultStartDir = sdProjectDir;
  DefaultRootDir = sdProjectDir;
  DefaultFilesInTree = False;
  DefaultDirectoriesBeforeFiles = True;
  DefaultSyncCurrentEditor = False;
  DefaultSplitterPos = 150;

  SConfigFile         = 'idebrowserwin.xml';
  KeyStartDir         = 'StartDir';
  KeyRootDir          = 'RootDir';
  KeyCustomStartDir   = 'CustomDir';
  KeyCustomRootDir    = 'CustomRootDir';
  KeySplitterPos      = 'SplitterPos';
  KeyFilesInTree      = 'FilesInTree';
  KeyDirectoriesBeforeFiles     = 'DirectoriesBeforeFiles';
  KeySyncCurrentEditor = 'SyncCurrentEditor';
  KeySearchMatchOnlyFilename = 'MatchOnlyFileNames';
  KeySearchAbsoluteFilenames = 'AbsoluteFileNames';
  KeySearchLetters = 'SearchLetters';

  SViewFilebrowser = 'File browser';

resourcestring
  SFileBrowserIDEMenuCaption = 'File Browser';
  SFileSearcherIDEMenuCaption = 'File Searcher';
  SErrSearching = 'Error searching for files in directory "%s": %s';
  SFilesFound = 'Collected %d files in directory "%s"';
  SSearchingFiles = 'Start collecting files in directory "%s"';

implementation

{ TFileSystemEntry }

function TFileSystemEntry.GetChildCount: Integer;
begin
  Result:=0;
end;

function TFileSystemEntry.GetEntry(index : integer): TFileSystemEntry;
begin
  Result:=Nil;
end;

constructor TFileSystemEntry.Create(aParent: TFileSystemEntry; const aName: string);
begin
  FParent:=aParent;
  FName:=aName;
end;

procedure TFileSystemEntry.AddEntry(aEntry: TFileSystemEntry);
begin
  Raise EFileEntry.CreateFmt('Not supported for class %s',[ClassName]);
end;

function TFileSystemEntry.AbsolutePath: String;

var
  E: TFileSystemEntry;
  S : String;

begin
  E:=Self;
  S:='';
  While Assigned(E) do
    begin
    if (S<>'') then
      S:=PathDelim+S;
    S:=E.Name+S;
    E:=E.Parent;
    end;
  Result:=S;
end;

procedure TFileSystemEntry.Clear;
begin
  // Do nothing;
end;

procedure TFileSystemEntry.ReadEntries(aOptions: TReadEntryOptions);
begin
  // Do Nothing
end;

function TFileSystemEntry.HasEntries(aShowHidden: Boolean; aTypes: TEntryTypes): Boolean;
begin
  Result:=False;
end;

{ TSymlinkEntry }

constructor TSymlinkEntry.Create(aParent: TFileSystemEntry; const aName, aTarget: string);
begin
  Inherited Create(aParent,aName);
  FTarget:=aTarget;
end;

class function TSymlinkEntry.EntryType: TEntryType;
begin
  Result:=etSymlink;
end;

{ TDirectoryEntry }

function TDirectoryEntry.GetChildCount: Integer;
begin
  Result:=FEntries.Count;
end;

function TDirectoryEntry.GetEntry(index: integer): TFileSystemEntry;
begin
  Result:=TFileSystemEntry(FEntries[Index]);
end;

constructor TDirectoryEntry.Create(aParent: TFileSystemEntry; const aName: string);
begin
  inherited Create(aParent, aName);
  FEntries:=TFPObjectList.Create(True);
end;

destructor TDirectoryEntry.Destroy;
begin
  FreeAndNil(FEntries);
  inherited Destroy;
end;

procedure TDirectoryEntry.Clear;
begin
  FEntries.Clear;
end;

procedure TDirectoryEntry.ReadEntries(aOptions: TReadEntryOptions);

var
  Info: TSearchRec;
  Entry : TFileSystemEntry;
  CurrentDir: string;
  LinkTarget : RawByteString;
  isHidden : Boolean;
  isType : TEntryType;

begin
  Clear;
  CurrentDir:=IncludeTrailingPathDelimiter(AbsolutePath);
  CurrentDir:=CurrentDir;
  if SysUtils.FindFirst(CurrentDir+AllFilesMask,faAnyFile or faSymLink, Info) <> 0 then
     Exit;
  Try
      repeat
        With Info do
          begin
          if Name = '' then
            Continue;
          // check if special dir
          if ((Name = '.') or (Name = '..')) then
            Continue;
          isHidden:=((faHidden and Attr)<>0);
          if isHidden and Not (reoHidden in aOptions) then
            Continue;

          if ((faDirectory and Attr) <> 0) then
            isType:=etDirectory
          else if ((faSymLink and Attr) <> 0) then
            isType:=etSymlink
          else
            isType:=etFile;
          case IsType of
            etFile : Entry:=TFileEntry.Create(Self,Name);
            etDirectory : Entry:=TDirectoryEntry.Create(Self,Name);
            etSymlink :
              begin
              try
              if not FileGetSymLinkTarget(CurrentDir+Name,LinkTarget) then
                LinkTarget:='<?>';
              except
                // We get an exception in 3.2.2
                LinkTarget:='<?>';
              end;
              Entry:=TSymLinkEntry.Create(Self,Name,LinkTarget);
              end;
          else
            Entry:=Nil;
          end;
          if Assigned(Entry) then
            AddEntry(Entry);
          if reoRecurse in aOptions then
            Entry.ReadEntries(aOptions);
          // We found at least one entry, so exit.
          end;
        until SysUtils.FindNext(Info) <> 0;
    finally
      SysUtils.FindClose(Info);
    end;
end;

class function TDirectoryEntry.EntryType: TEntryType;
begin
  Result:=etDirectory;
end;

class function TDirectoryEntry.HasEntries(aPath: String; aShowHidden : Boolean; aTypes: TEntryTypes): Boolean;

var
  Info: TSearchRec;
  CurrentDir: string;
  isHidden,isDir,isLink : Boolean;

begin
  Result := False;
  if aPath = '' then
    Exit;
  CurrentDir:=IncludeTrailingPathDelimiter(aPath);
  CurrentDir:=CurrentDir+AllFilesMask;
  if SysUtils.FindFirst(CurrentDir,faAnyFile or faSymLink, Info) <> 0 then
    Exit;
  Try
    repeat
      With Info do
        begin
        if Name = '' then
          Continue;
        // check if special dir
        if ((Name = '.') or (Name = '..')) then
          Continue;
        isHidden:=((faHidden and Attr)<>0) or (Name[1]='.');
        if isHidden and Not aShowHidden then
          Continue;
        isDir:=((faDirectory and Attr) <> 0);
        isLink:=((faSymLink and Attr) <> 0);

        Result:=(etFile in aTypes) and Not (isDir or IsLink);
        Result := Result or (IsDir and (etDirectory in aTypes));
        Result := Result or (isLink and (etSymlink in aTypes));

        // We found at least one entry, so exit.
        if Result then
          Exit;
        end;
      until SysUtils.FindNext(Info) <> 0;
  finally
    SysUtils.FindClose(Info);
  end;
end;

function TDirectoryEntry.HasEntries(aShowHidden: Boolean; aTypes: TEntryTypes): Boolean;

var
  I : Integer;

begin
  Result:=False;
  if (aTypes = AllEntryTypes) then
    Result:=FEntries.Count>0;
  if Not Result then
    if FEntries.Count=0 then
      Result:=HasEntries(AbsolutePath,aShowHidden,aTypes)
    else
      For I:=0 to EntryCount-1 do
        if Entries[i].EntryType in aTypes then
          exit(True);
end;

procedure TDirectoryEntry.AddEntry(aEntry: TFileSystemEntry);
begin
  if (aEntry=Nil) then
    exit;
  FEntries.Add(aEntry);
end;

{ TFileEntry }

class function TFileEntry.EntryType: TEntryType;
begin
  Result:=etFile;
end;

{ TTreeCreatorThread }

constructor TTreeCreatorThread.Create(aRootDir: String;
  aOptions: TReadEntryOptions; aOnDone: TTreeDoneEvent;
  aOnError: TTreeErrorEvent);
begin
  FRootDir:=aRootDir;
  FOptions:=aOptions;
  FOnDone:=aOnDone;
  FOnError:=aOnError;
  Inherited Create(false);
end;

procedure TTreeCreatorThread.FillNode(N : TDirectoryEntry);

var
  i : integer;

begin
  N.ReadEntries(FOptions);
  For I:=0 to N.EntryCount-1 do
    begin
    if terminated then
      break;
    if N.Entries[I].EntryType=etDirectory then
      FillNode(TDirectoryEntry(N.Entries[I]));
    end;
end;

procedure TTreeCreatorThread.DoDone;
begin
  FOnDone(Self,FNode);
  // Caller is responsible for freeing now...
  FNode:=Nil;
end;

procedure TTreeCreatorThread.DoError;
begin
  if assigned(FonError) then
    FOnError(Self,FError);
end;

procedure TTreeCreatorThread.execute;

begin

  FNode:=TDirectoryEntry.Create(Nil,FRootDir);
  try
    Try
      FillNode(FNode);
    except
      on E : Exception do
        begin
        FError:=Format('Error indexing %s : %s',[E.ClassName,E.Message]);
        if Assigned(FOnError) then
          Synchronize(@DoError);
        Terminate;
        end;
    end;
    if Not Terminated then
      begin
      if Assigned(FOnDOne) then
        Synchronize(@DoDone);
      end;
  finally
    FNode.Free;
  end;
end;

end.