Exemple #1
0
        public async Task <ArgbColor?> GetCalendarColorNoThrow()
        {
            try
            {
                var document = await _webDavClient.ExecuteWebDavRequestAndReadResponse(
                    _serverUrl,
                    "PROPFIND",
                    0,
                    null,
                    null,
                    "application/xml",
                    @"<?xml version='1.0'?>
                      <D:propfind xmlns:D=""DAV:"" xmlns:C=""urn:ietf:params:xml:ns:caldav"" xmlns:E=""http://apple.com/ns/ical/"">
                          <D:prop>
                              <E:calendar-color />
                          </D:prop>
                      </D:propfind>
                 "
                    );

                var calendarColorNode = document.XmlDocument.SelectSingleNode("/D:multistatus/D:response/D:propstat/D:prop/E:calendar-color", document.XmlNamespaceManager);
                if (calendarColorNode != null && calendarColorNode.InnerText.Length >= 7)
                {
                    return(ArgbColor.FromRgbaHexStringWithOptionalANoThrow(calendarColorNode.InnerText));
                }
                return(null);
            }
            catch (Exception x)
            {
                s_logger.Error(null, x);
                return(null);
            }
        }
Exemple #2
0
        public async Task <CalDavResources> GetUserResourcesNoThrow(bool useWellKnownUrl)
        {
            try
            {
                var autodiscoveryUrl = useWellKnownUrl ? AutoDiscoveryUrl : _serverUrl;

                var currentUserPrincipalUrl = await GetCurrentUserPrincipalUrl(autodiscoveryUrl);

                var calendars = new List <CalendarData>();
                var taskLists = new List <TaskListData>();

                if (currentUserPrincipalUrl != null)
                {
                    var calendarHomeSetProperties = await GetCalendarHomeSet(currentUserPrincipalUrl);

                    XmlNode homeSetNode = calendarHomeSetProperties.XmlDocument.SelectSingleNode("/D:multistatus/D:response/D:propstat/D:prop/C:calendar-home-set", calendarHomeSetProperties.XmlNamespaceManager);
                    if (homeSetNode != null && homeSetNode.HasChildNodes)
                    {
                        foreach (XmlNode homeSetNodeHref in homeSetNode.ChildNodes)
                        {
                            if (!string.IsNullOrEmpty(homeSetNodeHref.InnerText))
                            {
                                var calendarHomeSetUri = Uri.IsWellFormedUriString(homeSetNodeHref.InnerText, UriKind.Absolute) ?
                                                         new Uri(homeSetNodeHref.InnerText) :
                                                         new Uri(calendarHomeSetProperties.DocumentUri.GetLeftPart(UriPartial.Authority) + homeSetNodeHref.InnerText);

                                var calendarDocument = await ListCalendars(calendarHomeSetUri);

                                XmlNodeList responseNodes = calendarDocument.XmlDocument.SelectNodes("/D:multistatus/D:response", calendarDocument.XmlNamespaceManager);

                                foreach (XmlElement responseElement in responseNodes)
                                {
                                    var urlNode         = responseElement.SelectSingleNode("D:href", calendarDocument.XmlNamespaceManager);
                                    var displayNameNode = responseElement.SelectSingleNode("D:propstat/D:prop/D:displayname", calendarDocument.XmlNamespaceManager);
                                    if (urlNode != null && displayNameNode != null)
                                    {
                                        XmlNode isCollection = responseElement.SelectSingleNode("D:propstat/D:prop/D:resourcetype/C:calendar", calendarDocument.XmlNamespaceManager);
                                        if (isCollection != null)
                                        {
                                            var       calendarColorNode = responseElement.SelectSingleNode("D:propstat/D:prop/E:calendar-color", calendarDocument.XmlNamespaceManager);
                                            ArgbColor?calendarColor     = null;
                                            if (calendarColorNode != null && calendarColorNode.InnerText.Length >= 7)
                                            {
                                                calendarColor = ArgbColor.FromRgbaHexStringWithOptionalANoThrow(calendarColorNode.InnerText);
                                            }

                                            XmlNode supportedComponentsNode = responseElement.SelectSingleNode("D:propstat/D:prop/C:supported-calendar-component-set", calendarDocument.XmlNamespaceManager);
                                            if (supportedComponentsNode != null)
                                            {
                                                var path = urlNode.InnerText.EndsWith("/") ? urlNode.InnerText : urlNode.InnerText + "/";

                                                if (supportedComponentsNode.InnerXml.Contains("VEVENT"))
                                                {
                                                    var displayName = string.IsNullOrEmpty(displayNameNode.InnerText) ? "Default Calendar" : displayNameNode.InnerText;
                                                    calendars.Add(new CalendarData(new Uri(calendarDocument.DocumentUri, path), displayName, calendarColor));
                                                }
                                                if (supportedComponentsNode.InnerXml.Contains("VTODO"))
                                                {
                                                    var displayName = string.IsNullOrEmpty(displayNameNode.InnerText) ? "Default Tasks" : displayNameNode.InnerText;
                                                    taskLists.Add(new TaskListData(new Uri(calendarDocument.DocumentUri, path).ToString(), displayName));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return(new CalDavResources(calendars, taskLists));
            }
            catch (Exception x)
            {
                if (x.Message.Contains("404") || x.Message.Contains("405") || x is XmlException)
                {
                    return(new CalDavResources(new CalendarData[] { }, new TaskListData[] {}));
                }
                else
                {
                    throw;
                }
            }
        }
Exemple #3
0
        public async Task <IReadOnlyList <Tuple <Uri, string, ArgbColor?> > > GetUserCalendarsNoThrow(bool useWellKnownUrl)
        {
            try
            {
                var autodiscoveryUrl = useWellKnownUrl ? AutoDiscoveryUrl : _serverUrl;

                var properties = await GetCurrentUserPrincipal(autodiscoveryUrl);

                XmlNode principal = properties.XmlDocument.SelectSingleNode("/D:multistatus/D:response/D:propstat/D:prop/D:current-user-principal", properties.XmlNamespaceManager);

                // changes to handle Zoho Calendar
                // patch from Suki Hirata <*****@*****.**>
                if (null == principal)
                {
                    principal = properties.XmlDocument.SelectSingleNode("/D:multistatus/D:response/D:propstat/D:prop/D:principal-URL", properties.XmlNamespaceManager);
                }

                var cals = new List <Tuple <Uri, string, ArgbColor?> >();

                if (principal != null && !string.IsNullOrEmpty(principal.InnerText))
                {
                    properties = await GetCalendarHomeSet(new Uri (autodiscoveryUrl.GetLeftPart(UriPartial.Authority) + principal.InnerText));

                    XmlNode homeSet = properties.XmlDocument.SelectSingleNode("/D:multistatus/D:response/D:propstat/D:prop/C:calendar-home-set", properties.XmlNamespaceManager);

                    if (homeSet != null && !string.IsNullOrEmpty(homeSet.InnerText))
                    {
                        properties = await ListCalendars(new Uri (autodiscoveryUrl.GetLeftPart(UriPartial.Authority) + homeSet.InnerText));

                        XmlNodeList responseNodes = properties.XmlDocument.SelectNodes("/D:multistatus/D:response", properties.XmlNamespaceManager);

                        foreach (XmlElement responseElement in responseNodes)
                        {
                            var urlNode         = responseElement.SelectSingleNode("D:href", properties.XmlNamespaceManager);
                            var displayNameNode = responseElement.SelectSingleNode("D:propstat/D:prop/D:displayname", properties.XmlNamespaceManager);
                            if (urlNode != null && displayNameNode != null)
                            {
                                XmlNode isCollection = responseElement.SelectSingleNode("D:propstat/D:prop/D:resourcetype/C:calendar", properties.XmlNamespaceManager);
                                if (isCollection != null)
                                {
                                    var       calendarColorNode = responseElement.SelectSingleNode("D:propstat/D:prop/E:calendar-color", properties.XmlNamespaceManager);
                                    ArgbColor?calendarColor     = null;
                                    if (calendarColorNode != null && calendarColorNode.InnerText.Length >= 7)
                                    {
                                        calendarColor = ArgbColor.FromRgbaHexStringWithOptionalANoThrow(calendarColorNode.InnerText);
                                    }
                                    var uri = UriHelper.UnescapeRelativeUri(autodiscoveryUrl, urlNode.InnerText);
                                    cals.Add(Tuple.Create(uri, displayNameNode.InnerText, calendarColor));
                                }
                            }
                        }
                    }
                }
                return(cals);
            }
            catch (Exception x)
            {
                if (x.Message.Contains("404") || x.Message.Contains("405") || x is XmlException)
                {
                    return(new List <Tuple <Uri, string, ArgbColor?> >());
                }
                else
                {
                    throw;
                }
            }
        }