From d779c996149b746d300bfa0ab9b9e49cb226ff72 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Sun, 6 Jun 2010 01:14:11 +0000 Subject: [PATCH] Implemented priorities --- src/callbacks.c | 13 +++++++++++ src/callbacks.h | 2 ++ src/task.c | 14 ++++++++++++ src/task.h | 2 ++ src/todo.c | 60 ++++++++++++++++++++++++++++++++++++++++++++----- src/todo.h | 10 +++++++++ 6 files changed, 95 insertions(+), 6 deletions(-) diff --git a/src/callbacks.c b/src/callbacks.c index 81dc049..36a7aa6 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -156,6 +156,19 @@ void on_task_done_toggled(GtkCellRendererToggle * renderer, gchar * path, } +/* on_task_priority_edited */ +void on_task_priority_edited(GtkCellRendererText * renderer, gchar * path, + gchar * priority, gpointer data) +{ + Todo * todo = data; + GtkTreePath * treepath; + + treepath = gtk_tree_path_new_from_string(path); + todo_task_set_priority(todo, treepath, priority); + gtk_tree_path_free(treepath); +} + + /* on_task_title_edited */ void on_task_title_edited(GtkCellRendererText * renderer, gchar * path, gchar * title, gpointer data) diff --git a/src/callbacks.h b/src/callbacks.h index e68c141..aa537b4 100644 --- a/src/callbacks.h +++ b/src/callbacks.h @@ -48,6 +48,8 @@ void on_preferences(gpointer data); /* view */ void on_task_done_toggled(GtkCellRendererToggle * renderer, gchar * path, gpointer data); +void on_task_priority_edited(GtkCellRendererText * renderer, gchar * path, + gchar * priority, gpointer data); void on_task_title_edited(GtkCellRendererText * renderer, gchar * path, gchar * title, gpointer data); diff --git a/src/task.c b/src/task.c index 29a5b99..56e6967 100644 --- a/src/task.c +++ b/src/task.c @@ -101,6 +101,13 @@ char const * task_get_filename(Task * task) } +/* task_get_priority */ +char const * task_get_priority(Task * task) +{ + return config_get(task->config, NULL, "priority"); +} + + /* task_get_title */ char const * task_get_title(Task * task) { @@ -128,6 +135,13 @@ int task_set_filename(Task * task, char const * filename) } +/* task_set_priority */ +int task_set_priority(Task * task, char const * priority) +{ + return config_set(task->config, NULL, "priority", priority); +} + + /* task_set_title */ int task_set_title(Task * task, char const * title) { diff --git a/src/task.h b/src/task.h index 1aae711..78b2f05 100644 --- a/src/task.h +++ b/src/task.h @@ -33,10 +33,12 @@ void task_delete(Task * task); /* accessors */ int task_get_done(Task * task); char const * task_get_filename(Task * task); +char const * task_get_priority(Task * task); char const * task_get_title(Task * task); int task_set_done(Task * task, int done); int task_set_filename(Task * task, char const * filename); +int task_set_priority(Task * task, char const * priority); int task_set_title(Task * task, char const * title); diff --git a/src/todo.c b/src/todo.c index e51a7fd..5be889f 100644 --- a/src/todo.c +++ b/src/todo.c @@ -43,6 +43,7 @@ struct _Todo GtkWidget * window; GtkWidget * scrolled; GtkListStore * store; + GtkListStore * priorities; GtkWidget * view; GtkWidget * statusbar; GtkWidget * about; @@ -52,7 +53,7 @@ struct _Todo /* constants */ enum { TD_COL_TASK, TD_COL_DONE, TD_COL_TITLE, TD_COL_START, TD_COL_DISPLAY_START, TD_COL_END, TD_COL_DISPLAY_END, TD_COL_PRIORITY, - TD_COL_DISPLAY_PRIORITY, TD_COL_CATEGORY }; + TD_COL_CATEGORY }; #define TD_COL_LAST TD_COL_CATEGORY #define TD_NUM_COLS (TD_COL_LAST + 1) @@ -70,10 +71,22 @@ static struct on_task_title_edited) }, { TD_COL_DISPLAY_START, N_("Beginning"), TD_COL_START, NULL }, { TD_COL_DISPLAY_END, N_("Completion"), TD_COL_END, NULL }, - { TD_COL_DISPLAY_PRIORITY, N_("Priority"), TD_COL_PRIORITY, NULL }, { 0, NULL, 0, NULL } }; +static struct +{ + unsigned int priority; + char const * title; +} _todo_priorities[] = +{ + { TODO_PRIORITY_UNKNOWN,N_("Unknown") }, + { TODO_PRIORITY_LOW, N_("Low") }, + { TODO_PRIORITY_MEDIUM, N_("Medium") }, + { TODO_PRIORITY_HIGH, N_("High") }, + { 0, NULL } +}; + /* variables */ static char const * _authors[] = @@ -214,8 +227,9 @@ Todo * todo_new(void) static void _new_view(Todo * todo) { - GtkTreeSelection * sel; size_t i; + GtkTreeIter iter; + GtkTreeSelection * sel; GtkCellRenderer * renderer; GtkTreeViewColumn * column; @@ -227,9 +241,16 @@ static void _new_view(Todo * todo) G_TYPE_STRING, /* display start */ G_TYPE_UINT, /* end */ G_TYPE_STRING, /* display end */ - G_TYPE_UINT, /* priority */ - G_TYPE_STRING, /* display priority */ + G_TYPE_STRING, /* priority */ G_TYPE_STRING); /* category */ + todo->priorities = gtk_list_store_new(2, G_TYPE_UINT, G_TYPE_STRING); + for(i = 0; _todo_priorities[i].title != NULL; i++) + { + gtk_list_store_append(todo->priorities, &iter); + gtk_list_store_set(todo->priorities, &iter, + 0, _todo_priorities[i].priority, + 1, _todo_priorities[i].title, -1); + } todo->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(todo->store)); if((sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(todo->view))) != NULL) @@ -265,7 +286,18 @@ static void _new_view(Todo * todo) _todo_columns[i].sort); gtk_tree_view_append_column(GTK_TREE_VIEW(todo->view), column); } + /* priority column */ + renderer = gtk_cell_renderer_combo_new(); + g_object_set(renderer, "model", todo->priorities, + "text-column", 1, + "editable", TRUE, NULL); + g_signal_connect(renderer, "edited", G_CALLBACK( + on_task_priority_edited), todo); + column = gtk_tree_view_column_new_with_attributes(_("Priority"), + renderer, "text", TD_COL_PRIORITY, NULL); + gtk_tree_view_column_set_sort_column_id(column, TD_COL_PRIORITY); gtk_container_add(GTK_CONTAINER(todo->scrolled), todo->view); + gtk_tree_view_append_column(GTK_TREE_VIEW(todo->view), column); } static gboolean _new_idle(gpointer data) @@ -349,7 +381,8 @@ Task * todo_task_add(Todo * todo, Task * task) gtk_list_store_insert(todo->store, &iter, 0); gtk_list_store_set(todo->store, &iter, TD_COL_TASK, task, TD_COL_DONE, task_get_done(task) > 0 ? TRUE : FALSE, - TD_COL_TITLE, task_get_title(task), -1); + TD_COL_TITLE, task_get_title(task), + TD_COL_PRIORITY, task_get_priority(task), -1); return task; } @@ -498,6 +531,21 @@ void todo_task_select_all(Todo * todo) } +/* todo_task_set_priority */ +void todo_task_set_priority(Todo * todo, GtkTreePath * path, + char const * priority) +{ + GtkTreeModel * model = GTK_TREE_MODEL(todo->store); + GtkTreeIter iter; + Task * task; + + gtk_tree_model_get_iter(model, &iter, path); + gtk_tree_model_get(model, &iter, TD_COL_TASK, &task, -1); + task_set_priority(task, priority); + gtk_list_store_set(todo->store, &iter, TD_COL_PRIORITY, priority, -1); +} + + /* todo_task_set_title */ void todo_task_set_title(Todo * todo, GtkTreePath * path, char const * title) { diff --git a/src/todo.h b/src/todo.h index 2f3a44d..f5e10b3 100644 --- a/src/todo.h +++ b/src/todo.h @@ -26,6 +26,14 @@ /* types */ typedef struct _Todo Todo; +typedef enum _TodoPriority +{ + TODO_PRIORITY_UNKNOWN, + TODO_PRIORITY_LOW, + TODO_PRIORITY_MEDIUM, + TODO_PRIORITY_HIGH +} TodoPriority; + /* functions */ Todo * todo_new(void); @@ -41,6 +49,8 @@ void todo_task_delete_selected(Todo * todo); void todo_task_remove_all(Todo * todo); /* accessors */ +void todo_task_set_priority(Todo * todo, GtkTreePath * path, + char const * priority); void todo_task_set_title(Todo * todo, GtkTreePath * path, char const * title); void todo_task_edit(Todo * todo);