From ea3c890e361837a9dca990e54bb149db6154c09b Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Fri, 04 May 2007 17:57:47 +0000 Subject: Implemented saving web history to the journal. --- (limited to 'webactivity.py') diff --git a/webactivity.py b/webactivity.py index 880ea64..39c2bc6 100755 --- a/webactivity.py +++ b/webactivity.py @@ -13,8 +13,9 @@ # 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 St, Fifth Floor, Boston, MA 02110-1301 USA - +import os import logging +import time from gettext import gettext as _ import gtk @@ -22,6 +23,9 @@ import dbus import sugar.browser from sugar.activity import activity +from sugar.datastore import datastore +from sugar.datastore.datastore import WebSession +from sugar import profile from sugar.clipboard import clipboardservice from sugar import env @@ -36,6 +40,9 @@ class WebActivity(activity.Activity): logging.debug('Starting the web activity') + self._journal_handle = None + self._last_saved_session = None + self.set_title(_('Web Activity')) if browser: @@ -45,6 +52,8 @@ class WebActivity(activity.Activity): self._browser.connect('notify::title', self._title_changed_cb) toolbox = activity.ActivityToolbox(self) + activity_toolbar = toolbox.get_activity_toolbar() + activity_toolbar.close.connect('clicked', self._close_clicked_cb) toolbar = WebToolbar(self._browser) toolbox.add_toolbar(_('Browse'), toolbar) @@ -56,17 +65,72 @@ class WebActivity(activity.Activity): self.set_canvas(self._browser) self._browser.show() - if handle.uri: - url = handle.uri + if handle.object_id: + self._journal_handle = handle.object_id + # Will set the session in the realize callback. + self._browser.connect('realize', self._realize_cb) + elif handle.uri: + self._browser.load_url(handle.uri) else: - url = _HOMEPAGE + self._browser.load_url(_HOMEPAGE) - if url: - self._browser.load_url(url) + self.connect('focus-out-event', self._focus_out_event_cb) def _title_changed_cb(self, embed, pspec): self.set_title(embed.props.title) + def _realize_cb(self, browser): + if self._journal_handle: + obj = datastore.read(self._journal_handle) + f = open(obj.get_file_path(), 'r') + try: + session_data = f.read() + finally: + f.close() + logging.debug('Trying to set session: %s.' % session_data) + self._browser.set_session(session_data) + + def _focus_out_event_cb(self, widget, event): + self._autosave() + + def _close_clicked_cb(self, widget): + self._autosave() + return False + + def _autosave(self): + session_data = self._browser.get_session() + if not self._journal_handle: + home_dir = os.path.expanduser('~') + journal_dir = os.path.join(home_dir, "Journal") + web_session = WebSession({ + 'preview' : _('No preview'), + 'date' : str(time.time()), + 'title' : _('Web session'), + 'icon' : 'theme:object-link', + 'keep' : '0', + 'buddies' : str([ { 'name' : profile.get_nick_name(), + 'color': profile.get_color().to_string() }]), + 'icon-color' : profile.get_color().to_string()}) + f = open(os.path.join(journal_dir, '%i.txt' % time.time()), 'w') + try: + f.write(session_data) + finally: + f.close() + web_session.set_file_path(f.name) + self._journal_handle = datastore.write(web_session) + elif session_data != self._last_saved_session: + web_session = datastore.read(self._journal_handle) + metadata = web_session.get_metadata() + metadata['date'] = str(time.time()) + f = open(web_session.get_file_path(), 'w') + try: + f.write(session_data) + finally: + f.close() + datastore.write(web_session) + + self._last_saved_session = session_data + def start(): if not sugar.browser.startup(env.get_profile_path(), 'gecko'): raise "Error when initializising the web activity." -- cgit v0.9.1