table_widgets.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. from PyQt4 import QtGui # pylint: disable=import-error
  22. from PyQt4 import QtCore # pylint: disable=import-error
  23. import datetime
  24. # pylint: disable=too-few-public-methods
  25. power_order = QtCore.Qt.DescendingOrder
  26. update_order = QtCore.Qt.AscendingOrder
  27. row_height = 30
  28. class VmIconWidget(QtGui.QWidget):
  29. def __init__(self, icon_path, enabled=True, size_multiplier=0.7,
  30. tooltip=None, parent=None, icon_sz=(32, 32)):
  31. super(VmIconWidget, self).__init__(parent)
  32. self.label_icon = QtGui.QLabel()
  33. if icon_path[0] in ':/':
  34. icon = QtGui.QIcon(icon_path)
  35. else:
  36. icon = QtGui.QIcon.fromTheme(icon_path)
  37. icon_sz = QtCore.QSize(row_height * size_multiplier,
  38. row_height * size_multiplier)
  39. icon_pixmap = icon.pixmap(
  40. icon_sz,
  41. QtGui.QIcon.Disabled if not enabled else QtGui.QIcon.Normal)
  42. self.label_icon.setPixmap(icon_pixmap)
  43. self.label_icon.setFixedSize(icon_sz)
  44. if tooltip is not None:
  45. self.label_icon.setToolTip(tooltip)
  46. layout = QtGui.QHBoxLayout()
  47. layout.addWidget(self.label_icon)
  48. layout.setContentsMargins(0, 0, 0, 0)
  49. self.setLayout(layout)
  50. def setToolTip(self, tooltip): # pylint: disable=invalid-name
  51. if tooltip is not None:
  52. self.label_icon.setToolTip(tooltip)
  53. else:
  54. self.label_icon.setToolTip('')
  55. class VmTypeWidget(VmIconWidget):
  56. class VmTypeItem(QtGui.QTableWidgetItem):
  57. def __init__(self, value, vm):
  58. super(VmTypeWidget.VmTypeItem, self).__init__()
  59. self.value = value
  60. self.vm = vm
  61. def set_value(self, value):
  62. self.value = value
  63. def __lt__(self, other):
  64. if self.vm.qid == 0:
  65. return True
  66. elif other.vm.qid == 0:
  67. return False
  68. elif self.value == other.value:
  69. return self.vm.name < other.vm.name
  70. return self.value < other.value
  71. def __init__(self, vm, parent=None):
  72. (icon_path, tooltip) = self.get_vm_icon(vm)
  73. super(VmTypeWidget, self).__init__(
  74. icon_path, True, 0.8, tooltip, parent)
  75. self.vm = vm
  76. self.table_item = self.VmTypeItem(self.value, vm)
  77. self.value = None
  78. # TODO: add "provides network" column
  79. def get_vm_icon(self, vm):
  80. if vm.klass == 'AdminVM':
  81. self.value = 0
  82. icon_name = "dom0"
  83. elif vm.klass == 'TemplateVM':
  84. self.value = 3
  85. icon_name = "templatevm"
  86. elif vm.klass == 'StandaloneVM':
  87. self.value = 4
  88. icon_name = "standalonevm"
  89. else:
  90. self.value = 5 + vm.label.index
  91. icon_name = "appvm"
  92. return ":/" + icon_name + ".png", vm.klass
  93. class VmLabelWidget(VmIconWidget):
  94. class VmLabelItem(QtGui.QTableWidgetItem):
  95. def __init__(self, value, vm):
  96. super(VmLabelWidget.VmLabelItem, self).__init__()
  97. self.value = value
  98. self.vm = vm
  99. def set_value(self, value):
  100. self.value = value
  101. def __lt__(self, other):
  102. if self.vm.qid == 0:
  103. return True
  104. elif other.vm.qid == 0:
  105. return False
  106. elif self.value == other.value:
  107. return self.vm.name < other.vm.name
  108. return self.value < other.value
  109. def __init__(self, vm, parent=None):
  110. icon_path = self.get_vm_icon_path(vm)
  111. super(VmLabelWidget, self).__init__(icon_path, True, 0.8, None, parent)
  112. self.vm = vm
  113. self.table_item = self.VmLabelItem(self.value, vm)
  114. self.value = None
  115. def get_vm_icon_path(self, vm):
  116. self.value = vm.label.index
  117. return vm.label.icon
  118. class VmNameItem(QtGui.QTableWidgetItem):
  119. def __init__(self, vm):
  120. super(VmNameItem, self).__init__()
  121. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  122. self.setText(vm.name)
  123. self.setTextAlignment(QtCore.Qt.AlignVCenter)
  124. self.qid = vm.qid
  125. def __lt__(self, other):
  126. if self.qid == 0:
  127. return True
  128. elif other.qid == 0:
  129. return False
  130. return super(VmNameItem, self).__lt__(other)
  131. class VmStatusIcon(QtGui.QLabel):
  132. def __init__(self, vm, parent=None):
  133. super(VmStatusIcon, self).__init__(parent)
  134. self.vm = vm
  135. self.set_on_icon()
  136. self.previous_power_state = self.vm.get_power_state()
  137. def update(self):
  138. if self.previous_power_state != self.vm.get_power_state():
  139. self.set_on_icon()
  140. self.previous_power_state = self.vm.get_power_state()
  141. def set_on_icon(self):
  142. if self.vm.get_power_state() == "Running":
  143. icon = QtGui.QIcon(":/on.png")
  144. elif self.vm.get_power_state() in ["Paused", "Suspended"]:
  145. icon = QtGui.QIcon(":/paused.png")
  146. elif self.vm.get_power_state() in ["Transient", "Halting", "Dying"]:
  147. icon = QtGui.QIcon(":/transient.png")
  148. else:
  149. icon = QtGui.QIcon(":/off.png")
  150. icon_sz = QtCore.QSize(row_height * 0.5, row_height * 0.5)
  151. icon_pixmap = icon.pixmap(icon_sz)
  152. self.setPixmap(icon_pixmap)
  153. self.setFixedSize(icon_sz)
  154. class VmInfoWidget(QtGui.QWidget):
  155. class VmInfoItem(QtGui.QTableWidgetItem):
  156. def __init__(self, upd_info_item, vm):
  157. super(VmInfoWidget.VmInfoItem, self).__init__()
  158. self.upd_info_item = upd_info_item
  159. self.vm = vm
  160. def __lt__(self, other):
  161. # pylint: disable=too-many-return-statements
  162. if self.vm.qid == 0:
  163. return True
  164. elif other.vm.qid == 0:
  165. return False
  166. self_val = self.upd_info_item.value
  167. other_val = other.upd_info_item.value
  168. if self.tableWidget().\
  169. horizontalHeader().sortIndicatorOrder() == update_order:
  170. # the result will be sorted by upd, sorting order: Ascending
  171. self_val += 1 if self.vm.is_running() else 0
  172. other_val += 1 if other.vm.is_running() else 0
  173. if self_val == other_val:
  174. return self.vm.name < other.vm.name
  175. return self_val > other_val
  176. elif self.tableWidget().\
  177. horizontalHeader().sortIndicatorOrder() == power_order:
  178. # the result will be sorted by power state,
  179. # sorting order: Descending
  180. self_val = -(self_val/10 +
  181. 10*(1 if self.vm.is_running() else 0))
  182. other_val = -(other_val/10 +
  183. 10*(1 if other.vm.is_running() else 0))
  184. if self_val == other_val:
  185. return self.vm.name < other.vm.name
  186. return self_val > other_val
  187. else:
  188. # it would be strange if this happened
  189. return
  190. def __init__(self, vm, parent=None):
  191. super(VmInfoWidget, self).__init__(parent)
  192. self.vm = vm
  193. layout = QtGui.QHBoxLayout()
  194. self.on_icon = VmStatusIcon(vm)
  195. self.upd_info = VmUpdateInfoWidget(vm, show_text=False)
  196. self.error_icon = VmIconWidget(":/warning.png")
  197. self.blk_icon = VmIconWidget(":/mount.png")
  198. self.rec_icon = VmIconWidget(":/mic.png")
  199. layout.addWidget(self.on_icon)
  200. layout.addWidget(self.upd_info)
  201. layout.addWidget(self.error_icon)
  202. layout.addItem(QtGui.QSpacerItem(0, 10,
  203. QtGui.QSizePolicy.Expanding,
  204. QtGui.QSizePolicy.Expanding))
  205. layout.addWidget(self.blk_icon)
  206. layout.addWidget(self.rec_icon)
  207. layout.setContentsMargins(5, 0, 5, 0)
  208. self.setLayout(layout)
  209. self.rec_icon.setVisible(False)
  210. self.blk_icon.setVisible(False)
  211. self.error_icon.setVisible(False)
  212. self.table_item = self.VmInfoItem(self.upd_info.table_item, vm)
  213. def update_vm_state(self, vm):
  214. self.on_icon.update()
  215. self.upd_info.update_outdated(vm)
  216. class VmTemplateItem(QtGui.QTableWidgetItem):
  217. def __init__(self, vm):
  218. super(VmTemplateItem, self).__init__()
  219. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  220. self.vm = vm
  221. if getattr(vm, 'template', None) is not None:
  222. self.setText(vm.template.name)
  223. else:
  224. font = QtGui.QFont()
  225. font.setStyle(QtGui.QFont.StyleItalic)
  226. self.setFont(font)
  227. self.setTextColor(QtGui.QColor("gray"))
  228. self.setText(vm.klass)
  229. self.setTextAlignment(QtCore.Qt.AlignVCenter)
  230. def __lt__(self, other):
  231. if self.vm.qid == 0:
  232. return True
  233. elif other.vm.qid == 0:
  234. return False
  235. elif self.text() == other.text():
  236. return self.vm.name < other.vm.name
  237. return super(VmTemplateItem, self).__lt__(other)
  238. class VmNetvmItem(QtGui.QTableWidgetItem):
  239. def __init__(self, vm):
  240. super(VmNetvmItem, self).__init__()
  241. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  242. self.vm = vm
  243. if getattr(vm, 'netvm', None) is None:
  244. self.setText("n/a")
  245. else:
  246. self.setText(vm.netvm.name)
  247. self.setTextAlignment(QtCore.Qt.AlignVCenter)
  248. def __lt__(self, other):
  249. if self.vm.qid == 0:
  250. return True
  251. elif other.vm.qid == 0:
  252. return False
  253. elif self.text() == other.text():
  254. return self.vm.name < other.vm.name
  255. return super(VmNetvmItem, self).__lt__(other)
  256. class VmInternalItem(QtGui.QTableWidgetItem):
  257. def __init__(self, vm):
  258. super(VmInternalItem, self).__init__()
  259. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  260. self.vm = vm
  261. self.internal = vm.features.get('internal', False)
  262. self.setText("Yes" if self.internal else "")
  263. def __lt__(self, other):
  264. if self.vm.qid == 0:
  265. return True
  266. elif other.vm.qid == 0:
  267. return False
  268. return super(VmInternalItem, self).__lt__(other)
  269. # features man qvm-features
  270. class VmUpdateInfoWidget(QtGui.QWidget):
  271. class VmUpdateInfoItem(QtGui.QTableWidgetItem):
  272. def __init__(self, value, vm):
  273. super(VmUpdateInfoWidget.VmUpdateInfoItem, self).__init__()
  274. self.value = 0
  275. self.vm = vm
  276. self.set_value(value)
  277. def set_value(self, value):
  278. if value in ("outdated", "to-be-outdated"):
  279. self.value = 30
  280. elif value == "update":
  281. self.value = 20
  282. else:
  283. self.value = 0
  284. def __lt__(self, other):
  285. if self.vm.qid == 0:
  286. return True
  287. elif other.vm.qid == 0:
  288. return False
  289. elif self.value == other.value:
  290. return self.vm.name < other.vm.name
  291. return self.value < other.value
  292. def __init__(self, vm, show_text=True, parent=None):
  293. super(VmUpdateInfoWidget, self).__init__(parent)
  294. layout = QtGui.QHBoxLayout()
  295. self.show_text = show_text
  296. if self.show_text:
  297. self.label = QtGui.QLabel("")
  298. layout.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
  299. else:
  300. self.icon = QtGui.QLabel("")
  301. layout.addWidget(self.icon, alignment=QtCore.Qt.AlignCenter)
  302. self.setLayout(layout)
  303. self.previous_outdated_state = None
  304. self.previous_update_recommended = None
  305. self.value = None
  306. self.table_item = VmUpdateInfoWidget.VmUpdateInfoItem(self.value, vm)
  307. self.update_outdated(vm)
  308. def update_outdated(self, vm):
  309. outdated_state = False
  310. for vol in vm.volumes.values():
  311. if vol.is_outdated():
  312. outdated_state = "outdated"
  313. break
  314. if not outdated_state and getattr(vm, 'template', None)\
  315. and vm.template.is_running():
  316. outdated_state = "to-be-outdated"
  317. if outdated_state != self.previous_outdated_state:
  318. self.update_status_widget(outdated_state)
  319. self.previous_outdated_state = outdated_state
  320. updates_available = vm.features.get('updates-available', False)
  321. if updates_available != self.previous_update_recommended:
  322. self.update_status_widget("update" if updates_available else None)
  323. self.previous_update_recommended = updates_available
  324. def update_status_widget(self, state):
  325. self.value = state
  326. self.table_item.set_value(state)
  327. if state == "update":
  328. label_text = "<font color=\"#CCCC00\">Check updates</font>"
  329. icon_path = ":/update-recommended.png"
  330. tooltip_text = self.tr("Updates pending!")
  331. elif state == "outdated":
  332. label_text = "<font color=\"red\">Qube outdated</font>"
  333. icon_path = ":/outdated.png"
  334. tooltip_text = self.tr(
  335. "The qube must be restarted for its filesystem to reflect the "
  336. "template's recent committed changes.")
  337. elif state == "to-be-outdated":
  338. label_text = "<font color=\"#800000\">Template running</font>"
  339. icon_path = ":/to-be-outdated.png"
  340. tooltip_text = self.tr(
  341. "The Template must be stopped before changes from its "
  342. "current session can be picked up by this qube.")
  343. else:
  344. label_text = ""
  345. icon_path = None
  346. tooltip_text = None
  347. if self.show_text:
  348. self.label.setText(label_text)
  349. else:
  350. self.layout().removeWidget(self.icon)
  351. self.icon.deleteLater()
  352. if icon_path is not None:
  353. self.icon = VmIconWidget(icon_path, True, 0.7)
  354. self.icon.setToolTip(tooltip_text)
  355. else:
  356. self.icon = QtGui.QLabel(label_text)
  357. self.layout().addWidget(self.icon, alignment=QtCore.Qt.AlignCenter)
  358. class VmSizeOnDiskItem(QtGui.QTableWidgetItem):
  359. def __init__(self, vm):
  360. super(VmSizeOnDiskItem, self).__init__()
  361. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  362. self.vm = vm
  363. self.value = 0
  364. self.update()
  365. self.setTextAlignment(QtCore.Qt.AlignVCenter)
  366. def update(self):
  367. if self.vm.qid == 0:
  368. self.setText("n/a")
  369. else:
  370. self.value = 10
  371. self.value = round(self.vm.get_disk_utilization()/(1024*1024), 2)
  372. self.setText(str(self.value) + " MiB")
  373. def __lt__(self, other):
  374. if self.vm.qid == 0:
  375. return True
  376. elif other.vm.qid == 0:
  377. return False
  378. elif self.value == other.value:
  379. return self.vm.name < other.vm.name
  380. return self.value < other.value
  381. class VmIPItem(QtGui.QTableWidgetItem):
  382. def __init__(self, vm):
  383. super(VmIPItem, self).__init__()
  384. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  385. self.vm = vm
  386. self.ip = getattr(self.vm, 'ip', None)
  387. self.setText(self.ip if self.ip is not None else 'n/a')
  388. def __lt__(self, other):
  389. if self.vm.qid == 0:
  390. return True
  391. elif other.vm.qid == 0:
  392. return False
  393. return super(VmIPItem, self).__lt__(other)
  394. class VmIncludeInBackupsItem(QtGui.QTableWidgetItem):
  395. def __init__(self, vm):
  396. super(VmIncludeInBackupsItem, self).__init__()
  397. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  398. self.vm = vm
  399. if getattr(self.vm, 'include_in_backups', None):
  400. self.setText("Yes")
  401. else:
  402. self.setText("")
  403. def __lt__(self, other):
  404. if self.vm.qid == 0:
  405. return True
  406. elif other.vm.qid == 0:
  407. return False
  408. elif self.vm.include_in_backups == other.vm.include_in_backups:
  409. return self.vm.name < other.vm.name
  410. return self.vm.include_in_backups < other.vm.include_in_backups
  411. class VmLastBackupItem(QtGui.QTableWidgetItem):
  412. def __init__(self, vm):
  413. super(VmLastBackupItem, self).__init__()
  414. self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  415. self.vm = vm
  416. self.backup_timestamp = getattr(self.vm, 'backup_timestamp', None)
  417. if self.backup_timestamp:
  418. self.setText(
  419. str(datetime.datetime.fromtimestamp(self.backup_timestamp)))
  420. else:
  421. self.setText("")
  422. def __lt__(self, other):
  423. if self.vm.qid == 0:
  424. return True
  425. elif other.vm.qid == 0:
  426. return False
  427. elif self.backup_timestamp == other.backup_timestamp:
  428. return self.vm.name < other.vm.name
  429. elif not self.backup_timestamp:
  430. return False
  431. elif not other.backup_timestamp:
  432. return True
  433. return self.backup_timestamp < other.backup_timestamp