From b5b352f83dd1b25e0254715c0e676e8801d1f918 Mon Sep 17 00:00:00 2001 From: lukas Date: Fri, 15 May 2020 13:57:09 +0200 Subject: [PATCH] namespace for IPRefresher instead of class --- inc/IPRefresher.h | 28 ++++++++++++---------------- src/IPRefresher.cpp | 37 +++++++++++++++++++------------------ src/gui/MainWindow.cpp | 25 ++++++++++++------------- src/gui/MainWindow.h | 30 ++++++++++++++++++++++++------ src/main.cpp | 8 +++----- src/maingui.cpp | 8 ++++---- 6 files changed, 74 insertions(+), 62 deletions(-) diff --git a/inc/IPRefresher.h b/inc/IPRefresher.h index 70d8e0e..91819b1 100644 --- a/inc/IPRefresher.h +++ b/inc/IPRefresher.h @@ -9,29 +9,25 @@ #pragma once -namespace IPRefresher_Status_Code { - const int SUCCESS = 1; - const int ERROR = -1; - const int ERROR_NO_INTERNET = -2; - const int NOREFRESH = 0; -} +namespace IPRefresher { + /** + * Status return-codes for startUpService + */ + namespace 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 */ bool checkIPAdress(bool force); - /** - * default constructor - */ - IPRefresher() = default; - /** * start the service in loop mode * every 5 minutes the ip is checked an refreshed (needed for .service) - * @param loop true->loopmode on */ - explicit IPRefresher(bool loop); -}; \ No newline at end of file + void startUpService(int interval = 300); +} \ No newline at end of file diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp index 7238a02..17e7ba5 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -11,26 +11,28 @@ #include #include +using namespace IPRefresher; + 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; + return 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; + return Status_Code::ERROR; } else { std::string oldip = logger.readip(); if (oldip == ip && !force) { Logger::message("no change -- ip: " + ip); - return IPRefresher_Status_Code::NOREFRESH; + return Status_Code::NOREFRESH; } else { Logger::message("ip changed! -- from :" + oldip + "to: " + ip); @@ -46,27 +48,26 @@ bool IPRefresher::checkIPAdress(bool force) { } else if (!result) { //error Logger::error("failed to write ip to dynu api!"); - return IPRefresher_Status_Code::ERROR; + return Status_Code::ERROR; } logger.safeip(ip); - return result ? IPRefresher_Status_Code::SUCCESS : IPRefresher_Status_Code::ERROR; + return result ? Status_Code::SUCCESS : Status_Code::ERROR; } } } -IPRefresher::IPRefresher(bool loop) { - if (loop) { - Logger::message("startup of service"); - Logger::message("Version: " + Version::VERSION); - if (Config::readConfig()) { - while (true) { - Logger::message("starting check"); - checkIPAdress(false); - std::this_thread::sleep_for(std::chrono::milliseconds(300000)); - } - } else { - std::cout << "incorrect credentials!" << std::endl; +void IPRefresher::startUpService(int interval) { + Logger::message("startup of service"); + Logger::message("Version: " + Version::VERSION); + if (Config::readConfig()) { + while (true) { + Logger::message("starting check"); + checkIPAdress(false); + std::this_thread::sleep_for(std::chrono::milliseconds(interval * 1000)); } + } else { + std::cout << "incorrect credentials!" << std::endl; } + } diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 3471287..3bc8758 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -8,10 +8,10 @@ #include -MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { +MainWindow::MainWindow() : QMainWindow(), ui(new Ui::MainWindow) { ui->setupUi(this); - // needs to be defined out of scope -- would be termintated after this constructor. + // needs to be defined with new -- would be termintated after this constructor. new std::thread([this]() { IPAPI ipapi; std::string ip = ipapi.getGlobalIp(); @@ -20,11 +20,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi this->ui->labelCurrentIP->setText(msg.c_str()); }); - - if (Config::validateConfig()) - ui->labelConfig->setText("Config is: OK"); - else - ui->labelConfig->setText("Config is: NOT OK"); + // set config info label and initial check if config is valid + ui->labelConfig->setText(Config::validateConfig() ? "Config is: OK" : "Config is: NOT OK"); connect(ui->buttonCheckConfig, SIGNAL(clicked()), this, SLOT(checkConfigBtn())); connect(ui->buttonRefreshIP, SIGNAL(clicked()), this, SLOT(refreshIPBtn())); @@ -33,6 +30,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi } MainWindow::~MainWindow() { + // todo check if disconnects are really necessary + disconnect(ui->buttonCheckConfig); + disconnect(ui->buttonRefreshIP); delete ui; } @@ -56,20 +56,19 @@ void MainWindow::refreshIPBtn() { appendLogField(""); appendLogField("start refreshing Dynu IP."); new std::thread([this]() { - IPRefresher ipr; if (Config::readConfig()) { - int code = ipr.checkIPAdress(false); + int code = IPRefresher::checkIPAdress(false); switch (code) { - case IPRefresher_Status_Code::SUCCESS: + case IPRefresher::Status_Code::SUCCESS: appendLogField("successfully refreshed IP!"); break; - case IPRefresher_Status_Code::NOREFRESH: + case IPRefresher::Status_Code::NOREFRESH: appendLogField("IP is already correct."); break; - case IPRefresher_Status_Code::ERROR_NO_INTERNET: + case IPRefresher::Status_Code::ERROR_NO_INTERNET: appendLogField("Error: No Internet connection"); break; - case IPRefresher_Status_Code::ERROR: + case IPRefresher::Status_Code::ERROR: appendLogField("An error occured while refreshing."); break; default: diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 288a2fc..cb0ae06 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -1,6 +1,9 @@ -// -// Created by lukas on 09.05.20. -// +/** + * Main GUI controller - User IO handlings + * + * @author Lukas Heiligenbrunner + * @date 09.05.2020 + */ #pragma once @@ -14,19 +17,34 @@ class MainWindow : public QMainWindow { Q_OBJECT public: - explicit MainWindow(QWidget *parent = nullptr); + /** + * constructor with basic initializations + */ + explicit MainWindow(); + /** + * destruct all gui elements + */ ~MainWindow(); private: Ui::MainWindow *ui; private slots: - + /** + * executed click handler for config button + */ void checkConfigBtn(); + /** + * executed click handler for refresh btn + */ void refreshIPBtn(); signals: - + /** + * append a String line to the Log field + * + * @param QString string to be appended + */ void appendLogField(QString); }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6518f92..e16b7d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,15 +21,14 @@ int main(int argc, char *argv[]) { } else if (firstarg == "-v" || firstarg == "--version") { std::cout << "Version " << Version::VERSION << std::endl; } else if (firstarg == "-f" || firstarg == "--force") { - IPRefresher ipr; if (Config::readConfig()) { - ipr.checkIPAdress(true); + IPRefresher::checkIPAdress(true); } else { std::cout << "incorrect credentials!" << std::endl; } } else if (firstarg == "-l" || firstarg == "--loop") { - IPRefresher(true); + IPRefresher::startUpService(true); } else if (firstarg == "-c" || firstarg == "--checkconfig") { if (Config::validateConfig()) { Logger::message("Config file is OK"); @@ -44,10 +43,9 @@ int main(int argc, char *argv[]) { Logger::message("wrong arguments! -h for help"); } } else { - IPRefresher ipr; Logger::message("starting check"); if (Config::readConfig()) { - ipr.checkIPAdress(false); + IPRefresher::checkIPAdress(false); } else { std::cout << "incorrect credentials!" << std::endl; } diff --git a/src/maingui.cpp b/src/maingui.cpp index c144b60..a57ba7e 100644 --- a/src/maingui.cpp +++ b/src/maingui.cpp @@ -1,13 +1,13 @@ -// -// Created by lukas on 09.05.20. -// - #include #include "gui/MainWindow.h" +/** + * application entry point + */ int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; + w.setWindowTitle("startUpService"); w.show(); return QApplication::exec();