2010-12-08 16:26:19 -06: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 10:00:57 -05:00
|
|
|
import sys
|
|
|
|
import logging
|
2010-12-08 16:26:19 -06:00
|
|
|
|
|
|
|
import gtk
|
|
|
|
import gobject
|
|
|
|
|
2011-04-18 12:19:39 -05:00
|
|
|
import virtManager
|
|
|
|
import virtManager.util as util
|
2010-12-08 16:26:19 -06:00
|
|
|
|
2011-04-18 10:12:36 -05:00
|
|
|
def _safe_wrapper(func, *args):
|
|
|
|
gtk.gdk.threads_enter()
|
|
|
|
try:
|
|
|
|
return func(*args)
|
|
|
|
finally:
|
|
|
|
gtk.gdk.threads_leave()
|
|
|
|
|
2010-12-08 16:26:19 -06:00
|
|
|
class vmmGObject(gobject.GObject):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def type_register(*args, **kwargs):
|
|
|
|
gobject.type_register(*args, **kwargs)
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
gobject.GObject.__init__(self)
|
2011-04-18 12:19:39 -05:00
|
|
|
self.config = util.running_config
|
2010-12-08 16:26:19 -06:00
|
|
|
|
2011-04-11 10:00:57 -05:00
|
|
|
self._gobject_handles = []
|
|
|
|
self._gobject_timeouts = []
|
|
|
|
self._gconf_handles = []
|
|
|
|
|
2011-04-13 08:27:02 -05:00
|
|
|
self.object_key = str(self)
|
2011-04-15 17:38:29 -05: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 11:54:47 -05:00
|
|
|
|
2011-04-11 10:00:57 -05: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 12:06:59 -05:00
|
|
|
self.disconnect(h)
|
2011-04-11 10:00:57 -05:00
|
|
|
for h in self._gobject_timeouts[:]:
|
|
|
|
self.remove_gobject_timeout(h)
|
|
|
|
except:
|
|
|
|
logging.exception("Error cleaning up %s" % self)
|
|
|
|
|
2011-04-11 12:06:59 -05: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 10:00:57 -05: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 11:49:48 -05:00
|
|
|
def _logtrace(self, msg):
|
2011-04-13 08:27:02 -05:00
|
|
|
import traceback
|
2011-04-14 11:49:48 -05:00
|
|
|
logging.debug("%s (%s %s)\n:%s" %
|
|
|
|
(msg, self.object_key, self.refcount(),
|
|
|
|
"".join(traceback.format_stack())))
|
2011-04-13 08:27:02 -05:00
|
|
|
|
2011-04-11 10:00:57 -05:00
|
|
|
def refcount(self):
|
|
|
|
# Function generates 2 temporary refs, so adjust total accordingly
|
|
|
|
return (sys.getrefcount(self) - 2)
|
|
|
|
|
2010-12-09 13:06:00 -06:00
|
|
|
def get_hal_helper(self):
|
|
|
|
from virtManager import halhelper
|
2010-12-10 08:57:42 -06:00
|
|
|
return halhelper.get_hal_helper()
|
2010-12-09 13:06:00 -06:00
|
|
|
|
2011-04-17 17:27:41 -05: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-18 10:12:36 -05:00
|
|
|
def idle_emit(self, signal, *args):
|
|
|
|
"""
|
|
|
|
Safe wrapper for using 'self.emit' with gobject.idle_add
|
|
|
|
"""
|
|
|
|
def emitwrap(_s, *_a):
|
|
|
|
self.emit(_s, *_a)
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.safe_idle_add(emitwrap, signal, *args)
|
|
|
|
|
|
|
|
def safe_idle_add(self, func, *args):
|
|
|
|
"""
|
|
|
|
Make sure idle functions are run thread safe
|
|
|
|
"""
|
|
|
|
return gobject.idle_add(_safe_wrapper, func, *args)
|
|
|
|
|
|
|
|
def safe_timeout_add(self, timeout, func, *args):
|
|
|
|
"""
|
|
|
|
Make sure timeout functions are run thread safe
|
|
|
|
"""
|
|
|
|
return gobject.timeout_add(timeout, _safe_wrapper, func, *args)
|
|
|
|
|
2011-04-11 11:54:47 -05:00
|
|
|
def __del__(self):
|
|
|
|
if hasattr(gobject.GObject, "__del__"):
|
|
|
|
getattr(gobject.GObject, "__del__")(self)
|
|
|
|
|
|
|
|
try:
|
2011-04-15 17:38:29 -05:00
|
|
|
if self.config:
|
|
|
|
self.config.remove_object(self.object_key)
|
2011-04-11 11:54:47 -05:00
|
|
|
except:
|
2011-04-13 08:27:02 -05:00
|
|
|
logging.exception("Error removing %s" % self.object_key)
|
2011-04-11 10:00:57 -05:00
|
|
|
|
2010-12-08 16:26:19 -06: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 12:06:59 -05:00
|
|
|
self.err = None
|
2010-12-08 16:26:19 -06: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-11 15:26:41 -05:00
|
|
|
self.err = virtManager.error.vmmErrorDialog(self.topwin)
|
2011-04-11 12:06:59 -05:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
vmmGObject.cleanup(self)
|
|
|
|
self.window = None
|
2011-04-11 17:35:21 -05:00
|
|
|
self.topwin.destroy()
|
2011-04-11 12:06:59 -05:00
|
|
|
self.topwin = None
|
|
|
|
self.gladefile = None
|
|
|
|
self.err = None
|
2011-04-18 10:25:28 -05:00
|
|
|
|
2011-04-28 13:38:16 -05:00
|
|
|
def close(self, ignore1=None, ignore2=None):
|
|
|
|
pass
|
|
|
|
|
2011-04-18 10:25:28 -05:00
|
|
|
def bind_escape_key_close(self):
|
|
|
|
def close_on_escape(src_ignore, event):
|
|
|
|
if gtk.gdk.keyval_name(event.keyval) == "Escape":
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
self.topwin.connect("key-press-event", close_on_escape)
|