-
Notifications
You must be signed in to change notification settings - Fork 25
/
list_devices.py
178 lines (140 loc) · 7.01 KB
/
list_devices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
# list_devices.py
# Load and display root and group devices
#
# Copyright (C) 2014 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Vojtech Trefny <vtrefny@redhat.com>
#
# ---------------------------------------------------------------------------- #
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from .i18n import _
# ---------------------------------------------------------------------------- #
class ListDevices(object):
""" List of parent devices
"""
def __init__(self, blivet_gui):
"""
:param blivet_gui: instance of BlivetGUI class
:type blivet_gui: :class:`~.blivetgui.BlivetGUI`
"""
self.blivet_gui = blivet_gui
self.last_iter = None # last selected device from list
self.selected_device = None # currently selected device
self.device_list = self.blivet_gui.builder.get_object("liststore_devices")
self.disks_view = self.blivet_gui.builder.get_object("treeview_devices")
selection = self.disks_view.get_selection()
self.selection_signal = selection.connect("changed", self.on_disk_selection_changed)
def load_devices(self):
self.device_list.clear()
self.load_disks()
self.load_group_devices()
def load_disks(self):
""" Load disks
"""
icon_theme = Gtk.IconTheme.get_default() # pylint: disable=no-value-for-parameter
icon_disk = Gtk.IconTheme.load_icon(icon_theme, "drive-harddisk", 32, 0)
icon_disk_usb = Gtk.IconTheme.load_icon(icon_theme, "drive-removable-media", 32, 0)
disks = self.blivet_gui.client.remote_call("get_disks")
# hide protected and hidden disks in installer mode
# (this will hide USB drive with installation image, disks under FW RAID etc.)
if self.blivet_gui.installer_mode:
for disk in disks:
if disk.protected or disk.format.hidden:
disks.remove(disk)
if disks:
self.device_list.append([None, None, "<b>%s</b>" % _("Disks")])
for disk in disks:
if disk.removable:
self.device_list.append([disk, icon_disk_usb, str(disk.name + "\n<i><small>" +
str(disk.model) + "</small></i>")])
else:
self.device_list.append([disk, icon_disk, str(disk.name + "\n<i><small>" +
str(disk.model) + "</small></i>")])
def load_group_devices(self):
""" Load LVM2 VGs, Btrfs Volumes and MDArrays
"""
gdevices = self.blivet_gui.client.remote_call("get_group_devices")
icon_theme = Gtk.IconTheme.get_default() # pylint: disable=no-value-for-parameter
icon_group = Gtk.IconTheme.load_icon(icon_theme, "folder", 32, 0)
if gdevices["lvm"]:
self.device_list.append([None, None, "<b>%s</b>" % _("LVM")])
for device in gdevices["lvm"]:
self.device_list.append([device, icon_group,
str(device.name + "\n<i><small>%s</small></i>" % _("LVM2 VG"))])
if gdevices["raid"]:
self.device_list.append([None, None, "<b>%s</b>" % _("RAID")])
for device in gdevices["raid"]:
self.device_list.append([device, icon_group,
str(device.name + "\n<i><small>%s</small></i>" % _("MDArray"))])
if gdevices["btrfs"]:
self.device_list.append([None, None, "<b>%s</b>" % _("Btrfs Volumes")])
for device in gdevices["btrfs"]:
self.device_list.append([device, icon_group,
str(device.name + "\n<i><small>%s</small></i>" % _("Btrfs Volume"))])
def update_devices_view(self):
""" Update device view
"""
# remember previously selected device name
selection = self.disks_view.get_selection()
model, treeiter = selection.get_selected()
if treeiter and model:
selected_device = model[treeiter][0].name
else:
selected_device = None
# reload devices
# adding new devices into TreeStore causing "changed" signal being
# emitted, causing pointless reloading partitions views on all existing
# devices -> block the selection_signal for now to avoid this
selection.handler_block(self.selection_signal)
self.load_devices()
selection.handler_unblock(self.selection_signal)
# if the device still exists, select it; else select first device in list
for idx, device in enumerate(self.device_list):
if device[0] and device[0].name == selected_device:
self.disks_view.set_cursor(idx)
break
else:
self.disks_view.set_cursor(1)
def select_device_by_name(self, device_name):
for idx, device in enumerate(self.device_list):
if device[0] and device[0].name == device_name:
self.disks_view.set_cursor(idx)
def on_disk_selection_changed(self, selection):
""" Onselect action for devices
"""
model, treeiter = selection.get_selected()
if treeiter and model:
# 'Disks', 'LVM2 Volume Groups' and 'LVM2 Physical Volumes' are just
# labels. If user select one of these, we need to unselect this and
# select previous choice
if not model[treeiter][0]:
selection.handler_block(self.selection_signal)
selection.unselect_iter(treeiter)
selection.handler_unblock(self.selection_signal)
if not model.iter_is_valid(self.last_iter):
self.last_iter = model.get_iter(1)
selection.select_iter(self.last_iter)
treeiter = self.last_iter
else:
self.last_iter = treeiter
if self.device_list.iter_is_valid(treeiter):
self.selected_device = model[treeiter][0]
self.blivet_gui.update_partitions_view()
self.blivet_gui.update_physical_view()