File indexing completed on 2024-04-28 15:40:26

0001 /* SPDX-FileCopyrightText: 2008-2010 Henner Zeller <h.zeller@acm.org>
0002 
0003    based on Utilities::createUniqNameMap() by <blackie@kde.org>
0004 
0005    SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 #ifndef UTILITIES_UNIQ_FILENAME_MAPPER_H
0008 #define UTILITIES_UNIQ_FILENAME_MAPPER_H
0009 
0010 #include <kpabase/FileName.h>
0011 
0012 #include <QMap>
0013 #include <QSet>
0014 #include <QString>
0015 
0016 namespace Utilities
0017 {
0018 
0019 /**
0020  * The UniqFilenameMapper creates flat filenames from arbitrary input filenames
0021  * so that there are no conflicts if they're written in one directory together.
0022  * The resulting names do not contain any path unless a targetDirectory is
0023  * given in the constructor.
0024  *
0025  * Example:
0026  * uniqNameFor("cd1/def.jpg")      -> def.jpg
0027  * uniqNameFor("cd1/abc/file.jpg") -> file.jpg
0028  * uniqNameFor("cd3/file.jpg")     -> file-1.jpg
0029  * uniqNameFor("cd1/abc/file.jpg") -> file.jpg    // file from above.
0030  */
0031 class UniqFilenameMapper
0032 {
0033 public:
0034     UniqFilenameMapper();
0035 
0036     // Create a UniqFilenameMapper that returns filenames with the
0037     // targetDirectory prepended.
0038     // The UniqFilenameMapper makes sure, that generated filenames do not
0039     // previously exist in the targetDirectory.
0040     explicit UniqFilenameMapper(const QString &targetDirectory);
0041 
0042     // Create a unique, flat filename for the target directory. If this method
0043     // has been called before with the same argument, the unique name that has
0044     // been created before is returned (see example above).
0045     QString uniqNameFor(const DB::FileName &filename);
0046 
0047     // Reset all mappings.
0048     void reset();
0049 
0050 private:
0051     UniqFilenameMapper(const UniqFilenameMapper &); // don't copy.
0052 
0053     bool fileClashes(const QString &file);
0054 
0055     const QString m_targetDirectory;
0056     typedef QMap<DB::FileName, QString> FileNameMap;
0057     FileNameMap m_origToUniq;
0058     QSet<QString> m_uniqFiles;
0059 };
0060 }
0061 #endif /* UTILITIES_UNIQ_FILENAME_MAPPER_H */
0062 // vi:expandtab:tabstop=4 shiftwidth=4: