"Fossies" - the Fresh Open Source Software Archive 
Member "tdesktop-2.6.1/Telegram/SourceFiles/export/output/export_output_abstract.cpp" (24 Feb 2021, 15261 Bytes) of package /linux/misc/tdesktop-2.6.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "export_output_abstract.cpp" see the
Fossies "Dox" file reference documentation.
1 /*
2 This file is part of Telegram Desktop,
3 the official desktop application for the Telegram messaging service.
4
5 For license and copyright information please follow this link:
6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
7 */
8 #include "export/output/export_output_abstract.h"
9
10 #include "export/output/export_output_html.h"
11 #include "export/output/export_output_json.h"
12 #include "export/output/export_output_stats.h"
13 #include "export/output/export_output_result.h"
14
15 #include <QtCore/QDir>
16 #include <QtCore/QDate>
17
18 namespace Export {
19 namespace Output {
20
21 QString NormalizePath(const Settings &settings) {
22 QDir folder(settings.path);
23 const auto path = folder.absolutePath();
24 auto result = path.endsWith('/') ? path : (path + '/');
25 if (!folder.exists() && !settings.forceSubPath) {
26 return result;
27 }
28 const auto mode = QDir::AllEntries | QDir::NoDotAndDotDot;
29 const auto list = folder.entryInfoList(mode);
30 if (list.isEmpty() && !settings.forceSubPath) {
31 return result;
32 }
33 const auto date = QDate::currentDate();
34 const auto base = QString(settings.onlySinglePeer()
35 ? "ChatExport_%1"
36 : "DataExport_%1"
37 ).arg(date.toString(Qt::ISODate));
38 const auto add = [&](int i) {
39 return base + (i ? " (" + QString::number(i) + ')' : QString());
40 };
41 auto index = 0;
42 while (QDir(result + add(index)).exists()) {
43 ++index;
44 }
45 result += add(index) + '/';
46 return result;
47 }
48
49 std::unique_ptr<AbstractWriter> CreateWriter(Format format) {
50 switch (format) {
51 case Format::Html: return std::make_unique<HtmlWriter>();
52 case Format::Json: return std::make_unique<JsonWriter>();
53 }
54 Unexpected("Format in Export::Output::CreateWriter.");
55 }
56
57 Stats AbstractWriter::produceTestExample(
58 const QString &path,
59 const Environment &environment) {
60 auto result = Stats();
61 const auto folder = QDir(path).absolutePath();
62 auto settings = Settings();
63 settings.format = format();
64 settings.path = (folder.endsWith('/') ? folder : (folder + '/'))
65 + "ExportExample/";
66 settings.types = Settings::Type::AllMask;
67 settings.fullChats = Settings::Type::AllMask
68 & ~(Settings::Type::PublicChannels | Settings::Type::PublicGroups);
69 settings.media.types = MediaSettings::Type::AllMask;
70 settings.media.sizeLimit = 1024 * 1024;
71
72 const auto check = [](Result result) {
73 Assert(result.isSuccess());
74 };
75
76 check(start(settings, environment, &result));
77
78 const auto counter = [&] {
79 static auto GlobalCounter = 0;
80 return ++GlobalCounter;
81 };
82 const auto date = [&] {
83 return time(nullptr) - 86400 + counter();
84 };
85 const auto prevdate = [&] {
86 return date() - 86400;
87 };
88
89 auto personal = Data::PersonalInfo();
90 personal.bio = "Nice text about me.";
91 personal.user.info.firstName = "John";
92 personal.user.info.lastName = "Preston";
93 personal.user.info.phoneNumber = "447400000000";
94 personal.user.info.date = date();
95 personal.user.username = "preston";
96 personal.user.info.userId = counter();
97 personal.user.isBot = false;
98 personal.user.isSelf = true;
99 check(writePersonal(personal));
100
101 const auto generatePhoto = [&] {
102 static auto index = 0;
103 auto result = Data::Photo();
104 result.date = date();
105 result.id = counter();
106 result.image.width = 512;
107 result.image.height = 512;
108 result.image.file.relativePath = "files/photo_"
109 + QString::number(++index)
110 + ".jpg";
111 return result;
112 };
113
114 auto userpics = Data::UserpicsInfo();
115 userpics.count = 3;
116 auto userpicsSlice1 = Data::UserpicsSlice();
117 userpicsSlice1.list.push_back(generatePhoto());
118 userpicsSlice1.list.push_back(generatePhoto());
119 auto userpicsSlice2 = Data::UserpicsSlice();
120 userpicsSlice2.list.push_back(generatePhoto());
121 check(writeUserpicsStart(userpics));
122 check(writeUserpicsSlice(userpicsSlice1));
123 check(writeUserpicsSlice(userpicsSlice2));
124 check(writeUserpicsEnd());
125
126 auto contacts = Data::ContactsList();
127 auto topUser = Data::TopPeer();
128 auto user = personal.user;
129 auto peerUser = Data::Peer{ user };
130 topUser.peer = peerUser;
131 topUser.rating = 0.5;
132 auto topChat = Data::TopPeer();
133 auto chat = Data::Chat();
134 chat.id = counter();
135 chat.title = "Group chat";
136 auto peerChat = Data::Peer{ chat };
137 topChat.peer = peerChat;
138 topChat.rating = 0.25;
139 auto topBot = Data::TopPeer();
140 auto bot = Data::User();
141 bot.info.date = date();
142 bot.isBot = true;
143 bot.info.firstName = "Bot";
144 bot.info.lastName = "Father";
145 bot.info.userId = counter();
146 bot.username = "botfather";
147 auto peerBot = Data::Peer{ bot };
148 topBot.peer = peerBot;
149 topBot.rating = 0.125;
150
151 auto peers = std::map<Data::PeerId, Data::Peer>();
152 peers.emplace(peerUser.id(), peerUser);
153 peers.emplace(peerBot.id(), peerBot);
154 peers.emplace(peerChat.id(), peerChat);
155
156 contacts.correspondents.push_back(topUser);
157 contacts.correspondents.push_back(topChat);
158 contacts.inlineBots.push_back(topBot);
159 contacts.inlineBots.push_back(topBot);
160 contacts.phoneCalls.push_back(topUser);
161 contacts.list.push_back(user.info);
162 contacts.list.push_back(bot.info);
163
164 check(writeContactsList(contacts));
165
166 auto sessions = Data::SessionsList();
167 auto session = Data::Session();
168 session.applicationName = "Telegram Desktop";
169 session.applicationVersion = "1.3.8";
170 session.country = "GB";
171 session.created = date();
172 session.deviceModel = "PC";
173 session.ip = "127.0.0.1";
174 session.lastActive = date();
175 session.platform = "Windows";
176 session.region = "London";
177 session.systemVersion = "10";
178 sessions.list.push_back(session);
179 sessions.list.push_back(session);
180 auto webSession = Data::WebSession();
181 webSession.botUsername = "botfather";
182 webSession.browser = "Google Chrome";
183 webSession.created = date();
184 webSession.domain = "telegram.org";
185 webSession.ip = "127.0.0.1";
186 webSession.lastActive = date();
187 webSession.platform = "Windows";
188 webSession.region = "London, GB";
189 sessions.webList.push_back(webSession);
190 sessions.webList.push_back(webSession);
191 check(writeSessionsList(sessions));
192
193 auto sampleMessage = [&] {
194 auto message = Data::Message();
195 message.id = counter();
196 message.date = prevdate();
197 message.edited = date();
198 static auto count = 0;
199 if (++count % 3 == 0) {
200 message.forwardedFromId = Data::UserPeerId(user.info.userId);
201 message.forwardedDate = date();
202 } else if (count % 3 == 2) {
203 message.forwardedFromName = "Test hidden forward";
204 message.forwardedDate = date();
205 }
206 message.fromId = user.info.userId;
207 message.replyToMsgId = counter();
208 message.viaBotId = bot.info.userId;
209 message.text.push_back(Data::TextPart{
210 Data::TextPart::Type::Text,
211 ("Text message " + QString::number(counter())).toUtf8()
212 });
213 return message;
214 };
215 auto sliceBot1 = Data::MessagesSlice();
216 sliceBot1.peers = peers;
217 sliceBot1.list.push_back(sampleMessage());
218 sliceBot1.list.push_back([&] {
219 auto message = sampleMessage();
220 message.media.content = generatePhoto();
221 message.media.ttl = counter();
222 return message;
223 }());
224 sliceBot1.list.push_back([&] {
225 auto message = sampleMessage();
226 auto document = Data::Document();
227 document.date = prevdate();
228 document.duration = counter();
229 auto photo = generatePhoto();
230 document.file = photo.image.file;
231 document.width = photo.image.width;
232 document.height = photo.image.height;
233 document.id = counter();
234 message.media.content = document;
235 return message;
236 }());
237 sliceBot1.list.push_back([&] {
238 auto message = sampleMessage();
239 auto contact = Data::SharedContact();
240 contact.info = user.info;
241 message.media.content = contact;
242 return message;
243 }());
244 auto sliceBot2 = Data::MessagesSlice();
245 sliceBot2.peers = peers;
246 sliceBot2.list.push_back([&] {
247 auto message = sampleMessage();
248 auto point = Data::GeoPoint();
249 point.latitude = 1.5;
250 point.longitude = 2.8;
251 point.valid = true;
252 message.media.content = point;
253 message.media.ttl = counter();
254 return message;
255 }());
256 sliceBot2.list.push_back([&] {
257 auto message = sampleMessage();
258 message.replyToMsgId = sliceBot1.list.back().id;
259 auto venue = Data::Venue();
260 venue.point.latitude = 1.5;
261 venue.point.longitude = 2.8;
262 venue.point.valid = true;
263 venue.address = "Test address";
264 venue.title = "Test venue";
265 message.media.content = venue;
266 return message;
267 }());
268 sliceBot2.list.push_back([&] {
269 auto message = sampleMessage();
270 auto game = Data::Game();
271 game.botId = bot.info.userId;
272 game.title = "Test game";
273 game.description = "Test game description";
274 game.id = counter();
275 game.shortName = "testgame";
276 message.media.content = game;
277 return message;
278 }());
279 sliceBot2.list.push_back([&] {
280 auto message = sampleMessage();
281 auto invoice = Data::Invoice();
282 invoice.amount = counter();
283 invoice.currency = "GBP";
284 invoice.title = "Huge invoice.";
285 invoice.description = "So money.";
286 invoice.receiptMsgId = sliceBot2.list.front().id;
287 message.media.content = invoice;
288 return message;
289 }());
290 auto serviceMessage = [&] {
291 auto message = Data::Message();
292 message.id = counter();
293 message.date = prevdate();
294 message.fromId = user.info.userId;
295 return message;
296 };
297 auto sliceChat1 = Data::MessagesSlice();
298 sliceChat1.peers = peers;
299 sliceChat1.list.push_back([&] {
300 auto message = serviceMessage();
301 auto action = Data::ActionChatCreate();
302 action.title = "Test chat";
303 action.userIds.push_back(user.info.userId);
304 action.userIds.push_back(bot.info.userId);
305 message.action.content = action;
306 return message;
307 }());
308 sliceChat1.list.push_back([&] {
309 auto message = serviceMessage();
310 auto action = Data::ActionChatEditTitle();
311 action.title = "New title";
312 message.action.content = action;
313 return message;
314 }());
315 sliceChat1.list.push_back([&] {
316 auto message = serviceMessage();
317 auto action = Data::ActionChatEditPhoto();
318 action.photo = generatePhoto();
319 message.action.content = action;
320 return message;
321 }());
322 sliceChat1.list.push_back([&] {
323 auto message = serviceMessage();
324 auto action = Data::ActionChatDeletePhoto();
325 message.action.content = action;
326 return message;
327 }());
328 sliceChat1.list.push_back([&] {
329 auto message = serviceMessage();
330 auto action = Data::ActionChatAddUser();
331 action.userIds.push_back(user.info.userId);
332 action.userIds.push_back(bot.info.userId);
333 message.action.content = action;
334 return message;
335 }());
336 sliceChat1.list.push_back([&] {
337 auto message = serviceMessage();
338 auto action = Data::ActionChatDeleteUser();
339 action.userId = bot.info.userId;
340 message.action.content = action;
341 return message;
342 }());
343 sliceChat1.list.push_back([&] {
344 auto message = serviceMessage();
345 auto action = Data::ActionChatJoinedByLink();
346 action.inviterId = bot.info.userId;
347 message.action.content = action;
348 return message;
349 }());
350 sliceChat1.list.push_back([&] {
351 auto message = serviceMessage();
352 auto action = Data::ActionChannelCreate();
353 action.title = "Channel name";
354 message.action.content = action;
355 return message;
356 }());
357 sliceChat1.list.push_back([&] {
358 auto message = serviceMessage();
359 auto action = Data::ActionChatMigrateTo();
360 action.channelId = chat.id;
361 message.action.content = action;
362 return message;
363 }());
364 sliceChat1.list.push_back([&] {
365 auto message = serviceMessage();
366 auto action = Data::ActionChannelMigrateFrom();
367 action.chatId = chat.id;
368 action.title = "Supergroup now";
369 message.action.content = action;
370 return message;
371 }());
372 auto sliceChat2 = Data::MessagesSlice();
373 sliceChat2.peers = peers;
374 sliceChat2.list.push_back([&] {
375 auto message = serviceMessage();
376 auto action = Data::ActionPinMessage();
377 message.replyToMsgId = sliceChat1.list.back().id;
378 message.action.content = action;
379 return message;
380 }());
381 sliceChat2.list.push_back([&] {
382 auto message = serviceMessage();
383 auto action = Data::ActionHistoryClear();
384 message.action.content = action;
385 return message;
386 }());
387 sliceChat2.list.push_back([&] {
388 auto message = serviceMessage();
389 auto action = Data::ActionGameScore();
390 action.score = counter();
391 action.gameId = counter();
392 message.replyToMsgId = sliceChat2.list.back().id;
393 message.action.content = action;
394 return message;
395 }());
396 sliceChat2.list.push_back([&] {
397 auto message = serviceMessage();
398 auto action = Data::ActionPaymentSent();
399 action.amount = counter();
400 action.currency = "GBP";
401 message.replyToMsgId = sliceChat2.list.front().id;
402 message.action.content = action;
403 return message;
404 }());
405 sliceChat2.list.push_back([&] {
406 auto message = serviceMessage();
407 auto action = Data::ActionPhoneCall();
408 action.duration = counter();
409 action.discardReason = Data::ActionPhoneCall::DiscardReason::Busy;
410 message.action.content = action;
411 return message;
412 }());
413 sliceChat2.list.push_back([&] {
414 auto message = serviceMessage();
415 auto action = Data::ActionScreenshotTaken();
416 message.action.content = action;
417 return message;
418 }());
419 sliceChat2.list.push_back([&] {
420 auto message = serviceMessage();
421 auto action = Data::ActionCustomAction();
422 action.message = "Custom chat action.";
423 message.action.content = action;
424 return message;
425 }());
426 sliceChat2.list.push_back([&] {
427 auto message = serviceMessage();
428 auto action = Data::ActionBotAllowed();
429 action.domain = "telegram.org";
430 message.action.content = action;
431 return message;
432 }());
433 sliceChat2.list.push_back([&] {
434 auto message = serviceMessage();
435 auto action = Data::ActionSecureValuesSent();
436 using Type = Data::ActionSecureValuesSent::Type;
437 action.types.push_back(Type::BankStatement);
438 action.types.push_back(Type::Phone);
439 message.action.content = action;
440 return message;
441 }());
442 sliceChat2.list.push_back([&] {
443 auto message = serviceMessage();
444 auto action = Data::ActionContactSignUp();
445 message.action.content = action;
446 return message;
447 }());
448 auto dialogs = Data::DialogsInfo();
449 auto dialogBot = Data::DialogInfo();
450 dialogBot.messagesCountPerSplit.push_back(sliceBot1.list.size());
451 dialogBot.messagesCountPerSplit.push_back(sliceBot2.list.size());
452 dialogBot.type = Data::DialogInfo::Type::Bot;
453 dialogBot.name = peerBot.name();
454 dialogBot.onlyMyMessages = false;
455 dialogBot.peerId = peerBot.id();
456 dialogBot.relativePath = "chats/chat_"
457 + QString::number(counter())
458 + '/';
459 dialogBot.splits.push_back(0);
460 dialogBot.splits.push_back(1);
461 dialogBot.topMessageDate = sliceBot2.list.back().date;
462 dialogBot.topMessageId = sliceBot2.list.back().id;
463 auto dialogChat = Data::DialogInfo();
464 dialogChat.messagesCountPerSplit.push_back(sliceChat1.list.size());
465 dialogChat.messagesCountPerSplit.push_back(sliceChat2.list.size());
466 dialogChat.type = Data::DialogInfo::Type::PrivateGroup;
467 dialogChat.name = peerChat.name();
468 dialogChat.onlyMyMessages = true;
469 dialogChat.peerId = peerChat.id();
470 dialogChat.relativePath = "chats/chat_"
471 + QString::number(counter())
472 + '/';
473 dialogChat.splits.push_back(0);
474 dialogChat.splits.push_back(1);
475 dialogChat.topMessageDate = sliceChat2.list.back().date;
476 dialogChat.topMessageId = sliceChat2.list.back().id;
477 dialogs.chats.push_back(dialogBot);
478 dialogs.chats.push_back(dialogChat);
479
480 check(writeDialogsStart(dialogs));
481 check(writeDialogStart(dialogBot));
482 check(writeDialogSlice(sliceBot1));
483 check(writeDialogSlice(sliceBot2));
484 check(writeDialogEnd());
485 check(writeDialogStart(dialogChat));
486 check(writeDialogSlice(sliceChat1));
487 check(writeDialogSlice(sliceChat2));
488 check(writeDialogEnd());
489 check(writeDialogsEnd());
490
491 check(finish());
492
493 return result;
494 }
495
496 } // namespace Output
497 } // namespace Export