2019-06-25 17:08:31 -04:00
/*******************************************************************************
* retroshare - gui / src / gui / msgs / RsFriendListModel . 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>
2019-08-06 13:41:18 -04:00
# include "retroshare/rsstatus.h"
2019-06-25 17:08:31 -04:00
# include "retroshare/rsmsgs.h"
2019-06-28 05:12:44 -04:00
# include "retroshare/rspeers.h"
2019-06-25 17:08:31 -04:00
typedef uint32_t ForumModelIndex ;
// This class is the item model used by Qt to display the information
class RsFriendListModel : public QAbstractItemModel
{
Q_OBJECT
public :
explicit RsFriendListModel ( QObject * parent = NULL ) ;
~ RsFriendListModel ( ) { }
2019-06-28 05:12:44 -04:00
class RsNodeDetails : public RsPeerDetails { } ; // in the near future, there will be a specific class for Profile/Node details in replacement of RsPeerDetails
class RsProfileDetails : public RsPeerDetails { } ;
2019-06-29 11:50:13 -04:00
struct HierarchicalGroupInformation
{
2019-08-01 10:53:59 -04:00
RsGroupInfo group_info ;
std : : vector < uint32_t > child_profile_indices ; // index in the array of hierarchical profiles
2019-06-29 11:50:13 -04:00
} ;
struct HierarchicalProfileInformation
{
2019-08-01 10:53:59 -04:00
RsProfileDetails profile_info ;
std : : vector < uint32_t > child_node_indices ; // indices of nodes in the array of nodes.
2019-06-29 11:50:13 -04:00
} ;
struct HierarchicalNodeInformation
{
2019-08-03 17:22:48 -04:00
HierarchicalNodeInformation ( ) : last_update_ts ( 0 ) { }
rstime_t last_update_ts ;
2019-08-01 10:53:59 -04:00
RsNodeDetails node_info ;
2019-06-29 11:50:13 -04:00
} ;
2019-06-25 17:08:31 -04:00
enum Columns {
2019-06-28 05:12:44 -04:00
COLUMN_THREAD_NAME = 0x00 ,
COLUMN_THREAD_LAST_CONTACT = 0x01 ,
COLUMN_THREAD_IP = 0x02 ,
COLUMN_THREAD_ID = 0x03 ,
COLUMN_THREAD_NB_COLUMNS = 0x04
2019-06-25 17:08:31 -04:00
} ;
enum Roles { SortRole = Qt : : UserRole + 1 ,
StatusRole = Qt : : UserRole + 2 ,
UnreadRole = Qt : : UserRole + 3 ,
FilterRole = Qt : : UserRole + 4 ,
2019-08-12 06:49:04 -04:00
OnlineRole = Qt : : UserRole + 5
2019-06-25 17:08:31 -04:00
} ;
2019-06-28 05:12:44 -04:00
enum FilterType { FILTER_TYPE_NONE = 0x00 ,
FILTER_TYPE_ID = 0x01 ,
FILTER_TYPE_NAME = 0x02
} ;
2019-08-01 10:53:59 -04:00
enum EntryType { ENTRY_TYPE_UNKNOWN = 0x00 ,
ENTRY_TYPE_GROUP = 0x01 ,
ENTRY_TYPE_PROFILE = 0x02 ,
2019-08-02 09:40:11 -04:00
ENTRY_TYPE_NODE = 0x03
2019-06-28 05:12:44 -04:00
} ;
2019-08-01 10:53:59 -04:00
// This structure encodes the position of a node in the hierarchy. The type tells which of the index fields are valid.
2019-06-28 05:12:44 -04:00
struct EntryIndex
{
2019-08-01 17:55:42 -04:00
public :
2019-08-23 17:08:29 -04:00
EntryIndex ( ) ;
2019-08-01 10:53:59 -04:00
EntryType type ; // type of the entry (group,profile,location)
2019-08-01 17:55:42 -04:00
// Indices w.r.t. parent. The set of indices entirely determines the position of the entry in the hierarchy.
// An index of 0xff means "undefined"
2019-08-01 10:53:59 -04:00
2019-08-23 16:51:50 -04:00
uint16_t group_index ; // index of the group in mGroups tab
uint16_t profile_index ; // index of the child profile in its own group if group_index < 0xff, or in the mProfiles tab otherwise.
uint16_t node_index ; // index of the child node in its own profile
2019-06-28 05:12:44 -04:00
2019-08-01 10:53:59 -04:00
EntryIndex parent ( ) const ;
2019-08-01 17:55:42 -04:00
EntryIndex child ( int row , const std : : vector < EntryIndex > & top_level ) const ;
2019-08-01 10:53:59 -04:00
uint32_t parentRow ( uint32_t nb_groups ) const ;
2019-06-28 05:12:44 -04:00
} ;
2019-06-25 17:08:31 -04:00
QModelIndex root ( ) const { return createIndex ( 0 , 0 , ( void * ) NULL ) ; }
2019-06-28 05:12:44 -04:00
QModelIndex getIndexOfGroup ( const RsNodeGroupId & mid ) const ;
2019-06-25 17:08:31 -04:00
static const QString FilterString ;
// This method will asynchroneously update the data
2019-06-28 10:20:26 -04:00
void setDisplayGroups ( bool b ) ;
2019-08-11 05:46:49 -04:00
bool getDisplayGroups ( ) const { return mDisplayGroups ; }
2019-06-28 10:20:26 -04:00
2019-08-15 13:43:41 -04:00
void setDisplayStatusString ( bool b ) ;
bool getDisplayStatusString ( ) const { return mDisplayStatusString ; }
2019-06-28 10:20:26 -04:00
EntryType getType ( const QModelIndex & ) const ;
2019-08-01 10:53:59 -04:00
2019-06-28 10:20:26 -04:00
bool getGroupData ( const QModelIndex & , RsGroupInfo & ) const ;
bool getProfileData ( const QModelIndex & , RsProfileDetails & ) const ;
bool getNodeData ( const QModelIndex & , RsNodeDetails & ) const ;
2019-06-28 05:12:44 -04:00
2019-06-25 17:08:31 -04:00
void setFilter ( FilterType filter_type , const QStringList & strings ) ;
2019-06-28 10:20:26 -04:00
// Overloaded methods from QAbstractItemModel
2019-06-25 17:08:31 -04:00
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 ;
void clear ( ) ;
2019-08-06 13:41:18 -04:00
/* Color definitions (for standard see qss.default) */
QColor mTextColorGroup ;
QColor mTextColorStatus [ RS_STATUS_COUNT ] ;
2019-06-28 10:20:26 -04:00
private :
2019-08-07 16:35:57 -04:00
const HierarchicalGroupInformation * getGroupInfo ( const EntryIndex & ) const ;
2019-08-20 15:27:59 -04:00
const HierarchicalProfileInformation * getProfileInfo ( const EntryIndex & ) const ;
2019-08-07 16:35:57 -04:00
const HierarchicalNodeInformation * getNodeInfo ( const EntryIndex & ) const ;
2019-08-01 10:53:59 -04:00
2019-08-21 16:39:07 -04:00
void checkNode ( HierarchicalNodeInformation & node ) ;
2019-08-11 08:44:58 -04:00
bool getPeerOnlineStatus ( const EntryIndex & e ) const ;
2019-08-19 16:56:49 -04:00
std : : map < RsPgpId , uint32_t > : : const_iterator checkProfileIndex ( const RsPgpId & pgp_id ,
std : : map < RsPgpId , uint32_t > & pgp_indices ,
std : : vector < HierarchicalProfileInformation > & mProfiles ,
bool create ) ;
2019-08-06 13:41:18 -04:00
2019-08-12 07:43:45 -04:00
QVariant sizeHintRole ( const EntryIndex & e , int col ) const ;
QVariant displayRole ( const EntryIndex & e , int col ) const ;
2019-08-12 06:49:04 -04:00
QVariant decorationRole ( const EntryIndex & e , int col ) const ;
QVariant toolTipRole ( const EntryIndex & e , int col ) const ;
QVariant statusRole ( const EntryIndex & e , int col ) const ;
QVariant sortRole ( const EntryIndex & e , int col ) const ;
QVariant fontRole ( const EntryIndex & e , int col ) const ;
QVariant textColorRole ( const EntryIndex & e , int col ) const ;
QVariant onlineRole ( const EntryIndex & e , int col ) const ;
2019-06-28 05:12:44 -04:00
QVariant filterRole ( const EntryIndex & e , int col ) const ;
2019-06-25 17:08:31 -04:00
/*!
* \ brief debug_dump
* Dumps the hierarchy of posts in the terminal , to allow checking whether the internal representation is correct .
*/
public slots :
2019-08-21 16:39:07 -04:00
void checkInternalData ( bool force ) ;
2019-07-27 17:42:39 -04:00
void debug_dump ( ) const ;
2019-06-25 17:08:31 -04:00
signals :
void dataLoaded ( ) ; // emitted after the messages have been set. Can be used to updated the UI.
2019-06-28 05:12:44 -04:00
void friendListChanged ( ) ; // emitted after the messages have been set. Can be used to updated the UI.
2019-06-25 17:08:31 -04:00
void dataAboutToLoad ( ) ;
private :
2019-08-21 16:39:07 -04:00
void updateInternalData ( ) ;
2019-06-28 05:12:44 -04:00
bool passesFilter ( const EntryIndex & e , int column ) const ;
2019-06-25 17:08:31 -04:00
void preMods ( ) ;
void postMods ( ) ;
void * getParentRef ( void * ref , int & row ) const ;
void * getChildRef ( void * ref , int row ) const ;
int getChildrenCount ( void * ref ) const ;
2019-08-23 16:46:56 -04:00
template < uint8_t > static bool convertIndexToInternalId ( const EntryIndex & e , quintptr & ref ) ;
template < uint8_t > static bool convertInternalIdToIndex ( quintptr ref , EntryIndex & e ) ;
2019-06-25 17:08:31 -04:00
uint32_t updateFilterStatus ( ForumModelIndex i , int column , const QStringList & strings ) ;
QStringList mFilterStrings ;
FilterType mFilterType ;
2019-06-28 05:12:44 -04:00
bool mDisplayGroups ;
2019-08-15 13:43:41 -04:00
bool mDisplayStatusString ;
2019-08-20 15:27:59 -04:00
rstime_t mLastInternalDataUpdate ;
2019-08-21 16:39:07 -04:00
rstime_t mLastNodeUpdate ; ;
2019-06-28 05:12:44 -04:00
2019-08-01 10:53:59 -04:00
// The 3 vectors below store thehierarchical information for groups, profiles and locations,
// meaning which is the child/parent of which. The actual group/profile/node data are also stored in the
// structure.
2019-06-29 11:50:13 -04:00
2019-08-03 17:22:48 -04:00
mutable std : : vector < HierarchicalGroupInformation > mGroups ;
mutable std : : vector < HierarchicalProfileInformation > mProfiles ;
mutable std : : vector < HierarchicalNodeInformation > mLocations ;
2019-06-29 11:50:13 -04:00
2019-08-01 10:53:59 -04:00
// The top level list contains all nodes to display, which type depends on the option to display groups or not.
// Idices in the list may be profile indices or group indices. In the former case the profile child index refers to
// the inde in the mProfiles tab, whereas in the the later case, the child index refers to the index of the profile in the
// group it belows to.
std : : vector < EntryIndex > mTopLevel ;
2019-06-25 17:08:31 -04:00
} ;
2019-06-29 11:50:13 -04:00