File indexing completed on 2024-05-12 16:35:05
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2007, 2009 Thomas Zander <zander@kde.org> 0003 * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org> 0004 * Copyright (C) 2009 C. Boemann <cbo@boemann.dk> 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Library General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2 of the License, or (at your option) any later version. 0010 * 0011 * This library 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 GNU 0014 * Library General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Library General Public License 0017 * along with this library; see the file COPYING.LIB. If not, write to 0018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 * Boston, MA 02110-1301, USA. 0020 */ 0021 #include "VideoCollection.h" 0022 0023 #include "VideoData.h" 0024 #include "VideoDebug.h" 0025 0026 #include "KoShapeSavingContext.h" 0027 #include <KoStoreDevice.h> 0028 #include <QCryptographicHash> 0029 #include <KoXmlWriter.h> 0030 0031 #include <QMap> 0032 #include <QUrl> 0033 #include <QMimeDatabase> 0034 #include <QMimeType> 0035 0036 0037 class VideoCollection::Private 0038 { 0039 public: 0040 ~Private() 0041 { 0042 } 0043 0044 QMap<qint64, VideoData*> videos; 0045 // an extra map to find all dataObjects based on the key of a store. 0046 QMap<QByteArray, VideoData*> storeVideos; 0047 }; 0048 0049 VideoCollection::VideoCollection(QObject *parent) 0050 : QObject(parent) 0051 , saveCounter(0) 0052 , d(new Private()) 0053 { 0054 } 0055 0056 VideoCollection::~VideoCollection() 0057 { 0058 foreach(VideoData *id, d->videos) { 0059 id->setCollection(0); 0060 } 0061 delete d; 0062 } 0063 0064 bool VideoCollection::completeLoading(KoStore *store) 0065 { 0066 Q_UNUSED( store ); 0067 d->storeVideos.clear(); 0068 return true; 0069 } 0070 0071 bool VideoCollection::completeSaving(KoStore *store, KoXmlWriter *manifestWriter, KoShapeSavingContext *context) 0072 { 0073 Q_UNUSED(context); 0074 QMap<qint64, VideoData *>::ConstIterator dataIt(d->videos.constBegin()); 0075 0076 while (dataIt != d->videos.constEnd()) { 0077 if (!dataIt.value()->saveName().isEmpty()) { 0078 VideoData *videoData = dataIt.value(); 0079 if (store->open(videoData->saveName())) { 0080 KoStoreDevice device(store); 0081 bool ok = videoData->saveData(device); 0082 store->close(); 0083 // TODO error handling 0084 if (ok) { 0085 QMimeDatabase db; 0086 const QString mimetype(db.mimeTypeForFile(videoData->saveName(), QMimeDatabase::MatchExtension).name()); 0087 manifestWriter->addManifestEntry(videoData->saveName(), mimetype); 0088 } else { 0089 warnVideo << "saving video failed"; 0090 } 0091 } else { 0092 warnVideo << "saving video failed: open store failed"; 0093 } 0094 dataIt.value()->setSaveName(QString()); 0095 } 0096 ++dataIt; 0097 } 0098 saveCounter=0; 0099 return true; 0100 } 0101 0102 VideoData *VideoCollection::createExternalVideoData(const QUrl &url, bool saveInternal) 0103 { 0104 Q_ASSERT(!url.isEmpty() && url.isValid()); 0105 0106 QCryptographicHash md5(QCryptographicHash::Md5); 0107 md5.addData(url.toEncoded().append(saveInternal ? "true" : "false")); 0108 qint64 key = VideoData::generateKey(md5.result()); 0109 0110 if (d->videos.contains(key)) { 0111 return new VideoData(*(d->videos.value(key))); 0112 } 0113 0114 VideoData *data = new VideoData(); 0115 data->setExternalVideo(url, saveInternal); 0116 data->setCollection(this); 0117 Q_ASSERT(data->key() == key); 0118 d->videos.insert(key, data); 0119 return data; 0120 } 0121 0122 VideoData *VideoCollection::createVideoData(const QString &href, KoStore *store) 0123 { 0124 // the tricky thing with a 'store' is that we need to read the data now 0125 // as the store will no longer be readable after the loading completed. 0126 // The solution we use is to read the data, store it in a QTemporaryFile 0127 // and read and parse it on demand when the video data is actually needed. 0128 // This leads to having two keys, one for the store and one for the 0129 // actual video data. We need the latter so if someone else gets the same 0130 // video data they can find this data and share (warm fuzzy feeling here) 0131 QByteArray storeKey = (QString::number((qint64) store) + href).toLatin1(); 0132 if (d->storeVideos.contains(storeKey)) 0133 return new VideoData(*(d->storeVideos.value(storeKey))); 0134 0135 VideoData *data = new VideoData(); 0136 data->setVideo(href, store); 0137 data->setCollection(this); 0138 0139 d->storeVideos.insert(storeKey, data); 0140 return data; 0141 } 0142 0143 int VideoCollection::size() const 0144 { 0145 return d->videos.count(); 0146 } 0147 0148 int VideoCollection::count() const 0149 { 0150 return d->videos.count(); 0151 } 0152 0153 void VideoCollection::removeOnKey(qint64 videoDataKey) 0154 { 0155 d->videos.remove(videoDataKey); 0156 }