From 420541441c7713493064e877bb4bfedcc92bbb8f Mon Sep 17 00:00:00 2001 From: Scott Wheeler Date: Wed, 23 Jun 2004 18:35:52 +0000 Subject: [PATCH] Switch the return type of File::save() from void to bool to provide a little bit of feedback on whether or not the safe operation worked. CCMAIL:83882@bugs.kde.org git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@323165 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- mpeg/mpegfile.cpp | 35 +++++++++++++++++++---------------- mpeg/mpegfile.h | 11 ++++++----- ogg/oggfile.cpp | 9 ++++++++- ogg/oggfile.h | 2 +- ogg/vorbis/vorbisfile.cpp | 4 ++-- ogg/vorbis/vorbisfile.h | 2 +- toolkit/tfile.h | 4 ++-- 7 files changed, 39 insertions(+), 28 deletions(-) diff --git a/mpeg/mpegfile.cpp b/mpeg/mpegfile.cpp index 196e8552..4ec62102 100644 --- a/mpeg/mpegfile.cpp +++ b/mpeg/mpegfile.cpp @@ -245,29 +245,27 @@ MPEG::Properties *MPEG::File::audioProperties() const return d->properties; } -void MPEG::File::save() +bool MPEG::File::save() { - save(ID3v1 | ID3v2); + return save(ID3v1 | ID3v2); } -void MPEG::File::save(int tags) +bool MPEG::File::save(int tags) { - if(tags == NoTags) { - strip(AllTags); - return; - } + if(tags == NoTags) + return strip(AllTags); if(!d->ID3v2Tag && !d->ID3v1Tag) { if(d->hasID3v1 || d->hasID3v2) - strip(AllTags); + return strip(AllTags); - return; + return true; } if(readOnly()) { debug("MPEG::File::save() -- File is read only."); - return; + return false; } // Create the tags if we've been asked to. Copy the values from the tag that @@ -279,6 +277,7 @@ void MPEG::File::save(int tags) if(tags & ID3v1 && d->ID3v2Tag) Tag::duplicate(d->ID3v2Tag, ID3v1Tag(true), false); + bool success = true; if(ID3v2 & tags) { @@ -290,10 +289,10 @@ void MPEG::File::save(int tags) insert(d->ID3v2Tag->render(), d->ID3v2Location, d->ID3v2OriginalSize); } else - strip(ID3v2); + success = strip(ID3v2) && success; } else if(d->hasID3v2) - strip(ID3v2); + success = strip(ID3v2) && success; if(ID3v1 & tags) { if(d->ID3v1Tag && !d->ID3v1Tag->isEmpty()) { @@ -302,10 +301,12 @@ void MPEG::File::save(int tags) writeBlock(d->ID3v1Tag->render()); } else - strip(ID3v1); + success = strip(ID3v1) && success; } else if(d->hasID3v1) - strip(ID3v1); + success = strip(ID3v1) && success; + + return success; } ID3v2::Tag *MPEG::File::ID3v2Tag(bool create) @@ -330,11 +331,11 @@ ID3v1::Tag *MPEG::File::ID3v1Tag(bool create) return d->ID3v1Tag; } -void MPEG::File::strip(int tags) +bool MPEG::File::strip(int tags) { if(readOnly()) { debug("MPEG::File::strip() - Cannot strip tags from a read only file."); - return; + return false; } if(tags & ID3v2 && d->hasID3v2) @@ -345,6 +346,8 @@ void MPEG::File::strip(int tags) d->ID3v1Location = -1; d->hasID3v1 = false; } + + return true; } void MPEG::File::setID3v2FrameFactory(const ID3v2::FrameFactory *factory) diff --git a/mpeg/mpegfile.h b/mpeg/mpegfile.h index c70ca272..4b53ada2 100644 --- a/mpeg/mpegfile.h +++ b/mpeg/mpegfile.h @@ -114,7 +114,8 @@ namespace TagLib { /*! * Save the file. If at least one tag -- ID3v1 or ID3v2 -- exists this - * will duplicate its content into the other tag. + * will duplicate its content into the other tag. This returns true + * if saving was successful. * * If neither exists or if both tags are empty, this will strip the tags * from the file. @@ -126,18 +127,18 @@ namespace TagLib { * * \see save(int tags) */ - virtual void save(); + virtual bool save(); /*! * Save the file. This will attempt to save all of the tag types that are * specified by OR-ing together TagTypes values. The save() method above - * uses AllTags. + * uses AllTags. This returns true if saving was successful. * * This strips all tags not included in the mask, but does not modify them * in memory, so later calls to save() which make use of these tags will * remain valid. This also strips empty tags. */ - void save(int tags); + bool save(int tags); /*! * Returns a pointer to the ID3v2 tag of the file. @@ -169,7 +170,7 @@ namespace TagLib { * This will strip the tags that match the OR-ed together TagTypes from the * file. By default it strips all tags. */ - void strip(int tags = AllTags); + bool strip(int tags = AllTags); /*! * Set the ID3v2::FrameFactory to something other than the default. diff --git a/ogg/oggfile.cpp b/ogg/oggfile.cpp index 07340479..357a30ed 100644 --- a/ogg/oggfile.cpp +++ b/ogg/oggfile.cpp @@ -176,8 +176,13 @@ const Ogg::PageHeader *Ogg::File::lastPageHeader() return d->lastPageHeader->isValid() ? d->lastPageHeader : 0; } -void Ogg::File::save() +bool Ogg::File::save() { + if(readOnly()) { + debug("Ogg::File::save() - Cannot save to a read only file."); + return false; + } + List pageGroup; for(List::ConstIterator it = d->dirtyPages.begin(); it != d->dirtyPages.end(); ++it) { @@ -191,6 +196,8 @@ void Ogg::File::save() writePageGroup(pageGroup); d->dirtyPages.clear(); d->dirtyPackets.clear(); + + return true; } //////////////////////////////////////////////////////////////////////////////// diff --git a/ogg/oggfile.h b/ogg/oggfile.h index 29a0f650..0ca59dd9 100644 --- a/ogg/oggfile.h +++ b/ogg/oggfile.h @@ -73,7 +73,7 @@ namespace TagLib { */ const PageHeader *lastPageHeader(); - virtual void save(); + virtual bool save(); protected: /*! diff --git a/ogg/vorbis/vorbisfile.cpp b/ogg/vorbis/vorbisfile.cpp index 1b6b41fc..a6e8b94c 100644 --- a/ogg/vorbis/vorbisfile.cpp +++ b/ogg/vorbis/vorbisfile.cpp @@ -79,7 +79,7 @@ Vorbis::Properties *Vorbis::File::audioProperties() const return d->properties; } -void Vorbis::File::save() +bool Vorbis::File::save() { ByteVector v(vorbisCommentHeaderID); @@ -89,7 +89,7 @@ void Vorbis::File::save() setPacket(1, v); - Ogg::File::save(); + return Ogg::File::save(); } //////////////////////////////////////////////////////////////////////////////// diff --git a/ogg/vorbis/vorbisfile.h b/ogg/vorbis/vorbisfile.h index cc1e9f9c..f85e4bee 100644 --- a/ogg/vorbis/vorbisfile.h +++ b/ogg/vorbis/vorbisfile.h @@ -83,7 +83,7 @@ namespace TagLib { */ virtual Properties *audioProperties() const; - virtual void save(); + virtual bool save(); private: File(const File &); diff --git a/toolkit/tfile.h b/toolkit/tfile.h index 0c48e565..bde30d20 100644 --- a/toolkit/tfile.h +++ b/toolkit/tfile.h @@ -79,9 +79,9 @@ namespace TagLib { /*! * Save the file and its associated tags. This should be reimplemented in - * the concrete subclasses. + * the concrete subclasses. Returns true if the save succeeds. */ - virtual void save() = 0; + virtual bool save() = 0; /*! * Reads a block of size \a length at the current get pointer.