File indexing completed on 2024-05-12 16:36:48

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KPrSoundCollection.h"
0021 
0022 #include "KPrSoundData.h"
0023 #include "StageDebug.h"
0024 
0025 #include <KoStoreDevice.h>
0026 #include <KoXmlWriter.h>
0027 
0028 #include <QMimeDatabase>
0029 #include <QList>
0030 
0031 
0032 class Q_DECL_HIDDEN KPrSoundCollection::Private
0033 {
0034 public:
0035     QList<KPrSoundData*> sounds;
0036 };
0037 
0038 KPrSoundCollection::KPrSoundCollection(QObject *parent)
0039     : QObject(parent),
0040     d(new Private())
0041 {
0042 }
0043 
0044 KPrSoundCollection::~KPrSoundCollection() {
0045     delete d;
0046 }
0047 
0048 void KPrSoundCollection::addSound(KPrSoundData *image) {
0049     d->sounds.append(new KPrSoundData(*image));
0050 }
0051 
0052 void KPrSoundCollection::removeSound(KPrSoundData *image) {
0053     foreach(KPrSoundData *data, d->sounds) {
0054         if(data->operator==(*image)) {
0055             d->sounds.removeAll(data);
0056             delete data;
0057         }
0058     }
0059 }
0060 
0061 KPrSoundData *KPrSoundCollection::findSound(const QString &title)
0062 {
0063     foreach(KPrSoundData* sound, d->sounds) {
0064         if (sound->title() == title) {
0065             return sound;
0066         }
0067     }
0068     return 0;
0069 }
0070 
0071 QStringList KPrSoundCollection::titles()
0072 {
0073     QStringList list;
0074     list.reserve(d->sounds.size());
0075 
0076     foreach(KPrSoundData* sound, d->sounds) {
0077         list << sound->title();
0078     }
0079     return list;
0080 }
0081 
0082 // TODO move to loading of the actual element using the sound
0083 bool KPrSoundCollection::completeLoading(KoStore *store)
0084 {
0085     foreach(KPrSoundData *sound, d->sounds) {
0086         if(! store->open(sound->storeHref()))
0087             return false;
0088         bool ok = sound->loadFromFile(new KoStoreDevice(store));
0089         store->close();
0090         if(! ok) {
0091             return false;
0092         }
0093     }
0094     return true;
0095 }
0096 
0097 // use a KoSharedSavingData in the context to save which sounds need to be saved
0098 bool KPrSoundCollection::completeSaving(KoStore *store, KoXmlWriter * manifestWriter, KoShapeSavingContext * context )
0099 {
0100     Q_UNUSED(context);
0101     foreach(KPrSoundData *sound, d->sounds) {
0102         if(sound->isTaggedForSaving())
0103         {
0104             if(! store->open(sound->storeHref()))
0105                 return false;
0106             bool ok = sound->saveToFile(new KoStoreDevice(store));
0107             store->close();
0108             if(! ok)
0109                 return false;
0110             // TODO: can't we get the mimetype from elsewhere? e.g. already when loading? or from data?
0111             const QString mimetype( QMimeDatabase().mimeTypesForFileName(sound->storeHref()).first().name() );
0112             manifestWriter->addManifestEntry( sound->storeHref(), mimetype );
0113         }
0114     }
0115     return true;
0116 }
0117