/// <summary> /// Change task category to its full hierarchy. /// </summary> /// <param name="parent">Parent task</param> /// <param name="newCategoryId">New category ID</param> public static void ChangeCategoryToHierarchy(Task parent, int newCategoryId) { // Collect hierarchy List<Task> tasks = new List<Task>(); CollectHierarchy(parent, tasks); // Build and launch query string sql = "UPDATE task SET category = " + newCategoryId + " WHERE 0 "; foreach (Task task in tasks) sql += "OR (id = " + task.Id + ") "; db.Query(sql); // Change category to objects foreach (Task task in tasks) task.CategoryId = newCategoryId; }
/// <summary> /// Removes a task from the database. /// </summary> /// <param name="TaskId">Task to remove</param> public static void RemoveTask(Task task) { ArrayList children = GetChildrenTasks(task); foreach (Task child in children) RemoveTask(child); db.NonQuery("DELETE FROM note WHERE task = " + task.Id); db.NonQuery("DELETE FROM task WHERE id = " + task.Id); Tasks.Remove(task); }
/// <summary> /// Fills an List of tasks with a full hierarchy. /// </summary> /// <param name="parent">Parent task</param> /// <param name="tasks">List of tasks, already initialized</param> private static void CollectHierarchy(Task parent, List<Task> tasks) { if (tasks != null) tasks.Add(parent); foreach (Task child in Todomoo.GetChildrenTasks(parent)) CollectHierarchy(child, tasks); }
/// <summary> /// Get the root task of a task. /// </summary> /// <param name="child">Child Task</param> /// <returns>The root Task</returns> public static Task GetRoot(Task child) { Task root = child; while (root.ParentId != 0) root = GetParent(root); return root; }
/// <summary> /// Check if a task contains another one in its full hierarchy. /// </summary> /// <param name="parent">Container</param> /// <param name="child">Content</param> /// <returns>TRUE if container got the content is its full hierarchy, FALSE otherwise</returns> public static bool IsContained(Task parent, Task child) { while (child != null) { if (child.Id == parent.Id) return true; child = GetParent(child); } return false; }
/// <summary> /// Get the children tasks of a task. /// </summary> /// <param name="parent">Parent Task</param> /// <returns>An ArrayList of Task objects</returns> public static ArrayList GetChildrenTasks(Task parent) { ArrayList ret = new ArrayList(); foreach (Task t in Tasks) if (t.ParentId == parent.Id) if (LoadCompletedTasks || !t.IsCompleted) { ret.Add(t); } return ret; }
/// <summary> /// Get the parent of a task. /// </summary> /// <param name="child">Child Task</param> /// <returns>The parent Task, or null</returns> public static Task GetParent(Task child) { foreach (Task t in Tasks) if (child.ParentId == t.Id) return t; return null; }
public void Exit(object sender, FormClosingEventArgs e) { // Minize to tray if option if (!boolForceExit) if (Settings.Get("window_minimize_when_closing").ToString() == "1") { e.Cancel = true; MinimizeToTray(); return; } // Save stuffs if DB is open if (Todomoo.isDbOpen) { // Ask for running timers bool running_timers = (Todomoo.LostTimers.Count > 0); if (!running_timers) foreach (Task t in Todomoo.Tasks) if (t.IsTimerRunning()) running_timers = true; if (running_timers) if (!Utils.MsgDialog.Confirm(Lang.Get("running_timers_confirm"), Lang.Get("warning"))) { e.Cancel = true; return; } // Save running timers foreach (Task t in Todomoo.Tasks) if (t.IsTimerRunning()) try { t.StopTimer(); // Stop timer t.Save(); // Save task } catch { Utils.MsgDialog.Error(Lang.Get("task_timer_error"), Lang.Get("error")); } // Save lost running timers foreach (int key in Todomoo.LostTimers.Keys) try { Task t = new Task(key); // Load the task with the unsaved timer int stopped_at = (int)((object[])Todomoo.LostTimers[t.Id])[0]; // Timer was stopper at second DateTime stopped_dt = (DateTime)((object[])Todomoo.LostTimers[t.Id])[1]; // Timer was stopped at time t.Timer = stopped_at + (int)(DateTime.Now.Subtract(stopped_dt).TotalSeconds); // Restore timer t.Save(); // Save task with correct timer } catch { Utils.MsgDialog.Error(Lang.Get("task_timer_error"), Lang.Get("error")); } // Close database Todomoo.CloseDatabase(); } // If settings are loaded... if (Settings != null) { // Backup the database if (Settings.Get("backup_automatic").ToString() == "1") TaskBackup(true); // Save window bounds Settings.Set("window_xpos", Bounds.X); Settings.Set("window_ypos", Bounds.Y); Settings.Set("window_maximized", (WindowState == FormWindowState.Maximized ? 1 : 0)); if (WindowState == FormWindowState.Normal) { Settings.Set("window_width", Bounds.Width); Settings.Set("window_height", Bounds.Height); } // Save columns width Settings.Set("col_name", colName.Width); Settings.Set("col_category", colCategory.Width); Settings.Set("col_priority", colPriority.Width); Settings.Set("col_creation_date", colCreationDate.Width); Settings.Set("col_due_date", colDueDate.Width); Settings.Set("col_completed", colCompleted.Width); Settings.Set("col_timer", colTimer.Width); Settings.Set("col_price", colPrice.Width); Settings.Set("col_notes", colNotes.Width); // Save columns visibility Settings.Set("col_category_v", (colCategory.IsVisible ? 1 : 0)); Settings.Set("col_priority_v", (colPriority.IsVisible ? 1 : 0)); Settings.Set("col_creation_date_v", (colCreationDate.IsVisible ? 1 : 0)); Settings.Set("col_due_date_v", (colDueDate.IsVisible ? 1 : 0)); Settings.Set("col_completed_v", (colCompleted.IsVisible ? 1 : 0)); Settings.Set("col_timer_v", (colTimer.IsVisible ? 1 : 0)); Settings.Set("col_price_v", (colPrice.IsVisible ? 1 : 0)); Settings.Set("col_notes_v", (colNotes.IsVisible ? 1 : 0)); // Save columns position Settings.Set("col_category_o", Math.Max(colCategory.DisplayIndex, 1)); Settings.Set("col_priority_o", Math.Max(colPriority.DisplayIndex, 1)); Settings.Set("col_creation_date_o", Math.Max(colCreationDate.DisplayIndex, 1)); Settings.Set("col_due_date_o", Math.Max(colDueDate.DisplayIndex, 1)); Settings.Set("col_completed_o", Math.Max(colCompleted.DisplayIndex, 1)); Settings.Set("col_timer_o", Math.Max(colTimer.DisplayIndex, 1)); Settings.Set("col_price_o", Math.Max(colPrice.DisplayIndex, 1)); Settings.Set("col_notes_o", Math.Max(colNotes.DisplayIndex, 1)); // Save columns sorting Settings.Set("col_name_s", GetSortingNumberFor(colName)); Settings.Set("col_category_s", GetSortingNumberFor(colCategory)); Settings.Set("col_priority_s", GetSortingNumberFor(colPriority)); Settings.Set("col_creation_date_s", GetSortingNumberFor(colCreationDate)); Settings.Set("col_due_date_s", GetSortingNumberFor(colDueDate)); Settings.Set("col_completed_s", GetSortingNumberFor(colCompleted)); Settings.Set("col_timer_s", GetSortingNumberFor(colTimer)); Settings.Set("col_price_s", GetSortingNumberFor(colPrice)); Settings.Set("col_notes_s", GetSortingNumberFor(colNotes)); // Save settings Settings.Set("selected_category", (SelectedCategory == null) ? 0 : SelectedCategory.Id); Settings.Save(); } // Delete tray icon if one if (tray != null) tray.Visible = false; // Undo updates if (u != null) try { u.Stop(); } catch { } }
private void TaskNewRoot() { try { // Create a new Task in this category Task new_task = new Task(); new_task.CategoryId = (SelectedCategory == null) ? 0 : SelectedCategory.Id; // Edit the new task in a Task form. TaskForm form = new TaskForm(Lang, Settings, new_task); if (!this.Visible) { // From tray... form.MinimizeBox = true; form.ShowInTaskbar = true; form.StartPosition = FormStartPosition.CenterScreen; } DialogResult res = form.ShowDialog(); // If the task has been modified, save it to the DB. if (res != DialogResult.OK) return; new_task.Update(); // Save task in DB CountTasks(); // Save pending notes ArrayList pending_notes = new_task.GetUnsavedNotes(); foreach (Note note in pending_notes) { note.TaskId = new_task.Id; note.Save(); } if (pending_notes.Count > 0) { new_task.Reload(); new_task.ClearUnsavedNotes(); } // If the task is in the selected category, update the tree if (SelectedCategory != null) if (new_task.CategoryId != SelectedCategory.Id) return; Todomoo.Tasks.Add(new_task); // Add to tasks treeTasks.AddObject(new_task); // Add to tree (this is always a root task) treeTasks.RefreshItem(treeTasks.GetItem(0)); // Refresh first item. Bug? treeTasks.RefreshItem(treeTasks.GetItem(treeTasks.GetItemCount() - 1)); // Refresh last item. Bug? // Select created task treeTasks.SelectedObject = new_task; } catch { Utils.MsgDialog.Error(Lang.Get("task_save_error"), Lang.Get("error")); return; } }
private void TaskNewChild() { try { // Create a new Task with selected parent Task new_task = new Task(); Task parent_task = (Task)treeTasks.GetSelectedObject(); new_task.CategoryId = parent_task.CategoryId; new_task.ParentId = parent_task.Id; // Edit the new task in a Task form. TaskForm form = new TaskForm(Lang, Settings, new_task); if (!this.Visible) { // From tray... form.MinimizeBox = true; form.ShowInTaskbar = true; form.StartPosition = FormStartPosition.CenterScreen; } DialogResult res = form.ShowDialog(); // If the task has been modified, save it to the DB. if (res != DialogResult.OK) return; new_task.Update(); // Save task in DB CountTasks(); // Save pending notes ArrayList pending_notes = new_task.GetUnsavedNotes(); foreach (Note note in pending_notes) { note.TaskId = new_task.Id; note.Save(); } if (pending_notes.Count > 0) { new_task.Reload(); new_task.ClearUnsavedNotes(); } // If the task is in the selected category, update the tree if (SelectedCategory != null) if (new_task.CategoryId != SelectedCategory.Id) return; Todomoo.Tasks.Add(new_task); // Add to tasks if (flatView) { /* Flat view? Add task and select it */ treeTasks.AddObject(new_task); treeTasks.SelectedObject = new_task; } else { /* Tree view, update and expand parent */ treeTasks.RefreshObject(Todomoo.GetParent(new_task)); // Refresh parent task (this will add the created child), thanks to Dege treeTasks.Expand(parent_task); /* Expand parent task to view new task */ } treeTasks.RefreshItem(treeTasks.GetItem(0)); // Refresh first item. Bug? treeTasks.RefreshItem(treeTasks.GetItem(treeTasks.GetItemCount() - 1)); // Refresh last item. Bug? } catch { Utils.MsgDialog.Error(Lang.Get("task_save_error"), Lang.Get("error")); return; } }
// Confirm moving as sub task private void TaskMoveConfirm(Task target) { if (moving.Count == 0) return; foreach (Task tmoving in moving) { // A sub task if (target != null) { if (!Todomoo.IsContained(tmoving, target)) { // Update tmoving.ParentId = target.Id; tmoving.Update(); LoadTasks(true); // Expand to view moved task Task tl; Stack<Task> s = new Stack<Task>(); foreach (Task t in Todomoo.Tasks) if (t.Id == target.Id) { s.Push(tl = t); while (tl.ParentId > 0) s.Push(tl = Todomoo.GetParent(tl)); break; } while (s.Count > 0) treeTasks.Expand(s.Pop()); } else if (tmoving != target) { Utils.MsgDialog.Error(Lang.Get("task_moving_error") + ".", Lang.Get("error")); } } // As root if ((target == null) && (SelectedCategory != null)) { // Update tmoving.ParentId = 0; tmoving.Update(); LoadTasks(true); } } // Done TaskMoveCancel(); }
/// <summary> /// Creates a new Task cloning this one. /// </summary> /// <returns>The cloned task</returns> public Object Clone() { Task task = new Task(); task.name = this.name; task.has_description = this.has_description; task.description = this.description; task.colour = this.colour; task.priority = this.priority; task.has_due_date = this.has_due_date; task.due_date = this.due_date; task.has_completed = this.has_completed; task.completed = this.completed; task.has_timer = this.has_timer; task.timer = this.timer; task.has_paid = this.has_paid; task.paid = this.paid; task.has_price = this.has_price; task.price = this.price; task.has_payment = this.has_payment; task.payment = this.payment; task.has_payment_note = this.has_payment_note; task.payment_note = this.payment_note; task.pricing_by_hour = this.pricing_by_hour; task.price_by_hour = this.price_by_hour; task.category = this.category; return task; }