File indexing completed on 2024-05-19 04:55:59

0001 /**
0002  * \file abstractfiledecorationprovider.cpp
0003  * Indirection for QFileIconProvider to use it without Gui and Widgets.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 9 Nov 2018
0008  *
0009  * Copyright (C) 2018  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 "abstractfiledecorationprovider.h"
0028 #include <QString>
0029 #include <QDir>
0030 #include <QCoreApplication>
0031 
0032 AbstractFileDecorationProvider::~AbstractFileDecorationProvider()
0033 {
0034 }
0035 
0036 QString AbstractFileDecorationProvider::type(const QFileInfo &info) const
0037 {
0038     return fileTypeDescription(info);
0039 }
0040 
0041 /* Taken from QFileIconProvider, simplified. */
0042 #if defined(Q_OS_WIN)
0043 static bool isUncRoot(const QString &server)
0044 {
0045     QString localPath = QDir::toNativeSeparators(server);
0046     if (!localPath.startsWith(QLatin1String("\\\\")))
0047         return false;
0048 
0049     int idx = localPath.indexOf(QLatin1Char('\\'), 2);
0050     if (idx == -1 || idx + 1 == localPath.length())
0051         return true;
0052 
0053     return localPath.right(localPath.length() - idx - 1).trimmed().isEmpty();
0054 }
0055 
0056 static bool isDriveRootPath(const QString &path)
0057 {
0058 #ifndef Q_OS_WINRT
0059     return (path.length() == 3
0060            && path.at(0).isLetter() && path.at(1) == QLatin1Char(':')
0061            && path.at(2) == QLatin1Char('/'));
0062 #else // !Q_OS_WINRT
0063     return path == QDir::rootPath();
0064 #endif // !Q_OS_WINRT
0065 }
0066 #endif // Q_OS_WIN
0067 
0068 static bool isRootPath(const QString &path)
0069 {
0070     if (path == QLatin1String("/")
0071 #if defined(Q_OS_WIN)
0072             || isDriveRootPath(path)
0073             || isUncRoot(path)
0074 #endif
0075             )
0076         return true;
0077 
0078     return false;
0079 }
0080 
0081 QString AbstractFileDecorationProvider::fileTypeDescription(const QFileInfo &info)
0082 {
0083     if (isRootPath(info.absoluteFilePath())) {
0084         const char* const driveStr = QT_TRANSLATE_NOOP("@default", "Drive");
0085         return QCoreApplication::translate("@default", driveStr);
0086     }
0087     if (info.isFile()) {
0088         if (!info.suffix().isEmpty()) {
0089             //: %1 is a file name suffix, for example txt
0090             const char* const suffixStr = QT_TRANSLATE_NOOP("@default", "%1 File");
0091             return QCoreApplication::translate("@default", suffixStr).arg(info.suffix());
0092         }
0093         const char* const fileStr = QT_TRANSLATE_NOOP("@default", "File");
0094         return QCoreApplication::translate("@default", fileStr);
0095     }
0096 
0097     if (info.isDir()) {
0098         const char* const folderStr = QT_TRANSLATE_NOOP("@default", "Folder");
0099         return QCoreApplication::translate("@default", folderStr);
0100     }
0101     if (info.isSymLink()) {
0102         const char* const shortcutStr = QT_TRANSLATE_NOOP("@default", "Shortcut");
0103         return QCoreApplication::translate("@default", shortcutStr);
0104     }
0105     const char* const unknownStr = QT_TRANSLATE_NOOP("@default", "Unknown");
0106     return QCoreApplication::translate("@default", unknownStr);
0107 }