diff options
author | Thomas Hartmann <thomas.hartmann@qt.io> | 2022-05-12 17:22:58 +0200 |
---|---|---|
committer | Thomas Hartmann <thomas.hartmann@qt.io> | 2022-05-13 14:46:54 +0000 |
commit | 96144d12bb0a0188e29266bbc59c715a1ccec965 (patch) | |
tree | f1e43c4f7cbde6e4d53b76e2df91997dc9c1fff9 | |
parent | d0a9b873cb3c8a8b54cbfaa51fbf9a913bf192e7 (diff) |
StudioWelcome: Download data from Qt.io
We try to download new updated data from Qt.io
if it is newer.
In any case we provide a default that comes with QDS.
I removed the check for QmlData, since they should not be required
and they stop FileDownloader from running from "pure" C++.
Task-number: QDS-6886
Change-Id: I1332c194286e6e91dfcd1c6605931f1ed020bc29
Reviewed-by: Henning Gründl <henning.gruendl@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r-- | src/plugins/studiowelcome/examplecheckout.cpp | 84 | ||||
-rw-r--r-- | src/plugins/studiowelcome/examplecheckout.h | 26 | ||||
-rw-r--r-- | src/plugins/studiowelcome/studiowelcomeplugin.cpp | 26 |
3 files changed, 122 insertions, 14 deletions
diff --git a/src/plugins/studiowelcome/examplecheckout.cpp b/src/plugins/studiowelcome/examplecheckout.cpp index 057be29789..aae7eb0778 100644 --- a/src/plugins/studiowelcome/examplecheckout.cpp +++ b/src/plugins/studiowelcome/examplecheckout.cpp @@ -196,17 +196,6 @@ void FileDownloader::probeUrl() }); QNetworkReply::connect(reply, &QNetworkReply::finished, this, [this, reply]() { - QQmlData *data = QQmlData::get(this, false); - if (!data) { - qDebug() << Q_FUNC_INFO << "FileDownloader is nullptr."; - return; - } - - if (QQmlData::wasDeleted(this)) { - qDebug() << Q_FUNC_INFO << "FileDownloader was deleted."; - return; - } - if (reply->error()) return; @@ -435,3 +424,76 @@ void FileExtractor::extract() QTC_ASSERT(ret, ;); }); } + +static Utils::FilePath tempFilePath() +{ + QStandardPaths::StandardLocation location = QStandardPaths::CacheLocation; + + return Utils::FilePath::fromString(QStandardPaths::writableLocation(location)) + .pathAppended("QtDesignStudio"); +} + +DataModelDownloader::DataModelDownloader(QObject * /* parent */) +{ + auto fileInfo = targetFolder().toFileInfo(); + m_birthTime = fileInfo.birthTime(); + m_exists = fileInfo.exists(); +} + +void DataModelDownloader::start() +{ + m_fileDownloader.setUrl(QUrl::fromUserInput( + "https://download.qt.io/learning/examples/qtdesignstudio/dataImports.zip")); + + connect(&m_fileDownloader, &FileDownloader::availableChanged, this, [this]() { + + m_available = m_fileDownloader.available(); + + emit availableChanged(); + + if (!m_available) { + qWarning() << m_fileDownloader.url() << "failed to download"; + return; + } + + if (!m_forceDownload && m_fileDownloader.lastModified() < m_birthTime) + return; + + m_fileDownloader.start(); + connect(&m_fileDownloader, &FileDownloader::finishedChanged, this, [this]() { + if (m_fileDownloader.finished()) { + Utils::Archive *archive = Utils::Archive::unarchive(Utils::FilePath::fromString( + m_fileDownloader.tempFile()), + tempFilePath()); + + archive->setParent(this); + QTC_ASSERT(archive, return ); + + QObject::connect(archive, &Utils::Archive::finished, this, [this](bool ret) { + emit finished(); + QTC_ASSERT(ret, ;); + }); + } + }); + }); +} + +bool DataModelDownloader::exists() const +{ + return m_exists; +} + +bool DataModelDownloader::available() const +{ + return m_available; +} + +Utils::FilePath DataModelDownloader::targetFolder() const +{ + return Utils::FilePath::fromUserInput(tempFilePath().toString() + "/" + "dataImports"); +} + +void DataModelDownloader::setForceDownload(bool b) +{ + m_forceDownload = b; +} diff --git a/src/plugins/studiowelcome/examplecheckout.h b/src/plugins/studiowelcome/examplecheckout.h index af2be4893e..a6c0df3ace 100644 --- a/src/plugins/studiowelcome/examplecheckout.h +++ b/src/plugins/studiowelcome/examplecheckout.h @@ -154,5 +154,29 @@ private: int m_progress = 0; QFile m_tempFile; QDateTime m_lastModified; - bool m_available; + bool m_available = false; +}; + +class DataModelDownloader : public QObject +{ + Q_OBJECT + +public: + explicit DataModelDownloader(QObject *parent = nullptr); + void start(); + bool exists() const; + bool available() const; + Utils::FilePath targetFolder() const; + void setForceDownload(bool b); + +signals: + void finished(); + void availableChanged(); + +private: + FileDownloader m_fileDownloader; + QDateTime m_birthTime; + bool m_exists = false; + bool m_available = false; + bool m_forceDownload = false; }; diff --git a/src/plugins/studiowelcome/studiowelcomeplugin.cpp b/src/plugins/studiowelcome/studiowelcomeplugin.cpp index 17c37fccf3..338793e405 100644 --- a/src/plugins/studiowelcome/studiowelcomeplugin.cpp +++ b/src/plugins/studiowelcome/studiowelcomeplugin.cpp @@ -498,6 +498,7 @@ public: private: QQuickWidget *m_modeWidget = nullptr; + DataModelDownloader *m_dataModelDownloader = nullptr; }; void StudioWelcomePlugin::closeSplashScreen() @@ -647,6 +648,28 @@ WelcomeMode::WelcomeMode() { setDisplayName(tr("Welcome")); + const QString welcomePagePath = Core::ICore::resourcePath("qmldesigner/welcomepage").toString(); + + m_dataModelDownloader = new DataModelDownloader(this); + if (!m_dataModelDownloader->exists()) { //Fallback if data cannot be downloaded + Utils::FileUtils::copyRecursively(Utils::FilePath::fromUserInput(welcomePagePath + + "/dataImports"), + m_dataModelDownloader->targetFolder()); + m_dataModelDownloader->setForceDownload(true); + } + Utils::FilePath readme = Utils::FilePath::fromUserInput(welcomePagePath + + "/dataImports/readme.txt"); + + if (!readme.exists()) // Only downloads contain the readme + m_dataModelDownloader->setForceDownload(true); + + m_dataModelDownloader->start(); + + connect(m_dataModelDownloader, &DataModelDownloader::finished, this, [this](){ + auto source = m_modeWidget->source(); + m_modeWidget->engine()->clearComponentCache(); + m_modeWidget->setSource(source); + }); const Utils::Icon FLAT({{":/studiowelcome/images/mode_welcome_mask.png", Utils::Theme::IconsBaseColor}}); const Utils::Icon FLAT_ACTIVE({{":/studiowelcome/images/mode_welcome_mask.png", @@ -691,9 +714,8 @@ WelcomeMode::WelcomeMode() m_modeWidget->engine()->addImportPath(Core::ICore::resourcePath("qmldesigner/propertyEditorQmlSources/imports").toString()); - const QString welcomePagePath = Core::ICore::resourcePath("qmldesigner/welcomepage").toString(); m_modeWidget->engine()->addImportPath(welcomePagePath + "/imports"); - m_modeWidget->engine()->addImportPath(welcomePagePath + "/dataImports"); + m_modeWidget->engine()->addImportPath(m_dataModelDownloader->targetFolder().toString()); m_modeWidget->setSource(QUrl::fromLocalFile(welcomePagePath + "/main.qml")); QShortcut *updateShortcut = nullptr; |