[18] | 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,
|
---|
[32] | 14 | Classes,
|
---|
| 15 | Forms,
|
---|
| 16 | Buttons,
|
---|
| 17 | StdCtrls,
|
---|
| 18 | ComCtrls,
|
---|
| 19 | Outline2,
|
---|
| 20 | GenericThread,
|
---|
| 21 | ACLLanguageUnit,
|
---|
| 22 | TextSearchQuery,
|
---|
| 23 | HelpFile,
|
---|
| 24 | ExtCtrls,
|
---|
| 25 | LED;
|
---|
[18] | 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
|
---|
[313] | 110 | Procedure OnLanguageEvent( Language: TLanguageFile; const Apply: boolean );
|
---|
[18] | 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
|
---|
[32] | 141 | SysUtils,
|
---|
[43] | 142 | DebugUnit,
|
---|
[32] | 143 | ACLDialogs,
|
---|
| 144 | ControlsUtility,
|
---|
| 145 | DriveInfoUnit,
|
---|
| 146 | IPFFileFormatUnit,
|
---|
| 147 | HelpTopic,
|
---|
| 148 | SearchUnit,
|
---|
| 149 | SearchDirectoriesFormUnit,
|
---|
| 150 | SettingsUnit,
|
---|
[82] | 151 | InformationFormUnit,
|
---|
[102] | 152 | StringUtilsUnit,
|
---|
[82] | 153 | FileUtilsUnit;
|
---|
[18] | 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 |
|
---|
[98] | 198 | Procedure TGlobalSearchForm.GetSelectedDirectories(List: TStringList);
|
---|
| 199 | Var
|
---|
| 200 | tmpDirectories : TStringList;
|
---|
[18] | 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
|
---|
[98] | 220 | tmpDirectories := TStringList.Create;
|
---|
[18] | 221 | List.Sorted := true;
|
---|
| 222 | List.Duplicates := dupIgnore;
|
---|
| 223 |
|
---|
[98] | 224 | // GetDirsInPath clears the list tmpDirectories
|
---|
| 225 | GetDirsInPath(HelpPathEnvironmentVar, tmpDirectories);
|
---|
| 226 | List.AddStrings(tmpDirectories);
|
---|
[18] | 227 |
|
---|
[98] | 228 | GetDirsInPath(BookshelfEnvironmentVar, tmpDirectories);
|
---|
| 229 | List.AddStrings(tmpDirectories);
|
---|
[18] | 230 |
|
---|
[98] | 231 | tmpDirectories.Destroy;
|
---|
[18] | 232 |
|
---|
[98] | 233 | // for selected paths we have to adjust the selection
|
---|
[18] | 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
|
---|
[98] | 239 | Dir := List[i];
|
---|
[18] | 240 | if not Settings.SearchDirectories.Find( Dir, FoundIndex ) then
|
---|
[98] | 241 | List.Objects[i] := pointer(1);
|
---|
[18] | 242 | end;
|
---|
| 243 | end;
|
---|
| 244 | end;
|
---|
| 245 |
|
---|
[98] | 246 |
|
---|
[18] | 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...
|
---|
[98] | 265 | List.Assign(Settings.SearchDirectories);
|
---|
[18] | 266 | end;
|
---|
| 267 | end;
|
---|
| 268 | end;
|
---|
| 269 |
|
---|
| 270 | Procedure TGlobalSearchForm.SelectDrivesButtonOnClick (Sender: TObject);
|
---|
[98] | 271 | begin
|
---|
| 272 | GetSelectedDirectories(SearchDirectoriesForm.SelectedFolders);
|
---|
[18] | 273 |
|
---|
| 274 | SearchDirectoriesForm.ShowModal;
|
---|
| 275 | if SearchDirectoriesForm.ModalResult <> mrOK then
|
---|
| 276 | exit;
|
---|
| 277 |
|
---|
[98] | 278 | // there was a selection, so we have to change the dropdown
|
---|
[18] | 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 |
|
---|
[98] | 288 | // update the settings
|
---|
[18] | 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
|
---|
[472] | 303 | ScaleForm( self, 9, 16 );
|
---|
| 304 |
|
---|
[465] | 305 | // Fix scaling issues on some controls
|
---|
[472] | 306 | SearchLocationComboBox.Top := SelectDrivesButton.Top + 3;
|
---|
| 307 | SearchTextLabel1.Top := SelectDrivesButton.Top + 5;
|
---|
[465] | 308 | Bevel.Bottom := ClientHeight - ResultsLabel.Top + 4;
|
---|
[472] | 309 | Led.Height := 15;
|
---|
[465] | 310 | Led.Width := Led.Height;
|
---|
[472] | 311 | Led.Bottom := ResultsLabel.Bottom + 1;
|
---|
| 312 | Led.Right := ResultsOutline.Right - 1;
|
---|
[465] | 313 |
|
---|
[472] | 314 | SearchTextLabel1.XAlign := xaLeft;
|
---|
| 315 | SearchTextLabel1.YAlign := yaTop;
|
---|
| 316 |
|
---|
| 317 | Bevel.XStretch := xsFrame;
|
---|
| 318 | Bevel.YAlign := yaTop;
|
---|
| 319 |
|
---|
| 320 | LED.XAlign := xaRight;
|
---|
| 321 | LED.YAlign := yaTop;
|
---|
| 322 |
|
---|
[18] | 323 | SearchButton.Align := alFixedRightTop;
|
---|
| 324 |
|
---|
| 325 | if SearchLocationComboBox.Items.Count = 0 then
|
---|
| 326 | begin
|
---|
| 327 | // must match teh global search location enum...
|
---|
| 328 | SearchLocationComboBox.Items.Add( StandardHelpPathsLocation );
|
---|
| 329 | SearchLocationComboBox.Items.Add( FixedDrivesLocation );
|
---|
| 330 | SearchLocationComboBox.Items.Add( SelectedHelpPathsLocation );
|
---|
| 331 | SearchLocationComboBox.Items.Add( CustomPathsLocation );
|
---|
| 332 |
|
---|
| 333 | SearchLocationComboBox.ItemIndex := Ord( Settings.GlobalSearchLocation );
|
---|
| 334 | end;
|
---|
| 335 |
|
---|
| 336 | End;
|
---|
| 337 |
|
---|
[94] | 338 | Procedure TGlobalSearchForm.OnLanguageEvent(Language: TLanguageFile; const Apply: boolean );
|
---|
[18] | 339 | begin
|
---|
[94] | 340 | LogEvent(LogI18n, 'TGlobalSearchForm.OnLanguageEvent apply: "' + BoolToStr(Apply) + '"');
|
---|
[18] | 341 |
|
---|
[94] | 342 | Language.LoadComponentLanguage(self, Apply);
|
---|
[18] | 343 |
|
---|
[94] | 344 | Language.LL(Apply, SearchCaption, 'SearchCaption', '~Search');
|
---|
| 345 | Language.LL(Apply, StopCaption, 'StopCaption', '~Stop');
|
---|
| 346 | Language.LL(Apply, NoResultsMsg, 'NoResultsMsg', '(No results found)');
|
---|
| 347 | Language.LL(Apply, ScanDirectoriesMsg, 'ScanDirectoriesMsg', 'Finding help files...');
|
---|
| 348 | Language.LL(Apply, SearchingFileMsg, 'SearchingFileMsg', 'Searching ');
|
---|
| 349 | Language.LL(Apply, OfMsg, 'OfMsg', ' of ');
|
---|
| 350 | Language.LL(Apply, DoneMsg, 'DoneMsg', 'Done');
|
---|
| 351 | Language.LL(Apply, SearchErrorTitle, 'SearchErrorTitle', 'Search');
|
---|
| 352 | Language.LL(Apply, SearchError, 'SearchError', 'Error in search syntax: ');
|
---|
[18] | 353 |
|
---|
[94] | 354 | Language.LL(Apply, StandardHelpPathsLocation, 'StandardHelpPathsLocation', 'Standard Help Paths');
|
---|
| 355 | Language.LL(Apply, FixedDrivesLocation, 'FixedDrivesLocation', 'All Hard Drives');
|
---|
| 356 | Language.LL(Apply, SelectedHelpPathsLocation, 'SelectedHelpPathsLocation', 'Selected Help Paths');
|
---|
| 357 | Language.LL(Apply, CustomPathsLocation, 'CustomPathsLocation', 'Directory List');
|
---|
[18] | 358 | end;
|
---|
| 359 |
|
---|
[94] | 360 |
|
---|
[18] | 361 | Procedure TGlobalSearchForm.CancelButtonOnClick (Sender: TObject);
|
---|
| 362 | Begin
|
---|
| 363 | Close;
|
---|
| 364 | End;
|
---|
| 365 |
|
---|
| 366 | Procedure TGlobalSearchForm.GlobalSearchFormOnClose (Sender: TObject;
|
---|
| 367 | Var Action: TCloseAction);
|
---|
| 368 | Begin
|
---|
| 369 | ClearResults;
|
---|
| 370 |
|
---|
| 371 | if SearchLocationComboBox.ItemIndex = -1 then
|
---|
| 372 | begin
|
---|
| 373 | Settings.GlobalSearchLocation := gsCustom;
|
---|
| 374 | Settings.SearchDirectories.Clear;
|
---|
| 375 | Settings.SearchDirectories.Add( SearchLocationComboBox.Text );
|
---|
| 376 | end;
|
---|
| 377 |
|
---|
| 378 | Action := caFreeHandle; // DON'T release the form! (Default action for non-modal forms)
|
---|
| 379 | End;
|
---|
| 380 |
|
---|
| 381 | Procedure TGlobalSearchForm.ResultsOutlineOnItemDblClick (Node: TNode);
|
---|
| 382 | Begin
|
---|
| 383 | ViewTopic;
|
---|
| 384 | End;
|
---|
| 385 |
|
---|
| 386 | Procedure TGlobalSearchForm.ViewTopicButtonOnClick (Sender: TObject);
|
---|
| 387 | begin
|
---|
| 388 | ViewTopic;
|
---|
| 389 | end;
|
---|
| 390 |
|
---|
| 391 | Procedure TGlobalSearchForm.ViewTopic;
|
---|
| 392 | var
|
---|
| 393 | Node: TNode;
|
---|
| 394 | HelpFileInfo: THelpFileInfo;
|
---|
| 395 | TopicIndex: longint;
|
---|
| 396 | Begin
|
---|
| 397 | Node := ResultsOutline.SelectedNode;
|
---|
| 398 | if Node = nil then
|
---|
| 399 | exit;
|
---|
| 400 | case Node.Level of
|
---|
| 401 | 0:
|
---|
| 402 | begin
|
---|
| 403 | // file node
|
---|
| 404 | HelpFileInfo := Node.Data as THelpFileInfo;
|
---|
| 405 | TopicIndex := -1;
|
---|
| 406 | end;
|
---|
| 407 |
|
---|
| 408 | 1:
|
---|
| 409 | begin
|
---|
| 410 | // topic node
|
---|
| 411 | HelpFileInfo := Node.Parent.Data as THelpFileInfo;
|
---|
| 412 | TopicIndex := longint( Node.Data );
|
---|
| 413 | end;
|
---|
| 414 |
|
---|
| 415 | else
|
---|
| 416 | assert( false, 'Invalid node level in ViewTopic!: ' + IntToStr( Node.Level ) );
|
---|
| 417 | end;
|
---|
| 418 |
|
---|
| 419 | ViewTopicCallback( HelpFileInfo.FileName, TopicIndex );
|
---|
| 420 | End;
|
---|
| 421 |
|
---|
| 422 | Procedure TGlobalSearchForm.GlobalSearchFormOnCreate (Sender: TObject);
|
---|
| 423 | Begin
|
---|
| 424 | RegisterForLanguages( OnLanguageEvent );
|
---|
| 425 |
|
---|
| 426 | UpdateButtons;
|
---|
| 427 |
|
---|
| 428 | ThreadManager := TGenericThreadManager.Create( self );
|
---|
| 429 | ThreadManager.OnProgressUpdate := OnSearchProgress;
|
---|
| 430 | ThreadManager.OnDataFromThread := OnThreadData;
|
---|
| 431 | ThreadManager.OnJobComplete := OnSearchFinished;
|
---|
| 432 | End;
|
---|
| 433 |
|
---|
| 434 | Procedure TGlobalSearchForm.OnSearchFinished( Result: TObject );
|
---|
| 435 | Begin
|
---|
| 436 | SearchButton.Caption := SearchCaption;
|
---|
| 437 | ProgressBar.Hide;
|
---|
| 438 | LED.LedCondition := false;
|
---|
| 439 | LEDTimer.Stop;
|
---|
| 440 |
|
---|
| 441 | if ResultsOutline.ChildCount > 0 then
|
---|
| 442 | begin
|
---|
| 443 | ResultsOutline.SelectedNode:= ResultsOutline.Children[ 0 ];
|
---|
| 444 | ResultsOutline.Focus;
|
---|
| 445 | SetProgressLabel( '' );
|
---|
| 446 | end
|
---|
| 447 | else
|
---|
| 448 | begin
|
---|
| 449 | SetProgressLabel( NoResultsMsg );
|
---|
| 450 | end;
|
---|
| 451 |
|
---|
| 452 | End;
|
---|
| 453 |
|
---|
| 454 | Procedure TGlobalSearchForm.GlobalSearchFormOnCloseQuery (Sender: TObject;
|
---|
| 455 | Var CanClose: Boolean);
|
---|
| 456 | Begin
|
---|
| 457 | if ThreadManager.IsRunning then
|
---|
| 458 | begin
|
---|
| 459 | ThreadManager.Stop;
|
---|
| 460 | end;
|
---|
| 461 | End;
|
---|
| 462 |
|
---|
| 463 | Procedure TGlobalSearchForm.SetProgressLabel( const S: String );
|
---|
| 464 | begin
|
---|
| 465 | ProgressLabel.Text := S;
|
---|
| 466 | ProgressLabel.Refresh;
|
---|
| 467 | end;
|
---|
| 468 |
|
---|
| 469 | Procedure TGlobalSearchForm.OnSearchProgress ( n, outof: integer;
|
---|
| 470 | S: String );
|
---|
| 471 | Begin
|
---|
| 472 | ProgressBar.Position := n * 100 div outof;
|
---|
| 473 | SetProgressLabel( S );
|
---|
| 474 | End;
|
---|
| 475 |
|
---|
| 476 | Function TGlobalSearchForm.Search( Parameters: TObject ): TObject;
|
---|
| 477 | var
|
---|
[98] | 478 | Files : TStringList;
|
---|
[18] | 479 | FileIndex: longint;
|
---|
| 480 | Filename: string;
|
---|
| 481 | HelpFile: THelpFile;
|
---|
| 482 | SearchResult: TSearchResult;
|
---|
| 483 | MatchingTopics: TList;
|
---|
| 484 |
|
---|
[98] | 485 | tmpSearchParameters : TSearchParameters;
|
---|
[18] | 486 | Query: TTextSearchQuery;
|
---|
| 487 | i: longint;
|
---|
[313] | 488 | tmpDir: string;
|
---|
[18] | 489 |
|
---|
| 490 | Begin
|
---|
[313] | 491 | // LogEvent(LogDebug, 'TGlobalSearchForm.Search');
|
---|
| 492 |
|
---|
[98] | 493 | tmpSearchParameters := Parameters as TSearchParameters;
|
---|
[18] | 494 |
|
---|
[98] | 495 | Query := tmpSearchParameters.Query;
|
---|
[18] | 496 | Files := TStringList.Create;
|
---|
| 497 |
|
---|
| 498 | MatchingTopics := TList.Create;
|
---|
[98] | 499 | LogEvent(LogParse, 'Creating file list');
|
---|
[18] | 500 |
|
---|
| 501 | // make sure we ignore duplicate files...
|
---|
| 502 | Files.Sorted := true;
|
---|
| 503 | Files.CaseSensitive := false;
|
---|
| 504 | Files.Duplicates := dupIgnore;
|
---|
| 505 |
|
---|
[98] | 506 | for i := 0 to tmpSearchParameters.Directories.Count - 1 do
|
---|
[18] | 507 | begin
|
---|
| 508 | if ThreadManager.StopRequested then
|
---|
[98] | 509 | begin
|
---|
[18] | 510 | break;
|
---|
[98] | 511 | end;
|
---|
| 512 |
|
---|
[313] | 513 | ThreadManager.UpdateProgress( i * 10 div tmpSearchParameters.Directories.Count, 100, ScanDirectoriesMsg );
|
---|
| 514 | tmpDir := tmpSearchParameters.Directories[i];
|
---|
| 515 | // LogEvent(LogDebug, 'TGlobalSearchForm.Search in dir: ' + tmpDir);
|
---|
| 516 | if StrEndsWith(tmpDir, '...') then
|
---|
[18] | 517 | begin
|
---|
[313] | 518 | tmpDir := StrLeftWithout(tmpDir, 3 );
|
---|
[82] | 519 | ListFilesInDirectoryRecursiveWithTermination(
|
---|
[313] | 520 | tmpDir,
|
---|
[18] | 521 | '*.inf;*.hlp',
|
---|
[98] | 522 | true,
|
---|
[392] | 523 | false,
|
---|
[18] | 524 | Files,
|
---|
| 525 | ThreadManager.StopRequested,
|
---|
| 526 | true ); // check termination
|
---|
| 527 | end
|
---|
| 528 | else
|
---|
| 529 | begin
|
---|
[313] | 530 | ListFilesInDirectory( tmpDir, '*.inf;*.hlp', true, Files);
|
---|
[18] | 531 | end;
|
---|
| 532 | end;
|
---|
| 533 |
|
---|
| 534 | for FileIndex := 0 to Files.Count - 1 do
|
---|
| 535 | begin
|
---|
| 536 | if ThreadManager.StopRequested then
|
---|
[98] | 537 | begin
|
---|
[18] | 538 | break;
|
---|
[98] | 539 | end;
|
---|
| 540 |
|
---|
| 541 | Filename := Files[FileIndex];
|
---|
| 542 |
|
---|
[18] | 543 | ThreadManager.UpdateProgress( 10 + FileIndex * 95 div Files.Count,
|
---|
| 544 | 100,
|
---|
| 545 | SearchingFileMsg
|
---|
| 546 | + Filename
|
---|
| 547 | + ' ('
|
---|
| 548 | + IntToStr( FileIndex + 1 )
|
---|
| 549 | + OfMsg
|
---|
| 550 | + IntToStr( Files.Count )
|
---|
| 551 | + ')...' );
|
---|
| 552 |
|
---|
| 553 | try
|
---|
| 554 | HelpFile := THelpFile.Create( FileName );
|
---|
| 555 | MatchingTopics.Clear;
|
---|
| 556 | SearchHelpFile( HelpFile,
|
---|
| 557 | Query,
|
---|
| 558 | MatchingTopics,
|
---|
| 559 | nil // don't care about words matched
|
---|
| 560 | );
|
---|
| 561 |
|
---|
| 562 | if MatchingTopics.Count > 0 then
|
---|
| 563 | begin
|
---|
| 564 | // Create a searchresult object to send back to main thread.
|
---|
| 565 | SearchResult := TSearchResult.Create;
|
---|
| 566 | SearchResult.Filename := HelpFile.Filename;
|
---|
| 567 | SearchResult.FileTitle := HelpFile.Title;
|
---|
| 568 |
|
---|
| 569 | SearchResult.MatchingTopics.Assign( MatchingTopics );
|
---|
| 570 |
|
---|
| 571 | SearchResult.MatchingTopics.Sort( TopicRelevanceCompare );
|
---|
| 572 | ThreadManager.SendData( '', SearchResult );
|
---|
| 573 | end;
|
---|
| 574 |
|
---|
| 575 | HelpFile.Destroy;
|
---|
| 576 |
|
---|
| 577 | except
|
---|
| 578 | on E: EHelpFileException do
|
---|
| 579 | begin
|
---|
| 580 | ; // ignore exceptions
|
---|
| 581 | end;
|
---|
| 582 |
|
---|
| 583 | on E: EWindowsHelpFormatException do
|
---|
| 584 | begin
|
---|
| 585 | ; // ignore Windows help files
|
---|
| 586 | end;
|
---|
| 587 | end;
|
---|
| 588 |
|
---|
| 589 | end;
|
---|
| 590 | ThreadManager.UpdateProgress( 100, 100, DoneMsg );
|
---|
| 591 | Files.Destroy;
|
---|
| 592 |
|
---|
| 593 | Query.Destroy;
|
---|
| 594 |
|
---|
[98] | 595 | tmpSearchParameters.Directories.Destroy;
|
---|
| 596 | tmpSearchParameters.Destroy;
|
---|
[18] | 597 |
|
---|
| 598 | Result := nil;
|
---|
| 599 | End;
|
---|
| 600 |
|
---|
| 601 | Procedure TGlobalSearchForm.ClearResults;
|
---|
| 602 | var
|
---|
| 603 | FileIndex: longint;
|
---|
| 604 | begin
|
---|
| 605 | for FileIndex := 0 to ResultsOutline.ChildCount - 1 do
|
---|
| 606 | ResultsOutline.Children[ FileIndex ].Data.Free;
|
---|
| 607 | ResultsOutline.Clear;
|
---|
| 608 | ViewTopicButton.Enabled := false;
|
---|
| 609 | end;
|
---|
| 610 |
|
---|
| 611 | Procedure TGlobalSearchForm.SearchButtonOnClick (Sender: TObject);
|
---|
| 612 | begin
|
---|
| 613 | DoSearch;
|
---|
| 614 | end;
|
---|
| 615 |
|
---|
| 616 | Procedure TGlobalSearchForm.DoSearch;
|
---|
| 617 | var
|
---|
[98] | 618 | tmpSelectedDirectories : TStringList;
|
---|
| 619 | i : integer;
|
---|
[313] | 620 | tmpSearchText: string;
|
---|
[98] | 621 |
|
---|
[18] | 622 | Query: TTextSearchQuery;
|
---|
| 623 | SearchParameters: TSearchParameters;
|
---|
| 624 | Begin
|
---|
[313] | 625 | // LogEvent(LogDebug, 'DoSearch');
|
---|
| 626 |
|
---|
[18] | 627 | if ThreadManager.IsRunning then
|
---|
| 628 | begin
|
---|
| 629 | ThreadManager.Stop;
|
---|
| 630 | exit;
|
---|
| 631 | end;
|
---|
| 632 |
|
---|
[313] | 633 | tmpSearchText := trim( SearchTextEdit.Text );
|
---|
| 634 | if tmpSearchText = '' then
|
---|
[18] | 635 | exit;
|
---|
| 636 |
|
---|
[313] | 637 | // LogEvent(LogDebug, 'DoSearch: ' + tmpSearchText);
|
---|
[18] | 638 | try
|
---|
[313] | 639 | Query := TTextSearchQuery.Create(tmpSearchText);
|
---|
[18] | 640 | except
|
---|
| 641 | on e: ESearchSyntaxError do
|
---|
| 642 | begin
|
---|
| 643 | DoErrorDlg( SearchErrorTitle,
|
---|
| 644 | SearchError
|
---|
| 645 | + e.Message );
|
---|
| 646 | exit;
|
---|
| 647 | end;
|
---|
| 648 | end;
|
---|
| 649 |
|
---|
| 650 | ClearResults;
|
---|
| 651 |
|
---|
| 652 | SearchParameters := TSearchParameters.Create;
|
---|
| 653 |
|
---|
| 654 | SearchParameters.Query := Query;
|
---|
| 655 |
|
---|
[98] | 656 | // now the directories to search in
|
---|
| 657 | SearchParameters.Directories := TStringList.Create;
|
---|
[18] | 658 |
|
---|
[98] | 659 | tmpSelectedDirectories := TStringList.Create;
|
---|
| 660 | GetSelectedDirectories(tmpSelectedDirectories);
|
---|
| 661 |
|
---|
[313] | 662 | LogEvent(LogDebug, 'DoSearch: tmpSelectedDirectories.Count ' + IntToStr(tmpSelectedDirectories.Count));
|
---|
| 663 |
|
---|
[98] | 664 | // clear the list and add only the selected ones
|
---|
| 665 | for i := 0 to tmpSelectedDirectories.Count - 1 do
|
---|
| 666 | begin
|
---|
| 667 | if tmpSelectedDirectories.Objects[i] = nil then
|
---|
| 668 | begin
|
---|
| 669 | SearchParameters.Directories.add(tmpSelectedDirectories[i]);
|
---|
| 670 | end;
|
---|
| 671 | end;
|
---|
| 672 | tmpSelectedDirectories.Destroy;
|
---|
| 673 |
|
---|
[18] | 674 | ThreadManager.StartJob( Search, SearchParameters );
|
---|
| 675 | SearchButton.Caption := StopCaption;
|
---|
| 676 | ProgressBar.Show;
|
---|
| 677 |
|
---|
| 678 | LED.LedCondition := true;
|
---|
| 679 | LEDTimer.Start;
|
---|
| 680 | End;
|
---|
| 681 |
|
---|
| 682 | Procedure TGlobalSearchForm.GlobalSearchFormOnShow (Sender: TObject);
|
---|
| 683 | Begin
|
---|
| 684 | // make search button default
|
---|
| 685 | SearchButton.Focus;
|
---|
| 686 | SearchTextEdit.Focus;
|
---|
| 687 | SetProgressLabel( '' );
|
---|
| 688 | PostMsg( Handle, WM_OPENED, 0, 0 );
|
---|
| 689 | SearchButton.Caption := SearchCaption;
|
---|
| 690 | ProgressBar.Hide;
|
---|
| 691 | ViewTopicButton.Enabled := false;
|
---|
| 692 | End;
|
---|
| 693 |
|
---|
| 694 | Procedure TGlobalSearchForm.OnThreadData( S: string; Data: TObject );
|
---|
| 695 | var
|
---|
| 696 | SearchResult: TSearchResult;
|
---|
| 697 | begin
|
---|
| 698 | SearchResult := Data as TSearchResult;
|
---|
| 699 | OnMatchFound( SearchResult );
|
---|
| 700 | SearchResult.Destroy;
|
---|
| 701 | end;
|
---|
| 702 |
|
---|
| 703 | Procedure TGlobalSearchForm.OnMatchFound( SearchResult: TSearchResult );
|
---|
| 704 | var
|
---|
| 705 | Topic: TTopic;
|
---|
| 706 | HelpFileInfo: THelpFileInfo;
|
---|
| 707 | FileNode: TNode;
|
---|
| 708 | TopicIndex: longint;
|
---|
| 709 | begin
|
---|
| 710 | ViewTopicButton.Enabled := true;
|
---|
| 711 |
|
---|
| 712 | HelpFileInfo := THelpFileInfo.Create;
|
---|
| 713 | HelpFileInfo.FileName := SearchResult.FileName;
|
---|
| 714 | FileNode := ResultsOutline.AddChild( SearchResult.FileTitle
|
---|
| 715 | + ' ('
|
---|
| 716 | + SearchResult.FileName
|
---|
| 717 | + ')',
|
---|
| 718 | HelpFileInfo );
|
---|
| 719 | for TopicIndex := 0 to SearchResult.MatchingTopics.Count - 1 do
|
---|
| 720 | begin
|
---|
| 721 | Topic := SearchResult.MatchingTopics[ TopicIndex ];
|
---|
| 722 | FileNode.AddChild( Topic.Title,
|
---|
| 723 | TObject( Topic.Index ) );
|
---|
| 724 | end;
|
---|
| 725 | end;
|
---|
| 726 |
|
---|
| 727 | Procedure TGlobalSearchForm.WMOpened( Var Msg: TMessage );
|
---|
| 728 | begin
|
---|
[87] | 729 | SearchTextLabel.XAlign := xaLeft;
|
---|
| 730 | SearchTextLabel.YAlign := yaTop;
|
---|
| 731 |
|
---|
| 732 | SearchTextEdit.XStretch := xsFrame;
|
---|
| 733 | SearchTextEdit.YAlign := yaTop;
|
---|
| 734 | SearchTextEdit.Focus;
|
---|
| 735 |
|
---|
| 736 | SearchButton.XAlign := xaRight;
|
---|
| 737 | SearchButton.YAlign := yaTop;
|
---|
| 738 |
|
---|
| 739 | SearchLocationComboBox.XStretch := xsFrame;
|
---|
| 740 | SearchLocationComboBox.YAlign := yaTop;
|
---|
| 741 |
|
---|
| 742 | SelectDrivesButton.XAlign := xaRight;
|
---|
| 743 | SelectDrivesButton.YAlign := yaTop;
|
---|
| 744 |
|
---|
| 745 | ResultsLabel.XAlign := xaLeft;
|
---|
| 746 | ResultsLabel.YAlign := yaTop;
|
---|
| 747 |
|
---|
[18] | 748 | ProgressLabel.XStretch := xsFrame;
|
---|
[87] | 749 | ProgressLabel.YAlign := yaTop;
|
---|
| 750 |
|
---|
[18] | 751 | ResultsOutline.XStretch := xsFrame;
|
---|
| 752 | ResultsOutline.YStretch := ysFrame;
|
---|
[87] | 753 |
|
---|
[18] | 754 | ProgressBar.XStretch := xsFrame;
|
---|
| 755 | ProgressBar.YAlign := yaBottom;
|
---|
[87] | 756 |
|
---|
[18] | 757 | end;
|
---|
| 758 |
|
---|
| 759 | procedure EnsureGlobalSearchFormLoaded;
|
---|
| 760 | begin
|
---|
| 761 | if GlobalSearchForm = nil then
|
---|
| 762 | GlobalSearchForm := TGlobalSearchForm.Create( nil );
|
---|
| 763 | end;
|
---|
| 764 |
|
---|
| 765 | Initialization
|
---|
| 766 | RegisterClasses ([TGlobalSearchForm, TEdit, TLabel,
|
---|
| 767 | TProgressBar, TButton, TOutline2, TComboBox, TBevel, TLed, TTimer]);
|
---|
| 768 | RegisterUpdateProcForLanguages( EnsureGlobalSearchFormLoaded );
|
---|
| 769 | End.
|
---|