Реализована работа с БД

This commit is contained in:
Alexander Zhirov 2022-07-13 20:49:49 +03:00
parent b9f02e4211
commit 3fcfc8783f
7 changed files with 244 additions and 27 deletions

View File

@ -11,9 +11,12 @@
#include "concat.h"
#include "xrandr.h"
#include "db.h"
void settingsLoad()
{
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);
@ -43,6 +46,8 @@ void settingsLoad()
addParameterValue(PARAMETER_USB, VALUES_USB_DRIVE, "/mnt/usbdevice", true);
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)
{

169
db.c Normal file
View File

@ -0,0 +1,169 @@
/*
* db.c
*
* Created on: 13 июл. 2022 г.
* Author: alexander
*/
#include <stdbool.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <stdio.h>
#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;
}

18
db.h Normal file
View File

@ -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_ */

BIN
freerdp.db Normal file

Binary file not shown.

1
gui.c
View File

@ -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");

View File

@ -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)

View File

@ -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();