| 1 | /* |
| 2 | This file is part of the KDE project |
| 3 | SPDX-FileCopyrightText: 2004 Kevin Ottens <ervin ipsquad net> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "remoteimpl.h" |
| 9 | |
| 10 | #include "../utils_p.h" |
| 11 | |
| 12 | #include "debug.h" |
| 13 | #include <KConfigGroup> |
| 14 | #include <KDesktopFile> |
| 15 | #include <KLocalizedString> |
| 16 | |
| 17 | #include <QDir> |
| 18 | #include <QFile> |
| 19 | |
| 20 | RemoteImpl::RemoteImpl() |
| 21 | { |
| 22 | const QString path = QStringLiteral("%1/remoteview" ).arg(a: QStandardPaths::writableLocation(type: QStandardPaths::GenericDataLocation)); |
| 23 | QDir().mkpath(dirPath: path); |
| 24 | } |
| 25 | |
| 26 | void RemoteImpl::listRoot(KIO::UDSEntryList &list) const |
| 27 | { |
| 28 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::listRoot" ; |
| 29 | |
| 30 | QStringList names_found; |
| 31 | const QStringList dirList = QStandardPaths::locateAll(type: QStandardPaths::GenericDataLocation, QStringLiteral("remoteview" ), options: QStandardPaths::LocateDirectory); |
| 32 | |
| 33 | for (const QString &dirpath : dirList) { |
| 34 | QDir dir(dirpath); |
| 35 | if (!dir.exists()) { |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | const QStringList filenames = dir.entryList(nameFilters: {QStringLiteral("*.desktop" )}, filters: QDir::Files | QDir::Readable); |
| 40 | |
| 41 | KIO::UDSEntry entry; |
| 42 | for (const QString &name : filenames) { |
| 43 | if (!names_found.contains(str: name) && createEntry(entry, directory: dirpath, file: name)) { |
| 44 | list.append(t: entry); |
| 45 | names_found.append(t: name); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | bool RemoteImpl::findDirectory(const QString &filename, QString &directory) const |
| 52 | { |
| 53 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::findDirectory" ; |
| 54 | |
| 55 | const QStringList dirList = QStandardPaths::locateAll(type: QStandardPaths::GenericDataLocation, QStringLiteral("remoteview" ), options: QStandardPaths::LocateDirectory); |
| 56 | |
| 57 | for (const QString &dirpath : dirList) { |
| 58 | if (QFileInfo::exists(file: dirpath + QLatin1Char('/') + filename)) { |
| 59 | directory = dirpath + QLatin1Char('/'); |
| 60 | return true; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | QString RemoteImpl::findDesktopFile(const QString &filename) const |
| 68 | { |
| 69 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::findDesktopFile" ; |
| 70 | |
| 71 | QString directory; |
| 72 | const QString desktopFileName = filename + QLatin1String(".desktop" ); |
| 73 | if (findDirectory(filename: desktopFileName, directory)) { |
| 74 | return directory + desktopFileName; |
| 75 | } |
| 76 | |
| 77 | return QString(); |
| 78 | } |
| 79 | |
| 80 | QUrl RemoteImpl::findBaseURL(const QString &filename) const |
| 81 | { |
| 82 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::findBaseURL" ; |
| 83 | |
| 84 | const QString file = findDesktopFile(filename); |
| 85 | if (!file.isEmpty()) { |
| 86 | KDesktopFile desktop(file); |
| 87 | return QUrl::fromUserInput(userInput: desktop.readUrl()); |
| 88 | } |
| 89 | |
| 90 | return QUrl(); |
| 91 | } |
| 92 | |
| 93 | void RemoteImpl::createTopLevelEntry(KIO::UDSEntry &entry) const |
| 94 | { |
| 95 | entry.clear(); |
| 96 | entry.reserve(size: 8); |
| 97 | entry.fastInsert(field: KIO::UDSEntry::UDS_NAME, QStringLiteral("." )); |
| 98 | entry.fastInsert(field: KIO::UDSEntry::UDS_DISPLAY_NAME, i18n("Network" )); |
| 99 | entry.fastInsert(field: KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); |
| 100 | entry.fastInsert(field: KIO::UDSEntry::UDS_ACCESS, l: 0500); |
| 101 | entry.fastInsert(field: KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/directory" )); |
| 102 | entry.fastInsert(field: KIO::UDSEntry::UDS_ICON_NAME, QStringLiteral("folder-remote" )); |
| 103 | entry.fastInsert(field: KIO::UDSEntry::UDS_USER, QStringLiteral("root" )); |
| 104 | entry.fastInsert(field: KIO::UDSEntry::UDS_GROUP, QStringLiteral("root" )); |
| 105 | } |
| 106 | |
| 107 | bool RemoteImpl::createEntry(KIO::UDSEntry &entry, const QString &directory, const QString &file) const |
| 108 | { |
| 109 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::createEntry" ; |
| 110 | |
| 111 | const QString dir = Utils::slashAppended(s: directory); |
| 112 | |
| 113 | KDesktopFile desktop(dir + file); |
| 114 | |
| 115 | qCDebug(KIOREMOTE_LOG) << "path = " << directory << file << desktop.readName(); |
| 116 | |
| 117 | entry.clear(); |
| 118 | |
| 119 | if (desktop.readName().isEmpty()) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | QString new_filename = file; |
| 124 | new_filename.chop(n: 8); |
| 125 | |
| 126 | entry.reserve(size: 8); |
| 127 | entry.fastInsert(field: KIO::UDSEntry::UDS_NAME, value: desktop.readName()); |
| 128 | entry.fastInsert(field: KIO::UDSEntry::UDS_URL, value: QLatin1String("remote:/" ) + new_filename); |
| 129 | |
| 130 | entry.fastInsert(field: KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); |
| 131 | entry.fastInsert(field: KIO::UDSEntry::UDS_ACCESS, l: 0500); |
| 132 | entry.fastInsert(field: KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/directory" )); |
| 133 | |
| 134 | const QString icon = desktop.readIcon(); |
| 135 | entry.fastInsert(field: KIO::UDSEntry::UDS_ICON_NAME, value: icon); |
| 136 | entry.fastInsert(field: KIO::UDSEntry::UDS_LINK_DEST, value: desktop.readUrl()); |
| 137 | entry.fastInsert(field: KIO::UDSEntry::UDS_TARGET_URL, value: desktop.readUrl()); |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | bool RemoteImpl::statNetworkFolder(KIO::UDSEntry &entry, const QString &filename) const |
| 142 | { |
| 143 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::statNetworkFolder: " << filename; |
| 144 | |
| 145 | QString directory; |
| 146 | const QString desktopFileName = filename + QLatin1String(".desktop" ); |
| 147 | return findDirectory(filename: desktopFileName, directory) && createEntry(entry, directory, file: desktopFileName); |
| 148 | } |
| 149 | |
| 150 | bool RemoteImpl::deleteNetworkFolder(const QString &filename) const |
| 151 | { |
| 152 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::deleteNetworkFolder: " << filename; |
| 153 | |
| 154 | QString directory; |
| 155 | const QString desktopFileName = filename + QLatin1String(".desktop" ); |
| 156 | if (findDirectory(filename: desktopFileName, directory)) { |
| 157 | qCDebug(KIOREMOTE_LOG) << "Removing " << directory << filename << ".desktop" ; |
| 158 | return QFile::remove(fileName: directory + desktopFileName); |
| 159 | } |
| 160 | |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | bool RemoteImpl::renameFolders(const QString &src, const QString &dest, bool overwrite) const |
| 165 | { |
| 166 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::renameFolders: " << src << ", " << dest; |
| 167 | |
| 168 | QString directory; |
| 169 | const QString srcDesktopFileName = src + QLatin1String(".desktop" ); |
| 170 | if (findDirectory(filename: srcDesktopFileName, directory)) { |
| 171 | const QString destDesktopFileName = dest + QLatin1String(".desktop" ); |
| 172 | const QString destDesktopFilePath = directory + destDesktopFileName; |
| 173 | if (!overwrite && QFile::exists(fileName: destDesktopFilePath)) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | qCDebug(KIOREMOTE_LOG) << "Renaming " << directory << src << ".desktop" ; |
| 178 | QDir dir(directory); |
| 179 | bool res = dir.rename(oldName: srcDesktopFileName, newName: destDesktopFileName); |
| 180 | if (res) { |
| 181 | KDesktopFile desktop(destDesktopFilePath); |
| 182 | desktop.desktopGroup().writeEntry(key: "Name" , value: dest); |
| 183 | } |
| 184 | return res; |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | bool RemoteImpl::changeFolderTarget(const QString &src, const QString &target, bool overwrite) const |
| 191 | { |
| 192 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::changeFolderTarget: " << src << ", " << target; |
| 193 | |
| 194 | QString directory; |
| 195 | const QString srcDesktopFileName = src + QLatin1String(".desktop" ); |
| 196 | if (findDirectory(filename: srcDesktopFileName, directory)) { |
| 197 | const QString srcDesktopFilePath = directory + srcDesktopFileName; |
| 198 | if (!overwrite || !QFile::exists(fileName: srcDesktopFilePath)) { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | qCDebug(KIOREMOTE_LOG) << "Changing target " << directory << src << ".desktop" ; |
| 203 | KDesktopFile desktop(srcDesktopFilePath); |
| 204 | desktop.desktopGroup().writeEntry(key: "URL" , value: target); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |