File indexing completed on 2025-03-09 03:57:06
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2009-11-27 0007 * Description : A modifier for appending a suffix number to a renaming option. 0008 * This guarantees a unique string for duplicate values. 0009 * 0010 * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com> 0011 * 0012 * SPDX-License-Identifier: GPL-2.0-or-later 0013 * 0014 * ============================================================ */ 0015 0016 #include "uniquemodifier.h" 0017 0018 // Qt includes 0019 0020 #include <QRegularExpression> 0021 0022 // KDE includes 0023 0024 #include <klocalizedstring.h> 0025 0026 // Local includes 0027 0028 #include "digikam_debug.h" 0029 0030 namespace Digikam 0031 { 0032 0033 UniqueModifier::UniqueModifier() 0034 : Modifier(i18nc("unique value for duplicate strings", "Unique"), 0035 i18n("Add a suffix number to have unique strings in duplicate values"), 0036 QLatin1String("button_more")) 0037 { 0038 addToken(QLatin1String("{unique}"), 0039 description()); 0040 addToken(QLatin1String("{unique:||n||}"), 0041 i18n("Add a suffix number, ||n|| specifies the number of digits to use")); 0042 addToken(QLatin1String("{unique:||n||,||c||,||0||}"), 0043 i18n("Add a suffix number, " 0044 "||n|| specifies the number of digits to use, " 0045 "||c|| specifies the separator char before the numbers, " 0046 "||a|| optional to include all options for uniqueness, " 0047 "||0|| optional to always pad with ||n|| zero digits")); 0048 QRegularExpression reg(QLatin1String("\\{unique(:(\\d+))?(,([ -~]))?(,(a|0|a0|0a))?\\}")); 0049 reg.setPatternOptions(QRegularExpression::InvertedGreedinessOption); 0050 setRegExp(reg); 0051 } 0052 0053 QString UniqueModifier::parseOperation(ParseSettings& settings, const QRegularExpressionMatch& match) 0054 { 0055 ParseResults::ResultsKey key = settings.currentResultsKey; 0056 QString tempStr2Modify; 0057 0058 0059 if (match.captured(6).contains(QLatin1Char('a'))) 0060 { 0061 QFileInfo info(settings.fileUrl.toLocalFile()); 0062 tempStr2Modify = settings.results.resultValuesAsString() + info.suffix(); 0063 } 0064 else 0065 { 0066 tempStr2Modify = settings.str2Modify; 0067 } 0068 0069 cache[key] << tempStr2Modify; 0070 bool notUnique = (cache[key].count(tempStr2Modify) > 1); 0071 0072 if (notUnique || (match.captured(6).contains(QLatin1Char('0')))) 0073 { 0074 QString result = settings.str2Modify; 0075 int index = 0; 0076 0077 if (notUnique) 0078 { 0079 index = cache[key].count(tempStr2Modify) - 1; 0080 } 0081 0082 bool ok = true; 0083 int slength = match.captured(2).toInt(&ok); 0084 QString sep = match.captured(4); 0085 QString unique = sep.isEmpty() ? QString::fromUtf8("_%1") 0086 : sep + QString::fromUtf8("%1"); 0087 slength = (slength == 0 || !ok) ? 1 : slength; 0088 result += unique.arg(index, slength, 10, QLatin1Char('0')); 0089 0090 return result; 0091 } 0092 0093 return settings.str2Modify; 0094 } 0095 0096 void UniqueModifier::reset() 0097 { 0098 cache.clear(); 0099 } 0100 0101 } // namespace Digikam 0102 0103 #include "moc_uniquemodifier.cpp"