Skip to content

Commit

Permalink
version checking
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiZhang-kwf8 committed Jul 5, 2024
1 parent 8aa43b0 commit 0063b5f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 46 deletions.
27 changes: 5 additions & 22 deletions src/gui/mainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mainWindow::mainWindow(QWidget *parent)

QString v(NINJA_VERSION_STRING);
v = "Welcome to WindNinja " + v;

checkMessages();

writeToConsole(v, Qt::blue);

Expand Down Expand Up @@ -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()
Expand Down
48 changes: 24 additions & 24 deletions src/ninja/ninja_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@
#include <iostream>
#include <netinet/in.h>
#include <ctime>
#include <fstream>
#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:
Expand All @@ -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
}


Expand Down

0 comments on commit 0063b5f

Please sign in to comment.