diff --git a/Telegram/SourceFiles/export/output/export_output_text.cpp b/Telegram/SourceFiles/export/output/export_output_text.cpp index 9fb4d5fcc8e2c6..e69d364954afaa 100644 --- a/Telegram/SourceFiles/export/output/export_output_text.cpp +++ b/Telegram/SourceFiles/export/output/export_output_text.cpp @@ -676,7 +676,7 @@ Result TextWriter::writeDialogEnd() { } Result TextWriter::writeDialogsEnd() { - return Result::Success(); + return writeChatsEnd(); } Result TextWriter::writeLeftChannelsStart(const Data::DialogsInfo &data) { @@ -704,61 +704,16 @@ Result TextWriter::writeChatsStart( const QByteArray &listName, const QString &fileName) { Expects(_summary != nullptr); + Expects(_chats == nullptr); if (data.list.empty()) { return Result::Success(); } + _chats = fileWithRelativePath(fileName); _dialogIndex = 0; _dialogsCount = data.list.size(); - using Type = Data::DialogInfo::Type; - const auto TypeString = [](Type type) { - switch (type) { - case Type::Unknown: return "(unknown)"; - case Type::Personal: return "Personal chat"; - case Type::Bot: return "Bot chat"; - case Type::PrivateGroup: return "Private group"; - case Type::PublicGroup: return "Public group"; - case Type::PrivateChannel: return "Private channel"; - case Type::PublicChannel: return "Private channel"; - } - Unexpected("Dialog type in TypeString."); - }; - const auto NameString = []( - const Data::Utf8String &name, - Type type) -> QByteArray { - if (!name.isEmpty()) { - return name; - } - switch (type) { - case Type::Unknown: return "(unknown)"; - case Type::Personal: return "(deleted user)"; - case Type::Bot: return "(deleted bot)"; - case Type::PrivateGroup: - case Type::PublicGroup: return "(deleted group)"; - case Type::PrivateChannel: - case Type::PublicChannel: return "(deleted channel)"; - } - Unexpected("Dialog type in TypeString."); - }; - const auto file = fileWithRelativePath(fileName); - auto list = std::vector(); - list.reserve(data.list.size()); - auto index = 0; - for (const auto &dialog : data.list) { - const auto path = dialog.relativePath + "messages.txt"; - list.push_back(SerializeKeyValue({ - { "Name", NameString(dialog.name, dialog.type) }, - { "Type", TypeString(dialog.type) }, - { "Content", path.toUtf8() } - })); - } - const auto full = JoinList(kLineBreak, list); - if (const auto result = file->writeBlock(full); !result) { - return result; - } - const auto header = listName + " " "(" + Data::NumberToString(data.list.size()) + ") - " + fileName.toUtf8() @@ -774,8 +729,8 @@ Result TextWriter::writeChatStart(const Data::DialogInfo &data) { const auto digits = Data::NumberToString(_dialogsCount - 1).size(); const auto number = Data::NumberToString(++_dialogIndex, digits, '0'); _chat = fileWithRelativePath(data.relativePath + "messages.txt"); - _dialogEmpty = true; - _dialogOnlyMy = data.onlyMyMessages; + _messagesCount = 0; + _dialog = data; return Result::Success(); } @@ -783,10 +738,9 @@ Result TextWriter::writeChatSlice(const Data::MessagesSlice &data) { Expects(_chat != nullptr); Expects(!data.list.empty()); - _dialogEmpty = false; + _messagesCount += data.list.size(); auto list = std::vector(); list.reserve(data.list.size()); - auto index = 0; for (const auto &message : data.list) { list.push_back(SerializeMessage( message, @@ -800,17 +754,58 @@ Result TextWriter::writeChatSlice(const Data::MessagesSlice &data) { } Result TextWriter::writeChatEnd() { + Expects(_chats != nullptr); Expects(_chat != nullptr); - if (_dialogEmpty) { - const auto result = _chat->writeBlock(_dialogOnlyMy - ? "No outgoing messages in this chat." - : "No messages in this chat."); - if (!result) { - return result; - } - } _chat = nullptr; + + using Type = Data::DialogInfo::Type; + const auto TypeString = [](Type type) { + switch (type) { + case Type::Unknown: return "(unknown)"; + case Type::Personal: return "Personal chat"; + case Type::Bot: return "Bot chat"; + case Type::PrivateGroup: return "Private group"; + case Type::PublicGroup: return "Public group"; + case Type::PrivateChannel: return "Private channel"; + case Type::PublicChannel: return "Private channel"; + } + Unexpected("Dialog type in TypeString."); + }; + const auto NameString = []( + const Data::Utf8String &name, + Type type) -> QByteArray { + if (!name.isEmpty()) { + return name; + } + switch (type) { + case Type::Unknown: return "(unknown)"; + case Type::Personal: return "(deleted user)"; + case Type::Bot: return "(deleted bot)"; + case Type::PrivateGroup: + case Type::PublicGroup: return "(deleted group)"; + case Type::PrivateChannel: + case Type::PublicChannel: return "(deleted channel)"; + } + Unexpected("Dialog type in TypeString."); + }; + return _chats->writeBlock(SerializeKeyValue({ + { "Name", NameString(_dialog.name, _dialog.type) }, + { "Type", TypeString(_dialog.type) }, + { "Messages count", Data::NumberToString(_messagesCount) }, + { + "Content", + (_messagesCount > 0 + ? (_dialog.relativePath + "messages.txt").toUtf8() + : QByteArray()) + } + }) + kLineBreak); +} + +Result TextWriter::writeChatsEnd() { + Expects(_chats != nullptr); + + _chats = nullptr; return Result::Success(); } diff --git a/Telegram/SourceFiles/export/output/export_output_text.h b/Telegram/SourceFiles/export/output/export_output_text.h index ca0a37c98dd900..e2f466d3b6b8f2 100644 --- a/Telegram/SourceFiles/export/output/export_output_text.h +++ b/Telegram/SourceFiles/export/output/export_output_text.h @@ -10,6 +10,7 @@ For license and copyright information please follow this link: #include "export/output/export_output_abstract.h" #include "export/output/export_output_file.h" #include "export/export_settings.h" +#include "export/data/export_data_types.h" namespace Export { namespace Output { @@ -59,6 +60,7 @@ class TextWriter : public AbstractWriter { Result writeChatStart(const Data::DialogInfo &data); Result writeChatSlice(const Data::MessagesSlice &data); Result writeChatEnd(); + Result writeChatsEnd(); Settings _settings; Stats *_stats = nullptr; @@ -70,9 +72,10 @@ class TextWriter : public AbstractWriter { int _dialogsCount = 0; int _dialogIndex = 0; - bool _dialogOnlyMy = false; - bool _dialogEmpty = true; + Data::DialogInfo _dialog; + int _messagesCount = 0; + std::unique_ptr _chats; std::unique_ptr _chat; };