Debian Codegraph @

plugins/dsx/DSXLocate/src/DSXLocate.lpr

raw
1 {
2 DSXLocate
3 -------------------------------------------------------------------------
4 This is DSX (Search) plugin for Double Commander.
5 Plugin use locate and it's database for searching.
6
7 Copyright (C) 2008 Dmitry Kolomiets ([email protected])
8 The original class of TExProcess used in plugin was written by Anton Rjeshevsky.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 }
24
25
26 library DSXLocate;
27
28 {$mode objfpc}{$H+}
29 {$include calling.inc}
30
31 uses
32 Classes, SysUtils, DsxPlugin, un_process;
33
34 var
35 List: TStringList;
36 LocatePath: String;
37
38 type
39
40 { TPlugInfo }
41
42 TPlugInfo = class
43 private
44 FProcess: TExProcess;
45 FAddProc: TSAddFileProc;
46 FUpdateProc: TSUpdateStatusProc;
47 FSearchRec: TDsxSearchRecord;
48 FilesScanned: Integer;
49 public
50 PluginNr: Integer;
51 //---------------------
52 constructor Create(Nr: Integer);
53 procedure SetProcs(AddProc: TSAddFileProc; UpdateProc: TSUpdateStatusProc);
54 procedure SetDefs(pSearchRec: PDsxSearchRecord);
55 destructor Destroy; override;
56 //---------------------
57 procedure Start;
58 procedure Stop;
59 procedure OnReadLn(str: String);
60 end;
61
62 constructor TPlugInfo.Create(Nr: Integer);
63 begin
64 PluginNr := Nr;
65 FProcess := nil;
66 end;
67
68 procedure TPlugInfo.SetProcs(AddProc: TSAddFileProc; UpdateProc: TSUpdateStatusProc);
69 begin
70 FAddProc := AddProc;
71 FUpdateProc := UpdateProc;
72 end;
73
74 procedure TPlugInfo.SetDefs(pSearchRec: PDsxSearchRecord);
75 begin
76 FSearchRec := pSearchRec^;
77 end;
78
79 destructor TPlugInfo.Destroy;
80 begin
81 if Assigned(FProcess) then
82 FreeAndNil(FProcess);
83 inherited Destroy;
84 end;
85
86 procedure TPlugInfo.Start;
87 var
88 sSearch: String;
89 begin
90 FilesScanned := 0;
91 if Assigned(FProcess) then
92 FreeAndNil(FProcess);
93 FProcess := TExProcess.Create;
94 FProcess.OnReadLn := @OnReadLn;
95
96 with FSearchRec do
97 begin
98 // TProcess doesn't support passing parameters other than quoted in "".
99 // Adapt this code when this changes.
100 sSearch := String(StartPath);
101 if sSearch <> '' then
102 begin
103 // Search in given start path and in subdirectories.
104 sSearch := '"' + IncludeTrailingPathDelimiter(sSearch) + String(FileMask) + '" ' +
105 '"' + IncludeTrailingPathDelimiter(sSearch) + '*' + PathDelim + String(FileMask) + '"';
106 end
107 else
108 sSearch := '"' + String(FileMask) + '"';
109 end;
110
111 if LocatePath <> '' then
112 FProcess.SetCmdLine(LocatePath + ' ' + sSearch);
113 FProcess.Execute;
114 end;
115
116 procedure TPlugInfo.Stop;
117 begin
118 if Assigned(FProcess) then
119 begin
120 FProcess.Stop;
121 FreeAndNil(FProcess);
122 end;
123 end;
124
125 procedure TPlugInfo.OnReadLn(str: String);
126 begin
127 if str <> '' then
128 Inc(FilesScanned);
129 FAddProc(PluginNr, PChar(str));
130 FUpdateProc(PluginNr, PChar(str), FilesScanned);
131 end;
132
133
134 {Main --------------------------------------------------------------------------------}
135
136 function Init(dps: PDsxDefaultParamStruct; pAddFileProc: TSAddFileProc;
137 pUpdateStatus: TSUpdateStatusProc): Integer; dcpcall;
138 var
139 i: Integer;
140 begin
141 if not assigned(List) then
142 List := TStringList.Create;
143 I := List.Count;
144 List.AddObject(IntToStr(I), TPlugInfo.Create(I));
145 TPlugInfo(List.Objects[I]).SetProcs(pAddFileProc, pUpdateStatus);
146 Result := I;
147 end;
148
149 procedure StartSearch(FPluginNr: Integer; pSearchRecRec: PDsxSearchRecord); dcpcall;
150 begin
151 TPlugInfo(List.Objects[FPluginNr]).SetDefs(pSearchRecRec);
152 TPlugInfo(List.Objects[FPluginNr]).Start;
153 end;
154
155 procedure StopSearch(FPluginNr: Integer); dcpcall;
156 begin
157 TPlugInfo(List.Objects[FPluginNr]).Stop;
158 end;
159
160 procedure Finalize(FPluginNr: Integer); dcpcall;
161 begin
162 if not Assigned(List) then
163 exit;
164 if (FPluginNr > List.Count) or (FPluginNr < 0) or (List.Count = 0) then
165 exit;
166
167 //Destroy PlugInfo Item №
168 TPlugInfo(List.Objects[FPluginNr]).Free;
169 List.Delete(FPluginNr);
170 if List.Count = 0 then
171 FreeAndNil(List);
172 end;
173
174 exports
175 Init,
176 StartSearch,
177 StopSearch,
178 Finalize;
179
180 type
181 Tx = class
182 procedure OnReadLnWhich(str: String);
183 end;
184
185 procedure Tx.OnReadLnWhich(str: String);
186 begin
187 if str <> '' then
188 begin
189 LocatePath := str;
190 //WriteLn('PLUGIN: locate found in '+str);
191 end;
192 end;
193
194 var
195 Pr: TExProcess;
196 x: TX;
197
198 {$R *.res}
199
200 begin
201 pr := TExProcess.Create('which locate');
202 x := Tx.Create;
203 pr.OnReadLn := @x.OnReadLnWhich;
204 pr.Execute;
205 pr.Free;
206 x.Free;
207 {$IFDEF UNIX}
208 if LocatePath = '' then
209 Writeln('DSXLocate: Locate utility not found.');
210 {$ENDIF}
211 end.
212