diff --git a/Makefile b/Makefile index 80e4f88..64e3c29 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ FLAGS = -g -Wall -Wextra -DLOG_USE_COLOR `pkg-config --cflags --libs libnotify` -LD_FLAGS = -lsqlite3 +LD_FLAGS = -lsqlite3 -lpthread $(shell mkdir -p out) @@ -17,10 +17,10 @@ out/%.o: src/main/%.c out/%.o: src/log/%.c $(CC) -c $(FLAGS) $^ -o $@ -takl: out/main.o out/db.o out/tasks.o +takl: out/main.o out/db.o out/tasks.o out/utils.o $(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@ -takl-daemon: out/daemon.o out/db.o out/tasks.o out/log.o +takl-daemon: out/daemon.o out/db.o out/tasks.o out/utils.o out/log.o $(CC) $(FLAGS) $(LD_FLAGS) $^ -o $@ diff --git a/src/main/include/config.h b/src/main/include/config.h index a979e96..4739434 100644 --- a/src/main/include/config.h +++ b/src/main/include/config.h @@ -6,6 +6,9 @@ // By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here //#define DB_FILE "takl.sqlite3" +// By default, /tmp/takl.$USER is used. You can change this behaviour here +//#define SOCKET_FILE "takl.sock" + #define MAX_TASK_ID 10000 // max is set to MAX_TASK_ID-1 #define NEW_TASK_ID_MAX_RETRIES 10000 // number of retries before giving up diff --git a/src/main/include/utils.h b/src/main/include/utils.h new file mode 100644 index 0000000..61187a9 --- /dev/null +++ b/src/main/include/utils.h @@ -0,0 +1,14 @@ +#include + +#include "struct.h" + + +/* +Renvoie le chemin d'accès au socket (/tmp/takl.$USER sauf si défini différemment dans la config) +*/ +char* get_socket_path(); + +/* +Renvoie l'élément arrivant dans le moins de temps d'une liste de tâches. +*/ +task_t next_task(task_list_t* tasks); \ No newline at end of file diff --git a/src/main/utils.c b/src/main/utils.c new file mode 100644 index 0000000..9fca405 --- /dev/null +++ b/src/main/utils.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +#include "include/config.h" +#include "include/struct.h" + +char* get_socket_path() { + #ifndef SOCKET_FILE + char* base_path = "/tmp/takl"; + + char* username = getenv("USER"); + assert(username != NULL); + + char* socket_path = malloc(sizeof(char)*(strlen(base_path)+strlen(username)+1)); + sprintf(socket_path, "%s.%s", base_path, username); + + return socket_path; + #else + char* socket_path = malloc(sizeof(char)*(strlen(SOCKET_FILE)+1)); + memcpy(socket_path, SOCKET_FILE, sizeof(char)*(strlen(SOCKET_FILE)+1)); + + return socket_path; + #endif +} + + +task_t next_task(task_list_t* tasks) { + time_t now = time(0); + + task_t next; + next.due_to = 0; + + task_list_t* cur = tasks; + + while (cur) { + task_t t = cur->task; + + if (!t.done && (difftime(now, t.due_to) <= 0 || t.due_to == 0)) { // La tâche a une échéance, qui n'est pas passée + if (next.due_to == 0 || difftime(next.due_to, t.due_to) >= 0) { + next = t; + } + } + + cur = cur->next; + } + + return next; +} \ No newline at end of file