summaryrefslogtreecommitdiffstats
path: root/iw/partition_ui_helpers_gui.py
blob: 46e2efb56813c89e95359ad517bca1fb7811f236 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#
# partition_ui_helpers_gui.py: convenience functions for partition_gui.py
#                              and friends.
#
# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007  Red Hat, Inc.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty 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, see <http://www.gnu.org/licenses/>.
#
# Author(s): Michael Fulbright <msf@redhat.com>
#

import gobject
import gtk
import checklist
import datacombo
import iutil

from constants import *
from partIntfHelpers import *
from storage.formats import *

import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)

FLAG_FORMAT = 1
FLAG_MIGRATE = 2

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

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

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

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

        # make checkbox column wider
        column = self.get_column(columns)
        self.set_expander_column(column)
        column = self.get_column(0)
        column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
        column.set_fixed_width(25)

	self.clickCB = clickCB

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

    return label

defaultMountPoints = ['/', '/boot', '/home', '/tmp', '/usr',
                      '/var', '/usr/local', '/opt']

if iutil.isS390():
    # Many s390 have 2G DASDs, we recomment putting /usr/share on its own DASD
    defaultMountPoints.insert(5, '/usr/share')

if iutil.isEfi():
    defaultMountPoints.insert(2, '/boot/efi')

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

    mntptlist = []
    label = getattr(request.format, "label", None)
    if request.exists and label and label.startswith("/"):
        mntptlist.append(label)
        idx = 0

    for p in defaultMountPoints:
        if p in excludeMountPoints:
            continue

        if not p in mntptlist and (p[0] == "/"):
            mntptlist.append(p)

    map(mountCombo.append_text, mntptlist)

    if (request.format.type or request.format.migrate) and \
       request.format.mountable:
        mountpoint = request.format.mountpoint
        mountCombo.set_sensitive(1)
        if mountpoint:
            mountCombo.get_children()[0].set_text(mountpoint)
        else:
            mountCombo.get_children()[0].set_text("")
    else:
        mountCombo.get_children()[0].set_text(_("<Not Applicable>"))
        mountCombo.set_sensitive(0)

    mountCombo.set_data("saved_mntpt", None)

    return mountCombo

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

    format = fmt_class()
    if prevmountable and format.mountable:
        return

    if format.mountable:
        mountCombo.set_sensitive(1)
        if mountpoint != None:
            mountCombo.get_children()[0].set_text(mountpoint)
        else:
            mountCombo.get_children()[0].set_text("")
    else:
        if mountCombo.get_children()[0].get_text() != _("<Not Applicable>"):
            mountCombo.set_data("saved_mntpt", mountCombo.get_children()[0].get_text())
        mountCombo.get_children()[0].set_text(_("<Not Applicable>"))
        mountCombo.set_sensitive(0)

    mountCombo.set_data("prevmountable", format.mountable)

def fstypechangeCB(widget, mountCombo):
    fstype = widget.get_active_value()
    setMntPtComboStateFromType(fstype, mountCombo)

def createAllowedDrivesStore(disks, reqdrives, drivelist, selectDrives=True,
                             disallowDrives=[]):
    drivelist.clear()
    for disk in disks:
        selected = 0

        if selectDrives:
            if reqdrives:
                if disk.name in reqdrives:
                    selected = 1
            else:
                if disk.name not in disallowDrives:
                    selected = 1

        sizestr = "%8.0f MB" % disk.size
        drivelist.append_row((disk.name,
                              sizestr,
                              disk.description),
                             selected)

    if len(disks) < 2:
        drivelist.set_sensitive(False)
    else:
        drivelist.set_sensitive(True)

def createAllowedDrivesList(disks, reqdrives, selectDrives=True, disallowDrives=[]):
    store = gtk.TreeStore(gobject.TYPE_BOOLEAN,
                          gobject.TYPE_STRING,
                          gobject.TYPE_STRING,
                          gobject.TYPE_STRING,
                          gobject.TYPE_BOOLEAN)
    drivelist = WideCheckList(3, store, sensitivity=True)
    createAllowedDrivesStore(disks, reqdrives, drivelist, selectDrives=selectDrives,
                             disallowDrives=disallowDrives)

    return drivelist
    
    

# pass in callback for when fs changes because of python scope issues
def createFSTypeMenu(format, fstypechangeCB, mountCombo,
                     availablefstypes = None, ignorefs = None):
    store = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
    fstypecombo = datacombo.DataComboBox(store)
    
    if availablefstypes:
        names = availablefstypes
    else:
        names = device_formats.keys()
    if format and format.supported and format.formattable:
        default = format.type
    else:
        default = get_default_filesystem_type()
        
    names.sort()
    defindex = 0
    i = 0
    for name in names:
        # we could avoid instantiating them all if we made a static class
        # method that does what the supported property does
        format = device_formats[name]()
        if not format.supported:
            continue

        if ignorefs and name in ignorefs:
            continue
        
        if format.formattable:
            fstypecombo.append(format.name, device_formats[name])
            if default == name:
                defindex = i
                defismountable = format.mountable
            i = i + 1

    fstypecombo.set_active(defindex)

    if fstypechangeCB and mountCombo:
        fstypecombo.connect("changed", fstypechangeCB, mountCombo)

    if mountCombo:
        mountCombo.set_data("prevmountable",
                            fstypecombo.get_active_value()().mountable)
        mountCombo.connect("changed", mountptchangeCB, fstypecombo)

    return fstypecombo

def mountptchangeCB(widget, fstypecombo):
    if iutil.isEfi() and widget.get_children()[0].get_text() == "/boot/efi":
        fstypecombo.set_active_text(getFormat("efi").name)
    if widget.get_children()[0].get_text() == "/boot":
        fstypecombo.set_active_text(get_default_filesystem_type(boot=True))

def resizeOptionCB(widget, resizesb):
    resizesb.set_sensitive(widget.get_active())

def formatOptionResizeCB(widget, data):
    (resizesb, fmt) = data

    if widget.get_active():
        lower = 1
    else:
        lower = resizesb.get_data("reqlower")

    adj = resizesb.get_adjustment()
    adj.lower = lower
    resizesb.set_adjustment(adj)

    if resizesb.get_value_as_int() < lower:
        resizesb.set_value(adj.lower)

def formatMigrateOptionCB(widget, data):
    (sensitive,) = widget.get_properties('sensitive')
    if not sensitive:
        return

    (combowidget, mntptcombo, fs, lukscb, othercombo, othercb, flag) = data
    combowidget.set_sensitive(widget.get_active())

    if othercb is not None:
        othercb.set_sensitive(not widget.get_active())
        othercb.set_active(False)

        if othercombo is not None:
            othercombo.set_sensitive(othercb.get_active())

    if lukscb is not None:
        lukscb.set_data("formatstate", widget.get_active())
        if not widget.get_active():
            # set "Encrypt" checkbutton to match partition's initial state
            lukscb.set_active(lukscb.get_data("encrypted"))
            lukscb.set_sensitive(False)
        else:
            lukscb.set_sensitive(True)

    # inject event for fstype menu
    if widget.get_active():
        fstype = combowidget.get_active_value()
        setMntPtComboStateFromType(fstype, mntptcombo)
        combowidget.grab_focus()
    else:
        if isinstance(fs, type(fs)):
            fs = type(fs)

        setMntPtComboStateFromType(fs, mntptcombo)


def createPreExistFSOptionSection(origrequest, maintable, row, mountCombo,
                                  partitions, ignorefs=[], luksdev=None):
    """ 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:
           formatcb      - checkbutton for 'format as new fs'
           fstype        - part of format fstype menu
           fstypeMenu    - part of format fstype menu
           migratecb     - checkbutton for migrate fs
           migfstypeMenu - menu for migrate fs types
           lukscb        - checkbutton for 'encrypt using LUKS/dm-crypt'
           resizecb      - checkbutton for 'resize fs'
           resizesb      - spinbutton with resize target
    """
    rc = {}

    if luksdev:
        origfs = luksdev.format
    else:
        origfs = origrequest.format

    if origfs.formattable or not origfs.type:
        formatcb = gtk.CheckButton(label=_("_Format as:"))
        maintable.attach(formatcb, 0, 1, row, row + 1)
        formatcb.set_active(origfs.formattable and not origfs.exists)
        rc["formatcb"] = formatcb

        fstypeCombo = createFSTypeMenu(origfs, fstypechangeCB,
                                       mountCombo, ignorefs=ignorefs)
        fstypeCombo.set_sensitive(formatcb.get_active())
        maintable.attach(fstypeCombo, 1, 2, row, row + 1)
        row += 1
        rc["fstypeCombo"] = fstypeCombo
    else:
        formatcb = None
        fstypeCombo = None

    if formatcb and not formatcb.get_active() and not origfs.migrate:
        mountCombo.set_data("prevmountable", origfs.mountable)

    # this gets added to the table a bit later on
    lukscb = gtk.CheckButton(_("_Encrypt"))

    if origfs.migratable and origfs.exists:
        migratecb = gtk.CheckButton(label=_("Mi_grate filesystem to:"))
        if formatcb is not None:
            migratecb.set_active(origfs.migrate and (not formatcb.get_active()))
        else:
            migratecb.set_active(origfs.migrate)

        migtypes = [origfs.migrationTarget]

        maintable.attach(migratecb, 0, 1, row, row + 1)
        migfstypeCombo = createFSTypeMenu(origfs,
                                          None, None,
                                          availablefstypes = migtypes)
        migfstypeCombo.set_sensitive(migratecb.get_active())
        maintable.attach(migfstypeCombo, 1, 2, row, row + 1)
        row = row + 1
        rc["migratecb"] = migratecb
        rc["migfstypeCombo"] = migfstypeCombo
        migratecb.connect("toggled", formatMigrateOptionCB,
                          (migfstypeCombo, mountCombo, origfs, None,
                           fstypeCombo, formatcb, FLAG_MIGRATE))
    else:
        migratecb = None
        migfstypeCombo = None

    if formatcb:
        formatcb.connect("toggled", formatMigrateOptionCB,
                         (fstypeCombo, mountCombo, origfs, lukscb,
                          migfstypeCombo, migratecb, FLAG_FORMAT))

    if origrequest.resizable and origfs.exists:
        resizecb = gtk.CheckButton(label=_("_Resize"))
        resizecb.set_active(origfs.resizable and \
                            (origfs.currentSize != origfs.targetSize) and \
                            (origfs.currentSize != 0))
        rc["resizecb"] = resizecb
        maintable.attach(resizecb, 0, 1, row, row + 1)

        if origrequest.targetSize is not None:
            value = origrequest.targetSize
        else:
            value = origrequest.size

        reqlower = 1
        requpper = origrequest.maxSize

        if origfs.exists:
            reqlower = origrequest.minSize

            if origrequest.type == "partition":
                geomsize = origrequest.partedPartition.geometry.getSize(unit="MB")
                if (geomsize != 0) and (requpper > geomsize):
                    requpper = geomsize

        adj = gtk.Adjustment(value = value, lower = reqlower,
                             upper = requpper, step_incr = 1)
        resizesb = gtk.SpinButton(adj, digits = 0)
        resizesb.set_property('numeric', True)
        resizesb.set_data("requpper", requpper)
        resizesb.set_data("reqlower", reqlower)
        rc["resizesb"] = resizesb
        maintable.attach(resizesb, 1, 2, row, row + 1)
        resizecb.connect('toggled', resizeOptionCB, resizesb)
        resizeOptionCB(resizecb, resizesb)
        row = row + 1

        if formatcb:
            formatcb.connect("toggled", formatOptionResizeCB, (resizesb, origfs))

    if luksdev:
        lukscb.set_active(1)
        lukscb.set_data("encrypted", 1)
    else:
        lukscb.set_data("encrypted", 0)

    if formatcb:
        lukscb.set_sensitive(formatcb.get_active())
        lukscb.set_data("formatstate", formatcb.get_active())
    else:
        lukscb.set_sensitive(0)
        lukscb.set_data("formatstate", 0)

    rc["lukscb"] = lukscb
    maintable.attach(lukscb, 0, 2, row, row + 1)
    row = row + 1

    return (row, rc)

# do tests we just want in UI for now, not kickstart
def doUIRAIDLVMChecks(request, storage):
    fstype = request.format.name
    numdrives = len(storage.partitioned)
    
##     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 in ["physical volume (LVM)", "software RAID"]:
	if numdrives > 1 and (not request.req_disks or len(request.req_disks) > 1):
	    return (_("Partitions of type '%s' must be constrained to "
		      "a single drive.  To do this, select the "
		      "drive in the 'Allowable Drives' checklist.")) % (fstype.getName(),)
    
    return None