From 162e0ac3866fc7be9586b4cf062867a753e0989f Mon Sep 17 00:00:00 2001 From: Joao Mesquita Date: Fri, 2 Jul 2010 20:17:21 -0300 Subject: [PATCH] We dont depend on QSettings any longer and we are now pure XML configs. --- fscomm/accountmanager.cpp | 32 +++ fscomm/accountmanager.h | 26 +++ fscomm/fscomm.h | 33 +++ fscomm/isettings.cpp | 85 +++++++ fscomm/isettings.h | 20 ++ fscomm/mod_qsettings/mod_qsettings.cpp | 293 ------------------------- fscomm/mod_qsettings/mod_qsettings.h | 55 ----- 7 files changed, 196 insertions(+), 348 deletions(-) create mode 100644 fscomm/accountmanager.cpp create mode 100644 fscomm/accountmanager.h create mode 100644 fscomm/fscomm.h create mode 100644 fscomm/isettings.cpp create mode 100644 fscomm/isettings.h delete mode 100644 fscomm/mod_qsettings/mod_qsettings.cpp delete mode 100644 fscomm/mod_qsettings/mod_qsettings.h diff --git a/fscomm/accountmanager.cpp b/fscomm/accountmanager.cpp new file mode 100644 index 0000000000..7885bd7229 --- /dev/null +++ b/fscomm/accountmanager.cpp @@ -0,0 +1,32 @@ +#include "accountmanager.h" + + +QList > AccountManager::_accounts; + +AccountManager::AccountManager(QObject *parent) : + QObject(parent) +{ + connect(g_FSHost, SIGNAL(newEvent(QSharedPointer)), this, SLOT(newEventSlot(QSharedPointer))); +} + +void AccountManager::newEventSlot(QSharedPointer e) { + QString eName = switch_event_get_header_nil(e.data(), "Event-Name"); + QString eSub = e.data()->subclass_name; + qDebug() << eName; + switch(e.data()->event_id) { + case SWITCH_EVENT_CUSTOM: + { + qDebug() << eName << eSub; + break; + } + case SWITCH_EVENT_API: + { + /* Might not be necessary anymore */ + break; + } + default: + { + break; + } + } +} diff --git a/fscomm/accountmanager.h b/fscomm/accountmanager.h new file mode 100644 index 0000000000..f76d35dd84 --- /dev/null +++ b/fscomm/accountmanager.h @@ -0,0 +1,26 @@ +#ifndef ACCOUNTMANAGER_H +#define ACCOUNTMANAGER_H + +#include +#include "fscomm.h" + +class AccountManager : public QObject +{ +Q_OBJECT +public: + explicit AccountManager(QObject *parent = 0); + void newAccount(Account &acc); + +signals: + void sigNewAccount(Account &acc); + +public slots: +private slots: + void newEventSlot(QSharedPointer); + +private: + static QList > _accounts; + +}; + +#endif // ACCOUNTMANAGER_H diff --git a/fscomm/fscomm.h b/fscomm/fscomm.h new file mode 100644 index 0000000000..7fca6c7e93 --- /dev/null +++ b/fscomm/fscomm.h @@ -0,0 +1,33 @@ +#ifndef FSCOMM_H +#define FSCOMM_H + +#include "isettings.h" +#include "fshost.h" +#include "accountmanager.h" + +#define FSCOMM_GW_STATE_TRYING 0 +#define FSCOMM_GW_STATE_REGISTER 1 +#define FSCOMM_GW_STATE_REGED 2 +#define FSCOMM_GW_STATE_UNREGED 3 +#define FSCOMM_GW_STATE_UNREGISTER 4 +#define FSCOMM_GW_STATE_FAILED 5 +#define FSCOMM_GW_STATE_FAIL_WAIT 6 +#define FSCOMM_GW_STATE_EXPIRED 7 +#define FSCOMM_GW_STATE_NOREG 8 +#define FSCOMM_GW_STATE_NOAVAIL 9 + + +static QString fscomm_gw_state_names[] = { + QString("Trying"), + QString("Registering"), + QString("Registered"), + QString("Un-Registered"), + QString("Un-Registering"), + QString("Failed"), + QString("Failed"), + QString("Expired"), + QString("Not applicable"), + QString("Not available") +}; + +#endif // FSCOMM_H diff --git a/fscomm/isettings.cpp b/fscomm/isettings.cpp new file mode 100644 index 0000000000..8d5216c353 --- /dev/null +++ b/fscomm/isettings.cpp @@ -0,0 +1,85 @@ +#include "isettings.h" +#include + +QMutex *ISettings::mutex = new QMutex(); +QDomDocument *ISettings::xml = 0; + +ISettings::ISettings(QObject *parent) : + QObject(parent) +{ + ISettings::mutex->lock(); + if (!(ISettings::xml)) { + QFile *f = new QFile(QString("%1%2%3").arg(SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR ,"freeswitch.xml")); + if ( !f->open(QIODevice::ReadOnly | QIODevice::Text ) ) { + /* TODO: Let the user know */ + qDebug() << "Could not read from file."; + return; + } + QString errMsg; + int errLine = 0, errCol = 0; + ISettings::xml = new QDomDocument(); + if ( !ISettings::xml->setContent(f, &errMsg, &errLine, &errCol) ) { + /* TODO: Let the user know */ + qDebug() << "Could not set content"; + } + f->close(); + delete(f); + } + ISettings::mutex->unlock(); +} + +QDomElement ISettings::getConfigNode(QString module) { + /* We don't need to lock since we are just reading (true?) */ + QDomElement e = ISettings::xml->documentElement(); + QDomNodeList nl = e.elementsByTagName("configuration"); + for(int i = 0; i < nl.count(); i++) { + QDomElement el = nl.at(i).toElement(); + if ( el.attribute("name") == module ) { + return el; + } + } + return QDomElement(); +} + +void ISettings::setConfigNode(QDomElement node, QString module) { + ISettings::mutex->lock(); + QDomElement e = ISettings::xml->documentElement(); + QDomNodeList l = e.elementsByTagName("configuration"); + for (int i = 0; i < l.count(); i++) { + QDomElement el = l.at(i).toElement(); + if ( el.attribute("name") == module ) { + /* Found the proper module to replace */ + el.parentNode().replaceChild(node.toDocumentFragment(),el); + } + } + ISettings::mutex->unlock(); +} + +void ISettings::saveToFile() { + ISettings::mutex->lock(); + if (ISettings::xml) { + QFile *f = new QFile(QString("%1%2%3").arg(SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR ,"freeswitch.xml")); + if ( !f->open(QFile::WriteOnly | QFile::Truncate) ) { + /* TODO: Let the user know */ + qDebug() << "Could not open from file."; + return; + } + QTextStream out(f); + ISettings::xml->save(out, 2); + f->close(); + if ( !f->open(QFile::ReadOnly) ) { + /* TODO: Let the user know */ + qDebug() << "Could not open from file."; + return; + } + QString errMsg; + int errLine = 0, errCol = 0; + if ( !ISettings::xml->setContent(f, &errMsg, &errLine, &errCol) ) { + /* TODO: Let the user know */ + qDebug() << "Could not set content"; + } + f->close(); + delete(f); + } + ISettings::mutex->unlock(); +} diff --git a/fscomm/isettings.h b/fscomm/isettings.h new file mode 100644 index 0000000000..cd313651e5 --- /dev/null +++ b/fscomm/isettings.h @@ -0,0 +1,20 @@ +#ifndef ISETTINGS_H +#define ISETTINGS_H + +#include +#include +#include "fscomm.h" + +class ISettings : public QObject { + Q_OBJECT +public: + ISettings(QObject *parent = 0); + QDomElement getConfigNode(QString module); + void setConfigNode(QDomElement node, QString module); + void saveToFile(); +private: + static QDomDocument *xml; + static QMutex *mutex; +}; + +#endif // ISETTINGS_H diff --git a/fscomm/mod_qsettings/mod_qsettings.cpp b/fscomm/mod_qsettings/mod_qsettings.cpp deleted file mode 100644 index e8e873b5c7..0000000000 --- a/fscomm/mod_qsettings/mod_qsettings.cpp +++ /dev/null @@ -1,293 +0,0 @@ -/* - * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - * Copyright (C) 2005-2009, Anthony Minessale II - * - * Version: MPL 1.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - * - * The Initial Developer of the Original Code is - * Anthony Minessale II - * Portions created by the Initial Developer are Copyright (C) - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * Joao Mesquita - * - * - * Description: - * Module to load configurations from Qt preference system QSettings - * - */ -#include -#include -#include -#include -#include "mod_qsettings/mod_qsettings.h" - -switch_xml_t XMLBinding::getConfigXML(QString tmpl) -{ - _settings->beginGroup("FreeSWITCH/conf"); - if (!_settings->childGroups().contains(tmpl)) - { - _settings->endGroup(); - return NULL; - } - _settings->beginGroup(tmpl); - - 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()) - { - 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(); - - return switch_xml_parse_str(finalXML->data(), strlen(finalXML->data())); -} - -void XMLBinding::parseGroup(QXmlStreamWriter *streamWriter, QString group) -{ - if (group == "attrs") - { - _settings->beginGroup(group); - foreach (QString k, _settings->childKeys()) - { - streamWriter->writeAttribute(k, _settings->value(k).toString()); - } - _settings->endGroup(); - return; - } - - if (group == "params" || group == "customParams") - { - _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, - void *user_data) -{ - XMLBinding *binding = (XMLBinding *) user_data; - - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "We are being requested -> section: %s | tag_name: %s | key_name: %s | key_value: %s!\n", - section, tag_name, key_name, key_value); - if (!binding) { - return NULL; - } - - return binding->getConfigXML(key_value); -} - -static switch_status_t do_config(void) -{ - char *cf = "qsettings.conf"; - switch_xml_t cfg, xml, bindings_tag; - XMLBinding *binding = NULL; - - if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf); - return SWITCH_STATUS_TERM; - } - - if (!(bindings_tag = switch_xml_child(cfg, "bindings"))) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing tag!\n"); - switch_xml_free(xml); - return SWITCH_STATUS_FALSE; - } - - QString bind_mask = switch_xml_attr_soft(bindings_tag, "value"); - if (!bind_mask.isEmpty()) - { - binding = new XMLBinding(bind_mask); - } - - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding XML Fetch Function [%s]\n", - binding->getBinding().isEmpty() ? "all" : binding->getBinding().toAscii().constData()); - switch_xml_bind_search_function(xml_url_fetch, switch_xml_parse_section_string(binding->getBinding().toAscii().constData()), binding); - binding = NULL; - - switch_xml_free(xml); - - QSettings settings; - settings.beginGroup("FreeSWITCH/conf"); - if (settings.childGroups().isEmpty()) - { - setQSettingsDefaults(); - } - settings.endGroup(); - setGlobals(); - - return SWITCH_STATUS_SUCCESS; -} - -void setQSettingsDefaults() -{ - QSettings settings; - settings.beginGroup("FreeSWITCH/conf"); - - /* Globals config */ - /* Sofia config */ - settings.beginGroup("sofia.conf"); - - /* General Settings */ - settings.beginGroup("global_settings/params"); - settings.setValue("log-level", 0); - settings.setValue("auto-restart", "true"); - settings.setValue("debug-presence", 0); - settings.endGroup(); - - /* Profile settings */ - settings.beginGroup("profiles"); - settings.beginGroup("profile"); - - settings.beginGroup("attrs"); - settings.setValue("name", "softphone"); - settings.endGroup(); - - settings.beginGroup("settings/params"); - settings.setValue("user-agent-string", "FreeSWITCH/FSComm"); - settings.setValue("debug", 0); - settings.setValue("sip-trace", "no"); - settings.setValue("context", "public"); - settings.setValue("rfc2833-pt", 101); - settings.setValue("sip-port", 12345); - settings.setValue("dialplan", "XML"); - settings.setValue("dtmf-duration", 100); - settings.setValue("codec-prefs", "CELT@48000h,G7221@32000h,G7221@16000h,G722,PCMU,PCMA,GSM"); - settings.setValue("use-rtp-timer", "true"); - settings.setValue("rtp-timer-name", "soft"); - settings.setValue("rtp-ip", "auto"); - settings.setValue("sip-ip", "auto"); - settings.setValue("hold-music", "local_stream://moh"); - settings.setValue("apply-nat-acl", "rfc1918"); - settings.setValue("manage-presence", "false"); - settings.setValue("max-proceeding", 3); - settings.setValue("inbound-codec-negotiation", "generous"); - settings.setValue("nonce-ttl", 60); - settings.setValue("auth-calls", "false"); - settings.setValue("auth-all-packets", "false"); - settings.setValue("ext-rtp-ip", "stun:stun.freeswitch.org"); - settings.setValue("ext-sip-ip", "stun:stun.freeswitch.org"); - settings.setValue("rtp-timeout-sec", 300); - settings.setValue("rtp-hold-timeout-sec", 1800); - settings.setValue("disable-register", "true"); - settings.setValue("challenge-realm", "auto_from"); - settings.endGroup(); - - settings.endGroup(); - settings.endGroup(); - settings.endGroup(); - - /* PortAudio config */ - settings.beginGroup("portaudio.conf/settings/params"); - settings.setValue("cid-name", "FSComm"); - settings.setValue("cid-num", "00000000"); - settings.setValue("ring-file", "tone_stream://%(2000,4000,440.0,480.0);loops=20"); - settings.setValue("dialplan", "XML"); - settings.setValue("ring-interval", 5); - settings.setValue("hold-file", "local_stream://moh"); - settings.setValue("sample-rate", 48000); - settings.setValue("codec-ms", 10); - settings.setValue("indev", ""); - settings.setValue("outdev", ""); - settings.setValue("ringdev", ""); - settings.endGroup(); - - /* Finish configs */ - settings.endGroup(); -} - -void setGlobals() -{ - QSettings settings; - settings.beginGroup("FreeSWITCH/conf/globals"); - foreach (QString k, settings.childKeys()) - { - switch_core_set_variable(k.toAscii().data(), settings.value(k).toByteArray().data()); - } - settings.endGroup(); -} - -switch_status_t mod_qsettings_load(void) -{ - - if (do_config() == SWITCH_STATUS_SUCCESS) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Sucessfully configured.\n"); - } else { - return SWITCH_STATUS_FALSE; - } - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "We loaded mod_qsettings.\n"); - - return SWITCH_STATUS_SUCCESS; -} diff --git a/fscomm/mod_qsettings/mod_qsettings.h b/fscomm/mod_qsettings/mod_qsettings.h deleted file mode 100644 index b44a846681..0000000000 --- a/fscomm/mod_qsettings/mod_qsettings.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - * Copyright (C) 2005-2009, Anthony Minessale II - * - * Version: MPL 1.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - * - * The Initial Developer of the Original Code is - * Anthony Minessale II - * Portions created by the Initial Developer are Copyright (C) - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * Joao Mesquita - * - */ - -#ifndef MOD_QSETTINGS_H -#define MOD_QSETTINGS_H - -#include -#include -#include - -class QXmlStreamWriter; - -class XMLBinding -{ -public: - XMLBinding(QString binding) : _binding(binding), _settings(new QSettings) {} - QString getBinding(void) { return _binding; } - switch_xml_t getConfigXML(QString); -private: - void parseGroup(QXmlStreamWriter *,QString); - QString _binding; - QSettings* _settings; -}; - -switch_status_t mod_qsettings_load(void); -void setQSettingsDefaults(void); -void setGlobals(void); - -#endif // MOD_QSETTINGS_H