tasque r19 - in trunk: . src



Author: sharm
Date: Thu Mar 13 22:40:20 2008
New Revision: 19
URL: http://svn.gnome.org/viewvc/tasque?rev=19&view=rev

Log:
* src/Utilities.cs, src/TaskTreeView.cs: Moved ParseTaskText method
  from TaskTreeView to Utilities.
* src/TaskWindow.cs: Parse newly entered task text for due date
  information.

Modified:
   trunk/ChangeLog
   trunk/src/TaskTreeView.cs
   trunk/src/TaskWindow.cs
   trunk/src/Utilities.cs

Modified: trunk/src/TaskTreeView.cs
==============================================================================
--- trunk/src/TaskTreeView.cs	(original)
+++ trunk/src/TaskTreeView.cs	Thu Mar 13 22:40:20 2008
@@ -3,7 +3,6 @@
 
 using System;
 using System.Collections.Generic;
-using System.Text.RegularExpressions;
 using Gtk;
 using Mono.Unix;
 
@@ -545,79 +544,22 @@
 				return;
 			
 			string newText = args.NewText;
+			
+			// Attempt to derive due date information from text.
 			if (Application.Preferences.GetBool (Preferences.ParseDateEnabledKey) &&
 			    task.State == TaskState.Active &&
-			    task.DueDate == DateTime.MinValue)
-				newText = ParseTaskText (newText, task);
-			task.Name = newText;
-		}
-		
-		/// <summary>
-		/// Parse the task name in order to derive due date information.
-		/// </summary>
-		/// <param name="taskText">
-		/// A <see cref="System.String"/> representing the text entered
-		/// into the task name field.
-		/// </param>
-		/// <param name="task">
-		/// The <see cref="ITask"/> object to which due date information
-		/// should be added, if any is found.
-		/// </param>
-		/// <returns>
-		/// The taskText with the due date section of the string removed.
-		/// </returns>
-		string ParseTaskText (string taskText, ITask task)
-		{
-			// First, look for ways that the right side of the entered
-			// text can be directly parsed as a date
-			string[] words = taskText.Split (' ');
-			for (int i = 1; i < words.Length; i++) {
-				string possibleDate = string.Join (" ", words, i, words.Length - i);
-				DateTime result;
-				if (DateTime.TryParse (possibleDate, out result)) {
-					// Favor future dates, unless year was specifically mentioned
-					if (!possibleDate.Contains (result.Year.ToString ()))
-						while (result < DateTime.Today)
-							result = result.AddYears (1);
-					
-					// Set task due date and return the task
-					// name with the date part removed.
-					task.DueDate = result;
-					return string.Join (" ", words, 0, i);
-				}
-			}
-			
-			// Then try some more natural language parsing
-			
-			// A regular expression to capture a task that is due today
-			string today = Catalog.GetString (@"^(?<task>.+)\s+today\W*$");
-			// A regular expression to capture a task that is due tomorrow
-			string tomorrow = Catalog.GetString (@"^(?<task>.+)\s+tomorrow\W*$");
-			
-			// Additional regular expressions to consider using
-			//string abbrevDate = Catalog.GetString (@"^(?<task>.+)(on )?(the )?(?<day>\d{1,2})((th)|(nd)|(rd)|(st))\W*$");
-			//string nextDayName = Catalog.GetString (@"^(?<task>.+)(on )?next\s+(?<day>[a-z]+)\W*$");
-			//string dayName = Catalog.GetString (@"^(?<task>.+)\s+(on )?(?<day>[a-z]+)\W*$");
-			
-			Match match = Regex.Match (taskText, today, RegexOptions.IgnoreCase);
-			if (match.Success) {
-				string trimmedTaskText = match.Groups ["task"].Value;
-				if (!string.IsNullOrEmpty (trimmedTaskText)) {
-					task.DueDate = DateTime.Now;
-					return trimmedTaskText;
-				}
-			}
-			
-			match = Regex.Match (taskText, tomorrow, RegexOptions.IgnoreCase);
-			if (match.Success) {
-				string trimmedTaskText = match.Groups ["task"].Value;
-				if (!string.IsNullOrEmpty (trimmedTaskText)) {
-					task.DueDate = DateTime.Now.AddDays (1);
-					return trimmedTaskText;
-				}
+			    task.DueDate == DateTime.MinValue) {
+				
+				string parsedTaskText;
+				DateTime parsedDueDate;
+				Utilities.ParseTaskText (newText, out parsedTaskText, out parsedDueDate);
+				
+				if (parsedDueDate != DateTime.MinValue)
+					task.DueDate = parsedDueDate;
+				newText = parsedTaskText;
 			}
 			
-			return taskText;
+			task.Name = newText;
 		}
 		
 		/// <summary>

Modified: trunk/src/TaskWindow.cs
==============================================================================
--- trunk/src/TaskWindow.cs	(original)
+++ trunk/src/TaskWindow.cs	Thu Mar 13 22:40:20 2008
@@ -836,8 +836,8 @@
 
 		void OnAddTask (object sender, EventArgs args)
 		{
-			string newTaskText = addTaskEntry.Text.Trim ();
-			if (newTaskText.Length == 0)
+			string enteredTaskText = addTaskEntry.Text.Trim ();
+			if (enteredTaskText.Length == 0)
 				return;
 			
 			Gtk.TreeIter iter;
@@ -846,8 +846,22 @@
 			
 			ICategory category =
 				categoryComboBox.Model.GetValue (iter, 0) as ICategory;
-			
-			ITask task = CreateTask (newTaskText, category);
+		
+			// If enabled, attempt to parse due date information
+			// out of the entered task text.
+			DateTime taskDueDate = DateTime.MinValue;
+			string taskName;
+			if (Application.Preferences.GetBool (Preferences.ParseDateEnabledKey))
+				Utilities.ParseTaskText (
+				                         enteredTaskText,
+				                         out taskName,
+				                         out taskDueDate);
+			else
+				taskName = enteredTaskText;
+			
+			ITask task = CreateTask (taskName, category);
+			if (taskDueDate != DateTime.MinValue)
+				task.DueDate = taskDueDate;
 			
 			HighlightTask (task);
 		}

Modified: trunk/src/Utilities.cs
==============================================================================
--- trunk/src/Utilities.cs	(original)
+++ trunk/src/Utilities.cs	Thu Mar 13 22:40:20 2008
@@ -31,6 +31,7 @@
 using System;
 using System.Runtime.InteropServices;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Net;
 using System.IO;
 using System.Security.Cryptography;
@@ -335,5 +336,79 @@
 			
 			return string.Empty;
 		}
+		
+		/// <summary>
+		/// Parse the task name in order to derive due date information.
+		/// </summary>
+		/// <param name="enteredTaskText">
+		/// A <see cref="System.String"/> representing the text entered
+		/// into the task name field.
+		/// </param>
+		/// <param name="parsedTaskText">
+		/// The enteredTaskText with the due date section of the string
+		/// removed.
+		/// </param>
+		/// <param name="parsedDueDate">
+		/// The due date derived from enteredTaskText, or
+		/// DateTime.MinValue if no date information was found.
+		/// </param>
+		public static void ParseTaskText (string enteredTaskText, out string parsedTaskText, out DateTime parsedDueDate)
+		{
+			// First, look for ways that the right side of the entered
+			// text can be directly parsed as a date
+			string[] words = enteredTaskText.Split (' ');
+			for (int i = 1; i < words.Length; i++) {
+				string possibleDate = string.Join (" ", words, i, words.Length - i);
+				DateTime result;
+				if (DateTime.TryParse (possibleDate, out result)) {
+					// Favor future dates, unless year was specifically mentioned
+					if (!possibleDate.Contains (result.Year.ToString ()))
+						while (result < DateTime.Today)
+							result = result.AddYears (1);
+					
+					// Set task due date and return the task
+					// name with the date part removed.
+					parsedDueDate = result;
+					parsedTaskText = string.Join (" ", words, 0, i);
+					return;
+				}
+			}
+			
+			// Then try some more natural language parsing
+			
+			// A regular expression to capture a task that is due today
+			string today = Catalog.GetString (@"^(?<task>.+)\s+today\W*$");
+			// A regular expression to capture a task that is due tomorrow
+			string tomorrow = Catalog.GetString (@"^(?<task>.+)\s+tomorrow\W*$");
+			
+			// Additional regular expressions to consider using
+			//string abbrevDate = Catalog.GetString (@"^(?<task>.+)(on )?(the )?(?<day>\d{1,2})((th)|(nd)|(rd)|(st))\W*$");
+			//string nextDayName = Catalog.GetString (@"^(?<task>.+)(on )?next\s+(?<day>[a-z]+)\W*$");
+			//string dayName = Catalog.GetString (@"^(?<task>.+)\s+(on )?(?<day>[a-z]+)\W*$");
+			
+			Match match = Regex.Match (enteredTaskText, today, RegexOptions.IgnoreCase);
+			if (match.Success) {
+				string trimmedTaskText = match.Groups ["task"].Value;
+				if (!string.IsNullOrEmpty (trimmedTaskText)) {
+					parsedDueDate = DateTime.Now;
+					parsedTaskText = trimmedTaskText;
+					return;
+				}
+			}
+			
+			match = Regex.Match (enteredTaskText, tomorrow, RegexOptions.IgnoreCase);
+			if (match.Success) {
+				string trimmedTaskText = match.Groups ["task"].Value;
+				if (!string.IsNullOrEmpty (trimmedTaskText)) {
+					parsedDueDate = DateTime.Now.AddDays (1);
+					parsedTaskText = trimmedTaskText;
+					return;
+				}
+			}
+			
+			parsedTaskText = enteredTaskText;
+			parsedDueDate = DateTime.MinValue;
+			return;
+		}
 	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]