Menu

[r781]: / trunk / GoogleContactsSync / NotePropertiesUtils.cs  Maximize  Restore  History

Download this file

362 lines (325 with data), 13.7 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections.Generic;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Collections;
using Google.Documents;
using System.Runtime.InteropServices;
using System.IO;
namespace GoContactSyncMod
{
internal static class NotePropertiesUtils
{
public static string GetOutlookId(Outlook.NoteItem outlookNote)
{
return outlookNote.EntryID;
}
public static string GetGoogleId(Document googleNote)
{
string id = googleNote.Id.ToString();
if (id == null)
throw new Exception();
return id;
}
//public static void SetGoogleOutlookNoteId(string syncProfile, Document googleNote, Outlook.NoteItem outlookNote)
//{
// if (outlookNote.EntryID == null)
// throw new Exception("Must save outlook note before getting id");
// SetGoogleOutlookNoteId(syncProfile, googleNote, GetOutlookId(outlookNote));
//}
//public static void SetGoogleOutlookNoteId(string syncProfile, Document googleNote, string outlookNoteId)
//{
// // check if exists
// bool found = false;
// foreach (Google.GData.Extensions.ExtendedProperty p in googleNote.ExtendedProperties)
// {
// if (p.Name == "gos:oid:" + syncProfile + "")
// {
// p.Value = outlookNoteId;
// found = true;
// break;
// }
// }
// if (!found)
// {
// Google.GData.Extensions.ExtendedProperty prop = new ExtendedProperty(outlookNoteId, "gos:oid:" + syncProfile + "");
// prop.Value = outlookNoteId;
// googleNote.ExtendedProperties.Add(prop);
// }
//}
//public static string GetGoogleOutlookNoteId(string syncProfile, Document googleNote)
//{
// // get extended prop
// foreach (Google.GData.Extensions.ExtendedProperty p in googleNote.DocumentEntry.ExtendedProperties)
// {
// if (p.Name == "gos:oid:" + syncProfile + "")
// return (string)p.Value;
// }
// return null;
//}
//public static void ResetGoogleOutlookNoteId(string syncProfile, Document googleNote)
//{
// // get extended prop
// foreach (Google.GData.Extensions.ExtendedProperty p in googleNote.ExtendedProperties)
// {
// if (p.Name == "gos:oid:" + syncProfile + "")
// {
// // remove
// googleNote.ExtendedProperties.Remove(p);
// return;
// }
// }
//}
/// <summary>
/// Sets the syncId of the Outlook note and the last sync date.
/// Please assure to always call this function when saving OutlookItem
/// </summary>
/// <param name="sync"></param>
/// <param name="outlookNote"></param>
/// <param name="googleNote"></param>
public static void SetOutlookGoogleNoteId(Synchronizer sync, Outlook.NoteItem outlookNote, Document googleNote)
{
if (googleNote.DocumentEntry.Id.Uri == null)
throw new NullReferenceException("GoogleNote must have a valid Id");
//check if outlook note aready has google id property.
Outlook.ItemProperties userProperties = outlookNote.ItemProperties;
try
{
Outlook.ItemProperty prop = userProperties[sync.OutlookPropertyNameId];
if (prop == null)
prop = userProperties.Add(sync.OutlookPropertyNameId, Outlook.OlUserPropertyType.olText, true);
try
{
prop.Value = googleNote.DocumentEntry.Id.Uri.Content;
}
finally
{
Marshal.ReleaseComObject(prop);
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
//save last google's updated date as property
/*prop = outlookNote.UserProperties[OutlookPropertyNameUpdated];
if (prop == null)
prop = outlookNote.UserProperties.Add(OutlookPropertyNameUpdated, Outlook.OlUserPropertyType.olDateTime, null, null);
prop.Value = googleNote.Updated;*/
//Also set the OutlookLastSync date when setting a match between Outlook and Google to assure the lastSync updated when Outlook note is saved afterwards
SetOutlookLastSync(sync, outlookNote);
}
public static void SetOutlookLastSync(Synchronizer sync, Outlook.NoteItem outlookNote)
{
//save sync datetime
Outlook.ItemProperties userProperties = outlookNote.ItemProperties;
try
{
Outlook.ItemProperty prop = userProperties[sync.OutlookPropertyNameSynced];
if (prop == null)
prop = userProperties.Add(sync.OutlookPropertyNameSynced, Outlook.OlUserPropertyType.olDateTime, true);
try
{
prop.Value = DateTime.Now;
}
finally
{
Marshal.ReleaseComObject(prop);
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
}
public static DateTime? GetOutlookLastSync(Synchronizer sync, Outlook.NoteItem outlookNote)
{
DateTime? result = null;
Outlook.ItemProperties userProperties = outlookNote.ItemProperties;
try
{
Outlook.ItemProperty prop = userProperties[sync.OutlookPropertyNameSynced];
if (prop != null)
{
try
{
result = (DateTime)prop.Value;
}
finally
{
Marshal.ReleaseComObject(prop);
}
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
return result;
}
public static string GetOutlookGoogleNoteId(Synchronizer sync, Outlook.NoteItem outlookNote)
{
string id = null;
Outlook.ItemProperties userProperties = outlookNote.ItemProperties;
try
{
Outlook.ItemProperty idProp = userProperties[sync.OutlookPropertyNameId];
if (idProp != null)
{
try
{
id = (string)idProp.Value;
if (id == null)
throw new Exception();
}
finally
{
Marshal.ReleaseComObject(idProp);
}
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
return id;
}
public static void ResetOutlookGoogleNoteId(Synchronizer sync, Outlook.NoteItem outlookNote)
{
Outlook.ItemProperties userProperties = outlookNote.ItemProperties;
try
{
Outlook.ItemProperty idProp = userProperties[sync.OutlookPropertyNameId];
try
{
Outlook.ItemProperty lastSyncProp = userProperties[sync.OutlookPropertyNameSynced];
try
{
if (idProp == null && lastSyncProp == null)
return;
List<int> indexesToBeRemoved = new List<int>();
IEnumerator en = userProperties.GetEnumerator();
en.Reset();
int index = 0; // 0 based collection
while (en.MoveNext())
{
Outlook.ItemProperty userProperty = en.Current as Outlook.ItemProperty;
if (userProperty == idProp || userProperty == lastSyncProp)
{
indexesToBeRemoved.Add(index);
//outlookNote.UserProperties.Remove(index);
//Don't return to remove both properties, googleId and lastSynced
//return;
}
index++;
Marshal.ReleaseComObject(userProperty);
}
for (int i = indexesToBeRemoved.Count - 1; i >= 0; i--)
userProperties.Remove(indexesToBeRemoved[i]);
//throw new Exception("Did not find prop.");
}
finally
{
if (lastSyncProp != null)
Marshal.ReleaseComObject(lastSyncProp);
}
}
finally
{
if (idProp != null)
Marshal.ReleaseComObject(idProp);
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
}
public static string GetFileName(string Id, string syncProfile)
{
string fileName = "Note_" + Id + ".txt";
foreach (char c in Path.GetInvalidFileNameChars())
{
fileName = fileName.Replace(c, '_');
if (syncProfile != null)
syncProfile = syncProfile.Replace(c, '_');
}
//Only for backward compliance with version before 3.5.9 (before syncProfile can be changed)
CopyNoteFiles(syncProfile);
fileName = Logger.Folder + (string.IsNullOrEmpty(syncProfile) ? string.Empty : "\\" + syncProfile) + "\\" + fileName;
return fileName;
}
private static void CopyNoteFiles(string syncProfile)
{
if (!string.IsNullOrEmpty(syncProfile))
{
foreach (char c in Path.GetInvalidFileNameChars())
{
syncProfile = syncProfile.Replace(c, '_');
}
if (!Directory.Exists(Logger.Folder + "\\" + syncProfile))
{
Directory.CreateDirectory(Logger.Folder + "\\" + syncProfile);
//Only for backward compliance with version before 3.5.9 (before syncProfile can be changed)
//Create ProfileSync subfolder and copy all files to there
string[] files = Directory.GetFiles(Logger.Folder, @"Note_*.txt");
foreach (string file in files)
File.Move(file, file.Replace(Logger.Folder, Logger.Folder + "\\" + syncProfile + "\\"));
}
}
}
public static string GetBody(Synchronizer sync, Document entry)
{
string body = null;
StreamReader reader = null;
try
{
reader = new StreamReader(sync.DocumentsRequest.Download(entry, Document.DownloadType.txt));
body = reader.ReadToEnd();
}
finally
{
if (reader != null)
reader.Close();
}
return body;
}
public static bool NoteFileExists(string Id, string syncProfile)
{
if (File.Exists(GetFileName(Id, syncProfile)))
return true;
return false;
}
public static string CreateNoteFile(string Id, string body, string syncProfile)
{
string fileName = GetFileName(Id, syncProfile);
FileStream filestream = null;
try
{
filestream = new FileStream(fileName, FileMode.OpenOrCreate);
using (var writer = new StreamWriter(filestream))
{
filestream = null;
writer.Write(body);
}
}
catch (Exception e)
{
throw new Exception("Invalid path to create local note filename: " + fileName, e);
}
finally
{
if (filestream != null)
filestream.Dispose();
}
return fileName;
}
public static void DeleteNoteFiles(string syncProfile)
{
//Only for backward compliance with version before 3.5.9 (before syncProfile can be changed)
CopyNoteFiles(syncProfile);
string[] files = Directory.GetFiles(Logger.Folder + "\\" + syncProfile, @"Note_*.txt");
foreach (string file in files)
File.Delete(file);
}
}
}
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.