Add/edit all tables. Data gets save to DB at last\!
[ipypbx] / src / ipypbx / controllers.py
index 873d98d..6f7f173 100644 (file)
@@ -15,7 +15,6 @@
 # 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, QtGui, QtSql
 
 
@@ -25,49 +24,57 @@ class BaseController(QtCore.QObject):
 
     Doesn't do anything useful on its own.
     """
+    # TODO: possibly use a separate class for options and a meta-class.
     fields = ()
-    hidden_fields = ()
-    visible_fields = ()
+    view_list_fields = ()
+    view_display_fields = ()
+    view_display_fields_hidden = 'ID', 'Connection ID'
+    is_bound_to_connection = True
     
-    def __init__(self, model=None, view_list=None, view_display=None, parent=None):
-        super(BaseController, self).__init__(parent)
+    def __init__(self, model=None, view_list=None, view_display=None, parent=None, views=None):
+        super(BaseController, self).__init__(parent=parent)
+
+        self.views = views
         
         # Find out base name.
         classname = self.__class__.__name__
-        self.base_name = (
+        self.basename = (
             classname[:-10] if classname.endswith('Controller')
             else classname)
-        self.base_name = self.base_name[0].lower() + self.base_name[1:]
+        self.basename = self.basename[0].lower() + self.basename[1:]
 
+        # Are we given an existing model?
         if model:
-            # We're given an existing model.
             self.model = model
+        # Otherwise initialize a new model.
         else:
-            # Initialize a new model.
             self.model = QtSql.QSqlTableModel(parent)
-            self.model.setTable(self.base_name + 's')
+            self.model.setTable('ipypbxweb_%s' % self.basename.lower())
+            self.model.setEditStrategy(self.model.OnRowChange)
 
             # Create model header from fields list.
             for i, field in enumerate(self.fields):
                 self.model.setHeaderData(
-                    i, QtCore.Qt.Horizontal, QtCore.QVariant(field))
+                    i, QtCore.Qt.Horizontal,
+                    QtCore.QVariant(QtGui.QApplication.translate(
+                        "MainWindow", field, None,
+                        QtGui.QApplication.UnicodeUTF8)))
 
             # Fetch model data.
             self.model.select()
 
+        # Are we given an existing view list?
         if view_list:
-            # We're given an existing view list.
             self.view_list = view_list
+        # Otherwise get view list from the parent.            
         else:
-            # Get view list from the parent.
-            self.view_list = getattr(parent, self.base_name + 'ViewList')
+            self.view_list = getattr(views, self.basename + 'ViewList')
             self.view_list.setModel(self.model)
 
             # Hide fields not meant for display.
             for i, field in enumerate(self.fields):
-                if (field in self.hidden_fields or
-                    field not in self.visible_fields):
-                    self.view_list.setColumnHidden(i, True)
+                if field not in self.view_list_fields:
+                    self.view_list.hideColumn(i)
 
             # Stretch headers to fill all available width.
             self.view_list.setSelectionMode(QtGui.QTableView.SingleSelection)
@@ -76,130 +83,224 @@ class BaseController(QtCore.QObject):
             self.view_list.resizeRowsToContents()
             self.view_list.horizontalHeader().setStretchLastSection(True)
 
+        # Select first row.
+        self.view_list.selectRow(0)
+
+        # Are we given an existing view display?
         if view_display:
-            # We're given an existing view display.
             self.view_display = view_display
+        # Otherwise get view display from the parent.
         else:
-            # Get view display from the parent.
-            self.view_display = getattr(parent, self.base_name + 'ViewDisplay')
+            self.view_display = QtGui.QDataWidgetMapper(parent)
             self.view_display.setModel(self.model)
 
+            display_fields = self.getDisplayFields()
+            
+            for i, field in enumerate(self.fields):
+                if field in display_fields:
+                    field_widget = self.getFieldWidget(field)
+                    self.view_display.addMapping(field_widget, i)
+
+        # Select first row in the view list.
+        self.view_display.toFirst()
+        
         # Register signals for this controller.
-        for sender, signal, receiver in self.getSignalsData():
-            QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver)        
+        for data in self.getSignalsData():
+            if len(data) == 3:
+                sender, signal, receiver = data
+                QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver)
+            elif len(data) == 4:
+                sender, signal, receiver, slot = data
+                QtCore.QObject.connect(
+                    sender, QtCore.SIGNAL(signal), receiver, QtCore.SLOT(slot))
+                                       
+    def getFieldWidget(self, field):
+        """
+        Return widget for given field name.
+        """
+        return getattr(
+            self.views,
+            self.basename + ''.join(word.capitalize()
+                                    for word in field.split(' ')))
+
+    def getDisplayFields(self):
+        """
+        Return list of display fields.
+        
+        If view_display_fields is not send, display all fields except
+        the first one that is usually the ID.
+        """
+        return [
+            field for field in self.fields
+            if not field in self.view_display_fields_hidden]        
 
     def getSignalsData(self):
         """
         Default signals built from controller's base name.
         """
-        parent = self.parent()
-        
-        return (
-            (getattr(parent, self.base_name + 'Add'), 'clicked()', self.add),
-            (getattr(parent, self.base_name + 'ViewList'),
-             'currentRowChanged(int)', self.select),
-            (getattr(parent, self.base_name + 'Save'), 'clicked()',
-             self.save),
-            )
+        # Default signals handle row selection, Add and Save buttons.
+        return [
+            (getattr(self.views, self.basename + 'Add'), 'clicked()', self.add),
+            (self.view_list.selectionModel(),
+             'currentRowChanged(QModelIndex,QModelIndex)',
+             self.view_display, 'setCurrentModelIndex(QModelIndex)'),
+            (getattr(self.views, self.basename + 'Save'), 'clicked()',
+             self.save)]
 
     def add(self):
         """
-        TODO: Default implementation.
-        """
-        return NotImplemented
-    
-    def save(self):
+        Add new object.
         """
-        TODO: Default implementation.
-        """
-        return NotImplemented
+        # Add a new row to list view.
+        num_rows = self.model.rowCount()
+        self.model.insertRows(num_rows, 1)
+        self.view_list.selectRow(num_rows)
 
-    def add(self):
+        # Disable adding more than one row.
+        self.getFieldWidget('Add').setEnabled(False)
+
+        # Focust to the first displayed field.
+        self.getFieldWidget(self.getDisplayFields()[0]).setFocus()
+
+        # TODO: set default values?
+
+    def save(self):
         """
-        TODO: Default implementation.
+        Save to database.
         """
-        return NotImplemented
+        self.view_display.submit()
+        self.getFieldWidget('Add').setEnabled(True)
 
 
 class ConnectionController(BaseController):
     """
-    Connections handler.
+    Connections controller.
     """
     fields = (
-        'ID', 'Name', 'Local IP Address', 'Local Port',
-        'Freeswitch IP Address', 'Freeswitch Port')
-    visible_fields = ('Name', 'Freeswitch IP Address', 'Freeswitch Port')
-    
-    def select(self, row):
-        """
-        Select another connection as current.
-        """
-        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):
-        """
-        TODO: Clone an existing connection.
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Local IP Address'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Local Port'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Freeswitch IP Address'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Freeswitch Port'))
+    view_list_fields = 'Name', 'Freeswitch IP Address', 'Freeswitch Port'
 
-        This creates a new connection with bound data copied from another one.
-        """
 
-    def add(self):
+class ConnectionChangeListenerController(BaseController):
+    """
+    Mixin class for reacting on connection change.
+    """
+    def getSignalsData(self):
         """
-        Add new connection.
+        Listen to connection change signal.
         """
-        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()
+        connection_controller = self.parent().controllers[0]
 
-    def save(self):
+        signals = [
+            (connection_controller.view_list.selectionModel(),
+             'currentRowChanged(QModelIndex,QModelIndex)',
+             self.connectionChange),
+            (self.model, 'primeInsert(int,QSqlRecord&)',
+             self.setConnectionId)] 
+        signals.extend(super(
+            ConnectionChangeListenerController, self).getSignalsData())
+        return signals
+        
+    def connectionChange(self, index):
         """
-        Save new or existing connection.
+        Connection change handler.
+
+        Filters table by a new connection ID and stores last connection ID
+        locally.
         """
-        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()
+        if index.row() != -1:
+            connection_id, ok = index.model().data(
+                index.sibling(index.row(), 0)).toInt()
+            self.connection_id = connection_id
+            self.model.setFilter('connection_id = %i' % connection_id)
+
+    def setConnectionId(self, row, record):
+        record.setValue('connection_id', self.connection_id)
+
+        
+class SipProfileController(ConnectionChangeListenerController):
+    """
+    SIP Profile controller.
+    """
+    fields = (
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'External RTP IP'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'External SIP IP'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'RTP IP'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP IP'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Port'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Accept Blind Registration'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Authenticate Calls'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
+    view_list_fields = 'Name', 'SIP IP', 'SIP Port'
+    
+
+class DomainController(ConnectionChangeListenerController):
+    """
+    Domain controller.
+    """
+    fields = (
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Profile ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Host Name'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
+    view_list_fields = 'SIP Profile', 'Host Name'
+
+
+class GatewayController(ConnectionChangeListenerController):
+    """
+    Gateway controller.
+    """
+    fields = (
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Profile ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Username'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Password'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Realm'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'From Domain'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Expire In Seconds'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Retry In Seconds'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Caller ID In From Field'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
+    view_list_fields = 'SIP Profile ID', 'Name'
+
+
+class EndpointController(ConnectionChangeListenerController):
+    """
+    Endpoint controller.
+    """
+    fields = (
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'User ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Password'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Domain ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
+    view_list_fields = 'User ID', 'Domain ID'
+    
+
+class ExtensionController(ConnectionChangeListenerController):
+    """
+    Extension controller.
+    """
+    fields = (
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Destination Match'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'XML Dialplan'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Domain ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Endpoint ID'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Authenticate Calls'),
+        QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
+    view_list_fields = 'Destination Match',
+