File indexing completed on 2024-05-19 04:56:13

0001 /**
0002  * \file saferename.cpp
0003  * Safely rename a file.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 16 Feb 2012
0008  *
0009  * Copyright (C) 2012-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "saferename.h"
0028 #include <QDir>
0029 #include "formatconfig.h"
0030 
0031 #ifdef Q_OS_WIN32
0032 
0033 bool Utils::hasIllegalFileNameCharacters(const QString& fileName)
0034 {
0035   static const char illegalChars[] = "<>:\"|?*";
0036   QString fileNameWithoutDrive(
0037       (QDir::isAbsolutePath(fileName) && fileName.mid(1, 2) == QLatin1String(":/"))
0038         ? fileName.mid(3) : fileName);
0039   for (const char* chPtr = illegalChars; *chPtr; ++chPtr) {
0040     if (fileNameWithoutDrive.contains(QLatin1Char(*chPtr))) {
0041       return true;
0042     }
0043   }
0044   return false;
0045 }
0046 
0047 #else
0048 
0049 bool Utils::hasIllegalFileNameCharacters(const QString&)
0050 {
0051   return false;
0052 }
0053 
0054 #endif
0055 
0056 bool Utils::safeRename(const QString& oldName, const QString& newName)
0057 {
0058   if (hasIllegalFileNameCharacters(newName))
0059     return false;
0060 
0061   return QDir().rename(oldName, newName);
0062 }
0063 
0064 bool Utils::safeRename(const QString& dirPath,
0065                        const QString& oldName, const QString& newName)
0066 {
0067   if (hasIllegalFileNameCharacters(newName))
0068     return false;
0069 
0070   return QDir(dirPath).rename(oldName, newName);
0071 }
0072 
0073 bool Utils::replaceIllegalFileNameCharacters(
0074     QString& fileName, const QString& defaultReplacement,
0075     const char* illegalChars)
0076 {
0077   if (!illegalChars) {
0078 #ifdef Q_OS_WIN32
0079     illegalChars = "<>:\"|?*\\/";
0080 #else
0081     illegalChars = "/";
0082 #endif
0083   }
0084   QMap<QChar, QString> replaceMap;
0085   bool changed = false;
0086   for (const char* ic = illegalChars; *ic; ++ic) {
0087     if (QChar illegalChar = QLatin1Char(*ic); fileName.contains(illegalChar)) {
0088       if (!changed) {
0089         if (const FormatConfig& fnCfg = FilenameFormatConfig::instance();
0090             fnCfg.strRepEnabled()) {
0091           const auto constStrRepMap = fnCfg.strRepMap();
0092           for (const auto& keyVal : constStrRepMap) {
0093             if (keyVal.first.length() == 1) {
0094               replaceMap.insert(keyVal.first.at(0), keyVal.second);
0095             }
0096           }
0097         }
0098         changed = true;
0099       }
0100       QString replacement = replaceMap.value(illegalChar, defaultReplacement);
0101       fileName.replace(illegalChar, replacement);
0102     }
0103   }
0104   return changed;
0105 }