00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013 #include <unistd.h>
00014
00015 #include <iostream>
00016
00017 #include <QDir>
00018 #include <QFile>
00019 #include <QTextStream>
00020
00021 #include "configuration.h"
00022 #include "mythlogging.h"
00023 #include "mythdb.h"
00024 #include "mythdirs.h"
00025 #include "compat.h"
00026
00028
00030
00031 XmlConfiguration::XmlConfiguration( const QString &sFileName )
00032 {
00033 m_sPath = GetConfDir();
00034 m_sFileName = sFileName;
00035
00036 Load();
00037 }
00038
00040
00042
00043 bool XmlConfiguration::Load( void )
00044 {
00045 QString sName = m_sPath + '/' + m_sFileName;
00046
00047 QFile file( sName );
00048
00049 if (file.exists() && m_sFileName.length())
00050 {
00051
00052 if ( !file.open( QIODevice::ReadOnly ) )
00053 return false;
00054
00055 QString sErrMsg;
00056 int nErrLine = 0;
00057 int nErrCol = 0;
00058 bool bSuccess = m_config.setContent(&file, false, &sErrMsg,
00059 &nErrLine, &nErrCol );
00060
00061 file.close();
00062
00063 if (!bSuccess)
00064 {
00065 LOG(VB_GENERAL, LOG_ERR,
00066 QString("Error parsing: %1 at line: %2 column: %3")
00067 .arg( sName ) .arg( nErrLine ) .arg( nErrCol ));
00068
00069 LOG(VB_GENERAL, LOG_ERR, QString("Error Msg: %1" ) .arg( sErrMsg ));
00070 return false;
00071 }
00072
00073 m_rootNode = m_config.namedItem( "Configuration" );
00074 }
00075 else
00076 {
00077 m_rootNode = m_config.createElement( "Configuration" );
00078 m_config.appendChild( m_rootNode );
00079 }
00080
00081 return true;
00082 }
00083
00085
00087
00088 bool XmlConfiguration::Save( void )
00089 {
00090 if (m_sFileName.isEmpty())
00091 return true;
00092
00093 QString config_temppath = m_sPath + '/' + m_sFileName + ".new";
00094 QString config_filepath = m_sPath + '/' + m_sFileName;
00095 QString config_origpath = m_sPath + '/' + m_sFileName + ".orig";
00096
00097 QFile file(config_temppath);
00098
00099 if (!file.exists())
00100 {
00101 QDir createDir(m_sPath);
00102 if (!createDir.exists())
00103 {
00104 if (!createDir.mkdir(m_sPath))
00105 {
00106 LOG(VB_GENERAL, LOG_ERR,
00107 QString("Could not create %1").arg(m_sPath));
00108 return false;
00109 }
00110 }
00111 }
00112
00113 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
00114 {
00115 LOG(VB_GENERAL, LOG_ERR,
00116 QString("Could not open settings file %1 for writing")
00117 .arg(config_temppath));
00118
00119 return false;
00120 }
00121
00122 {
00123 QTextStream ts(&file);
00124 m_config.save(ts, 2);
00125 }
00126
00127 file.flush();
00128
00129 fsync(file.handle());
00130
00131 file.close();
00132
00133 bool ok = true;
00134 if (QFile::exists(config_filepath))
00135 ok = QFile::rename(config_filepath, config_origpath);
00136
00137 if (ok)
00138 {
00139 ok = file.rename(config_filepath);
00140 if (ok)
00141 QFile::remove(config_origpath);
00142 else if (QFile::exists(config_origpath))
00143 QFile::rename(config_origpath, config_filepath);
00144 }
00145
00146 if (!ok)
00147 {
00148 LOG(VB_GENERAL, LOG_ERR,
00149 QString("Could not save settings file %1").arg(config_filepath));
00150 }
00151
00152 return ok;
00153 }
00154
00156
00158
00159 QDomNode XmlConfiguration::FindNode( const QString &sName, bool bCreate )
00160 {
00161 QStringList parts = sName.split('/', QString::SkipEmptyParts);
00162
00163 return FindNode( parts, m_rootNode, bCreate );
00164
00165 }
00166
00168
00170
00171 QDomNode XmlConfiguration::FindNode( QStringList &sParts, QDomNode &curNode, bool bCreate )
00172 {
00173 if (sParts.empty())
00174 return curNode;
00175
00176 QString sName = sParts.front();
00177 sParts.pop_front();
00178
00179 QDomNode child = curNode.namedItem( sName );
00180
00181 if (child.isNull() )
00182 {
00183 if (bCreate)
00184 {
00185 QDomNode newNode = m_config.createElement( sName );
00186 if (!curNode.isNull())
00187 child = curNode.appendChild( newNode );
00188 }
00189 else
00190 sParts.clear();
00191 }
00192
00193 return FindNode( sParts, child, bCreate );
00194 }
00195
00197
00199
00200 int XmlConfiguration::GetValue( const QString &sSetting, int nDefault )
00201 {
00202 QDomNode node = FindNode( sSetting );
00203
00204 if (!node.isNull())
00205 {
00206
00207 QDomText oText = node.firstChild().toText();
00208
00209 if (!oText.isNull())
00210 return oText.nodeValue().toInt();
00211 }
00212
00213 return nDefault;
00214 }
00215
00217
00219
00220 QString XmlConfiguration::GetValue( const QString &sSetting, QString sDefault )
00221 {
00222 QDomNode node = FindNode( sSetting );
00223
00224 if (!node.isNull())
00225 {
00226
00227 QDomText oText = node.firstChild().toText();
00228
00229 if (!oText.isNull())
00230 return oText.nodeValue();
00231 }
00232
00233 return sDefault;
00234 }
00235
00237
00239
00240 void XmlConfiguration::SetValue( const QString &sSetting, int nValue )
00241 {
00242 QString sValue = QString::number( nValue );
00243 QDomNode node = FindNode( sSetting, true );
00244
00245 if (!node.isNull())
00246 {
00247 QDomText textNode;
00248
00249 if (node.hasChildNodes())
00250 {
00251
00252 textNode = node.firstChild().toText();
00253 textNode.setNodeValue( sValue );
00254 }
00255 else
00256 {
00257 textNode = m_config.createTextNode( sValue );
00258 node.appendChild( textNode );
00259 }
00260 }
00261 }
00262
00264
00266
00267 void XmlConfiguration::SetValue( const QString &sSetting, QString sValue )
00268 {
00269 QDomNode node = FindNode( sSetting, true );
00270
00271 if (!node.isNull())
00272 {
00273 QDomText textNode;
00274
00275 if (node.hasChildNodes())
00276 {
00277
00278 textNode = node.firstChild().toText();
00279 textNode.setNodeValue( sValue );
00280 }
00281 else
00282 {
00283 textNode = m_config.createTextNode( sValue );
00284 node.appendChild( textNode );
00285 }
00286 }
00287 }
00288
00290
00292
00293 void XmlConfiguration::ClearValue( const QString &sSetting )
00294 {
00295 QDomNode node = FindNode(sSetting);
00296 if (!node.isNull())
00297 {
00298 QDomNode parent = node.parentNode();
00299 parent.removeChild(node);
00300 while (parent.childNodes().count() == 0)
00301 {
00302 QDomNode next_parent = parent.parentNode();
00303 next_parent.removeChild(parent);
00304 parent = next_parent;
00305 }
00306 }
00307 }
00308
00311
00312
00313
00316
00317 DBConfiguration::DBConfiguration( )
00318 {
00319 }
00320
00322
00324
00325 bool DBConfiguration::Load( void )
00326 {
00327 return true;
00328 }
00329
00331
00333
00334 bool DBConfiguration::Save( void )
00335 {
00336 return true;
00337 }
00338
00340
00342
00343 int DBConfiguration::GetValue( const QString &sSetting, int nDefault )
00344 {
00345 return GetMythDB()->GetNumSetting( sSetting, nDefault );
00346 }
00347
00349
00351
00352 QString DBConfiguration::GetValue( const QString &sSetting, QString sDefault )
00353 {
00354 return GetMythDB()->GetSetting( sSetting, sDefault );
00355 }
00356
00358
00360
00361 void DBConfiguration::SetValue( const QString &sSetting, int nValue )
00362 {
00363 GetMythDB()->SaveSetting( sSetting, nValue );
00364
00365 }
00366
00368
00370
00371 void DBConfiguration::SetValue( const QString &sSetting, QString sValue )
00372 {
00373 GetMythDB()->SaveSetting( sSetting, sValue );
00374 }
00375
00377
00379
00380 void DBConfiguration::ClearValue( const QString &sSetting )
00381 {
00382 GetMythDB()->ClearSetting( sSetting );
00383 }