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.
|
|
|
|
#
|
|
|
|
|
2013-06-18 07:46:24 -05:00
|
|
|
import logging
|
2010-12-08 16:26:19 -06:00
|
|
|
import os
|
2011-04-11 10:00:57 -05:00
|
|
|
import sys
|
2013-06-18 07:46:24 -05:00
|
|
|
import traceback
|
2010-12-08 16:26:19 -06:00
|
|
|
|
2013-08-09 08:23:01 -05:00
|
|
|
from virtManager import config
|
2011-04-28 16:13:05 -05:00
|
|
|
|
2013-04-11 16:16:33 -05:00
|
|
|
# pylint: disable=E0611
|
2013-04-18 15:03:43 -05:00
|
|
|
from gi.repository import Gdk
|
2013-02-16 13:03:30 -06:00
|
|
|
from gi.repository import GLib
|
2012-05-14 08:24:56 -05:00
|
|
|
from gi.repository import GObject
|
2013-04-18 15:03:43 -05:00
|
|
|
from gi.repository import Gtk
|
2013-04-11 16:16:33 -05:00
|
|
|
# pylint: enable=E0611
|
2010-12-08 16:26:19 -06:00
|
|
|
|
2011-04-18 11:39:53 -05:00
|
|
|
|
2013-02-16 13:03:30 -06:00
|
|
|
class vmmGObject(GObject.GObject):
|
2011-04-28 16:13:05 -05:00
|
|
|
_leak_check = True
|
|
|
|
|
2013-09-06 18:36:09 -05:00
|
|
|
@staticmethod
|
|
|
|
def idle_add(func, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Make sure idle functions are run thread safe
|
|
|
|
"""
|
|
|
|
def cb():
|
|
|
|
try:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
except:
|
|
|
|
print traceback.format_exc()
|
|
|
|
return False
|
|
|
|
return GLib.idle_add(cb)
|
|
|
|
|
2010-12-08 16:26:19 -06:00
|
|
|
def __init__(self):
|
2012-05-14 08:24:56 -05:00
|
|
|
GObject.GObject.__init__(self)
|
2013-08-09 08:23:01 -05:00
|
|
|
self.config = config.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-07-22 17:07:57 -05:00
|
|
|
self._signal_id_map = {}
|
|
|
|
self._next_signal_id = 1
|
|
|
|
|
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
|
2011-04-28 16:03:22 -05:00
|
|
|
if self.config and self._leak_check:
|
2011-04-15 17:38:29 -05:00
|
|
|
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)
|
2011-07-23 20:16:54 -05:00
|
|
|
|
|
|
|
self._cleanup()
|
2011-04-11 10:00:57 -05:00
|
|
|
except:
|
2012-01-16 21:04:40 -06:00
|
|
|
logging.exception("Error cleaning up %s", self)
|
2011-04-11 10:00:57 -05:00
|
|
|
|
2011-07-23 20:16:54 -05:00
|
|
|
def _cleanup(self):
|
|
|
|
raise NotImplementedError("_cleanup must be implemented in subclass")
|
|
|
|
|
2011-04-11 12:06:59 -05:00
|
|
|
def connect(self, name, callback, *args):
|
2012-05-14 08:24:56 -05:00
|
|
|
ret = GObject.GObject.connect(self, name, callback, *args)
|
|
|
|
self._gobject_handles.append(ret)
|
|
|
|
return ret
|
2011-07-22 17:07:57 -05:00
|
|
|
|
2011-04-11 12:06:59 -05:00
|
|
|
def disconnect(self, handle):
|
2012-05-14 08:24:56 -05:00
|
|
|
ret = GObject.GObject.disconnect(self, handle)
|
|
|
|
self._gobject_handles.remove(handle)
|
|
|
|
return ret
|
2011-04-11 12:06:59 -05:00
|
|
|
|
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):
|
2013-02-16 13:03:30 -06:00
|
|
|
GLib.source_remove(handle)
|
2011-04-11 10:00:57 -05:00
|
|
|
self._gobject_timeouts.remove(handle)
|
|
|
|
|
2013-01-11 15:15:06 -06:00
|
|
|
def _logtrace(self, msg=""):
|
|
|
|
if msg:
|
|
|
|
msg += " "
|
|
|
|
logging.debug("%s(%s %s)\n:%s",
|
2013-04-25 11:07:57 -05:00
|
|
|
msg, self.object_key, self._refcount(),
|
2012-01-16 21:04:40 -06:00
|
|
|
"".join(traceback.format_stack()))
|
2011-04-13 08:27:02 -05:00
|
|
|
|
2013-04-25 11:07:57 -05:00
|
|
|
def _refcount(self):
|
2011-04-11 10:00:57 -05:00
|
|
|
# Function generates 2 temporary refs, so adjust total accordingly
|
|
|
|
return (sys.getrefcount(self) - 2)
|
|
|
|
|
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):
|
|
|
|
"""
|
2013-02-16 13:03:30 -06:00
|
|
|
Safe wrapper for using 'self.emit' with GLib.idle_add
|
2011-04-18 10:12:36 -05:00
|
|
|
"""
|
|
|
|
def emitwrap(_s, *_a):
|
|
|
|
self.emit(_s, *_a)
|
|
|
|
return False
|
|
|
|
|
2012-02-10 13:07:51 -06:00
|
|
|
self.idle_add(emitwrap, signal, *args)
|
2011-04-18 10:12:36 -05:00
|
|
|
|
2012-02-10 13:07:51 -06:00
|
|
|
def timeout_add(self, timeout, func, *args):
|
2011-04-18 10:12:36 -05:00
|
|
|
"""
|
|
|
|
Make sure timeout functions are run thread safe
|
|
|
|
"""
|
2013-06-18 07:46:24 -05:00
|
|
|
def cb():
|
|
|
|
try:
|
|
|
|
return func(*args)
|
|
|
|
except:
|
|
|
|
print traceback.format_exc()
|
|
|
|
return False
|
|
|
|
ret = GLib.timeout_add(timeout, cb)
|
2013-06-14 15:22:33 -05:00
|
|
|
self.add_gobject_timeout(ret)
|
2013-06-14 15:32:48 -05:00
|
|
|
return ret
|
2011-04-18 10:12:36 -05:00
|
|
|
|
2011-07-22 17:07:57 -05:00
|
|
|
def emit(self, signal_name, *args):
|
2012-05-14 08:24:56 -05:00
|
|
|
return GObject.GObject.emit(self, signal_name, *args)
|
2011-04-28 16:13:05 -05:00
|
|
|
|
2011-04-11 11:54:47 -05:00
|
|
|
def __del__(self):
|
|
|
|
try:
|
2011-07-22 18:12:31 -05:00
|
|
|
if self.config and self._leak_check:
|
2011-04-15 17:38:29 -05:00
|
|
|
self.config.remove_object(self.object_key)
|
2011-04-11 11:54:47 -05:00
|
|
|
except:
|
2012-01-16 21:04:40 -06:00
|
|
|
logging.exception("Error removing %s", self.object_key)
|
2011-04-11 10:00:57 -05:00
|
|
|
|
2013-04-13 13:34:52 -05:00
|
|
|
|
2010-12-08 16:26:19 -06:00
|
|
|
class vmmGObjectUI(vmmGObject):
|
2013-06-08 18:25:36 -05:00
|
|
|
def __init__(self, filename, windowname, builder=None, topwin=None):
|
2010-12-08 16:26:19 -06:00
|
|
|
vmmGObject.__init__(self)
|
|
|
|
|
|
|
|
if filename:
|
2013-06-08 18:25:36 -05:00
|
|
|
uifile = os.path.join(self.config.get_ui_dir(), filename)
|
2012-02-01 16:26:46 -06:00
|
|
|
|
2013-02-16 12:31:46 -06:00
|
|
|
self.builder = Gtk.Builder()
|
|
|
|
self.builder.set_translation_domain("virt-manager")
|
2013-06-08 18:25:36 -05:00
|
|
|
self.builder.add_from_string(file(uifile).read())
|
2012-02-01 16:26:46 -06:00
|
|
|
|
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 09:18:47 -05:00
|
|
|
if not topwin:
|
|
|
|
self.topwin = self.widget(windowname)
|
|
|
|
self.topwin.hide()
|
|
|
|
else:
|
|
|
|
self.topwin = topwin
|
2013-06-08 18:25:36 -05:00
|
|
|
else:
|
|
|
|
self.builder = builder
|
|
|
|
self.topwin = topwin
|
2010-12-08 16:26:19 -06:00
|
|
|
|
2013-09-01 19:24:15 -05:00
|
|
|
self._err = None
|
|
|
|
|
|
|
|
def _get_err(self):
|
|
|
|
if self._err is None:
|
|
|
|
from virtManager import error
|
|
|
|
self._err = error.vmmErrorDialog(self.topwin)
|
|
|
|
return self._err
|
|
|
|
err = property(_get_err)
|
2011-04-11 12:06:59 -05:00
|
|
|
|
2011-07-14 12:13:13 -05:00
|
|
|
def widget(self, name):
|
2013-02-16 12:31:46 -06:00
|
|
|
return self.builder.get_object(name)
|
2011-07-14 12:13:13 -05:00
|
|
|
|
2011-04-11 12:06:59 -05:00
|
|
|
def cleanup(self):
|
2012-07-08 19:56:36 -05:00
|
|
|
self.close()
|
2011-04-11 12:06:59 -05:00
|
|
|
vmmGObject.cleanup(self)
|
2013-02-16 12:31:46 -06:00
|
|
|
self.builder = None
|
2011-04-11 17:35:21 -05:00
|
|
|
self.topwin.destroy()
|
2011-04-11 12:06:59 -05:00
|
|
|
self.topwin = None
|
2013-09-01 19:24:15 -05:00
|
|
|
self._err = None
|
2011-04-18 10:25:28 -05:00
|
|
|
|
2011-07-23 20:16:54 -05:00
|
|
|
def _cleanup(self):
|
|
|
|
raise NotImplementedError("_cleanup must be implemented in subclass")
|
|
|
|
|
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):
|
2012-05-14 08:24:56 -05:00
|
|
|
if Gdk.keyval_name(event.keyval) == "Escape":
|
2011-04-18 10:25:28 -05:00
|
|
|
self.close()
|
|
|
|
|
|
|
|
self.topwin.connect("key-press-event", close_on_escape)
|