Merge pull request #2340 from PhenomRetroShare/Fix_SIGSEGV_InMessage

Fix SIGSEGV in Messages changing junk, start or read status.
This commit is contained in:
defnax 2021-02-19 18:35:54 +01:00 committed by GitHub
commit ddec32ac12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 130 additions and 61 deletions

View File

@ -680,28 +680,93 @@ void RsMessageModel::setMsgReadStatus(const QModelIndex& i,bool read_status)
if(!i.isValid())
return ;
preMods();
rsMsgs->MessageRead(i.data(MsgIdRole).toString().toStdString(),!read_status);
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));
emit dataChanged(i,i);
}
void RsMessageModel::setMsgsReadStatus(const QModelIndexList& mil,bool read_status)
{
//Get all msgId before changing model else Index are invalid and provoc SIGSEGV
QVector<std::string> list;
int start = rowCount(), stop = 0;
for(auto& it : mil)
if (it.isValid())
{
list.append(it.data(MsgIdRole).toString().toStdString());
start = std::min(start, it.row());
stop = std::max(stop, it.row());
}
preMods();
for(auto& it : list)
rsMsgs->MessageRead(it,!read_status);
emit dataChanged(createIndex(start,0),createIndex(stop,RsMessageModel::columnCount()-1));
}
void RsMessageModel::setMsgStar(const QModelIndex& i,bool star)
{
preMods();
rsMsgs->MessageStar(i.data(MsgIdRole).toString().toStdString(),star);
if(!i.isValid())
return ;
emit dataChanged(i.sibling(i.row(),0),i.sibling(i.row(),COLUMN_THREAD_NB_COLUMNS-1));
preMods();
rsMsgs->MessageStar(i.data(MsgIdRole).toString().toStdString(),star);
emit dataChanged(i,i);
}
void RsMessageModel::setMsgsStar(const QModelIndexList& mil,bool star)
{
//Get all msgId before changing model else Index are invalid and provoc SIGSEGV
QVector<std::string> list;
int start = rowCount(), stop = 0;
for(auto& it : mil)
if (it.isValid())
{
list.append(it.data(MsgIdRole).toString().toStdString());
start = std::min(start, it.row());
stop = std::max(stop, it.row());
}
preMods();
for(auto& it : list)
rsMsgs->MessageStar(it,star);
emit dataChanged(createIndex(start,0),createIndex(stop,RsMessageModel::columnCount()-1));
}
void RsMessageModel::setMsgJunk(const QModelIndex& i,bool junk)
{
preMods();
rsMsgs->MessageJunk(i.data(MsgIdRole).toString().toStdString(),junk);
if(!i.isValid())
return ;
emit dataChanged(i.sibling(i.row(),0),i.sibling(i.row(),COLUMN_THREAD_NB_COLUMNS-1));
preMods();
rsMsgs->MessageJunk(i.data(MsgIdRole).toString().toStdString(),junk);
emit dataChanged(i,i);
}
void RsMessageModel::setMsgsJunk(const QModelIndexList& mil,bool junk)
{
//Get all msgId before changing model else Index are invalid and provoc SIGSEGV
QVector<std::string> list;
int start = rowCount(), stop = 0;
for(auto& it : mil)
if (it.isValid())
{
list.append(it.data(MsgIdRole).toString().toStdString());
start = std::min(start, it.row());
stop = std::max(stop, it.row());
}
preMods();
for(auto& it : list)
rsMsgs->MessageJunk(it,junk);
emit dataChanged(createIndex(start,0),createIndex(stop,RsMessageModel::columnCount()-1));
}
QModelIndex RsMessageModel::getIndexOfMessage(const std::string& mid) const
{

View File

@ -150,8 +150,11 @@ public:
// 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) ;
void setMsgJunk(const QModelIndex& index,bool junk) ;
void setMsgsReadStatus(const QModelIndexList& mil, bool read_status);
void setMsgStar(const QModelIndex& i,bool star) ;
void setMsgsStar(const QModelIndexList& mil,bool star) ;
void setMsgJunk(const QModelIndex& i,bool junk) ;
void setMsgsJunk(const QModelIndexList& mil,bool junk) ;
public slots:
void updateMessages();

View File

@ -98,30 +98,34 @@
class MessageSortFilterProxyModel: public QSortFilterProxyModel
{
public:
MessageSortFilterProxyModel(QObject *parent = NULL): QSortFilterProxyModel(parent), m_sortingEnabled(false)
{
setDynamicSortFilter(false); // causes crashes when true
}
bool lessThan(const QModelIndex& left, const QModelIndex& right) const override
{
return left.data(RsMessageModel::SortRole) < right.data(RsMessageModel::SortRole) ;
}
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
{
return sourceModel()->index(source_row,0,source_parent).data(RsMessageModel::FilterRole).toString() == RsMessageModel::FilterString ;
}
MessageSortFilterProxyModel(QObject *parent = NULL)
: QSortFilterProxyModel(parent), m_sortingEnabled(false)
{
setDynamicSortFilter(false); // causes crashes when true
}
void sort( int column, Qt::SortOrder order = Qt::AscendingOrder ) override
{
if(m_sortingEnabled)
return QSortFilterProxyModel::sort(column,order) ;
if(m_sortingEnabled)
return QSortFilterProxyModel::sort(column,order) ;
}
void setSortingEnabled(bool b) { m_sortingEnabled = b ; }
protected:
bool lessThan(const QModelIndex& left, const QModelIndex& right) const override
{
return sourceModel()->data(left, RsMessageModel::SortRole) < sourceModel()->data(right, RsMessageModel::SortRole) ;
}
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
{
//No need Column as Model filter check with filter selection
return sourceModel()->index(source_row,0,source_parent).data(RsMessageModel::FilterRole).toString() == RsMessageModel::FilterString ;
}
void setSortingEnabled(bool b) { m_sortingEnabled = b ; }
private:
bool m_sortingEnabled;
bool m_sortingEnabled;
};
/** Constructor */
@ -260,7 +264,7 @@ MessagesDialog::MessagesDialog(QWidget *parent)
<p>Generally, you may use messages to recommend files to your friends by pasting file links, \
or recommend friend nodes to other friend nodes, in order to strengthen your network, or send feedback \
to a channel's owner.</p> \
").arg(QString::number(2*S)).arg(QString::number(S)) ;
").arg(QString::number(2*S), QString::number(S)) ;
registerHelpButton(ui.helpButton,help_str,"MessagesDialog") ;
@ -538,14 +542,14 @@ int MessagesDialog::getSelectedMsgCount (QList<QModelIndex> *items, QList<QModel
{
QModelIndexList qmil = ui.messageTreeWidget->selectionModel()->selectedRows();
if (items) items->clear();
if (itemsRead) itemsRead->clear();
if (itemsUnread) itemsUnread->clear();
if (itemsStar) itemsStar->clear();
if (itemsJunk) itemsJunk->clear();
if (items) items->clear();
if (itemsRead) itemsRead->clear();
if (itemsUnread) itemsUnread->clear();
if (itemsStar) itemsStar->clear();
if (itemsJunk) itemsJunk->clear();
foreach(const QModelIndex& m, qmil)
{
foreach(const QModelIndex& m, qmil)
{
if (items)
items->append(m);
@ -556,9 +560,12 @@ int MessagesDialog::getSelectedMsgCount (QList<QModelIndex> *items, QList<QModel
if (itemsStar && m.data(RsMessageModel::MsgFlagsRole).toInt() & RS_MSG_STAR)
itemsStar->append(m);
}
return qmil.size();
if (itemsJunk && m.data(RsMessageModel::MsgFlagsRole).toInt() & RS_MSG_SPAM)
itemsJunk->append(m);
}
return qmil.size();
}
bool MessagesDialog::isMessageRead(const QModelIndex& real_index)
@ -653,7 +660,7 @@ void MessagesDialog::messageTreeWidgetCustomPopupMenu(QPoint /*point*/)
action = contextMnu.addAction(tr("Mark as Junk"));
action->setCheckable(true);
action->setChecked(itemsStar.size());
action->setChecked(itemsJunk.size());
connect(action, SIGNAL(triggered(bool)), this, SLOT(markWithJunk(bool)));
contextMnu.addSeparator();
@ -1008,40 +1015,34 @@ void MessagesDialog::updateCurrentMessage()
void MessagesDialog::markAsRead()
{
QList<QModelIndex> itemsUnread;
getSelectedMsgCount (NULL, NULL, &itemsUnread, NULL, NULL);
QModelIndexList lst = ui.messageTreeWidget->selectionModel()->selectedRows();
foreach(const QModelIndex& index,itemsUnread)
mMessageModel->setMsgReadStatus(index,true);
mMessageModel->setMsgsReadStatus(lst,true);
updateMessageSummaryList();
updateMessageSummaryList();
}
void MessagesDialog::markAsUnread()
{
QList<QModelIndex> itemsRead;
getSelectedMsgCount (NULL, &itemsRead, NULL, NULL, NULL);
QModelIndexList lst = ui.messageTreeWidget->selectionModel()->selectedRows();
foreach(const QModelIndex& index,itemsRead)
mMessageModel->setMsgReadStatus(index,false);
mMessageModel->setMsgsReadStatus(lst,false);
updateMessageSummaryList();
updateMessageSummaryList();
}
void MessagesDialog::markWithStar(bool checked)
{
QModelIndexList lst = ui.messageTreeWidget->selectionModel()->selectedRows();
QModelIndexList lst = ui.messageTreeWidget->selectionModel()->selectedRows();
foreach(const QModelIndex& index,lst)
mMessageModel->setMsgStar(index, checked);
mMessageModel->setMsgsStar(lst, checked);
}
void MessagesDialog::markWithJunk(bool checked)
{
QModelIndexList lst = ui.messageTreeWidget->selectionModel()->selectedRows();
QModelIndexList lst = ui.messageTreeWidget->selectionModel()->selectedRows();
foreach(const QModelIndex& index,lst)
mMessageModel->setMsgJunk(index, checked);
mMessageModel->setMsgsJunk(lst, checked);
}
void MessagesDialog::insertMsgTxtAndFiles(const QModelIndex& proxy_index)

View File

@ -48,9 +48,9 @@ public:
/** Default Destructor */
~MessagesDialog();
virtual QIcon iconPixmap() const { return QIcon(IMAGE_MESSAGES) ; } //MainPage
virtual QString pageName() const { return tr("Mail") ; } //MainPage
virtual QString helpText() const { return ""; } //MainPage
virtual QIcon iconPixmap() const override { return QIcon(IMAGE_MESSAGES) ; } //MainPage
virtual QString pageName() const override { return tr("Mail") ; } //MainPage
virtual QString helpText() const override { return ""; } //MainPage
// replaced by shortcut
// virtual void keyPressEvent(QKeyEvent *) ;
@ -60,8 +60,8 @@ public:
void setTextColorInbox(QColor color) { mTextColorInbox = color; }
protected:
virtual UserNotify *createUserNotify(QObject *parent) override;
bool eventFilter(QObject *obj, QEvent *ev);
virtual UserNotify *createUserNotify(QObject *parent) override; //MainPage
bool eventFilter(QObject *obj, QEvent *ev) override; //MainPage
int getSelectedMessages(QList<QString>& mid);
public slots: