Download.hpp 3.3 KB

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