From be1ba804c66b59e987f1e1bc3b094b128ac0e9f4 Mon Sep 17 00:00:00 2001 From: Peinthor Rene Date: Mon, 2 Dec 2013 16:52:48 +0100 Subject: [PATCH] add a version check feature this is only enabled on windows as there is no package manager handling the versions and we don't want users to stay on old versions forever --- CMakeLists.txt | 5 ++- src/MainWindow.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++ src/MainWindow.h | 5 +++ src/gen_version.h | 6 ++++ src/src.pro | 2 +- src/version.sh | 7 +++-- 6 files changed, 99 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e7e6016..da89094c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ set(QHEXEDIT_DIR libs/qhexedit) add_subdirectory(${ANTLR_DIR}) add_subdirectory(${QHEXEDIT_DIR}) -find_package(Qt4 COMPONENTS QtCore QtGui QtTest REQUIRED) +find_package(Qt4 COMPONENTS QtCore QtGui QtNetwork QtTest REQUIRED) include(${QT_USE_FILE}) add_definitions(${QT_DEFINITIONS}) @@ -98,6 +98,9 @@ set_source_files_properties(src/AboutDialog.cpp PROPERTIES OBJECT_DEPENDS ${CMAK #icon and correct libs/subsystem for windows if(WIN32) + #enable version check for windows + add_definitions(-DCHECKNEWVERSION) + IF( MINGW ) # resource compilation for MinGW ADD_CUSTOM_COMMAND( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/sqlbicon.o diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 9df9751b..52fe2d75 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -15,6 +15,7 @@ #include "SqlExecutionArea.h" #include "VacuumDialog.h" #include "DbStructureModel.h" +#include "gen_version.h" #include #include @@ -29,6 +30,11 @@ #include #include #include +#include +#include +#include +#include + MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), @@ -122,6 +128,15 @@ void MainWindow::init() // Load all settings reloadSettings(); + +#ifdef CHECKNEWVERSION + // Check for a new release version, usually only enabled on windows + m_NetworkManager = new QNetworkAccessManager(this); + QObject::connect(m_NetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpresponse(QNetworkReply*))); + + QUrl url("https://raw.github.com/rp-/sqlitebrowser/master/currentrelease"); + m_NetworkManager->get(QNetworkRequest(url)); +#endif } bool MainWindow::fileOpen(const QString& fileName) @@ -1201,3 +1216,66 @@ void MainWindow::reloadSettings() populateStructure(); resetBrowser(); } + +void MainWindow::httpresponse(QNetworkReply *reply) +{ + //QVariant statuscode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); + + if(reply->error() == QNetworkReply::NoError) + { + // first line of the currentrelease file contains a major.minor.patch version string + QString sversion(reply->readLine()); + + QStringList versiontokens = sversion.split("."); + int major = versiontokens[0].toInt(); + int minor = versiontokens[1].toInt(); + int patch = versiontokens[2].toInt(); + + bool newversion = false; + if(major > MAJOR_VERSION) + newversion = true; + else if(major == MAJOR_VERSION) + { + if(minor > MINOR_VERSION) + newversion = true; + else if(minor == MINOR_VERSION) + { + if(patch > PATCH_VERSION) + newversion = true; + } + } + + if(newversion) + { + QSettings settings(QApplication::organizationName(), QApplication::organizationName()); + bool disablecheck = settings.value("checkversion/disable", false).toBool(); + int ignmajor = settings.value("checkversion/major", 999).toInt(); + int ignminor = settings.value("checkversion/minor", 0).toInt(); + int ignpatch = settings.value("checkversion/patch", 0).toInt(); + + // check if the user doesn't care about the current update + if(!(ignmajor == major && ignminor == minor && ignpatch == patch && disablecheck)) + { + QMessageBox msgBox; + QPushButton *idontcarebutton = msgBox.addButton(tr("Don't show again"), QMessageBox::ActionRole); + msgBox.addButton(QMessageBox::Ok); + msgBox.setTextFormat(Qt::RichText); + msgBox.setWindowTitle(tr("New version available.")); + msgBox.setText(tr("A new sqlitebrowser version is available (%1.%2.%3).

" + "Please download at https://github.com/rp-/sqlitebrowser/releases.").arg(major).arg(minor).arg(patch)); + msgBox.exec(); + + if(msgBox.clickedButton() == idontcarebutton) + { + // save that the user don't want to get bothered about this update + settings.beginGroup("checkversion"); + settings.setValue("major", major); + settings.setValue("minor", minor); + settings.setValue("patch", minor); + settings.setValue("disable", true); + settings.endGroup(); + } + } + } + } +} diff --git a/src/MainWindow.h b/src/MainWindow.h index 4763e604..a1ac03e1 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -16,6 +16,8 @@ class QLabel; class QModelIndex; class SqliteTableModel; class DbStructureModel; +class QNetworkReply; +class QNetworkAccessManager; namespace Ui { class MainWindow; @@ -81,6 +83,8 @@ private: QStandardItemModel completerModelTables; SqlTextEdit::FieldCompleterModelMap completerModelsFields; + QNetworkAccessManager* m_NetworkManager; + void init(); void updateRecentFileActions(); @@ -148,6 +152,7 @@ private slots: virtual void saveSqlFile(); virtual void loadExtension(); virtual void reloadSettings(); + virtual void httpresponse(QNetworkReply* reply); }; #endif diff --git a/src/gen_version.h b/src/gen_version.h index bb178d74..f7bd5d52 100644 --- a/src/gen_version.h +++ b/src/gen_version.h @@ -1 +1,7 @@ +#ifndef __GEN_VERSIONS_H__ +#define __GEN_VERISONS_H__ #define APP_VERSION "master_git" +#define MAJOR_VERSION 999 +#define MINOR_VERSION 0 +#define PATCH_VERSION 0 +#endif diff --git a/src/src.pro b/src/src.pro index dcd4fbeb..a63e9b80 100644 --- a/src/src.pro +++ b/src/src.pro @@ -1,6 +1,6 @@ TEMPLATE = app -QT += core gui +QT += core gui network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = sqlitebrowser diff --git a/src/version.sh b/src/version.sh index e9931cc4..ee3d6b96 100755 --- a/src/version.sh +++ b/src/version.sh @@ -2,12 +2,15 @@ cd $1 version=`git log -n1 --format="\"%h_git\""` -version_macro="#define APP_VERSION $version" +inc_guard_head="#ifndef __GEN_VERSIONS_H__\n#define __GEN_VERISONS_H__\n" +inc_guard_foot="#endif" +version_numbers="#define MAJOR_VERSION 999\n#define MINOR_VERSION 0\n#define PATCH_VERSION 0\n" +version_macro="#define APP_VERSION $version\n" if [ -f gen_version.h ]; then current_file=`cat gen_version.h` fi if [ "$version_macro" != "$current_file" ]; then - echo "$version_macro" > gen_version.h + echo -e "$inc_guard_head$version_macro$version_numbers$inc_guard_foot" > gen_version.h fi