2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2016-04-09 20:41:20 +00:00
|
|
|
#include "app.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2018-06-20 17:30:57 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_user.h"
|
2016-06-09 11:51:24 +00:00
|
|
|
#include "data/data_abstract_structure.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
#include "data/data_media_types.h"
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2018-12-18 10:45:06 +00:00
|
|
|
#include "data/data_document.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2016-09-27 13:37:18 +00:00
|
|
|
#include "history/history_location_manager.h"
|
2017-12-18 15:44:50 +00:00
|
|
|
#include "history/history_item_components.h"
|
2018-01-09 17:08:31 +00:00
|
|
|
#include "history/view/history_view_service_message.h"
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2020-10-13 15:11:53 +00:00
|
|
|
#include "ui/cached_round_corners.h"
|
2016-07-08 13:56:53 +00:00
|
|
|
#include "inline_bots/inline_bot_layout_item.h"
|
2018-12-18 10:45:06 +00:00
|
|
|
#include "core/crash_reports.h"
|
|
|
|
#include "core/update_checker.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/sandbox.h"
|
|
|
|
#include "core/application.h"
|
2018-12-18 10:45:06 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
|
|
|
#include "window/notifications_manager.h"
|
2019-06-06 11:20:21 +00:00
|
|
|
#include "window/window_controller.h"
|
2018-12-18 10:45:06 +00:00
|
|
|
#include "platform/platform_notifications_manager.h"
|
|
|
|
#include "storage/file_upload.h"
|
|
|
|
#include "storage/localstorage.h"
|
|
|
|
#include "storage/storage_facade.h"
|
|
|
|
#include "storage/storage_shared_media.h"
|
2017-02-23 10:59:19 +00:00
|
|
|
#include "mainwindow.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "apiwrap.h"
|
2015-07-21 14:55:23 +00:00
|
|
|
#include "numbers.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-01-05 16:14:08 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "styles/style_overview.h"
|
2020-02-03 09:26:37 +00:00
|
|
|
#include "styles/style_media_view.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
2020-10-10 09:15:37 +00:00
|
|
|
#include "styles/style_chat.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2015-07-21 14:55:23 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
#include <QtGui/QFontDatabase>
|
|
|
|
|
2018-10-11 15:54:57 +00:00
|
|
|
#ifdef OS_MAC_OLD
|
|
|
|
#include <libexif/exif-data.h>
|
|
|
|
#endif // OS_MAC_OLD
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-04-13 11:12:43 +00:00
|
|
|
constexpr auto kImageAreaLimit = 12'032 * 9'024;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2020-03-23 11:27:07 +00:00
|
|
|
App::LaunchState _launchState = App::Launched;
|
2015-05-20 19:28:24 +00:00
|
|
|
|
2020-03-23 11:27:07 +00:00
|
|
|
HistoryView::Element *hoveredItem = nullptr,
|
|
|
|
*pressedItem = nullptr,
|
|
|
|
*hoveredLinkItem = nullptr,
|
|
|
|
*pressedLinkItem = nullptr,
|
|
|
|
*mousedItem = nullptr;
|
|
|
|
|
2017-12-17 08:13:26 +00:00
|
|
|
} // namespace
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
namespace App {
|
|
|
|
|
|
|
|
QString formatPhone(QString phone) {
|
2015-07-21 14:55:23 +00:00
|
|
|
if (phone.isEmpty()) return QString();
|
2016-06-08 16:40:33 +00:00
|
|
|
if (phone.at(0) == '0') return phone;
|
|
|
|
|
2015-07-21 14:55:23 +00:00
|
|
|
QString number = phone;
|
|
|
|
for (const QChar *ch = phone.constData(), *e = ch + phone.size(); ch != e; ++ch) {
|
|
|
|
if (ch->unicode() < '0' || ch->unicode() > '9') {
|
|
|
|
number = phone.replace(QRegularExpression(qsl("[^\\d]")), QString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QVector<int> groups = phoneNumberParse(number);
|
|
|
|
if (groups.isEmpty()) return '+' + number;
|
|
|
|
|
|
|
|
QString result;
|
|
|
|
result.reserve(number.size() + groups.size() + 1);
|
|
|
|
result.append('+');
|
|
|
|
int32 sum = 0;
|
|
|
|
for (int32 i = 0, l = groups.size(); i < l; ++i) {
|
|
|
|
result.append(number.midRef(sum, groups.at(i)));
|
|
|
|
sum += groups.at(i);
|
|
|
|
if (sum < number.size()) result.append(' ');
|
|
|
|
}
|
|
|
|
if (sum < number.size()) result.append(number.midRef(sum));
|
|
|
|
return result;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
void initMedia() {
|
2020-10-13 15:11:53 +00:00
|
|
|
Ui::StartCachedCorners();
|
2016-12-20 13:03:51 +00:00
|
|
|
|
|
|
|
using Update = Window::Theme::BackgroundUpdate;
|
|
|
|
static auto subscription = Window::Theme::Background()->add_subscription([](const Update &update) {
|
2016-12-23 13:21:01 +00:00
|
|
|
if (update.paletteChanged()) {
|
2020-06-10 18:08:17 +00:00
|
|
|
if (const auto m = App::main()) { // multi good
|
|
|
|
m->updateScrollColors();
|
2016-12-20 13:03:51 +00:00
|
|
|
}
|
2018-01-09 17:08:31 +00:00
|
|
|
HistoryView::serviceColorsUpdated();
|
2016-11-03 10:33:57 +00:00
|
|
|
}
|
|
|
|
});
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-01-03 01:43:42 +00:00
|
|
|
|
2015-12-27 21:37:48 +00:00
|
|
|
void deinitMedia() {
|
2020-10-13 15:11:53 +00:00
|
|
|
Ui::FinishCachedCorners();
|
2016-06-09 11:51:24 +00:00
|
|
|
Data::clearGlobalStructures();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void hoveredItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::hoveredItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *hoveredItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::hoveredItem;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void pressedItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::pressedItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *pressedItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::pressedItem;
|
|
|
|
}
|
2016-01-03 01:43:42 +00:00
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void hoveredLinkItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::hoveredLinkItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *hoveredLinkItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::hoveredLinkItem;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void pressedLinkItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::pressedLinkItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *pressedLinkItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::pressedLinkItem;
|
|
|
|
}
|
2016-01-03 01:43:42 +00:00
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void mousedItem(HistoryView::Element *item) {
|
2014-05-30 08:53:19 +00:00
|
|
|
::mousedItem = item;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
HistoryView::Element *mousedItem() {
|
2014-05-30 08:53:19 +00:00
|
|
|
return ::mousedItem;
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:26:41 +00:00
|
|
|
void clearMousedItems() {
|
|
|
|
hoveredItem(nullptr);
|
|
|
|
pressedItem(nullptr);
|
|
|
|
hoveredLinkItem(nullptr);
|
|
|
|
pressedLinkItem(nullptr);
|
|
|
|
mousedItem(nullptr);
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void quit() {
|
2018-06-20 17:30:57 +00:00
|
|
|
if (quitting()) {
|
|
|
|
return;
|
2019-07-24 14:00:30 +00:00
|
|
|
} else if (Core::IsAppLaunched()
|
|
|
|
&& Core::App().exportPreventsQuit()) {
|
2018-06-20 17:30:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-02 18:34:42 +00:00
|
|
|
setLaunchState(QuitRequested);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-12-04 12:10:00 +00:00
|
|
|
if (auto window = App::wnd()) {
|
2019-01-21 13:42:21 +00:00
|
|
|
if (!Core::Sandbox::Instance().isSavingSession()) {
|
2016-06-21 15:58:07 +00:00
|
|
|
window->hide();
|
|
|
|
}
|
2016-06-07 19:59:39 +00:00
|
|
|
}
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::Application::QuitAttempt();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 18:34:42 +00:00
|
|
|
bool quitting() {
|
|
|
|
return _launchState != Launched;
|
|
|
|
}
|
|
|
|
|
|
|
|
LaunchState launchState() {
|
|
|
|
return _launchState;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 18:34:42 +00:00
|
|
|
void setLaunchState(LaunchState state) {
|
|
|
|
_launchState = state;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:44:28 +00:00
|
|
|
void restart() {
|
2018-07-11 22:14:44 +00:00
|
|
|
using namespace Core;
|
|
|
|
const auto updateReady = !UpdaterDisabled()
|
|
|
|
&& (UpdateChecker().state() == UpdateChecker::State::Ready);
|
2016-10-28 12:44:28 +00:00
|
|
|
if (updateReady) {
|
|
|
|
cSetRestartingUpdate(true);
|
|
|
|
} else {
|
|
|
|
cSetRestarting(true);
|
|
|
|
cSetRestartingToSettings(true);
|
|
|
|
}
|
|
|
|
App::quit();
|
|
|
|
}
|
|
|
|
|
2014-12-22 23:11:37 +00:00
|
|
|
QImage readImage(QByteArray data, QByteArray *format, bool opaque, bool *animated) {
|
2020-05-30 18:37:03 +00:00
|
|
|
if (data.isEmpty()) {
|
|
|
|
return QImage();
|
|
|
|
}
|
2020-01-02 11:25:52 +00:00
|
|
|
QByteArray tmpFormat;
|
2014-05-30 08:53:19 +00:00
|
|
|
QImage result;
|
|
|
|
QBuffer buffer(&data);
|
2020-01-02 11:25:52 +00:00
|
|
|
if (!format) {
|
|
|
|
format = &tmpFormat;
|
|
|
|
}
|
2015-12-23 11:13:08 +00:00
|
|
|
{
|
|
|
|
QImageReader reader(&buffer, *format);
|
2016-08-30 05:24:16 +00:00
|
|
|
#ifndef OS_MAC_OLD
|
2015-12-23 11:13:08 +00:00
|
|
|
reader.setAutoTransform(true);
|
2016-08-30 05:24:16 +00:00
|
|
|
#endif // OS_MAC_OLD
|
2015-12-23 11:13:08 +00:00
|
|
|
if (animated) *animated = reader.supportsAnimation() && reader.imageCount() > 1;
|
2020-03-23 11:27:07 +00:00
|
|
|
if (!reader.canRead()) {
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
const auto imageSize = reader.size();
|
|
|
|
if (imageSize.width() * imageSize.height() > kImageAreaLimit) {
|
|
|
|
return QImage();
|
|
|
|
}
|
2015-12-23 11:13:08 +00:00
|
|
|
QByteArray fmt = reader.format();
|
|
|
|
if (!fmt.isEmpty()) *format = fmt;
|
|
|
|
if (!reader.read(&result)) {
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
fmt = reader.format();
|
|
|
|
if (!fmt.isEmpty()) *format = fmt;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
buffer.seek(0);
|
2016-12-08 14:08:54 +00:00
|
|
|
auto fmt = QString::fromUtf8(*format).toLower();
|
2014-05-30 08:53:19 +00:00
|
|
|
if (fmt == "jpg" || fmt == "jpeg") {
|
2016-08-30 05:24:16 +00:00
|
|
|
#ifdef OS_MAC_OLD
|
2016-12-08 14:08:54 +00:00
|
|
|
if (auto exifData = exif_data_new_from_data((const uchar*)(data.constData()), data.size())) {
|
|
|
|
auto byteOrder = exif_data_get_byte_order(exifData);
|
|
|
|
if (auto exifEntry = exif_data_get_entry(exifData, EXIF_TAG_ORIENTATION)) {
|
|
|
|
auto orientationFix = [exifEntry, byteOrder] {
|
|
|
|
auto orientation = exif_get_short(exifEntry->data, byteOrder);
|
|
|
|
switch (orientation) {
|
|
|
|
case 2: return QTransform(-1, 0, 0, 1, 0, 0);
|
|
|
|
case 3: return QTransform(-1, 0, 0, -1, 0, 0);
|
|
|
|
case 4: return QTransform(1, 0, 0, -1, 0, 0);
|
|
|
|
case 5: return QTransform(0, -1, -1, 0, 0, 0);
|
|
|
|
case 6: return QTransform(0, 1, -1, 0, 0, 0);
|
|
|
|
case 7: return QTransform(0, 1, 1, 0, 0, 0);
|
|
|
|
case 8: return QTransform(0, -1, 1, 0, 0, 0);
|
|
|
|
}
|
|
|
|
return QTransform();
|
|
|
|
};
|
|
|
|
result = result.transformed(orientationFix());
|
2015-12-23 11:13:08 +00:00
|
|
|
}
|
|
|
|
exif_data_free(exifData);
|
|
|
|
}
|
2016-08-30 05:24:16 +00:00
|
|
|
#endif // OS_MAC_OLD
|
2016-11-28 15:45:07 +00:00
|
|
|
} else if (opaque) {
|
2017-02-21 13:45:56 +00:00
|
|
|
result = Images::prepareOpaque(std::move(result));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-02 14:55:24 +00:00
|
|
|
QImage readImage(const QString &file, QByteArray *format, bool opaque, bool *animated, QByteArray *content) {
|
2014-05-30 08:53:19 +00:00
|
|
|
QFile f(file);
|
2016-11-28 15:45:07 +00:00
|
|
|
if (f.size() > kImageSizeLimit || !f.open(QIODevice::ReadOnly)) {
|
2014-12-22 23:11:37 +00:00
|
|
|
if (animated) *animated = false;
|
2014-05-30 08:53:19 +00:00
|
|
|
return QImage();
|
|
|
|
}
|
2016-12-08 14:08:54 +00:00
|
|
|
auto imageBytes = f.readAll();
|
|
|
|
auto result = readImage(imageBytes, format, opaque, animated);
|
|
|
|
if (content && !result.isNull()) {
|
|
|
|
*content = imageBytes;
|
|
|
|
}
|
2015-01-02 14:55:24 +00:00
|
|
|
return result;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 10:57:11 +00:00
|
|
|
QPixmap pixmapFromImageInPlace(QImage &&image) {
|
2017-02-21 13:45:56 +00:00
|
|
|
return QPixmap::fromImage(std::move(image), Qt::ColorOnly);
|
2016-05-27 10:57:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 13:34:37 +00:00
|
|
|
}
|