internal UploadView(GoogleEmailUploaderModel googleEmailUploaderModel) {
      this.currentMailBatchSize = 0;
      this.googleEmailUploaderModel = googleEmailUploaderModel;

      this.notificationTrayIcon = new NotifyIcon();
      this.notificationTrayIcon.Icon = Resources.GMailIcon;
      this.notificationTrayIcon.Text = Resources.GoogleEmailUploaderAppNameText;
      this.notificationTrayIcon.Visible = false;
      this.notificationTrayIcon.DoubleClick +=
          new EventHandler(this.notificationTryIcon_DoubleClick);

      this.StartPosition = FormStartPosition.CenterScreen;
      this.Text = Resources.GoogleEmailUploaderAppNameText;
      this.Icon = Resources.GMailIcon;
      this.MaximizeBox = false;
      this.FormBorderStyle = FormBorderStyle.FixedSingle;
      this.BackgroundImage = Resources.GoogleEmailUploaderBackgroundImage;
      this.BackColor = Color.White;
      this.Size = new Size(530, 370);
      this.Closing +=
          new CancelEventHandler(this.UploadView_FormClosing);
      this.InitializeComponent();
      this.HookModelEvents();
      this.Load += new EventHandler(this.UploadView_Load);
    }
    internal LoginView(GoogleEmailUploaderModel googleEmailUploaderModel,
                       bool wasRestarted) {
      this.googleEmailUploaderModel = googleEmailUploaderModel;
      this.StartPosition = FormStartPosition.CenterScreen;
      this.Text = Resources.GoogleEmailUploaderAppNameText;
      this.Icon = Resources.GMailIcon;
      this.MaximizeBox = false;
      this.FormBorderStyle = FormBorderStyle.FixedDialog;
      this.BackgroundImage = Resources.GoogleEmailUploaderBackgroundImage;

      this.BackColor = Color.White;
      this.Size = new Size(530, 370);

      // We have to take care of the case when the SignIn View is restarted.
      // In that case we will not display the introduction dialog but will 
      // directly jump to the sign in Dialog.
      if (!wasRestarted) {
        this.ShowInstructionsScreen();
      } else {
        this.ShowSigninScreen();
      }

      this.result = LoginViewResult.Cancel;
      this.googleEmailUploaderModel.LoadingClientsEvent +=
          new VoidDelegate(this.googleEmailUploaderModel_LoadingClientsEvent);
    }
    internal SelectView(GoogleEmailUploaderModel googleEmailUploaderModel) {
      this.StartPosition = FormStartPosition.CenterScreen;
      this.googleEmailUploaderModel = googleEmailUploaderModel;
      this.Text = Resources.GoogleEmailUploaderAppNameText;
      this.Icon = Resources.GMailIcon;
      this.MaximizeBox = false;
      this.FormBorderStyle = FormBorderStyle.FixedSingle;
      this.BackColor = Color.White;
      this.BackgroundImage = Resources.GoogleEmailUploaderBackgroundImage;
      this.Size = new Size(530, 370);
      this.InitializeComponent();

      if (this.googleEmailUploaderModel.TotalSelectedItemCount == 0) {
        this.nextButton.Enabled = false;
      } else {
        this.nextButton.Enabled = true;
      }

      this.result = SelectViewResult.Closed;
    }
 /// <summary>
 /// Saves the LKG state of all the clients in model.
 /// </summary>
 internal void SaveLKGState(GoogleEmailUploaderModel uploaderModel) {
   this.userXmlElement.RemoveAll();
   this.userXmlElement.SetAttribute(
       LKGStatePersistor.MailIdAttrName,
       this.emailId);
   this.userXmlElement.SetAttribute(
       LKGStatePersistor.ArchiveEverythingAttrName,
       uploaderModel.IsArchiveEverything.ToString());
   this.userXmlElement.SetAttribute(
       LKGStatePersistor.UploadSpeedAttrName,
       uploaderModel.UploadSpeed.ToString());
   this.userXmlElement.SetAttribute(
       LKGStatePersistor.FolderToLabelMappingAttrName,
       uploaderModel.IsFolderToLabelMappingEnabled.ToString());
   foreach (ClientModel clientModel in uploaderModel.ClientModels) {
     this.SaveClientModelState(
         this.userXmlElement,
         clientModel);
   }
   this.xmlDocument.Save(this.lkgStateFilePath);
 }
 /// <summary>
 /// Loads the LKG state of the list of clients using the information
 /// persisted.
 /// This is done by walking the xml tree and finding the client model
 /// corresponding to the tree node and restoring its state.
 /// </summary>
 internal void LoadLKGState(GoogleEmailUploaderModel uploaderModel) {
   string archiveEverythingString =
       this.userXmlElement.GetAttribute(
           LKGStatePersistor.ArchiveEverythingAttrName);
   bool archiveEverything = false;
   try {
     archiveEverything = bool.Parse(archiveEverythingString);
   } catch {
     // If we get an exception we assume by default we do not archive
     // everything
   }
   uploaderModel.SetArchiving(archiveEverything);
   string labelMappingString =
       this.userXmlElement.GetAttribute(
           LKGStatePersistor.FolderToLabelMappingAttrName);
   bool labelMapping = true;
   try {
     labelMapping = bool.Parse(labelMappingString);
   } catch {
     // If we get an exception we assume by default we do label mapping
   }
   uploaderModel.SetFolderToLabelMapping(labelMapping);
   string uploadSpeedString =
       this.userXmlElement.GetAttribute(
           LKGStatePersistor.UploadSpeedAttrName);
   double uploadSpeed;
   try {
     uploadSpeed = double.Parse(uploadSpeedString);
     if (uploadSpeed <= 0.0) {
       uploadSpeed = 0.00005;
     }
     uploaderModel.SetUploadSpeed(uploadSpeed);
   } catch {
     // If we get an exception we dont update the speed. Let the speed
     // be whatever is the speed for the test upload.
   }
   foreach (XmlNode childXmlNode in this.userXmlElement.ChildNodes) {
     XmlElement childXmlElement = childXmlNode as XmlElement;
     if (childXmlElement == null) {
       continue;
     }
     if (childXmlElement.Name != LKGStatePersistor.ClientElementName) {
       continue;
     }
     string clientName =
         childXmlElement.GetAttribute(LKGStatePersistor.NameAttrName);
     foreach (ClientModel clientModel in uploaderModel.ClientModels) {
       if (clientName != clientModel.Client.Name) {
         continue;
       }
       this.LoadClientModelState(
           childXmlElement,
           clientModel);
     }
   }
 }
 internal FolderModel(TreeNodeModel parent,
                      IFolder folder,
                      GoogleEmailUploaderModel googleEmailUploaderModel)
   : base(parent, googleEmailUploaderModel) {
   this.Folder = folder;
   this.subFolderModels = new ArrayList();
   foreach (IFolder folderIter in folder.SubFolders) {
     this.subFolderModels.Add(
         new FolderModel(
             this,
             folderIter,
             googleEmailUploaderModel));
   }
   this.mailUploadData = new Hashtable();
 }
 internal StoreModel(TreeNodeModel parent,
                     IStore store,
                     GoogleEmailUploaderModel googleEmailUploaderModel)
   : base(parent, googleEmailUploaderModel) {
   this.Store = store;
   this.folderModels = new ArrayList();
   foreach (IFolder folderIter in store.Folders) {
     this.folderModels.Add(
         new FolderModel(
             this,
             folderIter,
             googleEmailUploaderModel));
   }
   this.contactUploadData = new Hashtable();
 }
 internal ClientModel(IClient client,
                      GoogleEmailUploaderModel googleEmailUploaderModel)
   : base(null, googleEmailUploaderModel) {
   this.Client = client;
   this.storeModels = new ArrayList();
   foreach (IStore storeIter in client.Stores) {
     this.storeModels.Add(
         new StoreModel(
             this,
             storeIter,
             googleEmailUploaderModel));
   }
 }
 internal TreeNodeModel(TreeNodeModel parent,
                        GoogleEmailUploaderModel googleEmailUploaderModel) {
   this.parent = parent;
   this.GoogleEmailUploaderModel = googleEmailUploaderModel;
   this.isSelected = false;
 }
 internal ContactEntry(GoogleEmailUploaderModel googleEmailUploaderModel) {
   this.googleEmailUploaderModel = googleEmailUploaderModel;
   this.MemoryStream = new MemoryStream(1024 * 1024);
 }
 internal MailBatch(GoogleEmailUploaderModel googleEmailUploaderModel) {
   this.GoogleEmailUploaderModel = googleEmailUploaderModel;
   this.MemoryStream = new MemoryStream(
       GoogleEmailUploaderConfig.MaximumBatchSize);
   this.MemoryBufferArray = new char[MailBatch.DefaultCopyStepSize];
   this.MailBatchData = new ArrayList();
 }
 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;
 }
    static void Main(string[] args) {
      Program.MessageId =
          Program.RegisterWindowMessage(Program.UniqueIdentifier);

      using (Mutex mutex =
          new Mutex(false, Program.UniqueIdentifier)) {
        if (mutex.WaitOne(1, false)) {
          // First instance...
          try {
            // Set up tracing facility
            string traceFilePath =
                Path.Combine(Application.LocalUserAppDataPath,
                             "GoogleEmailUploaderTrace.txt");

            GoogleEmailUploaderConfig.InitializeConfiguration();
            GoogleEmailUploaderTrace.Initalize(traceFilePath);
            GoogleEmailUploaderModel.LoadClientFactories();
            if (GoogleEmailUploaderModel.ClientFactories.Count == 0) {
              MessageBox.Show(Resources.NoClientPluginsText,
                              Resources.GoogleEmailUploaderAppNameText);
              return;
            }

            bool isRestarted = false;
            while (true) {
              // Create a google email uploader model.
              using (GoogleEmailUploaderModel googleEmailUploaderModel =
                  new GoogleEmailUploaderModel()) {

                // Try to login.
                LoginView loginView =
                    new LoginView(googleEmailUploaderModel, isRestarted);
                Application.Run(loginView);
                isRestarted = true;
                if (loginView.Result == LoginViewResult.UploadForbidden) {
                  continue;
                }

                if (loginView.Result == LoginViewResult.Cancel) {
                  // If could canceled or closed the login then
                  // exit the application.
                  break;
                }

                // Start the select View.
                SelectView selectView =
                    new SelectView(googleEmailUploaderModel);
                Application.Run(selectView);
                if (selectView.Result == SelectViewResult.Cancel ||
                    selectView.Result == SelectViewResult.Closed) {
                  // If could canceled or closed the select view then
                  // exit the application.
                  break;
                }

                if (selectView.Result == SelectViewResult.Restart) {
                  continue;
                }

                // Start the upload model.
                UploadView uploadView = new UploadView(googleEmailUploaderModel);
                googleEmailUploaderModel.StartUpload();
                Application.Run(uploadView);
                googleEmailUploaderModel.WaitForUploadingThread();
                break;
              }
            }
          } catch (Exception excep) {
            GoogleEmailUploaderTrace.WriteLine(excep.ToString());
          } finally {
            GoogleEmailUploaderTrace.Close();
          }
        } else {
          int bsmRecipients = Program.BSM_APPLICATIONS;
          Program.BroadcastSystemMessage(Program.BSF_IGNORECURRENTTASK |
                                            Program.BSF_POSTMESSAGE,
                                         ref bsmRecipients,
                                         Program.MessageId,
                                         0,
                                         0);
        }
      }
    }