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