LicenseSpring C++ SDK
Easily add Software Licensing to your application
LicenseHandler.h
Go to the documentation of this file.
1 #ifndef LS_LICENSE_HANDLER_H
2 #define LS_LICENSE_HANDLER_H
3 
4 #ifdef _MSC_VER
5 #pragma once
6 #endif
7 
8 #include "Configuration.h"
9 #include "ProductDetails.h"
10 #include "UserLicensesData.h"
11 #include "License.h"
12 #include "LicenseStorage.h"
13 #include "CryptoProvider.h"
14 
15 namespace LicenseSpring
16 {
17 class LicenseHandlerImpl;
18 
28 {
29 public:
38  static LicenseHandler &instance(bool globalInit = false);
39 
43  LicenseHandler(Configuration::ptr_t config = nullptr, LicenseStorage::ptr_t storage = nullptr);
44  LicenseHandler(const LicenseHandler &) = delete;
45  LicenseHandler &operator=(const LicenseHandler &) = delete;
47  ~LicenseHandler();
48 
50  void reconfigure(Configuration::ptr_t config, LicenseStorage::ptr_t storage = nullptr);
52  Configuration::ptr_t currentConfig() const;
53 
55  const std::wstring &dataLocation() const;
57  void setDataLocation(const std::wstring &path);
59  const std::wstring &licenseFileName() const;
61  void setLicenseFileName(const std::wstring &name);
63  const std::wstring &licenseFilePath() const;
64 
65  // ---------- Requests to the LicenseSpring backend ----------
66 
68  bool isOnline() const;
70  ProductDetails::ptr_t getProductDetails(bool includeLatestVersion = false) const;
72  ProductDetails::ptr_t getProductDetails(
73  bool includeLatestVersion, bool includeCustomFields, const std::string &env) const;
75  const LicenseID &getTrialLicense(
76  Customer::ptr_t user = nullptr, const std::string &licensePolicy = std::string()) const;
78  const LicenseID &getTrialLicense(const std::string &userEmail) const;
80  const std::string &getSSOUrl(
81  const std::string &accountCode = std::string(), bool useAuthCode = true);
83  const std::string &getAirGapActivationCode(
84  const std::string &initializationCode, const std::string &licenseKey);
86  const std::string &getAirGapDeactivationCode(const std::string &initializationCode);
88  void activateAirGapLicense(const std::string &confirmationCode,
89  const std::wstring &activationResponseFile, const std::string &licenseKey,
90  uint32_t policyId);
92  void deactivateAirGapLicense(const std::string &confirmationCode);
94  std::vector<LicenseUser::ptr_t> getLicenseUsers(const Customer &customer);
96  std::vector<LicenseUser::ptr_t> getLicenseUsers(const std::string &customerEmail);
98  std::vector<UserLicensesData::ptr_t> getUserLicenses(
99  const std::string &username, const std::string &password);
101  void activateLicense(const LicenseID &licenseID);
103  void activateLicense(
104  const LicenseID &licenseID, const std::vector<DeviceVariable> &deviceVariables);
106  void activateLicense(const std::string &authData,
107  const std::string &accountCode = std::string(), bool useAuthCode = true);
109  bool deactivateLicense();
111  bool changePassword(const std::string &password, const std::string &newPassword,
112  const std::string &user = std::string());
114  void relinkLicense(
115  DeviceIDAlgorithm deviceIdAlgorithm, const std::string &userPassword = std::string());
117  InstallationFile::ptr_t checkLicense(
118  const InstallFileFilter &filter = InstallFileFilter(), bool includeExpiredFeatures = false);
120  const std::vector<std::string> &getVersionList(const LicenseID &licenseID = LicenseID(),
121  const InstallFileFilter &filter = InstallFileFilter());
123  InstallationFile::ptr_t getInstallFile(const LicenseID &licenseID = LicenseID(),
124  const std::string &version = std::string(),
125  const InstallFileFilter &filter = InstallFileFilter());
127  void sendDeviceVariables();
129  std::vector<DeviceVariable> getDeviceVariables(bool getFromBackend = false);
131  void syncConsumption(int32_t requestOverage = -1);
133  void syncFeatureConsumption(const std::string &featureCode = std::string());
135  void registerFloatingLicense();
137  void borrowFloatingLicense(uint32_t hours, uint32_t days = 0);
139  void borrowFloatingLicense(const std::string &borrowEndDateTime = std::string());
141  void releaseFloatingLicense();
143  void registerFloatingFeature(const std::string &featureCode, bool addToWatchdog = true);
145  void releaseFloatingFeature(const std::string &featureCode);
146 
147  // ------------------------------------------------------------
148 
149  // -------- Local license management and helper methods -------
150  bool isInitialized() const;
151  bool isLicenseExists() const;
153  void updateConsumption(int32_t value = 1, bool saveLicense = true);
155  void updateFeatureConsumption(
156  const std::string &featureCode, int32_t value = 1, bool saveLicense = true);
158  void checkLicenseLocal(bool verifySignature = false);
160  void clearLocalStorage(); // remove local license and also all license information from memory
162  bool isLicenseFileCorrupted();
163 
165  void addUserData(const CustomField &data, bool saveLicense = true);
167  void removeUserData(const std::string &key = std::string(), bool saveLicense = true);
169  const std::vector<CustomField> &userData();
171  std::string userData(const std::string &key);
172 
174  bool isAutoReleaseSet() const;
176  void setAutoRelease(bool autoRelease);
178  void setupLicenseWatchdog(LicenseWatchdogCallback callback, uint32_t timeout = 0);
180  void resumeLicenseWatchdog();
182  void stopLicenseWatchdog();
184  void setupFeatureWatchdog(LicenseWatchdogCallback callback, uint32_t timeout = 0);
186  void resumeFeatureWatchdog();
188  void stopFeatureWatchdog();
189 
191  void addDeviceVariable(
192  const std::string &name, const std::string &value, bool saveLicense = true);
194  void addDeviceVariable(const DeviceVariable &variable, bool saveLicense = true);
196  void addDeviceVariables(const std::vector<DeviceVariable> &variables);
198  DeviceVariable deviceVariable(const std::string &name) const;
200  const std::string &deviceVariableValue(const std::string &name) const;
201  // ------------------------------------------------------------
202 
203  // ---------------- Offline license management ----------------
205  const std::wstring &createOfflineActivationFile(
206  const LicenseID &licenseID, const std::wstring &activationRequestFile = std::wstring());
208  const std::wstring &createOfflineActivationFile(const LicenseID &licenseID,
209  const std::vector<DeviceVariable> &deviceVariables,
210  const std::wstring &activationRequestFile = std::wstring());
212  void activateLicenseOffline(const std::wstring &activationResponseFile = std::wstring());
214  const std::wstring &deactivateLicenseOffline(
215  const std::wstring &deactivationRequestFile = std::wstring());
217  bool updateOffline(const std::wstring &path = std::wstring(), bool resetConsumption = false);
218  // ------------------------------------------------------------
219 
220  // ----------------------- License data -----------------------
222  bool isLicenseValid() const;
224  bool isLicenseTrial() const;
226  bool isLicenseAirGapped() const;
228  bool isLicenseActive() const;
230  bool isLicenseEnabled() const;
232  bool isLicenseExpired() const;
234  bool isLicenseMaintenanceExpired() const;
236  bool isLicenseOfflineActivated() const;
238  bool isLicenseFloating() const;
240  bool isLicenseBorrowed() const;
242  bool isLicenseOveragesAllowed() const;
244  bool isLicenseUnlimitedConsumptionAllowed() const;
246  bool isLicenseNegativeConsumptionAllowed() const;
248  bool isLicenseVMAllowed() const;
250  bool isSubscriptionGracePeriodStarted() const;
252  bool isGracePeriodStarted() const;
254  tm gracePeriodEndDateTime() const;
256  tm gracePeriodEndDateTimeUTC() const;
258  int gracePeriodHoursRemaining() const;
260  uint32_t getLicenseTrialPeriod() const;
261 
263  const LicenseID &getLicenseID() const;
265  const std::string &getLicenseKey() const;
267  const std::string &getLicenseUser() const;
268 
270  const uint64_t getLicenseServerId() const;
272  const std::string &getLicenseStatusStr() const;
274  const std::string &getLicenseStartDate() const;
276  const std::string &getLicenseMetadata() const;
278  const std::string &getOrderStoreId() const;
279 
281  LicenseType getLicenseType() const;
282 
284  tm getLicenseExpiryDate() const;
286  tm getLicenseExpiryDateUtc() const;
288  tm getLicenseMaintenancePeriod() const;
290  tm getLicenseMaintenancePeriodUtc() const;
292  tm getLicenseLastCheckDate() const;
294  tm getLicenseLastCheckDateUtc() const;
296  tm getLicenseFloatingEndDateTime() const;
298  tm getLicenseFloatingEndDateTimeUtc() const;
300  int getDaysRemaining() const;
302  int getMaintenanceDaysRemaining() const;
304  int getDaysPassedSinceLastCheck() const;
305 
307  uint32_t getSubscriptionLicenseGracePeriod() const;
309  tm validityWithGracePeriod() const;
311  tm validityWithGracePeriodUtc() const;
312 
314  const Customer &getLicenseOwner() const;
316  LicenseUser::ptr_t getLicenseUserPtr() const;
317 
318  bool hasLicenseFeatures() const;
320  const LicenseFeature &getLicenseFeature(const std::string &featureCode) const;
322  const std::vector<LicenseFeature> &getLicenseFeatures() const;
323 
324  bool hasLicenseCustomFields() const;
326  const std::vector<CustomField> &getLicenseCustomFields() const;
327 
329  uint32_t getLicenseTimesActivated() const;
331  uint32_t getLicenseMaxActivations() const;
333  uint32_t getLicenseTransferCount() const;
335  int32_t getLicenseTransferLimit() const;
337  bool isDeviceTransferAllowed() const;
339  bool isDeviceTransferLimited() const;
340 
342  uint32_t getLicensePolicyId() const;
343 
344  // Meaningful only for Consumption license
346  int32_t getLicenseTotalConsumption() const;
348  int32_t getLicenseMaxConsumption() const;
350  int32_t getLicenseMaxOverages() const;
351  bool isResetLicenseConsumptionEnabled() const;
352  ConsumptionPeriod getLicenseConsumptionPeriod() const;
353 
354  // Meaningful only for Floating license
356  uint32_t getLicenseFloatingTimeout() const;
358  uint32_t getLicenseFloatingInUseCount() const;
360  uint32_t getLicenseMaxFloatingUsers() const;
362  uint32_t getMaxBorrowTime() const;
363  // ------------------------------------------------------------
364 
365  // ---------------------- Error handling ----------------------
366  void enableExceptions(bool enable); // By default this class does not throw exceptions
367  bool isExceptionsEnabled() const;
368  bool wasError() const;
369  LSErrorCode getLastError() const;
370  const std::string &getLastErrorMsg() const;
371  bool isInitializationError() const;
372  bool isInternalServerError() const;
373  bool isNetworkTimeoutError() const;
374  bool isNoInternetError() const;
375  bool isActivationFailed() const;
376  bool isLicenseExpiredError() const;
377  bool isLicenseNotFoundError() const;
378  bool isLicenseStateError() const;
379  bool isHardwareIDError() const;
380  bool isGeneralError() const;
381  bool isLicenseFeatureError() const;
382  bool canIgnoreError() const; // No Internet, network timeout and server errors can be ignored
383  // ------------------------------------------------------------
384 
385 private:
386  LicenseHandlerImpl *m_impl;
387 };
388 
389 } // namespace LicenseSpring
390 
391 #endif // LS_LICENSE_HANDLER_H
LicenseSpring::Configuration::ptr_t
std::shared_ptr< Configuration > ptr_t
Definition: Configuration.h:21
LicenseSpring::InstallFileFilter
Helper struct for filtering installation files by environment and channel.
Definition: InstallationFile.h:18
LicenseSpring::LicenseStorage::ptr_t
std::shared_ptr< LicenseStorage > ptr_t
Definition: LicenseStorage.h:20
LicenseSpring::LicenseFeature
Class that encapsulates information about license feature.
Definition: LicenseFeature.h:21
LicenseSpring::Customer
Class that encapsulates information about a customer.
Definition: Customer.h:17
LicenseSpring::LicenseUser::ptr_t
std::shared_ptr< LicenseUser > ptr_t
Definition: LicenseUser.h:21
LicenseSpring::LicenseID
Class that represents License identifier.
Definition: LicenseID.h:20
ProductDetails.h
LicenseSpring::DeviceVariable
Class for storing key-value device variable.
Definition: DeviceVariable.h:19
LSErrorCode
LSErrorCode
LicenseSpring Error codes enum.
Definition: PODTypes.h:45
LicenseSpring
Definition: BaseManager.h:16
LicenseSpring::ConsumptionPeriod
Class used for encapsulating LSConsumptionPeriod and converting cosumption period to/from string.
Definition: ConsumptionPeriod.h:11
UserLicensesData.h
DeviceIDAlgorithm
DeviceIDAlgorithm
Type of Device ID algorithm used by the SDK.
Definition: PODTypes.h:114
LicenseSpring::LicenseHandler
This class encapsulates functionality of the both LicenseManager and the License. .
Definition: LicenseHandler.h:27
Configuration.h
LS_API
#define LS_API
Definition: APIDef.h:23
LicenseSpring::LicenseType
Class used for encapsulating the type of license, comparing types of licenses, and converting them to...
Definition: LicenseType.h:19
LicenseStorage.h
LicenseSpring::CustomField
Class for storing key-value data field.
Definition: CustomField.h:17
LicenseSpring::Customer::ptr_t
std::shared_ptr< Customer > ptr_t
Definition: Customer.h:20
LicenseSpring::LicenseWatchdogCallback
std::function< void(const LicenseSpringException &ex)> LicenseWatchdogCallback
Callback function which being called in case of error in background thread (watchdog thread).
Definition: License.h:30
CryptoProvider.h
LicenseSpring::ProductDetails::ptr_t
std::shared_ptr< ProductDetails > ptr_t
Definition: ProductDetails.h:25
License.h
LicenseSpring::InstallationFile::ptr_t
std::shared_ptr< InstallationFile > ptr_t
Definition: InstallationFile.h:59