Files
Work/guitar/License.hpp
2024-08-07 09:16:27 -04:00

149 lines
3.4 KiB
C++

#ifndef _GUITAR_LICENSE_HPP_
#define _GUITAR_LICENSE_HPP_
#ifndef _COMMON_STRING_HPP_
#include <common/string.hpp>
#endif
#ifndef _COMMON_SYSTEMTIME_HPP_
#include <common/systime.hpp>
#endif
#ifndef _COMMON_VERSIONINFO_HPP_
#include <common/versioninfo.hpp>
#endif
#ifndef _COMMON_SDATE_HPP_
#include <common/sdate.hpp>
#endif
class License
{
public:
typedef enum LicenseType{Expires=01,Permanent=02};
typedef enum Feature{MIDIFeature};
License(void);
License(const License &license);
License(const String &strLicenseKey); // this should be a uuencoded, xor'd string
License(Block<String> &strLicenseKeyLines); // this should be a uuencoded string surrounded by "BEGIN LICENSE" / "END LICENSE" sequence
License &operator=(const License &license);
static String generate(const String &strProductVersion,const SystemTime &issueDate,LicenseType licenseType,int dayCount); // produces a uuencoded, xor'd string
bool fromString(const String &string); // this should be a uuencoded, xor'd string
bool fromLines(Block<String> &strLicenseLines); // this should be a uuencoded string surrounded by "BEGIN LICENSE" / "END LICENSE" sequence
bool isValid(void)const;
bool isExpiring(void)const;
bool allowFeature(Feature feature)const;
int getRemainingDays(void)const;
int getDayCount(void)const;
int getDay(void)const;
const String &getUUEncodedLicense(void)const;
private:
enum {XORMagic=72};
static int calculateChecksum(const String &string);
static String xor(const String &strLicense);
static String numericEncode(const String &strLicense);
static String numericDecode(const String &strLicense);
String mProductVersion;
String mUUEncodedLicense;
SystemTime mIssueDate;
LicenseType mLicenseType;
int mDayCount;
bool mIsValid;
static const String smBeginLicense;
static const String smEndLicense;
};
inline
License::License(void)
: mIsValid(false)
{
}
inline
License::License(const License &license)
{
*this=license;
}
inline
License::License(const String &strLicenseKey)
: mIsValid(false)
{
fromString(strLicenseKey);
}
inline
License::License(Block<String> &strLicenseLines)
: mIsValid(false)
{
fromLines(strLicenseLines);
}
inline
License &License::operator=(const License &license)
{
mProductVersion=license.mProductVersion;
mIssueDate=license.mIssueDate;
mLicenseType=license.mLicenseType;
mDayCount=license.mDayCount;
mIsValid=license.mIsValid;
return *this;
}
inline
bool License::isValid(void)const
{
return mIsValid;
}
inline
bool License::isExpiring(void)const
{
return Expires==mLicenseType;
}
inline
int License::getRemainingDays(void)const
{
if(!isExpiring())return -1;
SDate issueDate;
SDate expireDate;
SDate currDate;
issueDate.year(mIssueDate.year());
issueDate.month(mIssueDate.month());
issueDate.day(mIssueDate.day());
expireDate=issueDate;
expireDate=expireDate.daysAdd360(mDayCount);
return currDate.daysBetween360(expireDate);
}
inline
int License::getDay(void)const
{
SDate issueDate;
SDate currDate;
issueDate.year(mIssueDate.year());
issueDate.month(mIssueDate.month());
issueDate.day(mIssueDate.day());
return issueDate.daysBetween360(currDate)+1;
}
inline
int License::getDayCount(void)const
{
return mDayCount;
}
inline
bool License::allowFeature(Feature feature)const
{
if(isExpiring())return false;
return true;
}
inline
const String &License::getUUEncodedLicense(void)const
{
return mUUEncodedLicense;
}
#endif