Ignore:
Timestamp:
Jan 14, 2007, 8:02:42 PM (19 years ago)
Author:
RBRi
Message:

another refactoring step for cleaning up the command line handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/NewView/ViewStub.pas

    r57 r61  
    1212  PmWin,
    1313  SysUtils,
     14  Classes,
    1415  DebugUnit,
     16  GlobalFilelistUnit,
    1517  CmdLineParameterUnit,
     18  HelpManagerUnit,
     19  StringUtilsUnit,
    1620  StartupUnit;
    1721
     
    4549
    4650var
    47     tmpCmdLine : String;
    48     tmpPCharCmdLine : PChar;
     51  tmpCmdLine : String;
     52  tmpPCharCmdLine : PChar;
     53  tmpCmdLineParameters: TCmdLineParameters;
     54  tmpExistingWindow: HWND;
     55
     56
     57  // If another instance already has the files open
     58  // activate it and return true.
     59  Function FindExistingWindow(aCmdLineParameters : TCmdLineParameters) : HWND;
     60  var
     61    tmpFileItems: TStringList;
     62    tmpFilenames: TStringList;
     63    tmpFullFilePath: string;
     64    i: longint;
     65
     66    FileWindow: HWND;
     67  begin
     68    result := NULLHANDLE;
     69
     70    if aCmdLineParameters.getInterpretedFileNames = '' then
     71      // not loading files; nothing to check
     72      exit;
     73
     74    tmpFileItems := TStringList.Create;
     75    tmpFilenames := TStringList.Create;
     76
     77    StrExtractStrings(tmpFileItems, aCmdLineParameters.getInterpretedFileNames, ['+'], #0);
     78    TranslateIPFEnvironmentVars(tmpFileItems, tmpFileNames );
     79
     80    for i := 0 to tmpFileNames.Count - 1 do
     81    begin
     82      tmpFullFilePath := FindHelpFile( tmpFilenames[ i ] );
     83      if tmpFullFilePath <> '' then
     84      begin
     85        FileWindow := GlobalFilelist.FindFile(tmpFullFilePath);
     86
     87        if FileWindow = NULLHANDLE then
     88        begin
     89          // not found - stop searching.
     90          Result := NULLHANDLE; // no match
     91          break;
     92        end;
     93
     94        // found it
     95
     96        // is it the same as any previous match?
     97        if Result <> NULLHANDLE then
     98        begin
     99          if FileWindow <> Result then
     100          begin
     101            // no, so we don't have a match.
     102            // NOTE: We just excluded something: if the same file is
     103            // open in multiple windows then we may not check all combinations
     104            Result := NULLHANDLE; // no match
     105            break;
     106          end;
     107        end
     108        else
     109        begin
     110          // no match yet - store this one
     111          result := FileWindow;
     112        end;
     113      end;
     114    end;
     115
     116    tmpFilenames.Destroy;
     117    tmpFileItems.Destroy;
     118  end;
     119
     120
    49121
    50122Begin
    51123  LogEvent(LogViewStub, 'Starting' );
    52124
    53   if Startup then
     125  // open shared memory
     126  SharedMemory := AccessSharedMemory;
     127
     128  // get access to the system-global filelist
     129  GlobalFilelist := TGlobalFilelist.Create;
     130
     131  // parse parameters into Parameters object
     132  tmpCmdLine := nativeOS2GetCmdLineParameter;
     133
     134  tmpCmdLineParameters := TCmdLineParameters.Create;
     135  tmpCmdLineParameters.parseCmdLine(tmpCmdLine);
     136
     137  tmpExistingWindow := FindExistingWindow(tmpCmdLineParameters);
     138
     139  if tmpExistingWindow = NULLHANDLE then
    54140  begin
    55141    // Want a new instance.
    56 
    57     tmpCmdLine := nativeOS2GetCmdLineParameter;
     142    LogEvent(LogViewStub, 'Starting a new NewView instance');
     143
    58144    tmpPCharCmdLine := StrAlloc(length(tmpCmdLine) + 1);
    59145    StrPCopy(tmpPCharCmdLine, tmpCmdLine);
    60146
     147    LogEvent(LogViewStub, 'CmdLine for NewView exe: ' + tmpCmdLine);
    61148    // set up details for launching newview
    62149    With Details do
     
    83170
    84171    StrDispose(tmpPCharCmdLine);
     172  end
     173  else
     174  begin
     175    // the file is already opend in a running instance
     176    LogEvent(LogViewStub, 'Adequate running NewView instance found');
     177
     178    WinSetFocus( HWND_DESKTOP, tmpExistingWindow );
     179
     180    // search and topic search
     181    // topic search is also handled this way
     182    // at the moment there is no support for a
     183    // seperate window message
     184    if not tmpCmdLineParameters.getGlobalSearchFlag
     185       AND (tmpCmdLineParameters.getInterpretedSearchText <> '')
     186    then
     187    begin
     188      PostNewViewTextMessage( tmpExistingWindow,
     189                              NHM_SEARCH,
     190                              tmpCmdLineParameters.getInterpretedSearchText);
     191    end;
     192
     193    if tmpCmdLineParameters.getGlobalSearchFlag then
     194    begin
     195      PostNewViewTextMessage( tmpExistingWindow,
     196                              NHM_GLOBAL_SEARCH,
     197                              tmpCmdLineParameters.getInterpretedSearchText);
     198    end;
     199
     200    if tmpCmdLineParameters.getShowUsageFlag then
     201    begin
     202      WinPostMsg( tmpExistingWindow,
     203               NHM_SHOW_USAGE,
     204               0,
     205               0 );
     206    end;
     207
     208    if tmpCmdLineParameters.getHelpManagerFlag then
     209    begin
     210      // tell the new help manager instance to talk to the
     211      // other viewer
     212      WinPostMsg( tmpCmdLineParameters.getHelpManagerWindow,
     213               NHM_VIEWER_READY,
     214               tmpExistingWindow,
     215               0 );
     216    end;
    85217
    86218  end;
     219
     220  // destroy global list
     221  GlobalFilelist.Destroy;
     222
     223  tmpCmdLineParameters.Destroy;
    87224  LogEvent(LogViewStub, 'Finished' );
    88225End.
Note: See TracChangeset for help on using the changeset viewer.