Commit 52e8b616 authored by Armin Felder's avatar Armin Felder
Browse files

anged mutex

parent 2880990c
...@@ -168,13 +168,13 @@ void UserModel::onCurrentChannelChanged( const QString &newText ) ...@@ -168,13 +168,13 @@ void UserModel::onCurrentChannelChanged( const QString &newText )
QSharedPointer<RocketChatUser> UserModel::getUserById( const QString &pId ) QSharedPointer<RocketChatUser> UserModel::getUserById( const QString &pId )
{ {
QSharedPointer<RocketChatUser> user; QSharedPointer<RocketChatUser> user;
mMutex.lock(); //mMutex.lock();
if ( mAllUsers.contains( pId ) ) { if ( mAllUsers.contains( pId ) ) {
user = mAllUsers[pId]; user = mAllUsers[pId];
} }
mMutex.unlock(); //mMutex.unlock();
return user; return user;
} }
......
...@@ -68,7 +68,7 @@ class UserModel: public QAbstractListModel ...@@ -68,7 +68,7 @@ class UserModel: public QAbstractListModel
QMap<QString, QSharedPointer<RocketChatUser>> userOfCurrentChannel; QMap<QString, QSharedPointer<RocketChatUser>> userOfCurrentChannel;
QMap<QString, QSharedPointer<RocketChatUser>> mAllUsers; QMap<QString, QSharedPointer<RocketChatUser>> mAllUsers;
QString current; QString current;
private: public:
QMutex mMutex; QMutex mMutex;
signals: signals:
......
...@@ -87,7 +87,11 @@ RocketChat::RocketChat( QGuiApplication *app ) ...@@ -87,7 +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*" );
// qInstallMessageHandler( customOutput ); // qInstallMessageHandler( customOutput );
qDebug()<<"threadId ctr"<<QThread::currentThreadId();
} }
RocketChat::~RocketChat() RocketChat::~RocketChat()
...@@ -319,19 +323,22 @@ bool RocketChat::newServerByDomain( const QString &domain, bool pUnsecure = fals ...@@ -319,19 +323,22 @@ bool RocketChat::newServerByDomain( const QString &domain, bool pUnsecure = fals
mServerMap.clear(); mServerMap.clear();
if ( firstServer != nullptr ) { if ( firstServer != nullptr ) {
connect( firstServer, &RocketChatServerData::destroyed, [ = ]() { connect( firstServer, &RocketChatServerData::destroyed, [ =,serverptr=this ]() {
qDebug() << "domain: " << domain; qDebug() << "domain: " << domain;
Models::resetModels();
qDebug()<<"threadId destroyed"<<QThread::currentThreadId();
Models::resetModels(); RocketChatServerData *server = new RocketChatServerData( domain, domain, pUnsecure );
QMetaObject::invokeMethod(serverptr, "registerServer", Q_ARG(RocketChatServerData*, server ));
RocketChatServerData *server = new RocketChatServerData( domain, domain, pUnsecure ); //registerServer( server );
newServerMutex.unlock();
registerServer( server );
newServerMutex.unlock();
} ); } );
QMetaObject::invokeMethod( firstServer, "deleteLater" ); QMetaObject::invokeMethod( firstServer, "deleteLater" );
} }
} else { } else {
QString protocol = QStringLiteral( "https://" ); QString protocol = QStringLiteral( "https://" );
...@@ -601,6 +608,8 @@ void RocketChat::createPrivateGroup( const QString &pServerId, const QString &pC ...@@ -601,6 +608,8 @@ void RocketChat::createPrivateGroup( const QString &pServerId, const QString &pC
void RocketChat::registerServer( RocketChatServerData *pServer ) void RocketChat::registerServer( RocketChatServerData *pServer )
{ {
qDebug()<<"threadId register"<<QThread::currentThreadId();
pServer->moveToThread( &mServerThread ); pServer->moveToThread( &mServerThread );
qDebug() << "add server to map"; qDebug() << "add server to map";
mServerMap[pServer->getServerId()] = pServer; mServerMap[pServer->getServerId()] = pServer;
......
...@@ -141,7 +141,7 @@ class RocketChat : public QObject ...@@ -141,7 +141,7 @@ class RocketChat : public QObject
Q_INVOKABLE void deleteMessage(QString rid,QString id); Q_INVOKABLE void deleteMessage(QString rid,QString id);
Q_INVOKABLE void resetConnection(void); Q_INVOKABLE void resetConnection(void);
void registerServer( RocketChatServerData *pServer ); Q_INVOKABLE void registerServer( RocketChatServerData *pServer );
void addLoginMethod( const QMap<QString, QVariant> &pMethod ); void addLoginMethod( const QMap<QString, QVariant> &pMethod );
......
...@@ -38,6 +38,18 @@ RocketChatServerData::RocketChatServerData( QString pId, QString pBaseUrl, bool ...@@ -38,6 +38,18 @@ RocketChatServerData::RocketChatServerData( QString pId, QString pBaseUrl, bool
} }
RocketChatServerData::~RocketChatServerData()
{
mMessageService->deleteLater();
mUnsendDdpRequests.clear();
mEmojiRepo->clear();
mChannels->clear();
mFilesRepo->clear();
delete mFileService;
delete mEmojiService;
mFileUploads.clear();
}
void RocketChatServerData::init() void RocketChatServerData::init()
{ {
initDb(); initDb();
...@@ -45,6 +57,8 @@ void RocketChatServerData::init() ...@@ -45,6 +57,8 @@ void RocketChatServerData::init()
void RocketChatServerData::initDb() void RocketChatServerData::initDb()
{ {
qDebug()<<" "<<QThread::currentThreadId();
mStorage = PersistanceLayer::instance(); mStorage = PersistanceLayer::instance();
connect( mStorage, &PersistanceLayer::ready, this, &RocketChatServerData::initConnections, Qt::UniqueConnection ); connect( mStorage, &PersistanceLayer::ready, this, &RocketChatServerData::initConnections, Qt::UniqueConnection );
mStorage->init(); mStorage->init();
...@@ -52,6 +66,8 @@ void RocketChatServerData::initDb() ...@@ -52,6 +66,8 @@ void RocketChatServerData::initDb()
void RocketChatServerData::initConnections() void RocketChatServerData::initConnections()
{ {
qDebug()<<" "<<QThread::currentThreadId();
historyLoaded = [ = ]( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages ) { historyLoaded = [ = ]( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages ) {
if ( messages ) { if ( messages ) {
mStorage->transaction(); mStorage->transaction();
...@@ -486,21 +502,28 @@ QString RocketChatServerData::getUserId() const ...@@ -486,21 +502,28 @@ QString RocketChatServerData::getUserId() const
void RocketChatServerData::setUserId( const QString &userId ) void RocketChatServerData::setUserId( const QString &userId )
{ {
auto users = Models::getUsersModel(); //TODO: do this differently
auto user = users->getUserById( userId ); auto users = Models::getUsersModel();
if(users){
qDebug()<<" "<<QThread::currentThreadId();
users->mMutex.lock();
auto user = users->getUserById( userId );
if ( user.isNull() && !userId.isEmpty() ) {
user = QSharedPointer<RocketChatUser>::create( userId ) ; //QSharedPointer<RocketChatUser>( new RocketChatUser( userId ) );
auto result = QMetaObject::invokeMethod( userModel, "addUser", Q_ARG( User, user ) );
Q_ASSERT( result );
}
if ( user.isNull() && !userId.isEmpty() ) { if ( !user.isNull() && !userId.isEmpty() ) {
user = QSharedPointer<RocketChatUser>::create( userId ) ; //QSharedPointer<RocketChatUser>( new RocketChatUser( userId ) ); mUserId = userId;
auto result = QMetaObject::invokeMethod( userModel, "addUser", Q_ARG( User, user ) );
Q_ASSERT( result );
}
if ( !user.isNull() && !userId.isEmpty() ) { connect( user.data(), &RocketChatUser::statusChanged, this, [ = ]() {
mUserId = userId; emit userStatusChanged( static_cast<int>( user->getStatus() ) );
} );
}
users->mMutex.unlock();
connect( user.data(), &RocketChatUser::statusChanged, this, [ = ]() {
emit userStatusChanged( static_cast<int>( user->getStatus() ) );
} );
} }
} }
...@@ -1359,6 +1382,8 @@ void RocketChatServerData::onDDPMessageReceived( const QJsonObject &pMessage ) ...@@ -1359,6 +1382,8 @@ void RocketChatServerData::onDDPMessageReceived( const QJsonObject &pMessage )
QString msg = pMessage[QStringLiteral( "msg" )].toString(); QString msg = pMessage[QStringLiteral( "msg" )].toString();
QJsonObject fields = pMessage["fields"].toObject(); QJsonObject fields = pMessage["fields"].toObject();
userModel->mMutex.lock();
//race condition regarding user insertion into model, fixed for now with mutex and merge in model //race condition regarding user insertion into model, fixed for now with mutex and merge in model
auto user = userModel->getUserById( userId ); auto user = userModel->getUserById( userId );
...@@ -1386,6 +1411,7 @@ void RocketChatServerData::onDDPMessageReceived( const QJsonObject &pMessage ) ...@@ -1386,6 +1411,7 @@ void RocketChatServerData::onDDPMessageReceived( const QJsonObject &pMessage )
//TODO: delete user //TODO: delete user
user->setStatus( RocketChatUser::status::OFFLINE ); user->setStatus( RocketChatUser::status::OFFLINE );
} }
} else { } else {
auto newUser = QSharedPointer<RocketChatUser>::create( userId ); auto newUser = QSharedPointer<RocketChatUser>::create( userId );
...@@ -1412,6 +1438,8 @@ void RocketChatServerData::onDDPMessageReceived( const QJsonObject &pMessage ) ...@@ -1412,6 +1438,8 @@ void RocketChatServerData::onDDPMessageReceived( const QJsonObject &pMessage )
auto result = QMetaObject::invokeMethod( userModel, "addUser", Q_ARG( User, newUser ) ); auto result = QMetaObject::invokeMethod( userModel, "addUser", Q_ARG( User, newUser ) );
Q_ASSERT( result ); Q_ASSERT( result );
} }
userModel->mMutex.unlock();
} else if ( pMessage[QStringLiteral( "collection" )] == QStringLiteral( "autocompleteRecords" ) ) { } else if ( pMessage[QStringLiteral( "collection" )] == QStringLiteral( "autocompleteRecords" ) ) {
......
...@@ -120,6 +120,7 @@ class RocketChatServerData : public MessageListener ...@@ -120,6 +120,7 @@ class RocketChatServerData : public MessageListener
RocketChatServerData( QString pId, QString pBaseUrl, RocketChatServerData( QString pId, QString pBaseUrl,
bool pUnsecure = false ); bool pUnsecure = false );
~RocketChatServerData();
void setServerId( const QString &pValue ); void setServerId( const QString &pValue );
void persistData(); void persistData();
......
...@@ -51,7 +51,9 @@ void EmojiService::loadCustomEmojis( std::function<void ( QList<QSharedPointer<E ...@@ -51,7 +51,9 @@ void EmojiService::loadCustomEmojis( std::function<void ( QList<QSharedPointer<E
handleCustomEmojisReceived( data ); handleCustomEmojisReceived( data );
}; };
auto request = QSharedPointer<DDPMethodRequest>::create( QStringLiteral( "listEmojiCustom" ), params, success ); auto request = QSharedPointer<DDPMethodRequest>::create( QStringLiteral( "listEmojiCustom" ), params, success );
server->sendDdprequest( request ); if(server){
server->sendDdprequest( request );
}
} }
/** /**
* Loads the Emoji Data from the databse and processes it to emoji Objects * Loads the Emoji Data from the databse and processes it to emoji Objects
......
...@@ -70,6 +70,7 @@ FileService::FileService( RocketChatServerData *server ) ...@@ -70,6 +70,7 @@ FileService::FileService( RocketChatServerData *server )
*/ */
bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pRequest ) bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pRequest )
{ {
if(mServer){
auto repo = mServer->getFiles(); auto repo = mServer->getFiles();
if ( !pRequest.isNull() && pRequest->url.size() > 0 ) { if ( !pRequest.isNull() && pRequest->url.size() > 0 ) {
...@@ -137,25 +138,28 @@ bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pReques ...@@ -137,25 +138,28 @@ bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pReques
} }
} }
}
return false; return false;
} }
void FileService::getFilesFromDb() void FileService::getFilesFromDb()
{ {
auto files = mStorage->getFiles(); if(mServer){
auto repo = mServer->getFiles(); auto files = mStorage->getFiles();
auto repo = mServer->getFiles();
for ( const auto &file : files ) { for ( const auto &file : files ) {
auto url = file["url"]; auto url = file["url"];
auto path = file["path"]; auto path = file["path"];
repo->add( url, QSharedPointer<TempFile>::create( path, url ) ); repo->add( url, QSharedPointer<TempFile>::create( path, url ) );
}
} }
} }
QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< FileRequest > &pRequest ) QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< FileRequest > &pRequest )
{ {
if ( !pRequest.isNull() ) { if ( !pRequest.isNull()&&mServer ) {
auto repo = mServer->getFiles(); auto repo = mServer->getFiles();
if ( repo == nullptr ) { if ( repo == nullptr ) {
...@@ -205,7 +209,7 @@ QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< Fi ...@@ -205,7 +209,7 @@ QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< Fi
void FileService::getFileFromServer( const QSharedPointer< FileRequest > &pRequest ) void FileService::getFileFromServer( const QSharedPointer< FileRequest > &pRequest )
{ {
if ( Q_LIKELY( !pRequest.isNull() ) ) { if ( Q_LIKELY( !pRequest.isNull()&&mServer ) ) {
auto baseUrl = mServer->getRestApi()->getBaseUrl(); auto baseUrl = mServer->getRestApi()->getBaseUrl();
auto fullUrl = baseUrl + pRequest->url ; auto fullUrl = baseUrl + pRequest->url ;
......
...@@ -110,6 +110,7 @@ void MessageService::persistMessages( const MessageList &pMessage ) ...@@ -110,6 +110,7 @@ void MessageService::persistMessages( const MessageList &pMessage )
void MessageService::persist() void MessageService::persist()
{ {
if(mServer){
mPersistanceLayer->transaction(); mPersistanceLayer->transaction();
auto channels = mServer->getChannels(); auto channels = mServer->getChannels();
...@@ -126,11 +127,13 @@ void MessageService::persist() ...@@ -126,11 +127,13 @@ void MessageService::persist()
} }
mPersistanceLayer->askForcommit(); mPersistanceLayer->askForcommit();
}
} }
QSharedPointer<RocketChatMessage> MessageService::parseMessage( const QJsonObject &pMessageData, QSharedPointer<RocketChatMessage> MessageService::parseMessage( const QJsonObject &pMessageData,
bool linkify ) bool linkify )
{ {
if(mServer){
ChatMessage message( nullptr ); ChatMessage message( nullptr );
QString userId = mServer->getUserId(); QString userId = mServer->getUserId();
bool blocked = false; bool blocked = false;
...@@ -241,7 +244,7 @@ QSharedPointer<RocketChatMessage> MessageService::parseMessage( const QJsonObjec ...@@ -241,7 +244,7 @@ QSharedPointer<RocketChatMessage> MessageService::parseMessage( const QJsonObjec
if ( !blocked ) { if ( !blocked ) {
return message; return message;
} }
}
return nullptr; return nullptr;
} }
...@@ -255,14 +258,17 @@ void MessageService::addUserToBlockList( const QString &pId ) ...@@ -255,14 +258,17 @@ void MessageService::addUserToBlockList( const QString &pId )
void MessageService::addChannelToBlockList( const QString &pId ) void MessageService::addChannelToBlockList( const QString &pId )
{ {
if(mServer){
QString roomId = pId; QString roomId = pId;
QString userId = mServer->getUserId(); QString userId = mServer->getUserId();
QString otherUserId = roomId.replace( userId, "" ); QString otherUserId = roomId.replace( userId, "" );
addUserToBlockList( otherUserId ); addUserToBlockList( otherUserId );
}
} }
void MessageService::searchMessage( const QString &pTerm, const QString &pRoom ) void MessageService::searchMessage( const QString &pTerm, const QString &pRoom )
{ {
if(mServer){
DdpCallback ddpSuccess = [ = ]( const QJsonObject & pResponse, DdpCallback ddpSuccess = [ = ]( const QJsonObject & pResponse,
MeteorDDP * pDdp ) { MeteorDDP * pDdp ) {
Q_UNUSED( pDdp ) Q_UNUSED( pDdp )
...@@ -290,11 +296,12 @@ void MessageService::searchMessage( const QString &pTerm, const QString &pRoom ) ...@@ -290,11 +296,12 @@ void MessageService::searchMessage( const QString &pTerm, const QString &pRoom )
request->setSuccess( ddpSuccess ); request->setSuccess( ddpSuccess );
mServer->sendDdprequest( request ); mServer->sendDdprequest( request );
}
} }
void MessageService::loadHistoryFromServer( const QSharedPointer<LoadHistoryRequestContainer> &pContainer ) void MessageService::loadHistoryFromServer( const QSharedPointer<LoadHistoryRequestContainer> &pContainer )
{ {
if ( !pContainer.isNull() ) { if ( !pContainer.isNull()&&mServer ) {
auto requests = pContainer->getRequests(); auto requests = pContainer->getRequests();
pContainer->mAlredayReceived = 0; pContainer->mAlredayReceived = 0;
...@@ -572,6 +579,7 @@ void MessageService::deleteMessagesNotInList(MessageMap *pMessages,QString pChan ...@@ -572,6 +579,7 @@ void MessageService::deleteMessagesNotInList(MessageMap *pMessages,QString pChan
{ {
QMap<qint64,QSharedPointer<RocketChatMessage>> timeIndex; QMap<qint64,QSharedPointer<RocketChatMessage>> timeIndex;
QList<QString> ids; QList<QString> ids;
if(mServer){
auto channel = mServer->getChannels()->get(pChannelId); auto channel = mServer->getChannels()->get(pChannelId);
for(const auto message : pMessages->values(pChannelId)){ for(const auto message : pMessages->values(pChannelId)){
...@@ -588,4 +596,5 @@ void MessageService::deleteMessagesNotInList(MessageMap *pMessages,QString pChan ...@@ -588,4 +596,5 @@ void MessageService::deleteMessagesNotInList(MessageMap *pMessages,QString pChan
for(QString messageId : blacklist){ for(QString messageId : blacklist){
mPersistanceLayer->deleteMessage(messageId); mPersistanceLayer->deleteMessage(messageId);
} }
}
} }
...@@ -38,6 +38,7 @@ RocketChatChannelService::RocketChatChannelService( QObject *parent, RocketChatS ...@@ -38,6 +38,7 @@ RocketChatChannelService::RocketChatChannelService( QObject *parent, RocketChatS
QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, bool insertIntoRepo ) QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, bool insertIntoRepo )
{ {
if(mServer){
auto ptr = QSharedPointer<RocketChatChannel>::create( mServer, mMessageService, pRoomId, pName, pType ); auto ptr = QSharedPointer<RocketChatChannel>::create( mServer, mMessageService, pRoomId, pName, pType );
if ( Q_LIKELY( mChannels ) ) { if ( Q_LIKELY( mChannels ) ) {
...@@ -49,10 +50,14 @@ QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( ...@@ -49,10 +50,14 @@ QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject(
fillChannelWithMessages( ptr ); fillChannelWithMessages( ptr );
return ptr; return ptr;
}else{
return nullptr;
}
} }
QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, const QString &pUsername, bool insertIntoRepo ) QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, const QString &pUsername, bool insertIntoRepo )
{ {
if(mServer){
auto ptr = QSharedPointer<RocketChatChannel>::create( mServer, mMessageService, pRoomId, pName, pType ); auto ptr = QSharedPointer<RocketChatChannel>::create( mServer, mMessageService, pRoomId, pName, pType );
if ( pType == "d" ) { if ( pType == "d" ) {
...@@ -77,6 +82,9 @@ QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( ...@@ -77,6 +82,9 @@ QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject(
fillChannelWithMessages( ptr ); fillChannelWithMessages( ptr );
return ptr; return ptr;
}else{
return nullptr;
}
} }
...@@ -84,6 +92,7 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha ...@@ -84,6 +92,7 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha
{ {
QVector<QSharedPointer<RocketChatChannel> > vec; QVector<QSharedPointer<RocketChatChannel> > vec;
if(mServer){
std::tuple<QString, QString> openChannelTupel = mStorage->getCurrentChannel(); std::tuple<QString, QString> openChannelTupel = mStorage->getCurrentChannel();
QString openChannel = std::get<0>( openChannelTupel ); QString openChannel = std::get<0>( openChannelTupel );
mStorage->transaction(); mStorage->transaction();
...@@ -207,7 +216,7 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha ...@@ -207,7 +216,7 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha
} }
mStorage->askForcommit(); mStorage->askForcommit();
}
return vec; return vec;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment