Download.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef FREESHOP_DOWNLOAD_HPP
  2. #define FREESHOP_DOWNLOAD_HPP
  3. #include <functional>
  4. #include <queue>
  5. #include <cpp3ds/System/Thread.hpp>
  6. #include <cpp3ds/Network/Http.hpp>
  7. #include <cpp3ds/System/Clock.hpp>
  8. #include <cpp3ds/Graphics/Sprite.hpp>
  9. #include <cpp3ds/Window/Event.hpp>
  10. #include <cpp3ds/System/Mutex.hpp>
  11. #include "TweenObjects.hpp"
  12. #include "GUI/NinePatch.hpp"
  13. #include "AppItem.hpp"
  14. namespace FreeShop {
  15. class Download;
  16. typedef std::function<bool(const void*,size_t,size_t)> DownloadDataCallback;
  17. typedef std::function<void(bool,bool)> DownloadFinishCallback;
  18. typedef std::function<void(Download*)> SendTopCallback;
  19. class Download : public cpp3ds::Drawable, public util3ds::TweenTransformable<cpp3ds::Transformable>{
  20. friend class DownloadQueue;
  21. public:
  22. enum Status {
  23. Queued,
  24. Downloading,
  25. Finished,
  26. Failed,
  27. Canceled,
  28. Suspended,
  29. };
  30. Download(const std::string& url, const std::string& destination = std::string());
  31. ~Download();
  32. void setProgressMessage(const std::string& message);
  33. const std::string& getProgressMessage() const;
  34. void setProgress(float progress);
  35. float getProgress() const;
  36. const cpp3ds::Vector2f &getSize() const;
  37. void fillFromAppItem(std::shared_ptr<AppItem> app);
  38. void processEvent(const cpp3ds::Event& event);
  39. bool setUrl(const std::string& url);
  40. void setDestination(const std::string& destination);
  41. void pushUrl(const std::string& url, cpp3ds::Uint64 position = 0);
  42. void start();
  43. void setDataCallback(cpp3ds::Http::RequestCallback onData);
  44. void setFinishCallback(DownloadFinishCallback onFinish);
  45. void setSendTopCallback(SendTopCallback onSendTop);
  46. void setField(const std::string &field, const std::string &value);
  47. void setRetryCount(int timesToRetry);
  48. void run();
  49. void suspend();
  50. void resume();
  51. void cancel(bool wait = true);
  52. bool isCanceled();
  53. Status getStatus() const;
  54. const cpp3ds::Http::Response &getLastResponse() const;
  55. void setTimeout(cpp3ds::Time timeout);
  56. void setBufferSize(size_t size);
  57. bool markedForDelete();
  58. protected:
  59. virtual void draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const;
  60. private:
  61. cpp3ds::Http::Response m_response;
  62. cpp3ds::Http m_http;
  63. cpp3ds::Http::Request m_request;
  64. std::string m_host;
  65. std::string m_uri;
  66. std::string m_destination;
  67. std::queue<std::pair<std::string, cpp3ds::Uint64>> m_urlQueue;
  68. cpp3ds::Http::RequestCallback m_onData;
  69. DownloadFinishCallback m_onFinish;
  70. cpp3ds::Thread m_thread;
  71. std::shared_ptr<AppItem> m_appItem;
  72. cpp3ds::Mutex m_mutex;
  73. Status m_status;
  74. bool m_canSendTop;
  75. bool m_cancelFlag;
  76. bool m_markedForDelete;
  77. SendTopCallback m_onSendTop;
  78. FILE* m_file;
  79. std::vector<char> m_buffer;
  80. cpp3ds::Time m_timeout;
  81. size_t m_bufferSize;
  82. // For queue UI
  83. gui3ds::NinePatch m_background;
  84. cpp3ds::RectangleShape m_icon;
  85. cpp3ds::RectangleShape m_progressBar;
  86. cpp3ds::Text m_textTitle;
  87. cpp3ds::Text m_textProgress;
  88. util3ds::TweenText m_textCancel;
  89. util3ds::TweenText m_textSendTop;
  90. util3ds::TweenText m_textRestart;
  91. size_t m_downloadPos;
  92. float m_progress;
  93. int m_timesToRetry;
  94. cpp3ds::Vector2f m_size;
  95. std::string m_progressMessage;
  96. };
  97. } // namespace FreeShop
  98. #endif // FREESHOP_DOWNLOAD_HPP