inline_bot_downloads.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "ui/chat/attach/attach_bot_webview.h"
  9. class webFileLoader;
  10. namespace Main {
  11. class Session;
  12. } // namespace Main
  13. namespace Ui {
  14. class GenericBox;
  15. } // namespace Ui
  16. namespace InlineBots {
  17. using DownloadId = uint32;
  18. using ::Ui::BotWebView::DownloadsProgress;
  19. using ::Ui::BotWebView::DownloadsEntry;
  20. using ::Ui::BotWebView::DownloadsAction;
  21. class Downloads final {
  22. public:
  23. explicit Downloads(not_null<Main::Session*> session);
  24. ~Downloads();
  25. struct StartArgs {
  26. not_null<UserData*> bot;
  27. QString url;
  28. QString path;
  29. };
  30. uint32 start(StartArgs &&args); // Returns download id.
  31. void action(
  32. not_null<UserData*> bot,
  33. DownloadId id,
  34. DownloadsAction type);
  35. [[nodiscard]] rpl::producer<DownloadsProgress> progress(
  36. not_null<UserData*> bot);
  37. [[nodiscard]] const std::vector<DownloadsEntry> &list(
  38. not_null<UserData*> bot,
  39. bool check = false);
  40. private:
  41. struct List {
  42. std::vector<DownloadsEntry> list;
  43. };
  44. struct Loader {
  45. std::unique_ptr<webFileLoader> loader;
  46. PeerId botId = 0;
  47. };
  48. void read();
  49. void write();
  50. void load(
  51. PeerId botId,
  52. DownloadId id,
  53. DownloadsEntry &entry);
  54. void progress(PeerId botId, DownloadId id);
  55. void fail(PeerId botId, DownloadId id, bool cancel = false);
  56. void done(PeerId botId, DownloadId id);
  57. void applyProgress(
  58. PeerId botId,
  59. DownloadId id,
  60. int64 total,
  61. int64 ready);
  62. void applyProgress(
  63. PeerId botId,
  64. DownloadsEntry &entry,
  65. int64 total,
  66. int64 ready);
  67. const not_null<Main::Session*> _session;
  68. base::flat_map<PeerId, List> _lists;
  69. base::flat_map<DownloadId, Loader> _loaders;
  70. base::flat_map<
  71. PeerId,
  72. rpl::variable<DownloadsProgress>> _progressView;
  73. DownloadId _autoIncrementId = 0;
  74. };
  75. struct DownloadBoxArgs {
  76. not_null<Main::Session*> session;
  77. QString bot;
  78. QString name;
  79. QString url;
  80. Fn<void(QString)> done;
  81. };
  82. void DownloadFileBox(not_null<Ui::GenericBox*> box, DownloadBoxArgs args);
  83. } // namespace InlineBots