Skip to content

Commit

Permalink
Support loadConfig & saveConfig API functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
horacekj committed Jan 8, 2020
1 parent 607b575 commit 7cddccd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
2 changes: 0 additions & 2 deletions config-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ void LibMain::cb_connections_changed(int) {
s["XN"]["port"] = form.ui.cb_serial_port->currentText();
s["XN"]["baudrate"] = form.ui.cb_serial_speed->currentText().toInt();
s["XN"]["flowcontrol"] = form.ui.cb_serial_flowcontrol->currentIndex();

s.save(_CONFIG_FILENAME);
}

void LibMain::fillConnectionsCbs() {
Expand Down
21 changes: 21 additions & 0 deletions lib-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ unsigned int features() {
return 0; // no features yet
}

///////////////////////////////////////////////////////////////////////////////
// Config

int loadConfig(char16_t *filename) {
if (lib.xn.connected())
return TRK_FILE_DEVICE_OPENED;
try {
lib.config_filename = QString::fromUtf16(filename);
lib.s.load(lib.config_filename);
lib.fillConnectionsCbs();
} catch (...) { return TRK_FILE_CANNOT_ACCESS; }
return 0;
}

int saveConfig(char16_t *filename) {
try {
lib.s.save(QString::fromUtf16(filename));
} catch (...) { return TRK_FILE_CANNOT_ACCESS; }
return 0;
}

///////////////////////////////////////////////////////////////////////////////
// Connect / disconnect

Expand Down
3 changes: 3 additions & 0 deletions lib-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ XN_SHARED_EXPORT bool CALL_CONV apiSupportsVersion(unsigned int version);
XN_SHARED_EXPORT int CALL_CONV apiSetVersion(unsigned int version);
XN_SHARED_EXPORT unsigned int CALL_CONV features();

XN_SHARED_EXPORT int CALL_CONV loadConfig(char16_t *filename);
XN_SHARED_EXPORT int CALL_CONV saveConfig(char16_t *filename);

XN_SHARED_EXPORT int CALL_CONV connect();
XN_SHARED_EXPORT int CALL_CONV disconnect();
XN_SHARED_EXPORT bool CALL_CONV connected();
Expand Down
2 changes: 2 additions & 0 deletions lib-errors.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef LIB_ERRORS_H
#define LIB_ERRORS_H

constexpr int TRK_FILE_CANNOT_ACCESS = 1010;
constexpr int TRK_FILE_DEVICE_OPENED = 1011;
constexpr int TRK_ALREADY_OPENNED = 2001;
constexpr int TRK_CANNOT_OPEN_PORT = 2002;
constexpr int TRK_NOT_OPENED = 2011;
Expand Down
5 changes: 4 additions & 1 deletion lib-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ LibMain::LibMain() {
QObject::connect(&xn, SIGNAL(onTrkStatusChanged(Xn::TrkStatus)), this,
SLOT(xnOnTrkStatusChanged(Xn::TrkStatus)));

s.load(_CONFIG_FILENAME);
this->config_filename = _DEFAULT_CONFIG_FILENAME;
s.load(this->config_filename);
this->guiInit();
log("Library loaded.", LogLevel::Info);
}
Expand All @@ -28,6 +29,8 @@ LibMain::~LibMain() {
try {
if (xn.connected())
xn.disconnect();
if (this->config_filename != "")
this->s.save(this->config_filename);
} catch (...) {
// No exceptions in destructor!
}
Expand Down
3 changes: 2 additions & 1 deletion lib-main.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Xn {

///////////////////////////////////////////////////////////////////////////////

const QString _CONFIG_FILENAME = "trakce-xn.ini";
const QString _DEFAULT_CONFIG_FILENAME = "trakce-xn.ini";

class ConfigWindow : public QMainWindow {
Q_OBJECT
Expand All @@ -30,6 +30,7 @@ class LibMain : public QObject {
ConfigWindow form;
XnEvents events;
Settings s;
QString config_filename = "";
unsigned int api_version = 0x0001;
bool gui_config_changing = false;
bool opening = false;
Expand Down

0 comments on commit 7cddccd

Please sign in to comment.