1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "settingspage.h"
#include "updateinfoplugin.h"
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/settingsdatabase.h>
#include <coreplugin/shellcommand.h>
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/infobar.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <QDate>
#include <QDomDocument>
#include <QFile>
#include <QFileInfo>
#include <QLabel>
#include <QLoggingCategory>
#include <QMenu>
#include <QMetaEnum>
#include <QPointer>
#include <QProcessEnvironment>
#include <QTimer>
#include <QVersionNumber>
Q_LOGGING_CATEGORY(log, "qtc.updateinfo", QtWarningMsg)
const char UpdaterGroup[] = "Updater";
const char MaintenanceToolKey[] = "MaintenanceTool";
const char AutomaticCheckKey[] = "AutomaticCheck";
const char CheckForNewQtVersionsKey[] = "CheckForNewQtVersions";
const char CheckIntervalKey[] = "CheckUpdateInterval";
const char LastCheckDateKey[] = "LastCheckDate";
const char LastMaxQtVersionKey[] = "LastMaxQtVersion";
const quint32 OneMinute = 60000;
const quint32 OneHour = 3600000;
const char InstallUpdates[] = "UpdateInfo.InstallUpdates";
const char InstallQtUpdates[] = "UpdateInfo.InstallQtUpdates";
using namespace Core;
namespace UpdateInfo {
namespace Internal {
class UpdateInfoPluginPrivate
{
public:
QString m_maintenanceTool;
QPointer<ShellCommand> m_checkUpdatesCommand;
QPointer<FutureProgress> m_progress;
QString m_collectedOutput;
QTimer *m_checkUpdatesTimer = nullptr;
struct Settings
{
bool automaticCheck = true;
UpdateInfoPlugin::CheckUpdateInterval checkInterval = UpdateInfoPlugin::WeeklyCheck;
bool checkForQtVersions = true;
};
Settings m_settings;
QDate m_lastCheckDate;
QVersionNumber m_lastMaxQtVersion;
};
UpdateInfoPlugin::UpdateInfoPlugin()
: d(new UpdateInfoPluginPrivate)
{
d->m_checkUpdatesTimer = new QTimer(this);
d->m_checkUpdatesTimer->setTimerType(Qt::VeryCoarseTimer);
d->m_checkUpdatesTimer->setInterval(OneHour);
connect(d->m_checkUpdatesTimer, &QTimer::timeout,
this, &UpdateInfoPlugin::doAutoCheckForUpdates);
}
UpdateInfoPlugin::~UpdateInfoPlugin()
{
stopCheckForUpdates();
if (!d->m_maintenanceTool.isEmpty())
saveSettings();
delete d;
}
void UpdateInfoPlugin::startAutoCheckForUpdates()
{
doAutoCheckForUpdates();
d->m_checkUpdatesTimer->start();
}
void UpdateInfoPlugin::stopAutoCheckForUpdates()
{
d->m_checkUpdatesTimer->stop();
}
void UpdateInfoPlugin::doAutoCheckForUpdates()
{
if (d->m_checkUpdatesCommand)
return; // update task is still running (might have been run manually just before)
if (nextCheckDate().isValid() && nextCheckDate() > QDate::currentDate())
return; // not a time for check yet
startCheckForUpdates();
}
void UpdateInfoPlugin::startCheckForUpdates()
{
stopCheckForUpdates();
d->m_checkUpdatesCommand = new ShellCommand({}, Utils::Environment::systemEnvironment());
d->m_checkUpdatesCommand->setDisplayName(tr("Checking for Updates"));
connect(d->m_checkUpdatesCommand, &ShellCommand::stdOutText, this, &UpdateInfoPlugin::collectCheckForUpdatesOutput);
connect(d->m_checkUpdatesCommand, &ShellCommand::finished, this, &UpdateInfoPlugin::checkForUpdatesFinished);
d->m_checkUpdatesCommand->addJob({Utils::FilePath::fromString(d->m_maintenanceTool),
{"ch", "-g", "*=false,ifw.package.*=true"}},
60 * 3, // 3 minutes timeout
/*workingDirectory=*/{},
[](int /*exitCode*/) {
return Utils::QtcProcess::FinishedWithSuccess;
});
if (d->m_settings.checkForQtVersions) {
d->m_checkUpdatesCommand
->addJob({Utils::FilePath::fromString(d->m_maintenanceTool),
{"se", "qt[.]qt[0-9][.][0-9]+$", "-g", "*=false,ifw.package.*=true"}},
60 * 3, // 3 minutes timeout
/*workingDirectory=*/{},
[](int /*exitCode*/) { return Utils::QtcProcess::FinishedWithSuccess; });
}
d->m_checkUpdatesCommand->execute();
d->m_progress = d->m_checkUpdatesCommand->futureProgress();
if (d->m_progress) {
d->m_progress->setKeepOnFinish(FutureProgress::KeepOnFinishTillUserInteraction);
d->m_progress->setSubtitleVisibleInStatusBar(true);
}
emit checkForUpdatesRunningChanged(true);
}
void UpdateInfoPlugin::stopCheckForUpdates()
{
if (!d->m_checkUpdatesCommand)
return;
d->m_collectedOutput.clear();
d->m_checkUpdatesCommand->disconnect();
d->m_checkUpdatesCommand->cancel();
d->m_checkUpdatesCommand = nullptr;
emit checkForUpdatesRunningChanged(false);
}
void UpdateInfoPlugin::collectCheckForUpdatesOutput(const QString &contents)
{
d->m_collectedOutput += contents;
}
struct Update
{
QString name;
QString version;
};
static QList<Update> availableUpdates(const QDomDocument &document)
{
if (document.isNull() || !document.firstChildElement().hasChildNodes())
return {};
QList<Update> result;
const QDomNodeList updates = document.firstChildElement().elementsByTagName("update");
for (int i = 0; i < updates.size(); ++i) {
const QDomNode node = updates.item(i);
if (node.isElement()) {
const QDomElement element = node.toElement();
if (element.hasAttribute("name"))
result.append({element.attribute("name"), element.attribute("version")});
}
}
return result;
}
struct QtPackage
{
QString displayName;
QVersionNumber version;
bool installed;
bool isPrerelease = false;
};
static QList<QtPackage> availableQtPackages(const QDomDocument &document)
{
if (document.isNull() || !document.firstChildElement().hasChildNodes())
return {};
QList<QtPackage> result;
const QDomNodeList packages = document.firstChildElement().elementsByTagName("package");
for (int i = 0; i < packages.size(); ++i) {
const QDomNode node = packages.item(i);
if (node.isElement()) {
const QDomElement element = node.toElement();
if (element.hasAttribute("displayname") && element.hasAttribute("name")
&& element.hasAttribute("version")) {
QtPackage package{element.attribute("displayname"),
QVersionNumber::fromString(element.attribute("version")),
element.hasAttribute("installedVersion")};
// Heuristic: Prerelease if the name is not "Qt x.y.z"
// (prereleases are named "Qt x.y.z-alpha" etc)
package.isPrerelease = package.displayName
!= QString("Qt %1").arg(package.version.toString());
result.append(package);
}
}
}
std::sort(result.begin(), result.end(), [](const QtPackage &p1, const QtPackage &p2) {
return p1.version > p2.version;
});
return result;
}
// Expects packages to be sorted, high version first.
static Utils::optional<QtPackage> highestInstalledQt(const QList<QtPackage> &packages)
{
const auto highestInstalledIt = std::find_if(packages.cbegin(),
packages.cend(),
[](const QtPackage &p) { return p.installed; });
if (highestInstalledIt == packages.cend()) // Qt not installed
return {};
return *highestInstalledIt;
}
// Expects packages to be sorted, high version first.
static Utils::optional<QtPackage> qtToNagAbout(const QList<QtPackage> &allPackages,
QVersionNumber *highestSeen)
{
// Filter out any Qt prereleases
const QList<QtPackage> packages = Utils::filtered(allPackages, [](const QtPackage &p) {
return !p.isPrerelease;
});
if (packages.isEmpty())
return {};
const QtPackage highest = packages.constFirst();
qCDebug(log) << "Highest available (non-prerelease) Qt:" << highest.version;
qCDebug(log) << "Highest previously seen (non-prerelease) Qt:" << *highestSeen;
// if the highestSeen version is null, we don't know if the Qt version is new, and better don't nag
const bool isNew = !highestSeen->isNull() && highest.version > *highestSeen;
if (highestSeen->isNull() || isNew)
*highestSeen = highest.version;
if (!isNew)
return {};
const Utils::optional<QtPackage> highestInstalled = highestInstalledQt(packages);
qCDebug(log) << "Highest installed Qt:"
<< qPrintable(highestInstalled ? highestInstalled->version.toString()
: QString("none"));
if (!highestInstalled) // don't nag if no Qt is installed at all
return {};
if (highestInstalled->version == highest.version)
return {};
return highest;
}
static void showUpdateInfo(const QList<Update> &updates, const std::function<void()> &startUpdater)
{
Utils::InfoBarEntry info(InstallUpdates,
UpdateInfoPlugin::tr("New updates are available. Start the update?"));
info.addCustomButton(UpdateInfoPlugin::tr("Start Update"), [startUpdater] {
Core::ICore::infoBar()->removeInfo(InstallUpdates);
startUpdater();
});
info.setDetailsWidgetCreator([updates]() -> QWidget * {
const QString updateText = Utils::transform(updates, [](const Update &u) {
return u.version.isEmpty()
? u.name
: UpdateInfoPlugin::tr("%1 (%2)",
"Package name and version")
.arg(u.name, u.version);
}).join("</li><li>");
auto label = new QLabel;
label->setText("<qt><p>" + UpdateInfoPlugin::tr("Available updates:") + "<ul><li>"
+ updateText + "</li></ul></p></qt>");
label->setContentsMargins(0, 0, 0, 8);
return label;
});
Core::ICore::infoBar()->removeInfo(InstallUpdates); // remove any existing notifications
Core::ICore::infoBar()->unsuppressInfo(InstallUpdates);
Core::ICore::infoBar()->addInfo(info);
}
static void showQtUpdateInfo(const QtPackage &package,
const std::function<void()> &startPackageManager)
{
Utils::InfoBarEntry info(InstallQtUpdates,
UpdateInfoPlugin::tr(
"%1 is available. Check the <a "
"href=\"https://www.qt.io/blog\">Qt blog</a> for details.")
.arg(package.displayName));
info.addCustomButton(UpdateInfoPlugin::tr("Start Package Manager"), [startPackageManager] {
Core::ICore::infoBar()->removeInfo(InstallQtUpdates);
startPackageManager();
});
info.addCustomButton(UpdateInfoPlugin::tr("Open Settings"), [] {
Core::ICore::infoBar()->removeInfo(InstallQtUpdates);
Core::ICore::showOptionsDialog(FILTER_OPTIONS_PAGE_ID);
});
Core::ICore::infoBar()->removeInfo(InstallQtUpdates); // remove any existing notifications
Core::ICore::infoBar()->unsuppressInfo(InstallQtUpdates);
Core::ICore::infoBar()->addInfo(info);
}
void UpdateInfoPlugin::checkForUpdatesFinished()
{
setLastCheckDate(QDate::currentDate());
QDomDocument document;
// since the output can contain two toplevel items from the two separate MaintenanceTool runs,
// surround with a toplevel element
const QString xml = d->m_collectedOutput.isEmpty()
? QString()
: ("<doc>" + d->m_collectedOutput + "</doc>");
qCDebug(log) << "--- MaintenanceTool output (combined):";
qCDebug(log) << qPrintable(xml);
document.setContent(xml);
stopCheckForUpdates();
const QList<Update> updates = availableUpdates(document);
const QList<QtPackage> qtPackages = availableQtPackages(document);
if (log().isDebugEnabled()) {
qCDebug(log) << "--- Available updates:";
for (const Update &u : updates)
qCDebug(log) << u.name << u.version;
qCDebug(log) << "--- Available Qt packages:";
for (const QtPackage &p : qtPackages) {
qCDebug(log) << p.displayName << p.version << "installed:" << p.installed
<< "prerelease:" << p.isPrerelease;
}
}
Utils::optional<QtPackage> qtToNag = qtToNagAbout(qtPackages, &d->m_lastMaxQtVersion);
if (!updates.isEmpty() || qtToNag) {
// progress details are shown until user interaction for the "no updates" case,
// so we can show the "No updates found" text, but if we have updates we don't
// want to keep it around
if (d->m_progress)
d->m_progress->setKeepOnFinish(FutureProgress::HideOnFinish);
emit newUpdatesAvailable(true);
if (!updates.isEmpty())
showUpdateInfo(updates, [this] { startUpdater(); });
if (qtToNag)
showQtUpdateInfo(*qtToNag, [this] { startPackageManager(); });
} else {
emit newUpdatesAvailable(false);
if (d->m_progress)
d->m_progress->setSubtitle(tr("No updates found."));
}
}
bool UpdateInfoPlugin::isCheckForUpdatesRunning() const
{
return d->m_checkUpdatesCommand;
}
void UpdateInfoPlugin::extensionsInitialized()
{
if (isAutomaticCheck())
QTimer::singleShot(OneMinute, this, &UpdateInfoPlugin::startAutoCheckForUpdates);
}
bool UpdateInfoPlugin::initialize(const QStringList & /* arguments */, QString *errorMessage)
{
loadSettings();
if (d->m_maintenanceTool.isEmpty()) {
*errorMessage = tr("Could not determine location of maintenance tool. Please check "
"your installation if you did not enable this plugin manually.");
return false;
}
if (!QFileInfo(d->m_maintenanceTool).isExecutable()) {
*errorMessage = tr("The maintenance tool at \"%1\" is not an executable. Check your installation.")
.arg(d->m_maintenanceTool);
d->m_maintenanceTool.clear();
return false;
}
connect(ICore::instance(), &ICore::saveSettingsRequested,
this, &UpdateInfoPlugin::saveSettings);
(void) new SettingsPage(this);
QAction *checkForUpdatesAction = new QAction(tr("Check for Updates"), this);
checkForUpdatesAction->setMenuRole(QAction::ApplicationSpecificRole);
Core::Command *checkForUpdatesCommand = Core::ActionManager::registerAction(checkForUpdatesAction, "Updates.CheckForUpdates");
connect(checkForUpdatesAction, &QAction::triggered, this, &UpdateInfoPlugin::startCheckForUpdates);
ActionContainer *const helpContainer = ActionManager::actionContainer(Core::Constants::M_HELP);
helpContainer->addAction(checkForUpdatesCommand, Constants::G_HELP_UPDATES);
return true;
}
void UpdateInfoPlugin::loadSettings() const
{
UpdateInfoPluginPrivate::Settings def;
QSettings *settings = ICore::settings();
const QString updaterKey = QLatin1String(UpdaterGroup) + '/';
d->m_maintenanceTool = settings->value(updaterKey + MaintenanceToolKey).toString();
d->m_lastCheckDate = settings->value(updaterKey + LastCheckDateKey, QDate()).toDate();
d->m_settings.automaticCheck
= settings->value(updaterKey + AutomaticCheckKey, def.automaticCheck).toBool();
const QMetaObject *mo = metaObject();
const QMetaEnum me = mo->enumerator(mo->indexOfEnumerator(CheckIntervalKey));
if (QTC_GUARD(me.isValid())) {
const QString checkInterval = settings
->value(updaterKey + CheckIntervalKey,
me.valueToKey(def.checkInterval))
.toString();
bool ok = false;
const int newValue = me.keyToValue(checkInterval.toUtf8(), &ok);
if (ok)
d->m_settings.checkInterval = static_cast<CheckUpdateInterval>(newValue);
}
const QString lastMaxQtVersionString = settings->value(updaterKey + LastMaxQtVersionKey)
.toString();
d->m_lastMaxQtVersion = QVersionNumber::fromString(lastMaxQtVersionString);
d->m_settings.checkForQtVersions
= settings->value(updaterKey + CheckForNewQtVersionsKey, def.checkForQtVersions).toBool();
}
void UpdateInfoPlugin::saveSettings()
{
UpdateInfoPluginPrivate::Settings def;
Utils::QtcSettings *settings = ICore::settings();
settings->beginGroup(UpdaterGroup);
settings->setValueWithDefault(LastCheckDateKey, d->m_lastCheckDate, QDate());
settings->setValueWithDefault(AutomaticCheckKey,
d->m_settings.automaticCheck,
def.automaticCheck);
// Note: don't save MaintenanceToolKey on purpose! This setting may be set only by installer.
// If creator is run not from installed SDK, the setting can be manually created here:
// [CREATOR_INSTALLATION_LOCATION]/share/qtcreator/QtProject/QtCreator.ini or
// [CREATOR_INSTALLATION_LOCATION]/Qt Creator.app/Contents/Resources/QtProject/QtCreator.ini on OS X
const QMetaObject *mo = metaObject();
const QMetaEnum me = mo->enumerator(mo->indexOfEnumerator(CheckIntervalKey));
settings->setValueWithDefault(CheckIntervalKey,
QString::fromUtf8(me.valueToKey(d->m_settings.checkInterval)),
QString::fromUtf8(me.valueToKey(def.checkInterval)));
settings->setValueWithDefault(LastMaxQtVersionKey, d->m_lastMaxQtVersion.toString());
settings->setValueWithDefault(CheckForNewQtVersionsKey,
d->m_settings.checkForQtVersions,
def.checkForQtVersions);
settings->endGroup();
}
bool UpdateInfoPlugin::isAutomaticCheck() const
{
return d->m_settings.automaticCheck;
}
void UpdateInfoPlugin::setAutomaticCheck(bool on)
{
if (d->m_settings.automaticCheck == on)
return;
d->m_settings.automaticCheck = on;
if (on)
startAutoCheckForUpdates();
else
stopAutoCheckForUpdates();
}
UpdateInfoPlugin::CheckUpdateInterval UpdateInfoPlugin::checkUpdateInterval() const
{
return d->m_settings.checkInterval;
}
void UpdateInfoPlugin::setCheckUpdateInterval(UpdateInfoPlugin::CheckUpdateInterval interval)
{
if (d->m_settings.checkInterval == interval)
return;
d->m_settings.checkInterval = interval;
}
bool UpdateInfoPlugin::isCheckingForQtVersions() const
{
return d->m_settings.checkForQtVersions;
}
void UpdateInfoPlugin::setCheckingForQtVersions(bool on)
{
d->m_settings.checkForQtVersions = on;
}
QDate UpdateInfoPlugin::lastCheckDate() const
{
return d->m_lastCheckDate;
}
void UpdateInfoPlugin::setLastCheckDate(const QDate &date)
{
if (d->m_lastCheckDate == date)
return;
d->m_lastCheckDate = date;
emit lastCheckDateChanged(date);
}
QDate UpdateInfoPlugin::nextCheckDate() const
{
return nextCheckDate(d->m_settings.checkInterval);
}
QDate UpdateInfoPlugin::nextCheckDate(CheckUpdateInterval interval) const
{
if (!d->m_lastCheckDate.isValid())
return QDate();
if (interval == DailyCheck)
return d->m_lastCheckDate.addDays(1);
if (interval == WeeklyCheck)
return d->m_lastCheckDate.addDays(7);
return d->m_lastCheckDate.addMonths(1);
}
void UpdateInfoPlugin::startUpdater()
{
Utils::QtcProcess::startDetached(
{Utils::FilePath::fromString(d->m_maintenanceTool), {"--updater"}});
}
void UpdateInfoPlugin::startPackageManager()
{
Utils::QtcProcess::startDetached(
{Utils::FilePath::fromString(d->m_maintenanceTool), {"--start-package-manager"}});
}
} //namespace Internal
} //namespace UpdateInfo
|