@@ -95,6 +95,9 @@ private async Task<bool> ProcessSelectedDocument(string file)
9595 {
9696 try
9797 {
98+ // lock the interface
99+ LockInterface ( ) ;
100+
98101 // notify the user
99102 LblState . Text = "checking the selected document .." ;
100103
@@ -114,7 +117,7 @@ private async Task<bool> ProcessSelectedDocument(string file)
114117 LblState . Text = string . Empty ;
115118 return false ;
116119 }
117-
120+
118121 // check its size
119122 var ( tooLarge , sizeMB ) = await Task . Run ( ( ) => DocumentManager . CheckDocumentSize ( file ) ) ;
120123 if ( tooLarge )
@@ -145,14 +148,27 @@ private async Task<bool> ProcessSelectedDocument(string file)
145148 break ;
146149 }
147150
148- // ask the user if they're sure
149- using var confirmDoc = new ConfirmDocument ( characterCount , AccountManager . CalculateCost ( characterCount ) , Path . GetFileName ( file ) , docType == DocumentType . Text ) ;
150- var res = confirmDoc . ShowDialog ( ) ;
151-
152- if ( res != DialogResult . OK )
151+ // do we have enough chars left?
152+ if ( await SubscriptionManager . CharactersWillExceedLimit ( characterCount ) )
153153 {
154- LblState . Text = string . Empty ;
155- return false ;
154+ using var limit = new LimitExceeded ( characterCount ) ;
155+ var ignoreLimit = limit . ShowDialog ( ) ;
156+ if ( ignoreLimit != DialogResult . OK )
157+ {
158+ LblState . Text = string . Empty ;
159+ return false ;
160+ }
161+ }
162+ else
163+ {
164+ // yep, ask the user if they're sure
165+ using var confirmDoc = new ConfirmDocument ( characterCount , SubscriptionManager . CalculateCost ( characterCount ) , Path . GetFileName ( file ) , docType == DocumentType . Text ) ;
166+ var confirmed = confirmDoc . ShowDialog ( ) ;
167+ if ( confirmed != DialogResult . OK )
168+ {
169+ LblState . Text = string . Empty ;
170+ return false ;
171+ }
156172 }
157173
158174 // done
@@ -167,6 +183,10 @@ private async Task<bool> ProcessSelectedDocument(string file)
167183 LblState . Text = string . Empty ;
168184 return false ;
169185 }
186+ finally
187+ {
188+ LockInterface ( false ) ;
189+ }
170190 }
171191
172192 /// <summary>
@@ -178,7 +198,7 @@ private async void ExecuteTranslation()
178198 {
179199 // lock the interface
180200 LockInterface ( ) ;
181-
201+
182202 // check the source file
183203 var file = TbSourceDocument . Text ;
184204 if ( string . IsNullOrEmpty ( file ) )
@@ -277,11 +297,12 @@ private async void ExecuteTranslation()
277297 }
278298
279299 // prepare the translation state
280- DocumentStatus state ;
300+ DocumentStatus state = null ;
281301
282302 // wait for DeepL to finish translating
283303 var dotCount = 2 ;
284- while ( true )
304+ var awaitUpload = true ;
305+ while ( awaitUpload )
285306 {
286307 // get the current state
287308 state = await Variables . Translator . TranslateDocumentStatusAsync ( documentHandle ) ;
@@ -336,7 +357,8 @@ private async void ExecuteTranslation()
336357 break ;
337358
338359 case DocumentStatus . StatusCode . Error :
339- LblState . Text = "translation failed." ;
360+ LblState . Text = "translation failed" ;
361+ awaitUpload = false ;
340362 break ;
341363 }
342364
@@ -345,10 +367,25 @@ private async void ExecuteTranslation()
345367 }
346368
347369 // did we succeed?
348- if ( ! state . Ok )
370+ if ( ! state . Ok || state . Status == DocumentStatus . StatusCode . Error )
349371 {
350372 // nope
351- LblState . Text = $ "translation failed: { state . ErrorMessage } ";
373+ // error message?
374+ if ( string . IsNullOrWhiteSpace ( state . ErrorMessage ) )
375+ {
376+ LblState . Text = "translation failed:\r \n \r \n DeepL provided no reason :(" ;
377+ return ;
378+ }
379+
380+ // yep, first check for 'equal language'
381+ if ( state . ErrorMessage . Contains ( "are equal" ) )
382+ {
383+ LblState . Text = "translation failed:\r \n \r \n source and target language are equal" ;
384+ return ;
385+ }
386+
387+ // unknown
388+ LblState . Text = $ "translation failed:\r \n \r \n { state . ErrorMessage } ";
352389 return ;
353390 }
354391
@@ -370,7 +407,9 @@ private async void ExecuteTranslation()
370407 if ( state . BilledCharacters != null )
371408 {
372409 var billedCharacters = Convert . ToDouble ( state . BilledCharacters ) ;
373- LblState . Text = $ "translation complete!\r \n \r \n the document has been billed for { billedCharacters } characters, costing { AccountManager . CalculateCost ( billedCharacters ) } .";
410+ LblState . Text = SubscriptionManager . UsingFreeSubscription ( )
411+ ? $ "translation complete!\r \n \r \n the document has been billed for { billedCharacters } characters\r \n you're on a free subscription, so no costs"
412+ : $ "translation complete!\r \n \r \n the document has been billed for { billedCharacters } characters, costing { SubscriptionManager . CalculateCost ( billedCharacters ) } .";
374413 }
375414 else LblState . Text = "translation complete!" ;
376415
@@ -459,7 +498,7 @@ private void CbTargetLanguage_SelectedValueChanged(object sender, EventArgs e)
459498 // notify
460499 return ;
461500 }
462-
501+
463502 // check formality supported
464503 var supported = Variables . FormalitySupportedLanguages . Contains ( targetLanguage ) ;
465504 CbTargetFormality . Visible = supported ;
@@ -542,6 +581,31 @@ private void LblFormalityInfo_Click(object sender, EventArgs e)
542581 MessageBoxAdv . Show ( this , HelperFunctions . GetFormalityExplanation ( ) , Variables . MessageBoxTitle , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
543582 }
544583
584+ private void BtnClean_Click ( object sender , EventArgs e )
585+ {
586+ // clear docs
587+ TbSourceDocument . Text = string . Empty ;
588+ TbTranslatedDocument . Text = string . Empty ;
589+
590+ // clear state info
591+ LblState . Text = string . Empty ;
592+
593+ // load default formality
594+ CbTargetFormality . SelectedItem = Variables . Formalities . GetEntry ( ( int ) Variables . AppSettings . DefaultFormality ) ;
595+
596+ // optionally set last source language
597+ if ( Variables . AppSettings . StoreLastUsedSourceLanguage && ! string . IsNullOrEmpty ( Variables . AppSettings . LastSourceLanguage ) )
598+ {
599+ CbSourceLanguage . SelectedItem = Variables . SourceLanguages . GetKeyByEntry ( Variables . AppSettings . LastSourceLanguage ) ;
600+ }
601+
602+ // optionally set last target language
603+ if ( Variables . AppSettings . StoreLastUsedTargetLanguage && ! string . IsNullOrEmpty ( Variables . AppSettings . LastTargetLanguage ) )
604+ {
605+ CbTargetLanguage . SelectedItem = Variables . TargetLanguages . GetKeyByEntry ( Variables . AppSettings . LastTargetLanguage ) ;
606+ }
607+ }
608+
545609 private void BtnTranslate_Click ( object sender , EventArgs e ) => ExecuteTranslation ( ) ;
546610
547611 private void TbSourceDocument_DoubleClick ( object sender , EventArgs e ) => SelectSourceFile ( ) ;
0 commit comments