2010-12-09 01:26:19 +03:00
#
# Copyright (C) 2010 Red Hat, Inc.
# Copyright (C) 2010 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 os
2011-04-11 19:00:57 +04:00
import sys
import logging
2010-12-09 01:26:19 +03:00
import gtk
import gobject
import virtManager . config
class vmmGObject ( gobject . GObject ) :
@staticmethod
def type_register ( * args , * * kwargs ) :
gobject . type_register ( * args , * * kwargs )
def __init__ ( self ) :
gobject . GObject . __init__ ( self )
self . config = virtManager . config . running_config
2011-04-11 19:00:57 +04:00
self . _gobject_handles = [ ]
self . _gobject_timeouts = [ ]
self . _gconf_handles = [ ]
2011-04-13 17:27:02 +04:00
self . object_key = str ( self )
2011-04-16 02:38:29 +04:00
# Config might not be available if we error early in startup
if self . config :
self . config . add_object ( self . object_key )
2011-04-11 20:54:47 +04:00
2011-04-11 19:00:57 +04:00
def cleanup ( self ) :
# Do any cleanup required to drop reference counts so object is
# actually reaped by python. Usually means unregistering callbacks
try :
for h in self . _gconf_handles [ : ] :
self . remove_gconf_handle ( h )
for h in self . _gobject_handles [ : ] :
2011-04-11 21:06:59 +04:00
self . disconnect ( h )
2011-04-11 19:00:57 +04:00
for h in self . _gobject_timeouts [ : ] :
self . remove_gobject_timeout ( h )
except :
logging . exception ( " Error cleaning up %s " % self )
2011-04-11 21:06:59 +04:00
def connect ( self , name , callback , * args ) :
ret = gobject . GObject . connect ( self , name , callback , * args )
self . _gobject_handles . append ( ret )
return ret
def disconnect ( self , handle ) :
ret = gobject . GObject . disconnect ( self , handle )
self . _gobject_handles . remove ( handle )
return ret
2011-04-11 19:00:57 +04:00
def add_gconf_handle ( self , handle ) :
self . _gconf_handles . append ( handle )
def remove_gconf_handle ( self , handle ) :
self . config . remove_notifier ( handle )
self . _gconf_handles . remove ( handle )
def add_gobject_timeout ( self , handle ) :
self . _gobject_timeouts . append ( handle )
def remove_gobject_timeout ( self , handle ) :
gobject . source_remove ( handle )
self . _gobject_timeouts . remove ( handle )
2011-04-14 20:49:48 +04:00
def _logtrace ( self , msg ) :
2011-04-13 17:27:02 +04:00
import traceback
2011-04-14 20:49:48 +04:00
logging . debug ( " %s ( %s %s ) \n : %s " %
( msg , self . object_key , self . refcount ( ) ,
" " . join ( traceback . format_stack ( ) ) ) )
2011-04-13 17:27:02 +04:00
2011-04-11 19:00:57 +04:00
def refcount ( self ) :
# Function generates 2 temporary refs, so adjust total accordingly
return ( sys . getrefcount ( self ) - 2 )
2010-12-09 22:06:00 +03:00
def get_hal_helper ( self ) :
from virtManager import halhelper
2010-12-10 17:57:42 +03:00
return halhelper . get_hal_helper ( )
2010-12-09 22:06:00 +03:00
2011-04-18 02:27:41 +04:00
def connect_once ( self , signal , func , * args ) :
id_list = [ ]
def wrap_func ( * wrapargs ) :
if id_list :
self . disconnect ( id_list [ 0 ] )
return func ( * wrapargs )
conn_id = self . connect ( signal , wrap_func , * args )
id_list . append ( conn_id )
return conn_id
def connect_opt_out ( self , signal , func , * args ) :
id_list = [ ]
def wrap_func ( * wrapargs ) :
ret = func ( * wrapargs )
if ret and id_list :
self . disconnect ( id_list [ 0 ] )
conn_id = self . connect ( signal , wrap_func , * args )
id_list . append ( conn_id )
return conn_id
2011-04-11 20:54:47 +04:00
def __del__ ( self ) :
if hasattr ( gobject . GObject , " __del__ " ) :
getattr ( gobject . GObject , " __del__ " ) ( self )
try :
2011-04-16 02:38:29 +04:00
if self . config :
self . config . remove_object ( self . object_key )
2011-04-11 20:54:47 +04:00
except :
2011-04-13 17:27:02 +04:00
logging . exception ( " Error removing %s " % self . object_key )
2011-04-11 19:00:57 +04:00
2010-12-09 01:26:19 +03:00
class vmmGObjectUI ( vmmGObject ) :
def __init__ ( self , filename , windowname ) :
vmmGObject . __init__ ( self )
self . windowname = windowname
self . window = None
self . topwin = None
self . gladefile = None
2011-04-11 21:06:59 +04:00
self . err = None
2010-12-09 01:26:19 +03:00
if filename :
self . gladefile = os . path . join ( self . config . get_glade_dir ( ) ,
filename )
self . window = gtk . glade . XML ( self . gladefile ,
self . windowname ,
domain = " virt-manager " )
self . topwin = self . window . get_widget ( self . windowname )
self . topwin . hide ( )
2011-04-12 00:26:41 +04:00
self . err = virtManager . error . vmmErrorDialog ( self . topwin )
2011-04-11 21:06:59 +04:00
def cleanup ( self ) :
vmmGObject . cleanup ( self )
self . window = None
2011-04-12 02:35:21 +04:00
self . topwin . destroy ( )
2011-04-11 21:06:59 +04:00
self . topwin = None
self . gladefile = None
self . err = None