From d6d705bf12e6f7ecc9a01fd2fb2a91f9f2283abc Mon Sep 17 00:00:00 2001 From: erich koslovsky Date: Thu, 16 Aug 2007 21:01:40 +0000 Subject: Made the model more flexible in terms of future extensions. Fixed some minor bugs. --- diff --git a/browser.dtd b/browser.dtd index 901b7d9..ae2da59 100644 --- a/browser.dtd +++ b/browser.dtd @@ -11,7 +11,11 @@ > diff --git a/linkbutton.py b/linkbutton.py index 7f2a203..c363eaf 100644 --- a/linkbutton.py +++ b/linkbutton.py @@ -21,12 +21,11 @@ from sugar.graphics.palette import Palette, WidgetInvoker from sugar.graphics import style class LinkButton(gtk.RadioToolButton): - def __init__(self, link_name, buffer=None, group=None): + def __init__(self, buffer, pos, group=None): gtk.RadioToolButton.__init__(self, group=group) self._palette = None self.set_image(buffer) - self.link_name = link_name - self.buf = buffer + self.pos = pos def set_image(self, buffer): img = gtk.Image() diff --git a/linktoolbar.py b/linktoolbar.py index 30d3875..ffadf45 100644 --- a/linktoolbar.py +++ b/linktoolbar.py @@ -36,26 +36,26 @@ class LinkToolbar(gtk.Toolbar): ([str])), 'link-rm': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, - ([str])) + ([int])) } def __init__(self): gtk.Toolbar.__init__(self) - def _add_link(self, link_name, buffer, pos): + def _add_link(self, url, buffer, pos): if self.get_children(): group = self.get_children()[0] else: group = None - palette = Palette(link_name) + palette = Palette(url) palette.props.position = Palette.TOP - link = LinkButton(link_name, buffer, group) + link = LinkButton(buffer, pos, group) link.set_palette(palette) - link.connect('clicked', self._link_clicked_cb, link_name) - self.insert(link, pos) + link.connect('clicked', self._link_clicked_cb, url) + self.insert(link, 0) link.show() menu_item = gtk.MenuItem(_('remove')) @@ -68,22 +68,23 @@ class LinkToolbar(gtk.Toolbar): if len(self.get_children()) > 0: self.show() - def _link_clicked_cb(self, link, link_name): + def _link_clicked_cb(self, link, url): if link.get_active(): - _logger.debug('link clicked=%s' %link_name) - self.emit('link-selected', link_name) + _logger.debug('link clicked=%s' %url) + self.emit('link-selected', url) def _rm_link(self): childs = self.get_children() for child in childs: if child.get_active(): - link_name = child.link_name + index = child.pos self.remove(child) # self.get_children()[0].props.active = True if len(self.get_children()) is 0: self.hide() - return link_name + return index + ''' deprecated def _rm_link_messenger(self, linkname): childs = self.get_children() for child in childs: @@ -92,9 +93,10 @@ class LinkToolbar(gtk.Toolbar): if len(self.get_children()) is 0: self.hide() return - + ''' + def _link_rm_palette_cb(self, widget, link): - self.emit('link-rm', link.link_name) + self.emit('link-rm', link.pos) self.remove(link) # self.get_children()[0].props.active = True if len(self.get_children()) is 0: diff --git a/messenger.py b/messenger.py index f21dfa5..932f570 100644 --- a/messenger.py +++ b/messenger.py @@ -21,6 +21,7 @@ import os import dbus from dbus.gobject_service import ExportedGObject import base64 +import sha SERVICE = "org.laptop.WebActivity" IFACE = SERVICE @@ -29,13 +30,14 @@ PATH = "/org/laptop/WebActivity" _logger = logging.getLogger('messenger') class Messenger(ExportedGObject): - def __init__(self, tube, is_initiator, linkbar): + def __init__(self, tube, is_initiator, model, linkbar): ExportedGObject.__init__(self, tube, PATH) self.tube = tube self.is_initiator = is_initiator self.members = [] self.entered = False self.linkbar = linkbar + self.model = model self.tube.watch_participants(self.participant_change_cb) def participant_change_cb(self, added, removed): @@ -77,8 +79,9 @@ class Messenger(ExportedGObject): self.members.append(sender) if self.is_initiator: self.tube.get_object(sender, PATH).init_members(self.members) - for child in self.linkbar.get_children(): - self.tube.get_object(sender, PATH).transfer_links(child.link_name, base64.b64encode(child.buf),dbus_interface=IFACE, reply_handler=self.reply_transfer, error_handler=lambda e:self.error_transfer(e, 'transfering file')) + for link in self.model.links: + if link['deleted'] == 0: + self.tube.get_object(sender, PATH).transfer_links(link['url'], link['title'], base64.b64encode(link['thumb']),dbus_interface=IFACE, reply_handler=self.reply_transfer, error_handler=lambda e:self.error_transfer(e, 'transfering file')) def reply_transfer(self): pass @@ -93,28 +96,33 @@ class Messenger(ExportedGObject): self.members = members self.id = self.members.index(self.tube.get_unique_name()) - @dbus.service.method(dbus_interface=IFACE, in_signature='ss', out_signature='') - def transfer_links(self, linkname, thumb): + @dbus.service.method(dbus_interface=IFACE, in_signature='sss', out_signature='') + def transfer_links(self, url, title, buffer): '''Sync the link list with the others ''' _logger.debug('Data received to sync link list.') - self.linkbar._add_link(linkname, base64.b64decode(thumb), -1) + thumb = base64.b64decode(buffer) + self.model.links.append( {'hash':sha.new(url).hexdigest(), 'url':url, 'title':title, 'thumb':thumb, + 'owner':'me', 'color':'red', 'deleted':0} ) + self.linkbar._add_link(url, thumb, len(self.model.links)-1) - def add_link(self, linkname, pix): - _logger.debug('Add Link: %s '%linkname) - thumb = base64.b64encode(pix) - self._add_link(linkname, thumb) + def add_link(self, url, title, thumb): + _logger.debug('Add Link: %s '%url) + self._add_link(url, title, base64.b64encode(thumb)) - @dbus.service.signal(IFACE, signature='ss') - def _add_link(self, linkname, thumb): + @dbus.service.signal(IFACE, signature='sss') + def _add_link(self, url, title, thumb): '''Signal to send the link information (add)''' - def _add_link_receiver(self, linkname, thumb, sender=None): + def _add_link_receiver(self, url, title, thumb, sender=None): '''Member sent a link''' handle = self.tube.bus_name_to_handle[sender] if self.tube.self_handle != handle: - data = base64.b64decode(thumb) - self.linkbar._add_link(linkname, data, -1) - _logger.debug('Added link: %s to linkbar.'%(linkname)) + buffer = base64.b64decode(thumb) + + self.model.links.append( {'hash':sha.new(url).hexdigest(), 'url':url, 'title':title, 'thumb':buffer, + 'owner':'me', 'color':'red', 'deleted':0} ) + self.linkbar._add_link(url, buffer, len(self.model.links)-1) + _logger.debug('Added link: %s to linkbar.'%(url)) def rm_link(self, linkname): _logger.debug('Remove Link: %s '%linkname) diff --git a/xmlio.py b/model.py index fe1b4d8..7c91107 100644 --- a/xmlio.py +++ b/model.py @@ -22,16 +22,16 @@ import logging import base64 -_logger = logging.getLogger('xmlio') +_logger = logging.getLogger('model') -class Xmlio(object): - ''' The xmlio of the activity. Contains methods to read and write +class Model(object): + ''' The model of the activity. Contains methods to read and write the configuration for a browser session to and from xml. ''' - def __init__(self, dtdpath, linkbar): - self.linkbar = linkbar + def __init__(self, dtdpath): + self.links = [] self.data = {} self.dtdpath = dtdpath self.data['name'] = 'first' @@ -58,18 +58,25 @@ class Xmlio(object): # write their content to the data structure for elem in res: attributes = elem.get_properties() - thumb = '' - link_name = '' - id = None if( elem.name == 'link' ): for attribute in attributes: - if(attribute.name == 'id'): - id = int(attribute.content) - elif(attribute.name == 'name'): - link_name = attribute.content + if(attribute.name == 'hash'): + hash = attribute.content + elif(attribute.name == 'url'): + url = attribute.content + elif(attribute.name == 'title'): + title = attribute.content elif(attribute.name == 'thumb'): - thumb = attribute.content - self.linkbar._add_link(link_name, base64.b64decode(thumb), id) + thumb = base64.b64decode(attribute.content) + elif(attribute.name == 'owner'): + owner = attribute.content + elif(attribute.name == 'color'): + color = attribute.content + elif(attribute.name == 'deleted'): + deleted = int(attribute.content) + + self.links.append( {'hash':hash, 'url':url, 'title':title, 'thumb':thumb, + 'owner':owner, 'color':color, 'deleted':deleted} ) elif( elem.name == 'session' ): for attribute in attributes: @@ -107,12 +114,15 @@ class Xmlio(object): elem = root.newChild(None, "session", None) elem.setProp("data", self.session_data) - for child in self.linkbar.get_children(): + for link in self.links: elem = root.newChild(None, "link", None) - elem.setProp("id", str(self.linkbar.get_item_index(child))) - elem.setProp("name", child.link_name) - elem.setProp("thumb", base64.b64encode(child.buf)) - + elem.setProp("hash", link['hash']) + elem.setProp("url", link['url']) + elem.setProp("title", link['title']) + elem.setProp("thumb", base64.b64encode(link['thumb'])) + elem.setProp("owner", link['owner']) + elem.setProp("color", link['color']) + elem.setProp("deleted", str(link['deleted'])) if doc.validateDtd(self.ctxt, self.dtd): doc.saveFormatFile(filepath, 1) @@ -125,3 +135,38 @@ class Xmlio(object): +if __name__ == '__main__': + model = Model(os.path.dirname(__file__)) + + filepath = 'sports.png' + + target = os.open(filepath, os.O_RDONLY) + filelen = os.stat(filepath).st_size + data = os.read(target, filelen) + os.close(target) + + ''' + import sha + url = 'www.sport.de' + title = 'sports' + hash = sha.new(url) + model.links.append({'hash':hash.hexdigest(), 'url':url, 'title':title}) + + url = 'www.jazz.de' + title = 'more on jazz' + hash = sha.new(url) + model.links.append({'hash':hash.hexdigest(), 'url':url, 'title':title}) + + url = 'www.taz.de' + title = 'die zeitung' + hash = sha.new(url) + model.links.append({'hash':hash.hexdigest(), 'url':url, 'title':title}) + + model.write('/tmp/test.bwr') + ''' + + model.read('/tmp/test.bwr') + + model.links.remove(model.links[1]) + + print model.links diff --git a/webactivity.py b/webactivity.py index feecb5a..6752513 100755 --- a/webactivity.py +++ b/webactivity.py @@ -20,6 +20,7 @@ from gettext import gettext as _ import gtk import dbus +import sha from sugar.activity import activity from sugar import env @@ -44,7 +45,7 @@ import progresslistener _LIBRARY_PATH = '/home/olpc/Library/index.html' from linktoolbar import LinkToolbar -from xmlio import Xmlio +from model import Model from tubeconn import TubeConnection from messenger import Messenger @@ -88,7 +89,7 @@ class WebActivity(activity.Activity): self.session_history.connect('session-link-changed', self._session_history_changed_cb) self._browser.connect("notify::title", self._title_changed_cb) - self.xmlio = Xmlio(os.path.dirname(__file__), self.linkbar) + self.model = Model(os.path.dirname(__file__)) self._main_view = gtk.VBox() self.set_canvas(self._main_view) @@ -101,6 +102,7 @@ class WebActivity(activity.Activity): self.linkbar.show() self.current = 'blank' + self.webtitle = 'blank' self.connect('key-press-event', self.key_press_cb) self.sname = _sugarext.get_prgname() _logger.debug('PNAME: %s' %self.sname) @@ -231,7 +233,7 @@ class WebActivity(activity.Activity): id, group_iface=self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP]) _logger.debug('Tube created') - self.messenger = Messenger(self.tube_conn, self.initiating, self.linkbar) + self.messenger = Messenger(self.tube_conn, self.initiating, self.model, self.linkbar) def _load_homepage(self): @@ -249,15 +251,21 @@ class WebActivity(activity.Activity): if embed.props.title is not '': #self.set_title(embed.props.title) _logger.debug('Title changed=%s' % embed.props.title) + self.webtitle = embed.props.title _sugarext.set_prgname("org.laptop.WebActivity") def read_file(self, file_path): if self.metadata['mime_type'] == 'text/plain': - - self.xmlio.read(file_path) - - _logger.debug('Trying to set session: %s.' % self.xmlio.session_data) - self._browser.set_session(self.xmlio.session_data) + self.model.read(file_path) + i=0 + for link in self.model.links: + _logger.debug('read: url=%s title=%s d=%s' % (link['url'], link['title'], link['deleted'])) + if link['deleted'] == 0: + self.linkbar._add_link(link['url'], link['thumb'], i) + i+=1 + + if self.model.session_data is not '': + self._browser.set_session(self.model.session_data) else: self._browser.load_uri(file_path) _sugarext.set_prgname(self.sname) @@ -271,9 +279,12 @@ class WebActivity(activity.Activity): if self._browser.props.title: self.metadata['title'] = self._browser.props.title - self.xmlio.session_data = self._browser.get_session() - _logger.debug('Trying save session: %s.' % self.xmlio.session_data) - self.xmlio.write(file_path) + for link in self.model.links: + _logger.debug('write: url=%s title=%s d=%s' % (link['url'], link['title'], link['deleted'])) + + self.model.session_data = self._browser.get_session() + _logger.debug('Trying save session: %s.' % self.model.session_data) + self.model.write(file_path) def destroy(self): if downloadmanager.can_quit(): @@ -288,24 +299,28 @@ class WebActivity(activity.Activity): def _link_selected_cb(self, linkbar, link): self._browser.load_uri(link) - def _link_rm_cb(self, linkbar, link): - if self.messenger is not None: - self.messenger.rm_link(link) + def _link_rm_cb(self, linkbar, index): + self.model.links[index]['deleted'] = 1 + self.model.links[index]['thumb'] = '' def key_press_cb(self, widget, event): if event.state & gtk.gdk.CONTROL_MASK: if gtk.gdk.keyval_name(event.keyval) == "l": buffer = self._get_screenshot() - _logger.debug('Add link: %s.' % self.current) - self.linkbar._add_link(self.current, buffer, -1) + _logger.debug('Add link: %s.' % self.current) + self.model.links.append( {'hash':sha.new(self.current).hexdigest(), 'url':self.current, 'title':self.webtitle, 'thumb':buffer, + 'owner':'me', 'color':'red', 'deleted':0} ) + + self.linkbar._add_link(self.current, buffer, len(self.model.links)-1) if self.messenger is not None: - self.messenger.add_link(self.current, buffer) + self.messenger.add_link(self.current, self.webtitle, buffer) return True elif gtk.gdk.keyval_name(event.keyval) == "r": _logger.debug('Remove link: %s.' % self.current) current = self.linkbar._rm_link() - if self.messenger is not None: - self.messenger.rm_link(current) + _logger.debug('Remove link: %s.' % self.current) + self.model.links[current]['deleted'] = 1 + self.model.links[current]['thumb'] = '' return True return False -- cgit v0.9.1