Index: sources/cloudsync/CloudSyncController.cpp =================================================================== diff -u -r73d619581633ed6053b61837f1f42ccbe22059f1 -r6c6f1f5d466badd9b4fd67be7c907234c342b2a2 --- sources/cloudsync/CloudSyncController.cpp (.../CloudSyncController.cpp) (revision 73d619581633ed6053b61837f1f42ccbe22059f1) +++ sources/cloudsync/CloudSyncController.cpp (.../CloudSyncController.cpp) (revision 6c6f1f5d466badd9b4fd67be7c907234c342b2a2) @@ -7,7 +7,7 @@ * * \file CloudSyncController.cpp * \author (last) Behrouz NematiPour - * \date (last) 20-Dec-2022 + * \date (last) 07-Apr-2023 * \author (original) Behrouz NematiPour * \date (original) 14-Oct-2021 * @@ -81,6 +81,15 @@ // disabled coco end /*! + * \brief CloudSyncController::doRegister + * \details Send the register device command. + */ +void CloudSyncController::doRegister() +{ + sendDeviceRegister(); +} + +/*! * \brief CloudSyncController::initConnections * \details Initializes the required signal/slot connection between this class and other objects * to be able to communicate. @@ -91,6 +100,12 @@ SINGLETON_DISABLE_CONNECT(didPOSTCloudSync) } + connect(&_ApplicationController , SIGNAL(didPOSTCloudSync(bool)), + this , SLOT( onPOSTCloudSync(bool))); + + connect(&_DeviceController , SIGNAL(didCryptSetupMount(bool)), + this , SLOT( onCryptSetupMount(bool))); + connect(&_DeviceController , SIGNAL(didWatchFileChange (const QString &)), this , SLOT( onWatchFileChange (const QString &))); connect(&_MessageDispatcher , SIGNAL(didActionReceive (GuiActionType , const QVariantList &)), @@ -165,14 +180,36 @@ } /*! - * \brief CloudSyncController::onInitComplete - * \details The slot to be called when the CloudSync initialization is complete, to call the testDeviceRegister. + * \brief CloudSyncController::onInitComplete + * \details The slot to be called when the CloudSync initialization is complete + * Does nothing for now */ void CloudSyncController::onInitComplete() { - testDeviceRegister(); + // Nothing for now } /*! + * \brief CloudSyncController::onPOSTCloudSync + * \details This the handler for the ApplicationController::didPOSTCloudSync(bool) + * \param vPass - will be true if the POST test of CloudSync passed and it is running. + */ +void CloudSyncController::onPOSTCloudSync(bool vPass) +{ + _postPass = vPass; +} + +/*! + * \brief CloudSyncController::onCryptSetupMount + * \details This the handler for the DeviceController::onCryptSetupMount() + */ +void CloudSyncController::onCryptSetupMount(bool vPass) +{ + if ( vPass ) { + testReady(); + } +} + +/*! * \brief CloudSyncController::onWatchFileChange * \details This slot will be called when the Device Controller identifies any changes in the watched files. * \param vFile - watched file @@ -323,9 +360,8 @@ case eError_LogFolder : text = tr( "CS The log folder cannot be created." ) ; break; case eError_LogFileInp : text = tr( "CS Error writing to the input file." ) ; break; case eError_CredentialFile : text = tr( "CS The credentials file does not exist." ) ; break; - case eError_CredentialMake : text = tr( "CS The credentials folder make failed." ) ; break; - case eError_CredentialCopy : text = tr( "CS The credentials file copy failed." ) ; break; - case eError_CredentialRemove: text = tr( "CS The credentials file remove failed." ) ; break; + case eError_CredentialPath : text = tr( "CS The credentials folder is incorrect." ) ; break; + case eError_CredentialCount : text = tr( "CS No credential file name provided." ) ; break; case eError_CredentialEmpty : text = tr( "CS The credentials folder is empty." ) ; break; case eError_TxCodeNoParam : text = tr( "CS No Treatment Code provided." ) ; break; case eError_TxCodeEmpty : text = tr( "CS The provided Treatment Code is empty." ) ; break; @@ -368,9 +404,8 @@ case eError_LogFolder : ; break; case eError_LogFileInp : ; break; case eError_CredentialFile : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; - case eError_CredentialMake : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; - case eError_CredentialCopy : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; - case eError_CredentialRemove: info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; + case eError_CredentialPath : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; + case eError_CredentialCount : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; case eError_CredentialEmpty : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; case eError_TxCodeNoParam : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; case eError_TxCodeEmpty : info = QString( "[%1:%2]" ).arg( vErrorID ).arg( item(0) ) ; break; @@ -465,7 +500,7 @@ int count = vPrm.count(); QStringList prm = count ? vPrm : _uiHistory[ vMessageID ]; QString msg = QString::number(vMessageID); - QString len = QString::number(count ); + QString len = QString::number(prm.count()); QStringList dataList; dataList += msg; @@ -504,11 +539,17 @@ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // writing the message into the buffer. LOG_APPED_UI( vInpBuff ); + if(!QFile::exists(_date_inp_File)){ + Storage::FileHandler::makeFolder(_location); + } if ( ! Storage::FileHandler::write(_date_inp_File, vInpBuff + "\n") ) { error = eError_LogFileInp; args = { _date_inp_File }; ok = false; goto lErr; } return ok; lErr: - toLog(error, args); + // don't send the error back to the CS + // it calls this same function if the error is in this function + // and creates a loop + LOG_DEBUG(toText(error) + " " + toInfo(error, args)); return ok; } @@ -638,10 +679,17 @@ void CloudSyncController::onTxPending(const QString &vFileName) { - // if (! isRegistered() ) return; + bool ok = true; Q_UNUSED(ok); + QVariantList args ; + Errors_Enum error = eError_Unknown; qint32 messageID = UI2CS(static_cast( eMessageID_TxReport )); - sendCredentialsSend(); // TODO: This has to be removed later, it is a workaround for Cloud issue, during our talk with KBM. + + if ( ! isRegistered() ) { error = eError_NotRegistered ; args = { vFileName }; ok = false; goto lErr; } sendUIBuff(makeUIBuff( messageID , { vFileName } )); + + return; +lErr: + toLog(error, args); } /*! @@ -659,7 +707,6 @@ case eMessageID_DeviceInfo : ok = sendDeviceInfo ( ); break; case eMessageID_CredentialsSave : ok = sendCredentialsSave( vMessage ); break; - case eMessageID_CredentialsSend : ok = sendCredentialsSend( ); break; case eMessageID_ResetFactory : ok = sendResetFactory ( ); break; case eMessageID_DeviceState : ok = sendDeviceState ( ); break; @@ -693,7 +740,6 @@ bool CloudSyncController::sendDeviceState() { bool ok = false; - sendCredentialsSend(); // TODO: This has to be removed later, it is a workaround for Cloud issue, during our talk with KBM. ok = sendUIHistory(eMessageID_DeviceState); return ok; } @@ -731,63 +777,32 @@ /*! * \brief CloudSyncController::sendCredentialsSave - * \details saves the credentials files which their location have been sent. + * \details validates the credentials files which their sent location. * \param vMessage - the message received from CloudSync - * \return true on successfully saving the files. + * \return true on successfully validate the files. */ bool CloudSyncController::sendCredentialsSave( const Message &vMessage) { bool ok = true; QString destination = QString(Storage::CloudSync_Base_Path_Name) + Storage::CloudSync_Credentials_Folder_Name; - // create the destination folder if does not exist. - if ( ! Storage::FileHandler::makeFolder( destination ) ) { toLog(eError_CredentialMake , { }); ok = false; goto lOut; } - // file existence has been separated from copy remove to avoid partial copy. + if ( ! vMessage.params.count() ) { toLog(eError_CredentialCount , { }); ok = false; goto lOut; } for ( auto fileName : vMessage.params ) { - if ( ! QFileInfo::exists(fileName) ) { toLog(eError_CredentialFile , {fileName }); ok = false; goto lOut; } + QFileInfo fileinfo(fileName); + if ( ! fileinfo.exists() ) { toLog(eError_CredentialFile , {fileName }); ok = false; goto lOut; } + if ( fileinfo.absolutePath() != destination ) { toLog(eError_CredentialPath , {fileName }); ok = false; goto lOut; } } + // no need to call for isRegistered() function, we are testing for the count, location, and existence. - if ( ok ) { - for ( auto sourceFile : vMessage.params ) { - QString fileName = QFileInfo(sourceFile).fileName(); - // NOTE: If a file with the name newName already exists, copy() returns false (i.e., QFile will not overwrite it). - // For the current scenario it is ideal, since that folder will turn RO after the successful registration and copying the files and reboot. - if ( ! QFile::copy (sourceFile, destination + fileName) ) { toLog(eError_CredentialCopy , {fileName }); ok = false; goto lOut; } - } - // if all the copies are successful then remove them. - for ( auto sourceFile : vMessage.params ) { - QString fileName = QFileInfo(sourceFile).fileName(); - if ( ! QFile::remove(sourceFile ) ) { toLog(eError_CredentialRemove , {fileName }); ok = false; goto lOut; } - } - } - lOut: if ( ok ) sendCredentialsResponse(); + emit didRegisterDone(ok); + testReady(); return ok; } /*! - * \brief CloudSyncController::sendSaveCredentials - * \details saves the credentials files which their location have been sent. - * \param vMessage - the message received from CloudSync - * \return true on successfully saving the files. - */ -bool CloudSyncController::sendCredentialsSend() -{ - bool ok = true; - qint32 messageID = UI2CS(eMessageID_CredentialsSend); - QString source = QString(Storage::CloudSync_Base_Path_Name) + Storage::CloudSync_Credentials_Folder_Name; - QString destination = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/" + Storage::CloudSync_Credentials_Folder_Name; - - if ( Storage::FileHandler::copyFolder(source, destination) != 0 ) { toLog(eError_CredentialCopy , {destination }); ok = false; goto lOut; } - ok = sendUIBuff({QString("%1").arg( messageID ), "1", destination}); - -lOut: - return ok; -} - -/*! * \brief CloudSyncController::sendCredentialsResponse * \return send the confirmation of the credential save on the device from UI to CS to let CS know when to delete them. */ @@ -818,7 +833,7 @@ if ( mTxCode.isEmpty() ) { toLog(eError_TxCodeEmpty , {}); ok = false; goto lOut; } emit didTxCodeReceive ( mTxCode ); - qDebug() << " ---------- " << mTxCode; + //DEBUG qDebug() << " ---------- " << mTxCode; lOut: return ok; @@ -849,21 +864,6 @@ } /*! - * \brief CloudSyncController::testDeviceRegister - * \details checks if the device needs registration. - * \return true if registration required. - */ -bool CloudSyncController::testDeviceRegister() -{ - toLog(eError_CredentialEmpty,{}); // It is intentional that the vault folder path has not been sent to the log. - if( ! isRegistered() ) { - sendDeviceRegister(); - } - - return true; // for now always true. -} - -/*! * \brief CloudSyncController::isRegistered * \details checks if the device is registered by looking for existing credentials. * \return true if device has been registered. @@ -876,6 +876,16 @@ } /*! + * \brief CloudSyncController::testReady + * \details Checks if the CloudSync POST was passed and the device registration is complete. + */ +void CloudSyncController::testReady() +{ + bool ok = _postPass && isRegistered(); + emit didCloudSyncStatus( ok ); +} + +/*! * \brief CloudSyncController::saveDeviceInfo * \details keeps the received device information and set a flag to wait for the next message. * \param vAction - the action enum which identifies information source of HD or DG. @@ -926,7 +936,6 @@ saveUIHistory(eMessageID_DeviceInfo , { _deviceInfoHD, _deviceInfoDG, _deviceInfoUI } ); saveUIHistory(eMessageID_DeviceRegister , { _deviceInfoHD, _deviceInfoDG, _deviceInfoUI } ); // the device registration request format is the same as Device info with different message id. - testDeviceRegister (); // it is expected on the CloudSync App to get the sendDeviceInfo (); // this one may need to be removed and only will be sent on the request stopDeviceInfoWait();