1 | /* $Id: UIUpdateDefs.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Update routine related implementations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QCoreApplication>
|
---|
30 | #include <QLocale>
|
---|
31 | #include <QStringList>
|
---|
32 |
|
---|
33 | /* GUI includes: */
|
---|
34 | #include "UINotificationCenter.h"
|
---|
35 | #include "UIUpdateDefs.h"
|
---|
36 | #include "UIVersion.h"
|
---|
37 |
|
---|
38 | /* COM includes: */
|
---|
39 | #include "CUpdateAgent.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /* static: */
|
---|
43 | VBoxUpdateDayList VBoxUpdateData::s_days = VBoxUpdateDayList();
|
---|
44 |
|
---|
45 | /* static */
|
---|
46 | void VBoxUpdateData::populate()
|
---|
47 | {
|
---|
48 | /* Clear list initially: */
|
---|
49 | s_days.clear();
|
---|
50 |
|
---|
51 | // WORKAROUND:
|
---|
52 | // To avoid re-translation complexity
|
---|
53 | // all values will be retranslated separately.
|
---|
54 |
|
---|
55 | /* Separately retranslate each day: */
|
---|
56 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "1 day"), "1 d", 86400);
|
---|
57 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "2 days"), "2 d", 172800);
|
---|
58 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "3 days"), "3 d", 259200);
|
---|
59 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "4 days"), "4 d", 345600);
|
---|
60 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "5 days"), "5 d", 432000);
|
---|
61 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "6 days"), "6 d", 518400);
|
---|
62 |
|
---|
63 | /* Separately retranslate each week: */
|
---|
64 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "1 week"), "1 w", 604800);
|
---|
65 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "2 weeks"), "2 w", 1209600);
|
---|
66 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "3 weeks"), "3 w", 1814400);
|
---|
67 |
|
---|
68 | /* Separately retranslate each month: */
|
---|
69 | s_days << VBoxUpdateDay(QCoreApplication::translate("UIUpdateManager", "1 month"), "1 m", 2592000);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* static */
|
---|
73 | QStringList VBoxUpdateData::list()
|
---|
74 | {
|
---|
75 | QStringList result;
|
---|
76 | foreach (const VBoxUpdateDay &day, s_days)
|
---|
77 | result << day.val;
|
---|
78 | return result;
|
---|
79 | }
|
---|
80 |
|
---|
81 | VBoxUpdateData::VBoxUpdateData(const QString &strData /* = QString("never") */)
|
---|
82 | : m_strData(strData)
|
---|
83 | , m_fCheckEnabled(false)
|
---|
84 | , m_fCheckRequired(false)
|
---|
85 | , m_enmUpdatePeriod(UpdatePeriodType_Never)
|
---|
86 | , m_enmUpdateChannel(KUpdateChannel_Invalid)
|
---|
87 | {
|
---|
88 | /* Skip 'never' case: */
|
---|
89 | if (m_strData == "never")
|
---|
90 | return;
|
---|
91 |
|
---|
92 | /* Check is enabled in all cases besides 'never': */
|
---|
93 | m_fCheckEnabled = true;
|
---|
94 |
|
---|
95 | /* Parse the data we have: */
|
---|
96 | const QStringList parser = m_strData.split(", ", Qt::SkipEmptyParts);
|
---|
97 |
|
---|
98 | /* Parse 'period' value: */
|
---|
99 | if (parser.size() > 0)
|
---|
100 | {
|
---|
101 | if (s_days.isEmpty())
|
---|
102 | populate();
|
---|
103 | m_enmUpdatePeriod = (UpdatePeriodType)s_days.indexOf(VBoxUpdateDay(QString(), parser.at(0), 0));
|
---|
104 | if (m_enmUpdatePeriod == UpdatePeriodType_Never)
|
---|
105 | m_enmUpdatePeriod = UpdatePeriodType_1Day;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* Parse 'date' value: */
|
---|
109 | if (parser.size() > 1)
|
---|
110 | {
|
---|
111 | QDate date = QDate::fromString(parser.at(1), Qt::ISODate);
|
---|
112 | m_date = date.isValid() ? date : QDate::currentDate();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* Parse 'update channel' value: */
|
---|
116 | if (parser.size() > 2)
|
---|
117 | {
|
---|
118 | m_enmUpdateChannel = updateChannelFromInternalString(parser.at(2));
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* Parse 'version' value: */
|
---|
122 | if (parser.size() > 3)
|
---|
123 | {
|
---|
124 | m_version = UIVersion(parser.at(3));
|
---|
125 | }
|
---|
126 |
|
---|
127 | /* Decide whether we need to check: */
|
---|
128 | m_fCheckRequired = (QDate::currentDate() >= date())
|
---|
129 | && ( !version().isValid()
|
---|
130 | || version() != UIVersion(UIVersionInfo::vboxVersionStringNormalized()));
|
---|
131 | }
|
---|
132 |
|
---|
133 | VBoxUpdateData::VBoxUpdateData(bool fCheckEnabled, UpdatePeriodType enmUpdatePeriod, KUpdateChannel enmUpdateChannel)
|
---|
134 | : m_strData("never")
|
---|
135 | , m_fCheckEnabled(fCheckEnabled)
|
---|
136 | , m_fCheckRequired(false)
|
---|
137 | , m_enmUpdatePeriod(enmUpdatePeriod)
|
---|
138 | , m_enmUpdateChannel(enmUpdateChannel)
|
---|
139 | {
|
---|
140 | /* Skip 'check disabled' case: */
|
---|
141 | if (!m_fCheckEnabled)
|
---|
142 | return;
|
---|
143 |
|
---|
144 | /* Encode 'period' value: */
|
---|
145 | if (s_days.isEmpty())
|
---|
146 | populate();
|
---|
147 | const QString strRemindPeriod = s_days.at(m_enmUpdatePeriod).key;
|
---|
148 |
|
---|
149 | /* Encode 'date' value: */
|
---|
150 | m_date = QDate::currentDate();
|
---|
151 | QStringList parser(strRemindPeriod.split(' '));
|
---|
152 | if (parser[1] == "d")
|
---|
153 | m_date = m_date.addDays(parser[0].toInt());
|
---|
154 | else if (parser[1] == "w")
|
---|
155 | m_date = m_date.addDays(parser[0].toInt() * 7);
|
---|
156 | else if (parser[1] == "m")
|
---|
157 | m_date = m_date.addDays(parser[0].toInt() * 30);
|
---|
158 | const QString strRemindDate = m_date.toString(Qt::ISODate);
|
---|
159 |
|
---|
160 | /* Encode 'update channel' value: */
|
---|
161 | const QString strUpdateChannel = updateChannelName();
|
---|
162 |
|
---|
163 | /* Encode 'version' value: */
|
---|
164 | m_version = UIVersion(UIVersionInfo::vboxVersionStringNormalized());
|
---|
165 | const QString strVersionValue = m_version.toString();
|
---|
166 |
|
---|
167 | /* Compose m_strData: */
|
---|
168 | m_strData = QString("%1, %2, %3, %4").arg(strRemindPeriod, strRemindDate, strUpdateChannel, strVersionValue);
|
---|
169 |
|
---|
170 | /* Decide whether we need to check: */
|
---|
171 | m_fCheckRequired = (QDate::currentDate() >= date())
|
---|
172 | && ( !version().isValid()
|
---|
173 | || version() != UIVersion(UIVersionInfo::vboxVersionStringNormalized()));
|
---|
174 | }
|
---|
175 |
|
---|
176 | bool VBoxUpdateData::load(const CHost &comHost)
|
---|
177 | {
|
---|
178 | /* Acquire update agent: */
|
---|
179 | CUpdateAgent comAgent = comHost.GetUpdateHost();
|
---|
180 | if (!comHost.isOk())
|
---|
181 | {
|
---|
182 | UINotificationMessage::cannotAcquireHostParameter(comHost);
|
---|
183 | return false;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Fetch whether agent is enabled: */
|
---|
187 | const BOOL fEnabled = comAgent.GetEnabled();
|
---|
188 | if (!comAgent.isOk())
|
---|
189 | {
|
---|
190 | UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
|
---|
191 | return false;
|
---|
192 | }
|
---|
193 | m_fCheckEnabled = fEnabled;
|
---|
194 |
|
---|
195 | /* Fetch 'period' value: */
|
---|
196 | const ULONG uFrequency = comAgent.GetCheckFrequency();
|
---|
197 | if (!comAgent.isOk())
|
---|
198 | {
|
---|
199 | UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
|
---|
200 | return false;
|
---|
201 | }
|
---|
202 | m_enmUpdatePeriod = gatherSuitablePeriod(uFrequency);
|
---|
203 |
|
---|
204 | /* Fetch 'date' value: */
|
---|
205 | const QString strLastDate = comAgent.GetLastCheckDate();
|
---|
206 | if (!comAgent.isOk())
|
---|
207 | {
|
---|
208 | UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
|
---|
209 | return false;
|
---|
210 | }
|
---|
211 | m_date = QDate::fromString(strLastDate, Qt::ISODate);
|
---|
212 | const ULONG uFrequencyInDays = (uFrequency / 86400) + 1;
|
---|
213 | m_date = m_date.addDays(uFrequencyInDays);
|
---|
214 |
|
---|
215 | /* Fetch 'update channel' value: */
|
---|
216 | KUpdateChannel enmUpdateChannel = comAgent.GetChannel();
|
---|
217 | if (!comAgent.isOk())
|
---|
218 | {
|
---|
219 | UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
|
---|
220 | return false;
|
---|
221 | }
|
---|
222 | m_enmUpdateChannel = enmUpdateChannel;
|
---|
223 |
|
---|
224 | /* Fetch 'version' value: */
|
---|
225 | const QString strVersion = comAgent.GetVersion();
|
---|
226 | if (!comAgent.isOk())
|
---|
227 | {
|
---|
228 | UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
|
---|
229 | return false;
|
---|
230 | }
|
---|
231 | m_version = strVersion;
|
---|
232 |
|
---|
233 | /* Fetch whether we need to check: */
|
---|
234 | const BOOL fNeedToCheck = comAgent.GetIsCheckNeeded();
|
---|
235 | if (!comAgent.isOk())
|
---|
236 | {
|
---|
237 | UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
|
---|
238 | return false;
|
---|
239 | }
|
---|
240 | m_fCheckRequired = fNeedToCheck;
|
---|
241 |
|
---|
242 | /* Optional stuff goes last; Fetch supported update channels: */
|
---|
243 | const QVector<KUpdateChannel> supportedUpdateChannels = comAgent.GetSupportedChannels();
|
---|
244 | if (!comAgent.isOk())
|
---|
245 | {
|
---|
246 | UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
|
---|
247 | return false;
|
---|
248 | }
|
---|
249 | m_supportedUpdateChannels = supportedUpdateChannels;
|
---|
250 |
|
---|
251 | /* Success finally: */
|
---|
252 | return true;
|
---|
253 | }
|
---|
254 |
|
---|
255 | bool VBoxUpdateData::save(const CHost &comHost) const
|
---|
256 | {
|
---|
257 | /* Acquire update agent: */
|
---|
258 | CUpdateAgent comAgent = comHost.GetUpdateHost();
|
---|
259 | if (!comHost.isOk())
|
---|
260 | {
|
---|
261 | UINotificationMessage::cannotAcquireHostParameter(comHost);
|
---|
262 | return false;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /* Save whether agent is enabled: */
|
---|
266 | comAgent.SetEnabled(m_fCheckEnabled);
|
---|
267 | if (!comAgent.isOk())
|
---|
268 | {
|
---|
269 | UINotificationMessage::cannotChangeUpdateAgentParameter(comAgent);
|
---|
270 | return false;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /* Save 'period' value: */
|
---|
274 | comAgent.SetCheckFrequency(s_days.at(m_enmUpdatePeriod).length);
|
---|
275 | if (!comAgent.isOk())
|
---|
276 | {
|
---|
277 | UINotificationMessage::cannotChangeUpdateAgentParameter(comAgent);
|
---|
278 | return false;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /* Save 'update channel' value: */
|
---|
282 | comAgent.SetChannel(m_enmUpdateChannel);
|
---|
283 | if (!comAgent.isOk())
|
---|
284 | {
|
---|
285 | UINotificationMessage::cannotChangeUpdateAgentParameter(comAgent);
|
---|
286 | return false;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /* Success finally: */
|
---|
290 | return true;
|
---|
291 | }
|
---|
292 |
|
---|
293 | bool VBoxUpdateData::isCheckEnabled() const
|
---|
294 | {
|
---|
295 | return m_fCheckEnabled;
|
---|
296 | }
|
---|
297 |
|
---|
298 | bool VBoxUpdateData::isCheckRequired() const
|
---|
299 | {
|
---|
300 | return m_fCheckRequired;
|
---|
301 | }
|
---|
302 |
|
---|
303 | QString VBoxUpdateData::data() const
|
---|
304 | {
|
---|
305 | return m_strData;
|
---|
306 | }
|
---|
307 |
|
---|
308 | UpdatePeriodType VBoxUpdateData::updatePeriod() const
|
---|
309 | {
|
---|
310 | return m_enmUpdatePeriod;
|
---|
311 | }
|
---|
312 |
|
---|
313 | QDate VBoxUpdateData::date() const
|
---|
314 | {
|
---|
315 | return m_date;
|
---|
316 | }
|
---|
317 |
|
---|
318 | QString VBoxUpdateData::dateToString() const
|
---|
319 | {
|
---|
320 | return isCheckEnabled()
|
---|
321 | ? QLocale::system().toString(m_date, QLocale::ShortFormat)
|
---|
322 | : QCoreApplication::translate("UIUpdateManager", "Never");
|
---|
323 | }
|
---|
324 |
|
---|
325 | KUpdateChannel VBoxUpdateData::updateChannel() const
|
---|
326 | {
|
---|
327 | return m_enmUpdateChannel;
|
---|
328 | }
|
---|
329 |
|
---|
330 | QString VBoxUpdateData::updateChannelName() const
|
---|
331 | {
|
---|
332 | return updateChannelToInternalString(m_enmUpdateChannel);
|
---|
333 | }
|
---|
334 |
|
---|
335 | UIVersion VBoxUpdateData::version() const
|
---|
336 | {
|
---|
337 | return m_version;
|
---|
338 | }
|
---|
339 |
|
---|
340 | QVector<KUpdateChannel> VBoxUpdateData::supportedUpdateChannels() const
|
---|
341 | {
|
---|
342 | return m_supportedUpdateChannels;
|
---|
343 | }
|
---|
344 |
|
---|
345 | bool VBoxUpdateData::isEqual(const VBoxUpdateData &another) const
|
---|
346 | {
|
---|
347 | return true
|
---|
348 | && (m_fCheckEnabled == another.isCheckEnabled())
|
---|
349 | && (m_enmUpdatePeriod == another.updatePeriod())
|
---|
350 | && (m_enmUpdateChannel == another.updateChannel())
|
---|
351 | ;
|
---|
352 | }
|
---|
353 |
|
---|
354 | bool VBoxUpdateData::operator==(const VBoxUpdateData &another) const
|
---|
355 | {
|
---|
356 | return isEqual(another);
|
---|
357 | }
|
---|
358 |
|
---|
359 | bool VBoxUpdateData::operator!=(const VBoxUpdateData &another) const
|
---|
360 | {
|
---|
361 | return !isEqual(another);
|
---|
362 | }
|
---|
363 |
|
---|
364 | /* static */
|
---|
365 | QString VBoxUpdateData::updateChannelToInternalString(KUpdateChannel enmUpdateChannel)
|
---|
366 | {
|
---|
367 | switch (enmUpdateChannel)
|
---|
368 | {
|
---|
369 | case KUpdateChannel_WithTesting: return "withtesting";
|
---|
370 | case KUpdateChannel_WithBetas: return "withbetas";
|
---|
371 | case KUpdateChannel_All: return "allrelease";
|
---|
372 | default: return "stable";
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | /* static */
|
---|
377 | KUpdateChannel VBoxUpdateData::updateChannelFromInternalString(const QString &strUpdateChannel)
|
---|
378 | {
|
---|
379 | QMap<QString, KUpdateChannel> pairs;
|
---|
380 | pairs["withtesting"] = KUpdateChannel_WithTesting;
|
---|
381 | pairs["withbetas"] = KUpdateChannel_WithBetas;
|
---|
382 | pairs["allrelease"] = KUpdateChannel_All;
|
---|
383 | return pairs.value(strUpdateChannel, KUpdateChannel_Stable);
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* static */
|
---|
387 | UpdatePeriodType VBoxUpdateData::gatherSuitablePeriod(ULONG uFrequency)
|
---|
388 | {
|
---|
389 | if (s_days.isEmpty())
|
---|
390 | populate();
|
---|
391 |
|
---|
392 | UpdatePeriodType enmType = UpdatePeriodType_1Day;
|
---|
393 | foreach (const VBoxUpdateDay &day, s_days)
|
---|
394 | {
|
---|
395 | if (uFrequency <= day.length)
|
---|
396 | return enmType;
|
---|
397 | enmType = (UpdatePeriodType)(enmType + 1);
|
---|
398 | }
|
---|
399 |
|
---|
400 | return UpdatePeriodType_1Month;
|
---|
401 | }
|
---|