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

1029 lines
29 KiB
C++
Raw Normal View History

2014-07-07 00:19:45 +08:00
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
2014-09-11 21:44:34 +08:00
#include <QDebug>
#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>
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"
#include "src/historykeeper.h"
#include "src/misc/style.h"
#include "src/misc/settings.h"
#include "src/misc/cstring.h"
#include "src/widget/callconfirmwidget.h"
#include "src/widget/friendwidget.h"
2014-10-08 12:26:25 +08:00
#include "src/widget/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"
#include "src/widget/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"
#include "src/offlinemsgengine.h"
#include "src/widget/tool/screenshotgrabber.h"
2014-06-25 04:11:11 +08:00
ChatForm::ChatForm(Friend* chatFriend)
: f(chatFriend)
2014-10-10 03:20:06 +08:00
, callId(0)
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);
netcam = new NetCamView();
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
menu.addAction(tr("Load chat history..."), 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-08-19 23:20:20 +08:00
connect(micButton, SIGNAL(clicked()), this, SLOT(onMicMuteToggle()));
2014-10-28 20:50:30 +08:00
connect(volButton, SIGNAL(clicked()), this, SLOT(onVolMuteToggle()));
2014-09-24 00:52:06 +08:00
connect(Core::getInstance(), &Core::fileSendFailed, this, &ChatForm::onFileSendFailed);
connect(this, SIGNAL(chatAreaCleared()), getOfflineMsgEngine(), SLOT(removeAllReciepts()));
connect(&typingTimer, &QTimer::timeout, this, [=]{
Core::getInstance()->sendTyping(f->getFriendID(), false);
isTyping = false;
} );
2015-01-02 20:04:59 +08:00
connect(nameLabel, &CroppingLabel::textChanged, this, [=](QString text, QString orig) {
if (text != orig) emit aliasChanged(text);
} );
2014-09-26 00:03:25 +08:00
setAcceptDrops(true);
2014-06-25 04:11:11 +08:00
}
ChatForm::~ChatForm()
{
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()
{
2014-10-17 20:29:48 +08:00
QStringList paths = QFileDialog::getOpenFileNames(0,tr("Send a file"));
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("File not read"), 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())
{
2014-10-30 00:25:47 +08:00
QMessageBox::critical(0, 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.isMine())
{
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()));
}
void ChatForm::onFileRecvRequest(ToxFile file)
{
2014-11-06 23:26:22 +08:00
if (file.friendId != f->getFriendID())
return;
Widget* w = Widget::getInstance();
if (!w->isFriendWidgetCurActiveWidget(f)|| w->isMinimized() || !w->isActiveWindow())
{
w->newMessageAlert(f->getFriendWidget());
2014-11-06 23:26:22 +08:00
f->setEventFlag(true);
f->getFriendWidget()->updateStatusLight();
}
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
2014-11-17 18:10:38 +08:00
if (!Settings::getInstance().getAutoAcceptDir(f->getToxID()).isEmpty()
2015-01-03 21:08:04 +08:00
|| Settings::getInstance().getAutoSaveEnabled())
2014-12-14 04:11:03 +08:00
{
2015-01-11 18:57:33 +08:00
ChatLineContentProxy* proxy = dynamic_cast<ChatLineContentProxy*>(msg->getContent(1));
if(proxy)
{
FileTransferWidget* tfWidget = dynamic_cast<FileTransferWidget*>(proxy->getWidget());
if(tfWidget)
tfWidget->autoAcceptTransfer(Settings::getInstance().getAutoAcceptDir(f->getToxID()));
}
2014-12-14 04:11:03 +08:00
}
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)
{
callConfirm = new CallConfirmWidget(videoButton);
if (isVisible())
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
{
callConfirm = new CallConfirmWidget(callButton);
if (isVisible())
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);
2015-01-05 01:21:35 +08:00
insertChatMessage(ChatMessage::createChatInfoMessage(tr("%1 calling").arg(f->getDisplayedName()), ChatMessage::INFO, QDateTime::currentDateTime()));
2014-07-01 03:14:51 +08:00
Widget* w = Widget::getInstance();
if (!w->isFriendWidgetCurActiveWidget(f)|| w->isMinimized() || !w->isActiveWindow())
2014-07-01 03:14:51 +08:00
{
w->newMessageAlert(f->getFriendWidget());
2014-11-06 23:26:22 +08:00
f->setEventFlag(true);
f->getFriendWidget()->updateStatusLight();
2014-07-01 03:14:51 +08:00
}
}
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
2014-11-06 23:26:22 +08:00
netcam->show(Core::getInstance()->getVideoSourceFromCall(CallId), f->getDisplayedName());
}
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
netcam->hide();
2015-01-05 01:21:35 +08:00
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();
netcam->hide();
}
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()));
}
2015-01-05 01:21:35 +08:00
addSystemInfoMessage(tr("Calling to %1").arg(f->getDisplayedName()), ChatMessage::INFO, QDateTime::currentDateTime());
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";
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
2014-11-06 23:26:22 +08:00
netcam->show(Core::getInstance()->getVideoSourceFromCall(CallId), f->getDisplayedName());
}
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();
netcam->hide();
}
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();
netcam->hide();
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();
2014-11-02 19:47:42 +08:00
netcam->hide();
}
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();
2015-01-05 01:21:35 +08:00
insertChatMessage(ChatMessage::createChatInfoMessage(tr("Call rejected"), ChatMessage::INFO, QDateTime::currentDateTime()));
2014-10-16 21:37:48 +08:00
netcam->hide();
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)
2014-11-06 23:26:22 +08:00
netcam->show(Core::getInstance()->getVideoSourceFromCall(CallId), f->getDisplayedName());
else
netcam->hide();
}
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
if(netcam->isFullScreen())
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();
netcam->hide();
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();
2015-01-26 06:19:25 +08:00
if(disableCallButtonsTimer == nullptr)
{
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("File not read"), 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
2015-01-05 02:29:06 +08:00
ToxID storedPrevId = previousId;
ToxID prevId;
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
2015-01-05 02:29:06 +08:00
ToxID authorId = ToxID::fromString(it.sender);
QString authorStr = authorId.isMine() ? Core::getInstance()->getUsername() : resolveToxID(authorId);
bool isAction = it.message.startsWith("/me ");
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,
2015-01-05 02:29:06 +08:00
authorId.isMine(),
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.isMine())
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()
{
ScreenshotGrabber *screenshotGrabber = new ScreenshotGrabber (this);
connect(screenshotGrabber, &ScreenshotGrabber::screenshotTaken, this, &ChatForm::onScreenshotTaken);
// Try to not grab the context-menu
QTimer::singleShot(200, screenshotGrabber, &ScreenshotGrabber::showGrabber);
}
void ChatForm::onScreenshotTaken (const QPixmap &pixmap) {
QTemporaryFile file("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);
Text* text = dynamic_cast<Text*>(chatWidget->getTypingNotification()->getContent(1));
if(text)
2015-02-01 02:22:45 +08:00
text->setText("<div class=typing>" + QString("%1 is typing").arg(f->getDisplayedName()) + "</div>");
}
void ChatForm::show(Ui::MainWindow &ui)
{
GenericChatForm::show(ui);
if (callConfirm)
callConfirm->show();
}
void ChatForm::hideEvent(QHideEvent*)
{
if (callConfirm)
callConfirm->hide();
}
OfflineMsgEngine *ChatForm::getOfflineMsgEngine()
{
return offlineEngine;
}
2015-04-13 02:24:33 +08:00
void ChatForm::SendMessageStr(QString msg)
{
if (msg.isEmpty())
return;
bool isAction = msg.startsWith("/me ");
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,
Core::getInstance()->getSelfId().publicKey, timestamp, status);
ChatMessage::Ptr ma = addSelfMessage(msg, isAction, timestamp, false);
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
}
}