File: uninst.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 (415 lines) | stat: -rw-r--r-- 15,715 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

type
  TUninstallState = (
    uiUnknown,
    UIDone,         // There IS no uninstaller, OR it was already executed during this install
    UIOtherNeeded,  // The uninstaller ('Inno Setup: App Path') points to a different Path than "WizardFolder"
                    // Uninstall for OTHER folder NEEDED
    uiDestNeeded,   // Uninstaller for "WizardFolder" found
                    // Uninstall for DESTINATION folder NEEDED
    uiInconsistent  // Path of uninstaller and lazarus to be removed, do not match
  );

var
  UninstallState: TUninstallState;
  UninstallDoneState: TUninstallState; // Set only if uninstall was executed
  UnInstallerInAppPath: Boolean; // The uninstaller is in the directory that it will remove

  OldPath,              // Registry 'Inno Setup: App Path'
  OldName,              // Registry 'DisplayName'
  UnInstaller: String;  // Registry 'UninstallString'
  PathEqual: Boolean;

  UninstDir: String;  // The directory from which lazarus was uninstalled
  CFGFileForUninstDir: TStringList;
  CFGPathForUninstDir: String; // the PCP
  CFGStateForUninstDir: TCfgFileState;

var
  wpAskUnistall: TWizardPage;
  wpLabel1, wpLabel2, wpLabel3, wpLabel4: TNewStaticText;
  wpCheckBox: TNewCheckBox;
  wpButton: TNewButton;

function dbgsUiState(u: TUninstallState): String;
begin
  case u of
    uiUnknown:      Result := 'uiUnknown';
    UIDone:         Result := 'UIDone';
    UIOtherNeeded:  Result := 'UIOtherNeeded';
    uiDestNeeded:   Result := 'uiDestNeeded';
    uiInconsistent: Result := 'uiInconsistent';
  end;
end;

function DidRunUninstaller: Boolean;
begin
  Result := (UninstallDoneState <> uiUnknown);
end;

// check if: unistall was run, and run in AFolder, and did have a lazarus.cfg file
function HasSavedConfigFromUninstall(AFolder: String): Boolean;
begin
  Result := DidRunUninstaller and
            (UninstDir = AFolder) and
            (CFGFileForUninstDir <> nil) and
            (CFGFileForUninstDir.Count > 0); // only if content
end;

function GetSavedConfigFromUninstall(AFolder: String): TStringList;
begin
  Result := nil;
  if HasSavedConfigFromUninstall(AFolder) then
    Result :=  CFGFileForUninstDir;
end;

function GetSavedPCPFromUninstall(AFolder: String): String;
begin
  Result := '';
  if HasSavedConfigFromUninstall(AFolder) then
    Result :=  CFGPathForUninstDir;
end;

function GetSavedStateFromUninstall(AFolder: String): TCfgFileState;
begin
  Result := csNoFile;
  if HasSavedConfigFromUninstall(AFolder) then
    Result :=  CFGStateForUninstDir;
end;

function GetUninstallData(ARegName: String): String; // Get one entry from registry e.g. 'UninstallString'
var
  Path: String;
begin
  Path := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\'+GetAppId('')+'_is1');
  Result := '';
  if not RegQueryStringValue(HKLM, Path, ARegName, Result) then
    RegQueryStringValue(HKCU, Path, ARegName, Result);
end;

procedure InitializeUninstallInfo;
begin
  UninstallState := uiUnknown;
  UninstallDoneState := uiUnknown;
end;

procedure UpdateUninstallInfo;
begin
  Log('Enter UninstallState '+dbgsUiState(UninstallState));
  OldPath := '';
  OldName := '';
  UnInstaller := '';
  PathEqual := False;
  if UninstallState = uiDone then exit;

  UnInstaller := RemoveQuotes(GetUninstallData('UninstallString'));
  Log(' UnInstaller:  '+UnInstaller);  
  if (UnInstaller <> '') and FileExists(UnInstaller) then
  begin
    OldPath := RemoveQuotes((GetUninstallData('Inno Setup: App Path')));
	OldName := GetUninstallData('DisplayName');

    PathEqual := (OldPath <> '') and
                 (CompareText(RemoveBackslashUnlessRoot(OldPath), RemoveBackslashUnlessRoot(WizardDirValue)) = 0);
    Log(' OldPath: '+OldPath+'  OldName: '+ OldName);
	if PathEqual then
      UninstallState := uiDestNeeded
	else
      UninstallState := uiOtherNeeded;

    UnInstallerInAppPath := (CompareText(RemoveBackslashUnlessRoot(OldPath), RemoveBackslashUnlessRoot(ExtractFilePath(UnInstaller))) = 0);
    if (not UnInstallerInAppPath) and
       ( (CompareText(RemoveBackslashUnlessRoot(OldPath), RemoveBackslashUnlessRoot(WizardDirValue)) = 0) or
         (CompareText(RemoveBackslashUnlessRoot(ExtractFilePath(UnInstaller)), RemoveBackslashUnlessRoot(WizardDirValue)) = 0)
       )
    then
      UninstallState := uiInconsistent;

  end
  else
  begin
	if (IsSecondaryCheckBoxChecked) or IsSecondaryUpdate then
    begin
	  if ForcePrimaryAppId then begin
	    Log('UpdateUninstallInfo recursion detected');
	    UninstallState := uiInconsistent;
	    exit;
	  end;
      ForcePrimaryAppId := True;
      Log('REDO UninstallState '+GetUninstallData('Inno Setup: App Path')+' // '+WizardDirValue);
      if CompareText(RemoveBackslashUnlessRoot(RemoveQuotes(GetUninstallData('Inno Setup: App Path'))),
           RemoveBackslashUnlessRoot(WizardDirValue)) = 0
      then
        UpdateUninstallInfo // use the plain installer
      else
        UninstallState := uiDone;
      ForcePrimaryAppId := False;
    end
    else
      UninstallState := uiDone;
  end;

  Log('UninstallState is now '+dbgsUiState(UninstallState)+', OldPath='+OldPath+' OldName='+OldName+' UnInstaller='+UnInstaller);
end;

// *** Display/Use Wizzard Page

procedure InitAskUninstall(s1, s2, s3, s4: String);
var
  y: integer;
begin
  wpLabel1.Caption := s1;
  wpLabel2.Caption := s2;
  wpLabel3.Caption := s3;
  wpLabel4.Caption := s4;

  wpLabel1.AdjustHeight;
  wpLabel2.AdjustHeight;
  wpLabel3.AdjustHeight;
  wpLabel4.AdjustHeight;

  wpLabel2.Top := wpLabel1.Top + wpLabel1.Height + ScaleY(5);
  wpLabel3.Top := wpLabel2.Top + wpLabel2.Height + ScaleY(5);
  wpLabel4.Top := wpLabel3.Top + wpLabel3.Height + ScaleY(5);
  y := wpLabel4.Top + wpLabel4.Height + ScaleY(20);
  if y > wpAskUnistall.SurfaceHeight - wpCheckBox.Height - wpButton.Height then
    y := wpAskUnistall.SurfaceHeight - wpCheckBox.Height - wpButton.Height;
  wpButton.Top := y;
end;

procedure UnInstUpdateGUI;
begin
  UpdateUninstallInfo;
    Log('UnInstUpdateGUI UninstallState='+dbgsUiState(UninstallState)+
       ' IsSecondaryUpdate='+dbgsBool(IsSecondaryUpdate)+
       '  Check='+dbgsBool(IsSecondaryCheckBoxChecked)
       );

  WizardForm.NextButton.Enabled := (UninstallState = uiDone) or (UninstallState = uiDestNeeded) or wpCheckBox.Checked;
  wpCheckBox.Enabled := not(UninstallState = uiDone);
  wpButton.Enabled := not(UninstallState = uiDone);
end;

procedure ActivateAskUninst(Sender: TWizardPage);
begin
  UnInstUpdateGUI;
end;

function SkipAskUninst(Sender: TWizardPage): Boolean;
begin
    Log('SkipAskUninst UninstallState='+dbgsUiState(UninstallState)+
       ', OldPath='+OldPath+' OldName='+OldName+' UnInstaller='+UnInstaller +
       ' IsSecondaryUpdate='+dbgsBool(IsSecondaryUpdate)+
       '  Check='+dbgsBool(IsSecondaryCheckBoxChecked)
       );
  Result := UninstallState = uiDone;
  if Result Then exit;

  UnInstUpdateGUI;
  //UpdateUninstallInfo;

  // OldName, Registry 'DisplayName'
  // OldPath,  Registry 'Inno Setup: App Path'
  // UnInstaller

  case UninstallState of
    uiDestNeeded: begin
	    wpLabel2.Font.Color := clDefault;
        wpCheckBox.Visible := False;
        if IsSecondaryUpdate then
          InitAskUninstall(Format(SaveCustomMessage('OldSecondInDestFolder1', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
	                       Format(SaveCustomMessage('OldSecondInDestFolder2', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
                           Format(SaveCustomMessage('OldSecondInDestFolder3', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
                           Format(SaveCustomMessage('OldSecondInDestFolder4', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]))
        else
          InitAskUninstall(Format(SaveCustomMessage('OldInDestFolder1', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
	                       Format(SaveCustomMessage('OldInDestFolder2', ''), {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
                           Format(SaveCustomMessage('OldInDestFolder3', ''), {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
                           Format(SaveCustomMessage('OldInDestFolder4', ''), {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]));
      end;
    UIOtherNeeded: begin
	    wpLabel2.Font.Color := clRed;
	    wpLabel2.Font.Color := clRed;
        wpCheckBox.Visible := True;
        //if IsSecondaryUpdate then
        //  InitAskUninstall(Format(SaveCustomMessage('InOtherFolder1', ''),
        //                                        {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
	       //                Format(SaveCustomMessage('OldSecondInOtherFolder2', ''),
        //                                        {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
        //                   Format(SaveCustomMessage('OldSecondInOtherFolder3', ''),
        //                                        {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
        //                   Format(SaveCustomMessage('OldSecondInOtherFolder4', ''),
        //                                        {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]));
        //else
          InitAskUninstall(Format(SaveCustomMessage('OldInOtherFolder1', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
	                       Format(SaveCustomMessage('OldInOtherFolder2', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
                           Format(SaveCustomMessage('OldInOtherFolder3', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
                           Format(SaveCustomMessage('OldInOtherFolder4', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]));
        //end;
      end;
    uiInconsistent: begin
	    wpLabel1.Font.Color := clRed;
	    wpLabel2.Font.Color := clRed;
        wpCheckBox.Visible := True;
        //if IsSecondaryUpdate then
        //  InitAskUninstall(Format(SaveCustomMessage('OldSecondInBadFolder1', ''),
        //                                        {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
	       //                Format(SaveCustomMessage('OldSecondInBadFolder2', ''),
        //                                        {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
        //                   Format(SaveCustomMessage('OldSecondInBadFolder3', ''),
        //                                        {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
        //                   Format(SaveCustomMessage('OldSecondInBadFolder4', ''),
        //                                        {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]));
        //else
          InitAskUninstall(Format(SaveCustomMessage('OldInBadFolder1', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
	                       Format(SaveCustomMessage('OldInBadFolder2', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
                           Format(SaveCustomMessage('OldInBadFolder3', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]),
                           Format(SaveCustomMessage('OldInBadFolder4', ''),
                                                {}[#13#10, OldName, OldPath, UnInstaller, SecondPCP]));
        //end;
      end;
  end;

end;

procedure UnInstBtnClick(Sender: TObject);
var
  s, UnInstaller: String;
  b, FolderEmpty : Boolean;
  i: integer;
begin
  UninstallDoneState := UninstallState;
  UninstallState := uiDone;

  UnInstaller := RemoveQuotes(GetUninstallData('UninstallString'));

  b := (UnInstaller <> '') and FileExists(UnInstaller);
  if b then begin
    if PathEqual then begin
      LoadCFGFile(OldPath, CFGFileForUninstDir);
      CFGStateForUninstDir := ParseCFGList(CFGFileForUninstDir, CFGPathForUninstDir);
      UninstDir := OldPath;
    end;

    if UninstallState = uiInconsistent then
      b := Exec(UnInstaller, '/VERBOSE /NORESTART','', SW_SHOW, ewWaitUntilTerminated, i)
    else
      b := Exec(UnInstaller, '/SILENT /NORESTART','', SW_SHOW, ewWaitUntilTerminated, i);
  end;
  if not b then
    MsgBox('Uninstall failed.', mbConfirmation, MB_OK)
  else begin
    if (UninstallDoneState = uiDestNeeded) then
    begin
      FolderEmpty := IsDirEmpty(WizardDirValue);
	  if not FolderEmpty then begin Sleep(500); FolderEmpty := IsDirEmpty(WizardDirValue); end;
	  if not FolderEmpty then begin Sleep(500); FolderEmpty := IsDirEmpty(WizardDirValue); end;
	  if not FolderEmpty then begin Sleep(500); FolderEmpty := IsDirEmpty(WizardDirValue); end;
      if not(FolderEmpty) then begin
        // Dir NOT empty, after uninstall
	    try
		  s := CustomMessage('FolderNotEmpty2');
	    except
		  s := 'The target folder is not empty.';
        end;
        MsgBox(s, mbConfirmation, MB_OK);
      end;
    end;
  end;

  UnInstUpdateGUI;
end;

procedure UnInstCheckboxClick(Sender: TObject);
begin
  UnInstUpdateGUI;
end;

// *** Create Wizzard Page

procedure CreateUninstallWizardPage;
var
  s, s2 : String;
begin
  try
    s := CustomMessage('AskUninstallTitle1');
    s2 := CustomMessage('AskUninstallTitle2');
  except
    s := 'Previous Installation';
	s2 := 'Do you want to run the uninstaller?';
  end;
  wpAskUnistall := CreateCustomPage(wpSelectDir, s, s2);
  wpAskUnistall.OnShouldSkipPage := @SkipAskUninst;
  wpAskUnistall.OnActivate := @ActivateAskUninst;

  wpLabel1 := TNewStaticText.Create(wpAskUnistall);
  wpLabel1.Parent := wpAskUnistall.Surface;
  wpLabel1.Top := 0;
  wpLabel1.Left := 0;
  wpLabel1.Width := wpAskUnistall.SurfaceWidth;
  wpLabel1.Autosize:= False;
  wpLabel1.WordWrap := True;
  wpLabel1.Caption := '';

  wpLabel2 := TNewStaticText.Create(wpAskUnistall);
  wpLabel2.Parent := wpAskUnistall.Surface;
  wpLabel2.Left := 0;
  wpLabel2.Width := wpAskUnistall.SurfaceWidth;
  wpLabel2.Autosize:= False;
  wpLabel2.WordWrap := True;
  wpLabel2.Caption := '';

  wpLabel3 := TNewStaticText.Create(wpAskUnistall);
  wpLabel3.Parent := wpAskUnistall.Surface;
  wpLabel3.Left := 0;
  wpLabel3.Width := wpAskUnistall.SurfaceWidth;
  wpLabel3.Autosize:= False;
  wpLabel3.WordWrap := True;
  wpLabel3.Caption := '';

  wpLabel4 := TNewStaticText.Create(wpAskUnistall);
  wpLabel4.Parent := wpAskUnistall.Surface;
  wpLabel4.Left := 0;
  wpLabel4.Width := wpAskUnistall.SurfaceWidth;
  wpLabel4.Autosize:= False;
  wpLabel4.WordWrap := True;
  wpLabel4.Caption := '';

  try
    s := CustomMessage('BtnUninstall');
  except
    s := 'Uninstall';
  end;
  wpButton := TNewButton.Create(wpAskUnistall);
  wpButton.Parent := wpAskUnistall.Surface;
  wpButton.Width := ScaleX(80);
  wpButton.Left := (wpAskUnistall.SurfaceWidth div 2) - ScaleX(40);
  wpButton.Caption := s;
  wpButton.OnClick := @UnInstBtnClick;

  try
    s := CustomMessage('ChkContinue');
  except
    s := 'Continue without uninstall';
  end;
  wpCheckBox := TNewCheckBox.Create(wpAskUnistall);
  wpCheckBox.Parent := wpAskUnistall.Surface;
  wpCheckBox.Top := wpAskUnistall.SurfaceHeight - wpCheckBox.Height - 1;
  wpCheckBox.Width := wpAskUnistall.SurfaceWidth;
  wpCheckBox.Caption := s;
  wpCheckBox.OnClick := @UnInstCheckboxClick;
end;