File: compatibilityrestrictions.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 (392 lines) | stat: -rw-r--r-- 11,010 bytes parent folder | download | duplicates (2)
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
{ /***************************************************************************
              CompatibilityRestrictions.pas  -  Lazarus IDE unit
              --------------------------------------------------

 ***************************************************************************/

 ***************************************************************************
 *                                                                         *
 *   This source 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 code 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.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************

  Abstract:
    Compatiblity restrictions utilities

}
unit CompatibilityRestrictions;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  // LCL
  Forms, LCLProc, LCLPlatformDef,
  // LazUtils
  Laz2_DOM, Laz2_XMLRead, Laz2_XMLWrite, StringHashList,
  // IdeIntf
  OIFavoriteProperties, PackageIntf, ComponentReg,
  // IDE
  PackageSystem, PackageDefs;

type
  TReadRestrictedEvent = procedure (const RestrictedName, WidgetSetName: String) of object;
  TReadRestrictedContentEvent = procedure (const Short, Description: String) of object;

  PRestriction = ^TRestriction;
  TRestriction = record
    Name: String;
    Short: String;
    Description: String;
    WidgetSet: TLCLPlatform;
  end;
  
  { TClassHashList }

  TClassHashList = class
  private
    FHashList: TStringHashList;
  public
    constructor Create;
    destructor Destroy; override;
    
    procedure Add(const AClass: TPersistentClass);
    procedure AddComponent(const AClass: TComponentClass);
    function Find(const AClassName: String): TPersistentClass;
  end;
  
  TRestrictedList = array of TRestriction;

  { TRestrictedManager }

  TRestrictedManager = class
  private
    FRestrictedProperties: TOIRestrictedProperties;
    FRestrictedList: TRestrictedList;
    FRestrictedFiles: TStringList;
    FClassList: TClassHashList;
    procedure AddPackage(APackage: TLazPackageID);
    procedure AddRestricted(const RestrictedName, WidgetSetName: String);
    procedure AddRestrictedContent(const Short, Description: String);
    procedure AddRestrictedProperty(const RestrictedName, WidgetSetName: String);
    procedure GatherRestrictedFiles;
    procedure ReadRestrictions(const Filename: String;
      OnReadRestricted: TReadRestrictedEvent;
      OnReadRestrictedContent: TReadRestrictedContentEvent);
  public
    constructor Create;
    destructor Destroy; override;
    
    function GetRestrictedProperties: TOIRestrictedProperties;
    function GetRestrictedList: TRestrictedList;
  end;

  
function GetRestrictedProperties: TOIRestrictedProperties;
function GetRestrictedList: TRestrictedList;
  
implementation

var
  RestrictedManager: TRestrictedManager = nil;
  
{ TClassHashList }

constructor TClassHashList.Create;
begin
  inherited;
  
  FHashList := TStringHashList.Create(False);
end;

destructor TClassHashList.Destroy;
begin
  FHashList.Free;
  
  inherited;
end;

procedure TClassHashList.Add(const AClass: TPersistentClass);
var
  C: TClass;
begin
  C := AClass;
  while (C <> nil) and (FHashList.Find(C.ClassName) < 0) do
  begin
    FHashList.Add(C.ClassName, Pointer(C));
    if (C = TPersistent) then Break;
    C := C.ClassParent;
  end;
end;

procedure TClassHashList.AddComponent(const AClass: TComponentClass);
begin
  Add(AClass);
end;

function TClassHashList.Find(const AClassName: String): TPersistentClass;
begin
  Result := TPersistentClass(FHashList.Data[AClassName]);
end;


function GetRestrictedProperties: TOIRestrictedProperties;
begin
  if RestrictedManager = nil then
    RestrictedManager := TRestrictedManager.Create;
  Result := RestrictedManager.GetRestrictedProperties;
end;

function GetRestrictedList: TRestrictedList;
begin
  if RestrictedManager = nil then
    RestrictedManager := TRestrictedManager.Create;
  Result := RestrictedManager.GetRestrictedList;
end;

{ TRestrictedManager }

function TRestrictedManager.GetRestrictedProperties: TOIRestrictedProperties;
var
  I: Integer;
begin
  Result := nil;
  FreeAndNil(FRestrictedProperties);
  FRestrictedProperties := TOIRestrictedProperties.Create;


  FClassList := TClassHashList.Create;
  try
    IDEComponentPalette.IterateRegisteredClasses(@(FClassList.AddComponent));
    FClassList.Add(TForm);
    FClassList.Add(TDataModule);
  
    for I := 0 to FRestrictedFiles.Count - 1 do
      ReadRestrictions(FRestrictedFiles[I], @AddRestrictedProperty, nil);
    
    Result := FRestrictedProperties;
  finally
    FreeAndNil(FClassList);
  end;
end;

function TRestrictedManager.GetRestrictedList: TRestrictedList;
var
  I: Integer;
begin
  SetLength(FRestrictedList, 0);

  for I := 0 to FRestrictedFiles.Count - 1 do
    ReadRestrictions(FRestrictedFiles[I], @AddRestricted, @AddRestrictedContent);
    
  Result := FRestrictedList;
end;

procedure TRestrictedManager.AddPackage(APackage: TLazPackageID);
var
  ALazPackage: TLazPackage;
  I: Integer;
begin
  if APackage = nil then Exit;
  ALazPackage := PackageGraph.FindPackageWithID(APackage);
  if ALazPackage = nil then Exit;
  
  for I := 0 to ALazPackage.FileCount - 1 do
    if ALazPackage.Files[I].FileType = pftIssues then
      FRestrictedFiles.Add(ALazPackage.Files[I].GetFullFilename);
end;

procedure TRestrictedManager.AddRestricted(const RestrictedName, WidgetSetName: String);
begin
  SetLength(FRestrictedList, Succ(Length(FRestrictedList)));
  FRestrictedList[High(FRestrictedList)].Name := RestrictedName;
  FRestrictedList[High(FRestrictedList)].WidgetSet := DirNameToLCLPlatform(WidgetSetName);
  FRestrictedList[High(FRestrictedList)].Short := '';
  FRestrictedList[High(FRestrictedList)].Description := '';
end;

procedure TRestrictedManager.AddRestrictedContent(const Short, Description: String);
begin
  if Length(FRestrictedList) = 0 then Exit;
  FRestrictedList[High(FRestrictedList)].Short := Short;
  FRestrictedList[High(FRestrictedList)].Description := Description;
end;

procedure TRestrictedManager.AddRestrictedProperty(const RestrictedName, WidgetSetName: String);
var
  Issue: TOIRestrictedProperty;
  AClass: TPersistentClass;
  AProperty: String;
  P: Integer;
begin
  if RestrictedName = '' then Exit;

  P := Pos('.', RestrictedName);
  if P = 0 then
  begin
    AClass := FClassList.Find(RestrictedName);
    AProperty := '';
  end
  else
  begin
    AClass := FClassList.Find(Copy(RestrictedName, 0, P - 1));
    AProperty := Copy(RestrictedName, P + 1, MaxInt);
  end;
  
  if AClass = nil then
  begin
    // add as generic widgetset issue
    FRestrictedProperties.WidgetSetRestrictions[DirNameToLCLPlatform(WidgetSetName)] := FRestrictedProperties.WidgetSetRestrictions[DirNameToLCLPlatform(WidgetSetName)] + 1;
    Exit;
  end;
  
  Issue := TOIRestrictedProperty.Create(AClass, AProperty, True);
  Issue.WidgetSets := [DirNameToLCLPlatform(WidgetSetName)];
  FRestrictedProperties.Add(Issue);
end;

procedure TRestrictedManager.GatherRestrictedFiles;
begin
  FRestrictedFiles.Clear;
  PackageGraph.IteratePackages([fpfSearchInInstalledPckgs], @AddPackage);
end;

procedure TRestrictedManager.ReadRestrictions(const Filename: String;
  OnReadRestricted: TReadRestrictedEvent;
  OnReadRestrictedContent: TReadRestrictedContentEvent);
var
  IssueFile: TXMLDocument;
  R, N: TDOMNode;
  
  function ReadContent(ANode: TDOMNode): String;
  var
    S: TStringStream;
    N: TDOMNode;
  begin
    Result := '';
    S := TStringStream.Create('');
    try
      N := ANode.FirstChild;
      while N <> nil do
      begin
        WriteXML(N, S);
        N := N.NextSibling;
      end;
      
      Result := S.DataString;
    finally
      S.Free;
    end;
  end;
  
  procedure ParseWidgetSet(ANode: TDOMNode);
  var
    WidgetSetName, IssueName, Short, Description: String;
    IssueNode, AttrNode, IssueContentNode: TDOMNode;
  begin
    AttrNode := ANode.Attributes.GetNamedItem('name');
    if AttrNode <> nil then WidgetSetName := AttrNode.NodeValue
    else WidgetSetName := 'win32';
    
    IssueNode := ANode.FirstChild;
    while IssueNode <> nil do
    begin
      if IssueNode.NodeName = 'issue' then
      begin
        AttrNode := IssueNode.Attributes.GetNamedItem('name');
        if AttrNode <> nil then IssueName := AttrNode.NodeValue
        else IssueName := 'win32';
        
        if Assigned(OnReadRestricted) then
          OnReadRestricted(IssueName, WidgetSetName);
        if Assigned(OnReadRestrictedContent) then
        begin
          Short := '';
          Description := '';
          
          IssueContentNode := IssueNode.FirstChild;
          while IssueContentNode <> nil do
          begin
            if IssueContentNode.NodeName = 'short' then
              Short := ReadContent(IssueContentNode)
            else
              if IssueContentNode.NodeName = 'descr' then
                Description := ReadContent(IssueContentNode);

            IssueContentNode := IssueContentNode.NextSibling;
          end;
          
          OnReadRestrictedContent(Short, Description);
        end;
      end;
      IssueNode := IssueNode.NextSibling;
    end;
  end;
  
begin
  try
    ReadXMLFile(IssueFile, Filename);
  except
     on E: Exception do
       DebugLn('TIssueManager.ReadFileIssues failed: ' + E.Message);
  end;
  
  try
    if IssueFile = nil then Exit;

    R := IssueFile.FindNode('package');
    if R = nil then Exit;

    N := R.FirstChild;
    while N <> nil do
    begin
      if N.NodeName = 'widgetset' then
        ParseWidgetSet(N);

      N := N.NextSibling;
    end;
  finally
    IssueFile.Free;
  end;
end;

constructor TRestrictedManager.Create;
begin
  inherited;
  
  FRestrictedFiles := TStringList.Create;
  FRestrictedProperties := nil;
  
  GatherRestrictedFiles;
end;

destructor TRestrictedManager.Destroy;
begin
  FreeAndNil(FRestrictedFiles);
  FreeAndNil(FRestrictedProperties);
  
  inherited Destroy;
end;


finalization

  FreeAndNil(RestrictedManager);


end.