5059
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2019 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
|
16 |
Object subclass:#PersistentFileHistory
|
|
17 |
instanceVariableNames:'fileHistory fileHistorySizeLimit topDirectoryName applicationKey
|
|
18 |
topRegistryKeyName'
|
|
19 |
classVariableNames:''
|
|
20 |
poolDictionaries:''
|
|
21 |
category:'Interface-Framework'
|
|
22 |
!
|
|
23 |
|
|
24 |
PersistentFileHistory class instanceVariableNames:'fileHistory'
|
|
25 |
|
|
26 |
"
|
|
27 |
No other class instance variables are inherited by this class.
|
|
28 |
"
|
|
29 |
!
|
|
30 |
|
|
31 |
!PersistentFileHistory class methodsFor:'documentation'!
|
|
32 |
|
|
33 |
copyright
|
|
34 |
"
|
|
35 |
COPYRIGHT (c) 2019 by Claus Gittinger
|
|
36 |
All Rights Reserved
|
|
37 |
|
|
38 |
This software is furnished under a license and may be used
|
|
39 |
only in accordance with the terms of that license and with the
|
|
40 |
inclusion of the above copyright notice. This software may not
|
|
41 |
be provided or otherwise made available to, or used by, any
|
|
42 |
other person. No title to or ownership of the software is
|
|
43 |
hereby transferred.
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
|
47 |
documentation
|
|
48 |
"
|
|
49 |
Support code tp provide a common access layer for persistent storage for applications.
|
|
50 |
For example, a persistent history of previously opened files.
|
|
51 |
On Windows systems, this is stored in the registry.
|
|
52 |
On Unix systems, it is stored in the user's home folder under '.smalltalk/registry/...'.
|
|
53 |
Both registry and directory use the same hierarchical structure.
|
|
54 |
|
|
55 |
PersistentFileHistory new
|
|
56 |
applicationKey:'GDBApplication';
|
|
57 |
fileHistory
|
|
58 |
|
|
59 |
[author:]
|
|
60 |
Claus Gittinger
|
|
61 |
"
|
|
62 |
! !
|
|
63 |
|
|
64 |
!PersistentFileHistory class methodsFor:'instance creation'!
|
|
65 |
|
|
66 |
new
|
|
67 |
"return an initialized instance"
|
|
68 |
|
|
69 |
^ self basicNew initialize.
|
|
70 |
! !
|
|
71 |
|
|
72 |
!PersistentFileHistory methodsFor:'accessing - configuration'!
|
|
73 |
|
|
74 |
applicationKey:aString
|
|
75 |
"this is used as the key under which the application's values are stored.
|
|
76 |
In the registry, this is the last key above the history entry);
|
|
77 |
if files are used, this is the folder name above which history, and other files are stored.
|
|
78 |
By default, the name of the application without prefix is used."
|
|
79 |
|
|
80 |
applicationKey := aString.
|
|
81 |
|
|
82 |
"Modified (comment): / 23-07-2019 / 17:33:49 / Claus Gittinger"
|
|
83 |
!
|
|
84 |
|
|
85 |
fileHistorySize
|
|
86 |
"defines the number of remembered files in the history"
|
|
87 |
|
|
88 |
^ fileHistorySizeLimit ? 20.
|
|
89 |
|
|
90 |
"Modified: / 23-07-2019 / 17:24:39 / Claus Gittinger"
|
|
91 |
!
|
|
92 |
|
|
93 |
fileHistorySizeLimit:something
|
|
94 |
fileHistorySizeLimit := something.
|
|
95 |
!
|
|
96 |
|
|
97 |
topDirectoryName:topKeyName
|
|
98 |
"defaults to '.smalltalk',
|
|
99 |
but can be changed to '.expecco'."
|
|
100 |
|
|
101 |
topDirectoryName := topKeyName.
|
|
102 |
|
|
103 |
"Created: / 23-07-2019 / 17:22:16 / Claus Gittinger"
|
|
104 |
!
|
|
105 |
|
|
106 |
topRegistryKeyName:topKeyName
|
|
107 |
"defaults to 'SmalltalkX',
|
|
108 |
but can be changed to 'Expecco'."
|
|
109 |
|
|
110 |
topRegistryKeyName := topKeyName.
|
|
111 |
|
|
112 |
"Created: / 23-07-2019 / 17:25:27 / Claus Gittinger"
|
|
113 |
! !
|
|
114 |
|
|
115 |
!PersistentFileHistory methodsFor:'accessing - file history'!
|
|
116 |
|
|
117 |
addToFileHistory:aFilename
|
|
118 |
"remember a filename in the file history"
|
|
119 |
|
|
120 |
|fileHistory|
|
|
121 |
|
|
122 |
fileHistory := self fileHistory.
|
|
123 |
fileHistory synchronized:[
|
|
124 |
fileHistory addFirst:aFilename asFilename.
|
|
125 |
fileHistory size > self fileHistorySize ifTrue:[
|
|
126 |
fileHistory removeLast.
|
|
127 |
].
|
|
128 |
self makeFileHistoryPersistent
|
|
129 |
]
|
|
130 |
|
|
131 |
"Modified: / 09-11-2010 / 16:07:14 / cg"
|
|
132 |
"Modified (comment): / 29-10-2017 / 10:51:11 / cg"
|
|
133 |
"Modified (comment): / 23-07-2019 / 17:47:17 / Claus Gittinger"
|
|
134 |
!
|
|
135 |
|
|
136 |
fileHistory
|
|
137 |
"return the file history"
|
|
138 |
|
|
139 |
fileHistory isNil ifTrue:[
|
|
140 |
self initializeFileHistory
|
|
141 |
].
|
|
142 |
^ fileHistory
|
|
143 |
|
|
144 |
"Modified (comment): / 23-07-2019 / 17:47:06 / Claus Gittinger"
|
|
145 |
!
|
|
146 |
|
|
147 |
fileHistoryFilteredForStillExistingFilesDo:aBlock removeNonExisting:aBoolean
|
|
148 |
|history toRemove|
|
|
149 |
|
|
150 |
toRemove := OrderedCollection new.
|
|
151 |
|
|
152 |
history := self fileHistory.
|
|
153 |
history notEmptyOrNil ifTrue:[
|
|
154 |
history synchronized:[
|
|
155 |
history do:[:aFilename|
|
|
156 |
aFilename exists ifTrue:[
|
|
157 |
aBlock value:aFilename.
|
|
158 |
] ifFalse:[
|
|
159 |
aBoolean ifTrue:[
|
|
160 |
toRemove add:aFilename.
|
|
161 |
]
|
|
162 |
]
|
|
163 |
].
|
|
164 |
aBoolean ifTrue:[
|
|
165 |
"/ remove all non-existing history entries
|
|
166 |
fileHistory removeAll:toRemove.
|
|
167 |
].
|
|
168 |
].
|
|
169 |
].
|
|
170 |
|
|
171 |
"Created: / 29-10-2017 / 10:55:46 / cg"
|
|
172 |
"Modified: / 23-07-2019 / 17:49:34 / Claus Gittinger"
|
|
173 |
! !
|
|
174 |
|
|
175 |
!PersistentFileHistory methodsFor:'file history support'!
|
|
176 |
|
|
177 |
registryKeyForApplication
|
|
178 |
"HKEY_CURRENT_USER\Software\Exept\SmalltalkX\<appname>"
|
|
179 |
|
|
180 |
|k_software k_exept k_stx k_app keyName|
|
|
181 |
|
|
182 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
183 |
keyName := applicationKey.
|
|
184 |
|
|
185 |
k_software := Win32OperatingSystem registryEntry key:'HKEY_CURRENT_USER\Software\'.
|
|
186 |
(k_exept := k_software subKeyNamed:'Exept') isNil ifTrue:[
|
|
187 |
k_exept := k_software createSubKeyNamed:'Exept'
|
|
188 |
].
|
|
189 |
(k_stx := k_exept subKeyNamed:topRegistryKeyName) isNil ifTrue:[
|
|
190 |
k_stx := k_exept createSubKeyNamed:topRegistryKeyName
|
|
191 |
].
|
|
192 |
(k_app := k_stx subKeyNamed:keyName) isNil ifTrue:[
|
|
193 |
k_app := k_stx createSubKeyNamed:keyName
|
|
194 |
].
|
|
195 |
^ k_app
|
|
196 |
].
|
|
197 |
|
|
198 |
^ nil
|
|
199 |
|
|
200 |
"
|
|
201 |
self registryKeyForApplication
|
|
202 |
"
|
|
203 |
|
|
204 |
"Created: / 11-01-2011 / 19:58:26 / cg"
|
|
205 |
"Modified: / 24-07-2019 / 08:30:34 / Claus Gittinger"
|
|
206 |
!
|
|
207 |
|
|
208 |
registryKeyForFileHistory
|
|
209 |
"HKEY_CURRENT_USER\Software\Exept\SmalltalkX\<appName>\History"
|
|
210 |
|
|
211 |
|k_app k_history|
|
|
212 |
|
|
213 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
214 |
k_app := self registryKeyForApplication.
|
|
215 |
(k_history := k_app subKeyNamed:'History') isNil ifTrue:[
|
|
216 |
k_history := k_app createSubKeyNamed:'History'
|
|
217 |
].
|
|
218 |
^ k_history
|
|
219 |
].
|
|
220 |
|
|
221 |
^ nil
|
|
222 |
|
|
223 |
"
|
|
224 |
self registryKeyForFileHistory
|
|
225 |
"
|
|
226 |
! !
|
|
227 |
|
|
228 |
!PersistentFileHistory methodsFor:'initialization'!
|
|
229 |
|
|
230 |
initialize
|
|
231 |
"can be set to '.expecco'"
|
|
232 |
topDirectoryName := '.smalltalk'.
|
|
233 |
topRegistryKeyName := 'SmalltalkX'.
|
|
234 |
fileHistorySizeLimit := 25.
|
|
235 |
|
|
236 |
"Created: / 23-07-2019 / 17:21:27 / Claus Gittinger"
|
|
237 |
! !
|
|
238 |
|
|
239 |
!PersistentFileHistory methodsFor:'private'!
|
|
240 |
|
|
241 |
directoryForFileHistory
|
|
242 |
"the filename where the history is made persistent.
|
|
243 |
This depends on the owningApplication which should not be nil."
|
|
244 |
|
|
245 |
^ Filename homeDirectory / topDirectoryName / applicationKey
|
|
246 |
|
|
247 |
"
|
|
248 |
self new directoryForFileHistory
|
|
249 |
"
|
|
250 |
|
|
251 |
"Modified (comment): / 29-10-2017 / 10:51:29 / cg"
|
|
252 |
"Modified: / 23-07-2019 / 17:45:22 / Claus Gittinger"
|
|
253 |
!
|
|
254 |
|
|
255 |
fetchFileHistoryFromPersistentStore
|
|
256 |
|k index value dir fn|
|
|
257 |
|
|
258 |
fileHistory := OrderedSet new.
|
|
259 |
|
|
260 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
261 |
k := self registryKeyForFileHistory.
|
|
262 |
index := 1.
|
|
263 |
[
|
|
264 |
value := k valueNamed:(index printString).
|
|
265 |
value notNil
|
|
266 |
] whileTrue:[
|
|
267 |
fileHistory add:value asFilename.
|
|
268 |
index := index + 1
|
|
269 |
].
|
|
270 |
k close.
|
|
271 |
] ifFalse:[
|
|
272 |
dir := self directoryForFileHistory.
|
|
273 |
dir isDirectory ifTrue:[
|
|
274 |
fn := dir / 'history'.
|
|
275 |
fn exists ifTrue:[
|
|
276 |
fileHistory addAll:(fn contents collect:[:s | s asFilename])
|
|
277 |
]
|
|
278 |
]
|
|
279 |
]
|
|
280 |
|
|
281 |
"Modified: / 16-05-2019 / 19:05:49 / Stefan Vogel"
|
|
282 |
"Modified: / 23-07-2019 / 17:20:08 / Claus Gittinger"
|
|
283 |
!
|
|
284 |
|
|
285 |
initializeFileHistory
|
|
286 |
fileHistory := OrderedSet new.
|
|
287 |
self fetchFileHistoryFromPersistentStore.
|
|
288 |
!
|
|
289 |
|
|
290 |
makeFileHistoryPersistent
|
|
291 |
|k index dir fn|
|
|
292 |
|
|
293 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
294 |
k := self registryKeyForFileHistory.
|
|
295 |
fileHistory doWithIndex:[:filePath :index |
|
|
296 |
k valueNamed:(index printString) put:filePath asFilename pathName.
|
|
297 |
].
|
|
298 |
|
|
299 |
"/ remove the remaining keys
|
|
300 |
index := fileHistory size + 1.
|
|
301 |
[
|
|
302 |
k deleteValueNamed:(index printString)
|
|
303 |
] whileTrue:[
|
|
304 |
index := index + 1
|
|
305 |
].
|
|
306 |
] ifFalse:[
|
|
307 |
(dir := self directoryForFileHistory) exists ifFalse:[
|
|
308 |
dir recursiveMakeDirectory
|
|
309 |
].
|
|
310 |
fn := dir / 'history'.
|
|
311 |
fn contents:(fileHistory collect:[:fn | fn pathName]).
|
|
312 |
]
|
|
313 |
! !
|
|
314 |
|
|
315 |
!PersistentFileHistory class methodsFor:'documentation'!
|
|
316 |
|
|
317 |
version_CVS
|
|
318 |
^ '$Header$'
|
|
319 |
! !
|
|
320 |
|