1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(chatform): Small ChatForm refactoring.

This commit is contained in:
Diadlo 2016-12-25 12:45:25 +03:00 committed by Zetok Zalbavar
parent 2aed859bdb
commit 3307fcd96f
No known key found for this signature in database
GPG Key ID: C953D3880212068A

View File

@ -197,10 +197,13 @@ void ChatForm::onSendTriggered()
void ChatForm::onTextEditChanged() void ChatForm::onTextEditChanged()
{ {
Core* core = Core::getInstance();
if (!Settings::getInstance().getTypingNotification()) if (!Settings::getInstance().getTypingNotification())
{ {
if (isTyping) if (isTyping)
Core::getInstance()->sendTyping(f->getFriendId(), false); {
core->sendTyping(f->getFriendId(), false);
}
isTyping = false; isTyping = false;
return; return;
@ -210,24 +213,28 @@ void ChatForm::onTextEditChanged()
{ {
typingTimer.start(3000); typingTimer.start(3000);
if (!isTyping) if (!isTyping)
Core::getInstance()->sendTyping(f->getFriendId(), (isTyping = true)); {
isTyping = true;
core->sendTyping(f->getFriendId(), isTyping);
}
} }
else else
{ {
Core::getInstance()->sendTyping(f->getFriendId(), (isTyping = false)); isTyping = false;
core->sendTyping(f->getFriendID(), isTyping);
} }
} }
void ChatForm::onAttachClicked() void ChatForm::onAttachClicked()
{ {
QStringList paths = QFileDialog::getOpenFileNames(this, QStringList paths = QFileDialog::getOpenFileNames(
tr("Send a file"), this, tr("Send a file"), QDir::homePath(),
QDir::homePath(), 0, 0, QFileDialog::DontUseNativeDialog);
0,
0,
QFileDialog::DontUseNativeDialog);
if (paths.isEmpty()) if (paths.isEmpty())
{
return; return;
}
Core* core = Core::getInstance(); Core* core = Core::getInstance();
for (QString path : paths) for (QString path : paths)
@ -235,21 +242,23 @@ void ChatForm::onAttachClicked()
QFile file(path); QFile file(path);
if (!file.exists() || !file.open(QIODevice::ReadOnly)) if (!file.exists() || !file.open(QIODevice::ReadOnly))
{ {
QMessageBox::warning(this, QString fileName = QFileInfo(path).fileName();
tr("Unable to open"), QMessageBox::warning(
tr("qTox wasn't able to open %1").arg(QFileInfo(path).fileName())); this, tr("Unable to open"),
tr("qTox wasn't able to open %1").arg(fileName));
continue; continue;
} }
if (file.isSequential()) if (file.isSequential())
{ {
QMessageBox::critical(this, QMessageBox::critical(
tr("Bad idea"), this, tr("Bad idea"),
tr("You're trying to send a sequential file," tr("You're trying to send a sequential file,"
" which is not going to work!")); " which is not going to work!"));
file.close(); file.close();
continue; continue;
} }
qint64 filesize = file.size(); qint64 filesize = file.size();
file.close(); file.close();
QFileInfo fi(path); QFileInfo fi(path);
@ -261,7 +270,9 @@ void ChatForm::onAttachClicked()
void ChatForm::startFileSend(ToxFile file) void ChatForm::startFileSend(ToxFile file)
{ {
if (file.friendId != f->getFriendId()) if (file.friendId != f->getFriendId())
{
return; return;
}
QString name; QString name;
const Core* core = Core::getInstance(); const Core* core = Core::getInstance();
@ -272,7 +283,8 @@ void ChatForm::startFileSend(ToxFile file)
previousId = self; previousId = self;
} }
insertChatMessage(ChatMessage::createFileTransferMessage(name, file, true, QDateTime::currentDateTime())); insertChatMessage(ChatMessage::createFileTransferMessage(
name, file, true, QDateTime::currentDateTime()));
Widget::getInstance()->updateFriendActivity(f); Widget::getInstance()->updateFriendActivity(f);
} }
@ -280,7 +292,9 @@ void ChatForm::startFileSend(ToxFile file)
void ChatForm::onFileRecvRequest(ToxFile file) void ChatForm::onFileRecvRequest(ToxFile file)
{ {
if (file.friendId != f->getFriendId()) if (file.friendId != f->getFriendId())
{
return; return;
}
Widget::getInstance()->newFriendMessageAlert(file.friendId); Widget::getInstance()->newFriendMessageAlert(file.friendId);
@ -292,7 +306,8 @@ void ChatForm::onFileRecvRequest(ToxFile file)
previousId = friendId; previousId = friendId;
} }
ChatMessage::Ptr msg = ChatMessage::createFileTransferMessage(name, file, false, QDateTime::currentDateTime()); ChatMessage::Ptr msg = ChatMessage::createFileTransferMessage(
name, file, false, QDateTime::currentDateTime());
insertChatMessage(msg); insertChatMessage(msg);
ChatLineContentProxy* proxy = static_cast<ChatLineContentProxy*>(msg->getContent(1)); ChatLineContentProxy* proxy = static_cast<ChatLineContentProxy*>(msg->getContent(1));
@ -315,12 +330,15 @@ void ChatForm::onFileRecvRequest(ToxFile file)
void ChatForm::onAvInvite(uint32_t friendId, bool video) void ChatForm::onAvInvite(uint32_t friendId, bool video)
{ {
if (friendId != f->getFriendId()) if (friendId != f->getFriendId())
{
return; return;
}
callConfirm = new CallConfirmWidget(video ? videoButton : callButton, *f); callConfirm = new CallConfirmWidget(video ? videoButton : callButton, *f);
insertChatMessage(ChatMessage::createChatInfoMessage(tr("%1 calling").arg(f->getDisplayedName()), insertChatMessage(ChatMessage::createChatInfoMessage(
ChatMessage::INFO, tr("%1 calling").arg(f->getDisplayedName()),
QDateTime::currentDateTime())); ChatMessage::INFO, QDateTime::currentDateTime()));
/* AutoAcceptCall is set for this friend */ /* AutoAcceptCall is set for this friend */
if ((video && Settings::getInstance().getAutoAcceptCall(f->getPublicKey()).testFlag(Settings::AutoAcceptCall::Video)) || if ((video && Settings::getInstance().getAutoAcceptCall(f->getPublicKey()).testFlag(Settings::AutoAcceptCall::Video)) ||
(!video && Settings::getInstance().getAutoAcceptCall(f->getPublicKey()).testFlag(Settings::AutoAcceptCall::Audio))) (!video && Settings::getInstance().getAutoAcceptCall(f->getPublicKey()).testFlag(Settings::AutoAcceptCall::Audio)))
@ -328,7 +346,8 @@ void ChatForm::onAvInvite(uint32_t friendId, bool video)
uint32_t friendId = f->getFriendId(); uint32_t friendId = f->getFriendId();
qDebug() << "automatic call answer"; qDebug() << "automatic call answer";
CoreAV* coreav = Core::getInstance()->getAv(); CoreAV* coreav = Core::getInstance()->getAv();
QMetaObject::invokeMethod(coreav, "answerCall", Qt::QueuedConnection, Q_ARG(uint32_t, friendId)); QMetaObject::invokeMethod(coreav, "answerCall", Qt::QueuedConnection,
Q_ARG(uint32_t, friendId));
onAvStart(friendId,video); onAvStart(friendId,video);
} }
else else
@ -352,30 +371,40 @@ void ChatForm::onAvInvite(uint32_t friendId, bool video)
} }
} }
void ChatForm::onAvStart(uint32_t FriendId, bool video) void ChatForm::onAvStart(uint32_t friendId, bool video)
{ {
if (FriendId != f->getFriendId()) if (friendId != f->getFriendId())
{
return; return;
}
if (video) if (video)
{
showNetcam(); showNetcam();
}
else else
{
hideNetcam(); hideNetcam();
}
updateCallButtons(); updateCallButtons();
startCounter(); startCounter();
} }
void ChatForm::onAvEnd(uint32_t FriendId) void ChatForm::onAvEnd(uint32_t friendId)
{ {
if (FriendId != f->getFriendId()) if (friendId != f->getFriendId())
{
return; return;
}
delete callConfirm; delete callConfirm;
//Fixes an OS X bug with ending a call while in full screen //Fixes an OS X bug with ending a call while in full screen
if (netcam && netcam->isFullScreen()) if (netcam && netcam->isFullScreen())
{
netcam->showNormal(); netcam->showNormal();
}
Audio::getInstance().stopLoop(); Audio::getInstance().stopLoop();
@ -525,16 +554,21 @@ void ChatForm::onVolMuteToggle()
void ChatForm::onFileSendFailed(uint32_t friendId, const QString &fname) void ChatForm::onFileSendFailed(uint32_t friendId, const QString &fname)
{ {
if (friendId != f->getFriendId()) if (friendId != f->getFriendId())
{
return; return;
}
addSystemInfoMessage(tr("Failed to send file \"%1\"").arg(fname), ChatMessage::ERROR, QDateTime::currentDateTime()); addSystemInfoMessage(tr("Failed to send file \"%1\"").arg(fname),
ChatMessage::ERROR, QDateTime::currentDateTime());
} }
void ChatForm::onFriendStatusChanged(uint32_t friendId, Status status) void ChatForm::onFriendStatusChanged(uint32_t friendId, Status status)
{ {
// Disable call buttons if friend is offline // Disable call buttons if friend is offline
if(friendId != f->getFriendId()) if(friendId != f->getFriendId())
{
return; return;
}
if (status == Status::Offline) if (status == Status::Offline)
{ {
@ -572,13 +606,17 @@ void ChatForm::onFriendStatusChanged(uint32_t friendId, Status status)
void ChatForm::onFriendTypingChanged(quint32 friendId, bool isTyping) void ChatForm::onFriendTypingChanged(quint32 friendId, bool isTyping)
{ {
if (friendId == f->getFriendId()) if (friendId == f->getFriendId())
{
setFriendTyping(isTyping); setFriendTyping(isTyping);
}
} }
void ChatForm::onFriendNameChanged(const QString& name) void ChatForm::onFriendNameChanged(const QString& name)
{ {
if (sender() == f) if (sender() == f)
{
setName(name); setName(name);
}
} }
void ChatForm::onFriendMessageReceived(quint32 friendId, const QString& message, void ChatForm::onFriendMessageReceived(quint32 friendId, const QString& message,
@ -625,7 +663,8 @@ GenericNetCamView *ChatForm::createNetcam()
uint32_t friendId = f->getFriendId(); uint32_t friendId = f->getFriendId();
NetCamView* view = new NetCamView(friendId, this); NetCamView* view = new NetCamView(friendId, this);
CoreAV* av = Core::getInstance()->getAv(); CoreAV* av = Core::getInstance()->getAv();
view->show(av->getVideoSourceFromCall(friendId), f->getDisplayedName()); VideoSource* source = av->getVideoSourceFromCall(friendId);
view->show(source, f->getDisplayedName());
return view; return view;
} }
@ -645,9 +684,10 @@ void ChatForm::dropEvent(QDropEvent *ev)
for (QUrl url : ev->mimeData()->urls()) for (QUrl url : ev->mimeData()->urls())
{ {
QFileInfo info(url.path()); QFileInfo info(url.path());
QFile file(info.absoluteFilePath()); QFile file(info.absoluteFilePath());
if (url.isValid() && !url.isLocalFile() && (url.toString().length() < TOX_MAX_MESSAGE_LENGTH))
if (url.isValid() && !url.isLocalFile() &&
url.toString().length() < TOX_MAX_MESSAGE_LENGTH)
{ {
SendMessageStr(url.toString()); SendMessageStr(url.toString());
continue; continue;
@ -686,10 +726,12 @@ void ChatForm::dropEvent(QDropEvent *ev)
} }
} }
void ChatForm::onAvatarRemoved(uint32_t FriendId) void ChatForm::onAvatarRemoved(uint32_t friendId)
{ {
if (FriendId != f->getFriendId()) if (friendId != f->getFriendId())
{
return; return;
}
avatar->setPixmap(QPixmap(":/img/contact_dark.svg")); avatar->setPixmap(QPixmap(":/img/contact_dark.svg"));
} }
@ -708,7 +750,9 @@ void ChatForm::onDeliverOfflineMessages()
void ChatForm::onLoadChatHistory() void ChatForm::onLoadChatHistory()
{ {
if (sender() == f) if (sender() == f)
{
loadHistory(QDateTime::currentDateTime().addDays(-7), true); loadHistory(QDateTime::currentDateTime().addDays(-7), true);
}
} }
// TODO: Split on smaller methods (style) // TODO: Split on smaller methods (style)
@ -717,12 +761,16 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered)
QDateTime now = historyBaselineDate.addMSecs(-1); QDateTime now = historyBaselineDate.addMSecs(-1);
if (since > now) if (since > now)
{
return; return;
}
if (!earliestMessage.isNull()) if (!earliestMessage.isNull())
{ {
if (earliestMessage < since) if (earliestMessage < since)
{
return; return;
}
if (earliestMessage < now) if (earliestMessage < now)
{ {
@ -731,7 +779,9 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered)
} }
} }
auto msgs = Nexus::getProfile()->getHistory()->getChatHistory(f->getPublicKey().toString(), since, now); History* history = Nexus::getProfile()->getHistory();
QString pk = f->getToxId().getPublicKeyString();
QList<History::HistMessage> msgs = history->getChatHistory(pk, since, now);
ToxPk storedPrevId = previousId; ToxPk storedPrevId = previousId;
ToxPk prevId; ToxPk prevId;
@ -749,10 +799,10 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered)
{ {
lastDate = msgDate; lastDate = msgDate;
QString dateText = msgDate.toString(Settings::getInstance().getDateFormat()); QString dateText = msgDate.toString(Settings::getInstance().getDateFormat());
historyMessages.append( auto msg = ChatMessage::createChatInfoMessage(dateText,
ChatMessage::createChatInfoMessage(dateText,
ChatMessage::INFO, ChatMessage::INFO,
QDateTime())); QDateTime());
historyMessages.append(msg);
} }
// Show each messages // Show each messages
@ -777,32 +827,30 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered)
bool isAction = it.message.startsWith(ACTION_PREFIX, Qt::CaseInsensitive); bool isAction = it.message.startsWith(ACTION_PREFIX, Qt::CaseInsensitive);
bool needSending = !it.isSent && isSelf; bool needSending = !it.isSent && isSelf;
ChatMessage::Ptr msg = QString messageText = isAction ? it.message.mid(4) : it.message;
ChatMessage::createChatMessage(authorStr, ChatMessage::MessageType type = isAction ? ChatMessage::ACTION : ChatMessage::NORMAL;
isAction ? it.message.mid(4) : it.message, QDateTime dateTime = needSending ? QDateTime() : msgDateTime;
isAction ? ChatMessage::ACTION : ChatMessage::NORMAL, auto msg = ChatMessage::createChatMessage(
isSelf, authorStr, messageText, type, isSelf, dateTime);
needSending ? QDateTime() : msgDateTime);
if (!isAction && (prevId == authorPk) && (prevMsgDateTime.secsTo(msgDateTime) < getChatLog()->repNameAfter) ) uint prev = prevMsgDateTime.secsTo(msgDateTime);
if (!isAction && prevId == authorId && prev < getChatLog()->repNameAfter)
{
msg->hideSender(); msg->hideSender();
}
prevId = authorPk; prevId = authorPk;
prevMsgDateTime = msgDateTime; prevMsgDateTime = msgDateTime;
if (needSending) if (needSending && processUndelivered)
{ {
if (processUndelivered) Core* core = Core::getInstance();
{ uint32_t friendId = f->getFriendId();
int rec; int rec = isAction ? core->sendAction(friendId, msg->toString())
if (!isAction) : core->sendMessage(friendId, msg->toString());
rec = Core::getInstance()->sendMessage(f->getFriendId(), msg->toString());
else
rec = Core::getInstance()->sendAction(f->getFriendId(), msg->toString());
getOfflineMsgEngine()->registerReceipt(rec, it.id, msg); getOfflineMsgEngine()->registerReceipt(rec, it.id, msg);
} }
}
historyMessages.append(msg); historyMessages.append(msg);
} }
@ -1017,12 +1065,20 @@ QString ChatForm::secondsToDHMS(quint32 duration)
// I assume no one will ever have call longer than a month // I assume no one will ever have call longer than a month
if (days) if (days)
{
return cD + res.sprintf("%dd%02dh %02dm %02ds", days, hours, minutes, seconds); return cD + res.sprintf("%dd%02dh %02dm %02ds", days, hours, minutes, seconds);
else if (hours) }
if (hours)
{
return cD + res.sprintf("%02dh %02dm %02ds", hours, minutes, seconds); return cD + res.sprintf("%02dh %02dm %02ds", hours, minutes, seconds);
else if (minutes) }
if (minutes)
{
return cD + res.sprintf("%02dm %02ds", minutes, seconds); return cD + res.sprintf("%02dm %02ds", minutes, seconds);
else }
return cD + res.sprintf("%02ds", seconds); return cD + res.sprintf("%02ds", seconds);
} }
@ -1099,20 +1155,21 @@ void ChatForm::SendMessageStr(QString msg)
ChatMessage::Ptr ma = addSelfMessage(qt_msg, isAction, timestamp, false); ChatMessage::Ptr ma = addSelfMessage(qt_msg, isAction, timestamp, false);
int rec; Core* core = Core::getInstance();
if (isAction) uint32_t friendId = f->getFriendId();
rec = Core::getInstance()->sendAction(f->getFriendId(), qt_msg); int rec = isAction ? core->sendAction(friendId, qt_msg)
else : core->sendMessage(friendId, qt_msg);
rec = Core::getInstance()->sendMessage(f->getFriendId(), qt_msg);
Profile* profile = Nexus::getProfile(); Profile* profile = Nexus::getProfile();
if (profile->isHistoryEnabled()) if (profile->isHistoryEnabled())
{ {
auto* offMsgEngine = getOfflineMsgEngine(); auto* offMsgEngine = getOfflineMsgEngine();
profile->getHistory()->addNewMessage(f->getPublicKey().toString(), qt_msg_hist, QString selfPk = Core::getInstance()->getSelfId().publicKey;
Core::getInstance()->getSelfId().toString(), timestamp, status, Core::getInstance()->getUsername(), QString pk = f->getToxId().getPublicKeyString();
[offMsgEngine,rec,ma](int64_t id) QString name = Core::getInstance()->getUsername();
profile->getHistory()->addNewMessage(
pk, qt_msg_hist, selfPk, timestamp, status,
name, [offMsgEngine, rec, ma](int64_t id)
{ {
offMsgEngine->registerReceipt(rec, id, ma); offMsgEngine->registerReceipt(rec, id, ma);
}); });
@ -1140,5 +1197,7 @@ void ChatForm::retranslateUi()
updateMuteVolButton(); updateMuteVolButton();
if (netcam) if (netcam)
{
netcam->setShowMessages(chatWidget->isVisible()); netcam->setShowMessages(chatWidget->isVisible());
}
} }