aboutsummaryrefslogtreecommitdiff
path: root/bin/odfrecode-gtk
blob: bd822c326c10b33fc3e61edfccd17e96c986090f (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
#!/usr/bin/python
# vim:encoding=utf-8:fileencoding=utf-8
#
# call the apropriate recoders for the unicode open document format converter
#
# Migrating from M* W*rd to ODF can be a pain if documents use characters from
# fonts that only support some non unicode encodings like armscii. Remap these
# to the appriopriate unicode codepoints.  Any corrections are very welcome.
#
# (c) 2007,2008,2009 Guido Günther <agx@sigxcpu.org>
# (c) 2008 Torsten Werner <twerner@debian.org>
#
#   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import sys, os, shutil
import odfrecode
from optparse import OptionParser
import gtk
import gnomevfs

fontmap = { "EastRoman": "DejaVu Sans" }

class ConvError(Exception):
    pass

class SuccessDialog(gtk.Dialog):
    def __init__(self, filename, backup):
        self.uri = "file://%s" % filename
        gtk.Dialog.__init__(self, buttons=(gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
        label = gtk.Label("succesfully converted to Unicode.")
        uri = gtk.LinkButton(self.uri, label=os.path.basename(filename))
        uri.connect('clicked', self.show_uri)
        uri.set_tooltip_text("Click to open '%s'" % filename)

        stock = gtk.image_new_from_stock(gtk.STOCK_DIALOG_INFO,
                                         gtk.ICON_SIZE_DIALOG)
        hbox = gtk.HBox(False, 3)
        hbox.pack_start(stock, True, True, 0)
        hbox.add(uri)
        hbox.add(label)
        self.vbox.add(hbox)
        self.vbox.add(gtk.Label("A backup copy was saved as '%s'."
                                % os.path.basename(backup)))
        self.show_all()

    def show_uri(self, dummy):
        gnomevfs.url_show(self.uri)
        self.destroy()

def main(argv):
    try:
        encoding = sys.getfilesystemencoding()
        parser = OptionParser()
        parser.add_option("--recoder", dest="recoder",
                          help="use recoder RECODER", metavar="recoder")
        (options, args) = parser.parse_args()

        if len(args) != 1:
           raise ConvError, "Need a file to convert."
        else:
            filename = os.path.abspath(args[0])
            try:
                fileinfo = gnomevfs.get_file_info(filename, gnomevfs.FILE_INFO_GET_MIME_TYPE)
            except gnomevfs.NotFoundError:
                raise ConvError, "'%s' nicht gefunden" % filename
            if fileinfo.mime_type not in [ "application/vnd.oasis.opendocument.text",
                                           "application/vnd.oasis.opendocument.text-template" ]:
                 raise ConvError, "'%s' is no OpenDocument Text, but '%s'." % (filename, fileinfo.mime_type)

        if not options.recoder:
           # FIXME: list available recoders
           raise ConvError, "Missing converter.\nSupported converters are: '%s'" % \
                                ", ".join(odfrecode.get_recoders().keys())
        else:
            try:
                converter = odfrecode.get_recoder(options.recoder)
            except KeyError:
                raise ConvError, "No recoder for '%s' found." % options.recoder

        prefix, postfix = filename.decode(encoding).rsplit(u'.', 1)
        backup = u"%s.%s.%s" % (prefix, options.recoder, postfix)
        try:
            os.unlink(backup)
        except OSError:
            pass # file doesn't exist
        shutil.copy(filename, backup)

        dstname = odfrecode.to_utf8(filename, converter, fontmap)
        os.unlink(filename)
        shutil.copy(dstname, filename)
        os.unlink(dstname)
        dialog = SuccessDialog(filename, backup)
    except ConvError, error:
        dialog = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK,
                                   message_format="Error during conversion")
        dialog.format_secondary_text(str(error))
    dialog.set_property("title", "ODF Charset Converter")
    dialog.run()
    dialog.destroy()

if __name__ == "__main__":
   main(sys.argv)

# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: