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/>.
18 from PyQt4 import QtCore, QtGui, QtSql
21 class BaseController(QtCore.QObject):
23 Base class for other controllers.
25 Doesn't do anything useful on its own.
27 # TODO: possibly use a separate class for options and a meta-class.
30 view_display_fields = ()
31 view_display_fields_hidden = 'ID', 'Connection ID'
32 is_bound_to_connection = True
36 def __init__(self, model=None, view_list=None, view_display=None, parent=None, views=None):
37 super(BaseController, self).__init__(parent=parent)
42 classname = self.__class__.__name__
44 classname[:-10] if classname.endswith('Controller')
46 self.basename = self.basename[0].lower() + self.basename[1:]
48 # Are we given an existing model?
51 # Otherwise initialize a new model.
53 self.model = QtSql.QSqlRelationalTableModel(parent)
54 self.model.setTable('ipypbxweb_%s' % self.basename.lower())
55 self.model.setEditStrategy(self.model.OnRowChange)
57 # Create model header from fields list.
58 for i, field in enumerate(self.fields):
59 self.model.setHeaderData(
60 i, QtCore.Qt.Horizontal,
61 QtCore.QVariant(QtGui.QApplication.translate(
62 "MainWindow", field, None,
63 QtGui.QApplication.UnicodeUTF8)))
68 # Are we given an existing view list?
70 self.view_list = view_list
71 # Otherwise get view list from the parent.
73 self.view_list = getattr(views, self.basename + 'ViewList')
74 self.view_list.setModel(self.model)
76 # Hide fields not meant for display.
77 for i, field in enumerate(self.fields):
78 if field not in self.view_list_fields:
79 self.view_list.hideColumn(i)
81 # Stretch headers to fill all available width.
82 self.view_list.setSelectionMode(QtGui.QTableView.SingleSelection)
83 self.view_list.setSelectionBehavior(QtGui.QTableView.SelectRows)
84 self.view_list.resizeColumnsToContents()
85 self.view_list.resizeRowsToContents()
86 self.view_list.horizontalHeader().setStretchLastSection(True)
89 self.view_list.selectRow(0)
91 # Are we given an existing view display?
93 self.view_display = view_display
94 # Otherwise get view display from the parent.
96 self.view_display = QtGui.QDataWidgetMapper(parent)
97 self.view_display.setModel(self.model)
99 display_fields = self.getDisplayFields()
101 for i, field in enumerate(self.fields):
102 if field in display_fields:
103 field_widget = self.getFieldWidget(field)
104 self.view_display.addMapping(field_widget, i)
106 # Set relations for model & view display.
108 self.delegate = QtSql.QSqlRelationalDelegate(self)
109 self.view_display.setItemDelegate(self.delegate)
111 for data in self.relations:
112 column, name, table, display = data
113 column_index = self.model.fieldIndex(column)
115 # SetRelation screws table data filtering?
116 self.model.setRelation(
118 QtSql.QSqlRelation('ipypbxweb_%s' % table, 'id', display))
121 rel = self.model.relationModel(column_index)
123 widget = self.getFieldWidget(name)
124 widget.setModel(self.parent().controllers[table].model)
125 widget.setModelColumn(rel.fieldIndex(display))
126 #widget.setItemDelegate(self.delegate)
129 # Select first row in the view list.
130 self.view_display.toFirst()
132 # Register signals for this controller.
133 for data in self.getSignalsData():
135 sender, signal, receiver = data
136 QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver)
138 sender, signal, receiver, slot = data
139 QtCore.QObject.connect(
140 sender, QtCore.SIGNAL(signal), receiver, QtCore.SLOT(slot))
142 def getFieldWidget(self, field):
144 Return widget for given field name.
148 self.basename + ''.join(word.capitalize()
149 for word in field.split(' ')))
151 def getDisplayFields(self):
153 Return list of display fields.
155 If view_display_fields is not send, display all fields except
156 the first one that is usually the ID.
159 field for field in self.fields
160 if not field in self.view_display_fields_hidden]
162 def getSignalsData(self):
164 Default signals built from controller's base name.
166 # Default signals handle row selection, Add and Save buttons.
168 (getattr(self.views, self.basename + 'Add'), 'clicked()', self.add),
169 (self.view_list.selectionModel(),
170 'currentRowChanged(QModelIndex,QModelIndex)',
171 self.view_display, 'setCurrentModelIndex(QModelIndex)'),
172 (getattr(self.views, self.basename + 'Save'), 'clicked()',
179 # Add a new row to list view.
180 num_rows = self.model.rowCount()
181 self.model.insertRows(num_rows, 1)
182 self.view_list.selectRow(num_rows)
184 # Disable adding more than one row.
185 self.getFieldWidget('Add').setEnabled(False)
187 # Focust to the first displayed field.
188 self.getFieldWidget(self.getDisplayFields()[0]).setFocus()
194 self.view_display.submit()
195 self.getFieldWidget('Add').setEnabled(True)
198 class ConnectionController(BaseController):
200 Connections controller.
203 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
204 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
205 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Local IP Address'),
206 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Local Port'),
207 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Freeswitch IP Address'),
208 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Freeswitch Port'))
209 view_list_fields = 'Name', 'Freeswitch IP Address', 'Freeswitch Port'
212 class ConnectionChangeListenerController(BaseController):
214 Mixin class for reacting on connection change.
216 def getSignalsData(self):
218 Listen to connection change signal.
220 connection_controller = self.parent().controllers['connection']
223 (connection_controller.view_list.selectionModel(),
224 'currentRowChanged(QModelIndex,QModelIndex)',
225 self.connectionChange),
226 (self.model, 'primeInsert(int,QSqlRecord&)',
227 self.setConnectionId)]
228 signals.extend(super(
229 ConnectionChangeListenerController, self).getSignalsData())
232 def connectionChange(self, index):
234 Connection change handler.
236 Filters table by a new connection ID and stores last connection ID
239 if index.row() != -1:
240 connection_id, ok = index.model().data(
241 index.sibling(index.row(), 0)).toInt()
242 self.connection_id = connection_id
243 self.model.setFilter('ipypbxweb_%s.connection_id = %i' % (self.basename, connection_id))
244 self.view_list.selectRow(0)
245 if not self.model.rowCount():
248 def setConnectionId(self, row, record):
249 record.setValue('connection_id', self.connection_id)
252 class SipProfileController(ConnectionChangeListenerController):
254 SIP Profile controller.
257 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
258 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
259 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
260 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'External RTP IP'),
261 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'External SIP IP'),
262 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'RTP IP'),
263 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP IP'),
264 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Port'),
265 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Accept Blind Registration'),
266 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Authenticate Calls'),
267 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
268 view_list_fields = 'Name', 'SIP IP', 'SIP Port'
271 class DomainController(ConnectionChangeListenerController):
276 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
277 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
278 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Profile ID'),
279 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Host Name'),
280 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
281 view_list_fields = 'SIP Profile ID', 'Host Name'
282 relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'name'),)
285 class GatewayController(ConnectionChangeListenerController):
290 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
291 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
292 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Profile ID'),
293 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
294 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Username'),
295 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Password'),
296 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Realm'),
297 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'From Domain'),
298 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Expire In Seconds'),
299 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Retry In Seconds'),
300 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Caller ID In From Field'),
301 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
302 view_list_fields = 'SIP Profile ID', 'Name'
303 relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'name'),)
306 class EndpointController(ConnectionChangeListenerController):
311 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
312 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
313 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'User ID'),
314 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Password'),
315 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Domain ID'),
316 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
317 view_list_fields = 'User ID', 'Domain ID'
318 relations = (('domain_id', 'Domain ID', 'domain', 'host_name'),)
321 class ExtensionController(ConnectionChangeListenerController):
323 Extension controller.
326 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
327 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
328 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Destination Match'),
329 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'XML Dialplan'),
330 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Domain ID'),
331 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Endpoint ID'),
332 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Authenticate Calls'),
333 QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
334 view_list_fields = 'Destination Match',
336 ('domain_id', 'Domain ID', 'domain', 'host_name'),
337 ('endpoint_id', 'Endpoint ID', 'endpoint', 'user_id'))