Split messages.html by 1000 messages.

This commit is contained in:
John Preston 2018-06-24 12:43:57 +01:00
parent 97d27fe869
commit 1ae3af0e80
2 changed files with 33 additions and 1 deletions

View File

@ -17,6 +17,8 @@ namespace Export {
namespace Output {
namespace {
constexpr auto kMessagesInFile = 1000;
const auto kLineBreak = QByteArrayLiteral("<br>");
QByteArray SerializeString(const QByteArray &value) {
@ -1164,7 +1166,7 @@ Result HtmlWriter::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.html");
_chat = fileWithRelativePath(data.relativePath + messagesFile(0));
_messagesCount = 0;
_dialog = data;
return Result::Success();
@ -1174,7 +1176,15 @@ Result HtmlWriter::writeChatSlice(const Data::MessagesSlice &data) {
Expects(_chat != nullptr);
Expects(!data.list.empty());
const auto wasIndex = (_messagesCount / kMessagesInFile);
_messagesCount += data.list.size();
const auto nowIndex = (_messagesCount / kMessagesInFile);
if (nowIndex != wasIndex) {
if (const auto result = switchToNextChatFile(nowIndex); !result) {
return result;
}
}
auto list = std::vector<QByteArray>();
list.reserve(data.list.size());
for (const auto &message : data.list) {
@ -1263,6 +1273,20 @@ Result HtmlWriter::writeChatsEnd() {
return Result::Success();
}
Result HtmlWriter::switchToNextChatFile(int index) {
Expects(_chat != nullptr);
const auto nextPath = messagesFile(index);
const auto link = kLineBreak + "<a href=\""
+ nextPath.toUtf8()
+ "\">Next messages part</a>";
if (const auto result = _chat->writeBlock(link); !result) {
return result;
}
_chat = fileWithRelativePath(_dialog.relativePath + nextPath);
return Result::Success();
}
Result HtmlWriter::finish() {
Expects(_summary != nullptr);
@ -1290,6 +1314,12 @@ QString HtmlWriter::pathWithRelativePath(const QString &path) const {
return _settings.path + path;
}
QString HtmlWriter::messagesFile(int index) const {
return "messages"
+ (index > 0 ? QString::number(index + 1) : QString())
+ ".html";
}
std::unique_ptr<HtmlWriter::Wrap> HtmlWriter::fileWithRelativePath(
const QString &path) const {
return std::make_unique<Wrap>(

View File

@ -68,6 +68,7 @@ private:
QString mainFileRelativePath() const;
QString pathWithRelativePath(const QString &path) const;
std::unique_ptr<Wrap> fileWithRelativePath(const QString &path) const;
QString messagesFile(int index) const;
Result writeSavedContacts(const Data::ContactsList &data);
Result writeFrequentContacts(const Data::ContactsList &data);
@ -84,6 +85,7 @@ private:
Result writeChatSlice(const Data::MessagesSlice &data);
Result writeChatEnd();
Result writeChatsEnd();
Result switchToNextChatFile(int index);
Settings _settings;
Environment _environment;