give external programs access to translateLine via %GDSEARCH% (#417)

* give external programs access to translateLine via %GDSEARCH%

* remove code smell

* reformat with clang-format

* update translations

* don't write to stdin of an external program if GDSEARCH is set

* update ui docs
This commit is contained in:
Ren Tatsumoto 2023-03-24 11:52:38 +00:00 committed by GitHub
parent 75593e4487
commit 7fc2d64cd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 7 deletions

View file

@ -26,6 +26,7 @@ public:
bool existedInWhitelist(QString host); bool existedInWhitelist(QString host);
static GlobalBroadcaster * instance(); static GlobalBroadcaster * instance();
unsigned currentGroupId; unsigned currentGroupId;
QString translateLineText{};
signals: signals:
void dictionaryChanges( ActiveDictIds ad ); void dictionaryChanges( ActiveDictIds ad );

View file

@ -4738,8 +4738,8 @@ of the appropriate groups to use them.</source>
</message> </message>
<message> <message>
<location filename="../sources.ui" line="409"/> <location filename="../sources.ui" line="409"/>
<source>Any external programs. A string %GDWORD% will be replaced with the query word. If such string is not presented, the word will be fed into standard input.</source> <source>Any external programs. A string %GDWORD% will be replaced with the query word. A string %GDSEARCH% will be replaced with the text in the search bar. If both of the parameters are not provided, the headword will be fed into standard input.</source>
<translation>Любые внешние программы. Строка %GDWORD% будет заменена на запрашиваемое слово. Если такой строки нет, слово будет отправлено в стандартный входной поток.</translation> <translation>Любые внешние программы. Строка %GDWORD% будет заменена на запрашиваемое слово. Строка %GDSEARCH% будет заменена на текст в поисковой строке. Если обоих строк нет задано, запрашиваемое слово будет отправлено в стандартный входной поток внешней программы.</translation>
</message> </message>
<message> <message>
<location filename="../sources.ui" line="462"/> <location filename="../sources.ui" line="462"/>

View file

@ -2342,6 +2342,8 @@ void MainWindow::translateInputChanged( QString const & newValue )
{ {
updateSuggestionList( newValue ); updateSuggestionList( newValue );
translateBoxSuffix = QString(); translateBoxSuffix = QString();
// Save translate line text. Later it can be passed to external applications.
GlobalBroadcaster::instance()->translateLineText = newValue;
} }
void MainWindow::updateSuggestionList() void MainWindow::updateSuggestionList()

View file

@ -9,6 +9,7 @@
#include "parsecmdline.hh" #include "parsecmdline.hh"
#include "iconv.hh" #include "iconv.hh"
#include "utils.hh" #include "utils.hh"
#include "globalbroadcaster.h"
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
@ -154,13 +155,18 @@ bool RunInstance::start( Config::Program const & prg, QString const & word,
args.pop_front(); args.pop_front();
bool writeToStdInput = true; bool writeToStdInput = true;
auto const & search_string = GlobalBroadcaster::instance()->translateLineText;
for( int x = 0; x < args.size(); ++x ) for( auto & arg : args ) {
if( args[ x ].indexOf( "%GDWORD%" ) >= 0 ) if( arg.indexOf( "%GDWORD%" ) >= 0 ) {
{
writeToStdInput = false; writeToStdInput = false;
args[ x ].replace( "%GDWORD%", word ); arg.replace( "%GDWORD%", word );
} }
if( arg.indexOf( "%GDSEARCH%" ) >= 0 ) {
writeToStdInput = false;
arg.replace( "%GDSEARCH%", search_string );
}
}
process.start( programName, args ); process.start( programName, args );
if( writeToStdInput ) if( writeToStdInput )

View file

@ -409,7 +409,7 @@ of the appropriate groups to use them.</string>
<item> <item>
<widget class="QLabel" name="label_16"> <widget class="QLabel" name="label_16">
<property name="text"> <property name="text">
<string>Any external programs. A string %GDWORD% will be replaced with the query word. If such string is not presented, the word will be fed into standard input.</string> <string>Any external programs. A string %GDWORD% will be replaced with the query word. A string %GDSEARCH% will be replaced with the text in the search bar. If both of the parameters are not provided, the headword will be fed into standard input.</string>
</property> </property>
<property name="wordWrap"> <property name="wordWrap">
<bool>true</bool> <bool>true</bool>