table_widgets.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #!/usr/bin/python3
  2. # -*- coding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2014 Marek Marczykowski-Górecki
  7. # <marmarek@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public License along
  20. # with this program; if not, see <http://www.gnu.org/licenses/>.
  21. import datetime
  22. from PyQt5 import QtWidgets, QtCore, QtGui # pylint: disable=import-error
  23. # pylint: disable=too-few-public-methods
  24. power_order = QtCore.Qt.DescendingOrder
  25. update_order = QtCore.Qt.AscendingOrder
  26. row_height = 30
  27. class VmIconWidget(QtWidgets.QWidget):
  28. def __init__(self, icon_path, enabled=True, size_multiplier=0.7,
  29. tooltip=None, parent=None,
  30. icon_sz=(32, 32)): # pylint: disable=unused-argument
  31. super(VmIconWidget, self).__init__(parent)
  32. self.enabled = enabled
  33. self.size_multiplier = size_multiplier
  34. self.label_icon = QtWidgets.QLabel()
  35. self.set_icon(icon_path)
  36. if tooltip is not None:
  37. self.label_icon.setToolTip(tooltip)
  38. layout = QtWidgets.QHBoxLayout()
  39. layout.addWidget(self.label_icon)
  40. layout.setContentsMargins(0, 0, 0, 0)
  41. self.setLayout(layout)
  42. def setToolTip(self, tooltip): # pylint: disable=invalid-name
  43. if tooltip is not None:
  44. self.label_icon.setToolTip(tooltip)
  45. else:
  46. self.label_icon.setToolTip('')
  47. def set_icon(self, icon_path):
  48. if icon_path[0] in ':/':
  49. icon = QtGui.QIcon(icon_path)
  50. else:
  51. icon = QtGui.QIcon.fromTheme(icon_path)
  52. icon_sz = QtCore.QSize(row_height * self.size_multiplier,
  53. row_height * self.size_multiplier)
  54. icon_pixmap = icon.pixmap(
  55. icon_sz,
  56. QtGui.QIcon.Disabled if not self.enabled else QtGui.QIcon.Normal)
  57. self.label_icon.setPixmap(icon_pixmap)
  58. self.label_icon.setFixedSize(icon_sz)
  59. class VmTypeWidget(VmIconWidget):
  60. class VmTypeItem(QtWidgets.QTableWidgetItem):
  61. def __init__(self, value, vm):
  62. super(VmTypeWidget.VmTypeItem, self).__init__()
  63. self.value = value
  64. self.qid = vm.qid
  65. self.name = vm.name
  66. def set_value(self, value):
  67. self.value = value
  68. # pylint: disable=too-many-return-statements
  69. def __lt__(self, other):
  70. if self.qid == 0:
  71. return True
  72. if other.qid == 0:
  73. return False
  74. if self.value == other.value:
  75. return self.name < other.name
  76. return self.value < other.value
  77. def __init__(self, vm, parent=None):
  78. (icon_path, tooltip) = self.get_vm_icon(vm)
  79. super(VmTypeWidget, self).__init__(
  80. icon_path, True, 0.8, tooltip, parent)
  81. self.vm = vm
  82. self.table_item = self.VmTypeItem(self.value, vm)
  83. self.value = None
  84. # TODO: add "provides network" column
  85. def get_vm_icon(self, vm):
  86. if vm.klass == 'AdminVM':
  87. self.value = 0
  88. icon_name = "dom0"
  89. elif vm.klass == 'TemplateVM':
  90. self.value = 3
  91. icon_name = "templatevm"
  92. elif vm.klass == 'StandaloneVM':
  93. self.value = 4
  94. icon_name = "standalonevm"
  95. else:
  96. self.value = 5 + vm.label.index
  97. icon_name = "appvm"
  98. return ":/" + icon_name + ".png", vm.klass
  99. class VmLabelWidget(VmIconWidget):
  100. class VmLabelItem(QtWidgets.QTableWidgetItem):
  101. def __init__(self, value, vm):
  102. super(VmLabelWidget.VmLabelItem, self).__init__()
  103. self.value = value
  104. self.qid = vm.qid
  105. self.name = vm.name
  106. def set_value(self, value):
  107. self.value = value
  108. # pylint: disable=too-many-return-statements
  109. def __lt__(self, other):
  110. if self.qid == 0:
  111. return True
  112. if other.qid == 0:
  113. return False
  114. if self.value == other.value:
  115. return self.name < other.name
  116. return self.value < other.value
  117. def __init__(self, vm, parent=None):
  118. self.icon_path = self.get_vm_icon_path(vm)
  119. super(VmLabelWidget, self).__init__(self.icon_path,
  120. True, 0.8, None, parent)
  121. self.vm = vm
  122. self.table_item = self.VmLabelItem(self.value, vm)
  123. self.value = None
  124. def get_vm_icon_path(self, vm):
  125. self.value = vm.label.index
  126. return vm.label.icon
  127. def update(self):
  128. icon_path = self.get_vm_icon_path(self.vm)
  129. if icon_path != self.icon_path:
  130. self.icon_path = icon_path
  131. self.set_icon(icon_path)
  132. class VmStatusIcon(QtWidgets.QLabel):
  133. def __init__(self, vm, parent=None):
  134. super(VmStatusIcon, self).__init__(parent)
  135. self.vm = vm
  136. self.status = None
  137. self.set_on_icon()
  138. self.previous_power_state = self.vm.get_power_state()
  139. def update(self):
  140. if self.previous_power_state != self.vm.get_power_state():
  141. self.set_on_icon()
  142. self.previous_power_state = self.vm.get_power_state()
  143. def set_on_icon(self):
  144. if self.vm.get_power_state() == "Running":
  145. icon = QtGui.QIcon(":/on.png")
  146. self.status = 3
  147. elif self.vm.get_power_state() in ["Paused", "Suspended"]:
  148. icon = QtGui.QIcon(":/paused.png")
  149. self.status = 2
  150. elif self.vm.get_power_state() in ["Transient", "Halting", "Dying"]:
  151. icon = QtGui.QIcon(":/transient.png")
  152. self.status = 1
  153. else:
  154. icon = QtGui.QIcon(":/off.png")
  155. self.status = 0
  156. icon_sz = QtCore.QSize(row_height * 0.5, row_height * 0.5)
  157. icon_pixmap = icon.pixmap(icon_sz)
  158. self.setPixmap(icon_pixmap)
  159. self.setFixedSize(icon_sz)
  160. class VmInfoWidget(QtWidgets.QWidget):
  161. class VmInfoItem(QtWidgets.QTableWidgetItem):
  162. def __init__(self, on_icon, upd_info_item, vm):
  163. super(VmInfoWidget.VmInfoItem, self).__init__()
  164. self.on_icon = on_icon
  165. self.upd_info_item = upd_info_item
  166. self.vm = vm
  167. self.qid = vm.qid
  168. self.name = vm.name
  169. def __lt__(self, other):
  170. # pylint: disable=too-many-return-statements
  171. if self.qid == 0:
  172. return True
  173. if other.qid == 0:
  174. return False
  175. self_val = self.upd_info_item.value
  176. other_val = other.upd_info_item.value
  177. if self.tableWidget().\
  178. horizontalHeader().sortIndicatorOrder() == update_order:
  179. # the result will be sorted by upd, sorting order: Ascending
  180. self_val += 1 if self.on_icon.status > 0 else 0
  181. other_val += 1 if other.on_icon.status > 0 else 0
  182. if self_val == other_val:
  183. return self.name < other.name
  184. return self_val > other_val
  185. if self.tableWidget().\
  186. horizontalHeader().sortIndicatorOrder() == power_order:
  187. # the result will be sorted by power state,
  188. # sorting order: Descending
  189. if self.on_icon.status == other.on_icon.status:
  190. return self.name < other.name
  191. return self_val > other_val
  192. # it would be strange if this happened
  193. return
  194. def __init__(self, vm, parent=None):
  195. super(VmInfoWidget, self).__init__(parent)
  196. self.vm = vm
  197. layout = QtWidgets.QHBoxLayout()
  198. self.on_icon = VmStatusIcon(vm)
  199. self.upd_info = VmUpdateInfoWidget(vm, show_text=False)
  200. self.error_icon = VmIconWidget(":/warning.png")
  201. self.blk_icon = VmIconWidget(":/mount.png")
  202. self.rec_icon = VmIconWidget(":/mic.png")
  203. layout.addWidget(self.on_icon)
  204. layout.addWidget(self.upd_info)
  205. layout.addWidget(self.error_icon)
  206. layout.addItem(QtWidgets.QSpacerItem(0, 10,
  207. QtWidgets.QSizePolicy.Expanding,
  208. QtWidgets.QSizePolicy.Expanding))
  209. layout.addWidget(self.blk_icon)
  210. layout.addWidget(self.rec_icon)
  211. layout.setContentsMargins(5, 0, 5, 0)
  212. self.setLayout(layout)
  213. self.rec_icon.setVisible(False)
  214. self.blk_icon.setVisible(False)
  215. self.error_icon.setVisible(False)
  216. self.table_item = self.VmInfoItem(self.on_icon,
  217. self.upd_info.table_item, vm)
  218. def update_vm_state(self):
  219. self.on_icon.update()
  220. self.upd_info.update_outdated()
  221. class VMPropertyItem(QtWidgets.QTableWidgetItem):
  222. def __init__(self, vm, property_name, empty_function=(lambda x: False),
  223. check_default=False):
  224. """
  225. Class used to represent Qube Manager table widget.
  226. :param vm: vm object
  227. :param property_name: name of the property the widget represents
  228. :param empty_function: a function that, when applied to values of
  229. vm.property_name, returns True when the property value should be
  230. represented as an empty string and False otherwise; by default this
  231. function always returns false (vm.property_name is represented by an
  232. empty string only when it actually is one)
  233. :param check_default: if True, the widget will prepend its text with
  234. "default" if the if the property is set to DEFAULT
  235. """
  236. super(VMPropertyItem, self).__init__()
  237. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  238. self.setTextAlignment(QtCore.Qt.AlignVCenter)
  239. self.vm = vm
  240. self.qid = vm.qid
  241. self.property_name = property_name
  242. self.name = vm.name
  243. self.empty_function = empty_function
  244. self.check_default = check_default
  245. self.update()
  246. def update(self):
  247. val = getattr(self.vm, self.property_name, None)
  248. if self.empty_function(val):
  249. text = ""
  250. elif val is None:
  251. text = "n/a"
  252. elif val is True:
  253. text = "Yes"
  254. else:
  255. text = str(val)
  256. if self.check_default and hasattr(self.vm, self.property_name) and \
  257. self.vm.property_is_default(self.property_name):
  258. text = 'default (' + text + ')'
  259. self.setText(text)
  260. def __lt__(self, other):
  261. if self.qid == 0:
  262. return True
  263. if other.qid == 0:
  264. return False
  265. if self.text() == other.text():
  266. return self.name < other.name
  267. return super(VMPropertyItem, self).__lt__(other)
  268. class VmTemplateItem(VMPropertyItem):
  269. def __init__(self, vm):
  270. super(VmTemplateItem, self).__init__(vm, "template")
  271. def update(self):
  272. if getattr(self.vm, 'template', None) is not None:
  273. self.setText(self.vm.template.name)
  274. else:
  275. font = QtGui.QFont()
  276. font.setStyle(QtGui.QFont.StyleItalic)
  277. self.setFont(font)
  278. self.setForeground(QtGui.QBrush(QtGui.QColor("gray")))
  279. self.setText(self.vm.klass)
  280. class VmInternalItem(VMPropertyItem):
  281. def __init__(self, vm):
  282. super(VmInternalItem, self).__init__(vm, None)
  283. def update(self):
  284. internal = self.vm.features.get('internal', False)
  285. self.setText("Yes" if internal else "")
  286. # features man qvm-features
  287. class VmUpdateInfoWidget(QtWidgets.QWidget):
  288. class VmUpdateInfoItem(QtWidgets.QTableWidgetItem):
  289. def __init__(self, value, vm):
  290. super(VmUpdateInfoWidget.VmUpdateInfoItem, self).__init__()
  291. self.value = 0
  292. self.vm = vm
  293. self.qid = vm.qid
  294. self.name = vm.name
  295. self.set_value(value)
  296. def set_value(self, value):
  297. if value in ("outdated", "to-be-outdated"):
  298. self.value = 30
  299. elif value == "update":
  300. self.value = 20
  301. else:
  302. self.value = 0
  303. def __lt__(self, other):
  304. if self.qid == 0:
  305. return True
  306. if other.qid == 0:
  307. return False
  308. if self.value == other.value:
  309. return self.name < other.name
  310. return self.value < other.value
  311. def __init__(self, vm, show_text=True, parent=None):
  312. super(VmUpdateInfoWidget, self).__init__(parent)
  313. layout = QtWidgets.QHBoxLayout()
  314. self.show_text = show_text
  315. if self.show_text:
  316. self.label = QtWidgets.QLabel("")
  317. layout.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
  318. else:
  319. self.icon = QtWidgets.QLabel("")
  320. layout.addWidget(self.icon, alignment=QtCore.Qt.AlignCenter)
  321. self.setLayout(layout)
  322. self.vm = vm
  323. self.previous_outdated_state = None
  324. self.previous_update_recommended = None
  325. self.value = None
  326. self.table_item = VmUpdateInfoWidget.VmUpdateInfoItem(self.value, vm)
  327. self.update_outdated()
  328. def update_outdated(self):
  329. outdated_state = False
  330. if self.vm.is_running():
  331. if hasattr(self.vm, 'template') and self.vm.template.is_running():
  332. outdated_state = "to-be-outdated"
  333. if not outdated_state:
  334. for vol in self.vm.volumes.values():
  335. if vol.is_outdated():
  336. outdated_state = "outdated"
  337. break
  338. if self.vm.klass in {'TemplateVM', 'StandaloneVM'} and \
  339. self.vm.features.get('updates-available', False):
  340. outdated_state = 'update'
  341. self.update_status_widget(outdated_state)
  342. def update_status_widget(self, state):
  343. if state == self.previous_outdated_state:
  344. return
  345. self.previous_outdated_state = state
  346. self.value = state
  347. self.table_item.set_value(state)
  348. if state == "update":
  349. label_text = "<font color=\"#CCCC00\">Check updates</font>"
  350. icon_path = ":/update-recommended.png"
  351. tooltip_text = self.tr("Updates pending!")
  352. elif state == "outdated":
  353. label_text = "<font color=\"red\">Qube outdated</font>"
  354. icon_path = ":/outdated.png"
  355. tooltip_text = self.tr(
  356. "The qube must be restarted for its filesystem to reflect the "
  357. "template's recent committed changes.")
  358. elif state == "to-be-outdated":
  359. label_text = "<font color=\"#800000\">Template running</font>"
  360. icon_path = ":/to-be-outdated.png"
  361. tooltip_text = self.tr(
  362. "The Template must be stopped before changes from its "
  363. "current session can be picked up by this qube.")
  364. else:
  365. label_text = None
  366. tooltip_text = None
  367. icon_path = None
  368. if hasattr(self, 'icon'):
  369. self.icon.setVisible(False)
  370. self.layout().removeWidget(self.icon)
  371. del self.icon
  372. if self.show_text:
  373. self.label.setText(label_text)
  374. else:
  375. if icon_path is not None:
  376. self.icon = VmIconWidget(icon_path, True, 0.7)
  377. self.icon.setToolTip(tooltip_text)
  378. self.layout().addWidget(self.icon,
  379. alignment=QtCore.Qt.AlignCenter)
  380. self.icon.setVisible(True)
  381. class VmSizeOnDiskItem(QtWidgets.QTableWidgetItem):
  382. def __init__(self, vm):
  383. super(VmSizeOnDiskItem, self).__init__()
  384. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  385. self.vm = vm
  386. self.qid = vm.qid
  387. self.name = vm.name
  388. self.value = 0
  389. self.update()
  390. self.setTextAlignment(QtCore.Qt.AlignVCenter)
  391. def update(self):
  392. if self.vm.qid == 0:
  393. self.setText("n/a")
  394. else:
  395. self.value = 10
  396. self.value = round(self.vm.get_disk_utilization()/(1024*1024), 2)
  397. self.setText(str(self.value) + " MiB")
  398. def __lt__(self, other):
  399. if self.qid == 0:
  400. return True
  401. if other.qid == 0:
  402. return False
  403. if self.value == other.value:
  404. return self.name < other.name
  405. return self.value < other.value
  406. class VmLastBackupItem(VMPropertyItem):
  407. def __init__(self, vm, property_name):
  408. super(VmLastBackupItem, self).__init__(vm, property_name)
  409. def update(self):
  410. backup_timestamp = getattr(self.vm, 'backup_timestamp', None)
  411. if backup_timestamp:
  412. self.setText(
  413. str(datetime.datetime.fromtimestamp(backup_timestamp)))
  414. else:
  415. self.setText("")