Commits (13)
...@@ -143,10 +143,12 @@ QVariant ChannelModel::data( const QModelIndex &index, int role ) const ...@@ -143,10 +143,12 @@ QVariant ChannelModel::data( const QModelIndex &index, int role ) const
bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel ) bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel )
{ {
if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) { if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) {
connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection ); //connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection );
connect(channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged);
duplicateCheck.insert( channel->getRoomId() ); duplicateCheck.insert( channel->getRoomId() );
int pos = channelList.findPosition( channel ); int pos = channelList.findPosition( channel );
if(pos <0){ if(pos <0){
...@@ -168,10 +170,11 @@ bool ChannelModel::addChannelsSlot(const QVector<QSharedPointer<RocketChatChanne ...@@ -168,10 +170,11 @@ bool ChannelModel::addChannelsSlot(const QVector<QSharedPointer<RocketChatChanne
beginResetModel(); beginResetModel();
for(const auto &channel: pChannels){ for(const auto &channel: pChannels){
if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) { if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) {
connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection ); // connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection );
connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection );
connect(channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged);
duplicateCheck.insert( channel->getRoomId() ); duplicateCheck.insert( channel->getRoomId() );
channelList.insertSort( channel ); channelList.insertSort( channel );
} }
...@@ -220,7 +223,7 @@ void ChannelModel::onNewerMessage( const QString &id, qint64 timestamp ) ...@@ -220,7 +223,7 @@ void ChannelModel::onNewerMessage( const QString &id, qint64 timestamp )
//emit dataChanged(index(pos),index(pos),{lastMessageText}); //emit dataChanged(index(pos),index(pos),{lastMessageText});
} }
sortChanged(); // sortChanged();
} }
void ChannelModel::onUnreadMessageChanged( const QString &id, int number ) void ChannelModel::onUnreadMessageChanged( const QString &id, int number )
...@@ -291,6 +294,13 @@ void ChannelModel::onDeleted( const QString &pId, bool deleted ) ...@@ -291,6 +294,13 @@ void ChannelModel::onDeleted( const QString &pId, bool deleted )
} }
void ChannelModel::onChannelOrderChanged(const QString &pId, qint64 pTimestamp)
{
beginResetModel();
channelList.reOrder();
endResetModel();
}
void ChannelModel::addChannelSlot( const QSharedPointer<RocketChatChannel> &channel ) void ChannelModel::addChannelSlot( const QSharedPointer<RocketChatChannel> &channel )
{ {
addChannel( channel ); addChannel( channel );
...@@ -308,11 +318,29 @@ void ChannelModel::setType( const QString &value ) ...@@ -308,11 +318,29 @@ void ChannelModel::setType( const QString &value )
void ChannelModel::sortChanged() void ChannelModel::sortChanged()
{ {
static int counter = 0;
counter++;
qDebug()<<counter;
beginResetModel(); beginResetModel();
channelList.reOrder(); std::sort( channelList.begin(), channelList.end(), []( const QSharedPointer<RocketChatChannel> &channel1, const QSharedPointer<RocketChatChannel> &channel2 ) {
auto youngest1 = channel1->getYoungestMessage();
auto youngest2 = channel2->getYoungestMessage();
if ( !youngest1.isNull() && !youngest2.isNull() ) {
auto timestamp1 = youngest1->getTimestamp();
auto timestamp2 = youngest2->getTimestamp();
return timestamp2 < timestamp1;
}
if ( youngest1.isNull() && !youngest2.isNull() ) {
return false;
}
if ( youngest2.isNull() && !youngest1.isNull() ) {
return true;
}
return false;
} );
endResetModel(); endResetModel();
emit countChanged(); emit countChanged();
} }
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "sortedvector.h" #include "sortedvector.h"
template<typename T> template<typename T>
int SortedVector<T>::insertSort( const QSharedPointer<T> &pointer ) int SortedVector<T>::insertSort( const QSharedPointer<T> &pointer )
{ {
...@@ -32,31 +31,12 @@ int SortedVector<T>::insertSort( const QSharedPointer<T> &pointer ) ...@@ -32,31 +31,12 @@ int SortedVector<T>::insertSort( const QSharedPointer<T> &pointer )
this->append( pointer ); this->append( pointer );
row = 0; row = 0;
} else { } else {
bool lastElementGreaterThanNew = *( *this )[this->count() - 1] > *pointer ; auto elementSmallerThanNew = std::lower_bound( this->begin(), this->end(), pointer, []( const auto & first, const auto & second )->bool{
return ( *first ) > ( *second );
if ( lastElementGreaterThanNew ) { } );
for ( int i = 0; i < this->count(); i++ ) {
if ( *( *this )[i] > *pointer ) {
this->insert( i, pointer );
row = i;
break;
}
}
} else {
for ( int i = this->count() - 1; i ; i-- ) {
if ( *( *this )[i] < *pointer ) {
this->insert( i, pointer );
row = i;
break;
}
}
}
if ( row == -1 ) {
row = this->count();
this->append( pointer );
}
this->insert( elementSmallerThanNew, pointer );
row = elementSmallerThanNew - this->begin();
} }
return row; return row;
...@@ -66,17 +46,11 @@ template<typename T> ...@@ -66,17 +46,11 @@ template<typename T>
int SortedVector<T>::findInsertPosition( const QSharedPointer<T> &pointer ) const int SortedVector<T>::findInsertPosition( const QSharedPointer<T> &pointer ) const
{ {
int row = -1; int row = -1;
auto elementSmallerThanNew = std::lower_bound( this->begin(), this->end(), pointer, []( const auto & first, const auto & second )->bool{
return ( *first ) > ( *second );
} );
for ( int i = 0; i < this->count(); i++ ) { row = elementSmallerThanNew - this->begin();
if ( *( *this )[i] > *pointer ) {
row = i;
break;
}
}
if ( row == -1 ) {
row = this->count();
}
return row; return row;
} }
...@@ -84,21 +58,15 @@ int SortedVector<T>::findInsertPosition( const QSharedPointer<T> &pointer ) cons ...@@ -84,21 +58,15 @@ int SortedVector<T>::findInsertPosition( const QSharedPointer<T> &pointer ) cons
template<typename T> template<typename T>
int SortedVector<T>::findPosition( const QSharedPointer<T> &pointer ) const int SortedVector<T>::findPosition( const QSharedPointer<T> &pointer ) const
{ {
int row = -1; auto pos = find( this->begin(), this->end(), pointer );
return pos - this->begin();
for ( int i = 0; i < this->count(); i++ ) {
if ( ( *this )[i] == pointer ) {
row = i;
break;
}
}
return row;
} }
template<typename T> template<typename T>
void SortedVector<T>::reOrder() void SortedVector<T>::reOrder()
{ {
std::sort(this->begin(),this->end()); std::sort( this->begin(), this->end(), [ = ]( const auto & first, const auto & second )->bool{
return ( *first ) > ( *second );
} );
} }
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <QVector> #include <QVector>
#include <QSharedPointer> #include <QSharedPointer>
#include <QMutex>
template<typename T> class SortedVector : public QVector<QSharedPointer<T>> template<typename T> class SortedVector : public QVector<QSharedPointer<T>>
{ {
public: public:
...@@ -37,6 +38,8 @@ template<typename T> class SortedVector : public QVector<QSharedPointer<T>> ...@@ -37,6 +38,8 @@ template<typename T> class SortedVector : public QVector<QSharedPointer<T>>
this->append( o ); this->append( o );
return true; return true;
} }
private:
mutable QMutex mSortMutex;
}; };
#include "sortedvector.cpp" #include "sortedvector.cpp"
#endif // SORTEDVECTOR_H #endif // SORTEDVECTOR_H
...@@ -168,7 +168,7 @@ QList<QSharedPointer<RocketChatMessage>> RocketChatChannel::addMessages( const Q ...@@ -168,7 +168,7 @@ QList<QSharedPointer<RocketChatMessage>> RocketChatChannel::addMessages( const Q
} }
} }
if(newMessages.count()){ if(newMessages.count()){
emit messageAdded( getRoomId(), 0 ); // emit messageAdded( getRoomId(), 0 );
} }
return newMessages; return newMessages;
} }
...@@ -192,8 +192,10 @@ unsigned int RocketChatChannel::getUnreadMessages() const ...@@ -192,8 +192,10 @@ unsigned int RocketChatChannel::getUnreadMessages() const
void RocketChatChannel::setUnreadMessages( unsigned int value ) void RocketChatChannel::setUnreadMessages( unsigned int value )
{ {
qDebug() << "set unread to: " << value; qDebug() << "set unread to: " << value;
mUnreadMessages = value; if(mUnreadMessages != value){
emit unreadMessagesChanged( mRoomId, value ); mUnreadMessages = value;
emit unreadMessagesChanged( mRoomId, value );
}
} }
const QString &RocketChatChannel::getType() const const QString &RocketChatChannel::getType() const
...@@ -262,12 +264,12 @@ void RocketChatChannel::setSelfMuted( bool value ) ...@@ -262,12 +264,12 @@ void RocketChatChannel::setSelfMuted( bool value )
int RocketChatChannel::operator >( const RocketChatChannel &channel ) const int RocketChatChannel::operator >( const RocketChatChannel &channel ) const
{ {
return channel.getUpdatedAt() > channel.getUpdatedAt(); return getUpdatedAt() > channel.getUpdatedAt();
} }
int RocketChatChannel::operator <( const RocketChatChannel &channel ) const int RocketChatChannel::operator <( const RocketChatChannel &channel ) const
{ {
return channel.getUpdatedAt() < channel.getUpdatedAt(); return getUpdatedAt() < channel.getUpdatedAt();
} }
bool RocketChatChannel::getDeleted() const bool RocketChatChannel::getDeleted() const
...@@ -327,10 +329,12 @@ const QSharedPointer<RocketChatUser> &RocketChatChannel::getChatPartner() const ...@@ -327,10 +329,12 @@ const QSharedPointer<RocketChatUser> &RocketChatChannel::getChatPartner() const
void RocketChatChannel::setChatPartner( const QSharedPointer<RocketChatUser> &chatPartner ) void RocketChatChannel::setChatPartner( const QSharedPointer<RocketChatUser> &chatPartner )
{ {
mChatPartner = chatPartner; if(mChatPartner != chatPartner){
connect( chatPartner.data(), &RocketChatUser::statusChanged, this, [ = ]() { mChatPartner = chatPartner;
emit dataChanged( mRoomId, "userStatus" ); connect( chatPartner.data(), &RocketChatUser::statusChanged, this, [ = ]() {
} ); emit dataChanged( mRoomId, "userStatus" );
} );
}
} }
const QString &RocketChatChannel::getChatPartnerId() const const QString &RocketChatChannel::getChatPartnerId() const
...@@ -366,9 +370,12 @@ qint64 RocketChatChannel::getUpdatedAt() const ...@@ -366,9 +370,12 @@ qint64 RocketChatChannel::getUpdatedAt() const
void RocketChatChannel::setUpdatedAt( const qint64 &updatedAt ) void RocketChatChannel::setUpdatedAt( const qint64 &updatedAt )
{ {
//skip signal the first time
if(mUpdatedAt != updatedAt){ if(mUpdatedAt != updatedAt){
if(mUpdatedAt != -1){
emit updatedChanged(mRoomId,mUpdatedAt);
}
mUpdatedAt = updatedAt; mUpdatedAt = updatedAt;
emit updatedChanged(mRoomId,mUpdatedAt);
} }
} }
......
...@@ -87,11 +87,11 @@ RocketChat::RocketChat( QGuiApplication *app ) ...@@ -87,11 +87,11 @@ RocketChat::RocketChat( QGuiApplication *app )
#endif #endif
qRegisterMetaType<ConnectionState>( "ConnectionState" ); qRegisterMetaType<ConnectionState>( "ConnectionState" );
qRegisterMetaType<Qt::ApplicationState>( "Qt::ApplicationState" ); qRegisterMetaType<Qt::ApplicationState>( "Qt::ApplicationState" );
qRegisterMetaType<RocketChatServerData*>( "RocketChatServerData*" ); qRegisterMetaType<RocketChatServerData *>( "RocketChatServerData*" );
// qInstallMessageHandler( customOutput ); // qInstallMessageHandler( customOutput );
qDebug()<<"threadId ctr"<<QThread::currentThreadId(); qDebug() << "threadId ctr" << QThread::currentThreadId();
} }
RocketChat::~RocketChat() RocketChat::~RocketChat()
...@@ -305,7 +305,7 @@ void RocketChat::deleteMessage( QString rid, QString id ) ...@@ -305,7 +305,7 @@ void RocketChat::deleteMessage( QString rid, QString id )
QMetaObject::invokeMethod( mServerMap.first(), "deleteMessage", Q_ARG( QString, rid ), Q_ARG( QString, id ) ); QMetaObject::invokeMethod( mServerMap.first(), "deleteMessage", Q_ARG( QString, rid ), Q_ARG( QString, id ) );
} }
void RocketChat::resetConnection(void) void RocketChat::resetConnection( void )
{ {
QMetaObject::invokeMethod( mServerMap.first(), "reconnect" ); QMetaObject::invokeMethod( mServerMap.first(), "reconnect" );
} }
...@@ -323,15 +323,15 @@ bool RocketChat::newServerByDomain( const QString &domain, bool pUnsecure = fals ...@@ -323,15 +323,15 @@ bool RocketChat::newServerByDomain( const QString &domain, bool pUnsecure = fals
mServerMap.clear(); mServerMap.clear();
if ( firstServer != nullptr ) { if ( firstServer != nullptr ) {
connect( firstServer, &RocketChatServerData::destroyed, [ =,serverptr=this ]() { connect( firstServer, &RocketChatServerData::destroyed, [ =, serverptr = this ]() {
qDebug() << "domain: " << domain; qDebug() << "domain: " << domain;
Models::resetModels(); Models::resetModels();
qDebug()<<"threadId destroyed"<<QThread::currentThreadId(); qDebug() << "threadId destroyed" << QThread::currentThreadId();
RocketChatServerData *server = new RocketChatServerData( domain, domain, pUnsecure ); RocketChatServerData *server = new RocketChatServerData( domain, domain, pUnsecure );
QMetaObject::invokeMethod(serverptr, "registerServer", Q_ARG(RocketChatServerData*, server )); QMetaObject::invokeMethod( serverptr, "registerServer", Q_ARG( RocketChatServerData *, server ) );
//registerServer( server ); //registerServer( server );
newServerMutex.unlock(); newServerMutex.unlock();
} ); } );
QMetaObject::invokeMethod( firstServer, "deleteLater" ); QMetaObject::invokeMethod( firstServer, "deleteLater" );
...@@ -445,17 +445,17 @@ void RocketChat::openFileExternally( const QString &pPath ) ...@@ -445,17 +445,17 @@ void RocketChat::openFileExternally( const QString &pPath )
#if defined( Q_OS_ANDROID) #if defined( Q_OS_ANDROID)
QString q = pPath; QString q = pPath;
QAndroidJniObject jniObject = QAndroidJniObject::fromString(q); QAndroidJniObject jniObject = QAndroidJniObject::fromString( q );
jstring j = jniObject.object<jstring>(); jstring j = jniObject.object<jstring>();
QtAndroid::androidActivity().callObjectMethod("openFile","(Ljava/lang/String;)Ljava/lang/String;",j); QtAndroid::androidActivity().callObjectMethod( "openFile", "(Ljava/lang/String;)Ljava/lang/String;", j );
#elif defined(Q_OS_LINUX) #elif defined(Q_OS_LINUX)
QDesktopServices::openUrl(pPath); QDesktopServices::openUrl( pPath );
#elif defined (Q_OS_IOS) #elif defined (Q_OS_IOS)
emit openIOSFileDialog( pPath ); emit openIOSFileDialog( pPath );
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
QDesktopServices::openUrl(pPath); QDesktopServices::openUrl( pPath );
#elif defined(Q_OS_MACOS) #elif defined(Q_OS_MACOS)
QDesktopServices::openUrl(pPath); QDesktopServices::openUrl( pPath );
#endif #endif
...@@ -608,7 +608,7 @@ void RocketChat::createPrivateGroup( const QString &pServerId, const QString &pC ...@@ -608,7 +608,7 @@ void RocketChat::createPrivateGroup( const QString &pServerId, const QString &pC
void RocketChat::registerServer( RocketChatServerData *pServer ) void RocketChat::registerServer( RocketChatServerData *pServer )
{ {
qDebug()<<"threadId register"<<QThread::currentThreadId(); qDebug() << "threadId register" << QThread::currentThreadId();
pServer->moveToThread( &mServerThread ); pServer->moveToThread( &mServerThread );
qDebug() << "add server to map"; qDebug() << "add server to map";
...@@ -671,7 +671,7 @@ void RocketChat::onUnreadCountChanged( const QString &pServerId, uint pUnread ) ...@@ -671,7 +671,7 @@ void RocketChat::onUnreadCountChanged( const QString &pServerId, uint pUnread )
mUnreadSum[pServerId] = pUnread; mUnreadSum[pServerId] = pUnread;
// qDebug() << "onUnreadCountChanged"; // qDebug() << "onUnreadCountChanged";
uint number = 0; int number = 0;
if ( mServerStatus ) { if ( mServerStatus ) {
for ( auto count : mUnreadSum ) { for ( auto count : mUnreadSum ) {
......
...@@ -1517,16 +1517,16 @@ bool RocketChatServerData::isUserJoinMessage( const QJsonObject &pMessage ) cons ...@@ -1517,16 +1517,16 @@ bool RocketChatServerData::isUserJoinMessage( const QJsonObject &pMessage ) cons
void RocketChatServerData::handleStreamRoomMessage( const QJsonObject &pMessage ) void RocketChatServerData::handleStreamRoomMessage( const QJsonObject &pMessage )
{ {
if ( Q_LIKELY( pMessage.contains( QStringLiteral( "fields" ) ) ) ) { if ( Q_LIKELY( pMessage.contains( QStringLiteral( "fields" ) ) ) ) {
QJsonObject fields = pMessage[QStringLiteral( "fields" )].toObject(); auto fields = pMessage[QStringLiteral( "fields" )].toObject();
if ( Q_LIKELY( fields.contains( QStringLiteral( "args" ) ) ) ) { if ( Q_LIKELY( fields.contains( QStringLiteral( "args" ) ) ) ) {
QJsonArray args = fields[QStringLiteral( "args" )].toArray(); auto args = fields[QStringLiteral( "args" )].toArray();
for ( const auto currentArg : args ) { for ( const auto currentArg : args ) {
QJsonObject currentArgObj = currentArg.toObject(); auto currentArgObj = currentArg.toObject();
if ( Q_LIKELY( currentArgObj.contains( QStringLiteral( "rid" ) ) && currentArgObj.contains( QStringLiteral( "_id" ) ) ) ) { if ( Q_LIKELY( currentArgObj.contains( QStringLiteral( "rid" ) ) && currentArgObj.contains( QStringLiteral( "_id" ) ) ) ) {
QString roomId = currentArgObj[QStringLiteral( "rid" )].toString(); auto roomId = currentArgObj[QStringLiteral( "rid" )].toString();
auto message = mMessageService->parseMessage( currentArgObj, true ); auto message = mMessageService->parseMessage( currentArgObj, true );
if ( Q_LIKELY( mChannels->contains( roomId ) ) ) { if ( Q_LIKELY( mChannels->contains( roomId ) ) ) {
...@@ -1541,6 +1541,7 @@ void RocketChatServerData::handleStreamRoomMessage( const QJsonObject &pMessage ...@@ -1541,6 +1541,7 @@ void RocketChatServerData::handleStreamRoomMessage( const QJsonObject &pMessage
} }
} }
} }
} }
} }
......