Beispiel #1
0
        public void UpdateContactPhoto(Uri contactURL, Stream photoStream)
        {
            Google.Contacts.Contact contact = cr.Retrieve <Google.Contacts.Contact>(contactURL);

            try
            {
                cr.SetPhoto(contact, photoStream);
            }
            catch (GDataVersionConflictException e)
            {
                // Etags mismatch: handle the exception.
            }
        }
Beispiel #2
0
        public static void UpdatePhoto(UserCredential creds, Contact contact, byte[] photo)
        {
            ContactsRequest cr = BuildContactsRequest(creds);

            try
            {
                Stream io = new MemoryStream(photo);
                cr.SetPhoto(contact, io);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Could not update picture in Google Contact");
            }
            //catch (GDataVersionConflictException e)
            //{
            //    // Etags mismatch: handle the exception.
            //}
        }
        /// <summary>
        /// Creates or updates photo information for a google contact with the information found in the std-contact.
        /// </summary>
        /// <param name="googleContact">The google contact.</param>
        /// <param name="contact">The contact containing the image information.</param>
        /// <param name="credentials">The credentials providing object.</param>
        public static void UpdatePhoto(this Contact googleContact, StdContact contact, ContactClient credentials)
        {
            if (contact.PictureData == null || contact.PictureData.Length <= 0 || string.IsNullOrEmpty(googleContact.Id))
            {
                return;
            }

            var rs = new RequestSettings("Sem.Sync.Connector.Google", credentials.LogOnUserId, credentials.LogOnPassword)
            {
                AutoPaging = true
            };
            var cr = new ContactsRequest(rs);

            using (var photoStream = new MemoryStream(contact.PictureData))
            {
                for (var i = 0; i < 3; i++)
                {
                    try
                    {
                        cr.SetPhoto(googleContact, photoStream);
                        break;
                    }
                    catch (GDataRequestException ex)
                    {
                        credentials.LogError(ex);
                        if (string.IsNullOrEmpty(ex.ResponseString))
                        {
                            break;
                        }
                    }
                    catch (CaptchaRequiredException)
                    {
                        UnlockCaptch();
                    }
                    catch (Exception ex)
                    {
                        credentials.LogError(ex);
                        break;
                    }
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////



        //////////////////////////////////////////////////////////////////////
        /// <summary>runs an authentication test, inserts a new contact</summary>
        //////////////////////////////////////////////////////////////////////
        [Test] public void ModelPhotoTest()
        {
            Tracing.TraceMsg("Entering ModelPhotoTest");

            RequestSettings rs = new RequestSettings(this.ApplicationName, this.userName, this.passWord);

            rs.AutoPaging = true;

            ContactsRequest cr = new ContactsRequest(rs);

            Feed <Contact> f = cr.GetContacts();

            Contact e = null;

            if (f != null)
            {
                Contact entry = new Contact();
                entry.AtomEntry            = ObjectModelHelper.CreateContactEntry(1);
                entry.PrimaryEmail.Address = "*****@*****.**";
                e = cr.Insert(f, entry);
            }
            Assert.IsTrue(e != null, "we should have a contact here");

            Stream s = cr.GetPhoto(e);

            Assert.IsTrue(s == null, "There should be no photo yet");


            using (FileStream fs = new FileStream(this.resourcePath + "contactphoto.jpg", System.IO.FileMode.Open))
            {
                cr.SetPhoto(e, fs);
            }

            // now delete the guy, which requires us to reload him from the server first, as the photo change operation
            // changes the etag off the entry
            e = cr.Retrieve(e);
            cr.Delete(e);
        }
        /////////////////////////////////////////////////////////////////////////////



        //////////////////////////////////////////////////////////////////////
        /// <summary>runs an authentication test, inserts a new contact</summary> 
        //////////////////////////////////////////////////////////////////////
        [Test] public void ModelPhotoTest()
        {
            Tracing.TraceMsg("Entering ModelPhotoTest");

            RequestSettings rs = new RequestSettings(this.ApplicationName, this.userName, this.passWord);
            rs.AutoPaging = true; 

            ContactsRequest cr = new ContactsRequest(rs);

            Feed<Contact> f = cr.GetContacts();

            Contact e = null;

            if (f != null)
            {
                Contact entry = new Contact();
                entry.AtomEntry = ObjectModelHelper.CreateContactEntry(1);
                entry.PrimaryEmail.Address = "*****@*****.**";
                e = cr.Insert(f, entry);
                
            }
            Assert.IsTrue(e!=null, "we should have a contact here");

            Stream s = cr.GetPhoto(e);

            Assert.IsTrue(s == null, "There should be no photo yet"); 


            using (FileStream fs = new FileStream(this.resourcePath + "contactphoto.jpg", System.IO.FileMode.Open))
            {
                cr.SetPhoto(e, fs); 
            }

            // now delete the guy, which requires us to reload him from the server first, as the photo change operation
            // changes the etag off the entry
            e = cr.Retrieve(e);
            cr.Delete(e);
        }