Halfway rewriting to use QT MVC
[ipypbx] / src / ipypbx / controllers.py
index f4cbd26..2bb3169 100644 (file)
@@ -1,4 +1,22 @@
-from ipypbx import models, state
+# Copyright (c) Stas Shtin, 2010
+
+# This file is part of IPyPBX.
+
+# IPyPBX is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# IPyPBX is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with IPyPBX.  If not, see <http://www.gnu.org/licenses/>.
+
+#from ipypbx import models
+from PyQt4 import QtCore
 
 
 class BaseHandler(object):
@@ -12,17 +30,46 @@ class BaseHandler(object):
 
     def __init__(self, parent):
         self.parent = parent
+        self.initState()
+
+    def initState(self):
+        return NotImplemented
 
 
-class ConnectionsHandler(BaseHandler):
+class ConnectionController(QtCore.QObject):
     """
     Connections handler.
     """
-    def select(self, index):
+#    def initState(self):
+#        self.connections = []
+#        #self.connections = list(state.store.query(models.Connection))
+#        self.currentConnection = None
+
+#        for connection in self.connections:
+#            self.parent.ui.connectionList.addItem(connection.name)
+
+#        if self.connections:
+#            print self.parent.ui.connectionList.currentRow()
+#            self.parent.ui.connectionList.setCurrentRow(0)
+#            QtCore.QObject.emit(
+#                self.parent.ui.connectionList, QtCore.SIGNAL('currentRowChanged(int)'), 0)
+        
+    def select(self, row):
         """
         Select another connection as current.
         """
-        state.currentConnection = state.connections[index]
+        self.currentConnection = self.connections[row]
+
+        # Fill in form based on selection.
+        self.parent.ui.connectionName.setText(self.currentConnection.name)
+        self.parent.ui.connectionLocalIpAddress.setText(
+            self.currentConnection.local_ip_address)
+        self.parent.ui.connectionLocalPort.setText(
+            unicode(self.currentConnection.local_port))
+        self.parent.ui.connectionFreeswitchIpAddress.setText(
+            self.currentConnection.freeswitch_ip_address)
+        self.parent.ui.connectionFreeswitchPort.setText(
+            unicode(self.currentConnection.freeswitch_port))
 
     def clone(self):
         """
@@ -33,23 +80,52 @@ class ConnectionsHandler(BaseHandler):
 
     def add(self):
         """
-        Add a new connection.
+        Add new connection.
         """
-        state.currentConnection = None
-        self.parent.ui.connectionName.setText('')
-        self.parent.ui.connectionLocalIpAddress.setText('')
-        self.parent.ui.connectionLocalPort.setText('')
-        self.parent.ui.connectionFreeswitchIpAddress.setText('')
-        self.parent.ui.connectionFreeswitchPort.setText('')
+        print '!'
+        print self.parent
+        self.currentConnection = None
+
+        name_template = 'New connection [{0:02}]'
+        for i in xrange(1, 100):
+            name = name_template.format(i)
+            connection_exists = False
+            for connection in self.connections:
+                if connection.name == name:
+                    connection_exists = True
+                    break
+
+            if not connection_exists:
+                break
+            
+        self.parent.ui.connectionName.setText('New connection')
+        self.parent.ui.connectionName.setFocus()
+        self.parent.ui.connectionName.selectAll()
+        self.parent.ui.connectionLocalIpAddress.clear()
+        self.parent.ui.connectionLocalPort.clear()
+        self.parent.ui.connectionFreeswitchIpAddress.clear()
+        self.parent.ui.connectionFreeswitchPort.clear()
 
     def save(self):
-        connection = models.Connection(
-            name=self.parent.ui.connectionName.getText(),
-            local_ip_address=self.parent.ui.connectionLocalIpAddress.\
-                getText(),
-            local_port=self.parent.ui.connectionLocalPort.getText(),
-            freeswitch_ip_address=self.parent.ui.\
-                connectionFreeswitchIpAddress.getText(),
-            freeswitch_port=self.parent.ui.connectionFreeswitchPort.getText()
-            )
-    #state.currentConnection = 
+        """
+        Save new or existing connection.
+        """
+        name = unicode(self.parent.ui.connectionName.text())
+
+        # Add to connection list if we've created it.
+        if self.currentConnection is None:            
+            #self.currentConnection = models.Connection(store=state.store)            
+            self.connections.append(self.currentConnection)
+            self.parent.ui.connectionList.addItem(name)
+
+        self.currentConnection.name = name
+        self.currentConnection.local_ip_address = unicode(
+            self.parent.ui.connectionLocalIpAddress.text())
+        self.currentConnection.local_port = int(
+            self.parent.ui.connectionLocalPort.text())
+        self.currentConnection.freeswitch_ip_address = unicode(
+            self.parent.ui.connectionFreeswitchIpAddress.text())
+        self.currentConnection.freeswitch_port = int(
+            self.parent.ui.connectionFreeswitchPort.text())
+
+        self.currentConnection.checkpoint()