Example #1
0
        public static bool SaveGooglePhoto(ContactsSynchronizer sync, Contact googleContact, Image image)
        {
            if (googleContact.ContactEntry.PhotoUri == null)
            {
                throw new Exception("Must reload contact from google.");
            }

            try
            {
                using (var client = new WebClient())
                {
                    client.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + sync.ContactsRequest.Settings.OAuth2Parameters.AccessToken);
                    client.Headers.Add(HttpRequestHeader.ContentType, "image/*");
                    using (var pic = new Bitmap(image))
                    {
                        using (var s = client.OpenWrite(googleContact.ContactEntry.PhotoUri.AbsoluteUri, "PUT"))
                        {
                            byte[] bytes = BitmapToBytes(pic);
                            s.Write(bytes, 0, bytes.Length);
                            s.Flush();
                        }
                    }
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
        public static string GetOutlookGoogleContactId(ContactsSynchronizer sync, Outlook.ContactItem outlookContact)
        {
            string id = null;

            Outlook.UserProperties userProperties = null;
            Outlook.UserProperty   idProp         = null;
            try
            {
                userProperties = outlookContact.UserProperties;
                idProp         = userProperties[sync.OutlookPropertyNameId];
                if (idProp != null)
                {
                    id = (string)idProp.Value;
                    if (id == null)
                    {
                        throw new Exception();
                    }
                }
            }
            finally
            {
                if (idProp != null)
                {
                    Marshal.ReleaseComObject(idProp);
                }
                if (userProperties != null)
                {
                    Marshal.ReleaseComObject(userProperties);
                }
            }
            return(id);
        }
        public static DateTime?GetOutlookLastSync(ContactsSynchronizer sync, Outlook.ContactItem outlookContact)
        {
            DateTime?result = null;

            Outlook.UserProperties userProperties = null;
            Outlook.UserProperty   prop           = null;

            try
            {
                userProperties = outlookContact.UserProperties;
                prop           = userProperties[sync.OutlookPropertyNameSynced];
                if (prop != null)
                {
                    result = (DateTime)prop.Value;
                }
            }
            finally
            {
                if (prop != null)
                {
                    Marshal.ReleaseComObject(prop);
                }
                if (userProperties != null)
                {
                    Marshal.ReleaseComObject(userProperties);
                }
            }
            return(result);
        }
 public static void SetOutlookLastSync(ContactsSynchronizer sync, Outlook.ContactItem outlookContact)
 {
     //save sync datetime
     Outlook.UserProperties userProperties = null;
     Outlook.UserProperty   prop           = null;
     try
     {
         userProperties = outlookContact.UserProperties;
         prop           = userProperties[sync.OutlookPropertyNameSynced];
         if (prop == null)
         {
             prop = userProperties.Add(sync.OutlookPropertyNameSynced, Outlook.OlUserPropertyType.olDateTime, false);
         }
         prop.Value = DateTime.Now;
     }
     finally
     {
         if (prop != null)
         {
             Marshal.ReleaseComObject(prop);
         }
         if (userProperties != null)
         {
             Marshal.ReleaseComObject(userProperties);
         }
     }
 }
Example #5
0
        internal void Update(ContactItem outlookContactItem, ContactsSynchronizer sync)
        {
            EntryID                 = outlookContactItem.EntryID;
            FileAs                  = outlookContactItem.FileAs;
            FullName                = outlookContactItem.FullName;
            Email1Address           = ContactPropertiesUtils.GetOutlookEmailAddress1(outlookContactItem);
            MobileTelephoneNumber   = outlookContactItem.MobileTelephoneNumber;
            Categories              = outlookContactItem.Categories;
            LastModificationTime    = outlookContactItem.LastModificationTime;
            Company                 = outlookContactItem.CompanyName;
            TitleFirstLastAndSuffix = GetTitleFirstLastAndSuffix(outlookContactItem);

            UserProperties userProperties = outlookContactItem.UserProperties;
            UserProperty   prop           = userProperties[sync.OutlookPropertyNameId];

            UserProperties.GoogleContactId = prop != null?string.Copy((string)prop.Value) : null;

            if (prop != null)
            {
                Marshal.ReleaseComObject(prop);
            }

            prop = userProperties[sync.OutlookPropertyNameSynced];
            UserProperties.LastSync = prop != null ? (DateTime)prop.Value : (DateTime?)null;
            if (prop != null)
            {
                Marshal.ReleaseComObject(prop);
            }

            Marshal.ReleaseComObject(userProperties);
        }
Example #6
0
        public static bool ContainsGroup(ContactsSynchronizer sync, Contact googleContact, string groupName)
        {
            Group group = sync.GetGoogleGroupByName(groupName);

            if (group == null)
            {
                return(false);
            }
            return(ContainsGroup(googleContact, group));
        }
Example #7
0
        public static Collection <Group> GetGoogleGroups(ContactsSynchronizer sync, Contact googleContact)
        {
            var c      = googleContact.GroupMembership.Count;
            var groups = new Collection <Group>();

            for (int i = 0; i < c; i++)
            {
                var id    = googleContact.GroupMembership[i].HRef;
                var group = sync.GetGoogleGroupById(id);

                groups.Add(group);
            }
            return(groups);
        }
        /// <summary>
        /// Sets the syncId of the Outlook contact and the last sync date.
        /// Please assure to always call this function when saving OutlookItem
        /// </summary>
        /// <param name="sync"></param>
        /// <param name="outlookContact"></param>
        /// <param name="googleContact"></param>
        public static void SetOutlookGoogleContactId(ContactsSynchronizer sync, Outlook.ContactItem outlookContact, Contact googleContact)
        {
            if (googleContact.ContactEntry.Id.Uri == null)
            {
                throw new NullReferenceException("GoogleContact must have a valid Id");
            }

            Outlook.UserProperties userProperties = null;
            Outlook.UserProperty   prop           = null;

            try
            {
                userProperties = outlookContact.UserProperties;
                prop           = userProperties[sync.OutlookPropertyNameId];
                //check if outlook contact aready has google id property.
                if (prop == null)
                {
                    prop = userProperties.Add(sync.OutlookPropertyNameId, Outlook.OlUserPropertyType.olText, false);
                }

                prop.Value = googleContact.ContactEntry.Id.Uri.Content;
            }
            catch (Exception ex)
            {
                Logger.Log(ex, EventType.Debug);
                Logger.Log("Name: " + sync.OutlookPropertyNameId, EventType.Debug);
                Logger.Log("Value: " + googleContact.ContactEntry.Id.Uri.Content, EventType.Debug);
                throw;
            }
            finally
            {
                if (prop != null)
                {
                    Marshal.ReleaseComObject(prop);
                }
                if (userProperties != null)
                {
                    Marshal.ReleaseComObject(userProperties);
                }
            }
            SetOutlookLastSync(sync, outlookContact);
        }
Example #9
0
        public static Image GetGooglePhoto(ContactsSynchronizer sync, Contact googleContact)
        {
            if (!HasPhoto(googleContact))
            {
                return(null);
            }

            try
            {
                using (var client = new WebClient())
                {
                    client.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + sync.ContactsRequest.Settings.OAuth2Parameters.AccessToken);
                    Stream       stream = client.OpenRead(googleContact.PhotoUri.AbsoluteUri);
                    BinaryReader reader = new BinaryReader(stream);
                    Image        image  = Image.FromStream(stream);
                    reader.Close();
                    return(image);
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
        public static void ResetOutlookGoogleContactId(ContactsSynchronizer sync, Outlook.ContactItem outlookContact)
        {
            Outlook.UserProperties userProperties = null;

            try
            {
                userProperties = outlookContact.UserProperties;

                for (var i = userProperties.Count; i > 0; i--)
                {
                    Outlook.UserProperty p = null;
                    try
                    {
                        p = userProperties[i];
                        if (p.Name == sync.OutlookPropertyNameId || p.Name == sync.OutlookPropertyNameSynced)
                        {
                            userProperties.Remove(i);
                        }
                    }
                    finally
                    {
                        if (p != null)
                        {
                            Marshal.ReleaseComObject(p);
                        }
                    }
                }
            }
            finally
            {
                if (userProperties != null)
                {
                    Marshal.ReleaseComObject(userProperties);
                }
            }
        }
Example #11
0
 public OutlookContactInfo(ContactItem item, ContactsSynchronizer sync)
 {
     UserProperties = new UserPropertiesHolder();
     Update(item, sync);
 }