VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/networking/UIUpdateDefs.cpp@ 103977

Last change on this file since 103977 was 103793, checked in by vboxsync, 9 months ago

FE/Qt: UICommon: Move versioning related functionality to UIVersion / UIVersionInfo; Rework user cases accordingly.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 KB
Line 
1/* $Id: UIUpdateDefs.cpp 103793 2024-03-11 19:17:31Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - Update routine related implementations.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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: */
43VBoxUpdateDayList VBoxUpdateData::s_days = VBoxUpdateDayList();
44
45/* static */
46void 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 */
73QStringList VBoxUpdateData::list()
74{
75 QStringList result;
76 foreach (const VBoxUpdateDay &day, s_days)
77 result << day.val;
78 return result;
79}
80
81VBoxUpdateData::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#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
96 const QStringList parser = m_strData.split(", ", Qt::SkipEmptyParts);
97#else
98 const QStringList parser = m_strData.split(", ", QString::SkipEmptyParts);
99#endif
100
101 /* Parse 'period' value: */
102 if (parser.size() > 0)
103 {
104 if (s_days.isEmpty())
105 populate();
106 m_enmUpdatePeriod = (UpdatePeriodType)s_days.indexOf(VBoxUpdateDay(QString(), parser.at(0), 0));
107 if (m_enmUpdatePeriod == UpdatePeriodType_Never)
108 m_enmUpdatePeriod = UpdatePeriodType_1Day;
109 }
110
111 /* Parse 'date' value: */
112 if (parser.size() > 1)
113 {
114 QDate date = QDate::fromString(parser.at(1), Qt::ISODate);
115 m_date = date.isValid() ? date : QDate::currentDate();
116 }
117
118 /* Parse 'update channel' value: */
119 if (parser.size() > 2)
120 {
121 m_enmUpdateChannel = updateChannelFromInternalString(parser.at(2));
122 }
123
124 /* Parse 'version' value: */
125 if (parser.size() > 3)
126 {
127 m_version = UIVersion(parser.at(3));
128 }
129
130 /* Decide whether we need to check: */
131 m_fCheckRequired = (QDate::currentDate() >= date())
132 && ( !version().isValid()
133 || version() != UIVersion(UIVersionInfo::vboxVersionStringNormalized()));
134}
135
136VBoxUpdateData::VBoxUpdateData(bool fCheckEnabled, UpdatePeriodType enmUpdatePeriod, KUpdateChannel enmUpdateChannel)
137 : m_strData("never")
138 , m_fCheckEnabled(fCheckEnabled)
139 , m_fCheckRequired(false)
140 , m_enmUpdatePeriod(enmUpdatePeriod)
141 , m_enmUpdateChannel(enmUpdateChannel)
142{
143 /* Skip 'check disabled' case: */
144 if (!m_fCheckEnabled)
145 return;
146
147 /* Encode 'period' value: */
148 if (s_days.isEmpty())
149 populate();
150 const QString strRemindPeriod = s_days.at(m_enmUpdatePeriod).key;
151
152 /* Encode 'date' value: */
153 m_date = QDate::currentDate();
154 QStringList parser(strRemindPeriod.split(' '));
155 if (parser[1] == "d")
156 m_date = m_date.addDays(parser[0].toInt());
157 else if (parser[1] == "w")
158 m_date = m_date.addDays(parser[0].toInt() * 7);
159 else if (parser[1] == "m")
160 m_date = m_date.addDays(parser[0].toInt() * 30);
161 const QString strRemindDate = m_date.toString(Qt::ISODate);
162
163 /* Encode 'update channel' value: */
164 const QString strUpdateChannel = updateChannelName();
165
166 /* Encode 'version' value: */
167 m_version = UIVersion(UIVersionInfo::vboxVersionStringNormalized());
168 const QString strVersionValue = m_version.toString();
169
170 /* Compose m_strData: */
171 m_strData = QString("%1, %2, %3, %4").arg(strRemindPeriod, strRemindDate, strUpdateChannel, strVersionValue);
172
173 /* Decide whether we need to check: */
174 m_fCheckRequired = (QDate::currentDate() >= date())
175 && ( !version().isValid()
176 || version() != UIVersion(UIVersionInfo::vboxVersionStringNormalized()));
177}
178
179bool VBoxUpdateData::load(const CHost &comHost)
180{
181 /* Acquire update agent: */
182 CUpdateAgent comAgent = comHost.GetUpdateHost();
183 if (!comHost.isOk())
184 {
185 UINotificationMessage::cannotAcquireHostParameter(comHost);
186 return false;
187 }
188
189 /* Fetch whether agent is enabled: */
190 const BOOL fEnabled = comAgent.GetEnabled();
191 if (!comAgent.isOk())
192 {
193 UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
194 return false;
195 }
196 m_fCheckEnabled = fEnabled;
197
198 /* Fetch 'period' value: */
199 const ULONG uFrequency = comAgent.GetCheckFrequency();
200 if (!comAgent.isOk())
201 {
202 UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
203 return false;
204 }
205 m_enmUpdatePeriod = gatherSuitablePeriod(uFrequency);
206
207 /* Fetch 'date' value: */
208 const QString strLastDate = comAgent.GetLastCheckDate();
209 if (!comAgent.isOk())
210 {
211 UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
212 return false;
213 }
214 m_date = QDate::fromString(strLastDate, Qt::ISODate);
215 const ULONG uFrequencyInDays = (uFrequency / 86400) + 1;
216 m_date = m_date.addDays(uFrequencyInDays);
217
218 /* Fetch 'update channel' value: */
219 KUpdateChannel enmUpdateChannel = comAgent.GetChannel();
220 if (!comAgent.isOk())
221 {
222 UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
223 return false;
224 }
225 m_enmUpdateChannel = enmUpdateChannel;
226
227 /* Fetch 'version' value: */
228 const QString strVersion = comAgent.GetVersion();
229 if (!comAgent.isOk())
230 {
231 UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
232 return false;
233 }
234 m_version = strVersion;
235
236 /* Fetch whether we need to check: */
237 const BOOL fNeedToCheck = comAgent.GetIsCheckNeeded();
238 if (!comAgent.isOk())
239 {
240 UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
241 return false;
242 }
243 m_fCheckRequired = fNeedToCheck;
244
245 /* Optional stuff goes last; Fetch supported update channels: */
246 const QVector<KUpdateChannel> supportedUpdateChannels = comAgent.GetSupportedChannels();
247 if (!comAgent.isOk())
248 {
249 UINotificationMessage::cannotAcquireUpdateAgentParameter(comAgent);
250 return false;
251 }
252 m_supportedUpdateChannels = supportedUpdateChannels;
253
254 /* Success finally: */
255 return true;
256}
257
258bool VBoxUpdateData::save(const CHost &comHost) const
259{
260 /* Acquire update agent: */
261 CUpdateAgent comAgent = comHost.GetUpdateHost();
262 if (!comHost.isOk())
263 {
264 UINotificationMessage::cannotAcquireHostParameter(comHost);
265 return false;
266 }
267
268 /* Save whether agent is enabled: */
269 comAgent.SetEnabled(m_fCheckEnabled);
270 if (!comAgent.isOk())
271 {
272 UINotificationMessage::cannotChangeUpdateAgentParameter(comAgent);
273 return false;
274 }
275
276 /* Save 'period' value: */
277 comAgent.SetCheckFrequency(s_days.at(m_enmUpdatePeriod).length);
278 if (!comAgent.isOk())
279 {
280 UINotificationMessage::cannotChangeUpdateAgentParameter(comAgent);
281 return false;
282 }
283
284 /* Save 'update channel' value: */
285 comAgent.SetChannel(m_enmUpdateChannel);
286 if (!comAgent.isOk())
287 {
288 UINotificationMessage::cannotChangeUpdateAgentParameter(comAgent);
289 return false;
290 }
291
292 /* Success finally: */
293 return true;
294}
295
296bool VBoxUpdateData::isCheckEnabled() const
297{
298 return m_fCheckEnabled;
299}
300
301bool VBoxUpdateData::isCheckRequired() const
302{
303 return m_fCheckRequired;
304}
305
306QString VBoxUpdateData::data() const
307{
308 return m_strData;
309}
310
311UpdatePeriodType VBoxUpdateData::updatePeriod() const
312{
313 return m_enmUpdatePeriod;
314}
315
316QDate VBoxUpdateData::date() const
317{
318 return m_date;
319}
320
321QString VBoxUpdateData::dateToString() const
322{
323 return isCheckEnabled()
324 ? QLocale::system().toString(m_date, QLocale::ShortFormat)
325 : QCoreApplication::translate("UIUpdateManager", "Never");
326}
327
328KUpdateChannel VBoxUpdateData::updateChannel() const
329{
330 return m_enmUpdateChannel;
331}
332
333QString VBoxUpdateData::updateChannelName() const
334{
335 return updateChannelToInternalString(m_enmUpdateChannel);
336}
337
338UIVersion VBoxUpdateData::version() const
339{
340 return m_version;
341}
342
343QVector<KUpdateChannel> VBoxUpdateData::supportedUpdateChannels() const
344{
345 return m_supportedUpdateChannels;
346}
347
348bool VBoxUpdateData::isEqual(const VBoxUpdateData &another) const
349{
350 return true
351 && (m_fCheckEnabled == another.isCheckEnabled())
352 && (m_enmUpdatePeriod == another.updatePeriod())
353 && (m_enmUpdateChannel == another.updateChannel())
354 ;
355}
356
357bool VBoxUpdateData::operator==(const VBoxUpdateData &another) const
358{
359 return isEqual(another);
360}
361
362bool VBoxUpdateData::operator!=(const VBoxUpdateData &another) const
363{
364 return !isEqual(another);
365}
366
367/* static */
368QString VBoxUpdateData::updateChannelToInternalString(KUpdateChannel enmUpdateChannel)
369{
370 switch (enmUpdateChannel)
371 {
372 case KUpdateChannel_WithTesting: return "withtesting";
373 case KUpdateChannel_WithBetas: return "withbetas";
374 case KUpdateChannel_All: return "allrelease";
375 default: return "stable";
376 }
377}
378
379/* static */
380KUpdateChannel VBoxUpdateData::updateChannelFromInternalString(const QString &strUpdateChannel)
381{
382 QMap<QString, KUpdateChannel> pairs;
383 pairs["withtesting"] = KUpdateChannel_WithTesting;
384 pairs["withbetas"] = KUpdateChannel_WithBetas;
385 pairs["allrelease"] = KUpdateChannel_All;
386 return pairs.value(strUpdateChannel, KUpdateChannel_Stable);
387}
388
389/* static */
390UpdatePeriodType VBoxUpdateData::gatherSuitablePeriod(ULONG uFrequency)
391{
392 if (s_days.isEmpty())
393 populate();
394
395 UpdatePeriodType enmType = UpdatePeriodType_1Day;
396 foreach (const VBoxUpdateDay &day, s_days)
397 {
398 if (uFrequency <= day.length)
399 return enmType;
400 enmType = (UpdatePeriodType)(enmType + 1);
401 }
402
403 return UpdatePeriodType_1Month;
404}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette