Exemplo n.º 1
0
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            // log the start
            Logger.Log("Worker started.", EventType.Debug);

            // create vars
            var worker  = (BackgroundWorker)sender;
            var context = (SyncContext)e.Argument;

            e.Result = context;
            bool duplicates = false;

            // initialize the syncer
            context.Report(worker, Resources.SettingsForm_InitializeSync, ToolTipIcon.Info);
            var sync = new Syncronizer()
            {
                SyncProfile  = WindowsIdentity.GetCurrent().User.Value,
                SyncOption   = context.Mode,
                SyncDelete   = true,
                PromptDelete = false,
                UseFileAs    = true,
                SyncNotes    = false,
                SyncContacts = true,
            };

            sync.ErrorEncountered += (title, ex, type) =>
                                     // simply log the error (it's counted by the Syncronizer)
                                     Logger.Log(ex.Message, type);
            sync.DuplicatesFound += (title, text) =>
            {
                // log all duplicates and set the flag
                Logger.Log(text, EventType.Warning);
                duplicates = true;
            };

            // log into Google
            context.Report(worker, Resources.SettingsForm_GoogleLogon, ToolTipIcon.Info);
            sync.LoginToGoogle(context.UserName, DecodePassword(context.Password));
            try
            {
                // access Outlook
                context.Report(worker, Resources.SettingsForm_OutlookLogon, ToolTipIcon.Info);
                sync.LoginToOutlook();
                try
                {
                    // reset matches
                    if ((context.Tasks & WorkerTasks.ResetMatches) != 0)
                    {
                        context.Report(worker, Resources.SettingsForm_ResetMatches, ToolTipIcon.Info);
                        sync.LoadContacts();
                        sync.ResetContactMatches();
                    }

                    // sync
                    if ((context.Tasks & WorkerTasks.Synchronize) != 0)
                    {
                        context.Report(worker, Resources.SettingsForm_SyncContacts, ToolTipIcon.Info);
                        sync.Sync();
                    }
                }
                finally
                {
                    // log out from Outlook
                    context.Report(worker, Resources.SettingsForm_OutlookLogoff, ToolTipIcon.Info);
                    sync.LogoffOutlook();
                }
            }
            finally
            {
                // log out from Google
                context.Report(worker, Resources.SettingsForm_GoogleLogoff, ToolTipIcon.Info);
                sync.LogoffGoogle();
            }

            // finalizing
            var successful = sync.ErrorCount == 0 && sync.SkippedCountNotMatches == 0;

            context.Report
            (
                worker,
                string.Format(!successful ? Resources.SettingsForm_SyncIncomplete : duplicates ? Resources.SettingsForm_SyncSuccessfulWithDuplicates : Resources.SettingsForm_SyncSuccessful, DateTime.Now, sync.TotalCount, sync.SyncedCount, sync.DeletedCount, sync.SkippedCountNotMatches, sync.ErrorCount),
                successful ? ToolTipIcon.Info : ToolTipIcon.Warning
            );

            // log the result
            Logger.Log("Worker ended.", EventType.Debug);
        }