Skip to content

Commit

Permalink
Detect if TinyCAD has been reinstalled.
Browse files Browse the repository at this point in the history
  • Loading branch information
matt123p committed Nov 14, 2019
1 parent c4ffe89 commit c5d5185
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
5 changes: 5 additions & 0 deletions installer/TinyCAD.nsi
Expand Up @@ -152,6 +152,10 @@ Section "MainSection" SEC01
File "..\examples\circuits\CurrSens.dsn"
File "..\examples\circuits\nanocomp6802.dsn"
File "..\examples\circuits\WaterSensor.dsn"

; A file purely for the purpose of setting a timestamp of when TinyCAD was installed
FileOpen $0 "$INSTDIR\installed.txt" w
FileClose $0

; Install the C++ redistributable
SetOutPath "$INSTDIR"
Expand Down Expand Up @@ -284,6 +288,7 @@ Section Uninstall
Delete "$INSTDIR\zlib.dll"
Delete "$INSTDIR\libpng16.dll"
Delete "$INSTDIR\TinyCad.exe"
Delete "$INSTDIR\installed.txt"

Delete "$SMPROGRAMS\TinyCAD\Uninstall.lnk"
Delete "$SMPROGRAMS\TinyCAD\TinyCAD Website.lnk"
Expand Down
50 changes: 47 additions & 3 deletions src/TinyCadRegistry.cpp
Expand Up @@ -13,7 +13,8 @@
#include <assert.h>

const CString CTinyCadRegistry::M_SKEY = "Software\\TinyCAD\\TinyCAD\\1x20";
const CString CTinyCadRegistry::M_SEXAMPLESSETUP = "ExamplesSetup";
const CString CTinyCadRegistry::M_BUILDID = "BuildID";
const CString CTinyCadRegistry::M_INSTALLED = "Installed";
const CString CTinyCadRegistry::M_SPAGESIZE = "PageSize";
const CString CTinyCadRegistry::M_SPRINTSCALE = "PrintScaleFactor";
const CString CTinyCadRegistry::M_SPRINTBANDW = "PrintBandW";
Expand All @@ -34,10 +35,17 @@ CTinyCadRegistry::CTinyCadRegistry() :
{
m_oKey.Create(HKEY_CURRENT_USER, M_SKEY);

// Get the installer date
CString installedAt = GetInstalledFileTime();

// Do we need to copy the example files?
if (super::GetString(M_SEXAMPLESSETUP, "") != BUILD_UUID)
if (super::GetString(M_BUILDID, "") != BUILD_UUID || super::GetString(M_INSTALLED, "") != installedAt)
{
CopyExampleFiles();
if (!installedAt.IsEmpty())
{
super::Set(M_INSTALLED, installedAt);
}
}

// Does the registry information exist - if not create it
Expand All @@ -51,6 +59,42 @@ CTinyCadRegistry::~CTinyCadRegistry()
{
}

//-------------------------------------------------------------------------
//-- Try and determine when TinyCAD was installed, so we can see if it has
// changed/been re-installed.
CString CTinyCadRegistry::GetInstalledFileTime()
{
CString app_installed_file = CTinyCadApp::GetAppDir("") + _T("installed.txt");
auto hFile = CreateFile(app_installed_file, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);

if (hFile == INVALID_HANDLE_VALUE)
{
// File not found
return "";
}
FILETIME ftCreate, ftAccess, ftWrite;
SYSTEMTIME stUTC, stLocal;
DWORD dwRet;

// Retrieve the file times for the file.
if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
{
return "";
}

// Convert the last-write time to local time.
FileTimeToSystemTime(&ftWrite, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

// Build a string showing the date and time.
CString r;
r.Format(TEXT("%02d/%02d/%d %02d:%02d"),
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);

return r;
}

//-------------------------------------------------------------------------
//-- Copy the example files from the installation folder to the users
Expand Down Expand Up @@ -84,7 +128,7 @@ void CTinyCadRegistry::CopyExampleFiles()
}

// Set a flag in the registry
super::Set(M_SEXAMPLESSETUP, CString(BUILD_UUID));
super::Set(M_BUILDID, CString(BUILD_UUID));

}

Expand Down
6 changes: 5 additions & 1 deletion src/TinyCadRegistry.h
Expand Up @@ -85,6 +85,9 @@ class CTinyCadRegistry: public CRegistry
//-- What was the last version of TinyCAD update the user was told about
static void SetLastAutomaticUpdateVersion(CString version);

// Get when TinyCAD was installed
static CString GetInstalledFileTime();

//=====================================================================
//== class constants ==
//=====================================================================
Expand All @@ -93,7 +96,8 @@ class CTinyCadRegistry: public CRegistry
static const CString M_SKEY;

static const CString M_SPAGESIZE;
static const CString M_SEXAMPLESSETUP;
static const CString M_BUILDID;
static const CString M_INSTALLED;
static const CString M_SPRINTSCALE;
static const CString M_SPRINTBANDW;
static const CString M_SMDIMAXIMIZE;
Expand Down

0 comments on commit c5d5185

Please sign in to comment.