Menu

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

Download this file

684 lines (612 with data), 19.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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
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;
using System.Diagnostics;
using System.IO;
using System.Runtime.Remoting;
namespace GoContactSyncMod
{
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();
Logger.LogUpdated += new Logger.LogUpdatedHandler(Logger_LogUpdated);
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"));
if (regKeyAppRoot.GetValue("ReportSyncResult") != null)
reportSyncResultCheckBox.Checked = Convert.ToBoolean(regKeyAppRoot.GetValue("ReportSyncResult"));
if (regKeyAppRoot.GetValue("SyncDeletion") != null)
btSyncDelete.Checked = Convert.ToBoolean(regKeyAppRoot.GetValue("SyncDeletion"));
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);
regKeyAppRoot.SetValue("ReportSyncResult", reportSyncResultCheckBox.Checked);
regKeyAppRoot.SetValue("SyncDeletion", btSyncDelete.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()
{
try
{
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);
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
}
private void Sync_ThreadStarter()
{
try
{
TimerSwitch(false);
SetLastSyncText("Syncing...");
SetFormEnabled(false);
if (_sync == null)
{
_sync = new Syncronizer();
_sync.DuplicatesFound += new Syncronizer.NotificationHandler(_sync_DuplicatesFound);
_sync.ErrorEncountered += new Syncronizer.ErrorNotificationHandler(_sync_ErrorEncountered);
}
Logger.ClearLog();
SetSyncConsoleText("");
Logger.Log("Sync started.", EventType.Information);
//SetSyncConsoleText(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());
Logger.Log("Sync complete.", EventType.Information);
//SetSyncConsoleText(Logger.GetText());
if (reportSyncResultCheckBox.Checked)
{
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 (Google.GData.Client.GDataRequestException ex)
{
if (ex.InnerException is System.Net.WebException)
{
Logger.Log("Cannot connect to Google, please check if for available internet connection and proxy settings if applicable: "+((System.Net.WebException)ex.InnerException).Message, EventType.Warning);
}
else
{
ErrorHandler.Handle(ex);
}
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
lastSync = DateTime.Now;
TimerSwitch(true);
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
finally
{
SetFormEnabled(true);
}
}
void Logger_LogUpdated(string Message)
{
AppendSyncConsoleText(Message);
}
void _sync_ErrorEncountered(string title, Exception ex, EventType eventType)
{
// do not show ErrorHandler, as there may be multiple exceptions that would nag the user
Logger.Log(ex.ToString(), EventType.Error);
string message = String.Format("Error Saving Contact: {0}.\nPlease report complete ErrorMessage from Log to the Tracker\nat https://sourceforge.net/tracker/?group_id=369321", ex.Message);
notifyIcon.BalloonTipTitle = title;
notifyIcon.BalloonTipText = message;
notifyIcon.BalloonTipIcon = ToolTipIcon.Error;
notifyIcon.ShowBalloonTip(5000);
}
void _sync_DuplicatesFound(string title, string message, EventType eventType)
{
Logger.Log(message, EventType.Warning);
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
{
resetMatchesLinkLabel.Enabled = enabled;
settingsGroupBox.Enabled = enabled;
syncButton.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;
}
}
}
// Fix for WinXP and older systems, that do not continue with shutdown until all programs have closed
// FormClosing would hold system shutdown, when it sets the cancel to true
private static int WM_QUERYENDSESSION = 0x11;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_QUERYENDSESSION)
requestClose = true;
// If this is WM_QUERYENDSESSION, the form must exit and not just hide
base.WndProc(ref m);
}
private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!requestClose)
{
SaveSettings();
e.Cancel = true;
}
HideForm();
}
private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
if (_sync != null)
_sync.LogoffOutlook();
SaveSettings();
notifyIcon.Dispose();
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
}
private void syncOptionBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int index = syncOptionBox.SelectedIndex;
if (index == -1)
return;
SetSyncOption(index);
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
}
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 resetMatchesLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
SetFormEnabled(false);
// force deactivation to show up
Application.DoEvents();
try
{
if (_sync == null)
{
_sync = new Syncronizer();
}
_sync.LoginToGoogle(UserName.Text, Password.Text);
_sync.LoginToOutlook();
_sync.Load();
_sync.ResetMatches();
AppendSyncConsoleText(DateTime.Now + " Contacts unlinked" + Environment.NewLine);
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
finally
{
SetFormEnabled(true);
}
}
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.SyncDelete = btSyncDelete.Checked;
}
private void Donate_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://sourceforge.net/project/project_donations.php?group_id=369321");
}
private void Donate_MouseEnter(object sender, EventArgs e)
{
Donate.BackColor = System.Drawing.Color.LightGray;
}
private void Donate_MouseLeave(object sender, EventArgs e)
{
Donate.BackColor = System.Drawing.Color.Transparent;
}
private void hideButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void proxySettingsLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// TODO: Implement a dialog with proxy settings and use them when connecting with Google
// Alpha quick'm'dirty workaround solution
try
{
if (MessageBox.Show("The proxy configuration is in beta stage, a more comfortable solution is to come. For now, you have to edit the Applications Config file with administrator privileges.\n\nOpen Configuration file now?",
"GO Contact Sync Mod", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.OK)
{
StartFileSystemWatcher();
try
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
// full path not required, notepad is usually installed in system directory
psi.FileName = "notepad.exe";
// Vista and Win7 UAC Control
psi.Verb = "runas";
Process.Start(psi);
}
catch (Exception)
{
// fallback if notepad is not found/installed
// in default configuration this will open Internet Explorer, but user can then see the path and open an editor himself
Process.Start(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
}
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
}
FileSystemWatcher fsw = null;
private void StartFileSystemWatcher()
{
if (fsw == null)
{
fsw = new FileSystemWatcher();
fsw.Path = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
fsw.Filter = Path.GetFileName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
fsw.NotifyFilter = NotifyFilters.LastWrite;
fsw.Changed += delegate
{
this.BeginInvoke((MethodInvoker)delegate
{
MessageBox.Show(this, "If you have changed your proxy configuration, restart the Program to take effect.", "GO Contact Sync Mod");
});
// cleanup after we've notified the user
fsw.EnableRaisingEvents = false;
fsw.Dispose();
fsw = null;
};
fsw.EnableRaisingEvents = true;
}
}
private void SettingsForm_HelpButtonClicked(object sender, CancelEventArgs e)
{
ShowHelp();
}
private void SettingsForm_HelpRequested(object sender, HelpEventArgs hlpevent)
{
ShowHelp();
}
private void ShowHelp()
{
// go to the page showing the help and howto instructions
Process.Start("http://googlesyncmod.sourceforge.net/");
}
}
//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.