UIActivation.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/AtomicEditor
  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/TBUI.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. TBUI* tbui = GetSubsystem<TBUI>();
  28. window_->SetText("Product Activation");
  29. tbui->LoadResourceFile(window_->GetContentRoot(), "AtomicEditor/editor/ui/activation.tb.txt");
  30. licenseKey_ = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("license_key"));
  31. assert(licenseKey_);
  32. window_->ResizeToFitContent();
  33. Center();
  34. progressModal_ = new ProgressModal(context_, "Activation", "Activating, please wait...");
  35. }
  36. UIActivation::~UIActivation()
  37. {
  38. }
  39. void UIActivation::RequestServerActivation(const String& key)
  40. {
  41. if (serverActivation_.NotNull())
  42. {
  43. LOGERROR("UIActivation::RequestServerActivation - request already exists");
  44. return;
  45. }
  46. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  47. key_ = key;
  48. CurlManager* cm = GetSubsystem<CurlManager>();
  49. String post;
  50. String id = licenseSystem->GenerateMachineID();
  51. post.AppendWithFormat("key=%s&id=%s", key.CString(), id.CString());
  52. // todo, this should be a verify url (shouldn't auto add id)
  53. serverActivation_ = cm->MakeRequest("https://store.atomicgameengine.com/licenses/license_activate.php", post);
  54. SubscribeToEvent(serverActivation_, E_CURLCOMPLETE, HANDLER(UIActivation, HandleCurlComplete));
  55. }
  56. bool UIActivation::OnEvent(const TBWidgetEvent &ev)
  57. {
  58. Editor* editor = GetSubsystem<Editor>();
  59. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  60. if (ev.type == EVENT_TYPE_CLICK)
  61. {
  62. if (ev.target->GetID() == TBIDC("activate"))
  63. {
  64. TBStr _key;
  65. licenseKey_->GetText(_key);
  66. String key = _key.CStr();
  67. key = key.ToUpper().Trimmed();
  68. if (!licenseSystem->ValidateKey(key))
  69. {
  70. editor->PostModalError("Invalid Product Key",
  71. "The key entered is invalid\n\nProduct keys are in the form of ATOMIC-XXXX-XXXX-XXXX-XXXX");
  72. }
  73. else
  74. {
  75. progressModal_->Show();
  76. RequestServerActivation(key);
  77. }
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. void UIActivation::HandleCurlComplete(StringHash eventType, VariantMap& eventData)
  84. {
  85. // we want to stay alive through the call, though will be swapped for success modal
  86. SharedPtr<UIModalOpWindow> keepAlive(this);
  87. Editor* editor = GetSubsystem<Editor>();
  88. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  89. progressModal_->Hide();
  90. if (serverActivation_->GetError().Length())
  91. {
  92. String errorMessage;
  93. errorMessage.AppendWithFormat("There was an error contacting the activation server\n\n%s", serverActivation_->GetError().CString());
  94. editor->PostModalError("Error Contacting Activation Server", errorMessage);
  95. }
  96. else
  97. {
  98. LicenseSystem::LicenseParse parse;
  99. int code = licenseSystem->ParseResponse(serverActivation_->GetResponse(), parse);
  100. if (code == 0)
  101. {
  102. licenseSystem->Activate(key_, parse);
  103. UIModalOps* ops = GetSubsystem<UIModalOps>();
  104. ops->Hide();
  105. ops->ShowActivationSuccess();
  106. }
  107. else if (code == 1)
  108. {
  109. editor->PostModalError("Activations Exceeded",
  110. "The key has exceeded the activation limit");
  111. }
  112. else if (code == 2)
  113. {
  114. editor->PostModalError("License Error",
  115. "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]");
  116. }
  117. else if (code == 3)
  118. {
  119. editor->PostModalError("Activation Server Error",
  120. "There was an error on the activation server\n\nIf the problem persists please contact [email protected]");
  121. }
  122. }
  123. UnsubscribeFromEvents(serverActivation_);
  124. serverActivation_ = 0;
  125. }
  126. }
  127. // END LICENSE MANAGEMENT