Queries for sofian.conf generator
[ipypbx] / src / ipypbx / controllers.py
1 # Copyright (c) Stas Shtin, 2010
2
3 # This file is part of IPyPBX.
4
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.
9
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.
14
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/>.
17
18 """
19 GUI controllers.
20 """
21
22 from ipypbx import http
23 from PyQt4 import QtCore, QtGui, QtSql
24
25
26 class BaseController(QtCore.QObject):
27     """
28     Base class for other controllers.
29
30     Doesn't do anything useful on its own.
31     """
32     # TODO: possibly use a separate class for options and a meta-class.
33     fields = ()
34     view_list_fields = ()
35     view_display_fields = ()
36     view_display_fields_hidden = 'ID', 'Connection ID'
37     is_bound_to_connection = True
38     relations = ()
39     delegate = None
40     
41     def __init__(self, parent=None, views=None):
42         super(BaseController, self).__init__(parent=parent)
43
44         self.views = views
45         
46         # Find out base name.
47         classname = self.__class__.__name__
48         self.basename = (
49             classname[:-10] if classname.endswith('Controller')
50             else classname)
51         self.basename = self.basename[0].lower() + self.basename[1:]
52
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)
57
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)))
65
66         # Fetch model data.
67         self.model.select()
68
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)
73         
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)
78
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)
85
86         # Select first row.
87         self.view_list.selectRow(0)
88
89         # Get view display from the parent.
90         self.view_display = QtGui.QDataWidgetMapper(parent)
91         self.view_display.setModel(self.model)
92         
93         display_fields = self.getDisplayFields()
94         
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)
99
100         # Set relations for model & view display.
101         if self.relations:
102             self.delegate = QtSql.QSqlRelationalDelegate(self)
103             self.view_display.setItemDelegate(self.delegate)
104
105             for data in self.relations:
106                 column, name, table, display = data                
107                 column_index = self.model.fieldIndex(column)
108
109                 # SetRelation screws table data filtering?
110                 self.model.setRelation(
111                     column_index,
112                     QtSql.QSqlRelation('ipypbxweb_%s' % table, 'id', display))
113                 #self.model.select()
114
115                 rel = self.model.relationModel(column_index)
116
117                 widget = self.getFieldWidget(name)
118                 widget.setModel(self.parent().controllers[table].model)
119                 widget.setModelColumn(rel.fieldIndex(display))
120                 #widget.setItemDelegate(self.delegate)
121
122
123         # Select first row in the view list.
124         self.view_display.toFirst()
125
126         # Signals for this controller.
127         signal_data = (
128             (getattr(self.views, self.basename + 'Add'), 'clicked()',
129              self.add),
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()',
139              self.save))
140
141         # Connect all signals.
142         for data in signal_data:
143             if len(data) == 3:
144                 # Connect to python function.
145                 sender, signal, receiver = data
146                 QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver)
147             elif len(data) == 4:
148                 # Connect to Qt slot.
149                 sender, signal, receiver, slot = data
150                 QtCore.QObject.connect(
151                     sender, QtCore.SIGNAL(signal), receiver, QtCore.SLOT(slot))
152                                        
153     def getFieldWidget(self, field):
154         """
155         Return widget for given field name.
156         """
157         return getattr(
158             self.views,
159             self.basename + ''.join(word.capitalize()
160                                     for word in field.split(' ')))
161
162     def getDisplayFields(self):
163         """
164         Return list of display fields.
165         
166         If view_display_fields is not send, display all fields except
167         the first one that is usually the ID.
168         """
169         return [
170             field for field in self.fields
171             if not field in self.view_display_fields_hidden]        
172
173     def add(self):
174         """
175         Add new object.
176         """
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)
181
182         # Disable adding more than one row.
183         self.getFieldWidget('Add').setEnabled(False)
184
185         # Focust to the first displayed field.
186         self.getFieldWidget(self.getDisplayFields()[0]).setFocus()
187
188     def connectionChange(self, index, row):
189         """
190         Overload to handle connection change.
191         """
192         return NotImplemented
193
194     def save(self):
195         """
196         Save to database.
197         """
198         index = self.view_list.currentIndex()
199         self.view_display.submit()
200         self.view_list.setCurrentIndex(index)
201         self.getFieldWidget('Add').setEnabled(True)
202
203
204 class ConnectionController(BaseController):
205     """
206     Connections controller.
207     """
208     fields = (
209         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
210         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
211         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Local IP Address'),
212         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Local Port'),
213         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Freeswitch IP Address'),
214         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Freeswitch Port'))
215     view_list_fields = 'Name', 'Freeswitch IP Address', 'Freeswitch Port'
216     servers = []
217
218     def __init__(self, parent=None, views=None):
219         super(ConnectionController, self).__init__(parent, views)
220
221         for row in range(self.model.rowCount()):
222             # Get local IP address and port from the table for each row.
223             server = http.FreeswitchConfigServer(self.parent(), )
224             server.setSocketData(*self.getSocketData(row))
225             server.startServer()
226             self.servers.append(server)
227
228     def getSocketData(self, row):
229         """
230         Return socket data for given row number.
231         """
232         record = self.model.record(row)
233         
234         # Local IP address.
235         local_ip_address = record.value('local_ip_address').toString()
236
237         # Local port.
238         local_port, _ok = record.value('local_port').toInt()
239         if not _ok:
240             local_port = None
241
242         # Connection ID.
243         connection_id, _ok = record.value('id').toInt()
244         if not _ok:
245             connection_id = None
246         
247         print local_ip_address, local_port, connection_id
248         return local_ip_address, local_port, connection_id
249     
250     def connectionChange(self, index):
251         """
252         Restart config server on connection change if necessary.
253         """
254         current_row = index.row()
255         if current_row != -1:
256             # Select the new row.
257             connection_id, _ok = index.model().data(
258                 index.sibling(current_row, 0)).toInt()
259
260             # Apply new socket location.
261             self.servers[current_row].setSocketData(
262                 *self.getSocketData(current_row))
263         
264     def objectAdded(self, row, record):
265         """
266         New connection added.
267         """
268         self.addServer(*self.getSocketData(row))
269         
270     def addServer(self, host=None, port=None, connection_id=None):
271         """
272         Add a new config server.
273         """
274         server = http.FreeswitchConfigServer(self.model)
275         server.setSocketData(host, port, connection_id)
276         server.startServer()
277         self.servers.append(server)
278
279
280 class ConnectionChangeListenerController(BaseController):
281     """
282     Mixin class for reacting on connection change.
283     """
284     def connectionChange(self, index):
285         """
286         Connection change handler.
287
288         Filters table by a new connection ID and stores last connection ID
289         locally.
290         """
291         index_row = index.row()
292         if index_row != -1:
293             # Get connection_id field value.
294             connection_id, _ok = index.model().data(
295                 index.sibling(index.row(), 0)).toInt()
296             self.connection_id = connection_id
297
298             # Filter is customizable in order to allow ugly hacks :-)
299             self.model.setFilter(self.getFilter(connection_id))
300
301             # Select first row.
302             self.view_list.selectRow(0)
303
304             # Create a new object if none exist.
305             if not self.model.rowCount():
306                 self.add()
307
308     def getFilter(self, connection_id):
309         return 'ipypbxweb_%s.connection_id = %i' % (
310             self.basename, connection_id)        
311
312     def objectAdded(self, row, record):
313         """
314         Set connection_id from currently selected connection.
315         """
316         record.setValue('connection_id', self.connection_id)
317
318         
319 class SipProfileController(ConnectionChangeListenerController):
320     """
321     SIP Profile controller.
322     """
323     fields = (
324         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
325         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
326         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
327         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'External RTP IP'),
328         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'External SIP IP'),
329         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'RTP IP'),
330         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP IP'),
331         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Port'),
332         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Accept Blind Registration'),
333         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Authenticate Calls'),
334         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
335     view_list_fields = 'Name', 'SIP IP', 'SIP Port'
336     
337
338 class DomainController(ConnectionChangeListenerController):
339     """
340     Domain controller.
341     """
342     fields = (
343         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
344         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
345         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Profile ID'),
346         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Host Name'),
347         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
348     view_list_fields = 'SIP Profile ID', 'Host Name'
349     relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'name'),)
350     
351
352 class GatewayController(ConnectionChangeListenerController):
353     """
354     Gateway controller.
355     """
356     fields = (
357         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
358         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
359         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'SIP Profile ID'),
360         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Name'),
361         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Username'),
362         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Password'),
363         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Realm'),
364         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'From Domain'),
365         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Extension'),
366         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Expire In Seconds'),
367         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Retry In Seconds'),
368         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Caller ID In From Field'),
369         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
370     view_list_fields = 'SIP Profile ID', 'Name'
371     relations = (('sip_profile_id', 'SIP Profile ID', 'sipprofile', 'name'),)
372     
373
374 class EndpointController(ConnectionChangeListenerController):
375     """
376     Endpoint controller.
377     """
378     fields = (
379         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
380         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
381         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'User ID'),
382         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Password'),
383         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Domain ID'),
384         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
385     view_list_fields = 'User ID', 'Domain ID'
386     relations = (('domain_id', 'Domain ID', 'domain', 'host_name'),)
387     
388
389 class ExtensionController(ConnectionChangeListenerController):
390     """
391     Extension controller.
392     """
393     fields = (
394         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'ID'),
395         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Connection ID'),
396         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Destination Match'),
397         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'XML Dialplan'),
398         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Domain ID'),
399         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Endpoint ID'),
400         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Authenticate Calls'),
401         QtCore.QT_TRANSLATE_NOOP('MainWindow', 'Is Active'))
402     view_list_fields = 'Destination Match',
403     view_display_fields_hidden = 'ID', 'Connection ID', 'Endpoint ID'
404     relations = (
405         ('domain_id', 'Domain ID', 'domain', 'host_name'),
406 #        ('endpoint_id', 'Endpoint ID', 'endpoint', 'user_id'),
407         )
408         
409     def objectAdded(self, row, record):
410         record.setValue(
411             'xml_dialplan', '<action application="echo" data=""/>')
412         super(ExtensionController, self).objectAdded(row, record)
413         
414     def getFilter(self, connection_id):
415         # Workaround for Qt bug:
416         # http://bugreports.qt.nokia.com/browse/QTBUG-8217 . Apparently they
417         # don't hurry to fix it.
418         return '1 = 1) or (ipypbxweb_%s.connection_id = %i' % (
419             self.basename, connection_id)