Commit 66adc01c authored by armin's avatar armin
Browse files

Merge branch 'master' into 'loginProblemsAfterRegistration'

# Conflicts:
#   engine.pro
#   rocketchatserver.cpp
#   rocketchatserver.h
parents 22ddc54f 9c0c0416
......@@ -21,11 +21,6 @@
#include "channelmodel.h"
ChannelModel::ChannelModel()
{
// connect(&timer,&QTimer::timeout,this,&ChannelModel::sortChanged);
}
int ChannelModel::rowCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent )
......@@ -43,59 +38,102 @@ QVariant ChannelModel::data( const QModelIndex &index, int role ) const
auto currentChannel = channelList.at( row );
if ( role == name ) {
return currentChannel->getName();
}
auto roleType = static_cast<ChannelRoles>( role );
if ( role == roomId ) {
return currentChannel->getRoomId();
}
switch ( roleType ) {
case ChannelRoles::name:
return currentChannel->getName();
if ( role == lastMessageText ) {
auto channel = channelList.at( row );
auto message = channel->getYoungestMessage();
case ChannelRoles::roomId:
return currentChannel->getRoomId();
if ( message.isNull() ) {
return "";
}
case ChannelRoles::lastMessageText: {
auto channel = channelList.at( row );
auto message = channel->getYoungestMessage();
if ( message->getMessageType() == QStringLiteral( "file" ) || message->getMessageType() == QStringLiteral( "image" ) || message->getMessageType() == QStringLiteral( "audio" ) ) {
return tr( QByteArrayLiteral( "file upload" ) );
}
if ( message.isNull() ) {
return "";
}
return message->getMessageString();
}
if ( message->getMessageType() == QStringLiteral( "file" ) || message->getMessageType() == QStringLiteral( "image" ) || message->getMessageType() == QStringLiteral( "audio" ) ) {
return tr( QByteArrayLiteral( "file upload" ) );
}
auto messageText =message->getMessageString();
messageText = messageText.replace("<img height='20' width='20'","<img height='10' width='10'");
return messageText;
}
if ( role == channelType ) {
return currentChannel->getType();
}
case ChannelRoles::channelType:
return currentChannel->getType();
if ( role == unreadMessages ) {
return currentChannel->getUnreadMessages();
}
case ChannelRoles::unreadMessages:
return currentChannel->getUnreadMessages();
if ( role == readonly ) {
return currentChannel->getReadOnly() || currentChannel->getArchived() || currentChannel->getBlocked();
}
case ChannelRoles::readonly:
return currentChannel->getReadOnly() || currentChannel->getArchived() || currentChannel->getBlocked();
if ( role == ownerId ) {
return currentChannel->getOwnerName();
}
case ChannelRoles::ownerId:
return currentChannel->getOwnerId();
if ( role == ownerId ) {
return currentChannel->getOwnerId();
}
case ChannelRoles::archived:
return currentChannel->getArchived();
if ( role == archived ) {
return currentChannel->getArchived();
}
case ChannelRoles::blocked:
return currentChannel->getBlocked();
if ( role == blocked ) {
return currentChannel->getBlocked();
}
case ChannelRoles::username:
return currentChannel->getUsername();
case ChannelRoles::userStatus: {
int status = 0;
auto partner = currentChannel->getChatPartner();
if ( !partner.isNull() ) {
switch ( partner->getStatus() ) {
case RocketChatUser::status::ONLINE :
status = 0;
break;
case RocketChatUser::status::AWAY :
status = 1;
break;
case RocketChatUser::status::OFFLINE:
status = 2;
break;
case RocketChatUser::status::BUSY:
status = 3;
break;
}
}
return status;
}
case ChannelRoles::deleted:
return currentChannel->getDeleted();
case ChannelRoles::ownerName:
return currentChannel->getOwnerName();
case ChannelRoles::avatarImg: {
auto avatarImg = currentChannel->getAvatarImg();
if ( !avatarImg.isNull() ) {
auto path = QStringLiteral( "file://" ) + avatarImg->getFilePath();
if ( path.endsWith( "svg" ) ) {
return "qrc:res/user-identity.svg";
} else {
return path;
}
} else {
return "";
}
}
if ( role == username ) {
return currentChannel->getUsername();
}
return QVariant();
......@@ -111,31 +149,34 @@ bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel
connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection );
duplicateCheck.insert( channel->getRoomId() );
int pos = channelList.findPosition( channel );
emit beginInsertRows( QModelIndex(), pos, pos );
beginInsertRows( QModelIndex(), pos, pos );
channelList.insertSort( channel );
emit endInsertRows();
endInsertRows();
sortChanged();
return true;
} else {
return false;
}
return false;
}
QHash<int, QByteArray> ChannelModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[name] = QByteArrayLiteral( "name" );
roles[roomId] = QByteArrayLiteral( "roomId" );
roles[unreadMessages] = QByteArrayLiteral( "unreadMessages" );
roles[channelType] = QByteArrayLiteral( "type" );
roles[lastMessageText] = QByteArrayLiteral( "lastMessageText" );
roles[readonly] = QByteArrayLiteral( "readonly" );
roles[archived] = QByteArrayLiteral( "archived" );
roles[blocked] = QByteArrayLiteral( "blocked" );
roles[deleted] = QByteArrayLiteral( "deleted" );
roles[ownerId] = QByteArrayLiteral( "ownerId" );
roles[ownerName] = QByteArrayLiteral( "ownerName" );
roles[username] = QByteArrayLiteral( "username" );
roles[static_cast<int>( ChannelRoles::name )] = QByteArrayLiteral( "name" );
roles[static_cast<int>( ChannelRoles::roomId )] = QByteArrayLiteral( "roomId" );
roles[static_cast<int>( ChannelRoles::unreadMessages )] = QByteArrayLiteral( "unreadMessages" );
roles[static_cast<int>( ChannelRoles::channelType )] = QByteArrayLiteral( "type" );
roles[static_cast<int>( ChannelRoles::lastMessageText )] = QByteArrayLiteral( "lastMessageText" );
roles[static_cast<int>( ChannelRoles::readonly )] = QByteArrayLiteral( "readonly" );
roles[static_cast<int>( ChannelRoles::archived )] = QByteArrayLiteral( "archived" );
roles[static_cast<int>( ChannelRoles::blocked )] = QByteArrayLiteral( "blocked" );
roles[static_cast<int>( ChannelRoles::deleted )] = QByteArrayLiteral( "deleted" );
roles[static_cast<int>( ChannelRoles::ownerId )] = QByteArrayLiteral( "ownerId" );
roles[static_cast<int>( ChannelRoles::ownerName )] = QByteArrayLiteral( "ownerName" );
roles[static_cast<int>( ChannelRoles::username )] = QByteArrayLiteral( "username" );
roles[static_cast<int>( ChannelRoles::userStatus )] = QByteArrayLiteral( "userStatus" );
roles[static_cast<int>( ChannelRoles::avatarImg )] = QByteArrayLiteral( "avatarImg" );
return roles;
}
......@@ -165,7 +206,7 @@ void ChannelModel::onNewerMessage( const QString &id, qint64 timestamp )
void ChannelModel::onUnreadMessageChanged( const QString &id, int number )
{
Q_UNUSED( number )
qDebug() << "unread signal catched";
qDebug() << QStringLiteral( "unread signal catched" );
int pos = 0;
bool found = false;
......@@ -180,7 +221,7 @@ void ChannelModel::onUnreadMessageChanged( const QString &id, int number )
if ( found ) {
qDebug() << "channel found";
emit dataChanged( index( pos ), index( pos ), {unreadMessages} );
emit dataChanged( index( pos ), index( pos ), {static_cast<int>( ChannelRoles::unreadMessages )} );
emit unreadMessagesChanged();
}
}
......@@ -201,11 +242,10 @@ void ChannelModel::onDataChanged( const QString &id, const QString &property )
if ( found ) {
auto roles = roleNames();
auto roleKeys = roles.keys();
for ( auto roleKey : roleKeys ) {
if ( roles[roleKey] == property ) {
emit dataChanged( index( pos ), index( pos ), {roleKey} );
for ( auto it = roles.begin(); it != roles.end(); it++ ) {
if ( roles[it.key()] == property ) {
emit dataChanged( index( pos ), index( pos ), {it.key()} );
}
}
}
......@@ -213,6 +253,7 @@ void ChannelModel::onDataChanged( const QString &id, const QString &property )
void ChannelModel::onDeleted( const QString &pId, bool deleted )
{
Q_UNUSED( deleted )
int idx = -1;
for ( int i = 0; i < channelList.length(); i++ ) {
......@@ -249,7 +290,7 @@ void ChannelModel::sortChanged()
{
beginResetModel();
std::sort( channelList.begin(), channelList.end(), []( QSharedPointer<RocketChatChannel> channel1, QSharedPointer<RocketChatChannel> channel2 ) {
std::sort( channelList.begin(), channelList.end(), []( const QSharedPointer<RocketChatChannel> &channel1, const QSharedPointer<RocketChatChannel> &channel2 ) {
auto youngest1 = channel1->getYoungestMessage();
auto youngest2 = channel2->getYoungestMessage();
......@@ -257,13 +298,18 @@ void ChannelModel::sortChanged()
auto timestamp1 = youngest1->getTimestamp();
auto timestamp2 = youngest2->getTimestamp();
return timestamp2 < timestamp1;
} else if ( youngest1.isNull() && !youngest2.isNull() ) {
}
if ( youngest1.isNull() && !youngest2.isNull() ) {
return false;
} else if ( youngest2.isNull() && !youngest1.isNull() ) {
}
if ( youngest2.isNull() && !youngest1.isNull() ) {
return true;
} else {
return false;
}
return false;
} );
endResetModel();
emit countChanged();
......
......@@ -38,7 +38,7 @@ class RocketChatChannel;
class ChannelModel : public QAbstractListModel
{
Q_OBJECT
enum ChannelRoles {
enum class ChannelRoles : int {
name = Qt::UserRole + 1,
roomId,
unreadMessages,
......@@ -50,10 +50,12 @@ class ChannelModel : public QAbstractListModel
deleted,
ownerId,
ownerName,
username
username,
userStatus,
avatarImg
};
public:
ChannelModel();
ChannelModel() = default;
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
bool addChannel( const QSharedPointer<RocketChatChannel> & );
......
#include "emojismodel.h"
#include <QDebug>
EmojisModel::EmojisModel()
{
mDuplicateCheck.reserve( 2000 );
}
int EmojisModel::rowCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent );
if ( mData.contains( mCurrentCategory ) ) {
return mData[mCurrentCategory].size();
}
return 0;
}
QVariant EmojisModel::data( const QModelIndex &index, int role ) const
{
int row = index.row();
auto list = mData[mCurrentCategory];
auto emoji = list[row];
auto roleType = static_cast<EmojiRoles>( role );
switch ( roleType ) {
case EmojiRoles::emojiText:
return emoji->getIdentifier();
case EmojiRoles::image:
return emoji->getFilePath();
case EmojiRoles::category:
return emoji->getCategory();
case EmojiRoles::order:
return emoji->getOrder();
case EmojiRoles::custom:
if ( emoji->getCategory() == "custom" ) {
return true;
} else {
return false;
}
}
}
void EmojisModel::addEmoji( const QSharedPointer<Emoji> &pEmoji )
{
if ( !mDuplicateCheck.contains( pEmoji->getIdentifier() ) ) {
auto category = pEmoji->getCategory();
int order = pEmoji->getOrder();
if ( mData.contains( category ) ) {
auto list = mData[category];
auto firstElem = list.first();
auto lastElem = list.last();
if ( order <= firstElem->getOrder() ) {
list.prepend( pEmoji );
} else if ( order >= lastElem->getOrder() ) {
list.append( pEmoji );
} else {
int orderDiffLast = lastElem->getOrder() - order;
int listLength = list.length();
if ( orderDiffLast > order ) {
for ( int i = listLength; i >= 0; i-- ) {
if ( list[i]->getOrder() >= order ) {
list.insert( i - 1, pEmoji );
}
}
} else {
for ( int i = 0; i < listLength; i++ ) {
if ( list[i]->getOrder() <= order ) {
list.insert( i, pEmoji );
}
}
}
}
} else {
mData.insert( category, {pEmoji} );
}
mDuplicateCheck.insert( pEmoji->getIdentifier() );
}
}
void EmojisModel::addEmojisByCategory( const QString &pCategory, const QList<QSharedPointer<Emoji> > &pList )
{
if ( mCurrentCategory == pCategory ) {
beginResetModel();
}
if ( !mData.contains( pCategory ) ) {
mData.insert( pCategory, QList<QSharedPointer<Emoji>>() );
mData[pCategory].reserve( 600 );
}
for ( auto &element : pList ) {
if ( !mDuplicateCheck.contains( element->getIdentifier() ) ) {
mDuplicateCheck.insert( element->getIdentifier() );
mData[pCategory].append( element );
}
}
if ( mCurrentCategory == pCategory ) {
endResetModel();
}
}
void EmojisModel::clear()
{
mData.clear();
mDuplicateCheck.clear();
mCurrentCategory.clear();
}
QHash<int, QByteArray> EmojisModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[static_cast<int>( EmojiRoles::emojiText )] = QByteArrayLiteral( "emojiText" );
roles[static_cast<int>( EmojiRoles::category )] = QByteArrayLiteral( "category" );
roles[static_cast<int>( EmojiRoles::image )] = QByteArrayLiteral( "image" );
roles[static_cast<int>( EmojiRoles::order )] = QByteArrayLiteral( "order" );
roles[static_cast<int>( EmojiRoles::custom )] = QByteArrayLiteral( "custom" );
return roles;
}
QString EmojisModel::currentCategory() const
{
return mCurrentCategory;
}
void EmojisModel::setCurrentCategory( const QString &currentCategory )
{
beginResetModel();
mCurrentCategory = currentCategory;
endResetModel();
}
#ifndef EMOJISMODEL_H
#define EMOJISMODEL_H
#include <QAbstractListModel>
#include <QSet>
#include <QMap>
#include <QSharedPointer>
#include <repos/entities/emoji.h>
class EmojisModel: public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY( QString currentCategory READ currentCategory WRITE setCurrentCategory NOTIFY currentCategoryChanged )
public:
enum class EmojiRoles : int {
emojiText = Qt::UserRole,
image,
category,
order,
custom
};
EmojisModel();
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
QString currentCategory() const;
void setCurrentCategory( const QString &currentCategory );
public slots:
void addEmoji( const QSharedPointer<Emoji> &pEmoji );
void addEmojisByCategory( const QString &pCategory, const QList<QSharedPointer<Emoji>> &pList );
void clear();
protected:
QHash<int, QByteArray> roleNames() const;
QString mCurrentCategory;
QSet<QString> mDuplicateCheck;
QMap<QString, QList<QSharedPointer<Emoji>>> mData;
signals:
void currentCategoryChanged( void );
};
#endif // EMOJISMODEL_H
......@@ -46,26 +46,20 @@ int LoginMethodsModel::rowCount( const QModelIndex &parent ) const
QVariant LoginMethodsModel::data( const QModelIndex &index, int role ) const
{
if ( elements.size() >= index.row() ) {
switch ( role ) {
case LoginMethodsRles::service :
auto roleType = static_cast<LoginMethodsRoles>( role );
switch ( roleType ) {
case LoginMethodsRoles::service :
return elements[index.row()][QStringLiteral( "service" )];
break;
case LoginMethodsRles::idpUrl :
case LoginMethodsRoles::idpUrl :
return elements[index.row()][QStringLiteral( "idpUrl" )];
break;
case LoginMethodsRles::buttonText :
case LoginMethodsRoles::buttonText :
return elements[index.row()][QStringLiteral( "buttonText" )];
break;
case LoginMethodsRles::redirectUrl :
case LoginMethodsRoles::redirectUrl :
return elements[index.row()][QStringLiteral( "redirectUrl" )];
break;
default:
return QVariant();
break;
}
}
......@@ -86,10 +80,10 @@ void LoginMethodsModel::clear()
QHash<int, QByteArray> LoginMethodsModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[service] = QByteArrayLiteral( "service" );
roles[idpUrl] = QByteArrayLiteral( "idpUrl" );
roles[buttonText] = QByteArrayLiteral( "buttonText" );
roles[redirectUrl] = QByteArrayLiteral( "redirectUrl" );
roles[static_cast<int>( LoginMethodsRoles::service )] = QByteArrayLiteral( "service" );
roles[static_cast<int>( LoginMethodsRoles::idpUrl )] = QByteArrayLiteral( "idpUrl" );
roles[static_cast<int>( LoginMethodsRoles::buttonText )] = QByteArrayLiteral( "buttonText" );
roles[static_cast<int>( LoginMethodsRoles::redirectUrl )] = QByteArrayLiteral( "redirectUrl" );
return roles;
}
......
......@@ -30,14 +30,15 @@ class LoginMethodsModel : public QAbstractListModel
{
Q_OBJECT
public:
enum LoginMethodsRles {
service = Qt::UserRole + 1,
idpUrl,
buttonText,
redirectUrl
};
explicit LoginMethodsModel( QObject *parent = 0 );
enum class LoginMethodsRoles : int {
service = Qt::UserRole + 1,
idpUrl,
buttonText,
redirectUrl
};
explicit LoginMethodsModel( QObject *parent = nullptr );
public slots:
Q_INVOKABLE QVariantMap get( int row );
Q_INVOKABLE void clear();
......
......@@ -26,118 +26,165 @@
MessagesModel::MessagesModel()
{
duplicateCheck.reserve( 10000 );
}
QVariant MessagesModel::data( const QModelIndex &index, int role ) const
{
int row = index.row();
QSharedPointer<RocketChatMessage> messageAtIndex = messagesOfCurrentChannel.at( row );
auto messageAtIndex = messagesOfCurrentChannel.at( row );
if ( role == MessageRoles::type ) {
return messageAtIndex->getMessageType();
}
auto roleType = static_cast<MessageRoles>( role );
if ( role == MessageRoles::text ) {
return messageAtIndex->getMessageString();
}
switch ( roleType ) {
case MessageRoles::type:
return messageAtIndex->getMessageType();
if ( role == MessageRoles::linkurl ) {
if ( !messageAtIndex->getAttachments().empty() ) {
return messageAtIndex->getAttachments().first()->getUrl();
} else {