mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-08-13 17:38:59 +00:00
Quite a big commit that might break a lot of things. Will fix tomorrow if it does. There is now the possibility of adding accounts. Make sure to remove your configs before trying this.
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@16297 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
@@ -33,128 +33,106 @@
|
||||
#include <QString>
|
||||
#include <QtGui>
|
||||
#include <QDir>
|
||||
#include <QDomDocument>
|
||||
#include <QDomNodeList>
|
||||
#include <QXmlStreamWriter>
|
||||
#include "mod_qsettings/mod_qsettings.h"
|
||||
|
||||
switch_xml_t XMLBinding::getConfigXML(QString tmpl)
|
||||
{
|
||||
switch_event_t *e;
|
||||
switch_event_create_plain(&e, SWITCH_EVENT_REQUEST_PARAMS);
|
||||
switch_assert(e);
|
||||
|
||||
if (QFile::exists(QString("%1/.fscomm/templates/%2.xml").arg(QDir::homePath(),tmpl))) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
|
||||
"Using template %s.xml on .fscomm/.\n",
|
||||
tmpl.toAscii().constData());
|
||||
}
|
||||
else if(QFile::exists(QString(":/confs/%1.xml").arg(tmpl)))
|
||||
_settings->beginGroup("FreeSWITCH/conf");
|
||||
if (!_settings->childGroups().contains(tmpl))
|
||||
{
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
|
||||
"Template %s.xml, doesn't exist on directory, copying embedded template.\n",
|
||||
tmpl.toAscii().constData());
|
||||
QString dest = QString("%1/.fscomm/templates/%2.xml").arg(QDir::homePath(),tmpl);
|
||||
QString orig = QString(":/confs/%1.xml").arg(tmpl);
|
||||
QFile::copy(orig, dest);
|
||||
}
|
||||
|
||||
QFile tmplFile(QString("%1/.fscomm/templates/%2.xml").arg(QDir::homePath(),tmpl));
|
||||
if (!tmplFile.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||
{
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Template %s could not be read!\n", tmpl.toAscii().constData());
|
||||
_settings->endGroup();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Open template file and expand all strings based on QSettings */
|
||||
QByteArray tmplContents(tmplFile.readAll());
|
||||
tmplFile.close();
|
||||
|
||||
_settings->beginGroup("FreeSWITCH/conf");
|
||||
_settings->beginGroup(tmpl);
|
||||
foreach(QString k, _settings->childKeys())
|
||||
|
||||
QByteArray *finalXML = new QByteArray();
|
||||
QXmlStreamWriter streamWriter(finalXML);
|
||||
|
||||
streamWriter.setAutoFormatting(true);
|
||||
streamWriter.writeStartElement("document");
|
||||
streamWriter.writeAttribute("type", "freeswitch/xml");
|
||||
|
||||
streamWriter.writeStartElement("section");
|
||||
streamWriter.writeAttribute("name", "configuration");
|
||||
|
||||
streamWriter.writeStartElement("configuration");
|
||||
streamWriter.writeAttribute("name", tmpl);
|
||||
streamWriter.writeAttribute("description", "Configuration generated by QSettings");
|
||||
|
||||
foreach (QString group, _settings->childGroups())
|
||||
{
|
||||
switch_event_add_header_string(e, SWITCH_STACK_BOTTOM, k.toAscii().constData(), _settings->value(k).toByteArray().constData());
|
||||
parseGroup(&streamWriter, group);
|
||||
}
|
||||
|
||||
streamWriter.writeEndElement();
|
||||
streamWriter.writeEndElement();
|
||||
streamWriter.writeEndElement();
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Config for %s requested. Providing the following XML:\n%s\n",
|
||||
tmpl.toAscii().constData(), finalXML->data());
|
||||
|
||||
_settings->endGroup();
|
||||
_settings->endGroup();
|
||||
|
||||
char *res = switch_event_expand_headers(e, tmplContents.data());
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Template %s as follows:\n%s", tmpl.toAscii().constData(), res);
|
||||
switch_safe_free(e);
|
||||
if (tmpl == "sofia.conf")
|
||||
{
|
||||
return proccessAccounts(tmpl, res);
|
||||
}
|
||||
return switch_xml_parse_str(res, strlen(res));
|
||||
return switch_xml_parse_str(finalXML->data(), strlen(finalXML->data()));
|
||||
}
|
||||
|
||||
switch_xml_t XMLBinding::proccessAccounts(QString tmpl, QByteArray xml)
|
||||
void XMLBinding::parseGroup(QXmlStreamWriter *streamWriter, QString group)
|
||||
{
|
||||
char *res = NULL;
|
||||
|
||||
QDomDocument xmlDom;
|
||||
|
||||
/* Process sofia accounts */
|
||||
if (tmpl == "sofia.conf")
|
||||
if (group == "attrs")
|
||||
{
|
||||
int errorLine, errorColumn;
|
||||
QString errorMsg;
|
||||
|
||||
if (!xmlDom.setContent(xml, &errorMsg, &errorLine, &errorColumn))
|
||||
_settings->beginGroup(group);
|
||||
foreach (QString k, _settings->childKeys())
|
||||
{
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not parse the xml template from sofia.conf.xml to add the accounts!\n");
|
||||
}
|
||||
QDomNodeList gatewaysNodeList = xmlDom.elementsByTagName("gateways");
|
||||
if (gatewaysNodeList.isEmpty() || gatewaysNodeList.count() > 1)
|
||||
{
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Where is my gateways tag? Or do we have more then one match?\n");
|
||||
}
|
||||
|
||||
QDomNode gatewaysNode = gatewaysNodeList.at(0);
|
||||
|
||||
_settings->beginGroup("FreeSWITCH/conf/accounts");
|
||||
foreach (QString account, _settings->childGroups())
|
||||
{
|
||||
switch_event_t *e;
|
||||
switch_event_create_plain(&e, SWITCH_EVENT_REQUEST_PARAMS);
|
||||
switch_assert(e);
|
||||
|
||||
_settings->beginGroup(account);
|
||||
switch_event_add_header_string(e, SWITCH_STACK_BOTTOM, "name", account.toAscii().data());
|
||||
foreach (QString k, _settings->childKeys())
|
||||
{
|
||||
switch_event_add_header_string(e, SWITCH_STACK_BOTTOM, k.toAscii().constData(), _settings->value(k).toByteArray().constData());
|
||||
}
|
||||
_settings->endGroup();
|
||||
|
||||
/* Open template file and expand all strings based on QSettings */
|
||||
QFile tmplFile(QString("%1/.fscomm/templates/account.conf.xml").arg(QDir::homePath()));
|
||||
if (!tmplFile.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||
{
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Template for accounts could not be read!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QByteArray tmplContents(tmplFile.readAll());
|
||||
tmplFile.close();
|
||||
|
||||
res = switch_event_expand_headers(e, tmplContents.data());
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Template of account %s as follows:\n%s", account.toAscii().data(), res);
|
||||
|
||||
QDomDocument gatewayXML;
|
||||
if (!gatewayXML.setContent(QByteArray(res), &errorMsg, &errorLine, &errorColumn))
|
||||
{
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "We could not parse the XML for the account!\n");
|
||||
}
|
||||
|
||||
gatewaysNode.appendChild(gatewayXML);
|
||||
switch_safe_free(e);
|
||||
streamWriter->writeAttribute(k, _settings->value(k).toString());
|
||||
}
|
||||
_settings->endGroup();
|
||||
return;
|
||||
}
|
||||
|
||||
return switch_xml_parse_str(xmlDom.toByteArray().data(), strlen(xmlDom.toByteArray().data()));
|
||||
if (group == "params")
|
||||
{
|
||||
_settings->beginGroup(group);
|
||||
foreach(QString param, _settings->childKeys())
|
||||
{
|
||||
streamWriter->writeStartElement("param");
|
||||
streamWriter->writeAttribute("name", param);
|
||||
streamWriter->writeAttribute("value", _settings->value(param).toString());
|
||||
streamWriter->writeEndElement();
|
||||
}
|
||||
_settings->endGroup();
|
||||
return;
|
||||
}
|
||||
|
||||
if (group == "gateways")
|
||||
{
|
||||
streamWriter->writeStartElement(group);
|
||||
_settings->beginGroup(group);
|
||||
foreach (QString gw, _settings->childGroups())
|
||||
{
|
||||
_settings->beginGroup(gw);
|
||||
foreach(QString g, _settings->childGroups())
|
||||
{
|
||||
parseGroup(streamWriter, g);
|
||||
}
|
||||
_settings->endGroup();
|
||||
}
|
||||
_settings->endGroup();
|
||||
streamWriter->writeEndElement();
|
||||
return;
|
||||
}
|
||||
|
||||
_settings->beginGroup(group);
|
||||
streamWriter->writeStartElement(group);
|
||||
|
||||
foreach (QString group2, _settings->childGroups())
|
||||
{
|
||||
parseGroup(streamWriter, group2);
|
||||
}
|
||||
|
||||
streamWriter->writeEndElement();
|
||||
_settings->endGroup();
|
||||
}
|
||||
|
||||
static switch_xml_t xml_url_fetch(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params,
|
||||
|
Reference in New Issue
Block a user