ADD: Viewer - allow quick view using internal associations with '{!DC-VIEWER}' command

(cherry picked from commit dec24438eb)
This commit is contained in:
Alexander Koblov 2023-10-21 00:20:07 +03:00
commit 2ecbdcd461
2 changed files with 84 additions and 28 deletions

View file

@ -51,6 +51,7 @@ type
function handleLinksToLocal( const Sender:TFileView; const aFile:TFile; var fullPath:String; var showMsg:String ): Boolean;
function handleNotDirect( const Sender:TFileView; const aFile:TFile; var fullPath:String; var showMsg:String ): Boolean;
function handleDirect( const Sender:TFileView; const aFile:TFile; var fullPath:String; var showMsg:String ): Boolean;
procedure PrepareView(const aFile: TFile; var FileName: String);
protected
procedure DoOnShowHint(HintInfo: PHintInfo) override;
public
@ -67,8 +68,9 @@ var
implementation
uses
LCLProc, Forms, fMain, uTempFileSystemFileSource, uLng,
uFileSourceProperty, uFileSourceOperation, uFileSourceOperationTypes;
LCLProc, Forms, DCOSUtils, fMain, uTempFileSystemFileSource, uLng,
uFileSourceProperty, uFileSourceOperation, uFileSourceOperationTypes,
uGlobs, uShellExecute;
procedure QuickViewShow(aFileViewPage: TFileViewPage; aFileView: TFileView);
var
@ -188,7 +190,9 @@ begin
end;
end;
if not fullPath.IsEmpty() then begin
if not fullPath.IsEmpty() then
begin
PrepareView(aFile, fullPath);
LoadFile( fullPath );
end else begin
FViewer.Hide;
@ -292,5 +296,44 @@ begin
end;
end;
procedure TQuickViewPanel.PrepareView(const aFile: TFile; var FileName: String);
var
sCmd: string = '';
sParams: string = '';
sStartPath: string = '';
iStart, iFinish: Integer;
bTerm, bKeepTerminalOpen: Boolean;
bAbortOperationFlag: Boolean = False;
bShowCommandLinePriorToExecute: Boolean = False;
begin
// Try to find 'view' command in the internal associations
if gExts.GetExtActionCmd(aFile, 'view', sCmd, sParams, sStartPath) then
begin
// Internal viewer command
if sCmd = '{!DC-VIEWER}' then
begin
aFile.FullPath:= FileName;
sCmd:= PrepareParameter(sCmd, AFile, [ppoReplaceTilde]);
sParams:= PrepareParameter(sParams, AFile, [], @bShowCommandLinePriorToExecute, @bTerm, @bKeepTerminalOpen, @bAbortOperationFlag);
if not bAbortOperationFlag then
begin
iStart:= Pos('<?', sParams);
iFinish:= Pos('?>', sParams, iStart + 2);
if (iStart > 0) and (iFinish > iStart) then
begin
if FFileSource is TTempFileSystemFileSource then
sStartPath:= FFileSource.GetRootDir
else begin
sStartPath:= EmptyStr;
end;
PrepareOutput(sParams, sStartPath);
if mbFileExists(sParams) then FileName:= sParams;
end;
end;
end;
end;
end;
end.

View file

@ -3,7 +3,7 @@
-------------------------------------------------------------------------
This unit contains some functions for open files in associated applications.
Copyright (C) 2006-2019 Alexander Koblov (alexx2000@mail.ru)
Copyright (C) 2006-2023 Alexander Koblov (alexx2000@mail.ru)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -35,6 +35,8 @@ type
TPrepareParameterOption = (ppoNormalizePathDelims, ppoReplaceTilde);
TPrepareParameterOptions = set of TPrepareParameterOption;
procedure PrepareOutput(var sParams: String; const ATemp: String = '');
function PrepareParameter(sParam: string; paramFile: TFile = nil; options: TPrepareParameterOptions = []; pbShowCommandLinePriorToExecute: PBoolean = nil; pbRunInTerminal: PBoolean = nil; pbKeepTerminalOpen: PBoolean = nil; pbAbortOperation: PBoolean = nil): string; overload;
{en
@ -935,6 +937,40 @@ begin
end;
end;
procedure PrepareOutput(var sParams: String; const ATemp: String);
var
Process: TProcessUTF8;
iStart, iCount: Integer;
sTmpFile, sShellCmdLine: String;
iLastConsoleCommandPos: Integer = 0;
begin
repeat
iStart := Posex('<?', sParams, (iLastConsoleCommandPos + 1)) + 2;
iCount := Posex('?>', sParams, iStart) - iStart;
if (iStart <> 0) and (iCount >= 0) then
begin
if Length(ATemp) > 0 then
sTmpFile:= ATemp
else begin
sTmpFile:= GetTempFolderDeletableAtTheEnd;
end;
sTmpFile := GetTempName(sTmpFile);
sShellCmdLine := Copy(sParams, iStart, iCount) + ' > ' + QuoteStr(sTmpFile);
Process := TProcessUTF8.Create(nil);
try
Process.CommandLine := FormatShell(sShellCmdLine);
Process.Options := [poWaitOnExit];
Process.ShowWindow := swoHide;
Process.Execute;
finally
Process.Free;
end;
sParams := Copy(sParams, 1, iStart - 3) + sTmpFile + Copy(sParams, iStart + iCount + 2, MaxInt);
iLastConsoleCommandPos := iStart;
end;
until ((iStart = 0) or (iCount < 0));
end;
{ PrepareParameter }
function PrepareParameter(sParam: string; paramFile: TFile; options: TPrepareParameterOptions; pbShowCommandLinePriorToExecute: PBoolean; pbRunInTerminal: PBoolean; pbKeepTerminalOpen: PBoolean; pbAbortOperation: PBoolean = nil): string; overload;
begin
@ -956,10 +992,6 @@ end;
{ ProcessExtCommandFork }
function ProcessExtCommandFork(sCmd, sParams, sWorkPath: string; paramFile: TFile; bTerm: boolean; bKeepTerminalOpen: boolean): boolean;
var
sTmpFile, sShellCmdLine: string;
iStart, iCount: integer;
iLastConsoleCommandPos: integer = 0;
Process: TProcessUTF8;
sl: TStringList;
bShowCommandLinePriorToExecute: boolean = False;
bAbortOperationFlag: boolean = false;
@ -986,26 +1018,7 @@ begin
// For example:
// {!VIEWER} <?rpm -qivlp --scripts %p?>
// Show in Viewer information about RPM package
repeat
iStart := Posex('<?', sParams, (iLastConsoleCommandPos + 1)) + 2;
iCount := Posex('?>', sParams, iStart) - iStart;
if (iStart <> 0) and (iCount >= 0) then
begin
sTmpFile := GetTempName(GetTempFolderDeletableAtTheEnd);
sShellCmdLine := Copy(sParams, iStart, iCount) + ' > ' + QuoteStr(sTmpFile);
Process := TProcessUTF8.Create(nil);
try
Process.CommandLine := FormatShell(sShellCmdLine);
Process.Options := [poWaitOnExit];
Process.ShowWindow := swoHide;
Process.Execute;
finally
Process.Free;
end;
sParams := Copy(sParams, 1, iStart - 3) + sTmpFile + Copy(sParams, iStart + iCount + 2, MaxInt);
iLastConsoleCommandPos := iStart;
end;
until ((iStart = 0) or (iCount < 0));
PrepareOutput(sParams);
//4. If user user wanted to execute an internal command, let's do it.
if frmMain.Commands.Commands.ExecuteCommand(sCmd, [sParams]) = cfrSuccess then