示例#1
0
void XSPFParser::readTrack(QXmlStreamReader & xml, MediaInfo & mediaInfo) const {
	while (!xml.atEnd() && !_stop) {
		xml.readNext();

		QString element(xml.name().toString());

		if (xml.isStartElement()) {

			//Filename
			if (element == XSPF_LOCATION) {
				QUrl url = QUrl::fromEncoded(xml.readElementText().toUtf8());
				QString location(url.toString());
				bool isUrl = MediaInfo::isUrl(location);
				mediaInfo.setUrl(isUrl);
				if (isUrl) {
					mediaInfo.setFileName(location);
				} else {
					QString path(QFileInfo(_filename).path());
					mediaInfo.setFileName(Util::canonicalFilePath(path, location));
				}
			}

			//Unique ID
			else if (element == XSPF_IDENTIFIER) {
				QString identifier(xml.readElementText());
				//FIXME not implemented yet
			}

			//Artist
			else if (element == XSPF_CREATOR) {
				QString creator(xml.readElementText());
				mediaInfo.insertMetadata(MediaInfo::Artist, creator);
			}

			//Album
			else if (element == XSPF_ALBUM) {
				QString album(xml.readElementText());
				mediaInfo.insertMetadata(MediaInfo::Album, album);
			}

			//Track number
			else if (element == XSPF_TRACKNUM) {
				QString trackNum(xml.readElementText());
				mediaInfo.insertMetadata(MediaInfo::TrackNumber, trackNum);
			}

			//Title
			else if (element == XSPF_TITLE) {
				QString title(xml.readElementText());
				mediaInfo.insertMetadata(MediaInfo::Title, title);
			}

			//Comment
			else if (element == XSPF_ANNOTATION) {
				QString annotation(xml.readElementText());
				if (mediaInfo.metadataValue(MediaInfo::Title).isEmpty()) {
					//Some people didn't understand how XSPF works
					//and confused annotation with title
					mediaInfo.insertMetadata(MediaInfo::Title, annotation);
				}
				mediaInfo.insertMetadata(MediaInfo::Comment, annotation);
			}

			//Length
			else if (element == XSPF_DURATION) {
				int duration = xml.readElementText().toInt();
				//XSPF gives us the duration in milliseconds
				//Let's convert it to seconds
				mediaInfo.setLength(duration / 1000);
			}

			//Album art URL
			else if (element == XSPF_IMAGE) {
				QString image(xml.readElementText());
				//FIXME not implemented yet
				//mediaInfo.insertMetadata(MediaInfo::AlbumArt, image);
			}

			//URL of the original web page
			else if (element == XSPF_INFO) {
				QString info(xml.readElementText());
				mediaInfo.insertMetadata(MediaInfo::URL, info);
			}

			//Meta
			else if (element == XSPF_META) {

				//These tags are specific to foobar2000 XSPF plugin

				QXmlStreamAttributes attributes = xml.attributes();

				//Date
				if (attributes.hasAttribute(XSPF_FOOBAR2000_DATE)) {
					QString date(attributes.value(XSPF_FOOBAR2000_DATE).toString());
					mediaInfo.insertMetadata(MediaInfo::Year, date);
				}

				//Genre
				else if (attributes.hasAttribute(XSPF_FOOBAR2000_GENRE)) {
					QString genre(attributes.value(XSPF_FOOBAR2000_GENRE).toString());
					mediaInfo.insertMetadata(MediaInfo::Genre, genre);
				}
			}

			else if (element == XSPF_EXTENSION) {
				QString xspfNamespace(xml.attributes().value(XSPF_APPLICATION).toString());

				if (xspfNamespace == XSPF_QUARKPLAYER_NAMESPACE) {
					while (!xml.atEnd() && !_stop) {
						xml.readNext();

						QString extensionElement(xml.name().toString());
						if (xml.isStartElement()) {

							if (extensionElement == XSPF_QUARKPLAYER_CUE_START_INDEX) {
								QString cueStartIndex(xml.readElementText());
								mediaInfo.setCueStartIndex(cueStartIndex);
							}

							else if (extensionElement == XSPF_QUARKPLAYER_CUE_END_INDEX) {
								QString cueEndIndex(xml.readElementText());
								mediaInfo.setCueEndIndex(cueEndIndex);
							}

							else if (extensionElement == XSPF_QUARKPLAYER_YEAR) {
								QString year(xml.readElementText());
								mediaInfo.insertMetadata(MediaInfo::Year, year);
							}

							else if (extensionElement == XSPF_QUARKPLAYER_GENRE) {
								QString genre(xml.readElementText());
								mediaInfo.insertMetadata(MediaInfo::Genre, genre);
							}
						}

						if (xml.isEndElement()) {
							if (extensionElement == XSPF_EXTENSION) {
								break;
							}
						}
					}
				}
			}
		}

		if (xml.isEndElement()) {
			if (element == XSPF_TRACK) {
				return;
			}
		}
	}
}