| 1 | /* |
| 2 | * Copyright (C) 2020 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser 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, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "WildcardResolver.h" |
| 21 | |
| 22 | QString WildcardResolver::replaceDateTimeWildcards(const QString &filename) |
| 23 | { |
| 24 | auto filenameWithoutWildcards = filename; |
| 25 | filenameWithoutWildcards.replace(QLatin1String("$Y"), QDateTime::currentDateTime().toString(QLatin1String("yyyy"))); |
| 26 | filenameWithoutWildcards.replace(QLatin1String("$M"), QDateTime::currentDateTime().toString(QLatin1String("MM"))); |
| 27 | filenameWithoutWildcards.replace(QLatin1String("$D"), QDateTime::currentDateTime().toString(QLatin1String("dd"))); |
| 28 | filenameWithoutWildcards.replace(QLatin1String("$T"), QDateTime::currentDateTime().toString(QLatin1String("hhmmss"))); |
| 29 | filenameWithoutWildcards.replace(QLatin1String("$h"), QDateTime::currentDateTime().toString(QLatin1String("hh"))); |
| 30 | filenameWithoutWildcards.replace(QLatin1String("$m"), QDateTime::currentDateTime().toString(QLatin1String("mm"))); |
| 31 | filenameWithoutWildcards.replace(QLatin1String("$s"), QDateTime::currentDateTime().toString(QLatin1String("ss"))); |
| 32 | return filenameWithoutWildcards; |
| 33 | } |
| 34 | |
| 35 | QString WildcardResolver::replaceNumberWildcards(const QString &filename, const QString &directory, const QString &format) |
| 36 | { |
| 37 | auto filenameWithoutWildcards = filename; |
| 38 | auto wildcard = QLatin1Char('#'); |
| 39 | if(filenameWithoutWildcards.contains(wildcard)) { |
| 40 | auto firstWildcardIndex = filename.indexOf(wildcard); |
| 41 | auto lastWildcardIndex = filename.lastIndexOf(wildcard); |
| 42 | auto leftPart = filename.left(firstWildcardIndex); |
| 43 | auto rightPart = filename.mid(lastWildcardIndex + 1); |
| 44 | auto digitCount = filename.count(wildcard); |
| 45 | auto highestNumber = getHighestWildcardNumber(directory, leftPart, rightPart, format); |
| 46 | |
| 47 | filenameWithoutWildcards = leftPart + QString("%1").arg(highestNumber + 1, digitCount, 10, QChar('0')) + rightPart; |
| 48 | } |
| 49 | |
| 50 | return filenameWithoutWildcards; |
| 51 | } |
| 52 | |
| 53 | int WildcardResolver::getHighestWildcardNumber(const QString &directory, const QString &leftPart, const QString &rightPart, const QString &format) |
| 54 | { |
| 55 | auto rightPartWithFormat = rightPart + format; |
| 56 | QDir parentDirectory(directory); |
| 57 | auto number = 0; |
| 58 | auto allFiles = parentDirectory.entryList({ QLatin1String("*") + format }, QDir::Files); |
| 59 | for(auto file : allFiles) { |
| 60 | if(file.startsWith(leftPart) && file.endsWith(rightPartWithFormat)) { |
| 61 | file.remove(leftPart); |
| 62 | file.remove(rightPartWithFormat); |
| 63 | auto currentNumber = file.toInt(); |
| 64 | number = qMax(number, currentNumber); |
| 65 | } |
| 66 | } |
| 67 | return number; |
| 68 | } |