/// <summary> /// Gets called when the <c>PasswordStrengthMeter</c> has calculated a new /// password strength rating. /// </summary> /// <param name="sender">The <c>PasswordStrengthMeter</c> object posting the event.</param> /// <param name="e">The parameter containing the updated password rating.</param> private void PasswordStrengthScoreUpdated(object sender, ScoreUpdatedEventArgs e) { Dispatcher.UIThread.Post(() => { string ratingText = _loc.GetLocalizationValue("PasswordStrength") + ": "; switch (e.Score.Rating) { case PasswordRating.POOR: ratingText += _loc.GetLocalizationValue("PasswordStrengthRatingPoor"); _pnlPasswordRating.Background = BRUSH_POOR; break; case PasswordRating.MEDIUM: ratingText += _loc.GetLocalizationValue("PasswordStrengthRatingMedium"); _pnlPasswordRating.Background = BRUSH_MEDIUM; break; case PasswordRating.GOOD: ratingText += _loc.GetLocalizationValue("PasswordStrengthRatingGood"); _pnlPasswordRating.Background = BRUSH_GOOD; break; } _shapeUppercaseIndicator.Fill = e.Score.UppercaseUsed ? BRUSH_GOOD : BRUSH_POOR; _shapeLowercaseIndicator.Fill = e.Score.LowercaseUsed ? BRUSH_GOOD : BRUSH_POOR; _shapeDigigsIndicator.Fill = e.Score.DigitsUsed ? BRUSH_GOOD : BRUSH_POOR; _shapeSymbolsIndicator.Fill = e.Score.SymbolsUsed ? BRUSH_GOOD : BRUSH_POOR; _lblPasswordStrength.Text = ratingText; _progressPwStrength.Value = (double)e.Score.StrengthPoints; }); }
/// <summary> /// Event handler for the "DataContextChanged" event. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">The event arguments.</param> private void InputSecretDialogView_DataContextChanged(object sender, System.EventArgs e) { if (this.DataContext != null) { var ipdm = (InputSecretDialogViewModel)this.DataContext; switch (ipdm.SecretType) { case SecretType.Password: _lblMessage.Text = _loc.GetLocalizationValue("EnterPasswordMessage"); _txtSecret.PasswordChar = '*'; break; case SecretType.RescueCode: _lblMessage.Text = _loc.GetLocalizationValue("EnterRescueCodeMessage"); break; default: break; } } }
#pragma warning disable 1998 /// <summary> /// Downloads the zip archive of the latest release to a local file and /// returns the full path to that file. /// </summary> /// <param name="release">The release to download the installation archive for.</param> /// <param name="progress">An optional progress object to receive download progress notifications.</param> /// <returns>Returns the full file path of the downloaded file.</returns> public async static Task <string> DownloadRelease(GithubRelease release, IProgress <KeyValuePair <int, string> > progress = null) { return(await Task.Run(async() => { AutoResetEvent downloadComplete = new AutoResetEvent(false); bool success = false; var wc = new WebClient { CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore) }; AddHeaders(wc); if (progress != null) { wc.DownloadProgressChanged += ((s, e) => { decimal doneMB = Math.Round(e.BytesReceived / 1024M / 1024M, 2); decimal totalMB = Math.Round(e.TotalBytesToReceive / 1024M / 1024M, 2); string msg = _loc.GetLocalizationValue("InstallStatusDownloading") + $" {doneMB}/{totalMB} MB"; progress.Report(new KeyValuePair <int, string>(e.ProgressPercentage, msg)); }); } wc.DownloadFileCompleted += ((s, e) => { success = (!e.Cancelled && e.Error == null); downloadComplete.Set(); }); var downloadLink = CommonUtils.GetDownloadLinkByPlatform(release); var fileName = Path.GetFileName(downloadLink); var tempFilePath = Path.GetTempFileName(); try { wc.DownloadFileAsync(new Uri(downloadLink), tempFilePath); downloadComplete.WaitOne(); return success ? tempFilePath : null; } catch (Exception ex) { Log.Error($"Error downloading \"{downloadLink}\":\r\n{ex}"); return null; } })); }
/// <summary> /// Creates a new <c>InputSecretDialogView</c> instance and /// specifies the type of secret that the user will be asked to input. /// </summary> /// <param name="secretType">Tht type of secret that the user should input (password, rescue code...).</param> public InputSecretDialogView(SecretType secretType) { this.InitializeComponent(); //Prevent closing the dialog externally. this.Closing += InputSecretDialogView_Closing; _txtSecret = this.FindControl <CopyPasteTextBox> ("txtSecret"); _btnOK = this.FindControl <Button>("btnOK"); _lblMessage = this.FindControl <TextBlock>("lblMessage"); _txtSecret.Text = ""; _btnOK.Click += (object sender, RoutedEventArgs e) => { AllGood = true; this.Close(_txtSecret.Text); }; this._secretType = secretType; switch (secretType) { case SecretType.Password: _lblMessage.Text = _loc.GetLocalizationValue("EnterPasswordMessage"); _txtSecret.PasswordChar = '*'; break; case SecretType.RescueCode: _lblMessage.Text = _loc.GetLocalizationValue("EnterRescueCodeMessage"); break; default: break; } #if DEBUG this.AttachDevTools(); #endif }
public MainWindow() { InitializeComponent(); if (AvaloniaLocator.Current.GetService <MainWindow>() == null) { AvaloniaLocator.CurrentMutable.Bind <MainWindow>().ToConstant(this); } this.LocalizationService = new LocalizationExtension(); #if DEBUG this.AttachDevTools(); #endif // Set up and configure the notification icon // Get the type of the platform-specific implementation Type type = Implementation.ForType <INotifyIcon>(); if (type != null) { // If we have one, create an instance for it NotifyIcon = (INotifyIcon)Activator.CreateInstance(type); } if (NotifyIcon != null) { NotifyIcon.ToolTipText = "SQRL .NET Client"; NotifyIcon.IconPath = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "resm:SQRLDotNetClientUI.Assets.SQRL_icon_normal_16.png" : RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"resm:SQRLDotNetClientUI.Assets.sqrl_icon_normal_256.ico": @"resm:SQRLDotNetClientUI.Assets.sqrl_icon_normal_256_32_icon.ico"; NotifyIcon.DoubleClick += (s, e) => { RestoreWindow(); }; _NotifyIconContextMenu = new ContextMenu(); List <object> menuItems = new List <object>(); menuItems.AddRange(new[] { new MenuItem() { Header = LocalizationService.GetLocalizationValue("NotifyIconContextMenuItemHeaderRestore"), Command = ReactiveCommand.Create(RestoreWindow) }, new MenuItem() { Header = LocalizationService.GetLocalizationValue("NotifyIconContextMenuItemHeaderExit"), Command = ReactiveCommand.Create(Exit) } }); _NotifyIconContextMenu.Items = menuItems; NotifyIcon.ContextMenu = _NotifyIconContextMenu; NotifyIcon.Visible = true; } // Prevent the main window from closing. Just hide it instead // if we have a notify icon, or minimize it otherwise. this.Closing += (s, e) => { if (_reallyClose) { return; } if (NotifyIcon != null) { Log.Information("Hiding main window"); Dispatcher.UIThread.Post(() => { ((Window)s).Hide(); }); NotifyIcon.Visible = true; } else { Log.Information("Minimizing main window"); Dispatcher.UIThread.Post(() => { ((Window)s).WindowState = WindowState.Minimized; }); } e.Cancel = true; }; this.Closed += (s, e) => { // Remove the notify icon when the main window closes if (NotifyIcon != null) { NotifyIcon?.Remove(); } }; }
/// <summary> /// Creates a PDF document prominently displaying the given rescue code and /// providing some guidance for the user. /// </summary> /// <param name="fileName">The full file name (including the path) for the document.</param> /// <param name="rescueCode">The rescue code to be printed to the document.</param> /// <param name="identityName">The name of the identity. This is optional, just /// pass <c>null</c> if no identity name should be printed to the pdf file.</param> public static void CreateRescueCodeDocument(string fileName, string rescueCode, string identityName = null) { if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(rescueCode)) { throw new ArgumentException(string.Format("{0} and {1} must be specified!", nameof(fileName), nameof(rescueCode))); } identityName = identityName != null ? identityName : ""; float yPos = MARGIN_TOP + HEADER_ICON_SIZE + 80; string title = _loc.GetLocalizationValue("RescueCodeDocumentTitle"); string warning = _loc.GetLocalizationValue("RescueCodeDocumentDiscardWarning"); string text = _loc.GetLocalizationValue("RescueCodeDocumentText"); string identityLabel = _loc.GetLocalizationValue("IdentityNameLabel"); string rescueCodeLabel = _loc.GetLocalizationValue("RescueCodeLabel"); var metadata = new SKDocumentPdfMetadata { Author = _assemblyName, Creation = DateTime.Now, Creator = _assemblyName, Keywords = "SQRL,Rescue code", Modified = DateTime.Now, Producer = "SkiaSharp", Subject = "SQRL Rescue Code Document", Title = "SQRL Rescue Code Document" }; using (var stream = new SKFileWStream(fileName)) using (var document = SKDocument.CreatePdf(stream, metadata)) using (var canvas = document.BeginPage(PAGE_WIDTH, PAGE_HEIGHT)) { DrawHeader(canvas); yPos += 10 + DrawTextBlock(canvas, title, yPos, _fontBold, 25, SKColors.Black); yPos += 20 + DrawTextBlock(canvas, warning, yPos, _fontBold, 20, SKColors.Red); yPos += 30 + DrawTextBlock(canvas, text, yPos, _fontRegular, 12, SKColors.DarkGray, SKTextAlign.Left, 1.3f); if (!string.IsNullOrEmpty(identityName)) { yPos += 05 + DrawTextBlock(canvas, identityLabel, yPos, _fontRegular, 12, SKColors.DarkGray); yPos += 15 + DrawTextBlock(canvas, identityName, yPos, _fontBold, 16, SKColors.Black); } yPos += 10 + DrawTextBlock(canvas, rescueCodeLabel, yPos, _fontRegular, 12, SKColors.DarkGray); yPos += 15 + DrawTextBlock(canvas, rescueCode, yPos, _fontBold, 24, SKColors.Black); DrawFooter(canvas); document.EndPage(); document.Close(); } }
/// <summary> /// Creates a PDF document prominently displaying the given <paramref name="rescueCode"/> /// and <paramref name="identityName"/>, providing additional guidance for the user. /// </summary> /// <param name="fileName">The full file name (including the path) for the document to be created.</param> /// <param name="rescueCode">The rescue code to be printed to the document.</param> /// <param name="identityName">The name of the identity. This is optional, just /// pass <c>null</c> if no identity name should be printed to the pdf file.</param> public static void CreateRescueCodeDocument(string fileName, string rescueCode, string identityName = null) { if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(rescueCode)) { throw new ArgumentException(string.Format("{0} and {1} must be specified!", nameof(fileName), nameof(rescueCode))); } identityName = identityName != null ? identityName : ""; _pageNr = 0; string title = _loc.GetLocalizationValue("RescueCodeDocumentTitle"); string warning = _loc.GetLocalizationValue("RescueCodeDocumentDiscardWarning"); string text = _loc.GetLocalizationValue("RescueCodeDocumentText"); string identityLabel = _loc.GetLocalizationValue("IdentityNameLabel"); string rescueCodeLabel = _loc.GetLocalizationValue("RescueCodeLabel"); var metadata = new SKDocumentPdfMetadata { Author = _assemblyName, Creation = DateTime.Now, Creator = _assemblyName, Keywords = "SQRL,Rescue code", Modified = DateTime.Now, Producer = "SkiaSharp", Subject = "SQRL Rescue Code Document", Title = "SQRL Rescue Code Document" }; using (var stream = new SKFileWStream(fileName)) using (_document = SKDocument.CreatePdf(stream, metadata)) { StartNextPage(); DrawHeader(); _yPos = MARGIN_TOP + HEADER_ICON_SIZE + 80; DrawTextBlock(title, _fontBold, 25, SKColors.Black, 15f); DrawTextBlock(warning, _fontBold, 20, SKColors.Red, 20f); DrawTextBlock(text, _fontRegular, 12, SKColors.DarkGray, 30f, SKTextAlign.Left, 1.3f); if (!string.IsNullOrEmpty(identityName)) { DrawTextBlock(identityLabel, _fontRegular, 12, SKColors.DarkGray, 15f); DrawTextBlock(identityName, _fontBold, 16, SKColors.Black); } DrawTextBlock(rescueCodeLabel, _fontRegular, 12, SKColors.DarkGray, 25f); DrawTextBlock(rescueCode, _fontBold, 24, SKColors.Black); EndPage(); _document.Close(); } }