summaryrefslogtreecommitdiffstats
path: root/iw/partition_ui_helpers_gui.py
blob: 784a855b56b608305f1db37f8273033f4cc05077 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#
# partition_ui_helpers_gui.py: convenience functions for partition_gui.py
#                              and friends.
#
# Michael Fulbright <msf@redhat.com>
#
# Copyright 2001-2002 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import gobject
import gtk
import checklist

from constants import *
from fsset import *
from partitioning import *
from partIntfHelpers import *
from partRequests import *
from partedUtils import *

from rhpl.translate import _, N_

class WideCheckList(checklist.CheckList):
    def toggled_item(self, data, row):

	rc = gtk.TRUE
	if self.clickCB:
	    rc = self.clickCB(data, row)

	if rc == gtk.TRUE:
	    checklist.CheckList.toggled_item(self, data, row)

    
    def __init__(self, columns, store, clickCB=None):
	checklist.CheckList.__init__(self, columns=columns,
				     custom_store = store)

	selection = self.get_selection()
	selection.set_mode(gtk.SELECTION_NONE)

	# make checkbox column wider
	column = self.get_column(0)
	column.set_fixed_width(75)
	column.set_alignment(0.0)

	self.clickCB = clickCB

def createAlignedLabel(text):
    label = gtk.Label(text)
    label.set_alignment(0.0, 0.5)
    label.set_property("use-underline", gtk.TRUE)

    return label

def createMountPointCombo(request, excludeMountPoints=[]):
    mountCombo = gtk.Combo()

    mntptlist = []
    if request.type != REQUEST_NEW and request.fslabel:
	mntptlist.append(request.fslabel)
    
    for p in defaultMountPoints:
	if p in excludeMountPoints:
	    continue
	
	if not p in mntptlist and (p[0] == "/"):
	    mntptlist.append(p)
	
    mountCombo.set_popdown_strings (mntptlist)

    mountpoint = request.mountpoint

    if request.fstype and request.fstype.isMountable():
        mountCombo.set_sensitive(1)
        if mountpoint:
            mountCombo.entry.set_text(mountpoint)
        else:
            mountCombo.entry.set_text("")
    else:
        mountCombo.entry.set_text(_("<Not Applicable>"))
        mountCombo.set_sensitive(0)

    mountCombo.set_data("saved_mntpt", None)

    return mountCombo

def setMntPtComboStateFromType(fstype, mountCombo):
    prevmountable = mountCombo.get_data("prevmountable")
    mountpoint = mountCombo.get_data("saved_mntpt")

    if prevmountable and fstype.isMountable():
        return

    if fstype.isMountable():
        mountCombo.set_sensitive(1)
        if mountpoint != None:
            mountCombo.entry.set_text(mountpoint)
        else:
            mountCombo.entry.set_text("")
    else:
        if mountCombo.entry.get_text() != _("<Not Applicable>"):
            mountCombo.set_data("saved_mntpt", mountCombo.entry.get_text())
        mountCombo.entry.set_text(_("<Not Applicable>"))
        mountCombo.set_sensitive(0)

    mountCombo.set_data("prevmountable", fstype.isMountable())
    
def fstypechangeCB(widget, mountCombo):
    fstype = widget.get_data("type")
    setMntPtComboStateFromType(fstype, mountCombo)

def createAllowedDrivesList(disks, reqdrives):
    store = gtk.TreeStore(gobject.TYPE_BOOLEAN,
			  gobject.TYPE_STRING,
			  gobject.TYPE_STRING,
			  gobject.TYPE_STRING)
    drivelist = WideCheckList(3, store)

    driverow = 0
    drives = disks.keys()
    drives.sort()
    for drive in drives:
        size = getDeviceSizeMB(disks[drive].dev)
	selected = 0
        if reqdrives:
            if drive in reqdrives:
		selected = 1
        else:
	    selected = 1

	sizestr = "%8.0f MB" % size
	drivelist.append_row((drive, sizestr, disks[drive].dev.model),selected)

    if len(drives) < 2:
	drivelist.set_sensitive(0)

    return drivelist

# pass in callback for when fs changes because of python scope issues
def createFSTypeMenu(fstype, fstypechangeCB, mountCombo,
                     availablefstypes = None, ignorefs = None):
    fstypeoption = gtk.OptionMenu()
    fstypeoptionMenu = gtk.Menu()
    types = fileSystemTypeGetTypes()
    if availablefstypes:
        names = availablefstypes
    else:
        names = types.keys()
    if fstype and fstype.isSupported() and fstype.isFormattable():
        default = fstype
    else:
        default = fileSystemTypeGetDefault()
        
    names.sort()
    defindex = None
    i = 0
    for name in names:
        if not fileSystemTypeGet(name).isSupported():
            continue

        if ignorefs and name in ignorefs:
            continue
        
        if fileSystemTypeGet(name).isFormattable():
            item = gtk.MenuItem(name)
            item.set_data("type", types[name])
            # XXX gtk bug, if you don't show then the menu will be larger
            # than the largest menu item
            item.show()
            fstypeoptionMenu.add(item)
            if default and default.getName() == name:
                defindex = i
                defismountable = types[name].isMountable()
            if fstypechangeCB and mountCombo:
                item.connect("activate", fstypechangeCB, mountCombo)
            i = i + 1

    fstypeoption.set_menu(fstypeoptionMenu)

    if defindex:
        fstypeoption.set_history(defindex)

    if mountCombo:
        mountCombo.set_data("prevmountable",
                            fstypeoptionMenu.get_active().get_data("type").isMountable())

    return (fstypeoption, fstypeoptionMenu)

def formatOptionCB(widget, data):
    (menuwidget, menu, mntptcombo, ofstype) = data
    menuwidget.set_sensitive(widget.get_active())

    # inject event for fstype menu
    if widget.get_active():
	fstype = menu.get_active().get_data("type")
	setMntPtComboStateFromType(fstype, mntptcombo)
        menuwidget.grab_focus()
    else:
	setMntPtComboStateFromType(ofstype, mntptcombo)

def noformatCB(widget, badblocks):
    badblocks.set_sensitive(widget.get_active())

def noformatCB2(widget, data):
    (menuwidget, menu, mntptcombo, ofstype) = data
    menuwidget.set_sensitive(not widget.get_active())

    # inject event for fstype menu
    if widget.get_active():
	setMntPtComboStateFromType(ofstype, mntptcombo)


""" createPreExistFSOptionSection: given inputs for a preexisting partition,
    create a section that will provide format and migrate options

    Returns the value of row after packing into the maintable,
    and a dictionary consistenting of:
       noformatrb    - radiobutton for 'leave fs unchanged'
       formatrb      - radiobutton for 'format as new fs'
       fstype        - part of format fstype menu
       fstypeMenu    - part of format fstype menu
       migraterb     - radiobutton for migrate fs
       migfstype     - menu for migrate fs types
       migfstypeMenu - menu for migrate fs types
       badblocks     - toggle button for badblock check
"""
def createPreExistFSOptionSection(origrequest, maintable, row, mountCombo,
                                  showbadblocks=1, ignorefs=[]):
    ofstype = origrequest.fstype

    maintable.attach(gtk.HSeparator(), 0, 2, row, row + 1)
    row = row + 1

    label = gtk.Label(_("How would you like to prepare the filesystem "
		       "on this partition?"))
    label.set_line_wrap(1)
    label.set_alignment(0.0, 0.0)

    maintable.attach(label, 0, 2, row, row + 1)
    row = row + 1

    noformatrb = gtk.RadioButton(label=_("Leave _unchanged "
					 "(preserve data)"))
    noformatrb.set_active(1)
    maintable.attach(noformatrb, 0, 2, row, row + 1)
    row = row + 1

    formatrb = gtk.RadioButton(label=_("_Format partition as:"),
				    group=noformatrb)
    formatrb.set_active(0)
    if origrequest.format:
	formatrb.set_active(1)

    maintable.attach(formatrb, 0, 1, row, row + 1)
    (fstype, fstypeMenu) = createFSTypeMenu(ofstype, fstypechangeCB,
					    mountCombo, ignorefs=ignorefs)
    fstype.set_sensitive(formatrb.get_active())
    maintable.attach(fstype, 1, 2, row, row + 1)
    row = row + 1

    if not formatrb.get_active() and not origrequest.migrate:
	mountCombo.set_data("prevmountable", ofstype.isMountable())

    formatrb.connect("toggled", formatOptionCB,
		     (fstype, fstypeMenu, mountCombo, ofstype))

    noformatrb.connect("toggled", noformatCB2,
		     (fstype, fstypeMenu, mountCombo, origrequest.origfstype))

    if origrequest.origfstype.isMigratable():
	migraterb = gtk.RadioButton(label=_("Mi_grate partition to:"),
				    group=noformatrb)
	migraterb.set_active(0)
	if origrequest.migrate:
	    migraterb.set_active(1)

	migtypes = origrequest.origfstype.getMigratableFSTargets()

	maintable.attach(migraterb, 0, 1, row, row + 1)
	(migfstype, migfstypeMenu)=createFSTypeMenu(ofstype, None, None,
						    availablefstypes = migtypes)
	migfstype.set_sensitive(migraterb.get_active())
	maintable.attach(migfstype, 1, 2, row, row + 1)
	row = row + 1

	migraterb.connect("toggled", formatOptionCB,
			       (migfstype, migfstypeMenu, mountCombo, ofstype))
    else:
	migraterb = None
	migfstype = None
	migfstypeMenu = None

    if showbadblocks:
        badblocks = gtk.CheckButton(_("Check for _bad blocks?"))
        badblocks.set_active(0)
        maintable.attach(badblocks, 0, 1, row, row + 1)
        formatrb.connect("toggled", noformatCB, badblocks)
        if not origrequest.format:
            badblocks.set_sensitive(0)

        if origrequest.badblocks:
            badblocks.set_active(1)

    else:
        badblocks = None
        
    row = row + 1

    rc = {}
    for var in ['noformatrb', 'formatrb', 'fstype', 'fstypeMenu',
	    'migraterb', 'migfstype', 'migfstypeMenu', 'badblocks' ]:
	rc[var] = eval("%s" % (var,))

    return (row, rc)

# do tests we just want in UI for now, not kickstart
def doUIRAIDLVMChecks(request, diskset):
    fstype = request.fstype
    numdrives = len(diskset.disks.keys())
    
##     if fstype and fstype.getName() == "physical volume (LVM)":
## 	if request.grow:
## 	    return (_("Partitions of type '%s' must be of fixed size, and "
## 		     "cannot be marked to fill to use available space.")) % (fstype.getName(),)

    if fstype and fstype.getName() in ["physical volume (LVM)", "software RAID"]:
	if numdrives > 1 and (request.drive is None or len(request.drive) > 1):
	    return (_("Partitions of type '%s' must be constrained to "
		      "a single drive.  This is done by selecting the "
		      "drive in the 'Allowed Drives' checklist.")) % (fstype.getName(),)
    
    return None