Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/AbiWordActivity.py
blob: 47467405c889ada0c6fea709927350110ed79b11 (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
# Copyright (C) 2006 by Martin Sevior
# Copyright (C) 2006-2007 Marc Maurer <uwog@uwog.net>
# Copyright (C) 2007, One Laptop Per Child
#
# 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 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)

        # FIXME: we need to load_file('') before show(), why?
        self.abiword_canvas.load_file('')
        self.abiword_canvas.show()

        if not self.jobject['title']:
            self.jobject['title'] = _('Text document')
        
        # FIXME: this should be called by activity.Activity on realize
        self.read_file()

    def read_file(self):
        logging.debug('AbiWordActivity.read_file')
        self.abiword_canvas.load_file('file://' + self.jobject.file_path)

    def write_file(self):
        text_content = self.abiword_canvas.get_content(".txt")[0]
        self.jobject['preview'] = text_content[0:60]
        self.jobject['icon'] = 'theme:object-text'
        f = open(self.jobject.file_path, 'w')
        try:
            f.write(self.abiword_canvas.get_content(".abw")[0])
        finally:
            f.close()

        self._last_saved_text = text_content

        return f.name

    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()