From 7fc2d64cd83eefb49ef63205d8ae84071c36bcdb Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Fri, 24 Mar 2023 11:52:38 +0000 Subject: [PATCH] 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 --- globalbroadcaster.h | 1 + locale/ru_RU.ts | 4 ++-- mainwindow.cc | 2 ++ programs.cc | 14 ++++++++++---- sources.ui | 2 +- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/globalbroadcaster.h b/globalbroadcaster.h index 83f7ff8e..e1e81704 100644 --- a/globalbroadcaster.h +++ b/globalbroadcaster.h @@ -26,6 +26,7 @@ public: bool existedInWhitelist(QString host); static GlobalBroadcaster * instance(); unsigned currentGroupId; + QString translateLineText{}; signals: void dictionaryChanges( ActiveDictIds ad ); diff --git a/locale/ru_RU.ts b/locale/ru_RU.ts index 75f870f5..fcc708bd 100644 --- a/locale/ru_RU.ts +++ b/locale/ru_RU.ts @@ -4738,8 +4738,8 @@ of the appropriate groups to use them. - 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. - Любые внешние программы. Строка %GDWORD% будет заменена на запрашиваемое слово. Если такой строки нет, слово будет отправлено в стандартный входной поток. + 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. + Любые внешние программы. Строка %GDWORD% будет заменена на запрашиваемое слово. Строка %GDSEARCH% будет заменена на текст в поисковой строке. Если обоих строк нет задано, запрашиваемое слово будет отправлено в стандартный входной поток внешней программы. diff --git a/mainwindow.cc b/mainwindow.cc index 7078c849..e5c8f66b 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -2342,6 +2342,8 @@ void MainWindow::translateInputChanged( QString const & newValue ) { updateSuggestionList( newValue ); translateBoxSuffix = QString(); + // Save translate line text. Later it can be passed to external applications. + GlobalBroadcaster::instance()->translateLineText = newValue; } void MainWindow::updateSuggestionList() diff --git a/programs.cc b/programs.cc index 76e002bd..0df09d1c 100644 --- a/programs.cc +++ b/programs.cc @@ -9,6 +9,7 @@ #include "parsecmdline.hh" #include "iconv.hh" #include "utils.hh" +#include "globalbroadcaster.h" #include #include @@ -154,13 +155,18 @@ bool RunInstance::start( Config::Program const & prg, QString const & word, args.pop_front(); bool writeToStdInput = true; + auto const & search_string = GlobalBroadcaster::instance()->translateLineText; - for( int x = 0; x < args.size(); ++x ) - if( args[ x ].indexOf( "%GDWORD%" ) >= 0 ) - { + for( auto & arg : args ) { + if( arg.indexOf( "%GDWORD%" ) >= 0 ) { 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 ); if( writeToStdInput ) diff --git a/sources.ui b/sources.ui index 0596f121..f93684d8 100644 --- a/sources.ui +++ b/sources.ui @@ -409,7 +409,7 @@ of the appropriate groups to use them. - 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. + 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. true