|
| 1 | +/* |
| 2 | + * Copyright (C) by Oleksandr Zolotov <alex@nextcloud.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but |
| 10 | + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 11 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * for more details. |
| 13 | + */ |
| 14 | + |
| 15 | +#include "accountsetupcommandlinemanager.h" |
| 16 | +#include "accountsetupfromcommandlinejob.h" |
| 17 | + |
| 18 | +namespace OCC |
| 19 | +{ |
| 20 | +Q_LOGGING_CATEGORY(lcAccountSetupCommandLineManager, "nextcloud.gui.accountsetupcommandlinemanager", QtInfoMsg) |
| 21 | + |
| 22 | +AccountSetupCommandLineManager *AccountSetupCommandLineManager::_instance = nullptr; |
| 23 | + |
| 24 | +AccountSetupCommandLineManager::AccountSetupCommandLineManager(QObject *parent) |
| 25 | + : QObject{parent} |
| 26 | +{ |
| 27 | +} |
| 28 | + |
| 29 | +AccountSetupCommandLineManager *AccountSetupCommandLineManager::instance() |
| 30 | +{ |
| 31 | + if (!_instance) { |
| 32 | + _instance = new AccountSetupCommandLineManager(); |
| 33 | + } |
| 34 | + return _instance; |
| 35 | +} |
| 36 | + |
| 37 | +void AccountSetupCommandLineManager::destroy() |
| 38 | +{ |
| 39 | + if (_instance) { |
| 40 | + _instance->deleteLater(); |
| 41 | + _instance = nullptr; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +bool AccountSetupCommandLineManager::parseCommandlineOption(const QString &option, QStringListIterator &optionsIterator, QString &errorMessage) |
| 46 | +{ |
| 47 | + if (option == QStringLiteral("--apppassword")) { |
| 48 | + if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) { |
| 49 | + _appPassword = optionsIterator.next(); |
| 50 | + return true; |
| 51 | + } else { |
| 52 | + errorMessage = QStringLiteral("apppassword not specified"); |
| 53 | + } |
| 54 | + } else if (option == QStringLiteral("--localdirpath")) { |
| 55 | + if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) { |
| 56 | + _localDirPath = optionsIterator.next(); |
| 57 | + return true; |
| 58 | + } else { |
| 59 | + errorMessage = QStringLiteral("basedir not specified"); |
| 60 | + } |
| 61 | + } else if (option == QStringLiteral("--remotedirpath")) { |
| 62 | + if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) { |
| 63 | + _remoteDirPath = optionsIterator.next(); |
| 64 | + return true; |
| 65 | + } else { |
| 66 | + errorMessage = QStringLiteral("remotedir not specified"); |
| 67 | + } |
| 68 | + } else if (option == QStringLiteral("--serverurl")) { |
| 69 | + if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) { |
| 70 | + _serverUrl = optionsIterator.next(); |
| 71 | + return true; |
| 72 | + } else { |
| 73 | + errorMessage = QStringLiteral("serverurl not specified"); |
| 74 | + } |
| 75 | + } else if (option == QStringLiteral("--userid")) { |
| 76 | + if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) { |
| 77 | + _userId = optionsIterator.next(); |
| 78 | + return true; |
| 79 | + } else { |
| 80 | + errorMessage = QStringLiteral("userid not specified"); |
| 81 | + } |
| 82 | + } else if (option == QLatin1String("--isvfsenabled")) { |
| 83 | + if (optionsIterator.hasNext() && !optionsIterator.peekNext().startsWith(QLatin1String("--"))) { |
| 84 | + _isVfsEnabled = optionsIterator.next().toInt() != 0; |
| 85 | + return true; |
| 86 | + } else { |
| 87 | + errorMessage = QStringLiteral("isvfsenabled not specified"); |
| 88 | + } |
| 89 | + } |
| 90 | + return false; |
| 91 | +} |
| 92 | + |
| 93 | +bool AccountSetupCommandLineManager::isCommandLineParsed() const |
| 94 | +{ |
| 95 | + return !_appPassword.isEmpty() && !_userId.isEmpty() && _serverUrl.isValid(); |
| 96 | +} |
| 97 | + |
| 98 | +void AccountSetupCommandLineManager::setupAccountFromCommandLine() |
| 99 | +{ |
| 100 | + if (isCommandLineParsed()) { |
| 101 | + qCInfo(lcAccountSetupCommandLineManager) << QStringLiteral("Command line has been parsed and account setup parameters have been found. Attempting setup a new account %1...").arg(_userId); |
| 102 | + const auto accountSetupJob = new AccountSetupFromCommandLineJob(_appPassword, _userId, _serverUrl, _localDirPath, _isVfsEnabled, _remoteDirPath, parent()); |
| 103 | + accountSetupJob->handleAccountSetupFromCommandLine(); |
| 104 | + } else { |
| 105 | + qCInfo(lcAccountSetupCommandLineManager) << QStringLiteral("No account setup parameters have been found, or they are invalid. Proceed with normal startup..."); |
| 106 | + } |
| 107 | + _appPassword.clear(); |
| 108 | + _userId.clear(); |
| 109 | + _serverUrl.clear(); |
| 110 | + _remoteDirPath.clear(); |
| 111 | + _localDirPath.clear(); |
| 112 | + _isVfsEnabled = true; |
| 113 | +} |
| 114 | +} |
0 commit comments