2006-08-11 17:05:06 +02:00
|
|
|
import logging
|
|
|
|
|
2006-07-20 12:13:47 +02:00
|
|
|
import gtk
|
2006-08-12 16:19:47 +02:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
2006-07-20 12:13:47 +02:00
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
class Console(gtk.ScrolledWindow):
|
|
|
|
def __init__(self):
|
|
|
|
gtk.ScrolledWindow.__init__(self)
|
2006-08-11 17:05:06 +02:00
|
|
|
|
|
|
|
self._show_debug = False
|
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
self.set_policy(gtk.POLICY_AUTOMATIC,
|
|
|
|
gtk.POLICY_AUTOMATIC)
|
2006-08-11 17:05:06 +02:00
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
self._textview = gtk.TextView()
|
|
|
|
self._textview.set_wrap_mode(gtk.WRAP_WORD)
|
|
|
|
self.add(self._textview)
|
|
|
|
self._textview.show()
|
|
|
|
|
|
|
|
buf = self._textview.get_buffer()
|
2006-08-11 17:05:06 +02:00
|
|
|
self._debug_tag = buf.create_tag("debug")
|
|
|
|
self._debug_tag.set_property("invisible", True)
|
|
|
|
|
|
|
|
def get_show_debug(self):
|
|
|
|
return self._show_debug
|
|
|
|
|
|
|
|
def set_show_debug(self, show_debug):
|
|
|
|
self._show_debug = show_debug
|
|
|
|
self._debug_tag.set_property("invisible", not show_debug)
|
|
|
|
|
|
|
|
def log(self, level, message):
|
|
|
|
msg = message + '\n'
|
|
|
|
buf = self._textview.get_buffer()
|
|
|
|
it = buf.get_end_iter()
|
|
|
|
|
|
|
|
if level == logging.DEBUG:
|
|
|
|
buf.insert_with_tags(it, msg, self._debug_tag)
|
|
|
|
else:
|
|
|
|
buf.insert(it, msg)
|
2006-08-12 16:19:47 +02:00
|
|
|
|
|
|
|
class ConsoleDbusService(dbus.service.Object):
|
|
|
|
def __init__(self, console, bus_name):
|
|
|
|
dbus.service.Object.__init__(self, bus_name, '/org/laptop/Sugar/Console')
|
|
|
|
self._console = console
|
|
|
|
|
|
|
|
@dbus.service.method('org.laptop.Sugar.Console')
|
|
|
|
def log(self, level, module_id, message):
|
|
|
|
self._console.log(level, module_id, message)
|
|
|
|
|
2006-07-20 12:13:47 +02:00
|
|
|
class ConsoleWindow(gtk.Window):
|
|
|
|
def __init__(self):
|
|
|
|
gtk.Window.__init__(self)
|
|
|
|
|
|
|
|
self.set_default_size(620, 440)
|
|
|
|
self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
|
|
|
|
self.set_title("Console")
|
|
|
|
self.connect("delete_event", lambda w, e: w.hide_on_delete())
|
|
|
|
|
2006-08-11 17:05:06 +02:00
|
|
|
vbox = gtk.VBox()
|
|
|
|
|
|
|
|
toolbar = gtk.Toolbar()
|
|
|
|
|
|
|
|
self._debug_toggle = gtk.ToggleToolButton()
|
|
|
|
self._debug_toggle.connect('toggled', self.__debug_toggled_cb)
|
|
|
|
self._debug_toggle.set_label('Debug')
|
|
|
|
toolbar.insert(self._debug_toggle, -1)
|
|
|
|
self._debug_toggle.show()
|
|
|
|
|
|
|
|
vbox.pack_start(toolbar, False)
|
|
|
|
toolbar.show()
|
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
self._nb = gtk.Notebook()
|
2006-08-11 17:05:06 +02:00
|
|
|
self._nb.connect('switch-page', self.__page_changed_cb)
|
|
|
|
vbox.pack_start(self._nb)
|
2006-08-11 15:21:11 +02:00
|
|
|
self._nb.show()
|
2006-07-20 12:13:47 +02:00
|
|
|
|
2006-08-11 17:05:06 +02:00
|
|
|
self.add(vbox)
|
|
|
|
vbox.show()
|
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
self._consoles = {}
|
|
|
|
|
2006-08-12 16:19:47 +02:00
|
|
|
session_bus = dbus.SessionBus()
|
|
|
|
bus_name = dbus.service.BusName('org.laptop.Sugar.Console', bus=session_bus)
|
|
|
|
ConsoleDbusService(self, bus_name)
|
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
def _add_console(self, page_id):
|
|
|
|
console = Console()
|
|
|
|
page = self._nb.append_page(console, gtk.Label(page_id))
|
|
|
|
console.show()
|
|
|
|
|
|
|
|
self._consoles[page_id] = console
|
|
|
|
|
|
|
|
return console
|
|
|
|
|
|
|
|
def _get_console(self, page_id):
|
|
|
|
if not self._consoles.has_key(page_id):
|
|
|
|
console = self._add_console(page_id)
|
|
|
|
else:
|
|
|
|
console = self._consoles[page_id]
|
|
|
|
return console
|
|
|
|
|
2006-08-11 17:05:06 +02:00
|
|
|
def __debug_toggled_cb(self, button):
|
|
|
|
console = self._nb.get_nth_page(self._nb.get_current_page())
|
|
|
|
console.set_show_debug(button.get_active())
|
|
|
|
|
|
|
|
def __page_changed_cb(self, notebook, page, page_num):
|
|
|
|
console = self._nb.get_nth_page(page_num)
|
|
|
|
self._debug_toggle.set_active(console.get_show_debug())
|
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
def set_page(self, page_id):
|
|
|
|
page = self._nb.page_num(self._consoles[page_id])
|
|
|
|
self._nb.set_current_page(page)
|
|
|
|
|
2006-08-11 17:05:06 +02:00
|
|
|
def log(self, level, page_id, message):
|
2006-08-11 15:21:11 +02:00
|
|
|
console = self._get_console(page_id)
|
2006-08-11 17:05:06 +02:00
|
|
|
console.log(level, message)
|