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

Merge pull request #307 from apprb/notif

Notifications
This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-09-23 19:02:28 +02:00
commit aa786b427a
19 changed files with 428 additions and 207 deletions

View File

@ -479,7 +479,8 @@ void Core::sendMessage(int friendId, const QString& message)
for (auto &cMsg :cMessages)
{
int messageId = tox_send_message(tox, friendId, cMsg.data(), cMsg.size());
emit messageSentResult(friendId, message, messageId);
if (messageId == 0)
emit messageSentResult(friendId, message, messageId);
}
}
@ -503,7 +504,9 @@ void Core::sendGroupMessage(int groupId, const QString& message)
for (auto &cMsg :cMessages)
{
tox_group_message_send(tox, groupId, cMsg.data(), cMsg.size());
int ret = tox_group_message_send(tox, groupId, cMsg.data(), cMsg.size());
if (ret == -1)
emit groupSentResult(groupId, message, ret);
}
}
@ -514,6 +517,7 @@ void Core::sendFile(int32_t friendId, QString Filename, QString FilePath, long l
if (fileNum == -1)
{
qWarning() << "Core::sendFile: Can't create the Tox file sender";
emit fileSendFailed(friendId, Filename);
return;
}
qDebug() << QString("Core::sendFile: Created file sender %1 with friend %2").arg(fileNum).arg(friendId);

3
core.h
View File

@ -125,6 +125,7 @@ signals:
void statusSet(Status status);
void messageSentResult(int friendId, const QString& message, int messageId);
void groupSentResult(int groupId, const QString& message, int result);
void actionSentResult(int friendId, const QString& action, int success);
void failedToAddFriend(const QString& userId);
@ -149,6 +150,8 @@ signals:
void fileTransferInfo(int FriendId, int FileNum, int64_t Filesize, int64_t BytesSent, ToxFile::FileDirection direction);
void fileTransferRemotePausedUnpaused(ToxFile file, bool paused);
void fileSendFailed(int FriendId, const QString& fname);
void avInvite(int friendId, int callIndex, bool video);
void avStart(int friendId, int callIndex, bool video);
void avCancel(int friendId, int callIndex);

View File

@ -107,13 +107,16 @@ HEADERS += widget/form/addfriendform.h \
widget/friendlistwidget.h \
widget/genericchatroomwidget.h \
widget/form/genericchatform.h \
widget/tool/chataction.h \
widget/tool/chatactions/chataction.h \
widget/chatareawidget.h \
filetransferinstance.h \
corestructs.h \
coredefines.h \
coreav.h \
widget/settingsdialog.h
widget/settingsdialog.h \
widget/tool/chatactions/messageaction.h \
widget/tool/chatactions/filetransferaction.h \
widget/tool/chatactions/systemmessageaction.h
SOURCES += \
widget/form/addfriendform.cpp \
@ -146,8 +149,11 @@ SOURCES += \
coreav.cpp \
widget/genericchatroomwidget.cpp \
widget/form/genericchatform.cpp \
widget/tool/chataction.cpp \
widget/tool/chatactions/chataction.cpp \
widget/chatareawidget.cpp \
filetransferinstance.cpp \
corestructs.cpp \
widget/settingsdialog.cpp
widget/settingsdialog.cpp \
widget/tool/chatactions/messageaction.cpp \
widget/tool/chatactions/filetransferaction.cpp \
widget/tool/chatactions/systemmessageaction.cpp

View File

@ -15,7 +15,7 @@
*/
#include "chatareawidget.h"
#include "widget/tool/chataction.h"
#include "widget/tool/chatactions/chataction.h"
#include <QScrollBar>
#include <QDesktopServices>
#include <QTextTable>

View File

@ -23,7 +23,7 @@
#include "friend.h"
#include "widget/friendwidget.h"
#include "filetransferinstance.h"
#include "widget/tool/chataction.h"
#include "widget/tool/chatactions/filetransferaction.h"
#include "widget/netcamview.h"
#include "widget/chatareawidget.h"
#include "widget/tool/chattextedit.h"
@ -51,6 +51,7 @@ ChatForm::ChatForm(Friend* chatFriend)
connect(msgEdit, &ChatTextEdit::enterPressed, this, &ChatForm::onSendTriggered);
connect(micButton, SIGNAL(clicked()), this, SLOT(onMicMuteToggle()));
connect(chatWidget, &ChatAreaWidget::onFileTranfertInterract, this, &ChatForm::onFileTansBtnClicked);
connect(Core::getInstance(), &Core::fileSendFailed, this, &ChatForm::onFileSendFailed);
}
ChatForm::~ChatForm()
@ -455,3 +456,11 @@ void ChatForm::onFileTansBtnClicked(QString widgetName, QString buttonName)
else
qDebug() << "no filetransferwidget: " << id;
}
void ChatForm::onFileSendFailed(int FriendId, const QString &fname)
{
if (FriendId != f->friendId)
return;
addSystemInfoMessage("File: \"" + fname + "\" failed to send.", "red");
}

View File

@ -65,6 +65,7 @@ private slots:
void onHangupCallTriggered();
void onCancelCallTriggered();
void onFileTansBtnClicked(QString widgetName, QString buttonName);
void onFileSendFailed(int FriendId, const QString &fname);
private:
Friend* f;

View File

@ -22,7 +22,8 @@
#include "style.h"
#include "widget/widget.h"
#include "settings.h"
#include "widget/tool/chataction.h"
#include "widget/tool/chatactions/messageaction.h"
#include "widget/tool/chatactions/systemmessageaction.h"
#include "widget/chatareawidget.h"
#include "widget/tool/chattextedit.h"
@ -208,3 +209,11 @@ void GenericChatForm::focusInput()
{
msgEdit->setFocus();
}
void GenericChatForm::addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime)
{
previousName = "";
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
chatWidget->insertMessage(new SystemMessageAction(message, type, date));
}

View File

@ -45,6 +45,7 @@ public:
virtual void setName(const QString &newName);
virtual void show(Ui::MainWindow &ui);
void addMessage(QString author, QString message, QDateTime datetime=QDateTime::currentDateTime());
void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime=QDateTime::currentDateTime());
signals:
void sendMessage(int, QString);

View File

@ -1,169 +0,0 @@
/*
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.
*/
#include "chataction.h"
#include "smileypack.h"
#include <QStringList>
#include <QBuffer>
#include "filetransferinstance.h"
QString ChatAction::toHtmlChars(const QString &str)
{
static QList<QPair<QString, QString>> replaceList = {{"&","&amp;"}, {">","&gt;"}, {"<","&lt;"}};
QString res = str;
for (auto &it : replaceList)
res = res.replace(it.first,it.second);
return res;
}
QString ChatAction::QImage2base64(const QImage &img)
{
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "PNG"); // writes image into ba in PNG format
return ba.toBase64();
}
QString ChatAction::getName()
{
if (isMe)
return QString("<div class=name_me>" + toHtmlChars(name) + "</div>");
else
return QString("<div class=name>" + toHtmlChars(name) + "</div>");
}
QString ChatAction::getDate()
{
QString res = date;
return res;
}
MessageAction::MessageAction(const QString &author, const QString &message, const QString &date, const bool &me) :
ChatAction(me, author, date),
message(message)
{
}
void MessageAction::setTextCursor(QTextCursor cursor)
{
// When this function is called, we're supposed to only update ourselve when needed
// Nobody should ask us to do anything with our content, we're on our own
// Except we never udpate on our own, so we can safely free our resources
(void) cursor;
message.clear();
message.squeeze();
name.clear();
name.squeeze();
date.clear();
date.squeeze();
}
QString MessageAction::getMessage()
{
QString message_ = SmileyPack::getInstance().smileyfied(toHtmlChars(message));
// detect urls
QRegExp exp("(www\\.|http[s]?:\\/\\/|ftp:\\/\\/)\\S+");
int offset = 0;
while ((offset = exp.indexIn(message_, offset)) != -1)
{
QString url = exp.cap();
// add scheme if not specified
if (exp.cap(1) == "www.")
url.prepend("http://");
QString htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url);
message_.replace(offset, exp.cap().length(), htmledUrl);
offset += htmledUrl.length();
}
// detect text quotes
QStringList messageLines = message_.split("\n");
message_ = "";
for (QString& s : messageLines)
{
if (QRegExp("^[ ]*&gt;.*").exactMatch(s))
message_ += "<span class=quote>>" + s.right(s.length()-4) + "</span><br/>";
else
message_ += s + "<br/>";
}
message_ = message_.left(message_.length()-4);
return QString("<div class=message>" + message_ + "</div>");
}
FileTransferAction::FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me) :
ChatAction(me, author, date)
{
w = widget;
connect(w, &FileTransferInstance::stateUpdated, this, &FileTransferAction::updateHtml);
}
FileTransferAction::~FileTransferAction()
{
}
QString FileTransferAction::getMessage()
{
QString widgetHtml;
if (w != nullptr)
widgetHtml = w->getHtmlImage();
else
widgetHtml = "<div class=quote>EMPTY CONTENT</div>";
return widgetHtml;
}
void FileTransferAction::setTextCursor(QTextCursor cursor)
{
cur = cursor;
cur.setKeepPositionOnInsert(true);
int end=cur.selectionEnd();
cur.setPosition(cur.position());
cur.setPosition(end, QTextCursor::KeepAnchor);
}
void FileTransferAction::updateHtml()
{
if (cur.isNull())
return;
int pos = cur.selectionStart();
cur.removeSelectedText();
cur.setKeepPositionOnInsert(false);
cur.insertHtml(getMessage());
cur.setKeepPositionOnInsert(true);
int end = cur.position();
cur.setPosition(pos);
cur.setPosition(end, QTextCursor::KeepAnchor);
// Free our ressources if we'll never need to update again
if (w->getState() == FileTransferInstance::TransfState::tsCanceled
|| w->getState() == FileTransferInstance::TransfState::tsFinished)
{
name.clear();
name.squeeze();
date.clear();
date.squeeze();
cur = QTextCursor();
}
}

View File

@ -0,0 +1,53 @@
/*
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.
*/
#include "chataction.h"
#include <QStringList>
#include <QBuffer>
QString ChatAction::toHtmlChars(const QString &str)
{
static QList<QPair<QString, QString>> replaceList = {{"&","&amp;"}, {">","&gt;"}, {"<","&lt;"}};
QString res = str;
for (auto &it : replaceList)
res = res.replace(it.first,it.second);
return res;
}
QString ChatAction::QImage2base64(const QImage &img)
{
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "PNG"); // writes image into ba in PNG format
return ba.toBase64();
}
QString ChatAction::getName()
{
if (isMe)
return QString("<div class=name_me>" + toHtmlChars(name) + "</div>");
else
return QString("<div class=name>" + toHtmlChars(name) + "</div>");
}
QString ChatAction::getDate()
{
QString res = date;
return res;
}

View File

@ -42,33 +42,4 @@ protected:
QString name, date;
};
class MessageAction : public ChatAction
{
public:
MessageAction(const QString &author, const QString &message, const QString &date, const bool &me);
virtual ~MessageAction(){;}
virtual QString getMessage();
virtual void setTextCursor(QTextCursor cursor) final;
private:
QString message;
};
class FileTransferAction : public ChatAction
{
Q_OBJECT
public:
FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me);
virtual ~FileTransferAction();
virtual QString getMessage();
virtual void setTextCursor(QTextCursor cursor) final;
private slots:
void updateHtml();
private:
FileTransferInstance *w;
QTextCursor cur;
};
#endif // CHATACTION_H

View File

@ -0,0 +1,75 @@
/*
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.
*/
#include "filetransferaction.h"
#include "filetransferinstance.h"
FileTransferAction::FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me) :
ChatAction(me, author, date)
{
w = widget;
connect(w, &FileTransferInstance::stateUpdated, this, &FileTransferAction::updateHtml);
}
FileTransferAction::~FileTransferAction()
{
}
QString FileTransferAction::getMessage()
{
QString widgetHtml;
if (w != nullptr)
widgetHtml = w->getHtmlImage();
else
widgetHtml = "<div class=quote>EMPTY CONTENT</div>";
return widgetHtml;
}
void FileTransferAction::setTextCursor(QTextCursor cursor)
{
cur = cursor;
cur.setKeepPositionOnInsert(true);
int end=cur.selectionEnd();
cur.setPosition(cur.position());
cur.setPosition(end, QTextCursor::KeepAnchor);
}
void FileTransferAction::updateHtml()
{
if (cur.isNull())
return;
int pos = cur.selectionStart();
cur.removeSelectedText();
cur.setKeepPositionOnInsert(false);
cur.insertHtml(getMessage());
cur.setKeepPositionOnInsert(true);
int end = cur.position();
cur.setPosition(pos);
cur.setPosition(end, QTextCursor::KeepAnchor);
// Free our ressources if we'll never need to update again
if (w->getState() == FileTransferInstance::TransfState::tsCanceled
|| w->getState() == FileTransferInstance::TransfState::tsFinished)
{
name.clear();
name.squeeze();
date.clear();
date.squeeze();
cur = QTextCursor();
}
}

View File

@ -0,0 +1,39 @@
/*
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.
*/
#ifndef FILETRANSFERACTION_H
#define FILETRANSFERACTION_H
#include "widget/tool/chatactions/chataction.h"
class FileTransferAction : public ChatAction
{
Q_OBJECT
public:
FileTransferAction(FileTransferInstance *widget, const QString &author, const QString &date, const bool &me);
virtual ~FileTransferAction();
virtual QString getMessage();
virtual void setTextCursor(QTextCursor cursor) final;
private slots:
void updateHtml();
private:
FileTransferInstance *w;
QTextCursor cur;
};
#endif // FILETRANSFERACTION_H

View File

@ -0,0 +1,75 @@
/*
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.
*/
#include "messageaction.h"
#include "smileypack.h"
MessageAction::MessageAction(const QString &author, const QString &message, const QString &date, const bool &me) :
ChatAction(me, author, date),
message(message)
{
}
void MessageAction::setTextCursor(QTextCursor cursor)
{
// When this function is called, we're supposed to only update ourselve when needed
// Nobody should ask us to do anything with our content, we're on our own
// Except we never udpate on our own, so we can safely free our resources
(void) cursor;
message.clear();
message.squeeze();
name.clear();
name.squeeze();
date.clear();
date.squeeze();
}
QString MessageAction::getMessage()
{
QString message_ = SmileyPack::getInstance().smileyfied(toHtmlChars(message));
// detect urls
QRegExp exp("(www\\.|http[s]?:\\/\\/|ftp:\\/\\/)\\S+");
int offset = 0;
while ((offset = exp.indexIn(message_, offset)) != -1)
{
QString url = exp.cap();
// add scheme if not specified
if (exp.cap(1) == "www.")
url.prepend("http://");
QString htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url);
message_.replace(offset, exp.cap().length(), htmledUrl);
offset += htmledUrl.length();
}
// detect text quotes
QStringList messageLines = message_.split("\n");
message_ = "";
for (QString& s : messageLines)
{
if (QRegExp("^[ ]*&gt;.*").exactMatch(s))
message_ += "<span class=quote>>" + s.right(s.length()-4) + "</span><br/>";
else
message_ += s + "<br/>";
}
message_ = message_.left(message_.length()-4);
return QString("<div class=message>" + message_ + "</div>");
}

View File

@ -0,0 +1,34 @@
/*
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.
*/
#ifndef MESSAGEACTION_H
#define MESSAGEACTION_H
#include "widget/tool/chatactions/chataction.h"
class MessageAction : public ChatAction
{
public:
MessageAction(const QString &author, const QString &message, const QString &date, const bool &me);
virtual ~MessageAction(){;}
virtual QString getMessage();
virtual void setTextCursor(QTextCursor cursor) final;
private:
QString message;
};
#endif // MESSAGEACTION_H

View File

@ -0,0 +1,46 @@
/*
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.
*/
#include "systemmessageaction.h"
SystemMessageAction::SystemMessageAction(const QString &message, const QString &type, const QString &date) :
ChatAction(false, QString(), date),
message(message),
type(type)
{
}
QString SystemMessageAction::getMessage()
{
return QString("<table width=100%><tr><td align=center><div class=" + type + ">" + message + "</td><tr></div></table>");
}
void SystemMessageAction::setTextCursor(QTextCursor cursor)
{
// When this function is called, we're supposed to only update ourselve when needed
// Nobody should ask us to do anything with our content, we're on our own
// Except we never udpate on our own, so we can safely free our resources
(void) cursor;
message.clear();
message.squeeze();
name.clear();
name.squeeze();
date.clear();
date.squeeze();
type.clear();
type.squeeze();
}

View File

@ -0,0 +1,37 @@
/*
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.
*/
#ifndef SYSTEMMESSAGEACTION_H
#define SYSTEMMESSAGEACTION_H
#include "widget/tool/chatactions/chataction.h"
class SystemMessageAction : public ChatAction
{
public:
SystemMessageAction(const QString &message, const QString& type, const QString &date);
virtual ~SystemMessageAction(){;}
virtual void setTextCursor(QTextCursor cursor) final;
virtual QString getName() {return QString();}
virtual QString getMessage();
private:
QString message;
QString type;
};
#endif // SYSTEMMESSAGEACTION_H

View File

@ -197,6 +197,9 @@ Widget::Widget(QWidget *parent)
connect(core, &Core::groupNamelistChanged, this, &Widget::onGroupNamelistChanged);
connect(core, &Core::emptyGroupCreated, this, &Widget::onEmptyGroupCreated);
connect(core, SIGNAL(messageSentResult(int,QString,int)), this, SLOT(onMessageSendResult(int,QString,int)));
connect(core, SIGNAL(groupSentResult(int,QString,int)), this, SLOT(onGroupSendResult(int,QString,int)));
connect(this, &Widget::statusSet, core, &Core::setStatus);
connect(this, &Widget::friendRequested, core, &Core::requestFriendship);
connect(this, &Widget::friendRequestAccepted, core, &Core::acceptFriendRequest);
@ -1060,3 +1063,25 @@ bool Widget::getIsWindowMinimized()
{
return static_cast<bool>(isWindowMinimized);
}
void Widget::onMessageSendResult(int friendId, const QString& message, int messageId)
{
Q_UNUSED(message)
Friend* f = FriendList::findFriend(friendId);
if (!f)
return;
if (!messageId)
f->chatForm->addSystemInfoMessage("Message failed to send", "red");
}
void Widget::onGroupSendResult(int groupId, const QString& message, int result)
{
Q_UNUSED(message)
Group* g = GroupList::findGroup(groupId);
if (!g)
return;
if (result == -1)
g->chatForm->addSystemInfoMessage("Message failed to send", "red");
}

View File

@ -104,6 +104,8 @@ private slots:
void setStatusOnline();
void setStatusAway();
void setStatusBusy();
void onMessageSendResult(int friendId, const QString& message, int messageId);
void onGroupSendResult(int groupId, const QString& message, int result);
protected slots:
void moveWindow(QMouseEvent *e);