Queries for sofian.conf generator
[ipypbx] / src / ipypbx / http.py
index dff9b24..a049e42 100644 (file)
@@ -23,30 +23,34 @@ class FreeswitchConfigServer(QtNetwork.QTcpServer):
     """
     TCP server that receives config requests from freeswitch.
     """
-    def __init__(self, parent):
-        super(FreeswitchConfigServer, self).__init__(parent)
+    def __init__(self, window):
+        super(FreeswitchConfigServer, self).__init__(window)
 
         self.host = None
         self.port = None
+        self.connection_id = None
         self.is_running = False
         self.generators = [
-            GenClass(self.parent().model) for GenClass in (
+            GenClass(window, self) for GenClass in (
                 SofiaConfGenerator,)]
         
         self.httpRequestParser = HttpRequestParser(self)
         
-    def setSocketData(self, host, port):
+    def setSocketData(self, host, port, connection_id):
         """
         Set host and port for socket to listen on.
 
         If the settings differ from previous values, server gets restarted.
         """
         # Check if restart is needed before new settings are applied.
-        needs_restart = (host, port) != (self.host, self.port)
+        needs_restart = (
+            (host, port) != (self.host, self.port)) and connection_id
 
         # Save new settings.
         self.host = host
         self.port = port
+        if connection_id:
+            self.connection_id = connection_id
 
         # Restart server if necessary.
         if needs_restart:
@@ -192,8 +196,9 @@ class FreeswitchConfigGenerator(object):
     param_match = {}
     section_name = None
 
-    def __init__(self, model):
+    def __init__(self, model, parent):
         self.model = model
+        self.parent = parent
 
     def canHandle(self, params):
         for key, value in self.param_match.iteritems():
@@ -213,6 +218,7 @@ class FreeswitchConfigGenerator(object):
     def generateConfig(self, params):
         return NotImplemented
 
+    @staticmethod
     def addParams(parent_elt, params):
         for name, value in params:
             etree.SubElement(parent_elt, 'param', name=name, value=value)
@@ -235,17 +241,27 @@ class SofiaConfGenerator(FreeswitchConfigGenerator):
             section_elt, 'configuration', name=self.config_name,
             description='%s config' % self.config_name)
         settings_elt = etree.SubElement(configuration_elt, 'settings')
-        profiles_elt = etree.SubElement(self.settings_elt, 'profiles')
+        profiles_elt = etree.SubElement(settings_elt, 'profiles')
 
+        database = self.model.controllers['connection'].model.database()
+        
         # Create all profiles for current host.
-        for profile in self.parent.get_profiles():
+        profiles_query = database.exec_(
+            'select id from ipypbxweb_sipprofile where connection_id = %i' %
+            self.parent.connection_id)
+        while profiles_query.next():
+            profile_id, _ok = profiles_query.value(0).toInt()
             profile_elt = etree.SubElement(profiles_elt, 'profile')
 
             # Create domains for current profile.
             domains_elt = etree.SubElement(profile_elt, 'domains')
-            for domain in self.parent.get_domains_for_profile(profile):
+
+            domains_query = database.exec_(
+                'select host_name from ipypbxweb_domain where profile_id = '
+                '%i' % profile_id)
+            while domains_query.next():
                 domain_elt = etree.SubElement(
-                    domains_elt, 'domain', name=domain.host_name,
+                    domains_elt, 'domain', name=domains_quey.value(0),
                     alias='true', parse='true')
 
             # Create settings for current profile.