56b6b17c2675f4cf714ce6d1fe6c0e8a84926610
[cascardo/ovs.git] / ovsdb / ovsdbmonitor / OVEApp.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
16 from OVEStandard import *
17 from OVEConfig import *
18 from OVEFetch import *
19
20 from OVEConfigWindow import *
21 from OVEFlowWindow import *
22 from OVELogWindow import *
23 from OVEMainWindow import *
24
25 class OVEApp:
26     def __init__(self):
27         self.app = globalApp
28         self.app.setOrganizationName("Citrix_Systems_Inc")
29         self.app.setOrganizationDomain("citrix.com")
30         self.app.setApplicationName("ovsdbmonitor")
31         self.mainWindows = []
32         self.flowWindows = []
33         self.configWindow = None
34
35     def enter(self):
36         if len(OVEConfig.Inst().hosts) < 1:
37             self.showConfig(True)
38             QtGui.QMessageBox.information(
39                     None, "OVSDB Monitor",
40                     "This application browses openvswitch databases on remote hosts.  Please add one or more openvswitch hosts to continue")
41         self.loadMainWindows()
42         self.loadFlowWindows()
43         if len(self.mainWindows) == 0 and len(self.flowWindows) == 0:
44             self.newMainWindow()
45         self.newLogWindow()
46         # Reactor must be started after the event loop is running, so use a zero timeout
47         QtCore.QTimer.singleShot(0, OVEFetch.startReactor)
48         OVELog("Application started")
49         retCode = self.app.exec_()
50         index = 0
51         for mainWindow in self.mainWindows:
52             if mainWindow.isVisible():
53                 mainWindow.saveSettings(index)
54                 index += 1 # Indent intentional
55         OVEMainWindow.terminateSettings(index)
56         index = 0
57         for flowWindow in self.flowWindows:
58             if flowWindow.isVisible():
59                 flowWindow.saveSettings(index)
60                 index += 1 # Indent intentional            
61         OVEFlowWindow.terminateSettings(index)
62         self.logWindow.saveSettings()
63     
64     def quit(self):
65         self.app.quit()
66     
67     def showLog(self, value):
68         if value:
69             self.logWindow.hide()
70             self.logWindow.show()
71         else:
72             self.logWindow.hide()
73
74     def showConfig(self, value):
75         if value:
76             del self.configWindow
77             self.configWindow = OVEConfigWindow(self)
78             self.configWindow.show()
79         else:
80             self.configWindow.hide()
81
82     def newMainWindow(self, loadIndex = None):
83         self.mainWindows.append(OVEMainWindow(self, loadIndex))
84         self.mainWindows[-1].show()
85
86     def newFlowWindow(self, loadIndex = None):
87         self.flowWindows.append(OVEFlowWindow(self, loadIndex))
88         self.flowWindows[-1].show()
89
90     def newLogWindow(self):
91         self.logWindow = OVELogWindow(self)
92
93     def loadMainWindows(self):
94         for loadIndex in range(0, 100):
95             if OVEMainWindow.isLoadable(loadIndex):
96                 self.newMainWindow(loadIndex)
97             else:
98                 break
99
100     def loadFlowWindows(self):
101         for loadIndex in range(0, 100):
102             if OVEFlowWindow.isLoadable(loadIndex):
103                 self.newFlowWindow(loadIndex)
104             else:
105                 break