qube_manager.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  6. # Copyright (C) 2012 Marek Marczykowski-Górecki
  7. # <marmarek@invisiblethingslab.com>
  8. # Copyright (C) 2017 Wojtek Porczyk <woju@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU General Public License
  12. # as published by the Free Software Foundation; either version 2
  13. # of the License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public License along
  21. # with this program; if not, see <http://www.gnu.org/licenses/>.
  22. #
  23. #
  24. import sys
  25. import os
  26. import os.path
  27. import subprocess
  28. import time
  29. from datetime import datetime, timedelta
  30. import traceback
  31. import threading
  32. from pydbus import SessionBus
  33. from qubesadmin import Qubes
  34. from qubesadmin import exc
  35. from PyQt4 import QtGui # pylint: disable=import-error
  36. from PyQt4 import QtCore # pylint: disable=import-error
  37. from qubesmanager.about import AboutDialog
  38. from . import ui_qubemanager # pylint: disable=no-name-in-module
  39. from . import thread_monitor
  40. from . import table_widgets
  41. from . import settings
  42. from . import global_settings
  43. from . import restore
  44. from . import backup
  45. from . import log_dialog
  46. class SearchBox(QtGui.QLineEdit):
  47. def __init__(self, parent=None):
  48. super(SearchBox, self).__init__(parent)
  49. self.focusing = False
  50. def focusInEvent(self, e): # pylint: disable=invalid-name
  51. super(SearchBox, self).focusInEvent(e)
  52. self.selectAll()
  53. self.focusing = True
  54. def mousePressEvent(self, e): # pylint: disable=invalid-name
  55. super(SearchBox, self).mousePressEvent(e)
  56. if self.focusing:
  57. self.selectAll()
  58. self.focusing = False
  59. class VmRowInTable:
  60. # pylint: disable=too-few-public-methods
  61. def __init__(self, vm, row_no, table):
  62. self.vm = vm
  63. # TODO: replace a various different widgets with a more generic
  64. # VmFeatureWidget or VMPropertyWidget
  65. table_widgets.row_height = VmManagerWindow.row_height
  66. table.setRowHeight(row_no, VmManagerWindow.row_height)
  67. self.type_widget = table_widgets.VmTypeWidget(vm)
  68. table.setCellWidget(row_no, VmManagerWindow.columns_indices['Type'],
  69. self.type_widget)
  70. table.setItem(row_no, VmManagerWindow.columns_indices['Type'],
  71. self.type_widget.table_item)
  72. self.label_widget = table_widgets.VmLabelWidget(vm)
  73. table.setCellWidget(row_no, VmManagerWindow.columns_indices['Label'],
  74. self.label_widget)
  75. table.setItem(row_no, VmManagerWindow.columns_indices['Label'],
  76. self.label_widget.table_item)
  77. self.name_widget = table_widgets.VmNameItem(vm)
  78. table.setItem(row_no, VmManagerWindow.columns_indices['Name'],
  79. self.name_widget)
  80. self.info_widget = table_widgets.VmInfoWidget(vm)
  81. table.setCellWidget(row_no, VmManagerWindow.columns_indices['State'],
  82. self.info_widget)
  83. table.setItem(row_no, VmManagerWindow.columns_indices['State'],
  84. self.info_widget.table_item)
  85. self.template_widget = table_widgets.VmTemplateItem(vm)
  86. table.setItem(row_no, VmManagerWindow.columns_indices['Template'],
  87. self.template_widget)
  88. self.netvm_widget = table_widgets.VmNetvmItem(vm)
  89. table.setItem(row_no, VmManagerWindow.columns_indices['NetVM'],
  90. self.netvm_widget)
  91. self.size_widget = table_widgets.VmSizeOnDiskItem(vm)
  92. table.setItem(row_no, VmManagerWindow.columns_indices['Size'],
  93. self.size_widget)
  94. self.internal_widget = table_widgets.VmInternalItem(vm)
  95. table.setItem(row_no, VmManagerWindow.columns_indices['Internal'],
  96. self.internal_widget)
  97. self.ip_widget = table_widgets.VmIPItem(vm)
  98. table.setItem(row_no, VmManagerWindow.columns_indices['IP'],
  99. self.ip_widget)
  100. self.include_in_backups_widget = \
  101. table_widgets.VmIncludeInBackupsItem(vm)
  102. table.setItem(row_no, VmManagerWindow.columns_indices[
  103. 'Backups'], self.include_in_backups_widget)
  104. self.last_backup_widget = table_widgets.VmLastBackupItem(vm)
  105. table.setItem(row_no, VmManagerWindow.columns_indices[
  106. 'Last backup'], self.last_backup_widget)
  107. self.table = table
  108. def update(self, update_size_on_disk=False):
  109. """
  110. Update info in a single VM row
  111. :param update_size_on_disk: should disk utilization be updated? the
  112. widget will extract the data from VM object
  113. :return: None
  114. """
  115. try:
  116. self.info_widget.update_vm_state()
  117. self.template_widget.update()
  118. self.netvm_widget.update()
  119. self.internal_widget.update()
  120. self.ip_widget.update()
  121. self.include_in_backups_widget.update()
  122. self.last_backup_widget.update()
  123. if update_size_on_disk:
  124. self.size_widget.update()
  125. except exc.QubesPropertyAccessError:
  126. pass
  127. #force re-sorting
  128. self.table.setSortingEnabled(True)
  129. vm_shutdown_timeout = 20000 # in msec
  130. vm_restart_check_timeout = 1000 # in msec
  131. class VmShutdownMonitor(QtCore.QObject):
  132. def __init__(self, vm, shutdown_time=vm_shutdown_timeout,
  133. check_time=vm_restart_check_timeout,
  134. and_restart=False, caller=None):
  135. QtCore.QObject.__init__(self)
  136. self.vm = vm
  137. self.shutdown_time = shutdown_time
  138. self.check_time = check_time
  139. self.and_restart = and_restart
  140. self.shutdown_started = datetime.now()
  141. self.caller = caller
  142. def restart_vm_if_needed(self):
  143. if self.and_restart and self.caller:
  144. self.caller.start_vm(self.vm)
  145. def check_again_later(self):
  146. # noinspection PyTypeChecker,PyCallByClass
  147. QtCore.QTimer.singleShot(self.check_time, self.check_if_vm_has_shutdown)
  148. def timeout_reached(self):
  149. actual = datetime.now() - self.shutdown_started
  150. allowed = timedelta(milliseconds=self.shutdown_time)
  151. return actual > allowed
  152. def check_if_vm_has_shutdown(self):
  153. vm = self.vm
  154. vm_is_running = vm.is_running()
  155. try:
  156. vm_start_time = datetime.fromtimestamp(float(vm.start_time))
  157. except (AttributeError, TypeError, ValueError):
  158. vm_start_time = None
  159. if vm_is_running and vm_start_time \
  160. and vm_start_time < self.shutdown_started:
  161. if self.timeout_reached():
  162. reply = QtGui.QMessageBox.question(
  163. None, self.tr("Qube Shutdown"),
  164. self.tr(
  165. "The Qube <b>'{0}'</b> hasn't shutdown within the last "
  166. "{1} seconds, do you want to kill it?<br>").format(
  167. vm.name, self.shutdown_time / 1000),
  168. self.tr("Kill it!"),
  169. self.tr("Wait another {0} seconds...").format(
  170. self.shutdown_time / 1000))
  171. if reply == 0:
  172. try:
  173. vm.kill()
  174. except exc.QubesVMNotStartedError:
  175. # the VM shut down while the user was thinking about
  176. # shutting it down
  177. pass
  178. self.restart_vm_if_needed()
  179. else:
  180. self.shutdown_started = datetime.now()
  181. self.check_again_later()
  182. else:
  183. self.check_again_later()
  184. else:
  185. if vm_is_running:
  186. # Due to unknown reasons, Xen sometimes reports that a domain
  187. # is running even though its start-up timestamp is not valid.
  188. # Make sure that "restart_vm_if_needed" is not called until
  189. # the domain has been completely shut down according to Xen.
  190. self.check_again_later()
  191. return
  192. self.restart_vm_if_needed()
  193. class VmManagerWindow(ui_qubemanager.Ui_VmManagerWindow, QtGui.QMainWindow):
  194. # pylint: disable=too-many-instance-attributes
  195. row_height = 30
  196. column_width = 200
  197. search = ""
  198. # suppress saving settings while initializing widgets
  199. settings_loaded = False
  200. columns_indices = {"Type": 0,
  201. "Label": 1,
  202. "Name": 2,
  203. "State": 3,
  204. "Template": 4,
  205. "NetVM": 5,
  206. "Size": 6,
  207. "Internal": 7,
  208. "IP": 8,
  209. "Backups": 9,
  210. "Last backup": 10,
  211. }
  212. def __init__(self, qt_app, qubes_app, parent=None):
  213. # pylint: disable=unused-argument
  214. super(VmManagerWindow, self).__init__()
  215. self.setupUi(self)
  216. self.manager_settings = QtCore.QSettings(self)
  217. self.qubes_app = qubes_app
  218. self.qt_app = qt_app
  219. self.searchbox = SearchBox()
  220. self.searchbox.setValidator(QtGui.QRegExpValidator(
  221. QtCore.QRegExp("[a-zA-Z0-9_-]*", QtCore.Qt.CaseInsensitive), None))
  222. self.searchContainer.addWidget(self.searchbox)
  223. self.connect(self.table, QtCore.SIGNAL("itemSelectionChanged()"),
  224. self.table_selection_changed)
  225. self.table.setColumnWidth(0, self.column_width)
  226. self.sort_by_column = "Type"
  227. self.sort_order = QtCore.Qt.AscendingOrder
  228. self.vms_list = []
  229. self.vms_in_table = {}
  230. self.frame_width = 0
  231. self.frame_height = 0
  232. self.move(self.x(), 0)
  233. self.columns_actions = {
  234. self.columns_indices["Type"]: self.action_vm_type,
  235. self.columns_indices["Label"]: self.action_label,
  236. self.columns_indices["Name"]: self.action_name,
  237. self.columns_indices["State"]: self.action_state,
  238. self.columns_indices["Template"]: self.action_template,
  239. self.columns_indices["NetVM"]: self.action_netvm,
  240. self.columns_indices["Size"]: self.action_size_on_disk,
  241. self.columns_indices["Internal"]: self.action_internal,
  242. self.columns_indices["IP"]: self
  243. .action_ip, self.columns_indices["Backups"]: self
  244. .action_backups, self.columns_indices["Last backup"]: self
  245. .action_last_backup
  246. }
  247. self.visible_columns_count = len(self.columns_indices)
  248. self.table.setColumnWidth(self.columns_indices["State"], 80)
  249. self.table.setColumnWidth(self.columns_indices["Name"], 150)
  250. self.table.setColumnWidth(self.columns_indices["Label"], 40)
  251. self.table.setColumnWidth(self.columns_indices["Type"], 40)
  252. self.table.setColumnWidth(self.columns_indices["Size"], 100)
  253. self.table.setColumnWidth(self.columns_indices["Internal"], 60)
  254. self.table.setColumnWidth(self.columns_indices["IP"], 100)
  255. self.table.setColumnWidth(self.columns_indices["Backups"], 60)
  256. self.table.setColumnWidth(self.columns_indices["Last backup"], 90)
  257. self.table.horizontalHeader().setResizeMode(
  258. QtGui.QHeaderView.Interactive)
  259. self.table.horizontalHeader().setStretchLastSection(True)
  260. self.context_menu = QtGui.QMenu(self)
  261. self.context_menu.addAction(self.action_settings)
  262. self.context_menu.addAction(self.action_editfwrules)
  263. self.context_menu.addAction(self.action_appmenus)
  264. self.context_menu.addAction(self.action_set_keyboard_layout)
  265. self.context_menu.addSeparator()
  266. self.context_menu.addAction(self.action_updatevm)
  267. self.context_menu.addAction(self.action_run_command_in_vm)
  268. self.context_menu.addAction(self.action_resumevm)
  269. self.context_menu.addAction(self.action_startvm_tools_install)
  270. self.context_menu.addAction(self.action_pausevm)
  271. self.context_menu.addAction(self.action_shutdownvm)
  272. self.context_menu.addAction(self.action_restartvm)
  273. self.context_menu.addAction(self.action_killvm)
  274. self.context_menu.addSeparator()
  275. self.context_menu.addAction(self.action_clonevm)
  276. self.context_menu.addAction(self.action_removevm)
  277. self.context_menu.addSeparator()
  278. self.context_menu.addMenu(self.logs_menu)
  279. self.context_menu.addSeparator()
  280. self.tools_context_menu = QtGui.QMenu(self)
  281. self.tools_context_menu.addAction(self.action_toolbar)
  282. self.tools_context_menu.addAction(self.action_menubar)
  283. self.dom0_context_menu = QtGui.QMenu(self)
  284. self.dom0_context_menu.addAction(self.action_global_settings)
  285. self.dom0_context_menu.addAction(self.action_updatevm)
  286. self.dom0_context_menu.addSeparator()
  287. self.dom0_context_menu.addMenu(self.logs_menu)
  288. self.dom0_context_menu.addSeparator()
  289. self.connect(
  290. self.table.horizontalHeader(),
  291. QtCore.SIGNAL("sortIndicatorChanged(int, Qt::SortOrder)"),
  292. self.sort_indicator_changed)
  293. self.connect(self.table,
  294. QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"),
  295. self.open_context_menu)
  296. self.connect(self.menubar,
  297. QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"),
  298. lambda pos: self.open_tools_context_menu(self.menubar,
  299. pos))
  300. self.connect(self.toolbar,
  301. QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"),
  302. lambda pos: self.open_tools_context_menu(self.toolbar,
  303. pos))
  304. self.connect(self.logs_menu, QtCore.SIGNAL("triggered(QAction *)"),
  305. self.show_log)
  306. self.connect(self.searchbox,
  307. QtCore.SIGNAL("textChanged(const QString&)"),
  308. self.do_search)
  309. self.table.setContentsMargins(0, 0, 0, 0)
  310. self.centralwidget.layout().setContentsMargins(0, 0, 0, 0)
  311. self.layout().setContentsMargins(0, 0, 0, 0)
  312. self.connect(self.action_menubar, QtCore.SIGNAL("toggled(bool)"),
  313. self.showhide_menubar)
  314. self.connect(self.action_toolbar, QtCore.SIGNAL("toggled(bool)"),
  315. self.showhide_toolbar)
  316. self.load_manager_settings()
  317. self.fill_table()
  318. self.update_size_on_disk = False
  319. self.shutdown_monitor = {}
  320. # Connect dbus events
  321. self.bus = SessionBus()
  322. manager = self.bus.get("org.qubes.DomainManager1")
  323. manager.DomainAdded.connect(self.on_domain_added)
  324. manager.DomainRemoved.connect(self.on_domain_removed)
  325. manager.Failed.connect(self.on_failed)
  326. manager.Halted.connect(self.on_halted)
  327. manager.Halting.connect(self.on_halting)
  328. manager.Starting.connect(self.on_starting)
  329. manager.Started.connect(self.on_started)
  330. # Check Updates Timer
  331. timer = QtCore.QTimer(self)
  332. timer.timeout.connect(self.check_updates)
  333. timer.start(1000 * 30) # 30s
  334. self.check_updates()
  335. def check_updates(self):
  336. for vm in self.qubes_app.domains:
  337. if vm.klass in {'TemplateVM', 'StandaloneVM'}:
  338. self.vms_in_table[vm.qid].update()
  339. def on_domain_added(self, _, domain):
  340. #needs to clear cache
  341. self.qubes_app.domains.clear_cache()
  342. qid = int(domain.split('/')[-1])
  343. self.table.setSortingEnabled(False)
  344. row_no = self.table.rowCount()
  345. self.table.setRowCount(row_no + 1)
  346. for vm in self.qubes_app.domains:
  347. if vm.qid == qid:
  348. vm_row = VmRowInTable(vm, row_no, self.table)
  349. self.vms_in_table[vm.qid] = vm_row
  350. self.table.setSortingEnabled(True)
  351. self.showhide_vms()
  352. return
  353. # Never should reach here
  354. raise RuntimeError('Added domain not found')
  355. def on_domain_removed(self, _, domain):
  356. #needs to clear cache
  357. self.qubes_app.domains.clear_cache()
  358. qid = int(domain.split('/')[-1])
  359. # Find row and remove
  360. try:
  361. row_index = 0
  362. vm_item = self.table.item(row_index, self.columns_indices["Name"])
  363. while vm_item.qid != qid:
  364. row_index += 1
  365. vm_item = self.table.item(row_index,\
  366. self.columns_indices["Name"])
  367. except:
  368. raise RuntimeError('Deleted domain not found')
  369. self.table.removeRow(row_index)
  370. del self.vms_in_table[qid]
  371. def on_failed(self, _, domain):
  372. qid = int(domain.split('/')[-1])
  373. self.vms_in_table[qid].update()
  374. if self.vms_in_table[qid].vm == self.get_selected_vm():
  375. self.table_selection_changed()
  376. def on_halted(self, _, domain):
  377. qid = int(domain.split('/')[-1])
  378. self.vms_in_table[qid].update()
  379. if self.vms_in_table[qid].vm == self.get_selected_vm():
  380. self.table_selection_changed()
  381. # Check if is TemplatVM and update related AppVMs
  382. starting_vm = self.vms_in_table[qid]
  383. if starting_vm.vm.klass == 'TemplateVM':
  384. for vm in starting_vm.vm.appvms:
  385. if vm.klass == 'AppVM':
  386. self.vms_in_table[vm.qid].update()
  387. def on_halting(self, _, domain):
  388. qid = int(domain.split('/')[-1])
  389. self.vms_in_table[qid].update()
  390. if self.vms_in_table[qid].vm == self.get_selected_vm():
  391. self.table_selection_changed()
  392. def on_started(self, _, domain):
  393. qid = int(domain.split('/')[-1])
  394. self.vms_in_table[qid].update()
  395. if self.vms_in_table[qid].vm == self.get_selected_vm():
  396. self.table_selection_changed()
  397. def on_starting(self, _, domain):
  398. qid = int(domain.split('/')[-1])
  399. self.vms_in_table[qid].update()
  400. if self.vms_in_table[qid].vm == self.get_selected_vm():
  401. self.table_selection_changed()
  402. # Check if is TemplatVM and update related AppVMs
  403. starting_vm = self.vms_in_table[qid]
  404. if starting_vm.vm.klass == 'TemplateVM':
  405. for vm in starting_vm.vm.appvms:
  406. if vm.klass == 'AppVM':
  407. self.vms_in_table[vm.qid].update()
  408. def load_manager_settings(self):
  409. # visible columns
  410. self.visible_columns_count = 0
  411. for col in self.columns_indices:
  412. col_no = self.columns_indices[col]
  413. visible = self.manager_settings.value(
  414. 'columns/%s' % col,
  415. defaultValue="true")
  416. self.columns_actions[col_no].setChecked(visible == "true")
  417. self.visible_columns_count += 1
  418. self.sort_by_column = str(
  419. self.manager_settings.value("view/sort_column",
  420. defaultValue=self.sort_by_column))
  421. self.sort_order = QtCore.Qt.SortOrder(
  422. self.manager_settings.value("view/sort_order",
  423. defaultValue=self.sort_order))
  424. self.table.sortItems(self.columns_indices[self.sort_by_column],
  425. self.sort_order)
  426. if not self.manager_settings.value("view/menubar_visible",
  427. defaultValue=True):
  428. self.action_menubar.setChecked(False)
  429. if not self.manager_settings.value("view/toolbar_visible",
  430. defaultValue=True):
  431. self.action_toolbar.setChecked(False)
  432. self.settings_loaded = True
  433. def get_vms_list(self):
  434. return [vm for vm in self.qubes_app.domains]
  435. def fill_table(self):
  436. progress = QtGui.QProgressDialog(
  437. self.tr(
  438. "Loading Qube Manager..."), "", 0, 0)
  439. progress.setWindowTitle(self.tr("Qube Manager"))
  440. progress.setWindowFlags(QtCore.Qt.Window |
  441. QtCore.Qt.WindowTitleHint |
  442. QtCore.Qt.CustomizeWindowHint)
  443. progress.setCancelButton(None)
  444. progress.setModal(True)
  445. progress.show()
  446. self.table.setSortingEnabled(False)
  447. vms_list = self.get_vms_list()
  448. vms_in_table = {}
  449. self.table.setRowCount(len(vms_list))
  450. row_no = 0
  451. for vm in vms_list:
  452. vm_row = VmRowInTable(vm, row_no, self.table)
  453. vms_in_table[vm.qid] = vm_row
  454. row_no += 1
  455. self.qt_app.processEvents()
  456. self.vms_list = vms_list
  457. self.vms_in_table = vms_in_table
  458. self.table.setSortingEnabled(True)
  459. progress.hide()
  460. def showhide_vms(self):
  461. if not self.search:
  462. for row_no in range(self.table.rowCount()):
  463. self.table.setRowHidden(row_no, False)
  464. else:
  465. for row_no in range(self.table.rowCount()):
  466. widget = self.table.cellWidget(row_no,
  467. self.columns_indices["State"])
  468. show = (self.search in widget.vm.name)
  469. self.table.setRowHidden(row_no, not show)
  470. @QtCore.pyqtSlot(str)
  471. def do_search(self, search):
  472. self.search = str(search)
  473. self.showhide_vms()
  474. # noinspection PyArgumentList
  475. @QtCore.pyqtSlot(name='on_action_search_triggered')
  476. def action_search_triggered(self):
  477. self.searchbox.setFocus()
  478. # noinspection PyPep8Naming
  479. def sort_indicator_changed(self, column, order):
  480. self.sort_by_column = [name for name in self.columns_indices if
  481. self.columns_indices[name] == column][0]
  482. self.sort_order = order
  483. if self.settings_loaded:
  484. self.manager_settings.setValue('view/sort_column',
  485. self.sort_by_column)
  486. self.manager_settings.setValue('view/sort_order', self.sort_order)
  487. self.manager_settings.sync()
  488. def table_selection_changed(self):
  489. vm = self.get_selected_vm()
  490. if vm is not None and vm in self.qubes_app.domains:
  491. # TODO: add boot from device to menu and add windows tools there
  492. # Update available actions:
  493. self.action_settings.setEnabled(vm.klass != 'AdminVM')
  494. self.action_removevm.setEnabled(
  495. vm.klass != 'AdminVM' and not vm.is_running())
  496. self.action_clonevm.setEnabled(vm.klass != 'AdminVM')
  497. self.action_resumevm.setEnabled(
  498. not vm.is_running() or vm.get_power_state() == "Paused")
  499. self.action_pausevm.setEnabled(
  500. vm.is_running() and vm.get_power_state() != "Paused"
  501. and vm.klass != 'AdminVM')
  502. self.action_shutdownvm.setEnabled(
  503. vm.is_running() and vm.get_power_state() != "Paused"
  504. and vm.klass != 'AdminVM')
  505. self.action_restartvm.setEnabled(
  506. vm.is_running() and vm.get_power_state() != "Paused"
  507. and vm.klass != 'AdminVM'
  508. and (vm.klass != 'DispVM' or not vm.auto_cleanup))
  509. self.action_killvm.setEnabled(
  510. (vm.get_power_state() == "Paused" or vm.is_running())
  511. and vm.klass != 'AdminVM')
  512. self.action_appmenus.setEnabled(
  513. vm.klass != 'AdminVM' and vm.klass != 'DispVM'
  514. and not vm.features.get('internal', False))
  515. self.action_editfwrules.setEnabled(vm.klass != 'AdminVM')
  516. self.action_updatevm.setEnabled(getattr(vm, 'updateable', False)
  517. or vm.qid == 0)
  518. self.action_run_command_in_vm.setEnabled(
  519. not vm.get_power_state() == "Paused" and vm.qid != 0)
  520. self.action_set_keyboard_layout.setEnabled(
  521. vm.qid != 0 and
  522. vm.get_power_state() != "Paused" and vm.is_running())
  523. else:
  524. self.action_settings.setEnabled(False)
  525. self.action_removevm.setEnabled(False)
  526. self.action_clonevm.setEnabled(False)
  527. self.action_resumevm.setEnabled(False)
  528. self.action_pausevm.setEnabled(False)
  529. self.action_shutdownvm.setEnabled(False)
  530. self.action_restartvm.setEnabled(False)
  531. self.action_killvm.setEnabled(False)
  532. self.action_appmenus.setEnabled(False)
  533. self.action_editfwrules.setEnabled(False)
  534. self.action_updatevm.setEnabled(False)
  535. self.action_run_command_in_vm.setEnabled(False)
  536. self.action_set_keyboard_layout.setEnabled(False)
  537. # noinspection PyArgumentList
  538. @QtCore.pyqtSlot(name='on_action_createvm_triggered')
  539. def action_createvm_triggered(self): # pylint: disable=no-self-use
  540. subprocess.check_call('qubes-vm-create')
  541. def get_selected_vm(self):
  542. # vm selection relies on the VmInfo widget's value used
  543. # for sorting by VM name
  544. row_index = self.table.currentRow()
  545. if row_index != -1:
  546. vm_item = self.table.item(row_index, self.columns_indices["Name"])
  547. # here is possible race with update_table timer so check
  548. # if really got the item
  549. if vm_item is None:
  550. return None
  551. qid = vm_item.qid
  552. assert self.vms_in_table[qid] is not None
  553. vm = self.vms_in_table[qid].vm
  554. return vm
  555. return None
  556. # noinspection PyArgumentList
  557. @QtCore.pyqtSlot(name='on_action_removevm_triggered')
  558. def action_removevm_triggered(self):
  559. # pylint: disable=no-else-return
  560. vm = self.get_selected_vm()
  561. if vm.klass == 'TemplateVM':
  562. dependent_vms = 0
  563. for single_vm in self.qubes_app.domains:
  564. if getattr(single_vm, 'template', None) == vm:
  565. dependent_vms += 1
  566. if dependent_vms > 0:
  567. QtGui.QMessageBox.warning(
  568. None, self.tr("Warning!"),
  569. self.tr("This Template Qube cannot be removed, "
  570. "because there is at least one Qube that is based "
  571. "on it.<br><small>If you want to remove this "
  572. "Template Qube and all the Qubes based on it, you "
  573. "should first remove each individual Qube that "
  574. "uses this template.</small>"))
  575. return
  576. (requested_name, ok) = QtGui.QInputDialog.getText(
  577. None, self.tr("Qube Removal Confirmation"),
  578. self.tr("Are you sure you want to remove the Qube <b>'{0}'</b>"
  579. "?<br> All data on this Qube's private storage will be "
  580. "lost!<br><br>Type the name of the Qube (<b>{1}</b>) below "
  581. "to confirm:").format(vm.name, vm.name))
  582. if not ok:
  583. # user clicked cancel
  584. return
  585. if requested_name != vm.name:
  586. # name did not match
  587. QtGui.QMessageBox.warning(
  588. None,
  589. self.tr("Qube removal confirmation failed"),
  590. self.tr(
  591. "Entered name did not match! Not removing "
  592. "{0}.").format(vm.name))
  593. return
  594. else:
  595. # remove the VM
  596. t_monitor = thread_monitor.ThreadMonitor()
  597. thread = threading.Thread(target=self.do_remove_vm,
  598. args=(vm, self.qubes_app, t_monitor))
  599. thread.daemon = True
  600. thread.start()
  601. progress = QtGui.QProgressDialog(
  602. self.tr(
  603. "Removing Qube: <b>{0}</b>...").format(vm.name), "", 0, 0)
  604. progress.setWindowFlags(QtCore.Qt.Window |
  605. QtCore.Qt.WindowTitleHint |
  606. QtCore.Qt.CustomizeWindowHint)
  607. progress.setCancelButton(None)
  608. progress.setModal(True)
  609. progress.show()
  610. while not t_monitor.is_finished():
  611. self.qt_app.processEvents()
  612. time.sleep(0.1)
  613. progress.hide()
  614. if t_monitor.success:
  615. pass
  616. else:
  617. QtGui.QMessageBox.warning(None, self.tr("Error removing Qube!"),
  618. self.tr("ERROR: {0}").format(
  619. t_monitor.error_msg))
  620. @staticmethod
  621. def do_remove_vm(vm, qubes_app, t_monitor):
  622. try:
  623. del qubes_app.domains[vm.name]
  624. except exc.QubesException as ex:
  625. t_monitor.set_error_msg(str(ex))
  626. t_monitor.set_finished()
  627. # noinspection PyArgumentList
  628. @QtCore.pyqtSlot(name='on_action_clonevm_triggered')
  629. def action_clonevm_triggered(self):
  630. vm = self.get_selected_vm()
  631. name_number = 1
  632. name_format = vm.name + '-clone-%d'
  633. while name_format % name_number in self.qubes_app.domains.keys():
  634. name_number += 1
  635. (clone_name, ok) = QtGui.QInputDialog.getText(
  636. self, self.tr('Qubes clone Qube'),
  637. self.tr('Enter name for Qube <b>{}</b> clone:').format(vm.name),
  638. text=(name_format % name_number))
  639. if not ok or clone_name == "":
  640. return
  641. t_monitor = thread_monitor.ThreadMonitor()
  642. thread = threading.Thread(target=self.do_clone_vm,
  643. args=(vm, self.qubes_app,
  644. clone_name, t_monitor))
  645. thread.daemon = True
  646. thread.start()
  647. progress = QtGui.QProgressDialog(
  648. self.tr("Cloning Qube <b>{0}</b> to <b>{1}</b>...").format(
  649. vm.name, clone_name), "", 0, 0)
  650. progress.setWindowFlags(QtCore.Qt.Window |
  651. QtCore.Qt.WindowTitleHint |
  652. QtCore.Qt.CustomizeWindowHint)
  653. progress.setCancelButton(None)
  654. progress.setModal(True)
  655. progress.show()
  656. while not t_monitor.is_finished():
  657. self.qt_app.processEvents()
  658. time.sleep(0.2)
  659. progress.hide()
  660. if not t_monitor.success:
  661. QtGui.QMessageBox.warning(
  662. None,
  663. self.tr("Error while cloning Qube"),
  664. self.tr("Exception while cloning:<br>{0}").format(
  665. t_monitor.error_msg))
  666. @staticmethod
  667. def do_clone_vm(src_vm, qubes_app, dst_name, t_monitor):
  668. dst_vm = None
  669. try:
  670. dst_vm = qubes_app.clone_vm(src_vm, dst_name)
  671. except exc.QubesException as ex:
  672. t_monitor.set_error_msg(str(ex))
  673. if dst_vm:
  674. pass
  675. t_monitor.set_finished()
  676. # noinspection PyArgumentList
  677. @QtCore.pyqtSlot(name='on_action_resumevm_triggered')
  678. def action_resumevm_triggered(self):
  679. vm = self.get_selected_vm()
  680. if vm.get_power_state() in ["Paused", "Suspended"]:
  681. try:
  682. vm.unpause()
  683. self.vms_in_table[vm.qid].update()
  684. self.table_selection_changed()
  685. except exc.QubesException as ex:
  686. QtGui.QMessageBox.warning(
  687. None, self.tr("Error unpausing Qube!"),
  688. self.tr("ERROR: {0}").format(ex))
  689. return
  690. self.start_vm(vm)
  691. def start_vm(self, vm):
  692. if vm.is_running():
  693. return
  694. t_monitor = thread_monitor.ThreadMonitor()
  695. thread = threading.Thread(target=self.do_start_vm,
  696. args=(vm, t_monitor))
  697. thread.daemon = True
  698. thread.start()
  699. while not t_monitor.is_finished():
  700. self.qt_app.processEvents()
  701. time.sleep(0.1)
  702. if not t_monitor.success:
  703. QtGui.QMessageBox.warning(
  704. None,
  705. self.tr("Error starting Qube!"),
  706. self.tr("ERROR: {0}").format(t_monitor.error_msg))
  707. @staticmethod
  708. def do_start_vm(vm, t_monitor):
  709. try:
  710. vm.start()
  711. except exc.QubesException as ex:
  712. t_monitor.set_error_msg(str(ex))
  713. t_monitor.set_finished()
  714. return
  715. t_monitor.set_finished()
  716. # noinspection PyArgumentList
  717. @QtCore.pyqtSlot(name='on_action_startvm_tools_install_triggered')
  718. # TODO: replace with boot from device
  719. def action_startvm_tools_install_triggered(self):
  720. # pylint: disable=invalid-name
  721. pass
  722. @QtCore.pyqtSlot(name='on_action_pausevm_triggered')
  723. def action_pausevm_triggered(self):
  724. vm = self.get_selected_vm()
  725. try:
  726. vm.pause()
  727. self.vms_in_table[vm.qid].update()
  728. self.table_selection_changed()
  729. except exc.QubesException as ex:
  730. QtGui.QMessageBox.warning(
  731. None,
  732. self.tr("Error pausing Qube!"),
  733. self.tr("ERROR: {0}").format(ex))
  734. return
  735. # noinspection PyArgumentList
  736. @QtCore.pyqtSlot(name='on_action_shutdownvm_triggered')
  737. def action_shutdownvm_triggered(self):
  738. vm = self.get_selected_vm()
  739. reply = QtGui.QMessageBox.question(
  740. None, self.tr("Qube Shutdown Confirmation"),
  741. self.tr("Are you sure you want to power down the Qube"
  742. " <b>'{0}'</b>?<br><small>This will shutdown all the "
  743. "running applications within this Qube.</small>").format(
  744. vm.name), QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel)
  745. self.qt_app.processEvents()
  746. if reply == QtGui.QMessageBox.Yes:
  747. self.shutdown_vm(vm)
  748. def shutdown_vm(self, vm, shutdown_time=vm_shutdown_timeout,
  749. check_time=vm_restart_check_timeout, and_restart=False):
  750. try:
  751. vm.shutdown()
  752. except exc.QubesException as ex:
  753. QtGui.QMessageBox.warning(
  754. None,
  755. self.tr("Error shutting down Qube!"),
  756. self.tr("ERROR: {0}").format(ex))
  757. return
  758. self.shutdown_monitor[vm.qid] = VmShutdownMonitor(vm, shutdown_time,
  759. check_time,
  760. and_restart, self)
  761. # noinspection PyCallByClass,PyTypeChecker
  762. QtCore.QTimer.singleShot(check_time, self.shutdown_monitor[
  763. vm.qid].check_if_vm_has_shutdown)
  764. # noinspection PyArgumentList
  765. @QtCore.pyqtSlot(name='on_action_restartvm_triggered')
  766. def action_restartvm_triggered(self):
  767. vm = self.get_selected_vm()
  768. reply = QtGui.QMessageBox.question(
  769. None, self.tr("Qube Restart Confirmation"),
  770. self.tr("Are you sure you want to restart the Qube <b>'{0}'</b>?"
  771. "<br><small>This will shutdown all the running "
  772. "applications within this Qube.</small>").format(vm.name),
  773. QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel)
  774. self.qt_app.processEvents()
  775. if reply == QtGui.QMessageBox.Yes:
  776. # in case the user shut down the VM in the meantime
  777. if vm.is_running():
  778. self.shutdown_vm(vm, and_restart=True)
  779. else:
  780. self.start_vm(vm)
  781. # noinspection PyArgumentList
  782. @QtCore.pyqtSlot(name='on_action_killvm_triggered')
  783. def action_killvm_triggered(self):
  784. vm = self.get_selected_vm()
  785. if not (vm.is_running() or vm.is_paused()):
  786. info = self.tr("Qube <b>'{0}'</b> is not running. Are you "
  787. "absolutely sure you want to try to kill it?<br>"
  788. "<small>This will end <b>(not shutdown!)</b> all "
  789. "the running applications within this "
  790. "Qube.</small>").format(vm.name)
  791. else:
  792. info = self.tr("Are you sure you want to kill the Qube "
  793. "<b>'{0}'</b>?<br><small>This will end <b>(not "
  794. "shutdown!)</b> all the running applications within "
  795. "this Qube.</small>").format(vm.name)
  796. reply = QtGui.QMessageBox.question(
  797. None, self.tr("Qube Kill Confirmation"), info,
  798. QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel,
  799. QtGui.QMessageBox.Cancel)
  800. self.qt_app.processEvents()
  801. if reply == QtGui.QMessageBox.Yes:
  802. try:
  803. vm.kill()
  804. except exc.QubesException as ex:
  805. QtGui.QMessageBox.critical(
  806. None, self.tr("Error while killing Qube!"),
  807. self.tr(
  808. "<b>An exception ocurred while killing {0}.</b><br>"
  809. "ERROR: {1}").format(vm.name, ex))
  810. return
  811. # noinspection PyArgumentList
  812. @QtCore.pyqtSlot(name='on_action_settings_triggered')
  813. def action_settings_triggered(self):
  814. vm = self.get_selected_vm()
  815. if vm:
  816. settings_window = settings.VMSettingsWindow(
  817. vm, self.qt_app, "basic")
  818. settings_window.exec_()
  819. self.vms_in_table[vm.qid].update()
  820. # noinspection PyArgumentList
  821. @QtCore.pyqtSlot(name='on_action_appmenus_triggered')
  822. def action_appmenus_triggered(self):
  823. vm = self.get_selected_vm()
  824. if vm:
  825. settings_window = settings.VMSettingsWindow(
  826. vm, self.qt_app, "applications")
  827. settings_window.exec_()
  828. # noinspection PyArgumentList
  829. @QtCore.pyqtSlot(name='on_action_updatevm_triggered')
  830. def action_updatevm_triggered(self):
  831. vm = self.get_selected_vm()
  832. if not vm.is_running():
  833. reply = QtGui.QMessageBox.question(
  834. None, self.tr("Qube Update Confirmation"),
  835. self.tr(
  836. "<b>{0}</b><br>The Qube has to be running to be updated."
  837. "<br>Do you want to start it?<br>").format(vm.name),
  838. QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel)
  839. if reply != QtGui.QMessageBox.Yes:
  840. return
  841. self.qt_app.processEvents()
  842. t_monitor = thread_monitor.ThreadMonitor()
  843. thread = threading.Thread(target=self.do_update_vm,
  844. args=(vm, t_monitor))
  845. thread.daemon = True
  846. thread.start()
  847. progress = QtGui.QProgressDialog(
  848. self.tr(
  849. "<b>{0}</b><br>Please wait for the updater to "
  850. "launch...").format(vm.name), "", 0, 0)
  851. progress.setWindowFlags(QtCore.Qt.Window |
  852. QtCore.Qt.WindowTitleHint |
  853. QtCore.Qt.CustomizeWindowHint)
  854. progress.setCancelButton(None)
  855. progress.setModal(True)
  856. progress.show()
  857. while not t_monitor.is_finished():
  858. self.qt_app.processEvents()
  859. time.sleep(0.2)
  860. progress.hide()
  861. if vm.qid != 0:
  862. if not t_monitor.success:
  863. QtGui.QMessageBox.warning(
  864. None,
  865. self.tr("Error on Qube update!"),
  866. self.tr("ERROR: {0}").format(t_monitor.error_msg))
  867. @staticmethod
  868. def do_update_vm(vm, t_monitor):
  869. try:
  870. if vm.qid == 0:
  871. subprocess.check_call(
  872. ["/usr/bin/qubes-dom0-update", "--clean", "--gui"])
  873. else:
  874. if not vm.is_running():
  875. vm.start()
  876. vm.run_service("qubes.InstallUpdatesGUI",
  877. user="root", wait=False)
  878. except (ChildProcessError, exc.QubesException) as ex:
  879. t_monitor.set_error_msg(str(ex))
  880. t_monitor.set_finished()
  881. return
  882. t_monitor.set_finished()
  883. # noinspection PyArgumentList
  884. @QtCore.pyqtSlot(name='on_action_run_command_in_vm_triggered')
  885. def action_run_command_in_vm_triggered(self):
  886. # pylint: disable=invalid-name
  887. vm = self.get_selected_vm()
  888. (command_to_run, ok) = QtGui.QInputDialog.getText(
  889. self, self.tr('Qubes command entry'),
  890. self.tr('Run command in <b>{}</b>:').format(vm.name))
  891. if not ok or command_to_run == "":
  892. return
  893. t_monitor = thread_monitor.ThreadMonitor()
  894. thread = threading.Thread(target=self.do_run_command_in_vm, args=(
  895. vm, command_to_run, t_monitor))
  896. thread.daemon = True
  897. thread.start()
  898. while not t_monitor.is_finished():
  899. self.qt_app.processEvents()
  900. time.sleep(0.2)
  901. if not t_monitor.success:
  902. QtGui.QMessageBox.warning(
  903. None, self.tr("Error while running command"),
  904. self.tr("Exception while running command:<br>{0}").format(
  905. t_monitor.error_msg))
  906. @staticmethod
  907. def do_run_command_in_vm(vm, command_to_run, t_monitor):
  908. try:
  909. vm.run(command_to_run)
  910. except (ChildProcessError, exc.QubesException) as ex:
  911. t_monitor.set_error_msg(str(ex))
  912. t_monitor.set_finished()
  913. # noinspection PyArgumentList
  914. @QtCore.pyqtSlot(name='on_action_set_keyboard_layout_triggered')
  915. def action_set_keyboard_layout_triggered(self):
  916. # pylint: disable=invalid-name
  917. vm = self.get_selected_vm()
  918. vm.run('qubes-change-keyboard-layout')
  919. # noinspection PyArgumentList
  920. @QtCore.pyqtSlot(name='on_action_editfwrules_triggered')
  921. def action_editfwrules_triggered(self):
  922. vm = self.get_selected_vm()
  923. settings_window = settings.VMSettingsWindow(vm, self.qt_app, "firewall")
  924. settings_window.exec_()
  925. # noinspection PyArgumentList
  926. @QtCore.pyqtSlot(name='on_action_global_settings_triggered')
  927. def action_global_settings_triggered(self): # pylint: disable=invalid-name
  928. global_settings_window = global_settings.GlobalSettingsWindow(
  929. self.qt_app,
  930. self.qubes_app)
  931. global_settings_window.exec_()
  932. # noinspection PyArgumentList
  933. @QtCore.pyqtSlot(name='on_action_show_network_triggered')
  934. def action_show_network_triggered(self):
  935. pass
  936. # TODO: revive for 4.1
  937. # network_notes_dialog = NetworkNotesDialog()
  938. # network_notes_dialog.exec_()
  939. # noinspection PyArgumentList
  940. @QtCore.pyqtSlot(name='on_action_restore_triggered')
  941. def action_restore_triggered(self):
  942. restore_window = restore.RestoreVMsWindow(self.qt_app, self.qubes_app)
  943. restore_window.exec_()
  944. # noinspection PyArgumentList
  945. @QtCore.pyqtSlot(name='on_action_backup_triggered')
  946. def action_backup_triggered(self):
  947. backup_window = backup.BackupVMsWindow(self.qt_app, self.qubes_app)
  948. backup_window.exec_()
  949. # noinspection PyArgumentList
  950. @QtCore.pyqtSlot(name='on_action_exit_triggered')
  951. def action_exit_triggered(self):
  952. self.close()
  953. def showhide_menubar(self, checked):
  954. self.menubar.setVisible(checked)
  955. if not checked:
  956. self.context_menu.addAction(self.action_menubar)
  957. else:
  958. self.context_menu.removeAction(self.action_menubar)
  959. if self.settings_loaded:
  960. self.manager_settings.setValue('view/menubar_visible', checked)
  961. self.manager_settings.sync()
  962. def showhide_toolbar(self, checked):
  963. self.toolbar.setVisible(checked)
  964. if not checked:
  965. self.context_menu.addAction(self.action_toolbar)
  966. else:
  967. self.context_menu.removeAction(self.action_toolbar)
  968. if self.settings_loaded:
  969. self.manager_settings.setValue('view/toolbar_visible', checked)
  970. self.manager_settings.sync()
  971. def showhide_column(self, col_num, show):
  972. self.table.setColumnHidden(col_num, not show)
  973. val = 1 if show else -1
  974. self.visible_columns_count += val
  975. if self.visible_columns_count == 1:
  976. # disable hiding the last one
  977. for col in self.columns_actions:
  978. if self.columns_actions[col].isChecked():
  979. self.columns_actions[col].setEnabled(False)
  980. break
  981. elif self.visible_columns_count == 2 and val == 1:
  982. # enable hiding previously disabled column
  983. for col in self.columns_actions:
  984. if not self.columns_actions[col].isEnabled():
  985. self.columns_actions[col].setEnabled(True)
  986. break
  987. if self.settings_loaded:
  988. col_name = [name for name in self.columns_indices if
  989. self.columns_indices[name] == col_num][0]
  990. self.manager_settings.setValue('columns/%s' % col_name, show)
  991. self.manager_settings.sync()
  992. def on_action_vm_type_toggled(self, checked):
  993. self.showhide_column(self.columns_indices['Type'], checked)
  994. def on_action_label_toggled(self, checked):
  995. self.showhide_column(self.columns_indices['Label'], checked)
  996. def on_action_name_toggled(self, checked):
  997. self.showhide_column(self.columns_indices['Name'], checked)
  998. def on_action_state_toggled(self, checked):
  999. self.showhide_column(self.columns_indices['State'], checked)
  1000. def on_action_internal_toggled(self, checked):
  1001. self.showhide_column(self.columns_indices['Internal'], checked)
  1002. def on_action_ip_toggled(self, checked):
  1003. self.showhide_column(self.columns_indices['IP'], checked)
  1004. def on_action_backups_toggled(self, checked):
  1005. self.showhide_column(self.columns_indices['Backups'], checked)
  1006. def on_action_last_backup_toggled(self, checked):
  1007. self.showhide_column(self.columns_indices['Last backup'], checked)
  1008. def on_action_template_toggled(self, checked):
  1009. self.showhide_column(self.columns_indices['Template'], checked)
  1010. def on_action_netvm_toggled(self, checked):
  1011. self.showhide_column(self.columns_indices['NetVM'], checked)
  1012. def on_action_size_on_disk_toggled(self, checked):
  1013. self.showhide_column(self.columns_indices['Size'], checked)
  1014. # noinspection PyArgumentList
  1015. @QtCore.pyqtSlot(name='on_action_about_qubes_triggered')
  1016. def action_about_qubes_triggered(self): # pylint: disable=no-self-use
  1017. about = AboutDialog()
  1018. about.exec_()
  1019. def createPopupMenu(self): # pylint: disable=invalid-name
  1020. menu = QtGui.QMenu()
  1021. menu.addAction(self.action_toolbar)
  1022. menu.addAction(self.action_menubar)
  1023. return menu
  1024. def open_tools_context_menu(self, widget, point):
  1025. self.tools_context_menu.exec_(widget.mapToGlobal(point))
  1026. @QtCore.pyqtSlot('const QPoint&')
  1027. def open_context_menu(self, point):
  1028. try:
  1029. vm = self.get_selected_vm()
  1030. # logs menu
  1031. self.logs_menu.clear()
  1032. if vm.qid == 0:
  1033. logfiles = ["/var/log/xen/console/hypervisor.log"]
  1034. else:
  1035. logfiles = [
  1036. "/var/log/xen/console/guest-" + vm.name + ".log",
  1037. "/var/log/xen/console/guest-" + vm.name + "-dm.log",
  1038. "/var/log/qubes/guid." + vm.name + ".log",
  1039. "/var/log/qubes/qrexec." + vm.name + ".log",
  1040. ]
  1041. menu_empty = True
  1042. for logfile in logfiles:
  1043. if os.path.exists(logfile):
  1044. action = self.logs_menu.addAction(QtGui.QIcon(":/log.png"),
  1045. logfile)
  1046. action.setData(logfile)
  1047. menu_empty = False
  1048. self.logs_menu.setEnabled(not menu_empty)
  1049. if vm.qid == 0:
  1050. self.dom0_context_menu.exec_(self.table.mapToGlobal(
  1051. point + QtCore.QPoint(10, 0)))
  1052. else:
  1053. self.context_menu.exec_(self.table.mapToGlobal(
  1054. point + QtCore.QPoint(10, 0)))
  1055. except exc.QubesPropertyAccessError:
  1056. pass
  1057. @QtCore.pyqtSlot('QAction *')
  1058. def show_log(self, action):
  1059. log = str(action.data())
  1060. log_dlg = log_dialog.LogDialog(self.qt_app, log)
  1061. log_dlg.exec_()
  1062. # Bases on the original code by:
  1063. # Copyright (c) 2002-2007 Pascal Varet <p.varet@gmail.com>
  1064. def handle_exception(exc_type, exc_value, exc_traceback):
  1065. filename, line, dummy, dummy = traceback.extract_tb(exc_traceback).pop()
  1066. filename = os.path.basename(filename)
  1067. error = "%s: %s" % (exc_type.__name__, exc_value)
  1068. strace = ""
  1069. stacktrace = traceback.extract_tb(exc_traceback)
  1070. while stacktrace:
  1071. (filename, line, func, txt) = stacktrace.pop()
  1072. strace += "----\n"
  1073. strace += "line: %s\n" % txt
  1074. strace += "func: %s\n" % func
  1075. strace += "line no.: %d\n" % line
  1076. strace += "file: %s\n" % filename
  1077. msg_box = QtGui.QMessageBox()
  1078. msg_box.setDetailedText(strace)
  1079. msg_box.setIcon(QtGui.QMessageBox.Critical)
  1080. msg_box.setWindowTitle("Houston, we have a problem...")
  1081. msg_box.setText("Whoops. A critical error has occured. "
  1082. "This is most likely a bug in Qubes Manager.<br><br>"
  1083. "<b><i>%s</i></b>" % error +
  1084. "<br/>at line <b>%d</b><br/>of file %s.<br/><br/>"
  1085. % (line, filename))
  1086. msg_box.exec_()
  1087. def main():
  1088. qt_app = QtGui.QApplication(sys.argv)
  1089. qt_app.setOrganizationName("The Qubes Project")
  1090. qt_app.setOrganizationDomain("http://qubes-os.org")
  1091. qt_app.setApplicationName("Qube Manager")
  1092. qt_app.setWindowIcon(QtGui.QIcon.fromTheme("qubes-manager"))
  1093. sys.excepthook = handle_exception
  1094. qubes_app = Qubes()
  1095. manager_window = VmManagerWindow(qt_app, qubes_app)
  1096. manager_window.show()
  1097. qt_app.exec_()
  1098. if __name__ == "__main__":
  1099. main()