From e806ca5f3b9e93ccf82e459673dea77fba56b369 Mon Sep 17 00:00:00 2001 From: fgerlits Date: Wed, 15 Jun 2005 13:38:52 +0000 Subject: [PATCH] added id3 tag recognition to UploadFileWindow --- livesupport/modules/core/src/AudioClip.cxx | 15 ++-- .../modules/core/src/AudioClipTest.cxx | 18 ++++- .../gLiveSupport/src/UploadFileWindow.cxx | 68 ++++++++++++------- .../gLiveSupport/src/UploadFileWindow.h | 23 ++----- 4 files changed, 76 insertions(+), 48 deletions(-) diff --git a/livesupport/modules/core/src/AudioClip.cxx b/livesupport/modules/core/src/AudioClip.cxx index 0ff8a7952..b05a27cbe 100644 --- a/livesupport/modules/core/src/AudioClip.cxx +++ b/livesupport/modules/core/src/AudioClip.cxx @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.27 $ + Version : $Revision: 1.28 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/AudioClip.cxx,v $ ------------------------------------------------------------------------------*/ @@ -610,11 +610,18 @@ AudioClip :: readTag(Ptr::Ref metadataTypes) throw std::invalid_argument("audio clip has no uri field"); } - if (!TagLib::File::isReadable(getUri()->c_str())) { + std::string uri = *getUri(); + if (uri.substr(0,7) == "file://") { + uri = uri.substr(7); + } else if (uri.substr(0,5) == "file:") { + uri = uri.substr(5); + } + + if (!TagLib::File::isReadable(uri.c_str())) { throw std::invalid_argument("binary sound file not found"); } - TagLib::MPEG::File mpegFile(getUri()->c_str()); + TagLib::MPEG::File mpegFile(uri.c_str()); TagLib::ID3v2::Tag* id3v2Tag = mpegFile.ID3v2Tag(); if (id3v2Tag) { Ptr::Ref metadata; @@ -641,7 +648,7 @@ AudioClip :: readTag(Ptr::Ref metadataTypes) return; } - TagLib::FileRef genericFileRef(getUri()->c_str()); + TagLib::FileRef genericFileRef(uri.c_str()); TagLib::Tag* tag = genericFileRef.tag(); if (tag) { TagLib::String stringValue; diff --git a/livesupport/modules/core/src/AudioClipTest.cxx b/livesupport/modules/core/src/AudioClipTest.cxx index 4412d9164..e1f6e0c90 100644 --- a/livesupport/modules/core/src/AudioClipTest.cxx +++ b/livesupport/modules/core/src/AudioClipTest.cxx @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.14 $ + Version : $Revision: 1.15 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/AudioClipTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -210,6 +210,7 @@ void AudioClipTest :: tagTest(void) throw (CPPUNIT_NS::Exception) { + // should work with either plain file path... Ptr::Ref uri(new std::string("var/test10001.mp3")); audioClip->setUri(uri); try { @@ -225,6 +226,21 @@ AudioClipTest :: tagTest(void) Ptr::Ref artist = audioClip->getMetadata("dc:creator"); CPPUNIT_ASSERT(*artist == "The Muppets"); + + // ... or with URI + uri.reset(new std::string("file:var/test10001.mp3")); + audioClip->setUri(uri); + try { + audioClip->readTag(metadataTypes); + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL(e.what()); + } + + title = audioClip->getMetadata("dc:title"); + CPPUNIT_ASSERT(*title == "Theme Song"); + + artist = audioClip->getMetadata("dc:creator"); + CPPUNIT_ASSERT(*artist == "The Muppets"); } diff --git a/livesupport/products/gLiveSupport/src/UploadFileWindow.cxx b/livesupport/products/gLiveSupport/src/UploadFileWindow.cxx index 0d5795625..9aa169b56 100644 --- a/livesupport/products/gLiveSupport/src/UploadFileWindow.cxx +++ b/livesupport/products/gLiveSupport/src/UploadFileWindow.cxx @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.10 $ + Version : $Revision: 1.11 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/gLiveSupport/src/UploadFileWindow.cxx,v $ ------------------------------------------------------------------------------*/ @@ -70,11 +70,10 @@ UploadFileWindow :: UploadFileWindow (Ptr::Ref gLiveSupport, : WhiteWindow("", Colors::White, WidgetFactory::getInstance()->getWhiteWindowCorners()), - LocalizedObject(bundle) + LocalizedObject(bundle), + gLiveSupport(gLiveSupport) { - this->gLiveSupport = gLiveSupport; - fileName.reset(new std::string()); - isFileGood = false; + isAudioClipValid = false; Ptr::Ref wf = WidgetFactory::getInstance(); @@ -224,29 +223,51 @@ UploadFileWindow :: onChooseFileButtonClicked(void) throw () void UploadFileWindow :: updateFileInfo(void) throw () { - std::string newFileName = fileNameEntry->get_text().raw(); + std::string fileName = fileNameEntry->get_text().raw(); + Ptr::Ref newUri(new std::string("file://")); + newUri->append(fileName); - if (*fileName != newFileName) { + if (!isAudioClipValid || + *audioClip->getUri() != *newUri) { // see if the file exists, and is readable - std::ifstream file(newFileName.c_str()); + std::ifstream file(fileName.c_str()); if (!file.good()) { - isFileGood = false; + isAudioClipValid = false; file.close(); return; } file.close(); - isFileGood = true; - fileName.reset(new std::string(newFileName)); - - fileURI.reset(new std::string("file://")); - *fileURI += *fileName; + isAudioClipValid = true; + Ptr::Ref playlength; try { playlength = readPlaylength(fileName); } catch (std::invalid_argument &e) { + statusBar->set_text(e.what()); playlength.reset(new time_duration(0,0,0,0)); } + Ptr::Ref tempTitle(new const Glib::ustring); + audioClip.reset(new AudioClip(tempTitle, playlength, newUri)); + + // read the id3 tags + try { + audioClip->readTag(gLiveSupport->getMetadataTypeContainer()); + } catch (std::invalid_argument &e) { + statusBar->set_text(e.what()); + isAudioClipValid = false; + return; + } + + titleEntry->set_text(*audioClip->getTitle()); + Ptr::Ref creator + = audioClip->getMetadata("dc:creator"); + creatorEntry->set_text(creator ? *creator : ""); + + Ptr::Ref genre + = audioClip->getMetadata("dc:type"); + genreEntry->set_text(genre ? *genre : ""); + // display the new play length std::ostringstream lengthStr; lengthStr << std::setfill('0') @@ -278,21 +299,15 @@ UploadFileWindow :: onUploadButtonClicked(void) throw () { try { updateFileInfo(); - if (!isFileGood) { + if (!isAudioClipValid) { // TODO: localize error message throw std::invalid_argument("file does not exist"); } - Ptr::Ref ustrValue; - - ustrValue.reset(new Glib::ustring(titleEntry->get_text())); - - // create and upload an AudioClip object - Ptr::Ref audioClip(new AudioClip(ustrValue, - playlength, - fileURI)); - // set the metadata available + Ptr::Ref ustrValue(new Glib::ustring( + titleEntry->get_text() )); + audioClip->setTitle(ustrValue); ustrValue.reset(new Glib::ustring(creatorEntry->get_text())); audioClip->setMetadata(ustrValue, "dc:creator"); ustrValue.reset(new Glib::ustring(genreEntry->get_text())); @@ -300,6 +315,7 @@ UploadFileWindow :: onUploadButtonClicked(void) throw () ustrValue.reset(new Glib::ustring( fileFormatComboBox->get_active_text())); audioClip->setMetadata(ustrValue, "dc:format"); + // TODO: is this really what we mean by dc:format? // upload the audio clip gLiveSupport->uploadFile(audioClip); @@ -335,10 +351,10 @@ UploadFileWindow :: onCloseButtonClicked(void) throw () * Determine the length of an audio file *----------------------------------------------------------------------------*/ Ptr::Ref -UploadFileWindow :: readPlaylength(Ptr::Ref fileName) +UploadFileWindow :: readPlaylength(const std::string & fileName) throw (std::invalid_argument) { - TagLib::FileRef fileRef(fileName->c_str()); + TagLib::FileRef fileRef(fileName.c_str()); TagLib::AudioProperties * audioProperties = fileRef.audioProperties(); if (audioProperties) { diff --git a/livesupport/products/gLiveSupport/src/UploadFileWindow.h b/livesupport/products/gLiveSupport/src/UploadFileWindow.h index 63bfe3071..3b5824a7a 100644 --- a/livesupport/products/gLiveSupport/src/UploadFileWindow.h +++ b/livesupport/products/gLiveSupport/src/UploadFileWindow.h @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.4 $ + Version : $Revision: 1.5 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/gLiveSupport/src/UploadFileWindow.h,v $ ------------------------------------------------------------------------------*/ @@ -87,7 +87,7 @@ using namespace LiveSupport::Widgets; * * * @author $Author: fgerlits $ - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ */ class UploadFileWindow : public WhiteWindow, public LocalizedObject { @@ -225,23 +225,12 @@ class UploadFileWindow : public WhiteWindow, public LocalizedObject /** * The name of the file to upload. */ - Ptr::Ref fileName; + Ptr::Ref audioClip; /** - * Signals if the file under fileName is good. + * Signals if the audio clip is valid. */ - bool isFileGood; - - /** - * The URI to the file to upload. - * Basically same as fileName, with 'file://' prepended. - */ - Ptr::Ref fileURI; - - /** - * The playling length of the file to upload. - */ - Ptr::Ref playlength; + bool isAudioClipValid; /** * Function to catch the event of the choose file button being @@ -289,7 +278,7 @@ class UploadFileWindow : public WhiteWindow, public LocalizedObject * length could not be determined */ Ptr::Ref - readPlaylength(Ptr::Ref fileName) + readPlaylength(const std::string & fileName) throw (std::invalid_argument);