| 1 |
{%MainUnit castleopendocument.pas} |
| 2 |
{ |
| 3 |
Copyright 2012-2024 Michalis Kamburelis and Lazarus developers. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
Parts of this file are based on Lazarus LCL code, which has |
| 10 |
exactly the same license as our "Castle Game Engine": |
| 11 |
LGPL with static linking exception, see COPYING.txt for details. |
| 12 |
|
| 13 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
|
| 17 |
---------------------------------------------------------------------------- |
| 18 |
} |
| 19 |
|
| 20 |
{ OpenDocument and OpenUrl implementations for desktops following |
| 21 |
XDG https://www.freedesktop.org/wiki/ , in practice: |
| 22 |
desktop Unix like Linux, FreeBSD. } |
| 23 |
|
| 24 |
// Relies on RunCmdFromPath |
| 25 |
{$I castleopendocument_process.inc} |
| 26 |
|
| 27 |
{$ifdef read_implementation} |
| 28 |
|
| 29 |
{ Based on Lazarus LCL: |
| 30 |
lcl/include/sysenvapis.inc |
| 31 |
lcl/include/sysenvapis_unix.inc } |
| 32 |
|
| 33 |
function FindDefaultBrowser(out ABrowser: String): Boolean; |
| 34 |
|
| 35 |
function Find(const ShortFilename: String; out ABrowser: String): Boolean; {$ifdef SUPPORTS_INLINE} inline; {$endif} |
| 36 |
begin |
| 37 |
ABrowser := PathFileSearch(ShortFilename + ExeExtension); |
| 38 |
Result := ABrowser <> ''; |
| 39 |
end; |
| 40 |
|
| 41 |
begin |
| 42 |
// search in path. Prefer open source ;) |
| 43 |
if Find('xdg-open', ABrowser) // Portland OSDL/FreeDesktop standard on Linux |
| 44 |
or Find('sensible-browser', ABrowser) // Kambi: Debian-based systems |
| 45 |
or Find('htmlview', ABrowser) // some redhat systems |
| 46 |
or Find('firefox', ABrowser) |
| 47 |
or Find('mozilla', ABrowser) |
| 48 |
or Find('galeon', ABrowser) |
| 49 |
or Find('konqueror', ABrowser) |
| 50 |
or Find('safari', ABrowser) |
| 51 |
or Find('netscape', ABrowser) |
| 52 |
or Find('opera', ABrowser) then ; |
| 53 |
Result := ABrowser <> ''; |
| 54 |
end; |
| 55 |
|
| 56 |
// Open a given Url with the default browser |
| 57 |
function OpenUrl(AUrl: String): Boolean; |
| 58 |
var |
| 59 |
ABrowser, FileName: String; |
| 60 |
begin |
| 61 |
{ If this is a local filename, open it using OpenDocument. } |
| 62 |
if not UrlHasAnchor(AUrl) then |
| 63 |
begin |
| 64 |
FileName := UriToFilenameSafe(AUrl); |
| 65 |
if FileName <> '' then |
| 66 |
Exit(OpenDocument(FileName)); |
| 67 |
end; |
| 68 |
|
| 69 |
Result := FindDefaultBrowser(ABrowser) and FileExists(ABrowser) and FileIsExecutable(ABrowser); |
| 70 |
if not Result then |
| 71 |
Exit; |
| 72 |
RunCmdFromPath(ABrowser, AUrl); |
| 73 |
end; |
| 74 |
|
| 75 |
// Open a document with the default application associated with it in the system |
| 76 |
function OpenDocument(APath: String): Boolean; |
| 77 |
var |
| 78 |
lApp: string; |
| 79 |
begin |
| 80 |
Result := True; |
| 81 |
if not (FileExists(APath) or DirectoryExists(APath)) then |
| 82 |
Exit(false); |
| 83 |
|
| 84 |
lApp:=PathFileSearch('xdg-open'); // Portland OSDL/FreeDesktop standard on Linux |
| 85 |
if lApp='' then |
| 86 |
lApp:=PathFileSearch('kfmclient'); // KDE command |
| 87 |
if lApp='' then |
| 88 |
lApp:=PathFileSearch('gnome-open'); // GNOME command |
| 89 |
if lApp='' then |
| 90 |
Exit(False); |
| 91 |
|
| 92 |
RunCmdFromPath(lApp,APath); |
| 93 |
end; |
| 94 |
|
| 95 |
{$endif read_implementation} |