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

1204 lines
34 KiB
C++
Raw Normal View History

2014-07-07 00:19:45 +08:00
/*
Copyright © 2014-2015 by The qTox Project Contributors
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
*/
#include "chatform.h"
#include "src/audio/audio.h"
#include "src/chatlog/chatlinecontentproxy.h"
#include "src/chatlog/chatlog.h"
#include "src/chatlog/chatmessage.h"
#include "src/chatlog/content/filetransferwidget.h"
#include "src/chatlog/content/text.h"
#include "src/core/core.h"
#include "src/core/coreav.h"
#include "src/core/cstring.h"
#include "src/friend.h"
#include "src/nexus.h"
#include "src/persistence/offlinemsgengine.h"
#include "src/persistence/profile.h"
#include "src/persistence/settings.h"
#include "src/video/camerasource.h"
#include "src/video/netcamview.h"
#include "src/video/videosource.h"
#include "src/widget/form/loadhistorydialog.h"
#include "src/widget/friendwidget.h"
#include "src/widget/maskablepixmapwidget.h"
#include "src/widget/style.h"
#include "src/widget/tool/callconfirmwidget.h"
#include "src/widget/tool/chattextedit.h"
#include "src/widget/tool/croppinglabel.h"
#include "src/widget/tool/flyoutoverlaywidget.h"
#include "src/widget/tool/screenshotgrabber.h"
#include "src/widget/translator.h"
#include "src/widget/widget.h"
#include <QApplication>
#include <QBitmap>
2015-06-11 00:11:50 +08:00
#include <QBoxLayout>
#include <QClipboard>
#include <QDebug>
#include <QDragEnterEvent>
2014-09-11 21:44:34 +08:00
#include <QFileDialog>
#include <QFileInfo>
2014-09-11 21:44:34 +08:00
#include <QMessageBox>
2014-09-26 00:03:25 +08:00
#include <QMimeData>
#include <QPushButton>
#include <QScreen>
#include <QScrollBar>
#include <QSplitter>
#include <QStyle>
#include <QTemporaryFile>
2015-06-08 02:24:55 +08:00
#include <cassert>
2014-06-25 04:11:11 +08:00
const QString ChatForm::ACTION_PREFIX = QStringLiteral("/me ");
2014-06-25 04:11:11 +08:00
ChatForm::ChatForm(Friend* chatFriend)
: f(chatFriend)
, callDuration(new QLabel(this))
, isTyping(false)
2014-06-25 04:11:11 +08:00
{
2014-11-06 23:26:22 +08:00
nameLabel->setText(f->getDisplayedName());
2016-01-21 11:47:26 +08:00
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"));
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);
statusMessageLabel->setContextMenuPolicy(Qt::CustomContextMenu);
callConfirm = nullptr;
offlineEngine = new OfflineMsgEngine(f);
typingTimer.setSingleShot(true);
2015-01-26 06:19:25 +08:00
callDurationTimer = nullptr;
chatWidget->setTypingNotification(ChatMessage::createTypingNotification());
chatWidget->setMinimumHeight(50);
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()));
copyStatusAction = statusMessageMenu.addAction(QString(), this, SLOT(onCopyStatusMessage()));
const Core* core = Core::getInstance();
connect(core, &Core::fileReceiveRequested,
this, &ChatForm::onFileRecvRequest);
connect(core, &Core::friendAvatarChanged,
this, &ChatForm::onAvatarChange);
connect(core, &Core::friendAvatarRemoved,
this, &ChatForm::onAvatarRemoved);
connect(core, &Core::fileSendStarted,
this, &ChatForm::startFileSend);
connect(core, &Core::fileSendFailed,
this, &ChatForm::onFileSendFailed);
connect(core, &Core::receiptRecieved,
this, &ChatForm::onReceiptReceived);
connect(core, &Core::friendMessageReceived,
this, &ChatForm::onFriendMessageReceived);
connect(core, &Core::friendTypingChanged,
this, &ChatForm::onFriendTypingChanged);
connect(core, &Core::friendStatusChanged,
this, &ChatForm::onFriendStatusChanged);
const CoreAV* av = core->getAv();
connect(av, &CoreAV::avInvite, this, &ChatForm::onAvInvite);
connect(av, &CoreAV::avStart, this, &ChatForm::onAvStart);
connect(av, &CoreAV::avEnd, this, &ChatForm::onAvEnd);
connect(sendButton, &QPushButton::clicked,
this, &ChatForm::onSendTriggered);
connect(fileButton, &QPushButton::clicked,
this, &ChatForm::onAttachClicked);
connect(screenshotButton, &QPushButton::clicked,
this, &ChatForm::onScreenshotClicked);
connect(callButton, &QAbstractButton::clicked,
this, &ChatForm::onCallTriggered);
connect(videoButton, &QAbstractButton::clicked,
this, &ChatForm::onVideoCallTriggered);
connect(micButton, &QAbstractButton::clicked,
this, &ChatForm::onMicMuteToggle);
connect(volButton, &QAbstractButton::clicked,
this, &ChatForm::onVolMuteToggle);
connect(msgEdit, &ChatTextEdit::enterPressed,
this, &ChatForm::onSendTriggered);
connect(msgEdit, &ChatTextEdit::textChanged,
this, &ChatForm::onTextEditChanged);
connect(statusMessageLabel, &CroppingLabel::customContextMenuRequested,
this, [&](const QPoint& pos) {
2016-07-07 17:55:53 +08:00
if (!statusMessageLabel->text().isEmpty())
{
QWidget* sender = static_cast<QWidget*>(this->sender());
statusMessageMenu.exec(sender->mapToGlobal(pos));
}
});
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);
});
2014-09-26 00:03:25 +08:00
updateCallButtons();
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;
2014-06-25 04:11:11 +08:00
}
void ChatForm::setStatusMessage(const QString& newMessage)
2014-06-25 04:11:11 +08:00
{
statusMessageLabel->setText(newMessage);
// for long messsages
statusMessageLabel->setToolTip(Qt::convertFromPlainText(newMessage, Qt::WhiteSpaceNormal));
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()
{
Core* core = Core::getInstance();
if (!Settings::getInstance().getTypingNotification())
2015-02-11 12:43:25 +08:00
{
if (isTyping)
{
core->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)
{
isTyping = true;
core->sendTyping(f->getFriendId(), isTyping);
}
2015-02-11 12:43:25 +08:00
}
else
{
isTyping = false;
core->sendTyping(f->getFriendId(), isTyping);
2015-02-11 12:43:25 +08:00
}
}
2014-06-26 04:43:28 +08:00
void ChatForm::onAttachClicked()
{
QStringList paths = QFileDialog::getOpenFileNames(
this, tr("Send a file"), QDir::homePath(),
0, 0, QFileDialog::DontUseNativeDialog);
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
Core* core = Core::getInstance();
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
{
QString fileName = QFileInfo(path).fileName();
QMessageBox::warning(
this, tr("Unable to open"),
tr("qTox wasn't able to open %1").arg(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 sequential file,"
" which is not going to work!"));
2014-10-17 20:29:48 +08:00
file.close();
continue;
}
qint64 filesize = file.size();
file.close();
2014-10-17 20:29:48 +08:00
QFileInfo fi(path);
2014-06-26 04:43:28 +08:00
core->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
{
if (file.friendId != f->getFriendId())
{
return;
}
QString name;
const Core* core = Core::getInstance();
ToxPk self = core->getSelfId().getPublicKey();
if (previousId != self)
{
name = core->getUsername();
previousId = self;
}
2014-09-07 19:42:06 +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)
{
if (file.friendId != f->getFriendId())
{
return;
}
Widget::getInstance()->newFriendMessageAlert(file.friendId);
2014-09-06 03:27:26 +08:00
QString name;
ToxPk friendId = f->getPublicKey();
if (friendId != previousId)
{
2014-11-06 23:26:22 +08:00
name = f->getDisplayedName();
previousId = friendId;
}
2014-09-07 19:42:06 +08:00
ChatMessage::Ptr msg = ChatMessage::createFileTransferMessage(
name, file, false, QDateTime::currentDateTime());
2015-01-05 01:21:35 +08:00
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->getPublicKey()).isEmpty())
2014-12-14 04:11:03 +08:00
{
tfWidget->autoAcceptTransfer(Settings::getInstance().getAutoAcceptDir(f->getPublicKey()));
}
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)
{
if (friendId != f->getFriendId())
{
return;
}
callConfirm = new CallConfirmWidget(video ? videoButton : callButton, *f);
insertChatMessage(ChatMessage::createChatInfoMessage(
tr("%1 calling").arg(f->getDisplayedName()),
ChatMessage::INFO, QDateTime::currentDateTime()));
/* AutoAcceptCall is set for this friend */
if ((video && Settings::getInstance().getAutoAcceptCall(f->getPublicKey()).testFlag(Settings::AutoAcceptCall::Video)) ||
(!video && Settings::getInstance().getAutoAcceptCall(f->getPublicKey()).testFlag(Settings::AutoAcceptCall::Audio)))
{
uint32_t friendId = f->getFriendId();
qDebug() << "automatic call answer";
CoreAV* coreav = Core::getInstance()->getAv();
QMetaObject::invokeMethod(coreav, "answerCall", Qt::QueuedConnection,
Q_ARG(uint32_t, friendId));
onAvStart(friendId,video);
}
else
{
callConfirm->show();
connect(callConfirm.data(), &CallConfirmWidget::accepted,
this, &ChatForm::onAnswerCallTriggered);
connect(callConfirm.data(), &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();
2016-10-26 17:17:08 +08:00
audio.playMono16Sound(Audio::getSound(Audio::Sound::IncomingCall));
}
}
void ChatForm::onAvStart(uint32_t friendId, bool video)
{
if (friendId != f->getFriendId())
{
return;
}
if (video)
{
2015-05-10 04:35:58 +08:00
showNetcam();
}
else
{
hideNetcam();
}
updateCallButtons();
2014-11-03 09:51:56 +08:00
startCounter();
}
void ChatForm::onAvEnd(uint32_t friendId)
2015-10-08 02:11:19 +08:00
{
if (friendId != f->getFriendId())
{
2015-10-08 02:11:19 +08:00
return;
}
2015-10-08 02:11:19 +08:00
delete callConfirm;
//Fixes an OS X bug with ending a call while in full screen
if (netcam && netcam->isFullScreen())
{
netcam->showNormal();
}
2015-10-08 02:11:19 +08:00
Audio::getInstance().stopLoop();
updateCallButtons();
2015-10-08 02:11:19 +08:00
stopCounter();
hideNetcam();
}
void ChatForm::showOutgoingCall(bool video)
{
if (video)
{
videoButton->setObjectName("yellow");
videoButton->setStyleSheet(Style::getStylesheet(QStringLiteral(":/ui/videoButton/videoButton.css")));
videoButton->setToolTip(tr("Cancel video call"));
}
else
{
callButton->setObjectName("yellow");
callButton->setStyleSheet(Style::getStylesheet(QStringLiteral(":/ui/callButton/callButton.css")));
callButton->setToolTip(tr("Cancel audio call"));
}
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()
{
delete callConfirm;
Audio::getInstance().stopLoop();
updateCallButtons();
2015-10-08 02:11:19 +08:00
CoreAV* av = Core::getInstance()->getAv();
if (!av->answerCall(f->getFriendId()))
{
updateCallButtons();
stopCounter();
hideNetcam();
return;
}
onAvStart(f->getFriendId(), av->isCallVideoEnabled(f));
}
void ChatForm::onRejectCallTriggered()
{
delete callConfirm;
Audio::getInstance().stopLoop();
CoreAV* av = Core::getInstance()->getAv();
av->cancelCall(f->getFriendId());
}
void ChatForm::onCallTriggered()
{
CoreAV* av = Core::getInstance()->getAv();
if (av->isCallStarted(f))
{
av->cancelCall(f->getFriendId());
}
else if (av->startCall(f->getFriendId(), false))
{
showOutgoingCall(false);
}
2014-06-27 09:06:56 +08:00
}
void ChatForm::onVideoCallTriggered()
{
CoreAV* av = Core::getInstance()->getAv();
if (av->isCallStarted(f))
{
// TODO: We want to activate video on the active call.
if (av->isCallVideoEnabled(f))
{
av->cancelCall(f->getFriendId());
}
}
else if (av->startCall(f->getFriendId(), true))
{
showOutgoingCall(true);
}
2015-01-26 06:19:25 +08:00
}
void ChatForm::updateCallButtons()
2015-01-26 06:19:25 +08:00
{
CoreAV* av = Core::getInstance()->getAv();
bool audio = av->isCallActive(f);
bool video = av->isCallVideoEnabled(f);
callButton->setEnabled(audio && !video);
videoButton->setEnabled(video);
if (audio)
{
videoButton->setObjectName(video ? "red" : "");
videoButton->setToolTip(video ? tr("End video call") :
tr("Can't start video call"));
2015-04-24 19:01:50 +08:00
callButton->setObjectName((audio && !video) ? "red" : "");
callButton->setToolTip((audio && !video) ? tr("End audio call") :
tr("Can't start audio call"));
}
else
{
const Status fs = f->getStatus();
bool online = fs != Status::Offline;
callButton->setEnabled(online);
videoButton->setEnabled(online);
callButton->setObjectName(online ? "green" : "");
callButton->setToolTip(online ? tr("Start audio call") :
tr("Can't start audio call"));
videoButton->setObjectName(online ? "green" : "");
videoButton->setToolTip(online ? tr("Start video call") :
tr("Can't start audio call"));
}
callButton->setStyleSheet(Style::getStylesheet(QStringLiteral(":/ui/callButton/callButton.css")));
videoButton->setStyleSheet(Style::getStylesheet(QStringLiteral(":/ui/videoButton/videoButton.css")));
updateMuteMicButton();
updateMuteVolButton();
2015-01-26 06:19:25 +08:00
}
void ChatForm::onMicMuteToggle()
2015-01-26 06:19:25 +08:00
{
CoreAV* av = Core::getInstance()->getAv();
av->toggleMuteCallInput(f);
updateMuteMicButton();
}
void ChatForm::onVolMuteToggle()
{
CoreAV* av = Core::getInstance()->getAv();
av->toggleMuteCallOutput(f);
updateMuteVolButton();
}
void ChatForm::onFileSendFailed(uint32_t friendId, const QString &fname)
{
if (friendId != f->getFriendId())
{
return;
}
addSystemInfoMessage(tr("Failed to send file \"%1\"").arg(fname),
ChatMessage::ERROR, QDateTime::currentDateTime());
2014-10-25 17:29:25 +08:00
}
void ChatForm::onFriendStatusChanged(uint32_t friendId, Status status)
2014-06-27 09:06:56 +08:00
{
// Disable call buttons if friend is offline
if(friendId != f->getFriendId())
{
return;
}
if (status == Status::Offline)
{
// Hide the "is typing" message when a friend goes offline
setFriendTyping(false);
}
else
2014-08-19 23:20:20 +08:00
{
QTimer::singleShot(250, this, SLOT(onDeliverOfflineMessages()));
2014-08-19 23:20:20 +08:00
}
2014-09-06 03:27:26 +08:00
updateCallButtons();
if (Settings::getInstance().getStatusChangeNotificationEnabled())
2014-10-28 20:50:30 +08:00
{
QString fStatus = "";
switch (status)
{
case Status::Away:
fStatus = tr("away", "contact status"); break;
case Status::Busy:
fStatus = tr("busy", "contact status"); break;
case Status::Offline:
fStatus = tr("offline", "contact status"); break;
case Status::Online:
fStatus = tr("online", "contact status"); break;
}
2014-10-28 20:50:30 +08:00
addSystemInfoMessage(tr("%1 is now %2", "e.g. \"Dubslow is now online\"")
.arg(f->getDisplayedName()).arg(fStatus),
ChatMessage::INFO, QDateTime::currentDateTime());
2014-10-28 20:50:30 +08:00
}
}
void ChatForm::onFriendTypingChanged(quint32 friendId, bool isTyping)
2014-09-24 00:52:06 +08:00
{
if (friendId == f->getFriendId())
{
setFriendTyping(isTyping);
}
}
2014-09-24 00:52:06 +08:00
void ChatForm::onFriendNameChanged(const QString& name)
{
if (sender() == f)
{
setName(name);
}
2014-09-24 00:52:06 +08:00
}
2014-09-24 23:28:38 +08:00
void ChatForm::onFriendMessageReceived(quint32 friendId, const QString& message,
bool isAction)
{
if (friendId != f->getFriendId())
{
return;
}
QDateTime timestamp = QDateTime::currentDateTime();
addMessage(f->getPublicKey(), message, isAction, timestamp, true);
}
void ChatForm::onStatusMessage(const QString& message)
{
if (sender() == f)
{
setStatusMessage(message);
}
}
void ChatForm::onReceiptReceived(quint32 friendId, int receipt)
{
if (friendId == f->getFriendId())
{
f->getChatForm()->getOfflineMsgEngine()->dischargeReceipt(receipt);
}
}
void ChatForm::onAvatarChange(uint32_t friendId, const QPixmap &pic)
2014-09-24 23:28:38 +08:00
{
if (friendId != f->getFriendId())
{
2014-09-24 23:28:38 +08:00
return;
}
2014-09-24 23:28:38 +08:00
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";
uint32_t friendId = f->getFriendId();
NetCamView* view = new NetCamView(friendId, this);
CoreAV* av = Core::getInstance()->getAv();
VideoSource* source = av->getVideoSourceFromCall(friendId);
view->show(source, 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())
{
2014-09-26 00:03:25 +08:00
ev->acceptProposedAction();
}
2014-09-26 00:03:25 +08:00
}
void ChatForm::dropEvent(QDropEvent *ev)
{
if (ev->mimeData()->hasUrls())
{
Core* core = Core::getInstance();
2014-09-26 00:03:25 +08:00
for (QUrl url : ev->mimeData()->urls())
{
QFileInfo info(url.path());
QFile file(info.absoluteFilePath());
if (url.isValid() && !url.isLocalFile() &&
url.toString().length() < TOX_MAX_MESSAGE_LENGTH)
2015-04-13 02:24:33 +08:00
{
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 sequential"
" file, which is not going to work!"));
file.close();
continue;
}
file.close();
2014-09-26 00:03:25 +08:00
if (info.exists())
{
core->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
{
if (friendId != f->getFriendId())
{
2014-09-27 03:23:20 +08:00
return;
}
2014-09-27 03:23:20 +08:00
2016-01-21 11:47:26 +08:00
avatar->setPixmap(QPixmap(":/img/contact_dark.svg"));
2014-09-27 03:23:20 +08:00
}
void ChatForm::clearChatArea(bool notInForm)
{
GenericChatForm::clearChatArea(notInForm);
f->getChatForm()->getOfflineMsgEngine()->removeAllReceipts();
}
void ChatForm::onDeliverOfflineMessages()
{
f->getChatForm()->getOfflineMsgEngine()->deliverOfflineMsgs();
}
void ChatForm::onLoadChatHistory()
{
if (sender() == f)
{
loadHistory(QDateTime::currentDateTime().addDays(-7), true);
}
}
// TODO: Split on smaller methods (style)
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
History* history = Nexus::getProfile()->getHistory();
QString pk = f->getPublicKey().toString();
QList<History::HistMessage> msgs = history->getChatHistory(pk, since, now);
2014-10-11 01:32:10 +08:00
ToxPk storedPrevId = previousId;
ToxPk 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;
QString dateText = msgDate.toString(Settings::getInstance().getDateFormat());
auto msg = ChatMessage::createChatInfoMessage(dateText,
ChatMessage::INFO,
QDateTime());
historyMessages.append(msg);
}
// Show each messages
const Core* core = Core::getInstance();
ToxPk authorPk(ToxId(it.sender).getPublicKey());
QString authorStr;
bool isSelf = authorPk == core->getSelfId().getPublicKey();
if (!it.dispName.isEmpty())
{
authorStr = it.dispName;
}
else if (isSelf)
{
authorStr = core->getUsername();
}
else
{
authorStr = resolveToxId(authorPk);
}
2015-01-03 21:08:04 +08:00
bool isAction = it.message.startsWith(ACTION_PREFIX, Qt::CaseInsensitive);
bool needSending = !it.isSent && isSelf;
QString messageText = isAction ? it.message.mid(4) : it.message;
ChatMessage::MessageType type = isAction ? ChatMessage::ACTION : ChatMessage::NORMAL;
QDateTime dateTime = needSending ? QDateTime() : msgDateTime;
auto msg = ChatMessage::createChatMessage(
authorStr, messageText, type, isSelf, dateTime);
2015-01-05 02:29:06 +08:00
uint prev = prevMsgDateTime.secsTo(msgDateTime);
if (!isAction && prevId == authorPk && prev < getChatLog()->repNameAfter)
{
2015-01-05 02:29:06 +08:00
msg->hideSender();
}
prevId = authorPk;
prevMsgDateTime = msgDateTime;
2015-01-05 02:29:06 +08:00
if (needSending && processUndelivered)
2014-11-10 19:30:56 +08:00
{
Core* core = Core::getInstance();
uint32_t friendId = f->getFriendId();
int rec = isAction ? core->sendAction(friendId, msg->toString())
: core->sendMessage(friendId, 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()
{
2016-07-21 16:49:50 +08:00
// note: grabber is self-managed and will destroy itself when done
ScreenshotGrabber* screenshotGrabber = new ScreenshotGrabber;
connect(screenshotGrabber, &ScreenshotGrabber::screenshotTaken,
this, &ChatForm::onScreenshotTaken);
screenshotGrabber->showGrabber();
2015-05-26 06:33:10 +08:00
// Create dir for screenshots
2016-03-23 19:16:38 +08:00
QDir(Settings::getInstance().getAppDataDirPath()).mkpath("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().getAppDataDirPath())
.arg(QDir::separator())
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH-mm-ss.zzz"));
QFile file(filepath);
if (file.open(QFile::ReadWrite))
{
pixmap.save(&file, "PNG");
qint64 filesize = file.size();
file.close();
QFileInfo fi(file);
Core::getInstance()->sendFile(f->getFriendId(), fi.fileName(),
fi.filePath(), filesize);
}
else
{
QMessageBox::warning(this,
tr("Failed to open temporary file",
"Temporary file for screenshot"),
tr("qTox wasn't able to save the screenshot"));
}
}
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::insertChatMessage(ChatMessage::Ptr msg)
{
GenericChatForm::insertChatMessage(msg);
if (netcam && bodySplitter->sizes()[1] == 0)
{
netcam->setShowMessages(true, true);
}
}
void ChatForm::onCopyStatusMessage()
{
// make sure to copy not truncated text directly from the friend
QString text = f->getStatusMessage();
QClipboard* clipboard = QApplication::clipboard();
if (clipboard)
{
clipboard->setText(text, QClipboard::Clipboard);
}
}
void ChatForm::updateMuteMicButton()
{
const CoreAV* av = Core::getInstance()->getAv();
micButton->setEnabled(av->isCallActive(f));
if (micButton->isEnabled())
{
if (av->isCallInputMuted(f))
{
micButton->setObjectName("red");
micButton->setToolTip(tr("Unmute microphone"));
}
else
{
micButton->setObjectName("green");
micButton->setToolTip(tr("Mute microphone"));
}
}
2016-08-29 21:48:38 +08:00
else
{
micButton->setObjectName("");
micButton->setToolTip(tr("Microphone can be muted only during a call"));
}
2016-08-29 21:48:38 +08:00
QString stylePath = QStringLiteral(":/ui/micButton/micButton.css");
QString style = Style::getStylesheet(stylePath);
micButton->setStyleSheet(style);
}
void ChatForm::updateMuteVolButton()
{
const CoreAV* av = Core::getInstance()->getAv();
volButton->setEnabled(av->isCallActive(f));
2016-08-29 21:48:38 +08:00
if (volButton->isEnabled())
{
if (av->isCallOutputMuted(f))
{
volButton->setObjectName("red");
volButton->setToolTip(tr("Unmute call"));
}
else
{
volButton->setObjectName("green");
volButton->setToolTip(tr("Mute call"));
}
}
else
{
volButton->setObjectName("");
volButton->setToolTip(tr("Sound can be disabled only during a call"));
}
2016-08-29 21:48:38 +08:00
QString stylePath = QStringLiteral(":/ui/volButton/volButton.css");
QString style = Style::getStylesheet(stylePath);
volButton->setStyleSheet(style);
}
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, &QTimer::timeout,
this, &ChatForm::onUpdateTime);
2015-01-26 06:19:25 +08:00
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
{
QString dhms = secondsToDHMS(timeElapsed.elapsed()/1000);
QString name = f->getDisplayedName();
addSystemInfoMessage(tr("Call with %1 ended. %2").arg(name, dhms),
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);
}
if (hours)
{
2014-11-03 09:51:56 +08:00
return cD + res.sprintf("%02dh %02dm %02ds", hours, minutes, seconds);
}
if (minutes)
{
return cD + res.sprintf("%02dm %02ds", minutes, seconds);
}
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));
QString name = f->getDisplayedName();
text->setText("<div class=typing>" + tr("%1 is typing").arg(name) + "</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)
{
2015-06-07 03:26:09 +08:00
callConfirm->show();
}
2015-06-07 03:26:09 +08:00
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())
{
2015-04-13 02:24:33 +08:00
return;
}
2015-04-13 02:24:33 +08:00
bool isAction = msg.startsWith(ACTION_PREFIX, Qt::CaseInsensitive);
2015-04-13 02:24:33 +08:00
if (isAction)
{
msg.remove(0, ACTION_PREFIX.length());
}
2015-04-13 02:24:33 +08:00
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 = ACTION_PREFIX + qt_msg;
}
2015-04-13 02:24:33 +08:00
bool status = !Settings::getInstance().getFauxOfflineMessaging();
ChatMessage::Ptr ma = addSelfMessage(qt_msg, isAction, timestamp, false);
2015-04-13 02:24:33 +08:00
Core* core = Core::getInstance();
uint32_t friendId = f->getFriendId();
int rec = isAction ? core->sendAction(friendId, qt_msg)
: core->sendMessage(friendId, qt_msg);
Profile* profile = Nexus::getProfile();
if (profile->isHistoryEnabled())
{
auto* offMsgEngine = getOfflineMsgEngine();
QString selfPk = Core::getInstance()->getSelfId().toString();
QString pk = f->getPublicKey().toString();
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);
});
}
else
{
2016-08-15 17:00:06 +08:00
// 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..."));
copyStatusAction->setText(tr("Copy"));
updateMuteMicButton();
updateMuteVolButton();
2015-10-10 20:49:30 +08:00
if (netcam)
{
netcam->setShowMessages(chatWidget->isVisible());
}
}