diff --git a/licheck/licheck.cpp b/licheck/licheck.cpp index 6fdd42b8..ba941162 100644 --- a/licheck/licheck.cpp +++ b/licheck/licheck.cpp @@ -163,6 +163,19 @@ License::License(const string &licenseId, const string &licensee, const string & Returns all valid license info entries. */ +bool LicenseChecker::isProductLicensed(const string &productName) +{ + const string licensePath = findLicenseFile(); + const vector licenses = readLicenseFile(licensePath); + + for (License license: licenses) { + if (license.isValid() + && license.allowedProduct(productName.c_str())) + return true; + } + return false; +} + vector LicenseChecker::readLicenseFile(const string &licensepath) { ifstream licenseFile; @@ -379,6 +392,68 @@ bool License::allowedTarget(const char *xplatformStr) return true; } +/*! + Checks whether \a product is supported by current key. + + Returns \c true on success, \c false otherwise. + + An error message is available through lastError(). + */ +bool License::allowedProduct(const char *productStr) +{ + uint products = m_keydec.getProducts(); + uint product = 0; + + string productName; + if (strstr(productStr, "QtEnterprise") + || strstr(productStr, "QtEnterpriseOnline")) { + product = KeyDecoder::QtEnterprise | KeyDecoder::QtEnterpriseOnline; + productName = "Enterprise"; + } else if (strstr(productStr, "QtMobile")) { + product = KeyDecoder::QtMobile; + productName = "Mobile"; + } else if (strstr(productStr, "QtIndie")) { + product = KeyDecoder::QtIndie; + productName = "Indie"; + } else if (strstr(productStr, "Automotive")) { + product = KeyDecoder::Automotive; + productName = "Qt Automotive Suite"; + } else if (strstr(productStr, "DeviceCreation")) { + product = KeyDecoder::DeviceCreation; + productName = "Qt for Device Creation"; + } else if (strstr(productStr, "ApplicationDev")) { + product = KeyDecoder::ApplicationDev; + productName = "Qt for Application Development"; + } else if (strstr(productStr, "Automation")) { + product = KeyDecoder::Automation; + productName = "Qt for Automation"; + } else if (strstr(productStr, "SafeRenderer")) { + product = KeyDecoder::SafeRenderer; + productName = "Qt Safe Renderer"; + } else if (strstr(productStr, "DesignStudio")) { + product = KeyDecoder::DesignStudio; + productName = "Qt Design Studio"; + } else if (strstr(productStr, "Qt3DStudio")) { + product = KeyDecoder::Qt3DStudio; + productName = "Qt 3D Studio"; + } else if (strstr(productStr, "MCU")) { + product = KeyDecoder::MCU; + productName = "Qt for MCUs"; + } else { + m_error = "Error: Unsupported product " + std::string(productStr) + "!"; + return false; + } + + if (!(product & products)) { + m_error = "Error: You are not licensed for the " + productName + " product!\n\n" + "Please log into your Qt Account at https://account.qt.io/\n" + "to upgrade your license to include the\n" + productName + " product."; + return false; + } + + return true; +} + string License::getLicensee() const { return m_licensee; diff --git a/licheck/licheck.h b/licheck/licheck.h index c2584388..e777b9c8 100644 --- a/licheck/licheck.h +++ b/licheck/licheck.h @@ -45,6 +45,7 @@ public: bool allowedHost(const char *platform); bool allowedTarget(const char *xplatform); + bool allowedProduct(const char *product); bool isExpired(CDate releaseDate); bool isSupported(); @@ -77,6 +78,7 @@ public: }; + static bool isProductLicensed(const string &productName); static vector readLicenseFile(const string &path); static License selectLicense(const vector &licenses); diff --git a/licheck/main.cpp b/licheck/main.cpp index 709faadc..40dac481 100644 --- a/licheck/main.cpp +++ b/licheck/main.cpp @@ -55,6 +55,13 @@ public: && (!checkTarget || license.allowedTarget(xplatform)) && !license.isExpired(releaseDate); } + + bool accept(License &license, const char *product) + { + return license.isValid() + && license.allowedProduct(product); + } + private: const char *platform; const char *xplatform; @@ -90,6 +97,13 @@ int main(int argc, char **argv) * platform is the build platform * xplatform is the target platform * + * for other usage: + * + * input parameters: "check_product", product + * + * where + * product is the name of the product such as "DeviceCreation", "ApplicationDev", "MCU" etc. + * * Returns * 0 - success * 1 - invalid command line arguments @@ -116,52 +130,79 @@ int main(int argc, char **argv) const char *outpath = NULL; const char *platform = NULL; const char *xplatform = NULL; + const char *productName = NULL; string releaseDateStr; + CDate releaseDate; mode = argv[1]; const bool checkMode = (strcmp(mode, "check") == 0); + const bool productCheckMode = (strcmp(mode, "check_product") == 0); - if (checkMode) { - if (argc != 5) { + if (productCheckMode) { + if (argc != 3) { std::cerr << endl << endl << "Error: Insufficient command line arguments " - "for licheck (qmake)" << endl << endl; + "for licheck" << endl << endl; return 1; } - releaseDateStr = argv[2]; - platform = argv[3]; - xplatform = argv[4]; + productName = argv[2]; + releaseDate = CDate::today(); } else { - if (argc != 5 && argc != 6) { - std::cerr << endl << endl << "Error: Insufficient command line arguments " - "for licheck (configure)" << endl << endl; - return 1; + if (checkMode) { + if (argc != 5) { + std::cerr << endl << endl << "Error: Insufficient command line arguments " + "for licheck (qmake)" << endl << endl; + return 1; + } + releaseDateStr = argv[2]; + platform = argv[3]; + xplatform = argv[4]; + } else { + if (argc != 5 && argc != 6) { + std::cerr << endl << endl << "Error: Insufficient command line arguments " + "for licheck (configure)" << endl << endl; + return 1; + } + licenseConfirmed = argv[1]; + srcpath = argv[2]; + outpath = argv[3]; + platform = argv[4]; + if (argc == 6) + xplatform = argv[5]; + + releaseDateStr = LicenseChecker::readReleaseDate(srcpath); } - licenseConfirmed = argv[1]; - srcpath = argv[2]; - outpath = argv[3]; - platform = argv[4]; - if (argc == 6) - xplatform = argv[5]; - - releaseDateStr = LicenseChecker::readReleaseDate(srcpath); if (releaseDateStr.empty()) { cerr << endl << "Error: Cannot determine package release date. " << "Please try re-installing." << endl << endl; return 2; } - } - - CDate releaseDate = LicenseChecker::parseReleaseDate(releaseDateStr); - if (releaseDate.julianDate() == 0) { - cerr << endl << "Error: Cannot determine package release date. " - << "Please try re-installing." << endl << endl; - return 2; + releaseDate = LicenseChecker::parseReleaseDate(releaseDateStr); + if (releaseDate.julianDate() == 0) { + cerr << endl << "Error: Cannot determine package release date. " + << "Please try re-installing." << endl << endl; + return 2; + } } LicenseValidator validator(platform, xplatform, releaseDate); std::string licenseFile = LicenseChecker::findLicenseFile(); - if (checkMode) { + if (productCheckMode) { + if (!licenseFile.empty()) { + std::vector licenses = LicenseChecker::readLicenseFile(licenseFile); + for (size_t i = 0; i < licenses.size(); ++i) { + if (validator.accept(licenses[i], productName)) + return 0; + } + // no valid licenses + for (size_t i = 0; i < licenses.size(); ++i) { + License &license = licenses[i]; + if (!validator.accept(license, productName)) + std::cerr << endl << endl << license.lastError() << endl << endl; + } + } + return 3; + } else if (checkMode) { if (!licenseFile.empty()) { std::vector licenses = LicenseChecker::readLicenseFile(licenseFile); for (size_t i = 0; i < licenses.size(); ++i) {