view Agendas/trunk/src/Agendas.Google/GCalendarAdapter.cs @ 170:13b661a29242

Ticket 153: UI de Publicar. Se internacionalizaron algunos mensajes automáticos en la pantalla de Publicar.
author juanjose.montesdeocaarbos
date Sat, 06 Aug 2011 12:57:54 -0300
parents 734d3f0853bf
children
line wrap: on
line source

using System;
using AltNetHispano.Agendas.Configurations;
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) : this(applicationName,
		                                                       AgendasConfigurationManager.Publicadores.Google.Calendar.
		                                                       	UserName,
		                                                       AgendasConfigurationManager.Publicadores.Google.Calendar.
		                                                       	Password,
		                                                       AgendasConfigurationManager.Publicadores.Google.Calendar.
		                                                       	CalendarId)
		{
		}

		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");
		}
	}
}