2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
5 # opensecurity-launcher
7 # launches an application inside a VM
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
11 # Copyright (C) 2013 AIT Austrian Institute of Technology
12 # AIT Austrian Institute of Technology GmbH
13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
14 # http://www.ait.ac.at
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation version 2.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 # Boston, MA 02110-1301, USA.
29 # ------------------------------------------------------------
32 # ------------------------------------------------------------
41 from PyQt4 import QtCore
42 from PyQt4 import QtGui
45 from about import About
46 from environment import Environment
49 # ------------------------------------------------------------
53 class Chooser(QtGui.QDialog):
55 """Ask the user what to launch."""
57 def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
59 super(Chooser, self).__init__(parent, flags)
60 self.setWindowTitle('OpenSecuirty Launch Application')
63 # positionate ourself central
64 screen = QtGui.QDesktopWidget().screenGeometry()
65 self.resize(self.geometry().width() * 1.25, self.geometry().height())
66 size = self.geometry()
67 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
70 def clicked_about(self):
71 """clicked the about button"""
76 def clicked_cancel(self):
77 """clicked the cancel button"""
82 """clicked the ok button"""
88 """Create the widgets."""
90 lyMain = QtGui.QVBoxLayout(self)
91 lyMain.setContentsMargins(8, 8, 8, 8)
93 # content area: left pixmap, right text
94 lyContent = QtGui.QHBoxLayout()
95 lyMain.addLayout(lyContent)
98 lbPix = QtGui.QLabel()
99 lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
100 lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
101 lyContent.addSpacing(16)
104 lyLaunch = QtGui.QGridLayout()
105 lyContent.addLayout(lyLaunch)
106 lbTitle = QtGui.QLabel('Specify details for application to launch.')
107 lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
109 lbVM = QtGui.QLabel('&VM-ID:')
110 lyLaunch.addWidget(lbVM, 1, 0)
111 self.edVM = QtGui.QLineEdit()
112 lyLaunch.addWidget(self.edVM, 1, 1)
113 lbVM.setBuddy(self.edVM)
116 self.edVM.setText('Debian 7')
118 lbApplication = QtGui.QLabel('&Application:')
119 lyLaunch.addWidget(lbApplication, 2, 0)
120 self.cbApplication = QtGui.QComboBox()
121 self.cbApplication.setEditable(True)
122 lyLaunch.addWidget(self.cbApplication, 2, 1)
123 lbApplication.setBuddy(self.cbApplication)
126 self.cbApplication.addItem('iceweasel')
127 self.cbApplication.addItem('vlc')
128 self.cbApplication.addItem('xfce4-terminal')
130 lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
131 lyLaunch.setColumnStretch(1, 1)
132 lyLaunch.setRowStretch(3, 1)
137 lyButton = QtGui.QHBoxLayout()
138 lyMain.addLayout(lyButton)
140 lyButton.addStretch(1)
141 btnOk = QtGui.QPushButton('&Ok', self)
142 btnOk.setDefault(True)
143 btnOk.setMinimumWidth(100)
144 lyButton.addWidget(btnOk)
145 btnCancel = QtGui.QPushButton('&Cancel', self)
146 btnCancel.setMinimumWidth(100)
147 lyButton.addWidget(btnCancel)
148 btnAbout = QtGui.QPushButton('&About', self)
149 btnAbout.setMinimumWidth(100)
150 lyButton.addWidget(btnAbout)
152 button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
153 btnOk.setMinimumWidth(button_width)
154 btnCancel.setMinimumWidth(button_width)
155 btnAbout.setMinimumWidth(button_width)
158 self.resize(lyMain.minimumSize())
161 btnOk.clicked.connect(self.clicked_ok)
162 btnCancel.clicked.connect(self.clicked_cancel)
163 btnAbout.clicked.connect(self.clicked_about)
169 app = QtGui.QApplication(sys.argv)
171 # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
172 data_path = Environment("opensecurity").image_path
173 for file in os.listdir(data_path):
174 if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
175 QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(data_path, file)))
177 # we should have now our application icon
178 app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
186 if dlg.result() == QtGui.QDialog.Accepted:
187 # encode an proper GET request to the opensecurity daemon
188 get_vm = urllib.quote(str(dlg.edVM.text()))
189 get_app = urllib.quote(str(dlg.cbApplication.currentText()))
190 osd_request = 'http://127.0.0.1:8080/application?vm={0}&app={1}'.format(get_vm, get_app)
191 urllib.urlopen(osd_request)
199 if __name__ == "__main__":