Krotos Modules 3
Loading...
Searching...
No Matches
VersionChecker.cpp
Go to the documentation of this file.
1namespace krotos
2{
3 /* Version internal class implementation */
4 VersionChecker::Version::Version(String versionString) { m_isValid = parseString(versionString); }
5
7 {
8 // Parse the versionString and split with std::strtok
9 StringArray versionArray;
10 versionArray.addTokens(v.removeCharacters("v"), ".", "");
11
12 if (versionArray.size() != NUM_FIELDS)
13 {
14 return false;
15 }
16
17 for (int i = 0; i < NUM_FIELDS; i++)
18 {
19 m_version[i] = versionArray[i].getIntValue();
20 }
21
22 return true;
23 }
24
26 {
27 // Work through maj, min, rev in turn to test
28 // return LT, EQUAL, GT for this<v, this==v, this>v
29 for (int i = 0; i < NUM_FIELDS; i++)
30 {
31 if (this->m_version[i] > v.m_version[i])
32 return GT;
33 else if (this->m_version[i] < v.m_version[i])
34 return LT;
35 }
36 return EQUAL;
37 }
38
40 {
41 String versionString("");
42 if (m_isValid)
43 {
44 String dot(".");
45 versionString = String("v") + std::to_string(m_version[MAJOR]) + dot + std::to_string(m_version[MINOR]) +
46 dot + std::to_string(m_version[REVISION]);
47 }
48 return versionString;
49 }
50
52 VersionChecker::VersionChecker(String pluginVersion, URL versionUrl, URL popupTextUrl, String versionFilePath)
53 : Thread{"VersionChecker"}, m_pluginVersion(pluginVersion), m_versionFilePath(versionFilePath)
54 {
55 m_versionUrl = versionUrl;
56 m_updateTextUrl = popupTextUrl;
57
58 // TODO Replace with settingsFile
60
61 this->addListener(this);
62
63#if !IS_RELEASE_BUILD
64 testHelper();
65#endif
66 }
67
68 VersionChecker::~VersionChecker() { stopThread(1000); }
69
71 {
72 // custom version and update text files which can be used for QA
73 File customVersion = utils::StringsIntoPath(m_versionFilePath, "UpdaterCustomVersion.txt");
74
75 if (customVersion.existsAsFile())
76 {
77 m_versionUrl = URL(customVersion);
78 }
79
80 File customText = utils::StringsIntoPath(m_versionFilePath, "UpdaterCustomText.txt");
81
82 if (customText.existsAsFile())
83 {
84 m_updateTextUrl = URL(customText);
85 }
86 }
87
88 void VersionChecker::runCheck() { startThread(); }
89
91 {
92 m_latestVersion = Version(m_versionUrl.readEntireTextStream());
93 return m_latestVersion.isValid();
94 }
95
97 {
98 if (testVersion.isValid())
99 return testVersion.compare(m_pluginVersion) == Version::eRtn::GT;
100
101 // Not valid, ignore
102 return false;
103 }
104
106 {
107 File dontCheckFile(m_dontCheckForUpdatesPath);
108
109 if (dontCheckFile.existsAsFile())
110 {
111 StringArray versionInCheckFile;
112 dontCheckFile.readLines(versionInCheckFile);
113 String ignoredVersion = versionInCheckFile[0];
114 if (ignoredVersion.compareIgnoreCase(version.toString()) == 0)
115 {
116 return true;
117 }
118 }
119
120 return false;
121 }
122
123 // TODO replace with settingsFile and get rid of the don't check for updates .txt
125 {
126 File dontCheckFile(m_dontCheckForUpdatesPath);
127 return (dontCheckFile.existsAsFile());
128 }
129
131 {
132 if (!threadShouldExit())
133 {
134 const bool connectedToServer = updateLatestVersion();
135 if (threadShouldExit())
136 {
137 return;
138 }
139 if (connectedToServer && isNewerVersion(m_latestVersion))
140 {
142 {
143 // This version is new
144 m_updateText = m_updateTextUrl.readEntireTextStream();
145 if (m_updateText.isNotEmpty())
146 {
147 m_updateAvailable = true;
148 }
149 }
150 }
151 signalThreadShouldExit();
152 }
153 }
154
156 {
157 if (!updateLatestVersion())
158 {
159 return CONNECTION_ERROR;
160 }
161
163 {
164 return IS_UP_TO_DATE;
165 }
166 else
167 {
168 m_updateText = m_updateTextUrl.readEntireTextStream();
169 return UPDATE_AVAILABLE;
170 }
171 }
172
174 {
175 if (onFinished != nullptr)
177 }
178
180 {
181 // TODO replace with settingsFile and get rid of the don't check for updates .txt
182 File dontCheckFile(m_dontCheckForUpdatesPath);
183
184 // if DoNotCheckForUpdates.txt doesn't exist, create it
185 if (!dontCheckFile.existsAsFile())
186 {
187 dontCheckFile.create();
188 }
189
190 // if DoNotCheckForUpdates.txt exists, replace version number from AWS
191 if (dontCheckFile.existsAsFile())
192 {
193 dontCheckFile.replaceWithText(m_latestVersion.toString());
194 }
195 }
196
198 {
199 File dontCheckFile(m_dontCheckForUpdatesPath);
200 return dontCheckFile.deleteFile();
201 }
202} // namespace krotos
Definition VersionChecker.h:14
String toString()
Definition VersionChecker.cpp:39
bool parseString(String versionString)
Definition VersionChecker.cpp:6
Version()
Definition VersionChecker.h:16
bool m_isValid
Definition VersionChecker.h:45
eRtn compare(Version v)
Definition VersionChecker.cpp:25
bool isValid()
Definition VersionChecker.h:38
int m_version[NUM_FIELDS]
Definition VersionChecker.h:42
eRtn
Definition VersionChecker.h:29
@ GT
Definition VersionChecker.h:32
String m_versionFilePath
Definition VersionChecker.h:105
URL m_updateTextUrl
Definition VersionChecker.h:114
Version m_latestVersion
Definition VersionChecker.h:116
void exitSignalSent() override
Definition VersionChecker.cpp:173
bool isVersionInDontCheckFile(Version version)
Definition VersionChecker.cpp:105
void runCheck()
Definition VersionChecker.cpp:88
bool resetDoNotCheckStatus()
Resets the user's "do not check" or "not now" status if they opted to skip the next version....
Definition VersionChecker.cpp:197
void run() override
Definition VersionChecker.cpp:130
std::atomic< bool > m_updateAvailable
Definition VersionChecker.h:112
std::function< void(bool)> onFinished
Definition VersionChecker.h:102
bool updateLatestVersion()
Definition VersionChecker.cpp:90
eReturn doRemoteVersionCheck()
Definition VersionChecker.cpp:155
Version m_pluginVersion
Definition VersionChecker.h:116
VersionChecker(String pluginVersion, URL versionUrl, URL popupTextUrl, String versionFilePath)
Definition VersionChecker.cpp:52
String m_updateText
Definition VersionChecker.h:118
bool isNewerVersion(Version testVersion)
Definition VersionChecker.cpp:96
String m_dontCheckForUpdatesPath
Definition VersionChecker.h:118
~VersionChecker()
Definition VersionChecker.cpp:68
void storeRetrievedVersionId()
Definition VersionChecker.cpp:179
bool dontCheckFileExists()
Definition VersionChecker.cpp:124
eReturn
Definition VersionChecker.h:63
@ UPDATE_AVAILABLE
Definition VersionChecker.h:65
@ IS_UP_TO_DATE
Definition VersionChecker.h:64
@ CONNECTION_ERROR
Definition VersionChecker.h:66
void testHelper()
Definition VersionChecker.cpp:70
URL m_versionUrl
Definition VersionChecker.h:114
String StringsIntoPath(Args... args)
Joins multiple string arguments into a path string.
Definition helpers.h:25
Definition AirAbsorptionFilter.cpp:2