b5b8d707d2a22f3d82226583c7f58aa35665c20b
[cascardo/ovs.git] / ovsdb / ovsdbmonitor / OVEConfigWindow.py
1 # Copyright (c) 2010 Citrix Systems, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from OVEStandard import *
16 from OVEConfig import *
17 from OVELogger import *
18 from Ui_ConfigWindow import *
19
20 from OVEHostWindow import *
21
22 class OVEConfigWindow(QtGui.QDialog):
23     def __init__(self, app):
24         QtGui.QDialog.__init__(self)
25         self.app = app
26         self.ui = Ui_ConfigWindow()
27         self.ui.setupUi(self)
28
29         self.connect(self.ui.hostAddButton, QtCore.SIGNAL("clicked()"), self.xon_hostAddButton_clicked)
30         self.connect(self.ui.hostEditButton, QtCore.SIGNAL("clicked()"), self.xon_hostEditButton_clicked)
31         self.connect(self.ui.hostDeleteButton, QtCore.SIGNAL("clicked()"), self.xon_hostDeleteButton_clicked)
32         self.connect(self.ui.buttonBox, QtCore.SIGNAL("clicked(QAbstractButton *)"), self.xon_actionButton_Box_clicked)
33         self.connect(self.ui.hostList, QtCore.SIGNAL("currentItemChanged(QListWidgetItem *,  QListWidgetItem *)"), self.xon_hostList_currentItemChanged)
34         self.connect(self.ui.logTrafficCheckBox, QtCore.SIGNAL("stateChanged(int)"), self.xon_logTrafficCheckBox_stateChanged)
35         self.connect(self.ui.truncateUuidsCheckBox, QtCore.SIGNAL("stateChanged(int)"), self.xon_truncateUuidsCheckBox_stateChanged)
36         self.readConfig()
37         self.updateWidgets()
38     
39     def handleHostWindowRecord(self, record, isEdit):
40         if record['accepted'] and record['address'].strip() != '':
41             currentRow = self.ui.hostList.currentRow()
42             if isEdit:
43                 self.configHosts[currentRow] = record
44             else:
45                 self.configHosts.append(record)
46
47         self.updateWidgets()
48
49     def xon_hostAddButton_clicked(self):
50         hostWindow = OVEHostWindow(self)
51         hostWindow.exec_()
52         self.handleHostWindowRecord(hostWindow.record(), False)
53
54     def xon_hostEditButton_clicked(self):
55         if self.ui.hostList.currentItem() is None:
56             pass # OVELog('No item to edit')
57         else:
58             currentRow = self.ui.hostList.currentRow()
59             hostWindow = OVEHostWindow(self, self.configHosts[currentRow])
60             hostWindow.exec_()
61             self.handleHostWindowRecord(hostWindow.record(), True)
62
63     def xon_hostDeleteButton_clicked(self):
64         if self.ui.hostList.currentItem() is not None:
65             currentRow = self.ui.hostList.currentRow()
66             del self.configHosts[currentRow]
67             self.updateWidgets()
68
69     def xon_actionButton_Box_clicked(self, button):
70         role = self.ui.buttonBox.buttonRole(button)
71         if role == QtGui.QDialogButtonBox.AcceptRole:
72             self.writeConfig()
73             self.close()
74         elif role == QtGui.QDialogButtonBox.ApplyRole:
75             self.writeConfig()
76         elif role == QtGui.QDialogButtonBox.RejectRole:
77             if self.configChanged():
78                 self.close()
79             else:
80                 ret = QtGui.QMessageBox.warning(
81                     self, "OVSDB Monitor",
82                     "Changes not applied. Discard?",
83                     QtGui.QMessageBox.Discard | QtGui.QMessageBox.Cancel | QtGui.QMessageBox.Apply,
84                     QtGui.QMessageBox.Discard)
85                 
86                 if ret == QtGui.QMessageBox.Apply:
87                     self.writeConfig()
88                 if ret != QtGui.QMessageBox.Cancel:
89                     self.close()
90
91     def xon_hostList_currentItemChanged(self, current, previous):
92         editable = (current is not None)
93         self.ui.hostEditButton.setEnabled(editable)
94         self.ui.hostDeleteButton.setEnabled(editable)
95
96     def xon_logTrafficCheckBox_stateChanged(self, value):
97         self.configLogTraffic = (value == Qt.Checked)
98     
99     def xon_truncateUuidsCheckBox_stateChanged(self, value):
100         self.configTruncateUuids = (value == Qt.Checked)
101     
102     def updateWidgets(self):
103         self.ui.hostList.clear()
104         for host in self.configHosts:
105             self.ui.hostList.addItem(host['address'])
106         self.ui.logTrafficCheckBox.setChecked(self.configLogTraffic)
107         self.ui.truncateUuidsCheckBox.setChecked(self.configTruncateUuids)
108
109     def configChanged(self):
110         return (
111             (self.configHosts == OVEConfig.Inst().hosts) and
112             (self.configLogTraffic == (OVEConfig.Inst().logTraffic))and
113             (self.configTruncateUuids == (OVEConfig.Inst().truncateUuids))
114         )
115         
116     def readConfig(self):
117         self.configHosts = deepcopy(OVEConfig.Inst().hosts)
118         self.configLogTraffic = OVEConfig.Inst().logTraffic
119         self.configTruncateUuids = OVEConfig.Inst().truncateUuids
120         
121     def writeConfig(self):
122         OVEConfig.Inst().hosts = deepcopy(self.configHosts)
123         OVEConfig.Inst().logTraffic = self.configLogTraffic
124         OVEConfig.Inst().truncateUuids = self.configTruncateUuids
125         OVEConfig.Inst().saveConfig()
126
127