File: MyxOptions.pas

package info (click to toggle)
mysql-gui-tools 5.0r12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 105,540 kB
  • ctags: 50,897
  • sloc: sql: 348,439; pascal: 285,780; cpp: 94,578; ansic: 90,768; objc: 33,761; sh: 25,629; xml: 10,924; yacc: 10,755; java: 9,986; php: 2,806; python: 2,068; makefile: 1,945; perl: 3
file content (541 lines) | stat: -rw-r--r-- 14,218 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
unit MyxOptions;

interface

uses
  Windows, GnuGetText, Classes, TntForms, SysUtils, TntSysUtils, TntSystem,
  TntClasses,
  Grt, AuxFuncs, Printers;

type
  // Universal connection between option storage and option user.
  IOptionChangeListener = interface
    procedure OptionChanged;
  end;

  TMyxOptions = class(TInterfacedObject, IGrtValueCallbackListener)
    constructor Create(Filename: WideString;
      GrtGlobalOptionPath: WideString); virtual;
    destructor Destroy; override;
  private
    FFileName: WideString;
    FOptionPath: WideString;
    FPOptionDict: Pointer;
    FFormatSettings: TFormatSettings;

    FChangeListeners: TInterfaceList;

    FFixedOptions: TTntStringList;
    FDefaultOptions: TTntStringList;

    function GetOptionInt(Name: WideString): Integer; virtual;
    function GetOptionString(Name: WideString): WideString; virtual;
    procedure SetOptionInt(Name: WideString; Value: Integer);
    procedure SetOptionString(Name: WideString; Value: WideString);
    function GetOptionFloat(Name: WideString): Single;
    procedure SetOptionFloat(Name: WideString; const Value: Single);
  protected
    function GetGrt: TGrt;
    function GetOptionDict: Pointer;

    procedure SetFixedOptions; virtual;
    procedure SetDefaultOptions; virtual;

    procedure Changed;
    function ValueChange(Value: Pointer; Reason: GrtValueCallbackReason): Integer;

    property OptionDict: Pointer read GetOptionDict;
  public
    property Grt: TGrt read GetGrt;

    property OptionFloat[Name: WideString]: Single read GetOptionFloat write SetOptionFloat;
    property OptionInt[Name: WideString]: Integer read GetOptionInt write SetOptionInt;
    property OptionString[Name: WideString]: WideString read GetOptionString write SetOptionString;

    property FixedOptions: TTntStringList read FFixedOptions;
    property DefaultOptions: TTntStringList read FDefaultOptions;

    procedure AddListener(Listener: IOptionChangeListener);
    procedure RemoveListener(Listener: IOptionChangeListener);

    procedure LoadFromFile(FileName: WideString = '');
    procedure SaveToFile(FileName: WideString = '');

    function GetLastFileDialogPaths(DlgName: WideString): WideString;
    procedure SetLastFileDialogPaths(DlgName: WideString; Path: WideString);
  end;

  TMyxCommonOptions = class(TMyxOptions)
  protected
    procedure SetFixedOptions; override;
    procedure SetDefaultOptions; override;
  end;

  function GetStringOptionFromCommandLine(Param: WideString): WideString;
  function GetBoolOptionFromCommandLine(Param: WideString): Boolean;

  function GlobalAppDict: Pointer;

  function CommonOptions: TMyxOptions;

//----------------------------------------------------------------------------------------------------------------------

implementation

var
  CommonOptionsInstance: TMyxOptions;

//----------------------------------------------------------------------------------------------------------------------

function GlobalAppDict: Pointer;

var
  Grt: TGrt;

begin
  Grt := RuntimeEnvironment;

  // Make sure /app is available
  Result := Grt.Global['/app'];
  if (Result = nil) then
  begin
    Result := Grt.DictNew('base.ApplicationData');
    Grt.Global['/app'] := Result;
  end;
end;

// -----------------------------------------------------------------------------

function CommonOptions: TMyxOptions;

var
  S: WideString;

begin
  if (CommonOptionsInstance = nil) then
  begin
    // UserDataDir
    S := GetStringOptionFromCommandLine('UserDataDir');

    if (S <> '') then
      S := WideIncludeTrailingBackslash(S)
    else
      S := GetApplicationDataDir + 'MySQL\';

    // Make sure /app exists
    GlobalAppDict;

    CommonOptionsInstance := TMyxCommonOptions.Create(
      S + 'Common Options.xml', '/app/commonOptions');
  end;

  Result := CommonOptionsInstance;
end;

// -----------------------------------------------------------------------------

constructor TMyxOptions.Create(Filename: WideString;
  GrtGlobalOptionPath: WideString);

begin
  inherited Create;

  FFileName := Filename;
  FOptionPath := GrtGlobalOptionPath;
  FPOptionDict := nil;

  FFixedOptions := TTntStringList.Create;
  FDefaultOptions := TTntStringList.Create;

  // Fill the format settings with language neutral values (actually english is used but for all system languages).
  GetLocaleFormatSettings(MakeLong(LANG_ENGLISH, SUBLANG_NEUTRAL), FFormatSettings);

  SetFixedOptions;
  SetDefaultOptions;

  LoadFromFile;
end;

// -----------------------------------------------------------------------------

destructor TMyxOptions.Destroy;

begin
  if (FPOptionDict <> nil) then
    Grt.ValueListenerRemove(FPOptionDict, self);

  FChangeListeners.Free;

  FFixedOptions.Free;
  FDefaultOptions.Free;

  inherited;
end;

// -----------------------------------------------------------------------------

function TMyxOptions.GetGrt: TGrt;

begin
  Result := RuntimeEnvironment;
end;

// -----------------------------------------------------------------------------

function TMyxOptions.GetOptionDict: Pointer;

begin
  if (FPOptionDict = nil) then
  begin
    FPOptionDict := Grt.Global[FOptionPath];

    if (FPOptionDict = nil) then
    begin
      FPOptionDict := Grt.DictNew('');
      Grt.ValueListenerAdd(FPOptionDict, self);

      Grt.Global[FOptionPath] := FPOptionDict;
    end;
  end;

  Result := FPOptionDict;
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.LoadFromFile(FileName: WideString);

var I: Integer;

begin
  if (FileName = '') then
    FileName := FFileName;

  if (FileName <> '') then
  begin
    FPOptionDict := Grt.ValueLoadFromFile(FileName);

    if (FPOptionDict = nil) then
      FPOptionDict := Grt.DictNew('');

    // ensure default options are set
    for I := 0 to FDefaultOptions.Count - 1 do
      if (Grt.DictItem[FPOptionDict, FDefaultOptions.Names[I]] = nil) then
        Grt.DictString[FPOptionDict, FDefaultOptions.Names[I]] :=
          FDefaultOptions.ValueFromIndex[I];

    // Attach listener
    Grt.ValueListenerAdd(FPOptionDict, self);

    // Set GRT global
    Grt.Global[FOptionPath] := FPOptionDict;
  end;
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.SaveToFile(FileName: WideString);

begin
  if (FPOptionDict <> nil) then
  begin
    if (FileName = '') then
      FileName := FFileName;

    WideForceDirectories(WideExtractFilePath(FileName));

    Grt.ValueSaveToFile(FileName, FPOptionDict);
  end;
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.AddListener(Listener: IOptionChangeListener);

begin
  if FChangeListeners = nil then
    FChangeListeners := TInterfaceList.Create;
  if FChangeListeners.IndexOf(Listener) = -1 then
    FChangeListeners.Add(Listener);
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.RemoveListener(Listener: IOptionChangeListener);

begin
  if Assigned(FChangeListeners) then
    FChangeListeners.Remove(Listener);
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.Changed;

var
  I: Integer;

begin
  if Assigned(FChangeListeners) then
  begin
    for I := 0 to FChangeListeners.Count - 1 do
      IOptionChangeListener(FChangeListeners[I]).OptionChanged;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMyxOptions.ValueChange(Value: Pointer; Reason: GrtValueCallbackReason): Integer;

begin
  if (Reason = GrtVcrDictItemChange) then
    Changed;

  Result := 1;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMyxOptions.GetOptionFloat(Name: WideString): Single;

var Value: WideString;
  PeriodPos,
  CommaPos: Integer;

begin
  Value := GetOptionString(name);

  if (Value <> '') then
  begin
    PeriodPos := Pos('.', Value);
    CommaPos := Pos(',', Value);

    if (PeriodPos < CommaPos) then
    begin
      FFormatSettings.DecimalSeparator := ',';
      FFormatSettings.ThousandSeparator := '.';
    end
    else
    begin
      FFormatSettings.DecimalSeparator := '.';
      FFormatSettings.ThousandSeparator := ',';
    end;

    Result := StrToFloatDef(Value, 0, FFormatSettings);
  end
  else
    Result := 0;
end;

//----------------------------------------------------------------------------------------------------------------------

function TMyxOptions.GetOptionInt(Name: WideString): Integer;

begin
  Result := StrToIntDef(GetOptionString(name), 0);
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.SetOptionFloat(Name: WideString; const Value: Single);

begin
  SetOptionString(Name, FloatToStr(Value, FFormatSettings));
end;

//----------------------------------------------------------------------------------------------------------------------

procedure TMyxOptions.SetOptionInt(Name: WideString; Value: Integer);

begin
  SetOptionString(Name, IntToStr(Value));
end;

// -----------------------------------------------------------------------------

function TMyxOptions.GetOptionString(Name: WideString): WideString;

var
  Value: Pointer;
  Index: Integer;

begin
  // look if there is a fixed option with this name
  Index := FFixedOptions.IndexOfName(Name);
  if (Index > -1) then
    Result := FFixedOptions.ValueFromIndex[Index]
  else
  begin
    Value := Grt.DictItem[OptionDict, Name];

    if (Value <> nil) then
      Result := Grt.ValueString[Value]
    else
    begin
      // if the value is not set yet, look at the defaults
      // and set the option with the default value
      Index := FDefaultOptions.IndexOfName(Name);
      if (Index > -1) then
        Result := FDefaultOptions.ValueFromIndex[Index]
      else
        Result := '';

      Grt.DictString[OptionDict, Name] := Result;
    end;
  end;
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.SetOptionString(Name: WideString; Value: WideString);

begin
  if (FFixedOptions.IndexOfName(Name) > -1) then
    raise Exception.CreateFmt(
      _('The option %s is a fixed option and cannot be set.'), [Name]);
  Grt.DictString[OptionDict, Name] := Value;
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.SetFixedOptions;

begin
  //
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.SetDefaultOptions;

begin
  //
end;

// -----------------------------------------------------------------------------

function TMyxOptions.GetLastFileDialogPaths(DlgName: WideString): WideString;

var
  PathList: TTntStringList;

begin
  PathList := TTntStringList.Create;
  try
    PathList.Text := OptionString['DialogPaths'];

    Result := PathList.Values['DlgName'];
  finally
    PathList.Free;
  end;
end;

// -----------------------------------------------------------------------------

procedure TMyxOptions.SetLastFileDialogPaths(DlgName: WideString; Path: WideString);

var
  DialogPaths: WideString;

begin
  DialogPaths := OptionString['DialogPaths'];

  if (DialogPaths = '') then
    DialogPaths := DlgName + '=' + Path
  else
    DialogPaths := DialogPaths + #13#10 + DlgName + '=' + Path;

  OptionString['DialogPaths'] := DialogPaths;
end;

// -----------------------------------------------------------------------------

function GetStringOptionFromCommandLine(Param: WideString): WideString;

var
  I: Integer;

begin
  Result := '';

  for I := 1 to WideParamCount do
    if (WideSameText(WideParamStr(I), '-' + Param)) and
      (I+1 <= WideParamCount) then
    begin
      Result := WideParamStr(I + 1);

      break;
    end;
end;

// -----------------------------------------------------------------------------

function GetBoolOptionFromCommandLine(Param: WideString): Boolean;

var
  I: Integer;

begin
  Result := False;

  for I := 1 to WideParamCount do
    if (WideSameText(WideParamStr(I), '-' + Param)) then
    begin
      Result := True;

      break;
    end;
end;

// -----------------------------------------------------------------------------
// TMyxCommonOptions
// -----------------------------------------------------------------------------

procedure TMyxCommonOptions.SetFixedOptions;

var
  S: WideString;

begin
  // UserDataDir
  S := GetStringOptionFromCommandLine('UserDataDir');

  if (S <> '') then
    S := WideIncludeTrailingBackslash(S)
  else
    S := GetApplicationDataDir + 'MySQL\';

  FixedOptions.Add('UserDataDir=' + S);

  // XMLDir
  S := GetStringOptionFromCommandLine('XMLDir');

  if (S <> '') then
    S := WideIncludeTrailingBackslash(S)
  else
    S := AuxFuncs.GetApplDir + 'XML\';

  FixedOptions.Add('XMLDir=' + S);

  // NoRegEntries
  FixedOptions.Add('NoRegEntries=' +
    IntToStr(Ord(GetBoolOptionFromCommandLine('NoRegEntries'))));
end;

// -----------------------------------------------------------------------------

procedure TMyxCommonOptions.SetDefaultOptions;

begin
  DefaultOptions.Add('DefaultFontName=Tahoma');
  DefaultOptions.Add('DefaultFontHeight=13');

  DefaultOptions.Add('DefaultCodeFontName=Bitstream Vera Sans Mono');
  DefaultOptions.Add('DefaultCodeFontWidth=7');
  DefaultOptions.Add('DefaultCodeFontHeight=-11');
  DefaultOptions.Add('DefaultCodeFontCharset=1');
end;

//----------------------------------------------------------------------------------------------------------------------

end.