private void LogoutImpl() { ClearGlobalClient(); this.IsLogined = false; this.encryptedPassword = null; InboxList.Clear(); System.Diagnostics.Debug.WriteLine("Logout invoke"); }
//Update client settings private void UpdateSettings(bool useImap) { ClearGlobalClient(); if (IsLogined) { IsLogined = false; } if (IsRequesting) { IsRequesting = false; } InboxList.Clear(); if (useImap) { settings = AppSettings.Default.ImapSettings; clientCreator = CreateImapClient; } else { settings = AppSettings.Default.Pop3Settings; clientCreator = CreatePOP3Client; } System.Diagnostics.Debug.WriteLine("UpdateSettings " + useImap); }
//Implement login function private async void LoginImpl(string psw, bool logIt) { if (string.IsNullOrEmpty(psw)) { return; } //Update client settings UpdateSettings(UseImap); this.encryptedPassword = null; IsRequesting = true; //TODO Login await Task.Run(() => { try { //Ensure client created. if (globalClient == null) { globalClient = clientCreator.Invoke(); } if (globalClient.IsConnected) { globalClient.Disconnect(); } Application.Current.Dispatcher.Invoke(() => { InboxList.Clear(); }); //Connect string userAddress = UserAddress; IsLandingIn = true; //Connect to server. globalClient.Connect(settings.Host, settings.Port, settings.EnableSsl); //Login if (!globalClient.Login(userAddress, psw, () => { IsLandingIn = false; this.encryptedPassword = psw.Protect(); this.IsLogined = true; //Notify login state Messenger.Default.Send(new NavigationMessage(), Tokens.Login); Messenger.Default.Send(new DisplayMessage("Login Successful!", DisplayType.Toast)); //Delete old inbox folder var dir = new DirectoryInfo(InboxFolder); if (dir.Exists) { dir.Delete(true); } //Do on logined AppSettings.Default.LastLogUser = userAddress; if (logIt) { //Log to local. userAssistant.StoreUserLocal(userAddress, psw); AppSettings.Default.LastLogUser = userAddress; AppSettings.Default.AutoLogin = true; } AppSettings.Default.Save(); DisplayName = userAssistant.GetUserDisplayName(userAddress); userAssistant.SaveToRecent(userAddress, DisplayName); //End }, msg => { Application.Current.Dispatcher.Invoke(() => { InboxList.Add(msg); }); })) { Messenger.Default.Send(new DisplayMessage("Login Error, authenticate fail.")); } IsLandingIn = false; } catch (Exception e) { string tips = globalClient == null ? "Create client fail." : (globalClient.IsConnected ? (globalClient.IsAuthenticated ? "Fetch messages fail." : "Login Error, authenticate fail.") : "Connect to service fail."); Messenger.Default.Send(new DisplayMessage(tips, e.Message)); } }); IsRequesting = false; IsLandingIn = false; }