export_view_content.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include "export/view/export_view_content.h"
  8. #include "export/export_settings.h"
  9. #include "lang/lang_keys.h"
  10. #include "ui/text/format_values.h"
  11. namespace Export {
  12. namespace View {
  13. const QString Content::kDoneId = "done";
  14. Content ContentFromState(
  15. not_null<Settings*> settings,
  16. const ProcessingState &state) {
  17. using Step = ProcessingState::Step;
  18. auto result = Content();
  19. const auto push = [&](
  20. const QString &id,
  21. const QString &label,
  22. const QString &info,
  23. float64 progress,
  24. uint64 randomId = 0) {
  25. result.rows.push_back({ id, label, info, progress, randomId });
  26. };
  27. const auto pushMain = [&](const QString &label) {
  28. const auto info = (state.entityCount > 0)
  29. ? (QString::number(state.entityIndex + 1)
  30. + " / "
  31. + QString::number(state.entityCount))
  32. : QString();
  33. if (!state.substepsTotal) {
  34. push("main", label, info, 0.);
  35. return;
  36. }
  37. const auto substepsTotal = state.substepsTotal;
  38. const auto done = state.substepsPassed;
  39. const auto add = state.substepsNow;
  40. const auto doneProgress = done / float64(substepsTotal);
  41. const auto addPart = [&](int index, int count) {
  42. return (count > 0)
  43. ? ((float64(add) * index)
  44. / (float64(substepsTotal) * count))
  45. : 0.;
  46. };
  47. const auto addProgress = (state.entityCount == 1
  48. && !state.entityIndex)
  49. ? addPart(state.itemIndex, state.itemCount)
  50. : addPart(state.entityIndex, state.entityCount);
  51. push("main", label, info, doneProgress + addProgress);
  52. };
  53. const auto pushBytes = [&](
  54. const QString &id,
  55. const QString &label,
  56. uint64 randomId) {
  57. if (!state.bytesCount) {
  58. return;
  59. }
  60. const auto progress = state.bytesLoaded / float64(state.bytesCount);
  61. const auto info = Ui::FormatDownloadText(
  62. state.bytesLoaded,
  63. state.bytesCount);
  64. push(id, label, info, progress, randomId);
  65. };
  66. switch (state.step) {
  67. case Step::Initializing:
  68. pushMain(tr::lng_export_state_initializing(tr::now));
  69. break;
  70. case Step::DialogsList:
  71. pushMain(tr::lng_export_state_chats_list(tr::now));
  72. break;
  73. case Step::PersonalInfo:
  74. pushMain(tr::lng_export_option_info(tr::now));
  75. break;
  76. case Step::Userpics:
  77. pushMain(tr::lng_export_state_userpics(tr::now));
  78. pushBytes(
  79. "userpic" + QString::number(state.entityIndex),
  80. state.bytesName,
  81. state.bytesRandomId);
  82. break;
  83. case Step::Contacts:
  84. pushMain(tr::lng_export_option_contacts(tr::now));
  85. break;
  86. case Step::Stories:
  87. pushMain(tr::lng_export_option_stories(tr::now));
  88. pushBytes(
  89. "story" + QString::number(state.entityIndex),
  90. state.bytesName,
  91. state.bytesRandomId);
  92. break;
  93. case Step::Sessions:
  94. pushMain(tr::lng_export_option_sessions(tr::now));
  95. break;
  96. case Step::OtherData:
  97. pushMain(tr::lng_export_option_other(tr::now));
  98. break;
  99. case Step::Dialogs:
  100. if (state.entityCount > 1) {
  101. pushMain(tr::lng_export_state_chats(tr::now));
  102. }
  103. push(
  104. "chat" + QString::number(state.entityIndex),
  105. (state.entityName.isEmpty()
  106. ? tr::lng_deleted(tr::now)
  107. : (state.entityType == ProcessingState::EntityType::Chat)
  108. ? state.entityName
  109. : (state.entityType == ProcessingState::EntityType::SavedMessages)
  110. ? tr::lng_saved_messages(tr::now)
  111. : tr::lng_replies_messages(tr::now)),
  112. (state.itemCount > 0
  113. ? (QString::number(state.itemIndex)
  114. + " / "
  115. + QString::number(state.itemCount))
  116. : QString()),
  117. (state.itemCount > 0
  118. ? (state.itemIndex / float64(state.itemCount))
  119. : 0.));
  120. pushBytes(
  121. ("file"
  122. + QString::number(state.entityIndex)
  123. + '_'
  124. + QString::number(state.itemIndex)),
  125. state.bytesName,
  126. state.bytesRandomId);
  127. break;
  128. default: Unexpected("Step in ContentFromState.");
  129. }
  130. const auto requiredRows = settings->onlySinglePeer() ? 2 : 3;
  131. while (result.rows.size() < requiredRows) {
  132. result.rows.emplace_back();
  133. }
  134. return result;
  135. }
  136. Content ContentFromState(const FinishedState &state) {
  137. auto result = Content();
  138. result.rows.push_back({
  139. Content::kDoneId,
  140. tr::lng_export_finished(tr::now),
  141. QString(),
  142. 1. });
  143. result.rows.push_back({
  144. Content::kDoneId,
  145. tr::lng_export_total_amount(
  146. tr::now,
  147. lt_amount,
  148. QString::number(state.filesCount)),
  149. QString(),
  150. 1. });
  151. result.rows.push_back({
  152. Content::kDoneId,
  153. tr::lng_export_total_size(
  154. tr::now,
  155. lt_size,
  156. Ui::FormatSizeText(state.bytesCount)),
  157. QString(),
  158. 1. });
  159. return result;
  160. }
  161. } // namespace View
  162. } // namespace Export