rename to blist
This commit is contained in:
parent
0509c37e38
commit
59f56c1a6e
22 changed files with 514 additions and 487 deletions
|
@ -1,5 +1,5 @@
|
|||
cmake_minimum_required(VERSION 3.31)
|
||||
project(klist C)
|
||||
project(blist C)
|
||||
|
||||
set(CMAKE_C_STANDARD 90)
|
||||
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
# klist
|
||||
# blist
|
||||
|
||||
todos for your browser and terminal
|
|
@ -1,2 +1,2 @@
|
|||
add_subdirectory(cli)
|
||||
add_subdirectory(libklist)
|
||||
add_subdirectory(libblist)
|
|
@ -1,4 +1,4 @@
|
|||
add_executable(klist main.c process.c config.c util.c)
|
||||
target_include_directories(klist PRIVATE include)
|
||||
target_link_libraries(klist libklist)
|
||||
target_compile_options(klist PRIVATE -Werror -Wall) #-Wextra)
|
||||
add_executable(blist main.c process.c config.c util.c)
|
||||
target_include_directories(blist PRIVATE include)
|
||||
target_link_libraries(blist libblist)
|
||||
target_compile_options(blist PRIVATE -Werror -Wall) #-Wextra)
|
|
@ -5,7 +5,7 @@
|
|||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
char *klist_config_key_to_string(KLIST_CONFIG_KEY key) {
|
||||
char *blist_config_key_to_string(BLIST_CONFIG_KEY key) {
|
||||
switch (key) {
|
||||
case CONFIG_DATABASE_NAME:
|
||||
return "database";
|
||||
|
@ -15,13 +15,13 @@ char *klist_config_key_to_string(KLIST_CONFIG_KEY key) {
|
|||
return "";
|
||||
}
|
||||
|
||||
KLIST_CONFIG_KEY klist_config_string_to_key(const char *key) {
|
||||
if (!strcmp(key, klist_config_key_to_string(CONFIG_DATABASE_NAME)))
|
||||
BLIST_CONFIG_KEY blist_config_string_to_key(const char *key) {
|
||||
if (!strcmp(key, blist_config_key_to_string(CONFIG_DATABASE_NAME)))
|
||||
return CONFIG_DATABASE_NAME;
|
||||
return _CONFIG_KEY_COUNT;
|
||||
}
|
||||
|
||||
char *klist_config_name(char *name) {
|
||||
char *blist_config_name(char *name) {
|
||||
char *config_home = getenv("XDG_CONFIG_HOME");
|
||||
bool config_home_a = false;
|
||||
if (!config_home) {
|
||||
|
@ -53,7 +53,7 @@ char *klist_config_name(char *name) {
|
|||
return config_file_name;
|
||||
}
|
||||
|
||||
void klist_config_parse(klist_config *ctx, FILE *config) {
|
||||
void blist_config_parse(blist_config *ctx, FILE *config) {
|
||||
fseek(config, 0, SEEK_SET);
|
||||
int c;
|
||||
char *key = NULL, *val = NULL;
|
||||
|
@ -68,7 +68,7 @@ void klist_config_parse(klist_config *ctx, FILE *config) {
|
|||
if (!val) {
|
||||
fprintf(stderr, "ignoring key '%s'", key);
|
||||
}
|
||||
if (!strcmp(key, klist_config_key_to_string(CONFIG_DATABASE_NAME))) {
|
||||
if (!strcmp(key, blist_config_key_to_string(CONFIG_DATABASE_NAME))) {
|
||||
*ctx[CONFIG_DATABASE_NAME] = malloc((strlen(val) + 1) * sizeof(char));
|
||||
strcpy(*ctx[CONFIG_DATABASE_NAME], val);
|
||||
} else {
|
||||
|
@ -127,7 +127,7 @@ void klist_config_parse(klist_config *ctx, FILE *config) {
|
|||
free(val);
|
||||
}
|
||||
|
||||
void klist_config_setup(char *name, FILE *config) {
|
||||
void blist_config_setup(char *name, FILE *config) {
|
||||
char *data_home = getenv("XDG_DATA_HOME");
|
||||
bool data_home_a = false;
|
||||
if (!data_home) {
|
||||
|
@ -151,16 +151,16 @@ void klist_config_setup(char *name, FILE *config) {
|
|||
free(data_home);
|
||||
|
||||
// fseek(config, 0, SEEK_END);
|
||||
fprintf(config, "# -- klist config file --\n"
|
||||
fprintf(config, "# -- blist config file --\n"
|
||||
"# please note that keys and values are case and space "
|
||||
"sensitive, only key=value is parsable\n"
|
||||
"# valid keys: ");
|
||||
int i = 0;
|
||||
for (; i < _CONFIG_KEY_COUNT; i++)
|
||||
fprintf(config, "%s,", klist_config_key_to_string(i));
|
||||
fprintf(config, "%s,", blist_config_key_to_string(i));
|
||||
fprintf(config, "\n\n");
|
||||
fprintf(config, "%s=%s/tasks.db\n",
|
||||
klist_config_key_to_string(CONFIG_DATABASE_NAME), db_file);
|
||||
blist_config_key_to_string(CONFIG_DATABASE_NAME), db_file);
|
||||
fclose(config);
|
||||
|
||||
if (db_file)
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
#pragma once
|
||||
#include <stdio.h>
|
||||
|
||||
enum KLIST_CONFIG_KEY { CONFIG_DATABASE_NAME, _CONFIG_KEY_COUNT };
|
||||
typedef enum KLIST_CONFIG_KEY KLIST_CONFIG_KEY;
|
||||
enum BLIST_CONFIG_KEY { CONFIG_DATABASE_NAME, _CONFIG_KEY_COUNT };
|
||||
typedef enum BLIST_CONFIG_KEY BLIST_CONFIG_KEY;
|
||||
|
||||
typedef char *klist_config[_CONFIG_KEY_COUNT];
|
||||
typedef char *blist_config[_CONFIG_KEY_COUNT];
|
||||
|
||||
char *klist_config_key_to_string(KLIST_CONFIG_KEY key);
|
||||
KLIST_CONFIG_KEY klist_config_string_to_key(const char *key);
|
||||
char *blist_config_key_to_string(BLIST_CONFIG_KEY key);
|
||||
BLIST_CONFIG_KEY blist_config_string_to_key(const char *key);
|
||||
|
||||
char *klist_config_name(char *);
|
||||
void klist_config_parse(klist_config *, FILE *);
|
||||
void klist_config_setup(char *, FILE *);
|
||||
char *blist_config_name(char *);
|
||||
void blist_config_parse(blist_config *, FILE *);
|
||||
void blist_config_setup(char *, FILE *);
|
|
@ -2,16 +2,16 @@
|
|||
|
||||
#include "util.h"
|
||||
|
||||
int klist_app_user_get(const klist_app *);
|
||||
int klist_app_user_create(const klist_app *);
|
||||
int klist_app_user_delete(const klist_app *);
|
||||
int blist_app_user_get(const blist_app *);
|
||||
int blist_app_user_create(const blist_app *);
|
||||
int blist_app_user_delete(const blist_app *);
|
||||
|
||||
int klist_app_list_add(const klist_app *);
|
||||
int klist_app_list_edit(const klist_app *);
|
||||
int klist_app_list_get(const klist_app *);
|
||||
int klist_app_list_delete(const klist_app *);
|
||||
int blist_app_list_add(const blist_app *);
|
||||
int blist_app_list_edit(const blist_app *);
|
||||
int blist_app_list_get(const blist_app *);
|
||||
int blist_app_list_delete(const blist_app *);
|
||||
|
||||
int klist_app_task_add(const klist_app *);
|
||||
int klist_app_task_edit(const klist_app *);
|
||||
int klist_app_task_get(const klist_app *);
|
||||
int klist_app_task_delete(const klist_app *);
|
||||
int blist_app_task_add(const blist_app *);
|
||||
int blist_app_task_edit(const blist_app *);
|
||||
int blist_app_task_get(const blist_app *);
|
||||
int blist_app_task_delete(const blist_app *);
|
|
@ -3,70 +3,71 @@
|
|||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "klist.h"
|
||||
#include "blist.h"
|
||||
|
||||
enum klist_command {
|
||||
enum blist_command {
|
||||
USER,
|
||||
LIST,
|
||||
TASK,
|
||||
};
|
||||
|
||||
struct klist_app {
|
||||
enum klist_command cmd;
|
||||
struct blist_app {
|
||||
enum blist_command cmd;
|
||||
void *cmd_ctx;
|
||||
int error;
|
||||
klist_logging_ctx *log_ctx;
|
||||
klist *handle;
|
||||
blist_logging_ctx *log_ctx;
|
||||
blist *handle;
|
||||
char *config;
|
||||
};
|
||||
typedef struct klist_app klist_app;
|
||||
typedef struct blist_app blist_app;
|
||||
|
||||
klist_app *klist_app_init(char *db);
|
||||
void klist_app_deinit(klist_app *app);
|
||||
blist_app *blist_app_init(char *db);
|
||||
void blist_app_deinit(blist_app *app);
|
||||
|
||||
enum klist_user_command { USER_GET, USER_CREATE, USER_DELETE };
|
||||
struct klist_user_context {
|
||||
enum klist_user_command cmd;
|
||||
enum blist_user_command { USER_GET, USER_CREATE, USER_DELETE };
|
||||
struct blist_user_context {
|
||||
enum blist_user_command cmd;
|
||||
};
|
||||
typedef struct klist_user_context klist_user_context;
|
||||
typedef struct blist_user_context blist_user_context;
|
||||
|
||||
klist_user_context *klist_user_context_init(void);
|
||||
void klist_user_context_deinit(klist_user_context *ctx);
|
||||
blist_user_context *blist_user_context_init(void);
|
||||
void blist_user_context_deinit(blist_user_context *ctx);
|
||||
|
||||
enum klist_list_command {
|
||||
enum blist_list_command {
|
||||
LIST_ADD,
|
||||
LIST_EDIT,
|
||||
LIST_GET,
|
||||
LIST_DELETE,
|
||||
};
|
||||
struct klist_list_context {
|
||||
enum klist_list_command cmd;
|
||||
struct blist_list_context {
|
||||
enum blist_list_command cmd;
|
||||
char *name;
|
||||
char *desc;
|
||||
char **stages;
|
||||
size_t stages_len;
|
||||
char *preset;
|
||||
};
|
||||
typedef struct klist_list_context klist_list_context;
|
||||
typedef struct blist_list_context blist_list_context;
|
||||
|
||||
klist_list_context *klist_list_context_init(void);
|
||||
klist_list_context *klist_list_context_get_by_id(klist_list_context *ctx,
|
||||
blist_list_context *blist_list_context_init(void);
|
||||
blist_list_context *blist_list_context_get_by_id(blist_list_context *ctx,
|
||||
u_int id);
|
||||
void klist_list_context_deinit(klist_list_context *ctx);
|
||||
void blist_list_context_deinit(blist_list_context *ctx);
|
||||
|
||||
enum klist_task_command {
|
||||
enum blist_task_command {
|
||||
TASK_ADD,
|
||||
TASK_EDIT,
|
||||
TASK_GET,
|
||||
TASK_DELETE,
|
||||
};
|
||||
struct klist_task_context {
|
||||
enum klist_task_command cmd;
|
||||
struct blist_task_context {
|
||||
enum blist_task_command cmd;
|
||||
char *list;
|
||||
char *name;
|
||||
char *desc;
|
||||
char *stage;
|
||||
};
|
||||
typedef struct klist_task_context klist_task_context;
|
||||
typedef struct blist_task_context blist_task_context;
|
||||
|
||||
klist_task_context *klist_task_context_init(void);
|
||||
void klist_task_context_deinit(klist_task_context *ctx);
|
||||
blist_task_context *blist_task_context_init(void);
|
||||
void blist_task_context_deinit(blist_task_context *ctx);
|
|
@ -8,24 +8,27 @@
|
|||
#include "util.h"
|
||||
|
||||
void print_help(char **argv);
|
||||
void setup(klist_app *ctx, int argc, char **argv);
|
||||
char *prepare(int argc, char **argv);
|
||||
void setup(blist_app *ctx, int argc, char **argv);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char *name = "klist";
|
||||
klist_config config;
|
||||
char *name = "blist";
|
||||
blist_config config;
|
||||
|
||||
char *config_file_name = klist_config_name(name);
|
||||
char *config_file_name = prepare(argc, argv);
|
||||
if (!config_file_name)
|
||||
config_file_name = blist_config_name(name);
|
||||
|
||||
FILE *config_file = NULL;
|
||||
if (access(config_file_name, F_OK) == -1) {
|
||||
config_file = fopen(config_file_name, "w");
|
||||
klist_config_setup(name, config_file);
|
||||
blist_config_setup(name, config_file);
|
||||
} else
|
||||
config_file = fopen(config_file_name, "a+");
|
||||
|
||||
klist_config_parse(&config, config_file);
|
||||
blist_config_parse(&config, config_file);
|
||||
|
||||
klist_app *ctx = klist_app_init(config[CONFIG_DATABASE_NAME]);
|
||||
blist_app *ctx = blist_app_init(config[CONFIG_DATABASE_NAME]);
|
||||
|
||||
free(config_file_name);
|
||||
free(config[CONFIG_DATABASE_NAME]);
|
||||
|
@ -39,68 +42,85 @@ int main(int argc, char **argv) {
|
|||
setup(ctx, argc, argv);
|
||||
switch (ctx->cmd) {
|
||||
case USER:
|
||||
klist_user_context *user_ctx = ctx->cmd_ctx;
|
||||
blist_user_context *user_ctx = ctx->cmd_ctx;
|
||||
switch (user_ctx->cmd) {
|
||||
case USER_GET:
|
||||
klist_app_user_get(ctx);
|
||||
blist_app_user_get(ctx);
|
||||
break;
|
||||
case USER_CREATE:
|
||||
klist_app_user_create(ctx);
|
||||
blist_app_user_create(ctx);
|
||||
break;
|
||||
case USER_DELETE:
|
||||
klist_app_user_delete(ctx);
|
||||
blist_app_user_delete(ctx);
|
||||
break;
|
||||
}
|
||||
klist_user_context_deinit(user_ctx);
|
||||
blist_user_context_deinit(user_ctx);
|
||||
break;
|
||||
case LIST:
|
||||
klist_list_context *list_ctx = ctx->cmd_ctx;
|
||||
blist_list_context *list_ctx = ctx->cmd_ctx;
|
||||
if (!list_ctx->name && list_ctx->cmd != LIST_GET) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "Missing name.\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "Missing name.\n");
|
||||
break;
|
||||
}
|
||||
switch (list_ctx->cmd) {
|
||||
case LIST_ADD:
|
||||
klist_app_list_add(ctx);
|
||||
blist_app_list_add(ctx);
|
||||
break;
|
||||
case LIST_EDIT:
|
||||
break;
|
||||
case LIST_GET:
|
||||
klist_app_list_get(ctx);
|
||||
blist_app_list_get(ctx);
|
||||
break;
|
||||
case LIST_DELETE:
|
||||
klist_app_list_delete(ctx);
|
||||
blist_app_list_delete(ctx);
|
||||
}
|
||||
if (list_ctx)
|
||||
klist_list_context_deinit(list_ctx);
|
||||
blist_list_context_deinit(list_ctx);
|
||||
break;
|
||||
case TASK:
|
||||
klist_task_context *task_ctx = ctx->cmd_ctx;
|
||||
switch (((klist_task_context *)ctx->cmd_ctx)->cmd) {
|
||||
blist_task_context *task_ctx = ctx->cmd_ctx;
|
||||
switch (((blist_task_context *)ctx->cmd_ctx)->cmd) {
|
||||
case TASK_ADD: // basically the same things happen and edit can create if
|
||||
// necessary
|
||||
case TASK_EDIT:
|
||||
klist_app_task_edit(ctx);
|
||||
blist_app_task_edit(ctx);
|
||||
break;
|
||||
case TASK_GET:
|
||||
klist_app_task_get(ctx);
|
||||
blist_app_task_get(ctx);
|
||||
break;
|
||||
case TASK_DELETE:
|
||||
klist_app_task_delete(ctx);
|
||||
blist_app_task_delete(ctx);
|
||||
}
|
||||
klist_task_context_deinit(task_ctx);
|
||||
blist_task_context_deinit(task_ctx);
|
||||
default:;
|
||||
}
|
||||
}
|
||||
|
||||
const int error = ctx->error;
|
||||
klist_app_deinit(ctx);
|
||||
blist_app_deinit(ctx);
|
||||
return error;
|
||||
}
|
||||
|
||||
void print_help(char **argv) { printf("Usage: %s <action>\n", argv[0]); }
|
||||
|
||||
void setup(klist_app *ctx, int argc, char **argv) {
|
||||
char *prepare(int argc, char **argv) {
|
||||
char *config = NULL;
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "hc")) != -1) {
|
||||
switch (opt) {
|
||||
case 'c':
|
||||
config = strdup(optarg);
|
||||
break;
|
||||
case 'h':
|
||||
print_help(argv);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
void setup(blist_app *ctx, int argc, char **argv) {
|
||||
if (strcmp(argv[1], "user") == 0)
|
||||
ctx->cmd = USER;
|
||||
else if (strcmp(argv[1], "list") == 0)
|
||||
|
@ -114,7 +134,7 @@ void setup(klist_app *ctx, int argc, char **argv) {
|
|||
switch (ctx->cmd) {
|
||||
case USER:
|
||||
optind = 2;
|
||||
klist_user_context *user_ctx = klist_user_context_init();
|
||||
blist_user_context *user_ctx = blist_user_context_init();
|
||||
while ((opt = getopt(argc, argv, "cdg")) != -1)
|
||||
switch (opt) {
|
||||
case 'c':
|
||||
|
@ -132,7 +152,7 @@ void setup(klist_app *ctx, int argc, char **argv) {
|
|||
ctx->cmd_ctx = user_ctx;
|
||||
break;
|
||||
case LIST:
|
||||
klist_list_context *list_ctx = klist_list_context_init();
|
||||
blist_list_context *list_ctx = blist_list_context_init();
|
||||
ctx->cmd_ctx = list_ctx;
|
||||
if (argc < 3) {
|
||||
list_ctx->cmd = LIST_GET;
|
||||
|
@ -183,7 +203,7 @@ void setup(klist_app *ctx, int argc, char **argv) {
|
|||
}
|
||||
break;
|
||||
case TASK:
|
||||
klist_task_context *task_ctx = klist_task_context_init();
|
||||
blist_task_context *task_ctx = blist_task_context_init();
|
||||
|
||||
optind = 2;
|
||||
|
||||
|
@ -212,7 +232,7 @@ void setup(klist_app *ctx, int argc, char **argv) {
|
|||
break;
|
||||
default:
|
||||
print_help(argv);
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
""
|
||||
"task options:"
|
||||
"-a\tadd a task"
|
||||
|
@ -225,7 +245,7 @@ void setup(klist_app *ctx, int argc, char **argv) {
|
|||
ctx->cmd_ctx = task_ctx;
|
||||
break;
|
||||
default:
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"How did we land here?! Pls report argv[1] = %s\n", argv[1]);
|
||||
print_help(argv);
|
||||
}
|
||||
|
|
|
@ -6,179 +6,179 @@
|
|||
|
||||
#include "models.h"
|
||||
|
||||
int klist_app_user_get(const klist_app *ctx) {
|
||||
klist_user *user = klist_user_get_by_local(ctx->handle, getuid());
|
||||
int blist_app_user_get(const blist_app *ctx) {
|
||||
blist_user *user = blist_user_get_by_local(ctx->handle, getuid());
|
||||
|
||||
if (user) {
|
||||
printf("User: %s\nID: %lu\n", (char *)user->name, user->id);
|
||||
size_t lists_len = 0;
|
||||
klist_list **lists =
|
||||
klist_list_get_all_by_user(ctx->handle, user->id, &lists_len);
|
||||
blist_list **lists =
|
||||
blist_list_get_all_by_user(ctx->handle, user->id, &lists_len);
|
||||
printf("Lists: %lu\n", lists_len);
|
||||
int i = 0;
|
||||
for (; i < lists_len; i++)
|
||||
klist_list_deinit(lists[i]);
|
||||
blist_list_deinit(lists[i]);
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "No user for '%s' found.\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "No user for '%s' found.\n",
|
||||
getlogin());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int klist_app_user_create(const klist_app *ctx) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_INFO,
|
||||
(klist_assure_user(ctx->handle, getuid(), getlogin()))
|
||||
int blist_app_user_create(const blist_app *ctx) {
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_INFO,
|
||||
(blist_assure_user(ctx->handle, getuid(), getlogin()))
|
||||
? "User created.\n"
|
||||
: "User already exists.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int klist_app_user_delete(const klist_app *ctx) {
|
||||
klist_user *user = klist_user_get_by_local(ctx->handle, getuid());
|
||||
int blist_app_user_delete(const blist_app *ctx) {
|
||||
blist_user *user = blist_user_get_by_local(ctx->handle, getuid());
|
||||
if (user) {
|
||||
klist_user_delete(ctx->handle, user);
|
||||
blist_user_delete(ctx->handle, user);
|
||||
printf("User deleted.\n");
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"User not found, no changes done.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int klist_app_list_add(const klist_app *ctx) {
|
||||
klist_list_context *list_ctx = ctx->cmd_ctx;
|
||||
int blist_app_list_add(const blist_app *ctx) {
|
||||
blist_list_context *list_ctx = ctx->cmd_ctx;
|
||||
if (!list_ctx->stages_len) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "Missing stages.\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "Missing stages.\n");
|
||||
return 1;
|
||||
}
|
||||
klist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
klist_user *user = klist_user_get_by_local(ctx->handle, getuid());
|
||||
klist_list *list =
|
||||
klist_list_get_by_user_and_name(ctx->handle, user->id, list_ctx->name);
|
||||
blist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
blist_user *user = blist_user_get_by_local(ctx->handle, getuid());
|
||||
blist_list *list =
|
||||
blist_list_get_by_user_and_name(ctx->handle, user->id, list_ctx->name);
|
||||
if (!list)
|
||||
list = klist_list_init();
|
||||
list = blist_list_init();
|
||||
list->name = (unsigned char *)strdup(list_ctx->name);
|
||||
if (list->desc)
|
||||
list->desc = (unsigned char *)strdup(list_ctx->desc);
|
||||
list->is_preset = false;
|
||||
klist_list_save(ctx->handle, list, user);
|
||||
blist_list_save(ctx->handle, list, user);
|
||||
printf("Added list '%s'\n", (char *)list->name);
|
||||
int i = 0;
|
||||
for (; i < list_ctx->stages_len; i++) {
|
||||
klist_stage *stage = klist_stage_init();
|
||||
blist_stage *stage = blist_stage_init();
|
||||
stage->name = (unsigned char *)strdup(list_ctx->stages[i]);
|
||||
stage->list_id = list->id;
|
||||
klist_stage_save(ctx->handle, stage);
|
||||
klist_stage_deinit(stage);
|
||||
blist_stage_save(ctx->handle, stage);
|
||||
blist_stage_deinit(stage);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int klist_app_list_edit(const klist_app *ctx) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "Not implemented\n");
|
||||
int blist_app_list_edit(const blist_app *ctx) {
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "Not implemented\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int klist_app_list_get(const klist_app *ctx) {
|
||||
klist_list_context *list_ctx = ctx->cmd_ctx;
|
||||
klist_user *user = klist_user_get_by_local(ctx->handle, getuid());
|
||||
int blist_app_list_get(const blist_app *ctx) {
|
||||
blist_list_context *list_ctx = ctx->cmd_ctx;
|
||||
blist_user *user = blist_user_get_by_local(ctx->handle, getuid());
|
||||
|
||||
size_t lists_len = 0;
|
||||
klist_list **lists = NULL;
|
||||
blist_list **lists = NULL;
|
||||
if (list_ctx->name) {
|
||||
klist_list *list =
|
||||
klist_list_get_by_user_and_name(ctx->handle, user->id, list_ctx->name);
|
||||
blist_list *list =
|
||||
blist_list_get_by_user_and_name(ctx->handle, user->id, list_ctx->name);
|
||||
if (list) {
|
||||
lists_len = 1;
|
||||
lists = malloc(lists_len * sizeof(klist_list));
|
||||
memcpy(lists[0], list, sizeof(klist_list));
|
||||
klist_list_deinit(list);
|
||||
lists = malloc(lists_len * sizeof(blist_list));
|
||||
memcpy(lists[0], list, sizeof(blist_list));
|
||||
blist_list_deinit(list);
|
||||
};
|
||||
} else
|
||||
lists = klist_list_get_all_by_user(ctx->handle, user->id, &lists_len);
|
||||
lists = blist_list_get_all_by_user(ctx->handle, user->id, &lists_len);
|
||||
|
||||
if (lists_len) {
|
||||
int i = 0;
|
||||
for (; i < lists_len; i++) {
|
||||
klist_list *list = lists[i];
|
||||
blist_list *list = lists[i];
|
||||
size_t tasks_len = 0;
|
||||
klist_task **tasks =
|
||||
klist_task_get_for_list(ctx->handle, list->id, &tasks_len);
|
||||
blist_task **tasks =
|
||||
blist_task_get_for_list(ctx->handle, list->id, &tasks_len);
|
||||
printf("Name: %s\n", (char *)list->name);
|
||||
printf("Description: %s\n", list->desc ? (char *)list->desc : "N/A");
|
||||
printf("Tasks: %ld\n", tasks_len);
|
||||
int j = 0;
|
||||
for (; j < tasks_len; j++)
|
||||
klist_task_deinit(tasks[j]);
|
||||
blist_task_deinit(tasks[j]);
|
||||
free(tasks);
|
||||
klist_list_deinit(list);
|
||||
blist_list_deinit(list);
|
||||
}
|
||||
free(lists);
|
||||
} else if (list_ctx->name)
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "List '%s' not found.\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "List '%s' not found.\n",
|
||||
list_ctx->name);
|
||||
else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_WARNING, "No lists found.\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_WARNING, "No lists found.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int klist_app_list_delete(const klist_app *ctx) {
|
||||
klist_list_context *list_ctx = ctx->cmd_ctx;
|
||||
klist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
klist_user *user = klist_user_get_by_local(ctx->handle, getuid());
|
||||
int blist_app_list_delete(const blist_app *ctx) {
|
||||
blist_list_context *list_ctx = ctx->cmd_ctx;
|
||||
blist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
blist_user *user = blist_user_get_by_local(ctx->handle, getuid());
|
||||
if (!user) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "User doesn't exist.\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "User doesn't exist.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
klist_list *list =
|
||||
klist_list_get_by_user_and_name(ctx->handle, user->id, list_ctx->name);
|
||||
blist_list *list =
|
||||
blist_list_get_by_user_and_name(ctx->handle, user->id, list_ctx->name);
|
||||
if (list) {
|
||||
size_t len = 0;
|
||||
klist_task **tasks = klist_task_get_for_list(ctx->handle, list->id, &len);
|
||||
blist_task **tasks = blist_task_get_for_list(ctx->handle, list->id, &len);
|
||||
int i = 0;
|
||||
for (; i < len; i++) {
|
||||
klist_task_delete(ctx->handle, tasks[i]);
|
||||
klist_task_deinit(tasks[i]);
|
||||
blist_task_delete(ctx->handle, tasks[i]);
|
||||
blist_task_deinit(tasks[i]);
|
||||
}
|
||||
klist_stage **stages =
|
||||
klist_stage_get_all_for_list(ctx->handle, list->id, &len);
|
||||
blist_stage **stages =
|
||||
blist_stage_get_all_for_list(ctx->handle, list->id, &len);
|
||||
for (i = 0; i < len; i++) {
|
||||
klist_stage_delete(ctx->handle, stages[i]);
|
||||
klist_stage_deinit(stages[i]);
|
||||
blist_stage_delete(ctx->handle, stages[i]);
|
||||
blist_stage_deinit(stages[i]);
|
||||
}
|
||||
klist_list_delete(ctx->handle, list);
|
||||
klist_list_deinit(list);
|
||||
blist_list_delete(ctx->handle, list);
|
||||
blist_list_deinit(list);
|
||||
free(stages);
|
||||
free(tasks);
|
||||
} else {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "List '%s' not found.\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "List '%s' not found.\n",
|
||||
list_ctx->name);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int klist_app_task_add(const klist_app *ctx) {
|
||||
return klist_app_task_edit(ctx);
|
||||
int blist_app_task_add(const blist_app *ctx) {
|
||||
return blist_app_task_edit(ctx);
|
||||
}
|
||||
|
||||
int klist_app_task_edit(const klist_app *ctx) {
|
||||
klist_task_context *task_ctx = ctx->cmd_ctx;
|
||||
klist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
klist_user *user = klist_user_get_by_local(ctx->handle, getuid());
|
||||
klist_list *list = NULL;
|
||||
int blist_app_task_edit(const blist_app *ctx) {
|
||||
blist_task_context *task_ctx = ctx->cmd_ctx;
|
||||
blist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
blist_user *user = blist_user_get_by_local(ctx->handle, getuid());
|
||||
blist_list *list = NULL;
|
||||
|
||||
if (!user) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "User doesn't exist.\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "User doesn't exist.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
list = klist_list_get_by_user_and_name(ctx->handle, user->id, task_ctx->list);
|
||||
list = blist_list_get_by_user_and_name(ctx->handle, user->id, task_ctx->list);
|
||||
if (list) {
|
||||
size_t stages_len = 0;
|
||||
int i = 0;
|
||||
klist_stage **stages =
|
||||
klist_stage_get_all_for_list(ctx->handle, list->id, &stages_len);
|
||||
blist_stage **stages =
|
||||
blist_stage_get_all_for_list(ctx->handle, list->id, &stages_len);
|
||||
if (!task_ctx->stage) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"Stage missing/wrong, please pass one of: ");
|
||||
for (i = 0; i < stages_len; i++)
|
||||
fprintf(ctx->log_ctx->log_target, "%s ", (char *)stages[i]->name);
|
||||
|
@ -190,20 +190,20 @@ int klist_app_task_edit(const klist_app *ctx) {
|
|||
stage_id = stages[i]->id;
|
||||
|
||||
if (stage_id == -1) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"Stage %s not found. Use one of: ", task_ctx->stage);
|
||||
for (i = 0; i < stages_len; i++)
|
||||
fprintf(ctx->log_ctx->log_target, "%s ", (char *)stages[i]->name);
|
||||
fprintf(ctx->log_ctx->log_target, "\n");
|
||||
}
|
||||
for (i = 0; i < stages_len; i++)
|
||||
klist_stage_deinit(stages[i]);
|
||||
blist_stage_deinit(stages[i]);
|
||||
free(stages);
|
||||
|
||||
klist_task *task =
|
||||
klist_task_get_for_list_by_name(ctx->handle, list->id, task_ctx->name);
|
||||
blist_task *task =
|
||||
blist_task_get_for_list_by_name(ctx->handle, list->id, task_ctx->name);
|
||||
if (!task)
|
||||
task = klist_task_init();
|
||||
task = blist_task_init();
|
||||
if (task->name)
|
||||
free(task->name);
|
||||
task->name = (unsigned char *)strdup(task_ctx->name);
|
||||
|
@ -212,84 +212,84 @@ int klist_app_task_edit(const klist_app *ctx) {
|
|||
if (task_ctx->desc)
|
||||
task->desc = (unsigned char *)strdup(task_ctx->desc);
|
||||
task->stage_id = stage_id;
|
||||
klist_task_save(ctx->handle, task);
|
||||
klist_task_deinit(task);
|
||||
blist_task_save(ctx->handle, task);
|
||||
blist_task_deinit(task);
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "List not found.\n");
|
||||
klist_user_deinit(user);
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "List not found.\n");
|
||||
blist_user_deinit(user);
|
||||
if (list)
|
||||
klist_list_deinit(list);
|
||||
blist_list_deinit(list);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int klist_app_task_get(const klist_app *ctx) {
|
||||
klist_task_context *task_ctx = ctx->cmd_ctx;
|
||||
klist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
klist_user *user = klist_user_get_by_local(ctx->handle, getuid());
|
||||
int blist_app_task_get(const blist_app *ctx) {
|
||||
blist_task_context *task_ctx = ctx->cmd_ctx;
|
||||
blist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
blist_user *user = blist_user_get_by_local(ctx->handle, getuid());
|
||||
if (!user) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "User doesn't exist.\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "User doesn't exist.\n");
|
||||
return 1;
|
||||
}
|
||||
klist_list *list =
|
||||
klist_list_get_by_user_and_name(ctx->handle, user->id, task_ctx->list);
|
||||
blist_list *list =
|
||||
blist_list_get_by_user_and_name(ctx->handle, user->id, task_ctx->list);
|
||||
if (list) {
|
||||
if (task_ctx->name) {
|
||||
klist_task *task = klist_task_get_for_list_by_name(ctx->handle, list->id,
|
||||
blist_task *task = blist_task_get_for_list_by_name(ctx->handle, list->id,
|
||||
task_ctx->name);
|
||||
if (task) {
|
||||
printf("Name: %s\n", (char *)task->name);
|
||||
printf("Description: %s\n", task->desc ? (char *)task->desc : "N/A");
|
||||
klist_stage *stage = klist_stage_get_by_id(ctx->handle, task->stage_id);
|
||||
blist_stage *stage = blist_stage_get_by_id(ctx->handle, task->stage_id);
|
||||
printf("Stage: %s\n", (char *)stage->name);
|
||||
klist_stage_deinit(stage);
|
||||
klist_task_deinit(task);
|
||||
blist_stage_deinit(stage);
|
||||
blist_task_deinit(task);
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "Task not found.\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "Task not found.\n");
|
||||
} else {
|
||||
size_t tasks_len = 0;
|
||||
klist_task **tasks =
|
||||
klist_task_get_for_list(ctx->handle, list->id, &tasks_len);
|
||||
klist_print_list(ctx->handle, list, tasks, tasks_len);
|
||||
blist_task **tasks =
|
||||
blist_task_get_for_list(ctx->handle, list->id, &tasks_len);
|
||||
blist_print_list(ctx->handle, list, tasks, tasks_len);
|
||||
|
||||
int i = 0;
|
||||
for (; i < tasks_len; i++)
|
||||
klist_task_deinit(tasks[i]);
|
||||
blist_task_deinit(tasks[i]);
|
||||
free(tasks);
|
||||
}
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "list not found\n");
|
||||
klist_user_deinit(user);
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "list not found\n");
|
||||
blist_user_deinit(user);
|
||||
if (list)
|
||||
klist_list_deinit(list);
|
||||
blist_list_deinit(list);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int klist_app_task_delete(const klist_app *ctx) {
|
||||
klist_task_context *task_ctx = ctx->cmd_ctx;
|
||||
klist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
klist_user *user = klist_user_get_by_local(ctx->handle, getuid());
|
||||
int blist_app_task_delete(const blist_app *ctx) {
|
||||
blist_task_context *task_ctx = ctx->cmd_ctx;
|
||||
blist_assure_user(ctx->handle, getuid(), getlogin());
|
||||
blist_user *user = blist_user_get_by_local(ctx->handle, getuid());
|
||||
if (!user) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "User doesn't exist.\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "User doesn't exist.\n");
|
||||
return 1;
|
||||
}
|
||||
klist_list *list =
|
||||
klist_list_get_by_user_and_name(ctx->handle, user->id, task_ctx->list);
|
||||
blist_list *list =
|
||||
blist_list_get_by_user_and_name(ctx->handle, user->id, task_ctx->list);
|
||||
if (list) {
|
||||
if (task_ctx->name) {
|
||||
klist_task *task = klist_task_get_for_list_by_name(ctx->handle, list->id,
|
||||
blist_task *task = blist_task_get_for_list_by_name(ctx->handle, list->id,
|
||||
task_ctx->name);
|
||||
if (task) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_WARNING, "Deleting task '%s'.\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_WARNING, "Deleting task '%s'.\n",
|
||||
(char *)task->name);
|
||||
klist_task_delete(ctx->handle, task);
|
||||
klist_task_deinit(task);
|
||||
blist_task_delete(ctx->handle, task);
|
||||
blist_task_deinit(task);
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "Task not found.\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "Task not found.\n");
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"Task not found, only deletion by name is "
|
||||
"currently supoprted.\n");
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "Not implemented\n");
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "Not implemented\n");
|
||||
return 0;
|
||||
}
|
|
@ -2,38 +2,43 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
|
||||
klist_app *klist_app_init(char *db) {
|
||||
klist_app *app = malloc(sizeof(klist_app));
|
||||
blist_app *blist_app_init(char *db) {
|
||||
blist_app *app = malloc(sizeof(blist_app));
|
||||
app->cmd = 0;
|
||||
app->cmd_ctx = 0;
|
||||
app->error = 0;
|
||||
app->log_ctx = malloc(sizeof(klist_logging_ctx));
|
||||
app->log_ctx->log_level = KLIST_LOG_DEBUG;
|
||||
app->config = NULL;
|
||||
app->log_ctx = malloc(sizeof(blist_logging_ctx));
|
||||
app->log_ctx->log_level = BLIST_LOG_DEBUG;
|
||||
app->log_ctx->log_target = stderr;
|
||||
app->handle = klist_init(db);
|
||||
app->handle = blist_init(db);
|
||||
app->handle->log_ctx->log_level = app->log_ctx->log_level;
|
||||
app->handle->log_ctx->log_target = app->log_ctx->log_target;
|
||||
return app;
|
||||
}
|
||||
void klist_app_deinit(klist_app *ctx) {
|
||||
free(ctx->log_ctx);
|
||||
void blist_app_deinit(blist_app *ctx) {
|
||||
if (ctx->config)
|
||||
free(ctx->config);
|
||||
if (ctx->handle)
|
||||
klist_deinit(ctx->handle);
|
||||
blist_deinit(ctx->handle);
|
||||
else if (ctx->log_ctx)
|
||||
fclose(ctx->log_ctx->log_target);
|
||||
free(ctx->log_ctx);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
klist_user_context *klist_user_context_init() {
|
||||
klist_user_context *ctx = malloc(sizeof(klist_user_context));
|
||||
blist_user_context *blist_user_context_init() {
|
||||
blist_user_context *ctx = malloc(sizeof(blist_user_context));
|
||||
ctx->cmd = -1;
|
||||
return ctx;
|
||||
}
|
||||
void klist_user_context_deinit(klist_user_context *ctx) {
|
||||
void blist_user_context_deinit(blist_user_context *ctx) {
|
||||
free(ctx);
|
||||
ctx = NULL;
|
||||
}
|
||||
|
||||
klist_list_context *klist_list_context_init() {
|
||||
klist_list_context *ctx = malloc(sizeof(klist_list_context));
|
||||
blist_list_context *blist_list_context_init() {
|
||||
blist_list_context *ctx = malloc(sizeof(blist_list_context));
|
||||
ctx->cmd = -1;
|
||||
ctx->name = NULL;
|
||||
ctx->desc = NULL;
|
||||
|
@ -42,7 +47,7 @@ klist_list_context *klist_list_context_init() {
|
|||
ctx->preset = NULL;
|
||||
return ctx;
|
||||
}
|
||||
void klist_list_context_deinit(klist_list_context *ctx) {
|
||||
void blist_list_context_deinit(blist_list_context *ctx) {
|
||||
if (ctx->name)
|
||||
free(ctx->name);
|
||||
if (ctx->desc)
|
||||
|
@ -59,8 +64,8 @@ void klist_list_context_deinit(klist_list_context *ctx) {
|
|||
ctx = NULL;
|
||||
}
|
||||
|
||||
klist_task_context *klist_task_context_init() {
|
||||
klist_task_context *ctx = malloc(sizeof(klist_task_context));
|
||||
blist_task_context *blist_task_context_init() {
|
||||
blist_task_context *ctx = malloc(sizeof(blist_task_context));
|
||||
ctx->cmd = -1;
|
||||
ctx->name = NULL;
|
||||
ctx->desc = NULL;
|
||||
|
@ -68,7 +73,7 @@ klist_task_context *klist_task_context_init() {
|
|||
ctx->stage = NULL;
|
||||
return ctx;
|
||||
}
|
||||
void klist_task_context_deinit(klist_task_context *ctx) {
|
||||
void blist_task_context_deinit(blist_task_context *ctx) {
|
||||
if (ctx->name)
|
||||
free(ctx->name);
|
||||
if (ctx->list)
|
||||
|
|
3
src/libblist/CMakeLists.txt
Normal file
3
src/libblist/CMakeLists.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
add_library(libblist models.c sql.c util.c)
|
||||
target_include_directories(libblist PUBLIC include)
|
||||
target_link_libraries(libblist sqlite3)
|
|
@ -1,5 +1,5 @@
|
|||
#ifndef KLIST_H
|
||||
#define KLIST_H
|
||||
#ifndef BLIST_H
|
||||
#define BLIST_H
|
||||
|
||||
#include "models.h"
|
||||
#include "sql.h"
|
78
src/libblist/include/models.h
Normal file
78
src/libblist/include/models.h
Normal file
|
@ -0,0 +1,78 @@
|
|||
#pragma once
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
struct blist_user {
|
||||
ssize_t id;
|
||||
unsigned char *name;
|
||||
ssize_t local_id;
|
||||
};
|
||||
typedef struct blist_user blist_user;
|
||||
|
||||
blist_user *blist_user_init();
|
||||
blist_user *blist_user_init_from_sql(sqlite3_stmt *);
|
||||
blist_user *blist_user_get_by_id(const blist *, u_int);
|
||||
blist_user *blist_user_get_by_local(const blist *, u_int);
|
||||
blist_user *blist_user_get_by_discord(const blist *, u_int);
|
||||
blist_user *blist_user_get_by_google(const blist *, u_int);
|
||||
void blist_user_save(const blist *, blist_user *);
|
||||
void blist_user_delete(const blist *, const blist_user *);
|
||||
void blist_user_deinit(blist_user *);
|
||||
|
||||
struct blist_list {
|
||||
ssize_t id;
|
||||
unsigned char *name;
|
||||
unsigned char *desc;
|
||||
bool is_preset;
|
||||
};
|
||||
typedef struct blist_list blist_list;
|
||||
|
||||
blist_list *blist_list_init();
|
||||
blist_list *blist_list_init_from_sql(sqlite3_stmt *);
|
||||
blist_list *blist_list_get_by_id(const blist *, u_int);
|
||||
blist_list **blist_list_get_all_by_user(const blist *, u_int, size_t *);
|
||||
blist_list *blist_list_get_by_user_and_name(const blist *, u_int, const char *);
|
||||
void blist_list_save(const blist *, blist_list *, const blist_user *);
|
||||
void blist_list_delete(const blist *, blist_list *);
|
||||
void blist_list_deinit(blist_list *);
|
||||
|
||||
struct blist_stage {
|
||||
ssize_t id;
|
||||
unsigned char *name;
|
||||
unsigned char *desc;
|
||||
ssize_t list_id;
|
||||
};
|
||||
typedef struct blist_stage blist_stage;
|
||||
|
||||
blist_stage *blist_stage_init();
|
||||
blist_stage *blist_stage_init_from_sql(sqlite3_stmt *);
|
||||
blist_stage *blist_stage_get_by_id(const blist *, u_int);
|
||||
blist_stage **blist_stage_get_all_for_list(const blist *, u_int, size_t *);
|
||||
void blist_stage_save(const blist *, blist_stage *);
|
||||
void blist_stage_delete(const blist *, const blist_stage *);
|
||||
void blist_stage_deinit(blist_stage *);
|
||||
|
||||
struct blist_task {
|
||||
ssize_t id;
|
||||
unsigned char *name;
|
||||
unsigned char *desc;
|
||||
int stage_id;
|
||||
time_t due;
|
||||
int target_stage;
|
||||
};
|
||||
typedef struct blist_task blist_task;
|
||||
|
||||
blist_task *blist_task_init();
|
||||
blist_task *blist_task_init_from_sql(sqlite3_stmt *);
|
||||
blist_task *blist_task_get_by_id(const blist *, u_int);
|
||||
blist_task **blist_task_get_for_list(const blist *, u_int, size_t *);
|
||||
blist_task *blist_task_get_for_list_by_name(const blist *, u_int, const char *);
|
||||
void blist_task_save(const blist *, blist_task *);
|
||||
void blist_task_delete(const blist *, const blist_task *);
|
||||
void blist_task_deinit(blist_task *);
|
||||
|
||||
void blist_print_user(blist *, const blist_user *);
|
||||
void blist_print_list(const blist *, const blist_list *, blist_task **, size_t);
|
||||
void blist_print_task(blist *, blist_task **, size_t);
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
enum KLIST_SQL {
|
||||
enum BLIST_SQL {
|
||||
INIT,
|
||||
ADD_LOGIN,
|
||||
ADD_USER,
|
||||
|
@ -29,7 +29,7 @@ enum KLIST_SQL {
|
|||
MOD_TASK_NAME,
|
||||
MOD_TASK_DESCRIPTION,
|
||||
DEL_TASK,
|
||||
_KLIST_SQL_COUNT
|
||||
_BLIST_SQL_COUNT
|
||||
};
|
||||
|
||||
const char *klist_sql_get(const enum KLIST_SQL);
|
||||
const char *blist_sql_get(const enum BLIST_SQL);
|
57
src/libblist/include/util.h
Normal file
57
src/libblist/include/util.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
#pragma once
|
||||
#include <sqlite3.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/*
|
||||
* command parsing
|
||||
*/
|
||||
|
||||
enum BLIST_LOG_LEVEL {
|
||||
BLIST_LOG_ERROR,
|
||||
BLIST_LOG_WARNING,
|
||||
BLIST_LOG_INFO,
|
||||
BLIST_LOG_DEBUG
|
||||
};
|
||||
typedef enum BLIST_LOG_LEVEL BLIST_LOG_LEVEL;
|
||||
|
||||
struct blist_logging_ctx {
|
||||
BLIST_LOG_LEVEL log_level;
|
||||
FILE *log_target;
|
||||
};
|
||||
|
||||
typedef struct blist_logging_ctx blist_logging_ctx;
|
||||
|
||||
struct blist {
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt **stmts;
|
||||
int error;
|
||||
blist_logging_ctx *log_ctx;
|
||||
};
|
||||
typedef struct blist blist;
|
||||
|
||||
blist *blist_init(char *db);
|
||||
void blist_deinit(blist *list);
|
||||
|
||||
/*
|
||||
* sql preparations
|
||||
*/
|
||||
|
||||
void blist_sql_prepare(blist *, char *);
|
||||
|
||||
/*
|
||||
* assuring things
|
||||
*/
|
||||
|
||||
bool blist_assure_user(const blist *, __uid_t, const char *);
|
||||
void blist_assure_list(blist *, char *);
|
||||
void blist_assure_task(blist *, char *);
|
||||
|
||||
/*
|
||||
* printing methods
|
||||
*
|
||||
* json printing will be implemented eventually
|
||||
*/
|
||||
|
||||
void blist_print(blist_logging_ctx *, BLIST_LOG_LEVEL, const char *, ...);
|
|
@ -6,43 +6,43 @@
|
|||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "klist.h"
|
||||
#include "blist.h"
|
||||
#include "sql.h"
|
||||
|
||||
klist_user *klist_user_init() {
|
||||
klist_user *user = malloc(sizeof(klist_user));
|
||||
blist_user *blist_user_init() {
|
||||
blist_user *user = malloc(sizeof(blist_user));
|
||||
user->id = -1;
|
||||
user->name = NULL;
|
||||
user->local_id = -1;
|
||||
return user;
|
||||
}
|
||||
|
||||
klist_user *klist_user_init_from_sql(sqlite3_stmt *stmt) {
|
||||
klist_user *user = klist_user_init();
|
||||
blist_user *blist_user_init_from_sql(sqlite3_stmt *stmt) {
|
||||
blist_user *user = blist_user_init();
|
||||
user->id = sqlite3_column_int(stmt, 0);
|
||||
if (sqlite3_column_bytes(stmt, 1) > 0)
|
||||
user->name = (unsigned char *)strdup((char *)sqlite3_column_text(stmt, 1));
|
||||
return user;
|
||||
}
|
||||
|
||||
klist_user *klist_user_get_by_id(const klist *ctx, u_int id) {
|
||||
blist_user *blist_user_get_by_id(const blist *ctx, u_int id) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_USER];
|
||||
klist_user *user = NULL;
|
||||
blist_user *user = NULL;
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW)
|
||||
return klist_user_init_from_sql(stmt);
|
||||
return blist_user_init_from_sql(stmt);
|
||||
sqlite3_reset(stmt);
|
||||
return user;
|
||||
}
|
||||
|
||||
klist_user *klist_user_get_by_local(const klist *ctx, u_int local_id) {
|
||||
blist_user *blist_user_get_by_local(const blist *ctx, u_int local_id) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_USER_BY_LOCAL];
|
||||
klist_user *user = NULL;
|
||||
blist_user *user = NULL;
|
||||
sqlite3_bind_int(stmt, 1, local_id);
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
user = klist_user_init_from_sql(stmt);
|
||||
user = blist_user_init_from_sql(stmt);
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"failed to get user by local id %u: %s\n", local_id,
|
||||
sqlite3_errmsg(ctx->db));
|
||||
sqlite3_reset(stmt);
|
||||
|
@ -50,7 +50,7 @@ klist_user *klist_user_get_by_local(const klist *ctx, u_int local_id) {
|
|||
return user;
|
||||
}
|
||||
|
||||
void klist_user_save(const klist *ctx, klist_user *user) {
|
||||
void blist_user_save(const blist *ctx, blist_user *user) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[ADD_USER];
|
||||
int result = 0;
|
||||
if (user->id > -1)
|
||||
|
@ -60,7 +60,7 @@ void klist_user_save(const klist *ctx, klist_user *user) {
|
|||
if ((result = sqlite3_step(stmt)) == SQLITE_ROW)
|
||||
user->id = sqlite3_column_int(stmt, 0);
|
||||
else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "failed to save user: %s - %d\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "failed to save user: %s - %d\n",
|
||||
sqlite3_errmsg(ctx->db), result);
|
||||
sqlite3_reset(stmt);
|
||||
|
||||
|
@ -69,30 +69,30 @@ void klist_user_save(const klist *ctx, klist_user *user) {
|
|||
sqlite3_bind_int(stmt, 1, user->id);
|
||||
sqlite3_bind_int(stmt, 2, user->local_id);
|
||||
if (sqlite3_step(stmt) != SQLITE_ROW)
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"failed to save logins for user: %s - %d\n",
|
||||
sqlite3_errmsg(ctx->db), result);
|
||||
sqlite3_reset(stmt);
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "no local uid saved for user %s",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "no local uid saved for user %s",
|
||||
(char *)user->name);
|
||||
}
|
||||
|
||||
void klist_user_delete(const klist *ctx, const klist_user *user) {
|
||||
void blist_user_delete(const blist *ctx, const blist_user *user) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[DEL_USER];
|
||||
sqlite3_bind_int(stmt, 1, (int)user->id);
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW)
|
||||
sqlite3_reset(stmt);
|
||||
}
|
||||
|
||||
void klist_user_deinit(klist_user *user) {
|
||||
void blist_user_deinit(blist_user *user) {
|
||||
if (user->name)
|
||||
free(user->name);
|
||||
free(user);
|
||||
}
|
||||
|
||||
klist_stage *klist_stage_init() {
|
||||
klist_stage *stage = malloc(sizeof(klist_stage));
|
||||
blist_stage *blist_stage_init() {
|
||||
blist_stage *stage = malloc(sizeof(blist_stage));
|
||||
stage->id = -1;
|
||||
stage->name = NULL;
|
||||
stage->desc = NULL;
|
||||
|
@ -100,8 +100,8 @@ klist_stage *klist_stage_init() {
|
|||
return stage;
|
||||
}
|
||||
|
||||
klist_stage *klist_stage_init_from_sql(sqlite3_stmt *stmt) {
|
||||
klist_stage *stage = klist_stage_init();
|
||||
blist_stage *blist_stage_init_from_sql(sqlite3_stmt *stmt) {
|
||||
blist_stage *stage = blist_stage_init();
|
||||
stage->id = sqlite3_column_int(stmt, 0);
|
||||
if (sqlite3_column_bytes(stmt, 1) > 0)
|
||||
stage->name = (unsigned char *)strdup((char *)sqlite3_column_text(stmt, 1));
|
||||
|
@ -110,28 +110,28 @@ klist_stage *klist_stage_init_from_sql(sqlite3_stmt *stmt) {
|
|||
return stage;
|
||||
}
|
||||
|
||||
klist_stage *klist_stage_get_by_id(const klist *ctx, u_int id) {
|
||||
blist_stage *blist_stage_get_by_id(const blist *ctx, u_int id) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_STAGE];
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
sqlite3_reset(stmt);
|
||||
return klist_stage_init_from_sql(stmt);
|
||||
return blist_stage_init_from_sql(stmt);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
klist_stage **klist_stage_get_all_for_list(const klist *ctx, u_int id,
|
||||
blist_stage **blist_stage_get_all_for_list(const blist *ctx, u_int id,
|
||||
size_t *count) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_STAGES_FOR_LIST];
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
klist_stage **stages = NULL;
|
||||
blist_stage **stages = NULL;
|
||||
size_t stages_len = 0;
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
klist_stage **_stages =
|
||||
realloc(stages, ++stages_len * sizeof(klist_stage *));
|
||||
blist_stage **_stages =
|
||||
realloc(stages, ++stages_len * sizeof(blist_stage *));
|
||||
if (_stages) {
|
||||
stages = _stages;
|
||||
stages[stages_len - 1] = klist_stage_init_from_sql(stmt);
|
||||
stages[stages_len - 1] = blist_stage_init_from_sql(stmt);
|
||||
}
|
||||
}
|
||||
if (count != NULL)
|
||||
|
@ -139,7 +139,7 @@ klist_stage **klist_stage_get_all_for_list(const klist *ctx, u_int id,
|
|||
return stages;
|
||||
}
|
||||
|
||||
void klist_stage_save(const klist *ctx, klist_stage *stage) {
|
||||
void blist_stage_save(const blist *ctx, blist_stage *stage) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[ADD_STAGE];
|
||||
int result = 0;
|
||||
if (stage->id > -1)
|
||||
|
@ -151,22 +151,22 @@ void klist_stage_save(const klist *ctx, klist_stage *stage) {
|
|||
stage->id = sqlite3_column_int(stmt, 0);
|
||||
sqlite3_reset(stmt);
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"failed to save stage: %s - %d\n", sqlite3_errmsg(ctx->db),
|
||||
result);
|
||||
}
|
||||
|
||||
void klist_stage_delete(const klist *ctx, const klist_stage *stage) {
|
||||
void blist_stage_delete(const blist *ctx, const blist_stage *stage) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[DEL_STAGE];
|
||||
sqlite3_bind_int(stmt, 1, stage->id);
|
||||
if (sqlite3_step(stmt) == SQLITE_DONE)
|
||||
sqlite3_reset(stmt);
|
||||
else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "failed to delete task: %s\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "failed to delete task: %s\n",
|
||||
sqlite3_errmsg(ctx->db));
|
||||
}
|
||||
|
||||
void klist_stage_deinit(klist_stage *stage) {
|
||||
void blist_stage_deinit(blist_stage *stage) {
|
||||
if (stage->name)
|
||||
free(stage->name);
|
||||
if (stage->desc)
|
||||
|
@ -174,8 +174,8 @@ void klist_stage_deinit(klist_stage *stage) {
|
|||
free(stage);
|
||||
}
|
||||
|
||||
klist_list *klist_list_init() {
|
||||
klist_list *list = malloc(sizeof(klist_list));
|
||||
blist_list *blist_list_init() {
|
||||
blist_list *list = malloc(sizeof(blist_list));
|
||||
list->id = -1;
|
||||
list->name = NULL;
|
||||
list->desc = NULL;
|
||||
|
@ -183,8 +183,8 @@ klist_list *klist_list_init() {
|
|||
return list;
|
||||
}
|
||||
|
||||
klist_list *klist_list_init_from_sql(sqlite3_stmt *stmt) {
|
||||
klist_list *list = klist_list_init();
|
||||
blist_list *blist_list_init_from_sql(sqlite3_stmt *stmt) {
|
||||
blist_list *list = blist_list_init();
|
||||
list->id = sqlite3_column_int(stmt, 0);
|
||||
if (sqlite3_column_bytes(stmt, 1) > 0)
|
||||
list->name = (unsigned char *)strdup((char *)sqlite3_column_text(stmt, 1));
|
||||
|
@ -195,27 +195,27 @@ klist_list *klist_list_init_from_sql(sqlite3_stmt *stmt) {
|
|||
return list;
|
||||
}
|
||||
|
||||
klist_list *klist_list_get_by_id(const klist *ctx, u_int id) {
|
||||
blist_list *blist_list_get_by_id(const blist *ctx, u_int id) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_LIST];
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
sqlite3_reset(stmt);
|
||||
return klist_list_init_from_sql(stmt);
|
||||
return blist_list_init_from_sql(stmt);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
klist_list **klist_list_get_all_by_user(const klist *ctx, u_int user_id,
|
||||
blist_list **blist_list_get_all_by_user(const blist *ctx, u_int user_id,
|
||||
size_t *count) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_LISTS_BY_USER];
|
||||
sqlite3_bind_int(stmt, 1, user_id);
|
||||
klist_list **lists = NULL;
|
||||
blist_list **lists = NULL;
|
||||
size_t lists_len = 0;
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
klist_list **_lists = realloc(lists, ++lists_len * sizeof(klist_list *));
|
||||
blist_list **_lists = realloc(lists, ++lists_len * sizeof(blist_list *));
|
||||
if (_lists) {
|
||||
lists = _lists;
|
||||
lists[lists_len - 1] = klist_list_init_from_sql(stmt);
|
||||
lists[lists_len - 1] = blist_list_init_from_sql(stmt);
|
||||
}
|
||||
}
|
||||
if (count != NULL)
|
||||
|
@ -223,24 +223,24 @@ klist_list **klist_list_get_all_by_user(const klist *ctx, u_int user_id,
|
|||
return lists;
|
||||
}
|
||||
|
||||
klist_list *klist_list_get_by_user_and_name(const klist *ctx, u_int user_id,
|
||||
blist_list *blist_list_get_by_user_and_name(const blist *ctx, u_int user_id,
|
||||
const char *name) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_LISTS_BY_USER_NAME];
|
||||
klist_list *list = NULL;
|
||||
blist_list *list = NULL;
|
||||
sqlite3_bind_int(stmt, 1, user_id);
|
||||
sqlite3_bind_text(stmt, 2, name, -1, SQLITE_STATIC);
|
||||
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW)
|
||||
list = klist_list_init_from_sql(stmt);
|
||||
list = blist_list_init_from_sql(stmt);
|
||||
else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"failed to get lists for user: %s\n", sqlite3_errmsg(ctx->db));
|
||||
sqlite3_reset(stmt);
|
||||
return list;
|
||||
}
|
||||
|
||||
void klist_list_save(const klist *ctx, klist_list *list,
|
||||
const klist_user *user) {
|
||||
void blist_list_save(const blist *ctx, blist_list *list,
|
||||
const blist_user *user) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[ADD_LIST];
|
||||
int result = 0;
|
||||
if (list->id > -1)
|
||||
|
@ -251,7 +251,7 @@ void klist_list_save(const klist *ctx, klist_list *list,
|
|||
if ((result = sqlite3_step(stmt)) == SQLITE_ROW)
|
||||
list->id = sqlite3_column_int(stmt, 0);
|
||||
else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "failed to save list: %s - %d\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "failed to save list: %s - %d\n",
|
||||
sqlite3_errmsg(ctx->db), result);
|
||||
sqlite3_reset(stmt);
|
||||
|
||||
|
@ -262,23 +262,23 @@ void klist_list_save(const klist *ctx, klist_list *list,
|
|||
if (sqlite3_step(stmt) == SQLITE_DONE)
|
||||
sqlite3_reset(stmt);
|
||||
else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"failed to save user - list relation in join table: %s\n",
|
||||
sqlite3_errmsg(ctx->db));
|
||||
}
|
||||
}
|
||||
|
||||
void klist_list_delete(const klist *ctx, klist_list *list) {
|
||||
void blist_list_delete(const blist *ctx, blist_list *list) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[DEL_LIST];
|
||||
sqlite3_bind_int(stmt, 1, list->id);
|
||||
if (sqlite3_step(stmt) == SQLITE_DONE)
|
||||
sqlite3_reset(stmt);
|
||||
else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "failed to delete task: %s\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "failed to delete task: %s\n",
|
||||
sqlite3_errmsg(ctx->db));
|
||||
}
|
||||
|
||||
void klist_list_deinit(klist_list *list) {
|
||||
void blist_list_deinit(blist_list *list) {
|
||||
if (list->name)
|
||||
free(list->name);
|
||||
if (list->desc)
|
||||
|
@ -286,8 +286,8 @@ void klist_list_deinit(klist_list *list) {
|
|||
free(list);
|
||||
}
|
||||
|
||||
klist_task *klist_task_init() {
|
||||
klist_task *task = malloc(sizeof(klist_task));
|
||||
blist_task *blist_task_init() {
|
||||
blist_task *task = malloc(sizeof(blist_task));
|
||||
task->id = -1;
|
||||
task->name = NULL;
|
||||
task->desc = NULL;
|
||||
|
@ -296,8 +296,8 @@ klist_task *klist_task_init() {
|
|||
return task;
|
||||
}
|
||||
|
||||
klist_task *klist_task_init_from_sql(sqlite3_stmt *stmt) {
|
||||
klist_task *task = klist_task_init();
|
||||
blist_task *blist_task_init_from_sql(sqlite3_stmt *stmt) {
|
||||
blist_task *task = blist_task_init();
|
||||
task->id = sqlite3_column_int(stmt, 0);
|
||||
if (sqlite3_column_bytes(stmt, 1) > 0)
|
||||
task->name = (unsigned char *)strdup((char *)sqlite3_column_text(stmt, 1));
|
||||
|
@ -309,28 +309,28 @@ klist_task *klist_task_init_from_sql(sqlite3_stmt *stmt) {
|
|||
return task;
|
||||
}
|
||||
|
||||
klist_task *klist_task_get_by_id(const klist *ctx, u_int id) {
|
||||
blist_task *blist_task_get_by_id(const blist *ctx, u_int id) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_TASK];
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
sqlite3_reset(stmt);
|
||||
return klist_task_init_from_sql(stmt);
|
||||
return blist_task_init_from_sql(stmt);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
klist_task **klist_task_get_for_list(const klist *ctx, u_int list_id,
|
||||
blist_task **blist_task_get_for_list(const blist *ctx, u_int list_id,
|
||||
size_t *count) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_TASKS_FOR_LIST];
|
||||
sqlite3_bind_int(stmt, 1, list_id);
|
||||
klist_task **tasks = NULL;
|
||||
blist_task **tasks = NULL;
|
||||
size_t tasks_len = 0;
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
tasks_len++;
|
||||
klist_task **_tasks = realloc(tasks, tasks_len * sizeof(klist_task *));
|
||||
blist_task **_tasks = realloc(tasks, tasks_len * sizeof(blist_task *));
|
||||
if (_tasks) {
|
||||
tasks = _tasks;
|
||||
tasks[tasks_len - 1] = klist_task_init_from_sql(stmt);
|
||||
tasks[tasks_len - 1] = blist_task_init_from_sql(stmt);
|
||||
}
|
||||
}
|
||||
sqlite3_reset(stmt);
|
||||
|
@ -338,23 +338,23 @@ klist_task **klist_task_get_for_list(const klist *ctx, u_int list_id,
|
|||
return tasks;
|
||||
}
|
||||
|
||||
klist_task *klist_task_get_for_list_by_name(const klist *ctx, u_int list_id,
|
||||
blist_task *blist_task_get_for_list_by_name(const blist *ctx, u_int list_id,
|
||||
const char *name) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[GET_TASKS_FOR_LIST_BY_NAME];
|
||||
klist_task *task = NULL;
|
||||
blist_task *task = NULL;
|
||||
sqlite3_bind_int(stmt, 1, list_id);
|
||||
sqlite3_bind_text(stmt, 2, name, -1, SQLITE_STATIC);
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW)
|
||||
task = klist_task_init_from_sql(stmt);
|
||||
task = blist_task_init_from_sql(stmt);
|
||||
else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_WARNING,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_WARNING,
|
||||
"no tasks found for list %d, name '%s': %s\n", list_id, name,
|
||||
sqlite3_errmsg(ctx->db));
|
||||
sqlite3_reset(stmt);
|
||||
return task;
|
||||
}
|
||||
|
||||
void klist_task_save(const klist *ctx, klist_task *task) {
|
||||
void blist_task_save(const blist *ctx, blist_task *task) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[ADD_TASK];
|
||||
int result = 0;
|
||||
if (task->id > -1)
|
||||
|
@ -369,21 +369,21 @@ void klist_task_save(const klist *ctx, klist_task *task) {
|
|||
if ((result = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
task->id = sqlite3_column_int(stmt, 0);
|
||||
} else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "failed to save list: %s - %d\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "failed to save list: %s - %d\n",
|
||||
sqlite3_errmsg(ctx->db), result);
|
||||
sqlite3_reset(stmt);
|
||||
}
|
||||
void klist_task_delete(const klist *ctx, const klist_task *task) {
|
||||
void blist_task_delete(const blist *ctx, const blist_task *task) {
|
||||
sqlite3_stmt *stmt = ctx->stmts[DEL_TASK];
|
||||
sqlite3_bind_int(stmt, 1, task->id);
|
||||
if (sqlite3_step(stmt) == SQLITE_DONE)
|
||||
sqlite3_reset(stmt);
|
||||
else
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "failed to delete task: %s\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "failed to delete task: %s\n",
|
||||
sqlite3_errmsg(ctx->db));
|
||||
}
|
||||
|
||||
void klist_task_deinit(klist_task *task) {
|
||||
void blist_task_deinit(blist_task *task) {
|
||||
if (task->name)
|
||||
free(task->name);
|
||||
if (task->desc)
|
||||
|
@ -395,11 +395,12 @@ void klist_task_deinit(klist_task *task) {
|
|||
* printing
|
||||
*/
|
||||
|
||||
void klist_print_user(klist *ctx, const klist_user *user) {
|
||||
fprintf(ctx->log_ctx->log_target, ""
|
||||
"Name: %s\n"
|
||||
"has local account: %s\n",
|
||||
(char *)user->name, user->local_id > 0 ? "yes" : "no");
|
||||
void blist_print_user(blist *ctx, const blist_user *user) {
|
||||
fprintf(ctx->log_ctx->log_target,
|
||||
""
|
||||
"Name: %s\n"
|
||||
"has local account: %s\n",
|
||||
(char *)user->name, user->local_id > 0 ? "yes" : "no");
|
||||
}
|
||||
|
||||
void print_table_line(char *left, size_t cols, char *filler, size_t col_width,
|
||||
|
@ -416,17 +417,18 @@ void print_table_line(char *left, size_t cols, char *filler, size_t col_width,
|
|||
printf("%s\n", right);
|
||||
}
|
||||
|
||||
void klist_print_list(const klist *ctx, const klist_list *list,
|
||||
klist_task **tasks, size_t tasks_len) {
|
||||
void blist_print_list(const blist *ctx, const blist_list *list,
|
||||
blist_task **tasks, size_t tasks_len) {
|
||||
struct winsize w;
|
||||
int i = 0;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||
u_int width = w.ws_col > 0 ? w.ws_col : 40;
|
||||
for (i = 0; i < tasks_len; i++) fprintf(stderr, "%d, %s\n", i, (char*)tasks[i]->name);
|
||||
for (i = 0; i < tasks_len; i++)
|
||||
fprintf(stderr, "%d, %s\n", i, (char *)tasks[i]->name);
|
||||
|
||||
size_t stages_len = 0;
|
||||
klist_stage **stages =
|
||||
klist_stage_get_all_for_list(ctx, list->id, &stages_len);
|
||||
blist_stage **stages =
|
||||
blist_stage_get_all_for_list(ctx, list->id, &stages_len);
|
||||
|
||||
int max_stages_id = 0;
|
||||
for (i = 0; i < stages_len; i++)
|
||||
|
@ -530,7 +532,6 @@ void klist_print_list(const klist *ctx, const klist_list *list,
|
|||
printf(" ");
|
||||
if (j + 1 != stages_len)
|
||||
printf(" │");
|
||||
|
||||
}
|
||||
printf("│\n");
|
||||
}
|
||||
|
@ -540,6 +541,6 @@ void klist_print_list(const klist *ctx, const klist_list *list,
|
|||
|
||||
// cleanup
|
||||
for (i = 0; i < stages_len; i++)
|
||||
klist_stage_deinit(stages[i]);
|
||||
blist_stage_deinit(stages[i]);
|
||||
free(stages);
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
* can be no id conflicts
|
||||
*/
|
||||
|
||||
const char *klist_sql_get(const enum KLIST_SQL sql) {
|
||||
const char *blist_sql_get(const enum BLIST_SQL sql) {
|
||||
switch (sql) {
|
||||
case INIT:
|
||||
return "create table if not exists users (\n"
|
||||
|
@ -123,7 +123,7 @@ const char *klist_sql_get(const enum KLIST_SQL sql) {
|
|||
return "update tasks set desc = ?1 where id = ?2";
|
||||
case DEL_TASK:
|
||||
return "delete from tasks where id = ?1";
|
||||
case _KLIST_SQL_COUNT:
|
||||
case _BLIST_SQL_COUNT:
|
||||
return "";
|
||||
}
|
||||
return "";
|
|
@ -12,18 +12,18 @@
|
|||
* command parsing
|
||||
*/
|
||||
|
||||
klist *klist_init(char *db) {
|
||||
klist *ctx = malloc(sizeof(klist));
|
||||
ctx->stmts = malloc(_KLIST_SQL_COUNT * sizeof(sqlite3_stmt *));
|
||||
blist *blist_init(char *db) {
|
||||
blist *ctx = malloc(sizeof(blist));
|
||||
ctx->stmts = malloc(_BLIST_SQL_COUNT * sizeof(sqlite3_stmt *));
|
||||
ctx->error = 0;
|
||||
ctx->log_ctx = malloc(sizeof(klist_logging_ctx));
|
||||
klist_sql_prepare(ctx, db);
|
||||
ctx->log_ctx = malloc(sizeof(blist_logging_ctx));
|
||||
blist_sql_prepare(ctx, db);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
void klist_deinit(klist *ctx) {
|
||||
void blist_deinit(blist *ctx) {
|
||||
int i = 0;
|
||||
for (; i < _KLIST_SQL_COUNT; i++)
|
||||
for (; i < _BLIST_SQL_COUNT; i++)
|
||||
sqlite3_finalize(ctx->stmts[i]);
|
||||
sqlite3_close(ctx->db);
|
||||
if (ctx->log_ctx) {
|
||||
|
@ -40,51 +40,51 @@ void klist_deinit(klist *ctx) {
|
|||
* sql preparations
|
||||
*/
|
||||
|
||||
void klist_sql_prepare(klist *ctx, char *db) {
|
||||
void blist_sql_prepare(blist *ctx, char *db) {
|
||||
if (!db)
|
||||
klist_print(
|
||||
ctx->log_ctx, KLIST_LOG_WARNING,
|
||||
blist_print(
|
||||
ctx->log_ctx, BLIST_LOG_WARNING,
|
||||
"Database will be in-memory only, changes are not persistent!\n");
|
||||
if (sqlite3_open(db ? db : ":memory:", &ctx->db) != SQLITE_OK) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR, "Can't open database: %s\n",
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR, "Can't open database: %s\n",
|
||||
sqlite3_errmsg(ctx->db));
|
||||
return;
|
||||
}
|
||||
char *errmsg = NULL;
|
||||
sqlite3_exec(ctx->db, klist_sql_get(INIT), NULL, NULL, &errmsg);
|
||||
sqlite3_exec(ctx->db, blist_sql_get(INIT), NULL, NULL, &errmsg);
|
||||
if (errmsg) {
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_ERROR,
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_ERROR,
|
||||
"Database initialization failed (%s), expect issues.\n",
|
||||
sqlite3_errmsg(ctx->db));
|
||||
sqlite3_free(errmsg);
|
||||
}
|
||||
int i = 0;
|
||||
for (; i < _KLIST_SQL_COUNT; i++)
|
||||
if (sqlite3_prepare(ctx->db, klist_sql_get(i), -1, &ctx->stmts[i], NULL) !=
|
||||
for (; i < _BLIST_SQL_COUNT; i++)
|
||||
if (sqlite3_prepare(ctx->db, blist_sql_get(i), -1, &ctx->stmts[i], NULL) !=
|
||||
SQLITE_OK)
|
||||
klist_print(ctx->log_ctx, KLIST_LOG_WARNING,
|
||||
"sqlite3_prepare for '%s' failed: %s\n", klist_sql_get(i),
|
||||
blist_print(ctx->log_ctx, BLIST_LOG_WARNING,
|
||||
"sqlite3_prepare for '%s' failed: %s\n", blist_sql_get(i),
|
||||
sqlite3_errmsg(ctx->db));
|
||||
}
|
||||
|
||||
void klist_sql_seed(klist *ctx) {}
|
||||
void blist_sql_seed(blist *ctx) {}
|
||||
|
||||
/*
|
||||
* assuring things
|
||||
*/
|
||||
|
||||
bool klist_assure_user(const klist *ctx, const __uid_t id, const char *name) {
|
||||
klist_user *user = klist_user_get_by_local(ctx, id);
|
||||
bool blist_assure_user(const blist *ctx, const __uid_t id, const char *name) {
|
||||
blist_user *user = blist_user_get_by_local(ctx, id);
|
||||
bool created = false;
|
||||
if (!user) {
|
||||
user = klist_user_init();
|
||||
user = blist_user_init();
|
||||
user->name = malloc((strlen(name) + 1) * sizeof(char));
|
||||
strcpy((char *)user->name, name);
|
||||
user->local_id = id;
|
||||
klist_user_save(ctx, user);
|
||||
blist_user_save(ctx, user);
|
||||
created = true;
|
||||
}
|
||||
klist_user_deinit(user);
|
||||
blist_user_deinit(user);
|
||||
return created;
|
||||
}
|
||||
|
||||
|
@ -92,20 +92,20 @@ bool klist_assure_user(const klist *ctx, const __uid_t id, const char *name) {
|
|||
* printing methods
|
||||
*/
|
||||
|
||||
void klist_print(klist_logging_ctx *ctx, KLIST_LOG_LEVEL log_level,
|
||||
void blist_print(blist_logging_ctx *ctx, BLIST_LOG_LEVEL log_level,
|
||||
const char *format, ...) {
|
||||
if (ctx->log_level >= log_level) {
|
||||
switch (log_level) {
|
||||
case KLIST_LOG_DEBUG:
|
||||
case BLIST_LOG_DEBUG:
|
||||
fprintf(ctx->log_target, "[\x1B[34mDBG\x1B[0m] ");
|
||||
break;
|
||||
case KLIST_LOG_INFO:
|
||||
case BLIST_LOG_INFO:
|
||||
fprintf(ctx->log_target, "[\x1B[36mINF\x1B[0m] ");
|
||||
break;
|
||||
case KLIST_LOG_WARNING:
|
||||
case BLIST_LOG_WARNING:
|
||||
fprintf(ctx->log_target, "[\x1B[33mWRN\x1B[0m] ");
|
||||
break;
|
||||
case KLIST_LOG_ERROR:
|
||||
case BLIST_LOG_ERROR:
|
||||
fprintf(ctx->log_target, "[\x1B[31mERR\x1B[0m] ");
|
||||
break;
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
add_library(libklist models.c sql.c util.c)
|
||||
target_include_directories(libklist PUBLIC include)
|
||||
target_link_libraries(libklist sqlite3)
|
|
@ -1,78 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
struct klist_user {
|
||||
ssize_t id;
|
||||
unsigned char *name;
|
||||
ssize_t local_id;
|
||||
};
|
||||
typedef struct klist_user klist_user;
|
||||
|
||||
klist_user *klist_user_init();
|
||||
klist_user *klist_user_init_from_sql(sqlite3_stmt *);
|
||||
klist_user *klist_user_get_by_id(const klist *, u_int);
|
||||
klist_user *klist_user_get_by_local(const klist *, u_int);
|
||||
klist_user *klist_user_get_by_discord(const klist *, u_int);
|
||||
klist_user *klist_user_get_by_google(const klist *, u_int);
|
||||
void klist_user_save(const klist *, klist_user *);
|
||||
void klist_user_delete(const klist *, const klist_user *);
|
||||
void klist_user_deinit(klist_user *);
|
||||
|
||||
struct klist_list {
|
||||
ssize_t id;
|
||||
unsigned char *name;
|
||||
unsigned char *desc;
|
||||
bool is_preset;
|
||||
};
|
||||
typedef struct klist_list klist_list;
|
||||
|
||||
klist_list *klist_list_init();
|
||||
klist_list *klist_list_init_from_sql(sqlite3_stmt *);
|
||||
klist_list *klist_list_get_by_id(const klist *, u_int);
|
||||
klist_list **klist_list_get_all_by_user(const klist *, u_int, size_t *);
|
||||
klist_list *klist_list_get_by_user_and_name(const klist *, u_int, const char *);
|
||||
void klist_list_save(const klist *, klist_list *, const klist_user *);
|
||||
void klist_list_delete(const klist *, klist_list *);
|
||||
void klist_list_deinit(klist_list *);
|
||||
|
||||
struct klist_stage {
|
||||
ssize_t id;
|
||||
unsigned char *name;
|
||||
unsigned char *desc;
|
||||
ssize_t list_id;
|
||||
};
|
||||
typedef struct klist_stage klist_stage;
|
||||
|
||||
klist_stage *klist_stage_init();
|
||||
klist_stage *klist_stage_init_from_sql(sqlite3_stmt *);
|
||||
klist_stage *klist_stage_get_by_id(const klist *, u_int);
|
||||
klist_stage **klist_stage_get_all_for_list(const klist *, u_int, size_t *);
|
||||
void klist_stage_save(const klist *, klist_stage *);
|
||||
void klist_stage_delete(const klist *, const klist_stage *);
|
||||
void klist_stage_deinit(klist_stage *);
|
||||
|
||||
struct klist_task {
|
||||
ssize_t id;
|
||||
unsigned char *name;
|
||||
unsigned char *desc;
|
||||
int stage_id;
|
||||
time_t due;
|
||||
int target_stage;
|
||||
};
|
||||
typedef struct klist_task klist_task;
|
||||
|
||||
klist_task *klist_task_init();
|
||||
klist_task *klist_task_init_from_sql(sqlite3_stmt *);
|
||||
klist_task *klist_task_get_by_id(const klist *, u_int);
|
||||
klist_task **klist_task_get_for_list(const klist *, u_int, size_t *);
|
||||
klist_task *klist_task_get_for_list_by_name(const klist *, u_int, const char *);
|
||||
void klist_task_save(const klist *, klist_task *);
|
||||
void klist_task_delete(const klist *, const klist_task *);
|
||||
void klist_task_deinit(klist_task *);
|
||||
|
||||
void klist_print_user(klist *, const klist_user *);
|
||||
void klist_print_list(const klist *, const klist_list *, klist_task **, size_t);
|
||||
void klist_print_task(klist *, klist_task **, size_t);
|
|
@ -1,57 +0,0 @@
|
|||
#pragma once
|
||||
#include <sqlite3.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/*
|
||||
* command parsing
|
||||
*/
|
||||
|
||||
enum KLIST_LOG_LEVEL {
|
||||
KLIST_LOG_ERROR,
|
||||
KLIST_LOG_WARNING,
|
||||
KLIST_LOG_INFO,
|
||||
KLIST_LOG_DEBUG
|
||||
};
|
||||
typedef enum KLIST_LOG_LEVEL KLIST_LOG_LEVEL;
|
||||
|
||||
struct klist_logging_ctx {
|
||||
KLIST_LOG_LEVEL log_level;
|
||||
FILE *log_target;
|
||||
};
|
||||
|
||||
typedef struct klist_logging_ctx klist_logging_ctx;
|
||||
|
||||
struct klist {
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt **stmts;
|
||||
int error;
|
||||
klist_logging_ctx *log_ctx;
|
||||
};
|
||||
typedef struct klist klist;
|
||||
|
||||
klist *klist_init(char *db);
|
||||
void klist_deinit(klist *list);
|
||||
|
||||
/*
|
||||
* sql preparations
|
||||
*/
|
||||
|
||||
void klist_sql_prepare(klist *, char *);
|
||||
|
||||
/*
|
||||
* assuring things
|
||||
*/
|
||||
|
||||
bool klist_assure_user(const klist *, __uid_t, const char *);
|
||||
void klist_assure_list(klist *, char *);
|
||||
void klist_assure_task(klist *, char *);
|
||||
|
||||
/*
|
||||
* printing methods
|
||||
*
|
||||
* json printing will be implemented eventually
|
||||
*/
|
||||
|
||||
void klist_print(klist_logging_ctx *, KLIST_LOG_LEVEL, const char *, ...);
|
Loading…
Add table
Reference in a new issue