Skip to content

Commit

Permalink
Merge pull request #3 from oclero/fix-bugs
Browse files Browse the repository at this point in the history
Improve API
  • Loading branch information
oclero authored May 20, 2022
2 parents abe841e + 2ce17fb commit ce8b025
Show file tree
Hide file tree
Showing 21 changed files with 1,280 additions and 57 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v1.1.0

- Improve core API
- Fix some bugs
- Add controller and widget
- Add exemple for controller/widget usage.
- The lib nows depends on `QtWidgets`.

## v1.0.12

- Update submodule QtUtils to v1.0.6
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include(DeployQt)
# Set project information.
project("QtUpdater"
LANGUAGES CXX
VERSION 1.0.12.0
VERSION 1.1.0.0
)
set(PROJECT_NAMESPACE "oclero")

Expand All @@ -28,4 +28,5 @@ if(${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})

# Examples.
add_subdirectory(examples/basic)
add_subdirectory(examples/qtwidgets)
endif()
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Updater for Qt5 (auto-updates).
### Table of Contents

- [Requirements](#requirements)
- [Features](#features)
- [Usage](#usage)
- [Server Specifications](#server-specifications)
- [Example](#example)
Expand All @@ -31,6 +32,23 @@ Updater for Qt5 (auto-updates).
- [Qt 5.15+](https://www.qt.io/download-qt-installer)
- [cpphttplib](https://github.com/yhirose/cpp-httplib) (Only for unit tests)

## Features

This library contains:

- A core: `QtUpdater`
- A controller: `QtUpdateController`, that may be use with QtWidgets or QtQuick/QML.
- A widget: `QtUpdateWidget`, that may be used as a `QWidget` or inside a `QDialog`.

It has these features:

- Get latest update information.
- Get changelog.
- Get installer.
- Execute installer.
- Temporarly stores the update data in the `temp` folder.
- Verify checksum after downloading and before executing installer.

## Usage

1. Add the library's repository as a Git submodule.
Expand Down Expand Up @@ -133,15 +151,17 @@ curl http://localhost:8000/v1.1.0.exe
oclero::QtUpdater updater("https://server/endpoint");

// Subscribe to all necessary signals. See documentation for complete list.
QObject::connect(&updater, &oclero::QtUpdater::updateAvailableChanged,
QObject::connect(&updater, &oclero::QtUpdater::updateAvailabilityChanged,
&updater, [&updater]() {
if (updater.updateAvailable()) {
if (updater.updateAvailability() == oclero::QtUpdater::UpdateAvailable::Available) {
qDebug() << "Update available! You have: "
<< qPrintable(updater.currentVersion())
<< " - Latest is: "
<< qPrintable(updater.latestVersion());
} else {
} else if (updater.updateAvailability() == oclero::QtUpdater::UpdateAvailable::UpToDate) {
qDebug() << "You have the latest version.";
} else {
qDebug() << "Error.";
}
});

Expand Down
9 changes: 5 additions & 4 deletions examples/basic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
#include <oclero/QtUpdater.hpp>

int main(int argc, char* argv[]) {
QCoreApplication::setApplicationName("SingleInstanceExample");
QCoreApplication::setApplicationName("BasicUpdaterExample");
QCoreApplication::setApplicationVersion("1.0.0");
QCoreApplication::setOrganizationName("example");
QCoreApplication app(argc, argv);

oclero::QtUpdater updater;
updater.setServerUrl("http://localhost:8000/");

QObject::connect(&updater, &oclero::QtUpdater::updateAvailableChanged, &updater, [&updater]() {
if (updater.updateAvailable()) {
QObject::connect(&updater, &oclero::QtUpdater::updateAvailabilityChanged, &updater, [&updater]() {
if (updater.updateAvailability() == oclero::QtUpdater::UpdateAvailability::Available) {
qDebug() << "Update available! You have " << qPrintable(updater.currentVersion()) << " - Latest is "
<< qPrintable(updater.latestVersion());

Expand All @@ -39,7 +39,8 @@ int main(int argc, char* argv[]) {

qDebug() << "Starting installation...";
const auto dest = QString{ QStandardPaths::standardLocations(QStandardPaths::DownloadLocation).first() };
updater.installUpdate(oclero::QtUpdater::InstallMode::MoveFile, dest, /* quitAfter */ false, /* dry */ false);
updater.installUpdate(
oclero::QtUpdater::InstallMode::MoveFileToDir, dest, /* quitAfter */ false, /* dry */ false);
}
});

Expand Down
1 change: 1 addition & 0 deletions examples/dev_server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server.log
2 changes: 1 addition & 1 deletion examples/dev_server/public/v1.1.0.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "1.1.0",
"date": "01/01/2022",
"checksum": "F7DD0B5215ADBB605C146B934A7E17F5",
"checksum": "f7dd0b5215adbb605c146b934a7e17f5",
"checksumType": "md5"
}
2 changes: 1 addition & 1 deletion examples/dev_server/public/v1.1.0.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# v1.1.0
### v1.1.0

- Some new feature
- Some fix
19 changes: 19 additions & 0 deletions examples/qtwidgets/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
find_package(Qt5 REQUIRED Core Network Widgets)

add_executable(QtWidgetsUpdaterExample WIN32 MACOSX_BUNDLE)
target_sources(QtWidgetsUpdaterExample PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/resources.qrc
)
target_link_libraries(QtWidgetsUpdaterExample PRIVATE oclero::QtUpdater Qt5::Core Qt5::Network Qt5::Widgets)

set_target_properties(QtWidgetsUpdaterExample PROPERTIES
INTERNAL_CONSOLE OFF
EXCLUDE_FROM_ALL ON
FOLDER examples
AUTOMOC ON
AUTORCC ON
)

############# Minimal example ends here #############
target_deploy_qt(QtWidgetsUpdaterExample)
Binary file added examples/qtwidgets/icon.ico
Binary file not shown.
32 changes: 32 additions & 0 deletions examples/qtwidgets/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <QApplication>
#include <QDebug>
#include <QStandardPaths>
#include <QIcon>

#include <oclero/QtUpdater.hpp>
#include <oclero/QtUpdateController.hpp>

#include <oclero/QtUpdateWidget.hpp>

int main(int argc, char* argv[]) {
Q_INIT_RESOURCE(resources);

QCoreApplication::setApplicationName("QtWidgetsUpdaterExample");
QCoreApplication::setApplicationVersion("1.0.0");
QCoreApplication::setOrganizationName("example");
QApplication app(argc, argv);
QApplication::setWindowIcon(QIcon(":/example/icon.ico"));

// 1. Create updater backend.
oclero::QtUpdater updater;
updater.setServerUrl("http://localhost:8000/");

// 2. Create update dialog controller.
oclero::QtUpdateController updateCtrl(updater);

// 3. Create and show dialog.
auto* widget = new oclero::QtUpdateWidget(updateCtrl);
widget->show();

return app.exec();
}
5 changes: 5 additions & 0 deletions examples/qtwidgets/resources.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="example">
<file>icon.ico</file>
</qresource>
</RCC>
6 changes: 6 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ find_package(Qt5
REQUIRED
Core
Network
Widgets
)

set(HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/include/oclero/QtUpdater.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/oclero/QtDownloader.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/oclero/QtUpdateController.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/oclero/QtUpdateWidget.hpp
)

set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/oclero/QtUpdater.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/oclero/QtDownloader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/oclero/QtUpdateController.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/oclero/QtUpdateWidget.cpp
)

# Configure target.
Expand All @@ -37,6 +42,7 @@ target_link_libraries(${LIB_TARGET_NAME}
PRIVATE
Qt5::Core
Qt5::Network
Qt5::Widgets
PUBLIC
oclero::QtUtils
)
Expand Down
81 changes: 81 additions & 0 deletions lib/include/oclero/QtUpdateController.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

#include <QObject>
#include <QDateTime>

#include <oclero/QtUpdater.hpp>

namespace oclero {
/**
* @brief Controller for the Update dialog. Might be used with a QtWidgets-based or QtQuick-based dialog.
*/
class QtUpdateController : public QObject {
Q_OBJECT

Q_PROPERTY(State state READ state NOTIFY stateChanged)
Q_PROPERTY(QString currentVersion READ currentVersion NOTIFY currentVersionChanged)
Q_PROPERTY(QDateTime currentVersionDate READ currentVersionDate NOTIFY currentVersionDateChanged)
Q_PROPERTY(QString latestVersion READ latestVersion NOTIFY latestVersionChanged)
Q_PROPERTY(QDateTime latestVersionDate READ latestVersionDate NOTIFY latestVersionDateChanged)
Q_PROPERTY(int downloadProgress READ downloadProgress NOTIFY downloadProgressChanged)
Q_PROPERTY(QString latestVersionChangelog READ latestVersionChangelog NOTIFY latestVersionChangelogChanged)

public:
enum class State {
None,
Checking,
CheckingFail,
CheckingSuccess,
CheckingUpToDate,
Downloading,
DownloadingFail,
DownloadingSuccess,
Installing,
InstallingFail,
InstallingSuccess,
};
Q_ENUM(State)

public:
explicit QtUpdateController(oclero::QtUpdater& updater, QObject* parent = nullptr);
~QtUpdateController() = default;

State state() const;
QString currentVersion() const;
QDateTime currentVersionDate() const;
QString latestVersion() const;
QDateTime latestVersionDate() const;
QString latestVersionChangelog() const;
int downloadProgress() const;

private:
void setState(State state);
void setDownloadProgress(int);

public slots:
void cancel();
void checkForUpdates();
void downloadUpdate();
void installUpdate();

signals:
void stateChanged();
void currentVersionChanged();
void currentVersionDateChanged();
void latestVersionChanged();
void latestVersionDateChanged();
void latestVersionChangelogChanged();
void downloadProgressChanged(int);
void manualCheckingRequested();
void closeDialogRequested();
void checkForUpdateErrorChanged(QtUpdater::ErrorCode code);
void changelogDownloadErrorChanged(QtUpdater::ErrorCode code);
void updateDownloadErrorChanged(QtUpdater::ErrorCode code);
void updateInstallationErrorChanged(QtUpdater::ErrorCode code);

private:
oclero::QtUpdater& _updater;
State _state{ State::None };
int _downloadProgress{ 0 };
};
} // namespace oclero
24 changes: 24 additions & 0 deletions lib/include/oclero/QtUpdateWidget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <QDialog>
#include <QMap>

#include <oclero/QtUpdateController.hpp>

class QStackedWidget;

namespace oclero {
class QtUpdateWidget : public QWidget {
public:
QtUpdateWidget(QtUpdateController& controller, QWidget* parent = nullptr);
~QtUpdateWidget();

private:
void setupUi();

private:
QtUpdateController& _controller;
QStackedWidget* _stackedWidget{ nullptr };
QMap<QtUpdateController::State, QWidget*> _pages;
};
} // namespace oclero
Loading

0 comments on commit ce8b025

Please sign in to comment.