diff --git a/arguments.c b/arguments.c index 938db53..f4903c5 100644 --- a/arguments.c +++ b/arguments.c @@ -11,38 +11,43 @@ #include "concat.h" #include "xrandr.h" +#include "db.h" void settingsLoad() { - addParameterKey(PARAMETER_XFREERDP, "xfreerdp", true, true, NULL, NULL); - addParameterKey(PARAMETER_SERVER, "/v:", true, true, NULL, NULL); - addParameterKey(PARAMETER_USERNAME, "/u:", true, true, NULL, NULL); - addParameterKey(PARAMETER_PASSWORD, "/p:", true, true, NULL, NULL); - addParameterKey(PARAMETER_CERTIGNORE, "/cert-ignore", true, true, NULL, NULL); - addParameterKey(PARAMETER_THEMES, "-themes", false, true, NULL, NULL); - addParameterKey(PARAMETER_WALLPAPER, "-wallpaper", false, true, NULL, NULL); - addParameterKey(PARAMETER_ENCRYPTION, "-encryption", false, true, NULL, NULL); - addParameterKey(PARAMETER_FONTS, "-fonts", false, true, NULL, NULL); - addParameterKey(PARAMETER_SOUND, "/sound", false, true, NULL, NULL); - addParameterKey(PARAMETER_COMPRESSION, "+compression", false, true, NULL, NULL); - addParameterKey(PARAMETER_FULLSCREEN, "/f", false, true, NULL, NULL); - addParameterKey(PARAMETER_MULTIMONITOR, "/multimon", false, true, getParameter(PARAMETER_FULLSCREEN), NULL); - addParameterKey(PARAMETER_AUTHENTICATION, "-authentication", true, true, NULL, NULL); - addParameterKey(PARAMETER_SECURITY, "/sec:", true, true, NULL, NULL); - addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_TLS, "tls", true); - addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_RDP, "rdp", false); - addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_NLA, "nla", false); - addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_EXT, "ext", false); - addParameterKey(PARAMETER_BITSPERPIXEL, "/bpp:", true, true, NULL, NULL); - addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_8, "8", true); - addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_16, "16", false); - addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_24, "24", false); - addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_32, "32", false); + if (!dbLoadData()) + { + addParameterKey(PARAMETER_XFREERDP, "xfreerdp", true, true, NULL, NULL); + addParameterKey(PARAMETER_SERVER, "/v:", true, true, NULL, NULL); + addParameterKey(PARAMETER_USERNAME, "/u:", true, true, NULL, NULL); + addParameterKey(PARAMETER_PASSWORD, "/p:", true, true, NULL, NULL); + addParameterKey(PARAMETER_CERTIGNORE, "/cert-ignore", true, true, NULL, NULL); + addParameterKey(PARAMETER_THEMES, "-themes", false, true, NULL, NULL); + addParameterKey(PARAMETER_WALLPAPER, "-wallpaper", false, true, NULL, NULL); + addParameterKey(PARAMETER_ENCRYPTION, "-encryption", false, true, NULL, NULL); + addParameterKey(PARAMETER_FONTS, "-fonts", false, true, NULL, NULL); + addParameterKey(PARAMETER_SOUND, "/sound", false, true, NULL, NULL); + addParameterKey(PARAMETER_COMPRESSION, "+compression", false, true, NULL, NULL); + addParameterKey(PARAMETER_FULLSCREEN, "/f", false, true, NULL, NULL); + addParameterKey(PARAMETER_MULTIMONITOR, "/multimon", false, true, getParameter(PARAMETER_FULLSCREEN), NULL); + addParameterKey(PARAMETER_AUTHENTICATION, "-authentication", true, true, NULL, NULL); + addParameterKey(PARAMETER_SECURITY, "/sec:", true, true, NULL, NULL); + addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_TLS, "tls", true); + addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_RDP, "rdp", false); + addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_NLA, "nla", false); + addParameterValue(PARAMETER_SECURITY, VALUE_SECURITY_EXT, "ext", false); + addParameterKey(PARAMETER_BITSPERPIXEL, "/bpp:", true, true, NULL, NULL); + addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_8, "8", true); + addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_16, "16", false); + addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_24, "24", false); + addParameterValue(PARAMETER_BITSPERPIXEL, VALUES_BITSPERPIXEL_32, "32", false); - addParameterKey(PARAMETER_USB, "/a:drive,USB,", true, true, NULL, NULL); - addParameterValue(PARAMETER_USB, VALUES_USB_DRIVE, "/mnt/usbdevice", true); + addParameterKey(PARAMETER_USB, "/a:drive,USB,", true, true, NULL, NULL); + addParameterValue(PARAMETER_USB, VALUES_USB_DRIVE, "/mnt/usbdevice", true); + + addParameterKey(PARAMETER_MONITORS, "/monitors:", false, true, getParameter(PARAMETER_FULLSCREEN), getParameter(PARAMETER_MULTIMONITOR)); + } - addParameterKey(PARAMETER_MONITORS, "/monitors:", false, true, getParameter(PARAMETER_FULLSCREEN), getParameter(PARAMETER_MULTIMONITOR)); x_info *monitors = getXInfo(); for (size_t i = 0; i < monitors->count; ++i) { diff --git a/db.c b/db.c new file mode 100644 index 0000000..ecf811b --- /dev/null +++ b/db.c @@ -0,0 +1,169 @@ +/* + * db.c + * + * Created on: 13 июл. 2022 г. + * Author: alexander + */ + +#include +#include +#include +#include + +#include "db.h" +#include "node_settings.h" + +static sqlite3 *dbGetBase(char *path) +{ + sqlite3 *db; + + if (sqlite3_open(path, &db) != SQLITE_OK) + { + fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + + return NULL; + } + + return db; +} + +/* + * 0 - parameter + * 1 - key + * 2 - set_key + * 3 - single + * 4 - value + * 5 - current (data) + * 6 - set_val + * 7 - dependence + * 8 - conflict + */ +static int dbLoad(void *NotUsed, int argc, char **argv, char **azColName) +{ + NotUsed = 0; + + char **parameters = (char **) malloc(sizeof(char*) * argc); + + for (int i = 0; i < argc; i++) + { + parameters[i] = argv[i]; + } + + if (!getParameter(atoi(parameters[0]))) + { + addParameterKey(atoi(parameters[0]), parameters[1], atoi(parameters[2]), atoi(parameters[3]), + parameters[7] ? getParameter(atoi(parameters[7])) : NULL, + parameters[8] ? getParameter(atoi(parameters[8])) : NULL); + } + + if (parameters[4]) + { + addParameterValue(atoi(parameters[0]), atoi(parameters[4]), parameters[5], atoi(parameters[6])); + } + + return 0; +} + +bool dbLoadData() +{ + sqlite3 *db = dbGetBase("freerdp.db"); + if (!db) + { + return false; + } + + char *err_msg = 0; + char *sql = + "SELECT `parameters`.`id` as `parameter`, `parameters`.`data` as `key`, `parameters`.`set` as `set_key`, `parameters`.`single` as `single`, `values`.`id` as `value`, `values`.`data` as `current`, `values`.`set` as `set_val`,`dependencies`.`dependence` as `dependence`, `conflicts`.`conflict` as `conflict` \ + FROM `parameters` as `parameters` \ + LEFT JOIN `arguments` as `arguments` ON `parameters`.`id` = `arguments`.`parameter` \ + LEFT JOIN `values` as `values` ON `arguments`.`value` = `values`.`id` \ + LEFT JOIN `dependencies` as `dependencies` ON `dependencies`.`parameter` = `parameters`.`id` \ + LEFT JOIN `conflicts` as `conflicts` ON `conflicts`.`parameter` = `parameters`.`id`"; + + if (sqlite3_exec(db, sql, dbLoad, 0, &err_msg) != SQLITE_OK) + { + fprintf(stderr, "SQL error: %s\n", err_msg); + sqlite3_free(err_msg); + sqlite3_close(db); + return false; + } + + sqlite3_close(db); + + return true; +} + +bool dbWriteParameter(Parameter name, bool set) +{ + sqlite3 *db = dbGetBase("freerdp.db"); + if (!db) + { + return false; + } + + sqlite3_stmt *res; + char *sql = "UPDATE `parameters` set `set` = ? where id = ?"; + + if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK) + { + sqlite3_bind_int(res, 1, set); + sqlite3_bind_int(res, 2, name); + } + + + if (sqlite3_step(res) == SQLITE_BUSY) + { + fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", set ? "включено" : "выключено", getParameter(name)->key); + } + if (sqlite3_step(res) == SQLITE_ERROR) + { + fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", set ? "включено" : "выключено", getParameter(name)->key); + } + if (sqlite3_step(res) == SQLITE_DONE) + { + fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", set ? "включено" : "выключено", getParameter(name)->key); + } + + sqlite3_finalize(res); + sqlite3_close(db); + + return true; +} + +bool dbWriteValue(Value name, bool set) +{ + sqlite3 *db = dbGetBase("freerdp.db"); + if (!db) + { + return false; + } + + sqlite3_stmt *res; + char *sql = "UPDATE `values` set `set` = ? where id = ?"; + + if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK) + { + sqlite3_bind_int(res, 1, set); + sqlite3_bind_int(res, 2, name); + } + + if (sqlite3_step(res) == SQLITE_BUSY) + { + fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", set ? "включено" : "выключено", getValue(name)->current); + } + if (sqlite3_step(res) == SQLITE_ERROR) + { + fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", set ? "включено" : "выключено", getValue(name)->current); + } + if (sqlite3_step(res) == SQLITE_DONE) + { + fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", set ? "включено" : "выключено", getValue(name)->current); + } + + sqlite3_finalize(res); + sqlite3_close(db); + + return true; +} diff --git a/db.h b/db.h new file mode 100644 index 0000000..b75d7dc --- /dev/null +++ b/db.h @@ -0,0 +1,18 @@ +/* + * db.h + * + * Created on: 13 июл. 2022 г. + * Author: alexander + */ + +#ifndef DB_H_ +#define DB_H_ + +#include "parameter.h" +#include "value.h" + +bool dbLoadData(); +bool dbWriteParameter(Parameter name, bool set); +bool dbWriteValue(Value name, bool set); + +#endif /* DB_H_ */ diff --git a/freerdp.db b/freerdp.db new file mode 100644 index 0000000..4967438 Binary files /dev/null and b/freerdp.db differ diff --git a/gui.c b/gui.c index e90ef26..814482d 100644 --- a/gui.c +++ b/gui.c @@ -101,6 +101,7 @@ Ihandle* guiStart() IupSetAttribute(btnClose, "NAME", "CLOSE"); btnSettings = IupButton("Настройки", NULL); IupSetAttribute(btnSettings, "NAME", "SETTINGS"); + hBoxButtons = IupHbox(IupFill(), btnConnect, btnClose, btnSettings, IupFill(), NULL); IupSetAttribute(hBoxButtons, "ALIGNMENT", "ACENTER:ACENTER"); IupSetAttribute(hBoxButtons, "GAP", "10"); diff --git a/node_settings.c b/node_settings.c index ba9b908..b88f194 100644 --- a/node_settings.c +++ b/node_settings.c @@ -11,6 +11,7 @@ #include "node_settings.h" #include "concat.h" +#include "db.h" NodeHead settings = { NULL, 0 }; @@ -119,6 +120,22 @@ static NodeParameter* getNodeParameter(Parameter name) return NULL; } +NodeValue* getValue(Value name) +{ + for (NodeParameter *pNode = settings.parameter; pNode; pNode = pNode->next) + { + for (NodeValue *vNode = pNode->value; vNode; vNode = vNode->next) + { + if (vNode->name == name) + { + return vNode; + } + } + } + + return NULL; +} + NodeParameter* getParameter(Parameter name) { for (NodeParameter *head = settings.parameter; head; head = head->next) @@ -367,9 +384,13 @@ static void saveChangeSingleValueSettings(NodeParameter *node) { nChange->change = false; nChange->set = true; + + dbWriteValue(nChange->name, nChange->set); + if (nSet) { nSet->set = false; + dbWriteValue(nSet->name, nSet->set); } ++node->countValueSet; } @@ -415,6 +436,8 @@ void saveChangeSettings() ++settings.countParameterSet; } + dbWriteParameter(pHead->name, pHead->set); + pHead->countValueSet = 0; if (pHead->singleValue) diff --git a/node_settings.h b/node_settings.h index c04935c..c044370 100644 --- a/node_settings.h +++ b/node_settings.h @@ -47,6 +47,7 @@ typedef struct NodeHead extern NodeHead settings; NodeParameter* getParameter(Parameter name); +NodeValue* getValue(Value name); void addParameterKey(Parameter name, char *key, bool set, bool singleValue, NodeParameter *dependence, NodeParameter *conflict); void addParameterValue(Parameter pName, Value vName, char *current, bool set); void freeSettings();