Connection adding implemented
[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 from ipypbx import models, state
19
20
21 class BaseHandler(object):
22     """
23     Base class for various entity handlers.
24
25     Doesn't do anything useful on its own.
26     """
27     items = None
28     current_items = None
29
30     def __init__(self, parent):
31         self.parent = parent
32         self.initState()
33
34     def initState(self):
35         return NotImplemented
36
37
38 class ConnectionsHandler(BaseHandler):
39     """
40     Connections handler.
41     """
42     def initState(self):
43         self.connections = list(state.store.query(models.Connection))
44         self.currentConnection = None        
45         
46     def select(self, index):
47         """
48         Select another connection as current.
49         """
50         self.currentConnection = state.connections[index]
51
52     def clone(self):
53         """
54         TODO: Clone an existing connection.
55
56         This creates a new connection with bound data copied from another one.
57         """
58
59     def add(self):
60         """
61         Add new connection.
62         """
63         self.currentConnection = None
64         self.parent.ui.connectionName.setText('')
65         self.parent.ui.connectionLocalIpAddress.setText('')
66         self.parent.ui.connectionLocalPort.setText('')
67         self.parent.ui.connectionFreeswitchIpAddress.setText('')
68         self.parent.ui.connectionFreeswitchPort.setText('')
69
70     def save(self):
71         """
72         Save new or existing connection.
73         """
74         name = unicode(self.parent.ui.connectionName.text())
75         connection = models.Connection(
76             name=name,
77             local_ip_address=unicode(
78                 self.parent.ui.connectionLocalIpAddress.text()),
79             local_port=int(
80                 self.parent.ui.connectionLocalPort.text()),
81             freeswitch_ip_address=unicode(
82                 self.parent.ui.connectionFreeswitchIpAddress.text()),
83             freeswitch_port=int(
84                 self.parent.ui.connectionFreeswitchPort.text()))
85         if not self.currentConnection:
86             self.connections.append(connection)
87             self.currentConnection = connection
88             self.parent.ui.connectionList.addItem(name)