⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qmxmlformat.cpp

📁 可以播放MP3,wma等文件格式的播放器
💻 CPP
字号:
/* qmxmlformat.cpp * * $Id: qmxmlformat.cpp,v 1.25.2.1 2002/10/10 21:40:52 kyllingstad Exp $ * * Apollo sound player: http://www.apolloplayer.org * Copyright(C) 2000-2002 Apollo Team.  See CREDITS file. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. * * The GNU General Public License is also available online at: * * http://www.gnu.org/copyleft/gpl.html */#include "message.h"#include "qmxmlformat.h"#include "qmconfig.h"#include "qmdiritem.h"#include "qmplaylist.h"#include "qmsongitem.h"#include "qmmainwindow.h"#include <time.h>#include <qdom.h>#include <qfile.h>#include <qfileinfo.h>#include <qmessagebox.h>#include <qobject.h>/* Disables false warning in Visual Studio about dynamic_cast */#ifdef _WS_WIN_#pragma warning(disable: 4541)#endif/** * @file qmxmlformat.cpp * @brief Loading/saving xml format playlists *//*!  \class QmXmlFormat qmxmlformat.h  \brief Provides functionality for loading and saving playlists in XML format.  For M3U format, see the QmM3uFormat class.*//*!*/QmXmlFormat::QmXmlFormat(){}/*!*/QmXmlFormat::~QmXmlFormat(){}/*!  Loads the XML playlist \a filename into \a list.  The \a above argument  specifies the position of where the new items will be placed.  Items  will be placed below \a above.  If \a above is null, the items will appear  at the top of the playlist.   */QmPlayListItem*QmXmlFormat::load(	QmPlayList *list,	const QString &filename,	QmPlayListItem *above){	CHECK_PTR(list);	m_pPlayList = list;	QString playlistfile;	if(filename.isNull())		playlistfile = QmConfig::instance()->configPath() + "playlist.xml";	else		playlistfile = filename;		QFile f( playlistfile );		if ( !f.open( IO_ReadOnly ) )    {        QmMainWindow::mainwin->statusUpdate(new Message(Message::Error,                                                        "Error: could not read playlist: %s",                                                        playlistfile.latin1()));        return 0;    }    	QDomDocument doc;	if ( ! doc.setContent( &f ) )		return 0;		f.close();	// Get root node (element)	QDomNode node = doc.documentElement();	if(node.nodeName() != "apollo-playlist")	{		QMessageBox::warning(0,							 QObject::tr("Apollo Warning"),							 QObject::tr("The file %1 is not a valid playlist format.").arg(filename));		return 0;	}	// Get the first child in the document.	node = node.firstChild();    long start_time = 0;    int song_count = 0;	QmPlayListItem *ret = loadPlayList(node, 0, above, start_time, song_count);	return ret;}/*!  Helper function for recursively loading the playlist tree.  The \a  node argument is the current node to scan and populate the list from  and \a parent is the item that is to be the parent for the new items  created as a result of scanning \a node.  If \a parent is null (the  default), these items will get the listview as the parent, i.e. they  become root items.  The \a above argument specifies which item that  shall be above the new items.  That is, it specifies the location of  where the new items will appear.  The argument defaults to null, being  placed topmost.*/QmPlayListItem*QmXmlFormat::loadPlayList(	QDomNode &node,	QmPlayListItem *parent,	QmPlayListItem *above,    long &start_time,    int &song_count){	QDomNode playlistnode;	QmPlayListItem *child = above;	QString path, name, open;    QString title, artist, album, cdPosition, displayString, comment;    long lengthSeconds;    int bitrate, year;    bool vbr, multiArtist;    if (start_time == 0)        start_time = time(0);	while( ! node.isNull() )	{		title = artist = album = cdPosition = path = name = open = comment = displayString = QString::null;        lengthSeconds = bitrate = year = 0;        vbr = multiArtist = false;		playlistnode = loadPlayListNode(node, path, name, open, lengthSeconds,										bitrate, vbr, title, artist, album,										cdPosition, displayString, multiArtist,										comment, year);		if(node.nodeName() == "dir")		{//            cout << "dir: parent = " << parent << " - child = " << child << " - name = " << name << std::endl;            if(name.isEmpty())                name = title;			if(parent == 0)				child = new QmDirItem(m_pPlayList, child, name);			else				child = new QmDirItem(dynamic_cast<QmDirItem*>(parent), child, name);			child->setExpandable(true);            child->setOpen(open == "1");			loadPlayList(playlistnode, child, 0, start_time, song_count);		}		else		{			if(parent == 0)				child = new QmSongItem(m_pPlayList, child, path, lengthSeconds,                                       bitrate, vbr, title, artist, album, cdPosition,									   multiArtist, year, comment, displayString);			else				child = new QmSongItem(parent, child, path, lengthSeconds,                                       bitrate, vbr, title, artist, album, cdPosition,									   multiArtist, year, comment, displayString);             if (++song_count%100 == 0)            {                QmMainWindow::mainwin->statusUpdate(new Message(Message::Update,                                                                "Loaded %d songs (%ld seconds)",                                                                song_count,                                                                time(0) - start_time));            }		}		node = node.nextSibling();	}    QmMainWindow::mainwin->statusUpdate(new Message(Message::Status,                                                    "Loaded %d songs in (%ld seconds)",                                                    song_count,                                                    time(0) - start_time));	return child;}/*!  Reads the 'dir'/'song' element \a node for all its  children and picks out the data and places it in \a text, \a path, and \a open.  The data can be in any order.  This function will print a warning if \a node is not either of kind mentioned above,  in which case \a text, \a path and \a open will remain unchanged and the  returned node will be empty.  \return The next child of \a node.  It can be either a directory or a song. */QDomNodeQmXmlFormat::loadPlayListNode(	const QDomNode &node,	QString &path,    QString &name,	QString &open,    long &lengthSeconds,    int &bitrate,    bool &vbr,    QString &title,    QString &artist,    QString &album,    QString &cdPosition,	QString &displayString,	bool &multiArtist,	QString &comment,	int &year){	QDomNode rc;	if(node.nodeName() != "dir" && node.nodeName() != "song")	{		qWarning("QmPlayList::loadPlayListNode(): Expected \"dir\" or \"song\","				 "found \"%s\".\n", node.nodeName().latin1());		return rc;	}	QDomNodeList children = node.childNodes();	QDomNode child, data;    bool isNumber;	for(unsigned int i = 0; i < children.count(); i++)	{		child = children.item(i);		data  = child.firstChild();		if(child.nodeName() == "title")			title = data.nodeValue();		else if(child.nodeName() == "path")			path = data.nodeValue();		else if(child.nodeName() == "open")			open = data.nodeValue();		else if(child.nodeName() == "name")			name = data.nodeValue();		else if(child.nodeName() == "artist")			artist = data.nodeValue();		else if(child.nodeName() == "album")			album = data.nodeValue();		else if (child.nodeName() == "comment")			comment = data.nodeValue();		else if (child.nodeName() == "display-string")			displayString = data.nodeValue();		else if(child.nodeName() == "cd-position")        {			cdPosition = data.nodeValue();            if (cdPosition.length() == 1)                cdPosition = "0" + cdPosition;        }		else if(child.nodeName() == "length")        {            lengthSeconds = data.nodeValue().toLong(&isNumber);            if (!isNumber)                lengthSeconds=0;        }		else if(child.nodeName() == "bitrate")        {            bitrate = data.nodeValue().toInt(&isNumber);            if (!isNumber)                bitrate=0;        }		else if (child.nodeName() == "year")		{			year = data.nodeValue().toInt(&isNumber);			if (!isNumber)				year = 0;		}        else if (child.nodeName() == "vbr")        {            vbr = child.nodeValue() == "true";        }		else if (child.nodeName() == "multi-artist")		{			multiArtist = data.nodeValue() == "true";		}		else if(rc.isNull())			rc = child;	}	return rc;}/*!  Saves the playlist \a list to \a filename in XML format. */voidQmXmlFormat::save(	QmPlayList *list,	const QString &filename){    long start_time = time(0);    	CHECK_PTR(list);		QString playlistfile;	if(filename.isNull())		playlistfile = QmConfig::instance()->configPath() + "playlist.xml";	else		playlistfile = filename;    QmMainWindow::mainwin->statusUpdate(new Message(Message::Status,                                                    "Saving playlist ..."));	QString temp = list->fileName();	list->enableSelfSave(playlistfile);	list->save();	list->enableSelfSave(temp);    QmMainWindow::mainwin->statusUpdate(new Message(Message::Status,                                                    "Saved playlist in %ld seconds",                                                    time(0) - start_time));}/*!  This function will try to determine whether the file \a filename is an  Apollo XML playlist or not.  If \a filename does not exist, the extension will be checked.  If it is  'xml' (case insensitive), true will be returned.  Otherwise, false.  If \a filename exist, the file will be opened and it will try to read the root  node to see if it is of 'apollo-playlist' type.  \return True if \a filename is believed to be an Apollo XML playlist, false  otherwise. */boolQmXmlFormat::isMatch(	const QString &filename){	QFile f( filename );	if( ! f.exists())	{		QFileInfo fi( f );		if(fi.extension(false).lower() == "xml")			return true;		else			return false;	}		if ( ! f.open( IO_ReadOnly ) )		return false;		QDomDocument doc;	if ( ! doc.setContent( &f ) )		return false;		f.close();	// Get root node (element)	QDomNode node = doc.documentElement();	return node.nodeName() == "listview-playlist" || node.nodeName() == "apollo-playlist";// 	if(node.nodeName() != "apollo-playlist")// 		return false;	return true;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -