view Agendas/trunk/src/Agendas.Google/GCalendarAdapter.cs @ 150:19bab30521da

Pasando DateTime a un EditorTemplates
author Nelo@Guinea.neluz.int
date Tue, 02 Aug 2011 23:24:19 -0300
parents 0ea32a748453
children 734d3f0853bf
line wrap: on
line source

using System;
using Google.GData.Calendar;
using Google.GData.Extensions;

namespace AltNetHispano.Agendas.Google
{
    public class GCalendarAdapter : IGCalendarAdapter
    {
        private readonly CalendarService _service;
        private readonly Uri _feedUri;

        public bool CreateEvent(IGEventDetail gEventDetail, out string message)
        {
            try
            {
                var entry = new EventEntry
                {
                    Title = { Text = gEventDetail.Title },
                    Content = { Content = gEventDetail.Summary }
                };

                var eventLocation = new Where
                {
                    ValueString = gEventDetail.Location
                };

                entry.Locations.Add(eventLocation);

                var eventTime = new When(gEventDetail.StartEvent, gEventDetail.EndEvent);
                entry.Times.Add(eventTime);

                _service.Insert(_feedUri, entry);
                message = "Event create successful";
                return true;
            }
            catch (Exception exception)
            {
                message = exception.Message;
                return false;
            }
        }

        public bool DeleteEvent(DateTime startEvent, DateTime endEvent, out string message)
        {
            try
            {
                var myQuery = new EventQuery
                {
                    StartTime = startEvent,
                    EndTime = endEvent,
                    Uri = _feedUri
                };

                var myResultsFeed = _service.Query(myQuery);
                if (myResultsFeed.Entries.Count > 0)
                {
                    myResultsFeed.Entries[0].Delete();
                    message = "Event delete successful";
                    return true;
                }

                message = "Event not found";
                return false;
            }
            catch (Exception exception)
            {
                message = exception.Message;
                return false;
            }
        }

        public GCalendarAdapter(string applicationName ,string userName, string password, string calendarId)
        {
            _service = new CalendarService(applicationName);
            _service.setUserCredentials(userName, password);
            _feedUri = new Uri("https://www.google.com/calendar/feeds/" + calendarId + "/private/full");
        }
    }
}