diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -2,7 +2,7 @@ ecm_add_test( urltest.cpp ../src/gdriveurl.cpp - LINK_LIBRARIES Qt5::Test + LINK_LIBRARIES Qt5::Test KF5::KIOCore TEST_NAME urltest NAME_PREFIX kio_gdrive-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,3 +43,21 @@ ${BACKEND_LIBS}) set_target_properties(kio_gdrive PROPERTIES OUTPUT_NAME "gdrive") + +set(copyurlitemaction_SRCS + gdriveurl.cpp + copyurlitemaction.cpp) + +ecm_qt_declare_logging_category(copyurlitemaction_SRCS + HEADER gdrivedebug.h + IDENTIFIER GDRIVE + CATEGORY_NAME kf5.kio.gdrive) + +kcoreaddons_add_plugin(copyurlitemaction + SOURCES ${copyurlitemaction_SRCS} + 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,43 @@ +/* + * 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 QUrl& url); + +}; + +#endif diff --git a/src/copyurlitemaction.cpp b/src/copyurlitemaction.cpp new file mode 100644 --- /dev/null +++ b/src/copyurlitemaction.cpp @@ -0,0 +1,85 @@ +/* + * 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 "gdriveurl.h" +#include "gdrivedebug.h" + +#include +#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.urlList().size() != 1) { + qCDebug(GDRIVE) << "Not showing, too many files selected"; + return {}; + } + + // Ignore if not a Google Drive url + QUrl url = fileItemInfos.urlList()[0]; + if (url.scheme() != GDriveUrl::Scheme) { + qCDebug(GDRIVE) << "Not showing, not GDriveUrl"; + return {}; + } + + QAction *copyUrlAction = createCopyUrlAction(parentWidget, fileItemInfos.urlList()[0]); + return { copyUrlAction }; +} + + +QAction *CopyUrlItemAction::createCopyUrlAction(QWidget *parent, const QUrl& url) +{ + const QString name = i18n("Copy Drive url to clipboard"); + const QIcon icon = QIcon::fromTheme(QStringLiteral("folder-gdrive")); + QAction *action = new QAction(icon, name, parent); + + connect(action, &QAction::triggered, this, [url]() { + qCDebug(GDRIVE) << "Getting gdrive web url for" << url; + KIO::StatJob *statJob = KIO::stat(url, KIO::HideProgressInfo); + if (statJob->exec()) { + const KIO::UDSEntry entry = statJob->statResult(); + const QString gdriveLink = entry.stringValue(GDriveUrl::UDS_ALTERNATE_LINK_FIELD); + qCDebug(GDRIVE) << "Copying" << gdriveLink << "to clipboard"; + QGuiApplication::clipboard()->setText(gdriveLink); + } + else { + qCDebug(GDRIVE) << "Failed getting Google Drive url, error:" << statJob->errorString(); + } + }); + + 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,15 @@ +{ + "KPlugin": { + "Icon": "folder-gdrive", + "MimeTypes": [ + "application/octet-stream", + "inode/directory" + ], + "Name": "'Copy Drive url to clipboard' action", + "Name[it]": "Azione 'Copia url Google Drive'", + "ServiceTypes": [ + "KFileItemAction/Plugin" + ] + }, + "MimeType": "application/octet-stream;inode/directory;" +} diff --git a/src/gdriveurl.h b/src/gdriveurl.h --- a/src/gdriveurl.h +++ b/src/gdriveurl.h @@ -24,9 +24,14 @@ #include +#include + class GDriveUrl { public: + + static const uint UDS_ALTERNATE_LINK_FIELD = KIO::UDSEntry::UDS_EXTRA; + explicit GDriveUrl(const QUrl &url); QString account() const; 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(GDriveUrl::UDS_ALTERNATE_LINK_FIELD, file->alternateLink().toString()); + return entry; }