RetroShare/retroshare-gui/src/gui/feeds/ChatMsgItem.cpp

275 lines
7.2 KiB
C++
Raw Normal View History

2018-11-13 16:02:26 -05:00
/*******************************************************************************
* gui/feeds/ChatMsgItem.cpp *
* *
* Copyright (c) 2008, Robert Fernie <retroshare.project@gmail.com> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero 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 *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include <QDateTime>
#include <QTimer>
#include "ChatMsgItem.h"
#include "FeedHolder.h"
#include "retroshare-gui/RsAutoUpdatePage.h"
#include "gui/msgs/MessageComposer.h"
#include "util/HandleRichText.h"
#include "util/DateTime.h"
#include "gui/common/AvatarDefs.h"
Chat service: - Added send time to ChatInfo. Chat: - Reworked IMHistoryKeeper to save only needed data as raw data (not formatted from PeersDialog). - Renamed the public chat history file from "his1.xml" to "chatPublic.xml" and moved it from data dir to config dir. The current history is lost. Please delete the file "his1.xml" manually. - Optimized save of the history. Save only when changed, also during the runtime. - Clear the history in PeersDialog clears the IMHistoryKeeper too. - New setting to send chat message with Ctrl+Return. Changed the check for Enter in PeersDialog and PopupChatDialog from textChanged to eventFilter. - Smileys are inserted at the current cursor position, not added at the end. - Don't send emty messages. New class ChatStyle: - Created a new class for the work with chat styles and smileys. - Currently only two internal styles are available - private and public. Later more external styles planned. - Moved functions for loading and showing the emoticons from PeersDialog and PopupChatDialog to the new class. Private chat: - Split private chat style into incoming.htm and outgoing.htm. - Removed style change. Public chat: - New chat style incoming.htm, outgoing.htm, hincoming.htm and houtgoing.htm. Chat feed: - Show links and emoticons if they are enabled for public chat. PeersDialog: - Show the own name and location only once at start and not with QTimer every 1.5 second. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3441 b45a01b8-16f6-495d-af2f-9b41ad6348cc
2010-09-04 10:23:30 -04:00
#include "gui/settings/rsharesettings.h"
#include "gui/notifyqt.h"
#include <retroshare/rsmsgs.h>
#include <retroshare/rspeers.h>
#include "gui/msgs/MessageInterface.h"
/*****
* #define DEBUG_ITEM 1
****/
/** Constructor */
ChatMsgItem::ChatMsgItem(FeedHolder *parent, uint32_t feedId, const RsPeerId &peerId, const std::string &message) :
FeedItem(NULL), mParent(parent), mFeedId(feedId), mPeerId(peerId)
{
/* Invoke the Qt Designer generated object setup routine */
setupUi(this);
messageFrame->setVisible(false);
sendButton->hide();
cancelButton->hide();
sendButton->setEnabled(false);
/* general ones */
connect( clearButton, SIGNAL( clicked( void ) ), this, SLOT( removeItem ( void ) ) );
/* specific ones */
connect( chatButton, SIGNAL( clicked( void ) ), this, SLOT( openChat ( void ) ) );
connect( msgButton, SIGNAL( clicked( void ) ), this, SLOT( sendMsg ( void ) ) );
connect( quickmsgButton, SIGNAL( clicked( ) ), this, SLOT( togglequickmessage() ) );
connect( cancelButton, SIGNAL( clicked( ) ), this, SLOT( togglequickmessage() ) );
connect( sendButton, SIGNAL( clicked( ) ), this, SLOT( sendMessage() ) );
avatar->setId(ChatId(mPeerId));
updateItemStatic();
updateItem();
insertChat(message);
}
void ChatMsgItem::updateItemStatic()
{
if (!rsPeers)
return;
RsPeerDetails details;
if (rsPeers->getPeerDetails(mPeerId, details))
{
/* set Peer name */
peerNameLabel->setText(QString::fromUtf8(details.name.c_str()));
}
else
{
chatButton->setEnabled(false);
msgButton->setEnabled(false);
}
/* fill in */
#ifdef DEBUG_ITEM
std::cerr << "ChatMsgItem::updateItemStatic()";
std::cerr << std::endl;
#endif
}
void ChatMsgItem::updateItem()
{
if (!rsPeers)
return;
/* fill in */
#ifdef DEBUG_ITEM
std::cerr << "ChatMsgItem::updateItem()";
std::cerr << std::endl;
#endif
if(!RsAutoUpdatePage::eventsLocked()) {
RsPeerDetails details;
if (!rsPeers->getPeerDetails(mPeerId, details))
{
return;
}
/* do buttons */
chatButton->setEnabled(details.state & RS_PEER_STATE_CONNECTED);
if (details.state & RS_PEER_STATE_FRIEND)
{
msgButton->setEnabled(true);
}
else
{
msgButton->setEnabled(false);
}
}
/* slow Tick */
int msec_rate = 10129;
QTimer::singleShot( msec_rate, this, SLOT(updateItem( void ) ));
return;
}
void ChatMsgItem::insertChat(const std::string &message)
{
#ifdef DEBUG_ITEM
std::cerr << "ChatMsgItem::insertChat(): " << msg << std::endl;
#endif
timestampLabel->setText(DateTime::formatLongDateTime(QDateTime::currentDateTime()));
QString formatMsg = QString::fromUtf8(message.c_str());
Chat service: - Added send time to ChatInfo. Chat: - Reworked IMHistoryKeeper to save only needed data as raw data (not formatted from PeersDialog). - Renamed the public chat history file from "his1.xml" to "chatPublic.xml" and moved it from data dir to config dir. The current history is lost. Please delete the file "his1.xml" manually. - Optimized save of the history. Save only when changed, also during the runtime. - Clear the history in PeersDialog clears the IMHistoryKeeper too. - New setting to send chat message with Ctrl+Return. Changed the check for Enter in PeersDialog and PopupChatDialog from textChanged to eventFilter. - Smileys are inserted at the current cursor position, not added at the end. - Don't send emty messages. New class ChatStyle: - Created a new class for the work with chat styles and smileys. - Currently only two internal styles are available - private and public. Later more external styles planned. - Moved functions for loading and showing the emoticons from PeersDialog and PopupChatDialog to the new class. Private chat: - Split private chat style into incoming.htm and outgoing.htm. - Removed style change. Public chat: - New chat style incoming.htm, outgoing.htm, hincoming.htm and houtgoing.htm. Chat feed: - Show links and emoticons if they are enabled for public chat. PeersDialog: - Show the own name and location only once at start and not with QTimer every 1.5 second. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3441 b45a01b8-16f6-495d-af2f-9b41ad6348cc
2010-09-04 10:23:30 -04:00
unsigned int formatFlag = RSHTML_FORMATTEXT_EMBED_LINKS;
Chat service: - Added send time to ChatInfo. Chat: - Reworked IMHistoryKeeper to save only needed data as raw data (not formatted from PeersDialog). - Renamed the public chat history file from "his1.xml" to "chatPublic.xml" and moved it from data dir to config dir. The current history is lost. Please delete the file "his1.xml" manually. - Optimized save of the history. Save only when changed, also during the runtime. - Clear the history in PeersDialog clears the IMHistoryKeeper too. - New setting to send chat message with Ctrl+Return. Changed the check for Enter in PeersDialog and PopupChatDialog from textChanged to eventFilter. - Smileys are inserted at the current cursor position, not added at the end. - Don't send emty messages. New class ChatStyle: - Created a new class for the work with chat styles and smileys. - Currently only two internal styles are available - private and public. Later more external styles planned. - Moved functions for loading and showing the emoticons from PeersDialog and PopupChatDialog to the new class. Private chat: - Split private chat style into incoming.htm and outgoing.htm. - Removed style change. Public chat: - New chat style incoming.htm, outgoing.htm, hincoming.htm and houtgoing.htm. Chat feed: - Show links and emoticons if they are enabled for public chat. PeersDialog: - Show the own name and location only once at start and not with QTimer every 1.5 second. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3441 b45a01b8-16f6-495d-af2f-9b41ad6348cc
2010-09-04 10:23:30 -04:00
// embed smileys ?
if (Settings->valueFromGroup(QString("Chat"), QString::fromUtf8("Emoteicons_GroupChat"), true).toBool()) {
formatFlag |= RSHTML_FORMATTEXT_EMBED_SMILEYS;
Chat service: - Added send time to ChatInfo. Chat: - Reworked IMHistoryKeeper to save only needed data as raw data (not formatted from PeersDialog). - Renamed the public chat history file from "his1.xml" to "chatPublic.xml" and moved it from data dir to config dir. The current history is lost. Please delete the file "his1.xml" manually. - Optimized save of the history. Save only when changed, also during the runtime. - Clear the history in PeersDialog clears the IMHistoryKeeper too. - New setting to send chat message with Ctrl+Return. Changed the check for Enter in PeersDialog and PopupChatDialog from textChanged to eventFilter. - Smileys are inserted at the current cursor position, not added at the end. - Don't send emty messages. New class ChatStyle: - Created a new class for the work with chat styles and smileys. - Currently only two internal styles are available - private and public. Later more external styles planned. - Moved functions for loading and showing the emoticons from PeersDialog and PopupChatDialog to the new class. Private chat: - Split private chat style into incoming.htm and outgoing.htm. - Removed style change. Public chat: - New chat style incoming.htm, outgoing.htm, hincoming.htm and houtgoing.htm. Chat feed: - Show links and emoticons if they are enabled for public chat. PeersDialog: - Show the own name and location only once at start and not with QTimer every 1.5 second. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3441 b45a01b8-16f6-495d-af2f-9b41ad6348cc
2010-09-04 10:23:30 -04:00
}
formatMsg = RsHtml().formatText(NULL, formatMsg, formatFlag);
Chat service: - Added send time to ChatInfo. Chat: - Reworked IMHistoryKeeper to save only needed data as raw data (not formatted from PeersDialog). - Renamed the public chat history file from "his1.xml" to "chatPublic.xml" and moved it from data dir to config dir. The current history is lost. Please delete the file "his1.xml" manually. - Optimized save of the history. Save only when changed, also during the runtime. - Clear the history in PeersDialog clears the IMHistoryKeeper too. - New setting to send chat message with Ctrl+Return. Changed the check for Enter in PeersDialog and PopupChatDialog from textChanged to eventFilter. - Smileys are inserted at the current cursor position, not added at the end. - Don't send emty messages. New class ChatStyle: - Created a new class for the work with chat styles and smileys. - Currently only two internal styles are available - private and public. Later more external styles planned. - Moved functions for loading and showing the emoticons from PeersDialog and PopupChatDialog to the new class. Private chat: - Split private chat style into incoming.htm and outgoing.htm. - Removed style change. Public chat: - New chat style incoming.htm, outgoing.htm, hincoming.htm and houtgoing.htm. Chat feed: - Show links and emoticons if they are enabled for public chat. PeersDialog: - Show the own name and location only once at start and not with QTimer every 1.5 second. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3441 b45a01b8-16f6-495d-af2f-9b41ad6348cc
2010-09-04 10:23:30 -04:00
chatTextlabel->setText(formatMsg);
}
void ChatMsgItem::removeItem()
{
#ifdef DEBUG_ITEM
std::cerr << "ChatMsgItem::removeItem()";
std::cerr << std::endl;
#endif
if (mParent) {
mParent->lockLayout(this, true);
}
hide();
if (mParent) {
mParent->lockLayout(this, false);
mParent->deleteFeedItem(this, mFeedId);
}
}
void ChatMsgItem::gotoHome()
{
#ifdef DEBUG_ITEM
std::cerr << "ChatMsgItem::gotoHome()";
std::cerr << std::endl;
#endif
}
/*********** SPECIFIC FUNCTIOSN ***********************/
void ChatMsgItem::sendMsg()
{
#ifdef DEBUG_ITEM
std::cerr << "ChatMsgItem::sendMsg()";
std::cerr << std::endl;
#endif
if (mParent)
{
MessageComposer *nMsgDialog = MessageComposer::newMsg();
if (nMsgDialog == NULL) {
return;
}
nMsgDialog->addRecipient(MessageComposer::TO, mPeerId);
nMsgDialog->show();
nMsgDialog->activateWindow();
/* window will destroy itself! */
}
}
void ChatMsgItem::openChat()
{
#ifdef DEBUG_ITEM
std::cerr << "ChatMsgItem::openChat()";
std::cerr << std::endl;
#endif
if (mParent)
{
mParent->openChat(mPeerId);
}
}
void ChatMsgItem::togglequickmessage()
{
mParent->lockLayout(this, true);
if (messageFrame->isHidden())
{
messageFrame->show();
sendButton->show();
cancelButton->show();
}
else
{
messageFrame->hide();
sendButton->hide();
cancelButton->hide();
}
emit sizeChanged(this);
mParent->lockLayout(this, false);
}
void ChatMsgItem::sendMessage()
{
/* construct a message */
MessageInfo mi;
mi.title = tr("Quick Message").toUtf8().constData();
mi.msg = quickmsgText->toHtml().toUtf8().constData();
mi.rspeerid_msgto.insert(mPeerId);
rsMail->MessageSend(mi);
quickmsgText->clear();
messageFrame->setVisible(false);
sendButton->hide();
cancelButton->hide();
emit sizeChanged(this);
}
void ChatMsgItem::on_quickmsgText_textChanged()
{
if (quickmsgText->toPlainText().isEmpty())
{
sendButton->setEnabled(false);
}
else
{
sendButton->setEnabled(true);
}
}