ChannelContact ParseFbContact(FbContact fbContact)
        {
            ChannelContact contact = new ChannelContact();

            contact.Person.Firstname = fbContact.Firstname;
            contact.Person.Lastname = fbContact.Lastname;

            contact.Profile.ChannelProfileKey = fbContact.UserId;
            contact.Profile.ProfileType = ProfileType.Social;
            contact.Profile.SourceAddress = new SourceAddress(fbContact.UserId, contact.Person.Name);

            if (!String.IsNullOrEmpty(fbContact.AvatarSquareUrl.Trim()))
            {
                ChannelAvatar avatar = new ChannelAvatar();
                avatar.Url = fbContact.AvatarSquareUrl;
                avatar.ContentStream = WebContentStreamHelper.GetContentStream(avatar.Url);

                contact.Profile.ChannelAvatar = avatar;
            }

            return contact;
        }
        ChannelContact ParseContact(XElement source)
        {
            ChannelContact contact = new ChannelContact();

            contact.Person.Firstname = source.Element("firstname").Value;
            contact.Person.Lastname = source.Element("lastname").Value;
            contact.Person.Gender = source.Element("gender").Value;

            contact.Profile.ScreenName = contact.Person.Name;
            contact.Profile.ChannelProfileKey = source.Element("userid").Value;
            contact.Profile.ProfileType = ProfileType.Social;
            contact.Profile.Url = source.Element("url").Value;
            contact.Profile.SourceAddress =
                new SourceAddress(source.Element("userid").Value, contact.Person.Name);

            if (source.Element("cityname") != null)
                contact.Profile.City = source.Element("cityname").Value;

            if (source.Element("countryname") != null)
                contact.Profile.Country = source.Element("countryname").Value;

            if (source.Element("countryname") != null)
                contact.Profile.Country = source.Element("countryname").Value;

            if (source.Element("birthday") != null &&
                !(String.IsNullOrEmpty(source.Element("birthday").Element("year").Value)) &&
                !(String.IsNullOrEmpty(source.Element("birthday").Element("month").Value)) &&
                !(String.IsNullOrEmpty(source.Element("birthday").Element("day").Value)))
            {
                contact.Person.DateOfBirth = new DateTime(
                        Int32.Parse(source.Element("birthday").Element("year").Value),
                        Int32.Parse(source.Element("birthday").Element("month").Value),
                        Int32.Parse(source.Element("birthday").Element("day").Value)
                    );
            }

            if (source.Element("profilepicture") != null)
            {
                string url = source.Element("profilepicture").Element("icon_extralarge").Element("src").Value;

                ChannelAvatar avatar = new ChannelAvatar();
                avatar.Url = url;
                avatar.ContentStream = WebContentStreamHelper.GetContentStream(avatar.Url);

                contact.Profile.ChannelAvatar = avatar;
            }

            return contact;
        }
        public IEnumerable<ChannelContact> GetContacts()
        {
            var cred = CredentialsProvider.GetCredentials();
            var rs = new RequestSettings("Tabdeelee-Inbox2-1", cred.Claim, cred.Evidence) { AutoPaging = true };
            var cr = new ContactsRequest(rs);

            var feed = cr.GetContacts();

            foreach (Contact entry in feed.Entries)
            {
                ChannelContact contact = new ChannelContact();

                contact.Person.Name = entry.Title;
                contact.Profile.ChannelProfileKey = entry.Id;

                if (entry.Phonenumbers.Count > 0)
                {
                    var phone = entry.Phonenumbers.First();
                    contact.Profile.PhoneNr = phone.Value;
                }

                if (entry.PrimaryEmail != null)
                    contact.Profile.SourceAddress = new SourceAddress(entry.PrimaryEmail.Address, contact.Person.Name);

                try
                {
                    // Check for 404 with custom httpclient on photourl since regular HttpWebClient keeps throwing exceptions
                    //var token = cr.Service.QueryAuthenticationToken();
                    //var code = HttpStatusClient.GetStatusCode(entry.PhotoUri.ToString(),
                    //    new Dictionary<string, string> { { "Authorization", "GoogleLogin auth=" + token }});

                    //if (code.HasValue && code == 200)
                    //{
                        IGDataRequest request = cr.Service.RequestFactory.CreateRequest(GDataRequestType.Query, entry.PhotoUri);
                        request.Execute();

                        using (var avatarstream = request.GetResponseStream())
                        {
                            if (avatarstream != null)
                            {
                                ChannelAvatar avatar = new ChannelAvatar();

                                // Copy avatarstream to a new memorystream because the source
                                // stream does not support seek operations.
                                MemoryStream ms = new MemoryStream();
                                avatarstream.CopyTo(ms);

                                avatar.Url = entry.PhotoUri.ToString();
                                avatar.ContentStream = ms;

                                contact.Profile.ChannelAvatar = avatar;
                            }
                        }
                    //}
                }
                catch (CaptchaRequiredException)
                {
                    // Since GMail will keep on raising CaptchaRequiredException, break out here
                    // todo let the user know in some way or another?
                    yield break;
                }
                catch (Exception)
                {

                }

                yield return contact;
            }
        }