示例#1
0
        private CardDavResponse SimplifyResponse(string results)
        {
            XmlDocument document = new XmlDocument();
            document.Load(new StringReader(results));

            XmlNodeList responses = document.GetElementsByTagName("response");
            CardDavResponse response = new CardDavResponse();
            Uri serverUri = new Uri(this.serverUrl);

            foreach (XmlNode node in responses)
            {
                string contentType = CardDavParser.GetNodeContents(node, "getcontenttype").FirstOrDefault();
                XmlNode addressBookNode = CardDavParser.GetNodesByTagName(node, "addressbook").FirstOrDefault();
                string href = CardDavParser.GetNodeContents(node, "href").FirstOrDefault();

                if (!String.IsNullOrEmpty(contentType) && !String.IsNullOrEmpty(href) && (contentType.IndexOf("vcard") > -1 || href.IndexOf("vcf") > -1))
                {
                    //It is a vcard element

                    Uri hrefUri = new Uri("https://localhost/" + href);
                    string id = Path.GetFileName(hrefUri.LocalPath).Replace(".vcf", "");
                    string idWithExtension = Path.GetFileName(hrefUri.LocalPath);
                    string hrefWithoutIdWithoutVcf = href.Replace(id, "").Replace(".vcf", "");
                    Uri addressBookUrlForCard = new Uri(this.ServerAddress + hrefWithoutIdWithoutVcf);

                    if (!String.IsNullOrEmpty(id))
                    {
                        CardElement card = new CardElement();

                        card.Id = id;
                        card.DavName = idWithExtension;
                        card.Url = addressBookUrlForCard;

                        string etag = CardDavParser.GetNodeContents(node, "getetag").FirstOrDefault();

                        if(!String.IsNullOrEmpty(etag))
                            card.eTag = etag.Replace("\"", "");

                        string dateString = CardDavParser.GetNodeContents(node, "getlastmodified").FirstOrDefault();

                        if (!String.IsNullOrEmpty(dateString))
                            card.Modified = DateTime.Parse(dateString);
                        else
                            card.Modified = DateTime.Now;

                        response.Cards.Add(card);
                    }
                }
                else if(addressBookNode != null && !String.IsNullOrEmpty(href))
                {
                    //It is a address book element
                    string url = this.ServerAddress + href;

                    AddressBookElement addressBook = new AddressBookElement();

                    addressBook.Url = new Uri(url);
                    addressBook.DisplayName = CardDavParser.GetNodeContents(node, "displayname").FirstOrDefault();

                    string dateString = CardDavParser.GetNodeContents(node, "getlastmodified").FirstOrDefault();

                    if (!String.IsNullOrEmpty(dateString))
                        addressBook.Modified = DateTime.Parse(dateString);
                    else
                        addressBook.Modified = DateTime.Now;

                    response.AddressBooks.Add(addressBook);
                }
            }

            return response;
        }
示例#2
0
        private CardDavResponse SimplifyResponse(string results)
        {
            XmlDocument document = new XmlDocument();
            document.Load(new StringReader(results));

            XmlNodeList responses = document.GetElementsByTagName("response");
            CardDavResponse response = new CardDavResponse();
            Uri serverUri = new Uri(this.serverUrl);

            foreach (XmlNode node in responses)
            {
                string contentType = CardDavParser.GetNodeContents(node, "getcontenttype").FirstOrDefault();
                string href = CardDavParser.GetNodeContents(node, "href").FirstOrDefault();

                if (contentType.IndexOf("vcard") > -1 || href.IndexOf("vcf") > -1)
                {
                    //It is a vcard element

                    Uri hrefUri = new Uri("https://localhost/" + href);
                    string id = Path.GetFileName(hrefUri.LocalPath).Replace(".vcf", "");
                    string hrefWithoutIdWithoutVcf = href.Replace(id, "").Replace(".vcf", "");
                    Uri addressBookUrlForCard = new Uri(serverUri.Scheme + "://" + serverUri.Host + hrefWithoutIdWithoutVcf);

                    if (!String.IsNullOrEmpty(id))
                    {
                        CardElement card = new CardElement();

                        card.Id = id;
                        card.Url = addressBookUrlForCard;
                        card.eTag = CardDavParser.GetNodeContents(node, "getetag").FirstOrDefault().Replace("\"", "");
                        card.Modified = DateTime.Parse(CardDavParser.GetNodeContents(node, "getlastmodified").FirstOrDefault());

                        response.Cards.Add(card);
                    }
                }
                else
                {
                    //It is a address book element

                    if (!serverUri.PathAndQuery.Equals(href))
                    {
                        string url = serverUri.Scheme + "://" + serverUri.Host + href;

                        AddressBookElement addressBook = new AddressBookElement();

                        addressBook.Url = new Uri(url);
                        addressBook.DisplayName = CardDavParser.GetNodeContents(node, "displayname").FirstOrDefault();
                        addressBook.Modified = DateTime.Parse(CardDavParser.GetNodeContents(node, "getlastmodified").FirstOrDefault());

                        response.AddressBooks.Add(addressBook);
                    }
                }
            }

            return response;
        }