Skip to content

Commit

Permalink
fix: 调用语音助手服务不响应导致右键菜单卡死
Browse files Browse the repository at this point in the history
语音助手在某些情况下会不响应,这是直接构造 QDBusInterface
会卡死,加一个 Ping 的操作,如果 300ms 内未响应则不添加
相关菜单

Bug: https://pms.uniontech.com/bug-view-151279.html
Log:
Influence: edit context menu
Change-Id: Id8e3f6f53a011b70fa12d95ff1cc3a6fec94021b
  • Loading branch information
kegechen authored and zccrs committed Aug 5, 2022
1 parent 22baf63 commit a393c85
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/widgets/dlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,40 @@ bool DLineEdit::eventFilter(QObject *watched, QEvent *event)
return QWidget::eventFilter(watched, event);
}

QLineEdit *pLineEdit = static_cast<QLineEdit*>(watched);
QMenu *menu = pLineEdit->createStandardContextMenu();

for (QAction *action : menu->actions()) {
if (action->text().startsWith(QLineEdit::tr("&Copy")) && !copyEnabled() ) {
action->setEnabled(false);
}
if (action->text().startsWith(QLineEdit::tr("Cu&t")) && !cutEnabled()) {
action->setEnabled(false);
}
}
connect(menu, &QMenu::triggered, this, [pLineEdit](QAction *pAction) {
if (pAction->text().startsWith(QLineEdit::tr("Select All"))) {
QApplication::clipboard()->setText(pLineEdit->text(), QClipboard::Mode::Selection);
}
});

auto msg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", "/",
"org.freedesktop.DBus.Peer", "Ping");
// 用之前 Ping 一下, 300ms 内没回复就认定是服务出问题,不再添加助手菜单项
auto pingReply = QDBusConnection::sessionBus().call(msg, QDBus::BlockWithGui, 300);
auto errorType = QDBusConnection::sessionBus().lastError().type();
if (errorType == QDBusError::Timeout || errorType == QDBusError::NoReply) {
qWarning() << pingReply << "\nwill not add aiassistant actions!";
menu->popup(static_cast<QContextMenuEvent*>(event)->globalPos());
event->accept();
return true;
}

QDBusInterface testSpeech("com.iflytek.aiassistant",
"/aiassistant/tts",
"com.iflytek.aiassistant.tts",
QDBusConnection::sessionBus());
//测试朗读接口是否开启
// 测试朗读接口是否开启
QDBusReply<bool> speechReply = testSpeech.call(QDBus::AutoDetect, "getTTSEnable");

QDBusInterface testReading("com.iflytek.aiassistant",
Expand All @@ -529,24 +558,6 @@ bool DLineEdit::eventFilter(QObject *watched, QEvent *event)
//测试听写接口是否开启
QDBusReply<bool> speechToTextReply = testSpeechToText.call(QDBus::AutoDetect, "getIatEnable");

QLineEdit *pLineEdit = static_cast<QLineEdit*>(watched);
QMenu *menu = pLineEdit->createStandardContextMenu();

for (QAction *action : menu->actions()) {
if (action->text().startsWith(QLineEdit::tr("&Copy")) && !copyEnabled() ) {
action->setEnabled(false);
}
if (action->text().startsWith(QLineEdit::tr("Cu&t")) && !cutEnabled()) {
action->setEnabled(false);
}
}

connect(menu, &QMenu::triggered, this, [pLineEdit](QAction *pAction) {
if (pAction->text().startsWith(QLineEdit::tr("Select All"))) {
QApplication::clipboard()->setText(pLineEdit->text(), QClipboard::Mode::Selection);
}
});

//朗读,翻译,听写都没有开启,则弹出默认菜单
if (!speechReply.value() && !translateReply.value() && !speechToTextReply.value()) {
menu->popup(static_cast<QContextMenuEvent*>(event)->globalPos());
Expand Down
10 changes: 10 additions & 0 deletions src/widgets/dtextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ void DTextEdit::keyPressEvent(QKeyEvent *event)

void DTextEdit::contextMenuEvent(QContextMenuEvent *e)
{
auto msg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", "/",
"org.freedesktop.DBus.Peer", "Ping");
// 用之前 Ping 一下, 300ms 内没回复就认定是服务出问题,不再添加助手菜单项
auto pingReply = QDBusConnection::sessionBus().call(msg, QDBus::BlockWithGui, 300);
auto errorType = QDBusConnection::sessionBus().lastError().type();
if (errorType == QDBusError::Timeout || errorType == QDBusError::NoReply) {
qWarning() << pingReply << "\nwill not add aiassistant actions!";
return QTextEdit::contextMenuEvent(e);
}

QDBusInterface testSpeech("com.iflytek.aiassistant",
"/aiassistant/tts",
"com.iflytek.aiassistant.tts",
Expand Down

0 comments on commit a393c85

Please sign in to comment.