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(
138  uint32_t hours, uint32_t days = 0, const std::string &password = std::string());
140  void borrowFloatingLicense(const std::string &borrowEndDateTime = std::string(),
141  const std::string &password = std::string());
143  void releaseFloatingLicense();
145  void registerFloatingFeature(const std::string &featureCode, bool addToWatchdog = true);
147  void releaseFloatingFeature(const std::string &featureCode);
148 
149  // ------------------------------------------------------------
150 
151  // -------- Local license management and helper methods -------
152  bool isInitialized() const;
153  bool isLicenseExists() const;
155  void updateConsumption(int32_t value = 1, bool saveLicense = true);
157  void updateFeatureConsumption(
158  const std::string &featureCode, int32_t value = 1, bool saveLicense = true);
160  void checkLicenseLocal(bool verifySignature = false);
162  void clearLocalStorage(); // remove local license and also all license information from memory
164  bool isLicenseFileCorrupted();
165 
167  void addUserData(const CustomField &data, bool saveLicense = true);
169  void removeUserData(const std::string &key = std::string(), bool saveLicense = true);
171  const std::vector<CustomField> &userData();
173  std::string userData(const std::string &key);
174 
176  bool isAutoReleaseSet() const;
178  void setAutoRelease(bool autoRelease);
180  void setupLicenseWatchdog(LicenseWatchdogCallback callback, uint32_t timeout = 0);
182  void resumeLicenseWatchdog();
184  void stopLicenseWatchdog();
186  void setupFeatureWatchdog(LicenseWatchdogCallback callback, uint32_t timeout = 0);
188  void resumeFeatureWatchdog();
190  void stopFeatureWatchdog();
191 
193  void addDeviceVariable(
194  const std::string &name, const std::string &value, bool saveLicense = true);
196  void addDeviceVariable(const DeviceVariable &variable, bool saveLicense = true);
198  void addDeviceVariables(const std::vector<DeviceVariable> &variables);
200  DeviceVariable deviceVariable(const std::string &name) const;
202  const std::string &deviceVariableValue(const std::string &name) const;
203  // ------------------------------------------------------------
204 
205  // ---------------- Offline license management ----------------
207  const std::wstring &createOfflineActivationFile(
208  const LicenseID &licenseID, const std::wstring &activationRequestFile = std::wstring());
210  const std::wstring &createOfflineActivationFile(const LicenseID &licenseID,
211  const std::vector<DeviceVariable> &deviceVariables,
212  const std::wstring &activationRequestFile = std::wstring());
214  void activateLicenseOffline(const std::wstring &activationResponseFile = std::wstring());
216  const std::wstring &deactivateLicenseOffline(
217  const std::wstring &deactivationRequestFile = std::wstring());
219  bool updateOffline(const std::wstring &path = std::wstring(), bool resetConsumption = false);
220  // ------------------------------------------------------------
221 
222  // ----------------------- License data -----------------------
224  bool isLicenseValid() const;
226  bool isLicenseTrial() const;
228  bool isLicenseAirGapped() const;
230  bool isLicenseActive() const;
232  bool isLicenseEnabled() const;
234  bool isLicenseExpired() const;
236  bool isLicenseMaintenanceExpired() const;
238  bool isLicenseOfflineActivated() const;
240  bool isLicenseFloating() const;
242  bool isLicenseBorrowed() const;
244  bool isLicenseOveragesAllowed() const;
246  bool isLicenseUnlimitedConsumptionAllowed() const;
248  bool isLicenseNegativeConsumptionAllowed() const;
250  bool isLicenseVMAllowed() const;
252  bool isSubscriptionGracePeriodStarted() const;
254  bool isGracePeriodStarted() const;
256  tm gracePeriodEndDateTime() const;
258  tm gracePeriodEndDateTimeUTC() const;
260  int gracePeriodHoursRemaining() const;
262  uint32_t getLicenseTrialPeriod() const;
263 
265  const LicenseID &getLicenseID() const;
267  const std::string &getLicenseKey() const;
269  const std::string &getLicenseUser() const;
270 
272  const uint64_t getLicenseServerId() const;
274  const std::string &getLicenseStatusStr() const;
276  const std::string &getLicenseStartDate() const;
278  const std::string &getLicenseMetadata() const;
280  const std::string &getOrderStoreId() const;
281 
283  LicenseType getLicenseType() const;
284 
286  tm getLicenseExpiryDate() const;
288  tm getLicenseExpiryDateUtc() const;
290  tm getLicenseMaintenancePeriod() const;
292  tm getLicenseMaintenancePeriodUtc() const;
294  tm getLicenseLastCheckDate() const;
296  tm getLicenseLastCheckDateUtc() const;
298  tm getLicenseFloatingEndDateTime() const;
300  tm getLicenseFloatingEndDateTimeUtc() const;
302  int getDaysRemaining() const;
304  int getMaintenanceDaysRemaining() const;
306  int getDaysPassedSinceLastCheck() const;
307 
309  uint32_t getSubscriptionLicenseGracePeriod() const;
311  tm validityWithGracePeriod() const;
313  tm validityWithGracePeriodUtc() const;
314 
316  const Customer &getLicenseOwner() const;
318  LicenseUser::ptr_t getLicenseUserPtr() const;
319 
320  bool hasLicenseFeatures() const;
322  const LicenseFeature &getLicenseFeature(const std::string &featureCode) const;
324  const std::vector<LicenseFeature> &getLicenseFeatures() const;
325 
326  bool hasLicenseCustomFields() const;
328  const std::vector<CustomField> &getLicenseCustomFields() const;
329 
331  uint32_t getLicenseTimesActivated() const;
333  uint32_t getLicenseMaxActivations() const;
335  uint32_t getLicenseTransferCount() const;
337  int32_t getLicenseTransferLimit() const;
339  bool isDeviceTransferAllowed() const;
341  bool isDeviceTransferLimited() const;
342 
344  uint32_t getLicensePolicyId() const;
345 
346  // Meaningful only for Consumption license
348  int32_t getLicenseTotalConsumption() const;
350  int32_t getLicenseMaxConsumption() const;
352  int32_t getLicenseMaxOverages() const;
353  bool isResetLicenseConsumptionEnabled() const;
354  ConsumptionPeriod getLicenseConsumptionPeriod() const;
355 
356  // Meaningful only for Floating license
358  uint32_t getLicenseFloatingTimeout() const;
360  uint32_t getLicenseFloatingInUseCount() const;
362  uint32_t getLicenseMaxFloatingUsers() const;
364  uint32_t getMaxBorrowTime() const;
365  // ------------------------------------------------------------
366 
367  // ---------------------- Error handling ----------------------
368  void enableExceptions(bool enable); // By default this class does not throw exceptions
369  bool isExceptionsEnabled() const;
370  bool wasError() const;
371  LSErrorCode getLastError() const;
372  const std::string &getLastErrorMsg() const;
373  bool isInitializationError() const;
374  bool isInternalServerError() const;
375  bool isNetworkTimeoutError() const;
376  bool isNoInternetError() const;
377  bool isActivationFailed() const;
378  bool isLicenseExpiredError() const;
379  bool isLicenseNotFoundError() const;
380  bool isLicenseStateError() const;
381  bool isHardwareIDError() const;
382  bool isGeneralError() const;
383  bool isLicenseFeatureError() const;
384  bool canIgnoreError() const; // No Internet, network timeout and server errors can be ignored
385  // ------------------------------------------------------------
386 
387 private:
388  LicenseHandlerImpl *m_impl;
389 };
390 
391 } // namespace LicenseSpring
392 
393 #endif // LS_LICENSE_HANDLER_H
LSErrorCode
LicenseSpring Error codes enum.
Definition: PODTypes.h:45
std::shared_ptr< Configuration > ptr_t
Definition: Configuration.h:21
std::shared_ptr< LicenseStorage > ptr_t
Class for storing key-value device variable.
Class used for encapsulating LSConsumptionPeriod and converting cosumption period to/from string...
Class for storing key-value data field.
Definition: CustomField.h:17
Class used for encapsulating the type of license, comparing types of licenses, and converting them to...
Definition: LicenseType.h:19
Class that encapsulates information about a customer.
Definition: Customer.h:17
Class that encapsulates information about license feature.
std::shared_ptr< LicenseUser > ptr_t
Definition: LicenseUser.h:21
DeviceIDAlgorithm
Type of Device ID algorithm used by the SDK.
Definition: PODTypes.h:118
std::shared_ptr< ProductDetails > ptr_t
std::shared_ptr< Customer > ptr_t
Definition: Customer.h:20
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
#define LS_API
Definition: APIDef.h:23
Class that represents License identifier.
Definition: LicenseID.h:20
This class encapsulates functionality of the both LicenseManager and the License. ...
std::shared_ptr< InstallationFile > ptr_t
Helper struct for filtering installation files by environment and channel.