Codebase list sugar-read-activity / 49824f7
#7948, #7168, #6488 Fix saving to the datastore Morgan Collett 15 years ago
2 changed file(s) with 40 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
0 * #7948, #7168, #6488 Fix saving to the datastore (morgs)
1
02 49
13
24 * New and updated translations
139139
140140 self.unused_download_tubes = set()
141141 self._want_document = True
142 # Status of temp file used for write_file:
143 self._tempfile = None
144 self._close_requested = False
142145
143146 fname = os.path.join('/etc', 'inhibit-ebook-sleep')
144147 if not os.path.exists(fname):
223226 def read_file(self, file_path):
224227 """Load a file from the datastore on activity start."""
225228 _logger.debug('ReadActivity.read_file: %s', file_path)
226 self._load_document('file://' + file_path)
229 tempfile = os.path.join(self.get_activity_root(), 'instance',
230 'tmp%i' % time.time())
231 os.link(file_path, tempfile)
232 self._tempfile = tempfile
233 self._load_document('file://' + self._tempfile)
227234
228235 # FIXME: This should obviously be fixed properly
229236 gobject.timeout_add(1000, self.__view_toolbar_needs_update_size_cb,
230237 None)
231238
232239 def write_file(self, file_path):
233 """Write metadata into datastore for Keep.
240 """Write into datastore for Keep.
234241
235 We only save meta data, not the document itself.
236 current page, view settings, search text.
242 The document is saved by hardlinking from the temporary file we
243 keep around instead of "saving".
244
245 The metadata is updated, including current page, view settings,
246 search text.
237247
238248 """
239249 try:
262272 self.metadata['Read_search'] = \
263273 self._edit_toolbar._search_entry.props.text
264274
275 os.link(self._tempfile, file_path)
276
277 if self._close_requested:
278 _logger.debug("Removing temp file %s because we will close",
279 self._tempfile)
280 os.unlink(self._tempfile)
281 self._tempfile = None
282
283 def can_close(self):
284 """Prepare to cleanup on closing.
285
286 Called from self.close()
287 """
288 self._close_requested = True
289 return True
290
265291 def _download_result_cb(self, getter, tempfile, suggested_name, tube_id):
266292 del self.unused_download_tubes
267293
268 _logger.debug("Moving file %s to datastore...", tempfile)
269 self._jobject.file_path = tempfile
294 self._tempfile = tempfile
295 file_path = os.path.join(self.get_activity_root(), 'instance',
296 '%i' % time.time())
297 _logger.debug("Saving file %s to datastore...", file_path)
298 os.link(tempfile, file_path)
299 self._jobject.file_path = file_path
270300 datastore.write(self._jobject, transfer_ownership=True)
271301
272302 _logger.debug("Got document %s (%s) from tube %u",
273303 tempfile, suggested_name, tube_id)
274304 self._load_document("file://%s" % tempfile)
275
276305 self.save()
277306
278307 def _download_progress_cb(self, getter, bytes_downloaded, tube_id):
321350 # Assign a file path to download if one doesn't exist yet
322351 if not self._jobject.file_path:
323352 path = os.path.join(self.get_activity_root(), 'instance',
324 '%i' % time.time())
353 'tmp%i' % time.time())
325354 else:
326355 path = self._jobject.file_path
327356
403432 # FIXME: should ideally have the fileserver listen on a Unix socket
404433 # instead of IPv4 (might be more compatible with Rainbow)
405434
406 # FIXME: there is an issue with the Activity class and Read that makes
407 # the pdf file disappear; probably related to write_file not writing a
408 # file. This is a dirty fix and should be improved later.
409 if self._jobject is None:
410 self._jobject = datastore.get(self._object_id)
411 elif not os.path.exists(self._jobject.get_file_path()):
412 _logger.debug('_jobject file does not exists; getting from DS...')
413 self._jobject.destroy()
414 self._jobject = datastore.get(self._object_id)
415
416435 logging.debug('Starting HTTP server on port %d', self.port)
417436 self._fileserver = ReadHTTPServer(("", self.port),
418 self._jobject.get_file_path())
437 self._tempfile)
419438
420439 # Make a tube for it
421440 chan = self._shared_activity.telepathy_tubes_chan