diff --git a/src/AppGui.cpp b/src/AppGui.cpp index bf033f68..aa33d662 100644 --- a/src/AppGui.cpp +++ b/src/AppGui.cpp @@ -24,6 +24,8 @@ #include "MacUtils.h" #endif +#define GITHUB_UPDATE_URL "https://api.github.com/repos/mooltipass/moolticute/releases" + #if defined(Q_OS_WIN) #define MC_UPDATE_URL "https://calaos.fr/mooltipass/windows/updater.json" #elif defined(Q_OS_MAC) @@ -521,12 +523,12 @@ void AppGui::checkUpdate(bool displayMessage) return; auto u = QSimpleUpdater::getInstance(); - u->setModuleVersion(MC_UPDATE_URL, APP_VERSION); - u->setNotifyOnUpdate(MC_UPDATE_URL, true); - u->setDownloaderEnabled(MC_UPDATE_URL, true); - u->setNotifyOnFinish(MC_UPDATE_URL, displayMessage); + u->setModuleVersion(GITHUB_UPDATE_URL, APP_VERSION); + u->setNotifyOnUpdate(GITHUB_UPDATE_URL, true); + u->setDownloaderEnabled(GITHUB_UPDATE_URL, true); + u->setNotifyOnFinish(GITHUB_UPDATE_URL, displayMessage); - u->checkForUpdates(MC_UPDATE_URL); + u->checkForUpdates(GITHUB_UPDATE_URL); //Recheck in at least 30minutes plus some random time if (!displayMessage) diff --git a/src/QSimpleUpdater/src/Updater.cpp b/src/QSimpleUpdater/src/Updater.cpp index 6c95a4fb..9640510c 100644 --- a/src/QSimpleUpdater/src/Updater.cpp +++ b/src/QSimpleUpdater/src/Updater.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -361,18 +362,58 @@ void Updater::onReply (QNetworkReply* reply) return; } - /* Get the platform information */ - QJsonObject updates = document.object().value ("updates").toObject(); - QJsonObject platform = updates.value (platformKey()).toObject(); + /* Get latest GitHub Release */ + QJsonArray githubReleases = document.array(); + QJsonObject latestGithubRelese = githubReleases.at(0).toObject(); + QString tagName = latestGithubRelese.value("tag_name").toString(); + QString htmlUrl = latestGithubRelese.value("html_url").toString(); + QString body = latestGithubRelese.value("body").toString(); - /* Get update information */ - m_openUrl = platform.value ("open-url").toString(); - m_changelog = platform.value ("changelog").toString(); - m_downloadUrl = platform.value ("download-url").toString(); - m_latestVersion = platform.value ("latest-version").toString(); + QString releaseFileSuffix; + if (platformKey().compare("osx") == 0) + releaseFileSuffix = ".dmg"; - /* Compare latest and current version */ - setUpdateAvailable (latestVersion() != moduleVersion()); + if (platformKey().compare("linux") == 0) + releaseFileSuffix = ".deb"; + + if (platformKey().compare("windows") == 0) + releaseFileSuffix = ".exe"; + + if (releaseFileSuffix.isEmpty()) + { + qWarning() << "Automatic updates are not suppurted in this platform " << platformKey(); + setUpdateAvailable(false); + emit checkingFinished (url()); + return; + } + + QJsonArray githubReleaseAssets = latestGithubRelese.value("assets").toArray(); + bool releaseFound = false; + QString releaseName; + QString downloadUrl; + for (QJsonValue jsonValue : githubReleaseAssets) + { + QJsonObject releaseAsset = jsonValue.toObject(); + releaseName = releaseAsset.value("name").toString(); + + if (releaseName.endsWith(releaseFileSuffix)) + { + releaseFound = true; + downloadUrl = releaseAsset.value("browser_download_url").toString(); + break; + } + } + if (releaseFound) + { + m_openUrl = htmlUrl; + m_changelog = body; + m_downloadUrl = downloadUrl; + m_latestVersion = tagName; + qDebug() << m_downloadUrl; + + /* Compare latest and current version */ + setUpdateAvailable (latestVersion() != moduleVersion()); + } emit checkingFinished (url()); } @@ -401,15 +442,14 @@ void Updater::setUpdateAvailable (const bool available) box.setDefaultButton (QMessageBox::Yes); if (box.exec() == QMessageBox::Yes) { - if (!openUrl().isEmpty()) + if (downloadUrl().isEmpty()) QDesktopServices::openUrl (QUrl (openUrl())); - else if (downloaderEnabled()) { + if (downloaderEnabled()) { m_downloader->setUrlId (url()); m_downloader->setFileName (downloadUrl().split ("/").last()); m_downloader->startDownload (QUrl (downloadUrl())); } - else QDesktopServices::openUrl (QUrl (downloadUrl())); } diff --git a/tests/tst_filescache.cpp b/tests/FilesCacheTests.cpp similarity index 62% rename from tests/tst_filescache.cpp rename to tests/FilesCacheTests.cpp index 41cf6c31..b036463a 100644 --- a/tests/tst_filescache.cpp +++ b/tests/FilesCacheTests.cpp @@ -1,25 +1,10 @@ -#include -#include +#include "FilesCacheTests.h" -#include -#include "../src/FilesCache.h" - -class TestsFilesCache : public QObject -{ - Q_OBJECT - -public: - TestsFilesCache(); - -private Q_SLOTS: - void testSaveAndLoadFileNames(); -}; - -TestsFilesCache::TestsFilesCache() +FilesCacheTests::FilesCacheTests() { } -void TestsFilesCache::testSaveAndLoadFileNames() +void FilesCacheTests::testSaveAndLoadFileNames() { QList testFiles; for (int i = 0; i< 3; i++) @@ -46,8 +31,3 @@ void TestsFilesCache::testSaveAndLoadFileNames() QVERIFY(cache.erase()); } - - -QTEST_APPLESS_MAIN(TestsFilesCache) - -#include "tst_filescache.moc" diff --git a/tests/FilesCacheTests.h b/tests/FilesCacheTests.h new file mode 100644 index 00000000..cfc6af1c --- /dev/null +++ b/tests/FilesCacheTests.h @@ -0,0 +1,20 @@ +#include +#include + +#include +#include "../src/FilesCache.h" + +class FilesCacheTests : public QObject +{ + Q_OBJECT + +public: + FilesCacheTests(); + +private Q_SLOTS: + void testSaveAndLoadFileNames(); +}; + + + + diff --git a/tests/UpdaterTests.cpp b/tests/UpdaterTests.cpp new file mode 100644 index 00000000..399258b3 --- /dev/null +++ b/tests/UpdaterTests.cpp @@ -0,0 +1,24 @@ +#include "UpdaterTests.h" + +#include + +#include "../src/QSimpleUpdater/include/QSimpleUpdater.h" + +void UpdaterTests::testCheckForUpdates() +{ + QString githubApiReleasesUrl ="https://api.github.com/repos/mooltipass/moolticute/releases"; + QSimpleUpdater *updater = QSimpleUpdater::getInstance(); + + updater->setModuleVersion(githubApiReleasesUrl, "0.0"); + updater->setNotifyOnUpdate(githubApiReleasesUrl, true); + updater->setDownloaderEnabled(githubApiReleasesUrl, true); + updater->setNotifyOnFinish(githubApiReleasesUrl, "All done"); + + updater->checkForUpdates(githubApiReleasesUrl); + + QSignalSpy spyChecking(updater, &QSimpleUpdater::checkingFinished); + spyChecking.wait(5000); + + QSignalSpy spyDownload(updater, &QSimpleUpdater::downloadFinished); + spyDownload.wait(30000); +} diff --git a/tests/UpdaterTests.h b/tests/UpdaterTests.h new file mode 100644 index 00000000..0cb3ecb1 --- /dev/null +++ b/tests/UpdaterTests.h @@ -0,0 +1,11 @@ +#include +#include + +#include + +class UpdaterTests : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void testCheckForUpdates(); +}; diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 00000000..c910a0bc --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,26 @@ +#include + +#include "FilesCacheTests.h" +#include "UpdaterTests.h" + +#define TEST_CLASS(TestClass, status) \ +{ \ + TestClass tc; \ + status |= QTest::qExec(&tc, argc, argv);\ +} + + +// Note: This is equivalent to QTEST_APPLESS_MAIN for multiple test classes. +int main(int argc, char** argv) +{ + QApplication app(argc, argv); + app.setAttribute(Qt::AA_Use96Dpi, true); + + int status = 0; + TEST_CLASS(FilesCache, status); + TEST_CLASS(UpdaterTests, status); + + return status; +} + + diff --git a/tests/tests.pro b/tests/tests.pro index ac5280a1..27b7842f 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -10,7 +10,7 @@ QT -= gui QMAKE_CXXFLAGS += -std=c++0x -TARGET = tst_testsfilescache +TARGET = tests CONFIG += console CONFIG -= app_bundle @@ -27,14 +27,19 @@ DEFINES += QT_DEPRECATED_WARNINGS # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 +include (../src/QSimpleUpdater/QSimpleUpdater.pri) SOURCES += \ - tst_filescache.cpp \ ../src/SimpleCrypt/SimpleCrypt.cpp \ - ../src/FilesCache.cpp + ../src/FilesCache.cpp \ + main.cpp \ + FilesCacheTests.cpp \ + UpdaterTests.cpp HEADERS += \ ../src/SimpleCrypt/SimpleCrypt.h \ - ../src/FilesCache.h + ../src/FilesCache.h \ + UpdaterTests.h \ + FilesCacheTests.h DEFINES += SRCDIR=\\\"$$PWD/\\\"