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

995 lines
29 KiB
C++
Raw Normal View History

2014-07-07 00:19:45 +08:00
/*
Copyright © 2014-2015 by The qTox Project
2014-07-07 00:19:45 +08:00
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
2014-07-07 00:19:45 +08:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
2014-07-07 00:19:45 +08:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2014-07-07 00:19:45 +08:00
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
2014-07-07 00:19:45 +08:00
*/
2014-09-11 21:44:34 +08:00
#include <QDebug>
2015-06-11 00:11:50 +08:00
#include <QBoxLayout>
2014-09-11 21:44:34 +08:00
#include <QScrollBar>
#include <QFileDialog>
#include <QMessageBox>
#include <QPushButton>
2014-09-26 00:03:25 +08:00
#include <QMimeData>
#include <QFileInfo>
#include <QDragEnterEvent>
#include <QBitmap>
#include <QScreen>
#include <QTemporaryFile>
#include <QGuiApplication>
2015-06-08 02:24:55 +08:00
#include <QStyle>
#include <QSplitter>
2015-06-08 02:24:55 +08:00
#include <cassert>
2014-06-25 04:11:11 +08:00
#include "chatform.h"
#include "src/audio/audio.h"
#include "src/core/core.h"
2015-06-26 19:00:16 +08:00
#include "src/core/coreav.h"
2014-10-08 12:26:25 +08:00
#include "src/friend.h"
2015-06-06 07:44:47 +08:00
#include "src/widget/style.h"
#include "src/persistence/settings.h"
#include "src/core/cstring.h"
#include "src/widget/tool/callconfirmwidget.h"
#include "src/widget/friendwidget.h"
#include "src/widget/form/loadhistorydialog.h"
2014-10-08 12:26:25 +08:00
#include "src/widget/tool/chattextedit.h"
#include "src/widget/widget.h"
#include "src/widget/maskablepixmapwidget.h"
2015-06-06 07:44:47 +08:00
#include "src/widget/tool/croppinglabel.h"
2014-11-12 23:45:24 +08:00
#include "src/chatlog/chatmessage.h"
2014-12-14 04:11:03 +08:00
#include "src/chatlog/content/filetransferwidget.h"
2015-01-11 18:57:33 +08:00
#include "src/chatlog/chatlinecontentproxy.h"
2015-01-10 18:57:46 +08:00
#include "src/chatlog/content/text.h"
2015-01-05 01:21:35 +08:00
#include "src/chatlog/chatlog.h"
2015-08-19 01:40:11 +08:00
#include "src/video/netcamview.h"
2015-06-06 07:44:47 +08:00
#include "src/persistence/offlinemsgengine.h"
#include "src/widget/tool/screenshotgrabber.h"
#include "src/widget/tool/flyoutoverlaywidget.h"
2015-06-06 07:44:47 +08:00
#include "src/widget/translator.h"
#include "src/video/videosource.h"
#include "src/video/camerasource.h"
#include "src/nexus.h"
#include "src/persistence/profile.h"
2014-06-25 04:11:11 +08:00
ChatForm::ChatForm(Friend* chatFriend)
: f(chatFriend)
, isTyping{false}
2014-06-25 04:11:11 +08:00
{
Core* core = Core::getInstance();
coreav = core->getAv();
2014-11-06 23:26:22 +08:00
nameLabel->setText(f->getDisplayedName());
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"), Qt::transparent);
2014-06-27 07:49:48 +08:00
2014-09-10 00:28:07 +08:00
statusMessageLabel = new CroppingLabel();
2014-10-10 03:20:06 +08:00
statusMessageLabel->setObjectName("statusLabel");
statusMessageLabel->setFont(Style::getFont(Style::Medium));
statusMessageLabel->setMinimumHeight(Style::getFont(Style::Medium).pixelSize());
statusMessageLabel->setTextFormat(Qt::PlainText);
callConfirm = nullptr;
offlineEngine = new OfflineMsgEngine(f);
typingTimer.setSingleShot(true);
2015-01-26 06:19:25 +08:00
callDurationTimer = nullptr;
disableCallButtonsTimer = nullptr;
chatWidget->setTypingNotification(ChatMessage::createTypingNotification());
2015-01-10 18:57:46 +08:00
headTextLayout->addWidget(statusMessageLabel);
2014-06-25 04:11:11 +08:00
headTextLayout->addStretch();
2014-11-03 09:51:56 +08:00
callDuration = new QLabel();
headTextLayout->addWidget(callDuration, 1, Qt::AlignCenter);
2015-01-03 21:08:04 +08:00
callDuration->hide();
2014-06-25 04:11:11 +08:00
chatWidget->setMinimumHeight(160);
connect(this, &GenericChatForm::messageInserted, this, &ChatForm::onMessageInserted);
loadHistoryAction = menu.addAction(QString(), this, SLOT(onLoadHistory()));
connect(core, &Core::fileSendStarted, this, &ChatForm::startFileSend);
connect(sendButton, &QPushButton::clicked, this, &ChatForm::onSendTriggered);
connect(fileButton, &QPushButton::clicked, this, &ChatForm::onAttachClicked);
connect(screenshotButton, &QPushButton::clicked, this, &ChatForm::onScreenshotClicked);
connect(callButton, &QPushButton::clicked, this, &ChatForm::onCallTriggered);
connect(videoButton, &QPushButton::clicked, this, &ChatForm::onVideoCallTriggered);
connect(msgEdit, &ChatTextEdit::enterPressed, this, &ChatForm::onSendTriggered);
connect(msgEdit, &ChatTextEdit::textChanged, this, &ChatForm::onTextEditChanged);
connect(core, &Core::fileSendFailed, this, &ChatForm::onFileSendFailed);
2016-01-18 21:50:11 +08:00
connect(this, &ChatForm::chatAreaCleared, getOfflineMsgEngine(), &OfflineMsgEngine::removeAllReceipts);
connect(&typingTimer, &QTimer::timeout, this, [=]{
Core::getInstance()->sendTyping(f->getFriendID(), false);
isTyping = false;
} );
connect(nameLabel, &CroppingLabel::editFinished, this, [=](const QString& newName)
{
nameLabel->setText(newName);
emit aliasChanged(newName);
2015-01-02 20:04:59 +08:00
} );
2014-09-26 00:03:25 +08:00
setAcceptDrops(true);
retranslateUi();
Translator::registerHandler(std::bind(&ChatForm::retranslateUi, this), this);
2014-06-25 04:11:11 +08:00
}
ChatForm::~ChatForm()
{
Translator::unregister(this);
delete netcam;
delete callConfirm;
delete offlineEngine;
2014-06-25 04:11:11 +08:00
}
void ChatForm::setStatusMessage(QString newMessage)
{
statusMessageLabel->setText(newMessage);
2015-10-10 04:53:04 +08:00
statusMessageLabel->setToolTip(newMessage.toHtmlEscaped()); // for overlength messsages
2014-06-25 04:11:11 +08:00
}
void ChatForm::onSendTriggered()
{
2015-04-13 02:24:33 +08:00
SendMessageStr(msgEdit->toPlainText());
2014-06-25 04:11:11 +08:00
msgEdit->clear();
}
void ChatForm::onTextEditChanged()
{
if (!Settings::getInstance().isTypingNotificationEnabled())
2015-02-11 12:43:25 +08:00
{
if (isTyping)
Core::getInstance()->sendTyping(f->getFriendID(), false);
2015-04-24 19:01:50 +08:00
2015-02-11 12:43:25 +08:00
isTyping = false;
return;
}
2015-02-11 12:43:25 +08:00
if (msgEdit->toPlainText().length() > 0)
{
typingTimer.start(3000);
2015-02-11 12:43:25 +08:00
if (!isTyping)
Core::getInstance()->sendTyping(f->getFriendID(), (isTyping = true));
}
else
{
Core::getInstance()->sendTyping(f->getFriendID(), (isTyping = false));
}
}
2014-06-26 04:43:28 +08:00
void ChatForm::onAttachClicked()
{
QStringList paths = QFileDialog::getOpenFileNames(this,
tr("Send a file"));
2014-10-17 20:29:48 +08:00
if (paths.isEmpty())
2014-06-26 04:43:28 +08:00
return;
2015-04-24 19:01:50 +08:00
2014-10-17 20:29:48 +08:00
for (QString path : paths)
{
2014-10-17 20:29:48 +08:00
QFile file(path);
if (!file.exists() || !file.open(QIODevice::ReadOnly))
2014-11-28 01:42:04 +08:00
{
QMessageBox::warning(this,
tr("Unable to open"),
tr("qTox wasn't able to open %1").arg(QFileInfo(path).fileName()));
2014-10-17 20:29:48 +08:00
continue;
2014-11-28 01:42:04 +08:00
}
2014-10-17 20:29:48 +08:00
if (file.isSequential())
{
QMessageBox::critical(this,
tr("Bad idea"),
tr("You're trying to send a special (sequential) file, that's not going to work!"));
2014-10-17 20:29:48 +08:00
file.close();
continue;
}
long long filesize = file.size();
file.close();
2014-10-17 20:29:48 +08:00
QFileInfo fi(path);
2014-06-26 04:43:28 +08:00
2014-11-06 23:26:22 +08:00
emit sendFile(f->getFriendID(), fi.fileName(), path, filesize);
2014-10-17 20:29:48 +08:00
}
2014-06-26 04:43:28 +08:00
}
void ChatForm::startFileSend(ToxFile file)
2014-06-26 06:30:24 +08:00
{
2014-11-06 23:26:22 +08:00
if (file.friendId != f->getFriendID())
return;
QString name;
if (!previousId.isSelf())
{
Core* core = Core::getInstance();
name = core->getUsername();
previousId = core->getSelfId();
}
2014-09-07 19:42:06 +08:00
2015-01-05 01:21:35 +08:00
insertChatMessage(ChatMessage::createFileTransferMessage(name, file, true, QDateTime::currentDateTime()));
2015-06-13 00:52:41 +08:00
Widget::getInstance()->updateFriendActivity(f);
}
void ChatForm::onFileRecvRequest(ToxFile file)
{
2014-11-06 23:26:22 +08:00
if (file.friendId != f->getFriendID())
return;
Widget::getInstance()->newFriendMessageAlert(file.friendId);
2014-09-06 03:27:26 +08:00
QString name;
ToxId friendId = f->getToxId();
if (friendId != previousId)
{
2014-11-06 23:26:22 +08:00
name = f->getDisplayedName();
previousId = friendId;
}
2014-09-07 19:42:06 +08:00
2015-01-05 01:21:35 +08:00
ChatMessage::Ptr msg = ChatMessage::createFileTransferMessage(name, file, false, QDateTime::currentDateTime());
insertChatMessage(msg);
2014-10-18 12:53:29 +08:00
ChatLineContentProxy* proxy = static_cast<ChatLineContentProxy*>(msg->getContent(1));
assert(proxy->getWidgetType() == ChatLineContentProxy::FileTransferWidgetType);
FileTransferWidget* tfWidget = static_cast<FileTransferWidget*>(proxy->getWidget());
// there is auto-accept for that conact
if (!Settings::getInstance().getAutoAcceptDir(f->getToxId()).isEmpty())
2014-12-14 04:11:03 +08:00
{
2015-06-08 02:24:55 +08:00
tfWidget->autoAcceptTransfer(Settings::getInstance().getAutoAcceptDir(f->getToxId()));
}
else if (Settings::getInstance().getAutoSaveEnabled())
{ //global autosave to global directory
2015-06-20 20:16:54 +08:00
tfWidget->autoAcceptTransfer(Settings::getInstance().getGlobalAutoAcceptDir());
2014-12-14 04:11:03 +08:00
}
2015-06-13 00:52:41 +08:00
Widget::getInstance()->updateFriendActivity(f);
2014-06-26 06:30:24 +08:00
}
void ChatForm::onAvInvite(uint32_t FriendId, bool video)
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
return;
qDebug() << "onAvInvite";
disableCallButtons();
if (video)
{
2015-06-09 03:31:40 +08:00
callConfirm = new CallConfirmWidget(videoButton, *f);
videoButton->setObjectName("yellow");
videoButton->setToolTip(tr("Accept video call"));
videoButton->style()->polish(videoButton);
connect(videoButton, &QPushButton::clicked, this, &ChatForm::onAnswerCallTriggered);
}
else
{
2015-06-09 03:31:40 +08:00
callConfirm = new CallConfirmWidget(callButton, *f);
callButton->setObjectName("yellow");
callButton->setToolTip(tr("Accept audio call"));
callButton->style()->polish(callButton);
connect(callButton, &QPushButton::clicked, this, &ChatForm::onAnswerCallTriggered);
}
if (f->getFriendWidget()->chatFormIsSet(false))
callConfirm->show();
connect(callConfirm, &CallConfirmWidget::accepted, this, &ChatForm::onAnswerCallTriggered);
connect(callConfirm, &CallConfirmWidget::rejected, this, &ChatForm::onRejectCallTriggered);
insertChatMessage(ChatMessage::createChatInfoMessage(tr("%1 calling").arg(f->getDisplayedName()),
ChatMessage::INFO,
QDateTime::currentDateTime()));
2014-07-01 03:14:51 +08:00
Widget::getInstance()->newFriendMessageAlert(FriendId, false);
Audio& audio = Audio::getInstance();
audio.startLoop();
audio.playMono16Sound(QStringLiteral(":/audio/ToxicIncomingCall.pcm"));
}
void ChatForm::onAvStart(uint32_t FriendId, bool video)
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
return;
qDebug() << "onAvStart";
audioInputFlag = true;
2014-10-28 20:50:30 +08:00
audioOutputFlag = true;
disableCallButtons();
2015-01-03 21:08:04 +08:00
if (video)
{
videoButton->setObjectName("red");
videoButton->setToolTip(tr("End video call"));
2015-10-08 02:11:19 +08:00
videoButton->style()->polish(videoButton);
2015-01-26 06:19:25 +08:00
connect(videoButton, SIGNAL(clicked()),
this, SLOT(onHangupCallTriggered()));
2014-10-16 21:37:48 +08:00
2015-05-10 04:35:58 +08:00
showNetcam();
}
else
{
callButton->setObjectName("red");
callButton->setToolTip(tr("End audio call"));
2015-10-08 02:11:19 +08:00
callButton->style()->polish(callButton);
2015-01-26 06:19:25 +08:00
connect(callButton, SIGNAL(clicked()),
this, SLOT(onHangupCallTriggered()));
hideNetcam();
}
micButton->setObjectName("green");
micButton->style()->polish(micButton);
micButton->setToolTip(tr("Mute microphone"));
volButton->setObjectName("green");
volButton->style()->polish(volButton);
volButton->setToolTip(tr("Mute call"));
connect(micButton, SIGNAL(clicked()),
this, SLOT(onMicMuteToggle()));
connect(volButton, SIGNAL(clicked()),
this, SLOT(onVolMuteToggle()));
2014-11-03 09:51:56 +08:00
startCounter();
}
2015-10-08 02:11:19 +08:00
void ChatForm::onAvEnd(uint32_t FriendId)
{
if (FriendId != f->getFriendID())
return;
qDebug() << "onAvEnd";
delete callConfirm;
callConfirm = nullptr;
Audio::getInstance().stopLoop();
2015-10-08 02:11:19 +08:00
enableCallButtons();
stopCounter();
hideNetcam();
}
void ChatForm::showOutgoingCall(bool video)
{
audioInputFlag = true;
audioOutputFlag = true;
2015-10-08 02:11:19 +08:00
disableCallButtons();
if (video)
{
videoButton->setObjectName("yellow");
videoButton->style()->polish(videoButton);
videoButton->setToolTip(tr("Cancel video call"));
connect(videoButton, &QPushButton::clicked,
this, &ChatForm::onCancelCallTriggered);
}
else
{
callButton->setObjectName("yellow");
callButton->style()->polish(callButton);
callButton->setToolTip(tr("Cancel audio call"));
connect(callButton, &QPushButton::clicked,
this, &ChatForm::onCancelCallTriggered);
}
2015-10-08 02:11:19 +08:00
addSystemInfoMessage(tr("Calling %1").arg(f->getDisplayedName()),
ChatMessage::INFO,
QDateTime::currentDateTime());
2015-06-13 00:52:41 +08:00
Widget::getInstance()->updateFriendActivity(f);
2014-06-27 09:06:56 +08:00
}
void ChatForm::onAnswerCallTriggered()
{
2014-11-03 09:51:56 +08:00
qDebug() << "onAnswerCallTriggered";
if (callConfirm)
{
delete callConfirm;
callConfirm = nullptr;
}
Audio::getInstance().stopLoop();
2015-10-08 02:11:19 +08:00
disableCallButtons();
if (!coreav->answerCall(f->getFriendID()))
{
enableCallButtons();
stopCounter();
hideNetcam();
return;
}
onAvStart(f->getFriendID(), coreav->isCallVideoEnabled(f->getFriendID()));
}
void ChatForm::onHangupCallTriggered()
{
2014-11-03 09:51:56 +08:00
qDebug() << "onHangupCallTriggered";
2015-03-06 19:01:12 +08:00
//Fixes an OS X bug with ending a call while in full screen
2015-05-10 04:35:58 +08:00
if (netcam && netcam->isFullScreen())
2015-03-06 19:01:12 +08:00
netcam->showNormal();
2015-04-24 19:01:50 +08:00
2014-08-30 00:16:16 +08:00
audioInputFlag = false;
2014-10-28 20:50:30 +08:00
audioOutputFlag = false;
coreav->cancelCall(f->getFriendID());
2015-01-26 07:06:44 +08:00
stopCounter();
2015-01-26 07:06:44 +08:00
enableCallButtons();
hideNetcam();
}
void ChatForm::onRejectCallTriggered()
{
qDebug() << "onRejectCallTriggered";
if (callConfirm)
{
delete callConfirm;
callConfirm = nullptr;
}
Audio::getInstance().stopLoop();
audioInputFlag = false;
audioOutputFlag = false;
coreav->cancelCall(f->getFriendID());
2015-01-26 06:19:25 +08:00
enableCallButtons();
stopCounter();
}
void ChatForm::onCallTriggered()
{
2014-11-03 09:51:56 +08:00
qDebug() << "onCallTriggered";
2015-10-08 02:11:19 +08:00
disableCallButtons();
if (coreav->startCall(f->getFriendID(), false))
showOutgoingCall(false);
2015-10-24 08:40:56 +08:00
else
enableCallButtons();
2014-06-27 09:06:56 +08:00
}
void ChatForm::onVideoCallTriggered()
{
2014-11-03 09:51:56 +08:00
qDebug() << "onVideoCallTriggered";
2015-10-08 02:11:19 +08:00
disableCallButtons();
if (coreav->startCall(f->getFriendID(), true))
showOutgoingCall(true);
else
enableCallButtons();
2015-01-26 06:19:25 +08:00
}
void ChatForm::onCancelCallTriggered()
{
qDebug() << "onCancelCallTriggered";
2015-04-24 19:01:50 +08:00
if (!coreav->cancelCall(f->getFriendID()))
qWarning() << "Failed to cancel a call! Assuming we're not in call";
2015-01-26 06:19:25 +08:00
enableCallButtons();
2015-09-28 05:16:40 +08:00
stopCounter();
2015-05-10 04:35:58 +08:00
hideNetcam();
2015-01-26 06:19:25 +08:00
}
void ChatForm::enableCallButtons()
{
qDebug() << "enableCallButtons";
2014-10-25 17:29:25 +08:00
audioInputFlag = false;
2014-10-28 20:50:30 +08:00
audioOutputFlag = false;
disableCallButtons();
if (disableCallButtonsTimer == nullptr)
{
disableCallButtonsTimer = new QTimer();
connect(disableCallButtonsTimer, SIGNAL(timeout()),
this, SLOT(onEnableCallButtons()));
disableCallButtonsTimer->start(1500); // 1.5sec
qDebug() << "timer started!!";
}
}
void ChatForm::disableCallButtons()
{
qDebug() << "disableCallButtons";
// Prevents race enable / disable / onEnable, when it should be disabled
if (disableCallButtonsTimer)
{
disableCallButtonsTimer->stop();
delete disableCallButtonsTimer;
disableCallButtonsTimer = nullptr;
}
2015-01-26 06:19:25 +08:00
micButton->setObjectName("grey");
micButton->style()->polish(micButton);
micButton->setToolTip("");
micButton->disconnect();
2015-01-26 06:19:25 +08:00
volButton->setObjectName("grey");
volButton->style()->polish(volButton);
volButton->setToolTip("");
2015-01-26 06:19:25 +08:00
volButton->disconnect();
2015-01-26 06:19:25 +08:00
callButton->setObjectName("grey");
callButton->style()->polish(callButton);
callButton->setToolTip("");
2014-10-25 17:29:25 +08:00
callButton->disconnect();
2015-01-26 06:19:25 +08:00
videoButton->setObjectName("grey");
videoButton->style()->polish(videoButton);
videoButton->setToolTip("");
2014-10-25 17:29:25 +08:00
videoButton->disconnect();
}
2015-01-26 06:19:25 +08:00
void ChatForm::onEnableCallButtons()
2014-06-27 09:06:56 +08:00
{
2015-01-26 06:19:25 +08:00
qDebug() << "onEnableCallButtons";
2014-08-30 00:16:16 +08:00
audioInputFlag = false;
2014-10-28 20:50:30 +08:00
audioOutputFlag = false;
2014-06-28 13:14:38 +08:00
callButton->setObjectName("green");
callButton->style()->polish(callButton);
callButton->setToolTip(tr("Start audio call"));
videoButton->setObjectName("green");
videoButton->style()->polish(videoButton);
videoButton->setToolTip(tr("Start video call"));
2015-01-26 06:19:25 +08:00
connect(callButton, SIGNAL(clicked()),
this, SLOT(onCallTriggered()));
connect(videoButton, SIGNAL(clicked()),
this, SLOT(onVideoCallTriggered()));
2015-01-26 06:19:25 +08:00
disableCallButtonsTimer->stop();
delete disableCallButtonsTimer;
2015-01-26 07:06:44 +08:00
disableCallButtonsTimer = nullptr;
}
2014-08-19 23:20:20 +08:00
void ChatForm::onMicMuteToggle()
{
2014-08-30 00:16:16 +08:00
if (audioInputFlag == true)
2014-08-19 23:20:20 +08:00
{
coreav->micMuteToggle(f->getFriendID());
2014-08-30 00:16:16 +08:00
if (micButton->objectName() == "red")
{
2014-08-30 00:16:16 +08:00
micButton->setObjectName("green");
micButton->setToolTip(tr("Mute microphone"));
}
2014-08-30 00:16:16 +08:00
else
{
2014-08-30 00:16:16 +08:00
micButton->setObjectName("red");
micButton->setToolTip(tr("Unmute microphone"));
}
2014-10-10 03:20:06 +08:00
Style::repolish(micButton);
2014-08-19 23:20:20 +08:00
}
}
2014-09-06 03:27:26 +08:00
2014-10-28 20:50:30 +08:00
void ChatForm::onVolMuteToggle()
{
if (audioOutputFlag == true)
{
coreav->volMuteToggle(f->getFriendID());
2014-10-28 20:50:30 +08:00
if (volButton->objectName() == "red")
{
2014-10-28 20:50:30 +08:00
volButton->setObjectName("green");
volButton->setToolTip(tr("Mute call"));
}
2014-10-28 20:50:30 +08:00
else
{
2014-10-28 20:50:30 +08:00
volButton->setObjectName("red");
volButton->setToolTip(tr("Unmute call"));
}
2014-10-28 20:50:30 +08:00
Style::repolish(volButton);
}
}
void ChatForm::onFileSendFailed(uint32_t FriendId, const QString &fname)
2014-09-24 00:52:06 +08:00
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
2014-09-24 00:52:06 +08:00
return;
2015-01-05 01:21:35 +08:00
addSystemInfoMessage(tr("Failed to send file \"%1\"").arg(fname), ChatMessage::ERROR, QDateTime::currentDateTime());
2014-09-24 00:52:06 +08:00
}
2014-09-24 23:28:38 +08:00
void ChatForm::onAvatarChange(uint32_t FriendId, const QPixmap &pic)
2014-09-24 23:28:38 +08:00
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
2014-09-24 23:28:38 +08:00
return;
avatar->setPixmap(pic);
2014-09-24 23:28:38 +08:00
}
2014-09-26 00:03:25 +08:00
2015-08-19 01:40:11 +08:00
GenericNetCamView *ChatForm::createNetcam()
{
qDebug() << "creating netcam";
2015-08-19 01:40:11 +08:00
NetCamView* view = new NetCamView(f->getFriendID(), this);
view->show(Core::getInstance()->getAv()->getVideoSourceFromCall(f->getFriendID()), f->getDisplayedName());
2015-08-19 01:40:11 +08:00
return view;
}
2014-09-26 00:03:25 +08:00
void ChatForm::dragEnterEvent(QDragEnterEvent *ev)
{
if (ev->mimeData()->hasUrls())
ev->acceptProposedAction();
}
void ChatForm::dropEvent(QDropEvent *ev)
{
if (ev->mimeData()->hasUrls())
{
for (QUrl url : ev->mimeData()->urls())
{
QFileInfo info(url.path());
QFile file(info.absoluteFilePath());
2015-04-13 02:24:33 +08:00
if (url.isValid() && !url.isLocalFile() && (url.toString().length() < TOX_MAX_MESSAGE_LENGTH))
{
SendMessageStr(url.toString());
continue;
}
if (!file.exists() || !file.open(QIODevice::ReadOnly))
{
info.setFile(url.toLocalFile());
file.setFileName(info.absoluteFilePath());
if (!file.exists() || !file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this, tr("Unable to open"), tr("qTox wasn't able to open %1").arg(info.fileName()));
continue;
}
}
if (file.isSequential())
{
QMessageBox::critical(0, tr("Bad idea"), tr("You're trying to send a special (sequential) file, that's not going to work!"));
file.close();
continue;
}
file.close();
2014-09-26 00:03:25 +08:00
if (info.exists())
2014-11-06 23:26:22 +08:00
Core::getInstance()->sendFile(f->getFriendID(), info.fileName(), info.absoluteFilePath(), info.size());
2014-09-26 00:03:25 +08:00
}
}
}
2014-09-27 03:23:20 +08:00
void ChatForm::onAvatarRemoved(uint32_t FriendId)
2014-09-27 03:23:20 +08:00
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
2014-09-27 03:23:20 +08:00
return;
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"), Qt::transparent);
2014-09-27 03:23:20 +08:00
}
2014-11-10 19:30:56 +08:00
void ChatForm::loadHistory(QDateTime since, bool processUndelivered)
{
2015-02-16 23:48:22 +08:00
QDateTime now = historyBaselineDate.addMSecs(-1);
if (since > now)
return;
2014-10-11 01:32:10 +08:00
2015-01-05 02:29:06 +08:00
if (!earliestMessage.isNull())
{
2015-01-05 02:29:06 +08:00
if (earliestMessage < since)
2014-10-11 01:32:10 +08:00
return;
2015-04-24 19:01:50 +08:00
2015-01-05 02:29:06 +08:00
if (earliestMessage < now)
2014-10-11 01:32:10 +08:00
{
2015-01-05 02:29:06 +08:00
now = earliestMessage;
now = now.addMSecs(-1);
2014-10-11 01:32:10 +08:00
}
}
2014-10-11 01:32:10 +08:00
auto msgs = Nexus::getProfile()->getHistory()->getChatHistory(f->getToxId().publicKey, since, now);
2014-10-11 01:32:10 +08:00
ToxId storedPrevId = previousId;
ToxId prevId;
2015-01-05 02:29:06 +08:00
QList<ChatLine::Ptr> historyMessages;
2014-10-11 01:32:10 +08:00
QDate lastDate(1,0,0);
for (const auto &it : msgs)
{
// Show the date every new day
QDateTime msgDateTime = it.timestamp.toLocalTime();
QDate msgDate = msgDateTime.date();
if (msgDate > lastDate)
{
lastDate = msgDate;
historyMessages.append(ChatMessage::createChatInfoMessage(msgDate.toString(Settings::getInstance().getDateFormat()), ChatMessage::INFO, QDateTime()));
}
// Show each messages
ToxId authorId = ToxId(it.sender);
QString authorStr = !it.dispName.isEmpty() ? it.dispName : (authorId.isSelf() ? Core::getInstance()->getUsername() : resolveToxId(authorId));
2015-05-13 10:18:55 +08:00
bool isAction = it.message.startsWith("/me ", Qt::CaseInsensitive);
2015-01-03 21:08:04 +08:00
2015-01-05 02:29:06 +08:00
ChatMessage::Ptr msg = ChatMessage::createChatMessage(authorStr,
isAction ? it.message.mid(4) : it.message,
2015-02-15 17:51:54 +08:00
isAction ? ChatMessage::ACTION : ChatMessage::NORMAL,
authorId.isSelf(),
2015-01-05 17:02:41 +08:00
QDateTime());
2015-01-05 02:29:06 +08:00
if (!isAction && (prevId == authorId) && (prevMsgDateTime.secsTo(msgDateTime) < getChatLog()->repNameAfter) )
2015-01-05 02:29:06 +08:00
msg->hideSender();
2015-01-05 02:29:06 +08:00
prevId = authorId;
prevMsgDateTime = msgDateTime;
2015-01-05 02:29:06 +08:00
if (it.isSent || !authorId.isSelf())
2014-12-14 04:11:03 +08:00
{
msg->markAsSent(msgDateTime);
}
else
2014-11-10 19:30:56 +08:00
{
if (processUndelivered)
{
int rec;
if (!isAction)
rec = Core::getInstance()->sendMessage(f->getFriendID(), msg->toString());
2014-11-10 19:30:56 +08:00
else
rec = Core::getInstance()->sendAction(f->getFriendID(), msg->toString());
getOfflineMsgEngine()->registerReceipt(rec, it.id, msg);
2014-11-10 19:30:56 +08:00
}
}
historyMessages.append(msg);
}
2015-01-05 02:29:06 +08:00
previousId = storedPrevId;
int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value();
2014-10-11 01:32:10 +08:00
2015-01-05 02:29:06 +08:00
earliestMessage = since;
2014-10-14 13:49:27 +08:00
chatWidget->insertChatlineOnTop(historyMessages);
2014-10-11 01:32:10 +08:00
savedSliderPos = chatWidget->verticalScrollBar()->maximum() - savedSliderPos;
chatWidget->verticalScrollBar()->setValue(savedSliderPos);
}
2014-10-11 01:32:10 +08:00
void ChatForm::onScreenshotClicked()
{
doScreenshot();
// Give the window manager a moment to open the fullscreen grabber window
QTimer::singleShot(500, this, SLOT(hideFileMenu()));
}
void ChatForm::doScreenshot()
{
ScreenshotGrabber* screenshotGrabber = new ScreenshotGrabber(this);
connect(screenshotGrabber, &ScreenshotGrabber::screenshotTaken, this, &ChatForm::onScreenshotTaken);
screenshotGrabber->showGrabber();
2015-05-26 06:33:10 +08:00
// Create dir for screenshots
2015-06-05 22:24:47 +08:00
QDir(Settings::getInstance().getSettingsDirPath()).mkdir("screenshots");
}
void ChatForm::onScreenshotTaken(const QPixmap &pixmap) {
// use ~ISO 8601 for screenshot timestamp, considering FS limitations
// https://en.wikipedia.org/wiki/ISO_8601
// Windows has to be supported, thus filename can't have `:` in it :/
// Format should be: `qTox_Screenshot_yyyy-MM-dd HH-mm-ss.zzz.png`
QString filepath = QString("%1screenshots%2qTox_Screenshot_%3.png")
.arg(Settings::getInstance().getSettingsDirPath())
.arg(QDir::separator())
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH-mm-ss.zzz"));
QFile file(filepath);
if (!file.open(QFile::ReadWrite))
{
QMessageBox::warning(this,
tr("Failed to open temporary file", "Temporary file for screenshot"),
tr("qTox wasn't able to save the screenshot"));
return;
}
pixmap.save(&file, "PNG");
long long filesize = file.size();
file.close();
QFileInfo fi(file);
emit sendFile(f->getFriendID(), fi.fileName(), fi.filePath(), filesize);
}
void ChatForm::onLoadHistory()
{
if (!Nexus::getProfile()->isHistoryEnabled())
return;
LoadHistoryDialog dlg;
if (dlg.exec())
{
QDateTime fromTime = dlg.getFromDate();
loadHistory(fromTime);
}
}
2014-11-03 09:51:56 +08:00
void ChatForm::onMessageInserted()
{
if (netcam && bodySplitter->sizes()[1] == 0)
netcam->setShowMessages(true, true);
}
2014-11-03 09:51:56 +08:00
void ChatForm::startCounter()
{
2015-01-26 06:19:25 +08:00
if (!callDurationTimer)
2014-11-03 09:51:56 +08:00
{
2015-01-26 06:19:25 +08:00
callDurationTimer = new QTimer();
connect(callDurationTimer, SIGNAL(timeout()), this, SLOT(onUpdateTime()));
callDurationTimer->start(1000);
2014-11-03 09:51:56 +08:00
timeElapsed.start();
callDuration->show();
}
}
void ChatForm::stopCounter()
{
2015-01-26 06:19:25 +08:00
if (callDurationTimer)
2014-11-03 09:51:56 +08:00
{
2014-11-16 20:13:39 +08:00
addSystemInfoMessage(tr("Call with %1 ended. %2").arg(f->getDisplayedName(),secondsToDHMS(timeElapsed.elapsed()/1000)),
2015-01-05 01:21:35 +08:00
ChatMessage::INFO, QDateTime::currentDateTime());
2015-01-26 06:19:25 +08:00
callDurationTimer->stop();
callDuration->setText("");
2014-11-03 09:51:56 +08:00
callDuration->hide();
2015-01-26 06:19:25 +08:00
delete callDurationTimer;
2015-01-26 07:06:44 +08:00
callDurationTimer = nullptr;
2014-11-03 09:51:56 +08:00
}
}
2015-01-26 06:19:25 +08:00
void ChatForm::onUpdateTime()
2014-11-03 09:51:56 +08:00
{
callDuration->setText(secondsToDHMS(timeElapsed.elapsed()/1000));
}
QString ChatForm::secondsToDHMS(quint32 duration)
{
QString res;
QString cD = tr("Call duration: ");
int seconds = (int) (duration % 60);
duration /= 60;
int minutes = (int) (duration % 60);
duration /= 60;
int hours = (int) (duration % 24);
int days = (int) (duration / 24);
// I assume no one will ever have call longer than a month
if (days)
return cD + res.sprintf("%dd%02dh %02dm %02ds", days, hours, minutes, seconds);
else if (hours)
2014-11-03 09:51:56 +08:00
return cD + res.sprintf("%02dh %02dm %02ds", hours, minutes, seconds);
else if (minutes)
return cD + res.sprintf("%02dm %02ds", minutes, seconds);
else
return cD + res.sprintf("%02ds", seconds);
2014-11-03 09:51:56 +08:00
}
2014-11-09 20:32:19 +08:00
void ChatForm::setFriendTyping(bool isTyping)
{
2015-01-10 18:57:46 +08:00
chatWidget->setTypingNotificationVisible(isTyping);
2015-06-08 02:24:55 +08:00
Text* text = static_cast<Text*>(chatWidget->getTypingNotification()->getContent(1));
text->setText("<div class=typing>" + QString("%1 is typing").arg(f->getDisplayedName().toHtmlEscaped()) + "</div>");
}
void ChatForm::show(ContentLayout* contentLayout)
{
GenericChatForm::show(contentLayout);
if (callConfirm)
callConfirm->show();
}
2015-06-07 03:26:09 +08:00
void ChatForm::showEvent(QShowEvent* event)
{
if (callConfirm)
callConfirm->show();
GenericChatForm::showEvent(event);
}
void ChatForm::hideEvent(QHideEvent* event)
{
if (callConfirm)
callConfirm->hide();
GenericChatForm::hideEvent(event);
}
OfflineMsgEngine *ChatForm::getOfflineMsgEngine()
{
return offlineEngine;
}
2015-04-13 02:24:33 +08:00
void ChatForm::SendMessageStr(QString msg)
{
if (msg.isEmpty())
return;
2015-05-13 10:18:55 +08:00
bool isAction = msg.startsWith("/me ", Qt::CaseInsensitive);
2015-04-13 02:24:33 +08:00
if (isAction)
msg = msg = msg.right(msg.length() - 4);
QList<CString> splittedMsg = Core::splitMessage(msg, TOX_MAX_MESSAGE_LENGTH);
QDateTime timestamp = QDateTime::currentDateTime();
for (CString& c_msg : splittedMsg)
{
QString qt_msg = CString::toString(c_msg.data(), c_msg.size());
QString qt_msg_hist = qt_msg;
if (isAction)
qt_msg_hist = "/me " + qt_msg;
bool status = !Settings::getInstance().getFauxOfflineMessaging();
ChatMessage::Ptr ma = addSelfMessage(qt_msg, isAction, timestamp, false);
2015-04-13 02:24:33 +08:00
int rec;
if (isAction)
rec = Core::getInstance()->sendAction(f->getFriendID(), qt_msg);
else
rec = Core::getInstance()->sendMessage(f->getFriendID(), qt_msg);
Profile* profile = Nexus::getProfile();
if (profile->isHistoryEnabled())
{
auto* offMsgEngine = getOfflineMsgEngine();
profile->getHistory()->addNewMessage(f->getToxId().publicKey, qt_msg_hist,
Core::getInstance()->getSelfId().publicKey, timestamp, status, Core::getInstance()->getUsername(),
[offMsgEngine,rec,ma](int64_t id)
{
offMsgEngine->registerReceipt(rec, id, ma);
});
}
else
{
/// TODO: Make faux-offline messaging work partially with the history disabled
ma->markAsSent(QDateTime::currentDateTime());
}
2015-04-13 02:24:33 +08:00
msgEdit->setLastMessage(msg); //set last message only when sending it
Widget::getInstance()->updateFriendActivity(f);
2015-04-13 02:24:33 +08:00
}
}
2015-05-10 04:35:58 +08:00
void ChatForm::retranslateUi()
{
QString volObjectName = volButton->objectName();
QString micObjectName = micButton->objectName();
loadHistoryAction->setText(tr("Load chat history..."));
if (volObjectName == QStringLiteral("green"))
volButton->setToolTip(tr("Mute call"));
else if (volObjectName == QStringLiteral("red"))
volButton->setToolTip(tr("Unmute call"));
if (micObjectName == QStringLiteral("green"))
micButton->setToolTip(tr("Mute microphone"));
else if (micObjectName == QStringLiteral("red"))
micButton->setToolTip(tr("Unmute microphone"));
2015-10-10 20:49:30 +08:00
if (netcam)
netcam->setShowMessages(chatWidget->isVisible());
}