Codebase list sugar-read-activity / 2058240
Save modified epub files When text is highlightedd, the pages are modified, and need be saved. Gonzalo Odiard 9 years ago
3 changed file(s) with 79 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
33 import epubview
44
55 # import speech
6 import os
67
78 from cStringIO import StringIO
89
2122
2223 activity._hbox.pack_start(self, True, True, 0)
2324 self.show_all()
25 self._modified_files = []
26
2427 # text to speech initialization
2528 self.current_word = 0
2629 self.word_tuples = []
100103 self._view.execute_script(js)
101104
102105 self._view.set_editable(False)
106 # mark the file as modified
107 current_file = self.get_current_file()
108 logging.error('file %s was modified', current_file)
109 if current_file not in self._modified_files:
110 self._modified_files.append(current_file)
111 GObject.idle_add(self._save_page)
112
113 def _save_page(self):
114 oldtitle = self._view.get_title()
115 self._view.execute_script(
116 "document.title=document.documentElement.innerHTML;")
117 html = self._view.get_title()
118 file_path = self.get_current_file().replace('file:///', '/')
119 logging.error(html)
120 with open(file_path, 'w') as fd:
121 header = """<?xml version="1.0" encoding="utf-8" standalone="no"?>
122 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
123 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
124 <html xmlns="http://www.w3.org/1999/xhtml">"""
125 fd.write(header)
126 fd.write(html)
127 fd.write('</html>')
128 self._view.execute_script('document.title=%s;' % oldtitle)
129
130 def save(self, file_path):
131 if self._modified_files:
132 self._epub.write(file_path)
133 return True
134
135 return False
103136
104137 def in_highlight(self):
105138 # Verify if the selection already exist or the cursor
155155 '''
156156 return self._info.title
157157
158 def write(self, file_path):
159 '''Create the ZIP archive.
160 The mimetype must be the first file in the archive
161 and it must not be compressed.'''
162
163 # The EPUB must contain the META-INF and mimetype files at the root, so
164 # we'll create the archive in the working directory first
165 # and move it later
166 current_dir = os.getcwd()
167 os.chdir(self._tempdir)
168
169 # Open a new zipfile for writing
170 epub = zipfile.ZipFile(file_path, 'w')
171
172 # Add the mimetype file first and set it to be uncompressed
173 epub.write('mimetype', compress_type=zipfile.ZIP_STORED)
174
175 # For the remaining paths in the EPUB, add all of their files
176 # using normal ZIP compression
177 self._scan_dir('.', epub)
178
179 epub.close()
180 os.chdir(current_dir)
181
182 def _scan_dir(self, path, epub_file):
183 for p in os.listdir(path):
184 logging.error('add file %s', p)
185 if os.path.isdir(os.path.join(path, p)):
186 self._scan_dir(os.path.join(path, p), epub_file)
187 else:
188 if p != 'mimetype':
189 epub_file.write(
190 os.path.join(path, p),
191 compress_type=zipfile.ZIP_DEFLATED)
192
158193 def close(self):
159194 '''
160195 Cleans up (closes open zip files and deletes
764764 self._edit_toolbar._search_entry.props.text
765765 self.metadata['activity'] = self.get_bundle_id()
766766
767 os.link(self._tempfile, file_path)
767 # the file is only saved if modified
768 saved = False
769 if hasattr(self._view, 'save'):
770 saved = self._view.save(file_path)
771
772 if saved:
773 self.filehash = get_md5(file_path)
774 self.metadata['filehash'] = self.filehash
775 else:
776 os.link(self._tempfile, file_path)
777
768778 if self.filehash is None:
769779 self.filehash = get_md5(file_path)
770780 self.metadata['filehash'] = self.filehash