Click here to Skip to main content
15,893,337 members
Articles / Programming Languages / C#

Linux Todolist

Rate me:
Please Sign up or sign in to vote.
4.59/5 (15 votes)
28 Jan 2008GPL35 min read 74K   2.3K   53  
A simple todolist designed for an Asus Eee Pc
/*
 * Created by SharpDevelop.
 * User: andy
 * Date: 21/12/2007
 * Time: 19:35
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Xml;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using log4net;

using uk.org.aspellclark.common;
using uk.org.aspellclark.todolist.engine;

namespace uk.org.aspellclark.todolist
{
    /// <summary>
    ///   <name>TodolistCtrl</name>
    ///   <namespace>uk.org.aspellclark.todolist</namespace>
    ///   <version>1.0</version>
    ///   <author>Andy Aspell-Clark</author>
    ///   <description>Display a simple about box
    ///   </description>
    ///   <history>
    ///     <historyitem> 1 Jan 2008  1.0 ARAC  Initial Version.</historyitem>
    ///   </history>
    /// </summary>
	public class TodolistCtrl : System.Windows.Forms.UserControl
	{
		#region members
		// Create a logger for use in this class
		private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		private System.Windows.Forms.TextBox txtListName;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.TreeView treeTasks;
		private uk.org.aspellclark.todolist.TaskDetailsCtrl taskDetailsCtrl1;
		private System.Windows.Forms.Panel pnlTaskDetails;
        private PreferencesXml m_preferences = null;
        private TodolistEngine engine;
        
        private CTask m_selectedTask;
        private TodoListTreeNode m_SelectedNode = null;
        private int m_iLevel;
        
        private CTodolist todolist = null;
        private const string M_NEW_TASK_NAME = "New Task";
        private string m_strFile = "TodoList.tdl";
        private bool m_bLoadingData=false;
        private bool m_bDataChanged = false;
        private bool m_bNodeDataChanged = false;
        private string m_sFileFormat = "6";
        private string m_sProjectName = null;
        private Int16 m_iNextUniqueID = -1;
        private string m_sFileVersion = "1";
        private string m_sLastModified = null;
        private string m_sLastSortBy = null;
        private string m_sLastSortDir = null;
        private DateTime m_dtEarliestDueDate;

        private System.Drawing.Font m_Font;
        #endregion
        
        #region properties
        public string FileName
        {
        	get {return m_strFile;}
        	set{m_strFile=value;}
        }
        public string ListName
        {
        	get {return m_sProjectName;}
        	set{m_sProjectName=value;}
        }
        public bool DataChanged
        {
            get { return m_bDataChanged; }
            set { m_bDataChanged = value; }
        }
        public TodolistEngine Engine
        {
            get { return engine; }
            set { engine = value; taskDetailsCtrl1.Engine = this.engine; }
        }
        private bool LoadingData
        {
            get { return m_bLoadingData; }
            set { m_bLoadingData = value; }
        }
        #endregion
        	
		
		public TodolistCtrl()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		public TodolistCtrl(TodolistEngine engine, CTodolist tdl)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			this.engine = engine;
			todolist=tdl;
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.pnlTaskDetails = new System.Windows.Forms.Panel();
			this.taskDetailsCtrl1 = new uk.org.aspellclark.todolist.TaskDetailsCtrl();
			this.treeTasks = new System.Windows.Forms.TreeView();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.panel1 = new System.Windows.Forms.Panel();
			this.txtListName = new System.Windows.Forms.TextBox();
			this.pnlTaskDetails.SuspendLayout();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// pnlTaskDetails
			// 
			this.pnlTaskDetails.Controls.Add(this.taskDetailsCtrl1);
			this.pnlTaskDetails.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlTaskDetails.Location = new System.Drawing.Point(150, 0);
			this.pnlTaskDetails.Name = "pnlTaskDetails";
			this.pnlTaskDetails.Size = new System.Drawing.Size(250, 248);
			this.pnlTaskDetails.TabIndex = 3;
			// 
			// taskDetailsCtrl1
			// 
			this.taskDetailsCtrl1.AutoSize = true;
			this.taskDetailsCtrl1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.taskDetailsCtrl1.Location = new System.Drawing.Point(0, 0);
			this.taskDetailsCtrl1.MinimumSize = new System.Drawing.Size(224, 218);
			this.taskDetailsCtrl1.Name = "taskDetailsCtrl1";
			this.taskDetailsCtrl1.Size = new System.Drawing.Size(250, 248);
			this.taskDetailsCtrl1.TabIndex = 0;
			this.taskDetailsCtrl1.Task = null;
			this.taskDetailsCtrl1.TodolistDataChanged += new System.EventHandler(this.TaskDetailsCtrl1TodolistDataChanged);
			// 
			// treeTasks
			// 
			this.treeTasks.CheckBoxes = true;
			this.treeTasks.Dock = System.Windows.Forms.DockStyle.Left;
			this.treeTasks.Location = new System.Drawing.Point(0, 0);
			this.treeTasks.Name = "treeTasks";
			this.treeTasks.Size = new System.Drawing.Size(150, 248);
			this.treeTasks.TabIndex = 1;
			this.treeTasks.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.TreeTasksAfterSelect);
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(150, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 248);
			this.splitter1.TabIndex = 4;
			this.splitter1.TabStop = false;
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.splitter1);
			this.panel1.Controls.Add(this.pnlTaskDetails);
			this.panel1.Controls.Add(this.treeTasks);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel1.Location = new System.Drawing.Point(0, 20);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(400, 248);
			this.panel1.TabIndex = 4;
			// 
			// txtListName
			// 
			this.txtListName.Dock = System.Windows.Forms.DockStyle.Top;
			this.txtListName.Location = new System.Drawing.Point(0, 0);
			this.txtListName.Name = "txtListName";
			this.txtListName.Size = new System.Drawing.Size(400, 20);
			this.txtListName.TabIndex = 3;
			this.txtListName.Text = "textBox1";
			this.txtListName.TextChanged += new System.EventHandler(this.TxtListNameTextChanged);
			// 
			// TodolistCtrl
			// 
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.txtListName);
			this.MinimumSize = new System.Drawing.Size(400, 268);
			this.Name = "TodolistCtrl";
			this.Size = new System.Drawing.Size(400, 268);
			this.Load += new System.EventHandler(this.TodolistCtrlLoad);
			this.pnlTaskDetails.ResumeLayout(false);
			this.pnlTaskDetails.PerformLayout();
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);
			this.PerformLayout();
		}
		#endregion
		

		
		private void TreeTasksAfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			if ( !LoadingData)
			{
				log.Info("TodolistCtrl::selecting task");
	            TodoListTreeNode tlNode = e.Node as TodoListTreeNode;
	            if (m_SelectedNode != null && m_bNodeDataChanged)
	            {
	                //save changes to node
	            }
	            if (tlNode.Task != null)
	            {
	                m_selectedTask = tlNode.Task;
	                taskDetailsCtrl1.Task=m_selectedTask;
	            }
			}
		}
	

		public void ReadFromXml(string filename)
		{
			log.Info("TodolistCtrl::opening file ["+filename+"]");
			LoadingData=true;
			this.FileName = filename;
			
            Cursor.Current = Cursors.WaitCursor;
            XmlDocument xmlTodoList;
            XmlNode xndList;
            TodoListTreeNode tnParent;

            try
            {
                xmlTodoList = new XmlDocument();

                xmlTodoList.Load(filename);

                treeTasks.BeginUpdate();

                treeTasks.Nodes.Clear();

                if (xmlTodoList.GetElementsByTagName("TODOLIST").Count > 0)
                {
                    xndList = xmlTodoList.GetElementsByTagName("TODOLIST").Item(0);
                    m_sProjectName = xndList.Attributes.GetNamedItem("PROJECTNAME").InnerText;
                    txtListName.Text = m_sProjectName;
                    m_sFileFormat = xndList.Attributes.GetNamedItem("FILEFORMAT").InnerText;
                    m_iNextUniqueID = Convert.ToInt16(xndList.Attributes.GetNamedItem("NEXTUNIQUEID").InnerText);
                    try { m_dtEarliestDueDate = CommonFunctions.OleDateToDateTime(Convert.ToDouble(xndList.Attributes.GetNamedItem("EARLIESTDUEDATE").InnerText)); }
                    catch { }
                    try { m_sLastSortBy = xndList.Attributes.GetNamedItem("LASTSORTBY").InnerText; }
                    catch { }
                    try { m_sFileVersion = xndList.Attributes.GetNamedItem("FILEVERSION").InnerText; }
                    catch { }
                    try { m_sLastSortDir = xndList.Attributes.GetNamedItem("LASTSORTDIR").InnerText; }
                    catch { }
                    try { m_sLastModified = xndList.Attributes.GetNamedItem("LASTMODIFIED").InnerText; }
                    catch { }

                    if (xndList.HasChildNodes == true)
                    {
                        for (int i = 0; i < xndList.ChildNodes.Count; i++)
                        {
                            AppendTask(null, xndList.ChildNodes.Item(i));
                        }
                    }
                }

                treeTasks.Font = m_Font;
                treeTasks.EndUpdate();
            }
            catch (Exception e)
            {
				log.Info("TodolistCtrl::exception caught ["+e.Message+"]");
                tnParent = new TodoListTreeNode("Invalid Todo List File");
                tnParent.Task = new CTask();
                tnParent.Task.CommentsStr=e.Message;
                tnParent.ImageIndex = 4;
                tnParent.SelectedImageIndex = 4;
                treeTasks.Nodes.Add(tnParent);
                tnParent.Expand();
            }

			log.Info("TodolistCtrl::setting checked for each task");
            foreach (TodoListTreeNode childNode in treeTasks.Nodes)
            {
                SetChecked(childNode);
            }
            LoadingData=false;
            Cursor.Current = Cursors.Default;
            
			log.Info("TodolistCtrl::file loaded :)");
		}
		
		
        private void AppendTask(TodoListTreeNode tnParent, XmlNode xndTask)
        {
            TodoListTreeNode tnChild;
            CTask clsTask;

            if (xndTask.Name.ToUpper() == "TASK")
            {
                clsTask = new CTask();
                try
                {
	                clsTask.ReadFromXml(xndTask);
	            }
	            catch (Exception e)
	            {
					log.Error("TodolistCtrl:AppendTask:exception caught reading an xml task ["+e.Message+"]");
					return;
	            }

                tnChild = new TodoListTreeNode(clsTask.Title);
                tnChild.TaskId = clsTask.TaskId;
                tnChild.Task = clsTask;
                if (tnParent != null)
                {
                    tnChild.ParentTaskId = tnParent.TaskId;
                }

                if (xndTask.HasChildNodes == true)
                {
                    for (int i = 0; i < xndTask.ChildNodes.Count; i++)
                    {
                        AppendTask(tnChild, xndTask.ChildNodes.Item(i));
                    }
                }
                tnChild.Checked = clsTask.Complete;

                if ((m_iLevel == 0) || ((m_iLevel == 1) && (clsTask.Complete == false)) || ((m_iLevel == 1) && (clsTask.Complete == true) && (tnChild.GetNodeCount(false) > 0)) || ((m_iLevel == 2) && (clsTask.Complete == true)) || ((m_iLevel == 2) && (clsTask.Complete == false) && (tnChild.GetNodeCount(false) > 0)))
                {
                    if (tnParent == null)
                    {
                        treeTasks.Nodes.Add(tnChild);
                    }
                    else
                    {
                        tnParent.Nodes.Add(tnChild);
                    }
                }
            }
            else if (xndTask.Name.ToUpper() == "COMMENTS")
            {
                if (tnParent != null)
                {
                    clsTask = tnParent.Task;
                    clsTask.CommentsStr = xndTask.InnerText;
                    tnParent.Task = clsTask;
                }
            }
        }

		
        private void SetChecked(TodoListTreeNode pNode)
        {
            if (pNode.Nodes.Count > 0)
            {
                foreach (TodoListTreeNode childNode in pNode.Nodes)
                {
                    SetChecked(childNode);
                }
            }
            CTask clsTask = pNode.Task;
            pNode.Checked = clsTask.Complete;
        }

        public void WriteToXml()
        {
            if (DataChanged == true)
            {
                if (File.Exists(m_strFile))
                {
                    if (File.Exists(m_strFile + ".bak"))
                    {
                        File.Delete(m_strFile + ".bak");
                    }
                    File.Move(m_strFile, m_strFile + ".bak");
                }
                XmlTextWriter xmlWriter = new XmlTextWriter(m_strFile, System.Text.Encoding.UTF8);

                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.Indentation = 0;
                xmlWriter.IndentChar = ' ';

                xmlWriter.WriteStartDocument();

                //write out the root element - Project
                xmlWriter.WriteStartElement("TODOLIST");

                xmlWriter.WriteAttributeString("", "FILEFORMAT", "", m_sFileFormat);
                xmlWriter.WriteAttributeString("", "PROJECTNAME", "", m_sProjectName);
                xmlWriter.WriteAttributeString("", "NEXTUNIQUEID", "", m_iNextUniqueID.ToString());
                xmlWriter.WriteAttributeString("", "EARLIESTDUEDATE", "", CommonFunctions.DateTimeToOleDate(m_dtEarliestDueDate).ToString());

                int iFileVer = Convert.ToInt32(m_sFileVersion);
                iFileVer++;
                m_sFileVersion = iFileVer.ToString();
                xmlWriter.WriteAttributeString("", "FILEVERSION", "", m_sFileVersion);

                m_sLastModified = DateTime.Now.ToString("yyyy-mm-dd");
                xmlWriter.WriteAttributeString("", "LASTMODIFIED", "", m_sLastModified);

                //write out the tree nodes

                foreach (TodoListTreeNode tmpNode in treeTasks.Nodes)
                {
                    OutputNodes(xmlWriter, tmpNode);
                }

                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndDocument();
                xmlWriter.Flush();
                xmlWriter.Close();
                DataChanged = false;

                txtListName.Text = m_sProjectName + " [v" + m_sFileVersion + " - " + m_sLastModified + "]";

            }//if (DataChanged)
            DataChanged = false;
        }


        private void OutputNodes(XmlTextWriter pXmlWriter, TodoListTreeNode pNode)
        {
            CTask currTask = pNode.Task;
            currTask.writeToXml(pXmlWriter);
            foreach (TodoListTreeNode tmpNode in pNode.Nodes)
            {
                OutputNodes(pXmlWriter, tmpNode);
            }
            currTask.endWriteToXml(pXmlWriter);
        }




		void TxtListNameTextChanged(object sender, EventArgs e)
		{
			if (!LoadingData)
			{
				m_sProjectName = txtListName.Text;
				m_bDataChanged = true;
			}
		}

		void TaskDetailsCtrl1TodolistDataChanged(object sender, EventArgs e)
		{
			DataChanged = true;
		}


		
		void TodolistCtrlLoad(object sender, EventArgs e)
		{
            DataChanged = false;
		}
	}//class
}//namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior) Airbus Defense and Space
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions