Skip to content

Commit

Permalink
Merge branch 'staged' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed May 21, 2022
2 parents 2f7f298 + 2b9b278 commit 413ce8c
Show file tree
Hide file tree
Showing 21 changed files with 404 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/AutoTag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest

env:
version: 22.4.24
version: 22.5.22
version-suffix: alpha
prerelease: true

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos-6.2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
qt_arch: [clang_64]
env:
targetName: GoldenDict
version: 22.4.24
version: 22.5.22
version-suffix: alpha
prerelease: true

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
qt_arch: [clang_64]
env:
targetName: GoldenDict
version: 22.4.24
version: 22.5.22
version-suffix: alpha
prerelease: true

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu-6.2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
qt_ver: [6.2.4,6.3.0]
qt_arch: [gcc_64]
env:
version: 22.4.24
version: 22.5.22
version-suffix: alpha
prerelease: true
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu-PR-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
qt_ver: [5.15.2,6.2.4]
qt_arch: [gcc_64]
env:
version: 22.4.24
version: 22.5.22
version-suffix: alpha
prerelease: true
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
qt_ver: [5.15.2]
qt_arch: [gcc_64]
env:
version: 22.4.24
version: 22.5.22
version-suffix: alpha
prerelease: true
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows-6.2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
qt_arch: [win64_msvc2019_64]
env:
targetName: GoldenDict.exe
version: 22.4.24
version: 22.5.22
version-suffix: alpha
prerelease: true
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows-PR-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

env:
targetName: GoldenDict.exe
version: 22.4.24
version: 22.5.22
version-suffix: alpha
prerelease: true
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
qt_arch: [win64_msvc2019_64]
env:
targetName: GoldenDict.exe
version: 22.4.24
version: 22.5.22
version-suffix: alpha
prerelease: true
# 步骤
Expand Down
85 changes: 85 additions & 0 deletions ankiconnector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "ankiconnector.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include "utils.hh"
AnkiConnector::AnkiConnector( QObject * parent, Config::Class const & _cfg ) : QObject{ parent }, cfg( _cfg )
{
mgr = new QNetworkAccessManager( this );
connect( mgr, &QNetworkAccessManager::finished, this, &AnkiConnector::finishedSlot );
}

void AnkiConnector::sendToAnki( QString const & word, QString const & text )
{
//for simplicity. maybe use QJsonDocument in future?
QString postTemplate = QString( "{"
"\"action\": \"addNote\","
"\"version\": 6,"
"\"params\": {"
" \"note\": {"
" \"deckName\": \"%1\","
" \"modelName\": \"%2\","
" \"fields\":%3,"
" \"options\": {"
" \"allowDuplicate\": true"
" },"
" \"tags\": []"
"}"
"}"
"}"
"" );

QJsonObject fields;
fields.insert( "Front", word );
fields.insert( "Back", text );

QString postData = postTemplate.arg( cfg.preferences.ankiConnectServer.deck,
cfg.preferences.ankiConnectServer.model,
Utils::json2String( fields ) );

// qDebug().noquote() << postData;
QUrl url;
url.setScheme( "http" );
url.setHost( cfg.preferences.ankiConnectServer.host );
url.setPort( cfg.preferences.ankiConnectServer.port );
QNetworkRequest request( url );
request.setTransferTimeout( 3000 );
// request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy );
request.setHeader( QNetworkRequest::ContentTypeHeader, "applicaion/json" );
auto reply = mgr->post( request, postData.toUtf8() );
connect( reply,
&QNetworkReply::errorOccurred,
this,
[ this ]( QNetworkReply::NetworkError e )
{
qWarning() << e;
emit this->errorText( tr( "anki: post to anki failed" ) );
} );
}

void AnkiConnector::finishedSlot( QNetworkReply * reply )
{
if( reply->error() == QNetworkReply::NoError )
{
QByteArray bytes = reply->readAll();
QJsonDocument json = QJsonDocument::fromJson( bytes );
auto obj = json.object();
if( obj.size() != 2 || !obj.contains( "error" ) || !obj.contains( "result" ) ||
obj[ "result" ].toString().isEmpty() )
{
emit errorText( QObject::tr( "anki: post to anki failed" ) );
}
QString result = obj[ "result" ].toString();

qDebug() << "anki result:" << result;

emit errorText( tr( "anki: post to anki success" ) );
}
else
{
qDebug() << "anki connect error" << reply->errorString();
emit errorText( "anki:" + reply->errorString() );
}

reply->deleteLater();
}
28 changes: 28 additions & 0 deletions ankiconnector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef ANKICONNECTOR_H
#define ANKICONNECTOR_H

#include "config.hh"

#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QObject>

class AnkiConnector : public QObject
{
Q_OBJECT
public:
explicit AnkiConnector( QObject * parent, Config::Class const & cfg );

void sendToAnki( QString const & word, QString const & text );

private:
QNetworkAccessManager * mgr;
Config::Class const & cfg;
public :
signals:
void errorText( QString const & );
private slots:
void finishedSlot(QNetworkReply * reply);
};

#endif // ANKICONNECTOR_H
22 changes: 22 additions & 0 deletions articleview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm, Au
channel = new QWebChannel(ui.definition->page());
agent = new ArticleViewAgent(this);
attachWebChannelToHtml();
ankiConnector = new AnkiConnector( this, cfg );
connect( ankiConnector,
&AnkiConnector::errorText,
this,
[ this ]( QString const & errorText ) { emit statusBarMessage( errorText ); } );
}

// explicitly report the minimum size, to avoid
Expand Down Expand Up @@ -489,6 +494,10 @@ void ArticleView::showDefinition( QString const & word, QStringList const & dict
ui.definition->setCursor( Qt::WaitCursor );
}

void ArticleView::sendToAnki(QString const & word, QString const & text ){
ankiConnector->sendToAnki(word,text);
}

void ArticleView::showAnticipation()
{
ui.definition->setHtml( "" );
Expand Down Expand Up @@ -1721,6 +1730,7 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
QAction * followLinkExternal = 0;
QAction * followLinkNewTab = 0;
QAction * lookupSelection = 0;
QAction * sendToAnkiAction = 0 ;
QAction * lookupSelectionGr = 0;
QAction * lookupSelectionNewTab = 0;
QAction * lookupSelectionNewTabGr = 0;
Expand Down Expand Up @@ -1850,6 +1860,14 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
}
}

// add anki menu
if( !text.isEmpty() && cfg.preferences.ankiConnectServer.enabled )
{
QString txt = ui.definition->title();
sendToAnkiAction = new QAction( tr( "&Send \"%1\" to anki with selected text." ).arg( txt ), &menu );
menu.addAction( sendToAnkiAction );
}

if( text.isEmpty() && !cfg.preferences.storeHistory)
{
QString txt = ui.definition->title();
Expand Down Expand Up @@ -1942,6 +1960,10 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
else
if ( result == lookupSelection )
showDefinition( selectedText, getGroup( ui.definition->url() ), getCurrentArticle() );
else if( result = sendToAnkiAction )
{
sendToAnki( ui.definition->title(), ui.definition->selectedText() );
}
else
if ( result == lookupSelectionGr && groupComboBox )
showDefinition( selectedText, groupComboBox->getCurrentGroup(), QString() );
Expand Down
4 changes: 4 additions & 0 deletions articleview.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#include <QtCore5Compat/QRegExp>
#endif
#include "ankiconnector.h"

class ResourceToSaveHandler;
class ArticleViewAgent ;
Expand All @@ -39,6 +40,8 @@ class ArticleView: public QFrame
ArticleViewAgent * agent;
Ui::ArticleView ui;

AnkiConnector * ankiConnector;

QAction pasteAction, articleUpAction, articleDownAction,
goBackAction, goForwardAction, selectCurrentArticleAction,
copyAsTextAction, inspectAction;
Expand Down Expand Up @@ -129,6 +132,7 @@ public:
QRegExp const & searchRegExp, unsigned group,
bool ignoreDiacritics );

void sendToAnki(QString const & word, QString const & text );
/// Clears the view and sets the application-global waiting cursor,
/// which will be restored when some article loads eventually.
void showAnticipation();
Expand Down
41 changes: 41 additions & 0 deletions config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ ProxyServer::ProxyServer(): enabled( false ), useSystemProxy( false ), type( Soc
{
}

AnkiConnectServer::AnkiConnectServer(): enabled( false ), host("127.0.0.1"), port( 8765 )
{
}

HotKey::HotKey(): modifiers( 0 ), key1( 0 ), key2( 0 )
{
}
Expand Down Expand Up @@ -937,6 +941,17 @@ Class load()
c.preferences.proxyServer.systemProxyPassword = proxy.namedItem( "systemProxyPassword" ).toElement().text();
}

QDomNode ankiConnectServer = preferences.namedItem( "ankiConnectServer" );

if ( !ankiConnectServer.isNull() )
{
c.preferences.ankiConnectServer.enabled = ( ankiConnectServer.toElement().attribute( "enabled" ) == "1" );
c.preferences.ankiConnectServer.host = ankiConnectServer.namedItem( "host" ).toElement().text();
c.preferences.ankiConnectServer.port = ankiConnectServer.namedItem( "port" ).toElement().text().toULong();
c.preferences.ankiConnectServer.deck = ankiConnectServer.namedItem( "deck" ).toElement().text();
c.preferences.ankiConnectServer.model = ankiConnectServer.namedItem( "model" ).toElement().text();
}

if ( !preferences.namedItem( "checkForNewReleases" ).isNull() )
c.preferences.checkForNewReleases = ( preferences.namedItem( "checkForNewReleases" ).toElement().text() == "1" );

Expand Down Expand Up @@ -1872,6 +1887,32 @@ void save( Class const & c )
proxy.appendChild( opt );
}

//anki connect
{
QDomElement proxy = dd.createElement( "ankiConnectServer" );
preferences.appendChild( proxy );

QDomAttr enabled = dd.createAttribute( "enabled" );
enabled.setValue( c.preferences.ankiConnectServer.enabled ? "1" : "0" );
proxy.setAttributeNode( enabled );

opt = dd.createElement( "host" );
opt.appendChild( dd.createTextNode( c.preferences.ankiConnectServer.host ) );
proxy.appendChild( opt );

opt = dd.createElement( "port" );
opt.appendChild( dd.createTextNode( QString::number( c.preferences.ankiConnectServer.port ) ) );
proxy.appendChild( opt );

opt = dd.createElement( "deck" );
opt.appendChild( dd.createTextNode( c.preferences.ankiConnectServer.deck ) );
proxy.appendChild( opt );

opt = dd.createElement( "model" );
opt.appendChild( dd.createTextNode( c.preferences.ankiConnectServer.model ) );
proxy.appendChild( opt );
}

opt = dd.createElement( "checkForNewReleases" );
opt.appendChild( dd.createTextNode( c.preferences.checkForNewReleases ? "1" : "0" ) );
preferences.appendChild( opt );
Expand Down
13 changes: 13 additions & 0 deletions config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ struct ProxyServer
ProxyServer();
};

struct AnkiConnectServer
{
bool enabled;

QString host;
unsigned port;
QString deck;
QString model;

AnkiConnectServer();
};

// A hotkey -- currently qt modifiers plus one or two keys
struct HotKey
{
Expand Down Expand Up @@ -329,6 +341,7 @@ struct Preferences
QString audioPlaybackProgram;

ProxyServer proxyServer;
AnkiConnectServer ankiConnectServer;

bool checkForNewReleases;
bool disallowContentFromOtherSites;
Expand Down
6 changes: 4 additions & 2 deletions goldendict.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TEMPLATE = app
TARGET = goldendict
VERSION = 22.4.24-alpha
VERSION = 22.5.22-alpha

# Generate version file. We do this here and in a build rule described later.
# The build rule is required since qmake isn't run each time the project is
Expand Down Expand Up @@ -70,7 +70,7 @@ win32 {
TARGET = GoldenDict

win32-msvc* {
VERSION = 22.4.24 # VS does not recognize 22.number.alpha,cause errors during compilation under MSVC++
VERSION = 22.5.22 # VS does not recognize 22.number.alpha,cause errors during compilation under MSVC++
DEFINES += __WIN32 _CRT_SECURE_NO_WARNINGS
contains(QMAKE_TARGET.arch, x86_64) {
DEFINES += NOMINMAX __WIN64
Expand Down Expand Up @@ -223,6 +223,7 @@ DEFINES += PROGRAM_VERSION=\\\"$$VERSION\\\"

# Input
HEADERS += folding.hh \
ankiconnector.h \
article_inspect.h \
articlewebpage.h \
globalbroadcaster.h \
Expand Down Expand Up @@ -364,6 +365,7 @@ FORMS += groups.ui \
fulltextsearch.ui

SOURCES += folding.cc \
ankiconnector.cpp \
article_inspect.cpp \
articlewebpage.cpp \
globalbroadcaster.cpp \
Expand Down
Loading

0 comments on commit 413ce8c

Please sign in to comment.