UIActivation.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. // BEGIN LICENSE MANAGEMENT
  5. #include "AtomicEditor.h"
  6. #include <TurboBadger/tb_window.h>
  7. #include <TurboBadger/tb_select.h>
  8. #include <TurboBadger/tb_editfield.h>
  9. #include <Atomic/Core/Context.h>
  10. #include <Atomic/IO/Log.h>
  11. #include <Atomic/UI/UI.h>
  12. #include "../Resources/AEResourceOps.h"
  13. #include "../AEPreferences.h"
  14. #include "../AEEditor.h"
  15. #include "../AEEvents.h"
  16. #include "../Project/AEProject.h"
  17. #include "../Project/ProjectUtils.h"
  18. #include "UIActivation.h"
  19. #include "AELicenseSystem.h"
  20. namespace AtomicEditor
  21. {
  22. // UIBuildSettings------------------------------------------------
  23. UIActivation::UIActivation(Context* context):
  24. UIModalOpWindow(context),
  25. licenseKey_(0)
  26. {
  27. UI* tbui = GetSubsystem<UI>();
  28. window_->SetSettings(WINDOW_SETTINGS_DEFAULT & ~WINDOW_SETTINGS_CLOSE_BUTTON);
  29. window_->SetText("Product Activation");
  30. tbui->LoadResourceFile(window_->GetContentRoot(), "AtomicEditor/editor/ui/activation.tb.txt");
  31. licenseKey_ = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("license_key"));
  32. assert(licenseKey_);
  33. window_->ResizeToFitContent();
  34. Center();
  35. progressModal_ = new ProgressModal(context_, "Activation", "Activating, please wait...");
  36. }
  37. UIActivation::~UIActivation()
  38. {
  39. }
  40. void UIActivation::RequestServerActivation(const String& key)
  41. {
  42. if (serverActivation_.NotNull())
  43. {
  44. LOGERROR("UIActivation::RequestServerActivation - request already exists");
  45. return;
  46. }
  47. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  48. key_ = key;
  49. CurlManager* cm = GetSubsystem<CurlManager>();
  50. String post;
  51. String id = licenseSystem->GenerateMachineID();
  52. post.AppendWithFormat("key=%s&id=%s", key.CString(), id.CString());
  53. // todo, this should be a verify url (shouldn't auto add id)
  54. serverActivation_ = cm->MakeRequest("https://store.atomicgameengine.com/licenses/license_activate.php", post);
  55. SubscribeToEvent(serverActivation_, E_CURLCOMPLETE, HANDLER(UIActivation, HandleCurlComplete));
  56. }
  57. bool UIActivation::OnEvent(const TBWidgetEvent &ev)
  58. {
  59. Editor* editor = GetSubsystem<Editor>();
  60. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  61. if (ev.type == EVENT_TYPE_CLICK)
  62. {
  63. if (ev.target->GetID() == TBIDC("get_key"))
  64. {
  65. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  66. fileSystem->SystemOpen("https://store.atomicgameengine.com/site");
  67. }
  68. else if (ev.target->GetID() == TBIDC("quit"))
  69. {
  70. editor->RequestExit();
  71. }
  72. else if (ev.target->GetID() == TBIDC("activate"))
  73. {
  74. TBStr _key;
  75. licenseKey_->GetText(_key);
  76. String key = _key.CStr();
  77. key = key.ToUpper().Trimmed();
  78. if (!licenseSystem->ValidateKey(key))
  79. {
  80. editor->PostModalError("Invalid Product Key",
  81. "The key entered is invalid\n\nProduct keys are in the form of ATOMIC-XXXX-XXXX-XXXX-XXXX");
  82. }
  83. else
  84. {
  85. progressModal_->Show();
  86. RequestServerActivation(key);
  87. }
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. void UIActivation::HandleCurlComplete(StringHash eventType, VariantMap& eventData)
  94. {
  95. // we want to stay alive through the call, though will be swapped for success modal
  96. SharedPtr<UIModalOpWindow> keepAlive(this);
  97. Editor* editor = GetSubsystem<Editor>();
  98. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  99. progressModal_->Hide();
  100. if (serverActivation_->GetError().Length())
  101. {
  102. String errorMessage;
  103. errorMessage.AppendWithFormat("There was an error contacting the activation server\n\n%s", serverActivation_->GetError().CString());
  104. editor->PostModalError("Error Contacting Activation Server", errorMessage);
  105. }
  106. else
  107. {
  108. LicenseSystem::LicenseParse parse;
  109. int code = licenseSystem->ParseResponse(serverActivation_->GetResponse(), parse);
  110. if (code == 0)
  111. {
  112. licenseSystem->Activate(key_, parse);
  113. UIModalOps* ops = GetSubsystem<UIModalOps>();
  114. ops->Hide();
  115. ops->ShowActivationSuccess();
  116. }
  117. else if (code == 1)
  118. {
  119. editor->PostModalError("Activations Exceeded",
  120. "This key has 2 activations in use.\n\nPlease return a license from Atomic Editor - Manage License menu on one of these active computers.\n\nIf you are unable to do so, please contact [email protected] providing the key to reset it");
  121. }
  122. else if (code == 2)
  123. {
  124. editor->PostModalError("License Error",
  125. "There was a problem with the license key.\n\nPlease check the key and try again.\n\nIf the problem persists please contact [email protected]");
  126. }
  127. else if (code == 3)
  128. {
  129. editor->PostModalError("Activation Server Error",
  130. "There was an error on the activation server\n\nIf the problem persists please contact [email protected]");
  131. }
  132. }
  133. UnsubscribeFromEvents(serverActivation_);
  134. serverActivation_ = 0;
  135. }
  136. }
  137. // END LICENSE MANAGEMENT