Merge pull request #1493 from csoler/v0.6-Messaging

V0.6 messaging
This commit is contained in:
csoler 2019-03-29 22:27:27 +01:00 committed by GitHub
commit ef1755d97f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 1459 additions and 1034 deletions

View File

@ -93,7 +93,7 @@ const ChatLobbyFlags RS_CHAT_LOBBY_FLAGS_PGP_SIGNED ( 0x00000010 ) ; // requi
typedef uint64_t ChatLobbyId ;
typedef uint64_t ChatLobbyMsgId ;
typedef std::string ChatLobbyNickName ;
typedef std::string RsMailMessageId; // should be uint32_t !!
typedef uint64_t MessageId ;
@ -237,10 +237,11 @@ struct MsgInfoSummary : RsSerializable
MsgInfoSummary() : msgflags(0), count(0), ts(0) {}
virtual ~MsgInfoSummary() = default;
std::string msgId;
RsMailMessageId msgId;
RsPeerId srcId;
uint32_t msgflags;
std::list<uint32_t> msgtags; // that leaves 25 bits for user-defined tags.
std::string title;
int count; /* file count */
@ -253,6 +254,7 @@ struct MsgInfoSummary : RsSerializable
RS_SERIAL_PROCESS(srcId);
RS_SERIAL_PROCESS(msgflags);
RS_SERIAL_PROCESS(msgtags);
RS_SERIAL_PROCESS(title);
RS_SERIAL_PROCESS(count);

View File

@ -1290,6 +1290,12 @@ bool p3MsgService::MessageToDraft(MessageInfo &info, const std::string &msgParen
return false;
}
bool p3MsgService::getMessageTag(const std::string &msgId, Rs::Msgs::MsgTagInfo& info)
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
return locked_getMessageTag(msgId,info);
}
bool p3MsgService::getMessageTagTypes(MsgTagType& tags)
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
@ -1413,10 +1419,8 @@ bool p3MsgService::removeMessageTagType(uint32_t tagId)
return true;
}
bool p3MsgService::getMessageTag(const std::string &msgId, MsgTagInfo& info)
bool p3MsgService::locked_getMessageTag(const std::string &msgId, MsgTagInfo& info)
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
uint32_t mid = atoi(msgId.c_str());
if (mid == 0) {
std::cerr << "p3MsgService::MessageGetMsgTag: Unknown msgId " << msgId << std::endl;
@ -1740,6 +1744,10 @@ void p3MsgService::initRsMIS(RsMsgItem *msg, MsgInfoSummary &mis)
mis.title = msg->subject;
mis.count = msg->attachment.items.size();
mis.ts = msg->sendTime;
MsgTagInfo taginfo;
locked_getMessageTag(mis.msgId,taginfo);
mis.msgtags = taginfo.tagIds ;
}
void p3MsgService::initMIRsMsg(RsMsgItem *msg,const MessageInfo& info)

View File

@ -77,11 +77,11 @@ public:
bool MessageToDraft(Rs::Msgs::MessageInfo &info, const std::string &msgParentId);
bool MessageToTrash(const std::string &mid, bool bTrash);
bool getMessageTag(const std::string &msgId, Rs::Msgs::MsgTagInfo& info);
bool getMessageTagTypes(Rs::Msgs::MsgTagType& tags);
bool setMessageTagType(uint32_t tagId, std::string& text, uint32_t rgb_color);
bool removeMessageTagType(uint32_t tagId);
bool getMessageTag(const std::string &msgId, Rs::Msgs::MsgTagInfo& info);
/* set == false && tagId == 0 --> remove all */
bool setMessageTag(const std::string &msgId, uint32_t tagId, bool set);
@ -142,6 +142,7 @@ public:
private:
void sendDistantMsgItem(RsMsgItem *msgitem);
bool locked_getMessageTag(const std::string &msgId, Rs::Msgs::MsgTagInfo& info);
/** This contains the ongoing tunnel handling contacts.
* The map is indexed by the hash */

File diff suppressed because it is too large Load Diff

View File

@ -30,6 +30,9 @@
class RSTreeWidgetItemCompareRole;
class MessageWidget;
class QTreeWidgetItem;
class RsMessageModel;
class MessageSortFilterProxyModel ;
class MessagesDialog : public MainPage
{
@ -56,26 +59,33 @@ public:
void setTextColorInbox(QColor color) { mTextColorInbox = color; }
signals:
void messagesAboutToLoad();
void messagesLoaded();
protected:
bool eventFilter(QObject *obj, QEvent *ev);
void changeEvent(QEvent *e);
int getSelectedMessages(QList<QString>& mid);
public slots:
void insertMessages();
//void insertMessages();
void messagesTagsChanged();
void preModelUpdate();
void postModelUpdate();
private slots:
/** Create the context popup menu and it's submenus */
void messageTreeWidgetCustomPopupMenu(QPoint point);
void folderlistWidgetCustomPopupMenu(QPoint);
void showAuthorInPeopleTab();
void sortColumn(int col,Qt::SortOrder so);
void changeBox(int newrow);
void changeQuickView(int newrow);
void updateCurrentMessage();
void currentItemChanged(QTreeWidgetItem *item);
void clicked(QTreeWidgetItem *item, int column);
void doubleClicked(QTreeWidgetItem *item, int column);
void clicked(const QModelIndex&);
void doubleClicked(const QModelIndex&);
void currentChanged(const QModelIndex& new_proxy_index, const QModelIndex &old_proxy_index);
void newmessage();
void openAsWindow();
@ -104,33 +114,19 @@ private slots:
void tabCloseRequested(int tab);
private:
class LockUpdate
{
public:
LockUpdate (MessagesDialog *pDialog, bool bUpdate);
~LockUpdate ();
void setUpdate(bool bUpdate);
private:
MessagesDialog *m_pDialog;
bool m_bUpdate;
};
void updateInterface();
void connectActions();
void updateMessageSummaryList();
void insertMsgTxtAndFiles(QTreeWidgetItem *item = NULL, bool bSetToRead = true);
void insertMsgTxtAndFiles(const QModelIndex& proxy_index = QModelIndex());
bool getCurrentMsg(std::string &cid, std::string &mid);
void setMsgAsReadUnread(const QList<QTreeWidgetItem *> &items, bool read);
void setMsgStar(const QList<QTreeWidgetItem *> &items, bool mark);
int getSelectedMsgCount (QList<QTreeWidgetItem *> *items, QList<QTreeWidgetItem *> *itemsRead, QList<QTreeWidgetItem *> *itemsUnread, QList<QTreeWidgetItem *> *itemsStar);
bool isMessageRead(QTreeWidgetItem *item);
bool hasMessageStar(QTreeWidgetItem *item);
int getSelectedMsgCount (QList<QModelIndex> *items, QList<QModelIndex> *itemsRead, QList<QModelIndex> *itemsUnread, QList<QModelIndex> *itemsStar);
bool isMessageRead(const QModelIndex &real_index);
bool hasMessageStar(const QModelIndex &index);
void processSettings(bool load);
@ -151,14 +147,19 @@ private:
QTimer *timer;
int timerIndex;
RSTreeWidgetItemCompareRole *mMessageCompareRole;
//RSTreeWidgetItemCompareRole *mMessageCompareRole;
MessageWidget *msgWidget;
RsMessageModel *mMessageModel;
MessageSortFilterProxyModel *mMessageProxyModel;
/* Color definitions (for standard see qss.default) */
QColor mTextColorInbox;
/** Qt Designer generated object */
Ui::MessagesDialog ui;
QList<QString> mTmpSavedSelectedIds;
};
#endif

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>775</width>
<width>842</width>
<height>485</height>
</rect>
</property>
@ -454,7 +454,7 @@
<enum>Qt::NoFocus</enum>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<iconset resource="images.qrc">
<normaloff>:/icons/help_64.png</normaloff>:/icons/help_64.png</iconset>
</property>
<property name="checkable">
@ -726,7 +726,7 @@
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="RSTreeWidget" name="messageTreeWidget">
<widget class="QTreeView" name="messageTreeWidget">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
@ -745,11 +745,9 @@
<property name="expandsOnDoubleClick">
<bool>false</bool>
</property>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
<attribute name="headerCascadingSectionResizes">
<bool>true</bool>
</attribute>
</widget>
<widget class="QWidget" name="layoutWidget">
<layout class="QGridLayout" name="msgLayout"/>
@ -843,11 +841,6 @@
<extends>QLineEdit</extends>
<header location="global">gui/common/LineEditClear.h</header>
</customwidget>
<customwidget>
<class>RSTreeWidget</class>
<extends>QTreeWidget</extends>
<header>gui/common/RSTreeWidget.h</header>
</customwidget>
<customwidget>
<class>RSTabWidget</class>
<extends>QTabWidget</extends>
@ -860,8 +853,8 @@
<tabstop>listWidget</tabstop>
</tabstops>
<resources>
<include location="icons.qrc"/>
<include location="images.qrc"/>
<include location="icons.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -21,7 +21,11 @@
#ifndef _GXS_ID_TREEWIDGETITEM_H
#define _GXS_ID_TREEWIDGETITEM_H
#include <QPainter>
#include <QTimer>
#include <QApplication>
#include <retroshare/rsidentity.h>
#include <retroshare/rspeers.h>
#include "gui/common/RSTreeWidgetItem.h"
#include "gui/gxs/GxsIdDetails.h"
@ -69,4 +73,151 @@ private:
RsGxsImage mAvatar;
};
// This class is responsible of rendering authors of type RsGxsId in tree views. Used in forums, messages, etc.
class GxsIdTreeItemDelegate: public QStyledItemDelegate
{
Q_OBJECT
public:
GxsIdTreeItemDelegate()
{
mLoading = false;
mReloadPeriod = 0;
}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
// disable default icon
opt.icon = QIcon();
const QRect r = option.rect;
RsGxsId id(index.data(Qt::UserRole).toString().toStdString());
QString str;
QList<QIcon> icons;
QString comment;
QFontMetricsF fm(option.font);
float f = fm.height();
QIcon icon ;
if(!GxsIdDetails::MakeIdDesc(id, true, str, icons, comment,GxsIdDetails::ICON_TYPE_AVATAR))
{
icon = GxsIdDetails::getLoadingIcon(id);
launchAsyncLoading();
}
else
icon = *icons.begin();
QPixmap pix = icon.pixmap(r.size());
return QSize(1.2*(pix.width() + fm.width(str)),std::max(1.1*pix.height(),1.4*fm.height()));
}
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex& index) const override
{
if(!index.isValid())
{
std::cerr << "(EE) attempt to draw an invalid index." << std::endl;
return ;
}
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
// disable default icon
opt.icon = QIcon();
// draw default item
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0);
const QRect r = option.rect;
RsGxsId id(index.data(Qt::UserRole).toString().toStdString());
QString str;
QString comment;
QFontMetricsF fm(painter->font());
float f = fm.height();
QIcon icon ;
if(id.isNull())
{
str = tr("[Notification]");
icon = QIcon(":/icons/logo_128.png");
}
else if(! computeNameIconAndComment(id,str,icon,comment))
if(mReloadPeriod > 3)
{
str = tr("[Unknown]");
icon = QIcon();
}
else
{
icon = GxsIdDetails::getLoadingIcon(id);
launchAsyncLoading();
}
QPixmap pix = icon.pixmap(r.size());
const QPoint p = QPoint(r.height()/2.0, (r.height() - pix.height())/2);
// draw pixmap at center of item
painter->drawPixmap(r.topLeft() + p, pix);
painter->drawText(r.topLeft() + QPoint(r.height()+ f/2.0 + f/2.0,f*1.0), str);
}
void launchAsyncLoading() const
{
if(mLoading)
return;
mLoading = true;
++mReloadPeriod;
QTimer::singleShot(1000,this,SLOT(reload()));
}
static bool computeName(const RsGxsId& id,QString& name)
{
QList<QIcon> icons;
QString comment;
if(rsPeers->isFriend(RsPeerId(id))) // horrible trick because some widgets still use locations as IDs (e.g. messages)
name = QString::fromUtf8(rsPeers->getPeerName(RsPeerId(id)).c_str()) ;
else if(!GxsIdDetails::MakeIdDesc(id, false, name, icons, comment,GxsIdDetails::ICON_TYPE_NONE))
return false;
return true;
}
static bool computeNameIconAndComment(const RsGxsId& id,QString& name,QIcon& icon,QString& comment)
{
QList<QIcon> icons;
if(rsPeers->isFriend(RsPeerId(id))) // horrible trick because some widgets still use locations as IDs (e.g. messages)
{
name = QString::fromUtf8(rsPeers->getPeerName(RsPeerId(id)).c_str()) ;
icon = QIcon(":/icons/avatar_128.png");
}
else if(!GxsIdDetails::MakeIdDesc(id, true, name, icons, comment,GxsIdDetails::ICON_TYPE_AVATAR))
return false;
else
icon = *icons.begin();
return true;
}
private slots:
void reload() { mLoading=false; emit commitData(NULL) ; }
private:
mutable bool mLoading;
mutable int mReloadPeriod;
};
#endif

View File

@ -976,7 +976,8 @@ MessageComposer *MessageComposer::newMsg(const std::string &msgId /* = ""*/)
msgComposer->addEmptyRecipient();
if (msgId.empty() == false) {
if (!msgId.empty())
{
// fill existing message
MessageInfo msgInfo;
if (!rsMail->getMessage(msgId, msgInfo)) {

View File

@ -0,0 +1,693 @@
/*******************************************************************************
* retroshare-gui/src/gui/msgs/RsMessageModel.cpp *
* *
* Copyright 2019 by Cyril Soler <csoler@users.sourceforge.net> *
* *
* 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 <list>
#include <QApplication>
#include <QDateTime>
#include <QFontMetrics>
#include <QModelIndex>
#include <QIcon>
#include "gui/common/TagDefs.h"
#include "util/HandleRichText.h"
#include "util/DateTime.h"
#include "gui/gxs/GxsIdDetails.h"
#include "gui/gxs/GxsIdTreeWidgetItem.h"
#include "MessageModel.h"
#include "retroshare/rsexpr.h"
#include "retroshare/rsmsgs.h"
//#define DEBUG_MESSAGE_MODEL
#define IS_MESSAGE_UNREAD(flags) (flags & (RS_MSG_NEW | RS_MSG_UNREAD_BY_USER))
#define IMAGE_STAR_ON ":/images/star-on-16.png"
#define IMAGE_STAR_OFF ":/images/star-off-16.png"
std::ostream& operator<<(std::ostream& o, const QModelIndex& i);// defined elsewhere
const QString RsMessageModel::FilterString("filtered");
RsMessageModel::RsMessageModel(QObject *parent)
: QAbstractItemModel(parent)
{
mCurrentBox = BOX_NONE;
mQuickViewFilter = QUICK_VIEW_ALL;
mFilterType = FILTER_TYPE_NONE;
mFilterStrings.clear();
}
void RsMessageModel::preMods()
{
emit layoutAboutToBeChanged();
}
void RsMessageModel::postMods()
{
emit dataChanged(createIndex(0,0,(void*)NULL), createIndex(mMessages.size()-1,COLUMN_THREAD_NB_COLUMNS-1,(void*)NULL));
}
int RsMessageModel::rowCount(const QModelIndex& parent) const
{
if(!parent.isValid())
return 0;
if(parent.column() > 0)
return 0;
if(mMessages.empty()) // security. Should never happen.
return 0;
if(parent.internalPointer() == NULL)
return mMessages.size();
return 0;
}
int RsMessageModel::columnCount(const QModelIndex &parent) const
{
return COLUMN_THREAD_NB_COLUMNS ;
}
bool RsMessageModel::getMessageData(const QModelIndex& i,Rs::Msgs::MessageInfo& fmpe) const
{
if(!i.isValid())
return true;
quintptr ref = i.internalId();
uint32_t index = 0;
if(!convertInternalIdToMsgIndex(ref,index) || index >= mMessages.size())
return false ;
return rsMsgs->getMessage(mMessages[index].msgId,fmpe);
}
bool RsMessageModel::hasChildren(const QModelIndex &parent) const
{
if(!parent.isValid())
return true;
return false ;
}
bool RsMessageModel::convertMsgIndexToInternalId(uint32_t index,quintptr& id)
{
// the internal id is set to the place in the table of items. We simply shift to allow 0 to mean something special.
id = index + 1 ;
return true;
}
bool RsMessageModel::convertInternalIdToMsgIndex(quintptr ref,uint32_t& index)
{
if(ref == 0)
return false ;
index = ref - 1;
return true;
}
QModelIndex RsMessageModel::index(int row, int column, const QModelIndex & parent) const
{
if(row < 0 || column < 0 || column >= COLUMN_THREAD_NB_COLUMNS)
return QModelIndex();
quintptr ref ;
if(parent.internalId() == 0 && convertMsgIndexToInternalId(row,ref))
return createIndex(row,column,ref) ;
return QModelIndex();
}
QModelIndex RsMessageModel::parent(const QModelIndex& index) const
{
if(!index.isValid())
return QModelIndex();
return QModelIndex();
}
Qt::ItemFlags RsMessageModel::flags(const QModelIndex& index) const
{
if (!index.isValid())
return 0;
return QAbstractItemModel::flags(index);
}
QVariant RsMessageModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::DisplayRole)
switch(section)
{
case COLUMN_THREAD_DATE: return tr("Date");
case COLUMN_THREAD_AUTHOR: return tr("From");
case COLUMN_THREAD_SUBJECT: return tr("Subject");
case COLUMN_THREAD_TAGS: return tr("Tags");
default:
return QVariant();
}
if(role == Qt::DecorationRole)
switch(section)
{
case COLUMN_THREAD_STAR: return QIcon(IMAGE_STAR_ON);
case COLUMN_THREAD_READ: return QIcon(":/images/message-state-header.png");
case COLUMN_THREAD_ATTACHMENT: return QIcon(":/images/attachment.png");
default:
return QVariant();
}
if(role == Qt::ToolTipRole)
switch(section)
{
case COLUMN_THREAD_ATTACHMENT: return tr("Click to sort by attachments");
case COLUMN_THREAD_SUBJECT: return tr("Click to sort by subject");
case COLUMN_THREAD_READ: return tr("Click to sort by read");
case COLUMN_THREAD_AUTHOR: return tr("Click to sort by from");
case COLUMN_THREAD_DATE: return tr("Click to sort by date");
case COLUMN_THREAD_TAGS: return tr("Click to sort by tags");
case COLUMN_THREAD_STAR: return tr("Click to sort by star");
default:
return QVariant();
}
return QVariant();
}
QVariant RsMessageModel::data(const QModelIndex &index, int role) const
{
#ifdef DEBUG_MESSAGE_MODEL
std::cerr << "calling data(" << index << ") role=" << role << std::endl;
#endif
if(!index.isValid())
return QVariant();
switch(role)
{
case Qt::SizeHintRole: return sizeHintRole(index.column()) ;
case Qt::StatusTipRole:return QVariant();
default: break;
}
quintptr ref = (index.isValid())?index.internalId():0 ;
uint32_t entry = 0;
#ifdef DEBUG_MESSAGE_MODEL
std::cerr << "data(" << index << ")" ;
#endif
if(!ref)
{
#ifdef DEBUG_MESSAGE_MODEL
std::cerr << " [empty]" << std::endl;
#endif
return QVariant() ;
}
if(!convertInternalIdToMsgIndex(ref,entry) || entry >= mMessages.size())
{
#ifdef DEBUG_MESSAGE_MODEL
std::cerr << "Bad pointer: " << (void*)ref << std::endl;
#endif
return QVariant() ;
}
const Rs::Msgs::MsgInfoSummary& fmpe(mMessages[entry]);
if(role == Qt::FontRole)
{
QFont font ;
font.setBold(fmpe.msgflags & (RS_MSG_NEW | RS_MSG_UNREAD_BY_USER));
return QVariant(font);
}
#ifdef DEBUG_MESSAGE_MODEL
std::cerr << " [ok]" << std::endl;
#endif
switch(role)
{
case Qt::DisplayRole: return displayRole (fmpe,index.column()) ;
case Qt::DecorationRole: return decorationRole(fmpe,index.column()) ;
case Qt::ToolTipRole: return toolTipRole (fmpe,index.column()) ;
case Qt::UserRole: return userRole (fmpe,index.column()) ;
case Qt::TextColorRole: return textColorRole (fmpe,index.column()) ;
case Qt::BackgroundRole: return backgroundRole(fmpe,index.column()) ;
case FilterRole: return filterRole (fmpe,index.column()) ;
case StatusRole: return statusRole (fmpe,index.column()) ;
case SortRole: return sortRole (fmpe,index.column()) ;
case MsgFlagsRole: return fmpe.msgflags ;
case UnreadRole: return fmpe.msgflags & (RS_MSG_NEW | RS_MSG_UNREAD_BY_USER);
case MsgIdRole: return QString::fromStdString(fmpe.msgId) ;
case SrcIdRole: return QString::fromStdString(fmpe.srcId.toStdString()) ;
default:
return QVariant();
}
}
QVariant RsMessageModel::textColorRole(const Rs::Msgs::MsgInfoSummary& fmpe,int column) const
{
Rs::Msgs::MsgTagType tags;
rsMsgs->getMessageTagTypes(tags);
for(auto it(fmpe.msgtags.begin());it!=fmpe.msgtags.end();++it)
for(auto it2(tags.types.begin());it2!=tags.types.end();++it2)
if(it2->first == *it)
return QColor(it2->second.second);
return QVariant();
}
QVariant RsMessageModel::statusRole(const Rs::Msgs::MsgInfoSummary& fmpe,int column) const
{
// if(column != COLUMN_THREAD_DATA)
// return QVariant();
return QVariant();//fmpe.mMsgStatus);
}
bool RsMessageModel::passesFilter(const Rs::Msgs::MsgInfoSummary& fmpe,int column) const
{
QString s ;
bool passes_strings = true ;
if(!mFilterStrings.empty())
{
switch(mFilterType)
{
case FILTER_TYPE_SUBJECT: s = displayRole(fmpe,COLUMN_THREAD_SUBJECT).toString();
break;
case FILTER_TYPE_FROM: s = sortRole(fmpe,COLUMN_THREAD_AUTHOR).toString();
if(s.isNull())
passes_strings = false;
break;
case FILTER_TYPE_DATE: s = displayRole(fmpe,COLUMN_THREAD_DATE).toString();
break;
case FILTER_TYPE_CONTENT: {
Rs::Msgs::MessageInfo minfo;
rsMsgs->getMessage(fmpe.msgId,minfo);
s = QTextDocument(QString::fromUtf8(minfo.msg.c_str())).toPlainText();
}
break;
case FILTER_TYPE_TAGS: s = displayRole(fmpe,COLUMN_THREAD_TAGS).toString();
break;
case FILTER_TYPE_ATTACHMENTS:
{
Rs::Msgs::MessageInfo minfo;
rsMsgs->getMessage(fmpe.msgId,minfo);
for(auto it(minfo.files.begin());it!=minfo.files.end();++it)
s += QString::fromUtf8((*it).fname.c_str())+" ";
}
};
}
if(!s.isNull())
for(auto iter(mFilterStrings.begin()); iter != mFilterStrings.end(); ++iter)
passes_strings = passes_strings && s.contains(*iter,Qt::CaseInsensitive);
bool passes_quick_view =
(mQuickViewFilter==QUICK_VIEW_ALL)
|| (std::find(fmpe.msgtags.begin(),fmpe.msgtags.end(),mQuickViewFilter) != fmpe.msgtags.end())
|| (mQuickViewFilter==QUICK_VIEW_STARRED && (fmpe.msgflags & RS_MSG_STAR))
|| (mQuickViewFilter==QUICK_VIEW_SYSTEM && (fmpe.msgflags & RS_MSG_SYSTEM));
std::cerr << "Passes filter: type=" << mFilterType << " s=\"" << s.toStdString()
<< "MsgFlags=" << fmpe.msgflags << " msgtags=" ;
foreach(uint32_t i,fmpe.msgtags) std::cerr << i << " " ;
std::cerr << "\" strings:" << passes_strings << " quick_view:" << passes_quick_view << std::endl;
return passes_quick_view && passes_strings;
}
QVariant RsMessageModel::filterRole(const Rs::Msgs::MsgInfoSummary& fmpe,int column) const
{
if(passesFilter(fmpe,column))
return QVariant(FilterString);
return QVariant(QString());
}
uint32_t RsMessageModel::updateFilterStatus(ForumModelIndex i,int column,const QStringList& strings)
{
QString s ;
uint32_t count = 0;
return count;
}
void RsMessageModel::setFilter(FilterType filter_type, const QStringList& strings)
{
std::cerr << "Setting filter to filter_type=" << int(filter_type) << " and strings to " ;
foreach(const QString& str,strings)
std::cerr << "\"" << str.toStdString() << "\" " ;
std::cerr << std::endl;
preMods();
mFilterType = filter_type;
mFilterStrings = strings;
postMods();
}
QVariant RsMessageModel::toolTipRole(const Rs::Msgs::MsgInfoSummary& fmpe,int column) const
{
if(column == COLUMN_THREAD_AUTHOR)
{
QString str,comment ;
QList<QIcon> icons;
if(!GxsIdDetails::MakeIdDesc(RsGxsId(fmpe.srcId.toStdString()), true, str, icons, comment,GxsIdDetails::ICON_TYPE_AVATAR))
return QVariant();
int S = QFontMetricsF(QApplication::font()).height();
QImage pix( (*icons.begin()).pixmap(QSize(4*S,4*S)).toImage());
QString embeddedImage;
if(RsHtml::makeEmbeddedImage(pix.scaled(QSize(4*S,4*S), Qt::KeepAspectRatio, Qt::SmoothTransformation), embeddedImage, 8*S * 8*S))
comment = "<table><tr><td>" + embeddedImage + "</td><td>" + comment + "</td></table>";
return comment;
}
return QVariant();
}
QVariant RsMessageModel::backgroundRole(const Rs::Msgs::MsgInfoSummary &fmpe, int column) const
{
return QVariant();
}
QVariant RsMessageModel::sizeHintRole(int col) const
{
float factor = QFontMetricsF(QApplication::font()).height()/14.0f ;
switch(col)
{
default:
case COLUMN_THREAD_SUBJECT: return QVariant( QSize(factor * 170, factor*14 ));
case COLUMN_THREAD_DATE: return QVariant( QSize(factor * 75 , factor*14 ));
case COLUMN_THREAD_AUTHOR: return QVariant( QSize(factor * 75 , factor*14 ));
}
}
QVariant RsMessageModel::authorRole(const Rs::Msgs::MsgInfoSummary& fmpe,int column) const
{
return QVariant();
}
QVariant RsMessageModel::sortRole(const Rs::Msgs::MsgInfoSummary& fmpe,int column) const
{
switch(column)
{
case COLUMN_THREAD_DATE: return QVariant(QString::number(fmpe.ts)); // we should probably have leading zeroes here
case COLUMN_THREAD_READ: return QVariant((bool)IS_MESSAGE_UNREAD(fmpe.msgflags));
case COLUMN_THREAD_STAR: return QVariant((fmpe.msgflags & RS_MSG_STAR)? 1:0);
case COLUMN_THREAD_AUTHOR:{
QString name;
if(GxsIdTreeItemDelegate::computeName(RsGxsId(fmpe.srcId.toStdString()),name))
return name;
}
default:
return displayRole(fmpe,column);
}
}
QVariant RsMessageModel::displayRole(const Rs::Msgs::MsgInfoSummary& fmpe,int col) const
{
switch(col)
{
case COLUMN_THREAD_SUBJECT: return QVariant(QString::fromUtf8(fmpe.title.c_str()));
case COLUMN_THREAD_ATTACHMENT:return QVariant(QString::number(fmpe.count));
case COLUMN_THREAD_STAR:
case COLUMN_THREAD_READ:return QVariant();
case COLUMN_THREAD_DATE:{
QDateTime qtime;
qtime.setTime_t(fmpe.ts);
return QVariant(DateTime::formatDateTime(qtime));
}
case COLUMN_THREAD_TAGS:{
// Tags
Rs::Msgs::MsgTagInfo tagInfo;
rsMsgs->getMessageTag(fmpe.msgId, tagInfo);
Rs::Msgs::MsgTagType Tags;
rsMsgs->getMessageTagTypes(Tags);
QString text;
// build tag names
std::map<uint32_t, std::pair<std::string, uint32_t> >::iterator Tag;
for (auto tagit = tagInfo.tagIds.begin(); tagit != tagInfo.tagIds.end(); ++tagit)
{
if (!text.isNull())
text += ",";
auto Tag = Tags.types.find(*tagit);
if (Tag != Tags.types.end())
text += TagDefs::name(Tag->first, Tag->second.first);
else
std::cerr << "(WW) unknown tag " << (int)Tag->first << " in message " << fmpe.msgId << std::endl;
}
return text;
}
case COLUMN_THREAD_AUTHOR: return QVariant();
default:
return QVariant("[ TODO ]");
}
return QVariant("[ERROR]");
}
QVariant RsMessageModel::userRole(const Rs::Msgs::MsgInfoSummary& fmpe,int col) const
{
switch(col)
{
case COLUMN_THREAD_AUTHOR: return QVariant(QString::fromStdString(fmpe.srcId.toStdString()));
case COLUMN_THREAD_MSGID: return QVariant(QString::fromStdString(fmpe.msgId));
default:
return QVariant();
}
}
QVariant RsMessageModel::decorationRole(const Rs::Msgs::MsgInfoSummary& fmpe,int col) const
{
if(col == COLUMN_THREAD_READ)
if(fmpe.msgflags & (RS_MSG_NEW | RS_MSG_UNREAD_BY_USER))
return QIcon(":/images/message-state-unread.png");
else
return QIcon(":/images/message-state-read.png");
if(col == COLUMN_THREAD_SUBJECT)
{
if(fmpe.msgflags & RS_MSG_NEW ) return QIcon(":/images/message-state-new.png");
if(fmpe.msgflags & RS_MSG_USER_REQUEST) return QIcon(":/images/user/user_request16.png");
if(fmpe.msgflags & RS_MSG_FRIEND_RECOMMENDATION) return QIcon(":/images/user/friend_suggestion16.png");
if(fmpe.msgflags & RS_MSG_PUBLISH_KEY) return QIcon(":/images/share-icon-16.png");
if(fmpe.msgflags & RS_MSG_UNREAD_BY_USER)
{
if((fmpe.msgflags & (RS_MSG_REPLIED | RS_MSG_FORWARDED)) == RS_MSG_REPLIED) return QIcon(":/images/message-mail-replied.png");
if((fmpe.msgflags & (RS_MSG_REPLIED | RS_MSG_FORWARDED)) == RS_MSG_FORWARDED) return QIcon(":/images/message-mail-forwarded.png");
if((fmpe.msgflags & (RS_MSG_REPLIED | RS_MSG_FORWARDED)) == (RS_MSG_REPLIED | RS_MSG_FORWARDED)) return QIcon(":/images/message-mail-replied-forw.png");
return QIcon(":/images/message-mail.png");
}
if((fmpe.msgflags & (RS_MSG_REPLIED | RS_MSG_FORWARDED)) == RS_MSG_REPLIED) return QIcon(":/images/message-mail-replied-read.png");
if((fmpe.msgflags & (RS_MSG_REPLIED | RS_MSG_FORWARDED)) == RS_MSG_FORWARDED) return QIcon(":/images/message-mail-forwarded-read.png");
if((fmpe.msgflags & (RS_MSG_REPLIED | RS_MSG_FORWARDED)) == (RS_MSG_REPLIED | RS_MSG_FORWARDED)) return QIcon(":/images/message-mail-replied-forw-read.png");
return QIcon(":/images/message-mail-read.png");
}
if(col == COLUMN_THREAD_STAR)
return QIcon((fmpe.msgflags & RS_MSG_STAR) ? (IMAGE_STAR_ON ): (IMAGE_STAR_OFF));
bool isNew = fmpe.msgflags & (RS_MSG_NEW | RS_MSG_UNREAD_BY_USER);
if(col == COLUMN_THREAD_READ)
return QIcon(isNew ? ":/images/message-state-unread.png": ":/images/message-state-read.png");
return QVariant();
}
void RsMessageModel::clear()
{
preMods();
mMessages.clear();
postMods();
emit messagesLoaded();
}
void RsMessageModel::setMessages(const std::list<Rs::Msgs::MsgInfoSummary>& msgs)
{
preMods();
beginRemoveRows(QModelIndex(),0,mMessages.size()-1);
endRemoveRows();
mMessages.clear();
mMessagesMap.clear();
for(auto it(msgs.begin());it!=msgs.end();++it)
{
mMessagesMap[(*it).msgId] = mMessages.size();
mMessages.push_back(*it);
}
// now update prow for all posts
#ifdef DEBUG_MESSAGE_MODEL
debug_dump();
#endif
beginInsertRows(QModelIndex(),0,mMessages.size()-1);
endInsertRows();
postMods();
emit messagesLoaded();
}
void RsMessageModel::setCurrentBox(BoxName bn)
{
if(mCurrentBox != bn)
{
mCurrentBox = bn;
updateMessages();
}
}
void RsMessageModel::setQuickViewFilter(QuickViewFilter fn)
{
if(fn != mQuickViewFilter)
{
std::cerr << "Changing new quickview filter to " << fn << std::endl;
preMods();
mQuickViewFilter = fn ;
postMods();
}
}
void RsMessageModel::getMessageSummaries(BoxName box,std::list<Rs::Msgs::MsgInfoSummary>& msgs)
{
rsMsgs->getMessageSummaries(msgs);
// filter out messages that are not in the right box.
for(auto it(msgs.begin());it!=msgs.end();)
{
bool ok = false;
switch(box)
{
case BOX_INBOX : ok = (it->msgflags & RS_MSG_BOXMASK) == RS_MSG_INBOX ; break ;
case BOX_SENT : ok = (it->msgflags & RS_MSG_BOXMASK) == RS_MSG_SENTBOX; break ;
case BOX_OUTBOX : ok = (it->msgflags & RS_MSG_BOXMASK) == RS_MSG_OUTBOX ; break ;
case BOX_DRAFTS : ok = (it->msgflags & RS_MSG_BOXMASK) == RS_MSG_DRAFTBOX ; break ;
case BOX_TRASH : ok = (it->msgflags & RS_MSG_TRASH) ; break ;
default:
++it;
continue;
}
if(ok)
++it;
else
it = msgs.erase(it) ;
}
}
void RsMessageModel::updateMessages()
{
emit messagesAboutToLoad();
std::list<Rs::Msgs::MsgInfoSummary> msgs;
getMessageSummaries(mCurrentBox,msgs);
setMessages(msgs);
emit messagesLoaded();
}
static bool decreasing_time_comp(const std::pair<time_t,RsGxsMessageId>& e1,const std::pair<time_t,RsGxsMessageId>& e2) { return e2.first < e1.first ; }
void RsMessageModel::setMsgReadStatus(const QModelIndex& i,bool read_status)
{
if(!i.isValid())
return ;
preMods();
rsMsgs->MessageRead(i.data(MsgIdRole).toString().toStdString(),!read_status);
emit dataChanged(i.sibling(i.row(),0),i.sibling(i.row(),COLUMN_THREAD_NB_COLUMNS-1));
}
void RsMessageModel::setMsgStar(const QModelIndex& i,bool star)
{
preMods();
rsMsgs->MessageStar(i.data(MsgIdRole).toString().toStdString(),star);
emit dataChanged(i.sibling(i.row(),0),i.sibling(i.row(),COLUMN_THREAD_NB_COLUMNS-1));
}
QModelIndex RsMessageModel::getIndexOfMessage(const std::string& mid) const
{
// Brutal search. This is not so nice, so dont call that in a loop! If too costly, we'll use a map.
auto it = mMessagesMap.find(mid);
if(it == mMessagesMap.end() || it->second >= mMessages.size())
return QModelIndex();
quintptr ref ;
convertMsgIndexToInternalId(it->second,ref);
return createIndex(it->second,0,ref);
}
void RsMessageModel::debug_dump() const
{
for(auto it(mMessages.begin());it!=mMessages.end();++it)
std::cerr << "Id: " << it->msgId << ": from " << it->srcId << ": flags=" << it->msgflags << ": title=\"" << it->title << "\"" << std::endl;
}

View File

@ -0,0 +1,189 @@
/*******************************************************************************
* retroshare-gui/src/gui/msgs/RsMessageModel.h *
* *
* Copyright 2019 by Cyril Soler <csoler@users.sourceforge.net> *
* *
* 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/>. *
* *
*******************************************************************************/
#pragma once
#include <QModelIndex>
#include <QColor>
#include "retroshare/rsmsgs.h"
// This class holds the actual hierarchy of posts, represented by identifiers
// It is responsible for auto-updating when necessary and holds a mutex to allow the Model to
// safely access the data.
// The model contains a post in place 0 that is the parent of all posts.
typedef uint32_t ForumModelIndex;
// This class is the item model used by Qt to display the information
class RsMessageModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit RsMessageModel(QObject *parent = NULL);
~RsMessageModel(){}
enum BoxName {
BOX_NONE = 0x00,
BOX_INBOX = 0x01,
BOX_OUTBOX = 0x02,
BOX_DRAFTS = 0x03,
BOX_SENT = 0x04,
BOX_TRASH = 0x05
};
enum Columns {
COLUMN_THREAD_STAR = 0x00,
COLUMN_THREAD_ATTACHMENT = 0x01,
COLUMN_THREAD_SUBJECT = 0x02,
COLUMN_THREAD_READ = 0x03,
COLUMN_THREAD_AUTHOR = 0x04,
COLUMN_THREAD_DATE = 0x05,
COLUMN_THREAD_TAGS = 0x06,
COLUMN_THREAD_MSGID = 0x07,
COLUMN_THREAD_NB_COLUMNS = 0x08,
COLUMN_THREAD_CONTENT = 0x08,
};
enum QuickViewFilter {
QUICK_VIEW_ALL = 0x00,
QUICK_VIEW_IMPORTANT = 0x01, // These numbers have been carefuly chosen to match the ones in rsmsgs.h
QUICK_VIEW_WORK = 0x02,
QUICK_VIEW_PERSONAL = 0x03,
QUICK_VIEW_TODO = 0x04,
QUICK_VIEW_LATER = 0x05,
QUICK_VIEW_STARRED = 0x06,
QUICK_VIEW_SYSTEM = 0x07,
QUICK_VIEW_USER = 100
};
enum FilterType {
FILTER_TYPE_NONE = 0x00,
FILTER_TYPE_SUBJECT = 0x01, // These numbers have been carefuly chosen to match the ones in rsmsgs.h
FILTER_TYPE_FROM = 0x02,
FILTER_TYPE_DATE = 0x03,
FILTER_TYPE_CONTENT = 0x04,
FILTER_TYPE_TAGS = 0x05,
FILTER_TYPE_ATTACHMENTS = 0x06,
};
enum Roles{ SortRole = Qt::UserRole+1,
StatusRole = Qt::UserRole+2,
UnreadRole = Qt::UserRole+3,
FilterRole = Qt::UserRole+4,
MsgIdRole = Qt::UserRole+5,
MsgFlagsRole = Qt::UserRole+6,
SrcIdRole = Qt::UserRole+7,
};
QModelIndex root() const{ return createIndex(0,0,(void*)NULL) ;}
QModelIndex getIndexOfMessage(const std::string &mid) const;
static const QString FilterString ;
static void getMessageSummaries(BoxName box, std::list<Rs::Msgs::MsgInfoSummary>& msgs);
// This method will asynchroneously update the data
void setCurrentBox(BoxName bn) ;
void setQuickViewFilter(QuickViewFilter fn) ;
const RsMessageId& currentMessageId() const;
void setFilter(FilterType filter_type, const QStringList& strings) ;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex& child) const override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool getMessageData(const QModelIndex& i,Rs::Msgs::MessageInfo& fmpe) const;
void clear() ;
QVariant sizeHintRole (int col) const;
QVariant displayRole (const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant decorationRole(const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant toolTipRole (const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant userRole (const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant statusRole (const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant authorRole (const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant sortRole (const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant fontRole (const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant filterRole (const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant textColorRole (const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
QVariant backgroundRole(const Rs::Msgs::MsgInfoSummary& fmpe, int col) const;
/*!
* \brief debug_dump
* Dumps the hierarchy of posts in the terminal, to allow checking whether the internal representation is correct.
*/
void debug_dump() const;
// control over message flags and so on. This is handled by the model because it will allow it to update accordingly
void setMsgReadStatus(const QModelIndex& i, bool read_status);
void setMsgStar(const QModelIndex& index,bool star) ;
public slots:
void updateMessages();
signals:
void messagesLoaded(); // emitted after the messages have been set. Can be used to updated the UI.
void messagesAboutToLoad();
private:
bool passesFilter(const Rs::Msgs::MsgInfoSummary& fmpe,int column) const;
void preMods() ;
void postMods() ;
void *getParentRef(void *ref,int& row) const;
void *getChildRef(void *ref,int row) const;
//bool hasIndex(int row,int column,const QModelIndex& parent)const;
int getChildrenCount(void *ref) const;
static bool convertMsgIndexToInternalId(uint32_t entry,quintptr& ref);
static bool convertInternalIdToMsgIndex(quintptr ref,uint32_t& index);
uint32_t updateFilterStatus(ForumModelIndex i,int column,const QStringList& strings);
void setMessages(const std::list<Rs::Msgs::MsgInfoSummary>& msgs);
QColor mTextColorRead ;
QColor mTextColorUnread ;
QColor mTextColorUnreadChildren;
QColor mTextColorNotSubscribed ;
QColor mTextColorMissing ;
BoxName mCurrentBox ;
QuickViewFilter mQuickViewFilter ;
QStringList mFilterStrings;
FilterType mFilterType;
std::vector<Rs::Msgs::MsgInfoSummary> mMessages;
std::map<std::string,uint32_t> mMessagesMap;
};

View File

@ -600,8 +600,8 @@ void MessageWidget::fill(const std::string &msgId)
link = RetroShareLink::createMessage(msgInfo.rspeerid_srcId, "");
}
if ((msgInfo.msgflags & RS_MSG_SYSTEM) && msgInfo.rspeerid_srcId == ownId) {
ui.fromText->setText("RetroShare");
if (((msgInfo.msgflags & RS_MSG_SYSTEM) && msgInfo.rspeerid_srcId == ownId) || msgInfo.rspeerid_srcId.isNull()) {
ui.fromText->setText("[Notification]");
if (toolButtonReply) toolButtonReply->setEnabled(false);
} else {
ui.fromText->setText(link.toHtml());

View File

@ -447,6 +447,7 @@ HEADERS += rshare.h \
gui/msgs/MessageComposer.h \
gui/msgs/MessageWindow.h \
gui/msgs/MessageWidget.h \
gui/msgs/MessageModel.h \
gui/msgs/TagsMenu.h \
gui/msgs/textformat.h \
gui/msgs/MessageUserNotify.h \
@ -802,6 +803,7 @@ SOURCES += main.cpp \
gui/msgs/MessageComposer.cpp \
gui/msgs/MessageWidget.cpp \
gui/msgs/MessageWindow.cpp \
gui/msgs/MessageModel.cpp \
gui/msgs/TagsMenu.cpp \
gui/msgs/MessageUserNotify.cpp \
gui/common/RsButtonOnText.cpp \