File indexing completed on 2024-05-05 04:19:19

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2020 Méven Car <meven.car@kdemail.net>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0019 
0020 */
0021 #include "slideshowfileitemaction.h"
0022 
0023 #include <QMenu>
0024 #include <QMimeDatabase>
0025 
0026 #include <KFileItem>
0027 #include <KIO/CommandLauncherJob>
0028 #include <KLocalizedString>
0029 #include <KNotificationJobUiDelegate>
0030 #include <KPluginFactory>
0031 #include <QDir>
0032 
0033 K_PLUGIN_CLASS_WITH_JSON(SlideShowFileItemAction, "slideshowfileitemaction.json")
0034 
0035 SlideShowFileItemAction::SlideShowFileItemAction(QObject *parent, const QVariantList &)
0036     : KAbstractFileItemActionPlugin(parent)
0037 {
0038 }
0039 
0040 QList<QAction *> SlideShowFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
0041 {
0042     QStringList itemsForSlideShow;
0043     QString text;
0044     const KFileItemList &fileItems = fileItemInfos.items();
0045     if (fileItemInfos.isDirectory()) {
0046         if (fileItemInfos.isLocal()) {
0047             QMimeDatabase db;
0048             // filter selected dirs containing at least one image
0049             for (const KFileItem &dirItem : fileItems) {
0050                 QDir dir = QDir(dirItem.localPath());
0051                 const QStringList fileList = dir.entryList(QDir::Filter::Files);
0052                 const bool containsAtLeastAnImage = std::any_of(fileList.cbegin(), fileList.cend(), [&db, &dir](const QString &fileName) {
0053                     const auto mimeType = db.mimeTypeForFile(dir.absoluteFilePath(fileName), QMimeDatabase::MatchExtension);
0054                     return mimeType.name().startsWith(QStringLiteral("image"));
0055                 });
0056                 if (containsAtLeastAnImage) {
0057                     itemsForSlideShow << dirItem.localPath();
0058                 }
0059             }
0060         }
0061         text = i18nc("@action:inmenu Start a slideshow Dolphin context menu", "Start a Slideshow");
0062     } else if (fileItemInfos.mimeGroup() == QLatin1String("image") && fileItems.length() > 1) {
0063         for (const KFileItem &fileItem : fileItems) {
0064             itemsForSlideShow << fileItem.url().toString();
0065         }
0066         text = i18nc("@action:inmenu Start a slideshow Dolphin context menu", "Start a Slideshow with selected images");
0067     }
0068     if (itemsForSlideShow.isEmpty()) {
0069         return {};
0070     }
0071 
0072     auto startSlideShowAction = new QAction(text, parentWidget);
0073     startSlideShowAction->setIcon(QIcon::fromTheme(QStringLiteral("gwenview")));
0074 
0075     connect(startSlideShowAction, &QAction::triggered, this, [=]() {
0076         // gwenview -s %u
0077         auto job = new KIO::CommandLauncherJob(QStringLiteral("gwenview"), QStringList(QStringLiteral("-s")) << itemsForSlideShow);
0078         job->setDesktopName(QStringLiteral("org.kde.gwenview"));
0079         job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled));
0080 
0081         job->start();
0082     });
0083 
0084     return {startSlideShowAction};
0085 }
0086 
0087 #include "slideshowfileitemaction.moc"
0088 
0089 #include "moc_slideshowfileitemaction.cpp"