internal void MailBatchUploaded(
     MailBatch mailBatch,
     UploadResult batchUploadResult) {
   Debug.Assert(batchUploadResult == UploadResult.BadRequest ||
                batchUploadResult == UploadResult.Created);
   try {
     GoogleEmailUploaderTrace.EnteringMethod(
         "GoogleEmailUploaderModel.MailBatchUploaded");
     this.ProcessUploadMailBatchData(mailBatch);
     this.WriteCurrentStatistics(mailBatch);
     if (this.MailBatchUploadedEvent != null) {
       this.MailBatchUploadedEvent(mailBatch);
     }
     this.pauseTime = GoogleEmailUploaderConfig.MinimumPauseTime;
   } finally {
     GoogleEmailUploaderTrace.ExitingMethod(
         "GoogleEmailUploaderModel.MailBatchUploaded");
   }
 }
 internal void MailBatchUploadTryStart(MailBatch mailBatch) {
   try {
     GoogleEmailUploaderTrace.EnteringMethod(
         "GoogleEmailUploaderModel.MailBatchUploadStart");
     if (GoogleEmailUploaderConfig.LogFullXml) {
       GoogleEmailUploaderTrace.WriteLine("Request Xml:");
       GoogleEmailUploaderTrace.WriteXml(mailBatch.GetBatchXML());
     }
     this.WriteCurrentStatistics(mailBatch);
     if (this.MailBatchUploadTryStartEvent != null) {
       this.MailBatchUploadTryStartEvent(mailBatch);
     }
   } finally {
     GoogleEmailUploaderTrace.ExitingMethod(
         "GoogleEmailUploaderModel.MailBatchUploadTryStart");
   }
 }
 internal bool MailBatchUploadFailure(
     MailBatch mailBatch,
     UploadResult batchUploadResult) {
   Debug.Assert(batchUploadResult == UploadResult.InternalError ||
       batchUploadResult == UploadResult.ServiceUnavailable ||
       batchUploadResult == UploadResult.Unknown);
   try {
     GoogleEmailUploaderTrace.EnteringMethod(
         "GoogleEmailUploaderModel.MailBatchUploadFailure");
     if (GoogleEmailUploaderConfig.LogFullXml) {
       GoogleEmailUploaderTrace.WriteLine("Response Xml:");
       GoogleEmailUploaderTrace.WriteXml(mailBatch.ResponseXml);
     }
     this.WriteCurrentStatistics(mailBatch);
     if (batchUploadResult == UploadResult.InternalError ||
         batchUploadResult == UploadResult.Unknown) {
       if (this.pauseTime == GoogleEmailUploaderConfig.MaximumPauseTime) {
         this.ProcessUploadMailBatchData(mailBatch);
         this.pauseTime = GoogleEmailUploaderConfig.MinimumPauseTime;
         return false;
       }
       this.TimedPauseUpload(PauseReason.ServerInternalError);
       this.RaisePauseTime();
     } else {
       GoogleEmailUploaderTrace.WriteLine(
           "Batch failed with service unavailable");
       this.TimedPauseUpload(PauseReason.ServiceUnavailable);
       this.RaisePauseTime();
     }
     return true;
   } finally {
     GoogleEmailUploaderTrace.ExitingMethod(
         "GoogleEmailUploaderModel.MailBatchUploadFailure");
   }
 }
 void WriteCurrentStatistics(MailBatch mailBatch) {
   StringBuilder sb = new StringBuilder();
   sb.AppendFormat(
       "RamainingItemCount: {0}",
       this.RemainingItemCount);
   sb.AppendFormat(
       " UploadedEmailCount: {0} SelectedEmailCount: {1}" +
           " FailedEmailCount: {2}",
       this.UploadedEmailCount,
       this.SelectedEmailCount,
       this.FailedEmailCount);
   sb.AppendFormat(
       " UploadedContactCount: {0} SelectedContactCount: {1}" +
           " FailedContactCount: {2}",
       this.UploadedContactCount,
       this.SelectedContactCount,
       this.FailedContactCount);
   sb.AppendFormat(
       " UploadSpeed: {0}",
       this.UploadSpeed);
   sb.AppendFormat(
       " UploadTimeRemaining: {0}",
       this.UploadTimeRemaining);
   if (mailBatch != null) {
     sb.AppendFormat(
         " mailBatch.MailCount: {0}",
         mailBatch.MailCount);
     sb.AppendFormat(
         " mailBatch.FailedCount: {0}",
         mailBatch.FailedCount);
   }
   GoogleEmailUploaderTrace.WriteLine(sb.ToString());
 }
 internal void FillMailBatch(MailBatch mailBatch) {
   Debug.Assert(this.modelState == ModelState.Uploading ||
                this.modelState == ModelState.UploadingPause);
   try {
     GoogleEmailUploaderTrace.EnteringMethod(  
         "GoogleEmailUploaderModel.FillMailBatch");
     this.WriteCurrentStatistics(mailBatch);
     if (this.MailBatchFillingStartEvent != null) {
       this.MailBatchFillingStartEvent(mailBatch);
     }
     if (this.useCurrent) {
       this.mailUploader.PauseEvent.WaitOne();
       bool added =
           mailBatch.AddMail(
               this.mailIterator.CurrentMail,
               this.mailIterator.CurrentFolderModel);
       Debug.Assert(added);
       if (this.MailBatchFillingEvent != null) {
         this.MailBatchFillingEvent(mailBatch,
                                    this.mailIterator.CurrentMail);
       }
       this.useCurrent = false;
     }
     while (this.mailIterator.MoveToNextMail()) {
       this.mailUploader.PauseEvent.WaitOne();
       if (this.mailIterator.CurrentMail.Rfc822Buffer.Length == 0) {
         // If we cant read the mail behave as if we have successfully
         // uploaded the mail.
         this.mailIterator.CurrentFolderModel.SuccessfullyUploaded(
             this.mailIterator.CurrentMail.MailId);
         this.uploadedEmailCount++;
         continue;
       }
       if (!mailBatch.AddMail(
               this.mailIterator.CurrentMail,
               this.mailIterator.CurrentFolderModel)) {
         // we could not add the current mail
         // so record so that we would add it in the next iteration.
         this.useCurrent = true;
         break;
       }
       if (this.MailBatchFillingEvent != null) {
         this.MailBatchFillingEvent(mailBatch,
                                    this.mailIterator.CurrentMail);
       }
       if (mailBatch.IsBatchFilled()) {
         // If mailbatch is filled to the rim then we cant add more.
         break;
       }
     }
     if (this.MailBatchFillingEndEvent != null) {
       this.MailBatchFillingEndEvent(mailBatch);
     }
   } finally {
     this.WriteCurrentStatistics(mailBatch);
     GoogleEmailUploaderTrace.ExitingMethod(
         "GoogleEmailUploaderModel.FillMailBatch");
   }
 }
 void ProcessUploadMailBatchData(MailBatch mailBatch) {
   Debug.Assert(this.modelState == ModelState.UploadingPause ||
                this.modelState == ModelState.Uploading);
   foreach (MailBatchDatum batchDatum in mailBatch.MailBatchData) {
     if (batchDatum.Uploaded) {
       batchDatum.FolderModel.SuccessfullyUploaded(batchDatum.MailId);
       this.uploadedEmailCount++;
     } else {
       FailedMailDatum failedMailDatum =
           new FailedMailDatum(batchDatum.MessageHead,
                               batchDatum.FailedReason);
       batchDatum.FolderModel.FailedToUpload(batchDatum.MailId,
                                             failedMailDatum);
       this.failedEmailCount++;
     }
   }
   this.lkgStatePersistor.SaveLKGState(this);
 }
 internal MailUploader(IHttpFactory httpFactory,
                       string emailId,
                       string password,
                       string applicationName,
                       GoogleEmailUploaderModel googleEmailUploaderModel) {
   this.HttpFactory = httpFactory;
   this.EmailId = emailId;
   this.Password = password;
   GoogleAuthenticator authenticator =
       new GoogleAuthenticator(httpFactory,
                               AccountType.GoogleOrHosted,
                               applicationName);
   AuthenticationResponse resp =
       authenticator.AuthenticateForService(this.EmailId,
                                            this.Password,
                                            "apps");
   this.MailAuthenticationToken = resp.AuthToken;
   resp = authenticator.AuthenticateForService(this.EmailId,
                                               this.Password,
                                               "cp");
   this.ContactAuthenticationToken = resp.AuthToken;
   this.GoogleEmailUploaderModel = googleEmailUploaderModel;
   string[] splits = emailId.Split('@');
   this.UserName = splits[0];
   this.DomainName = splits[1];
   this.MailBatch = new MailBatch(googleEmailUploaderModel);
   this.ContactEntry = new ContactEntry(googleEmailUploaderModel);
   this.PauseEvent = new ManualResetEvent(true);
   this.batchMailUploadUrl =
       string.Format(
           GoogleEmailUploaderConfig.EmailMigrationUrl,
           this.DomainName,
           this.UserName);
   this.batchContactUploadUrl =
       string.Format(
           MailUploader.ContactMigrationURLTemplate,
           this.EmailId);
   this.ApplicationName = applicationName;
 }
 void googleEmailUploaderModel_MailBatchUploadedEvent(MailBatch mailBatch) {
   if (this.IsHandleCreated && !this.IsDisposed) {
     this.Invoke(new BoolDelegate(this.UpdateStatistics),
                 new object[] { true });
   }
 }
 void googleEmailUploaderModel_MailBatchUploadTryStartEvent(
     MailBatch mailBatch) {
   if (this.IsHandleCreated && !this.IsDisposed) {
     if (!this.googleEmailUploaderModel.IsPaused) {
       this.Invoke(new StringDelegate(this.UpdateMessageLabel),
                   new object[] { string.Empty });
     }
   }
 }
 void googleEmailUploaderModel_MailBatchFillingEndEvent(
     MailBatch mailBatch) {
   if (this.IsHandleCreated && !this.IsDisposed) {
     if (!this.googleEmailUploaderModel.IsPaused) {
       this.currentMailBatchSize = mailBatch.MailCount;
       this.Invoke(new BoolDelegate(this.UpdateStatistics),
                   new object [] { true });
     }
   }
 }