File indexing completed on 2025-04-27 04:04:20
0001 // SPDX-FileCopyrightText: 2019 Linus Jahn <lnj@kaidan.im> 0002 // 0003 // SPDX-License-Identifier: LGPL-2.0-or-later 0004 0005 #include "dirmodelutils.h" 0006 0007 #include <QStandardPaths> 0008 0009 #include <KIO/MkdirJob> 0010 #include <QDir> 0011 #include <QStandardPaths> 0012 0013 DirModelUtils::DirModelUtils(QObject *parent) 0014 : QObject(parent) 0015 { 0016 } 0017 0018 bool DirModelUtils::inHome(const QUrl &url) const 0019 { 0020 const auto homes = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); 0021 QString home; 0022 if (homes.count() > 0) { 0023 home = homes[0]; 0024 } 0025 return !home.isEmpty() && url.path().startsWith(home) && url.path() != home; 0026 } 0027 0028 QUrl DirModelUtils::home() const 0029 { 0030 const auto homes = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); 0031 if (homes.count() > 0) { 0032 return homes[0]; 0033 } 0034 return {}; 0035 } 0036 0037 QUrl DirModelUtils::pictures() const 0038 { 0039 const auto pictures = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); 0040 if (pictures.count() > 0) { 0041 return pictures[0]; 0042 } 0043 return {}; 0044 } 0045 0046 QUrl DirModelUtils::videos() const 0047 { 0048 const auto videos = QStandardPaths::standardLocations(QStandardPaths::MoviesLocation); 0049 if (videos.count() > 0) { 0050 return videos[0]; 0051 } 0052 return {}; 0053 } 0054 0055 bool DirModelUtils::canBeSimplified(const QUrl &url) const 0056 { 0057 const auto homes = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); 0058 QString home; 0059 if (homes.count() > 0) { 0060 home = homes[0]; 0061 } 0062 return !home.isEmpty() && url.path() != home; 0063 } 0064 0065 QStringList DirModelUtils::getUrlParts(const QUrl &url) const 0066 { 0067 if (url.path() == QStringLiteral("/")) 0068 return {}; 0069 0070 const auto homes = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); 0071 QString home; 0072 if (homes.count() > 0) { 0073 home = homes[0]; 0074 } 0075 if (!home.isEmpty() && url.path() != home) { 0076 return url.path().replace(home, "").split(QStringLiteral("/")).mid(1); 0077 } 0078 return url.path().split(QStringLiteral("/")).mid(1); 0079 } 0080 0081 QUrl DirModelUtils::partialUrlForIndex(QUrl url, int index) const 0082 { 0083 const auto homes = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); 0084 QString home; 0085 if (homes.count() > 0) { 0086 home = homes[0]; 0087 } 0088 QStringList urlParts; 0089 bool inHome = false; 0090 if (!home.isEmpty() && url.path().startsWith(home) && url.path() != home) { 0091 urlParts = url.path().replace(home, "/").split(QStringLiteral("/")).mid(1); 0092 inHome = true; 0093 } else { 0094 urlParts = url.path().split(QStringLiteral("/")).mid(1); 0095 } 0096 QString path = QStringLiteral("/"); 0097 for (int i = 0; i < index + int(inHome); i++) { 0098 if (urlParts.at(i) != "") { 0099 path += urlParts.at(i); 0100 path += QStringLiteral("/"); 0101 } 0102 } 0103 if (inHome) { 0104 url.setPath(home + path); 0105 } else { 0106 url.setPath(path); 0107 } 0108 0109 return url; 0110 } 0111 0112 QUrl DirModelUtils::directoryOfUrl(const QString &path) const 0113 { 0114 const int index = path.lastIndexOf(QLatin1Char('/')); 0115 return QUrl::fromLocalFile(path.mid(0, index)); 0116 } 0117 0118 QString DirModelUtils::fileNameOfUrl(const QString &path) const 0119 { 0120 const int index = path.lastIndexOf(QLatin1Char('/')); 0121 return path.mid(index + 1); 0122 } 0123 0124 void DirModelUtils::mkdir(const QUrl &path) const 0125 { 0126 KIO::mkdir(path); 0127 } 0128 0129 QUrl DirModelUtils::parentOfUrl(const QUrl &url) const 0130 { 0131 auto path = QDir(url.toLocalFile()); 0132 path.cdUp(); 0133 return QUrl(path.absolutePath()); 0134 }