public void ReportingQueueLoop(object sender, EventArgs e) { _ReportingQueueTimer.Stop(); if (queue_in_processing == true) { return; } queue_in_processing = true; while (_DelayedReportingQueue.Count > 0) { try { DelayedReporting task = _DelayedReportingQueue[0]; Helpers.DebugInfo("ReportingQueue is going to process appointment: Id: " + task.item.GlobalAppointmentID + " Subject: " + task.item.Subject + " Duration: " + task.item.Duration); _DelayedReportingQueue.RemoveAt(0); ProcessTask(task); } catch (Exception ex) { Helpers.DebugInfo("ReportingQueue exception: " + ex.Message); } } queue_in_processing = false; }
// main processing private void ProcessTask(DelayedReporting task) { Outlook.AppointmentItem aitem = task.item; if (aitem.Subject == null) { return; } if (task.mode == DelayedReporting.Mode.Remove) { if (aitem.UserProperties.Find(_TimeReportedProperty) == null || aitem.UserProperties.Find(_TaskIdProperty) == null) { return; } int id = aitem.UserProperties.Find(_TaskIdProperty).Value; int duration = aitem.UserProperties.Find(_TimeReportedProperty).Value; string subject = aitem.UserProperties.Find(_PreviousAssignedSubjectProperty).Value; try { _tfs.IncreaseReportedTime(id, -duration, Settings.showMessageBoxWhenAppointementDeleted); } catch (Exception ex) { Outlook.AppointmentItem newItem = (Outlook.AppointmentItem) this.Application.CreateItem(Outlook.OlItemType.olAppointmentItem); newItem.Start = aitem.Start; newItem.Subject = aitem.Subject; newItem.Duration = duration; newItem.Categories = _CategoryName; newItem.UserProperties.Add(_TaskIdProperty, Outlook.OlUserPropertyType.olInteger).Value = id; newItem.UserProperties.Add(_TimeReportedProperty, Outlook.OlUserPropertyType.olInteger).Value = duration; newItem.UserProperties.Add(_PreviousAssignedSubjectProperty, Outlook.OlUserPropertyType.olText).Value = subject; // used to prevent firing "new item event" newItem.UserProperties.Add(_RestoredFromDeletedProperty, Outlook.OlUserPropertyType.olInteger).Value = 1; newItem.Save(); if (ex.Message != "Aborted") { MessageBox.Show(ex.Message, "TimeReporting"); } } DeleteProperty(aitem, _TaskIdProperty); DeleteProperty(aitem, _TimeReportedProperty); DeleteProperty(aitem, _PreviousAssignedSubjectProperty); aitem.Categories = ""; aitem.Subject = Helpers.GenerateSubject(id, ""); aitem.Save(); } else if (task.mode == DelayedReporting.Mode.New) { if (aitem.UserProperties.Find(_TimeReportedProperty) != null && aitem.UserProperties.Find(_TaskIdProperty) != null) { return; } if (aitem.UserProperties.Find(_MyLastModificationTimeProperty) != null && aitem.UserProperties.Find(_MyLastModificationTimeProperty).Value >= aitem.LastModificationTime) { return; } int id = Helpers.ParseID(aitem.Subject); if (id == 0) { return; } try { int duration = aitem.Duration; string user_title = Helpers.ParseTitle(aitem.Subject); if (user_title != "") { user_title += " "; } string work_item_title = _tfs.IncreaseReportedTime(id, duration, Settings.showMessageBoxWhenAppointementCreated); UpdateAppointment(aitem, id, user_title + work_item_title, work_item_title, duration); } catch (Exception ex) { aitem.UserProperties.Add(_MyLastModificationTimeProperty, Outlook.OlUserPropertyType.olDateTime).Value = DateTime.Now.AddSeconds(5); aitem.Save(); if (ex.Message != "Aborted") { MessageBox.Show(ex.Message, "TimeReporting"); } } } else if (task.mode == DelayedReporting.Mode.Edit) { if (aitem.UserProperties.Find(_TimeReportedProperty) == null || aitem.UserProperties.Find(_TaskIdProperty) == null) { return; } int duration = aitem.UserProperties.Find(_TimeReportedProperty).Value; int new_duration = aitem.Duration; if (new_duration == duration) { return; } try { int id = aitem.UserProperties.Find(_TaskIdProperty).Value; string user_title = Helpers.ParseTitleWithTime(aitem.Subject); string work_item_title = _tfs.IncreaseReportedTime(id, new_duration - duration, Settings.showMessageBoxWhenAppointementEdited); UpdateAppointment(aitem, id, user_title, work_item_title, new_duration); } catch (Exception ex) { aitem.Duration = aitem.UserProperties.Find(_TimeReportedProperty).Value; aitem.Save(); if (ex.Message != "Aborted") { MessageBox.Show(ex.Message, "TimeReporting"); } } } }