b0a3fb2eb94f45c5793865139d9c8a8722dda208
[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         for connection in self.connections:
47             self.parent.ui.connectionList.addItem(connection.name)
48         
49     def select(self, index):
50         """
51         Select another connection as current.
52         """
53         self.currentConnection = state.connections[index]
54
55     def clone(self):
56         """
57         TODO: Clone an existing connection.
58
59         This creates a new connection with bound data copied from another one.
60         """
61
62     def add(self):
63         """
64         Add new connection.
65         """
66         self.currentConnection = None
67
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
75                     break
76
77             if not connection_exists:
78                 break
79             
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()
87
88     def save(self):
89         """
90         Save new or existing connection.
91         """
92         name = unicode(self.parent.ui.connectionName.text())
93
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)
99
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())
109
110         self.currentConnection.checkpoint()