Menu

[r4761]: / branches / parsnip / Src / FirstRun.UInstallInfo.pas  Maximize  Restore  History

Download this file

299 lines (267 with data), 9.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
287
288
289
290
291
292
293
294
295
296
297
{
* 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-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a class that provides information about CodeSnip installations.
}
unit FirstRun.UInstallInfo;
interface
type
/// <summary>Identifiers for different type of CodeSnip installations that
/// can be detected.</summary>
TInstallId = (
piNone, // CodeSnip not installed
piOriginal, // "original" installation up to v1.8.11
piV1_9, // v1.9 to v1.9.4
piV2, // all v2 versions
piV3, // all v3 versions
piV4, // from v4.0 (including alpha & beta code v3.98.x & v3.99.x
piV5 // v5 (including all alpha and beta code from v4.97)
);
type
/// <summary>Class that provides information about CodeSnip installations.
/// </summary>
TInstallInfo = class(TObject)
strict private
const
/// <summary>ID of current version of CodeSnip.</summary>
CurrentVersionID = High(TInstallId);
var
/// <summary>Value of InstallID property.</summary>
fInstallID: TInstallId;
/// <summary>Maps given install IDs to relative paths to user config file
/// for that installation version.</summary>
class function UserConfigFileNames(const InstID: TInstallId): string;
/// <summary>Maps given install IDs to relative paths to database directory
/// for that installation version.</summary>
class function DatabaseDirs(const InstID: TInstallId): string;
/// <summary>Converts given relative directory path or file name into an
/// absolute path by pre-prending the user's application data folder.
/// </summary>
/// <remarks>If Name = '' then '' is returned.</remarks>
class function MakeFullUserPath(const Name: string): string;
/// <summary>Informs if user config file used by given installation ID is
/// in ANSI format.</summary>
class function IsUserConfigFileANSI(const InstallID: TInstallId): Boolean;
/// <summary>Detects latest version of CodeSnip for which user data can be
/// found and sets InstallID property accordingly.</summary>
procedure DetectInstall;
public
/// <summary>Creates object to provide information about latest CodeSnip
/// installation for which user data can be found.</summary>
constructor Create;
/// <summary>Returns full path to user config file for current version of
/// CodeSnip.</summary>
class function CurrentUserConfigFileName: string;
/// <summary>Returns full path to database directory for current version of
/// CodeSnip.</summary>
class function CurrentDatabaseDir: string;
/// <summary>Returns full path to user config file version of CodeSnip for
/// which user data is available.</summary>
/// <remarks>This can be current version's config file if present.
/// </remarks>
function PreviousUserConfigFileName: string;
/// <summary>Informs if user config file returned by
/// PreviousUserConfigFileName is in ANSI format.</summary>
function IsPreviousUserConfigFileANSI: Boolean;
/// <summary>Returns full path to database directory for latest version of
/// CodeSnip for which a snippets database is available.</summary>
/// <remarks>
/// <para>This can be current version's database directory if present.
/// </para>
/// <para>Prior to CodeSnip 5 the database in question will be the legacy
/// "user" database.</para>
/// </remarks>
function PreviousDatabaseDir: string;
/// <summary>Returns full path of snippets database file for latest version
/// of CodeSnip for which a database is available.</summary>
/// <remarks>
/// <para>This can be current version's database directory if present.
/// </para>
/// <para>Prior to CodeSnip 5 the database in question will be the legacy
/// "user" database.</para>
/// </remarks>
function PreviousDatabaseFileName: string;
/// <summary>Returns full path to common config file for current version of
/// CodeSnip.</summary>
function CurrentCommonConfigFileName: string;
/// <summary>ID of latest CodeSnip install found.</summary>
/// <remarks>This is ID of latest version for which user data can be found.
/// </remarks>
property InstallID: TInstallId read fInstallID;
end;
implementation
uses
// Delphi
SysUtils,
IOUtils,
// Project
CS.Init.CommandLineOpts,
FirstRun.UIniFile,
UAppInfo,
UIOUtils,
UStrUtils,
USystemInfo;
{ TInstallInfo }
constructor TInstallInfo.Create;
begin
inherited Create;
DetectInstall;
end;
function TInstallInfo.CurrentCommonConfigFileName: string;
begin
Result := IncludeTrailingPathDelimiter(TAppInfo.CommonAppDir)
+ 'Common.config';
end;
class function TInstallInfo.CurrentDatabaseDir: string;
begin
Result := DatabaseDirs(CurrentVersionID);
end;
class function TInstallInfo.CurrentUserConfigFileName: string;
begin
Result := UserConfigFileNames(CurrentVersionID);
end;
class function TInstallInfo.DatabaseDirs(const InstID: TInstallId): string;
// Gets database directory from config file associated with InstID.
// Returns DefaultDir if config file doesn't reference database directory.
function DBDirFromCfg(const DefaultDir: string): string;
begin
Result := GetIniString(
'Database',
'UserDataDir',
DefaultDir,
UserConfigFileNames(InstID)
);
end;
begin
if InstID = piV5 then
Exit(TAppInfo.UserDataDir);
Result := '';
if TCommandLineOpts.IsPortable then
begin
if InstID = piV4 then
// v3 and earlier did not support portability
Result := MakeFullUserPath('AppData\UserDB');
end
else
begin
case InstID of
// v1 and earlier did not have a "user" database
piV2:
Result := MakeFullUserPath('DelphiDabbler\CodeSnip\UserData');
piV3:
Result := MakeFullUserPath('DelphiDabbler\CodeSnip\UserData.3');
piV4:
Result := DBDirFromCfg(
MakeFullUserPath('DelphiDabbler\CodeSnip.4\UserDatabase')
);
end;
end;
end;
procedure TInstallInfo.DetectInstall;
// Checks if user config file for given installation exists and is non-empty.
function NonEmptyCfgFileExists(const InstID: TInstallID): Boolean;
var
CfgFileName: string; // cfg file name
Content: string; // content of cfg file
Encoding: TEncoding; // encoding of cfg file
begin
CfgFileName := UserConfigFileNames(InstID);
if (CfgFileName = EmptyStr) or not TFile.Exists(CfgFileName, False) then
Exit(False);
if IsUserConfigFileANSI(InstID) then
Encoding := TEncoding.Default
else
Encoding := TEncoding.Unicode;
Content := StrTrim(
TFileIO.ReadAllText(CfgFileName, Encoding, True)
);
Result := Content <> '';
end;
begin
fInstallID := piNone;
if NonEmptyCfgFileExists(piV5) then
fInstallID := piV5
else if NonEmptyCfgFileExists(piV4) then
fInstallID := piV4
else if not TCommandLineOpts.IsPortable then
begin
if NonEmptyCfgFileExists(piV3) then
fInstallID := piV3
else if TDirectory.Exists(DatabaseDirs(piV2)) then
fInstallID := piV2
else if NonEmptyCfgFileExists(piV1_9) then
fInstallID := piV1_9
else if NonEmptyCfgFileExists(piOriginal) then
fInstallID := piOriginal;
end;
end;
function TInstallInfo.IsPreviousUserConfigFileANSI: Boolean;
begin
Result := IsUserConfigFileANSI(fInstallID);
end;
class function TInstallInfo.IsUserConfigFileANSI(const InstallID: TInstallId):
Boolean;
begin
if TCommandLineOpts.IsPortable then
Result := False
else
Result := InstallID <= piV3;
end;
class function TInstallInfo.MakeFullUserPath(const Name: string): string;
begin
if Name = '' then
Exit('');
Result := IncludeTrailingPathDelimiter(
StrIf(
TCommandLineOpts.IsPortable,
TAppInfo.AppExeDir,
TSystemFolders.PerUserAppData
)
) + Name;
end;
function TInstallInfo.PreviousDatabaseDir: string;
begin
Result := DatabaseDirs(fInstallID);
end;
function TInstallInfo.PreviousDatabaseFileName: string;
begin
Result := IncludeTrailingPathDelimiter(PreviousDatabaseDir) + 'database.xml';
end;
function TInstallInfo.PreviousUserConfigFileName: string;
begin
Result := UserConfigFileNames(fInstallID);
end;
class function TInstallInfo.UserConfigFileNames(const InstID: TInstallId):
string;
begin
if InstID = piV5 then
Exit(TAppInfo.UserConfigFileName);
Result := '';
if TCommandLineOpts.IsPortable then
begin
if InstID = piV4 then
// v3 and earlier did not support portability
Result := MakeFullUserPath('AppData\User.config');
end
else
begin
case InstID of
piOriginal:
Result := MakeFullUserPath('DelphiDabbler\CodeSnip\CodeSnip.ini');
piV1_9, piV2:
Result := MakeFullUserPath('DelphiDabbler\CodeSnip\User.ini');
piV3:
Result := MakeFullUserPath('DelphiDabbler\CodeSnip\User.3.ini');
piV4:
Result := MakeFullUserPath('DelphiDabbler\CodeSnip.4\User.config');
end;
end;
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.