139 | 139 |
|
140 | 140 |
self.unused_download_tubes = set()
|
141 | 141 |
self._want_document = True
|
|
142 |
# Status of temp file used for write_file:
|
|
143 |
self._tempfile = None
|
|
144 |
self._close_requested = False
|
142 | 145 |
|
143 | 146 |
fname = os.path.join('/etc', 'inhibit-ebook-sleep')
|
144 | 147 |
if not os.path.exists(fname):
|
|
223 | 226 |
def read_file(self, file_path):
|
224 | 227 |
"""Load a file from the datastore on activity start."""
|
225 | 228 |
_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)
|
227 | 234 |
|
228 | 235 |
# FIXME: This should obviously be fixed properly
|
229 | 236 |
gobject.timeout_add(1000, self.__view_toolbar_needs_update_size_cb,
|
230 | 237 |
None)
|
231 | 238 |
|
232 | 239 |
def write_file(self, file_path):
|
233 | |
"""Write metadata into datastore for Keep.
|
|
240 |
"""Write into datastore for Keep.
|
234 | 241 |
|
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.
|
237 | 247 |
|
238 | 248 |
"""
|
239 | 249 |
try:
|
|
262 | 272 |
self.metadata['Read_search'] = \
|
263 | 273 |
self._edit_toolbar._search_entry.props.text
|
264 | 274 |
|
|
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 |
|
265 | 291 |
def _download_result_cb(self, getter, tempfile, suggested_name, tube_id):
|
266 | 292 |
del self.unused_download_tubes
|
267 | 293 |
|
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
|
270 | 300 |
datastore.write(self._jobject, transfer_ownership=True)
|
271 | 301 |
|
272 | 302 |
_logger.debug("Got document %s (%s) from tube %u",
|
273 | 303 |
tempfile, suggested_name, tube_id)
|
274 | 304 |
self._load_document("file://%s" % tempfile)
|
275 | |
|
276 | 305 |
self.save()
|
277 | 306 |
|
278 | 307 |
def _download_progress_cb(self, getter, bytes_downloaded, tube_id):
|
|
321 | 350 |
# Assign a file path to download if one doesn't exist yet
|
322 | 351 |
if not self._jobject.file_path:
|
323 | 352 |
path = os.path.join(self.get_activity_root(), 'instance',
|
324 | |
'%i' % time.time())
|
|
353 |
'tmp%i' % time.time())
|
325 | 354 |
else:
|
326 | 355 |
path = self._jobject.file_path
|
327 | 356 |
|
|
403 | 432 |
# FIXME: should ideally have the fileserver listen on a Unix socket
|
404 | 433 |
# instead of IPv4 (might be more compatible with Rainbow)
|
405 | 434 |
|
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 | |
|
416 | 435 |
logging.debug('Starting HTTP server on port %d', self.port)
|
417 | 436 |
self._fileserver = ReadHTTPServer(("", self.port),
|
418 | |
self._jobject.get_file_path())
|
|
437 |
self._tempfile)
|
419 | 438 |
|
420 | 439 |
# Make a tube for it
|
421 | 440 |
chan = self._shared_activity.telepathy_tubes_chan
|