File: opkman_packagelistfrm.pas

package info (click to toggle)
lazarus 2.2.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 219,980 kB
  • sloc: pascal: 1,944,919; xml: 357,634; makefile: 270,608; cpp: 57,115; sh: 3,249; java: 609; perl: 297; sql: 222; ansic: 137
file content (540 lines) | stat: -rw-r--r-- 17,243 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
unit opkman_packagelistfrm;

{$mode objfpc}{$H+}
{
 ***************************************************************************
 *                                                                         *
 *   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.   *
 *                                                                         *
 ***************************************************************************

 Author: Balázs Székely
}
interface

uses
  SysUtils, Classes,
  // LCL
  Forms, Controls, Graphics, ExtCtrls, StdCtrls, laz.VirtualTrees,
  // OpkMan
  opkman_const, opkman_common, opkman_serializablepackages, opkman_options, opkman_visualtree,
  opkman_maindm;

type

  { TPackageListFrm }

  TPackageListFrm = class(TForm)
    bOk: TButton;
    bYes: TButton;
    bNo: TButton;
    lbHint: TLabel;
    lbMessage: TLabel;
    pnUpDown: TPanel;
    pnMessage: TPanel;
    pnButtons: TPanel;
    procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: char);
    procedure lbMessageResize(Sender: TObject);
  private
    FVST: TLazVirtualStringTree;
    FSL: TStringList;
    FModRes: TModalResult;
    function GetCount: Integer;
    procedure SetupControls(const ATyp: Integer);
    procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; {%H-}TextType: TVSTTextType; var CellText: String);
    procedure VSTGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
      {%H-}Kind: TVTImageKind; Column: TColumnIndex; var {%H-}Ghosted: Boolean;
      var ImageIndex: Integer);
    procedure VSTPaintText(Sender: TBaseVirtualTree;
      const TargetCanvas: TCanvas; Node: PVirtualNode; {%H-}Column: TColumnIndex;
      {%H-}TextType: TVSTTextType);
    procedure VSTCompareNodes(Sender: TBaseVirtualTree; Node1,
      Node2: PVirtualNode; Column: TColumnIndex; var Result: Integer);
    procedure VSTGetHint(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
      var LineBreakStyle: TVTTooltipLineBreakStyle; var HintText: String);
    procedure VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
    function IsNodeVisible(const APackageName: String): Boolean;
    function IsLazarusCompatible(const ALazVersions: String): Boolean;
    function IsFPCCompatible(const AFPCVersions: String): Boolean;
    function IsWidgetSetCompatible(const AWidgetSets: String): Boolean;
  public
    procedure PopulateList(const ATyp: Integer; const AExtra: String = '');
    property Count: Integer read GetCount;
  end;

var
  PackageListFrm: TPackageListFrm;

implementation

{$R *.lfm}

{ TPackageListFrm }

type
  PData = ^TData;
  TData = record
    FName: string[100];
    FImageIndex: Integer;
    FSupLazVers: String;
    FIsLazComp: Boolean;
    FSupFPCVers: String;
    FIsFPCComp: Boolean;
    FSupWS: String;
    FIsWSComp: Boolean;
  end;

procedure TPackageListFrm.FormKeyPress(Sender: TObject; var Key: char);
begin
  if Key = #13 then
  begin
    FModRes := mrYes;
    Close;
  end
  else if Key = #27 then
  begin
    FModRes := mrNo;
    Close;
  end;
end;

procedure TPackageListFrm.lbMessageResize(Sender: TObject);
begin
  pnMessage.Height := lbMessage.Top + lbMessage.Height + 5;
end;

function TPackageListFrm.GetCount: Integer;
begin
  Result := FVST.RootNodeCount;
end;

procedure TPackageListFrm.SetupControls(const ATyp: Integer);
begin
  FModRes := mrNone;
  case ATyp of
    0: Caption := rsPackageListFrm_Caption0;
    1: Caption := rsPackageListFrm_Caption1;
    2: Caption := rsPackageListFrm_Caption0;
    3: begin
         Caption := rsPackageListFrm_Caption3;
         Self.Width := 650;
         Self.BorderStyle := bsSizeable;
         FVST.Header.Options := FVST.Header.Options + [hoVisible];
         FVST.Header.Columns.Items[1].Options := FVST.Header.Columns.Items[1].Options + [coVisible];
         FVST.Header.Columns.Items[2].Options := FVST.Header.Columns.Items[2].Options + [coVisible];
         FVST.Header.Columns.Items[3].Options := FVST.Header.Columns.Items[3].Options + [coVisible];
         FVST.TreeOptions.MiscOptions := FVST.TreeOptions.MiscOptions + [toReportMode];
         lbHint.Visible := True;
       end;
  end;
  bYes.Caption := rsPackageListFrm_bYes_Caption;
  bNo.Caption := rsPackageListFrm_bNo_Caption;
  bOk.Caption := rsPackageListFrm_bOk_Caption;
  lbHint.Caption := rsPackageListFrm_lbHint_Caption;
  bYes.Top := (pnButtons.Height - bYes.Height) div 2;
  bNo.Top := (pnButtons.Height - bNo.Height) div 2;
  bOk.Top := (pnButtons.Height - bOk.Height) div 2;
  pnMessage.Height := lbMessage.Top + lbMessage.Height + 5;
end;

procedure TPackageListFrm.PopulateList(const ATyp: Integer; const AExtra: String);
var
  I, J: Integer;
  Node: PVirtualNode;
  Data: PData;
  LazarusPkg: TLazarusPackage;
  ChkCnt, InvCnt: Integer;
  LazComp, FPCComp, WSComp: Boolean;
  NoURL: Boolean;
begin
  SetupControls(ATyp);
  ChkCnt := 0;
  InvCnt := 0;
  for I := 0 to SerializablePackages.Count - 1 do
  begin
    if not IsNodeVisible(SerializablePackages.Items[I].DisplayName) then
      Continue;
    if ATyp = 0 then
    begin
      for J := 0 to SerializablePackages.Items[I].LazarusPackages.Count - 1  do
      begin
        LazarusPkg := TLazarusPackage(SerializablePackages.Items[I].LazarusPackages.Items[J]);
        if (LazarusPkg.Checked) and (psInstalled in LazarusPkg.PackageStates) then
        begin
          Node := FVST.AddChild(nil);
          Data := FVST.GetNodeData(Node);
          Data^.FName := LazarusPkg.Name + '(' + LazarusPkg.InstalledFileVersion + ')';
          Data^.FImageIndex := IMG_PKG_FILE;
        end;
      end;
    end
    else if ATyp = 1 then
    begin
      if (SerializablePackages.Items[I].Checked) and (FileExists(AExtra + SerializablePackages.Items[I].RepositoryFileName)) then
      begin
        Node := FVST.AddChild(nil);
        Data := FVST.GetNodeData(Node);
        if SerializablePackages.Items[I].DisplayName <> '' then
          Data^.FName := SerializablePackages.Items[I].DisplayName
        else
          Data^.FName := SerializablePackages.Items[I].Name;
        Data^.FImageIndex := IMG_PKG_PLUS;
      end;
    end
    else if ATyp = 2 then
    begin
      if (SerializablePackages.Items[I].Checked) then
      begin
        Inc(ChkCnt);
        NoURL := (Trim(SerializablePackages.Items[I].DownloadURL) = '') or (Trim(SerializablePackages.Items[I].DownloadZipURL) = '');
        if NoURL and (not SerializablePackages.Items[I].IsDependencyPackage) then
        begin
          Inc(InvCnt);
          Node := FVST.AddChild(nil);
          Data := FVST.GetNodeData(Node);
          if SerializablePackages.Items[I].DisplayName <> '' then
            Data^.FName := SerializablePackages.Items[I].DisplayName
          else
            Data^.FName := SerializablePackages.Items[I].Name;
          Data^.FImageIndex := IMG_PKG_PLUS;
          SerializablePackages.Items[I].ChangePackageStates(ctAdd, psError);
        end;
      end;
    end
    else if ATyp = 3 then
    begin
      for J := 0 to SerializablePackages.Items[I].LazarusPackages.Count - 1  do
      begin
        LazarusPkg := TLazarusPackage(SerializablePackages.Items[I].LazarusPackages.Items[J]);
        if (LazarusPkg.Checked) then
        begin
          LazComp := IsLazarusCompatible(LazarusPkg.LazCompatibility);
          FPCComp := IsFPCCompatible(LazarusPkg.FPCCompatibility);
          WSComp := IsWidgetSetCompatible(LazarusPkg.SupportedWidgetSet);
          if (not LazComp) or (not FPCComp) or (not WSComp) then
          begin
            Node := FVST.AddChild(nil);
            Data := FVST.GetNodeData(Node);
            Data^.FName := LazarusPkg.Name + '(' + LazarusPkg.VersionAsString + ')';
            Data^.FImageIndex := IMG_PKG_FILE;
            Data^.FSupLazVers := LazarusPkg.LazCompatibility;
            Data^.FIsLazComp := LazComp;
            Data^.FSupFPCVers := LazarusPkg.FPCCompatibility;
            Data^.FIsFPCComp := FPCComp;
            Data^.FSupWS := LazarusPkg.SupportedWidgetSet;
            Data^.FIsWSComp := WSComp;
          end;
        end;
      end;
    end;
  end;
  if (ATyp = 2) and (ChkCnt = InvCnt) then
  begin
    bYes.Visible := False;
    bNo.Visible := False;
    bOk.Visible := True;
    lbMessage.Caption := rsMainFrm_PackageUpdate1;
  end;
  FVST.SortTree(0, sdAscending);
end;

procedure TPackageListFrm.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  if FModRes <> mrNone then
    ModalResult := FModRes;
end;

procedure TPackageListFrm.FormCreate(Sender: TObject);
begin
  if not Options.UseDefaultTheme then
    Self.Color := clBtnFace;
  FVST := TLazVirtualStringTree.Create(nil);
  with FVST do
  begin
    Parent := Self;
    Align := alClient;
    Anchors := [akLeft, akTop, akRight];
    Images := MainDM.Images;
    if not Options.UseDefaultTheme then
      Color := clBtnFace;
    DefaultNodeHeight := Scale96ToForm(25);
    Indent := 0;
    TabOrder := 1;
    DefaultText := '';
    Header.AutoSizeIndex := 0;
    Header.Height := Scale96ToForm(25);
    Colors.BorderColor := clBlack;
    BorderSpacing.Top := Scale96ToForm(5);
    BorderSpacing.Left := Scale96ToForm(15);
    BorderSpacing.Right := Scale96ToForm(15);
    Header.Options := [hoAutoResize, hoColumnResize, hoRestrictDrag, hoAutoSpring];
    with Header.Columns.Add do begin
      Position := 0;
      Width := Scale96ToForm(250);
      Text := rsMainFrm_VSTHeaderColumn_PackageName;
    end;
    with Header.Columns.Add do
    begin
      Position := 1;
      Width := FVST.Scale96ToForm(110);
      Alignment := taCenter;
      Options := Options - [coVisible];
      Text := 'FPC';
    end;
    with Header.Columns.Add do
    begin
      Position := 2;
      Width := FVST.Scale96ToForm(110);
      Alignment := taCenter;
      Options := Options - [coVisible];
      Text := 'Lazarus';
    end;
    with Header.Columns.Add do
    begin
      Position := 3;
      Width := FVST.Scale96ToForm(110);
      Alignment := taCenter;
      Options := Options - [coVisible];
      Text := 'Widgetset';
    end;
    Header.SortColumn := 0;
    HintMode := hmHint;
    ShowHint := True;
    TreeOptions.MiscOptions := [toFullRepaintOnResize, toInitOnSave, toToggleOnDblClick, toWheelPanning];
    TreeOptions.PaintOptions := [toHideFocusRect, toPopupMode, toShowButtons, toShowDropmark, toThemeAware, toUseBlendedImages];
    TreeOptions.SelectionOptions := [toFullRowSelect, toRightClickSelect];
    TreeOptions.AutoOptions := [toAutoTristateTracking];
    OnGetText := @VSTGetText;
    OnPaintText := @VSTPaintText;
    OnGetImageIndex := @VSTGetImageIndex;
    OnCompareNodes := @VSTCompareNodes;
    OnGetHint := @VSTGetHint;
    OnFreeNode := @VSTFreeNode;
  end;
  FVST.NodeDataSize := SizeOf(TData);
  FSL := TStringList.Create;
  FSL.Delimiter := ',';
  FSL.StrictDelimiter := True;
end;

procedure TPackageListFrm.FormDestroy(Sender: TObject);
begin
  FSL.Free;
  FVST.Clear;
  FVST.Free;
end;

procedure TPackageListFrm.VSTGetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: String);
var
  Data: PData;
begin
  Data := FVST.GetNodeData(Node);
  case Column of
    0: CellText := Data^.FName;
    1: if Data^.FIsFPCComp then
         CellText := 'OK'
       else
         CellText := rsPackageListFrm_Incompatible;
    2: if Data^.FIsLazComp then
         CellText := 'OK'
       else
         CellText := rsPackageListFrm_Incompatible;
    3: if Data^.FIsWSComp then
         CellText := 'OK'
       else
         CellText := rsPackageListFrm_Incompatible;
  end;
end;

procedure TPackageListFrm.VSTGetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: Integer);
var
  Data: PData;
begin
  Data := FVST.GetNodeData(Node);
  if Column = 0 then
    ImageIndex := Data^.FImageIndex;
end;

procedure TPackageListFrm.VSTPaintText(Sender: TBaseVirtualTree;
  const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  TextType: TVSTTextType);

  function GetColor(const AIsComp: Boolean): TColor;
  begin
    if (Node = Sender.FocusedNode) then
      Result := FVST.Colors.SelectionTextColor
    else
      if AIsComp then
        Result := clBlack
      else
        Result := clRed;
  end;

var
  Data: PData;
begin
  if TextType = ttNormal then
  begin
    Data := FVST.GetNodeData(Node);
    case Column of
      1: TargetCanvas.Font.Color := GetColor(Data^.FIsFPCComp);
      2: TargetCanvas.Font.Color := GetColor(Data^.FIsLazComp);
      3: TargetCanvas.Font.Color := GetColor(Data^.FIsWSComp);
    end;
  end;
end;

procedure TPackageListFrm.VSTCompareNodes(Sender: TBaseVirtualTree; Node1,
  Node2: PVirtualNode; Column: TColumnIndex; var Result: Integer);
var
  Data1: PData;
  Data2: PData;
begin
  Data1 := Sender.GetNodeData(Node1);
  Data2 := Sender.GetNodeData(Node2);
  if Column = 0 then
    Result := CompareText(Data1^.FName, Data2^.FName);
end;

procedure TPackageListFrm.VSTGetHint(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex;
  var LineBreakStyle: TVTTooltipLineBreakStyle; var HintText: String);
var
  Data: PData;
  CurFPCVer, CurLazVer: String;
  SupFPCVers, SupLazVers: String;
begin
  Data := FVST.GetNodeData(Node);
  LineBreakStyle := hlbForceSingleLine;

  if CurFPCVersion = FPCTrunk then
    CurFPCVer := 'Trunk'
  else
    CurFPCVer := CurFPCVersion;
  SupFPCVers := StringReplace(Data^.FSupFPCVers, FPCTrunk, 'Trunk', [rfIgnoreCase, rfReplaceAll]);

  if CurLazVersion = LazTrunk then
    CurLazVer := 'Trunk'
  else
    CurLazVer := CurLazVersion;
  SupLazVers := StringReplace(Data^.FSupLazVers, LazTrunk, 'Trunk', [rfIgnoreCase, rfReplaceAll]);
  case Column of
    1: HintText := rsPackageListFrm_CurFPCVer + CurFPCVer + sLineBreak +
                   rsPackageListFrm_SupFPCVers + SupFPCVers;
    2: HintText := rsPackageListFrm_CurLazVer + CurLazVer + sLineBreak +
                   rsPackageListFrm_SupLazVers + SupLazVers;
    3: HintText := rsPackageListFrm_CurWS + CurWidgetSet + sLineBreak +
                   rsPackageListFrm_SupWSs + Data^.FSupWS;
    else
       HintText := '';
  end;
end;

procedure TPackageListFrm.VSTFreeNode(Sender: TBaseVirtualTree;
  Node: PVirtualNode);
var
  Data: PData;
begin
  Data := FVST.GetNodeData(Node);
  Finalize(Data^);
end;

function TPackageListFrm.IsNodeVisible(const APackageName: String): Boolean;
var
  Node: PVirtualNode;
  Data: opkman_visualtree.PData;
begin
  Result := False;
  Node := VisualTree.VST.GetFirst;
  while Assigned(Node) do
  begin
    Data := VisualTree.VST.GetNodeData(Node);
    if (Data^.DataType = 1) and (Data^.PackageDisplayName = APackageName) then
    begin
      Result := VisualTree.VST.IsVisible[Node];
      Break;
    end;
    Node := VisualTree.VST.GetNext(Node);
  end;
end;

function TPackageListFrm.IsLazarusCompatible(const ALazVersions: String): Boolean;
var
  LazVersion: String;
  I: Integer;
begin
  Result := False;
  FSL.Clear;
  FSL.DelimitedText := ALazVersions;
  for I := 0 to FSL.Count - 1 do
  begin
    LazVersion := Trim(FSL.Strings[I]);
    if LazVersion <> 'Trunk' then
      Result := LazVersion = CurLazVersion
    else
      Result := CurLazVersion >= LazTrunk;
    if Result then
      Exit;
  end;
end;

function TPackageListFrm.IsFPCCompatible(const AFPCVersions: String): Boolean;
var
  FPCVersion: String;
  I: Integer;
begin
  Result := False;
  FSL.Clear;
  FSL.DelimitedText := AFPCVersions;
  for I := 0 to FSL.Count - 1 do
  begin
    FPCVersion := Trim(FSL.Strings[I]);
    if FPCVersion <> 'Trunk' then
      Result := FPCVersion = CurFPCVersion
    else
      Result := CurFPCVersion >= FPCTrunk;
    if Result then
      Exit;
  end;
end;

function TPackageListFrm.IsWidgetSetCompatible(const AWidgetSets: String): Boolean;
var
  WidgetSet: String;
  I: Integer;
begin
  FSL.Clear;
  FSL.DelimitedText := AWidgetSets;
  for I := 0 to FSL.Count - 1 do
  begin
    WidgetSet := Trim(FSL.Strings[I]);
    Result := WidgetSet = CurWidgetSet;
    if Result then
      Exit;
  end;
end;

end.