From e6d40be72eff546f07b30a2ad33dc95fc8a29362 Mon Sep 17 00:00:00 2001 From: "Jakie.OYMJ" Date: Fri, 5 Oct 2018 10:24:39 +0800 Subject: [PATCH] fix: add remove history prompt, clear log area after remove --- src/model/about/aboutfriend.cpp | 11 +++++++++++ src/model/about/aboutfriend.h | 1 + src/model/about/iaboutfriend.h | 1 + src/persistence/history.cpp | 10 ++++++++++ src/persistence/history.h | 2 ++ src/widget/about/aboutfriendform.cpp | 23 ++++++++++++++++++++--- src/widget/about/aboutfriendform.h | 3 +++ src/widget/form/chatform.cpp | 4 ++-- src/widget/form/chatform.h | 2 +- src/widget/form/genericchatform.cpp | 25 +++++++++++++------------ src/widget/form/genericchatform.h | 3 +-- src/widget/friendwidget.cpp | 1 + src/widget/friendwidget.h | 1 + src/widget/widget.cpp | 1 + src/widget/widget.h | 2 +- 15 files changed, 69 insertions(+), 21 deletions(-) diff --git a/src/model/about/aboutfriend.cpp b/src/model/about/aboutfriend.cpp index 48fa34927..98720416a 100644 --- a/src/model/about/aboutfriend.cpp +++ b/src/model/about/aboutfriend.cpp @@ -110,3 +110,14 @@ bool AboutFriend::clearHistory() return false; } + +bool AboutFriend::isHistoryExistence() +{ + History* const history = Nexus::getProfile()->getHistory(); + if (history) { + const ToxPk pk = f->getPublicKey(); + return history->isHistoryExistence(pk.toString()); + } + + return false; +} diff --git a/src/model/about/aboutfriend.h b/src/model/about/aboutfriend.h index 2d86db4ca..b98a25d56 100644 --- a/src/model/about/aboutfriend.h +++ b/src/model/about/aboutfriend.h @@ -36,6 +36,7 @@ public: void setAutoGroupInvite(bool enabled) override; bool clearHistory() override; + bool isHistoryExistence() override; SIGNAL_IMPL(AboutFriend, nameChanged, const QString&) SIGNAL_IMPL(AboutFriend, statusChanged, const QString&) diff --git a/src/model/about/iaboutfriend.h b/src/model/about/iaboutfriend.h index b8f3fc847..b046ab3f6 100644 --- a/src/model/about/iaboutfriend.h +++ b/src/model/about/iaboutfriend.h @@ -29,6 +29,7 @@ public: virtual void setAutoGroupInvite(bool enabled) = 0; virtual bool clearHistory() = 0; + virtual bool isHistoryExistence() = 0; /* signals */ DECLARE_SIGNAL(nameChanged, const QString&); diff --git a/src/persistence/history.cpp b/src/persistence/history.cpp index 114595d7e..b259e8bdd 100644 --- a/src/persistence/history.cpp +++ b/src/persistence/history.cpp @@ -85,6 +85,16 @@ bool History::isValid() return db && db->isOpen(); } +/** + * @brief Checks if a friend has chat history + * @param friendPk + * @return True if has, false otherwise. + */ +bool History::isHistoryExistence(const QString& friendPk) +{ + return !getChatHistoryDefaultNum(friendPk).isEmpty(); +} + /** * @brief Erases all the chat history from the database. */ diff --git a/src/persistence/history.h b/src/persistence/history.h index bca46daa2..b6cf34ab6 100644 --- a/src/persistence/history.h +++ b/src/persistence/history.h @@ -73,6 +73,8 @@ public: bool isValid(); void import(const HistoryKeeper& oldHistory); + bool isHistoryExistence(const QString& friendPk); + void eraseHistory(); void removeFriendHistory(const QString& friendPk); void addNewMessage(const QString& friendPk, const QString& message, const QString& sender, diff --git a/src/widget/about/aboutfriendform.cpp b/src/widget/about/aboutfriendform.cpp index e746e8fb9..e8e5ce6db 100644 --- a/src/widget/about/aboutfriendform.cpp +++ b/src/widget/about/aboutfriendform.cpp @@ -1,4 +1,5 @@ #include "aboutfriendform.h" +#include "src/widget/gui.h" #include "ui_aboutfriendform.h" #include "src/core/core.h" @@ -25,6 +26,8 @@ AboutFriendForm::AboutFriendForm(std::unique_ptr _about, QWidget* const QString dir = about->getAutoAcceptDir(); ui->autoacceptfile->setChecked(!dir.isEmpty()); + ui->removeHistory->setEnabled(about->isHistoryExistence()); + const int index = static_cast(about->getAutoAcceptCall()); ui->autoacceptcall->setCurrentIndex(index); @@ -105,10 +108,24 @@ void AboutFriendForm::onAcceptedClicked() void AboutFriendForm::onRemoveHistoryClicked() { - about->clearHistory(); + const bool retYes = GUI::askQuestion(tr("Confirmation"), + tr("Are you sure to remove %1 chat history?").arg(about->getName()), + /* defaultAns = */ false, /* warning = */ true, /* yesno = */ true); + if (!retYes) { + return; + } - QMessageBox::information(this, tr("History removed"), tr("Chat history with %1 removed!") - .arg(about->getName().toHtmlEscaped()), QMessageBox::Ok); + const bool result = about->clearHistory(); + + if (!result) { + GUI::showWarning(tr("History removed"), + tr("Failed to remove chat history with %1!").arg(about->getName()).toHtmlEscaped()); + return; + } + + emit histroyRemoved(); + + ui->removeHistory->setEnabled(false); // For know clearly to has removed the history } AboutFriendForm::~AboutFriendForm() diff --git a/src/widget/about/aboutfriendform.h b/src/widget/about/aboutfriendform.h index f80672628..e8fa12704 100644 --- a/src/widget/about/aboutfriendform.h +++ b/src/widget/about/aboutfriendform.h @@ -24,6 +24,9 @@ private: Ui::AboutFriendForm* ui; const std::unique_ptr about; +signals: + void histroyRemoved(); + private slots: void onAutoAcceptDirChanged(const QString& path); void onAcceptedClicked(); diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index b2c48a0e2..e2326c4b2 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -725,9 +725,9 @@ void ChatForm::dropEvent(QDropEvent* ev) } } -void ChatForm::clearChatArea(bool notInForm) +void ChatForm::clearChatArea() { - GenericChatForm::clearChatArea(notInForm); + GenericChatForm::clearChatArea(/* confirm = */ false, /* inform = */ true); offlineEngine->removeAllReceipts(); } diff --git a/src/widget/form/chatform.h b/src/widget/form/chatform.h index 6b68a9059..9f166d2db 100644 --- a/src/widget/form/chatform.h +++ b/src/widget/form/chatform.h @@ -74,6 +74,7 @@ public slots: void onAvEnd(uint32_t friendId, bool error); void onAvatarChanged(const ToxPk &friendPk, const QPixmap& pic); void onFileNameChanged(const ToxPk& friendPk); + void clearChatArea(); protected slots: void searchInBegin(const QString& phrase, const ParameterSearch& parameter) override; @@ -81,7 +82,6 @@ protected slots: void onSearchDown(const QString& phrase, const ParameterSearch& parameter) override; private slots: - void clearChatArea(bool notInForm) override final; void onSendTriggered() override; void onAttachClicked() override; void onScreenshotClicked() override; diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index dcc1f4579..f98537b7a 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -220,7 +220,7 @@ GenericChatForm::GenericChatForm(const Contact* contact, QWidget* parent) saveChatAction = menu.addAction(QIcon::fromTheme("document-save"), QString(), this, SLOT(onSaveLogClicked())); clearAction = menu.addAction(QIcon::fromTheme("edit-clear"), QString(), - this, SLOT(clearChatArea(bool)), + this, SLOT(clearChatArea()), QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_L)); addAction(clearAction); @@ -816,27 +816,28 @@ std::pair GenericChatForm::indexForSearchInLine(const QString& txt, co void GenericChatForm::clearChatArea() { - clearChatArea(true); + clearChatArea(/* confirm = */ true, /* inform = */ true); } -void GenericChatForm::clearChatArea(bool notinform) +void GenericChatForm::clearChatArea(bool confirm, bool inform) { - QMessageBox::StandardButton mboxResult = - QMessageBox::question(this, tr("Confirmation"), - tr("You are sure that you want to clear all displayed messages?"), - QMessageBox::Yes | QMessageBox::No, QMessageBox::No); - if (mboxResult == QMessageBox::No) { - return; + if (confirm) { + QMessageBox::StandardButton mboxResult = + QMessageBox::question(this, tr("Confirmation"), + tr("You are sure that you want to clear all displayed messages?"), + QMessageBox::Yes | QMessageBox::No, QMessageBox::No); + if (mboxResult == QMessageBox::No) { + return; + } } + chatWidget->clear(); previousId = ToxPk(); - if (!notinform) + if (inform) addSystemInfoMessage(tr("Cleared"), ChatMessage::INFO, QDateTime::currentDateTime()); earliestMessage = QDateTime(); // null - - emit chatAreaCleared(); } void GenericChatForm::onSelectAllClicked() diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index c91fa8314..65f1bf974 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -87,7 +87,6 @@ public: signals: void sendMessage(uint32_t, QString); void sendAction(uint32_t, QString); - void chatAreaCleared(); void messageInserted(); void messageNotFoundShow(SearchDirection direction); @@ -104,8 +103,8 @@ protected slots: void onEmoteInsertRequested(QString str); void onSaveLogClicked(); void onCopyLogClicked(); - virtual void clearChatArea(bool); void clearChatArea(); + void clearChatArea(bool confirm, bool inform); void onSelectAllClicked(); void showFileMenu(); void hideFileMenu(); diff --git a/src/widget/friendwidget.cpp b/src/widget/friendwidget.cpp index 5b45173aa..ea124746c 100644 --- a/src/widget/friendwidget.cpp +++ b/src/widget/friendwidget.cpp @@ -294,6 +294,7 @@ void FriendWidget::showDetails() const auto iabout = new AboutFriend(frnd, &Settings::getInstance()); std::unique_ptr about = std::unique_ptr(iabout); const auto aboutUser = new AboutFriendForm(std::move(about), Widget::getInstance()); + connect(aboutUser, &AboutFriendForm::histroyRemoved, this, &FriendWidget::friendHistoryRemoved); aboutUser->show(); } diff --git a/src/widget/friendwidget.h b/src/widget/friendwidget.h index bfed620ca..6e37cc345 100644 --- a/src/widget/friendwidget.h +++ b/src/widget/friendwidget.h @@ -48,6 +48,7 @@ signals: void removeFriend(int friendId); void copyFriendIdToClipboard(int friendId); void contextMenuCalled(QContextMenuEvent* event); + void friendHistoryRemoved(); public slots: void onAvatarSet(const ToxPk& friendPk, const QPixmap& pic); diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 874e034f2..7e3d7486d 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -1015,6 +1015,7 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk) connect(widget, &FriendWidget::newWindowOpened, this, &Widget::openNewDialog); connect(widget, &FriendWidget::chatroomWidgetClicked, this, &Widget::onChatroomWidgetClicked); connect(widget, &FriendWidget::chatroomWidgetClicked, friendForm, &ChatForm::focusInput); + connect(widget, &FriendWidget::friendHistoryRemoved, friendForm, &ChatForm::clearChatArea); connect(widget, &FriendWidget::copyFriendIdToClipboard, this, &Widget::copyFriendIdToClipboard); connect(widget, &FriendWidget::contextMenuCalled, widget, &FriendWidget::onContextMenuCalled); connect(widget, SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int))); diff --git a/src/widget/widget.h b/src/widget/widget.h index 6745bdfbd..c7e5938a5 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -104,7 +104,7 @@ private: public: explicit Widget(QWidget* parent = nullptr); - ~Widget(); + ~Widget() override; void init(); void setCentralWidget(QWidget* widget, const QString& widgetName); QString getUsername();