iv_data.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "iv/iv_data.h"
  8. #include "iv/iv_prepare.h"
  9. #include "webview/webview_interface.h"
  10. #include <QtCore/QRegularExpression>
  11. #include <QtCore/QUrl>
  12. namespace Iv {
  13. namespace {
  14. bool FailureRecorded/* = false*/;
  15. } // namespace
  16. QByteArray GeoPointId(Geo point) {
  17. const auto lat = int(point.lat * 1000000);
  18. const auto lon = int(point.lon * 1000000);
  19. const auto combined = (std::uint64_t(std::uint32_t(lat)) << 32)
  20. | std::uint64_t(std::uint32_t(lon));
  21. return QByteArray::number(quint64(combined))
  22. + ','
  23. + QByteArray::number(point.access);
  24. }
  25. Geo GeoPointFromId(QByteArray data) {
  26. const auto parts = data.split(',');
  27. if (parts.size() != 2) {
  28. return {};
  29. }
  30. const auto combined = parts[0].toULongLong();
  31. const auto lat = int(std::uint32_t(combined >> 32));
  32. const auto lon = int(std::uint32_t(combined & 0xFFFFFFFFULL));
  33. return {
  34. .lat = lat / 1000000.,
  35. .lon = lon / 1000000.,
  36. .access = parts[1].toULongLong(),
  37. };
  38. }
  39. Data::Data(const MTPDwebPage &webpage, const MTPPage &page)
  40. : _source(std::make_unique<Source>(Source{
  41. .pageId = webpage.vid().v,
  42. .page = page,
  43. .webpagePhoto = (webpage.vphoto()
  44. ? *webpage.vphoto()
  45. : std::optional<MTPPhoto>()),
  46. .webpageDocument = (webpage.vdocument()
  47. ? *webpage.vdocument()
  48. : std::optional<MTPDocument>()),
  49. .name = (webpage.vsite_name()
  50. ? qs(*webpage.vsite_name())
  51. : SiteNameFromUrl(qs(webpage.vurl())))
  52. })) {
  53. }
  54. QString Data::id() const {
  55. return qs(_source->page.data().vurl());
  56. }
  57. bool Data::partial() const {
  58. return _source->page.data().is_part();
  59. }
  60. Data::~Data() = default;
  61. void Data::updateCachedViews(int cachedViews) {
  62. _source->updatedCachedViews = std::max(
  63. _source->updatedCachedViews,
  64. cachedViews);
  65. }
  66. void Data::prepare(const Options &options, Fn<void(Prepared)> done) const {
  67. crl::async([source = *_source, options, done = std::move(done)] {
  68. done(Prepare(source, options));
  69. });
  70. }
  71. QString SiteNameFromUrl(const QString &url) {
  72. const auto u = QUrl(url);
  73. QString pretty = u.isValid() ? u.toDisplayString() : url;
  74. const auto m = QRegularExpression(u"^[a-zA-Z0-9]+://"_q).match(pretty);
  75. if (m.hasMatch()) pretty = pretty.mid(m.capturedLength());
  76. int32 slash = pretty.indexOf('/');
  77. if (slash > 0) pretty = pretty.mid(0, slash);
  78. QStringList components = pretty.split('.', Qt::SkipEmptyParts);
  79. if (components.size() >= 2) {
  80. components = components.mid(components.size() - 2);
  81. return components.at(0).at(0).toUpper()
  82. + components.at(0).mid(1)
  83. + '.'
  84. + components.at(1);
  85. }
  86. return QString();
  87. }
  88. bool ShowButton() {
  89. static const auto Supported = [&] {
  90. const auto availability = Webview::Availability();
  91. return availability.customSchemeRequests
  92. && availability.customRangeRequests;
  93. }();
  94. return Supported;
  95. }
  96. void RecordShowFailure() {
  97. FailureRecorded = true;
  98. }
  99. bool FailedToShow() {
  100. return FailureRecorded;
  101. }
  102. } // namespace Iv