-from ipypbx import models, state
+# Copyright (c) Stas Shtin, 2010
+# This file is part of IPyPBX.
-class BaseHandler(object):
+# 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/>.
+
+"""
+GUI controllers.
+"""
+
+from ipypbx import http
+from PyQt4 import QtCore, QtGui, QtSql
+
+
+class BaseController(QtCore.QObject):
"""
- Base class for various entity handlers.
+ Base class for other controllers.
Doesn't do anything useful on its own.
"""
- items = None
- current_items = None
+ # TODO: possibly use a separate class for options and a meta-class.
+ fields = ()
+ view_list_fields = ()
+ view_display_fields = ()
+ view_display_fields_hidden = 'ID', 'Connection ID'
+ is_bound_to_connection = True
+ relations = ()
+ delegate = None
+
+ def __init__(self, parent=None, views=None):
+ super(BaseController, self).__init__(parent=parent)
- def __init__(self, parent):
- self.parent = parent
+ self.views = views
+
+ # Find out base name.
+ classname = self.__class__.__name__
+ self.basename = (
+ classname[:-10] if classname.endswith('Controller')
+ else classname)
+ self.basename = self.basename[0].lower() + self.basename[1:]
+ # Initialize a new model.
+ self.model = QtSql.QSqlRelationalTableModel(parent)
+ self.model.setTable('ipypbxweb_%s' % self.basename.lower())
+ self.model.setEditStrategy(self.model.OnRowChange)
-class ConnectionsHandler(BaseHandler):
- """
- Connections handler.
- """
- def select(self, index):
+ # Create model header from fields list.
+ for i, field in enumerate(self.fields):
+ self.model.setHeaderData(
+ i, QtCore.Qt.Horizontal,
+ QtCore.QVariant(QtGui.QApplication.translate(
+ "MainWindow", field, None,
+ QtGui.QApplication.UnicodeUTF8)))
+
+ # Fetch model data.
+ self.model.select()
+
+ # Otherwise get view list from the parent.
+ self.view_list = getattr(views, self.basename + 'ViewList')
+ self.view_list.setModel(self.model)
+ self.view_list.setSelectionMode(self.view_list.SingleSelection)
+
+ # Hide fields not meant for display.
+ for i, field in enumerate(self.fields):
+ 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)
+ self.view_list.setSelectionBehavior(QtGui.QTableView.SelectRows)
+ self.view_list.resizeColumnsToContents()
+ self.view_list.resizeRowsToContents()
+ self.view_list.horizontalHeader().setStretchLastSection(True)
+
+ # Select first row.
+ self.view_list.selectRow(0)
+
+ # Get view display from the parent.
+ 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)
+
+ # Set relations for model & view display.
+ if self.relations:
+ self.delegate = QtSql.QSqlRelationalDelegate(self)
+ self.view_display.setItemDelegate(self.delegate)
+
+ for data in self.relations:
+ column, name, table, display = data
+ column_index = self.model.fieldIndex(column)
+
+ # SetRelation screws table data filtering?
+ self.model.setRelation(
+ column_index,
+ QtSql.QSqlRelation('ipypbxweb_%s' % table, 'id', display))
+ #self.model.select()
+
+ rel = self.model.relationModel(column_index)
+
+ widget = self.getFieldWidget(name)
+ widget.setModel(self.parent().controllers[table].model)
+ widget.setModelColumn(rel.fieldIndex(display))
+ #widget.setItemDelegate(self.delegate)
+
+
+ # Select first row in the view list.
+ self.view_display.toFirst()
+
+ # Register signals for this controller.
+ 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):
"""
- Select another connection as current.
+ Return widget for given field name.
"""
- state.currentConnection = state.connections[index]
+ return getattr(
+ self.views,
+ self.basename + ''.join(word.capitalize()
+ for word in field.split(' ')))
- def clone(self):
+ 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.
"""
- TODO: Clone an existing connection.
+ return [
+ field for field in self.fields
+ if not field in self.view_display_fields_hidden]
- This creates a new connection with bound data copied from another one.
+ def getSignalsData(self):
"""
+ Default signals built from controller's base name.
+ """
+ # Default signals handle row selection, Add and Save buttons.
+ return [
+ (getattr(self.views, self.basename + 'Add'), 'clicked()', self.add),
+ (self.model, 'primeInsert(int,QSqlRecord&)', self.objectAdded),
+ (self.view_list.selectionModel(),
+ 'currentRowChanged(QModelIndex,QModelIndex)',
+ self.view_display, 'setCurrentModelIndex(QModelIndex)'),
+ (getattr(self.views, self.basename + 'Save'), 'clicked()',
+ self.save),
+ ]
def add(self):
"""
- Add a new connection.
+ Add new object.
"""
- 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('')
+ # Add a new row to list view.
+ num_rows = self.model.rowCount()
+ self.model.insertRows(num_rows, 1)
+ self.view_list.selectRow(num_rows)
+
+ # Disable adding more than one row.
+ self.getFieldWidget('Add').setEnabled(False)
+
+ # Focust to the first displayed field.
+ self.getFieldWidget(self.getDisplayFields()[0]).setFocus()
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 to database.
+ """
+ self.view_display.submit()
+ self.getFieldWidget('Add').setEnabled(True)
+
+
+class ConnectionController(BaseController):
+ """
+ Connections controller.
+ """
+ fields = (
+ 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'
+ servers = []
+
+ def __init__(self, parent=None, views=None):
+ super(ConnectionController, self).__init__(parent, views)
+
+ self.last_row = -1
+
+ for row in range(self.model.rowCount()):
+ # Get local IP address and port from the table.
+ local_ip_address = self.model.record(row).value(
+ 'local_ip_address').toString()
+ local_port, _ok = self.model.record(row).value('local_port').toInt()
+ if not _ok:
+ local_port = None
+
+ server = http.FreeswitchConfigServer()
+ server.setSocketData(local_ip_address, local_port)
+ server.startServer()
+ self.servers.append(server)
+
+ def connectionEdit(self, index):
+ """
+ Restart config server on connection change if necessary.
+ """
+ current_row = index.row()
+ if current_row != -1:
+ self.last_row = current_row
+ # Select the new row.
+ connection_id, _ok = index.model().data(
+ index.sibling(index.row(), 0)).toInt()
+ #self.connection_id = connection_id
+ #if not self.model.rowCount():
+ # self.add()
+
+
+ def objectAdded(self, row, record):
+ """
+ New connection added.
+ """
+ self.addServer(record.value('host'), record.value('port'))
+
+ def addServer(self, host, port):
+ """
+ Add a new config server.
+ """
+ server = http.FreeswitchConfigServer(self)
+ server.setSocketData(host, port)
+ server.startServer()
+ self.servers.append(server)
+
+
+class ConnectionChangeListenerController(BaseController):
+ """
+ Mixin class for reacting on connection change.
+ """
+ def getSignalsData(self):
+ """
+ Listen to connection change signal.
+ """
+ # Find connection controller in controller registry.
+ connection_controller = self.parent().controllers['connection']
+
+ signals = [
+ (connection_controller.view_list.selectionModel(),
+ 'currentRowChanged(QModelIndex,QModelIndex)',
+ self.connectionChange)]
+ signals.extend(super(
+ ConnectionChangeListenerController, self).getSignalsData())
+ return signals
+
+ def connectionChange(self, index):
+ """
+ Connection change handler.
+
+ Filters table by a new connection ID and stores last connection ID
+ locally.
+ """
+ if index.row() != -1:
+ connection_id, _ok = index.model().data(
+ index.sibling(index.row(), 0)).toInt()
+ self.connection_id = connection_id
+ self.model.setFilter(
+ 'ipypbxweb_%s.connection_id = %i' %
+ (self.basename, connection_id))
+ self.view_list.selectRow(0)
+ if not self.model.rowCount():
+ self.add()
+
+ def objectAdded(self, row, record):
+ """
+ Set connection_id from currently selected connection.
+ """
+ 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 ID', 'Host Name'
+ relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', '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', 'Extension'),
+ 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'
+ relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', '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'
+ relations = (('domain_id', 'Domain ID', 'domain', 'host_name'),)
+
+
+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',
+ relations = (
+ ('domain_id', 'Domain ID', 'domain', 'host_name'),
+ ('endpoint_id', 'Endpoint ID', 'endpoint', 'user_id'))
+