diff --git a/persistancelayer.cpp b/persistancelayer.cpp index 1274e217dc011d29b6e0dc5991b427777d04573c..150761ff5f64d37e271448510026f3746bf373ba 100755 --- a/persistancelayer.cpp +++ b/persistancelayer.cpp @@ -42,7 +42,7 @@ PersistanceLayer *PersistanceLayer::instance() { if ( persistanceLayer == nullptr ) { persistanceLayer = new PersistanceLayer; - persistanceLayer->init(); + //persistanceLayer->init(); } return persistanceLayer; @@ -1435,7 +1435,6 @@ void PersistanceLayer::init() QString directory = QStandardPaths::writableLocation( QStandardPaths::AppLocalDataLocation ); QString filePath = directory + QStringLiteral( "/data.sqlite" ); qDebug() << "SQLITE: " << filePath; - mDb.setDatabaseName( filePath ); QDir dbPath( directory ); @@ -1458,3 +1457,8 @@ void PersistanceLayer::init() initShema(); emit( ready() ); } + +void PersistanceLayer::close() +{ + delete persistanceLayer; +} diff --git a/persistancelayer.h b/persistancelayer.h index ce4a8c5fa573b3563bfa021cca12340037aa9ed7..6e65901454fd229f8e7ba224ec1ca2c80dd5c728 100755 --- a/persistancelayer.h +++ b/persistancelayer.h @@ -90,6 +90,8 @@ class PersistanceLayer : public QObject ~PersistanceLayer(); + void init(); + void close(); private: PersistanceLayer() {} @@ -97,7 +99,6 @@ class PersistanceLayer : public QObject QMimeDatabase mMimeDb; QMutex mMutex; QAtomicInt counter = 0; - void init(); void initShema(); void upgradeSchema(); diff --git a/rocketchat.cpp b/rocketchat.cpp index 07db4445c72a917a1af396ba57014c3f58127f2a..31a08fb67dc518ab3c10b818c01120fe0ae0a6c8 100755 --- a/rocketchat.cpp +++ b/rocketchat.cpp @@ -52,22 +52,18 @@ RocketChat::RocketChat( QGuiApplication *app ) #endif this->mApp = app; connect( app, &QGuiApplication::applicationStateChanged, this, &RocketChat::onApplicationStateChanged, Qt::UniqueConnection ); - this->mStorage = PersistanceLayer::instance(); #ifdef Q_OS_ANDROID mAndroidStatusBarColor = new AndroidStatusBarColor; #endif qRegisterMetaType<ConnectionState>( "ConnectionState" ); qRegisterMetaType<Qt::ApplicationState>( "Qt::ApplicationState" ); - } RocketChat::~RocketChat() { mServerThread.quit(); - mStorageThread.quit(); mServerThread.wait(); - mStorageThread.wait(); } @@ -99,10 +95,6 @@ void RocketChat::login( const QString &pServerId, const QString &pUsername, cons { Q_UNUSED( pServerId ); - if ( pUsername != mStorage->getUserName() ) { - mStorage->wipeDb(); - } - if ( mServerStatus ) { QMetaObject::invokeMethod( mServerMap.first(), "login", Q_ARG( QString, pUsername ), Q_ARG( QString, pPassword ) ); } @@ -443,30 +435,6 @@ QString RocketChat::getUsername( const QString &pServerId ) } } -//TODO: make async -QString RocketChat::getPassword( const QString &pServerId ) -{ - Q_UNUSED( pServerId ); - - if ( mServerStatus ) { - return mStorage->getPassword(); - } else { - return ""; - } -} - -void RocketChat::setUsername( const QString &pServerId, const QString &pUsername ) -{ - Q_UNUSED( pServerId ); - mStorage->setUserName( pUsername ); -} - -void RocketChat::setPassword( const QString &pServerId, const QString &pPassword ) -{ - Q_UNUSED( pServerId ); - mStorage->setPassword( pPassword ); -} - void RocketChat::setCurrentChannel( const QString &pServerId, const QString &pCurrentChannel, const QString &pChannelName ) { Q_UNUSED( pServerId ); @@ -477,19 +445,25 @@ void RocketChat::setCurrentChannel( const QString &pServerId, const QString &pCu } } -QString RocketChat::getNewVideoPath() +void RocketChat::getNewVideoPath() { - return mStorage->getNewVideoPath(); + if ( mServerStatus ) { + QMetaObject::invokeMethod( mServerMap.first(), "requestNewVideoPath" ); + } } void RocketChat::setSetting( const QString &pKey, const QString &pValue ) { - mStorage->setSetting( pKey, pValue ); + if ( mServerStatus ) { + QMetaObject::invokeMethod( mServerMap.first(), "setSetting", Q_ARG( QString, pKey ), Q_ARG( QString, pValue ) ); + } } -QString RocketChat::getSetting( const QString &pKey ) +void RocketChat::getSetting( const QString &pKey ) { - return mStorage->getSetting( pKey ); + if ( mServerStatus ) { + QMetaObject::invokeMethod( mServerMap.first(), "getSetting", Q_ARG( QString, pKey ) ); + } } void RocketChat::callGalleryPicker( const QString &pChannelId ) @@ -518,14 +492,6 @@ void RocketChat::markChannelAsRead( const QString &pChannelId ) } } -QString RocketChat::getCurrentChannel() -{ - std::tuple<QString, QString> tuple = mStorage->getCurrentChannel(); - QJsonObject data = {{QStringLiteral( "id" ), std::get<0>( tuple )}, {QStringLiteral( "name" ), std::get<1>( tuple )}}; - QJsonDocument doc( data ); - return doc.toJson(); -} - void RocketChat::cancelUpload( const QString &pServerId, const QString &pFileId ) { Q_UNUSED( pServerId ); @@ -565,9 +531,6 @@ void RocketChat::createPrivateGroup( const QString &pServerId, const QString &pC void RocketChat::registerServer( RocketChatServerData *pServer ) { - qDebug() << "serverId:" << pServer->getServerId(); - qDebug() << "move to thread"; - pServer->moveToThread( &mServerThread ); qDebug() << "add server to map"; mServerMap[pServer->getServerId()] = pServer; @@ -703,6 +666,10 @@ void RocketChat::serverReadySlot() connect( pServer, &RocketChatServerData::userStatusChanged, this, &RocketChat::userStatusChanged, Qt::UniqueConnection ); + connect( pServer, &RocketChatServerData::videoPath, this, &RocketChat::videoPath, Qt::UniqueConnection ); + + connect( pServer, &RocketChatServerData::setting, this, &RocketChat::setting, Qt::UniqueConnection ); + QMetaObject::invokeMethod( pServer, "init" ); } diff --git a/rocketchat.h b/rocketchat.h index 519caf7a1c8983989d135d72728cedca8d3d9456..5ce07bea9ab7480d83b72a98037677e1acef27dc 100755 --- a/rocketchat.h +++ b/rocketchat.h @@ -56,7 +56,6 @@ class RocketChat : public QObject Q_INVOKABLE void getFileRessource( const QString &pUrl ); Q_INVOKABLE void getFileRessource( const QString &pUrl, const QString &pType ); - Q_INVOKABLE QString getCurrentChannel( void ); Q_INVOKABLE void cancelUpload( const QString &pServerId, const QString &pFileId ); Q_INVOKABLE void joinChannel( const QString &pServer, const QString &pChannelId ); @@ -85,14 +84,11 @@ class RocketChat : public QObject const QString &pMessageId ); Q_INVOKABLE QString getUsername( const QString &pServerId ); - Q_INVOKABLE QString getPassword( const QString &pServerId ); - Q_INVOKABLE void setUsername( const QString &pServerId, const QString &pUsername ); - Q_INVOKABLE void setPassword( const QString &pServerId, const QString &pPassword ); Q_INVOKABLE void setCurrentChannel( const QString &pServerId, const QString &pCurrentChannel, const QString &pChannelName ); - Q_INVOKABLE QString getNewVideoPath(); + Q_INVOKABLE void getNewVideoPath(); Q_INVOKABLE void setSetting( const QString &pKey, const QString &pValue ); - Q_INVOKABLE QString getSetting( const QString &pKey ); + Q_INVOKABLE void getSetting( const QString &pKey ); Q_INVOKABLE void callGalleryPicker( const QString &pChannelId ); Q_INVOKABLE void createAccount( const QString &serverId, const QString &email, const QString &username, const QString &password ); @@ -156,9 +152,7 @@ class RocketChat : public QObject //TODO: switch from shared pointer to normal ones, to avoid these hacks QThread mServerThread; - QThread mStorageThread; QGuiApplication *mApp = nullptr; - PersistanceLayer *mStorage = nullptr; QString mCurrentChannel; QMap<QString, RocketChatServerData * > mServerMap; QNetworkConfigurationManager mNetworkConfiguration; @@ -246,7 +240,8 @@ class RocketChat : public QObject void pushMessageReceived( QString pServer, QString pRid, QString pName, QString pType ); void loggingIn(); void userStatusChanged( int status ); - + void videoPath( const QString &pPath ); + void setting( const QString &key, const QString &value ); // iOS helper void setBadge( uint pNumber ); diff --git a/rocketchatserver.cpp b/rocketchatserver.cpp index 023d0c8b67c91b0a4b9da4bf4e576972d25fa71b..d94faa59bce07c8d1a37ebd1ea536e41fed85327 100755 --- a/rocketchatserver.cpp +++ b/rocketchatserver.cpp @@ -39,104 +39,107 @@ RocketChatServerData::RocketChatServerData( const QString &pId, QString pBaseUrl mApiUri = QStringLiteral( "/api/v1" ); + setRestApi( new RestApi( this, protocol + mBaseUrl ) ); +} +void RocketChatServerData::init() +{ mStorage = PersistanceLayer::instance(); - mEmojiRepo = new EmojiRepo; - historyLoaded = [ = ]( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages ) { - if ( messages ) { - mStorage->transaction(); + connect( mStorage, &PersistanceLayer::ready, [ = ]() { + mEmojiRepo = new EmojiRepo; + historyLoaded = [ = ]( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages ) { + if ( messages ) { + mStorage->transaction(); - for ( const auto &key : messages->uniqueKeys() ) { - if ( Q_LIKELY( mChannels->contains( key ) && !mChannels->get( key ).isNull() ) ) { - MessageList messageList = messages->values( key ); - mChannels->get( key )->addMessages( messageList ); - mMessageService->persistMessages( messageList ); + for ( const auto &key : messages->uniqueKeys() ) { + if ( Q_LIKELY( mChannels->contains( key ) && !mChannels->get( key ).isNull() ) ) { + MessageList messageList = messages->values( key ); + mChannels->get( key )->addMessages( messageList ); + mMessageService->persistMessages( messageList ); + } } - } - mStorage->commit(); + mStorage->commit(); - if ( messages != nullptr ) { - delete messages; + if ( messages != nullptr ) { + delete messages; + } } - } - }; - mFileService = new FileService( this ); - mEmojiService = new EmojiService( this, mFileService, mStorage ); - setRestApi( new RestApi( this, protocol + mBaseUrl ) ); -} + }; + mFileService = new FileService( this ); + mEmojiService = new EmojiService( this, mFileService, mStorage ); -void RocketChatServerData::init() -{ - if ( mUnsecureConnection ) { - mDdpApi = new MeteorDDP( this, mBaseUrl, true ); + if ( mUnsecureConnection ) { + mDdpApi = new MeteorDDP( this, mBaseUrl, true ); - } else { - mDdpApi = new MeteorDDP( this, mBaseUrl ); - } + } else { + mDdpApi = new MeteorDDP( this, mBaseUrl ); + } - mRestApi->moveToThread( QThread::currentThread() ); - mDdpApi->moveToThread( QThread::currentThread() ); - mUsername = mStorage->getUserName(); + mRestApi->moveToThread( QThread::currentThread() ); + mDdpApi->moveToThread( QThread::currentThread() ); + mUsername = mStorage->getUserName(); - mChannels = new ChannelRepository( ); + mChannels = new ChannelRepository( ); - RocketChatServerConfig::init(); + RocketChatServerConfig::init(); - QMetaObject::invokeMethod( mRestApi, "init" ); + QMetaObject::invokeMethod( mRestApi, "init" ); - mDdpApi->registerMessageHandler( this ); - connect( mDdpApi, &MeteorDDP::ddpConnected, this, &RocketChatServerData::onDDPConnected, Qt::UniqueConnection ); - connect( mDdpApi, &MeteorDDP::ddpDisconnected, this, &RocketChatServerData::onDDPDisonnected, Qt::UniqueConnection ); + mDdpApi->registerMessageHandler( this ); + connect( mDdpApi, &MeteorDDP::ddpConnected, this, &RocketChatServerData::onDDPConnected, Qt::UniqueConnection ); + connect( mDdpApi, &MeteorDDP::ddpDisconnected, this, &RocketChatServerData::onDDPDisonnected, Qt::UniqueConnection ); - QPair<QString, uint> tokenDb = mStorage->getToken(); - QString token = tokenDb.first; - QString userDb = mStorage->getUserName(); + QPair<QString, uint> tokenDb = mStorage->getToken(); + QString token = tokenDb.first; + QString userDb = mStorage->getUserName(); - if ( !token.isEmpty() && !userDb.isEmpty() ) { - emit offlineMode(); - } + if ( !token.isEmpty() && !userDb.isEmpty() ) { + emit offlineMode(); + } - setUserId( mStorage->getUserId() ); - mUsername = mStorage->getUserName(); + setUserId( mStorage->getUserId() ); + mUsername = mStorage->getUserName(); - // mEmojisMap.reserve(3000); - mDownloadsInProgress.reserve( 100 ); + // mEmojisMap.reserve(3000); + mDownloadsInProgress.reserve( 100 ); - loadEmojis(); - //mMessageService = new MessageService( mStorage, this, mEmojisMap ); - mMessageService = new MessageService( mStorage, this, mEmojiRepo ); + loadEmojis(); + //mMessageService = new MessageService( mStorage, this, mEmojisMap ); + mMessageService = new MessageService( mStorage, this, mEmojiRepo ); - mChannelService = new RocketChatChannelService( this, mMessageService ); - mChannelService->setDdp( mDdpApi ); - mChannelService->setChannels( mChannels ); - connect( mChannelService, &RocketChatChannelService::channelsLoaded, this, &RocketChatServerData::onChannelsLoaded, Qt::UniqueConnection ); - connect( mChannelService, &RocketChatChannelService::usersLoaded, this, &RocketChatServerData::onUsersLoaded, Qt::UniqueConnection ); - connect( mChannelService, &RocketChatChannelService::directChannelReady, this, &RocketChatServerData::switchChannelByName, Qt::UniqueConnection ); - connect( channelsModel, &ChannelModel::unreadMessagesChanged, this, &RocketChatServerData::onUnreadCountChanged, Qt::UniqueConnection ); - connect( directModel, &ChannelModel::unreadMessagesChanged, this, &RocketChatServerData::onUnreadCountChanged, Qt::UniqueConnection ); - connect( groupsModel, &ChannelModel::unreadMessagesChanged, this, &RocketChatServerData::onUnreadCountChanged, Qt::UniqueConnection ); + mChannelService = new RocketChatChannelService( this, mMessageService ); + mChannelService->setDdp( mDdpApi ); + mChannelService->setChannels( mChannels ); + connect( mChannelService, &RocketChatChannelService::channelsLoaded, this, &RocketChatServerData::onChannelsLoaded, Qt::UniqueConnection ); + connect( mChannelService, &RocketChatChannelService::usersLoaded, this, &RocketChatServerData::onUsersLoaded, Qt::UniqueConnection ); + connect( mChannelService, &RocketChatChannelService::directChannelReady, this, &RocketChatServerData::switchChannelByName, Qt::UniqueConnection ); + connect( channelsModel, &ChannelModel::unreadMessagesChanged, this, &RocketChatServerData::onUnreadCountChanged, Qt::UniqueConnection ); + connect( directModel, &ChannelModel::unreadMessagesChanged, this, &RocketChatServerData::onUnreadCountChanged, Qt::UniqueConnection ); + connect( groupsModel, &ChannelModel::unreadMessagesChanged, this, &RocketChatServerData::onUnreadCountChanged, Qt::UniqueConnection ); - QString lastServer = mStorage->getSetting( QStringLiteral( "currentServer" ) ); + QString lastServer = mStorage->getSetting( QStringLiteral( "currentServer" ) ); - if ( lastServer == mBaseUrl ) { - QPair<QString, uint> tokenDb = mStorage->getToken(); - QString userDb = mStorage->getUserName(); - QDateTime currentTime = QDateTime::currentDateTime(); + if ( lastServer == mBaseUrl ) { + QPair<QString, uint> tokenDb = mStorage->getToken(); + QString userDb = mStorage->getUserName(); + QDateTime currentTime = QDateTime::currentDateTime(); - if ( !tokenDb.first.isEmpty() && tokenDb.second > currentTime.toTime_t() ) { - loginWithToken( userDb, tokenDb.first ); - } else { + if ( !tokenDb.first.isEmpty() && tokenDb.second > currentTime.toTime_t() ) { + loginWithToken( userDb, tokenDb.first ); + } else { + mStorage->wipeDb(); + } + } else if ( !lastServer.isEmpty() ) { mStorage->wipeDb(); } - } else { - mStorage->wipeDb(); - } - emit readyToCheckForPendingNotification(); - mChannelService->loadJoinedChannelsFromDb(); + emit readyToCheckForPendingNotification(); + mChannelService->loadJoinedChannelsFromDb(); + } ); + mStorage->init(); } @@ -593,6 +596,11 @@ void RocketChatServerData::offlineLogin() } } +RocketChatServerData::~RocketChatServerData() +{ + mStorage->close(); +} + QSharedPointer<RocketChatUser> RocketChatServerData::getOwnUser() const { return mOwnUser; @@ -616,6 +624,10 @@ QString RocketChatServerData::getBaseUrl() const void RocketChatServerData::login( const QString &pUsername, const QString &pPassword ) { + if ( pUsername != mStorage->getUserName() ) { + mStorage->wipeDb(); + } + QPair<QString, uint> tokenDb = mStorage->getToken(); QString userIdDb = mStorage->getUserId(); QString userDb = mStorage->getUserName(); @@ -1879,6 +1891,22 @@ void RocketChatServerData::searchRoom( const QString &pTerm, const QString &pTyp mChannelService->searchRoom( pTerm, pType ); } +void RocketChatServerData::requestNewVideoPath() +{ + emit videoPath( mStorage->getNewVideoPath() ); +} + +void RocketChatServerData::setSetting( const QString &pKey, const QString &pValue ) +{ + mStorage->setSetting( pKey, pValue ); +} + +void RocketChatServerData::getSetting( const QString &pKey ) +{ + auto value = mStorage->getSetting( pKey ); + emit setting( pKey, value ); +} + RestApi *RocketChatServerData::getRestApi() const { return mRestApi; diff --git a/rocketchatserver.h b/rocketchatserver.h index 806c77de5b8b4d04475665be867194ec3ec90a90..35ac30e19936f1c687a62690fa609b9f1fccfea6 100755 --- a/rocketchatserver.h +++ b/rocketchatserver.h @@ -209,6 +209,11 @@ class RocketChatServerData : public MessageListener void searchRoom( const QString &pTerm, const QString &pType ); + void requestNewVideoPath(); + + void setSetting( const QString &pKey, const QString &pValue ); + + void getSetting( const QString &pKey ); public: QString getServerId() const; @@ -246,6 +251,7 @@ class RocketChatServerData : public MessageListener protected: RocketChatServerData(); + ~RocketChatServerData(); ConnectionState mConnectionState = ConnectionState::OFFLINE; QString mUsername = ""; QString mUserId = ""; @@ -341,6 +347,8 @@ class RocketChatServerData : public MessageListener void offlineMode( void ); void loggingIn(); void userStatusChanged( int status ); + void videoPath( const QString &path ); + void setting( const QString &pKey, const QString &pValue ); // MessageListener interface public: