File: addpkgdependencydlg.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (396 lines) | stat: -rw-r--r-- 12,107 bytes parent folder | download
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
unit AddPkgDependencyDlg;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, AVL_Tree, fgl,
  // LCL
  LCLType, LCLIntf, Forms, Controls, Dialogs, StdCtrls, ButtonPanel, Graphics, ExtCtrls,
  // LazControls
  ListFilterEdit,
  // LazUtils
  LazLoggerBase,
  // BuildIntf
  PackageIntf, PackageLinkIntf, PackageDependencyIntf,
  // IDEIntf
  IDEWindowIntf, IDEDialogs,
  // IdeConfig
  ProjPackCommon,
  // IDE
  MainIntf, LazarusIDEStrConsts, PackageDefs, PackageSystem, ProjPackChecks;

type

  TPkgDependencyList = specialize TFPGList<TPkgDependency>;

  { TAddPkgDependencyDialog }

  TAddPkgDependencyDialog = class(TForm)
    BP: TButtonPanel;
    cbLocalPkg: TCheckBox;
    cbOnlinePkg: TCheckBox;
    DependMaxVersionEdit: TEdit;
    DependMaxVersionLabel: TLabel;
    DependMinVersionEdit: TEdit;
    DependMinVersionLabel: TLabel;
    DependPkgNameFilter: TListFilterEdit;
    DependPkgNameLabel: TLabel;
    DependPkgTypeLabel: TLabel;
    DependPkgNameListBox: TListBox;
    pnLocalPkg: TPanel;
    pnOnlinePkg: TPanel;
    procedure cbLocalPkgChange(Sender: TObject);
    procedure cbOnlinePkgChange(Sender: TObject);
    procedure CloseButtonClick(Sender: TObject);
    procedure DependPkgNameListBoxDblClick(Sender: TObject);
    procedure DependPkgNameListBoxDrawItem(Control: TWinControl;
      Index: Integer; ARect: TRect; State: TOwnerDrawState);
    procedure DependPkgNameListBoxSelectionChange(Sender: TObject; {%H-}User: boolean);
    procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    function InstallOnlinePackages: TModalResult;
  private
    fUpdating: Boolean;
    fPackages: TAVLTree;    // tree of TLazPackage or TPackageLink.
    fProjPack: IProjPack;   // Project or package, a recipient of the dependency.
    fResultDependencies: TPkgDependencyList;
    procedure AddUniquePackagesToList(APackageID: TLazPackageID);
    procedure UpdateAvailableDependencyNames;
    function IsInstallButtonVisible: Boolean;
    procedure PackageListAvailable(Sender: TObject);
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  AddPkgDependencyDialog: TAddPkgDependencyDialog;

function ShowAddPkgDependencyDlg(AProjPack: IProjPack;
  out AResultDependencies: TPkgDependencyList): TModalResult;

implementation

{$R *.lfm}

function ShowAddPkgDependencyDlg(AProjPack: IProjPack;
  out AResultDependencies: TPkgDependencyList): TModalResult;
var
  AddDepDialog: TAddPkgDependencyDialog;
begin
  AddDepDialog:=TAddPkgDependencyDialog.Create(nil);
  AddDepDialog.fProjPack:=AProjPack;
  AddDepDialog.UpdateAvailableDependencyNames;

  Result:=AddDepDialog.ShowModal;
  if Result=mrOk then begin
    AResultDependencies:=AddDepDialog.fResultDependencies;
    AddDepDialog.fResultDependencies:=nil;
  end else begin
    AResultDependencies:=nil;
  end;
  AddDepDialog.Free;
end;

{ TAddPkgDependencyDialog }

constructor TAddPkgDependencyDialog.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  Caption:=lisProjAddNewRequirement;
  fPackages:=TAVLTree.Create(@CompareLazPackageIDNames);

  DependPkgNameLabel.Caption:=lisProjAddPackageName;
  DependPkgTypeLabel.Caption:=lisProjAddPackageType;
  cbLocalPkg.Caption:=lisProjAddLocalPkg;
  cbOnlinePkg.Caption:=lisProjAddOnlinePkg;
  BP.CloseButton.Caption := lisPckEditInstall;
  DependMinVersionLabel.Caption:=lisProjAddMinimumVersionOptional;
  DependMinVersionEdit.Text:='';
  DependMaxVersionLabel.Caption:=lisProjAddMaximumVersionOptional;
  DependMaxVersionEdit.Text:='';

  IDEDialogLayoutList.ApplyLayout(Self,400,360);
end;

destructor TAddPkgDependencyDialog.Destroy;
begin
  FreeAndNil(fPackages);
  inherited Destroy;
end;

procedure TAddPkgDependencyDialog.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  IDEDialogLayoutList.SaveLayout(Self);
end;

procedure TAddPkgDependencyDialog.FormCreate(Sender: TObject);
begin
  if Assigned(OPMInterface) then
    cbOnlinePkg.Checked := OPMInterface.IsPackageListLoaded
  else begin
    DependPkgTypeLabel.Visible := False;
    pnLocalPkg.Visible := False;
    pnOnlinePkg.Visible := False;
  end;
  cbOnlinePkg.OnChange := @cbOnlinePkgChange; // Set handler after setting Checked.
  BP.CloseButton.Visible := False;            // CloseButton is now "Install".
  if OPMInterface <> nil then
    OPMInterface.AddPackageListNotification(@PackageListAvailable);
end;

procedure TAddPkgDependencyDialog.FormDestroy(Sender: TObject);
begin
  if OPMInterface <> nil then
    OPMInterface.RemovePackageListNotification(@PackageListAvailable);
end;

procedure TAddPkgDependencyDialog.DependPkgNameListBoxDrawItem(
  Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);
var
  Txt: string;
  Pkg: TLazPackageID;
begin
  with (Control as TListBox).Canvas do
  begin
    Pkg := TLazPackageID(DependPkgNameListBox.Items.Objects[Index]);
    if odSelected In State then
    begin
      Pen.Color := clHighlightText;
      Brush.Color := clHighlight;
      if (Pkg is TPackageLink) and (TPackageLink(Pkg).Origin = ploOnline) then
        Font.Style := Font.Style + [fsBold]
    end
    else begin
      Pen.Color := (Control as TListBox).Font.Color;
      if (Pkg is TPackageLink) and (TPackageLink(Pkg).Origin = ploOnline) then
        Font.Style := Font.Style + [fsBold]
    end;
    FillRect(ARect);
    Txt := (Control as TListBox).Items[Index];
    inc(ARect.Left,3);
    DrawText(Handle, PChar(Txt), Length(Txt), ARect, DT_LEFT or DT_VCENTER or DT_SINGLELINE);
  end;
end;

function TAddPkgDependencyDialog.IsInstallButtonVisible: Boolean;
var
  I: Integer;
  Pkg: TLazPackageID;
begin
  for I := 0 to DependPkgNameListBox.Count - 1 do
  begin
    if DependPkgNameListBox.Selected[I] then
    begin
      Pkg := TLazPackageID(DependPkgNameListBox.Items.Objects[I]);
      if (Pkg is TPackageLink) and (TPackageLink(Pkg).Origin = ploOnline) then
        Exit(True);
    end;
  end;
  Result := False;
end;

procedure TAddPkgDependencyDialog.PackageListAvailable(Sender: TObject);
begin
  DebugLn(['TAddPkgDependencyDialog.PackageListAvailable: ', fProjPack.IDAsString]);
  UpdateAvailableDependencyNames;
end;

procedure TAddPkgDependencyDialog.DependPkgNameListBoxSelectionChange(
  Sender: TObject; User: boolean);
begin
  BP.CloseButton.Visible := IsInstallButtonVisible;
  BP.OKButton.Enabled := not BP.CloseButton.Visible;
end;

procedure TAddPkgDependencyDialog.cbLocalPkgChange(Sender: TObject);
begin
  UpdateAvailableDependencyNames;
end;

procedure TAddPkgDependencyDialog.cbOnlinePkgChange(Sender: TObject);
begin
  Assert(Assigned(OPMInterface), 'TAddPkgDependencyDialog: OPMInterface=Nil.');
  if (Sender as TCheckBox).Checked and not OPMInterface.IsPackageListLoaded then
    OPMInterface.GetPackageList  // ListBox will be updated later by an event.
  else
    UpdateAvailableDependencyNames;
end;

function TAddPkgDependencyDialog.InstallOnlinePackages: TModalResult;
var
  I: Integer;
  Pkg: TLazPackageID;
  PkgList: TList;
begin
  Result := mrOk;
  PkgList := TList.Create;
  try
    for I := 0 to DependPkgNameListBox.Count - 1 do
    begin
      if DependPkgNameListBox.Selected[I] then
      begin
        Pkg := TLazPackageID(DependPkgNameListBox.Items.Objects[I]);
        if (Pkg is TPackageLink) and (TPackageLink(Pkg).Origin = ploOnline) then
          PkgList.Add(Pkg);
      end;
    end;
    if PkgList.Count > 0 then
    begin
      Assert(Assigned(OPMInterface), 'InstallOnlinePackages: OPMInterface=Nil');
      Result := OPMInterface.InstallPackages(PkgList);
    end;
  finally
    PkgList.Free;
  end;
end;

procedure TAddPkgDependencyDialog.CloseButtonClick(Sender: TObject);
// CloseButton is now "Install".
begin
  ModalResult := mrNone;
  case InstallOnlinePackages of
    mrCancel: Exit;
    mrRetry:   // mrRetry means the IDE must be rebuilt.
      begin
        Self.Hide;
        MainIDEInterface.DoBuildLazarus([]);
      end;
    else
      UpdateAvailableDependencyNames;
  end;
end;

procedure TAddPkgDependencyDialog.DependPkgNameListBoxDblClick(Sender: TObject);
begin
  OKButtonClick(Self);
end;

procedure TAddPkgDependencyDialog.AddUniquePackagesToList(APackageID: TLazPackageID);
begin
  if (APackageID.IDAsString<>fProjPack.IDAsString) and (fPackages.Find(APackageID)=Nil) then
    fPackages.Add(APackageID);
end;

procedure TAddPkgDependencyDialog.UpdateAvailableDependencyNames;
var
  ANode: TAVLTreeNode;
  Pkg: TLazPackageID;
  CntLocalPkg: Integer;
  CntOnlinePkg: Integer;
begin
  if fUpdating then
    Exit;
  fUpdating := True;
  try
    CntLocalPkg := 0;
    CntOnlinePkg := 0;
    DependPkgNameFilter.Items.Clear;
    fPackages.Clear;
    PackageGraph.IteratePackages(fpfSearchAllExisting,@AddUniquePackagesToList);
    ANode:=fPackages.FindLowest;
    while ANode<>nil do
    begin
      Pkg := TLazPackageID(ANode.Data);
      if (Pkg is TPackageLink) and (TPackageLink(Pkg).Origin = ploOnline) then
      begin
        if cbOnlinePkg.Checked then
        begin
          Inc(CntOnlinePkg);
          DependPkgNameFilter.Items.AddObject(Pkg.Name, Pkg);
        end;
      end
      else if cbLocalPkg.Checked then
      begin
        Inc(CntLocalPkg);
        DependPkgNameFilter.Items.AddObject(Pkg.Name, Pkg);
      end;
      ANode:=fPackages.FindSuccessor(ANode);
    end;
    DependPkgNameFilter.InvalidateFilter;
    if Assigned(OPMInterface) then
    begin
      cbLocalPkg.Caption := Format(lisProjAddLocalPkg, [IntToStr(CntLocalPkg)]);
      cbOnlinePkg.Caption := Format(lisProjAddOnlinePkg, [IntToStr(CntOnlinePkg)]);
      BP.CloseButton.Visible := IsInstallButtonVisible;
    end;
  finally
    fUpdating := False;
  end;
end;

procedure TAddPkgDependencyDialog.OKButtonClick(Sender: TObject);
var
  NewDependency: TPkgDependency = nil;
  MinVerTest, MaxVerTest: TPkgVersion;
  MinMaxVerFlags: TPkgDependencyFlags;
  i: Integer;
begin
  MinVerTest := Nil;
  MaxVerTest := Nil;
  MinMaxVerFlags := [];
  try
    // check minimum version
    if DependMinVersionEdit.Text <> '' then
    begin
      MinVerTest := TPkgVersion.Create;
      if not MinVerTest.ReadString(DependMinVersionEdit.Text) then
      begin
        IDEMessageDialog(lisProjAddInvalidVersion,
          Format(lisProjAddTheMinimumVersionIsInvalid,
                 [DependMinVersionEdit.Text, LineEnding, LineEnding]),
          mtError,[mbCancel]);
        exit;
      end;
      MinMaxVerFlags := [pdfMinVersion];
    end;
    // check maximum version
    if DependMaxVersionEdit.Text <> '' then
    begin
      MaxVerTest := TPkgVersion.Create;
      if not MaxVerTest.ReadString(DependMaxVersionEdit.Text) then
      begin
        IDEMessageDialog(lisProjAddInvalidVersion,
          Format(lisProjAddTheMaximumVersionIsInvalid,
                 [DependMaxVersionEdit.Text, LineEnding, LineEnding]),
          mtError,[mbCancel]);
        exit;
      end;
      MinMaxVerFlags := MinMaxVerFlags + [pdfMaxVersion];
    end;

    // Add all selected packages.
    fResultDependencies := TPkgDependencyList.Create; // Will be freed by the caller.
    if DependPkgNameListBox.SelCount > 0 then
    begin
      for i := 0 to DependPkgNameListBox.Count-1 do
      begin
        if DependPkgNameListBox.Selected[i] then
        begin
          NewDependency := TPkgDependency.Create;   // Will be added to package graph.
          NewDependency.PackageName := DependPkgNameListBox.Items[i];
          if Assigned(MinVerTest) then
            NewDependency.MinVersion.Assign(MinVerTest);
          if Assigned(MaxVerTest) then
            NewDependency.MaxVersion.Assign(MaxVerTest);
          NewDependency.Flags := NewDependency.Flags + MinMaxVerFlags;
          if not CheckAddingDependency(fProjPack, NewDependency) then exit;
          fResultDependencies.Add(NewDependency);
          NewDependency := nil;
        end;
      end;
    end;
    ModalResult := mrOk;
  finally
    NewDependency.Free;
    MinVerTest.Free;
    MaxVerTest.Free;
  end;
end;

end.