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