diff --git a/src/daemon.c b/src/daemon.c index d1b3eda..7129187 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include "log/log.h" #include "main/include/db.h" diff --git a/src/main.c b/src/main.c index f7a5de6..f4fb939 100644 --- a/src/main.c +++ b/src/main.c @@ -148,9 +148,20 @@ int reschedule(int argc, char* argv[]) { return 0; } -int list_tasks(bool show_completed) { - task_list_t* tasks = get_task_list(show_completed); - print_task_list(tasks, show_completed); // show_completed: true would work as all the tasks are not loaded if false +int list_tasks(int argc, char* argv[]) { + bool show_completed = false; + char* category = NULL; + + for (int i=2; i < argc; i++) { + if (!strcmp(argv[i], "-a")) { + show_completed = true; + } else { + category = argv[i]; + } + } + + task_list_t* tasks = get_task_list(category, show_completed); + print_task_list(tasks, !category, show_completed); // show_completed: true would work as all the tasks are not loaded if false free_task_list(tasks); return 0; @@ -251,7 +262,7 @@ int main(int argc, char* argv[]) { } else if (!strcmp(argv[1], "reschedule")) { return reschedule(argc, argv); } else if (!strcmp(argv[1], "list")) { - return list_tasks(argc > 2 && !strcmp(argv[2], "-a")); + return list_tasks(argc, argv); } else if (!strcmp(argv[1], "info")) { return info(argc, argv); } else if (!strcmp(argv[1], "done")) { diff --git a/src/main/db.c b/src/main/db.c index 06cb89b..097ace2 100644 --- a/src/main/db.c +++ b/src/main/db.c @@ -15,6 +15,15 @@ Fonctions d'interaction avec la base de données #include "include/db.h" +#define sqlCheck(ret) { sqlAssert(ret, db, __FILE__, __LINE__); } +void sqlAssert(int ret, sqlite3* db, char* file, int line) { + if (ret != SQLITE_OK) { + printf( "%s:%d, error %d: %s\n", file, line, ret, sqlite3_errmsg(db)); + exit(1); + } +} + + char* get_db_path() { #ifndef DB_FILE //! We should check that $HOME/.config already exists even if it should be the case @@ -97,28 +106,28 @@ int add_task(task_t t) { sqlite3* db = get_db(); sqlite3_stmt* stmt; - sqlite3_prepare_v2(db, "INSERT INTO tasks (id, text, category, done, due_to) VALUES (?1, ?2, ?3, ?4, ?5);", -1, &stmt, 0); + sqlCheck( sqlite3_prepare_v2(db, "INSERT INTO tasks (id, text, category, done, due_to) VALUES (?1, ?2, ?3, ?4, ?5);", -1, &stmt, 0) ); char str_id[10]; sprintf(str_id, "%d", (int)t.id); - sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, t.text, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 3, t.category, -1, SQLITE_STATIC); + sqlCheck( sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC) ); + sqlCheck( sqlite3_bind_text(stmt, 2, t.text, -1, SQLITE_STATIC) ); + sqlCheck( sqlite3_bind_text(stmt, 3, t.category, -1, SQLITE_STATIC) ); char str_done[2]; // Just a boolean sprintf(str_done, "%d", t.done); - sqlite3_bind_text(stmt, 4, str_done, -1, SQLITE_STATIC); + sqlCheck( sqlite3_bind_text(stmt, 4, str_done, -1, SQLITE_STATIC) ); char str_due_to[15]; sprintf(str_due_to, "%ld", t.due_to); - sqlite3_bind_text(stmt, 5, str_due_to, -1, SQLITE_STATIC); + sqlCheck( sqlite3_bind_text(stmt, 5, str_due_to, -1, SQLITE_STATIC) ); sqlite3_step(stmt); - sqlite3_finalize(stmt); - sqlite3_close(db); + sqlCheck( sqlite3_finalize(stmt) ); + sqlCheck( sqlite3_close(db) ); notify_change(); return 0; @@ -128,37 +137,26 @@ int update_task(task_t t) { sqlite3* db = get_db(); sqlite3_stmt* stmt; - int ret = sqlite3_prepare_v2(db, "UPDATE tasks SET text=?2, done=?3, due_to=?4 WHERE id=?1;", -1, &stmt, 0); + sqlCheck( sqlite3_prepare_v2(db, "UPDATE tasks SET text=?2, done=?3, due_to=?4 WHERE id=?1;", -1, &stmt, 0) ); - if (ret != SQLITE_OK) { - printf("(get_task) failure fetching data\n"); - return 1; - } char str_id[10]; sprintf(str_id, "%d", (int)t.id); - sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, t.text, -1, SQLITE_STATIC); + sqlCheck( sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC) ); + sqlCheck( sqlite3_bind_text(stmt, 2, t.text, -1, SQLITE_STATIC) ); char str_done[2]; // Just a boolean sprintf(str_done, "%d", t.done); - sqlite3_bind_text(stmt, 3, str_done, -1, SQLITE_STATIC); + sqlCheck( sqlite3_bind_text(stmt, 3, str_done, -1, SQLITE_STATIC) ); char str_due_to[15]; sprintf(str_due_to, "%ld", t.due_to); - sqlite3_bind_text(stmt, 4, str_due_to, -1, SQLITE_STATIC); + sqlCheck( sqlite3_bind_text(stmt, 4, str_due_to, -1, SQLITE_STATIC) ); - - ret = sqlite3_step(stmt); - - sqlite3_finalize(stmt); - sqlite3_close(db); + sqlCheck( sqlite3_finalize(stmt) ); + sqlCheck( sqlite3_close(db) ); notify_change(); - if (ret != SQLITE_DONE) { - printf("Return value: %d\n", ret); - return 1; - } return 0; } @@ -167,21 +165,16 @@ void delete_task(int id) { sqlite3* db = get_db(); sqlite3_stmt* stmt; - int ret = sqlite3_prepare_v2(db, "DELETE FROM tasks WHERE id=?1;", -1, &stmt, 0); - - if (ret != SQLITE_OK) { - printf("(get_task) failure fetching data\n"); - exit(1); - } + sqlCheck( sqlite3_prepare_v2(db, "DELETE FROM tasks WHERE id=?1;", -1, &stmt, 0) ); char str_id[10]; sprintf(str_id, "%d", id); - sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC); + sqlCheck( sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC) ); sqlite3_step(stmt); - sqlite3_finalize(stmt); - sqlite3_close(db); + sqlCheck( sqlite3_finalize(stmt) ); + sqlCheck( sqlite3_close(db) ); notify_change(); } @@ -192,19 +185,13 @@ void get_task(int id, task_t* t) { sqlite3* db = get_db(); sqlite3_stmt* stmt; - int ret = sqlite3_prepare_v2(db, "SELECT id, text, done, due_to FROM tasks WHERE id=?1;", -1, &stmt, 0); - - if (ret != SQLITE_OK) { - printf("(get_task) failure fetching data\n"); - exit(1); - } - + sqlCheck( sqlite3_prepare_v2(db, "SELECT id, text, done, due_to, category FROM tasks WHERE id=?1;", -1, &stmt, NULL) ); char str_id[10]; sprintf(str_id, "%d", id); - sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC); + sqlCheck( sqlite3_bind_text(stmt, 1, str_id, -1, SQLITE_STATIC) ); - ret = sqlite3_step(stmt); + int ret = sqlite3_step(stmt); if (ret == SQLITE_ROW) { t->id = strtol((char*)sqlite3_column_text(stmt, 0), NULL, 10); t->done = strtol((char*)sqlite3_column_text(stmt, 2), NULL, 10); @@ -213,24 +200,29 @@ void get_task(int id, task_t* t) { char* text = (char*)sqlite3_column_text(stmt, 1); t->text = malloc(sizeof(char)*(strlen(text)+1)); strcpy(t->text, text); + + char* category = (char*)sqlite3_column_text(stmt, 4); + t->category = malloc(sizeof(char)*(strlen(category)+1)); + strcpy(t->category, category); } - sqlite3_finalize(stmt); - sqlite3_close(db); + sqlCheck( sqlite3_finalize(stmt) ); + sqlCheck( sqlite3_close(db) ); // t->id will be (-1) if not found } -task_list_t* get_task_list(bool include_completed) { +task_list_t* get_task_list(char* input_category, bool include_completed) { sqlite3* db = get_db(); sqlite3_stmt *stmt; - int ret = sqlite3_prepare_v2(db, "SELECT id, text, done, due_to FROM tasks;", -1, &stmt, NULL); - - if (ret != SQLITE_OK) { - printf("(get_task) failure fetching data\n"); - return NULL; + if (!input_category) { + sqlCheck( sqlite3_prepare_v2(db, "SELECT id, text, done, due_to, category FROM tasks;", -1, &stmt, NULL) ); + } else { + sqlCheck( sqlite3_prepare_v2(db, "SELECT id, text, done, due_to, category FROM tasks WHERE category=?1;", -1, &stmt, NULL) ); + sqlCheck( sqlite3_bind_text(stmt, 1, input_category, -1, SQLITE_STATIC) ); } + task_list_t* list = NULL; while (sqlite3_step(stmt) == SQLITE_ROW) { @@ -245,6 +237,10 @@ task_list_t* get_task_list(bool include_completed) { t.text = malloc(sizeof(char)*(strlen(text)+1)); strcpy(t.text, text); + char* category = (char*)sqlite3_column_text(stmt, 4); + t.category = malloc(sizeof(char)*(strlen(category)+1)); + strcpy(t.category, category); + task_list_t* cur = malloc(sizeof(task_list_t)); // Add the parsed task to the beginning of the list cur->task = t; cur->next = list; @@ -252,8 +248,8 @@ task_list_t* get_task_list(bool include_completed) { } } - sqlite3_finalize(stmt); - sqlite3_close(db); + sqlCheck( sqlite3_finalize(stmt) ); + sqlCheck( sqlite3_close(db) ); return list; } diff --git a/src/main/include/config.h b/src/main/include/config.h index 4eec098..863a87b 100644 --- a/src/main/include/config.h +++ b/src/main/include/config.h @@ -1,7 +1,7 @@ #ifndef DEF_CONFIG_H #define DEF_CONFIG_H -#define VERSION "1.1.0" +#define VERSION "1.2.0" // By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here //#define DB_FILE "takl.sqlite3" diff --git a/src/main/include/db.h b/src/main/include/db.h index bc1ab5f..c46b787 100644 --- a/src/main/include/db.h +++ b/src/main/include/db.h @@ -40,7 +40,7 @@ void get_task(int id, task_t* t); /* Renvoie la liste des tâches de la base de données */ -task_list_t* get_task_list(bool include_completed); +task_list_t* get_task_list(char* input_category, bool include_completed); /* Renvoie un identifiant de tâche encore non utilisé diff --git a/src/main/include/tasks.h b/src/main/include/tasks.h index d8e3c8a..2087b52 100644 --- a/src/main/include/tasks.h +++ b/src/main/include/tasks.h @@ -3,6 +3,12 @@ #ifndef DEF_TASKS_H #define DEF_TASKS_H +/* +Sépare la chaîne d'entrée en deux chaînes, l'une avant, l'autre après le premier ':'. +Si il n'y a pas de ':', out_category est NULL +*/ +void parse_input(char* in_text, char** out_text, char** out_category); + /* Renvoie une tâche avec un identifiant aléatoire valide */ @@ -16,7 +22,7 @@ void print_task(task_t task); /* Affiche une liste de tâches de manière compacte (et par catégorie: urgent, à faire, [complétée]) */ -void print_task_list(task_list_t* list, bool show_completed); +void print_task_list(task_list_t* list, bool show_category, bool show_completed); /* Libère la mémoire allouée à une liste de tâches diff --git a/src/main/tasks.c b/src/main/tasks.c index 3d89c54..1adf2a6 100644 --- a/src/main/tasks.c +++ b/src/main/tasks.c @@ -45,7 +45,6 @@ task_t create_task(char* text, time_t due_to) { task.category = NULL; parse_input(text, &(task.text), &(task.category)); - printf("task_id: %d\n", task.id); return task; } @@ -53,6 +52,9 @@ task_t create_task(char* text, time_t due_to) { void print_task(task_t task) { printf(YELLOW "=== Tâche " BOLD "[%d]" RESET YELLOW " ===\n" RESET, task.id); + if (task.category) { + printf(BLUE "%s:" RESET, task.category); + } printf(BOLD "%s\n" RESET, task.text); printf("Statut: "); @@ -81,13 +83,21 @@ void print_task(task_t task) { } -void print_task_list(task_list_t* list, bool show_completed) { +void print_single_task(task_t t, bool show_category, char* color) { + if (t.category && show_category) { + printf(BOLD "%s[%d]" RESET " " BLUE "%s:" RESET "%s\n", color, t.id, t.category, t.text); + } else { + printf(BOLD "%s[%d]" RESET " %s\n", color, t.id, t.text); + } +} + +void print_task_list(task_list_t* list, bool show_category, bool show_completed) { task_list_t* cur = list; time_t now = time(0); while (cur) { // Show not completed red tasks first task_t t = cur->task; if (!t.done && difftime(now, t.due_to) >= 0 && t.due_to != 0) { - printf(BOLD RED "[%d]" RESET " %s\n", t.id, t.text); // Task not completed but should be ! + print_single_task(t, show_category, RED); // Task not completed but should be ! } cur = cur->next; } @@ -96,7 +106,7 @@ void print_task_list(task_list_t* list, bool show_completed) { while (cur) { // Show not completed but not due_to now then task_t t = cur->task; if (!t.done && (difftime(now, t.due_to) <= 0 || t.due_to == 0)) { - printf(BOLD YELLOW "[%d]" RESET " %s\n", t.id, t.text); // Task not completed but fine + print_single_task(t, show_category, YELLOW); // Task not completed but fine } cur = cur->next; } @@ -106,7 +116,7 @@ void print_task_list(task_list_t* list, bool show_completed) { while (cur) { // Show completed task_t t = cur->task; if (t.done) { - printf(BOLD GREEN "[%d]" RESET " %s\n", t.id, t.text); // Task already done + print_single_task(t, show_category, GREEN); // Task already done } cur = cur->next; }