Menu

[r1519]: / branches / vs2013 / GoogleContactsSync / Utilities.cs  Maximize  Restore  History

Download this file

408 lines (358 with data), 14.2 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Net;
using System.IO;
using Google.GData.Contacts;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Threading;
using System.Collections.ObjectModel;
using Google.Contacts;
namespace GoContactSyncMod
{
internal static class Utilities
{
//private static string tempPhotoPath = AppDomain.CurrentDomain.BaseDirectory + "\\TempOutlookContactPhoto.jpg";
//private static string tempPhotoPath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + @"\" + System.Windows.Forms.Application.ProductName + @"\TempOutlookContactPhoto.jpg";
private static string tempPhotoPath = Path.GetTempPath() + @"\TempOutlookContactPhoto.jpg";
public static byte[] BitmapToBytes(Bitmap bitmap)
{
//bitmap
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
return stream.ToArray();
}
public static bool HasPhoto(Contact googleContact)
{
if (googleContact.PhotoEtag == null)
return false;
return true;
}
public static bool HasPhoto(Outlook.ContactItem outlookContact)
{
return outlookContact.HasPicture;
}
public static bool SaveGooglePhoto(Syncronizer sync, Contact googleContact, Image image)
{
if (googleContact.ContactEntry.PhotoUri == null)
throw new Exception("Must reload contact from google.");
try
{
WebClient client = new WebClient();
client.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + sync.ContactsRequest.Service.QueryClientLoginToken());
client.Headers.Add(HttpRequestHeader.ContentType, "image/*");
Bitmap pic = new Bitmap(image);
Stream s = client.OpenWrite(googleContact.ContactEntry.PhotoUri.AbsoluteUri, "PUT");
byte[] bytes = BitmapToBytes(pic);
s.Write(bytes, 0, bytes.Length);
s.Flush();
//s.Close();
s.Dispose();
client.Dispose();
pic.Dispose();
}
catch
{
return false;
}
return true;
}
public static Image GetGooglePhoto(Syncronizer sync, Contact googleContact)
{
if (!HasPhoto(googleContact))
return null;
try
{
WebClient client = new WebClient();
client.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + sync.ContactsRequest.Service.QueryClientLoginToken());
Stream stream = client.OpenRead(googleContact.PhotoUri.AbsoluteUri);
BinaryReader reader = new BinaryReader(stream);
Image image = Image.FromStream(stream);
reader.Close();
//stream.Close();
//stream.Dispose();
client.Dispose();
return image;
}
catch
{
return null;
}
}
public static bool SetOutlookPhoto(Outlook.ContactItem outlookContact, string fullImagePath)
{
try
{
outlookContact.AddPicture(fullImagePath);
//outlookContact.Save();
return true;
}
catch
{
return false;
}
}
public static bool SetOutlookPhoto(Outlook.ContactItem outlookContact, Image image)
{
try
{
image.Save(tempPhotoPath);
return SetOutlookPhoto(outlookContact, tempPhotoPath);
}
catch
{
return false;
}
}
public static Image GetOutlookPhoto(Outlook.ContactItem outlookContact)
{
if (!HasPhoto(outlookContact))
return null;
try
{
foreach (Outlook.Attachment a in outlookContact.Attachments)
{
// CH Fixed this to Contains, due to outlook picture that looks like "ContactPicture_138382.jpg"
if (a.DisplayName.ToUpper().Contains("CONTACTPICTURE") || a.DisplayName.ToUpper().Contains("CONTACTPHOTO"))
{
//TODO: Check why always the first added picture is returned
//If you add another picture, still the old picture is saved to tempPhotoPath
a.SaveAsFile(tempPhotoPath);
using (Image img = Image.FromFile(tempPhotoPath))
{
return new Bitmap(img);
}
}
}
return null;
}
catch
{
// There's an error here... If Outlook says it has a contact photo, and we can't get it, Something's broken.
return null;
}
}
public static Image CropImageGoogleFormat(Image original)
{
// crop image to a square in the center
int width, height, diff;
Point p;
Rectangle r;
if (original.Height == original.Width)
return original;
if (original.Height > original.Width)
{
// tall image
width = original.Width;
height = width;
diff = original.Height - height;
p = new Point(0, diff / 2);
r = new Rectangle(p, new Size(width, height));
return CropImage(original, r);
}
else
{
// flat image
height = original.Height;
width = height;
diff = original.Width - width;
p = new Point(diff / 2, 0);
r = new Rectangle(p, new Size(width, height));
return CropImage(original, r);
}
}
public static Image CropImage(Image original, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(original);
Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
return (Image)(bmpCrop);
}
public static void DeleteTempPhoto()
{
try
{
if (File.Exists(tempPhotoPath))
File.Delete(tempPhotoPath);
}
catch { }
}
public static bool ContainsGroup(Syncronizer sync, Contact googleContact, string groupName)
{
Group group = sync.GetGoogleGroupByName(groupName);
if (group == null)
return false;
return ContainsGroup(googleContact, group);
}
public static bool ContainsGroup(Contact googleContact, Group group)
{
foreach (GroupMembership m in googleContact.GroupMembership)
{
if (m.HRef == group.GroupEntry.Id.AbsoluteUri)
return true;
}
return false;
}
public static bool ContainsGroup(Outlook.ContactItem outlookContact, string group)
{
if (outlookContact.Categories == null)
return false;
return outlookContact.Categories.Contains(group);
}
public static Collection<Group> GetGoogleGroups(Syncronizer sync, Contact googleContact)
{
int c = googleContact.GroupMembership.Count;
Collection<Group> groups = new Collection<Group>();
string id;
Group group;
for (int i = 0; i < c; i++)
{
id = googleContact.GroupMembership[i].HRef;
group = sync.GetGoogleGroupById(id);
groups.Add(group);
}
return groups;
}
public static void AddGoogleGroup(Contact googleContact, Group group)
{
if (ContainsGroup(googleContact, group))
return;
GroupMembership m = new GroupMembership();
m.HRef = group.GroupEntry.Id.AbsoluteUri;
googleContact.GroupMembership.Add(m);
}
public static void RemoveGoogleGroup(Contact googleContact, Group group)
{
if (!ContainsGroup(googleContact, group))
return;
// TODO: broken. removes group membership but does not remove contact
// from group in the end.
// look for id
GroupMembership mem;
for (int i = 0; i < googleContact.GroupMembership.Count; i++)
{
mem = googleContact.GroupMembership[i];
if (mem.HRef == group.GroupEntry.Id.AbsoluteUri)
{
googleContact.GroupMembership.Remove(mem);
return;
}
}
throw new Exception("Did not find group");
}
public static string[] GetOutlookGroups(string outlookContactCategories)
{
if (outlookContactCategories == null)
return new string[] { };
char[] listseparator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator.ToCharArray();
string[] categories = outlookContactCategories.Split(listseparator);
for (int i = 0; i < categories.Length; i++)
{
categories[i] = categories[i].Trim();
}
return categories;
}
public static void AddOutlookGroup(Outlook.ContactItem outlookContact, string group)
{
if (ContainsGroup(outlookContact, group))
return;
// append
if (outlookContact.Categories == null)
outlookContact.Categories = "";
if (outlookContact.Categories != "")
outlookContact.Categories += ", " + group;
else
outlookContact.Categories += group;
}
public static void RemoveOutlookGroup(Outlook.ContactItem outlookContact, string group)
{
if (!ContainsGroup(outlookContact, group))
return;
outlookContact.Categories = outlookContact.Categories.Replace(", " + group, "");
outlookContact.Categories = outlookContact.Categories.Replace(group, "");
}
//ToDo: Workaround to save google Content is also not working, beause of error when closing the StreamWriter
//public static bool SaveGoogleNoteContent(Syncronizer sync, Google.Documents.Document updated, Google.Documents.Document googleNote)
//{
// if (updated.DocumentEntry.EditUri == null || googleNote.MediaSource == null)
// throw new Exception("Must reload note from google.");
// StreamWriter writer = null;
// StreamReader reader = null;
// WebClient client = null;
// try
// {
// client = new WebClient();
// client.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + sync.DocumentsRequest.Service.QueryClientLoginToken());
// client.Headers.Add(HttpRequestHeader.ContentType, googleNote.MediaSource.ContentType);
// Stream s = client.OpenWrite(updated.DocumentEntry.EditUri.ToString(), "PUT");
// writer = new StreamWriter(s);
// reader = new StreamReader(googleNote.MediaSource.GetDataStream());
// string body = reader.ReadToEnd();
// writer.Write(body);
// }
// catch
// {
// return false;
// }
// finally
// {
// if (client != null)
// client.Dispose();
// if (writer != null)
// writer.Close(); //This throws an exception 400 (Ungültige Anforderung)
// if (reader != null)
// reader.Close();
// }
// return true;
//}
}
public class OutlookFolder : IComparable
{
private string _folderName;
private string _folderID;
private bool _isDefaultFolder;
public OutlookFolder(string folderName, string folderID, bool isDefaultFolder)
{
this._folderName = folderName;
this._folderID = folderID;
this._isDefaultFolder = isDefaultFolder;
}
public string FolderName
{
get
{
return _folderName;
}
}
public string FolderID
{
get
{
return _folderID;
}
}
public bool IsDefaultFolder
{
get
{
return _isDefaultFolder;
}
}
public string DisplayName
{
get
{
return _folderName + (_isDefaultFolder?" (Default)":String.Empty);
}
}
public int CompareTo(object obj)
{
if (obj is OutlookFolder)
{
return this._folderName.CompareTo((obj as OutlookFolder)._folderName);
}
else
throw new ArgumentException("Object is not a OutlookFolder");
}
}
}
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.