1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Author: Mattias Gaertner
Abstract:
Methods and types for CHM help using chm viewer "lhelp".
}
unit LazHelpCHM;
{$mode objfpc}{$H+}
{$IFDEF VerboseLCLHelp}
{$DEFINE VerboseChmHelp}
{$ENDIF}
interface
uses
Classes, SysUtils, LazHelpIntf, LazConfigStorage, HelpIntfs,
Dialogs, Forms, LazLoggerBase, FileUtil, LazFileUtils, LHelpControl, LResources;
const
CHMMimeType = 'application/chm';
CHMPathParam = 'path';
type
{ TCHMHelpDatabase
KeywordPrefix: if set, then the database will handle all Keywords
beginning with this value. And when the path is created by replacing
the prefix with the BaseURL.
For example:
Create a chm. For example build and run chmmaker in lazarus/tools/chmmaker
to create the example.chm (lazarus/tools/chmmaker/example.chm).
Put a TCHMHelpDatabase on a form.
Set AutoRegister to true.
Set KeywordPrefix to 'example'
Set CHM file to '../../../tools/chmmaker/example.chm'
Put a TLHelpRemoteViewer on the form.
Set AutoRegister to true.
Set LHelpPath to the path of lhelp. E.g. '../../lhelp/lhelp'
Put a TEdit on a form.
Set HelpType to htKeyword
Set HelpKeyword to 'example/MainPage.html'
Run the program.
Focus the edit field and press F1. The page '/MainPage.html' will be shown.
Note: lhelp requires the leading slash.
}
TCHMHelpDatabase = class(THelpDatabase)
private
FFilename: string;
FHelpNode: THelpNode;
FKeywordPrefix: string;
procedure SetFilename(AValue: string);
procedure SetKeywordPrefix(AValue: string);
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
function ShowHelp({%H-}Query: THelpQuery; {%H-}BaseNode, NewNode: THelpNode;
{%H-}QueryItem: THelpQueryItem;
var ErrMsg: string): TShowHelpResult; override;
function ShowURL(const URL, Title: string;
var ErrMsg: string): TShowHelpResult; virtual;
function GetNodesForKeyword(const HelpKeyword: string;
var ListOfNodes: THelpNodeQueryList;
var ErrMsg: string): TShowHelpResult; override;
procedure Load(Storage: TConfigStorage); override;
procedure Save(Storage: TConfigStorage); override;
published
property AutoRegister;
property Filename: string read FFilename write SetFilename;
property KeywordPrefix: string read FKeywordPrefix write SetKeywordPrefix;
end;
type
TOnFindLHelp = procedure(var Path: string) of object;
{ TLHelpConnector }
TLHelpConnector = class(THelpViewer)
private
FConnection: TLHelpConnection;
FLHelpPath: string;
FOnFindLHelp: TOnFindLHelp;
procedure SetLHelpPath(AValue: string);
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
function ShowNode(Node: THelpNode; var ErrMsg: string): TShowHelpResult; override;
procedure Assign(Source: TPersistent); override;
procedure Load(Storage: TConfigStorage); override;
procedure Save(Storage: TConfigStorage); override;
function GetLocalizedName: string; override;
property OnFindLHelp: TOnFindLHelp read FOnFindLHelp write FOnFindLHelp;
property Connection: TLHelpConnection read FConnection;
published
property LHelpPath: string read FLHelpPath write SetLHelpPath;
property AutoRegister;
end;
procedure Register;
implementation
{$R lazhelpchm.res}
procedure Register;
begin
RegisterComponents('System',[TCHMHelpDatabase,TLHelpConnector]);
end;
{ TLHelpConnector }
procedure TLHelpConnector.SetLHelpPath(AValue: string);
begin
if FLHelpPath=AValue then Exit;
FLHelpPath:=AValue;
end;
constructor TLHelpConnector.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
AddSupportedMimeType(CHMMimeType);
end;
destructor TLHelpConnector.Destroy;
begin
FConnection.Free;
inherited;
end;
function TLHelpConnector.ShowNode(Node: THelpNode; var ErrMsg: string
): TShowHelpResult;
var
Path: String;
IPCFile: String;
URLScheme: string;
URLPath: string;
URLParams: string;
CHMFilename: String;
SubPath: String;
Response: TLHelpResponse;
s: String;
begin
{$IFDEF VerboseChmHelp}
debugln(['TLHelpConnector.ShowNode START URL="',Node.URL,'"']);
{$ENDIF}
Result:=shrViewerError;
ErrMsg:='';
if (not Node.URLValid) then
begin
ErrMsg:='TLHelpConnector.ShowNode Node.URLValid=false';
exit;
end;
if (Node.URL='') then
begin
ErrMsg:='TLHelpConnector.ShowNode Node.URL empty';
exit;
end;
SplitURL(Node.URL,URLScheme,URLPath,URLParams);
CHMFilename:=CleanAndExpandFilename(URLPath);
if not FileExistsUTF8(CHMFilename) then
begin
ErrMsg:='chm file "'+CHMFilename+'" not found';
exit;
end;
if DirPathExists(CHMFilename) then
begin
ErrMsg:='invalid chm file "'+CHMFilename+'"';
exit;
end;
SubPath:='';
if (URLParams<>'') and (URLParams[1]='?') then
Delete(URLParams,1,1);
if LeftStr(URLParams,length(CHMPathParam)+1)=CHMPathParam+'=' then
begin
SubPath:=URLParams;
Delete(SubPath,1,length(CHMPathParam)+1);
end;
if Connection=nil then
begin
// create a connection to lhelp:
FConnection := TLHelpConnection.Create;
Connection.ProcessWhileWaiting := @Application.ProcessMessages;
end;
if Connection.ServerRunning = false then
begin
// Use '_lhlpctl_' in case application developer uses SimpleIPC
// and also uses the exe name followed by the process ID.
// See help protocol specs defined in
// http://wiki.lazarus.freepascal.org/Help_protocol
// Use process id in order to avoid conflicts when multiple entries are running
IPCFile:=LowerCase(ExtractFileName(Application.ExeName))+
'_lhlpctl_'+
copy(inttostr(GetProcessID)+'00000',1,5);
{$IFDEF Unix}
if FileExistsUTF8('/tmp/'+IPCFile) then
DeleteFileUTF8('/tmp/'+IPCFile);
{$ENDIF}
// get lhelp path
Path:=LHelpPath;
if Assigned(OnFindLHelp) then
OnFindLHelp(Path);
// append exe extension
if (ExtractFileExt(Path)='') and (GetExeExt<>'') then
Path:=Path+GetExeExt;
// search in Path
if (Path<>'') and (ExtractFilePath(Path)='') then
begin
s:=FindDefaultExecutablePath(Path);
if s<>'' then Path:=s;
end;
if not FileExistsUTF8(Path) then
begin
ErrMsg:='The chm viewer program lhelp was not found at "'+Path+'"';
exit;
end;
Connection.StartHelpServer(IPCFile,Path);
end;
{$IFDEF VerboseChmHelp}
debugln(['TLHelpConnector.ShowNode CHMFilename="',CHMFilename,'" SubPath="',SubPath,'"']);
{$ENDIF}
Response:=Connection.OpenURL(CHMFilename,SubPath);
case Response of
srSuccess: exit(shrSuccess);
srNoAnswer: ErrMsg:='lhelp does not respond';
srInvalidFile: ErrMsg:='lhelp can not open the file "'+CHMFilename+'"';
srInvalidURL,srInvalidContext: ErrMsg:='lhelp can not find the help entry "'+SubPath+'"';
else
ErrMsg:='Something is wrong with lhelp';
end;
debugln(['TLHelpConnector.ShowNode error: ',ErrMsg]);
end;
procedure TLHelpConnector.Assign(Source: TPersistent);
var
Src: TLHelpConnector;
begin
if Source is TLHelpConnector then
begin
Src:=TLHelpConnector(Source);
LHelpPath:=Src.LHelpPath;
end;
inherited Assign(Source);
end;
procedure TLHelpConnector.Load(Storage: TConfigStorage);
begin
inherited Load(Storage);
LHelpPath:=Storage.GetValue('LHelp/Path','');
end;
procedure TLHelpConnector.Save(Storage: TConfigStorage);
begin
inherited Save(Storage);
Storage.SetDeleteValue('LHelp/Path',LHelpPath,'');
end;
function TLHelpConnector.GetLocalizedName: string;
begin
Result:='LHelp Connector';
end;
{ TCHMHelpDatabase }
procedure TCHMHelpDatabase.SetFilename(AValue: string);
begin
if FFilename=AValue then Exit;
FFilename:=AValue;
end;
procedure TCHMHelpDatabase.SetKeywordPrefix(AValue: string);
begin
if FKeywordPrefix=AValue then Exit;
FKeywordPrefix:=AValue;
end;
constructor TCHMHelpDatabase.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
AddSupportedMimeType(CHMMimeType);
end;
destructor TCHMHelpDatabase.Destroy;
begin
FreeAndNil(FHelpNode);
inherited Destroy;
end;
function TCHMHelpDatabase.ShowHelp(Query: THelpQuery; BaseNode,
NewNode: THelpNode; QueryItem: THelpQueryItem; var ErrMsg: string
): TShowHelpResult;
begin
ErrMsg:='';
Result:=shrContextNotFound;
if NewNode.URLValid then
begin
Result:=ShowURL(NewNode.URL,NewNode.Title,ErrMsg);
end
else
begin
Result:=shrContextNotFound;
ErrMsg:='TCHMHelpDatabase.ShowHelp Node.URLValid=false Node.URL="'+NewNode.URL+'"';
end;
end;
function TCHMHelpDatabase.ShowURL(const URL, Title: string; var ErrMsg: string
): TShowHelpResult;
var
Viewer: THelpViewer;
Node: THelpNode;
begin
//DebugLn('TCHMHelpDatabase.ShowURL A URL="',URL,'" Title="',Title,'"');
if not FileExistsUTF8(Filename) then
begin
ErrMsg:='chm help file "'+Filename+'" not found';
exit(shrDatabaseNotFound);
end;
// find HTML viewer
Result:=FindViewer(CHMMimeType,ErrMsg,Viewer);
if Result<>shrSuccess then exit;
// call viewer
Node:=nil;
try
Node:=THelpNode.CreateURL(Self,Title,URL);
Result:=Viewer.ShowNode(Node,ErrMsg);
finally
Node.Free;
end;
end;
function TCHMHelpDatabase.GetNodesForKeyword(const HelpKeyword: string;
var ListOfNodes: THelpNodeQueryList; var ErrMsg: string): TShowHelpResult;
var
Path: String;
begin
Result:=inherited GetNodesForKeyword(HelpKeyword, ListOfNodes, ErrMsg);
if Result<>shrSuccess then exit;
if not (csDesigning in ComponentState) and
(KeywordPrefix<>'') and
(LeftStr(HelpKeyword,length(KeywordPrefix))=KeywordPrefix) then
begin
// HelpKeyword starts with KeywordPrefix -> add default node
if FHelpNode=nil then
FHelpNode:=THelpNode.CreateURL(Self,'','');
Path:=copy(HelpKeyword,length(KeywordPrefix)+1,length(HelpKeyword));
FHelpNode.Title:='Show page '+Path+' of '+ExtractFileName(Filename);
FHelpNode.URL:='chmfile://'+FilenameToURLPath(Filename)+'?'+CHMPathParam+'='+Path;
CreateNodeQueryListAndAdd(FHelpNode,nil,ListOfNodes,true);
end;
end;
procedure TCHMHelpDatabase.Load(Storage: TConfigStorage);
begin
inherited Load(Storage);
KeywordPrefix:=Storage.GetValue('KeywordPrefix','');
Filename:=Storage.GetValue('Filename','');
end;
procedure TCHMHelpDatabase.Save(Storage: TConfigStorage);
begin
inherited Save(Storage);
Storage.SetDeleteValue('KeywordPrefix',KeywordPrefix,'');
Storage.SetDeleteValue('Filename',Filename, '');
end;
end.
|