UIActivation.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/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("get_key"))
  63. {
  64. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  65. fileSystem->SystemOpen("https://store.atomicgameengine.com/store/store.php");
  66. }
  67. else if (ev.target->GetID() == TBIDC("quit"))
  68. {
  69. editor->RequestExit();
  70. }
  71. else if (ev.target->GetID() == TBIDC("activate"))
  72. {
  73. TBStr _key;
  74. licenseKey_->GetText(_key);
  75. String key = _key.CStr();
  76. key = key.ToUpper().Trimmed();
  77. if (!licenseSystem->ValidateKey(key))
  78. {
  79. editor->PostModalError("Invalid Product Key",
  80. "The key entered is invalid\n\nProduct keys are in the form of ATOMIC-XXXX-XXXX-XXXX-XXXX");
  81. }
  82. else
  83. {
  84. progressModal_->Show();
  85. RequestServerActivation(key);
  86. }
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. void UIActivation::HandleCurlComplete(StringHash eventType, VariantMap& eventData)
  93. {
  94. // we want to stay alive through the call, though will be swapped for success modal
  95. SharedPtr<UIModalOpWindow> keepAlive(this);
  96. Editor* editor = GetSubsystem<Editor>();
  97. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  98. progressModal_->Hide();
  99. if (serverActivation_->GetError().Length())
  100. {
  101. String errorMessage;
  102. errorMessage.AppendWithFormat("There was an error contacting the activation server\n\n%s", serverActivation_->GetError().CString());
  103. editor->PostModalError("Error Contacting Activation Server", errorMessage);
  104. }
  105. else
  106. {
  107. LicenseSystem::LicenseParse parse;
  108. int code = licenseSystem->ParseResponse(serverActivation_->GetResponse(), parse);
  109. if (code == 0)
  110. {
  111. licenseSystem->Activate(key_, parse);
  112. UIModalOps* ops = GetSubsystem<UIModalOps>();
  113. ops->Hide();
  114. ops->ShowActivationSuccess();
  115. }
  116. else if (code == 1)
  117. {
  118. editor->PostModalError("Activations Exceeded",
  119. "The key has exceeded the activation limit");
  120. }
  121. else if (code == 2)
  122. {
  123. editor->PostModalError("License Error",
  124. "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]");
  125. }
  126. else if (code == 3)
  127. {
  128. editor->PostModalError("Activation Server Error",
  129. "There was an error on the activation server\n\nIf the problem persists please contact [email protected]");
  130. }
  131. }
  132. UnsubscribeFromEvents(serverActivation_);
  133. serverActivation_ = 0;
  134. }
  135. }
  136. // END LICENSE MANAGEMENT