Allow adding "ankisearch:" links in external programs. (#412)

* Add ankisearch link

* auto indent

* use raw string

* refuse to run if ankiconnect is not enabled

---------

Co-authored-by: GenjiFujimoto <dratschuk.d@gmail.com>
This commit is contained in:
Ren Tatsumoto 2023-03-22 02:04:54 +00:00 committed by GitHub
parent da57d82b47
commit 01c661b575
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 3 deletions

View file

@ -50,6 +50,28 @@ void AnkiConnector::sendToAnki( QString const & word, QString const & text, QStr
Utils::json2String( fields ) ); Utils::json2String( fields ) );
// qDebug().noquote() << postData; // qDebug().noquote() << postData;
postToAnki( postData );
}
void AnkiConnector::ankiSearch( QString const & word )
{
if( !cfg.preferences.ankiConnectServer.enabled ) {
emit this->errorText( tr( "Anki search: AnkiConnect is not enabled." ) );
return;
}
QString postTemplate = R"anki({
"action": "guiBrowse",
"version": 6,
"params": {
"query": "%1"
}
})anki";
postToAnki( postTemplate.arg( word ) );
}
void AnkiConnector::postToAnki( QString const & postData )
{
QUrl url; QUrl url;
url.setScheme( "http" ); url.setScheme( "http" );
url.setHost( cfg.preferences.ankiConnectServer.host ); url.setHost( cfg.preferences.ankiConnectServer.host );

View file

@ -14,10 +14,12 @@ public:
explicit AnkiConnector( QObject * parent, Config::Class const & cfg ); explicit AnkiConnector( QObject * parent, Config::Class const & cfg );
void sendToAnki( QString const & word, QString const & text, QString const & sentence ); void sendToAnki( QString const & word, QString const & text, QString const & sentence );
void ankiSearch( QString const & word);
private: private:
QNetworkAccessManager * mgr; QNetworkAccessManager * mgr;
Config::Class const & cfg; Config::Class const & cfg;
void postToAnki( QString const & postData );
public : public :
signals: signals:
void errorText( QString const & ); void errorText( QString const & );

View file

@ -1142,9 +1142,10 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, QString const &
if( url.scheme().compare( "gdpicture" ) == 0 ) if( url.scheme().compare( "gdpicture" ) == 0 )
load( url ); load( url );
else else if( url.scheme().compare( "ankisearch" ) == 0 ) {
if ( url.scheme().compare( "bword" ) == 0 || url.scheme().compare( "entry" ) == 0 ) ankiConnector->ankiSearch( url.path() );
{ }
else if( url.scheme().compare( "bword" ) == 0 || url.scheme().compare( "entry" ) == 0 ) {
if( Utils::Url::hasQueryItem( ref, "dictionaries" ) ) if( Utils::Url::hasQueryItem( ref, "dictionaries" ) )
{ {
QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" ) QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" )