/usr/src/castle-game-engine-4.1.1/base/castleopendocument.pas is in castle-game-engine-src 4.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | {
Copyright 2012-2013 Michalis Kamburelis. and Lazarus developers.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
Parts of this file are based on Lazarus LCL code, which has
exactly the same license as our "Castle Game Engine":
LGPL with static linking exception, see COPYING.txt for details.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Opening files and URLs. }
unit CastleOpenDocument;
interface
resourcestring
SCannotOpenURL = 'Browser not found on your system.';
{ Open URL with the suitable application.
This detects and handles also local files (as filenames, or URLs with "file:"
protocol). }
function OpenURL(AURL: String): Boolean;
{ Open a local file or directory.
@deprecated You should instead use OpenURL,
that automatically detects local filenames and URLs leading to local filenames. }
function OpenDocument(APath: String): Boolean;
implementation
{ Copied and adapted from Lazarus LCL unit LCLIntf. The core of our engine
cannot depend on LCL. Fortunately, Lazarus
has the same license as our engine, so copying code is fine.
We did some changes to the code here:
- Removed references to UTF8 classes and functions.
Not that I don't like them, but they just have no place in a game engine.
With time, hopefully FPC will have nice solution for UTF8.
Also, with time, hopefully these functions may be moved to FPC FCL too.
- Using TProcess.Executable, TProcess.Parameters instead of
TProcess.CommandLine. This avoids the need for many paranoid quoting
previously present here.
- Some bits may use our CastleUtils, CastleFilesUtils functions.
So:
- FilenameIsAbsolute => IsPathAbsolute
- FileExistsUTF8 => FileExists
- CleanAndExpandFilename => ExpandFilename
- AppendPathDelim => InclPathDelim
- TrimFilename => no need to
- GetExeExt => ExeExtension
- GetEnvironmentVariableUTF8 => GetEnvironmentVariable
- FindFilenameOfCmd => PathFileSearch
- FindDefaultBrowser => simplified and folded inside OpenURL for Unix,
LCL implementation was cross-platform but was used only on
Unix (except Darwin) (for these OpenXxx routines).
- SearchFileInPath => PathFileSearch (it's only used by Unix)
}
uses
{$ifdef UNIX} BaseUnix, {$endif}
{$ifdef MSWINDOWS} Windows, {$endif}
{$ifdef DARWIN} MacOSAll, {$endif} CastleURIUtils,
SysUtils, Classes, Process, CastleUtils, CastleFilesUtils, CastleLog;
{ lcl/lclstrconsts.pas ------------------------------------------------------- }
resourcestring
lisProgramFileNotFound = 'program file not found %s';
lisCanNotExecute = 'can not execute %s';
{$ifdef Windows}
{ lcl/include/sysenvapis_win.inc --------------------------------------------- }
function OpenURL(AURL: String): Boolean;
var
{$IFDEF WinCE}
Info: SHELLEXECUTEINFO;
{$ELSE}
ws: WideString;
ans: AnsiString;
{$ENDIF}
begin
Result := False;
if AURL = '' then Exit;
{$IFDEF WinCE}
FillChar(Info, SizeOf(Info), 0);
Info.cbSize := SizeOf(Info);
Info.fMask := SEE_MASK_FLAG_NO_UI;
Info.lpVerb := 'open';
Info.lpFile := PWideChar(UTF8Decode(AURL));
Result := ShellExecuteEx(@Info);
{$ELSE}
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
ws := UTF8Decode(AURL);
Result := ShellExecuteW(0, 'open', PWideChar(ws), nil, nil, SW_SHOWNORMAL) > 32;
end
else
begin
ans := Utf8ToAnsi(AURL); // utf8 must be converted to Windows Ansi-codepage
Result := ShellExecute(0, 'open', PAnsiChar(ans), nil, nil, SW_SHOWNORMAL) > 32;
end;
{$ENDIF}
end;
// Open a document with the default application associated with it in the system
function OpenDocument(APath: String): Boolean;
begin
Result := OpenURL(APath);
end;
{$endif}
{$ifdef UNIX}
{ lcl/include/unixfileutil.inc ----------------------------------------------- }
function FileIsExecutable(const AFilename: string): boolean;
var
Info : Stat;
begin
// first check AFilename is not a directory and then check if executable
Result:= (FpStat(AFilename,info)<>-1) and FPS_ISREG(info.st_mode) and
(BaseUnix.FpAccess(AFilename,BaseUnix.X_OK)=0);
end;
{ lcl/utf8process.pp --------------------------------------------------------- }
{ Runs a short command which should point to an executable in
the environment PATH.
Kambi: simplified this to use TProcess.Executable, TProcess.Parameters
instead of TProcess.CommandLine (Lazarus TProcessUTF8 didn't have these
improvements). This removes the need for paranoid quoting of strings
everywhere.
This always takes exactly 1 parameter now --- which is actually Ok
for usage in this unit. }
procedure RunCmdFromPath(ProgramFilename, Parameter: string);
var
OldProgramFilename: String;
BrowserProcess: TProcess;
begin
OldProgramFilename:=ProgramFilename;
ProgramFilename:=PathFileSearch(ProgramFilename);
if ProgramFilename='' then
raise EFOpenError.Create(Format(lisProgramFileNotFound, [OldProgramFilename]));
if not FileIsExecutable(ProgramFilename) then
raise EFOpenError.Create(Format(lisCanNotExecute, [ProgramFilename]));
// run
BrowserProcess := TProcess.Create(nil);
try
{ Only FPC >= 2.6.0 has TProcess.Parameters, TProcess.Executable }
{$define USE_PROCESS_PARAMETERS}
{$ifdef VER2_2} {$undef USE_PROCESS_PARAMETERS} {$endif}
{$ifdef VER2_4} {$undef USE_PROCESS_PARAMETERS} {$endif}
{$ifdef USE_PROCESS_PARAMETERS}
BrowserProcess.Executable := ProgramFilename;
BrowserProcess.Parameters.Add(Parameter);
{$else}
BrowserProcess.CommandLine := AnsiQuotedStr(ProgramFilename, '"') +
' ' + AnsiQuotedStr(Parameter, '"');
{$endif}
if Log then
WritelnLog('Executing', 'Executable: "' + ProgramFilename +
'", Parameter: "' + Parameter + '"');
BrowserProcess.Execute;
finally
BrowserProcess.Free;
end;
end;
{$ifdef darwin}
{ lcl/include/sysenvapis_mac.inc --------------------------------------------- }
// Open a given URL with the default browser
function OpenURL(AURL: String): Boolean;
var
cf: CFStringRef;
url: CFURLRef;
FileName: string;
begin
if AURL = '' then
Exit(False);
{ If this is a local filename, open it using OpenDocument. }
FileName := URIToFilenameSafe(AURL);
if FileName <> '' then
Exit(OpenDocument(FileName));
cf := CFStringCreateWithCString(kCFAllocatorDefault, @AURL[1], kCFStringEncodingUTF8);
if not Assigned(cf) then
Exit(False);
url := CFURLCreateWithString(nil, cf, nil);
Result := LSOpenCFURLRef(url, nil) = 0;
CFRelease(url);
CFRelease(cf);
end;
// Open a document with the default application associated with it in the system
function OpenDocument(APath: String): Boolean;
begin
Result := True;
RunCmdFromPath('open',APath);
end;
{$else}
{ lcl/include/sysenvapis.inc ------------------------------------------------- }
function FindDefaultBrowser(out ABrowser: String): Boolean;
function Find(const ShortFilename: String; out ABrowser: String): Boolean; inline;
begin
ABrowser := PathFileSearch(ShortFilename + ExeExtension);
Result := ABrowser <> '';
end;
begin
// search in path. Prefer open source ;)
if Find('xdg-open', ABrowser) // Portland OSDL/FreeDesktop standard on Linux
or Find('sensible-browser', ABrowser) // Kambi: Debian-based systems
or Find('htmlview', ABrowser) // some redhat systems
or Find('firefox', ABrowser)
or Find('mozilla', ABrowser)
or Find('galeon', ABrowser)
or Find('konqueror', ABrowser)
or Find('safari', ABrowser)
or Find('netscape', ABrowser)
or Find('opera', ABrowser) then ;
Result := ABrowser <> '';
end;
{ lcl/include/sysenvapis_unix.inc -------------------------------------------- }
// Open a given URL with the default browser
function OpenURL(AURL: String): Boolean;
var
ABrowser, FileName: String;
begin
{ If this is a local filename, open it using OpenDocument. }
FileName := URIToFilenameSafe(AURL);
if FileName <> '' then
Exit(OpenDocument(FileName));
Result := FindDefaultBrowser(ABrowser) and FileExists(ABrowser) and FileIsExecutable(ABrowser);
if not Result then
Exit;
RunCmdFromPath(ABrowser, AURL);
end;
// Open a document with the default application associated with it in the system
function OpenDocument(APath: String): Boolean;
var
lApp: string;
begin
Result := True;
if not FileExists(APath) then exit(false);
lApp:=PathFileSearch('xdg-open'); // Portland OSDL/FreeDesktop standard on Linux
if lApp='' then
lApp:=PathFileSearch('kfmclient'); // KDE command
if lApp='' then
lApp:=PathFileSearch('gnome-open'); // GNOME command
if lApp='' then
Exit(False);
RunCmdFromPath(lApp,APath);
end;
{$endif}
{$endif}
end.
|