diff options
18 files changed, 105 insertions, 151 deletions
diff --git a/src/libs/installer/consumeoutputoperation.cpp b/src/libs/installer/consumeoutputoperation.cpp index 00b396ad6..542626553 100644 --- a/src/libs/installer/consumeoutputoperation.cpp +++ b/src/libs/installer/consumeoutputoperation.cpp @@ -59,13 +59,10 @@ bool ConsumeOutputOperation::performOperation() // 2. executable path // 3. argument for the executable // 4. more arguments possible ... - if (arguments().count() < 3) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.").arg(name()).arg( - arguments().count()).arg(tr("at least 2"), QLatin1String("(<to be saved installer key name>, " - "<executable>, [argument1], [argument2], ...)"))); + + if (!checkArgumentCount(2, INT_MAX, tr("(<to be saved installer key name>, " + "<executable>, [argument1], [argument2], ...)"))) return false; - } PackageManagerCore *const core = value(QLatin1String("installer")).value<PackageManagerCore*>(); if (!core) { diff --git a/src/libs/installer/copydirectoryoperation.cpp b/src/libs/installer/copydirectoryoperation.cpp index cd2b1b341..d94a5849f 100644 --- a/src/libs/installer/copydirectoryoperation.cpp +++ b/src/libs/installer/copydirectoryoperation.cpp @@ -63,13 +63,10 @@ void CopyDirectoryOperation::backup() bool CopyDirectoryOperation::performOperation() { - const QStringList args = arguments(); - if (args.count() < 2 || args.count() > 3) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("2 or 3"), tr(" (<source> <target> [forceOverwrite])"))); + if (!checkArgumentCount(2, 3, tr(" (<source> <target> [forceOverwrite])"))) return false; - } + + const QStringList args = arguments(); const QString sourcePath = args.at(0); const QString targetPath = args.at(1); bool overwrite = false; diff --git a/src/libs/installer/createdesktopentryoperation.cpp b/src/libs/installer/createdesktopentryoperation.cpp index 11e977d08..8d71c2c07 100644 --- a/src/libs/installer/createdesktopentryoperation.cpp +++ b/src/libs/installer/createdesktopentryoperation.cpp @@ -127,16 +127,11 @@ void CreateDesktopEntryOperation::backup() bool CreateDesktopEntryOperation::performOperation() { - const QStringList args = arguments(); - if (args.count() != 2) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("exactly 2"), QLatin1String(""))); + if (!checkArgumentCount(2)) return false; - } const QString filename = absoluteFileName(); - const QString &values = args[1]; + const QString &values = arguments().at(1); QFile file(filename); if (file.exists() && !file.remove()) { diff --git a/src/libs/installer/createlinkoperation.cpp b/src/libs/installer/createlinkoperation.cpp index 6117ef602..fc0950870 100644 --- a/src/libs/installer/createlinkoperation.cpp +++ b/src/libs/installer/createlinkoperation.cpp @@ -50,15 +50,10 @@ void CreateLinkOperation::backup() bool CreateLinkOperation::performOperation() { - QStringList args = arguments(); - - if (args.count() != 2) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("exactly 2"), QLatin1String(""))); + if (!checkArgumentCount(2)) return false; - } + const QStringList args = arguments(); const QString& linkPath = args.at(0); const QString& targetPath = args.at(1); Link link = Link::create(linkPath, targetPath); diff --git a/src/libs/installer/createlocalrepositoryoperation.cpp b/src/libs/installer/createlocalrepositoryoperation.cpp index 55453a916..0904ab8dd 100644 --- a/src/libs/installer/createlocalrepositoryoperation.cpp +++ b/src/libs/installer/createlocalrepositoryoperation.cpp @@ -152,16 +152,12 @@ bool CreateLocalRepositoryOperation::performOperation() AutoHelper helper(this); emit progressChanged(0.0); - const QStringList args = arguments(); - - if (args.count() != 2) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("exactly 2"), QLatin1String(""))); + if (!checkArgumentCount(2)) return false; - } try { + const QStringList args = arguments(); + const QString binaryPath = QFileInfo(args.at(0)).absoluteFilePath(); // Note the "/" at the end, important to make copy directory operation behave well const QString repoPath = QFileInfo(args.at(1)).absoluteFilePath() + QLatin1Char('/'); diff --git a/src/libs/installer/createshortcutoperation.cpp b/src/libs/installer/createshortcutoperation.cpp index 060672bd8..2ee7dfbf4 100644 --- a/src/libs/installer/createshortcutoperation.cpp +++ b/src/libs/installer/createshortcutoperation.cpp @@ -179,23 +179,18 @@ void CreateShortcutOperation::backup() bool CreateShortcutOperation::performOperation() { + if (!checkArgumentCount(2, 3, tr("(optional: \"workingDirectory=...\", \"iconPath=...\", \"iconId=...\")"))) + return false; + QStringList args = arguments(); const QString iconId = takeArgument(QString::fromLatin1("iconId="), &args); const QString iconPath = takeArgument(QString::fromLatin1("iconPath="), &args); const QString workingDir = takeArgument(QString::fromLatin1("workingDirectory="), &args); - if (args.count() != 2 && args.count() != 3) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("2 or 3"), - tr(" (optional: 'workingDirectory=...', 'iconPath=...', 'iconId=...')"))); - return false; - } - const QString linkTarget = args.at(0); const QString linkLocation = args.at(1); - const QString targetArguments = args.value(2); //used value because it could be not existing + const QString targetArguments = args.value(2); // value() used since it's optional const QString linkPath = QFileInfo(linkLocation).absolutePath().trimmed(); const bool created = QDir(linkPath).exists() || QDir::root().mkpath(linkPath); diff --git a/src/libs/installer/elevatedexecuteoperation.cpp b/src/libs/installer/elevatedexecuteoperation.cpp index 3e766f801..1f439067c 100644 --- a/src/libs/installer/elevatedexecuteoperation.cpp +++ b/src/libs/installer/elevatedexecuteoperation.cpp @@ -80,12 +80,9 @@ bool ElevatedExecuteOperation::performOperation() { // This operation receives only one argument. It is the complete // command line of the external program to execute. - if (arguments().isEmpty()) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("at least 1"), QLatin1String(""))); + if (!checkArgumentCount(1, INT_MAX)) return false; - } + QStringList args; foreach (const QString &argument, arguments()) { if (argument!=QLatin1String("UNDOEXECUTE")) diff --git a/src/libs/installer/environmentvariablesoperation.cpp b/src/libs/installer/environmentvariablesoperation.cpp index f41b90893..55332f540 100644 --- a/src/libs/installer/environmentvariablesoperation.cpp +++ b/src/libs/installer/environmentvariablesoperation.cpp @@ -121,21 +121,16 @@ UpdateOperation::Error undoSetting(const QString ®Path, bool EnvironmentVariableOperation::performOperation() { - QStringList args = arguments(); - if (args.count() < 2 || args.count() > 4) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("2 to 4"), QLatin1String(""))); + if (!checkArgumentCount(2, 4)) return false; - } - const QString name = arguments().at(0); - const QString value = arguments().at(1); - bool isPersistent = false; + const QStringList args = arguments(); + const QString name = args.at(0); + const QString value = args.at(1); #ifdef Q_OS_WIN - isPersistent = arguments().count() >= 3 ? arguments().at(2) == QLatin1String("true") : true; - const bool isSystemWide = arguments().count() >= 4 ? arguments().at(3) == QLatin1String("true") : false; + const bool isPersistent = arguments().count() > 2 ? arguments().at(2) == QLatin1String("true") : true; + const bool isSystemWide = arguments().count() > 3 ? arguments().at(3) == QLatin1String("true") : false; QString oldvalue; if (isPersistent) { const QString regPath = isSystemWide ? QLatin1String("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet" @@ -158,9 +153,8 @@ bool EnvironmentVariableOperation::performOperation() setValue(QLatin1String("oldvalue"), oldvalue); return true; } -#endif Q_ASSERT(!isPersistent); - Q_UNUSED(isPersistent) +#endif setValue(QLatin1String("oldvalue"), Environment::instance().value(name)); Environment::instance().setTemporaryValue(name, value); diff --git a/src/libs/installer/extractarchiveoperation.cpp b/src/libs/installer/extractarchiveoperation.cpp index dba695593..d75913d7e 100644 --- a/src/libs/installer/extractarchiveoperation.cpp +++ b/src/libs/installer/extractarchiveoperation.cpp @@ -54,15 +54,11 @@ void ExtractArchiveOperation::backup() bool ExtractArchiveOperation::performOperation() { - const QStringList args = arguments(); - if (args.count() != 2) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("exactly 2"), QLatin1String(""))); + if (!checkArgumentCount(2)) return false; - } - const QString archivePath = args.first(); + const QStringList args = arguments(); + const QString archivePath = args.at(0); const QString targetDir = args.at(1); Receiver receiver; diff --git a/src/libs/installer/fakestopprocessforupdateoperation.cpp b/src/libs/installer/fakestopprocessforupdateoperation.cpp index d30aa370e..562816bfc 100644 --- a/src/libs/installer/fakestopprocessforupdateoperation.cpp +++ b/src/libs/installer/fakestopprocessforupdateoperation.cpp @@ -57,11 +57,8 @@ bool FakeStopProcessForUpdateOperation::performOperation() bool FakeStopProcessForUpdateOperation::undoOperation() { setError(KDUpdater::UpdateOperation::NoError); - if (arguments().size() != 1) { - setError(KDUpdater::UpdateOperation::InvalidArguments, tr("Number of arguments does not " - "match: one is required")); + if (!checkArgumentCount(1)) return false; - } PackageManagerCore *const core = value(QLatin1String("installer")).value<PackageManagerCore*>(); if (!core) { diff --git a/src/libs/installer/globalsettingsoperation.cpp b/src/libs/installer/globalsettingsoperation.cpp index 3623487a3..f8d5ee439 100644 --- a/src/libs/installer/globalsettingsoperation.cpp +++ b/src/libs/installer/globalsettingsoperation.cpp @@ -105,17 +105,13 @@ Operation *GlobalSettingsOperation::clone() const QSettingsWrapper *GlobalSettingsOperation::setup(QString *key, QString *value, const QStringList &arguments) { - if (arguments.count() != 3 && arguments.count() != 4 && arguments.count() != 5) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments.count()).arg(tr("3, 4 or 5"), QLatin1String(""))); + if (!checkArgumentCount(3, 5)) return 0; - } if (arguments.count() == 5) { QSettingsWrapper::Scope scope = QSettingsWrapper::UserScope; if (arguments.at(0) == QLatin1String("SystemScope")) - scope = QSettingsWrapper::SystemScope; + scope = QSettingsWrapper::SystemScope; const QString &company = arguments.at(1); const QString &application = arguments.at(2); *key = arguments.at(3); diff --git a/src/libs/installer/installiconsoperation.cpp b/src/libs/installer/installiconsoperation.cpp index f957623bb..7d5bf08ce 100644 --- a/src/libs/installer/installiconsoperation.cpp +++ b/src/libs/installer/installiconsoperation.cpp @@ -109,16 +109,12 @@ void InstallIconsOperation::backup() bool InstallIconsOperation::performOperation() { - const QStringList args = arguments(); - if ((args.count() != 1) && (args.count() != 2)) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("1 or 2"), tr(" (Sourcepath, [Vendorprefix])"))); + if (!checkArgumentCount(1, 2, tr("(Sourcepath, [Vendorprefix])"))) return false; - } + const QStringList args = arguments(); const QString source = args.at(0); - const QString vendor = args.value(1); + const QString vendor = args.value(1); // value() used since it's optional if (source.isEmpty()) { setError(InvalidArguments); diff --git a/src/libs/installer/linereplaceoperation.cpp b/src/libs/installer/linereplaceoperation.cpp index 9063f8848..c84d1922c 100644 --- a/src/libs/installer/linereplaceoperation.cpp +++ b/src/libs/installer/linereplaceoperation.cpp @@ -51,18 +51,14 @@ void LineReplaceOperation::backup() bool LineReplaceOperation::performOperation() { - const QStringList args = arguments(); - // Arguments: // 1. filename // 2. startsWith Search-String // 3. Replace-Line-String - if (args.count() != 3) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("exactly 3"), QLatin1String(""))); + if (!checkArgumentCount(3)) return false; - } + + const QStringList args = arguments(); const QString fileName = args.at(0); const QString searchString = args.at(1); const QString replaceString = args.at(2); diff --git a/src/libs/installer/replaceoperation.cpp b/src/libs/installer/replaceoperation.cpp index c2aec65a8..98a0e7bc9 100644 --- a/src/libs/installer/replaceoperation.cpp +++ b/src/libs/installer/replaceoperation.cpp @@ -51,18 +51,14 @@ void ReplaceOperation::backup() bool ReplaceOperation::performOperation() { - const QStringList args = arguments(); - // Arguments: // 1. filename // 2. Source-String // 3. Replace-String - if (args.count() != 3) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("exactly 3"), QLatin1String(""))); + if (!checkArgumentCount(3)) return false; - } + + const QStringList args = arguments(); const QString fileName = args.at(0); const QString before = args.at(1); const QString after = args.at(2); diff --git a/src/libs/installer/simplemovefileoperation.cpp b/src/libs/installer/simplemovefileoperation.cpp index 5edb392e1..54a5583ee 100644 --- a/src/libs/installer/simplemovefileoperation.cpp +++ b/src/libs/installer/simplemovefileoperation.cpp @@ -49,14 +49,10 @@ void SimpleMoveFileOperation::backup() bool SimpleMoveFileOperation::performOperation() { - const QStringList args = arguments(); - if (args.count() != 2) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("exactly 2"), QLatin1String(""))); + if (!checkArgumentCount(2)) return false; - } + const QStringList args = arguments(); const QString source = args.at(0); const QString target = args.at(1); diff --git a/src/libs/kdtools/kdupdaterupdateoperation.cpp b/src/libs/kdtools/kdupdaterupdateoperation.cpp index 8b6cbec64..05c0995ac 100644 --- a/src/libs/kdtools/kdupdaterupdateoperation.cpp +++ b/src/libs/kdtools/kdupdaterupdateoperation.cpp @@ -180,6 +180,42 @@ QStringList UpdateOperation::arguments() const return m_arguments; } +bool UpdateOperation::checkArgumentCount(int minArgCount, int maxArgCount, + const QString &argDescription) +{ + const int argCount = arguments().count(); + if (argCount < minArgCount || argCount > maxArgCount) { + setError(InvalidArguments); + QString countRange; + if (minArgCount == maxArgCount) + countRange = tr("exactly %1").arg(minArgCount); + else if (maxArgCount == INT_MAX) + countRange = tr("at least %1").arg(minArgCount); + else if (minArgCount == 0) + countRange = tr("not more than %1").arg(maxArgCount); + else if (minArgCount == maxArgCount - 1) + countRange = tr("%1 or %2").arg(minArgCount).arg(maxArgCount); + else + countRange = tr("%1 to %2").arg(minArgCount).arg(maxArgCount); + + if (argDescription.isEmpty()) + setErrorString(tr("Invalid arguments in %1: %n arguments given, " + "%2 arguments expected.", 0, argCount) + .arg(name(), countRange)); + else + setErrorString(tr("Invalid arguments in %1: %n arguments given, " + "%2 arguments expected in the form: %3.", 0, argCount) + .arg(name(), countRange, argDescription)); + return false; + } + return true; +} + +bool UpdateOperation::checkArgumentCount(int argCount) +{ + return checkArgumentCount(argCount, argCount); +} + struct StartsWith { StartsWith(const QString &searchTerm) diff --git a/src/libs/kdtools/kdupdaterupdateoperation.h b/src/libs/kdtools/kdupdaterupdateoperation.h index d841fb564..7707a2d62 100644 --- a/src/libs/kdtools/kdupdaterupdateoperation.h +++ b/src/libs/kdtools/kdupdaterupdateoperation.h @@ -90,6 +90,8 @@ protected: void setError(int error, const QString &errorString = QString()); void registerForDelayedDeletion(const QStringList &files); bool deleteFileNowOrLater(const QString &file, QString *errorString = 0); + bool checkArgumentCount(int minArgCount, int maxArgCount, const QString &argDescription = QString()); + bool checkArgumentCount(int argCount); private: QString m_name; diff --git a/src/libs/kdtools/kdupdaterupdateoperations.cpp b/src/libs/kdtools/kdupdaterupdateoperations.cpp index 31d9d1f44..90dbc7d60 100644 --- a/src/libs/kdtools/kdupdaterupdateoperations.cpp +++ b/src/libs/kdtools/kdupdaterupdateoperations.cpp @@ -146,11 +146,8 @@ bool CopyOperation::performOperation() { // We need two args to complete the copy operation. First arg provides the complete file name of source // Second arg provides the complete file name of dest - if (arguments().count() != 2) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments: %1 arguments given, 2 expected.").arg(arguments().count())); + if (!checkArgumentCount(2)) return false; - } QString source = sourcePath(); QString destination = destinationPath(); @@ -273,14 +270,11 @@ bool MoveOperation::performOperation() { // We need two args to complete the copy operation. // First arg provides the complete file name of // source, second arg provides the complete file name of dest - const QStringList args = this->arguments(); - if (args.count() != 2) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments: %1 arguments given, 2 expected.").arg(args.count())); + if (!checkArgumentCount(2)) return false; - } - const QString dest = args.last(); + const QStringList args = arguments(); + const QString dest = args.at(1); // If destination file exists, then we cannot use QFile::copy() because it does not overwrite an existing // file. So we remove the destination file. if (QFile::exists(dest)) { @@ -293,7 +287,7 @@ bool MoveOperation::performOperation() } // Copy source to destination. - QFile file(args.first()); + QFile file(args.at(0)); if (!file.copy(dest)) { setError(UserDefinedError); setErrorString(tr("Could not copy %1 to %2: %3").arg(file.fileName(), dest, file.errorString())); @@ -372,13 +366,10 @@ void DeleteOperation::backup() bool DeleteOperation::performOperation() { // Requires only one parameter. That is the name of the file to remove. - const QStringList args = this->arguments(); - if (args.count() != 1) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments: %1 arguments given, 1 expected.").arg(args.count())); + if (!checkArgumentCount(1)) return false; - } - return deleteFileNowOrLater(args.first()); + + return deleteFileNowOrLater(arguments().at(0)); } bool DeleteOperation::undoOperation() @@ -459,14 +450,10 @@ void MkdirOperation::backup() bool MkdirOperation::performOperation() { // Requires only one parameter. That is the path which should be created - QStringList args = this->arguments(); - if (args.count() != 1) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments: %1 arguments given, 1 expected.").arg(args.count())); + if (!checkArgumentCount(1)) return false; - } - const QString dirName = args.first(); + const QString dirName = arguments().at(0); const bool created = QDir::root().mkpath(dirName); if (!created) { setError(UserDefinedError); @@ -538,26 +525,23 @@ void RmdirOperation::backup() bool RmdirOperation::performOperation() { // Requires only one parameter. That is the name of the file to remove. - const QStringList args = this->arguments(); - if (args.count() != 1) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments: %1 arguments given, 1 expected.").arg(args.count())); + if (!checkArgumentCount(1)) return false; - } - QDir dir(args.first()); + const QString firstArg = arguments().at(0); + QDir dir(firstArg); if (!dir.exists()) { setError(UserDefinedError); - setErrorString(tr("Could not remove folder %1: The folder does not exist.").arg(args.first())); + setErrorString(tr("Could not remove folder %1: The folder does not exist.").arg(firstArg)); return false; } errno = 0; - const bool removed = dir.rmdir(args.first()); + const bool removed = dir.rmdir(firstArg); setValue(QLatin1String("removed"), removed); if (!removed) { setError(UserDefinedError); - setErrorString(tr("Could not remove folder %1: %2").arg(args.first(), errnoToQString(errno))); + setErrorString(tr("Could not remove folder %1: %2").arg(firstArg, errnoToQString(errno))); } return removed; } @@ -616,15 +600,11 @@ bool AppendFileOperation::performOperation() { // This operation takes two arguments. First argument is the name of the file into which a text has to be // appended. Second argument is the text to append. - const QStringList args = this->arguments(); - if (args.count() != 2) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments in %0: %1 arguments given, %2 expected%3.") - .arg(name()).arg(arguments().count()).arg(tr("exactly 2"), QLatin1String(""))); + if (!checkArgumentCount(2)) return false; - } - const QString fName = args.first(); + const QStringList args = this->arguments(); + const QString fName = args.at(0); QFile file(fName); if (!file.open(QFile::Append)) { // first we rename the file, then we copy it to the real target and open the copy - the renamed original is then marked for deletion @@ -653,7 +633,7 @@ bool AppendFileOperation::performOperation() } QTextStream ts(&file); - ts << args.last(); + ts << args.at(1); file.close(); return true; @@ -727,14 +707,11 @@ bool PrependFileOperation::performOperation() // This operation takes two arguments. First argument is the name // of the file into which a text has to be appended. Second argument // is the text to append. - const QStringList args = this->arguments(); - if (args.count() != 2) { - setError(InvalidArguments); - setErrorString(tr("Invalid arguments: %1 arguments given, 2 expected.").arg(args.count())); + if (!checkArgumentCount(2)) return false; - } - const QString fName = args.first(); + const QStringList args = this->arguments(); + const QString fName = args.at(0); // Load the file first. QFile file(fName); if (!file.open(QFile::ReadOnly)) { @@ -748,7 +725,7 @@ bool PrependFileOperation::performOperation() file.close(); // Prepend text to the file text - fContents = args.last() + fContents; + fContents = args.at(1) + fContents; // Now re-open the file in write only mode. if (!file.open(QFile::WriteOnly)) { |