diff --git a/stage/part/KPrSoundData.cpp b/stage/part/KPrSoundData.cpp index 524ed127722..2899633b5b7 100644 --- a/stage/part/KPrSoundData.cpp +++ b/stage/part/KPrSoundData.cpp @@ -1,191 +1,191 @@ /* This file is part of the KDE project * Copyright (C) 2007 Thomas Zander * Copyright (C) 2007 Jan Hambrecht * Copyright (C) 2008 C. Boemann * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KPrSoundData.h" #include "KPrSoundCollection.h" #include #include #include #include #include // make it a QSharedData class KPrSoundData::Private { public: Private(KPrSoundCollection *c) : refCount(0) , collection(c) , tempFile(0) , taggedForSaving(false) { } ~Private() { delete tempFile; } QString tempFileName; QString title; int refCount; QString storeHref; KPrSoundCollection *collection; KTemporaryFile *tempFile; bool taggedForSaving; }; -KPrSoundData::KPrSoundData(KPrSoundCollection *collection, QString href) +KPrSoundData::KPrSoundData(KPrSoundCollection *collection, const QString &href) : d(new Private(collection)) { Q_ASSERT(collection); collection->addSound(this); d->storeHref = href; //TODO make sure the title is not duplicated d->title = href.section('/', -1); // TODO only works on linux like filenames Q_ASSERT(d->refCount == 1); } KPrSoundData::KPrSoundData(const KPrSoundData &soundData) : d(soundData.d) { d->refCount++; } KPrSoundData::~KPrSoundData() { if(--d->refCount == 0) { d->collection->removeSound(this); delete d; } } bool KPrSoundData::operator==(const KPrSoundData &other) const { return other.d == d; } QString KPrSoundData::tagForSaving() { d->taggedForSaving=true; d->storeHref = QString("Sounds/%1").arg(d->title); return d->storeHref; } QString KPrSoundData::storeHref() const { return d->storeHref; } QString KPrSoundData::nameOfTempFile() const { return d->tempFileName; } QString KPrSoundData::title() const { return d->title; } bool KPrSoundData::saveToFile(QIODevice *device) { if(! d->tempFile->open()) return false; char * data = new char[32 * 1024]; while(true) { bool failed = false; qint64 bytes = d->tempFile->read(data, 32*1024); if(bytes == 0) break; else if(bytes == -1) { kWarning() << "Failed to read data from the tmpfile"; failed = true; } while(! failed && bytes > 0) { qint64 written = device->write(data, bytes); if(written < 0) {// error! kWarning() << "Failed to copy the sound from the temp file"; failed = true; } bytes -= written; } if(failed) { // read or write failed; so lets cleanly abort. delete[] data; return false; } } delete[] data; return true; } bool KPrSoundData::isTaggedForSaving() { return d->taggedForSaving; } bool KPrSoundData::loadFromFile(QIODevice *device) { struct Finally { Finally(QIODevice *d) : device (d), bytes(0) {} ~Finally() { delete device; delete[] bytes; } QIODevice *device; char *bytes; }; Finally finally(device); // remove prev data delete d->tempFile; d->tempFile = 0; d->tempFile = new KTemporaryFile(); if(! d->tempFile->open()) return false; char * data = new char[32 * 1024]; finally.bytes = data; while(true) { bool failed = false; qint64 bytes = device->read(data, 32*1024); if(bytes == 0) break; else if(bytes == -1) { kWarning() << "Failed to read sound data"; failed = true; } while(! failed && bytes > 0) { qint64 written = d->tempFile->write(data, bytes); if(written < 0) {// error! kWarning() << "Failed to copy the sound to temp"; failed = true; } bytes -= written; } if(failed) { // read or write failed; so lets cleanly abort. delete d->tempFile; d->tempFile = 0; return false; } } d->tempFileName = d->tempFile->fileName(); d->tempFile->close(); return true; } KPrSoundCollection * KPrSoundData::soundCollection() { return d->collection; } diff --git a/stage/part/KPrSoundData.h b/stage/part/KPrSoundData.h index 4cc9016af5d..36c3c9a7638 100644 --- a/stage/part/KPrSoundData.h +++ b/stage/part/KPrSoundData.h @@ -1,116 +1,116 @@ /* This file is part of the KDE project * Copyright (C) 2007 Thomas Zander * Copyright (C) 2007 Jan Hambrecht * Copyright (C) 2008 C. Boemann * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KPRSOUNDDATA_H #define KPRSOUNDDATA_H #include #include "stage_export.h" class KPrSoundCollection; class QIODevice; /** * Class meant to hold sound data so it can be shared between shapes. * In KPresenter shapes can have click actions attached to them. One such action is playing sound. * The binary data for those sounds are saved in this class. */ /* * TODO needs a file for playing, store it as a tmp file */ class STAGE_EXPORT KPrSoundData { public: /** * The storage location */ enum StorageLocation { SaveRelativeUrl, ///< in the odf use a relative (to document) xlink:href, if possible SaveAbsoluteUrl, ///< in the odf use a fully specified xlink:href SaveInStore ///< Save the sound data in the ODF store }; /** * constructor * @param collection the sound collection which will do the loading of the sound data for us. * @param href the url of the sound in the store. */ - explicit KPrSoundData(KPrSoundCollection *collection, QString href=""); + explicit KPrSoundData(KPrSoundCollection *collection, const QString &href = QString()); /** * copy constructor using ref-counting. * @param soundData the other one. */ KPrSoundData(const KPrSoundData &soundData); /// destructor ~KPrSoundData(); /** * Tags this sound to be saved and returns the href for reference in the xml. * @return returns the url-like location this sound will be saved to. */ QString tagForSaving(); /// returns the url-like location QString storeHref() const; /// returns the url-like location of the tmp file QString nameOfTempFile() const; /// returns the title of the sound (for now its the basename part of the filename QString title() const; /** * Load the sound data from the param device. * Note that it will copy the data to a temp-file and postpone loading it until the phonon plays it. * @para device the device that is used to get the data from. * @return returns true if load was successful. */ bool loadFromFile(QIODevice *device); /** * Save the sound data to the param device. * The full file is saved. * @para device the device that is used to get the data from. * @return returns true if load was successful. */ bool saveToFile(QIODevice *device); /** * Return whether this sound have been tagged for saving. * @return returns true if this sound should be saved. */ bool isTaggedForSaving(); bool operator==(const KPrSoundData &other) const; /** * Get the collection used */ KPrSoundCollection * soundCollection(); private: class Private; Private * const d; }; #endif