Skip to content

Commit

Permalink
Show messages count in an exported chat.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jun 20, 2018
1 parent 40c0286 commit fcda883
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 62 deletions.
115 changes: 55 additions & 60 deletions Telegram/SourceFiles/export/output/export_output_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ Result TextWriter::writeDialogEnd() {
}

Result TextWriter::writeDialogsEnd() {
return Result::Success();
return writeChatsEnd();
}

Result TextWriter::writeLeftChannelsStart(const Data::DialogsInfo &data) {
Expand Down Expand Up @@ -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<QByteArray>();
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()
Expand All @@ -774,19 +729,18 @@ 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();
}

Result TextWriter::writeChatSlice(const Data::MessagesSlice &data) {
Expects(_chat != nullptr);
Expects(!data.list.empty());

_dialogEmpty = false;
_messagesCount += data.list.size();
auto list = std::vector<QByteArray>();
list.reserve(data.list.size());
auto index = 0;
for (const auto &message : data.list) {
list.push_back(SerializeMessage(
message,
Expand All @@ -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();
}

Expand Down
7 changes: 5 additions & 2 deletions Telegram/SourceFiles/export/output/export_output_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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<File> _chats;
std::unique_ptr<File> _chat;

};
Expand Down

0 comments on commit fcda883

Please sign in to comment.