Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/AbiWordActivity.py
blob: 631b707d1f63dfbe933b0cf02bffc89044c61f36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright (C) 2006 by Martin Sevior
# Copyright (C) 2006-2007 Marc Maurer <uwog@uwog.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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
from gettext import gettext as _
import logging
import os
import time

import gtk

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 TextToolbar, ImageToolbar, TableToolbar, ViewToolbar

class AbiWordActivity (activity.Activity):

    def __init__ (self, handle):
        activity.Activity.__init__ (self, handle)
        self.set_title ("Write")

        # abiword uses the current directory for all its file dialogs 
        os.chdir(os.path.expanduser('~'))

        toolbox = activity.ActivityToolbox(self)
        self.set_toolbox(toolbox)
        toolbox.show()

        self._journal_handle = None
        self._last_saved_text = None

        # create our main abiword canvas
        self.abiword_canvas = Canvas()
        self.abiword_canvas.connect("can-undo", self._can_undo_cb)
        self.abiword_canvas.connect("can-redo", self._can_redo_cb)

        self._edit_toolbar = activity.EditToolbar()

        self._edit_toolbar.undo.set_sensitive(False)
        self._edit_toolbar.undo.connect('clicked', self._undo_cb)

        self._edit_toolbar.redo.set_sensitive(False)
        self._edit_toolbar.redo.connect('clicked', self._redo_cb)

        toolbox.add_toolbar(_('Edit'), self._edit_toolbar)
        self._edit_toolbar.show()

        text_toolbar = TextToolbar(self.abiword_canvas)
        toolbox.add_toolbar(_('Text'), text_toolbar)
        text_toolbar.show()

        image_toolbar = ImageToolbar(self.abiword_canvas)
        toolbox.add_toolbar(_('Image'), image_toolbar)
        image_toolbar.show()

        table_toolbar = TableToolbar(self.abiword_canvas)
        toolbox.add_toolbar(_('Table'), table_toolbar)
        table_toolbar.show()

        view_toolbar = ViewToolbar(self.abiword_canvas)
        toolbox.add_toolbar(_('View'), view_toolbar)
        view_toolbar.show()

        self.set_canvas(self.abiword_canvas)

        if handle.object_id:
            self._journal_handle = 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("")

        self.abiword_canvas.show()

        self.connect('focus-out-event', self._focus_out_event_cb)
        self.connect('delete-event', self._delete_event_cb)

    def _can_undo_cb(self, canvas, can_undo):
        self._edit_toolbar.undo.set_sensitive(can_undo)

    def _can_redo_cb(self, canvas, can_redo):
        self._edit_toolbar.redo.set_sensitive(can_redo)

    def _undo_cb(self, button):
        self.abiword_canvas.undo()

    def _redo_cb(self, button):
        self.abiword_canvas.redo()

    def _focus_out_event_cb(self, widget, event):
        self._autosave()

    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:60],
                         'date'         : str(time.time()),
                         'title'        : text_content[0:30],
                         '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)
            metadata = text.get_metadata()
            metadata['preview'] = text_content[0:60]
            metadata['title'] = text_content[0:30]
            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)

        self._last_saved_text = text_content