diff --git a/kioslave/CMakeLists.txt b/kioslave/CMakeLists.txt --- a/kioslave/CMakeLists.txt +++ b/kioslave/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(applications) add_subdirectory(desktop) +add_subdirectory(places) add_subdirectory(remote) diff --git a/kioslave/places/CMakeLists.txt b/kioslave/places/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/kioslave/places/CMakeLists.txt @@ -0,0 +1,8 @@ +add_definitions(-DTRANSLATION_DOMAIN=\"kio_places\") + +add_library(kio_places MODULE kio_places.cpp) + +target_link_libraries(kio_places KF5::KIOCore KF5::KIOFileWidgets KF5::I18n) + +install(TARGETS kio_places DESTINATION ${KDE_INSTALL_PLUGINDIR}) +install(FILES places.protocol DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) diff --git a/kioslave/places/Messages.sh b/kioslave/places/Messages.sh new file mode 100644 --- /dev/null +++ b/kioslave/places/Messages.sh @@ -0,0 +1,2 @@ +#! /usr/bin/env bash +$XGETTEXT `find . -name "*.cc" -o -name "*.cpp" -o -name "*.h"` -o $podir/kio_places.pot diff --git a/kioslave/places/kio_places.cpp b/kioslave/places/kio_places.cpp new file mode 100644 --- /dev/null +++ b/kioslave/places/kio_places.cpp @@ -0,0 +1,129 @@ +/* This file is part of the KDE project + Copyright (C) 2017 Kai Uwe Broulik + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include + +#include +#include + +#include +#include +#include +#include + +class PlacesProtocol : public KIO::SlaveBase +{ +public: + PlacesProtocol(const QByteArray &protocol, const QByteArray &pool, const QByteArray &app); + ~PlacesProtocol() override; + void stat(const QUrl& url) override; + void listDir(const QUrl& url) override; + +private: + QScopedPointer m_places; +}; + +extern "C" { + Q_DECL_EXPORT int kdemain(int argc, char **argv) + { + QGuiApplication app(argc, argv); // needs to be QGuiApplication for QIcon + app.setApplicationName("kio_places"); + + PlacesProtocol slave(argv[1], argv[2], argv[3]); + slave.dispatchLoop(); + return 0; + } +} + +PlacesProtocol::PlacesProtocol(const QByteArray &protocol, const QByteArray &pool, const QByteArray &app) + : SlaveBase(protocol, pool, app) + , m_places(new KFilePlacesModel()) +{ + +} + +PlacesProtocol::~PlacesProtocol() = default; + +static KIO::UDSEntry rootEntry() +{ + KIO::UDSEntry entry; + + entry.insert(KIO::UDSEntry::UDS_NAME, QStringLiteral(".")); + // neither FolderView nor Dolphin nor KPropertiesDialog pick that up for the root location + entry.insert(KIO::UDSEntry::UDS_DISPLAY_NAME, i18n("Places")); + // doesn't KFileItem have a fallback that uses the protocol icon also? + // It doesn't pick that here up either but KIO::iconNameForUrl does + entry.insert(KIO::UDSEntry::UDS_ICON_NAME, QStringLiteral("folder-bookmark")); + entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); + entry.insert(KIO::UDSEntry::UDS_SIZE, 0); + entry.insert(KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH); + + return entry; +} + +void PlacesProtocol::stat(const QUrl &url) +{ + // we only have a root folder + if (url.path() != QLatin1String("/")) { + error(KIO::ERR_DOES_NOT_EXIST, url.toDisplayString()); + return; + } + + statEntry(rootEntry()); + finished(); +} + +void PlacesProtocol::listDir(const QUrl &url) +{ + // we only have a root folder + if (url.path() != QLatin1String("/")) { + error(KIO::ERR_DOES_NOT_EXIST, url.toDisplayString()); + return; + } + + listEntry(rootEntry()); + + for (int i = 0; i < m_places->rowCount(); ++i) { + QModelIndex idx = m_places->index(i, 0); + + const QString &title = idx.data(Qt::DisplayRole).toString(); + const QIcon &icon = idx.data(Qt::DecorationRole).value(); + const QUrl &url = idx.data(KFilePlacesModel::UrlRole).toUrl(); + const bool hidden = idx.data(KFilePlacesModel::HiddenRole).toBool(); + + KIO::UDSEntry entry; + entry.insert(KIO::UDSEntry::UDS_NAME, KIO::encodeFileName(title)); + entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); + entry.insert(KIO::UDSEntry::UDS_URL, url.toString()); + entry.insert(KIO::UDSEntry::UDS_ACCESS, 0500); + entry.insert(KIO::UDSEntry::UDS_ICON_NAME, icon.name()); + + if (url.isLocalFile()) { + entry.insert(KIO::UDSEntry::UDS_LOCAL_PATH, url.toLocalFile()); + } + + if (hidden) { + entry.insert(KIO::UDSEntry::UDS_HIDDEN, 1); + } + + listEntry(entry); + } + + finished(); +} diff --git a/kioslave/places/places.protocol b/kioslave/places/places.protocol new file mode 100644 --- /dev/null +++ b/kioslave/places/places.protocol @@ -0,0 +1,15 @@ +[Protocol] +exec=kio_places +protocol=places +input=none +output=filesystem +listing=Name,Type,Size,Date,Access,Owner,Group,Link, +reading=true +writing=false +makedir=false +deleting=false +linking=false +moving=false +Icon=folder-bookmark +maxInstances=2 +Class=:local