From bdbc063e9e40546f4a107886ff374b8319f44f41 Mon Sep 17 00:00:00 2001
From: Frederic Culot <calcurse@culot.org>
Date: Thu, 31 Aug 2006 18:47:54 +0000
Subject: todo_insert() suppressed todo_new_item() updated to ask for priority
 todo_add() updated to sort items by priority order todo_get_priority(),
 todo_get_item() and todo_get_position() created

---
 src/todo.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 80 insertions(+), 18 deletions(-)

(limited to 'src')

diff --git a/src/todo.c b/src/todo.c
index fffe029..385acc7 100755
--- a/src/todo.c
+++ b/src/todo.c
@@ -1,4 +1,4 @@
-/*	$calcurse: todo.c,v 1.2 2006/08/30 17:48:41 culot Exp $	*/
+/*	$calcurse: todo.c,v 1.3 2006/08/31 18:47:54 culot Exp $	*/
 
 /*
  * Calcurse - text-based organizer
@@ -35,33 +35,30 @@
 
 struct todo_s *todolist;
 
-/* Add an item in the todo list. */
+/* Request user to enter a new todo item. */
 int todo_new_item(int total, int colr)
 {
+	int ch = 0;
 	char *mesg = _("Enter the new ToDo item : ");
+	char *mesg_id = 
+		_("Enter the ToDo priority [1 (highest) - 9 (lowest)] :");
 	char todo_input[MAX_LENGTH];
 
 	status_mesg(mesg, "");
 	getstring(swin, colr, todo_input, 0, 1);
 	if (strlen(todo_input) != 0) {
-		todo_insert(todo_input);
+		while ( (ch < '1') || (ch > '9') ) {
+			status_mesg(mesg_id, "");
+			ch = wgetch(swin);
+		}
+		todo_add(todo_input, ch - '0');
 		total++;
 	}
 
 	return total;
 }
 
-struct todo_s *todo_insert(char *mesg)
-{
-	struct todo_s *o;
-	o = (struct todo_s *) malloc(sizeof(struct todo_s));
-	o->mesg = (char *) malloc(strlen(mesg) + 1);
-	strcpy(o->mesg, mesg);
-	o->next = todolist;
-	todolist = o;
-	return o;
-}
-
+/* Add an item in the todo linked list. */
 struct todo_s *todo_add(char *mesg, int id)
 {
 	struct todo_s *o, **i;
@@ -69,13 +66,19 @@ struct todo_s *todo_add(char *mesg, int id)
 	o->mesg = (char *) malloc(strlen(mesg) + 1);
 	strcpy(o->mesg, mesg);
 	o->id = id;
-	for (i = &todolist; *i != 0; i = &(*i)->next) {
+	i = &todolist;
+	for (;;) {
+		if (*i == 0 || (*i)->id > id) {
+			o->next = *i;
+			*i = o;
+			break;
+		}
+		i = &(*i)->next;
 	}
-	o->next = *i;
-	*i = o;
 	return o;
 }
 
+/* Delete an item from the todo linked list. */
 void todo_delete_bynum(unsigned num)
 {
 	unsigned n;
@@ -93,7 +96,66 @@ void todo_delete_bynum(unsigned num)
 		iptr = &i->next;
 		n++;
 	}
-	/* not reached */
+	/* NOTREACHED */
 	fputs(_("FATAL ERROR in todo_delete_bynum: no such todo\n"), stderr);
 	exit(EXIT_FAILURE);
 }
+
+/* Returns a structure containing the selected item. */
+struct todo_s *todo_get_item(int item_number)
+{
+	struct todo_s *o;
+	int i;
+	
+	o = todolist;
+	for (i = 1; i < item_number; i++) {
+		o = o->next;
+	}
+	return o;
+}
+
+/* 
+ * Returns the position into the linked list corresponding to the
+ * given todo_s item.
+ */
+int todo_get_position(struct todo_s *i)
+{
+	struct todo_s *o = todolist;
+	int n = 1;
+	
+	for (;;) {
+		if (o == i) break;
+		o = o->next;
+		n++;
+	}
+	return n; 
+}
+
+/* Change an item priority by pressing '+' or '-' inside TODO panel. */
+int todo_chg_priority(int action, int item_num)
+{
+	struct todo_s *backup;
+	char backup_mesg[MAX_LENGTH];
+	int backup_id;
+	int do_chg = 1, new_position;
+
+	backup = todo_get_item(item_num);
+	strncpy(backup_mesg, backup->mesg, strlen(backup->mesg) + 1);
+	backup_id = backup->id;
+	if (action == '+') {
+		(backup_id > 1) ? backup_id-- : do_chg--;
+	} else if (action == '-') {
+		(backup_id < 9) ? backup_id++ : do_chg--;
+	} else { /* NOTREACHED */
+		fputs(_("FATAL ERROR in todo_chg_priority: no such action\n"),
+			stderr);
+	}	
+	if (do_chg) {
+		todo_delete_bynum(item_num - 1);
+		backup = todo_add(backup_mesg, backup_id);
+		new_position = todo_get_position(backup);	
+	} else {
+		new_position = item_num;
+	}
+	return new_position;
+}
-- 
cgit v1.2.3-70-g09d2