James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <thread> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <wpi/condition_variable.h> |
| 13 | #include <wpi/mutex.h> |
| 14 | |
| 15 | namespace glass { |
| 16 | class Storage; |
| 17 | } // namespace glass |
| 18 | |
| 19 | namespace pfd { |
| 20 | class select_folder; |
| 21 | } // namespace pfd |
| 22 | |
| 23 | class Downloader { |
| 24 | public: |
| 25 | explicit Downloader(glass::Storage& storage); |
| 26 | ~Downloader(); |
| 27 | |
| 28 | void Display(); |
| 29 | |
| 30 | private: |
| 31 | void DisplayConnect(); |
| 32 | void DisplayDisconnectButton(); |
| 33 | void DisplayRemoteDirSelector(); |
| 34 | void DisplayLocalDirSelector(); |
| 35 | size_t DisplayFiles(); |
| 36 | |
| 37 | void ThreadMain(); |
| 38 | |
| 39 | wpi::mutex m_mutex; |
| 40 | enum State { |
| 41 | kDisconnected, |
| 42 | kConnecting, |
| 43 | kConnected, |
| 44 | kDisconnecting, |
| 45 | kGetFiles, |
| 46 | kDownload, |
| 47 | kDownloadDone, |
| 48 | kExit |
| 49 | } m_state = kDisconnected; |
| 50 | std::condition_variable m_cv; |
| 51 | |
| 52 | std::string& m_serverTeam; |
| 53 | std::string& m_remoteDir; |
| 54 | std::string& m_username; |
| 55 | std::string m_password; |
| 56 | |
| 57 | std::string& m_localDir; |
| 58 | std::unique_ptr<pfd::select_folder> m_localDirSelector; |
| 59 | |
| 60 | bool& m_deleteAfter; |
| 61 | |
| 62 | std::vector<std::string> m_dirList; |
| 63 | struct DownloadState { |
| 64 | DownloadState(std::string_view name, uint64_t size) |
| 65 | : name{name}, size{size} {} |
| 66 | |
| 67 | std::string name; |
| 68 | uint64_t size; |
| 69 | bool enabled = true; |
| 70 | float complete = 0.0; |
| 71 | std::string status; |
| 72 | }; |
| 73 | std::vector<DownloadState> m_downloadList; |
| 74 | |
| 75 | std::string m_error; |
| 76 | |
| 77 | std::thread m_thread; |
| 78 | }; |