-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNotesRememberEditorState.pas
210 lines (188 loc) · 6.09 KB
/
NotesRememberEditorState.pas
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
//
// NotesRememberEditorState - classes para lembrar o estado do editor para
// um arquivo e para limpar os dados do cache
//
// Notes, https://github.com/jonasraoni/notes
// Copyright (C) 2003-2004, Equipe do Notes.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
(*
@abstract(NotesRememberEditorState - classes para lembrar o estado do editor para um arquivo e para limpar os dados do cache.)
@author(Anderson R. Barbieri <notesnr@ig.com.br>)
*)
unit NotesRememberEditorState;
interface
uses SysUtils, Classes, SynEdit, NotesEditorTab;
type
// Salva/Carrega o estado do editor dependendo do arquivo que está aberto nele
TNotesRemeberEditorState = class(TObject)
private
fEdTab: TNotesEditorTab;
public
constructor Create(const EditorTab: TNotesEditorTab);
// Carrega o estado
procedure LoadState;
// Salva o estado
procedure SaveState;
end;
type
// Limpa os dados para arquivos abertos a mais de "DaysOld"
// Passe True no método create para poder modificar as propriedades
// e depois chame o método Execute. Depois que Execute for chamado
// a ação será feita em uma Threaad em background.
TNotesEditorStateCleanUp = class(TThread)
private
fDaysOld: integer;
fDir: string;
fExt: string;
public
// Limpa os dados antigos
procedure Execute; override;
// número máximo de dias que os dados devem ter
property DaysOld: integer read fDaysOld write fDaysOld;
// pasta onde os dados estão gravados
property EditorStateDir: string read fDir write fDir;
// Extensão dos arquivos usados para guardar o estado do editor
property EditorStateExt: string read fExt write fExt;
end;
// Retorna o nome do arquivo que contém o estado do editor para um arquivo,
// mesmo que um arquivo de estado ainda não existe
function getStateFileForFile(const FileName: string): string;
implementation
uses NotesGlobals;
const
NOTES_EDITOR_STATE_SIGLEN = 3;
NOTES_EDITOR_STATE_SINATURE: string[NOTES_EDITOR_STATE_SIGLEN] = 'NES';
function getStateFileForFile(const FileName: string): string;
Var
I: integer;
begin
Result:= StringReplace(FileName, '/', '.', [rfReplaceall]);
Result:= StringReplace(Result, ':\', '.', [rfReplaceall]);
Result:= StringReplace(Result, ':/', '.', [rfReplaceall]);
Result:= StringReplace(Result, '\', '.', [rfReplaceall]);
I:= length(Result);
// se for mt grande o caminho, pegamos só os 100 primeiros caracteres
if I > 100 then
Result:= Copy(Result, I-100, I);
Result:= NProfile.Paths.EditorStateDir + Result + NOTES_EDITOR_STATE_EXT;
end;
{ TNotesRemeberEditorState }
{
--------------------
NSE
--------------------
topline
--------------------
line
--------------------
col
--------------------
marks count
--------------------
marks
x ( mark value )
count
-------------------------
}
constructor TNotesRemeberEditorState.Create(
const EditorTab: TNotesEditorTab);
begin
fEdTab:= EditorTab;
if EditorTab = nil then
raise Exception.Create(ClassName + '.Create - EditorTab can not be nil!!!');
end;
procedure TNotesRemeberEditorState.LoadState;
var
fs: TFileStream;
I, MCount, MarkValue: integer;
S: string[NOTES_EDITOR_STATE_SIGLEN];
FileN: string;
begin
if not FileExists(fEdTab.FullPath) then Exit;
FileN:= getStateFileForFile(fEdTab.FullPath);
if not FileExists(FileN) then Exit;
fs:= TFileStream.Create(FileN, fmOpenRead);
try
fs.Read(S, sizeOf(S));
if S <> NOTES_EDITOR_STATE_SINATURE then
raise Exception.Create(ClassName + '.LoadState - invalid signature! Can not load editor state for this file.');
fs.Read(MCount, sizeOf(Integer));
fEdTab.Editor.topline:= MCount;
fs.Read(MCount, sizeOf(Integer));
fEdTab.Editor.caretY:= MCount;
fs.Read(MCount, sizeOf(Integer));
fEdTab.Editor.caretX:= MCount;
fs.Read(MCount, sizeOf(integer));
for I:= 0 to MCount -1 do
begin
fs.Read(MarkValue, sizeOf(Integer));
fEdTab.Marks.Add(MarkValue);
end;
finally
fs.Free;
end;
end;
procedure TNotesRemeberEditorState.SaveState;
var
fs: TFileStream;
I, MarkValue, Buf: integer;
S: string[NOTES_EDITOR_STATE_SIGLEN];
begin
if fEdTab.FileName = '' then Exit;
fs:= TFileStream.Create(getStateFileForFile(fEdTab.FullPath), fmCreate);
try
S:= NOTES_EDITOR_STATE_SINATURE;
fs.Write(S, sizeOf(S));
Buf:= fEdTab.Editor.topline;
fs.Write(Buf, sizeOf(Integer));
Buf:= fEdTab.Editor.caretY;
fs.Write(Buf, sizeOf(Integer));
Buf:= fEdTab.Editor.caretX;
fs.Write(Buf, sizeOf(Integer));
Buf:= fEdTab.Marks.Count;
fs.Write(Buf, sizeOf(integer));
for I:= 0 to fEdTab.Marks.Count -1 do
begin
MarkValue:= fEdTab.Marks.items[I];
fs.Write(MarkValue, sizeOf(Integer));
end;
finally
fs.Free;
end;
end;
{ TNotesEditorStateCleanUp }
procedure TNotesEditorStateCleanUp.Execute;
Var
SR: TSearchRec;
D: TDateTime;
begin
inherited;
// Se fDaysOld for 0, vai deletar todos os arquivos da pasta
D:= Now - fDaysOld;
if FindFirst(fDir + '*' + fExt, faAnyFile, SR) <> 0 then Exit;
try
repeat
if FileDateToDateTime(SR.Time) <= D then
DeleteFile(fDir + SR.Name);
if Terminated then
Exit;
until FindNext(SR) <> 0;
finally
findClose(SR);
end;
end;
end.