diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,3 +43,12 @@ ${BACKEND_LIBS}) set_target_properties(kio_gdrive PROPERTIES OUTPUT_NAME "gdrive") + +kcoreaddons_add_plugin(copyurlitemaction + SOURCES copyurlitemaction.cpp + JSON copyurlitemaction.json + INSTALL_NAMESPACE "kf5/kfileitemaction") + +target_link_libraries(copyurlitemaction + KF5::I18n + KF5::KIOWidgets) diff --git a/src/copyurlitemaction.h b/src/copyurlitemaction.h new file mode 100644 --- /dev/null +++ b/src/copyurlitemaction.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2019 David Barchiesi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#ifndef COPYURLITEMACTION_H +#define COPYURLITEMACTION_H + +#include +#include + +#include +#include + + +class CopyUrlItemAction : public KAbstractFileItemActionPlugin +{ + + Q_OBJECT + + public: + CopyUrlItemAction(QObject* parent, const QVariantList& args); + QList actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) override; + + private: + QAction *createCopyUrlAction(QWidget *parent, const QString& gdriveLink); + +}; + +#endif diff --git a/src/copyurlitemaction.cpp b/src/copyurlitemaction.cpp new file mode 100644 --- /dev/null +++ b/src/copyurlitemaction.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2019 David Barchiesi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include "copyurlitemaction.h" + +#include +#include +#include + +#include +#include +#include +#include + + +K_PLUGIN_CLASS_WITH_JSON(CopyUrlItemAction, "copyurlitemaction.json") + +CopyUrlItemAction::CopyUrlItemAction(QObject* parent, const QVariantList&) + : KAbstractFileItemActionPlugin(parent) +{} + +QList CopyUrlItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) +{ + // Ignore if more than one file is selected + if (fileItemInfos.items().size() != 1) { + return {}; + } + + const KFileItem item = fileItemInfos.items().at(0); + + // Ignore if not a Google Drive url + if (item.url().scheme() != "gdrive") { + return {}; + } + + const KIO::UDSEntry entry = item.entry(); + const QString gdriveLink = entry.stringValue(KIO::UDSEntry::UDS_EXTRA); + // Ignore if missing a shareable link + if (gdriveLink.isEmpty()) { + return {}; + } + + QAction *copyUrlAction = createCopyUrlAction(parentWidget, gdriveLink); + return { copyUrlAction }; +} + + +QAction *CopyUrlItemAction::createCopyUrlAction(QWidget *parent, const QString& gdriveLink) +{ + const QString name = i18n("Copy Google URL to clipboard"); + const QIcon icon = QIcon::fromTheme(QStringLiteral("folder-gdrive")); + QAction *action = new QAction(icon, name, parent); + + connect(action, &QAction::triggered, this, [gdriveLink]() { + QApplication::clipboard()->setText(gdriveLink); + }); + + return action; +} + + +#include "copyurlitemaction.moc" diff --git a/src/copyurlitemaction.json b/src/copyurlitemaction.json new file mode 100644 --- /dev/null +++ b/src/copyurlitemaction.json @@ -0,0 +1,14 @@ +{ + "KPlugin": { + "Icon": "folder-gdrive", + "MimeTypes": [ + "application/octet-stream", + "inode/directory" + ], + "Name": "'Copy Drive url to clipboard' action", + "ServiceTypes": [ + "KFileItemAction/Plugin" + ] + }, + "MimeType": "application/octet-stream;inode/directory;" +} diff --git a/src/kio_gdrive.cpp b/src/kio_gdrive.cpp --- a/src/kio_gdrive.cpp +++ b/src/kio_gdrive.cpp @@ -231,6 +231,8 @@ entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH); } + entry.fastInsert(KIO::UDSEntry::UDS_EXTRA, file->alternateLink().toString()); + return entry; } @@ -471,6 +473,7 @@ QStringList({ KGAPI2::Drive::File::Fields::Labels, KGAPI2::Drive::File::Fields::ExportLinks, KGAPI2::Drive::File::Fields::LastViewedByMeDate, + KGAPI2::Drive::File::Fields::AlternateLink, }); fileFetchJob.setFields(KGAPI2::Drive::FileFetchJob::FieldShorthands::BasicFields + extraFields); if (!runJob(fileFetchJob, url, accountId)) {