File indexing completed on 2024-03-24 15:33:55

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2015 Harald Sitter <sitter@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "kioglobal_p.h"
0009 
0010 #include <QDir>
0011 #include <QStandardPaths>
0012 #include <QTextStream>
0013 
0014 using LocationMap = QMap<QString, QString>;
0015 
0016 static void getExtraXdgDirs(LocationMap &map)
0017 {
0018 #if QT_VERSION < QT_VERSION_CHECK(6, 4, 0) && defined(Q_OS_UNIX)
0019     // Qt5 does not provide an easy way to receive the xdg dir for the templates and public
0020     // directory so we have to find it on our own (QTBUG-86106 and QTBUG-78092)
0021     using QS = QStandardPaths;
0022     const QString xdgUserDirs = QS::locate(QS::ConfigLocation, QStringLiteral("user-dirs.dirs"), QS::LocateFile);
0023     if (xdgUserDirs.isEmpty()) {
0024         return;
0025     }
0026 
0027     QFile file(xdgUserDirs);
0028     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
0029         return;
0030     }
0031 
0032     QTextStream in(&file);
0033     const QLatin1String templatesLine("XDG_TEMPLATES_DIR=\"");
0034     const QLatin1String publicShareLine("XDG_PUBLICSHARE_DIR=\"");
0035     while (!in.atEnd()) {
0036         const QString line = in.readLine();
0037         if (line.startsWith(templatesLine)) {
0038             QString xdgTemplates = line.mid(templatesLine.size()).chopped(1);
0039             xdgTemplates.replace(QStringLiteral("$HOME"), QDir::homePath());
0040             map.insert(xdgTemplates, QStringLiteral("folder-templates"));
0041         } else if (line.startsWith(publicShareLine)) {
0042             QString xdgPublicShare = line.mid(publicShareLine.size()).chopped(1);
0043             xdgPublicShare.replace(QStringLiteral("$HOME"), QDir::homePath());
0044             map.insert(xdgPublicShare, QStringLiteral("folder-public"));
0045         }
0046     }
0047 #endif
0048 }
0049 
0050 static QMap<QString, QString> standardLocationsMap()
0051 {
0052     struct LocationInfo {
0053         QStandardPaths::StandardLocation location;
0054         const char *iconName;
0055     };
0056     static const LocationInfo mapping[] = {
0057 #if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
0058         {QStandardPaths::TemplatesLocation, "folder-templates"},
0059         {QStandardPaths::PublicShareLocation, "folder-public"},
0060 #endif
0061         {QStandardPaths::MusicLocation, "folder-music"},
0062         {QStandardPaths::MoviesLocation, "folder-videos"},
0063         {QStandardPaths::PicturesLocation, "folder-pictures"},
0064         {QStandardPaths::TempLocation, "folder-temp"},
0065         {QStandardPaths::DownloadLocation, "folder-download"},
0066         // Order matters here as paths can be reused for multiple purposes
0067         // We essentially want more generic choices to trump more specific
0068         // ones.
0069         // home > desktop > documents > *.
0070         {QStandardPaths::DocumentsLocation, "folder-documents"},
0071         {QStandardPaths::DesktopLocation, "user-desktop"},
0072         {QStandardPaths::HomeLocation, "user-home"},
0073     };
0074 
0075     LocationMap map;
0076     // Do this first so that e.g. QStandardPaths::HomeLocation is alwasy last
0077     // and it would get QStringLiteral("user-home") associated with it in "map"
0078     getExtraXdgDirs(map);
0079 
0080     for (const auto &row : mapping) {
0081         const QStringList locations = QStandardPaths::standardLocations(row.location);
0082         for (const QString &location : locations) {
0083             map.insert(location, QLatin1String(row.iconName));
0084         }
0085     }
0086     return map;
0087 }
0088 
0089 QString KIOPrivate::iconForStandardPath(const QString &localDirectory)
0090 {
0091     static auto map = standardLocationsMap();
0092     return map.value(localDirectory, QString());
0093 }