EditorPreferencesPageAWS.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorDefs.h"
  9. #include "EditorPreferencesPageAWS.h"
  10. // AzCore
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Settings/SettingsRegistryImpl.h>
  13. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  14. #include <AzCore/Jobs/JobFunction.h>
  15. void CEditorPreferencesPage_AWS::Reflect(AZ::SerializeContext& serialize)
  16. {
  17. serialize.Class<UsageOptions>()
  18. ->Version(1)
  19. ->Field("AWSAttributionEnabled", &UsageOptions::m_awsAttributionEnabled);
  20. serialize.Class<CEditorPreferencesPage_AWS>()
  21. ->Version(1)
  22. ->Field("UsageOptions", &CEditorPreferencesPage_AWS::m_usageOptions);
  23. AZ::EditContext* editContext = serialize.GetEditContext();
  24. if (editContext)
  25. {
  26. editContext->Class<UsageOptions>("Options", "")
  27. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &UsageOptions::m_awsAttributionEnabled, "Allow <a href=\"https://aws.amazon.com/privacy/\">O3DE</a> to send information about your use of AWS Core Gem to AWS",
  28. "");
  29. editContext->Class<CEditorPreferencesPage_AWS>("AWS Preferences", "AWS Preferences")
  30. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  31. ->Attribute(AZ::Edit::Attributes::Visibility, AZ_CRC_CE("PropertyVisibility_ShowChildrenOnly"))
  32. ->DataElement(AZ::Edit::UIHandlers::Default, &CEditorPreferencesPage_AWS::m_usageOptions, "AWS Data Collection and Use", "AWS Data Collection and Use");
  33. }
  34. }
  35. CEditorPreferencesPage_AWS::CEditorPreferencesPage_AWS()
  36. {
  37. m_settingsRegistry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  38. InitializeSettings();
  39. m_icon = QIcon(":/res/AWS_preferences_icon.svg");
  40. }
  41. CEditorPreferencesPage_AWS::~CEditorPreferencesPage_AWS()
  42. {
  43. m_settingsRegistry.reset();
  44. }
  45. const char* CEditorPreferencesPage_AWS::GetTitle()
  46. {
  47. return "Cloud";
  48. }
  49. QIcon& CEditorPreferencesPage_AWS::GetIcon()
  50. {
  51. return m_icon;
  52. }
  53. void CEditorPreferencesPage_AWS::OnApply()
  54. {
  55. m_settingsRegistry->Set(AWSAttributionEnabledKey, m_usageOptions.m_awsAttributionEnabled);
  56. SaveSettingsRegistryFile();
  57. }
  58. const CEditorPreferencesPage_AWS::UsageOptions& CEditorPreferencesPage_AWS::GetUsageOptions()
  59. {
  60. return m_usageOptions;
  61. }
  62. void CEditorPreferencesPage_AWS::SaveSettingsRegistryFile()
  63. {
  64. AZ::Job* job = AZ::CreateJobFunction(
  65. [this]()
  66. {
  67. AZ::IO::FileIOBase* fileIO = AZ::IO::FileIOBase::GetInstance();
  68. AZ_Assert(fileIO, "File IO is not initialized.");
  69. // Resolve path to editor_aws_preferences.setreg
  70. AZStd::string editorPreferencesFilePath =
  71. AZStd::string::format("@user@/%s/%s", AZ::SettingsRegistryInterface::RegistryFolder, EditorAWSPreferencesFileName);
  72. AZStd::array<char, AZ::IO::MaxPathLength> resolvedPath{};
  73. fileIO->ResolvePath(editorPreferencesFilePath.c_str(), resolvedPath.data(), resolvedPath.size());
  74. AZ::SettingsRegistryMergeUtils::DumperSettings dumperSettings;
  75. dumperSettings.m_prettifyOutput = true;
  76. dumperSettings.m_jsonPointerPrefix = AWSAttributionSettingsPrefixKey;
  77. AZStd::string stringBuffer;
  78. AZ::IO::ByteContainerStream stringStream(&stringBuffer);
  79. if (!AZ::SettingsRegistryMergeUtils::DumpSettingsRegistryToStream(
  80. *m_settingsRegistry, AWSAttributionSettingsPrefixKey, stringStream, dumperSettings))
  81. {
  82. AZ_Warning(
  83. "AWSAttributionManager", false, R"(Unable to save changes to the Editor AWS Preferences registry file at "%s"\n)",
  84. resolvedPath.data());
  85. return;
  86. }
  87. [[maybe_unused]] bool saved = false;
  88. constexpr auto configurationMode =
  89. AZ::IO::SystemFile::SF_OPEN_CREATE | AZ::IO::SystemFile::SF_OPEN_CREATE_PATH | AZ::IO::SystemFile::SF_OPEN_WRITE_ONLY;
  90. if (AZ::IO::SystemFile outputFile; outputFile.Open(resolvedPath.data(), configurationMode))
  91. {
  92. saved = outputFile.Write(stringBuffer.data(), stringBuffer.size()) == stringBuffer.size();
  93. }
  94. AZ_Warning(
  95. "AWSAttributionManager", saved, R"(Unable to save Editor AWS Preferences registry file to path "%s"\n)",
  96. editorPreferencesFilePath.c_str());
  97. },
  98. true);
  99. job->Start();
  100. }
  101. void CEditorPreferencesPage_AWS::InitializeSettings()
  102. {
  103. AZ::IO::FileIOBase* fileIO = AZ::IO::FileIOBase::GetInstance();
  104. AZ_Assert(fileIO, "File IO is not initialized.");
  105. // Resolve path to editor_aws_preferences.setreg
  106. AZStd::string editorAWSPreferencesFilePath =
  107. AZStd::string::format("@user@/%s/%s", AZ::SettingsRegistryInterface::RegistryFolder, EditorAWSPreferencesFileName);
  108. AZStd::array<char, AZ::IO::MaxPathLength> resolvedPathAWSPreference{};
  109. if (!fileIO->ResolvePath(editorAWSPreferencesFilePath.c_str(), resolvedPathAWSPreference.data(), resolvedPathAWSPreference.size()))
  110. {
  111. AZ_Warning("AWSAttributionManager", false, "Error resolving path %s", resolvedPathAWSPreference.data());
  112. return;
  113. }
  114. if (fileIO->Exists(resolvedPathAWSPreference.data()))
  115. {
  116. m_settingsRegistry->MergeSettingsFile(resolvedPathAWSPreference.data(), AZ::SettingsRegistryInterface::Format::JsonMergePatch, "");
  117. }
  118. if (!m_settingsRegistry->Get(m_usageOptions.m_awsAttributionEnabled, AWSAttributionEnabledKey))
  119. {
  120. // If key is missing default to on.
  121. m_usageOptions.m_awsAttributionEnabled = true;
  122. }
  123. }