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

1090 lines
31 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 <cassert>
2014-06-25 04:11:11 +08:00
#include "chatform.h"
#include "src/core/core.h"
2014-10-08 12:26:25 +08:00
#include "src/friend.h"
2015-06-06 07:44:47 +08:00
#include "src/persistence/historykeeper.h"
#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"
2015-06-06 07:44:47 +08:00
#include "src/video/netcamview.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-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"
2014-06-25 04:11:11 +08:00
ChatForm::ChatForm(Friend* chatFriend)
: f(chatFriend)
, callId{0}, isTyping{false}
2014-06-25 04:11:11 +08:00
{
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-05-10 04:35:58 +08:00
netcam = nullptr;
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
loadHistoryAction = menu.addAction(QString(), this, SLOT(onLoadHistory()));
2014-09-11 21:44:34 +08:00
connect(Core::getInstance(), &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);
2014-09-24 00:52:06 +08:00
connect(Core::getInstance(), &Core::fileSendFailed, this, &ChatForm::onFileSendFailed);
connect(this, &ChatForm::chatAreaCleared, getOfflineMsgEngine(), &OfflineMsgEngine::removeAllReciepts);
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);
statusMessageLabel->setToolTip(newMessage); // 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-11-09 20:32:19 +08:00
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.isActiveProfile())
{
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, int CallId, bool video)
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
return;
qDebug() << "onAvInvite";
callId = CallId;
callButton->disconnect();
videoButton->disconnect();
if (video)
{
2015-06-09 03:31:40 +08:00
callConfirm = new CallConfirmWidget(videoButton, *f);
if (f->getFriendWidget()->chatFormIsSet(false))
callConfirm->show();
2015-04-24 19:01:50 +08:00
connect(callConfirm, &CallConfirmWidget::accepted, this, &ChatForm::onAnswerCallTriggered);
connect(callConfirm, &CallConfirmWidget::rejected, this, &ChatForm::onRejectCallTriggered);
callButton->setObjectName("grey");
callButton->setToolTip("");
videoButton->setObjectName("yellow");
videoButton->setToolTip(tr("Accept video call"));
connect(videoButton, &QPushButton::clicked, this, &ChatForm::onAnswerCallTriggered);
}
else
{
2015-06-09 03:31:40 +08:00
callConfirm = new CallConfirmWidget(callButton, *f);
if (f->getFriendWidget()->chatFormIsSet(false))
callConfirm->show();
2015-04-24 19:01:50 +08:00
connect(callConfirm, &CallConfirmWidget::accepted, this, &ChatForm::onAnswerCallTriggered);
connect(callConfirm, &CallConfirmWidget::rejected, this, &ChatForm::onRejectCallTriggered);
callButton->setObjectName("yellow");
callButton->setToolTip(tr("Accept audio call"));
videoButton->setObjectName("grey");
videoButton->setToolTip("");
connect(callButton, &QPushButton::clicked, this, &ChatForm::onAnswerCallTriggered);
}
2015-01-26 06:19:25 +08:00
callButton->style()->polish(callButton);
videoButton->style()->polish(videoButton);
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);
}
void ChatForm::onAvStart(uint32_t FriendId, int CallId, 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;
callId = CallId;
callButton->disconnect();
videoButton->disconnect();
2015-01-03 21:08:04 +08:00
if (video)
{
callButton->setObjectName("grey");
callButton->setToolTip("");
videoButton->setObjectName("red");
videoButton->setToolTip(tr("End video call"));
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"));
videoButton->setObjectName("grey");
videoButton->setToolTip("");
2015-01-26 06:19:25 +08:00
connect(callButton, SIGNAL(clicked()),
this, SLOT(onHangupCallTriggered()));
}
2015-01-26 06:19:25 +08:00
callButton->style()->polish(callButton);
videoButton->style()->polish(videoButton);
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();
}
void ChatForm::onAvCancel(uint32_t FriendId, int)
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
return;
qDebug() << "onAvCancel";
delete callConfirm;
callConfirm = nullptr;
2015-01-26 06:19:25 +08:00
enableCallButtons();
2015-02-24 07:08:07 +08:00
stopCounter();
2014-10-16 21:37:48 +08:00
2015-05-10 04:35:58 +08:00
hideNetcam();
addSystemInfoMessage(tr("%1 stopped calling").arg(f->getDisplayedName()),
ChatMessage::INFO,
QDateTime::currentDateTime());
}
void ChatForm::onAvEnd(uint32_t FriendId, int)
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
return;
qDebug() << "onAvEnd";
delete callConfirm;
callConfirm = nullptr;
2015-01-26 06:19:25 +08:00
enableCallButtons();
2015-02-24 07:08:07 +08:00
stopCounter();
2015-05-10 04:35:58 +08:00
hideNetcam();
}
void ChatForm::onAvRinging(uint32_t FriendId, int CallId, bool video)
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
2014-06-27 09:06:56 +08:00
return;
qDebug() << "onAvRinging";
2014-06-27 09:06:56 +08:00
callId = CallId;
callButton->disconnect();
videoButton->disconnect();
if (video)
{
callButton->setObjectName("grey");
callButton->style()->polish(callButton);
callButton->setToolTip("");
videoButton->setObjectName("yellow");
videoButton->style()->polish(videoButton);
videoButton->setToolTip(tr("Cancel video call"));
2015-01-26 06:19:25 +08:00
connect(videoButton, SIGNAL(clicked()),
this, SLOT(onCancelCallTriggered()));
}
else
{
callButton->setObjectName("yellow");
callButton->style()->polish(callButton);
callButton->setToolTip(tr("Cancel audio call"));
videoButton->setObjectName("grey");
videoButton->style()->polish(videoButton);
videoButton->setToolTip("");
2015-01-26 06:19:25 +08:00
connect(callButton, SIGNAL(clicked()),
this, SLOT(onCancelCallTriggered()));
}
addSystemInfoMessage(tr("Calling to %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::onAvStarting(uint32_t FriendId, int CallId, bool video)
2014-06-27 09:06:56 +08:00
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
2014-06-27 09:06:56 +08:00
return;
qDebug() << "onAvStarting";
2015-05-14 10:46:28 +08:00
callId = CallId;
2014-06-27 09:06:56 +08:00
callButton->disconnect();
videoButton->disconnect();
if (video)
{
callButton->setObjectName("grey");
callButton->style()->polish(callButton);
callButton->setToolTip("");
videoButton->setObjectName("red");
videoButton->style()->polish(videoButton);
videoButton->setToolTip(tr("End video call"));
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->style()->polish(callButton);
callButton->setToolTip(tr("End audio call"));
videoButton->setObjectName("grey");
videoButton->style()->polish(videoButton);
videoButton->setToolTip("");
connect(callButton, SIGNAL(clicked()), this, SLOT(onHangupCallTriggered()));
}
2014-11-03 09:51:56 +08:00
startCounter();
2014-06-27 09:06:56 +08:00
}
void ChatForm::onAvEnding(uint32_t FriendId, int)
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
return;
qDebug() << "onAvEnding";
delete callConfirm;
callConfirm = nullptr;
2015-01-26 06:19:25 +08:00
enableCallButtons();
2015-02-24 07:08:07 +08:00
stopCounter();
2015-05-10 04:35:58 +08:00
hideNetcam();
}
void ChatForm::onAvRequestTimeout(uint32_t FriendId, int)
2014-06-27 09:06:56 +08:00
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
2014-06-27 09:06:56 +08:00
return;
qDebug() << "onAvRequestTimeout";
delete callConfirm;
callConfirm = nullptr;
2015-01-26 06:19:25 +08:00
enableCallButtons();
2015-02-24 07:08:07 +08:00
stopCounter();
2015-05-10 04:35:58 +08:00
hideNetcam();
2014-06-27 09:06:56 +08:00
}
void ChatForm::onAvPeerTimeout(uint32_t FriendId, int)
2014-11-02 19:47:42 +08:00
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
2014-11-02 19:47:42 +08:00
return;
qDebug() << "onAvPeerTimeout";
delete callConfirm;
callConfirm = nullptr;
2015-01-26 06:19:25 +08:00
enableCallButtons();
2015-02-24 07:08:07 +08:00
stopCounter();
2015-05-10 04:35:58 +08:00
hideNetcam();
2014-11-02 19:47:42 +08:00
}
void ChatForm::onAvRejected(uint32_t FriendId, int)
2014-06-28 03:59:25 +08:00
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
2014-06-28 03:59:25 +08:00
return;
qDebug() << "onAvRejected";
delete callConfirm;
callConfirm = nullptr;
2015-01-26 06:19:25 +08:00
enableCallButtons();
insertChatMessage(ChatMessage::createChatInfoMessage(tr("Call rejected"),
ChatMessage::INFO,
QDateTime::currentDateTime()));
2014-10-16 21:37:48 +08:00
2015-05-10 04:35:58 +08:00
hideNetcam();
2014-06-28 03:59:25 +08:00
}
void ChatForm::onAvMediaChange(uint32_t FriendId, int CallId, bool video)
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID() || CallId != callId)
return;
2014-10-16 21:37:48 +08:00
qDebug() << "onAvMediaChange";
if (video)
2015-05-10 04:35:58 +08:00
showNetcam();
else
2015-05-10 04:35:58 +08:00
hideNetcam();
}
void ChatForm::onAnswerCallTriggered()
{
2014-11-03 09:51:56 +08:00
qDebug() << "onAnswerCallTriggered";
if (callConfirm)
{
delete callConfirm;
callConfirm = nullptr;
}
2014-08-30 00:16:16 +08:00
audioInputFlag = true;
2014-10-28 20:50:30 +08:00
audioOutputFlag = true;
emit answerCall(callId);
}
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;
emit hangupCall(callId);
2015-01-26 07:06:44 +08:00
enableCallButtons();
}
void ChatForm::onRejectCallTriggered()
{
qDebug() << "onRejectCallTriggered";
if (callConfirm)
{
delete callConfirm;
callConfirm = nullptr;
}
audioInputFlag = false;
audioOutputFlag = false;
emit rejectCall(callId);
2015-01-26 06:19:25 +08:00
enableCallButtons();
}
void ChatForm::onCallTriggered()
{
2014-11-03 09:51:56 +08:00
qDebug() << "onCallTriggered";
2014-10-28 20:50:30 +08:00
audioInputFlag = true;
audioOutputFlag = true;
callButton->disconnect();
videoButton->disconnect();
emit startCall(f->getFriendID(), false);
2014-06-27 09:06:56 +08:00
}
void ChatForm::onVideoCallTriggered()
{
2014-11-03 09:51:56 +08:00
qDebug() << "onVideoCallTriggered";
2014-08-30 00:16:16 +08:00
audioInputFlag = true;
2014-10-28 20:50:30 +08:00
audioOutputFlag = true;
callButton->disconnect();
videoButton->disconnect();
emit startCall(f->getFriendID(), true);
}
void ChatForm::onAvCallFailed(uint32_t FriendId)
2014-10-25 17:29:25 +08:00
{
2014-11-06 23:26:22 +08:00
if (FriendId != f->getFriendID())
2014-10-25 17:29:25 +08:00
return;
qDebug() << "onAvCallFailed";
delete callConfirm;
callConfirm = nullptr;
2015-01-26 06:19:25 +08:00
enableCallButtons();
}
void ChatForm::onCancelCallTriggered()
{
qDebug() << "onCancelCallTriggered";
2015-04-24 19:01:50 +08:00
2015-01-26 06:19:25 +08:00
enableCallButtons();
2015-05-10 04:35:58 +08:00
hideNetcam();
2015-01-26 06:19:25 +08:00
emit cancelCall(callId, f->getFriendID());
}
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;
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();
if (disableCallButtonsTimer == nullptr)
2015-01-26 06:19:25 +08:00
{
disableCallButtonsTimer = new QTimer();
connect(disableCallButtonsTimer, SIGNAL(timeout()),
this, SLOT(onEnableCallButtons()));
disableCallButtonsTimer->start(1500); // 1.5sec
qDebug() << "timer started!!";
}
2014-10-25 17:29:25 +08:00
}
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
{
2014-08-30 00:16:16 +08:00
emit micMuteToggle(callId);
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)
{
emit volMuteToggle(callId);
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
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 = HistoryKeeper::getInstance()->getChatHistory(HistoryKeeper::ctSingle, 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);
2015-08-28 05:07:24 +08:00
QString authorStr = !it.dispName.isEmpty() ? it.dispName : (authorId.isActiveProfile() ? 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.right(it.message.length() - 4) : it.message,
2015-02-15 17:51:54 +08:00
isAction ? ChatMessage::ACTION : ChatMessage::NORMAL,
authorId.isActiveProfile(),
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.isActiveProfile())
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) {
2015-06-05 22:24:47 +08:00
QTemporaryFile file(Settings::getInstance().getSettingsDirPath()+"screenshots"+QDir::separator()+"qTox-Screenshot-XXXXXXXX.png");
if (!file.open())
{
QMessageBox::warning(this,
tr("Failed to open temporary file", "Temporary file for screenshot"),
tr("qTox wasn't able to save the screenshot"));
return;
}
file.setAutoRemove(false);
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()
{
LoadHistoryDialog dlg;
if (dlg.exec())
{
QDateTime fromTime = dlg.getFromDate();
loadHistory(fromTime);
}
}
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);
if (minutes == 0)
2014-11-03 09:51:56 +08:00
return cD + res.sprintf("%02ds", seconds);
if (hours == 0 && days == 0)
2014-11-03 09:51:56 +08:00
return cD + res.sprintf("%02dm %02ds", minutes, seconds);
2014-11-03 09:51:56 +08:00
if (days == 0)
return cD + res.sprintf("%02dh %02dm %02ds", hours, minutes, seconds);
//I assume no one will ever have call longer than ~30days
return cD + res.sprintf("%dd%02dh %02dm %02ds", days, hours, minutes, seconds);
}
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()) + "</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();
int id = HistoryKeeper::getInstance()->addChatEntry(f->getToxId().publicKey, qt_msg_hist,
2015-08-28 05:07:24 +08:00
Core::getInstance()->getSelfId().publicKey, timestamp, status, Core::getInstance()->getUsername());
2015-04-13 02:24:33 +08:00
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);
getOfflineMsgEngine()->registerReceipt(rec, id, ma);
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::showNetcam()
{
if (!netcam)
netcam = new NetCamView();
netcam->show(Core::getInstance()->getVideoSourceFromCall(callId), f->getDisplayedName());
}
void ChatForm::hideNetcam()
{
if (!netcam)
return;
netcam->hide();
delete netcam;
netcam = nullptr;
}
void ChatForm::retranslateUi()
{
loadHistoryAction->setText(tr("Load chat history..."));
}