Skip to content

Commit

Permalink
Split UpdateChecker file access into separate class
Browse files Browse the repository at this point in the history
Means we avoid pulling in the UpdateChecker dialog code into setup. We don't use any of the actual update checking code or user feedback in setup anyway, and so this change means we don't need to link swell at all
  • Loading branch information
firthm01 committed Oct 20, 2023
1 parent 4ed019c commit 54e0422
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 97 deletions.
2 changes: 2 additions & 0 deletions reaper-adm-extension/src/reaper_adm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ find_package(JUCE REQUIRED QUIET)
set(EXTENSION_SOURCES
${EPS_SHARED_DIR}/helper/common_definition_helper.cpp
${EPS_SHARED_DIR}/update_check.cpp
${EPS_SHARED_DIR}/update_check_settings_file.cpp

actionmanager.cpp
admchannel.cpp
Expand Down Expand Up @@ -84,6 +85,7 @@ set(EXTENSION_HEADERS
${EPS_SHARED_DIR}/helper/resource_paths.hpp
${EPS_SHARED_DIR}/helper/native_message_box.hpp
${EPS_SHARED_DIR}/update_check.h
${EPS_SHARED_DIR}/update_check_settings_file.h

AppConfig.h

Expand Down
82 changes: 5 additions & 77 deletions shared/update_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ UpdateChecker::UpdateChecker()
eps::versionMinor(),
eps::versionRevision());
}

settingsFile = ResourcePaths::getSettingsDirectory(true).getChildFile("UpdateCheck.settings");
if (!settingsFileExists()) {
// No settings file - perhaps first run?
if (saveSettings() && settingsFileExists()) {
// Settings file is writable so probably was just first run.
// Set autocheck on initially.
setAutoCheckEnabled(true);
}
}
loadSettings();
}

UpdateChecker::~UpdateChecker()
Expand All @@ -32,13 +21,12 @@ UpdateChecker::~UpdateChecker()

bool UpdateChecker::getAutoCheckEnabled()
{
return settingAutoCheckEnabled;
return settingsFile.getAutoCheckEnabled();
}

bool UpdateChecker::setAutoCheckEnabled(bool enabled, bool displayConfirmation)
{
settingAutoCheckEnabled = enabled;
auto success = saveSettings();
auto success = settingsFile.setAutoCheckEnabled(enabled);
if (displayConfirmation) {
if (success) {
if (enabled) {
Expand All @@ -63,10 +51,9 @@ void UpdateChecker::doUpdateCheck(bool manualCheck, int timeoutMs)

if (success) {
if (remoteVersion > currentVersion) {
if (manualCheck || remoteVersion != settingLastReportedVersion) {
if (manualCheck || remoteVersion != settingsFile.getLastReportedVersion()) {
// Haven't mentioned this version before or told to always show result
settingLastReportedVersion = remoteVersion;
saveSettings();
settingsFile.setLastReportedVersion(remoteVersion);
displayUpdateAvailable(remoteVersionStr, manualCheck);
}
}
Expand All @@ -76,16 +63,6 @@ void UpdateChecker::doUpdateCheck(bool manualCheck, int timeoutMs)
}
}

bool UpdateChecker::canReadSettingsFile()
{
return loadSettings();
}

bool UpdateChecker::canWriteSettingsFile()
{
return saveSettings();
}

bool UpdateChecker::getRemoteVersion(bool reportErrors, int timeoutMs, Version& version, std::string& versionStr)
{
std::string body;
Expand Down Expand Up @@ -179,53 +156,4 @@ void UpdateChecker::displayMessageBox(const std::string& title, const std::strin
{
// Windows version of Reaper locks up if you try show a message box during splash
NativeMessageBox::splashCompatibleMessage(title.c_str(), text.c_str(), nullptr, winIcon);
}

bool UpdateChecker::loadSettings()
{
if (!settingsFileExists()) {
return false;
}

juce::XmlDocument xml(settingsFile);
auto updateCheckElement = xml.getDocumentElementIfTagMatches("UpdateCheck");
if (!updateCheckElement) {
return false;
}

auto lastReportedElement = updateCheckElement->getChildByName("LastReportedVersion");
if (lastReportedElement) {
settingLastReportedVersion.major = lastReportedElement->getIntAttribute("VersionMajor", 0);
settingLastReportedVersion.minor = lastReportedElement->getIntAttribute("VersionMinor", 0);
settingLastReportedVersion.revision = lastReportedElement->getIntAttribute("VersionRevision", 0);
}

auto autoCheckElement = updateCheckElement->getChildByName("AutoCheck");
if (autoCheckElement) {
settingAutoCheckEnabled = autoCheckElement->getBoolAttribute("OnStartUp", false);
}

return true;
}

bool UpdateChecker::saveSettings()
{
auto updateCheckElement = juce::XmlElement("UpdateCheck");

auto lastReportedElement = new juce::XmlElement("LastReportedVersion");
lastReportedElement->setAttribute("VersionMajor", settingLastReportedVersion.major);
lastReportedElement->setAttribute("VersionMinor", settingLastReportedVersion.minor);
lastReportedElement->setAttribute("VersionRevision", settingLastReportedVersion.revision);
updateCheckElement.addChildElement(lastReportedElement);

auto autoCheckElement = new juce::XmlElement("AutoCheck");
autoCheckElement->setAttribute("OnStartUp", settingAutoCheckEnabled);
updateCheckElement.addChildElement(autoCheckElement);

return updateCheckElement.writeTo(settingsFile);
}

bool UpdateChecker::settingsFileExists()
{
return settingsFile.existsAsFile();
}
}
13 changes: 2 additions & 11 deletions shared/update_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <AppConfig.h>
#include <juce_core/juce_core.h>
#include <helper/version.hpp>
#include <update_check_settings_file.h>

class UpdateChecker {
public:
Expand All @@ -14,9 +15,6 @@ class UpdateChecker {

void doUpdateCheck(bool manualCheck=false, int timeoutMs=3000);

bool canReadSettingsFile();
bool canWriteSettingsFile();

private:
const std::string versionJsonUrl{ "https://ear-production-suite.ebu.io/version_info.json" };
const std::string messageBoxTitles{ "EAR Production Suite Update" };
Expand All @@ -29,14 +27,7 @@ class UpdateChecker {
void displayUpdateUnavailable();
void displayMessageBox(const std::string& title, const std::string& text, long winIcon);

juce::File settingsFile;
bool loadSettings();
bool saveSettings();
bool settingsFileExists();

bool settingAutoCheckEnabled{ false };
Version settingLastReportedVersion;

UpdateCheckerSettingsFile settingsFile;
Version currentVersion;

};
103 changes: 103 additions & 0 deletions shared/update_check_settings_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "update_check_settings_file.h"
#include <version/eps_version.h>
#include <helper/resource_paths_juce-file.hpp>

UpdateCheckerSettingsFile::UpdateCheckerSettingsFile()
{
settingsFile = ResourcePaths::getSettingsDirectory(true).getChildFile("UpdateCheck.settings");
if (!settingsFileExists()) {
// No settings file - perhaps first run?
if (saveSettings() && settingsFileExists()) {
// Settings file is writable so probably was just first run.
// Set autocheck on initially.
setAutoCheckEnabled(true);
}
}
loadSettings();
}

UpdateCheckerSettingsFile::~UpdateCheckerSettingsFile()
{
}

bool UpdateCheckerSettingsFile::getAutoCheckEnabled()
{
return settingAutoCheckEnabled;
}

bool UpdateCheckerSettingsFile::setAutoCheckEnabled(bool enabled)
{
settingAutoCheckEnabled = enabled;
return saveSettings();
}

Version UpdateCheckerSettingsFile::getLastReportedVersion()
{
return settingLastReportedVersion;
}

bool UpdateCheckerSettingsFile::setLastReportedVersion(Version version)
{
settingLastReportedVersion = version;
return saveSettings();
}


bool UpdateCheckerSettingsFile::canReadSettingsFile()
{
return loadSettings();
}

bool UpdateCheckerSettingsFile::canWriteSettingsFile()
{
return saveSettings();
}

bool UpdateCheckerSettingsFile::loadSettings()
{
if (!settingsFileExists()) {
return false;
}

juce::XmlDocument xml(settingsFile);
auto updateCheckElement = xml.getDocumentElementIfTagMatches("UpdateCheck");
if (!updateCheckElement) {
return false;
}

auto lastReportedElement = updateCheckElement->getChildByName("LastReportedVersion");
if (lastReportedElement) {
settingLastReportedVersion.major = lastReportedElement->getIntAttribute("VersionMajor", 0);
settingLastReportedVersion.minor = lastReportedElement->getIntAttribute("VersionMinor", 0);
settingLastReportedVersion.revision = lastReportedElement->getIntAttribute("VersionRevision", 0);
}

auto autoCheckElement = updateCheckElement->getChildByName("AutoCheck");
if (autoCheckElement) {
settingAutoCheckEnabled = autoCheckElement->getBoolAttribute("OnStartUp", false);
}

return true;
}

bool UpdateCheckerSettingsFile::saveSettings()
{
auto updateCheckElement = juce::XmlElement("UpdateCheck");

auto lastReportedElement = new juce::XmlElement("LastReportedVersion");
lastReportedElement->setAttribute("VersionMajor", settingLastReportedVersion.major);
lastReportedElement->setAttribute("VersionMinor", settingLastReportedVersion.minor);
lastReportedElement->setAttribute("VersionRevision", settingLastReportedVersion.revision);
updateCheckElement.addChildElement(lastReportedElement);

auto autoCheckElement = new juce::XmlElement("AutoCheck");
autoCheckElement->setAttribute("OnStartUp", settingAutoCheckEnabled);
updateCheckElement.addChildElement(autoCheckElement);

return updateCheckElement.writeTo(settingsFile);
}

bool UpdateCheckerSettingsFile::settingsFileExists()
{
return settingsFile.existsAsFile();
}
29 changes: 29 additions & 0 deletions shared/update_check_settings_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include <AppConfig.h>
#include <juce_core/juce_core.h>
#include <helper/version.hpp>

class UpdateCheckerSettingsFile {
public:
UpdateCheckerSettingsFile();
~UpdateCheckerSettingsFile();

bool getAutoCheckEnabled();
bool setAutoCheckEnabled(bool enabled);

Version getLastReportedVersion();
bool setLastReportedVersion(Version version);

bool canReadSettingsFile();
bool canWriteSettingsFile();

private:

juce::File settingsFile;
bool loadSettings();
bool saveSettings();
bool settingsFileExists();

bool settingAutoCheckEnabled{ false };
Version settingLastReportedVersion;
};
6 changes: 3 additions & 3 deletions tools/setup/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ set(HEADERS_SETUP
install_phases/component_complete.h
helpers/manifests.h
${EPS_SHARED_DIR}/helper/resource_paths_juce-file.hpp
${EPS_SHARED_DIR}/update_check.cpp
${EPS_SHARED_DIR}/update_check_settings_file.h
)

set(SOURCES_SETUP
Expand All @@ -42,7 +42,7 @@ set(SOURCES_SETUP
install_phases/component_complete.cpp
helpers/manifests.cpp
${EPS_SHARED_DIR}/binary_data.cpp
${EPS_SHARED_DIR}/update_check.h
${EPS_SHARED_DIR}/update_check_settings_file.cpp
)

source_group("Header Files" FILES ${HEADERS_SETUP})
Expand Down Expand Up @@ -83,6 +83,6 @@ add_custom_command(TARGET setup PRE_BUILD
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/helpers/gen_license_header.cmake
)

set_target_properties(setup PROPERTIES FOLDER tools)
set_target_properties(setup PROPERTIES FOLDER tools)
target_link_libraries(setup PRIVATE Juce::core ear-version)
target_include_directories(setup PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${EPS_SHARED_DIR})
8 changes: 4 additions & 4 deletions tools/setup/install_phases/component_complete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void ComponentComplete::configureForInstallPhase()

AutoUpdateCheckButton::AutoUpdateCheckButton()
{
bool success = updateChecker.canWriteSettingsFile();
bool success = updateCheckerSettingsFile.canWriteSettingsFile();

text1.setFont(EarFontsSingleton::instance().Label);
text1.setColour(Label::textColourId, EarColours::Label.withAlpha(Emphasis::high));
Expand Down Expand Up @@ -125,7 +125,7 @@ AutoUpdateCheckButton::~AutoUpdateCheckButton()
void AutoUpdateCheckButton::paint(Graphics& g)
{
auto area = getLocalBounds();
auto checked = updateChecker.getAutoCheckEnabled();
auto checked = updateCheckerSettingsFile.getAutoCheckEnabled();
getLookAndFeel().drawTickBox(g, text1, 1, 1, area.getHeight() - 2, area.getHeight() - 2, checked, true, true, false);
}

Expand All @@ -147,11 +147,11 @@ int AutoUpdateCheckButton::getMinReqWidth(int forComponentHeight)

void AutoUpdateCheckButton::setState(bool checked)
{
updateChecker.setAutoCheckEnabled(checked);
updateCheckerSettingsFile.setAutoCheckEnabled(checked);
repaint();
}

void AutoUpdateCheckButton::mouseDown(const MouseEvent& event)
{
setState(!updateChecker.getAutoCheckEnabled());
setState(!updateCheckerSettingsFile.getAutoCheckEnabled());
}
4 changes: 2 additions & 2 deletions tools/setup/install_phases/component_complete.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "JuceHeader.h"
#include <memory>
#include <update_check.h>
#include <update_check_settings_file.h>

class AutoUpdateCheckButton : public Component
{
Expand All @@ -25,7 +25,7 @@ class AutoUpdateCheckButton : public Component

const int marginBoxText{ 10 };

UpdateChecker updateChecker;
UpdateCheckerSettingsFile updateCheckerSettingsFile;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AutoUpdateCheckButton)
};
Expand Down

0 comments on commit 54e0422

Please sign in to comment.