2019-06-25 17:08:31 -04:00
/*******************************************************************************
2019-06-28 05:12:44 -04:00
* retroshare - gui / src / gui / msgs / RsFriendListModel . cpp *
2019-06-25 17:08:31 -04:00
* *
* 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>
2019-06-28 05:12:44 -04:00
# include "gui/common/StatusDefs.h"
2019-06-25 17:08:31 -04:00
# include "util/HandleRichText.h"
# include "util/DateTime.h"
2019-06-28 05:12:44 -04:00
# include "gui/common/FriendListModel.h"
2019-06-25 17:08:31 -04:00
# include "gui/gxs/GxsIdDetails.h"
# include "gui/gxs/GxsIdTreeWidgetItem.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
2019-06-28 05:12:44 -04:00
const QString RsFriendListModel : : FilterString ( " filtered " ) ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
RsFriendListModel : : RsFriendListModel ( QObject * parent )
2019-06-25 17:08:31 -04:00
: QAbstractItemModel ( parent )
{
2019-07-27 17:42:39 -04:00
mDisplayGroups = true ;
2019-06-25 17:08:31 -04:00
mFilterStrings . clear ( ) ;
}
2019-06-28 10:20:26 -04:00
void RsFriendListModel : : setDisplayGroups ( bool b )
{
mDisplayGroups = b ;
// should update here
}
2019-06-28 05:12:44 -04:00
void RsFriendListModel : : preMods ( )
2019-06-25 17:08:31 -04:00
{
emit layoutAboutToBeChanged ( ) ;
}
2019-06-28 05:12:44 -04:00
void RsFriendListModel : : postMods ( )
2019-06-25 17:08:31 -04:00
{
2019-06-28 05:12:44 -04:00
if ( mDisplayGroups )
emit dataChanged ( createIndex ( 0 , 0 , ( void * ) NULL ) , createIndex ( mGroups . size ( ) - 1 , COLUMN_THREAD_NB_COLUMNS - 1 , ( void * ) NULL ) ) ;
else
emit dataChanged ( createIndex ( 0 , 0 , ( void * ) NULL ) , createIndex ( mProfiles . size ( ) - 1 , COLUMN_THREAD_NB_COLUMNS - 1 , ( void * ) NULL ) ) ;
2019-06-25 17:08:31 -04:00
}
2019-06-28 05:12:44 -04:00
int RsFriendListModel : : rowCount ( const QModelIndex & parent ) const
2019-06-25 17:08:31 -04:00
{
2019-06-28 18:14:27 -04:00
if ( parent . column ( ) > = COLUMN_THREAD_NB_COLUMNS )
2019-06-25 17:08:31 -04:00
return 0 ;
2019-06-28 18:14:27 -04:00
if ( parent . internalId ( ) = = 0 )
if ( mDisplayGroups )
2019-06-28 05:12:44 -04:00
return mGroups . size ( ) ;
2019-06-28 18:14:27 -04:00
else
2019-06-28 05:12:44 -04:00
return mProfiles . size ( ) ;
2019-06-28 18:14:27 -04:00
EntryIndex index ;
if ( ! convertInternalIdToIndex ( parent . internalId ( ) , index ) )
return 0 ;
if ( index . type = = ENTRY_TYPE_GROUP )
2019-06-29 17:55:38 -04:00
return mGroups [ index . ind ] . child_indices . size ( ) ;
if ( index . type = = ENTRY_TYPE_PROFILE )
return mProfiles [ index . ind ] . child_indices . size ( ) ;
2019-06-25 17:08:31 -04:00
return 0 ;
}
2019-06-28 05:12:44 -04:00
int RsFriendListModel : : columnCount ( const QModelIndex & parent ) const
2019-06-25 17:08:31 -04:00
{
return COLUMN_THREAD_NB_COLUMNS ;
}
2019-06-28 05:12:44 -04:00
// bool RsFriendListModel::getProfileData(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);
// }
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
bool RsFriendListModel : : hasChildren ( const QModelIndex & parent ) const
2019-06-25 17:08:31 -04:00
{
if ( ! parent . isValid ( ) )
return true ;
2019-06-28 18:14:27 -04:00
EntryIndex parent_index ;
convertInternalIdToIndex ( parent . internalId ( ) , parent_index ) ;
if ( parent_index . type = = ENTRY_TYPE_NODE )
return false ;
if ( parent_index . type = = ENTRY_TYPE_PROFILE )
2019-06-29 17:55:38 -04:00
return ! mProfiles [ parent_index . ind ] . child_indices . empty ( ) ;
2019-06-28 18:14:27 -04:00
if ( parent_index . type = = ENTRY_TYPE_GROUP )
2019-06-29 17:55:38 -04:00
return ! mGroups [ parent_index . ind ] . child_indices . empty ( ) ;
2019-06-28 18:14:27 -04:00
return false ;
2019-06-25 17:08:31 -04:00
}
2019-06-28 05:12:44 -04:00
bool RsFriendListModel : : convertIndexToInternalId ( const EntryIndex & e , quintptr & id )
2019-06-25 17:08:31 -04:00
{
// the internal id is set to the place in the table of items. We simply shift to allow 0 to mean something special.
2019-06-28 05:12:44 -04:00
if ( e . ind > 0x0fffffff )
return false ;
if ( e . type = = ENTRY_TYPE_GROUP )
id = e . ind + 0x10000000 ;
else if ( e . type = = ENTRY_TYPE_PROFILE )
id = e . ind + 0x20000000 ;
else if ( e . type = = ENTRY_TYPE_NODE )
id = e . ind + 0x30000000 ;
else
return false ;
2019-06-25 17:08:31 -04:00
return true ;
}
2019-06-28 05:12:44 -04:00
bool RsFriendListModel : : convertInternalIdToIndex ( quintptr ref , EntryIndex & e )
2019-06-25 17:08:31 -04:00
{
if ( ref = = 0 )
return false ;
2019-06-28 05:12:44 -04:00
e . ind = ref & 0x0fffffff ;
int t = ( ref > > 28 ) & 0xf ;
if ( t = = 0x1 )
e . type = ENTRY_TYPE_GROUP ;
else if ( t = = 0x2 )
e . type = ENTRY_TYPE_PROFILE ;
else if ( t = = 0x3 )
e . type = ENTRY_TYPE_NODE ;
else
return false ;
2019-06-25 17:08:31 -04:00
return true ;
}
2019-06-28 05:12:44 -04:00
QModelIndex RsFriendListModel : : index ( int row , int column , const QModelIndex & parent ) const
2019-06-25 17:08:31 -04:00
{
if ( row < 0 | | column < 0 | | column > = COLUMN_THREAD_NB_COLUMNS )
return QModelIndex ( ) ;
2019-06-28 05:12:44 -04:00
if ( parent . internalId ( ) = = 0 )
{
quintptr ref ;
if ( mDisplayGroups )
convertIndexToInternalId ( EntryIndex ( ENTRY_TYPE_GROUP , row ) , ref ) ;
else
convertIndexToInternalId ( EntryIndex ( ENTRY_TYPE_PROFILE , row ) , ref ) ;
2019-06-25 17:08:31 -04:00
return createIndex ( row , column , ref ) ;
2019-06-28 05:12:44 -04:00
}
EntryIndex parent_index ;
convertInternalIdToIndex ( parent . internalId ( ) , parent_index ) ;
2019-07-27 17:42:39 -04:00
EntryIndex new_index ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
switch ( parent_index . type )
{
case ENTRY_TYPE_GROUP : new_index . type = ENTRY_TYPE_PROFILE ;
2019-07-27 17:42:39 -04:00
new_index . ind = mGroups [ parent_index . ind ] . child_indices [ row ] ;
break ;
2019-06-28 05:12:44 -04:00
case ENTRY_TYPE_PROFILE : new_index . type = ENTRY_TYPE_NODE ;
2019-07-27 17:42:39 -04:00
new_index . ind = mProfiles [ parent_index . ind ] . child_indices [ row ] ;
break ;
break ;
2019-06-28 05:12:44 -04:00
default :
return QModelIndex ( ) ;
}
quintptr ref ;
convertIndexToInternalId ( new_index , ref ) ;
return createIndex ( row , column , ref ) ;
2019-06-25 17:08:31 -04:00
}
2019-06-28 05:12:44 -04:00
QModelIndex RsFriendListModel : : parent ( const QModelIndex & index ) const
2019-06-25 17:08:31 -04:00
{
if ( ! index . isValid ( ) )
return QModelIndex ( ) ;
2019-06-28 18:14:27 -04:00
EntryIndex I ;
convertInternalIdToIndex ( index . internalId ( ) , I ) ;
if ( I . type = = ENTRY_TYPE_GROUP )
return QModelIndex ( ) ;
if ( I . type = = ENTRY_TYPE_PROFILE )
2019-06-29 17:55:38 -04:00
{
quintptr ref = 0 ;
convertIndexToInternalId ( EntryIndex ( ENTRY_TYPE_GROUP , mProfiles [ I . ind ] . parent_group_index ) , ref ) ;
2019-06-28 18:14:27 -04:00
2019-06-29 17:55:38 -04:00
return createIndex ( mProfiles [ I . ind ] . parent_row , 0 , ref ) ;
}
2019-06-28 18:14:27 -04:00
2019-06-29 17:55:38 -04:00
if ( I . type = = ENTRY_TYPE_NODE )
{
quintptr ref = 0 ;
convertIndexToInternalId ( EntryIndex ( ENTRY_TYPE_PROFILE , mLocations [ I . ind ] . parent_profile_index ) , ref ) ;
return createIndex ( mLocations [ I . ind ] . parent_row , 0 , ref ) ;
}
2019-06-28 18:14:27 -04:00
2019-06-25 17:08:31 -04:00
return QModelIndex ( ) ;
}
2019-06-28 05:12:44 -04:00
Qt : : ItemFlags RsFriendListModel : : flags ( const QModelIndex & index ) const
2019-06-25 17:08:31 -04:00
{
if ( ! index . isValid ( ) )
return 0 ;
return QAbstractItemModel : : flags ( index ) ;
}
2019-06-28 05:12:44 -04:00
QVariant RsFriendListModel : : headerData ( int section , Qt : : Orientation orientation , int role ) const
2019-06-25 17:08:31 -04:00
{
if ( role = = Qt : : DisplayRole )
switch ( section )
{
2019-06-28 05:12:44 -04:00
case COLUMN_THREAD_NAME : return tr ( " Name " ) ;
case COLUMN_THREAD_ID : return tr ( " Id " ) ;
case COLUMN_THREAD_LAST_CONTACT : return tr ( " Last contact " ) ;
case COLUMN_THREAD_IP : return tr ( " IP " ) ;
2019-06-25 17:08:31 -04:00
default :
return QVariant ( ) ;
}
return QVariant ( ) ;
}
2019-06-28 05:12:44 -04:00
QVariant RsFriendListModel : : data ( const QModelIndex & index , int role ) const
2019-06-25 17:08:31 -04:00
{
# 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 ;
# ifdef DEBUG_MESSAGE_MODEL
std : : cerr < < " data( " < < index < < " ) " ;
# endif
if ( ! ref )
{
# ifdef DEBUG_MESSAGE_MODEL
std : : cerr < < " [empty] " < < std : : endl ;
# endif
return QVariant ( ) ;
}
2019-06-28 05:12:44 -04:00
EntryIndex entry ;
if ( ! convertInternalIdToIndex ( ref , entry ) )
2019-06-25 17:08:31 -04:00
{
# ifdef DEBUG_MESSAGE_MODEL
std : : cerr < < " Bad pointer: " < < ( void * ) ref < < std : : endl ;
# endif
return QVariant ( ) ;
}
switch ( role )
{
2019-06-28 05:12:44 -04:00
case Qt : : DisplayRole : return displayRole ( entry , index . column ( ) ) ;
2019-06-25 17:08:31 -04:00
default :
return QVariant ( ) ;
}
2019-06-28 05:12:44 -04:00
// if(role == Qt::FontRole)
// {
// QFont font ;
// font.setBold(fmpe.msgflags & (RS_MSG_NEW | RS_MSG_UNREAD_BY_USER));
//
// return QVariant(font);
// }
// 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()) ;
}
QVariant RsFriendListModel : : textColorRole ( const EntryIndex & fmpe , int column ) const
2019-06-25 17:08:31 -04:00
{
return QVariant ( ) ;
}
2019-06-28 05:12:44 -04:00
QVariant RsFriendListModel : : statusRole ( const EntryIndex & fmpe , int column ) const
2019-06-25 17:08:31 -04:00
{
// if(column != COLUMN_THREAD_DATA)
// return QVariant();
return QVariant ( ) ; //fmpe.mMsgStatus);
}
2019-06-28 05:12:44 -04:00
bool RsFriendListModel : : passesFilter ( const EntryIndex & e , int column ) const
{
// QString s ;
// bool passes_strings = true ;
//
// if(!mFilterStrings.empty())
// {
// switch(mFilterType)
// {
// case FILTER_TYPE_ID: s = displayRole(fmpe,COLUMN_THREAD_SUBJECT).toString();
// break;
//
// case FILTER_TYPE_NAME: s = sortRole(fmpe,COLUMN_THREAD_AUTHOR).toString();
// if(s.isNull())
// passes_strings = false;
// break;
// };
// }
//
// 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));
// #ifdef DEBUG_MESSAGE_MODEL
// 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;
// #endif
//
// return passes_quick_view && passes_strings;
}
QVariant RsFriendListModel : : filterRole ( const EntryIndex & e , int column ) const
{
if ( passesFilter ( e , column ) )
2019-06-25 17:08:31 -04:00
return QVariant ( FilterString ) ;
return QVariant ( QString ( ) ) ;
}
2019-06-28 05:12:44 -04:00
uint32_t RsFriendListModel : : updateFilterStatus ( ForumModelIndex i , int column , const QStringList & strings )
2019-06-25 17:08:31 -04:00
{
QString s ;
uint32_t count = 0 ;
return count ;
}
2019-06-28 05:12:44 -04:00
void RsFriendListModel : : setFilter ( FilterType filter_type , const QStringList & strings )
2019-06-25 17:08:31 -04:00
{
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 ( ) ;
}
2019-06-28 05:12:44 -04:00
QVariant RsFriendListModel : : toolTipRole ( const EntryIndex & 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;
// }
2019-06-25 17:08:31 -04:00
return QVariant ( ) ;
}
2019-06-28 05:12:44 -04:00
QVariant RsFriendListModel : : sizeHintRole ( int col ) const
2019-06-25 17:08:31 -04:00
{
float factor = QFontMetricsF ( QApplication : : font ( ) ) . height ( ) / 14.0f ;
switch ( col )
{
default :
2019-06-28 05:12:44 -04:00
case COLUMN_THREAD_NAME : return QVariant ( QSize ( factor * 170 , factor * 14 ) ) ;
case COLUMN_THREAD_IP : return QVariant ( QSize ( factor * 75 , factor * 14 ) ) ;
case COLUMN_THREAD_ID : return QVariant ( QSize ( factor * 75 , factor * 14 ) ) ;
case COLUMN_THREAD_LAST_CONTACT : return QVariant ( QSize ( factor * 75 , factor * 14 ) ) ;
2019-06-25 17:08:31 -04:00
}
}
2019-06-28 05:12:44 -04:00
QVariant RsFriendListModel : : sortRole ( const EntryIndex & 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);
// }
2019-06-25 17:08:31 -04:00
return QVariant ( ) ;
}
2019-06-28 05:12:44 -04:00
QVariant RsFriendListModel : : displayRole ( const EntryIndex & e , int col ) const
2019-06-25 17:08:31 -04:00
{
2019-06-28 05:12:44 -04:00
switch ( e . type )
2019-06-25 17:08:31 -04:00
{
2019-06-28 05:12:44 -04:00
case ENTRY_TYPE_GROUP :
if ( e . ind > = mGroups . size ( ) )
return QVariant ( ) ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
switch ( col )
{
2019-06-29 17:55:38 -04:00
case COLUMN_THREAD_NAME : return QVariant ( QString : : fromUtf8 ( mGroups [ e . ind ] . group . name . c_str ( ) ) ) ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
default :
return QVariant ( ) ;
} break ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
case ENTRY_TYPE_PROFILE :
if ( e . ind > = mProfiles . size ( ) )
return QVariant ( ) ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
switch ( col )
{
2019-06-29 17:55:38 -04:00
case COLUMN_THREAD_NAME : return QVariant ( QString : : fromUtf8 ( mProfileDetails [ mProfiles [ e . ind ] . profile_index ] . name . c_str ( ) ) ) ;
case COLUMN_THREAD_ID : return QVariant ( QString : : fromStdString ( mProfileDetails [ mProfiles [ e . ind ] . profile_index ] . gpg_id . toStdString ( ) ) ) ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
default :
return QVariant ( ) ;
} break ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
case ENTRY_TYPE_NODE :
if ( e . ind > = mLocations . size ( ) )
return QVariant ( ) ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
switch ( col )
{
2019-06-29 17:55:38 -04:00
case COLUMN_THREAD_NAME : return QVariant ( QString : : fromUtf8 ( mNodeDetails [ mLocations [ e . ind ] . node_index ] . location . c_str ( ) ) ) ;
case COLUMN_THREAD_LAST_CONTACT : return QVariant ( QDateTime : : fromTime_t ( mNodeDetails [ mLocations [ e . ind ] . node_index ] . lastConnect ) . toString ( ) ) ;
case COLUMN_THREAD_IP : return QVariant ( ( mNodeDetails [ mLocations [ e . ind ] . node_index ] . state & RS_PEER_STATE_CONNECTED ) ? StatusDefs : : connectStateIpString ( mNodeDetails [ mLocations [ e . ind ] . node_index ] ) : QString ( " --- " ) ) ;
case COLUMN_THREAD_ID : return QVariant ( QString : : fromStdString ( mNodeDetails [ mLocations [ e . ind ] . node_index ] . id . toStdString ( ) ) ) ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
default :
return QVariant ( ) ;
} break ;
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
return QVariant ( ) ;
2019-06-25 17:08:31 -04:00
}
}
2019-06-28 05:12:44 -04:00
QVariant RsFriendListModel : : userRole ( const EntryIndex & fmpe , int col ) const
2019-06-25 17:08:31 -04:00
{
2019-06-28 05:12:44 -04:00
// 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();
// }
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
return QVariant ( ) ;
2019-06-25 17:08:31 -04:00
}
2019-06-28 05:12:44 -04:00
QVariant RsFriendListModel : : decorationRole ( const EntryIndex & 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");
2019-06-25 17:08:31 -04:00
2019-06-28 05:12:44 -04:00
return QVariant ( ) ;
2019-06-25 17:08:31 -04:00
}
2019-06-28 05:12:44 -04:00
void RsFriendListModel : : clear ( )
2019-06-25 17:08:31 -04:00
{
preMods ( ) ;
2019-06-28 05:12:44 -04:00
mGroups . clear ( ) ;
mProfiles . clear ( ) ;
mLocations . clear ( ) ;
2019-06-25 17:08:31 -04:00
postMods ( ) ;
2019-06-28 05:12:44 -04:00
emit friendListChanged ( ) ;
}
// void RsFriendListModel::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 RsFriendListModel::updateMessages()
// {
// emit messagesAboutToLoad();
//
// std::list<Rs::Msgs::MsgInfoSummary> msgs;
//
// getMessageSummaries(mCurrentBox,msgs);
// setMessages(msgs);
//
// emit messagesLoaded();
// }
2019-06-25 17:08:31 -04:00
static bool decreasing_time_comp ( const std : : pair < time_t , RsGxsMessageId > & e1 , const std : : pair < time_t , RsGxsMessageId > & e2 ) { return e2 . first < e1 . first ; }
2019-06-28 05:12:44 -04:00
void RsFriendListModel : : debug_dump ( ) const
2019-06-25 17:08:31 -04:00
{
2019-07-27 17:42:39 -04:00
for ( uint32_t j = 0 ; j < mGroups . size ( ) ; + + j )
2019-06-29 17:55:38 -04:00
{
2019-07-27 17:42:39 -04:00
std : : cerr < < " Group: " < < mGroups [ j ] . group . name < < " , prow= " < < mGroups [ j ] . parent_row < < " , " ;
std : : cerr < < " children indices: " ; for ( uint32_t i = 0 ; i < mGroups [ j ] . child_indices . size ( ) ; + + i ) std : : cerr < < mGroups [ j ] . child_indices [ i ] < < " " ; std : : cerr < < std : : endl ;
for ( uint32_t i = 0 ; i < mGroups [ j ] . child_indices . size ( ) ; + + i )
{
uint32_t profile_index = mGroups [ j ] . child_indices [ i ] ;
std : : cerr < < " Profile " < < mProfileDetails [ mProfiles [ profile_index ] . profile_index ] . gpg_id < < " , prow= " < < mProfiles [ profile_index ] . parent_row < < " , parent_index= " < < mProfiles [ profile_index ] . parent_group_index < < std : : endl ;
const HierarchicalProfileInformation & hprof ( mProfiles [ profile_index ] ) ;
for ( uint32_t k = 0 ; k < hprof . child_indices . size ( ) ; + + k )
{
std : : cerr < < " Node " < < mNodeDetails [ mLocations [ hprof . child_indices [ k ] ] . node_index ] . id < < " , prow= " < < mLocations [ hprof . child_indices [ k ] ] . parent_row < < " , parent_index= " < < mLocations [ hprof . child_indices [ k ] ] . parent_profile_index < < std : : endl ;
}
}
2019-06-29 17:55:38 -04:00
}
2019-06-25 17:08:31 -04:00
}
2019-06-28 10:20:26 -04:00
bool RsFriendListModel : : getGroupData ( const QModelIndex & i , RsGroupInfo & data ) const
{
if ( ! i . isValid ( ) )
return false ;
EntryIndex e ;
if ( ! convertInternalIdToIndex ( i . internalId ( ) , e ) | | e . type ! = ENTRY_TYPE_GROUP | | e . ind > = mGroups . size ( ) )
return false ;
2019-06-29 17:55:38 -04:00
data = mGroups [ e . ind ] . group ;
2019-06-28 10:20:26 -04:00
return true ;
}
bool RsFriendListModel : : getProfileData ( const QModelIndex & i , RsProfileDetails & data ) const
{
if ( ! i . isValid ( ) )
return false ;
EntryIndex e ;
if ( ! convertInternalIdToIndex ( i . internalId ( ) , e ) | | e . type ! = ENTRY_TYPE_PROFILE | | e . ind > = mProfiles . size ( ) )
return false ;
2019-06-29 17:55:38 -04:00
data = mProfileDetails [ mProfiles [ e . ind ] . profile_index ] ;
2019-06-28 10:20:26 -04:00
return true ;
}
bool RsFriendListModel : : getNodeData ( const QModelIndex & i , RsNodeDetails & data ) const
{
if ( ! i . isValid ( ) )
return false ;
EntryIndex e ;
if ( ! convertInternalIdToIndex ( i . internalId ( ) , e ) | | e . type ! = ENTRY_TYPE_NODE | | e . ind > = mLocations . size ( ) )
return false ;
2019-06-29 17:55:38 -04:00
data = mNodeDetails [ mLocations [ e . ind ] . node_index ] ;
2019-06-28 10:20:26 -04:00
return true ;
}
RsFriendListModel : : EntryType RsFriendListModel : : getType ( const QModelIndex & i ) const
{
if ( ! i . isValid ( ) )
return ENTRY_TYPE_UNKNOWN ;
EntryIndex e ;
if ( ! convertInternalIdToIndex ( i . internalId ( ) , e ) )
return ENTRY_TYPE_UNKNOWN ;
return e . type ;
}
2019-06-28 16:36:19 -04:00
void RsFriendListModel : : updateInternalData ( )
{
preMods ( ) ;
mGroups . clear ( ) ;
mLocations . clear ( ) ;
mProfiles . clear ( ) ;
2019-06-29 17:55:38 -04:00
mNodeDetails . clear ( ) ;
mProfileDetails . clear ( ) ;
2019-06-29 11:50:13 -04:00
// create a map of profiles and groups
std : : map < RsPgpId , uint32_t > pgp_indexes ;
std : : map < RsPeerId , uint32_t > ssl_indexes ;
std : : map < RsNodeGroupId , uint32_t > grp_indexes ;
2019-06-29 17:55:38 -04:00
// we start from the top and fill in the blanks as needed
// groups
2019-06-28 16:36:19 -04:00
std : : list < RsGroupInfo > groupInfoList ;
rsPeers - > getGroupInfoList ( groupInfoList ) ;
2019-06-29 17:55:38 -04:00
uint32_t group_row = 0 ;
2019-06-28 16:36:19 -04:00
2019-07-27 17:42:39 -04:00
RsDbg ( ) < < " Updating Groups information: " < < std : : endl ;
2019-06-29 17:55:38 -04:00
for ( auto it ( groupInfoList . begin ( ) ) ; it ! = groupInfoList . end ( ) ; + + it , + + group_row )
2019-06-29 11:50:13 -04:00
{
2019-06-29 17:55:38 -04:00
// first, fill the group hierarchical info
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
HierarchicalGroupInformation groupinfo ;
groupinfo . group = * it ;
groupinfo . parent_row = group_row ;
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
uint32_t profile_row = 0 ;
2019-06-28 16:36:19 -04:00
2019-07-27 17:42:39 -04:00
RsDbg ( ) < < " Group \" " < < groupinfo . group . name < < " \" " < < std : : endl ;
2019-06-29 17:55:38 -04:00
for ( auto it2 ( ( * it ) . peerIds . begin ( ) ) ; it2 ! = ( * it ) . peerIds . end ( ) ; + + it2 , + + profile_row )
{
// Then for each peer in this group, make sure that the peer is already known, and if not create it
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
auto it3 = pgp_indexes . find ( * it2 ) ;
if ( it3 = = pgp_indexes . end ( ) ) // not found
{
RsProfileDetails profdet ;
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
rsPeers - > getGPGDetails ( * it2 , profdet ) ;
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
pgp_indexes [ * it2 ] = mProfileDetails . size ( ) ;
mProfileDetails . push_back ( profdet ) ;
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
it3 = pgp_indexes . find ( * it2 ) ;
}
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
// ...and also fill the hierarchical profile info
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
HierarchicalProfileInformation profinfo ;
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
profinfo . parent_row = profile_row ;
profinfo . parent_group_index = mGroups . size ( ) ;
profinfo . profile_index = it3 - > second ;
2019-06-28 16:36:19 -04:00
2019-06-29 17:55:38 -04:00
// now fill the children nodes of the profile
2019-06-29 11:50:13 -04:00
2019-06-29 17:55:38 -04:00
std : : list < RsPeerId > ssl_ids ;
rsPeers - > getAssociatedSSLIds ( * it2 , ssl_ids ) ;
2019-06-29 11:50:13 -04:00
2019-07-27 17:42:39 -04:00
RsDbg ( ) < < " Profile: " < < * it2 < < std : : endl ;
2019-06-29 17:55:38 -04:00
uint32_t node_row = 0 ;
2019-06-29 11:50:13 -04:00
2019-06-29 17:55:38 -04:00
for ( auto it4 ( ssl_ids . begin ( ) ) ; it4 ! = ssl_ids . end ( ) ; + + it4 , + + node_row )
{
auto it5 = ssl_indexes . find ( * it4 ) ;
if ( it5 = = ssl_indexes . end ( ) )
{
RsNodeDetails nodedet ;
rsPeers - > getPeerDetails ( * it4 , nodedet ) ;
ssl_indexes [ * it4 ] = mNodeDetails . size ( ) ;
mNodeDetails . push_back ( nodedet ) ;
it5 = ssl_indexes . find ( * it4 ) ;
}
2019-07-27 17:42:39 -04:00
RsDbg ( ) < < " Node: " < < * it4 < < std : : endl ;
2019-06-29 17:55:38 -04:00
HierarchicalNodeInformation nodeinfo ;
nodeinfo . parent_row = node_row ;
nodeinfo . node_index = it5 - > second ;
nodeinfo . parent_profile_index = mProfiles . size ( ) ;
profinfo . child_indices . push_back ( mLocations . size ( ) ) ;
mLocations . push_back ( nodeinfo ) ;
}
2019-07-27 17:42:39 -04:00
groupinfo . child_indices . push_back ( mProfiles . size ( ) ) ;
2019-06-29 17:55:38 -04:00
mProfiles . push_back ( profinfo ) ;
2019-06-29 11:50:13 -04:00
}
2019-06-29 17:55:38 -04:00
mGroups . push_back ( groupinfo ) ;
2019-06-29 11:50:13 -04:00
}
2019-06-28 16:36:19 -04:00
postMods ( ) ;
}