Fixed the "mark" command to mark *all* the dependencies as done if the
[ctodo.git] / list.c
blobda934bf68a5f0a61393f012e22412fa377734ae0
1 #include <list.h>
2 #include <sqlite3.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <time.h>
7 void list_help() {
8 printf(" Usage: list\n");
9 printf(" Lists all the tasks and their children.\n");
12 int print_children(sqlite3 *db, int parent, int depth) {
13 sqlite3_stmt *statement;
14 char *query;
15 int retVal;
17 if (parent == -1) {
18 query = sqlite3_mprintf("SELECT * FROM todo WHERE (parent IS NULL) ORDER BY key ASC;");
19 } else {
20 query = sqlite3_mprintf("SELECT * FROM todo WHERE (parent = %d) ORDER BY key ASC;", parent);
23 retVal = sqlite3_prepare_v2(db, query, -1, &statement, NULL);
24 if (retVal != SQLITE_OK) {
25 printf("An error occurred while trying to fetch the todo's from the database: %s\n", sqlite3_errmsg(db));
26 sqlite3_finalize(statement);
27 return 1;
30 while ((retVal = sqlite3_step(statement)) == SQLITE_ROW) {
31 time_t startDate, dueDate;
32 const unsigned char *hasDueDate = sqlite3_column_text(statement, 3);
33 startDate = (time_t) sqlite3_column_int(statement, 2);
34 printf("%-*sTodo #%d: %s\n", depth*2, "", sqlite3_column_int(statement, 0), sqlite3_column_text(statement, 1));
35 printf("%-*sCreated on: %s", depth*2, "", ctime(&startDate));
36 if (hasDueDate) {
37 dueDate = (time_t) sqlite3_column_int(statement, 3);
38 printf("%-*sDue on: %s", depth*2, "", ctime(&dueDate));
39 } else {
40 printf("%-*sNo due date.\n", depth*2, "");
42 printf("\n");
43 print_children(db, sqlite3_column_int(statement, 0), depth+1);
46 sqlite3_free(query);
47 sqlite3_finalize(statement);
49 return 0;
52 int list(sqlite3 *db, MArray tokens) {
53 if (tokens->len == 2 && !strcmp(myarray_get(tokens, char *, 1), "help")) {
54 list_help();
55 return 0;
56 } else if (tokens->len > 1) {
57 printf("Invalid amount of arguments.\n");
58 list_help();
59 return 0;
62 return print_children(db, -1, 0);