From 9d1eb1b76639d6fb714e6d9028a78b80228517e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marta=20Marczykowska-G=C3=B3recka?= Date: Tue, 5 May 2020 15:50:41 +0200 Subject: [PATCH] Add vm.icon property This is a property for handling vm icons that change depending on vm type. Depends on QubesOS/qubes-artwork#17 references QubesOS/qubes-issues#5767 --- qubes/tests/vm/__init__.py | 5 ++++- qubes/tests/vm/qubesvm.py | 37 +++++++++++++++++++++++++++++++------ qubes/vm/qubesvm.py | 13 +++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/qubes/tests/vm/__init__.py b/qubes/tests/vm/__init__.py index ffb1f7c1e..9850a4593 100644 --- a/qubes/tests/vm/__init__.py +++ b/qubes/tests/vm/__init__.py @@ -65,7 +65,10 @@ def init_volume(self, *args, **kwargs): return TestVolume(self) class TestApp(qubes.tests.TestEmitter): - labels = {1: qubes.Label(1, '0xcc0000', 'red')} + labels = {1: qubes.Label(1, '0xcc0000', 'red'), + 2: qubes.Label(2, '0x00cc00', 'green'), + 3: qubes.Label(3, '0x0000cc', 'blue'), + 4: qubes.Label(4, '0xcccccc', 'black')} check_updates_vm = False def get_label(self, label): diff --git a/qubes/tests/vm/qubesvm.py b/qubes/tests/vm/qubesvm.py index c46280e61..dee064886 100644 --- a/qubes/tests/vm/qubesvm.py +++ b/qubes/tests/vm/qubesvm.py @@ -47,7 +47,10 @@ import qubes.tests.vm class TestApp(object): - labels = {1: qubes.Label(1, '0xcc0000', 'red')} + labels = {1: qubes.Label(1, '0xcc0000', 'red'), + 2: qubes.Label(2, '0x00cc00', 'green'), + 3: qubes.Label(3, '0x0000cc', 'blue'), + 4: qubes.Label(4, '0xcccccc', 'black')} def __init__(self): self.domains = {} @@ -394,24 +397,46 @@ def test_120_uuid(self): with self.assertRaises(AttributeError): vm.uuid = uuid.uuid4() - @unittest.skip('TODO: how to not fail on making an icon symlink here?') - def test_130_label(self): + @unittest.mock.patch("os.symlink") + def test_130_label(self, _): vm = self.get_vm() self.assertPropertyDefaultValue(vm, 'label') self.assertPropertyValue(vm, 'label', self.app.labels[1], - self.app.labels[1], 'label-1') + self.app.labels[1], 'red') del vm.label self.assertPropertyDefaultValue(vm, 'label') self.assertPropertyValue(vm, 'label', 'red', - self.app.labels[1], 'label-1') + self.app.labels[1], 'red') self.assertPropertyValue(vm, 'label', 'label-1', - self.app.labels[1], 'label-1') + self.app.labels[1], 'red') def test_131_label_invalid(self): vm = self.get_vm() self.assertPropertyInvalidValue(vm, 'label', 'invalid') self.assertPropertyInvalidValue(vm, 'label', 123) + @unittest.mock.patch("os.symlink") + def test_135_icon(self, _): + vm = self.get_vm(cls=qubes.vm.appvm.AppVM) + vm.label = "red" + self.assertEqual(vm.icon, "appvm-red") + + templatevm = self.get_vm(cls=qubes.vm.templatevm.TemplateVM) + templatevm.label = "blue" + self.assertEqual(templatevm.icon, "templatevm-blue") + + vm.template_for_dispvms = True + dispvm = self.get_vm(cls=qubes.vm.dispvm.DispVM, template=vm, + dispid=10) + dispvm.label = "black" + self.assertEqual(dispvm.icon, "dispvm-black") + + vm = self.get_vm() + vm.label = "green" + vm.features["servicevm"] = 1 + self.assertEqual(vm.icon, "servicevm-green") + + def test_160_memory(self): vm = self.get_vm() self.assertPropertyDefaultValue(vm, 'memory', 400) diff --git a/qubes/vm/qubesvm.py b/qubes/vm/qubesvm.py index 47b488f74..60b904347 100644 --- a/qubes/vm/qubesvm.py +++ b/qubes/vm/qubesvm.py @@ -2101,6 +2101,19 @@ def start_time(self): return None + @qubes.stateless_property + def icon(self): + """freedesktop icon name, suitable for use in + :py:meth:`PyQt4.QtGui.QIcon.fromTheme`""" + raw_icon_name = self.label.name + if self.klass == 'TemplateVM': + return 'templatevm-' + raw_icon_name + if self.klass == 'DispVM': + return 'dispvm-' + raw_icon_name + if self.features.get('servicevm', False): + return 'servicevm-' + raw_icon_name + return 'appvm-' + raw_icon_name + @property def kernelopts_common(self): """Kernel options which should be used in addition to *kernelopts*