| 1 | Unit GlobalSearchForm;
|
|---|
| 2 |
|
|---|
| 3 | // NewView - a new OS/2 Help Viewer
|
|---|
| 4 | // Copyright 2003 Aaron Lawrence (aaronl at consultant dot com)
|
|---|
| 5 | // This software is released under the Gnu Public License - see readme.txt
|
|---|
| 6 |
|
|---|
| 7 | Interface
|
|---|
| 8 |
|
|---|
| 9 | // Global search uses a thread to load helpfiles, search them, and
|
|---|
| 10 | // send the results to be displayed.
|
|---|
| 11 |
|
|---|
| 12 | Uses
|
|---|
| 13 | PmWin,
|
|---|
| 14 | Classes,
|
|---|
| 15 | Forms,
|
|---|
| 16 | Buttons,
|
|---|
| 17 | StdCtrls,
|
|---|
| 18 | ComCtrls,
|
|---|
| 19 | Outline2,
|
|---|
| 20 | GenericThread,
|
|---|
| 21 | ACLLanguageUnit,
|
|---|
| 22 | TextSearchQuery,
|
|---|
| 23 | HelpFile,
|
|---|
| 24 | ExtCtrls,
|
|---|
| 25 | LED;
|
|---|
| 26 |
|
|---|
| 27 | const
|
|---|
| 28 | // Custom window messages for this form
|
|---|
| 29 | // NOTE! Sibyl uses WM_USER+1 and +2!
|
|---|
| 30 | WM_OPENED = WM_USER + 10;
|
|---|
| 31 |
|
|---|
| 32 | Type
|
|---|
| 33 | TViewTopicCallback = procedure( Filename: string;
|
|---|
| 34 | TopicIndex: longint ) of object;
|
|---|
| 35 |
|
|---|
| 36 | TSearchParameters = class
|
|---|
| 37 | Query: TTextSearchQuery;
|
|---|
| 38 | Directories: TStringList;
|
|---|
| 39 | end;
|
|---|
| 40 |
|
|---|
| 41 | // Returned by thread. (Note - helpfiles and topics are destroyed dynamically.)
|
|---|
| 42 | TSearchResult = class
|
|---|
| 43 | Filename: string;
|
|---|
| 44 | FileTitle: string;
|
|---|
| 45 | MatchingTopics: TList;
|
|---|
| 46 | constructor Create;
|
|---|
| 47 | destructor Destroy; override;
|
|---|
| 48 | end;
|
|---|
| 49 |
|
|---|
| 50 | TGlobalSearchForm = Class (TForm)
|
|---|
| 51 | SearchTextEdit: TEdit;
|
|---|
| 52 | SearchTextLabel: TLabel;
|
|---|
| 53 | SelectDrivesButton: TButton;
|
|---|
| 54 | SearchTextLabel1: TLabel;
|
|---|
| 55 | ResultsLabel: TLabel;
|
|---|
| 56 | Led: TLed;
|
|---|
| 57 | LEDTimer: TTimer;
|
|---|
| 58 | SearchLocationComboBox: TComboBox;
|
|---|
| 59 | Bevel: TBevel;
|
|---|
| 60 | HelpButton: TButton;
|
|---|
| 61 | ResultsOutline: TOutline2;
|
|---|
| 62 | SearchButton: TButton;
|
|---|
| 63 | ViewTopicButton: TButton;
|
|---|
| 64 | CancelButton: TButton;
|
|---|
| 65 | ProgressBar: TProgressBar;
|
|---|
| 66 | ProgressLabel: TLabel;
|
|---|
| 67 | Procedure SearchLocationComboBoxOnItemSelect (Sender: TObject;
|
|---|
| 68 | Index: LongInt);
|
|---|
| 69 | Procedure LEDTimerOnTimer (Sender: TObject);
|
|---|
| 70 | Procedure SearchInComboBoxOnChange (Sender: TObject);
|
|---|
| 71 | Procedure ResultsOutlineOnEnter (Sender: TObject);
|
|---|
| 72 | Procedure SearchTextEditOnEnter (Sender: TObject);
|
|---|
| 73 | Procedure SelectDrivesButtonOnClick (Sender: TObject);
|
|---|
| 74 | Procedure GlobalSearchFormOnSetupShow (Sender: TObject);
|
|---|
| 75 | Procedure CancelButtonOnClick (Sender: TObject);
|
|---|
| 76 | Procedure GlobalSearchFormOnClose (Sender: TObject;
|
|---|
| 77 | Var Action: TCloseAction);
|
|---|
| 78 | Procedure ResultsOutlineOnItemDblClick (Node: TNode);
|
|---|
| 79 | Procedure ViewTopicButtonOnClick (Sender: TObject);
|
|---|
| 80 | Procedure GlobalSearchFormOnCreate (Sender: TObject);
|
|---|
| 81 | Procedure GlobalSearchFormOnCloseQuery (Sender: TObject;
|
|---|
| 82 | Var CanClose: Boolean);
|
|---|
| 83 | Procedure SearchButtonOnClick (Sender: TObject);
|
|---|
| 84 | Procedure GlobalSearchFormOnShow (Sender: TObject);
|
|---|
| 85 | Protected
|
|---|
| 86 | Procedure OnSearchProgress ( n, outof: integer;
|
|---|
| 87 | S: String );
|
|---|
| 88 | Procedure OnMatchFound( SearchResult: TSearchResult );
|
|---|
| 89 | Procedure OnThreadData( S: string; Data: TObject );
|
|---|
| 90 | Procedure OnSearchFinished( Result: TObject );
|
|---|
| 91 | Procedure SetProgressLabel( const S: String );
|
|---|
| 92 |
|
|---|
| 93 | // Handle our own WM_OPENED message
|
|---|
| 94 | Procedure WMOpened( Var Msg: TMessage ); Message WM_OPENED;
|
|---|
| 95 | Procedure ClearResults;
|
|---|
| 96 | Procedure ViewTopic;
|
|---|
| 97 |
|
|---|
| 98 | ThreadManager: TGenericThreadManager;
|
|---|
| 99 |
|
|---|
| 100 | Procedure UpdateButtons;
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | Procedure GetSelectedDirectories( List: TStringList );
|
|---|
| 104 |
|
|---|
| 105 | // search thread context
|
|---|
| 106 |
|
|---|
| 107 | Function Search( Parameters: TObject ): TObject;
|
|---|
| 108 |
|
|---|
| 109 | protected
|
|---|
| 110 | Procedure OnLanguageEvent( Language: TLanguageFile; const Apply: boolean );
|
|---|
| 111 |
|
|---|
| 112 | SearchCaption: string;
|
|---|
| 113 | StopCaption: string;
|
|---|
| 114 | NoResultsMsg: string;
|
|---|
| 115 | ScanDirectoriesMsg: string;
|
|---|
| 116 | SearchingFileMsg: string;
|
|---|
| 117 | OfMsg: string;
|
|---|
| 118 | DoneMsg: string;
|
|---|
| 119 | SearchErrorTitle: string;
|
|---|
| 120 | SearchError: string;
|
|---|
| 121 |
|
|---|
| 122 | StandardHelpPathsLocation: string;
|
|---|
| 123 | FixedDrivesLocation: string;
|
|---|
| 124 | SelectedHelpPathsLocation: string;
|
|---|
| 125 | CustomPathsLocation: string;
|
|---|
| 126 |
|
|---|
| 127 | public
|
|---|
| 128 | // set by caller
|
|---|
| 129 | ViewTopicCallback: TViewTopicCallback;
|
|---|
| 130 | Procedure DoSearch;
|
|---|
| 131 | End;
|
|---|
| 132 |
|
|---|
| 133 | Var
|
|---|
| 134 | GlobalSearchForm: TGlobalSearchForm;
|
|---|
| 135 |
|
|---|
| 136 | procedure EnsureGlobalSearchFormLoaded;
|
|---|
| 137 |
|
|---|
| 138 | Implementation
|
|---|
| 139 |
|
|---|
| 140 | uses
|
|---|
| 141 | SysUtils,
|
|---|
| 142 | DebugUnit,
|
|---|
| 143 | ACLDialogs,
|
|---|
| 144 | ControlsUtility,
|
|---|
| 145 | DriveInfoUnit,
|
|---|
| 146 | IPFFileFormatUnit,
|
|---|
| 147 | HelpTopic,
|
|---|
| 148 | SearchUnit,
|
|---|
| 149 | SearchDirectoriesFormUnit,
|
|---|
| 150 | SettingsUnit,
|
|---|
| 151 | InformationFormUnit,
|
|---|
| 152 | StringUtilsUnit,
|
|---|
| 153 | FileUtilsUnit;
|
|---|
| 154 |
|
|---|
| 155 | type
|
|---|
| 156 | // Used to store filenames in outline
|
|---|
| 157 | THelpFileInfo = class
|
|---|
| 158 | FileName: string;
|
|---|
| 159 | end;
|
|---|
| 160 |
|
|---|
| 161 | constructor TSearchResult.Create;
|
|---|
| 162 | begin
|
|---|
| 163 | inherited Create;
|
|---|
| 164 | MatchingTopics := TList.Create;
|
|---|
| 165 | end;
|
|---|
| 166 |
|
|---|
| 167 | destructor TSearchResult.Destroy;
|
|---|
| 168 | begin
|
|---|
| 169 | MatchingTopics.Destroy;
|
|---|
| 170 | inherited Destroy;
|
|---|
| 171 | end;
|
|---|
| 172 |
|
|---|
| 173 | Procedure TGlobalSearchForm.SearchLocationComboBoxOnItemSelect (Sender: TObject; Index: LongInt);
|
|---|
| 174 | Begin
|
|---|
| 175 | Settings.GlobalSearchLocation := TGlobalSearchLocation( SearchLocationComboBox.ItemIndex );
|
|---|
| 176 | End;
|
|---|
| 177 |
|
|---|
| 178 | Procedure TGlobalSearchForm.LEDTimerOnTimer (Sender: TObject);
|
|---|
| 179 | Begin
|
|---|
| 180 | LED.LedCondition := not LED.LedCondition;
|
|---|
| 181 | End;
|
|---|
| 182 |
|
|---|
| 183 | Procedure TGlobalSearchForm.SearchInComboBoxOnChange (Sender: TObject);
|
|---|
| 184 | Begin
|
|---|
| 185 | End;
|
|---|
| 186 |
|
|---|
| 187 | Procedure TGlobalSearchForm.ResultsOutlineOnEnter (Sender: TObject);
|
|---|
| 188 | Begin
|
|---|
| 189 | ViewTopicButton.Default := true;
|
|---|
| 190 | End;
|
|---|
| 191 |
|
|---|
| 192 | Procedure TGlobalSearchForm.SearchTextEditOnEnter (Sender: TObject);
|
|---|
| 193 | Begin
|
|---|
| 194 | SearchButton.Default := true;
|
|---|
| 195 | End;
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 | Procedure TGlobalSearchForm.GetSelectedDirectories(List: TStringList);
|
|---|
| 199 | Var
|
|---|
| 200 | tmpDirectories : TStringList;
|
|---|
| 201 |
|
|---|
| 202 | DriveNumber: longint;
|
|---|
| 203 | DriveType: TDriveType;
|
|---|
| 204 | DriveLetter: char;
|
|---|
| 205 | i: longint;
|
|---|
| 206 | Dir: string;
|
|---|
| 207 | FoundIndex: longint;
|
|---|
| 208 | begin
|
|---|
| 209 | List.Clear;
|
|---|
| 210 | case SearchLocationComboBox.ItemIndex of
|
|---|
| 211 | -1:
|
|---|
| 212 | begin
|
|---|
| 213 | // one custom dir...
|
|---|
| 214 | List.Add( SearchLocationComboBox.Text );
|
|---|
| 215 | end;
|
|---|
| 216 |
|
|---|
| 217 | Ord( gsHelpPaths ),
|
|---|
| 218 | Ord( gsSelectedHelpPaths ): // standard or selected help paths
|
|---|
| 219 | Begin
|
|---|
| 220 | tmpDirectories := TStringList.Create;
|
|---|
| 221 | List.Sorted := true;
|
|---|
| 222 | List.Duplicates := dupIgnore;
|
|---|
| 223 |
|
|---|
| 224 | // GetDirsInPath clears the list tmpDirectories
|
|---|
| 225 | GetDirsInPath(HelpPathEnvironmentVar, tmpDirectories);
|
|---|
| 226 | List.AddStrings(tmpDirectories);
|
|---|
| 227 |
|
|---|
| 228 | GetDirsInPath(BookshelfEnvironmentVar, tmpDirectories);
|
|---|
| 229 | List.AddStrings(tmpDirectories);
|
|---|
| 230 |
|
|---|
| 231 | tmpDirectories.Destroy;
|
|---|
| 232 |
|
|---|
| 233 | // for selected paths we have to adjust the selection
|
|---|
| 234 | if SearchLocationComboBox.ItemIndex = Ord( gsSelectedHelpPaths ) then
|
|---|
| 235 | begin
|
|---|
| 236 | // now mark some as non-selected...
|
|---|
| 237 | for i := 0 to List.Count - 1 do
|
|---|
| 238 | begin
|
|---|
| 239 | Dir := List[i];
|
|---|
| 240 | if not Settings.SearchDirectories.Find( Dir, FoundIndex ) then
|
|---|
| 241 | List.Objects[i] := pointer(1);
|
|---|
| 242 | end;
|
|---|
| 243 | end;
|
|---|
| 244 | end;
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 | Ord( gsFixedDrives ):
|
|---|
| 248 | begin
|
|---|
| 249 | // drives
|
|---|
| 250 | For DriveNumber := MinDriveNumber To MaxDriveNumber Do
|
|---|
| 251 | Begin
|
|---|
| 252 | DriveType := GetDriveType( DriveNumber );
|
|---|
| 253 | DriveLetter := Chr( DriveNumber + Ord( 'A' ) - 1 );
|
|---|
| 254 |
|
|---|
| 255 | if DriveType = dtHard then
|
|---|
| 256 | begin
|
|---|
| 257 | List.Add( DriveLetter + ':\...' );
|
|---|
| 258 | end;
|
|---|
| 259 | end;
|
|---|
| 260 | end;
|
|---|
| 261 |
|
|---|
| 262 | Ord( gsCustom ):
|
|---|
| 263 | begin
|
|---|
| 264 | // already custom...
|
|---|
| 265 | List.Assign(Settings.SearchDirectories);
|
|---|
| 266 | end;
|
|---|
| 267 | end;
|
|---|
| 268 | end;
|
|---|
| 269 |
|
|---|
| 270 | Procedure TGlobalSearchForm.SelectDrivesButtonOnClick (Sender: TObject);
|
|---|
| 271 | begin
|
|---|
| 272 | GetSelectedDirectories(SearchDirectoriesForm.SelectedFolders);
|
|---|
| 273 |
|
|---|
| 274 | SearchDirectoriesForm.ShowModal;
|
|---|
| 275 | if SearchDirectoriesForm.ModalResult <> mrOK then
|
|---|
| 276 | exit;
|
|---|
| 277 |
|
|---|
| 278 | // there was a selection, so we have to change the dropdown
|
|---|
| 279 | if SearchLocationComboBox.ItemIndex = Ord( gsHelpPaths ) then
|
|---|
| 280 | SearchLocationComboBox.ItemIndex := Ord( gsSelectedHelpPaths );
|
|---|
| 281 |
|
|---|
| 282 | if SearchLocationComboBox.ItemIndex = Ord( gsFixedDrives ) then
|
|---|
| 283 | SearchLocationComboBox.ItemIndex := Ord( gsCustom );
|
|---|
| 284 |
|
|---|
| 285 | if SearchDirectoriesForm.CustomDirAdded then
|
|---|
| 286 | SearchLocationComboBox.ItemIndex := Ord( gsCustom );
|
|---|
| 287 |
|
|---|
| 288 | // update the settings
|
|---|
| 289 | Settings.SearchDirectories.Assign( SearchDirectoriesForm.SelectedFolders );
|
|---|
| 290 | SaveSettings;
|
|---|
| 291 | End;
|
|---|
| 292 |
|
|---|
| 293 | Procedure TGlobalSearchForm.UpdateButtons;
|
|---|
| 294 | Begin
|
|---|
| 295 | // HelpPathsRadioButton.Checked := SearchType = stHelpPaths;
|
|---|
| 296 | // EverywhereRadioButton.Checked := SearchType = stDrives;
|
|---|
| 297 | // FolderRadioButton.Checked := SearchType = stFolder;
|
|---|
| 298 |
|
|---|
| 299 | End;
|
|---|
| 300 |
|
|---|
| 301 | Procedure TGlobalSearchForm.GlobalSearchFormOnSetupShow (Sender: TObject);
|
|---|
| 302 | Begin
|
|---|
| 303 | ScaleForm( self, 11, 16 );
|
|---|
| 304 | SearchButton.Align := alFixedRightTop;
|
|---|
| 305 |
|
|---|
| 306 | if SearchLocationComboBox.Items.Count = 0 then
|
|---|
| 307 | begin
|
|---|
| 308 | // must match teh global search location enum...
|
|---|
| 309 | SearchLocationComboBox.Items.Add( StandardHelpPathsLocation );
|
|---|
| 310 | SearchLocationComboBox.Items.Add( FixedDrivesLocation );
|
|---|
| 311 | SearchLocationComboBox.Items.Add( SelectedHelpPathsLocation );
|
|---|
| 312 | SearchLocationComboBox.Items.Add( CustomPathsLocation );
|
|---|
| 313 |
|
|---|
| 314 | SearchLocationComboBox.ItemIndex := Ord( Settings.GlobalSearchLocation );
|
|---|
| 315 | end;
|
|---|
| 316 |
|
|---|
| 317 | End;
|
|---|
| 318 |
|
|---|
| 319 | Procedure TGlobalSearchForm.OnLanguageEvent(Language: TLanguageFile; const Apply: boolean );
|
|---|
| 320 | begin
|
|---|
| 321 | LogEvent(LogI18n, 'TGlobalSearchForm.OnLanguageEvent apply: "' + BoolToStr(Apply) + '"');
|
|---|
| 322 |
|
|---|
| 323 | Language.LoadComponentLanguage(self, Apply);
|
|---|
| 324 |
|
|---|
| 325 | Language.LL(Apply, SearchCaption, 'SearchCaption', '~Search');
|
|---|
| 326 | Language.LL(Apply, StopCaption, 'StopCaption', '~Stop');
|
|---|
| 327 | Language.LL(Apply, NoResultsMsg, 'NoResultsMsg', '(No results found)');
|
|---|
| 328 | Language.LL(Apply, ScanDirectoriesMsg, 'ScanDirectoriesMsg', 'Finding help files...');
|
|---|
| 329 | Language.LL(Apply, SearchingFileMsg, 'SearchingFileMsg', 'Searching ');
|
|---|
| 330 | Language.LL(Apply, OfMsg, 'OfMsg', ' of ');
|
|---|
| 331 | Language.LL(Apply, DoneMsg, 'DoneMsg', 'Done');
|
|---|
| 332 | Language.LL(Apply, SearchErrorTitle, 'SearchErrorTitle', 'Search');
|
|---|
| 333 | Language.LL(Apply, SearchError, 'SearchError', 'Error in search syntax: ');
|
|---|
| 334 |
|
|---|
| 335 | Language.LL(Apply, StandardHelpPathsLocation, 'StandardHelpPathsLocation', 'Standard Help Paths');
|
|---|
| 336 | Language.LL(Apply, FixedDrivesLocation, 'FixedDrivesLocation', 'All Hard Drives');
|
|---|
| 337 | Language.LL(Apply, SelectedHelpPathsLocation, 'SelectedHelpPathsLocation', 'Selected Help Paths');
|
|---|
| 338 | Language.LL(Apply, CustomPathsLocation, 'CustomPathsLocation', 'Directory List');
|
|---|
| 339 | end;
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 | Procedure TGlobalSearchForm.CancelButtonOnClick (Sender: TObject);
|
|---|
| 343 | Begin
|
|---|
| 344 | Close;
|
|---|
| 345 | End;
|
|---|
| 346 |
|
|---|
| 347 | Procedure TGlobalSearchForm.GlobalSearchFormOnClose (Sender: TObject;
|
|---|
| 348 | Var Action: TCloseAction);
|
|---|
| 349 | Begin
|
|---|
| 350 | ClearResults;
|
|---|
| 351 |
|
|---|
| 352 | if SearchLocationComboBox.ItemIndex = -1 then
|
|---|
| 353 | begin
|
|---|
| 354 | Settings.GlobalSearchLocation := gsCustom;
|
|---|
| 355 | Settings.SearchDirectories.Clear;
|
|---|
| 356 | Settings.SearchDirectories.Add( SearchLocationComboBox.Text );
|
|---|
| 357 | end;
|
|---|
| 358 |
|
|---|
| 359 | Action := caFreeHandle; // DON'T release the form! (Default action for non-modal forms)
|
|---|
| 360 | End;
|
|---|
| 361 |
|
|---|
| 362 | Procedure TGlobalSearchForm.ResultsOutlineOnItemDblClick (Node: TNode);
|
|---|
| 363 | Begin
|
|---|
| 364 | ViewTopic;
|
|---|
| 365 | End;
|
|---|
| 366 |
|
|---|
| 367 | Procedure TGlobalSearchForm.ViewTopicButtonOnClick (Sender: TObject);
|
|---|
| 368 | begin
|
|---|
| 369 | ViewTopic;
|
|---|
| 370 | end;
|
|---|
| 371 |
|
|---|
| 372 | Procedure TGlobalSearchForm.ViewTopic;
|
|---|
| 373 | var
|
|---|
| 374 | Node: TNode;
|
|---|
| 375 | HelpFileInfo: THelpFileInfo;
|
|---|
| 376 | TopicIndex: longint;
|
|---|
| 377 | Begin
|
|---|
| 378 | Node := ResultsOutline.SelectedNode;
|
|---|
| 379 | if Node = nil then
|
|---|
| 380 | exit;
|
|---|
| 381 | case Node.Level of
|
|---|
| 382 | 0:
|
|---|
| 383 | begin
|
|---|
| 384 | // file node
|
|---|
| 385 | HelpFileInfo := Node.Data as THelpFileInfo;
|
|---|
| 386 | TopicIndex := -1;
|
|---|
| 387 | end;
|
|---|
| 388 |
|
|---|
| 389 | 1:
|
|---|
| 390 | begin
|
|---|
| 391 | // topic node
|
|---|
| 392 | HelpFileInfo := Node.Parent.Data as THelpFileInfo;
|
|---|
| 393 | TopicIndex := longint( Node.Data );
|
|---|
| 394 | end;
|
|---|
| 395 |
|
|---|
| 396 | else
|
|---|
| 397 | assert( false, 'Invalid node level in ViewTopic!: ' + IntToStr( Node.Level ) );
|
|---|
| 398 | end;
|
|---|
| 399 |
|
|---|
| 400 | ViewTopicCallback( HelpFileInfo.FileName, TopicIndex );
|
|---|
| 401 | End;
|
|---|
| 402 |
|
|---|
| 403 | Procedure TGlobalSearchForm.GlobalSearchFormOnCreate (Sender: TObject);
|
|---|
| 404 | Begin
|
|---|
| 405 | RegisterForLanguages( OnLanguageEvent );
|
|---|
| 406 |
|
|---|
| 407 | UpdateButtons;
|
|---|
| 408 |
|
|---|
| 409 | ThreadManager := TGenericThreadManager.Create( self );
|
|---|
| 410 | ThreadManager.OnProgressUpdate := OnSearchProgress;
|
|---|
| 411 | ThreadManager.OnDataFromThread := OnThreadData;
|
|---|
| 412 | ThreadManager.OnJobComplete := OnSearchFinished;
|
|---|
| 413 | End;
|
|---|
| 414 |
|
|---|
| 415 | Procedure TGlobalSearchForm.OnSearchFinished( Result: TObject );
|
|---|
| 416 | Begin
|
|---|
| 417 | SearchButton.Caption := SearchCaption;
|
|---|
| 418 | ProgressBar.Hide;
|
|---|
| 419 | LED.LedCondition := false;
|
|---|
| 420 | LEDTimer.Stop;
|
|---|
| 421 |
|
|---|
| 422 | if ResultsOutline.ChildCount > 0 then
|
|---|
| 423 | begin
|
|---|
| 424 | ResultsOutline.SelectedNode:= ResultsOutline.Children[ 0 ];
|
|---|
| 425 | ResultsOutline.Focus;
|
|---|
| 426 | SetProgressLabel( '' );
|
|---|
| 427 | end
|
|---|
| 428 | else
|
|---|
| 429 | begin
|
|---|
| 430 | SetProgressLabel( NoResultsMsg );
|
|---|
| 431 | end;
|
|---|
| 432 |
|
|---|
| 433 | End;
|
|---|
| 434 |
|
|---|
| 435 | Procedure TGlobalSearchForm.GlobalSearchFormOnCloseQuery (Sender: TObject;
|
|---|
| 436 | Var CanClose: Boolean);
|
|---|
| 437 | Begin
|
|---|
| 438 | if ThreadManager.IsRunning then
|
|---|
| 439 | begin
|
|---|
| 440 | ThreadManager.Stop;
|
|---|
| 441 | end;
|
|---|
| 442 | End;
|
|---|
| 443 |
|
|---|
| 444 | Procedure TGlobalSearchForm.SetProgressLabel( const S: String );
|
|---|
| 445 | begin
|
|---|
| 446 | ProgressLabel.Text := S;
|
|---|
| 447 | ProgressLabel.Refresh;
|
|---|
| 448 | end;
|
|---|
| 449 |
|
|---|
| 450 | Procedure TGlobalSearchForm.OnSearchProgress ( n, outof: integer;
|
|---|
| 451 | S: String );
|
|---|
| 452 | Begin
|
|---|
| 453 | ProgressBar.Position := n * 100 div outof;
|
|---|
| 454 | SetProgressLabel( S );
|
|---|
| 455 | End;
|
|---|
| 456 |
|
|---|
| 457 | Function TGlobalSearchForm.Search( Parameters: TObject ): TObject;
|
|---|
| 458 | var
|
|---|
| 459 | Files : TStringList;
|
|---|
| 460 | FileIndex: longint;
|
|---|
| 461 | Filename: string;
|
|---|
| 462 | HelpFile: THelpFile;
|
|---|
| 463 | SearchResult: TSearchResult;
|
|---|
| 464 | MatchingTopics: TList;
|
|---|
| 465 |
|
|---|
| 466 | tmpSearchParameters : TSearchParameters;
|
|---|
| 467 | Query: TTextSearchQuery;
|
|---|
| 468 | i: longint;
|
|---|
| 469 | tmpDir: string;
|
|---|
| 470 |
|
|---|
| 471 | Begin
|
|---|
| 472 | // LogEvent(LogDebug, 'TGlobalSearchForm.Search');
|
|---|
| 473 |
|
|---|
| 474 | tmpSearchParameters := Parameters as TSearchParameters;
|
|---|
| 475 |
|
|---|
| 476 | Query := tmpSearchParameters.Query;
|
|---|
| 477 | Files := TStringList.Create;
|
|---|
| 478 |
|
|---|
| 479 | MatchingTopics := TList.Create;
|
|---|
| 480 | LogEvent(LogParse, 'Creating file list');
|
|---|
| 481 |
|
|---|
| 482 | // make sure we ignore duplicate files...
|
|---|
| 483 | Files.Sorted := true;
|
|---|
| 484 | Files.CaseSensitive := false;
|
|---|
| 485 | Files.Duplicates := dupIgnore;
|
|---|
| 486 |
|
|---|
| 487 | for i := 0 to tmpSearchParameters.Directories.Count - 1 do
|
|---|
| 488 | begin
|
|---|
| 489 | if ThreadManager.StopRequested then
|
|---|
| 490 | begin
|
|---|
| 491 | break;
|
|---|
| 492 | end;
|
|---|
| 493 |
|
|---|
| 494 | ThreadManager.UpdateProgress( i * 10 div tmpSearchParameters.Directories.Count, 100, ScanDirectoriesMsg );
|
|---|
| 495 | tmpDir := tmpSearchParameters.Directories[i];
|
|---|
| 496 | // LogEvent(LogDebug, 'TGlobalSearchForm.Search in dir: ' + tmpDir);
|
|---|
| 497 | if StrEndsWith(tmpDir, '...') then
|
|---|
| 498 | begin
|
|---|
| 499 | tmpDir := StrLeftWithout(tmpDir, 3 );
|
|---|
| 500 | ListFilesInDirectoryRecursiveWithTermination(
|
|---|
| 501 | tmpDir,
|
|---|
| 502 | '*.inf;*.hlp',
|
|---|
| 503 | true,
|
|---|
| 504 | false,
|
|---|
| 505 | Files,
|
|---|
| 506 | ThreadManager.StopRequested,
|
|---|
| 507 | true ); // check termination
|
|---|
| 508 | end
|
|---|
| 509 | else
|
|---|
| 510 | begin
|
|---|
| 511 | ListFilesInDirectory( tmpDir, '*.inf;*.hlp', true, Files);
|
|---|
| 512 | end;
|
|---|
| 513 | end;
|
|---|
| 514 |
|
|---|
| 515 | for FileIndex := 0 to Files.Count - 1 do
|
|---|
| 516 | begin
|
|---|
| 517 | if ThreadManager.StopRequested then
|
|---|
| 518 | begin
|
|---|
| 519 | break;
|
|---|
| 520 | end;
|
|---|
| 521 |
|
|---|
| 522 | Filename := Files[FileIndex];
|
|---|
| 523 |
|
|---|
| 524 | ThreadManager.UpdateProgress( 10 + FileIndex * 95 div Files.Count,
|
|---|
| 525 | 100,
|
|---|
| 526 | SearchingFileMsg
|
|---|
| 527 | + Filename
|
|---|
| 528 | + ' ('
|
|---|
| 529 | + IntToStr( FileIndex + 1 )
|
|---|
| 530 | + OfMsg
|
|---|
| 531 | + IntToStr( Files.Count )
|
|---|
| 532 | + ')...' );
|
|---|
| 533 |
|
|---|
| 534 | try
|
|---|
| 535 | HelpFile := THelpFile.Create( FileName );
|
|---|
| 536 | MatchingTopics.Clear;
|
|---|
| 537 | SearchHelpFile( HelpFile,
|
|---|
| 538 | Query,
|
|---|
| 539 | MatchingTopics,
|
|---|
| 540 | nil // don't care about words matched
|
|---|
| 541 | );
|
|---|
| 542 |
|
|---|
| 543 | if MatchingTopics.Count > 0 then
|
|---|
| 544 | begin
|
|---|
| 545 | // Create a searchresult object to send back to main thread.
|
|---|
| 546 | SearchResult := TSearchResult.Create;
|
|---|
| 547 | SearchResult.Filename := HelpFile.Filename;
|
|---|
| 548 | SearchResult.FileTitle := HelpFile.Title;
|
|---|
| 549 |
|
|---|
| 550 | SearchResult.MatchingTopics.Assign( MatchingTopics );
|
|---|
| 551 |
|
|---|
| 552 | SearchResult.MatchingTopics.Sort( TopicRelevanceCompare );
|
|---|
| 553 | ThreadManager.SendData( '', SearchResult );
|
|---|
| 554 | end;
|
|---|
| 555 |
|
|---|
| 556 | HelpFile.Destroy;
|
|---|
| 557 |
|
|---|
| 558 | except
|
|---|
| 559 | on E: EHelpFileException do
|
|---|
| 560 | begin
|
|---|
| 561 | ; // ignore exceptions
|
|---|
| 562 | end;
|
|---|
| 563 |
|
|---|
| 564 | on E: EWindowsHelpFormatException do
|
|---|
| 565 | begin
|
|---|
| 566 | ; // ignore Windows help files
|
|---|
| 567 | end;
|
|---|
| 568 | end;
|
|---|
| 569 |
|
|---|
| 570 | end;
|
|---|
| 571 | ThreadManager.UpdateProgress( 100, 100, DoneMsg );
|
|---|
| 572 | Files.Destroy;
|
|---|
| 573 |
|
|---|
| 574 | Query.Destroy;
|
|---|
| 575 |
|
|---|
| 576 | tmpSearchParameters.Directories.Destroy;
|
|---|
| 577 | tmpSearchParameters.Destroy;
|
|---|
| 578 |
|
|---|
| 579 | Result := nil;
|
|---|
| 580 | End;
|
|---|
| 581 |
|
|---|
| 582 | Procedure TGlobalSearchForm.ClearResults;
|
|---|
| 583 | var
|
|---|
| 584 | FileIndex: longint;
|
|---|
| 585 | begin
|
|---|
| 586 | for FileIndex := 0 to ResultsOutline.ChildCount - 1 do
|
|---|
| 587 | ResultsOutline.Children[ FileIndex ].Data.Free;
|
|---|
| 588 | ResultsOutline.Clear;
|
|---|
| 589 | ViewTopicButton.Enabled := false;
|
|---|
| 590 | end;
|
|---|
| 591 |
|
|---|
| 592 | Procedure TGlobalSearchForm.SearchButtonOnClick (Sender: TObject);
|
|---|
| 593 | begin
|
|---|
| 594 | DoSearch;
|
|---|
| 595 | end;
|
|---|
| 596 |
|
|---|
| 597 | Procedure TGlobalSearchForm.DoSearch;
|
|---|
| 598 | var
|
|---|
| 599 | tmpSelectedDirectories : TStringList;
|
|---|
| 600 | i : integer;
|
|---|
| 601 | tmpSearchText: string;
|
|---|
| 602 |
|
|---|
| 603 | Query: TTextSearchQuery;
|
|---|
| 604 | SearchParameters: TSearchParameters;
|
|---|
| 605 | Begin
|
|---|
| 606 | // LogEvent(LogDebug, 'DoSearch');
|
|---|
| 607 |
|
|---|
| 608 | if ThreadManager.IsRunning then
|
|---|
| 609 | begin
|
|---|
| 610 | ThreadManager.Stop;
|
|---|
| 611 | exit;
|
|---|
| 612 | end;
|
|---|
| 613 |
|
|---|
| 614 | tmpSearchText := trim( SearchTextEdit.Text );
|
|---|
| 615 | if tmpSearchText = '' then
|
|---|
| 616 | exit;
|
|---|
| 617 |
|
|---|
| 618 | // LogEvent(LogDebug, 'DoSearch: ' + tmpSearchText);
|
|---|
| 619 | try
|
|---|
| 620 | Query := TTextSearchQuery.Create(tmpSearchText);
|
|---|
| 621 | except
|
|---|
| 622 | on e: ESearchSyntaxError do
|
|---|
| 623 | begin
|
|---|
| 624 | DoErrorDlg( SearchErrorTitle,
|
|---|
| 625 | SearchError
|
|---|
| 626 | + e.Message );
|
|---|
| 627 | exit;
|
|---|
| 628 | end;
|
|---|
| 629 | end;
|
|---|
| 630 |
|
|---|
| 631 | ClearResults;
|
|---|
| 632 |
|
|---|
| 633 | SearchParameters := TSearchParameters.Create;
|
|---|
| 634 |
|
|---|
| 635 | SearchParameters.Query := Query;
|
|---|
| 636 |
|
|---|
| 637 | // now the directories to search in
|
|---|
| 638 | SearchParameters.Directories := TStringList.Create;
|
|---|
| 639 |
|
|---|
| 640 | tmpSelectedDirectories := TStringList.Create;
|
|---|
| 641 | GetSelectedDirectories(tmpSelectedDirectories);
|
|---|
| 642 |
|
|---|
| 643 | LogEvent(LogDebug, 'DoSearch: tmpSelectedDirectories.Count ' + IntToStr(tmpSelectedDirectories.Count));
|
|---|
| 644 |
|
|---|
| 645 | // clear the list and add only the selected ones
|
|---|
| 646 | for i := 0 to tmpSelectedDirectories.Count - 1 do
|
|---|
| 647 | begin
|
|---|
| 648 | if tmpSelectedDirectories.Objects[i] = nil then
|
|---|
| 649 | begin
|
|---|
| 650 | SearchParameters.Directories.add(tmpSelectedDirectories[i]);
|
|---|
| 651 | end;
|
|---|
| 652 | end;
|
|---|
| 653 | tmpSelectedDirectories.Destroy;
|
|---|
| 654 |
|
|---|
| 655 | ThreadManager.StartJob( Search, SearchParameters );
|
|---|
| 656 | SearchButton.Caption := StopCaption;
|
|---|
| 657 | ProgressBar.Show;
|
|---|
| 658 |
|
|---|
| 659 | LED.LedCondition := true;
|
|---|
| 660 | LEDTimer.Start;
|
|---|
| 661 | End;
|
|---|
| 662 |
|
|---|
| 663 | Procedure TGlobalSearchForm.GlobalSearchFormOnShow (Sender: TObject);
|
|---|
| 664 | Begin
|
|---|
| 665 | // make search button default
|
|---|
| 666 | SearchButton.Focus;
|
|---|
| 667 | SearchTextEdit.Focus;
|
|---|
| 668 | SetProgressLabel( '' );
|
|---|
| 669 | PostMsg( Handle, WM_OPENED, 0, 0 );
|
|---|
| 670 | SearchButton.Caption := SearchCaption;
|
|---|
| 671 | ProgressBar.Hide;
|
|---|
| 672 | ViewTopicButton.Enabled := false;
|
|---|
| 673 | End;
|
|---|
| 674 |
|
|---|
| 675 | Procedure TGlobalSearchForm.OnThreadData( S: string; Data: TObject );
|
|---|
| 676 | var
|
|---|
| 677 | SearchResult: TSearchResult;
|
|---|
| 678 | begin
|
|---|
| 679 | SearchResult := Data as TSearchResult;
|
|---|
| 680 | OnMatchFound( SearchResult );
|
|---|
| 681 | SearchResult.Destroy;
|
|---|
| 682 | end;
|
|---|
| 683 |
|
|---|
| 684 | Procedure TGlobalSearchForm.OnMatchFound( SearchResult: TSearchResult );
|
|---|
| 685 | var
|
|---|
| 686 | Topic: TTopic;
|
|---|
| 687 | HelpFileInfo: THelpFileInfo;
|
|---|
| 688 | FileNode: TNode;
|
|---|
| 689 | TopicIndex: longint;
|
|---|
| 690 | begin
|
|---|
| 691 | ViewTopicButton.Enabled := true;
|
|---|
| 692 |
|
|---|
| 693 | HelpFileInfo := THelpFileInfo.Create;
|
|---|
| 694 | HelpFileInfo.FileName := SearchResult.FileName;
|
|---|
| 695 | FileNode := ResultsOutline.AddChild( SearchResult.FileTitle
|
|---|
| 696 | + ' ('
|
|---|
| 697 | + SearchResult.FileName
|
|---|
| 698 | + ')',
|
|---|
| 699 | HelpFileInfo );
|
|---|
| 700 | for TopicIndex := 0 to SearchResult.MatchingTopics.Count - 1 do
|
|---|
| 701 | begin
|
|---|
| 702 | Topic := SearchResult.MatchingTopics[ TopicIndex ];
|
|---|
| 703 | FileNode.AddChild( Topic.Title,
|
|---|
| 704 | TObject( Topic.Index ) );
|
|---|
| 705 | end;
|
|---|
| 706 | end;
|
|---|
| 707 |
|
|---|
| 708 | Procedure TGlobalSearchForm.WMOpened( Var Msg: TMessage );
|
|---|
| 709 | begin
|
|---|
| 710 | SearchTextLabel.XAlign := xaLeft;
|
|---|
| 711 | SearchTextLabel.YAlign := yaTop;
|
|---|
| 712 |
|
|---|
| 713 | SearchTextEdit.XStretch := xsFrame;
|
|---|
| 714 | SearchTextEdit.YAlign := yaTop;
|
|---|
| 715 | SearchTextEdit.Focus;
|
|---|
| 716 |
|
|---|
| 717 | SearchButton.XAlign := xaRight;
|
|---|
| 718 | SearchButton.YAlign := yaTop;
|
|---|
| 719 |
|
|---|
| 720 | SearchTextLabel1.XAlign := xaLeft;
|
|---|
| 721 | SearchTextLabel1.YAlign := yaTop;
|
|---|
| 722 |
|
|---|
| 723 | SearchLocationComboBox.XStretch := xsFrame;
|
|---|
| 724 | SearchLocationComboBox.YAlign := yaTop;
|
|---|
| 725 |
|
|---|
| 726 | SelectDrivesButton.XAlign := xaRight;
|
|---|
| 727 | SelectDrivesButton.YAlign := yaTop;
|
|---|
| 728 |
|
|---|
| 729 | Bevel.XStretch := xsFrame;
|
|---|
| 730 | Bevel.YAlign := yaTop;
|
|---|
| 731 |
|
|---|
| 732 | LED.XAlign := xaRight;
|
|---|
| 733 | LED.YAlign := yaTop;
|
|---|
| 734 |
|
|---|
| 735 | ResultsLabel.XAlign := xaLeft;
|
|---|
| 736 | ResultsLabel.YAlign := yaTop;
|
|---|
| 737 |
|
|---|
| 738 | ProgressLabel.XStretch := xsFrame;
|
|---|
| 739 | ProgressLabel.YAlign := yaTop;
|
|---|
| 740 |
|
|---|
| 741 | ResultsOutline.XStretch := xsFrame;
|
|---|
| 742 | ResultsOutline.YStretch := ysFrame;
|
|---|
| 743 |
|
|---|
| 744 | ProgressBar.XStretch := xsFrame;
|
|---|
| 745 | ProgressBar.YAlign := yaBottom;
|
|---|
| 746 |
|
|---|
| 747 | end;
|
|---|
| 748 |
|
|---|
| 749 | procedure EnsureGlobalSearchFormLoaded;
|
|---|
| 750 | begin
|
|---|
| 751 | if GlobalSearchForm = nil then
|
|---|
| 752 | GlobalSearchForm := TGlobalSearchForm.Create( nil );
|
|---|
| 753 | end;
|
|---|
| 754 |
|
|---|
| 755 | Initialization
|
|---|
| 756 | RegisterClasses ([TGlobalSearchForm, TEdit, TLabel,
|
|---|
| 757 | TProgressBar, TButton, TOutline2, TComboBox, TBevel, TLed, TTimer]);
|
|---|
| 758 | RegisterUpdateProcForLanguages( EnsureGlobalSearchFormLoaded );
|
|---|
| 759 | End.
|
|---|