public static GoogleContactsManager CreateContactsManager()
        {
            var settings = new RequestSettings("Maintain Work Contacts",
                                               Settings.Default.EmailAddressUsername,
                                               Utility.ToInsecureString(Utility.DecryptString(Settings.Default.EmailAddressPassword)));
            settings.AutoPaging = true;
            var request = new ContactsRequest(settings);

            IEnumerable<Group> groups;
            try
            {
                groups = request.GetGroups().Entries;

                string groupName = Settings.Default.GoogleWorkGroupName;
                var workGroup = groups.Where(g => g.Title.Contains(groupName)).FirstOrDefault();
                if (workGroup == null)
                {
                    throw new ArgumentNullException("WorkGroup", "There is no group called " + groupName + " in the Google account.");
                }

                var myContactsGroup = groups.Where(g => g.Title.Contains("My Contacts")).FirstOrDefault();
                if (myContactsGroup == null)
                {
                    throw new ArgumentNullException("MyContactsGroup", "There is no group called My Contacts in the Google account.");
                }

                GoogleContactsManager contactsManager = new GoogleContactsManager();
                contactsManager.Request = request;
                contactsManager.MyContactsGroup = myContactsGroup;
                contactsManager.WorkGroup = workGroup;
                return contactsManager;
            }
            catch (GDataRequestException exception)
            {
                throw new InvalidCredentialsException("Invalid username and password were provided.", exception);
            }
        }
 public ContactComparer(List<WorkContact> oohSheetContacts, GoogleContactsManager contactsManager)
 {
     _oohSheetContacts = oohSheetContacts;
     _googleContacts = contactsManager.GetContacts();
     _contactsManager = contactsManager;
 }
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            WorkerState state = new WorkerState();
            state.ProgressText = "Parsing word document...";

            BackgroundWorker worker = sender as BackgroundWorker;
            worker.ReportProgress(0, state);

            WordDocumentParser wordDocumentParser;
            try
            {
                wordDocumentParser = new WordDocumentParser(Settings.Default.OohSheetLocation);
            }
            catch (FileNotFoundException exception)
            {
                state.ThrownException = exception;
                state.ErrorMessage = "Could not find the word document. File path is: " + exception.FileName + ".";
                worker.ReportProgress(0, state);
                return;
            }

            List<WorkContact> oohSheetContacts = wordDocumentParser.GetContacts();

            try
            {
                _contactManager = GoogleContactsManager.CreateContactsManager();
            }
            catch (InvalidCredentialsException argumentException)
            {
                state.ThrownException = argumentException;
                state.ErrorMessage = "Invalid username and password were provided.";
                worker.ReportProgress(0, state);
                return;
            }
            catch (ArgumentNullException nullArg)
            {
                state.ThrownException = nullArg;

                string groupName = string.Empty;
                string paramName = nullArg.ParamName;
                if (paramName == "WorkGroup")
                {
                    groupName = Settings.Default.GoogleWorkGroupName;
                }
                else
                {
                    groupName = "My Contacts";
                }
                state.ErrorMessage = "There is no group called " + groupName + " in the Google account.";
                worker.ReportProgress(0, state);
                return;
            }

            state.ProgressText = "Comparing Google contacts to the word document...";
            worker.ReportProgress(0, state);

            var comparer = new ContactComparer(oohSheetContacts, _contactManager);
            _actions = comparer.Compare();

            state.ProgressText = "Finished comparing.";
            worker.ReportProgress(0, state);
        }