File indexing completed on 2025-03-09 03:57:07

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-08-08
0007  * Description : an option to provide file information to the parser
0008  *
0009  * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "filepropertiesoption.h"
0016 
0017 // Qt includes
0018 
0019 #include <QFileInfo>
0020 #include <QRegularExpression>
0021 
0022 // KDE includes
0023 
0024 #include <klocalizedstring.h>
0025 
0026 namespace
0027 {
0028 static const QString KEY_FILE(QLatin1String("[file]"));
0029 static const QString KEY_EXT(QLatin1String("[ext]"));
0030 static const QString KEY_USER(QLatin1String("[user]"));
0031 static const QString KEY_GROUP(QLatin1String("[group]"));
0032 }
0033 
0034 namespace Digikam
0035 {
0036 
0037 FilePropertiesOption::FilePropertiesOption()
0038     : Option(i18n("File"),
0039              i18n("Add file properties"),
0040              QLatin1String("folder-pictures"))
0041 {
0042     setUseTokenMenu(true);
0043 
0044     addToken(KEY_FILE, i18n("Filename"),
0045              i18nc("File name", "Name"));
0046 
0047     addToken(KEY_EXT, i18n("File extension, prepend with a '.' character, to modify the real file extension"),
0048              i18nc("File extension", "Extension"));
0049 
0050     addToken(KEY_USER, i18n("Owner of the file"),
0051              i18nc("Owner of the file", "Owner"));
0052 
0053     addToken(KEY_GROUP, i18n("Group of the file"),
0054              i18nc("Group of the file", "Group"));
0055 
0056     QString regExpStr;
0057     regExpStr.append(QLatin1Char('('));
0058     regExpStr.append(escapeToken(KEY_FILE)).append(QLatin1Char('|'));
0059     regExpStr.append(escapeToken(KEY_USER)).append(QLatin1Char('|'));
0060     regExpStr.append(escapeToken(KEY_GROUP)).append(QLatin1Char('|'));
0061     regExpStr.append(QLatin1String("(\\.?)")).append(escapeToken(KEY_EXT));
0062     regExpStr.append(QLatin1Char(')'));
0063 
0064     QRegularExpression reg(regExpStr);
0065     reg.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
0066     setRegExp(reg);
0067 }
0068 
0069 QString FilePropertiesOption::parseOperation(ParseSettings& settings, const QRegularExpressionMatch& match)
0070 {
0071     QString result;
0072     QFileInfo fi(settings.fileUrl.toLocalFile());
0073 
0074     const QString& token = match.captured(1);
0075 
0076     if      (token == KEY_FILE)
0077     {
0078         QString fileName = fi.completeBaseName();
0079         int index        = settings.cutFileName;
0080 
0081         result = fileName.mid(index);
0082     }
0083     else if (token == KEY_USER)
0084     {
0085         result = fi.owner();
0086     }
0087     else if (token == KEY_GROUP)
0088     {
0089         result = fi.group();
0090     }
0091     else if (token == KEY_EXT)
0092     {
0093         result = fi.suffix();
0094     }
0095     else if (token == (QLatin1Char('.') + KEY_EXT))
0096     {
0097         result                            = QLatin1Char('.') + fi.suffix();
0098         settings.useOriginalFileExtension = false;
0099     }
0100 
0101     return result;
0102 }
0103 
0104 } // namespace Digikam
0105 
0106 #include "moc_filepropertiesoption.cpp"