Queries for sofian.conf generator
authorStas Shtin <antisvin@gmail.com>
Thu, 15 Apr 2010 20:35:01 +0000 (00:35 +0400)
committerStas Shtin <antisvin@gmail.com>
Thu, 15 Apr 2010 20:35:01 +0000 (00:35 +0400)
src/ipypbx/controllers.py
src/ipypbx/http.py

index 2063db9..414eae0 100644 (file)
@@ -220,19 +220,32 @@ class ConnectionController(BaseController):
 
         for row in range(self.model.rowCount()):
             # Get local IP address and port from the table for each row.
-            server = http.FreeswitchConfigServer(self)
+            server = http.FreeswitchConfigServer(self.parent(), )
             server.setSocketData(*self.getSocketData(row))
             server.startServer()
             self.servers.append(server)
 
     def getSocketData(self, row):
-        local_ip_address = self.model.record(row).value(
-            'local_ip_address').toString()
-        local_port, _ok = self.model.record(row).value(
-            'local_port').toInt()
+        """
+        Return socket data for given row number.
+        """
+        record = self.model.record(row)
+        
+        # Local IP address.
+        local_ip_address = record.value('local_ip_address').toString()
+
+        # Local port.
+        local_port, _ok = record.value('local_port').toInt()
         if not _ok:
             local_port = None
-        return local_ip_address, local_port
+
+        # Connection ID.
+        connection_id, _ok = record.value('id').toInt()
+        if not _ok:
+            connection_id = None
+        
+        print local_ip_address, local_port, connection_id
+        return local_ip_address, local_port, connection_id
     
     def connectionChange(self, index):
         """
@@ -252,14 +265,14 @@ class ConnectionController(BaseController):
         """
         New connection added.
         """
-        self.addServer()
+        self.addServer(*self.getSocketData(row))
         
-    def addServer(self, host=None, port=None):
+    def addServer(self, host=None, port=None, connection_id=None):
         """
         Add a new config server.
         """
-        server = http.FreeswitchConfigServer(self)
-        server.setSocketData(host, port)
+        server = http.FreeswitchConfigServer(self.model)
+        server.setSocketData(host, port, connection_id)
         server.startServer()
         self.servers.append(server)
 
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.