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, state
21 class BaseHandler(object):
23 Base class for various entity handlers.
25 Doesn't do anything useful on its own.
30 def __init__(self, parent):
38 class ConnectionsHandler(BaseHandler):
43 self.connections = list(state.store.query(models.Connection))
44 self.currentConnection = None
46 for connection in self.connections:
47 self.parent.ui.connectionList.addItem(connection.name)
49 def select(self, index):
51 Select another connection as current.
53 self.currentConnection = state.connections[index]
57 TODO: Clone an existing connection.
59 This creates a new connection with bound data copied from another one.
66 self.currentConnection = None
68 name_template = 'New connection [{0:02}]'
69 for i in xrange(1, 100):
70 name = name_template.format(i)
71 connection_exists = False
72 for connection in self.connections:
73 if connection.name == name:
74 connection_exists = True
77 if not connection_exists:
80 self.parent.ui.connectionName.setText(name)
81 self.parent.ui.connectionName.setFocus()
82 self.parent.ui.connectionName.selectAll()
83 self.parent.ui.connectionLocalIpAddress.clear()
84 self.parent.ui.connectionLocalPort.clear()
85 self.parent.ui.connectionFreeswitchIpAddress.clear()
86 self.parent.ui.connectionFreeswitchPort.clear()
90 Save new or existing connection.
92 name = unicode(self.parent.ui.connectionName.text())
94 # Add to connection list if we've created it.
95 if self.currentConnection is None:
96 self.currentConnection = models.Connection(store=state.store)
97 self.connections.append(self.currentConnection)
98 self.parent.ui.connectionList.addItem(name)
100 self.currentConnection.name = name
101 self.currentConnection.local_ip_address = unicode(
102 self.parent.ui.connectionLocalIpAddress.text())
103 self.currentConnection.local_port = int(
104 self.parent.ui.connectionLocalPort.text())
105 self.currentConnection.freeswitch_ip_address = unicode(
106 self.parent.ui.connectionFreeswitchIpAddress.text())
107 self.currentConnection.freeswitch_port = int(
108 self.parent.ui.connectionFreeswitchPort.text())
110 self.currentConnection.checkpoint()