added return codes for IPrefresher refresh method.

error messages in gui.
This commit is contained in:
lukas 2020-05-14 23:08:19 +02:00
parent 0eea7095e0
commit 3b32f60190
5 changed files with 34 additions and 10 deletions

View File

@ -20,7 +20,7 @@ option(BUILD_DOC "Build documentation" ON) # additional dependency for Doxygen
option(PACKAGING "Allow Packaging to <exe>, <deb> or <rpm>" ON) # additional dependencies for RPMbuild,dpkg or NSIS
option(TESTS "Build Tests" ON) # additional dependencies for GTEST - to build tests
option(GUI "Build GUI elements" ON) # additional dependencies to QT libraries needed
set(WinBuild true)
set(WinBuild false)
# helper variables
SET(CMAKE_CXX_STANDARD 17)

View File

@ -9,12 +9,19 @@
#pragma once
namespace IPRefresher_Status_Code {
const int SUCCESS = 1;
const int ERROR = -1;
const int ERROR_NO_INTERNET = -2;
const int NOREFRESH = 0;
}
class IPRefresher {
public:
/**
* refresh ip address on Dynu server
*/
void checkIPAdress(bool force);
bool checkIPAdress(bool force);
/**
* default constructor
@ -27,4 +34,4 @@ public:
* @param loop true->loopmode on
*/
explicit IPRefresher(bool loop);
};
};

View File

@ -11,23 +11,26 @@
#include <thread>
#include <Logger.h>
void IPRefresher::checkIPAdress(bool force) {
bool IPRefresher::checkIPAdress(bool force) {
FileLogger logger;
testspace::testi5();
IPAPI ipapi;
std::string ip = ipapi.getGlobalIp();
if (ip.empty()) {
//no internet connection (or other error)
Logger::warning("no internet connection");
return IPRefresher_Status_Code::ERROR_NO_INTERNET;
} else if (!IpHelper::isIpValid(ip)) {
// error when ip doesn't contain a :
Logger::warning("an error occured when getting the global ip");
return IPRefresher_Status_Code::ERROR;
} else {
std::string oldip = logger.readip();
if (oldip == ip && !force) {
Logger::message("no change -- ip: " + ip);
return IPRefresher_Status_Code::NOREFRESH;
} else {
Logger::message("ip changed! -- from :" + oldip + "to: " + ip);
@ -43,9 +46,11 @@ void IPRefresher::checkIPAdress(bool force) {
} else if (!result) {
//error
Logger::error("failed to write ip to dynu api!");
return IPRefresher_Status_Code::ERROR;
}
logger.safeip(ip);
return result ? IPRefresher_Status_Code::SUCCESS : IPRefresher_Status_Code::ERROR;
}
}
}

View File

@ -58,7 +58,23 @@ void MainWindow::refreshIPBtn() {
new std::thread([this]() {
IPRefresher ipr;
if (Config::readConfig()) {
ipr.checkIPAdress(false);
int code = ipr.checkIPAdress(false);
switch (code) {
case IPRefresher_Status_Code::SUCCESS:
appendLogField("successfully refreshed IP!");
break;
case IPRefresher_Status_Code::NOREFRESH:
appendLogField("IP is already correct.");
break;
case IPRefresher_Status_Code::ERROR_NO_INTERNET:
appendLogField("Error: No Internet connection");
break;
case IPRefresher_Status_Code::ERROR:
appendLogField("An error occured while refreshing.");
break;
default:
appendLogField("An unknown error code occured");
}
} else {
std::cout << "incorrect credentials!" << std::endl;
}

View File

@ -3,13 +3,9 @@
//
#include <QApplication>
#include <iostream>
#include "gui/MainWindow.h"
int main(int argc, char *argv[]) {
std::cout << "gui here!" << std::endl;
QCoreApplication::addLibraryPath(".");
QApplication a(argc, argv);
MainWindow w;
w.show();