File indexing completed on 2024-05-05 03:56:07

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2004 Kevin Ottens <ervin ipsquad net>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "remoteimpl.h"
0009 
0010 #include "../utils_p.h"
0011 
0012 #include "debug.h"
0013 #include <KConfigGroup>
0014 #include <KDesktopFile>
0015 #include <KLocalizedString>
0016 
0017 #include <QDir>
0018 #include <QFile>
0019 
0020 RemoteImpl::RemoteImpl()
0021 {
0022     const QString path = QStringLiteral("%1/remoteview").arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
0023     QDir().mkpath(path);
0024 }
0025 
0026 void RemoteImpl::listRoot(KIO::UDSEntryList &list) const
0027 {
0028     qCDebug(KIOREMOTE_LOG) << "RemoteImpl::listRoot";
0029 
0030     QStringList names_found;
0031     const QStringList dirList = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("remoteview"), QStandardPaths::LocateDirectory);
0032 
0033     for (const QString &dirpath : dirList) {
0034         QDir dir(dirpath);
0035         if (!dir.exists()) {
0036             continue;
0037         }
0038 
0039         const QStringList filenames = dir.entryList({QStringLiteral("*.desktop")}, QDir::Files | QDir::Readable);
0040 
0041         KIO::UDSEntry entry;
0042         for (const QString &name : filenames) {
0043             if (!names_found.contains(name) && createEntry(entry, dirpath, name)) {
0044                 list.append(entry);
0045                 names_found.append(name);
0046             }
0047         }
0048     }
0049 }
0050 
0051 bool RemoteImpl::findDirectory(const QString &filename, QString &directory) const
0052 {
0053     qCDebug(KIOREMOTE_LOG) << "RemoteImpl::findDirectory";
0054 
0055     const QStringList dirList = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("remoteview"), QStandardPaths::LocateDirectory);
0056 
0057     for (const QString &dirpath : dirList) {
0058         if (QFileInfo::exists(dirpath + QLatin1Char('/') + filename)) {
0059             directory = dirpath + QLatin1Char('/');
0060             return true;
0061         }
0062     }
0063 
0064     return false;
0065 }
0066 
0067 QString RemoteImpl::findDesktopFile(const QString &filename) const
0068 {
0069     qCDebug(KIOREMOTE_LOG) << "RemoteImpl::findDesktopFile";
0070 
0071     QString directory;
0072     const QString desktopFileName = filename + QLatin1String(".desktop");
0073     if (findDirectory(desktopFileName, directory)) {
0074         return directory + desktopFileName;
0075     }
0076 
0077     return QString();
0078 }
0079 
0080 QUrl RemoteImpl::findBaseURL(const QString &filename) const
0081 {
0082     qCDebug(KIOREMOTE_LOG) << "RemoteImpl::findBaseURL";
0083 
0084     const QString file = findDesktopFile(filename);
0085     if (!file.isEmpty()) {
0086         KDesktopFile desktop(file);
0087         return QUrl::fromUserInput(desktop.readUrl());
0088     }
0089 
0090     return QUrl();
0091 }
0092 
0093 void RemoteImpl::createTopLevelEntry(KIO::UDSEntry &entry) const
0094 {
0095     entry.clear();
0096     entry.reserve(8);
0097     entry.fastInsert(KIO::UDSEntry::UDS_NAME, QStringLiteral("."));
0098     entry.fastInsert(KIO::UDSEntry::UDS_DISPLAY_NAME, i18n("Network"));
0099     entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
0100     entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, 0500);
0101     entry.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/directory"));
0102     entry.fastInsert(KIO::UDSEntry::UDS_ICON_NAME, QStringLiteral("folder-remote"));
0103     entry.fastInsert(KIO::UDSEntry::UDS_USER, QStringLiteral("root"));
0104     entry.fastInsert(KIO::UDSEntry::UDS_GROUP, QStringLiteral("root"));
0105 }
0106 
0107 bool RemoteImpl::createEntry(KIO::UDSEntry &entry, const QString &directory, const QString &file) const
0108 {
0109     qCDebug(KIOREMOTE_LOG) << "RemoteImpl::createEntry";
0110 
0111     const QString dir = Utils::slashAppended(directory);
0112 
0113     KDesktopFile desktop(dir + file);
0114 
0115     qCDebug(KIOREMOTE_LOG) << "path = " << directory << file << desktop.readName();
0116 
0117     entry.clear();
0118 
0119     if (desktop.readName().isEmpty()) {
0120         return false;
0121     }
0122 
0123     QString new_filename = file;
0124     new_filename.chop(8);
0125 
0126     entry.reserve(8);
0127     entry.fastInsert(KIO::UDSEntry::UDS_NAME, desktop.readName());
0128     entry.fastInsert(KIO::UDSEntry::UDS_URL, QLatin1String("remote:/") + new_filename);
0129 
0130     entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
0131     entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, 0500);
0132     entry.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/directory"));
0133 
0134     const QString icon = desktop.readIcon();
0135     entry.fastInsert(KIO::UDSEntry::UDS_ICON_NAME, icon);
0136     entry.fastInsert(KIO::UDSEntry::UDS_LINK_DEST, desktop.readUrl());
0137     entry.fastInsert(KIO::UDSEntry::UDS_TARGET_URL, desktop.readUrl());
0138     return true;
0139 }
0140 
0141 bool RemoteImpl::statNetworkFolder(KIO::UDSEntry &entry, const QString &filename) const
0142 {
0143     qCDebug(KIOREMOTE_LOG) << "RemoteImpl::statNetworkFolder: " << filename;
0144 
0145     QString directory;
0146     const QString desktopFileName = filename + QLatin1String(".desktop");
0147     return findDirectory(desktopFileName, directory) && createEntry(entry, directory, desktopFileName);
0148 }
0149 
0150 bool RemoteImpl::deleteNetworkFolder(const QString &filename) const
0151 {
0152     qCDebug(KIOREMOTE_LOG) << "RemoteImpl::deleteNetworkFolder: " << filename;
0153 
0154     QString directory;
0155     const QString desktopFileName = filename + QLatin1String(".desktop");
0156     if (findDirectory(desktopFileName, directory)) {
0157         qCDebug(KIOREMOTE_LOG) << "Removing " << directory << filename << ".desktop";
0158         return QFile::remove(directory + desktopFileName);
0159     }
0160 
0161     return false;
0162 }
0163 
0164 bool RemoteImpl::renameFolders(const QString &src, const QString &dest, bool overwrite) const
0165 {
0166     qCDebug(KIOREMOTE_LOG) << "RemoteImpl::renameFolders: " << src << ", " << dest;
0167 
0168     QString directory;
0169     const QString srcDesktopFileName = src + QLatin1String(".desktop");
0170     if (findDirectory(srcDesktopFileName, directory)) {
0171         const QString destDesktopFileName = dest + QLatin1String(".desktop");
0172         const QString destDesktopFilePath = directory + destDesktopFileName;
0173         if (!overwrite && QFile::exists(destDesktopFilePath)) {
0174             return false;
0175         }
0176 
0177         qCDebug(KIOREMOTE_LOG) << "Renaming " << directory << src << ".desktop";
0178         QDir dir(directory);
0179         bool res = dir.rename(srcDesktopFileName, destDesktopFileName);
0180         if (res) {
0181             KDesktopFile desktop(destDesktopFilePath);
0182             desktop.desktopGroup().writeEntry("Name", dest);
0183         }
0184         return res;
0185     }
0186 
0187     return false;
0188 }
0189 
0190 bool RemoteImpl::changeFolderTarget(const QString &src, const QString &target, bool overwrite) const
0191 {
0192     qCDebug(KIOREMOTE_LOG) << "RemoteImpl::changeFolderTarget: " << src << ", " << target;
0193 
0194     QString directory;
0195     const QString srcDesktopFileName = src + QLatin1String(".desktop");
0196     if (findDirectory(srcDesktopFileName, directory)) {
0197         const QString srcDesktopFilePath = directory + srcDesktopFileName;
0198         if (!overwrite || !QFile::exists(srcDesktopFilePath)) {
0199             return false;
0200         }
0201 
0202         qCDebug(KIOREMOTE_LOG) << "Changing target " << directory << src << ".desktop";
0203         KDesktopFile desktop(srcDesktopFilePath);
0204         desktop.desktopGroup().writeEntry("URL", target);
0205         return true;
0206     }
0207 
0208     return false;
0209 }