File indexing completed on 2024-05-12 05:10:10

0001 /***************************************************************************
0002     Copyright (C) 2024 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include <config.h>
0026 
0027 #include "filereadervideo.h"
0028 #include "../fieldformat.h"
0029 #include "../images/imagefactory.h"
0030 #include "../core/netaccess.h"
0031 #include "../tellico_debug.h"
0032 
0033 #include <KLocalizedString>
0034 #include <KFileItem>
0035 
0036 #include <QPixmap>
0037 #include <QIcon>
0038 
0039 namespace {
0040   static const int FILE_PREVIEW_SIZE = 128;
0041 }
0042 
0043 using Tellico::FileReaderVideo;
0044 
0045 class FileReaderVideo::Private {
0046 public:
0047   Private() {}
0048 
0049   // cache the icon image ids to avoid repeated creation of Data::Image objects
0050   QHash<QString, QString> iconImageId;
0051 };
0052 
0053 FileReaderVideo::FileReaderVideo(const QUrl& url_) : FileReaderMetaData(url_), d(new Private) {
0054 }
0055 
0056 FileReaderVideo::~FileReaderVideo() = default;
0057 
0058 bool FileReaderVideo::populate(Data::EntryPtr entry, const KFileItem& item) {
0059   // reads pdf and ebooks
0060   if(!item.mimetype().startsWith(QLatin1String("video"))) {
0061     return false;
0062   }
0063 #ifndef HAVE_KFILEMETADATA
0064   return false;
0065 #else
0066   bool isEmpty = true;
0067   QStringList genres, keywords;
0068   const auto props = properties(item);
0069   for(auto it = props.constBegin(); it != props.constEnd(); ++it) {
0070     const QString value = it.value().toString();
0071     if(value.isEmpty()) continue;
0072     switch(it.key()) {
0073       case KFileMetaData::Property::Title:
0074         isEmpty = false; // require a title or author
0075         entry->setField(QStringLiteral("title"), value);
0076         break;
0077 
0078       case KFileMetaData::Property::Subject:
0079         keywords += value;
0080         break;
0081 
0082       case KFileMetaData::Property::Genre:
0083         genres += value;
0084         break;
0085 
0086       case KFileMetaData::Property::ReleaseYear:
0087         entry->setField(QStringLiteral("year"), value);
0088         break;
0089 
0090       case KFileMetaData::Property::AspectRatio:
0091         entry->setField(QStringLiteral("aspect-ratio"), value);
0092         break;
0093 
0094       case KFileMetaData::Property::Duration:
0095         entry->setField(QStringLiteral("running-time"), QString::number(value.toInt()/60));
0096         break;
0097 
0098       case KFileMetaData::Property::Description:
0099         entry->setField(QStringLiteral("plot"), value);
0100         break;
0101 
0102       default:
0103         if(!value.isEmpty()) {
0104           myDebug() << "skipping" << it.key() << it.value();
0105         }
0106         break;
0107     }
0108   }
0109 
0110   if(isEmpty) return false;
0111 
0112   if(!genres.isEmpty()) {
0113     entry->setField(QStringLiteral("genre"), genres.join(FieldFormat::delimiterString()));
0114   }
0115   if(!keywords.isEmpty()) {
0116     entry->setField(QStringLiteral("keyword"), keywords.join(FieldFormat::delimiterString()));
0117   }
0118 
0119   const QString url = QStringLiteral("url");
0120   if(!entry->collection()->hasField(url)) {
0121     Data::FieldPtr f(new Data::Field(url, i18n("URL"), Data::Field::URL));
0122     f->setCategory(i18n("Personal"));
0123     entry->collection()->addField(f);
0124   }
0125   entry->setField(url, item.url().url());
0126 
0127   const QString cover = QStringLiteral("cover");
0128   QPixmap pixmap;
0129   if(useFilePreview()) {
0130     pixmap = Tellico::NetAccess::filePreview(item, FILE_PREVIEW_SIZE);
0131   }
0132   if(pixmap.isNull()) {
0133     if(d->iconImageId.contains(item.iconName())) {
0134       entry->setField(cover, d->iconImageId.value(item.iconName()));
0135     } else {
0136       pixmap = QIcon::fromTheme(item.iconName()).pixmap(QSize(FILE_PREVIEW_SIZE, FILE_PREVIEW_SIZE));
0137       const QString id = ImageFactory::addImage(pixmap, QStringLiteral("PNG"));
0138       if(!id.isEmpty()) {
0139         entry->setField(cover, id);
0140         d->iconImageId.insert(item.iconName(), id);
0141       }
0142     }
0143   } else {
0144     const QString id = ImageFactory::addImage(pixmap, QStringLiteral("PNG"));
0145     if(!id.isEmpty()) {
0146       entry->setField(cover, id);
0147     }
0148   }
0149 
0150 #endif
0151   return true;
0152 }