;;;;
;;;; A simple TODO app for the GNOME desktop
;;;; (mainly written to try out GTK+Lisp)
;;;;
;;;; (c) Daniel Vedder 2020
;;;; Licensed under the terms of the MIT license
;;;;

(in-package :cl-todo)

(defvar *task-list* NIL)
(defvar *runfile* (uiop:native-namestring "~/.todo"))


(defun launch ()
	(setf *task-list* (load-task-file))
	(make-gui)
	(bt:join-thread (gtk-thread)))

(defun load-task-file (&optional (file-name *runfile*))
	"Load a list of (task) strings from the run file"
	(with-open-file (f file-name :if-does-not-exist nil)
		(when (null f)
			(format t "~&No task file found at ~S" file-name)
			(return-from load-task-file))
		(do* ((line (read-line f nil nil)
				  (read-line f nil nil))
				 (tasks NIL))
			((null line) tasks)
			(setf tasks (append tasks (list line))))))

(defun write-task-list (&optional (lst *task-list*) (filename *runfile*))
	"Write the task list to file, one entry per line."
	(let ((f (open filename :direction :output :if-exists :supersede)))
		(dolist (i lst) (format f "~&~A" i))
		(close f)))

(defun add-task! (task)
	"Append a task to the task list and save to file"
	(setf *task-list* (append *task-list* (list task)))
	(write-task-list))

(defun remove-task! (task)
	"Remove a task from the list and save to file"
	(setf *task-list*
		(remove-if #'(lambda (s) (equalp s task))
			*task-list* :count 1))
	(write-task-list))

