|
28 | 28 | from gettext import gettext as _ |
29 | 29 | from PyQt5.QtCore import QObject, QThread, pyqtSignal, QTimer, QProcess |
30 | 30 | from sdclientapi import RequestTimeoutError |
31 | | -from typing import Dict, Tuple # noqa: F401 |
| 31 | +from typing import Dict, Tuple, Union # noqa: F401 |
32 | 32 |
|
33 | 33 | from securedrop_client import storage |
34 | 34 | from securedrop_client import db |
35 | | -from securedrop_client.utils import check_dir_permissions |
36 | 35 | from securedrop_client.crypto import GpgHelper, CryptoError |
37 | 36 | from securedrop_client.message_sync import MessageSync, ReplySync |
| 37 | +from securedrop_client.queue import ApiJobQueue, DownloadSubmissionJob |
| 38 | +from securedrop_client.utils import check_dir_permissions |
38 | 39 |
|
39 | 40 | logger = logging.getLogger(__name__) |
40 | 41 |
|
@@ -144,6 +145,10 @@ def __init__(self, hostname, gui, session, |
144 | 145 |
|
145 | 146 | # Reference to the API for secure drop proxy. |
146 | 147 | self.api = None # type: sdclientapi.API |
| 148 | + |
| 149 | + # Queue that handles running API job |
| 150 | + self.api_job_queue = ApiJobQueue(self.api, self) |
| 151 | + |
147 | 152 | # Contains active threads calling the API. |
148 | 153 | self.api_threads = {} # type: Dict[str, Dict] |
149 | 154 |
|
@@ -310,6 +315,7 @@ def on_authenticate_success(self, result): |
310 | 315 | self.gui.show_main_window(self.api.username) |
311 | 316 | self.start_message_thread() |
312 | 317 | self.start_reply_thread() |
| 318 | + self.api_job_queue.start_queues() # TODO <------------------- this is wrong somehow? |
313 | 319 |
|
314 | 320 | # Clear the sidebar error status bar if a message was shown |
315 | 321 | # to the user indicating they should log in. |
@@ -508,35 +514,47 @@ def on_file_open(self, file_db_object): |
508 | 514 | # Non Qubes OS. Just log the event for now. |
509 | 515 | logger.info('Opening file "{}".'.format(submission_filepath)) |
510 | 516 |
|
511 | | - def on_file_download(self, source_db_object, message): |
| 517 | + def on_reply_download(self, source_db_object: db.Source, reply: db.Reply) -> None: |
512 | 518 | """ |
513 | | - Download the file associated with the associated message (which may |
514 | | - be a Submission or Reply). |
| 519 | + Download the file associated with the Reply. |
515 | 520 | """ |
516 | 521 | if not self.api: # Then we should tell the user they need to login. |
517 | 522 | self.on_action_requiring_login() |
518 | 523 | return |
519 | 524 |
|
520 | | - if isinstance(message, db.File) or isinstance(message, db.Message): |
521 | | - # Handle submissions. |
522 | | - func = self.api.download_submission |
523 | | - sdk_object = sdclientapi.Submission(uuid=message.uuid) |
524 | | - sdk_object.filename = message.filename |
525 | | - sdk_object.source_uuid = source_db_object.uuid |
526 | | - elif isinstance(message, db.Reply): |
527 | | - # Handle journalist's replies. |
528 | | - func = self.api.download_reply |
529 | | - sdk_object = sdclientapi.Reply(uuid=message.uuid) |
530 | | - sdk_object.filename = message.filename |
531 | | - sdk_object.source_uuid = source_db_object.uuid |
| 525 | + sdk_object = sdclientapi.Reply(uuid=reply.uuid) |
| 526 | + sdk_object.filename = reply.filename |
| 527 | + sdk_object.source_uuid = source_db_object.uuid |
532 | 528 |
|
533 | 529 | self.set_status(_('Downloading {}'.format(sdk_object.filename))) |
534 | | - self.call_api(func, |
| 530 | + |
| 531 | + self.call_api(self.api.download_reply, |
535 | 532 | self.on_file_download_success, |
536 | 533 | self.on_file_download_failure, |
537 | 534 | sdk_object, |
538 | 535 | self.data_dir, |
539 | | - current_object=message) |
| 536 | + current_object=reply) |
| 537 | + |
| 538 | + def on_submission_download( |
| 539 | + self, |
| 540 | + source_db_object: db.Source, |
| 541 | + submission: Union[db.File, db.Message], |
| 542 | + ) -> None: |
| 543 | + """ |
| 544 | + Download the file associated with the Submission (which may be a File or Message). |
| 545 | + """ |
| 546 | + print('on sub') |
| 547 | + if not self.api: # Then we should tell the user they need to login. |
| 548 | + self.on_action_requiring_login() |
| 549 | + return |
| 550 | + |
| 551 | + sdk_object = sdclientapi.Submission(uuid=submission.uuid) |
| 552 | + sdk_object.filename = submission.filename |
| 553 | + sdk_object.source_uuid = source_db_object.uuid |
| 554 | + |
| 555 | + job = DownloadSubmissionJob(sdk_object, self.data_dir, submission) |
| 556 | + self.api_job_queue.enqueue(job) |
| 557 | + self.set_status(_('Downloading {}'.format(sdk_object.filename))) |
540 | 558 |
|
541 | 559 | def on_file_download_success(self, result, current_object): |
542 | 560 | """ |
|
0 commit comments