From 23fb24b4b0dadbe2129e9d45b2fb020ffbdf946e Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Sun, 04 Mar 2007 21:25:55 +0000 Subject: Added support for saving to and opening from the journal. --- (limited to 'AbiWordActivity.py') diff --git a/AbiWordActivity.py b/AbiWordActivity.py index f8a05db..bd70193 100644 --- a/AbiWordActivity.py +++ b/AbiWordActivity.py @@ -18,42 +18,90 @@ import logging import os import time -import gtk + +import gobject import hippo + +from sugar.activity import activity +from sugar.datastore import datastore +from sugar.datastore.datastore import Text +from sugar import profile from abiword import Canvas + from toolbar import AbiToolbar -from sugar.activity import activity class AbiWordActivity (activity.Activity): - def __init__ (self, handle): - activity.Activity.__init__ (self, handle) - self.set_title ("Write") + def __init__ (self, handle): + activity.Activity.__init__ (self, handle) + self.set_title ("Write") + + self._journal_handle = None + self._last_saved_text = None + + hippoCanvasBox = hippo.CanvasBox() + self.set_root(hippoCanvasBox) + + # create our main abiword canvas + self.abiword_canvas = Canvas() + + # create and add a toolbar for our window, which listens to our canvas + abiToolbar = AbiToolbar(hippoCanvasBox, self.abiword_canvas) + + # create a hippo container to embed our canvas in + abiwordCanvasContainer = hippo.CanvasWidget() + abiwordCanvasContainer.props.widget = self.abiword_canvas - hippoCanvasBox = hippo.CanvasBox() - self.set_root(hippoCanvasBox) + # add the controls to our window + hippoCanvasBox.append(abiwordCanvasContainer, hippo.PACK_EXPAND) - # create our main abiword canvas - self.abiword_canvas = Canvas() + if handle.object_id: + obj = datastore.read(handle.object_id) + self.abiword_canvas.load_file('file://' + obj.get_file_path()) + else: + # open a blank file + self.abiword_canvas.load_file("") - # create and add a toolbar for our window, which listens to our canvas - abiToolbar = AbiToolbar(hippoCanvasBox, self.abiword_canvas) + self.abiword_canvas.show() - # create a hippo container to embed our canvas in - abiwordCanvasContainer = hippo.CanvasWidget() - abiwordCanvasContainer.props.widget = self.abiword_canvas + self.connect('focus-out-event', self._focus_out_event_cb) + self.connect('delete-event', self._delete_event_cb) - # add the controls to our window - hippoCanvasBox.append(abiwordCanvasContainer, hippo.PACK_EXPAND) + def _focus_out_event_cb(self, widget, event): + self._autosave() - # show the abiword canvas and have it open a blank file - self.abiword_canvas.load_file("") - self.abiword_canvas.show() + def _delete_event_cb(self, widget, event): + self._autosave() + + def _autosave(self): + text_content = self.abiword_canvas.get_content(".txt")[0] + if not self._journal_handle: + home_dir = os.path.expanduser('~') + journal_dir = os.path.join(home_dir, "Journal") + text = Text({'preview' : text_content[0:30], + 'date' : str(time.time()), + 'title' : 'A text document', + 'icon' : 'theme:object-text', + '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.abw' % time.time()), 'w') + try: + f.write(self.abiword_canvas.get_content(".abw")[0]) + finally: + f.close() + text.set_file_path(f.name) + self._journal_handle = datastore.write(text) + elif text_content != self._last_saved_text: + text = datastore.read(self._journal_handle) + text.get_metadata()['preview'] = text_content[0:30] + text.get_metadata()['date'] = str(time.time()) + f = open(text.get_file_path(), 'w') + try: + f.write(self.abiword_canvas.get_content(".abw")[0]) + finally: + f.close() + datastore.write(text) - def execute(self, command, args): - if(command == 'open_document'): - self.abiword_canvas.load_file('file://' + args[0]) - - return True - else: - return False + self._last_saved_text = text_content -- cgit v0.9.1