File: codyopts.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (218 lines) | stat: -rw-r--r-- 5,339 bytes parent folder | download | duplicates (3)
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
211
212
213
214
215
216
217
218
unit CodyOpts;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  // CodeTools
  FileProcs,
  // LazUtils
  LazMethodList, LazConfigStorage,
  // IdeIntf
  BaseIDEIntf;

const
  CodyConfigVersion = 1;
var
  CodyMiscOptionID: integer = 1000;
type

  { TCodyMiscOptions }

  TCodyMiscOptions = class(TPersistent)
  private
    FChangeStep: integer;
    FPreferImplementationUsesSection: boolean;
    FUDLoadDelayInS: integer;
    FUDSaveIntervalInS: integer;
    fLastSavedChangeStep: integer;
    fApplyHandlers: TMethodList;
    function GetModified: boolean;
    procedure SetModified(AValue: boolean);
    procedure SetUDLoadDelayInS(AValue: integer);
    procedure SetUDSaveIntervalInS(AValue: integer);
  public
    // unit / identifier dictionary
    property UDLoadDelayInS: integer read FUDLoadDelayInS write SetUDLoadDelayInS;
    property UDSaveIntervalInS: integer read FUDSaveIntervalInS write SetUDSaveIntervalInS;
    property PreferImplementationUsesSection: boolean
      read FPreferImplementationUsesSection write FPreferImplementationUsesSection;
    procedure Assign(Source: TPersistent); override;
    constructor Create;
    destructor Destroy; override;
    function Equals(Obj: TObject): boolean; override;
    procedure SaveSafe;
    procedure LoadSafe;
    procedure SaveToFile(Filename: string);
    procedure LoadFromFile(Filename: string);
    procedure Clear;
    procedure Apply;
    procedure AddHandlerApply(const OnApplyEvent: TNotifyEvent; AsLast: boolean = false);
    procedure RemoveHandlerApply(const OnApplyEvent: TNotifyEvent);
    property ChangeStep: integer read FChangeStep;
    procedure IncreaseChangeStep;
    property Modified: boolean read GetModified write SetModified;
  end;

var
  CodyOptions: TCodyMiscOptions = nil;

implementation

{ TCodyMiscOptions }

procedure TCodyMiscOptions.SetModified(AValue: boolean);
begin
  if AValue then
    IncreaseChangeStep
  else
    fLastSavedChangeStep:=FChangeStep;
end;

function TCodyMiscOptions.GetModified: boolean;
begin
  Result:=fLastSavedChangeStep<>FChangeStep;
end;

procedure TCodyMiscOptions.SetUDLoadDelayInS(AValue: integer);
begin
  if FUDLoadDelayInS=AValue then Exit;
  FUDLoadDelayInS:=AValue;
  IncreaseChangeStep;
end;

procedure TCodyMiscOptions.SetUDSaveIntervalInS(AValue: integer);
begin
  if FUDSaveIntervalInS=AValue then Exit;
  FUDSaveIntervalInS:=AValue;
  IncreaseChangeStep;
end;

constructor TCodyMiscOptions.Create;
begin
  inherited Create;
  FChangeStep:=CTInvalidChangeStamp;
  fApplyHandlers:=TMethodList.Create;
end;

destructor TCodyMiscOptions.Destroy;
begin
  FreeAndNil(fApplyHandlers);
  inherited Destroy;
end;

procedure TCodyMiscOptions.Assign(Source: TPersistent);
var
  aSource: TCodyMiscOptions;
begin
  if Source is TCodyMiscOptions then
  begin
    aSource:=TCodyMiscOptions(Source);
    UDSaveIntervalInS:=aSource.UDSaveIntervalInS;
    UDLoadDelayInS:=aSource.UDLoadDelayInS;
  end else
    inherited Assign(Source);
end;

function TCodyMiscOptions.Equals(Obj: TObject): boolean;
var
  Src: TCodyMiscOptions;
begin
  Result:=false;
  if not (Obj is TCodyMiscOptions) then exit;
  Src:=TCodyMiscOptions(Obj);
  if (UDLoadDelayInS<>Src.UDLoadDelayInS)
  or (UDSaveIntervalInS<>Src.UDSaveIntervalInS)
  then exit;
  Result:=true;
end;

procedure TCodyMiscOptions.SaveSafe;
begin
  try
    SaveToFile('codyoptions.xml');
  except
    on E: Exception do
      debugln(['TCodyMiscOptions.SaveSafe ',E.Message]);
  end;
  Modified:=false;
end;

procedure TCodyMiscOptions.LoadSafe;
begin
  try
    LoadFromFile('codyoptions.xml');
  except
    on E: Exception do
      debugln(['TCodyMiscOptions.LoadSafe ',E.Message]);
  end;
  Modified:=false;
end;

procedure TCodyMiscOptions.SaveToFile(Filename: string);
var
  Cfg: TConfigStorage;
begin
  Cfg:=GetIDEConfigStorage(Filename,false);
  try
    Cfg.SetDeleteValue('UnitDictionary/LoadDelay',UDLoadDelayInS,10);
    Cfg.SetDeleteValue('UnitDictionary/SaveInterval',UDSaveIntervalInS,600);
    Cfg.SetDeleteValue('Uses/PreferImplementationSection',PreferImplementationUsesSection,false);
  finally
    Cfg.Free;
  end;
end;

procedure TCodyMiscOptions.LoadFromFile(Filename: string);
var
  Cfg: TConfigStorage;
begin
  Clear;
  Cfg:=GetIDEConfigStorage(Filename,true);
  try
    UDLoadDelayInS:=Cfg.GetValue('UnitDictionary/LoadDelay',10);
    UDSaveIntervalInS:=Cfg.GetValue('UnitDictionary/SaveInterval',600);
    PreferImplementationUsesSection:=Cfg.GetValue('Uses/PreferImplementationSection',false);
    //debugln(['TCodyMiscOptions.LoadFromFile UDSaveIntervalInS=',UDSaveIntervalInS,' LoadDelay=',UDLoadDelayInS]);
  finally
    Cfg.Free;
  end;
end;

procedure TCodyMiscOptions.Clear;
begin
  UDLoadDelayInS:=10;
  UDSaveIntervalInS:=600;
end;

procedure TCodyMiscOptions.Apply;
begin
  fApplyHandlers.CallNotifyEvents(Self);
end;

procedure TCodyMiscOptions.AddHandlerApply(const OnApplyEvent: TNotifyEvent;
  AsLast: boolean);
begin
  fApplyHandlers.Add(TMethod(OnApplyEvent),AsLast);
end;

procedure TCodyMiscOptions.RemoveHandlerApply(const OnApplyEvent: TNotifyEvent
  );
begin
  fApplyHandlers.Remove(TMethod(OnApplyEvent));
end;

procedure TCodyMiscOptions.IncreaseChangeStep;
begin
  CTIncreaseChangeStamp(FChangeStep);
end;

finalization
  if CodyOptions<>nil then
    CodyOptions.SaveSafe;
  FreeAndNil(CodyOptions);

end.