Menu

[r3]: / trunk / UnitTests / OutlookTests.cs  Maximize  Restore  History

Download this file

113 lines (90 with data), 4.4 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
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Configuration;
namespace WebGear.GoogleContactsSync.UnitTests
{
[TestFixture]
public class OutlookTests
{
[Test]
[Ignore("Not needed anymore since it was used as a fix")]
public void FixUserProperties()
{
Outlook.Application outlookApp;
Outlook.NameSpace outlookNamespace;
Outlook.Items outlookContacts;
outlookApp = new Outlook.Application();
outlookNamespace = outlookApp.GetNamespace("mapi");
outlookNamespace.Logon("Outlook", null, true, false);
try
{
Outlook.MAPIFolder contactsFolder = outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
outlookContacts = contactsFolder.Items;
string oldPrefixId = string.Format("google/contacts/{0}/id", ConfigurationManager.AppSettings["Gmail.Username"]);
string oldPrefixUpdated = string.Format("google/contacts/{0}/updated", ConfigurationManager.AppSettings["Gmail.Username"]);
int hashCode = ConfigurationManager.AppSettings["Gmail.Username"].GetHashCode();
int maxUserIdLength = 32-("g/con/" + "/up").Length;
string userId = ConfigurationManager.AppSettings["Gmail.Username"];
if (userId.Length > maxUserIdLength)
userId = userId.GetHashCode().ToString("X"); //if a user id would overflow UserProperty name, then use that user id hash code as id.
string newPrefixId = "g/con/"+userId+"/id";
string newPrefixUpdated = "g/con/"+userId+"/up";
//max property length: 32
//foreach (Outlook.ContactItem contact in outlookContacts)
for (int i = 0; i < outlookContacts.Count; i++)
{
try
{
if (!(outlookContacts[i] is Outlook.ContactItem))
continue;
}
catch (Exception)
{
continue;
}
Outlook.ContactItem contact = outlookContacts[i] as Outlook.ContactItem;
Outlook.UserProperty updatedProp = contact.UserProperties[newPrefixUpdated];
if (updatedProp != null)
{
string lastUpdatedStr = (string)updatedProp.Value;
updatedProp.Delete();
updatedProp = contact.UserProperties.Add(newPrefixUpdated, Outlook.OlUserPropertyType.olDateTime, null, null);
DateTime lastUpdated = DateTime.Parse(lastUpdatedStr);
updatedProp.Value = lastUpdated;
if (!contact.Saved)
contact.Save();
continue;
}
Outlook.UserProperty prop = contact.UserProperties[newPrefixId];
if (prop != null)
continue;
prop = contact.UserProperties[oldPrefixId];
if (prop != null)
{
string id = (string)prop.Value;
prop.Delete();
prop = contact.UserProperties.Add(newPrefixId, Outlook.OlUserPropertyType.olText, null, null);
prop.Value = id;
}
prop = contact.UserProperties[oldPrefixUpdated];
if (prop != null)
{
DateTime lastUpdated = (DateTime)prop.Value;
prop.Delete();
prop = contact.UserProperties.Add(newPrefixUpdated, Outlook.OlUserPropertyType.olDateTime, null, null);
prop.Value = lastUpdated;
}
if (!contact.Saved)
contact.Save();
}
}
finally
{
outlookNamespace.Logoff();
}
}
}
}
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.