Statistics
| Revision:

root / trunk / gummi / IOFunctions.py @ 299

History | View | Annotate | Download (4.3 KB)

1
#!/usr/bin/python
2
# -*- encoding: utf-8 -*-
3
4
# Copyright (c) 2009 Alexander van der Mey <alexvandermey@gmail.com>
5
6
# Permission is hereby granted, free of charge, to any person obtaining a copy
7
# of this software and associated documentation files (the "Software"), to deal
8
# in the Software without restriction, including without limitation the rights
9
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
# copies of the Software, and to permit persons to whom the Software is
11
# furnished to do so, subject to the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be included in
14
# all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
# THE SOFTWARE.
23
24
import os
25
import glib
26
import shutil
27
import tempfile
28
29
30
class IOFunctions:
31
        
32
        def __init__(self, config, statusbar, editorpane, motion):
33
                
34
                self.config = config
35
                self.editorpane = editorpane
36
                self.motion = motion
37
38
                self.statusbar = statusbar
39
                self.statusbar_cid = self.statusbar.get_context_id("Gummi")
40
41
                self.tempdir = os.environ.get("TMPDIR", "/tmp")
42
                self.filename = None
43
                self.texpath = None
44
                self.workfile = None
45
                self.workfd = None
46
                self.pdffile = None
47
48
        def make_environment(self, filename=None):
49
                if self.workfd: # close previous workfile
50
                        os.close(self.workfd)
51
                self.filename = filename
52
                self.create_envfiles(filename)
53
                env = self.return_envfiles()
54
                self.motion.update_envfiles(env)
55
                self.motion.initial_preview()
56
                if self.config.get_value("editor", "autosaving"):                
57
                        self.reset_autosave()
58
59
        def create_envfiles(self, filename):
60
                if filename is not None:
61
                        self.filename = filename
62
                        self.texpath = os.path.dirname(self.filename) + "/"
63
                        if ".tex" in self.filename:
64
                                self.texname = os.path.basename(self.filename)[:-4]
65
                        else:
66
                                self.texname = os.path.basename(self.filename)
67
                (self.workfd, self.workfile) = tempfile.mkstemp(".tex")
68
                self.pdffile = self.workfile[:-4] + ".pdf"
69
                print ("\nEnvironment created for: \n" + \
70
                                "TEX: " + str(self.filename) + "\n" \
71
                                "TMP: " + self.workfile + "\n" + \
72
                                "PDF: " + self.pdffile + "\n")
73
74
        def load_default_text(self):
75
                self.editorpane.fill_buffer \
76
                        (self.config.get_value("default_text", "welcome"))
77
                os.chdir(os.environ['HOME'])
78
79
        def load_file(self, filename):
80
                try:
81
                        decode = self.editorpane.decode_text(filename)
82
                        self.editorpane.fill_buffer(decode)
83
                        self.filename = filename
84
                        self.make_environment(self.filename)
85
                        self.set_status("Loading file " + self.filename)
86
87
                except: print "load_file failed"
88
89
        def save_file(self, filename):
90
                try:                
91
                        content = self.editorpane.grab_buffer()
92
                        self.editorpane.editorviewer.grab_focus()
93
                        encoded = self.editorpane.encode_text(content)
94
                        self.set_status("Saving file " + self.filename)
95
                        fout = open(self.filename, "w")
96
                        fout.write(encoded)
97
                        fout.close()
98
                except: print "save_file failed"
99
100
        def export_pdffile(self):
101
                try:
102
                        export = self.texpath + self.texname + ".pdf"
103
                        shutil.copy2(self.pdffile, export)
104
                        os.chdir(self.texpath)
105
                except IOError: pass
106
107
        def start_autosave(self, time):
108
                self.autosave = glib.timeout_add_seconds(time, self.autosave_document)
109
110
        def stop_autosave(self):
111
                try:
112
                        glib.source_remove(self.autosave)
113
                except AttributeError: pass 
114
115
        def reset_autosave(self):
116
                self.stop_autosave()
117
                time = int(self.config.get_value("editor", "autosave_timer"))
118
                self.start_autosave(time)
119
120
        def autosave_document(self):
121
                if self.filename is not None:
122
                        self.save_file(self.filename)
123
                        self.set_status("Autosaving file " + self.filename)
124
                return True
125
126
        def set_status(self, message):
127
                self.statusbar.push(self.statusbar_cid, message)
128
                glib.timeout_add(4000, self.remove_status)
129
130
        def remove_status(self):
131
                self.statusbar.push(self.statusbar_cid, "")
132
133
        def filename(self):
134
                return self.filename
135
136
        def return_envfiles(self):
137
                return self.tempdir, \
138
                                self.filename, \
139
                                self.texpath, \
140
                                self.workfile, \
141
                                self.pdffile
142
143
144
145