Make MainWindow::setTranslateBoxTextAnd*() harder to misuse

Most callers of these member functions should escape wildcard symbols in
the `text` argument. Yet nothing in the functions' signatures suggested
such escaping. With the added enum WildcardPolicy argument, the callers
are forced to decide whether or not the wildcards should be escaped.
This commit is contained in:
Igor Kushnir 2021-06-30 19:49:58 +03:00
parent 9ff28e226f
commit baff02a14b
2 changed files with 22 additions and 13 deletions

View file

@ -959,7 +959,7 @@ void MainWindow::updateSearchPaneAndBar( bool searchInDock )
updateGroupList();
applyWordsZoomLevel();
setTranslateBoxTextAndKeepSuffix( text, DisablePopup );
setTranslateBoxTextAndKeepSuffix( text, WildcardsAreAlreadyEscaped, DisablePopup );
focusTranslateLine();
}
@ -979,7 +979,7 @@ void MainWindow::mousePressEvent( QMouseEvent *event)
QString str = QApplication::clipboard()->text(subtype,
QClipboard::Selection);
setTranslateBoxTextAndClearSuffix( Folding::escapeWildcardSymbols( str ), NoPopupChange );
setTranslateBoxTextAndClearSuffix( str, EscapeWildcards, NoPopupChange );
QKeyEvent ev(QEvent::KeyPress, Qt::Key_Enter,
Qt::NoModifier);
@ -2350,17 +2350,22 @@ void MainWindow::respondToTranslationRequest( Config::InputPhrase const & phrase
}
}
void MainWindow::setTranslateBoxTextAndKeepSuffix( QString const & text, TranslateBoxPopup popupAction )
void MainWindow::setTranslateBoxTextAndKeepSuffix( QString text, WildcardPolicy wildcardPolicy,
TranslateBoxPopup popupAction )
{
if( wildcardPolicy == EscapeWildcards )
text = Folding::escapeWildcardSymbols( text );
if( popupAction == NoPopupChange || cfg.preferences.searchInDock )
translateLine->setText( text );
else
translateBox->setText( text, popupAction == EnablePopup );
}
void MainWindow::setTranslateBoxTextAndClearSuffix( QString const & text, TranslateBoxPopup popupAction )
void MainWindow::setTranslateBoxTextAndClearSuffix( QString const & text, WildcardPolicy wildcardPolicy,
TranslateBoxPopup popupAction )
{
setTranslateBoxTextAndKeepSuffix( text, popupAction );
setTranslateBoxTextAndKeepSuffix( text, wildcardPolicy, popupAction );
translateBoxSuffix = QString();
}
@ -2753,7 +2758,8 @@ void MainWindow::typingEvent( QString const & t )
if( translateLine->isEnabled() )
{
translateLine->setFocus();
setTranslateBoxTextAndClearSuffix( t, EnablePopup );
// Escaping the typed-in characters is the user's responsibility.
setTranslateBoxTextAndClearSuffix( t, WildcardsAreAlreadyEscaped, EnablePopup );
translateLine->setCursorPosition( t.size() );
}
}
@ -2771,7 +2777,7 @@ void MainWindow::showHistoryItem( QString const & word )
history.enableAdd( false );
setTranslateBoxTextAndClearSuffix( Folding::escapeWildcardSymbols( word ), DisablePopup );
setTranslateBoxTextAndClearSuffix( word, EscapeWildcards, DisablePopup );
showTranslationFor( word );
history.enableAdd( cfg.preferences.storeHistory );
@ -3891,7 +3897,7 @@ ArticleView * MainWindow::getCurrentArticleView()
void MainWindow::phraseReceived( Config::InputPhrase const & phrase )
{
toggleMainWindow( true );
setTranslateBoxTextAndKeepSuffix( Folding::escapeWildcardSymbols( phrase.phrase ), NoPopupChange );
setTranslateBoxTextAndKeepSuffix( phrase.phrase, EscapeWildcards, NoPopupChange );
translateBoxSuffix = phrase.punctuationSuffix;
respondToTranslationRequest( phrase, false );
}
@ -3904,7 +3910,7 @@ void MainWindow::wordReceived( const QString & word)
void MainWindow::headwordReceived( const QString & word, const QString & ID )
{
toggleMainWindow( true );
setTranslateBoxTextAndClearSuffix( Folding::escapeWildcardSymbols( word ), NoPopupChange );
setTranslateBoxTextAndClearSuffix( word, EscapeWildcards, NoPopupChange );
respondToTranslationRequest( Config::InputPhrase::fromPhrase( word ),
false, ArticleView::scrollToFromDictionaryId( ID ) );
}
@ -4544,7 +4550,7 @@ void MainWindow::foundDictsContextMenuRequested( const QPoint &pos )
void MainWindow::sendWordToInputLine( const QString & word )
{
setTranslateBoxTextAndClearSuffix( Folding::escapeWildcardSymbols( word ), NoPopupChange );
setTranslateBoxTextAndClearSuffix( word, EscapeWildcards, NoPopupChange );
}
void MainWindow::storeResourceSavePath( const QString & newPath )
@ -4847,7 +4853,7 @@ void MainWindow::headwordFromFavorites( QString const & headword,
}
// Show headword without lost of focus on Favorites tree
setTranslateBoxTextAndClearSuffix( Folding::escapeWildcardSymbols( headword ), DisablePopup );
setTranslateBoxTextAndClearSuffix( headword, EscapeWildcards, DisablePopup );
showTranslationFor(headword );
}

View file

@ -265,9 +265,12 @@ private:
void updateSuggestionList();
void updateSuggestionList( QString const & text );
enum WildcardPolicy { EscapeWildcards, WildcardsAreAlreadyEscaped };
enum TranslateBoxPopup { NoPopupChange, EnablePopup, DisablePopup };
void setTranslateBoxTextAndKeepSuffix( QString const & text, TranslateBoxPopup popupAction );
void setTranslateBoxTextAndClearSuffix( QString const & text, TranslateBoxPopup popupAction );
void setTranslateBoxTextAndKeepSuffix( QString text, WildcardPolicy wildcardPolicy,
TranslateBoxPopup popupAction );
void setTranslateBoxTextAndClearSuffix( QString const & text, WildcardPolicy wildcardPolicy,
TranslateBoxPopup popupAction );
private slots: