00001
00002
00003
00004
00005
00006
00007
00008
00010
00011 #include "configuration.h"
00012 #include "mythcontext.h"
00013
00014 #include <qfile.h>
00015 #include <qdir.h>
00016
00018
00020
00021 XmlConfiguration::XmlConfiguration( const QString &sFileName )
00022 {
00023 m_sPath = MythContext::GetConfDir();
00024 m_sFileName = sFileName;
00025
00026 Load();
00027 }
00028
00030
00032
00033 bool XmlConfiguration::Load( void )
00034 {
00035 QString sName = m_sPath + "/" + m_sFileName;
00036
00037 QFile file( sName );
00038
00039 if (file.exists() && m_sFileName.length())
00040 {
00041
00042 if ( !file.open( IO_ReadOnly ) )
00043 return false;
00044
00045 QString sErrMsg;
00046 int nErrLine = 0;
00047 int nErrCol = 0;
00048 bool bSuccess = m_config.setContent( &file, false, &sErrMsg, &nErrLine, &nErrCol );
00049
00050 file.close();
00051
00052 if (!bSuccess)
00053 {
00054 VERBOSE(VB_IMPORTANT, QString("Configuration::Load - "
00055 "Error parsing: %1 "
00056 "at line: %2 column: %3")
00057 .arg( sName )
00058 .arg( nErrLine )
00059 .arg( nErrCol ));
00060
00061 VERBOSE(VB_IMPORTANT, QString("Configuration::Load - Error Msg: %1" )
00062 .arg( sErrMsg ));
00063 return false;
00064 }
00065
00066 m_rootNode = m_config.namedItem( "Configuration" );
00067 }
00068 else
00069 {
00070 m_rootNode = m_config.createElement( "Configuration" );
00071 m_config.appendChild( m_rootNode );
00072 }
00073
00074 return true;
00075 }
00076
00078
00080
00081 bool XmlConfiguration::Save( void )
00082 {
00083 if (m_sFileName.isEmpty())
00084 return true;
00085
00086 QString sName = m_sPath + "/" + m_sFileName;
00087
00088 QFile file( sName );
00089
00090 if (!file.exists())
00091 {
00092 QDir createDir( m_sPath );
00093
00094 if (!createDir.exists())
00095 {
00096 if (!createDir.mkdir( m_sPath, true ))
00097 {
00098 VERBOSE(VB_IMPORTANT, QString("Could not create %1").arg( m_sPath ));
00099 return false;
00100 }
00101 }
00102 }
00103
00104 if (!file.open( IO_WriteOnly | IO_Truncate ))
00105 {
00106 VERBOSE(VB_IMPORTANT, QString("Could not open settings file %1 "
00107 "for writing").arg( sName ));
00108
00109 return false;
00110 }
00111
00112 QTextStream ts( &file );
00113
00114 m_config.save( ts, 2 );
00115
00116 file.close();
00117
00118 return true;
00119 }
00120
00122
00124
00125 QDomNode XmlConfiguration::FindNode( const QString &sName, bool bCreate )
00126 {
00127 QStringList parts = QStringList::split( "/", sName );
00128
00129 return FindNode( parts, m_rootNode, bCreate );
00130
00131 }
00132
00134
00136
00137 QDomNode XmlConfiguration::FindNode( QStringList &sParts, QDomNode &curNode, bool bCreate )
00138 {
00139 if (sParts.empty())
00140 return curNode;
00141
00142 QString sName = sParts.front();
00143 sParts.pop_front();
00144
00145 QDomNode child = curNode.namedItem( sName );
00146
00147 if (child.isNull() )
00148 {
00149 if (bCreate)
00150 {
00151 QDomNode newNode = m_config.createElement( sName );
00152
00153 child = curNode.appendChild( newNode );
00154 }
00155 else
00156 sParts.clear();
00157 }
00158
00159 return FindNode( sParts, child, bCreate );
00160 }
00161
00163
00165
00166 int XmlConfiguration::GetValue( const QString &sSetting, int nDefault )
00167 {
00168 QDomNode node = FindNode( sSetting );
00169
00170 if (!node.isNull())
00171 {
00172
00173 QDomText oText = node.firstChild().toText();
00174
00175 if (!oText.isNull())
00176 return oText.nodeValue().toInt();
00177 }
00178
00179 return nDefault;
00180 }
00181
00183
00185
00186 QString XmlConfiguration::GetValue( const QString &sSetting, QString sDefault )
00187 {
00188 QDomNode node = FindNode( sSetting );
00189
00190 if (!node.isNull())
00191 {
00192
00193 QDomText oText = node.firstChild().toText();
00194
00195 if (!oText.isNull())
00196 return oText.nodeValue();
00197 }
00198
00199 return sDefault;
00200 }
00201
00203
00205
00206 void XmlConfiguration::SetValue( const QString &sSetting, int nValue )
00207 {
00208 QString sValue = QString::number( nValue );
00209 QDomNode node = FindNode( sSetting, TRUE );
00210
00211 if (!node.isNull())
00212 {
00213 QDomText textNode;
00214
00215 if (node.hasChildNodes())
00216 {
00217
00218 textNode = node.firstChild().toText();
00219 textNode.setNodeValue( sValue );
00220 }
00221 else
00222 {
00223 textNode = m_config.createTextNode( sValue );
00224 node.appendChild( textNode );
00225 }
00226 }
00227 }
00228
00230
00232
00233 void XmlConfiguration::SetValue( const QString &sSetting, QString sValue )
00234 {
00235 QDomNode node = FindNode( sSetting, TRUE );
00236
00237 if (!node.isNull())
00238 {
00239 QDomText textNode;
00240
00241 if (node.hasChildNodes())
00242 {
00243
00244 textNode = node.firstChild().toText();
00245 textNode.setNodeValue( sValue );
00246 }
00247 else
00248 {
00249 textNode = m_config.createTextNode( sValue );
00250 node.appendChild( textNode );
00251 }
00252 }
00253 }
00254
00255
00258
00259
00260
00263
00264 DBConfiguration::DBConfiguration( )
00265 {
00266 }
00267
00269
00271
00272 bool DBConfiguration::Load( void )
00273 {
00274 return true;
00275 }
00276
00278
00280
00281 bool DBConfiguration::Save( void )
00282 {
00283 return true;
00284 }
00285
00287
00289
00290 int DBConfiguration::GetValue( const QString &sSetting, int nDefault )
00291 {
00292 return gContext->GetNumSetting( sSetting, nDefault );
00293 }
00294
00296
00298
00299 QString DBConfiguration::GetValue( const QString &sSetting, QString sDefault )
00300 {
00301 return gContext->GetSetting( sSetting, sDefault );
00302 }
00303
00305
00307
00308 void DBConfiguration::SetValue( const QString &sSetting, int nValue )
00309 {
00310 gContext->SaveSetting( sSetting, nValue );
00311
00312 }
00313
00315
00317
00318 void DBConfiguration::SetValue( const QString &sSetting, QString sValue )
00319 {
00320 gContext->SaveSetting( sSetting, sValue );
00321 }