diff options
-rw-r--r-- | browser.py | 4 | ||||
-rw-r--r-- | model.py | 168 | ||||
-rw-r--r-- | sessionstore.py | 25 | ||||
-rwxr-xr-x | webactivity.py | 55 |
4 files changed, 57 insertions, 195 deletions
@@ -53,8 +53,8 @@ class Browser(WebView): def get_session(self): return sessionstore.get_session(self) - def set_session(self, session_data): - return sessionstore.set_session(self, session_data) + def set_session(self, data): + return sessionstore.set_session(self, data) class WindowCreator: _com_interfaces_ = interfaces.nsIWindowCreator @@ -16,177 +16,51 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # -import libxml2 import os import logging -import base64 +import json import sha import gobject _logger = logging.getLogger('model') - class Model(gobject.GObject): - #__gtype_name__ = 'Model' + ''' The model of the activity which uses json to serialize its data + to a file and deserelize from it. + ''' __gsignals__ = { 'add_link': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, ([int])) + gobject.TYPE_NONE, ([int])) } - - ''' 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): + def __init__(self): gobject.GObject.__init__(self) - self.links = [] + self.data = {} - self.dtdpath = dtdpath - self.data['name'] = 'first' - self.session_data = '' - - try: - self.dtd = libxml2.parseDTD(None, os.path.join(self.dtdpath, 'browser.dtd')) - except libxml2.parserError, e: - _logger.error('Init: no browser.dtd found ' +str(e)) - self.dtd = None - self.ctxt = libxml2.newValidCtxt() + self._links = [] + self.data['shared_links'] = self._links def add_link(self, url, title, thumb, owner, color): - print 'model: add link' self.links.append( {'hash':sha.new(url).hexdigest(), 'url':url, 'title':title, 'thumb':thumb, - 'owner':owner, 'color':color, 'deleted':0} ) + 'owner':owner, 'color':color, 'deleted':0} ) self.emit('add_link', len(self.links)-1) - - def read(self, filepath): - ''' reads the configuration from an xml file ''' - - try: - doc = libxml2.parseFile(filepath) - if doc.validateDtd(self.ctxt, self.dtd): - - # get the requested nodes - xpa = doc.xpathNewContext() - res = xpa.xpathEval("//*") - # write their content to the data structure - for elem in res: - attributes = elem.get_properties() - if( elem.name == 'link' ): - for attribute in attributes: - 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 = 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: - if(attribute.name == 'data'): - self.session_data = attribute.content - - elif( elem.name == 'browser' ): - for attribute in attributes: - if(attribute.name == 'name'): - self.data['name'] = attribute.content - - xpa.xpathFreeContext() - else: - _logger.error('Read: Error in validation of the file') - doc.freeDoc() - return 1 - doc.freeDoc() - return 0 - except libxml2.parserError, e: - _logger.error('Read: Error parsing file ' +str(e)) - return 2 - - - def write(self, filepath): - ''' writes the configuration to an xml file ''' - doc = libxml2.newDoc("1.0") - root = doc.newChild(None, "browser", None) + def mark_link_deleted(self, index): + self._links[index]['deleted'] = 1 + self._links[index]['thumb'] = '' - if(self.data.get('name', None) != None): - root.setProp("name", self.data['name']) - else: - _logger.error('Write: No name is specified. Can not write session.') - return 1 - - elem = root.newChild(None, "session", None) - elem.setProp("data", self.session_data) - - for link in self.links: - elem = root.newChild(None, "link", None) - 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) - else: - _logger.error('Write: Error in validation of the file') - doc.freeDoc() - return 2 - doc.freeDoc() - return 0 + def serialize(self): + self.get_session() + return json.write(self.data) + def deserialize(self, data): + self.data = json.read(data) + self.links = self.data + def get_links_ids(self): ids = [] - for link in self.links: + for link in self._links: ids.append(link['hash']) ids.append('') return ids - - -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/sessionstore.py b/sessionstore.py index 73251ff..2b96ea0 100644 --- a/sessionstore.py +++ b/sessionstore.py @@ -22,25 +22,18 @@ import logging from xpcom import components from xpcom.components import interfaces -import json - def get_session(browser): session_history = browser.web_navigation.sessionHistory - + if session_history.count == 0: - return '' - - session_data = {} - session_data['history'] = _get_history(session_history) - logging.debug('%r' % session_data) - return json.write(session_data) - -def set_session(browser, session_str): - session_data = json.read(session_str) - _set_history(browser.web_navigation.sessionHistory, session_data['history']) - - if session_data['history']: - browser.web_navigation.gotoIndex(len(session_data['history']) - 1) + return '' + return _get_history(session_history) + +def set_session(browser, data): + _set_history(browser.web_navigation.sessionHistory, data) + + if data: + browser.web_navigation.gotoIndex(len(data) - 1) else: browser.load_uri('about:blank') diff --git a/webactivity.py b/webactivity.py index 95b810b..85ce780 100755 --- a/webactivity.py +++ b/webactivity.py @@ -89,7 +89,8 @@ class WebActivity(activity.Activity): self.toolbar._add_link.connect('clicked', self._share_link_button_cb) self._browser.connect("notify::title", self._title_changed_cb) - self.model = Model(os.path.dirname(__file__)) + + self.model = Model() self.model.connect('add_link', self._add_link_model_cb) self._main_view = gtk.VBox() @@ -261,9 +262,9 @@ class WebActivity(activity.Activity): def read_file(self, file_path): if self.metadata['mime_type'] == 'text/plain': - self.model.read(file_path) + self.model.deserialize(file_path) i=0 - for link in self.model.links: + for link in self.model['shared_links']: _logger.debug('read: url=%s title=%s d=%s' % (link['url'], link['title'], link['color'])) @@ -273,8 +274,8 @@ class WebActivity(activity.Activity): link['owner'], i) i+=1 - if self.model.session_data is not '': - self._browser.set_session(self.model.session_data) + if self.model.data['history'] is not '': + self._browser.set_session(self.model.data['history']) else: self._browser.load_uri(file_path) _sugarext.set_prgname(self.sname) @@ -288,21 +289,17 @@ class WebActivity(activity.Activity): if self._browser.props.title: self.metadata['title'] = self._browser.props.title - for link in self.model.links: - _logger.debug('write: url=%s title=%s d=%s' % (link['url'], - link['title'], - link['color'])) - - self.model.session_data = self._browser.get_session() - _logger.debug('Trying save session: %s.' % self.model.session_data) - self.model.write(file_path) + self.model.data['history'] = self._browser.get_session() + # self.model.write(file_path) def _share_link_button_cb(self, button): + _logger.debug('button: Add link: %s.' % self.current) self._add_link() def key_press_cb(self, widget, event): if event.state & gtk.gdk.CONTROL_MASK: - if gtk.gdk.keyval_name(event.keyval) == "l": + if gtk.gdk.keyval_name(event.keyval) == "l": + _logger.debug('keyboard: Add link: %s.' % self.current) self._add_link() return True elif gtk.gdk.keyval_name(event.keyval) == "s": @@ -312,16 +309,11 @@ class WebActivity(activity.Activity): return False def _add_link(self): + ''' take screenshot and add link info to the model ''' buffer = self._get_screenshot() - _logger.debug('keyboard: Add link: %s.' % self.current) - self.model.links.append( {'hash':sha.new(self.current).hexdigest(), - 'url':self.current, 'title':self.webtitle, - 'thumb':buffer, 'owner':self.owner.props.nick, - 'color':self.owner.props.color, 'deleted':0} ) - - self._add_link_totray(self.current, buffer, self.owner.props.color, - self.webtitle, self.owner.props.nick, - len(self.model.links)-1) + self.model.add_link( self.current, self.webtitle, buffer, + self.owner.props.nick, self.owner.props.color) + if self.messenger is not None: import base64 self.messenger._add_link(self.current, self.webtitle, @@ -330,25 +322,28 @@ class WebActivity(activity.Activity): base64.b64encode(buffer)) def _add_link_model_cb(self, model, index): - link = self.model.links[index] + ''' receive index of new link from the model ''' + link = self.model._links[index] self._add_link_totray(link['url'], link['thumb'], link['color'], link['title'], link['owner'], index) - def _add_link_totray(self, url, buffer, color, title, owner, index): + def _add_link_totray(self, url, buffer, color, title, owner, index): + ''' add a link to the tray ''' item = LinkButton(url, buffer, color, title, owner, index) item.connect('clicked', self._link_clicked_cb, url) item.connect('remove_link', self._link_removed_cb) self._tray.add_item(item, 0) # add to the beginning of the tray item.show() - def _link_clicked_cb(self, button, url): - self._browser.load_uri(url) - def _link_removed_cb(self, button, index): - self.model.links[index]['deleted'] = 1 - self.model.links[index]['thumb'] = '' + ''' remove a link from tray and mark deleted in the model ''' + self.model.mark_link_deleted(index) self._tray.remove_item(button) + + def _link_clicked_cb(self, button, url): + ''' an item of the link tray has been clicked ''' + self._browser.load_uri(url) def _toggle_visibility_tray(self): if self._tray.isvisible is True: |