1 # Copyright (c) Stas Shtin, 2010
3 # This file is part of IPyPBX.
5 # IPyPBX is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # IPyPBX is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with IPyPBX. If not, see <http://www.gnu.org/licenses/>.
22 from ipypbx import http
23 from PyQt4 import QtCore, QtGui, QtSql
26 class BaseController(QtCore.QObject):
28 Base class for other controllers.
30 Doesn't do anything useful on its own.
32 # TODO: possibly use a separate class for options and a meta-class.
35 view_display_fields = ()
36 view_display_fields_hidden = 'ID', 'Connection ID'
37 is_bound_to_connection = True
41 def __init__(self, parent=None, views=None):
42 super(BaseController, self).__init__(parent=parent)
47 classname = self.__class__.__name__
49 classname[:-10] if classname.endswith('Controller')
51 self.basename = self.basename[0].lower() + self.basename[1:]
53 # Initialize a new model.
54 self.model = QtSql.QSqlRelationalTableModel(parent)
55 self.model.setTable('ipypbxweb_%s' % self.basename.lower())
56 self.model.setEditStrategy(self.model.OnRowChange)
58 # Create model header from fields list.
59 for i, field in enumerate(self.fields):
60 self.model.setHeaderData(
61 i, QtCore.Qt.Horizontal,
62 QtCore.QVariant(QtGui.QApplication.translate(
63 "MainWindow", field, None,
64 QtGui.QApplication.UnicodeUTF8)))
69 # Otherwise get view list from the parent.
70 self.view_list = getattr(views, self.basename + 'ViewList')
71 self.view_list.setModel(self.model)
72 self.view_list.setSelectionMode(self.view_list.SingleSelection)
74 # Hide fields not meant for display.
75 for i, field in enumerate(self.fields):
76 if field not in self.view_list_fields:
77 self.view_list.hideColumn(i)
79 # Stretch headers to fill all available width.
80 self.view_list.setSelectionMode(QtGui.QTableView.SingleSelection)
81 self.view_list.setSelectionBehavior(QtGui.QTableView.SelectRows)
82 self.view_list.resizeColumnsToContents()
83 self.view_list.resizeRowsToContents()
84 self.view_list.horizontalHeader().setStretchLastSection(True)
87 self.view_list.selectRow(0)
89 # Get view display from the parent.
90 self.view_display = QtGui.QDataWidgetMapper(parent)
91 self.view_display.setModel(self.model)
93 display_fields = self.getDisplayFields()
95 for i, field in enumerate(self.fields):
96 if field in display_fields:
97 field_widget = self.getFieldWidget(field)
98 self.view_display.addMapping(field_widget, i)
100 # Set relations for model & view display.
102 self.delegate = QtSql.QSqlRelationalDelegate(self)
103 self.view_display.setItemDelegate(self.delegate)
105 for data in self.relations:
106 column, name, table, display = data
107 column_index = self.model.fieldIndex(column)
109 # SetRelation screws table data filtering?
110 self.model.setRelation(
112 QtSql.QSqlRelation('ipypbxweb_%s' % table, 'id', display))
115 rel = self.model.relationModel(column_index)
117 widget = self.getFieldWidget(name)
118 widget.setModel(self.parent().controllers[table].model)
119 widget.setModelColumn(rel.fieldIndex(display))
120 #widget.setItemDelegate(self.delegate)
123 # Select first row in the view list.
124 self.view_display.toFirst()
126 # Signals for this controller.
128 (getattr(self.views, self.basename + 'Add'), 'clicked()',
130 (self.model, 'primeInsert(int,QSqlRecord&)', self.objectAdded),
131 (self.view_list.selectionModel(),
132 'currentRowChanged(QModelIndex,QModelIndex)',
133 self.view_display, 'setCurrentModelIndex(QModelIndex)'),
134 (self.parent().controllers.get('connection', self
135 ).view_list.selectionModel(),
136 'currentRowChanged(QModelIndex,QModelIndex)',
137 self.connectionChange),
138 (getattr(self.views, self.basename + 'Save'), 'clicked()',
141 # Connect all signals.
142 for data in signal_data:
144 # Connect to python function.
145 sender, signal, receiver = data
146 QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver)
148 # Connect to Qt slot.
149 sender, signal, receiver, slot = data
150 QtCore.QObject.connect(
151 sender, QtCore.SIGNAL(signal), receiver, QtCore.SLOT(slot))
153 def getFieldWidget(self, field):
155 Return widget for given field name.
159 self.basename + ''.join(word.capitalize()
160 for word in field.split(' ')))
162 def getDisplayFields(self):
164 Return list of display fields.
166 If view_display_fields is not send, display all fields except
167 the first one that is usually the ID.
170 field for field in self.fields
171 if not field in self.view_display_fields_hidden]
177 # Add a new row to list view.
178 num_rows = self.model.rowCount()
179 self.model.insertRows(num_rows, 1)
180 self.view_list.selectRow(num_rows)
182 # Disable adding more than one row.
183 self.getFieldWidget('Add').setEnabled(False)
185 # Focust to the first displayed field.
186 self.getFieldWidget(self.getDisplayFields()[0]).setFocus()
188 def connectionChange(self, index, row):
190 Overload to handle connection change.
192 return NotImplemented
198 self.view_display.submit()
199 self.getFieldWidget('Add').setEnabled(True)
202 class ConnectionController(BaseController):
204 Connections controller.
207 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
208 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
209 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Local IP Address'),
210 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Local Port'),
211 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Freeswitch IP Address'),
212 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Freeswitch Port'))
213 view_list_fields = 'Name', 'Freeswitch IP Address', 'Freeswitch Port'
216 def __init__(self, parent=None, views=None):
217 super(ConnectionController, self).__init__(parent, views)
219 # We track last selected row because Qt deselects in on submit.
222 for row in range(self.model.rowCount()):
223 # Get local IP address and port from the table for each row.
224 server = http.FreeswitchConfigServer()
225 server.setSocketData(*self.getSocketData(row))
227 self.servers.append(server)
229 def getSocketData(self, row):
230 local_ip_address = self.model.record(row).value(
231 'local_ip_address').toString()
232 local_port, _ok = self.model.record(row).value(
233 'local_port').toInt()
236 return local_ip_address, local_port
238 def connectionChange(self, index):
240 Restart config server on connection change if necessary.
242 current_row = index.row()
243 if current_row != -1:
244 self.last_row = current_row
245 # Select the new row.
246 connection_id, _ok = index.model().data(
247 index.sibling(index.row(), 0)).toInt()
249 # Apply new socket location.
250 self.servers[self.last_row].setSocketData(
251 *self.getSocketData(self.last_row))
253 def objectAdded(self, row, record):
255 New connection added.
259 def addServer(self, host=None, port=None):
261 Add a new config server.
263 server = http.FreeswitchConfigServer(self)
264 server.setSocketData(host, port)
266 self.servers.append(server)
269 super(ConnectionController, self).save()
270 self.view_list.selectRow(self.last_row)
273 class ConnectionChangeListenerController(BaseController):
275 Mixin class for reacting on connection change.
277 def connectionChange(self, index):
279 Connection change handler.
281 Filters table by a new connection ID and stores last connection ID
284 if index.row() != -1:
285 connection_id, _ok = index.model().data(
286 index.sibling(index.row(), 0)).toInt()
287 self.connection_id = connection_id
289 # Filter is customizable in order to allow ugly hacks :-)
290 self.model.setFilter(self.getFilter(connection_id))
293 self.view_list.selectRow(0)
295 # Create a new object if none exist.
296 if not self.model.rowCount():
299 def getFilter(self, connection_id):
300 return 'ipypbxweb_%s.connection_id = %i' % (
301 self.basename, connection_id)
303 def objectAdded(self, row, record):
305 Set connection_id from currently selected connection.
307 record.setValue('connection_id', self.connection_id)
310 class SipProfileController(ConnectionChangeListenerController):
312 SIP Profile controller.
315 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
316 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
317 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
318 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'External RTP IP'),
319 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'External SIP IP'),
320 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'RTP IP'),
321 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP IP'),
322 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Port'),
323 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Accept Blind Registration'),
324 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Authenticate Calls'),
325 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
326 view_list_fields = 'Name', 'SIP IP', 'SIP Port'
329 class DomainController(ConnectionChangeListenerController):
334 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
335 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
336 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Profile ID'),
337 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Host Name'),
338 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
339 view_list_fields = 'SIP Profile ID', 'Host Name'
340 relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'name'),)
343 class GatewayController(ConnectionChangeListenerController):
348 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
349 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
350 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Profile ID'),
351 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
352 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Username'),
353 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Password'),
354 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Realm'),
355 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'From Domain'),
356 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Extension'),
357 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Expire In Seconds'),
358 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Retry In Seconds'),
359 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Caller ID In From Field'),
360 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
361 view_list_fields = 'SIP Profile ID', 'Name'
362 relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'name'),)
365 class EndpointController(ConnectionChangeListenerController):
370 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
371 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
372 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'User ID'),
373 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Password'),
374 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Domain ID'),
375 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
376 view_list_fields = 'User ID', 'Domain ID'
377 relations = (('domain_id', 'Domain ID', 'domain', 'host_name'),)
380 class ExtensionController(ConnectionChangeListenerController):
382 Extension controller.
385 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
386 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
387 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Destination Match'),
388 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'XML Dialplan'),
389 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Domain ID'),
390 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Endpoint ID'),
391 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Authenticate Calls'),
392 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
393 view_list_fields = 'Destination Match',
395 ('domain_id', 'Domain ID', 'domain', 'host_name'),
396 ('endpoint_id', 'Endpoint ID', 'endpoint', 'user_id'))
398 def objectAdded(self, row, record):
400 'xml_dialplan', '<action application="echo" data=""/>')
401 super(ExtensionController, self).objectAdded(row, record)
403 def getFilter(self, connection_id):
404 # Workaround for Qt bug:
405 # http://bugreports.qt.nokia.com/browse/QTBUG-8217 . Apparently they
406 # don't hurry to fix it.
407 return '1 = 1) or (ipypbxweb_%s.connection_id = %i' % (
408 self.basename, connection_id)