File indexing completed on 2024-05-12 15:55:34

0001 // SPDX-FileCopyrightText: 2012-2020 The KPhotoAlbum Development Team
0002 // SPDX-FileCopyrightText: 2021 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 
0006 #include "FileName.h"
0007 
0008 #include "FileNameUtil.h"
0009 #include "Logging.h"
0010 #include "SettingsData.h"
0011 
0012 #include <QFile>
0013 #include <QLoggingCategory>
0014 
0015 DB::FileName::FileName()
0016     : m_isNull(true)
0017 {
0018 }
0019 
0020 DB::FileName DB::FileName::fromAbsolutePath(const QString &fileName)
0021 {
0022     const QString imageRoot = Utilities::stripEndingForwardSlash(Settings::SettingsData::instance()->imageDirectory()) + QLatin1String("/");
0023     if (!fileName.startsWith(imageRoot)) {
0024         qCWarning(DBLog) << "Absolute filename is outside of image root:" << fileName;
0025         return FileName();
0026     }
0027 
0028     FileName res;
0029     res.m_isNull = false;
0030     res.m_absoluteFilePath = fileName;
0031     res.m_relativePath = fileName.mid(imageRoot.length());
0032     if (res.m_relativePath.isEmpty() || res.m_absoluteFilePath.isEmpty()) {
0033         qCWarning(DBLog) << "Relative or absolute filename cannot be empty!";
0034         return {};
0035     }
0036     return res;
0037 }
0038 
0039 DB::FileName DB::FileName::fromRelativePath(const QString &fileName)
0040 {
0041     if (fileName.startsWith(QChar::fromLatin1('/'))) {
0042         qCWarning(DBLog) << "Relative filename cannot start with '/':" << fileName;
0043         return {};
0044     }
0045     FileName res;
0046     res.m_isNull = false;
0047     res.m_relativePath = fileName;
0048     res.m_absoluteFilePath = Utilities::stripEndingForwardSlash(Settings::SettingsData::instance()->imageDirectory()) + QLatin1String("/") + fileName;
0049     if (res.m_relativePath.isEmpty() || res.m_absoluteFilePath.isEmpty()) {
0050         qCWarning(DBLog) << "Relative or absolute filename cannot be empty!";
0051         return {};
0052     }
0053     return res;
0054 }
0055 
0056 QString DB::FileName::absolute() const
0057 {
0058     Q_ASSERT(!isNull());
0059     return m_absoluteFilePath;
0060 }
0061 
0062 QString DB::FileName::relative() const
0063 {
0064     Q_ASSERT(!m_isNull);
0065     return m_relativePath;
0066 }
0067 
0068 bool DB::FileName::isNull() const
0069 {
0070     return m_isNull;
0071 }
0072 
0073 bool DB::FileName::isValid() const
0074 {
0075     return !isNull();
0076 }
0077 
0078 bool DB::FileName::operator==(const DB::FileName &other) const
0079 {
0080     return m_isNull == other.m_isNull && m_relativePath == other.m_relativePath;
0081 }
0082 
0083 bool DB::FileName::operator!=(const DB::FileName &other) const
0084 {
0085     return !(*this == other);
0086 }
0087 
0088 bool DB::FileName::operator<(const DB::FileName &other) const
0089 {
0090     if (isNull()) {
0091         qCWarning(DBLog) << "FileName for comparison is null!";
0092         return true;
0093     }
0094     if (other.isNull()) {
0095         qCWarning(DBLog) << "FileName for comparison is null!";
0096         return false;
0097     }
0098     return relative() < other.relative();
0099 }
0100 
0101 bool DB::FileName::exists() const
0102 {
0103     return QFile::exists(absolute());
0104 }
0105 
0106 DB::FileName::operator QUrl() const
0107 {
0108     return QUrl::fromLocalFile(absolute());
0109 }
0110 
0111 uint DB::qHash(const DB::FileName &fileName)
0112 {
0113     if (fileName.isNull())
0114         return qHash(QString());
0115     return qHash(fileName.relative());
0116 }
0117 // vi:expandtab:tabstop=4 shiftwidth=4: