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 ipypbx import models
19 from PyQt4 import QtCore, QtGui, QtSql
22 class BaseController(QtCore.QObject):
24 Base class for other controllers.
26 Doesn't do anything useful on its own.
32 def __init__(self, model=None, view_list=None, view_display=None, parent=None):
33 super(BaseController, self).__init__(parent)
36 classname = self.__class__.__name__
38 classname[:-10] if classname.endswith('Controller')
40 self.base_name = self.base_name[0].lower() + self.base_name[1:]
43 # We're given an existing model.
46 # Initialize a new model.
47 self.model = QtSql.QSqlTableModel(parent)
48 self.model.setTable(self.base_name + 's')
50 # Create model header from fields list.
51 for i, field in enumerate(self.fields):
52 self.model.setHeaderData(
53 i, QtCore.Qt.Horizontal, QtCore.QVariant(field))
59 # We're given an existing view list.
60 self.view_list = view_list
62 # Get view list from the parent.
63 self.view_list = getattr(parent, self.base_name + 'ViewList')
64 self.view_list.setModel(self.model)
66 # Hide fields not meant for display.
67 for i, field in enumerate(self.fields):
68 if (field in self.hidden_fields or
69 field not in self.visible_fields):
70 self.view_list.setColumnHidden(i, True)
72 # Stretch headers to fill all available width.
73 self.view_list.setSelectionMode(QtGui.QTableView.SingleSelection)
74 self.view_list.setSelectionBehavior(QtGui.QTableView.SelectRows)
75 self.view_list.resizeColumnsToContents()
76 self.view_list.resizeRowsToContents()
77 self.view_list.horizontalHeader().setStretchLastSection(True)
80 # We're given an existing view display.
81 self.view_display = view_display
83 # Get view display from the parent.
84 self.view_display = getattr(parent, self.base_name + 'ViewDisplay')
85 self.view_display.setModel(self.model)
87 # Register signals for this controller.
88 for sender, signal, receiver in self.getSignalsData():
89 QtCore.QObject.connect(sender, QtCore.SIGNAL(signal), receiver)
91 def getSignalsData(self):
93 Default signals built from controller's base name.
95 parent = self.parent()
98 (getattr(parent, self.base_name + 'Add'), 'clicked()', self.add),
99 (getattr(parent, self.base_name + 'ViewList'),
100 'currentRowChanged(int)', self.select),
101 (getattr(parent, self.base_name + 'Save'), 'clicked()',
107 TODO: Default implementation.
109 return NotImplemented
113 TODO: Default implementation.
115 return NotImplemented
119 TODO: Default implementation.
121 return NotImplemented
124 class ConnectionController(BaseController):
129 'ID', 'Name', 'Local IP Address', 'Local Port',
130 'Freeswitch IP Address', 'Freeswitch Port')
131 visible_fields = ('Name', 'Freeswitch IP Address', 'Freeswitch Port')
133 def select(self, row):
135 Select another connection as current.
137 self.currentConnection = self.connections[row]
139 # Fill in form based on selection.
140 self.parent.ui.connectionName.setText(self.currentConnection.name)
141 self.parent.ui.connectionLocalIpAddress.setText(
142 self.currentConnection.local_ip_address)
143 self.parent.ui.connectionLocalPort.setText(
144 unicode(self.currentConnection.local_port))
145 self.parent.ui.connectionFreeswitchIpAddress.setText(
146 self.currentConnection.freeswitch_ip_address)
147 self.parent.ui.connectionFreeswitchPort.setText(
148 unicode(self.currentConnection.freeswitch_port))
152 TODO: Clone an existing connection.
154 This creates a new connection with bound data copied from another one.
161 self.currentConnection = None
163 name_template = 'New connection [{0:02}]'
164 for i in xrange(1, 100):
165 name = name_template.format(i)
166 connection_exists = False
167 for connection in self.connections:
168 if connection.name == name:
169 connection_exists = True
172 if not connection_exists:
175 self.parent.ui.connectionName.setText('New connection')
176 self.parent.ui.connectionName.setFocus()
177 self.parent.ui.connectionName.selectAll()
178 self.parent.ui.connectionLocalIpAddress.clear()
179 self.parent.ui.connectionLocalPort.clear()
180 self.parent.ui.connectionFreeswitchIpAddress.clear()
181 self.parent.ui.connectionFreeswitchPort.clear()
185 Save new or existing connection.
187 name = unicode(self.parent.ui.connectionName.text())
189 # Add to connection list if we've created it.
190 if self.currentConnection is None:
191 #self.currentConnection = models.Connection(store=state.store)
192 self.connections.append(self.currentConnection)
193 self.parent.ui.connectionList.addItem(name)
195 self.currentConnection.name = name
196 self.currentConnection.local_ip_address = unicode(
197 self.parent.ui.connectionLocalIpAddress.text())
198 self.currentConnection.local_port = int(
199 self.parent.ui.connectionLocalPort.text())
200 self.currentConnection.freeswitch_ip_address = unicode(
201 self.parent.ui.connectionFreeswitchIpAddress.text())
202 self.currentConnection.freeswitch_port = int(
203 self.parent.ui.connectionFreeswitchPort.text())
205 self.currentConnection.checkpoint()