2022-12-29 14:50:17 +03:00
/*
* Copyright ( C ) by Oleksandr Zolotov < alex @ nextcloud . com >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful , but
* WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
* for more details .
*/
# include "accountsetupcommandlinemanager.h"
2022-12-29 20:28:28 +03:00
# include "accountsetupfromcommandlinejob.h"
2022-12-29 14:50:17 +03:00
namespace OCC
{
Q_LOGGING_CATEGORY ( lcAccountSetupCommandLineManager , " nextcloud.gui.accountsetupcommandlinemanager " , QtInfoMsg )
2022-12-30 16:45:47 +03:00
AccountSetupCommandLineManager * AccountSetupCommandLineManager : : _instance = nullptr ;
AccountSetupCommandLineManager : : AccountSetupCommandLineManager ( QObject * parent )
: QObject { parent }
{
}
AccountSetupCommandLineManager * AccountSetupCommandLineManager : : instance ( )
{
if ( ! _instance ) {
_instance = new AccountSetupCommandLineManager ( ) ;
}
return _instance ;
}
void AccountSetupCommandLineManager : : destroy ( )
{
if ( _instance ) {
_instance - > deleteLater ( ) ;
_instance = nullptr ;
}
}
2022-12-29 14:50:17 +03:00
bool AccountSetupCommandLineManager : : parseCommandlineOption ( const QString & option , QStringListIterator & optionsIterator , QString & errorMessage )
{
if ( option = = QStringLiteral ( " --apppassword " ) ) {
if ( optionsIterator . hasNext ( ) & & ! optionsIterator . peekNext ( ) . startsWith ( QLatin1String ( " -- " ) ) ) {
_appPassword = optionsIterator . next ( ) ;
return true ;
} else {
errorMessage = QStringLiteral ( " apppassword not specified " ) ;
}
} else if ( option = = QStringLiteral ( " --localdirpath " ) ) {
if ( optionsIterator . hasNext ( ) & & ! optionsIterator . peekNext ( ) . startsWith ( QLatin1String ( " -- " ) ) ) {
_localDirPath = optionsIterator . next ( ) ;
return true ;
} else {
errorMessage = QStringLiteral ( " basedir not specified " ) ;
}
} else if ( option = = QStringLiteral ( " --remotedirpath " ) ) {
if ( optionsIterator . hasNext ( ) & & ! optionsIterator . peekNext ( ) . startsWith ( QLatin1String ( " -- " ) ) ) {
_remoteDirPath = optionsIterator . next ( ) ;
return true ;
} else {
errorMessage = QStringLiteral ( " remotedir not specified " ) ;
}
} else if ( option = = QStringLiteral ( " --serverurl " ) ) {
if ( optionsIterator . hasNext ( ) & & ! optionsIterator . peekNext ( ) . startsWith ( QLatin1String ( " -- " ) ) ) {
_serverUrl = optionsIterator . next ( ) ;
return true ;
} else {
errorMessage = QStringLiteral ( " serverurl not specified " ) ;
}
} else if ( option = = QStringLiteral ( " --userid " ) ) {
if ( optionsIterator . hasNext ( ) & & ! optionsIterator . peekNext ( ) . startsWith ( QLatin1String ( " -- " ) ) ) {
_userId = optionsIterator . next ( ) ;
return true ;
} else {
errorMessage = QStringLiteral ( " userid not specified " ) ;
}
2022-12-29 20:28:28 +03:00
} else if ( option = = QLatin1String ( " --isvfsenabled " ) ) {
2022-12-29 14:50:17 +03:00
if ( optionsIterator . hasNext ( ) & & ! optionsIterator . peekNext ( ) . startsWith ( QLatin1String ( " -- " ) ) ) {
2022-12-29 20:28:28 +03:00
_isVfsEnabled = optionsIterator . next ( ) . toInt ( ) ! = 0 ;
2022-12-29 14:50:17 +03:00
return true ;
} else {
2022-12-29 20:28:28 +03:00
errorMessage = QStringLiteral ( " isvfsenabled not specified " ) ;
2022-12-29 14:50:17 +03:00
}
}
return false ;
}
2022-12-30 16:45:47 +03:00
bool AccountSetupCommandLineManager : : isCommandLineParsed ( ) const
2022-12-29 14:50:17 +03:00
{
return ! _appPassword . isEmpty ( ) & & ! _userId . isEmpty ( ) & & _serverUrl . isValid ( ) ;
}
2022-12-30 16:45:47 +03:00
void AccountSetupCommandLineManager : : setupAccountFromCommandLine ( )
2022-12-29 14:50:17 +03:00
{
if ( isCommandLineParsed ( ) ) {
qCInfo ( lcAccountSetupCommandLineManager ) < < QStringLiteral ( " Command line has been parsed and account setup parameters have been found. Attempting setup a new account %1... " ) . arg ( _userId ) ;
2022-12-30 16:45:47 +03:00
const auto accountSetupJob = new AccountSetupFromCommandLineJob ( _appPassword , _userId , _serverUrl , _localDirPath , _isVfsEnabled , _remoteDirPath , parent ( ) ) ;
2022-12-29 14:50:17 +03:00
accountSetupJob - > handleAccountSetupFromCommandLine ( ) ;
} else {
qCInfo ( lcAccountSetupCommandLineManager ) < < QStringLiteral ( " No account setup parameters have been found, or they are invalid. Proceed with normal startup... " ) ;
}
_appPassword . clear ( ) ;
_userId . clear ( ) ;
_serverUrl . clear ( ) ;
_remoteDirPath . clear ( ) ;
_localDirPath . clear ( ) ;
2022-12-29 20:28:28 +03:00
_isVfsEnabled = true ;
2022-12-29 14:50:17 +03:00
}
}