예제 #1
0
 public void MakeAppointment(
     [CommandParameter("Calendar")] ICalendarFacet calendar,
     [CommandParameter("With")] IContactFacet contact,
     [CommandParameter("Time")] IDateTimeFacet time,
     [CommandParameter("Duration", Optional = true)] IDurationFacet duration,
     [CommandParameter("Subject", Optional = true)] ITextFacet subject)
 {
     calendar.MakeAppointment(contact, time, duration, subject);
 }
예제 #2
0
        public void MakeAppointment(IContactFacet contact, IDateTimeFacet at, IDurationFacet duration, ITextFacet subject)
        {
            if (contact == null || at == null)
            {
                return;
            }

            var store    = ExtensionHooks.GetKeyValueStore();
            var username = store.GetString("username");
            var password = store.GetProtectedString("password");

            if (username == null || password == null)
            {
                throw new RequiresConfigurationException(typeof(Configurators.CredentialsConfigurator), typeof(CalendarFacet));
            }

            var cal = new CalendarService("Commando");

            cal.setUserCredentials(username, password);

            var finalDuration = duration != null ? duration.Duration : TimeSpan.FromHours(1);

            var e = new EventEntry(subject == null ? "Appointment" : subject.Text);

            e.Times.Add(new When(at.Value, at.Value.Add(finalDuration)));
            var who = new Who();

            who.Email = contact.Email;
            who.Rel   = Who.RelType.EVENT_ATTENDEE;
            e.Participants.Add(who);

            try
            {
                cal.Insert(new Uri("https://www.google.com/calendar/feeds/default/private/full"), e);
            }
            catch (CaptchaRequiredException)
            {
                throw new InvalidOperationException(
                          "Google has locked the account. Please visit http://www.google.com/accounts/DisplayUnlockCaptcha to unlock it.");
            }
            catch (InvalidCredentialsException)
            {
                throw new RequiresConfigurationException(typeof(Configurators.CredentialsConfigurator), typeof(CalendarFacet),
                                                         "The username and/or password are incorrect.");
            }
            catch (ClientFeedException ex)
            {
                throw new InvalidOperationException(string.Format("Could not add the appointment: {0}", ex.Message));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Could not add the appointment.", ex);
            }
        }