Debian Codegraph @

src/files/castlerecentfiles.pas

raw
1 {
2 Copyright 2006-2018 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Manage a list of recently open files (TRecentFiles). }
17 unit CastleRecentFiles;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses Classes, CastleXMLConfig;
24
25 type
26 TOnOpenRecent = procedure (const Url: String) of object;
27
28 { Manage a list of recently open files.
29
30 This is designed as a base class, usable on it's own, but also
31 as a parent for classes that show this list inside a menu.
32 For Lazarus menu version, see TLazRecentFiles.
33 For TCastleWindow menu version, see TCastleRecentFiles. }
34 TRecentFiles = class(TComponent)
35 private
36 FUrls: TStringList;
37 FMaxCount: Cardinal;
38 FOnOpenRecent: TOnOpenRecent;
39 protected
40 { Create and destroy menu (or anything else that mirrors Urls contents).
41 @groupBegin }
42 procedure MenuCreate; virtual;
43 procedure MenuDestroy; virtual;
44 { @groupEnd }
45 public
46 const
47 DefaultMaxCount = 5;
48
49 constructor Create(AOwner: TComponent); override;
50 destructor Destroy; override;
51
52 { Adds as the most recent file Url.
53
54 Note that we want to store only absolute URLs.
55 So this method will always call AbsoluteURI (which will eventually
56 call ExpandFileName on filename inside, and make sure it has appropriate
57 protocol) on the given URL. }
58 procedure Add(const Url: String); overload; virtual;
59
60 procedure Add(const Url: String; const Ignored: Boolean); overload; deprecated 'use Add(Url)';
61
62 { Remove URL. }
63 procedure Remove(const Url: String);
64
65 { List of currently stored URLs. @italic(This is readonly.) }
66 property Urls: TStringList read FUrls;
67
68 { Load and save recently opened files list to/from the Config file.
69 @groupBegin }
70 procedure LoadFromConfig(const Config: TCastleConfig);
71 procedure SaveToConfig(const Config: TCastleConfig);
72 { @groupEnd }
73 published
74 property OnOpenRecent: TOnOpenRecent read FOnOpenRecent write FOnOpenRecent;
75
76 property MaxCount: Cardinal read FMaxCount write FMaxCount
77 default DefaultMaxCount;
78 end;
79
80 implementation
81
82 uses SysUtils, CastleClassUtils, CastleUriUtils;
83
84 constructor TRecentFiles.Create(AOwner: TComponent);
85 begin
86 inherited;
87 FUrls := TStringList.Create;
88 FMaxCount := DefaultMaxCount;
89
90 // automatic loading/saving is more troublesome than it's worth
91 // Config.AddLoadListener(@LoadFromConfig);
92 // Config.AddSaveListener(@SaveToConfig);
93 end;
94
95 destructor TRecentFiles.Destroy;
96 begin
97 // automatic loading/saving is more troublesome than it's worth
98 // if Config <> nil then
99 // begin
100 // Config.RemoveLoadListener(@LoadFromConfig);
101 // Config.RemoveSaveListener(@SaveToConfig);
102 // end;
103
104 FreeAndNil(FUrls);
105 inherited;
106 end;
107
108 procedure TRecentFiles.MenuCreate;
109 begin
110 end;
111
112 procedure TRecentFiles.MenuDestroy;
113 begin
114 end;
115
116 procedure TRecentFiles.Add(const Url: String);
117 var
118 F: string;
119 Index: Integer;
120 begin
121 F := AbsoluteURI(Url);
122
123 { We calculate Index, because if user opens a file already on the "recent files"
124 list then we want to just move this URL to the top. }
125 Index := Urls.IndexOf(F);
126
127 if Index = 0 then
128 Exit { user reopens last opened file, nothing to do };
129
130 if Index <> -1 then
131 begin
132 { Just move Index to the beginning }
133 Urls.Delete(Index);
134 Urls.Insert(0, F);
135 end else
136 begin
137 Urls.Insert(0, F);
138 Strings_Trim(Urls, MaxCount);
139 end;
140
141 MenuDestroy;
142 MenuCreate;
143 end;
144
145 procedure TRecentFiles.Add(const Url: String; const Ignored: Boolean);
146 begin
147 Add(Url);
148 end;
149
150 procedure TRecentFiles.Remove(const Url: String);
151 var
152 F: string;
153 Index: Integer;
154 begin
155 F := AbsoluteURI(Url);
156
157 { We calculate Index, because if user opens a file already on the "recent files"
158 list then we want to just move this URL to the top. }
159 Index := Urls.IndexOf(F);
160
161 if Index <> -1 then
162 begin
163 Urls.Delete(Index);
164 MenuDestroy;
165 MenuCreate;
166 end;
167 end;
168
169 const
170 { Should not contain final "/" --- it will be added automatically. }
171 Path = 'recent_files';
172
173 procedure TRecentFiles.LoadFromConfig(const Config: TCastleConfig);
174 var
175 I, C: Integer;
176 S: string;
177 begin
178 Urls.Clear;
179 C := Config.GetValue(Path + '/count', 0);
180 for I := 0 to C - 1 do
181 begin
182 S := Config.GetValue(Path + '/item' + IntToStr(I) + '/url', '');
183 if S <> '' then
184 Urls.Append(S);
185 end;
186
187 MenuDestroy;
188 MenuCreate;
189 end;
190
191 procedure TRecentFiles.SaveToConfig(const Config: TCastleConfig);
192 var
193 I: Integer;
194 begin
195 Config.SetDeleteValue(Path + '/count', Urls.Count, 0);
196 for I := 0 to Urls.Count - 1 do
197 Config.SetDeleteValue(Path + '/item' + IntToStr(I) + '/url', Urls[I], '');
198 end;
199
200 end.