File: idedatadict.pp

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 (314 lines) | stat: -rw-r--r-- 6,971 bytes parent folder | download | duplicates (4)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
unit idedatadict;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpdatadict, LazUTF8, LazIDEIntf, ldd_consts;

Type

  { TIDEDataDictionary }

  { TDDFile }

  TDDFile = Class(TObject)
    FFileName : String;
    Constructor Create(AFileName : String);
  end;

  TIDEDataDictionary = Class(TComponent)
  private
    FDataDict: TFPDatadictionary;
    FDictName: String;
    FFileName: String;
    FKnownDicts : TStringList;
    procedure DDProgress(Sender: TObject; const Msg: String);
    procedure SetDictName(const AValue: String);
    procedure SetFileName(const AValue: String);
  Protected
    function GetActive: Boolean;
    Procedure SetDictNameAndFile(Const AName,AFileName : String); virtual;
    procedure SetActive(const AValue: Boolean);
    Procedure Initialize; virtual;
  Public
    Constructor Create(AOwner : TComponent); override;
    Procedure Update;
    Procedure Load;
    procedure Save;
    Procedure GetKnownDictionaries(List : TStrings);
    Function GetDictionaryFileName(ADict : String) : String;
    Property Dictionary :  TFPDatadictionary Read FDataDict;
    Property FileName :  String Read FFileName Write SetFileName;
    Property DictionaryName : String Read FDictName Write SetDictName;
    Property Active : Boolean Read GetActive Write SetActive;
  end;

Function IDEDatadictionary : TIDEDataDictionary;
Procedure InitDDSettings;
procedure SaveDDSettings;

Var
  DataDesktopBinary : String;
  DefaultDictionaryDir : String;
  
implementation

uses
  inifiles, IDEMsgIntf,
  IDEExternToolIntf,
  BaseIDEIntf;

Const
  // Do not localize
  DDXML      = 'datadict.xml';
  KeyDesktop = 'DatabaseDesktop';
  KeyDir     = 'DictionaryDir';

Var
  Inited : Boolean;

Procedure InitDDSettings;

begin
  If Inited then
    Exit;
  With GetIDEConfigStorage(DDXML,True) do
    try
      DataDesktopBinary:=GetValue(KeyDesktop,'');
      DefaultDictionaryDir:=GetValue(KeyDir,'');
    Finally
      Free;
    end;
  Inited:=true;
end;

procedure SaveDDSettings;

begin
  With GetIDEConfigStorage(DDXML,False) do
    try
      SetValue(KeyDesktop,DataDesktopBinary);
      SetValue(KeyDir,DefaultDictionaryDir);
      WriteToDisk;
    Finally
      Free;
    end;
end;


{ ---------------------------------------------------------------------
  Application name magic
  ---------------------------------------------------------------------}
Var
  OldAppName : TGetAppNameEvent;

Function FakeName : String;

begin
  Result:='Lazarus Data Desktop';
end;

Procedure OverrideAppName;

begin
  OldAppName:=OnGetApplicationName;
  OnGetApplicationName:=@FakeName;
end;

Procedure RestoreAppName;

begin
  OnGetApplicationName:=OldAppName;
end;

{ ---------------------------------------------------------------------
  Public functions
  ---------------------------------------------------------------------}

Var
  TheDataDict : TIDEDataDictionary;
  
Function IDEDatadictionary : TIDEDataDictionary;

begin
  If Not Assigned(TheDataDict) then
    TheDataDict:=TIDEDataDictionary.Create(nil);
  TheDataDict.Update;
  Result:=TheDataDict;
end;


{ ---------------------------------------------------------------------
  TIDEDataDictionary
  ---------------------------------------------------------------------}

procedure TIDEDataDictionary.SetDictName(const AValue: String);
begin
  if (FDictName<>AValue) then
    SetDictNameAndFile(AValue,GetDictionaryFileName(AValue));
end;

function TIDEDataDictionary.GetActive: Boolean;
begin
  Result:=FDataDict<>Nil;
end;

procedure TIDEDataDictionary.SetDictNameAndFile(const AName, AFileName: String);
begin
  FDictName:=AName;
  FFileName:=AFileName;
  If Active then
    FDataDict.LoadFromFile(UTF8ToSys(FFileName));
end;

procedure TIDEDataDictionary.SetActive(const AValue: Boolean);
begin
  If (AValue<>GetActive) then
    If AValue then
      begin
      FDataDict:=TFPDataDictionary.Create;
      FDataDict.OnProgress:=@DDProgress;
      Load;
      end
    else
      FreeAndNil(FDataDict);
end;

procedure TIDEDataDictionary.DDProgress(Sender : TObject; Const Msg : String);

begin
  IDEMessagesWindow.AddCustomMessage(mluImportant,SLoadingDataDict+Msg);
end;

procedure TIDEDataDictionary.SetFileName(const AValue: String);
begin
  If (AValue<>FFileName) or (FDictName<>'') then
    SetDictnameAndFile('',AValue);
end;

procedure TIDEDataDictionary.Initialize;

Var
  FN,DN : String;
  Ini : TMemInifile;
  I,Count : Integer;

begin
  OverrideAppName;
  try
    FN:=GetAppConfigFile(False);
  finally
    RestoreAppName;
  end;
  Ini:=TMemIniFile.Create(FN);
  try
    FKnownDicts.Clear;
    Count:=Ini.ReadInteger('RecentDicts','Count',0);
    For I:=0 to Count do
      begin
      DN:=Ini.ReadString('RecentDicts','DataDict'+InttoStr(I),'');
      If (DN<>'') then
        FKnownDicts.AddObject(DN,TDDFile.Create(Ini.ReadString('DD_'+DN,'FileName','')));
      end;
  finally
    Ini.Free;
  end;
end;

constructor TIDEDataDictionary.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FKnownDicts:=TStringList.Create;
  FKnownDicts.Sorted:=True;
  Initialize;
end;

procedure TIDEDataDictionary.Update;

Var
  ProjActive : Boolean;
  ProjFileName : String;
  ProjDD : String;

begin
  If Not(Assigned(LazarusIDE) and Assigned(LazarusIDE.ActiveProject)) then
    begin
    Active:=False;
    FileName:='';
    Exit;
    end;
  With LazarusIDE.ActiveProject do
    begin
    ProjDD:=Customdata.Values['DataDict/KnownDict'];
    ProjFileName:=Customdata.Values['DataDict/FileName'];
    ProjActive:=(ProjDD<>'') or (ProjFileName<>'');
    If ProjActive then
      ProjActive:=StrToIntDef(Customdata.Values['DataDict/Active'],ord(ProjActive))<>0;
    end;
  If (Active<>ProjActive) or (ProjDD<>DictionaryName) or (FileName<>ProjFileName) then
    begin
    Active:=ProjActive;
    If (ProjDD<>'') then
      DictionaryName:=ProjDD
    else
      FileName:=ProjFileName;
    end;
end;


procedure TIDEDataDictionary.Save;

begin
  If Not(Assigned(LazarusIDE) and Assigned(LazarusIDE.ActiveProject)) then
    Exit;
  With LazarusIDE.ActiveProject do
    begin
    Customdata.Values['DataDict/FileName']:=FileName;
    Customdata.Values['DataDict/KnownDict']:=DictionaryName;
    Customdata.Values['DataDict/Active']:=IntToStr(Ord(Active));
    end;
end;


procedure TIDEDataDictionary.Load;
begin
  If (FFileName<>'') and Active then
    begin
      IDEMessagesWindow.AddCustomMessage(mluImportant,SLoadingDataDict+SFromfile+FFileName);
      FDataDict.LoadFromFile(UTF8ToSys(FFileName));
    end;
end;



procedure TIDEDataDictionary.GetKnownDictionaries(List: TStrings);

begin
  List.Clear;
  List.AddStrings(FKnownDicts);
end;

function TIDEDataDictionary.GetDictionaryFileName(ADict: String): String;

Var
  I : Integer;

begin
  I:=FKnownDicts.IndexOf(ADict);
  If (I<>-1) then
    Result:=(FKnownDicts.Objects[i] as TDDFile).FFileName
  Else
    Result:='';
end;

{ TDDFile }

constructor TDDFile.Create(AFileName: String);
begin
  FFileName:=AFileName;
end;

end.