From 09ad16bc44f1668fdae6c750514f4a543096a285 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Mon, 8 Jan 2018 18:04:35 +0100 Subject: [PATCH] refactor: cleanup notifications and core --- src/core/core.cpp | 37 ++++++++++-------------------------- src/core/core.h | 3 +-- src/widget/form/chatform.cpp | 15 ++++++++++++--- src/widget/form/chatform.h | 3 ++- src/widget/widget.cpp | 22 +++++++++------------ src/widget/widget.h | 3 +-- 6 files changed, 35 insertions(+), 48 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 6a5d724c8..65589a289 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -234,26 +234,6 @@ void Core::makeTox(QByteArray savedata) } } -/** - * @brief Creates CoreAv instance. Should be called after makeTox method - */ -void Core::makeAv() -{ - if (!tox) { - qCritical() << "No Tox instance, can't create ToxAV"; - return; - } - av = new CoreAV(tox); - if (!av->getToxAv()) { - qCritical() << "Toxav core failed to start"; - emit failedToStart(); - } - for (const auto& callback : toCallWhenAvReady) { - callback(av); - } - toCallWhenAvReady.clear(); -} - /** * @brief Initializes the core, must be called before anything else */ @@ -263,7 +243,6 @@ void Core::start(const QByteArray& savedata) if (isNewProfile) { qDebug() << "Creating a new profile"; makeTox(QByteArray()); - makeAv(); setStatusMessage(tr("Toxing on qTox")); setUsername(profile.getName()); } else { @@ -274,7 +253,6 @@ void Core::start(const QByteArray& savedata) } makeTox(savedata); - makeAv(); } qsrand(time(nullptr)); @@ -284,6 +262,15 @@ void Core::start(const QByteArray& savedata) return; } + // toxcore is successfully created, create toxav + av = new CoreAV(tox); + if (!av->getToxAv()) { + qCritical() << "Toxav failed to start"; + emit failedToStart(); + deadifyTox(); + return; + } + // set GUI with user and statusmsg QString name = getUsername(); if (!name.isEmpty()) { @@ -332,6 +319,7 @@ void Core::start(const QByteArray& savedata) process(); // starts its own timer av->start(); + emit avReady(); } /* Using the now commented out statements in checkConnection(), I watched how @@ -1385,11 +1373,6 @@ bool Core::isReady() const return av && av->getToxAv() && tox && ready; } -void Core::callWhenAvReady(std::function&& toCall) -{ - toCallWhenAvReady.emplace_back(std::move(toCall)); -} - /** * @brief Sets the NoSpam value to prevent friend request spam * @param nospam an arbitrary which becomes part of the Tox ID diff --git a/src/core/core.h b/src/core/core.h index 8ecc9dfb9..ef4747f08 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -81,7 +81,6 @@ public: QPair getKeypair() const; bool isReady() const; - void callWhenAvReady(std::function&& toCall); void sendFile(uint32_t friendId, QString filename, QString filePath, long long filesize); @@ -170,6 +169,7 @@ signals: void failedToStart(); void badProxy(); + void avReady(); void fileSendStarted(ToxFile file); void fileReceiveRequested(ToxFile file); @@ -235,7 +235,6 @@ private: QMutex messageSendMutex; bool ready; const ICoreSettings* const s; - std::vector> toCallWhenAvReady; static QThread* coreThread; diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 6ba5fb7f1..68a84c341 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -18,8 +18,6 @@ */ #include "chatform.h" - -#include "src/audio/audio.h" #include "src/chatlog/chatlinecontentproxy.h" #include "src/chatlog/chatlog.h" #include "src/chatlog/chatmessage.h" @@ -53,6 +51,15 @@ #include +/** + * @brief ChatForm::incomingNotification Notify that we are called by someone. + * @param friendId Friend that is calling us. + * + * @brief ChatForm::outgoingNotification Notify that we are calling someone. + * + * @brief stopNotification Tell others to stop notification of a call. + */ + static const int CHAT_WIDGET_MIN_HEIGHT = 50; static const int DELIVER_OFFLINE_MESSAGES_DELAY = 250; static const int SCREENSHOT_GRABBER_OPENING_DELAY = 500; @@ -368,7 +375,7 @@ void ChatForm::onAvStart(uint32_t friendId, bool video) hideNetcam(); } - Audio::getInstance().stopLoop(); + emit stopNotification(); updateCallButtons(); startCounter(); } @@ -385,6 +392,7 @@ void ChatForm::onAvEnd(uint32_t friendId, bool error) netcam->showNormal(); } + emit stopNotification(); updateCallButtons(); stopCounter(error); hideNetcam(); @@ -403,6 +411,7 @@ void ChatForm::onAnswerCallTriggered(bool video) { headWidget->removeCallConfirm(); uint32_t friendId = f->getId(); + emit stopNotification(); emit acceptCall(friendId); updateCallButtons(); diff --git a/src/widget/form/chatform.h b/src/widget/form/chatform.h index def00a65d..bb926533d 100644 --- a/src/widget/form/chatform.h +++ b/src/widget/form/chatform.h @@ -30,7 +30,6 @@ #include "src/widget/tool/screenshotgrabber.h" class CallConfirmWidget; -class CoreAV; class FileTransferInstance; class Friend; class History; @@ -57,8 +56,10 @@ public: static const QString ACTION_PREFIX; signals: + void incomingNotification(uint32_t friendId); void outgoingNotification(); + void stopNotification(); void rejectCall(uint32_t friendId); void acceptCall(uint32_t friendId); diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 19b3f3ab5..667a4027d 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -244,7 +244,6 @@ void Widget::init() const Settings& s = Settings::getInstance(); - core->callWhenAvReady([this](CoreAV* av){connect(av, &CoreAV::avEnd, this, &Widget::onCallEnd);}); connect(core, &Core::fileDownloadFinished, filesForm, &FilesForm::onFileDownloadComplete); connect(core, &Core::fileUploadFinished, filesForm, &FilesForm::onFileUploadComplete); connect(ui->addButton, &QPushButton::clicked, this, &Widget::onAddClicked); @@ -954,23 +953,20 @@ void Widget::outgoingNotification() audio.playMono16Sound(Audio::getSound(Audio::Sound::OutgoingCall)); } +/** + * @brief Widget::onStopNotification Stop the notification sound. + */ +void Widget::onStopNotification() +{ + Audio::getInstance().stopLoop(); +} + void Widget::onRejectCall(uint32_t friendId) { - Audio::getInstance().stopLoop(); CoreAV* av = Core::getInstance()->getAv(); av->cancelCall(friendId); } -void Widget::onAcceptCall(uint32_t friendId) -{ - Audio::getInstance().stopLoop(); -} - -void Widget::onCallEnd(uint32_t friendId) -{ - Audio::getInstance().stopLoop(); -} - void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk) { Settings& s = Settings::getInstance(); @@ -1000,8 +996,8 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk) connect(friendForm, &ChatForm::incomingNotification, this, &Widget::incomingNotification); connect(friendForm, &ChatForm::outgoingNotification, this, &Widget::outgoingNotification); + connect(friendForm, &ChatForm::stopNotification, this, &Widget::onStopNotification); connect(friendForm, &ChatForm::rejectCall, this, &Widget::onRejectCall); - connect(friendForm, &ChatForm::acceptCall, this, &Widget::onAcceptCall); connect(widget, &FriendWidget::newWindowOpened, this, &Widget::openNewDialog); connect(widget, &FriendWidget::chatroomWidgetClicked, this, &Widget::onChatroomWidgetClicked); diff --git a/src/widget/widget.h b/src/widget/widget.h index d3d75f262..4b5c56f21 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -217,8 +217,7 @@ private slots: void outgoingNotification(); void incomingNotification(uint32_t friendId); void onRejectCall(uint32_t friendId); - void onAcceptCall(uint32_t friendId); - void onCallEnd(uint32_t friendId); + void onStopNotification(); private: // QMainWindow overrides