added id3 tag recognition to UploadFileWindow

This commit is contained in:
fgerlits 2005-06-15 13:38:52 +00:00
parent a4c3aa0fc4
commit e806ca5f3b
4 changed files with 76 additions and 48 deletions

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $ 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 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/AudioClip.cxx,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -610,11 +610,18 @@ AudioClip :: readTag(Ptr<MetadataTypeContainer>::Ref metadataTypes)
throw std::invalid_argument("audio clip has no uri field"); 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"); 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(); TagLib::ID3v2::Tag* id3v2Tag = mpegFile.ID3v2Tag();
if (id3v2Tag) { if (id3v2Tag) {
Ptr<const MetadataType>::Ref metadata; Ptr<const MetadataType>::Ref metadata;
@ -641,7 +648,7 @@ AudioClip :: readTag(Ptr<MetadataTypeContainer>::Ref metadataTypes)
return; return;
} }
TagLib::FileRef genericFileRef(getUri()->c_str()); TagLib::FileRef genericFileRef(uri.c_str());
TagLib::Tag* tag = genericFileRef.tag(); TagLib::Tag* tag = genericFileRef.tag();
if (tag) { if (tag) {
TagLib::String stringValue; TagLib::String stringValue;

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $ 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 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/AudioClipTest.cxx,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -210,6 +210,7 @@ void
AudioClipTest :: tagTest(void) AudioClipTest :: tagTest(void)
throw (CPPUNIT_NS::Exception) throw (CPPUNIT_NS::Exception)
{ {
// should work with either plain file path...
Ptr<std::string>::Ref uri(new std::string("var/test10001.mp3")); Ptr<std::string>::Ref uri(new std::string("var/test10001.mp3"));
audioClip->setUri(uri); audioClip->setUri(uri);
try { try {
@ -225,6 +226,21 @@ AudioClipTest :: tagTest(void)
Ptr<const Glib::ustring>::Ref artist Ptr<const Glib::ustring>::Ref artist
= audioClip->getMetadata("dc:creator"); = audioClip->getMetadata("dc:creator");
CPPUNIT_ASSERT(*artist == "The Muppets"); 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");
} }

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $ 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 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/gLiveSupport/src/UploadFileWindow.cxx,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -70,11 +70,10 @@ UploadFileWindow :: UploadFileWindow (Ptr<GLiveSupport>::Ref gLiveSupport,
: WhiteWindow("", : WhiteWindow("",
Colors::White, Colors::White,
WidgetFactory::getInstance()->getWhiteWindowCorners()), WidgetFactory::getInstance()->getWhiteWindowCorners()),
LocalizedObject(bundle) LocalizedObject(bundle),
gLiveSupport(gLiveSupport)
{ {
this->gLiveSupport = gLiveSupport; isAudioClipValid = false;
fileName.reset(new std::string());
isFileGood = false;
Ptr<WidgetFactory>::Ref wf = WidgetFactory::getInstance(); Ptr<WidgetFactory>::Ref wf = WidgetFactory::getInstance();
@ -224,29 +223,51 @@ UploadFileWindow :: onChooseFileButtonClicked(void) throw ()
void void
UploadFileWindow :: updateFileInfo(void) throw () UploadFileWindow :: updateFileInfo(void) throw ()
{ {
std::string newFileName = fileNameEntry->get_text().raw(); std::string fileName = fileNameEntry->get_text().raw();
Ptr<std::string>::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 // see if the file exists, and is readable
std::ifstream file(newFileName.c_str()); std::ifstream file(fileName.c_str());
if (!file.good()) { if (!file.good()) {
isFileGood = false; isAudioClipValid = false;
file.close(); file.close();
return; return;
} }
file.close(); file.close();
isFileGood = true; isAudioClipValid = true;
fileName.reset(new std::string(newFileName));
fileURI.reset(new std::string("file://"));
*fileURI += *fileName;
Ptr<time_duration>::Ref playlength;
try { try {
playlength = readPlaylength(fileName); playlength = readPlaylength(fileName);
} catch (std::invalid_argument &e) { } catch (std::invalid_argument &e) {
statusBar->set_text(e.what());
playlength.reset(new time_duration(0,0,0,0)); playlength.reset(new time_duration(0,0,0,0));
} }
Ptr<const Glib::ustring>::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<const Glib::ustring>::Ref creator
= audioClip->getMetadata("dc:creator");
creatorEntry->set_text(creator ? *creator : "");
Ptr<const Glib::ustring>::Ref genre
= audioClip->getMetadata("dc:type");
genreEntry->set_text(genre ? *genre : "");
// display the new play length // display the new play length
std::ostringstream lengthStr; std::ostringstream lengthStr;
lengthStr << std::setfill('0') lengthStr << std::setfill('0')
@ -278,21 +299,15 @@ UploadFileWindow :: onUploadButtonClicked(void) throw ()
{ {
try { try {
updateFileInfo(); updateFileInfo();
if (!isFileGood) { if (!isAudioClipValid) {
// TODO: localize error message // TODO: localize error message
throw std::invalid_argument("file does not exist"); throw std::invalid_argument("file does not exist");
} }
Ptr<const Glib::ustring>::Ref ustrValue;
ustrValue.reset(new Glib::ustring(titleEntry->get_text()));
// create and upload an AudioClip object
Ptr<AudioClip>::Ref audioClip(new AudioClip(ustrValue,
playlength,
fileURI));
// set the metadata available // set the metadata available
Ptr<const Glib::ustring>::Ref ustrValue(new Glib::ustring(
titleEntry->get_text() ));
audioClip->setTitle(ustrValue);
ustrValue.reset(new Glib::ustring(creatorEntry->get_text())); ustrValue.reset(new Glib::ustring(creatorEntry->get_text()));
audioClip->setMetadata(ustrValue, "dc:creator"); audioClip->setMetadata(ustrValue, "dc:creator");
ustrValue.reset(new Glib::ustring(genreEntry->get_text())); ustrValue.reset(new Glib::ustring(genreEntry->get_text()));
@ -300,6 +315,7 @@ UploadFileWindow :: onUploadButtonClicked(void) throw ()
ustrValue.reset(new Glib::ustring( ustrValue.reset(new Glib::ustring(
fileFormatComboBox->get_active_text())); fileFormatComboBox->get_active_text()));
audioClip->setMetadata(ustrValue, "dc:format"); audioClip->setMetadata(ustrValue, "dc:format");
// TODO: is this really what we mean by dc:format?
// upload the audio clip // upload the audio clip
gLiveSupport->uploadFile(audioClip); gLiveSupport->uploadFile(audioClip);
@ -335,10 +351,10 @@ UploadFileWindow :: onCloseButtonClicked(void) throw ()
* Determine the length of an audio file * Determine the length of an audio file
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
Ptr<time_duration>::Ref Ptr<time_duration>::Ref
UploadFileWindow :: readPlaylength(Ptr<const std::string>::Ref fileName) UploadFileWindow :: readPlaylength(const std::string & fileName)
throw (std::invalid_argument) throw (std::invalid_argument)
{ {
TagLib::FileRef fileRef(fileName->c_str()); TagLib::FileRef fileRef(fileName.c_str());
TagLib::AudioProperties * audioProperties = fileRef.audioProperties(); TagLib::AudioProperties * audioProperties = fileRef.audioProperties();
if (audioProperties) { if (audioProperties) {

View file

@ -22,7 +22,7 @@
Author : $Author: fgerlits $ 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 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/gLiveSupport/src/UploadFileWindow.h,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -87,7 +87,7 @@ using namespace LiveSupport::Widgets;
* </code></pre> * </code></pre>
* *
* @author $Author: fgerlits $ * @author $Author: fgerlits $
* @version $Revision: 1.4 $ * @version $Revision: 1.5 $
*/ */
class UploadFileWindow : public WhiteWindow, public LocalizedObject class UploadFileWindow : public WhiteWindow, public LocalizedObject
{ {
@ -225,23 +225,12 @@ class UploadFileWindow : public WhiteWindow, public LocalizedObject
/** /**
* The name of the file to upload. * The name of the file to upload.
*/ */
Ptr<std::string>::Ref fileName; Ptr<AudioClip>::Ref audioClip;
/** /**
* Signals if the file under fileName is good. * Signals if the audio clip is valid.
*/ */
bool isFileGood; bool isAudioClipValid;
/**
* The URI to the file to upload.
* Basically same as fileName, with 'file://' prepended.
*/
Ptr<std::string>::Ref fileURI;
/**
* The playling length of the file to upload.
*/
Ptr<time_duration>::Ref playlength;
/** /**
* Function to catch the event of the choose file button being * 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 * length could not be determined
*/ */
Ptr<time_duration>::Ref Ptr<time_duration>::Ref
readPlaylength(Ptr<const std::string>::Ref fileName) readPlaylength(const std::string & fileName)
throw (std::invalid_argument); throw (std::invalid_argument);