DownloadQueue.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef FREESHOP_DOWNLOADQUEUE_HPP
  2. #define FREESHOP_DOWNLOADQUEUE_HPP
  3. #include <bits/unique_ptr.h>
  4. #include <cpp3ds/Window/Event.hpp>
  5. #include <TweenEngine/TweenManager.h>
  6. #include <cpp3ds/Audio/Sound.hpp>
  7. #include <cpp3ds/Audio/SoundBuffer.hpp>
  8. #include "Download.hpp"
  9. #include "GUI/AppItem.hpp"
  10. #include "Installer.hpp"
  11. #include "GUI/Scrollable.hpp"
  12. namespace FreeShop {
  13. typedef std::function<void(bool)> DownloadCompleteCallback;
  14. struct DownloadItem {
  15. DownloadItem(std::shared_ptr<AppItem> appItem, Download *download, Installer *installer);
  16. ~DownloadItem();
  17. std::shared_ptr<AppItem> appItem;
  18. Download *download;
  19. Installer *installer;
  20. };
  21. class DownloadQueue : public cpp3ds::Drawable, public Scrollable, public util3ds::TweenTransformable<cpp3ds::Transformable> {
  22. public:
  23. ~DownloadQueue();
  24. static DownloadQueue &getInstance();
  25. void addDownload(std::shared_ptr<AppItem> app, cpp3ds::Uint64 titleId = 0, DownloadCompleteCallback callback = nullptr, int contentIndex = -1, float progress = 0.f);
  26. void restartDownload(Download *download);
  27. void addSleepDownload(std::shared_ptr<AppItem> app, cpp3ds::Uint64 titleId = 0, cpp3ds::String title = "");
  28. bool isDownloading(std::shared_ptr<AppItem> app);
  29. bool isDownloading(cpp3ds::Uint64 titleId);
  30. void suspend();
  31. void resume();
  32. void save();
  33. void sendTop(Download* download);
  34. size_t getCount();
  35. size_t getActiveCount();
  36. void update(float delta);
  37. bool processEvent(const cpp3ds::Event& event);
  38. virtual void setScroll(float position);
  39. virtual float getScroll();
  40. virtual const cpp3ds::Vector2f &getScrollSize();
  41. protected:
  42. DownloadQueue();
  43. void load();
  44. void realign();
  45. virtual void draw(cpp3ds::RenderTarget& target, cpp3ds::RenderStates states) const;
  46. // Go through list make sure only top item is downloading and rest are suspended if necessary
  47. void refresh();
  48. private:
  49. std::vector<std::unique_ptr<DownloadItem>> m_downloads;
  50. TweenEngine::TweenManager m_tweenManager;
  51. cpp3ds::SoundBuffer m_soundBufferFinish;
  52. cpp3ds::Sound m_soundFinish;
  53. float m_scrollPos;
  54. cpp3ds::Vector2f m_size;
  55. cpp3ds::Thread m_threadRefresh;
  56. cpp3ds::Mutex m_mutexRefresh;
  57. cpp3ds::Mutex m_mutexSleepInstall;
  58. cpp3ds::Clock m_clockRefresh;
  59. volatile bool m_refreshEnd;
  60. //Installer for sleep download
  61. Installer *m_sleepInstaller;
  62. bool m_isSleepInstalling;
  63. };
  64. } // namespace FreeShop
  65. #endif // FREESHOP_DOWNLOADQUEUE_HPP