/usr/src/castle-game-engine-5.2.0/base/castlerecentfiles.pas is in castle-game-engine-src 5.2.0-2.
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 | {
Copyright 2006-2014 Michalis Kamburelis.
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.
"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.
----------------------------------------------------------------------------
}
{ Manage a list of recently open files (TRecentFiles). }
unit CastleRecentFiles;
interface
uses Classes, CastleXMLConfig;
type
TOnOpenRecent = procedure (const URL: string) of object;
{ Manage a list of recently open files.
This is designed as a base class, usable on it's own, but also
as a parent for classes that show this list inside a menu.
For Lazarus menu version, see TLazRecentFiles.
For TCastleWindowCustom menu version, see TCastleRecentFiles. }
TRecentFiles = class(TComponent)
private
FURLs: TStringList;
FMaxCount: Cardinal;
FOnOpenRecent: TOnOpenRecent;
{ Load and save recently opened files list to/from the Config file. }
procedure LoadFromConfig(const Config: TCastleConfig);
procedure SaveToConfig(const Config: TCastleConfig);
protected
{ Create and destroy menu (or anything else that mirrors URLs contents).
@groupBegin }
procedure MenuCreate; virtual;
procedure MenuDestroy; virtual;
{ @groupEnd }
public
const
DefaultMaxCount = 5;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{ Adds as the most recent file URL.
If MaybeStdIn, then we treat URL = '-' specially:
it's ignored. Use this if your program interprets '-' as "load file
from standard input", such files should not be added to recent files menu.
Note that we want to store only absolute URLs.
So this method will always call AbsoluteURI (which will eventually
call ExpandFileName on filename inside, and make sure it has appropriate
protocol) on the given URL. }
procedure Add(const URL: string; const MaybeStdIn: boolean = true); virtual;
{ List of currently stored URLs. @italic(This is readonly.) }
property URLs: TStringList read FURLs;
published
property OnOpenRecent: TOnOpenRecent read FOnOpenRecent write FOnOpenRecent;
property MaxCount: Cardinal read FMaxCount write FMaxCount
default DefaultMaxCount;
end;
implementation
uses SysUtils, CastleClassUtils, CastleConfig, CastleURIUtils;
constructor TRecentFiles.Create(AOwner: TComponent);
begin
inherited;
FURLs := TStringList.Create;
FMaxCount := DefaultMaxCount;
{ one day, make this optional, to also enable many TRecentFiles instances
in a program not overwriting each others' state. }
Config.OnLoad.Add(@LoadFromConfig);
Config.OnSave.Add(@SaveToConfig);
end;
destructor TRecentFiles.Destroy;
begin
if Config <> nil then
begin
Config.OnLoad.Remove(@LoadFromConfig);
Config.OnSave.Remove(@SaveToConfig);
end;
FreeAndNil(FURLs);
inherited;
end;
procedure TRecentFiles.MenuCreate;
begin
end;
procedure TRecentFiles.MenuDestroy;
begin
end;
procedure TRecentFiles.Add(const URL: string; const MaybeStdIn: boolean);
var
F: string;
Index: Integer;
begin
if MaybeStdIn and (URL = '-') then Exit;
F := AbsoluteURI(URL);
{ We calculate Index, because if user opens a file already on the "recent files"
list then we want to just move this URL to the top. }
Index := URLs.IndexOf(F);
if Index = 0 then
Exit { user reopens last opened file, nothing to do };
if Index <> -1 then
{ Just move Index to the beginning }
URLs.Exchange(Index, 0) else
begin
URLs.Insert(0, F);
Strings_Trim(URLs, MaxCount);
end;
MenuDestroy;
MenuCreate;
end;
const
{ Should not contain final "/" --- it will be added automatically. }
Path = 'recent_files';
procedure TRecentFiles.LoadFromConfig(const Config: TCastleConfig);
var
I, C: Integer;
S: string;
begin
URLs.Clear;
C := Config.GetValue(Path + '/count', 0);
for I := 0 to C - 1 do
begin
S := Config.GetValue(Path + '/item' + IntToStr(I) + '/url', '');
if S <> '' then
URLs.Append(S);
end;
MenuDestroy;
MenuCreate;
end;
procedure TRecentFiles.SaveToConfig(const Config: TCastleConfig);
var
I: Integer;
begin
Config.SetDeleteValue(Path + '/count', URLs.Count, 0);
for I := 0 to URLs.Count - 1 do
Config.SetDeleteValue(Path + '/item' + IntToStr(I) + '/url', URLs[I], '');
end;
end.
|