Menu

[r3]: / trunk / GoogleContactsSync / SettingsForm.cs  Maximize  Restore  History

Download this file

518 lines (453 with data), 17.9 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Threading;
using System.Text.RegularExpressions;
namespace WebGear.GoogleContactsSync
{
internal partial class SettingsForm : Form
{
internal Syncronizer _sync;
private SyncOption _syncOption;
private DateTime lastSync;
private bool requestClose = false;
delegate void TextHandler(string text);
delegate void SwitchHandler(bool value);
public SettingsForm()
{
InitializeComponent();
PopulateSyncOptionBox();
LoadSettings();
lastSync = DateTime.Now.AddSeconds(15) - new TimeSpan(0, (int)autoSyncInterval.Value, 0);
lastSyncLabel.Text = "Not synced";
ValidateSyncButton();
}
private void PopulateSyncOptionBox()
{
string str;
for (int i = 0; i < 20; i++)
{
str = ((SyncOption)i).ToString();
if (str == i.ToString())
break;
// format (to add space before capital)
MatchCollection matches = Regex.Matches(str, "[A-Z]");
for (int k = 0; k < matches.Count; k++)
{
str = str.Replace(str[matches[k].Index].ToString(), " " + str[matches[k].Index]);
matches = Regex.Matches(str, "[A-Z]");
}
str = str.Replace(" ", " ");
// fix start
str = str.Substring(1);
syncOptionBox.Items.Add(str);
}
}
private void LoadSettings()
{
// default
SetSyncOption(0);
// load
RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(@"Software\Webgear\GOContactSync");
if (regKeyAppRoot.GetValue("SyncOption") != null)
{
_syncOption = (SyncOption)regKeyAppRoot.GetValue("SyncOption");
SetSyncOption((int)_syncOption);
}
if (regKeyAppRoot.GetValue("SyncProfile") != null)
tbSyncProfile.Text = (string)regKeyAppRoot.GetValue("SyncProfile");
if (regKeyAppRoot.GetValue("Username") != null)
{
UserName.Text = regKeyAppRoot.GetValue("Username") as string;
if (regKeyAppRoot.GetValue("Password") != null)
Password.Text = Encryption.DecryptPassword(UserName.Text, regKeyAppRoot.GetValue("Password") as string);
}
if (regKeyAppRoot.GetValue("AutoSync") != null)
autoSyncCheckBox.Checked = Convert.ToBoolean(regKeyAppRoot.GetValue("AutoSync"));
if (regKeyAppRoot.GetValue("AutoSyncInterval") != null)
autoSyncInterval.Value = Convert.ToDecimal(regKeyAppRoot.GetValue("AutoSyncInterval"));
if (regKeyAppRoot.GetValue("AutoStart") != null)
runAtStartupCheckBox.Checked = Convert.ToBoolean(regKeyAppRoot.GetValue("AutoStart"));
autoSyncCheckBox_CheckedChanged(null, null);
}
private void SaveSettings()
{
RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(@"Software\Webgear\GOContactSync");
regKeyAppRoot.SetValue("SyncOption", (int)_syncOption);
if (!string.IsNullOrEmpty(tbSyncProfile.Text))
regKeyAppRoot.SetValue("SyncProfile", tbSyncProfile.Text);
if (!string.IsNullOrEmpty(UserName.Text))
{
regKeyAppRoot.SetValue("Username", UserName.Text);
if (!string.IsNullOrEmpty(Password.Text))
regKeyAppRoot.SetValue("Password", Encryption.EncryptPassword(UserName.Text, Password.Text));
}
regKeyAppRoot.SetValue("AutoSync", autoSyncCheckBox.Checked.ToString());
regKeyAppRoot.SetValue("AutoSyncInterval", autoSyncInterval.Value.ToString());
regKeyAppRoot.SetValue("AutoStart", runAtStartupCheckBox.Checked);
}
private bool ValidCredentials
{
get
{
bool userNameIsValid = Regex.IsMatch(UserName.Text, @"^(?'id'[a-z0-9\'\%\._\+\-]+)@(?'domain'[a-z0-9\'\%\._\+\-]+)\.(?'ext'[a-z]{2,6})$", RegexOptions.IgnoreCase);
bool passwordIsValid = Password.Text.Length != 0;
bool syncProfileNameIsValid = tbSyncProfile.Text.Length != 0;
setBgColor(UserName, userNameIsValid);
setBgColor(Password, passwordIsValid);
setBgColor(tbSyncProfile, syncProfileNameIsValid);
return userNameIsValid && passwordIsValid && syncProfileNameIsValid;
}
}
private void setBgColor(TextBox box, bool isValid)
{
if (!isValid)
box.BackColor = Color.LightPink;
else
box.BackColor = Color.LightGreen;
}
private void button4_Click(object sender, EventArgs e)
{
Sync();
}
private void Sync()
{
if (!ValidCredentials)
return;
//Sync_ThreadStarter();
ThreadStart starter = new ThreadStart(Sync_ThreadStarter);
Thread thread = new Thread(starter);
thread.Start();
// wait for thread to start
while (!thread.IsAlive)
Thread.Sleep(1);
}
private void Sync_ThreadStarter()
{
TimerSwitch(false);
SetLastSyncText("Syncing...");
SetFormEnabled(false);
if (_sync == null)
{
_sync = new Syncronizer();
_sync.Logger = new Logger();
_sync.DuplicatesFound += new Syncronizer.NotificationHandler(_sync_DuplicatesFound);
_sync.ErrorEncountered += new Syncronizer.NotificationHandler(_sync_ErrorEncountered);
_sync.Logger.LogUpdated += new Logger.LogUpdatedHandler(Logger_LogUpdated);
}
_sync.Logger.ClearLog();
SetSyncConsoleText("");
_sync.Logger.Log("Sync started.", EventType.Information);
//SetSyncConsoleText(_sync.Logger.GetText());
_sync.SyncProfile = tbSyncProfile.Text;
_sync.SyncOption = _syncOption;
try
{
_sync.LoginToGoogle(UserName.Text, Password.Text);
_sync.LoginToOutlook();
_sync.Sync();
SetLastSyncText("Last synced at " + lastSync.ToString());
_sync.Logger.Log("Sync complete.", EventType.Information);
//SetSyncConsoleText(_sync.Logger.GetText());
notifyIcon.BalloonTipTitle = "Complete";
notifyIcon.BalloonTipText = string.Format("{0}. Sync complete.\n Synced: {2} out of {1}.\n Deleted: {3}.", DateTime.Now, _sync.TotalCount, _sync.SyncedCount, _sync.DeletedCount);
notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon.ShowBalloonTip(5000);
}
catch (Exception ex)
{
_sync.Logger.Log(ex.Message, EventType.Error);
//AppendSyncConsoleText(_sync.Logger.GetText());
_sync.Logger.Log("Sync failed.", EventType.Error);
notifyIcon.BalloonTipTitle = "Error";
notifyIcon.BalloonTipText = ex.Message;
notifyIcon.BalloonTipIcon = ToolTipIcon.Error;
notifyIcon.ShowBalloonTip(5000);
}
lastSync = DateTime.Now;
TimerSwitch(true);
SetFormEnabled(true);
}
void Logger_LogUpdated(string Message)
{
AppendSyncConsoleText(Message);
}
void _sync_ErrorEncountered(string title, string message, EventType eventType)
{
notifyIcon.BalloonTipTitle = title;
notifyIcon.BalloonTipText = message;
notifyIcon.BalloonTipIcon = ToolTipIcon.Error;
notifyIcon.ShowBalloonTip(5000);
}
void _sync_DuplicatesFound(string title, string message, EventType eventType)
{
notifyIcon.BalloonTipTitle = title;
notifyIcon.BalloonTipText = message;
notifyIcon.BalloonTipIcon = ToolTipIcon.Warning;
notifyIcon.ShowBalloonTip(5000);
}
public void SetFormEnabled(bool enabled)
{
if (this.InvokeRequired)
{
SwitchHandler h = new SwitchHandler(SetFormEnabled);
this.Invoke(h, new object[] { enabled });
}
else
{
settingsGroupBox.Enabled = enabled;
actionsTableLayout.Enabled = enabled;
}
}
public void SetLastSyncText(string text)
{
if (this.InvokeRequired)
{
TextHandler h = new TextHandler(SetLastSyncText);
this.Invoke(h, new object[] { text });
}
else
lastSyncLabel.Text = text;
}
public void SetSyncConsoleText(string text)
{
if (this.InvokeRequired)
{
TextHandler h = new TextHandler(SetSyncConsoleText);
this.Invoke(h, new object[] { text });
}
else
syncConsole.Text = text;
}
public void AppendSyncConsoleText(string text)
{
if (this.InvokeRequired)
{
TextHandler h = new TextHandler(AppendSyncConsoleText);
this.Invoke(h, new object[] { text });
}
else
syncConsole.Text += text;
}
public void TimerSwitch(bool value)
{
if (this.InvokeRequired)
{
SwitchHandler h = new SwitchHandler(TimerSwitch);
this.Invoke(h, new object[] { value });
}
else
{
if (value)
{
if (autoSyncCheckBox.Checked)
{
autoSyncInterval.Enabled = autoSyncCheckBox.Checked;
syncTimer.Enabled = autoSyncCheckBox.Checked;
nextSyncLabel.Visible = autoSyncCheckBox.Checked;
}
}
else
{
autoSyncInterval.Enabled = value;
syncTimer.Enabled = value;
nextSyncLabel.Visible = value;
}
}
}
private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!requestClose)
{
SaveSettings();
e.Cancel = true;
}
HideForm();
}
private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (_sync != null)
_sync.LogoffOutlook();
SaveSettings();
notifyIcon.Dispose();
}
private void syncOptionBox_SelectedIndexChanged(object sender, EventArgs e)
{
int index = syncOptionBox.SelectedIndex;
if (index == -1)
return;
SetSyncOption(index);
}
private void SetSyncOption(int index)
{
_syncOption = (SyncOption)index;
for (int i = 0; i < syncOptionBox.Items.Count; i++)
{
if (i == index)
syncOptionBox.SetItemCheckState(i, CheckState.Checked);
else
syncOptionBox.SetItemCheckState(i, CheckState.Unchecked);
}
}
private void SettingsForm_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
Hide();
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (WindowState == FormWindowState.Normal)
HideForm();
else
ShowForm();
}
private void autoSyncCheckBox_CheckedChanged(object sender, EventArgs e)
{
autoSyncInterval.Enabled = autoSyncCheckBox.Checked;
syncTimer.Enabled = autoSyncCheckBox.Checked;
nextSyncLabel.Visible = autoSyncCheckBox.Checked;
}
private void syncTimer_Tick(object sender, EventArgs e)
{
if (lastSync != null)
{
TimeSpan syncTime = DateTime.Now - lastSync;
TimeSpan limit = new TimeSpan(0, (int)autoSyncInterval.Value, 0);
if (syncTime < limit)
{
TimeSpan diff = limit - syncTime;
string str = "Next sync in";
if (diff.Hours != 0)
str += " " + diff.Hours + " h";
if (diff.Minutes != 0 || diff.Hours != 0)
str += " " + diff.Minutes + " min";
if (diff.Seconds != 0)
str += " " + diff.Seconds + " s";
nextSyncLabel.Text = str;
return;
}
}
Sync();
}
private void resetMatchesButton_Click(object sender, EventArgs e)
{
if (_sync == null)
{
_sync = new Syncronizer();
_sync.Logger = new Logger();
}
_sync.LoginToGoogle(UserName.Text, Password.Text);
_sync.LoginToOutlook();
_sync.Load();
_sync.ResetMatches();
AppendSyncConsoleText(DateTime.Now + " Contacts unlinked" + Environment.NewLine);
}
private void ShowForm()
{
Show();
WindowState = FormWindowState.Normal;
}
private void HideForm()
{
WindowState = FormWindowState.Minimized;
Hide();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
ShowForm();
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
HideForm();
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
requestClose = true;
Close();
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
AboutBox about = new AboutBox();
about.Show();
}
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
Sync();
}
private void SettingsForm_Load(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(UserName.Text) ||
string.IsNullOrEmpty(Password.Text) ||
string.IsNullOrEmpty(tbSyncProfile.Text))
{
// this is the first load, show form
ShowForm();
UserName.Focus();
}
else
HideForm();
}
private void runAtStartupCheckBox_CheckedChanged(object sender, EventArgs e)
{
RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
if (runAtStartupCheckBox.Checked)
{
// add to registry
regKeyAppRoot.SetValue("GoogleContactSync", Application.ExecutablePath);
}
else
{
// remove from registry
regKeyAppRoot.DeleteValue("GoogleContactSync");
}
}
private void UserName_TextChanged(object sender, EventArgs e)
{
ValidateSyncButton();
}
private void Password_TextChanged(object sender, EventArgs e)
{
ValidateSyncButton();
}
private void ValidateSyncButton()
{
syncButton.Enabled = ValidCredentials;
}
private void deleteDuplicatesButton_Click(object sender, EventArgs e)
{
//DeleteDuplicatesForm f = new DeleteDuplicatesForm(_sync
}
private void tbSyncProfile_TextChanged(object sender, EventArgs e)
{
ValidateSyncButton();
}
private void btSyncDelete_CheckedChanged(object sender, EventArgs e)
{
if (_sync == null)
{
_sync = new Syncronizer();
_sync.Logger = new Logger();
}
_sync.SyncDelete = btSyncDelete.Checked;
}
}
//internal class EventLogger : ILogger
//{
// private TextBox _box;
// public EventLogger(TextBox box)
// {
// _box = box;
// }
// #region ILogger Members
// public void Log(string message, EventType eventType)
// {
// _box.Text += message;
// }
// #endregion
//}
}
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.