File indexing completed on 2024-04-21 04:18:47
0001 // vim: set tabstop=4 shiftwidth=4 expandtab: 0002 /* 0003 Gwenview: an image viewer 0004 Copyright 2009 Aurélien Gâteau <agateau@kde.org> 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public License 0008 as published by the Free Software Foundation; either version 2 0009 of the License, or (at your option) any later version. 0010 0011 This program is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program; if not, write to the Free Software 0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA. 0019 0020 */ 0021 // Self 0022 #include "filenameformater.h" 0023 0024 // Qt 0025 #include <QDateTime> 0026 #include <QFileInfo> 0027 #include <QUrl> 0028 0029 // KF 0030 #include <KLocalizedString> 0031 0032 // Local 0033 0034 namespace Gwenview 0035 { 0036 using Dict = QHash<QString, QString>; 0037 0038 struct FileNameFormaterPrivate { 0039 QString mFormat; 0040 }; 0041 0042 FileNameFormater::FileNameFormater(const QString &format) 0043 : d(new FileNameFormaterPrivate) 0044 { 0045 d->mFormat = format; 0046 } 0047 0048 FileNameFormater::~FileNameFormater() 0049 { 0050 delete d; 0051 } 0052 0053 QString FileNameFormater::format(const QUrl &url, const QDateTime &dateTime) 0054 { 0055 QFileInfo info(url.fileName()); 0056 0057 // Keep in sync with helpMap() 0058 Dict dict; 0059 dict[QStringLiteral("date")] = dateTime.toString(QStringLiteral("yyyy-MM-dd")); 0060 dict[QStringLiteral("time")] = dateTime.toString(QStringLiteral("HH-mm-ss")); 0061 dict[QStringLiteral("ext")] = info.suffix(); 0062 dict[QStringLiteral("ext.lower")] = info.suffix().toLower(); 0063 dict[QStringLiteral("name")] = info.completeBaseName(); 0064 dict[QStringLiteral("name.lower")] = info.completeBaseName().toLower(); 0065 0066 QString name; 0067 const int length = d->mFormat.length(); 0068 for (int pos = 0; pos < length; ++pos) { 0069 QChar ch = d->mFormat[pos]; 0070 if (ch == QLatin1Char('{')) { 0071 if (pos == length - 1) { 0072 // We are at the end, ignore this 0073 break; 0074 } 0075 if (d->mFormat[pos + 1] == QLatin1Char('{')) { 0076 // This is an escaped '{', skip one 0077 name += '{'; 0078 ++pos; 0079 continue; 0080 } 0081 int end = d->mFormat.indexOf('}', pos + 1); 0082 if (end == -1) { 0083 // No '}' found, stop here 0084 return name; 0085 } 0086 // Replace keyword with its value 0087 const QString keyword = d->mFormat.mid(pos + 1, end - pos - 1); 0088 name += dict.value(keyword); 0089 pos = end; 0090 } else { 0091 name += ch; 0092 } 0093 } 0094 return name; 0095 } 0096 0097 FileNameFormater::HelpMap FileNameFormater::helpMap() 0098 { 0099 // Keep in sync with dict in format() 0100 static HelpMap map; 0101 if (map.isEmpty()) { 0102 map[QStringLiteral("date")] = i18n("Shooting date"); 0103 map[QStringLiteral("time")] = i18n("Shooting time"); 0104 map[QStringLiteral("ext")] = i18n("Original extension"); 0105 map[QStringLiteral("ext.lower")] = i18n("Original extension, in lower case"); 0106 map[QStringLiteral("name")] = i18n("Original filename"); 0107 map[QStringLiteral("name.lower")] = i18n("Original filename, in lower case"); 0108 } 0109 return map; 0110 } 0111 0112 } // namespace