/// <summary> /// Update the model to match what is in RTM /// FIXME: This is a lame implementation and needs to be optimized /// </summary> private void UpdateTasks(Lists lists) { Logger.Debug("RtmBackend.UpdateTasks was called"); try { foreach (List list in lists.listCollection) { Tasks tasks = null; try { tasks = rtm.TasksGetList(list.ID); } catch (Exception tglex) { Logger.Debug("Exception calling TasksGetList(list.ListID) " + tglex.Message); } if (tasks != null) { foreach (List tList in tasks.ListCollection) { if (tList.TaskSeriesCollection == null) { continue; } foreach (TaskSeries ts in tList.TaskSeriesCollection) { RtmTask rtmTask = new RtmTask(ts, this, list.ID); lock (taskLock) { Gtk.TreeIter iter; Gtk.Application.Invoke(delegate { if (taskIters.ContainsKey(rtmTask.ID)) { iter = taskIters[rtmTask.ID]; } else { iter = taskStore.AppendNode(); taskIters.Add(rtmTask.ID, iter); } taskStore.SetValue(iter, 0, rtmTask); }); } } } } } } catch (Exception e) { Logger.Debug("Exception in fetch " + e.Message); Logger.Debug(e.ToString()); } Logger.Debug("RtmBackend.UpdateTasks is done"); }
/// <summary> /// Updates the list of tasks as <see cref="RTMTaskItem"/>s from the RTM server. /// Also collects all the notes and tags during the update. /// </summary> public static void UpdateTasks() { lock (task_lock) { if (!IsAuthenticated && !TryConnect()) { return; } Tasks rtmTasks; Tasks rtmTasks_sync; if (last_sync == DateTime.MinValue) { tasks.Clear(); tags.Clear(); notes.Clear(); } try { // If first time sync, get full list of tasks restricted by filter // otherwise, only do incremental sync. if (last_sync == DateTime.MinValue) { rtmTasks = rtm.TasksGetList(null, null, filter); rtmTasks_sync = null; } else { rtmTasks_sync = rtm.TasksGetList(null, last_sync.ToUniversalTime().ToString("u"), null); rtmTasks = rtm.TasksGetList(null, last_sync.ToUniversalTime().ToString("u"), filter); } } catch (RtmException e) { rtmTasks = null; rtmTasks_sync = null; last_sync = DateTime.MinValue; Log <RTM> .Debug(AddinManager.CurrentLocalizer.GetString("An error occured when updating RTM tasks: {0}"), e.Message); return; } // if not first time sync, delete all changed tasks (using the list with nothing filtered) if (last_sync != DateTime.MinValue) { foreach (List rtmList in rtmTasks_sync.ListCollection) { if (rtmList.DeletedTaskSeries != null) { foreach (TaskSeries rtmTaskSeries in rtmList.DeletedTaskSeries.TaskSeriesCollection) { foreach (Task rtmTask in rtmTaskSeries.TaskCollection) { TryRemoveTask(rtmTask.TaskID); } } } if (rtmList.TaskSeriesCollection != null) { foreach (TaskSeries rtmTaskSeries in rtmList.TaskSeriesCollection) { foreach (Task rtmTask in rtmTaskSeries.TaskCollection) { TryRemoveTask(rtmTask.TaskID); } } } } } // add changed tasks from the list with filter used. foreach (List rtmList in rtmTasks.ListCollection) { if (rtmList.TaskSeriesCollection != null) { foreach (TaskSeries rtmTaskSeries in rtmList.TaskSeriesCollection) { foreach (Task rtmTask in rtmTaskSeries.TaskCollection) { // delete one recurrent task will cause other deleted instances // appear in the taskseries tag, so here we need to check again. if (rtmTask.Deleted == DateTime.MinValue) { // handle tags string temp_tags = ""; if (rtmTaskSeries.Tags.TagCollection.Length > 0) { foreach (Tag rtmTag in rtmTaskSeries.Tags.TagCollection) { if (tags.FindIndex(i => (i as RTMTagItem).Name == rtmTag.Text) == -1) { tags.Add(new RTMTagItem(rtmTag.Text)); } temp_tags += rtmTag.Text + ", "; } temp_tags = temp_tags.Remove(temp_tags.Length - 2); } // handle notes if (rtmTaskSeries.Notes.NoteCollection.Length > 0) { foreach (Note rtmNote in rtmTaskSeries.Notes.NoteCollection) { notes.Add(new RTMNoteItem(rtmNote.Title, rtmNote.Text, rtmNote.ID, String.Format("http://www.rememberthemilk.com/print/{0}/{1}/{2}/notes/", username, rtmList.ID, rtmTask.TaskID), rtmTask.TaskID)); } } // add new task RTMTaskItem new_task = new RTMTaskItem(rtmList.ID, rtmTaskSeries.TaskSeriesID, rtmTask.TaskID, rtmTaskSeries.Name, rtmTask.Due, rtmTask.Completed, rtmTaskSeries.TaskURL, rtmTask.Priority, rtmTask.HasDueTime, rtmTask.Estimate, rtmTaskSeries.LocationID, temp_tags); tasks.Add(new_task); } } } } } last_sync = DateTime.Now; } Log <RTM> .Debug("Received {0} tasks.", tasks.ToArray().Length); Log <RTM> .Debug("Received {0} notes.", notes.ToArray().Length); Log <RTM> .Debug("Received {0} tags.", tags.ToArray().Length); }