Пример #1
0
        /// <summary>
        /// Adds the picture.
        /// </summary>
        /// <param name="outlookContact">The outlook contact.</param>
        /// <param name="stream">The stream.</param>
        /// <returns>True if Changed.</returns>
        public static bool AddPicture(this ContactItem outlookContact, Stream stream)
        {
            if (stream == null || stream.Length == 0)
            {
                return(false);
            }

            var imageDir  = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache, Environment.SpecialFolderOption.None);
            var imagePath = Path.Combine(imageDir, string.Format("contact_{0}", outlookContact.EntryID));

            if (!Directory.Exists(imageDir))
            {
                Directory.CreateDirectory(imageDir);
            }

            var output = File.Create(imagePath);

            stream.Seek(0, SeekOrigin.Begin);
            stream.CopyTo(output);
            output.Close();

            outlookContact.AddPicture(imagePath);

            var attachment = outlookContact.Attachments.Cast <Attachment>().FirstOrDefault(a => a.FileName == "ContactPicture.jpg");

            if (attachment != null)
            {
                outlookContact.UserProperties.SetProperty("GooglePictureSize", attachment.Size.ToString(CultureInfo.InvariantCulture));
            }

            return(true);
        }
        private void MapPhoto2To1(vCard source, ContactItem target, IEntityMappingLogger logger)
        {
            if (source.Photos.Count > 0)
            {
                if (target.HasPicture && _configuration.KeepOutlookPhoto)
                {
                    return;
                }

                vCardPhoto contactPhoto = source.Photos[0];

                if (contactPhoto.IsLoaded)
                {
                    try
                    {
                        string picturePath = Path.GetTempPath() + @"\Contact_" + target.EntryID + ".jpg";
                        File.WriteAllBytes(picturePath, contactPhoto.GetBytes());
                        try
                        {
                            target.AddPicture(picturePath);
                        }
                        catch (COMException x)
                        {
                            s_logger.Warn("Could not add picture for contact.", x);
                            logger.LogMappingWarning("Could not add picture for contact.", x);
                        }
                        File.Delete(picturePath);
                    }
                    catch (Exception ex)
                    {
                        s_logger.Warn("Could not add picture for contact.", ex);
                        logger.LogMappingWarning("Could not add picture for contact.", ex);
                    }
                }
            }
            else
            {
                if (target.HasPicture)
                {
                    try
                    {
                        target.RemovePicture();
                    }
                    catch (COMException x)
                    {
                        s_logger.Warn("Could not remove picture for contact.", x);
                        logger.LogMappingWarning("Could not remove picture for contact.", x);
                    }
                }
            }
        }
        private void MapPhoto2To1(GoogleContactWrapper source, ContactItem target, IEntityMappingLogger logger)
        {
            if (source.PhotoOrNull != null)
            {
                if (target.HasPicture && _configuration.KeepOutlookPhoto)
                {
                    return;
                }

                try
                {
                    string picturePath = Path.GetTempPath() + @"\Contact_" + target.EntryID + ".jpg";
                    File.WriteAllBytes(picturePath, source.PhotoOrNull);
                    try
                    {
                        target.AddPicture(picturePath);
                    }
                    catch (COMException x)
                    {
                        s_logger.Warn("Could not add picture for contact.", x);
                        logger.LogMappingWarning("Could not add picture for contact.", x);
                    }
                    File.Delete(picturePath);
                }
                catch (System.Exception ex)
                {
                    s_logger.Warn("Could not add picture for contact.", ex);
                    logger.LogMappingWarning("Could not add picture for contact.", ex);
                }
            }
            else
            {
                if (target.HasPicture)
                {
                    try
                    {
                        target.RemovePicture();
                    }
                    catch (COMException x)
                    {
                        s_logger.Warn("Could not remove picture for contact.", x);
                        logger.LogMappingWarning("Could not remove picture for contact.", x);
                    }
                }
            }
        }
        private static void MapPhoto2To1(vCard source, ContactItem target)
        {
            if (source.Photos.Count > 0)
            {
                vCardPhoto contactPhoto = source.Photos[0];

                if (contactPhoto.IsLoaded)
                {
                    string picturePath = Path.GetTempPath() + @"\Contact_" + target.EntryID + ".jpg";
                    using (FileStream fs = new FileStream(picturePath, FileMode.Create))
                    {
                        fs.Write(contactPhoto.GetBytes(), 0, contactPhoto.GetBytes().Length);
                        fs.Flush();
                    }
                    target.AddPicture(picturePath);
                    File.Delete(picturePath);
                }
            }
        }
Пример #5
0
        private static bool ConvertToNativeContact(StdContact stdNewContact, ContactItem outlookContact)
        {
            var dirty         = false;
            var stdOldContact = ConvertToStandardContact(outlookContact, null);
            var gender        = stdNewContact.PersonGender == Gender.Unspecified
                             ? OlGender.olUnspecified
                             : ((stdNewContact.PersonGender == Gender.Male) ? OlGender.olMale : OlGender.olFemale);

            SyncTools.ClearNulls(stdNewContact, typeof(StdContact));
            SyncTools.ClearNulls(stdOldContact, typeof(StdContact));

            MappingHelper.MapIfDiffers(
                ref dirty, stdNewContact, stdOldContact, x => x.DateOfBirth, x => outlookContact.Birthday = x);
            MappingHelper.MapIfDiffers(
                ref dirty, stdNewContact, stdOldContact, x => x.PersonGender, x => outlookContact.Gender = gender);

            MappingHelper.MapIfDiffers(
                ref dirty, stdNewContact, stdOldContact, x => x.Name.FirstName, x => outlookContact.FirstName = x);
            MappingHelper.MapIfDiffers(
                ref dirty, stdNewContact, stdOldContact, x => x.Name.MiddleName, x => outlookContact.MiddleName = x);
            MappingHelper.MapIfDiffers(
                ref dirty, stdNewContact, stdOldContact, x => x.Name.LastName, x => outlookContact.LastName = x);
            MappingHelper.MapIfDiffers(
                ref dirty, stdNewContact, stdOldContact, x => x.Name.AcademicTitle, x => outlookContact.Title = x);

            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.BusinessEmailPrimary,
                x => outlookContact.Email2Address = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalEmailPrimary,
                x => outlookContact.Email1Address = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.BusinessHomepage,
                x => outlookContact.BusinessHomePage = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalHomepage,
                x => outlookContact.PersonalHomePage = x);
            MappingHelper.MapIfDiffers(
                ref dirty, stdNewContact, stdOldContact, x => x.BusinessCompanyName, x => outlookContact.CompanyName = x);
            MappingHelper.MapIfDiffers(
                ref dirty, stdNewContact, stdOldContact, x => x.BusinessPosition, x => outlookContact.JobTitle = x);

            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.BusinessAddressPrimary.CityName,
                x => outlookContact.BusinessAddressCity = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.BusinessAddressPrimary.CountryName,
                x => outlookContact.BusinessAddressCountry = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.BusinessAddressPrimary.PostalCode,
                x => outlookContact.BusinessAddressPostalCode = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.BusinessAddressPrimary.StateName,
                x => outlookContact.BusinessAddressState = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.BusinessAddressPrimary.StreetName,
                x => outlookContact.BusinessAddressStreet = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.BusinessAddressPrimary.Phone.ToString(),
                x => outlookContact.BusinessTelephoneNumber = x);

            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalAddressPrimary.CityName,
                x => outlookContact.HomeAddressCity = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalAddressPrimary.CountryName,
                x => outlookContact.HomeAddressCountry = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalAddressPrimary.PostalCode,
                x => outlookContact.HomeAddressPostalCode = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalAddressPrimary.StateName,
                x => outlookContact.HomeAddressState = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalAddressPrimary.StreetName,
                x => outlookContact.HomeAddressStreet = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalAddressPrimary.Phone.ToString(),
                x => outlookContact.HomeTelephoneNumber = x);

            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalPhoneMobile.ToString(),
                x => outlookContact.MobileTelephoneNumber = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.BusinessPhoneMobile.ToString(),
                x => outlookContact.Business2TelephoneNumber = x);
            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.PersonalInstantMessengerAddresses.MsnMessenger,
                x => outlookContact.IMAddress = x);

            MappingHelper.MapIfDiffers(
                ref dirty,
                stdNewContact,
                stdOldContact,
                x => x.AdditionalTextData.Replace("\r\n", "\n"),
                x => outlookContact.Body = x);

            if (stdOldContact.Id != stdNewContact.Id)
            {
                outlookContact.UserProperties.Add(ContactIdOutlookPropertyName, OlUserPropertyType.olText, true, 1).
                Value = stdNewContact.Id.ToString();
                dirty = true;
            }

            // import pictures if we have more data inside the new contact
            if (stdNewContact.PictureData != null && stdNewContact.PictureData.Length > stdOldContact.PictureData.Length)
            {
                var fullName = Path.GetTempFileName() + ".jpg";
                File.WriteAllBytes(fullName, stdNewContact.PictureData);
                outlookContact.AddPicture(fullName);
                File.Delete(fullName);
                dirty = true;
            }

            if (stdNewContact.Categories != null &&
                (stdOldContact.Categories == null || stdNewContact.Categories.Count != stdOldContact.Categories.Count))
            {
                outlookContact.Categories = string.Join(
                    ";", stdNewContact.Categories.MergeList(stdOldContact.Categories));
                dirty = true;
            }

            return(dirty);
        }
        private async Task MapPhoto2To1(vCard source, ContactItem target, IEntitySynchronizationLogger logger)
        {
            if (source.Photos.Count > 0)
            {
                if (target.HasPicture && _configuration.KeepOutlookPhoto)
                {
                    return;
                }

                vCardPhoto contactPhoto = source.Photos[0];

                string picturePath = Path.GetTempPath() + @"\Contact_" + target.EntryID + ".jpg";
                try
                {
                    if (!contactPhoto.IsLoaded && contactPhoto.Url != null)
                    {
                        using (var client = _webClientFactory())
                        {
                            await client.DownloadFileTaskAsync(contactPhoto.Url, picturePath);
                        }
                    }
                    else if (contactPhoto.IsLoaded)
                    {
                        File.WriteAllBytes(picturePath, contactPhoto.GetBytes());
                    }
                    else
                    {
                        s_logger.Warn("Could not load picture for contact.");
                        logger.LogWarning("Could not load picture for contact.");
                        return;
                    }

                    try
                    {
                        target.AddPicture(picturePath);
                    }
                    catch (COMException x)
                    {
                        s_logger.Warn("Could not add picture for contact.", x);
                        logger.LogWarning("Could not add picture for contact.", x);
                    }

                    File.Delete(picturePath);
                }
                catch (Exception ex)
                {
                    s_logger.Warn("Could not add picture for contact.", ex);
                    logger.LogWarning("Could not add picture for contact.", ex);
                }
            }
            else
            {
                if (target.HasPicture)
                {
                    try
                    {
                        target.RemovePicture();
                    }
                    catch (COMException x)
                    {
                        s_logger.Warn("Could not remove picture for contact.", x);
                        logger.LogWarning("Could not remove picture for contact.", x);
                    }
                }
            }
        }
Пример #7
0
 public void AddPicture(string Path)
 {
     contact.AddPicture(Path);
 }