Menu

[r2337]: / trunk / Src / FirstRun.UMain.pas  Maximize  Restore  History

Download this file

288 lines (251 with data), 8.6 kB

  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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2008-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements classes that handle application start up, determine if it the
* first run of the current version and perform necessary updates to per-user
* config file and database.
}
unit FirstRun.UMain;
interface
uses
// Project
FirstRun.UConfigFile, FirstRun.UDatabase, FirstRun.UInstallInfo;
type
/// <summary>Enumeration of changes that can be made to brought forward
/// config files that result in data loss.</summary>
TFirstRunCfgChanges = (
frcRegistration, // local registration record lost
frcHiliter, // syntax highlighter customisation lost
frcProxyPwd, // internet proxy password lost
frcSourceFormat // source code output formatting lost
);
type
/// <summary>Set of first run changes to brought forward config file that can
/// result in data loss.</summary>
TFirstRunCfgChangeSet = set of TFirstRunCfgChanges;
type
/// <summary>Class that provides information about current and previous
/// installations of CodeSnip along with operations to carry forward user
/// preferences and database and to update user config file when necessary.
/// </summary>
TFirstRun = class(TObject)
strict private
var
/// <summary>Object that provides information about current and earlier
/// installations.</summary>
fInstallInfo: TInstallInfo;
/// <summary>Object that interogates and updates user's config file.
/// </summary>
fConfigFile: TUserConfigFileUpdater;
/// <summary>Object used to copy forward older versions of user database.
/// </summary>
fDatabase: TUserDatabaseUpdater;
/// <summary>Checks if config file uses earlier format for storing proxy
/// server passwords.</summary>
function HasOldStyleProxyPwd: Boolean;
public
/// <summary>Constructs object and owned object.</summary>
constructor Create;
/// <summary>Frees object and owned objects.</summary>
destructor Destroy; override;
/// <summary>Checks if a user config file exists for an earlier CodeSnip
/// installation.</summary>
function HaveOldCfgFile: Boolean;
/// <summary>Copies user config file from an earlier CodeSnip installation.
/// </summary>
procedure BringForwardCfgFile;
/// <summary>Updates current version's user config file in place as
/// necessary and notifies caller of changes via Changes parameter.
/// </summary>
procedure UpdateCfgFile(out Changes: TFirstRunCfgChangeSet);
/// <summary>Checks if a user database exist for an earlier CodeSnip
/// installation.</summary>
function HaveOldUserDB: Boolean;
/// <summary>Copies user database from an earlier CodeSnip installation.
/// </summary>
procedure BringForwardUserDB;
/// <summary>Creates a new, empty, Unicode encoded config file for current
/// installation.</summary>
procedure CreateEmptyCfgFile;
/// <summary>
function IsProgramUpdated: Boolean;
end;
type
/// <summary>Static class that manages program's first run processing.
/// </summary>
/// <remarks>
/// <para>Designed to be called before any program preferences are read.
/// </para>
/// <para>If current major version of program has not been run before and
/// user data from an earlier version is detected then a dialogue box is
/// displayed to give user various options to bring forward data.</para>
/// <para>If major version is unchanged but minor version, or user config
/// file version, has changed then config file may be modified.</para>
/// </remarks>
TFirstRunMgr = class(TObject)
strict private
/// <summary>Checks if user config file exists for current program version.
/// </summary>
class function CfgFileExists: Boolean;
/// <summary>Determines if this is first time this major version of the
/// program has been run.</summary>
class function IsFirstRun: Boolean;
/// <summary>Determines if program has been updated since last run.
/// </summary>
class function IsProgramUpdated: Boolean;
public
/// <summary>Runs start-up checks to detect if program has been run before
/// and performs any required user config and user database updates.
/// </summary>
class procedure Execute;
end;
implementation
uses
// Delphi
SysUtils, IOUtils, Forms,
// Project
FirstRun.FmV4ConfigDlg;
{ TFirstRun }
procedure TFirstRun.BringForwardCfgFile;
begin
Assert(HaveOldCfgFile,
ClassName + '.BringForwardCfgFile: Old config file does not exist');
fConfigFile.CopyFile(
fInstallInfo.PreviousUserConfigFileName,
fInstallInfo.IsPreviousUserConfigFileANSI
);
end;
procedure TFirstRun.BringForwardUserDB;
begin
Assert(HaveOldUserDB,
ClassName + '.BringForwardUserDB: Old user database does not exist');
fDatabase.CopyDatabase(fInstallInfo.PreviousUserDatabaseDir);
end;
constructor TFirstRun.Create;
begin
inherited Create;
fInstallInfo := TInstallInfo.Create;
fConfigFile := TUserConfigFileUpdater.Create(
fInstallInfo.CurrentUserConfigFileName
);
fDatabase := TUserDatabaseUpdater.Create(
fInstallInfo.CurrentUserDatabaseDir
);
end;
procedure TFirstRun.CreateEmptyCfgFile;
begin
fConfigFile.CreateNewFile;
end;
destructor TFirstRun.Destroy;
begin
fDatabase.Free;
fConfigFile.Free;
fInstallInfo.Free;
inherited;
end;
function TFirstRun.HasOldStyleProxyPwd: Boolean;
begin
Result := (fConfigFile.FileVer <= 6) and fConfigFile.HasProxyPassword;
end;
function TFirstRun.HaveOldCfgFile: Boolean;
begin
Result := TFile.Exists(fInstallInfo.PreviousUserConfigFileName);
end;
function TFirstRun.HaveOldUserDB: Boolean;
begin
Result := TFile.Exists(fInstallInfo.PreviousUserDatabaseFileName);
end;
function TFirstRun.IsProgramUpdated: Boolean;
begin
Result := fConfigFile.IsCurrentProgramVer;
end;
procedure TFirstRun.UpdateCfgFile(out Changes: TFirstRunCfgChangeSet);
begin
Changes := [];
case fInstallInfo.InstallID of
piOriginal:
begin
fConfigFile.UpdateFromOriginal;
Include(Changes, frcHiliter);
Include(Changes, frcRegistration);
Include(Changes, frcSourceFormat);
end;
piV1_9, piV2:
begin
fConfigFile.DeleteHighligherPrefs;
Include(Changes, frcHiliter);
end;
piV3:
begin
if HasOldStyleProxyPwd then
begin
fConfigFile.DeleteProxyPassword;
Include(Changes, frcProxyPwd);
end;
end;
end;
if fConfigFile.FileVer < 6 then
// User ini file versions before 6 don't have the Prefs:CodeGen section and
// default entries for predefined warnings.
// NOTE: This works for a new config file providing it has not been stamped.
fConfigFile.CreateDefaultCodeGenEntries;
if fConfigFile.FileVer < 9 then
begin
fConfigFile.DeleteDetailsPaneIndex; // can be v8 file even tho not supported
fConfigFile.UpdateCodeGenEntries;
end;
fConfigFile.Stamp;
end;
{ TFirstRunMgr }
class function TFirstRunMgr.CfgFileExists: Boolean;
begin
Result := TFile.Exists(TInstallInfo.CurrentUserConfigFileName);
end;
class procedure TFirstRunMgr.Execute;
var
FR: TFirstRun;
Changes: TFirstRunCfgChangeSet;
begin
if IsFirstRun then
begin
FR := TFirstRun.Create;
try
if FR.HaveOldCfgFile or FR.HaveOldUserDB then
TV4ConfigDlg.Execute(Application, FR);
if not CfgFileExists then
begin
FR.CreateEmptyCfgFile;
FR.UpdateCfgFile(Changes);
end;
finally
FR.Free;
end;
end
else if IsProgramUpdated then
begin
FR := TFirstRun.Create;
try
FR.UpdateCfgFile(Changes);
finally
FR.Free;
end;
end;
end;
class function TFirstRunMgr.IsFirstRun: Boolean;
begin
Result := not CfgFileExists;
end;
class function TFirstRunMgr.IsProgramUpdated: Boolean;
begin
Result := not TUserConfigFileUpdater.IsCurrentProgramVer(
TInstallInfo.CurrentUserConfigFileName
);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.