Skip to content

Commit

Permalink
Update improvements. Fix #113, Fix #114.
Browse files Browse the repository at this point in the history
  • Loading branch information
niello committed Mar 19, 2022
1 parent 5b29bcc commit b8e1af4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 43 deletions.
124 changes: 82 additions & 42 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,60 +55,68 @@ Application::Application(int& argc, char** argv)
processEvents();
}

_cmdLine = new QCommandLineParser();
_cmdLine->setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
_cmdLine->addOptions(
{
{ "updateResult", tr("Update result code, 0 if succeeded."), tr("updateResult") },
{ "updateMessage", tr("Update results messaged by an updater."), tr("updateMessage") },
});
_cmdLine->process(*this);

_network = new QNetworkAccessManager(this);

_mainWindow = new MainWindow();

ImagesetEditor::createToolbar(*this);
LayoutEditor::createToolbar(*this);

_mainWindow->show();
_mainWindow->raise();

if (splash)
{
splash->finish(_mainWindow);
delete splash;
}

_cmdLine = new QCommandLineParser();
_cmdLine->setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
_cmdLine->addOptions(
{
{ "updateResult", tr("Update result code, 0 if succeeded."), tr("updateResult") },
{ "updateMessage", tr("Update results messaged by an updater."), tr("updateMessage") },
});
_cmdLine->process(*this);
// Bring our application to front before we show any message box
// TODO: leave only necessary calls
_mainWindow->show();
_mainWindow->raise();
_mainWindow->activateWindow();
_mainWindow->setWindowState(_mainWindow->windowState() | Qt::WindowState::WindowActive);

if (_cmdLine->positionalArguments().size() > 0)
{
// Load project specified in a command line
_mainWindow->loadProject(_cmdLine->positionalArguments().first());
}
else
checkUpdateResults();

// Checking for updates is async, initialization will continue after it finishes
checkForUpdates(false, [this]()
{
// Perform a startup action
const auto action = _settings->getEntryValue("global/app/startup_action").toInt();
switch (action)
_mainWindow->setStatusMessage("");

// Now we can load requested project, if any
if (_cmdLine->positionalArguments().size() > 0)
{
// Load project specified in a command line
_mainWindow->loadProject(_cmdLine->positionalArguments().first());
}
else
{
case 1:
// Perform a startup action
const auto action = _settings->getEntryValue("global/app/startup_action").toInt();
switch (action)
{
if (_settings->getQSettings()->contains("lastProject"))
case 1:
{
const QString lastProject = _settings->getQSettings()->value("lastProject").toString();
if (QFileInfo::exists(lastProject))
_mainWindow->loadProject(lastProject);
if (_settings->getQSettings()->contains("lastProject"))
{
const QString lastProject = _settings->getQSettings()->value("lastProject").toString();
if (QFileInfo::exists(lastProject))
_mainWindow->loadProject(lastProject);
}
break;
}
break;
default: break; // 0: empty environment
}
default: break; // 0: empty environment
}
}

checkUpdateResults();

// TODO: if not disabled in settings / if time has come
checkForUpdates(false);
});
}

Application::~Application()
Expand Down Expand Up @@ -205,28 +213,56 @@ QString Application::getUpdatePath() const
return QDir::temp().absoluteFilePath("CEEDUpdate");
}

void Application::checkForUpdates(bool manual)
void Application::checkForUpdates(bool manual, const std::function<void()>& cb)
{
if (!Utils::isInternetConnected())
{
qCritical() << "No Internet connection, update check skipped";
if (cb) cb();
return;
}

const QUrl infoUrl = _settings->getQSettings()->value("updateInfoUrl", "https://api.github.com/repos/cegui/ceed-cpp/releases/latest").toUrl();
const auto currTime = QDateTime::currentSecsSinceEpoch();

// Automatic update checks should honor their settings
if (!manual)
{
const auto updateCheckFrequencySec = _settings->getEntryValue("global/app/update_check_frequency").toInt();
if (updateCheckFrequencySec < 0)
{
if (cb) cb();
return;
}

const auto lastUpdateCheckTime = _settings->getQSettings()->value("update/lastTimestamp", 0).toLongLong();
if (currTime - lastUpdateCheckTime < updateCheckFrequencySec)
{
if (cb) cb();
return;
}
}

_settings->getQSettings()->setValue("update/lastTimestamp", currTime);

const QUrl infoUrl = _settings->getQSettings()->value("update/url", "https://api.github.com/repos/cegui/ceed-cpp/releases/latest").toUrl();

_mainWindow->setStatusMessage("Checking for updates...");

QNetworkReply* infoReply = _network->get(QNetworkRequest(infoUrl));
QObject::connect(infoReply, &QNetworkReply::errorOccurred, [this, infoReply](QNetworkReply::NetworkError)
QObject::connect(infoReply, &QNetworkReply::errorOccurred, [this, cb, infoReply](QNetworkReply::NetworkError)
{
onUpdateError(infoReply->url(), infoReply->errorString());
if (cb) cb();
});

QObject::connect(infoReply, &QNetworkReply::finished, [this, infoReply, manual]()
QObject::connect(infoReply, &QNetworkReply::finished, [this, cb, infoReply, manual]()
{
// Already processed by QNetworkReply::errorOccurred handler
if (infoReply->error() != QNetworkReply::NoError) return;
if (infoReply->error() != QNetworkReply::NoError)
{
if (cb) cb();
return;
}

try
{
Expand All @@ -251,6 +287,7 @@ void Application::checkForUpdates(bool manual)
_mainWindow->setStatusMessage(msg);
if (manual)
QMessageBox::warning(_mainWindow, tr("Auto-update blocked"), msg);
if (cb) cb();
return;
}
else
Expand All @@ -276,10 +313,9 @@ void Application::checkForUpdates(bool manual)
catch (const std::exception& e)
{
onUpdateError(infoReply->url(), e.what());
return;
}

_mainWindow->setStatusMessage("");
if (cb) cb();
});
}

Expand All @@ -295,8 +331,6 @@ void Application::onUpdateError(const QUrl& url, const QString& errorString)

if (response == QMessageBox::Yes)
QDesktopServices::openUrl(QUrl("https://github.com/cegui/ceed-cpp/releases"));

_mainWindow->setStatusMessage("");
}

void Application::checkUpdateResults()
Expand Down Expand Up @@ -394,6 +428,12 @@ void Application::createSettingsEntries()
"combobox", false, 1, { {0, "Empty environment"}, {1, "Most recent project"} }));
secApp->addEntry(std::move(entry));

entry.reset(new SettingsEntry(*secApp, "update_check_frequency", 0, "Check for updates",
"How frequently an update should be checked",
"combobox", false, 1, { {7 * 86400, "Once a week"}, {86400, "Once a day"}, {0, "Every launch"}, {-1, "Never"} }));
secApp->addEntry(std::move(entry));


// By default we limit the undo stack to 500 undo commands, should be enough and should
// avoid memory drainage. Keep in mind that every tabbed editor has it's own undo stack,
// so the overall command limit is number_of_tabs * 500!
Expand Down
2 changes: 1 addition & 1 deletion src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Application : public QApplication
QString getDocumentationPath() const;
QString getUpdatePath() const;

void checkForUpdates(bool manual);
void checkForUpdates(bool manual, const std::function<void()>& cb = nullptr);

private:

Expand Down

0 comments on commit b8e1af4

Please sign in to comment.