From 0063b5f54fddf8d1acf51ffd037e81959944a18f Mon Sep 17 00:00:00 2001 From: RuiZhang Date: Fri, 5 Jul 2024 12:21:40 -0600 Subject: [PATCH] version checking --- src/gui/mainWindow.cpp | 27 +++++----------------- src/ninja/ninja_init.cpp | 48 ++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 46 deletions(-) diff --git a/src/gui/mainWindow.cpp b/src/gui/mainWindow.cpp index e89042a2..76785ec5 100644 --- a/src/gui/mainWindow.cpp +++ b/src/gui/mainWindow.cpp @@ -86,7 +86,7 @@ mainWindow::mainWindow(QWidget *parent) QString v(NINJA_VERSION_STRING); v = "Welcome to WindNinja " + v; - + checkMessages(); writeToConsole(v, Qt::blue); @@ -115,28 +115,11 @@ mainWindow::mainWindow(QWidget *parent) */ void mainWindow::checkMessages(void) { QMessageBox mbox; - char **papszMsg = NinjaCheckVersion(); - if (papszMsg != NULL) { - const char *pszVers = - CSLFetchNameValueDef(papszMsg, "VERSION", NINJA_VERSION_STRING); - if (strcmp(pszVers, NINJA_VERSION_STRING) > 0) { - mbox.setText("A new version of WindNinja is available: " + - QString(pszVers)); - mbox.exec(); - } - char **papszUserMsg = CSLFetchNameValueMultiple(papszMsg, "MESSAGE"); - for (int i = 0; i < CSLCount(papszUserMsg); i++) { - mbox.setText(QString(papszUserMsg[i])); - mbox.exec(); - } - CSLDestroy(papszUserMsg); - if (CSLFetchNameValue(papszMsg, "ABORT") != NULL) { - mbox.setText("There is a fatal flaw in Windninja, it must close."); + QString versionstr = QString::fromStdString(NinjaCheckVersion()); + if (!versionstr.isEmpty()) { + mbox.setText("A new version of WindNinja is available: " + versionstr); mbox.exec(); - abort(); - } - } - CSLDestroy(papszMsg); +} } bool mainWindow::okToContinue() diff --git a/src/ninja/ninja_init.cpp b/src/ninja/ninja_init.cpp index 1128f188..aac6f0df 100644 --- a/src/ninja/ninja_init.cpp +++ b/src/ninja/ninja_init.cpp @@ -34,12 +34,16 @@ #include #include #include +#include #include "cpl_http.h" +#include "ninja_version.h" + boost::local_time::tz_database globalTimeZoneDB; /* + ** Query the windninja.org server and ask for the most up to date version. The ** return value is a set of key value pairs stored in a GDAL string list. ** There are three current values: @@ -57,30 +61,26 @@ boost::local_time::tz_database globalTimeZoneDB; ** ** The returned string list must be freed by the caller using CSLDestroy(). */ -char ** NinjaCheckVersion(void) { - CPLHTTPResult *poResult; - char **papszTokens = NULL; - char *pszResp = NULL; - CPLPushErrorHandler(CPLQuietErrorHandler); - poResult = CPLHTTPFetch("http://windninja.org/version/", NULL); - CPLPopErrorHandler(); - if (!poResult || poResult->nStatus != 0 || poResult->nDataLen == 0) { - return NULL; - } - pszResp = (char *)malloc(poResult->nDataLen + 1); - if (pszResp == 0) { - return NULL; - } - /* - ** Copy the message body into a null terminated string for - ** CSLTokenizeString() - */ - memcpy(pszResp, poResult->pabyData, poResult->nDataLen); - pszResp[poResult->nDataLen] = '\0'; - papszTokens = CSLTokenizeString2((const char *)pszResp, ";", 0); - free(pszResp); - CPLHTTPDestroyResult( poResult ); - return papszTokens; +std::string NinjaCheckVersion(void) { + const char* str = NINJA_VERSION_STRING; + std::ifstream file("../../../windninja/VERSION.txt"); + if (!file.is_open()) { + std::cerr << "Failed to open version file.\n"; + std::cerr << "Error: " << strerror(errno) << "\n"; // Print detailed error message + return ""; + } + + std::string line; + while (std::getline(file, line)) { + + if (std::string(str) < line) { + file.close(); + + return line; + } + } + + return ""; // Return an empty string if no match is found }