Halfway rewriting to use QT MVC
[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
19 from PyQt4 import QtCore
20
21
22 class BaseHandler(object):
23     """
24     Base class for various entity handlers.
25
26     Doesn't do anything useful on its own.
27     """
28     items = None
29     current_items = None
30
31     def __init__(self, parent):
32         self.parent = parent
33         self.initState()
34
35     def initState(self):
36         return NotImplemented
37
38
39 class ConnectionController(QtCore.QObject):
40     """
41     Connections handler.
42     """
43 #    def initState(self):
44 #        self.connections = []
45 #        #self.connections = list(state.store.query(models.Connection))
46 #        self.currentConnection = None
47
48 #        for connection in self.connections:
49 #            self.parent.ui.connectionList.addItem(connection.name)
50
51 #        if self.connections:
52 #            print self.parent.ui.connectionList.currentRow()
53 #            self.parent.ui.connectionList.setCurrentRow(0)
54 #            QtCore.QObject.emit(
55 #                self.parent.ui.connectionList, QtCore.SIGNAL('currentRowChanged(int)'), 0)
56         
57     def select(self, row):
58         """
59         Select another connection as current.
60         """
61         self.currentConnection = self.connections[row]
62
63         # Fill in form based on selection.
64         self.parent.ui.connectionName.setText(self.currentConnection.name)
65         self.parent.ui.connectionLocalIpAddress.setText(
66             self.currentConnection.local_ip_address)
67         self.parent.ui.connectionLocalPort.setText(
68             unicode(self.currentConnection.local_port))
69         self.parent.ui.connectionFreeswitchIpAddress.setText(
70             self.currentConnection.freeswitch_ip_address)
71         self.parent.ui.connectionFreeswitchPort.setText(
72             unicode(self.currentConnection.freeswitch_port))
73
74     def clone(self):
75         """
76         TODO: Clone an existing connection.
77
78         This creates a new connection with bound data copied from another one.
79         """
80
81     def add(self):
82         """
83         Add new connection.
84         """
85         print '!'
86         print self.parent
87         self.currentConnection = None
88
89         name_template = 'New connection [{0:02}]'
90         for i in xrange(1, 100):
91             name = name_template.format(i)
92             connection_exists = False
93             for connection in self.connections:
94                 if connection.name == name:
95                     connection_exists = True
96                     break
97
98             if not connection_exists:
99                 break
100             
101         self.parent.ui.connectionName.setText('New connection')
102         self.parent.ui.connectionName.setFocus()
103         self.parent.ui.connectionName.selectAll()
104         self.parent.ui.connectionLocalIpAddress.clear()
105         self.parent.ui.connectionLocalPort.clear()
106         self.parent.ui.connectionFreeswitchIpAddress.clear()
107         self.parent.ui.connectionFreeswitchPort.clear()
108
109     def save(self):
110         """
111         Save new or existing connection.
112         """
113         name = unicode(self.parent.ui.connectionName.text())
114
115         # Add to connection list if we've created it.
116         if self.currentConnection is None:            
117             #self.currentConnection = models.Connection(store=state.store)            
118             self.connections.append(self.currentConnection)
119             self.parent.ui.connectionList.addItem(name)
120
121         self.currentConnection.name = name
122         self.currentConnection.local_ip_address = unicode(
123             self.parent.ui.connectionLocalIpAddress.text())
124         self.currentConnection.local_port = int(
125             self.parent.ui.connectionLocalPort.text())
126         self.currentConnection.freeswitch_ip_address = unicode(
127             self.parent.ui.connectionFreeswitchIpAddress.text())
128         self.currentConnection.freeswitch_port = int(
129             self.parent.ui.connectionFreeswitchPort.text())
130
131         self.currentConnection.checkpoint()