2008-08-15 19:24:24 +04:00
#
# Copyright (C) 2008 Red Hat, Inc.
# Copyright (C) 2008 Cole Robinson <crobinso@redhat.com>
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
import gobject
2010-11-30 23:01:02 +03:00
import gtk
2008-08-15 19:24:24 +04:00
import logging
2009-03-09 08:04:29 +03:00
from virtManager import util
2010-12-09 01:26:19 +03:00
from virtManager . baseclass import vmmGObjectUI
2008-08-15 19:24:24 +04:00
from virtManager . asyncjob import vmmAsyncJob
from virtManager . createmeter import vmmCreateMeter
from virtinst import Storage
2008-09-08 03:08:13 +04:00
DEFAULT_ALLOC = 0
DEFAULT_CAP = 1000
2008-08-15 19:24:24 +04:00
2010-12-09 01:26:19 +03:00
class vmmCreateVolume ( vmmGObjectUI ) :
2008-08-15 19:24:24 +04:00
__gsignals__ = {
" vol-created " : ( gobject . SIGNAL_RUN_FIRST , gobject . TYPE_NONE , [ ] )
}
2010-12-09 01:26:19 +03:00
def __init__ ( self , conn , parent_pool ) :
vmmGObjectUI . __init__ ( self , " vmm-create-vol.glade " , " vmm-create-vol " )
2008-08-15 19:24:24 +04:00
self . conn = conn
self . parent_pool = parent_pool
2009-11-15 23:57:38 +03:00
self . name_hint = None
2008-08-15 19:24:24 +04:00
self . vol = None
self . vol_class = Storage . StoragePool . get_volume_for_pool ( parent_pool . get_type ( ) )
self . window . signal_autoconnect ( {
2008-09-06 21:20:35 +04:00
" on_vmm_create_vol_delete_event " : self . close ,
2008-08-15 19:24:24 +04:00
" on_vol_cancel_clicked " : self . close ,
" on_vol_create_clicked " : self . finish ,
2009-10-05 19:34:39 +04:00
" on_vol_name_changed " : self . vol_name_changed ,
2009-10-05 19:50:10 +04:00
" on_vol_allocation_value_changed " : self . vol_allocation_changed ,
" on_vol_capacity_value_changed " : self . vol_capacity_changed ,
2008-08-15 19:24:24 +04:00
} )
2009-12-15 00:45:55 +03:00
util . bind_escape_key_close ( self )
2008-08-15 19:24:24 +04:00
format_list = self . window . get_widget ( " vol-format " )
format_model = gtk . ListStore ( str , str )
format_list . set_model ( format_model )
text2 = gtk . CellRendererText ( )
format_list . pack_start ( text2 , False )
format_list . add_attribute ( text2 , ' text ' , 1 )
self . window . get_widget ( " vol-info-view " ) . modify_bg ( gtk . STATE_NORMAL ,
gtk . gdk . color_parse ( " grey " ) )
2009-09-29 22:25:37 +04:00
# XXX: Help docs useless/out of date
self . window . get_widget ( " pool-help " ) . hide ( )
2009-11-17 23:06:15 +03:00
finish_img = gtk . image_new_from_stock ( gtk . STOCK_QUIT ,
gtk . ICON_SIZE_BUTTON )
self . window . get_widget ( " vol-create " ) . set_image ( finish_img )
2009-09-29 22:25:37 +04:00
2008-08-15 19:24:24 +04:00
self . reset_state ( )
def show ( self ) :
2009-03-08 22:16:47 +03:00
self . reset_state ( )
2008-08-15 19:24:24 +04:00
self . topwin . present ( )
def close ( self , ignore1 = None , ignore2 = None ) :
self . topwin . hide ( )
2009-03-08 22:16:47 +03:00
self . set_modal ( False )
2008-08-15 19:24:24 +04:00
return 1
2009-11-15 23:57:38 +03:00
def set_name_hint ( self , hint ) :
self . name_hint = hint
2009-03-08 22:16:47 +03:00
def set_modal ( self , modal ) :
self . topwin . set_modal ( bool ( modal ) )
2008-08-15 19:24:24 +04:00
def set_parent_pool ( self , pool ) :
self . parent_pool = pool
self . vol_class = Storage . StoragePool . get_volume_for_pool ( self . parent_pool . get_type ( ) )
2009-11-15 23:57:38 +03:00
def default_vol_name ( self ) :
if not self . name_hint :
return " "
suffix = self . default_suffix ( )
2010-02-22 16:49:21 +03:00
ret = " "
2009-11-15 23:57:38 +03:00
try :
2010-02-22 16:49:21 +03:00
ret = Storage . StorageVolume . find_free_name ( self . name_hint ,
2009-11-15 23:57:38 +03:00
pool_object = self . parent_pool . pool ,
suffix = suffix )
2010-02-22 16:49:21 +03:00
ret = ret . rstrip ( suffix )
2009-11-15 23:57:38 +03:00
except :
2010-02-22 16:49:21 +03:00
pass
return ret
2009-11-15 23:57:38 +03:00
def default_suffix ( self ) :
suffix = " "
if self . vol_class == Storage . FileVolume :
suffix = " .img "
return suffix
2008-08-15 19:24:24 +04:00
def reset_state ( self ) :
2010-02-22 16:49:21 +03:00
default_name = self . default_vol_name ( )
self . window . get_widget ( " vol-name " ) . set_text ( " " )
2009-10-05 19:34:39 +04:00
self . window . get_widget ( " vol-create " ) . set_sensitive ( False )
2010-02-22 16:49:21 +03:00
if default_name :
self . window . get_widget ( " vol-name " ) . set_text ( default_name )
self . window . get_widget ( " vol-name " ) . grab_focus ( )
2008-08-15 19:24:24 +04:00
self . populate_vol_format ( )
2008-09-06 21:39:21 +04:00
self . populate_vol_suffix ( )
2008-08-15 19:24:24 +04:00
if len ( self . vol_class . formats ) :
self . window . get_widget ( " vol-format " ) . set_sensitive ( True )
self . window . get_widget ( " vol-format " ) . set_active ( 0 )
else :
self . window . get_widget ( " vol-format " ) . set_sensitive ( False )
self . window . get_widget ( " vol-allocation " ) . set_range ( 0 , int ( self . parent_pool . get_available ( ) / 1024 / 1024 ) )
self . window . get_widget ( " vol-allocation " ) . set_value ( DEFAULT_ALLOC )
self . window . get_widget ( " vol-capacity " ) . set_range ( 1 , int ( self . parent_pool . get_available ( ) / 1024 / 1024 ) )
self . window . get_widget ( " vol-capacity " ) . set_value ( DEFAULT_CAP )
self . window . get_widget ( " vol-parent-name " ) . set_markup ( " <b> " + self . parent_pool . get_name ( ) + " ' s</b> " )
self . window . get_widget ( " vol-parent-space " ) . set_text ( self . parent_pool . get_pretty_available ( ) )
def get_config_format ( self ) :
format_combo = self . window . get_widget ( " vol-format " )
model = format_combo . get_model ( )
if format_combo . get_active_iter ( ) != None :
model = format_combo . get_model ( )
return model . get_value ( format_combo . get_active_iter ( ) , 0 )
return None
def populate_vol_format ( self ) :
model = self . window . get_widget ( " vol-format " ) . get_model ( )
model . clear ( )
2011-03-08 21:57:56 +03:00
2008-08-15 19:24:24 +04:00
formats = self . vol_class . formats
2011-03-08 21:57:56 +03:00
if hasattr ( self . vol_class , " create_formats " ) :
formats = getattr ( self . vol_class , " create_formats " )
2008-08-15 19:24:24 +04:00
for f in formats :
model . append ( [ f , f ] )
2008-09-06 21:39:21 +04:00
def populate_vol_suffix ( self ) :
2009-11-15 23:57:38 +03:00
suffix = self . default_suffix ( )
2008-09-06 21:39:21 +04:00
if self . vol_class == Storage . FileVolume :
suffix = " .img "
self . window . get_widget ( " vol-name-suffix " ) . set_text ( suffix )
2009-10-05 19:34:39 +04:00
def vol_name_changed ( self , src ) :
text = src . get_text ( )
self . window . get_widget ( " vol-create " ) . set_sensitive ( bool ( text ) )
2009-10-05 19:50:10 +04:00
def vol_allocation_changed ( self , src ) :
cap_widget = self . window . get_widget ( " vol-capacity " )
alloc = src . get_value ( )
cap = cap_widget . get_value ( )
if alloc > cap :
cap_widget . set_value ( alloc )
def vol_capacity_changed ( self , src ) :
alloc_widget = self . window . get_widget ( " vol-allocation " )
cap = src . get_value ( )
alloc = self . window . get_widget ( " vol-allocation " ) . get_value ( )
if cap < alloc :
alloc_widget . set_value ( cap )
2010-12-09 19:22:35 +03:00
def finish ( self , src_ignore ) :
2008-08-15 19:24:24 +04:00
try :
if not self . validate ( ) :
return
except Exception , e :
2011-04-06 19:22:03 +04:00
self . show_err ( _ ( " Uncaught error validating input: %s " ) % str ( e ) )
2008-08-15 19:24:24 +04:00
return
logging . debug ( " Creating volume with xml: \n %s " %
self . vol . get_xml_config ( ) )
self . topwin . set_sensitive ( False )
self . topwin . window . set_cursor ( gtk . gdk . Cursor ( gtk . gdk . WATCH ) )
2010-12-09 20:37:48 +03:00
progWin = vmmAsyncJob ( self . _async_vol_create , [ ] ,
2010-12-10 17:57:42 +03:00
_ ( " Creating storage volume... " ) ,
_ ( " Creating the storage volume may take a "
" while... " ) )
error , details = progWin . run ( )
2008-08-15 19:24:24 +04:00
self . topwin . set_sensitive ( True )
self . topwin . window . set_cursor ( gtk . gdk . Cursor ( gtk . gdk . TOP_LEFT_ARROW ) )
2009-04-03 22:15:15 +04:00
2010-12-10 17:57:42 +03:00
if error :
error = _ ( " Error creating vol: %s " ) % error
2011-04-06 19:22:03 +04:00
self . show_err ( error ,
details = ( error + " \n " + details ) )
2010-12-10 17:57:42 +03:00
else :
2009-04-03 22:15:15 +04:00
self . emit ( " vol-created " )
self . close ( )
2008-08-15 19:24:24 +04:00
def _async_vol_create ( self , asyncjob ) :
2010-12-10 17:57:42 +03:00
newconn = util . dup_conn ( self . conn ) . vmm
2008-08-15 19:24:24 +04:00
2010-12-10 17:57:42 +03:00
# Lookup different pool obj
newpool = newconn . storagePoolLookupByName ( self . parent_pool . get_name ( ) )
self . vol . pool = newpool
2008-08-15 19:24:24 +04:00
2010-12-10 17:57:42 +03:00
meter = vmmCreateMeter ( asyncjob )
logging . debug ( " Starting backround vol creation. " )
self . vol . install ( meter = meter )
2008-08-15 19:24:24 +04:00
def validate ( self ) :
name = self . window . get_widget ( " vol-name " ) . get_text ( )
2008-09-06 21:39:21 +04:00
suffix = self . window . get_widget ( " vol-name-suffix " ) . get_text ( )
volname = name + suffix
2009-05-11 18:13:32 +04:00
fmt = self . get_config_format ( )
2008-08-15 19:24:24 +04:00
alloc = self . window . get_widget ( " vol-allocation " ) . get_value ( )
cap = self . window . get_widget ( " vol-capacity " ) . get_value ( )
try :
2008-09-06 21:39:21 +04:00
self . vol = self . vol_class ( name = volname ,
2008-08-15 19:24:24 +04:00
allocation = ( alloc * 1024 * 1024 ) ,
capacity = ( cap * 1024 * 1024 ) ,
pool = self . parent_pool . pool )
2009-05-11 18:13:32 +04:00
if fmt :
self . vol . format = fmt
2008-08-15 19:24:24 +04:00
except ValueError , e :
2010-02-22 17:04:48 +03:00
return self . val_err ( _ ( " Volume Parameter Error " ) , str ( e ) )
2008-08-15 19:24:24 +04:00
return True
2011-04-06 19:22:03 +04:00
def show_err ( self , info , details = None ) :
2009-03-08 22:16:47 +03:00
async = not self . topwin . get_modal ( )
self . err . show_err ( info , details , async = async )
2010-02-22 17:04:48 +03:00
def val_err ( self , info , details ) :
modal = self . topwin . get_modal ( )
ret = False
try :
self . topwin . set_modal ( False )
ret = self . err . val_err ( info , details , async = not modal )
finally :
self . topwin . set_modal ( modal )
return ret
2010-12-09 01:26:19 +03:00
vmmGObjectUI . type_register ( vmmCreateVolume )