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

📄 qmconfig.cpp

📁 可以播放MP3,wma等文件格式的播放器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* qmconfig.cpp * * $Id: qmconfig.cpp,v 1.54.2.3 2002/10/11 06:39:03 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 "qmconfig.h"#include "qmrecoverymanager.h"#include <qdir.h>#include <qdom.h>#include <qfile.h>#include <qmessagebox.h>#include <qstring.h>#include <qtextstream.h>#include <iostream>#include <unistd.h>/** * \file qmconfig.cpp * \brief Reads, writes and gives access to the configuration*/QmAutoPtr<QmConfig> QmConfig::s_pInstance;/*!  \class QmConfig qmconfig.h  \brief Provides functionality for persistently storing values in XML file.  This class is used for persistently storing data.  The class provides the  usual get() and set() methods to locate the values.  The values are identified  in <group, key> pairs.  Groups can be assigned to a parent group so that the  XML tree can be built.    Remember that only one instance of this class is allowed, this means  that you cannot create this on your own, instead use  QmConfig::instance() to obtain the pointer.*//*!  Creates a config object with default data.*/QmConfig::QmConfig()	: m_LoadConfig(true),	  m_LoadPlayList(true),	  m_LoadPlayListTree(true),	  m_LoadDirectoryTree(true),	  m_LoadBadlist(true){	m_pDefaultsRoot = new QDomDocument("apollo");	m_pFileRoot 	= new QDomDocument("apollo");		m_pRoot = m_pDefaultsRoot;		m_pRoot->setNodeValue("apollo");	// Create the root document element.  This is necessary if we build our own tree.	m_pRoot->appendChild(m_pRoot->createElement("apollo"));	m_LazySetMode	= false;	m_SilentMode	= true;	mergeDefaults();	m_SilentMode	= false;//	QTextStream conf_stream(stdout, IO_WriteOnly); // Debug output//	m_pDefaultsRoot->save(conf_stream, 0);	}/*!  */QmConfig::~QmConfig(){    delete m_pDefaultsRoot;    delete m_pFileRoot;}/*!  Returns the only instance of the QmConfig class. Uses QmAutoPtr for storing the object  so it is automaticly destroyed when quitting.*/QmConfig *QmConfig::instance(){    if ( QmConfig::s_pInstance.get() == 0 )    {        s_pInstance.reset( new QmConfig );    }	    return reinterpret_cast<QmConfig *>(QmConfig::s_pInstance.get());}/*!  Merges the loaded settings with the defaults.  The default values cannot  be stored in the same QDomDocument as the QDomDocument populated from the  configuration file because the default values will be lost, even if there  is no value defined in the configuration file to replace the default.  So, the defauls are stored in a separate QDomDocument than the configuration  file.  This function will merge these two documents.  If a value is present  in the configuration file, the default will be ignored.  If, however, there  is a default value for something not present in the configuration file, this  default value will be used.  This function is typically only used once, and that is after the configuration  file is loaded. */voidQmConfig::mergeDefaults(){	// *** BUG: Everything will be lost if a config file exists so the values below only	// work when there's no config file.  Fix this.    // Set default data	assign("main-window");    set( "main-window", "geometry",			QRect( 0, 0, 400, 400 ) );    set( "main-window", "scroll-pos",		QPoint( -1, -1 ) );    set( "main-window", "playing-song",		"" );    set( "main-window", "browser-width",	100 );    set( "main-window", "playfirst-heigt",	100 );    set( "main-window", "playlist-width",	300 );    set( "main-window", "playlist-height",	300 );	set( "main-window", "browser-position",	0 ); // 0 = left, 1 = right, 2 = top, 3 = bottom	set( "main-window", "show-browser",		0 ); // 0 = hide, 1 = show	set( "main-window", "full-browser",		0 ); // 0 = not full, 1 = full	set( "main-window", "browser-mode",		0 ); // 0 = dir, 1 = playlist    set( "main-window", "playlist",         "playlist.xml");    set( "main-window", "playlisttree",     "playlisttree.xml");    assign( "file-dialog" );    set( "file-dialog", "geometry",			QRect( 0, 0, 600, 400 ) );	assign("path");    set( "path", "music",                   QDir::homeDirPath() );    set( "path", "playlist",                QDir::homeDirPath() );    set( "path", "playlisttree",            QDir::homeDirPath() );    set( "path", "startup-playlist",	    QDir::homeDirPath() + "/.apollo/playlist.xml"); 	assign("mpg123");    set( "mpg123", "buffer",				0 );    set( "mpg123", "path",					"/usr/bin/mpg123" );    set( "mpg123", "channels",              "stereo" );    set( "mpg123", "downsample",            0 );    assign("stuff");    set( "stuff", "encoding",               "utf-8");    set( "stuff", "packed",                 false);	set( "stuff", "display-format",         "%p  %a  %2n %t");	set( "stuff", "display-format-multi",   "Various  %a  %2n %p  %t");		set( "stuff", "random",					false);	set( "stuff", "loop",					false);	set( "stuff", "loop-one",               false); 	assign("font");	assign("key"); // values: next, prev, play, stop,	assign("shortcut-keys"); }/*!  Loads the configuration file from ~/.apollo/.  If the folder does not  exist, it will attempt to create it.*/voidQmConfig::load(){	if(QmRecoveryManager::instance()->loadingEnabled(Config) == false)		return;	QDir dir( configPath() );	if( ! dir.exists() )	{		if ( dir.mkdir( configPath() ) )        {            QMessageBox::information(				0, QObject::tr("Welcome to Apollo"),				QObject::tr("It seems this is the first time you run Apollo.<br>"							"Welcome!  A '.apollo' folder has been created in<br>"							"your home folder in which Apollo will store its<br>"							"configuration."));        }        else        {            QMessageBox::warning(				0, QObject::tr("Welcome to Apollo"),				QObject::tr("READ THIS: Apollo needs a directory named `.apollo'<br>"							"in your home directory to store its configuration.<br>"							"Apollo was unable to create it - perhaps you could help?<br>"							"Type `chmod u+w ~ && mkdir ~/.apollo && chmod u+w ~/.apollo && chmod u-w ~`<br>"							"in a shell.<br>"));        }		return;	}    QFile conf_file( configPath() + configFileName() );	    if ( ! conf_file.open( IO_ReadOnly ) )    {        std::cerr << "Failed to open configuration file \"" << conf_file.name() << "\" for reading." << std::endl				  << "Default settings will be used." << std::endl;        return;    }	    m_pFileRoot->setContent( &conf_file );    QDomElement root = m_pFileRoot->documentElement();    if ( root.nodeName() != "apollo" )    {		std::cerr << "The configuration file appears to be corrupt.  The root node of the XML file is " << std::endl				  << "to be \"apollo\", but instead \"" << root.nodeName() << "\" was found.  Default " << std::endl				  << "values will be used." << std::endl;		m_pRoot = m_pDefaultsRoot;        mergeDefaults();        return;    }		m_pRoot = m_pFileRoot;		m_LazySetMode = true;	mergeDefaults();	m_LazySetMode = false;}/*!  Saves the configurtion as XML to ~/.apollo/config.*/voidQmConfig::save(){	QDir dir( configPath() );	if( ! dir.exists() )	{		if ( ! dir.mkdir( configPath() ) )        {			std::cerr << "Failed to create the directory \"" << configPath() << "\".  Configuration "					  << "will not be saved." << std::endl;            return;        }	}	    QFile conf_file( configPath() + configFileName() );	    if ( ! conf_file.open( IO_WriteOnly ) )    {		std::cerr << "Failed to write configuration to \"" << conf_file.name() << "\".  Configuration "				  << "will not be saved." << std::endl;        return;    }	    QTextStream conf_stream( &conf_file );//	QTextStream conf_stream(stdout, IO_WriteOnly); // Debug output	m_pRoot->save(conf_stream, 0);}/*!  Creates a string which contains the correct indentation for a given  level, each level adds 4 spaces to the string. Level 0 is an empty string.  This is used for correctly indenting the output of the config, each  time a sub function is called the level is increased by one.*/QStringQmConfig::indent( int level ){    QString tmp;    for ( ; level > 0; --level )        tmp += "    ";    return tmp;}/*!  Returns the path of the config file. This is not including the filename.*/QStringQmConfig::configPath() const{    return QString( QDir::homeDirPath() + "/.apollo/");}/*!  \return The path for \a f. */QStringQmConfig::path(	ConfigFile f) const{	switch(f)	{	case Config:		return configPath() + configFileName();		break;	case PlayList:		return configPath() + "playlist.xml";		break;	case PlayListTree:		return configPath() + "playlisttree";		break;	case DirectoryTree:		return configPath() + "directorytree";		break;	case Badlist:		return configPath() + "badlist";		break;	default:		break;	}	return QString("");}/*!  hmm */voidQmConfig::setLoading(	ConfigFile f,	bool e){	switch(f)	{	case Config:		m_LoadConfig = e;		break;	case PlayList:		m_LoadPlayList = e;		break;	case PlayListTree:		m_LoadPlayListTree = e;		break;	case DirectoryTree:		m_LoadDirectoryTree = e;		break;	case Badlist:		m_LoadBadlist = e;		break;	default:		qWarning("QmConfig::setLoading(): Unknown file.");		break;	}}/*!  hmm */boolQmConfig::loadingEnabled(	ConfigFile f) const{	bool e;	switch(f)	{	case Config:		e = m_LoadConfig;		break;	case PlayList:		e = m_LoadPlayList;		break;	case PlayListTree:		e = m_LoadPlayListTree;		break;	case DirectoryTree:		e = m_LoadDirectoryTree;		break;	case Badlist:		e = m_LoadBadlist;		break;	default:		qWarning("QmConfig::loadingEnabled(): Unknown file.");		e = false;		break;	}	return e;}/*!  Returns the file name of the config file. This is not including the file path.*/QStringQmConfig::configFileName() const{    return QString( "config" );}/*!  Assigns the group \a group as a child of \a parent.  If \a parent is null  (default), \a group will be assigned as a child to the root.  If \a group  already exist \e anywhere in the tree, no changes will be made so make sure  the groupname is unique.  If \a parent does not exist a warning will be printed and \a group will be  assigned as a child to root.  The \a group name must be different from \a parent name.  The \a group name cannot  be blank.  For example, if 'apollo' is root, and we do:  \code  assign("player");  assign("mpg123", "player");  \endcode  will create a tree like this:  \verbatim  <apollo>      <players>          <mpg123>          </mpg123>	  </players>  </apollo>  \endverbatim  Note that the group name \e must be unique in the entire tree.  So, this is not  possible:  \verbatim  <apollo>     <foobar>	 </foobar>	 <foo>	     <bar>		     <foobar>			 </foobar>		 </bar>	 </foo>  </apollo>  \endverbatim  Here we have two 'foobar' groups.  Since variables are assigned to groupnames  only, they must be unique.  This limitation is for convenience. */voidQmConfig::assign(	const QString &group,	const QString &parent){	if(group.isEmpty())	{		qWarning("QmConfig::assign(): The group name cannot be empty.");		return;	}		if(group == parent)	{		qWarning("QmConfig::assign(): The group name and parent name cannot be equal (\'%s\').", group.latin1());		return;	}	if(findNode(group).isNull() == false)	{		if(m_SilentMode == false && m_LazySetMode == false)			qWarning("QmConfig::assign(): The group \'%s\' already exists.", group.latin1());		return;	}	QDomNode parent_node = findNode(parent);	if(parent_node.isNull())	{		if( ! parent.isEmpty())		{			qWarning("QmConfig::assign(): Could not locate the parent node \'%s\'.  Assigning \'%s\' to root.",					 parent.latin1(), group.latin1());		}		parent_node = m_pRoot->documentElement();	}	parent_node.appendChild( m_pRoot->createElement(group) );}/*!  Find the node with name \a name.  There can be at most \e one node with the  given name.  \return The node found, or a null node if not found. */QDomNodeQmConfig::findNode(	const QString &name) const{	if(name == QString::null)		return m_pRoot->documentElement();	else		return findNode(m_pRoot->documentElement(), name);}/*!  Searches for the node with name \a name among the children of \a start.  Note that the function will traverse down the tree of all children of \a  start.    Only elements will be considered, i.e. QDomElement objects.  \return The node found, or a null node if not found.*/QDomNodeQmConfig::findNode(	const QDomNode &start,	const QString &name) const{	QDomNode n = start.firstChild();	while ( ! n.isNull() )

⌨️ 快捷键说明

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