コード例 #1
0
ファイル: GoogleService.cs プロジェクト: seniorOtaka/ndoctor
        /// <summary>
        /// Adds the specified appointment into the configured Google Calendar.
        /// This action is done into a thread and the action will be done when it's done
        /// </summary>
        /// <param name="appointment">The appointment to insert.</param>
        public void AddAppointment(Appointment appointment)
        {
            var context = TaskScheduler.FromCurrentSynchronizationContext();
            var token = new CancellationTokenSource().Token;
            var task = System.Threading.Tasks.Task.Factory.StartNew(() =>
               {
                   try
                   {
                       var service = this.GetService();

                       var entry = new EventEntry();
                       entry.Title.Text = appointment.Subject;
                       entry.Times.Add(new When()
                       {
                           StartTime = appointment.StartTime,
                           EndTime = appointment.EndTime,
                       });

                       var postUri = new Uri(this.CalendarUri);
                       entry = service.Insert(postUri, entry) as EventEntry;

                       entry.ExtensionElements.Add(new ExtendedProperty()
                       {
                           Name = SynchronisationProperty,
                           Value = appointment.GoogleSynchronisationId.ToString(),
                       });
                       entry.Update();
                       this.Logger.Info("Appointment correctly added into Google Calendar.");
                   }
                   catch (Exception ex) { throw new GoogleCalendarException(ex.Message, ex); }
               });
            task.ContinueWith(t => this.Logger.Warn(t.Exception.InnerException.InnerException), token, TaskContinuationOptions.OnlyOnFaulted, context);
        }
コード例 #2
0
ファイル: GoogleService.cs プロジェクト: seniorOtaka/ndoctor
        /// <summary>
        /// Removes the specified appointment into the configured Google Calendar.
        /// This action is done into a thread and the action will be done when it's done.
        /// </summary>
        /// <param name="appointment">The appointment.</param>
        public void RemoveAppointment(Appointment appointment)
        {
            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                try
                {
                    //If the appointment to remove is binded to a Google Calendar appointment then do nothing
                    if (appointment.GoogleSynchronisationId == null || appointment.GoogleSynchronisationId == new Guid()) { return; }

                    var service = GetService();
                    var query = new EventQuery()
                    {
                        Uri = new Uri(this.CalendarUri),
                        ExtraParameters = string.Format("extq=[{0}:{1}]", SynchronisationProperty, appointment.GoogleSynchronisationId),
                    };

                    var feed = service.Query(query) as EventFeed;

                    var appointments = new List<EventEntry>();
                    while (feed != null && feed.Entries.Count > 0)
                    {
                        foreach (EventEntry entry in feed.Entries)
                        {
                            appointments.Add(entry);
                        }

                        if (feed.NextChunk != null)
                        {
                            query.Uri = new Uri(feed.NextChunk);
                            feed = service.Query(query) as EventFeed;
                        }
                        else { feed = null; }

                        if (appointments.Count == 1)
                        {
                            foreach (var a in appointments)
                            {
                                a.Delete();
                                this.Logger.Info("Appointment correctly removed from Google Calendar.");
                            }
                        }
                        else if (appointments.Count == 0) { this.Logger.Debug("This appointment doesn't exist in Google Calendar"); }
                        else { throw new Exception("This id exist more than once"); }
                    }
                }
                catch (Exception ex) { throw new GoogleCalendarException(ex.Message, ex); }
            });
        }