Ejemplo n.º 1
0
        private bool WriteLn(Exception ex, CalendarItem item)
        {
            try
            {
#if DEBUG
                Debug.WriteLine(ex);
                m_builder.AppendLine(DateTime.Now.ToString("G") + " - " + ex);
                m_builder.AppendLine(DateTime.Now.ToString("G") + " - " + $"Calendar Item: {item}");

                RefreshStream?.Invoke(this, m_builder.ToString());
#endif
                m_writer.WriteLine(DateTime.Now.ToString("G") + " - " + ex);
                m_writer.WriteLine(DateTime.Now.ToString("G") + " - " + $"Calendar Item: {item}");
                m_writer.Flush();

                var client  = new RestClient("http://webapi.gamsapps.com");
                var request = new RestRequest("api/Issues/FromForm", Method.POST);
                var builder = new StringBuilder();
                var exeAss  = Assembly.GetExecutingAssembly();

                builder.AppendLine("## Exception:");
                builder.AppendLine("**Version:** " + exeAss.GetName().Version);
                builder.AppendLine("**Message:** " + ex.Message);
                builder.AppendLine("**Source:** " + ex.Source);
                builder.AppendLine("**Target Site:** " + ex.TargetSite);
                builder.AppendLine("**Stack Trace:**");
                builder.AppendLine("```\n\r" + ex.StackTrace + "\n\r```");

#if DEBUG
                builder.AppendLine("\n\r##Calendar Item:");
                builder.AppendLine(item.ToString());
#endif

                request.AddParameter("summary", "O2G Calendar Sync Exception");
                request.AddParameter("dateOfDiscovery", DateTime.Now.ToString("G"));
                request.AddParameter("application", "O2G Calendar Sync");
                request.AddParameter("details", builder.ToString());
                request.AddParameter("category", 2);

                IRestResponse response = client.Execute(request);
                var           code     = response.StatusCode;

                return(true);
            }
            catch (Exception e)
            {
                Debug.Write(e);
            }

            return(false);
        }
Ejemplo n.º 2
0
        public void UpdateAppointment(CalendarItem ev)
        {
            try
            {
                var oldId = ev.CalendarItemIdentifier;
                Log.Write($"Updating recurring Outlook appointment, {ev.Subject}");

                if (!string.IsNullOrEmpty(ev.CalendarItemIdentifier.OutlookEntryId))
                {
                    if (CurrentApplication.Session.GetItemFromID(ev.CalendarItemIdentifier.OutlookEntryId, m_folder.Store.StoreID) is
                        AppointmentItem item)
                    {
                        if (item.RecurrenceState == OlRecurrenceState.olApptMaster)
                        {
                            AppointmentItem temp = ev.GetOutlookAppointment(
                                CurrentApplication.CreateItem(OlItemType.olAppointmentItem));
                            var newMaster = temp.Move(m_folder);

                            item.Delete();
                            newMaster.Save();

                            ev.CalendarItemIdentifier = new Identifier(oldId.GoogleId, oldId.GoogleICalUId, newMaster.EntryID, newMaster.GlobalAppointmentID, EventHasher.GetHash(ev));

                            Marshal.ReleaseComObject(temp);
                            Marshal.ReleaseComObject(newMaster);
                        }
                        else if (item.RecurrenceState == OlRecurrenceState.olApptNotRecurring)
                        {
                            ev.GetOutlookAppointment(item);
                            item.Save();

                            ev.CalendarItemIdentifier = new Identifier(oldId.GoogleId, oldId.GoogleICalUId, item.EntryID, item.GlobalAppointmentID, EventHasher.GetHash(ev));
                        }

                        Marshal.ReleaseComObject(item);
                    }
                }

                ev.Action &= ~CalendarItemAction.GeneratedId;
                ev.Action &= ~CalendarItemAction.OutlookUpdate;

                Log.Write($"Finished updating Outlook appointment, {ev.Subject}");

                Archiver.Instance.UpdateIdentifier(oldId, ev.CalendarItemIdentifier);
            } catch (Exception ex)
            {
                Log.Write(ex, ev);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Performs the actual pulling of events and appointments, compares them, and returns a list of the ones with differences.
        /// </summary>
        /// <param name="byDate">Do you want to restrict the query by date?</param>
        /// <param name="start">The start date of the restriction</param>
        /// <param name="end">The end date of the restriction</param>
        /// <returns>A list of calendar items that need to be updated or added.</returns>
        public List <CalendarItem> GetFinalList(bool byDate = false, DateTime start = default(DateTime), DateTime end = default(DateTime), bool forceContentsEqual = false)
        {
            List <CalendarItem> outlookList;
            List <CalendarItem> googleList;

            if (byDate)
            {
                outlookList = m_outlookSync.PullListOfAppointmentsByDate(start, end);
                googleList  = m_googleSync.PullListOfAppointmentsByDate(start, end);
            }
            else if (IsUsingSyncToken)
            {
                googleList  = m_googleSync.PullListOfAppointmentsBySyncToken();
                outlookList = new List <CalendarItem>();
                foreach (var calendarItem in googleList)
                {
                    // Skip items that are not tracked yet. They'll need to get added to Outlook.
                    if (string.IsNullOrEmpty(calendarItem.CalendarItemIdentifier.OutlookEntryId))
                    {
                        continue;
                    }

                    if (OutlookSync.Syncer.CurrentApplication.Session.GetItemFromID(
                            calendarItem.CalendarItemIdentifier.OutlookEntryId) is AppointmentItem appt)
                    {
                        var calItem = new CalendarItem();
                        calItem.LoadFromOutlookAppointment(appt);
                        outlookList.Add(calItem);

                        Marshal.ReleaseComObject(appt);
                    }
                }
            }
            else
            {
                outlookList = m_outlookSync.PullListOfAppointments();
                googleList  = m_googleSync.PullListOfAppointments();
            }

            // Check to see what events need to be added to google from outlook
            var finalList = CompareLists(outlookList, googleList, forceContentsEqual);

#if DEBUG
            WriteToLog(outlookList, "Outlook List Log.rtf");
            WriteToLog(googleList, "Google List Log.rtf");
            WriteToLog(finalList, "Final List Log.rtf");
#endif
            return(finalList);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Pulls a list of appointments for the set calendar between a given date range.
        /// </summary>
        /// <param name="startDate">The start of the date range</param>
        /// <param name="endDate">The end of the date range</param>
        /// <returns>List of CalendarItems with the startDate and endDate</returns>
        public List <CalendarItem> PullListOfAppointmentsByDate(DateTime startDate, DateTime endDate)
        {
            try
            {
                PerformAuthentication();

                List <CalendarItem> items = new List <CalendarItem>();

                Log.Write($"Pulling a list of Google Appointments by date from {m_currentCalendar}.");

                // Iterate over the events in the specified calendar
                string pageToken = null;
                do
                {
                    EventsResource.ListRequest list = m_service.Events.List(m_currentCalendar);
                    list.TimeMin   = startDate;
                    list.TimeMax   = endDate;
                    list.PageToken = pageToken;

                    Events       events = list.Execute();
                    List <Event> i      = events.Items.ToList();

                    foreach (var @event in i)
                    {
                        if (@event.Status.Equals("cancelled"))
                        {
                            continue;
                        }

                        var cal = new CalendarItem();
                        cal.LoadFromGoogleEvent(@event);
                        if (!items.Exists(x => x.CalendarItemIdentifier.GoogleId.Equals(cal.CalendarItemIdentifier.GoogleId)))
                        {
                            items.Add(cal);
                        }
                    }

                    pageToken = events.NextPageToken;
                } while (pageToken != null);

                return(items);
            } catch (GoogleApiException ex)
            {
                Log.Write(ex);
                HandleException(ex, "There was an error when trying to pull a list of events from google.");
                return(null);
            }
        }
Ejemplo n.º 5
0
        //public CalendarItem FindEvent( string gid )
        //{
        //    Log.Write( "Looking up an Outlook appointment using the ID" );
        //    foreach ( var i in m_folder.Items )
        //    {
        //        var cal = new CalendarItem();
        //        cal.LoadFromOutlookAppointment( (AppointmentItem) i );
        //        if ( cal.ID.Equals( gid ) )
        //        {
        //            Log.Write( $"Found an Outlook appointment with ID, {gid}" );
        //            return cal;
        //        }
        //    }

        //    Log.Write( $"Unable to find an Outlook appointment with ID, {gid}" );
        //    return null;
        //}

        public CalendarItem FindEventByEntryId(string entryId)
        {
            try
            {
                Log.Write("Looking up an Outlook appointment using the EntryId");
                var appt = (AppointmentItem)CurrentApplication.Session.GetItemFromID(entryId, m_folder.Store.StoreID);
                if (appt != null)
                {
                    var calEvent = new CalendarItem();
                    calEvent.LoadFromOutlookAppointment(appt);

                    Log.Write($"Found an Outlook appointment with EntryID, {entryId}");
                    Marshal.ReleaseComObject(appt);
                    return(calEvent);
                }
            } catch (COMException ex)
            {
                Log.Write(ex);
            }
            Log.Write($"Unable to find an Outlook appointment with EntryID, {entryId}");
            return(null);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Deletes all appointments that share the same ID provided by CalendarItem
        /// </summary>
        /// <param name="ev">The CalendarItem to be deleted</param>
        public void DeleteAppointment(CalendarItem ev)
        {
            try
            {
                PerformAuthentication();

                if (ev.Recurrence != null)
                {
                    string pageToken = null;
                    do
                    {
                        var instancesRequest = m_service.Events.Instances(m_currentCalendar, ev.CalendarItemIdentifier.GoogleId);
                        instancesRequest.PageToken = pageToken;

                        var events = instancesRequest.Execute();
                        pageToken = events.NextPageToken;

                        var list = events.Items.ToList();
                        foreach (var @event in list)
                        {
                            m_service.Events.Delete(m_currentCalendar, @event.Id).Execute();
                        }

                        Log.Write($"Deleted Google recurring appointment {ev.Subject}.");
                    } while (pageToken != null);
                }
                else
                {
                    m_service.Events.Delete(m_currentCalendar, ev.CalendarItemIdentifier.GoogleId).Execute();
                    Log.Write($"Deleted Google appointment {ev.Subject}.");
                }

                Archiver.Instance.Delete(ev.CalendarItemIdentifier);
            } catch (GoogleApiException ex)
            {
                Log.Write(ex, ev);
                HandleException(ex, "There was an error when trying to delete an event from google.", ev, RetryAction.Delete);
            }
        }
Ejemplo n.º 7
0
 public static bool Write(Exception ex, CalendarItem item)
 {
     return(Instance.WriteLn(ex, item));
 }
Ejemplo n.º 8
0
        public void LoadData(CalendarItem outlook, CalendarItem google)
        {
            OutlookSubject_LBL.Text = outlook.Subject;
            GoogleSubject_LBL.Text  = google.Subject;

            //////////////////////////////////////////////////////////////////////////
            // Outlook Changes
            //////////////////////////////////////////////////////////////////////////

            if (outlook.Changes.HasFlag(CalendarItemChanges.StartDate))
            {
                AppendOutlook_RTBText("Start Date:\n\t" + DateTime.Parse(outlook.Start).ToString("R"));
            }

            if (outlook.Changes.HasFlag(CalendarItemChanges.EndDate))
            {
                AppendOutlook_RTBText("End Date:\n\t" + DateTime.Parse(outlook.End).ToString("R"));
            }

            if (outlook.Changes.HasFlag(CalendarItemChanges.Location))
            {
                AppendOutlook_RTBText("Location:\n\t" + outlook.Location);
            }

            if (outlook.Changes.HasFlag(CalendarItemChanges.Body))
            {
                AppendOutlook_RTBText("Body:\n\t" + outlook.Body);
            }

            if (outlook.Changes.HasFlag(CalendarItemChanges.Subject))
            {
                AppendOutlook_RTBText("Subject:\n\t" + outlook.Subject);
            }

            if (outlook.Changes.HasFlag(CalendarItemChanges.StartTimeZone))
            {
                AppendOutlook_RTBText("Start Time Zone:\n\t" + outlook.StartTimeZone);
            }

            if (outlook.Changes.HasFlag(CalendarItemChanges.EndTimeZone))
            {
                AppendOutlook_RTBText("End Time Zone:\n\t" + outlook.EndTimeZone);
            }

            if (outlook.Changes.HasFlag(CalendarItemChanges.ReminderTime))
            {
                AppendOutlook_RTBText("Reminder Time:\n\t" + outlook.ReminderTime);
            }

            if (outlook.Changes.HasFlag(CalendarItemChanges.Attendees))
            {
                AppendOutlook_RTBText("Attendees:");
                foreach (var outlookAttendee in outlook.Attendees)
                {
                    AppendOutlook_RTBText("\n\t" + outlookAttendee);
                }
            }

            if (outlook.Changes.HasFlag(CalendarItemChanges.Recurrence))
            {
                AppendOutlook_RTBText("Recurrence:\n" + outlook.Recurrence);
            }

            if (outlook.Changes == CalendarItemChanges.Nothing)
            {
                throw new Exception("Why is the differences form being shown if there are no differences.\nOutlook:\n" + outlook + "\nGoogle\n" + google);
            }

            //////////////////////////////////////////////////////////////////////////
            // Google Changes
            //////////////////////////////////////////////////////////////////////////

            if (google.Changes.HasFlag(CalendarItemChanges.StartDate))
            {
                AppendGoogle_RTBText("Start Date:\n\t" + DateTime.Parse(google.Start).ToString("R"));
            }

            if (google.Changes.HasFlag(CalendarItemChanges.EndDate))
            {
                AppendGoogle_RTBText("End Date:\n\t" + DateTime.Parse(google.End).ToString("R"));
            }

            if (google.Changes.HasFlag(CalendarItemChanges.Location))
            {
                AppendGoogle_RTBText("Location:\n\t" + google.Location);
            }

            if (google.Changes.HasFlag(CalendarItemChanges.Body))
            {
                AppendGoogle_RTBText("Body:\n\t" + google.Body);
            }

            if (google.Changes.HasFlag(CalendarItemChanges.Subject))
            {
                AppendGoogle_RTBText("Subject:\n\t" + google.Subject);
            }

            if (google.Changes.HasFlag(CalendarItemChanges.StartTimeZone))
            {
                AppendGoogle_RTBText("Start Time Zone:\n\t" + google.StartTimeZone);
            }

            if (google.Changes.HasFlag(CalendarItemChanges.EndTimeZone))
            {
                AppendGoogle_RTBText("End Time Zone:\n\t" + google.EndTimeZone);
            }

            if (google.Changes.HasFlag(CalendarItemChanges.ReminderTime))
            {
                AppendGoogle_RTBText("Reminder Time:\n\t" + google.ReminderTime);
            }

            if (google.Changes.HasFlag(CalendarItemChanges.Attendees))
            {
                AppendGoogle_RTBText("Attendees:");
                foreach (var googleAttendee in google.Attendees)
                {
                    AppendGoogle_RTBText("\n\t" + googleAttendee);
                }
            }

            if (google.Changes.HasFlag(CalendarItemChanges.Recurrence))
            {
                AppendGoogle_RTBText("Recurrence\n" + google.Recurrence);
            }

            if (google.Changes == CalendarItemChanges.Nothing)
            {
                throw new Exception("Why is the differences form being shown if there are no differences.\nOutlook:\n" + outlook + "\nGoogle\n" + google);
            }
        }
Ejemplo n.º 9
0
 public static string GetHash(CalendarItem evnt)
 {
     return(CreateHash(evnt.GetHasherString()));
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Creates a Recurrence object
        /// </summary>
        /// <param name="rrule">The Google recurrence string</param>
        /// <param name="calItem">The CalendarItem currently representing the Google Event</param>
        public Recurrence(string rrule, CalendarItem calItem)
        {
            try {
                // "RRULE:FREQ=WEEKLY;UNTIL=20160415T200000Z;BYDAY=WE,FR"
                var items = rrule.Replace("RRULE:", "").Split(';');

                // Pull all the information from the RRULE string
                foreach (var item in items)
                {
                    var split = item.Split('=');
                    switch (split[0])
                    {
                    case "FREQ":

                        if (split[1] == "DAILY")
                        {
                            Type = RecurrenceType.Daily;
                        }
                        else if (split[1] == "WEEKLY")
                        {
                            Type = RecurrenceType.Weekly;
                        }
                        else if (split[1] == "MONTHLY")
                        {
                            Type = RecurrenceType.Monthly;
                        }
                        else if (split[1] == "YEARLY")
                        {
                            Type = RecurrenceType.Yearly;
                        }

                        break;

                    case "UNTIL":
                        var date = (calItem.IsAllDayEvent && split[1].Length == 8)
                                ? DateTime.ParseExact(split[1], "yyyyMMdd", CultureInfo.InvariantCulture)
                                : DateTime.ParseExact(split[1], "yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture);
                        PatternEndDate = date.ToString("yyyy-MM-dd");
                        EndTime        = date.ToString("HH:mm:sszzz");
                        break;

                    case "BYDAY":
                        var days = split[1].Split(',');

                        foreach (var dday in days)
                        {
                            // This ensures we grab the right day of the month
                            var day = dday;
                            if (day.Length == 3)
                            {
                                Instance = int.Parse(day.Substring(0, 1));
                                day      = day.Remove(0, 1);
                            }
                            else if (day.Length == 4)
                            {
                                Instance = int.Parse(day.Substring(0, 2));
                                day      = day.Remove(0, 2);
                            }

                            if (day.Equals("MO"))
                            {
                                DaysOfTheWeekMask |= DaysOfWeek.Monday;
                            }
                            else if (day.Equals("TU"))
                            {
                                DaysOfTheWeekMask |= DaysOfWeek.Tuesday;
                            }
                            else if (day.Equals("WE"))
                            {
                                DaysOfTheWeekMask |= DaysOfWeek.Wednesday;
                            }
                            else if (day.Equals("TH"))
                            {
                                DaysOfTheWeekMask |= DaysOfWeek.Thursday;
                            }
                            else if (day.Equals("FR"))
                            {
                                DaysOfTheWeekMask |= DaysOfWeek.Friday;
                            }
                            else if (day.Equals("SA"))
                            {
                                DaysOfTheWeekMask |= DaysOfWeek.Saturday;
                            }
                            else if (day.Equals("SU"))
                            {
                                DaysOfTheWeekMask |= DaysOfWeek.Sunday;
                            }
                        }

                        break;

                    case "BYMONTH":
                        MonthOfYear = int.Parse(split[1]);
                        break;

                    case "INTERVAL":
                        Interval = int.Parse(split[1]);
                        break;

                    case "COUNT":
                        Occurrences = int.Parse(split[1]);
                        break;
                    } // switch
                }     // foreach

                if (Type == RecurrenceType.Daily && Interval == 0)
                {
                    Interval = 1;
                }

                if (Type == RecurrenceType.Weekly && Interval == 0)
                {
                    Interval = 1;
                }

                if (Type == RecurrenceType.Monthly && Interval == 0)
                {
                    Interval = 1;
                    if (Instance == 0)
                    {
                        DayOfMonth = DateTime.Parse(calItem.Start).Day;
                    }
                    else
                    {
                        Type = RecurrenceType.MonthNth;
                    }
                }

                if (Type == RecurrenceType.Yearly)
                {
                    var date = DateTime.Parse(calItem.Start);
                    DayOfMonth  = date.Day;
                    Interval    = 12;
                    MonthOfYear = date.Month;
                }

                var startDate = (calItem.IsAllDayEvent && calItem.Start.Length == 10)
                    ? DateTime.ParseExact(calItem.Start, "yyyy-MM-dd", CultureInfo.InvariantCulture)
                    : DateTime.ParseExact(calItem.Start, "yyyy-MM-ddTHH:mm:sszzz", CultureInfo.InvariantCulture);
                PatternStartDate = startDate.ToString("yyyy-MM-dd");
                StartTime        = startDate.ToString("HH:mm:sszzz");

                if (Type == RecurrenceType.Weekly && DaysOfTheWeekMask == 0)
                {
                    DaysOfTheWeekMask = (DaysOfWeek)(1 << (int)startDate.DayOfWeek);
                }

                // If there is no specified end date set NoEndDate to true
                if (string.IsNullOrEmpty(PatternEndDate))
                {
                    NoEndDate = true;
                }

                if (string.IsNullOrEmpty(EndTime) && !calItem.IsAllDayEvent && calItem.End.Length > 10)
                {
                    EndTime = calItem.End.Substring(11);
                }
            } catch (Exception ex) {
                Log.Write("An Error occured when trying to parse Google Recurrence data, " + ex.Message);
            }
        }