Skip to content

Commit

Permalink
added Updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Niakr1s committed Nov 23, 2019
1 parent 406d1f5 commit 3ef2238
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(rrt CXX)
project(rrt LANGUAGES CXX VERSION 0.1.0)

set(BUILD_SHARED_LIBS TRUE)

Expand All @@ -13,6 +13,9 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/a")


configure_file(src/version.h.in ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/version.h)

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
Expand Down
6 changes: 5 additions & 1 deletion src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(CMAKE_AUTOUIC ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5 COMPONENTS Widgets LinguistTools REQUIRED)
find_package(Qt5 COMPONENTS Widgets LinguistTools Network REQUIRED)

set(SRC
main.cpp
Expand All @@ -27,6 +27,8 @@ set(SRC
xmltreebuttons.cpp
xmltreesortfilterproxymodel.h
xmltreesortfilterproxymodel.cpp
updater.h
updater.cpp
typedefs.h
)

Expand All @@ -52,9 +54,11 @@ target_link_libraries(rrt
xml
Qt5::Widgets
Boost::log
Qt5::Network
)

target_include_directories(rrt PUBLIC
../
${Boost_INCLUDE_DIRS}
${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}
)
4 changes: 4 additions & 0 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
#include <QStatusBar>
#include <QVBoxLayout>
#include <boost/log/trivial.hpp>
#include "updater.h"
#include "xmltreeview.h"

MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
auto updater = new Updater();
updater->startUpdateQuery();

setWindowIcon(QIcon(":/icons/rrt.png"));
setWindowTitle("Rosreestr Tools");

Expand Down
73 changes: 73 additions & 0 deletions src/gui/updater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "updater.h"

#include <QDesktopServices>
#include <QMessageBox>
#include <QObject>
#include <QUrl>
#include <QVersionNumber>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include "version.h"

Updater::Updater(QObject* parent) : QObject(parent) {}

void Updater::startUpdateQuery() {
manager = new QNetworkAccessManager(this);
QNetworkRequest request;
QUrl url("https://api.github.com/repos/Niakr1s/rrt/releases/latest");
request.setUrl(url);
QNetworkReply* reply = manager->get(request);
connect(reply, &QNetworkReply::finished, this,
&Updater::onUpdateQueryFinished);
}

void Updater::onUpdateQueryFinished() {
QNetworkReply* reply = qobject_cast<QNetworkReply*>(QObject::sender());
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "MainWindow::onUpdateQueryFinished error: "
<< reply->errorString();
return;
}
reply->deleteLater();
auto json = QJsonDocument::fromJson(reply->readAll());
parseJson(json);
}

void Updater::parseJson(const QJsonDocument json) {
auto netVer = json["tag_name"].toString();
if (netVer.isNull()) {
qDebug() << "Updater::parseJson: tag_name is empty";
return;
}
while (netVer[0].isLetter()) {
netVer.remove(0, 1);
}
auto netVerVec = netVer.split(".");
QVersionNumber netVerNum(netVerVec[0].toInt(), netVerVec[1].toInt(),
netVerVec[2].toInt());
QVersionNumber localVerNum(std::stoi(PROJECT_VER_MAJOR),
std::stoi(PROJECT_VER_MINOR),
std::stoi(PROJECT_VER_PATCH));
qDebug() << "Updater::parseJson: got versions: local = " << localVerNum
<< ", net = " << netVerNum;
if (netVerNum > localVerNum) {
showUpdateNotification(json["html_url"].toString());
}
}

void Updater::showUpdateNotification(const QString& updateUrl) {
auto msg = new QMessageBox();
msg->setWindowTitle(tr("Got new update!"));
msg->setText(QString("<div>%1 <a href=%2>%3</a> %4</div>")
.arg(tr("Please, visit"))
.arg(updateUrl)
.arg("github")
.arg(tr("to update")));
msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);

if (auto btn = msg->exec(); btn == QMessageBox::Ok) {
QDesktopServices::openUrl(QUrl(updateUrl));
}
delete msg;
}
27 changes: 27 additions & 0 deletions src/gui/updater.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef UPDATER_H
#define UPDATER_H

#include <QJsonDocument>
#include <QNetworkAccessManager>
#include <QObject>

class Updater : public QObject {
Q_OBJECT

public:
Updater(QObject* parent = nullptr);
virtual ~Updater() {}

void startUpdateQuery();

public slots:
void onUpdateQueryFinished();

private:
QNetworkAccessManager* manager;

void parseJson(const QJsonDocument json);
void showUpdateNotification(const QString& updateUrl);
};

#endif // UPDATER_H
10 changes: 10 additions & 0 deletions src/version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef VERSION_H
#define VERSION_H

#define PROJECT_NAME "@PROJECT_NAME@"
#define PROJECT_VER "@PROJECT_VERSION@"
#define PROJECT_VER_MAJOR "@PROJECT_VERSION_MAJOR@"
#define PROJECT_VER_MINOR "@PROJECT_VERSION_MINOR@"
#define PROJECT_VER_PATCH "@PROJECT_VERSION_PATCH@"

#endif // VERSION_H

0 comments on commit 3ef2238

Please sign in to comment.