isettings.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "isettings.h"
  2. #include <QtGui>
  3. QMutex *ISettings::mutex = new QMutex();
  4. QDomDocument *ISettings::xml = 0;
  5. ISettings::ISettings(QObject *parent) :
  6. QObject(parent)
  7. {
  8. ISettings::mutex->lock();
  9. if (!(ISettings::xml)) {
  10. QFile *f = new QFile(QString("%1%2%3").arg(SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR ,"freeswitch.xml"));
  11. if ( !f->open(QIODevice::ReadOnly | QIODevice::Text ) ) {
  12. /* TODO: Let the user know */
  13. qDebug() << "Could not read from file.";
  14. return;
  15. }
  16. QString errMsg;
  17. int errLine = 0, errCol = 0;
  18. ISettings::xml = new QDomDocument();
  19. if ( !ISettings::xml->setContent(f, &errMsg, &errLine, &errCol) ) {
  20. /* TODO: Let the user know */
  21. qDebug() << "Could not set content";
  22. }
  23. f->close();
  24. delete(f);
  25. }
  26. ISettings::mutex->unlock();
  27. }
  28. QDomElement ISettings::getConfigNode(QString module) {
  29. /* We don't need to lock since we are just reading (true?) */
  30. QDomElement e = ISettings::xml->documentElement();
  31. QDomNodeList nl = e.elementsByTagName("configuration");
  32. for(int i = 0; i < nl.count(); i++) {
  33. QDomElement el = nl.at(i).toElement();
  34. if ( el.attribute("name") == module ) {
  35. return el;
  36. }
  37. }
  38. return QDomElement();
  39. }
  40. void ISettings::setConfigNode(QDomElement node, QString module) {
  41. ISettings::mutex->lock();
  42. QDomElement e = ISettings::xml->documentElement();
  43. QDomNodeList l = e.elementsByTagName("configuration");
  44. for (int i = 0; i < l.count(); i++) {
  45. QDomElement el = l.at(i).toElement();
  46. if ( el.attribute("name") == module ) {
  47. /* Found the proper module to replace */
  48. el.parentNode().replaceChild(node.toDocumentFragment(),el);
  49. }
  50. }
  51. ISettings::mutex->unlock();
  52. }
  53. void ISettings::saveToFile() {
  54. ISettings::mutex->lock();
  55. if (ISettings::xml) {
  56. QFile *f = new QFile(QString("%1%2%3").arg(SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR ,"freeswitch.xml"));
  57. if ( !f->open(QFile::WriteOnly | QFile::Truncate) ) {
  58. /* TODO: Let the user know */
  59. qDebug() << "Could not open from file.";
  60. return;
  61. }
  62. QTextStream out(f);
  63. ISettings::xml->save(out, 2);
  64. f->close();
  65. if ( !f->open(QFile::ReadOnly) ) {
  66. /* TODO: Let the user know */
  67. qDebug() << "Could not open from file.";
  68. return;
  69. }
  70. QString errMsg;
  71. int errLine = 0, errCol = 0;
  72. if ( !ISettings::xml->setContent(f, &errMsg, &errLine, &errCol) ) {
  73. /* TODO: Let the user know */
  74. qDebug() << "Could not set content";
  75. }
  76. f->close();
  77. delete(f);
  78. }
  79. ISettings::mutex->unlock();
  80. }