From 4344e7c1184dec5c3fc733f00d04b64584d9eecd Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 19 May 2020 16:23:00 +0200 Subject: [PATCH] added tabbed view new config page with ability to edit config file new features in config class - setting member variables new button locations in gui --- CMakeLists.txt | 3 +- inc/Config.h | 29 +++- {src => inc}/gui/MainWindow.h | 13 ++ src/Config.cpp | 17 +- src/gui/MainWindow.cpp | 69 ++++++-- src/gui/mainwindow.ui | 311 +++++++++++++++++++++++++++++----- src/maingui.cpp | 2 +- 7 files changed, 381 insertions(+), 63 deletions(-) rename {src => inc}/gui/MainWindow.h (80%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c3ed6a9..dcf4afc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -203,7 +203,8 @@ if (${GUI}) add_executable(iprefresher-gui src/maingui.cpp src/gui/MainWindow.cpp - src/gui/MainWindow.h ${UI_GENERATED_HEADERS}) + inc/gui/MainWindow.h + ${UI_GENERATED_HEADERS}) if (${WinBuild}) # hide console window when starting ui on windows diff --git a/inc/Config.h b/inc/Config.h index 1529671..db806e7 100644 --- a/inc/Config.h +++ b/inc/Config.h @@ -18,6 +18,13 @@ public: */ static bool readConfig(); + /** + * save back configuration to file + * + * @return success of config write + */ + static bool saveConfig(); + /** * validate config file * @@ -63,12 +70,32 @@ public: */ static const std::string &getChatId(); + /** + * set all parameters without telegram support + * + * @param domainname Dynu Domain name + * @param dynuapikey Dynu api key + * @param domainid Dynu domain id + */ + static void setValues(const std::string &domainname, const std::string &dynuapikey, const std::string &domainid); + + /** + * set all parameters with telegram support + * + * @param domainname Dynu Domain name + * @param dynuapikey Dynu api key + * @param domainid Dynu domain id + * @param telegramApiKey Telegram api key + * @param chatId Telegram chat id + */ + static void setValues(const std::string &domainname, const std::string &dynuapikey, const std::string &domainid, + const std::string &telegramApiKey, const std::string &chatId); private: /** * private constructor --> don't allow instance of this class */ - Config(); + Config() = default; /** * helper variable for managing telegram Support diff --git a/src/gui/MainWindow.h b/inc/gui/MainWindow.h similarity index 80% rename from src/gui/MainWindow.h rename to inc/gui/MainWindow.h index cb0ae06..88879e7 100644 --- a/src/gui/MainWindow.h +++ b/inc/gui/MainWindow.h @@ -29,7 +29,14 @@ public: private: Ui::MainWindow *ui; + + /** + * all static initializations of custom gui elements + */ + void initGui(); + private slots: + /** * executed click handler for config button */ @@ -40,7 +47,13 @@ private slots: */ void refreshIPBtn(); + /** + * executed click handler for save config btn + */ + void saveConfigBtn(); + signals: + /** * append a String line to the Log field * diff --git a/src/Config.cpp b/src/Config.cpp index 32aba2a..882f5fe 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -64,6 +64,11 @@ bool Config::readConfig() { return !(Config::dynuapikey.empty() || Config::domainid.empty() || Config::domainname.empty()); } +bool Config::saveConfig() { + // todo save config + return false; +} + bool Config::validateConfig() { libconfig::Config cfg; try { @@ -137,4 +142,14 @@ const std::string &Config::getChatId() { return chatId; } -Config::Config() = default; +void Config::setValues(const std::string &domainname, const std::string &dynuapikey, const std::string &domainid) { + Config::domainname = domainname; + Config::dynuapikey = dynuapikey; + Config::domainid = domainid; +} + +void Config::setValues(const std::string &domainname, const std::string &dynuapikey, const std::string &domainid, const std::string &telegramApiKey, const std::string &chatId) { + setValues(domainname, dynuapikey, domainid); + Config::telegramApiKey = telegramApiKey; + Config::chatId = chatId; +} diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 3bc8758..0a4c720 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -1,4 +1,4 @@ -#include "MainWindow.h" +#include "inc/gui/MainWindow.h" #include "ui_mainwindow.h" #include "api/IPAPI.h" @@ -11,20 +11,12 @@ MainWindow::MainWindow() : QMainWindow(), ui(new Ui::MainWindow) { ui->setupUi(this); - // needs to be defined with new -- would be termintated after this constructor. - new std::thread([this]() { - IPAPI ipapi; - std::string ip = ipapi.getGlobalIp(); - Logger::message("Current global IP: " + ip); - std::string msg = "Your current global IP: " + ip; - this->ui->labelCurrentIP->setText(msg.c_str()); - }); - - // set config info label and initial check if config is valid - ui->labelConfig->setText(Config::validateConfig() ? "Config is: OK" : "Config is: NOT OK"); + // initialize gui with start parameters + initGui(); connect(ui->buttonCheckConfig, SIGNAL(clicked()), this, SLOT(checkConfigBtn())); connect(ui->buttonRefreshIP, SIGNAL(clicked()), this, SLOT(refreshIPBtn())); + connect(ui->buttonSaveConfig, SIGNAL(clicked()), this, SLOT(saveConfigBtn())); connect(this, SIGNAL(appendLogField(QString)), ui->textLog, SLOT(appendPlainText(QString))); } @@ -33,6 +25,7 @@ MainWindow::~MainWindow() { // todo check if disconnects are really necessary disconnect(ui->buttonCheckConfig); disconnect(ui->buttonRefreshIP); + this->destroy(); delete ui; } @@ -81,4 +74,56 @@ void MainWindow::refreshIPBtn() { Logger::message("Finished refreshing Dynu IP."); this->appendLogField("Finished refreshing Dynu IP."); }); +} + +void MainWindow::saveConfigBtn() { + if (ui->telegramsupportCheckbox->isChecked()) { + Config::setValues( + ui->domainnameedit->text().toStdString(), + ui->dynuapikeyedit->text().toStdString(), + ui->domainidedit->text().toStdString(), + ui->telegramapikeyedit->text().toStdString(), + ui->chatidedit->text().toStdString()); + } else { + Config::setValues( + ui->domainnameedit->text().toStdString(), + ui->dynuapikeyedit->text().toStdString(), + ui->domainidedit->text().toStdString()); + } + Config::saveConfig(); +} + +void MainWindow::initGui() { + // needs to be defined with new -- would be termintated after the constructor call. + new std::thread([this]() { + IPAPI ipapi; + std::string ip = ipapi.getGlobalIp(); + Logger::message("Current global IP: " + ip); + std::string msg = "Your current global IP: " + ip; + this->ui->labelCurrentIP->setText(msg.c_str()); + }); + + // set config info label and initial check if config is valid + ui->labelConfig->setText(Config::validateConfig() ? "Config is: OK" : "Config is: NOT OK"); + + if (Config::readConfig()) { + ui->dynuapikeyedit->setText(Config::getDynuapikey().c_str()); + ui->domainidedit->setText(Config::getDomainid().c_str()); + ui->domainnameedit->setText(Config::getDomainname().c_str()); + + if (Config::isTelegramSupported()) { + ui->telegramsupportCheckbox->setCheckState(Qt::Checked); + ui->telegramapikeyedit->setText(Config::getTelegramApiKey().c_str()); + ui->chatidedit->setText(Config::getChatId().c_str()); + } else { + ui->telegramsupportCheckbox->setCheckState(Qt::Unchecked); + ui->telegramapikeyedit->setDisabled(true); + ui->chatidedit->setDisabled(true); + } + } else { + // todo duplicate code with above + ui->telegramsupportCheckbox->setCheckState(Qt::Unchecked); + ui->telegramapikeyedit->setDisabled(true); + ui->chatidedit->setDisabled(true); + } } \ No newline at end of file diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index bd9e356..d0fb62d 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -10,36 +10,16 @@ 618 + + ArrowCursor + MainWindow + + /*background-color: rgb(69, 196, 255); + - - - - 290 - 300 - 91 - 33 - - - - Refresh IP - - - - - - 40 - 60 - 301 - 17 - - - - Your current global IP: - - false @@ -48,7 +28,7 @@ 40 410 - 691 + 741 191 @@ -69,31 +49,268 @@ Log: - + - 270 - 140 - 91 - 33 + 40 + 20 + 741 + 351 - - Check Config - - - - - - 270 - 180 - 141 - 17 - - - - Config is: undefined + + 0 + + + Basic + + + + + 20 + 30 + 301 + 17 + + + + Your current global IP: + + + + + + 20 + 70 + 141 + 17 + + + + Config is: undefined + + + + + + 20 + 120 + 231 + 151 + + + + Actions + + + + + 30 + 50 + 91 + 33 + + + + ArrowCursor + + + Check Config + + + + + + 30 + 100 + 91 + 33 + + + + Refresh IP + + + + + + + Config + + + + + 30 + 120 + 181 + 21 + + + + Telegram Notifications + + + + + + 630 + 270 + 91 + 33 + + + + Save Config + + + + + + 30 + 20 + 101 + 17 + + + + Dynu API Key + + + + + + 460 + 20 + 111 + 17 + + + + Domain ID + + + + + + 30 + 170 + 131 + 17 + + + + Telegram API Key + + + + + + 30 + 240 + 64 + 17 + + + + Chat ID + + + + + + 30 + 40 + 211 + 31 + + + + + + + 460 + 40 + 113 + 31 + + + + + + + 30 + 260 + 113 + 31 + + + + + + + 30 + 190 + 311 + 31 + + + + + + + 280 + 40 + 161 + 31 + + + + domain.dynu.net + + + + + + 280 + 20 + 111 + 17 + + + + Domainname + + + + + + Settings + + + + + 20 + 30 + 161 + 17 + + + + Select your language: + + + + + + 20 + 60 + 94 + 31 + + + + diff --git a/src/maingui.cpp b/src/maingui.cpp index a57ba7e..21df7b6 100644 --- a/src/maingui.cpp +++ b/src/maingui.cpp @@ -7,7 +7,7 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; - w.setWindowTitle("startUpService"); + w.setWindowTitle("Dynu IP Refresher"); w.show(); return QApplication::exec();