Menu

[r144]: / trunk / GoogleContactsSync / ContactPropertiesUtils.cs  Maximize  Restore  History

Download this file

161 lines (148 with data), 7.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Text;
using Google.GData.Contacts;
using Outlook = Microsoft.Office.Interop.Outlook;
using Google.GData.Extensions;
using System.Collections;
using Google.Contacts;
namespace GoContactSyncMod
{
internal static class ContactPropertiesUtils
{
public static string GetOutlookId(Outlook.ContactItem outlookContact)
{
return outlookContact.EntryID;
}
public static string GetGoogleId(Contact googleContact)
{
string id = googleContact.Id.ToString();
if (id == null)
throw new Exception();
return id;
}
public static void SetGoogleOutlookContactId(string syncProfile, Contact googleContact, Outlook.ContactItem outlookContact)
{
if (outlookContact.EntryID == null)
throw new Exception("Must save outlook contact before getting id");
SetGoogleOutlookContactId(syncProfile, googleContact, GetOutlookId(outlookContact));
}
public static void SetGoogleOutlookContactId(string syncProfile, Contact googleContact, string outlookContactId)
{
// check if exists
bool found = false;
foreach (Google.GData.Extensions.ExtendedProperty p in googleContact.ExtendedProperties)
{
if (p.Name == "gos:oid:" + syncProfile + "")
{
p.Value = outlookContactId;
found = true;
break;
}
}
if (!found)
{
Google.GData.Extensions.ExtendedProperty prop = new ExtendedProperty(outlookContactId, "gos:oid:" + syncProfile + "");
prop.Value = outlookContactId;
googleContact.ExtendedProperties.Add(prop);
}
}
public static string GetGoogleOutlookContactId(string syncProfile, Contact googleContact)
{
// get extended prop
foreach (Google.GData.Extensions.ExtendedProperty p in googleContact.ExtendedProperties)
{
if (p.Name == "gos:oid:" + syncProfile + "")
return (string)p.Value;
}
return null;
}
public static void ResetGoogleOutlookContactId(string syncProfile, Contact googleContact)
{
// get extended prop
foreach (Google.GData.Extensions.ExtendedProperty p in googleContact.ExtendedProperties)
{
if (p.Name == "gos:oid:" + syncProfile + "")
{
// remove
googleContact.ExtendedProperties.Remove(p);
return;
}
}
}
/// <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(Syncronizer sync, Outlook.ContactItem outlookContact, Contact googleContact)
{
if (googleContact.ContactEntry.Id.Uri == null)
throw new NullReferenceException("GoogleContact must have a valid Id");
//check if outlook contact aready has google id property.
Outlook.UserProperty prop = outlookContact.UserProperties[sync.OutlookPropertyNameId];
if (prop == null)
prop = outlookContact.UserProperties.Add(sync.OutlookPropertyNameId, Outlook.OlUserPropertyType.olText, true);
prop.Value = googleContact.ContactEntry.Id.Uri.Content;
//save last google's updated date as property
/*prop = outlookContact.UserProperties[OutlookPropertyNameUpdated];
if (prop == null)
prop = outlookContact.UserProperties.Add(OutlookPropertyNameUpdated, Outlook.OlUserPropertyType.olDateTime, null, null);
prop.Value = googleContact.Updated;*/
//Also set the OutlookLastSync date when setting a match between Outlook and Google to assure the lastSync updated when Outlook contact is saved afterwards
SetOutlookLastSync(sync, outlookContact);
}
public static void SetOutlookLastSync(Syncronizer sync, Outlook.ContactItem outlookContact)
{
//save sync datetime
Outlook.UserProperty prop = outlookContact.UserProperties[sync.OutlookPropertyNameSynced];
if (prop == null)
prop = outlookContact.UserProperties.Add(sync.OutlookPropertyNameSynced, Outlook.OlUserPropertyType.olDateTime, true);
prop.Value = DateTime.Now;
}
public static DateTime? GetOutlookLastSync(Syncronizer sync, Outlook.ContactItem outlookContact)
{
Outlook.UserProperty prop = outlookContact.UserProperties[sync.OutlookPropertyNameSynced];
if (prop != null)
return (DateTime)prop.Value;
return null;
}
public static string GetOutlookGoogleContactId(Syncronizer sync, Outlook.ContactItem outlookContact)
{
Outlook.UserProperty idProp = outlookContact.UserProperties[sync.OutlookPropertyNameId];
if (idProp == null)
return null;
string id = (string)idProp.Value;
if (id == null)
throw new Exception();
return id;
}
public static void ResetOutlookGoogleContactId(Syncronizer sync, Outlook.ContactItem outlookContact)
{
Outlook.UserProperty idProp = outlookContact.UserProperties[sync.OutlookPropertyNameId];
Outlook.UserProperty lastSyncProp = outlookContact.UserProperties[sync.OutlookPropertyNameSynced];
if (idProp == null && lastSyncProp == null)
return;
List<int> indexesToBeRemoved = new List<int>();
IEnumerator en = outlookContact.UserProperties.GetEnumerator();
en.Reset();
int index = 1; // 1 based collection
while (en.MoveNext())
{
if (en.Current as Outlook.UserProperty == idProp || en.Current as Outlook.UserProperty == lastSyncProp)
{
indexesToBeRemoved.Add(index);
//outlookContact.UserProperties.Remove(index);
//Don't return to remove both properties, googleId and lastSynced
//return;
}
index++;
}
for (int i = indexesToBeRemoved.Count-1; i>=0 ; i--)
outlookContact.UserProperties.Remove(indexesToBeRemoved[i]);
//throw new Exception("Did not find prop.");
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.