source: trunk/NewView/StartupUnit.pas@ 48

Last change on this file since 48 was 48, checked in by RBRi, 19 years ago

Format fixes

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1Unit StartupUnit;
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// Code related to startup and finding help files.
8// Shared between NewView.exe and ViewStub.exe
9
10Interface
11
12uses
13 OS2Def,
14 Classes,
15 ACLString,
16 GlobalFilelistUnit,
17 SharedMemoryUnit,
18 CmdLineParameterUnit;
19
20const
21 OWN_HELP_MARKER = '[NVHELP]';
22
23
24function AccessSharedMemory: TSuballocatedSharedMemory;
25
26// Returns true if the program should be started as normal.
27// False if it should immediately exit.
28function Startup: boolean;
29
30function GetOwnHelpFileName: string;
31
32// Look for any items that are actually specifiying environment
33// variables, and expand them to the contents of the variables
34Procedure TranslateIPFEnvironmentVars( Items: TStrings;
35 ExpandedItems: TStrings );
36
37// Given a filename, which may or may not contain a path or extension,
38// finds the actual file. This can involve searching
39// the help and bookshelf paths.
40Function FindHelpFile( FileName: string ): string;
41
42var
43 CmdLineParameters: TCmdLineParameters;
44 SharedMemory: TSubAllocatedSharedMemory;
45 GlobalFilelist: TGlobalFilelist;
46
47Implementation
48
49uses
50 Dos,
51 SysUtils,
52 DebugUnit,
53 PMWin,
54 ACLUtility,
55 ACLStringUtility,
56 ACLFileUtility,
57 AStringUtilityUnit,
58 HelpManagerUnit;
59
60// Look for any items that are actually specifiying environment
61// variables, and expand them to the contents of the variables
62Procedure TranslateIPFEnvironmentVars( Items: TStrings;
63 ExpandedItems: TStrings );
64var
65 i: longint;
66 Item: string;
67 EnvironmentVarValue: string;
68begin
69 LogEvent(LogStartup, 'Translating environment vars' );
70 for i := 0 to Items.Count - 1 do
71 begin
72 Item := Items[ i ];
73
74 Item := StrUnQuote( Item ); // remove single quotes
75 Item := StrUnDoubleQuote( Item ); // remove double quotes
76
77 LogEvent(LogStartup, ' Item: ' + Item );
78 EnvironmentVarValue := GetEnv( Uppercase( Item ) );
79 if DosError = 0 then
80 begin
81 // environment var exists - use it's value
82 LogEvent(LogStartup, ' Translated: ' + EnvironmentVarValue );
83 while EnvironmentVarValue <> '' do
84 begin
85 Item := ExtractNextValue( EnvironmentVarValue, '+' );
86 ExpandedItems.Add( Item );
87 end;
88 end
89 else
90 begin
91 // not an environment var
92 ExpandedItems.Add( Item );
93 end;
94 end;
95end;
96
97// Given a filename, which may or may not contain a path or extension,
98// finds the actual file. This can involve searching
99// the help and bookshelf paths.
100Function FindHelpFile( FileName: string ): string;
101var
102 AlternativeFileName: string;
103begin
104 if FileName = OWN_HELP_MARKER then
105 begin
106 Result := GetOwnHelpFileName;
107 exit;
108 end;
109
110 Result := '';
111
112 AlternativeFileName := '';
113 if ExtractFileExt( Filename ) = '' then
114 begin
115 Filename := ChangeFileExt( Filename, '.inf' );
116 AlternativeFileName := ChangeFileExt( Filename, '.hlp' );
117 end;
118
119 if ExtractFilePath( FileName ) <> '' then
120 begin
121 // Path specified; just see if it exists
122
123 // expand out relative paths
124 Filename := ExpandFileName( FileName );
125 AlternativeFilename := ExpandFileName( AlternativeFilename );
126
127 if FileExists( Filename ) then
128 Result := Filename
129 else if FileExists( AlternativeFilename ) then
130 Result := AlternativeFilename;
131
132 end
133 else
134 begin
135 // Path not specified; search current
136 if FileExists( ExpandFileName( FileName ) ) then
137 begin
138 Result := ExpandFileName( FileName );
139 exit;
140 end;
141
142 if FileExists( ExpandFileName( AlternativeFilename ) ) then
143 begin
144 Result := ExpandFileName( AlternativeFilename );
145 exit;
146 end;
147
148 // Search help paths
149
150 if not SearchHelpPaths( FileName,
151 Result,
152 false // don't search our app dir
153 ) then
154 begin
155 // Didn't find as specified or as .inf, try .hlp
156 if AlternativeFilename <> '' then
157 begin
158 if not SearchHelpPaths( AlternativeFileName,
159 Result,
160 false // don't search our app dir
161 ) then
162 begin
163 Result := '';
164 end;
165 end;
166 end;
167 end;
168end;
169
170function GetOwnHelpFileName: string;
171begin
172 Result := FindDefaultLanguageHelpFile( 'NewView' );
173end;
174
175// If another instance already has the files open
176// activate it and return true.
177function FindExistingWindow: HWND;
178var
179 FileItems: TStringList;
180 Filenames: TStringList;
181 FullFilePath: string;
182 i: longint;
183
184 FileWindow: HWND;
185begin
186 Result := NULLHANDLE;
187
188 if length(CmdLineParameters.getFileNames) = 0 then
189 // not loading files; nothing to check
190 exit;
191
192 FileItems := TStringList.Create;
193 Filenames := TStringList.Create;
194
195 StringToList(CmdLineParameters.getFileNames, FileItems, '+' );
196 TranslateIPFEnvironmentVars( FileItems, FileNames );
197
198 for i := 0 to FileNames.Count - 1 do
199 begin
200 FullFilePath := FindHelpFile( Filenames[ i ] );
201 if FullFilePath <> '' then
202 begin
203 FileWindow := GlobalFilelist.FindFile( FullFilePath );
204
205 if FileWindow = NULLHANDLE then
206 begin
207 // not found - stop searching.
208 Result := NULLHANDLE; // no match
209 break;
210 end;
211
212 // found it
213
214 // is it the same as any previous match?
215 if Result <> NULLHANDLE then
216 begin
217 if FileWindow <> Result then
218 begin
219 // no, so we don't have a match.
220 // NOTE: We just excluded something: if the same file is
221 // open in multiple windows then we may not check all combinations
222 Result := NULLHANDLE; // no match
223 break;
224 end;
225 end
226 else
227 begin
228 // no match yet - store this one
229 Result := FileWindow;
230 end;
231
232 end;
233 end;
234
235 Filenames.Destroy;
236 FileItems.Destroy;
237
238end;
239
240function AccessSharedMemory: TSuballocatedSharedMemory;
241begin
242 Result := TSuballocatedSharedMemory.Create( SharedMemName,
243 SharedMemSize,
244 SharedMemReserveSize );
245end;
246
247procedure PostNewViewTextMessage( Window: HWND;
248 MessageType: ULONG;
249 s: string );
250var
251 ps: pchar;
252begin
253 SharedMemory.Allocate( ps, length( s ) + 1 );
254 ps ^ := s;
255 WinPostMsg( Window,
256 MessageType,
257 LONG( ps ),
258 0 );
259end;
260
261function Startup: boolean;
262var
263 tmpCmdLine: String;
264 ExistingWindow: HWND;
265begin
266 // open shared memory
267 SharedMemory := AccessSharedMemory;
268
269 // get access to the system-global filelist
270 GlobalFilelist := TGlobalFilelist.Create;
271
272 // parse parameters into Parameters object
273 tmpCmdLine := nativeOS2GetCmdLineParameter;
274
275 CmdLineParameters := TCmdLineParameters.Create;
276 CmdLineParameters.parseCmdLine(tmpCmdLine);
277
278 ExistingWindow := FindExistingWindow;
279
280 if ExistingWindow <> NULLHANDLE then
281 begin
282 // want to exit without running fully
283 Result := false;
284
285 // destroy global list - nobody else will
286 GlobalFilelist.Destroy;
287
288 WinSetFocus( HWND_DESKTOP, ExistingWindow );
289
290 // if CmdLineParameters.getTopics <> '' then
291 if not CmdLineParameters.getSearchFlag AND not CmdLineParameters.getGlobalSearchFlag then
292 begin
293 PostNewViewTextMessage( ExistingWindow,
294 NHM_SEARCH,
295 CmdLineParameters.getSearchText);
296 end;
297
298 if CmdLineParameters.getGlobalSearchFlag then
299 begin
300 PostNewViewTextMessage( ExistingWindow,
301 NHM_GLOBAL_SEARCH,
302 CmdLineParameters.getSearchText);
303 end;
304
305 if CmdLineParameters.getShowUsageFlag then
306 begin
307 WinPostMsg( ExistingWindow,
308 NHM_SHOW_USAGE,
309 0,
310 0 );
311 end;
312
313 if CmdLineParameters.getHelpManagerFlag then
314 begin
315 // tell the new help manager instance to talk to the
316 // other viewer
317 WinPostMsg( CmdLineParameters.getHelpManagerWindow,
318 NHM_VIEWER_READY,
319 ExistingWindow,
320 0 );
321 end;
322
323 end
324 else
325 begin
326 // run as normal
327 Result := true;
328 end;
329end;
330
331Initialization
332End.
Note: See TracBrowser for help on using the repository browser.