{
* DataLocations.ps
*
* Pascal script for use in [Code] Section of CodeSnip's Install.iss.
*
* Gets information about location of application data folder and any existing
* config files and databaseon on the system where CodeSnip is being installed.
* Sets global variables storing folder paths that indicate where config files
* and database can be found.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is DataLocations.ps.
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2008-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
{
File locations in CodeSnip versions:
+ Versions up to v1.8.11:
- Config file is CodeSnip.ini and is stored in DelphiDabbler\CodeSnip sub-
directory of the per-user application data folder.
- Main database is in DelphiDabbler\CodeSnip\Data sub-directory of the
per-user application data folder.
- No user defined database.
+ From v1.9 to v1.9.4:
- Two config files:
o Common.ini in DelphiDabbler\CodeSnip sub-directory of common application
data foler.
o User.ini in DelphiDabbler\CodeSnip sub-directory of per user application
data foler.
- Main database moved to the DelphiDabbler\CodeSnip\Data sub-directory of
the common application data folder.
- No user defined database.
+ All v2 versions:
- Two config files:
o Common.ini in DelphiDabbler\CodeSnip sub-directory of common application
data foler.
o User.ini in DelphiDabbler\CodeSnip sub-directory of per user application
data foler.
- Main database moved to the DelphiDabbler\CodeSnip\Data sub-directory of
the common application data folder.
- User defined database in the DelphiDabbler\CodeSnip\UserData sub-directory
of the per user application data folder.
+ From v3.0:
- Two config files:
o Common.ini in DelphiDabbler\CodeSnip sub-directory of common application
data foler.
o User.3.ini in DelphiDabbler\CodeSnip sub-directory of per user
application data foler.
- Main database remains in the DelphiDabbler\CodeSnip\Data sub-directory of
the common application data folder.
- User defined database moves to the DelphiDabbler\CodeSnip\UserData.3 sub-
directory of the per user application data folder.
}
type
// Different editions of program with different file locations
TFileLocations = (
flNone, // CodeSnip not installed
flOriginal, // original locations
flV1_9, // v1.9 to v1.9.4
flV2, // all v2 versions
flV3 // v3.0 and later
);
var
// Name of original user's CodeSnip data folder
gUserAppDataDir: string;
// Name of CodeSnip's common data folder
gCommonAppDataDir: string;
// Information about location of config file(s)
gFileLocation: TFileLocations;
function MainDatabaseExists: Boolean;
{Checks if database is installed in "new" location, i.e. under common app data
folder.
@return True if database is installed in new location, False if not.
}
begin
Result := FileExists(
AddBackslash(gCommonAppDataDir) + 'Data\categories.ini'
);
end;
function DataConversionRequired: Boolean;
{Checks if database and config files need to be converted and copied to new
location in order for installed application to use them.
@return True if data conversion required, False if not.
}
begin
Result := (gFileLocation = flOriginal) or (gFileLocation = flV1_9) or
(gFileLocation = flV2);
end;
procedure InitAppDataFolders;
{Records the application data folders: both common and user. The user folder
relates to the original user. This is not necessarily the user who is
installing the application on Vista.
}
var
HelperApp: string; // name of setup helper app
TmpDir: string; // temp dir where helper app writes required info
TmpFile: string; // temp file that received helper app's info
Res: Integer; // return code from helper app: ignored
UserAppDir: string; // user's application data directory
begin
// Create name of a temporary directory in which helper app can store data
TmpDir := ExpandConstant('{commonappdata}\DelphiDabbler\~Tmp');
TmpFile := TmpDir + '\data';
ForceDirectories(TmpDir);
// Copy helper app into temp directory
HelperApp := ExpandConstant('{#SetupHelper}');
ExtractTemporaryFile(HelperApp);
// Run helper app as original user. Helper app writes information relating to
// original user to a ini file in temp directory created above.
ExecAsOriginalUser(
ExpandConstant('{tmp}\' + HelperApp),
'"' + TmpFile + '"',
'',
SW_HIDE,
ewWaitUntilTerminated,
Res
);
// Get original user's application data directory from temp file
UserAppDir := GetIniString('SpecialFolders', 'CSIDL_APPDATA', '', TmpFile);
// Delete temporary directory
DelTree(TmpDir, True, True, True);
// Record per-user and common application data directories used by CodeSnip
// per-user directory relates to original user.
gUserAppDataDir := AddBackslash(UserAppDir) + 'DelphiDabbler\CodeSnip';
gCommonAppDataDir := ExpandConstant('{commonappdata}\DelphiDabbler\CodeSnip');
end;
procedure InitDataLocations;
{Initialises global variables that store information about location of config
files and database.
}
begin
InitAppDataFolders;
if FileExists(AddBackslash(gCommonAppDataDir) + 'Common.ini') then
begin
// We have CodeSnip v1.9 or later
if FileExists(AddBackslash(gUserAppDataDir) + 'User.3.ini') then
// We have CodeSnip v3 or later
gFileLocation := flV3
else
begin
// We have CodeSnip v1.9 to v2.4.1
if FileExists(
AddBackslash(gUserAppDataDir) + 'UserData\database.xml'
) then
// We have CodeSnip v2.x
gFileLocation := flV2
else
// We have CodeSnip v1.9 to v1.9.4 (or v2.x with no user database)
gFileLocation := flV1_9;
end;
end
else if FileExists(AddBackslash(gUserAppDataDir) + 'CodeSnip.ini') then
// We have up to v1.8.11
gFileLocation := flOriginal
else
// We have no installation
gFileLocation := flNone;
end;