diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index e787c1d7b5..31695e9bc9 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -11,7 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/timer.h" #include "base/flat_map.h" #include "base/flat_set.h" -#include "core/single_timer.h" #include "mtproto/sender.h" #include "chat_helpers/stickers.h" #include "data/data_messages.h" diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 97fd21d2f0..53623537c6 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -810,7 +810,7 @@ namespace App { setLaunchState(QuitRequested); if (auto window = App::wnd()) { - if (!Sandbox::isSavingSession()) { + if (!Core::App().isSavingSession()) { window->hide(); } } diff --git a/Telegram/SourceFiles/application.cpp b/Telegram/SourceFiles/application.cpp index 4485cc7368..417e0b9b77 100644 --- a/Telegram/SourceFiles/application.cpp +++ b/Telegram/SourceFiles/application.cpp @@ -510,39 +510,8 @@ void Application::closeApplication() { _updateChecker = nullptr; } -inline Application *application() { - return qobject_cast(QApplication::instance()); -} - namespace Sandbox { -QRect availableGeometry() { - if (auto a = application()) { - return a->desktop()->availableGeometry(); - } - return QDesktopWidget().availableGeometry(); -} - -QRect screenGeometry(const QPoint &p) { - if (auto a = application()) { - return a->desktop()->screenGeometry(p); - } - return QDesktopWidget().screenGeometry(p); -} - -void setActiveWindow(QWidget *window) { - if (auto a = application()) { - a->setActiveWindow(window); - } -} - -bool isSavingSession() { - if (auto a = application()) { - return a->isSavingSession(); - } - return false; -} - void execExternal(const QString &cmd) { DEBUG_LOG(("Application Info: executing external command '%1'").arg(cmd)); if (cmd == "show") { @@ -554,23 +523,7 @@ void execExternal(const QString &cmd) { } } -void adjustSingleTimers() { - if (auto a = application()) { - a->adjustSingleTimers(); - } - base::Timer::Adjust(); - base::ConcurrentTimerEnvironment::Adjust(); -} - -void connect(const char *signal, QObject *object, const char *method) { - if (auto a = application()) { - a->connect(a, signal, object, method); - } -} - void launch() { - Assert(application() != 0); - const auto dpi = Application::primaryScreen()->logicalDotsPerInch(); LOG(("Primary screen DPI: %1").arg(dpi)); if (dpi <= 108) { @@ -587,7 +540,7 @@ void launch() { cSetScreenScale(300); // 300%: 288 DPI (264-inf) } - auto devicePixelRatio = application()->devicePixelRatio(); + auto devicePixelRatio = Core::App().devicePixelRatio(); if (devicePixelRatio > 1.) { if ((cPlatform() != dbipMac && cPlatform() != dbipMacOld) || (devicePixelRatio != 2.)) { LOG(("Found non-trivial Device Pixel Ratio: %1").arg(devicePixelRatio)); @@ -601,13 +554,7 @@ void launch() { cSetScreenScale(kInterfaceScaleDefault); } - application()->createMessenger(); -} - -void refreshGlobalProxy() { - if (const auto instance = application()) { - instance->refreshGlobalProxy(); - } + Core::App().createMessenger(); } } // namespace Sandbox diff --git a/Telegram/SourceFiles/application.h b/Telegram/SourceFiles/application.h index 97683f4ec8..3e82601f45 100644 --- a/Telegram/SourceFiles/application.h +++ b/Telegram/SourceFiles/application.h @@ -40,9 +40,6 @@ public: ~Application(); -signals: - void adjustSingleTimers(); - // Single instance application public slots: void socketConnected(); @@ -115,19 +112,7 @@ inline Application &App() { namespace Sandbox { -QRect availableGeometry(); -QRect screenGeometry(const QPoint &p); -void setActiveWindow(QWidget *window); -bool isSavingSession(); - void execExternal(const QString &cmd); - -void adjustSingleTimers(); - -void refreshGlobalProxy(); - -void connect(const char *signal, QObject *object, const char *method); - void launch(); } // namespace Sandbox diff --git a/Telegram/SourceFiles/boxes/about_box.cpp b/Telegram/SourceFiles/boxes/about_box.cpp index 2551546f5b..fe0093fb2c 100644 --- a/Telegram/SourceFiles/boxes/about_box.cpp +++ b/Telegram/SourceFiles/boxes/about_box.cpp @@ -11,7 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwidget.h" #include "mainwindow.h" #include "boxes/confirm_box.h" -#include "application.h" #include "ui/widgets/buttons.h" #include "ui/widgets/labels.h" #include "styles/style_boxes.h" @@ -70,7 +69,7 @@ void AboutBox::showVersionHistory() { } url = url.arg(qsl("talpha%1_%2").arg(cRealAlphaVersion()).arg(Core::countAlphaVersionSignature(cRealAlphaVersion()))); - Application::clipboard()->setText(url); + QApplication::clipboard()->setText(url); Ui::show(Box("The link to the current private alpha version of Telegram Desktop was copied to the clipboard.")); } else { diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp index 9e3f4f85e8..878820f98f 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp @@ -33,7 +33,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwidget.h" #include "messenger.h" #include "apiwrap.h" -#include "application.h" #include "auth_session.h" #include "observer_peer.h" #include "styles/style_boxes.h" @@ -742,7 +741,7 @@ object_ptr Controller::createInviteLinkEdit() { _controls.inviteLink->setContextCopyText(QString()); _controls.inviteLink->setBreakEverywhere(true); _controls.inviteLink->setClickHandlerFilter([=](auto&&...) { - Application::clipboard()->setText(inviteLinkText()); + QApplication::clipboard()->setText(inviteLinkText()); Ui::Toast::Show(lang(lng_group_invite_copied)); return false; }); diff --git a/Telegram/SourceFiles/boxes/username_box.cpp b/Telegram/SourceFiles/boxes/username_box.cpp index bc8af847ad..def070a560 100644 --- a/Telegram/SourceFiles/boxes/username_box.cpp +++ b/Telegram/SourceFiles/boxes/username_box.cpp @@ -8,7 +8,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/username_box.h" #include "lang/lang_keys.h" -#include "application.h" #include "mainwidget.h" #include "mainwindow.h" #include "ui/widgets/buttons.h" @@ -166,7 +165,7 @@ void UsernameBox::changed() { } void UsernameBox::linkClick() { - Application::clipboard()->setText(Messenger::Instance().createInternalLinkFull(getName())); + QApplication::clipboard()->setText(Messenger::Instance().createInternalLinkFull(getName())); Ui::Toast::Show(lang(lng_username_copied)); } @@ -180,7 +179,7 @@ bool UsernameBox::onUpdateFail(const RPCError &error) { _saveRequestId = 0; const auto self = Auth().user(); - const auto err = error.type(); + const auto &err = error.type(); if (err == qstr("USERNAME_NOT_MODIFIED") || _sentUsername == self->username) { self->setName( TextUtilities::SingleLine(self->firstName), diff --git a/Telegram/SourceFiles/config.h b/Telegram/SourceFiles/config.h index bbf97ebeb9..d2ee936576 100644 --- a/Telegram/SourceFiles/config.h +++ b/Telegram/SourceFiles/config.h @@ -26,8 +26,6 @@ enum { MTPKillFileSessionTimeout = 5000, // how much time without upload / download causes additional session kill - MTPChannelGetDifferenceLimit = 100, - MaxSelectedItems = 100, MaxPhoneCodeLength = 4, // max length of country phone code @@ -80,8 +78,6 @@ enum { ServiceUserId = 777000, WebPageUserId = 701000, - CacheBackgroundTimeout = 3000, // cache background scaled image after 3s - UpdateDelayConstPart = 8 * 3600, // 8 hour min time between update check requests UpdateDelayRandPart = 8 * 3600, // 8 hour max - min time between update check requests @@ -246,32 +242,6 @@ inline const QString &cTempDir() { return res; } -enum { - DialogsFirstLoad = 20, // first dialogs part size requested - DialogsPerPage = 500, // next dialogs part size - - UseBigFilesFrom = 10 * 1024 * 1024, // mtp big files methods used for files greater than 10mb - - UploadPartSize = 32 * 1024, // 32kb for photo - DocumentMaxPartsCount = 3000, // no more than 3000 parts - DocumentUploadPartSize0 = 32 * 1024, // 32kb for tiny document ( < 1mb ) - DocumentUploadPartSize1 = 64 * 1024, // 64kb for little document ( <= 32mb ) - DocumentUploadPartSize2 = 128 * 1024, // 128kb for small document ( <= 375mb ) - DocumentUploadPartSize3 = 256 * 1024, // 256kb for medium document ( <= 750mb ) - DocumentUploadPartSize4 = 512 * 1024, // 512kb for large document ( <= 1500mb ) - UploadRequestInterval = 500, // one part each half second, if not uploaded faster - - MaxPhotosInMemory = 50, // try to clear some memory after 50 photos are created - NoUpdatesTimeout = 60 * 1000, // if nothing is received in 1 min we ping - NoUpdatesAfterSleepTimeout = 60 * 1000, // if nothing is received in 1 min when was a sleepmode we ping - WaitForSkippedTimeout = 1000, // 1s wait for skipped seq or pts in updates - WaitForChannelGetDifference = 1000, // 1s wait after show channel history before sending getChannelDifference - - IdleMsecs = 60 * 1000, // after 60secs without user input we think we are idle - - SendViewsTimeout = 1000, // send views each second -}; - inline const QRegularExpression &cRussianLetters() { static QRegularExpression regexp(QString::fromUtf8("[а-яА-ЯёЁ]")); return regexp; diff --git a/Telegram/SourceFiles/core/crash_report_window.cpp b/Telegram/SourceFiles/core/crash_report_window.cpp index d13a164a9e..dfb09ea63c 100644 --- a/Telegram/SourceFiles/core/crash_report_window.cpp +++ b/Telegram/SourceFiles/core/crash_report_window.cpp @@ -806,7 +806,7 @@ void LastCrashedWindow::onNetworkSettingsSaved( proxy.user = username; proxy.password = password; - Sandbox::refreshGlobalProxy(); + Core::App().refreshGlobalProxy(); if (_updaterData && ((_updaterData->state == UpdatingCheck) diff --git a/Telegram/SourceFiles/core/single_timer.cpp b/Telegram/SourceFiles/core/single_timer.cpp deleted file mode 100644 index 81eeaad175..0000000000 --- a/Telegram/SourceFiles/core/single_timer.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* -This file is part of Telegram Desktop, -the official desktop application for the Telegram messaging service. - -For license and copyright information please follow this link: -https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL -*/ -#include "core/single_timer.h" - -#include "application.h" - -SingleTimer::SingleTimer(QObject *parent) : QTimer(parent) { - QTimer::setSingleShot(true); - Sandbox::connect(SIGNAL(adjustSingleTimers()), this, SLOT(adjust())); -} - -void SingleTimer::setTimeoutHandler(Fn handler) { - if (_handler && !handler) { - disconnect(this, SIGNAL(timeout()), this, SLOT(onTimeout())); - } else if (handler && !_handler) { - connect(this, SIGNAL(timeout()), this, SLOT(onTimeout())); - } - _handler = std::move(handler); -} - -void SingleTimer::adjust() { - auto n = getms(true); - if (isActive()) { - if (n >= _finishing) { - start(0); - } else { - start(_finishing - n); - } - } -} - -void SingleTimer::onTimeout() { - if (_handler) { - _handler(); - } -} - -void SingleTimer::start(int msec) { - _finishing = getms(true) + (msec < 0 ? 0 : msec); - QTimer::start(msec); -} - -void SingleTimer::startIfNotActive(int msec) { - if (isActive()) { - int remains = remainingTime(); - if (remains > msec) { - start(msec); - } else if (!remains) { - start(1); - } - } else { - start(msec); - } -} diff --git a/Telegram/SourceFiles/core/single_timer.h b/Telegram/SourceFiles/core/single_timer.h deleted file mode 100644 index 6b72b2fee4..0000000000 --- a/Telegram/SourceFiles/core/single_timer.h +++ /dev/null @@ -1,33 +0,0 @@ -/* -This file is part of Telegram Desktop, -the official desktop application for the Telegram messaging service. - -For license and copyright information please follow this link: -https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL -*/ -#pragma once - -class SingleTimer : public QTimer { // single shot timer with check - Q_OBJECT - -public: - SingleTimer(QObject *parent = nullptr); - - void setSingleShot(bool); // is not available - void start(); // is not available - - void setTimeoutHandler(Fn handler); - -public slots: - void start(int msec); - void startIfNotActive(int msec); - -private slots: - void adjust(); - void onTimeout(); - -private: - TimeMs _finishing = 0; - Fn _handler; - -}; diff --git a/Telegram/SourceFiles/core/utils.cpp b/Telegram/SourceFiles/core/utils.cpp index 9c3c285b4b..e010ac2ae8 100644 --- a/Telegram/SourceFiles/core/utils.cpp +++ b/Telegram/SourceFiles/core/utils.cpp @@ -8,7 +8,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/utils.h" #include "base/qthelp_url.h" -#include "application.h" +#include "base/timer.h" +#include "base/concurrent_timer.h" #include "platform/platform_specific.h" extern "C" { @@ -84,7 +85,7 @@ std::atomic GlobalAtomicRequestId = 0; } TimeId LocalUnixtime() { - return (TimeId)time(NULL); + return (TimeId)time(nullptr); } void unixtimeInit() { @@ -173,24 +174,24 @@ namespace { int _ffmpegLockManager(void **mutex, AVLockOp op) { switch (op) { case AV_LOCK_CREATE: { - Assert(*mutex == 0); + Assert(*mutex == nullptr); *mutex = reinterpret_cast(new QMutex()); } break; case AV_LOCK_OBTAIN: { - Assert(*mutex != 0); + Assert(*mutex != nullptr); reinterpret_cast(*mutex)->lock(); } break; case AV_LOCK_RELEASE: { - Assert(*mutex != 0); + Assert(*mutex != nullptr); reinterpret_cast(*mutex)->unlock(); }; break; case AV_LOCK_DESTROY: { - Assert(*mutex != 0); + Assert(*mutex != nullptr); delete reinterpret_cast(*mutex); - *mutex = 0; + *mutex = nullptr; } break; } return 0; @@ -428,7 +429,8 @@ namespace ThirdParty { bool checkms() { if (crl::adjust_time()) { - Sandbox::adjustSingleTimers(); + base::Timer::Adjust(); + base::ConcurrentTimerEnvironment::Adjust(); return true; } return false; diff --git a/Telegram/SourceFiles/data/data_pts_waiter.cpp b/Telegram/SourceFiles/data/data_pts_waiter.cpp index 4ae9c6a819..9ffb5f642c 100644 --- a/Telegram/SourceFiles/data/data_pts_waiter.cpp +++ b/Telegram/SourceFiles/data/data_pts_waiter.cpp @@ -139,7 +139,8 @@ bool PtsWaiter::updateAndApply(ChannelData *channel, int32 pts, int32 count) { return true; } -bool PtsWaiter::check(ChannelData *channel, int32 pts, int32 count) { // return false if need to save that update and apply later +// Return false if need to save that update and apply later. +bool PtsWaiter::check(ChannelData *channel, int32 pts, int32 count) { if (!inited()) { init(pts); return true; @@ -153,7 +154,7 @@ bool PtsWaiter::check(ChannelData *channel, int32 pts, int32 count) { // return } else if (_last < _count) { setWaitingForSkipped(channel, 1); } else { - setWaitingForSkipped(channel, WaitForSkippedTimeout); + setWaitingForSkipped(channel, kWaitForSkippedTimeout); } return !count; } diff --git a/Telegram/SourceFiles/data/data_pts_waiter.h b/Telegram/SourceFiles/data/data_pts_waiter.h index 7714704749..9d45c0be42 100644 --- a/Telegram/SourceFiles/data/data_pts_waiter.h +++ b/Telegram/SourceFiles/data/data_pts_waiter.h @@ -15,6 +15,10 @@ enum PtsSkippedQueue { class PtsWaiter { public: PtsWaiter() = default; + + // 1s wait for skipped seq or pts in updates. + static constexpr auto kWaitForSkippedTimeout = 1000; + void init(int32 pts) { _good = _last = _count = pts; clearSkippedUpdates(); diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp index efd816da11..a5a8a52d74 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp @@ -41,6 +41,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace { +constexpr auto kDialogsFirstLoad = 20; +constexpr auto kDialogsPerPage = 500; + QString SwitchToChooseFromQuery() { return qsl("from:"); } @@ -877,7 +880,7 @@ void DialogsWidget::loadDialogs() { } const auto firstLoad = !_dialogsOffsetDate; - const auto loadCount = firstLoad ? DialogsFirstLoad : DialogsPerPage; + const auto loadCount = firstLoad ? kDialogsFirstLoad : kDialogsPerPage; const auto flags = MTPmessages_GetDialogs::Flag::f_exclude_pinned; const auto feedId = 0; const auto hash = 0; diff --git a/Telegram/SourceFiles/facades.h b/Telegram/SourceFiles/facades.h index 7469866a35..821d61be94 100644 --- a/Telegram/SourceFiles/facades.h +++ b/Telegram/SourceFiles/facades.h @@ -182,8 +182,6 @@ void WorkingDirReady(); void WriteInstallBetaVersionsSetting(); void WriteDebugModeSetting(); -void MainThreadTaskAdded(); - void start(); bool started(); void finish(); diff --git a/Telegram/SourceFiles/history/history_inner_widget.h b/Telegram/SourceFiles/history/history_inner_widget.h index 75877a1680..17c5874425 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.h +++ b/Telegram/SourceFiles/history/history_inner_widget.h @@ -39,7 +39,7 @@ class HistoryInner : public Ui::RpWidget , public Ui::AbstractTooltipShower , private base::Subscriber { - // The Q_OBJECT meta info is used for qobject_cast to HistoryInner! + // The Q_OBJECT meta info is used for qobject_cast! Q_OBJECT public: diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 25d3b29543..958de686cd 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -176,6 +176,7 @@ HistoryWidget::HistoryWidget( QWidget *parent, not_null controller) : Window::AbstractSectionWidget(parent, controller) +, _updateEditTimeLeftDisplay([=] { updateField(); }) , _fieldBarCancel(this, st::historyReplyCancel) , _previewTimer([=] { requestPreview(); }) , _topBar(this, controller) @@ -360,8 +361,6 @@ HistoryWidget::HistoryWidget( ActivateWindow(this->controller()); }); - connect(&_updateEditTimeLeftDisplay, SIGNAL(timeout()), this, SLOT(updateField())); - subscribe(Adaptive::Changed(), [this] { update(); }); Auth().data().itemRemoved( ) | rpl::start_with_next( @@ -6425,7 +6424,7 @@ void HistoryWidget::paintEditHeader(Painter &p, const QRect &rect, int left, int // Restart timer only if we are sure that we've painted the whole timer. if (rect.contains(myrtlrect(left, top, width() - left, st::normalFont->height)) && updateIn > 0) { - _updateEditTimeLeftDisplay.start(updateIn); + _updateEditTimeLeftDisplay.callOnce(updateIn); } if (!editTimeLeftText.isEmpty()) { diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 62bf01413e..c8fa56e670 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -11,7 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwidget.h" #include "chat_helpers/field_autocomplete.h" #include "window/section_widget.h" -#include "core/single_timer.h" #include "ui/widgets/input_fields.h" #include "ui/rp_widget.h" #include "base/flags.h" @@ -358,14 +357,13 @@ private slots: void onModerateKeyActivate(int index, bool *outHandled); - void updateField(); - private: using TabbedPanel = ChatHelpers::TabbedPanel; using TabbedSelector = ChatHelpers::TabbedSelector; using DragState = Storage::MimeDataState; void initTabbedSelector(); + void updateField(); void send(Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers()); void handlePendingHistoryUpdate(); @@ -516,7 +514,7 @@ private: HistoryItem *_replyEditMsg = nullptr; Text _replyEditMsgText; - mutable SingleTimer _updateEditTimeLeftDisplay; + mutable base::Timer _updateEditTimeLeftDisplay; object_ptr _fieldBarCancel; void updateReplyEditTexts(bool force = false); diff --git a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp index 9f53d47c2b..ed27dfbf1a 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp @@ -40,7 +40,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "auth_session.h" #include "messenger.h" #include "apiwrap.h" -#include "application.h" #include "styles/style_info.h" #include "styles/style_boxes.h" @@ -293,7 +292,7 @@ object_ptr DetailsFiller::setupInfo() { auto link = Messenger::Instance().createInternalLinkFull( peer->userName()); if (!link.isEmpty()) { - Application::clipboard()->setText(link); + QApplication::clipboard()->setText(link); Ui::Toast::Show(lang(lng_username_copied)); } return false; diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 9176352cf7..a8629e00b0 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -100,6 +100,23 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace { +constexpr auto kChannelGetDifferenceLimit = 100; + +// 1s wait after show channel history before sending getChannelDifference. +constexpr auto kWaitForChannelGetDifference = TimeMs(1000); + +// If nothing is received in 1 min we ping. +constexpr auto kNoUpdatesTimeout = 60 * 1000; + +// If nothing is received in 1 min when was a sleepmode we ping. +constexpr auto kNoUpdatesAfterSleepTimeout = 60 * TimeMs(1000); + +// Send channel views each second. +constexpr auto kSendViewsTimeout = TimeMs(1000); + +// Cache background scaled image after 3s. +constexpr auto kCacheBackgroundTimeout = 3000; + bool IsForceLogoutNotification(const MTPDupdateServiceNotification &data) { return qs(data.vtype).startsWith(qstr("AUTH_KEY_DROP_")); } @@ -239,7 +256,16 @@ MainWidget::MainWidget( this, _controller, Media::Player::Panel::Layout::OnlyPlaylist) -, _playerPanel(this, _controller, Media::Player::Panel::Layout::Full) { +, _playerPanel(this, _controller, Media::Player::Panel::Layout::Full) +, _noUpdatesTimer([=] { sendPing(); }) +, _byPtsTimer([=] { getDifferenceByPts(); }) +, _bySeqTimer([=] { getDifference(); }) +, _byMinChannelTimer([=] { getDifference(); }) +, _onlineTimer([=] { updateOnline(); }) +, _idleFinishTimer([=] { checkIdleFinish(); }) +, _failDifferenceTimer([=] { getDifferenceAfterFail(); }) +, _cacheBackgroundTimer([=] { cacheBackground(); }) +, _viewsIncrementTimer([=] { viewsIncrement(); }) { Messenger::Instance().mtp()->setUpdatesHandler(rpcDone(&MainWidget::updateReceived)); Messenger::Instance().mtp()->setGlobalFailHandler(rpcFail(&MainWidget::updateFail)); @@ -256,13 +282,6 @@ MainWidget::MainWidget( connect(_dialogs, SIGNAL(cancelled()), this, SLOT(dialogsCancelled())); connect(this, SIGNAL(dialogsUpdated()), _dialogs, SLOT(onListScroll())); connect(_history, SIGNAL(cancelled()), _dialogs, SLOT(activate())); - connect(&noUpdatesTimer, SIGNAL(timeout()), this, SLOT(mtpPing())); - connect(&_onlineTimer, SIGNAL(timeout()), this, SLOT(updateOnline())); - connect(&_idleFinishTimer, SIGNAL(timeout()), this, SLOT(checkIdleFinish())); - connect(&_bySeqTimer, SIGNAL(timeout()), this, SLOT(getDifference())); - connect(&_byPtsTimer, SIGNAL(timeout()), this, SLOT(onGetDifferenceTimeByPts())); - connect(&_byMinChannelTimer, SIGNAL(timeout()), this, SLOT(getDifference())); - connect(&_failDifferenceTimer, SIGNAL(timeout()), this, SLOT(onGetDifferenceTimeAfterFail())); subscribe(Media::Player::Updated(), [this](const AudioMsgId &audioId) { if (audioId.type() != AudioMsgId::Type::Video) { handleAudioUpdate(audioId); @@ -310,15 +329,12 @@ MainWidget::MainWidget( QCoreApplication::instance()->installEventFilter(this); - connect(&_viewsIncrementTimer, SIGNAL(timeout()), this, SLOT(onViewsIncrement())); - using Update = Window::Theme::BackgroundUpdate; subscribe(Window::Theme::Background(), [this](const Update &update) { if (update.type == Update::Type::New || update.type == Update::Type::Changed) { clearCachedBackground(); } }); - connect(&_cacheBackgroundTimer, SIGNAL(timeout()), this, SLOT(onCacheBackground())); _playerPanel->setPinCallback([this] { switchToFixedPlayer(); }); _playerPanel->setCloseCallback([this] { closeBothPlayers(); }); @@ -925,7 +941,7 @@ bool MainWidget::sendMessageFail(const RPCError &error) { return false; } -void MainWidget::onCacheBackground() { +void MainWidget::cacheBackground() { if (Window::Theme::Background()->color()) { return; } else if (Window::Theme::Background()->tile()) { @@ -1025,7 +1041,7 @@ void MainWidget::itemEdited(not_null item) { void MainWidget::checkLastUpdate(bool afterSleep) { auto n = getms(true); - if (_lastUpdateTime && n > _lastUpdateTime + (afterSleep ? NoUpdatesAfterSleepTimeout : NoUpdatesTimeout)) { + if (_lastUpdateTime && n > _lastUpdateTime + (afterSleep ? kNoUpdatesAfterSleepTimeout : kNoUpdatesTimeout)) { _lastUpdateTime = n; MTP::ping(); } @@ -1399,7 +1415,7 @@ bool MainWidget::isIdle() const { void MainWidget::clearCachedBackground() { _cachedBackground = QPixmap(); - _cacheBackgroundTimer.stop(); + _cacheBackgroundTimer.cancel(); update(); } @@ -1411,7 +1427,7 @@ QPixmap MainWidget::cachedBackground(const QRect &forRect, int &x, int &y) { } if (_willCacheFor != forRect || !_cacheBackgroundTimer.isActive()) { _willCacheFor = forRect; - _cacheBackgroundTimer.start(CacheBackgroundTimeout); + _cacheBackgroundTimer.callOnce(kCacheBackgroundTimeout); } return QPixmap(); } @@ -1562,12 +1578,12 @@ void MainWidget::scheduleViewIncrement(HistoryItem *item) { auto j = _viewsToIncrement.find(peer); if (j == _viewsToIncrement.cend()) { j = _viewsToIncrement.insert(peer, ViewsIncrementMap()); - _viewsIncrementTimer.start(SendViewsTimeout); + _viewsIncrementTimer.callOnce(kSendViewsTimeout); } j.value().insert(item->id, true); } -void MainWidget::onViewsIncrement() { +void MainWidget::viewsIncrement() { for (auto i = _viewsToIncrement.begin(); i != _viewsToIncrement.cend();) { if (_viewsIncrementRequests.contains(i.key())) { ++i; @@ -1603,7 +1619,7 @@ void MainWidget::viewsIncrementDone(QVector ids, const MTPVector } } if (!_viewsToIncrement.isEmpty() && !_viewsIncrementTimer.isActive()) { - _viewsIncrementTimer.start(SendViewsTimeout); + _viewsIncrementTimer.callOnce(kSendViewsTimeout); } } @@ -1617,7 +1633,7 @@ bool MainWidget::viewsIncrementFail(const RPCError &error, mtpRequestId req) { } } if (!_viewsToIncrement.isEmpty() && !_viewsIncrementTimer.isActive()) { - _viewsIncrementTimer.start(SendViewsTimeout); + _viewsIncrementTimer.callOnce(kSendViewsTimeout); } return false; } @@ -1775,7 +1791,7 @@ void MainWidget::ui_showPeerHistory( if (nowActivePeer && nowActivePeer != wasActivePeer) { if (const auto channel = nowActivePeer->asChannel()) { channel->ptsWaitingForShortPoll( - WaitForChannelGetDifference); + kWaitForChannelGetDifference); } _viewsIncremented.remove(nowActivePeer); } @@ -2787,7 +2803,7 @@ void MainWidget::keyPressEvent(QKeyEvent *e) { bool MainWidget::eventFilter(QObject *o, QEvent *e) { if (e->type() == QEvent::FocusIn) { - if (auto widget = qobject_cast(o)) { + if (const auto widget = qobject_cast(o)) { if (_history == widget || _history->isAncestorOf(widget) || (_mainSection && (_mainSection == widget || _mainSection->isAncestorOf(widget))) || (_thirdSection && (_thirdSection == widget || _thirdSection->isAncestorOf(widget)))) { @@ -2928,7 +2944,9 @@ void MainWidget::updSetState(int32 pts, int32 date, int32 qts, int32 seq) { } if (seq && seq != updSeq) { updSeq = seq; - if (_bySeqTimer.isActive()) _bySeqTimer.stop(); + if (_bySeqTimer.isActive()) { + _bySeqTimer.cancel(); + } for (QMap::iterator i = _bySeqUpdates.begin(); i != _bySeqUpdates.end();) { int32 s = i.key(); if (s <= seq + 1) { @@ -2938,7 +2956,9 @@ void MainWidget::updSetState(int32 pts, int32 date, int32 qts, int32 seq) { return feedUpdates(v); } } else { - if (!_bySeqTimer.isActive()) _bySeqTimer.start(WaitForSkippedTimeout); + if (!_bySeqTimer.isActive()) { + _bySeqTimer.callOnce(PtsWaiter::kWaitForSkippedTimeout); + } break; } } @@ -3008,7 +3028,9 @@ void MainWidget::gotChannelDifference( MTP_LOG(0, ("getChannelDifference { good - after not final channelDifference was received }%1").arg(cTestMode() ? " TESTMODE" : "")); getChannelDifference(channel); } else if (_controller->activeChatCurrent().peer() == channel) { - channel->ptsWaitingForShortPoll(timeout ? (timeout * 1000) : WaitForChannelGetDifference); + channel->ptsWaitingForShortPoll(timeout + ? (timeout * TimeMs(1000)) + : kWaitForChannelGetDifference); } } @@ -3037,7 +3059,7 @@ void MainWidget::gotState(const MTPupdates_State &state) { updSetState(d.vpts.v, d.vdate.v, d.vqts.v, d.vseq.v); _lastUpdateTime = getms(true); - noUpdatesTimer.start(NoUpdatesTimeout); + _noUpdatesTimer.callOnce(kNoUpdatesTimeout); _ptsWaiter.setRequesting(false); _dialogs->loadDialogs(); @@ -3053,7 +3075,7 @@ void MainWidget::gotDifference(const MTPupdates_Difference &difference) { updSetState(_ptsWaiter.current(), d.vdate.v, updQts, d.vseq.v); _lastUpdateTime = getms(true); - noUpdatesTimer.start(NoUpdatesTimeout); + _noUpdatesTimer.callOnce(kNoUpdatesTimeout); _ptsWaiter.setRequesting(false); } break; @@ -3125,7 +3147,7 @@ bool MainWidget::getDifferenceTimeChanged(ChannelData *channel, int32 ms, Channe void MainWidget::ptsWaiterStartTimerFor(ChannelData *channel, int32 ms) { if (getDifferenceTimeChanged(channel, ms, _channelGetDifferenceTimeByPts, _getDifferenceTimeByPts)) { - onGetDifferenceTimeByPts(); + getDifferenceByPts(); } } @@ -3140,7 +3162,7 @@ void MainWidget::failDifferenceStartTimerFor(ChannelData *channel) { : i.value(); }(); if (getDifferenceTimeChanged(channel, timeout * 1000, _channelGetDifferenceTimeAfterFail, _getDifferenceTimeAfterFail)) { - onGetDifferenceTimeAfterFail(); + getDifferenceAfterFail(); } if (timeout < 64) timeout *= 2; } @@ -3174,11 +3196,11 @@ bool MainWidget::failDifference(const RPCError &error) { if (MTP::isDefaultHandledError(error)) return false; LOG(("RPC Error in getDifference: %1 %2: %3").arg(error.code()).arg(error.type()).arg(error.description())); - failDifferenceStartTimerFor(0); + failDifferenceStartTimerFor(nullptr); return true; } -void MainWidget::onGetDifferenceTimeByPts() { +void MainWidget::getDifferenceByPts() { auto now = getms(true), wait = 0LL; if (_getDifferenceTimeByPts) { if (_getDifferenceTimeByPts > now) { @@ -3197,13 +3219,13 @@ void MainWidget::onGetDifferenceTimeByPts() { } } if (wait) { - _byPtsTimer.start(wait); + _byPtsTimer.callOnce(wait); } else { - _byPtsTimer.stop(); + _byPtsTimer.cancel(); } } -void MainWidget::onGetDifferenceTimeAfterFail() { +void MainWidget::getDifferenceAfterFail() { auto now = getms(true), wait = 0LL; if (_getDifferenceTimeAfterFail) { if (_getDifferenceTimeAfterFail > now) { @@ -3224,9 +3246,9 @@ void MainWidget::onGetDifferenceTimeAfterFail() { } } if (wait) { - _failDifferenceTimer.start(wait); + _failDifferenceTimer.callOnce(wait); } else { - _failDifferenceTimer.stop(); + _failDifferenceTimer.cancel(); } } @@ -3238,14 +3260,22 @@ void MainWidget::getDifference() { if (requestingDifference()) return; _bySeqUpdates.clear(); - _bySeqTimer.stop(); + _bySeqTimer.cancel(); - noUpdatesTimer.stop(); + _noUpdatesTimer.cancel(); _getDifferenceTimeAfterFail = 0; _ptsWaiter.setRequesting(true); - MTP::send(MTPupdates_GetDifference(MTP_flags(0), MTP_int(_ptsWaiter.current()), MTPint(), MTP_int(updDate), MTP_int(updQts)), rpcDone(&MainWidget::gotDifference), rpcFail(&MainWidget::failDifference)); + MTP::send( + MTPupdates_GetDifference( + MTP_flags(0), + MTP_int(_ptsWaiter.current()), + MTPint(), + MTP_int(updDate), + MTP_int(updQts)), + rpcDone(&MainWidget::gotDifference), + rpcFail(&MainWidget::failDifference)); } void MainWidget::getChannelDifference(ChannelData *channel, ChannelDifferenceRequest from) { @@ -3270,10 +3300,18 @@ void MainWidget::getChannelDifference(ChannelData *channel, ChannelDifferenceReq flags = 0; // No force flag when requesting for short poll. } } - MTP::send(MTPupdates_GetChannelDifference(MTP_flags(flags), channel->inputChannel, filter, MTP_int(channel->pts()), MTP_int(MTPChannelGetDifferenceLimit)), rpcDone(&MainWidget::gotChannelDifference, channel), rpcFail(&MainWidget::failChannelDifference, channel)); + MTP::send( + MTPupdates_GetChannelDifference( + MTP_flags(flags), + channel->inputChannel, + filter, + MTP_int(channel->pts()), + MTP_int(kChannelGetDifferenceLimit)), + rpcDone(&MainWidget::gotChannelDifference, channel), + rpcFail(&MainWidget::failChannelDifference, channel)); } -void MainWidget::mtpPing() { +void MainWidget::sendPing() { MTP::ping(); } @@ -3470,7 +3508,7 @@ void MainWidget::incrementSticker(DocumentData *sticker) { it->stickers.removeAt(index); for (auto i = it->emoji.begin(); i != it->emoji.end();) { if (const auto index = i->indexOf(sticker); index >= 0) { - removedFromEmoji.push_back(i.key()); + removedFromEmoji.emplace_back(i.key()); i->removeAt(index); if (i->isEmpty()) { i = it->emoji.erase(i); @@ -3620,7 +3658,7 @@ void MainWidget::updateOnline(bool gotOtherOffline) { isOnline = false; if (!_isIdle) { _isIdle = true; - _idleFinishTimer.start(900); + _idleFinishTimer.callOnce(900); } } else { updateIn = qMin(updateIn, int(Global::OfflineIdleTimeout() - idle)); @@ -3652,7 +3690,7 @@ void MainWidget::updateOnline(bool gotOtherOffline) { } else if (isOnline) { updateIn = qMin(updateIn, int(_lastSetOnline + Global::OnlineUpdatePeriod() - ms)); } - _onlineTimer.start(updateIn); + _onlineTimer.callOnce(updateIn); } void MainWidget::saveDraftToCloud() { @@ -3703,14 +3741,13 @@ void MainWidget::writeDrafts(History *history) { } void MainWidget::checkIdleFinish() { - if (this != App::main()) return; if (psIdleTime() < Global::OfflineIdleTimeout()) { - _idleFinishTimer.stop(); + _idleFinishTimer.cancel(); _isIdle = false; updateOnline(); - if (App::wnd()) App::wnd()->checkHistoryActivation(); + App::wnd()->checkHistoryActivation(); } else { - _idleFinishTimer.start(900); + _idleFinishTimer.callOnce(900); } } @@ -3734,7 +3771,7 @@ void MainWidget::updateReceived(const mtpPrime *from, const mtpPrime *end) { updates.read(from, end); _lastUpdateTime = getms(true); - noUpdatesTimer.start(NoUpdatesTimeout); + _noUpdatesTimer.callOnce(kNoUpdatesTimeout); if (!requestingDifference() || HasForceLogoutNotification(updates)) { feedUpdates(updates); @@ -3860,7 +3897,8 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) { } if (d.vseq.v > updSeq + 1) { _bySeqUpdates.insert(d.vseq.v, updates); - return _bySeqTimer.start(WaitForSkippedTimeout); + _bySeqTimer.callOnce(PtsWaiter::kWaitForSkippedTimeout); + return; } } @@ -3879,7 +3917,8 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) { } if (d.vseq_start.v > updSeq + 1) { _bySeqUpdates.insert(d.vseq_start.v, updates); - return _bySeqTimer.start(WaitForSkippedTimeout); + _bySeqTimer.callOnce(PtsWaiter::kWaitForSkippedTimeout); + return; } } @@ -4019,7 +4058,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { } if (!_byMinChannelTimer.isActive()) { // getDifference after timeout - _byMinChannelTimer.start(WaitForSkippedTimeout); + _byMinChannelTimer.callOnce(PtsWaiter::kWaitForSkippedTimeout); } return; } @@ -4068,7 +4107,7 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { if (!channel) { if (!_byMinChannelTimer.isActive()) { // getDifference after timeout. - _byMinChannelTimer.start(WaitForSkippedTimeout); + _byMinChannelTimer.callOnce(PtsWaiter::kWaitForSkippedTimeout); } return; } @@ -4615,13 +4654,13 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { auto &v = set.vdocuments.v; it->stickers.clear(); it->stickers.reserve(v.size()); - for (int i = 0, l = v.size(); i < l; ++i) { - const auto doc = Auth().data().document(v.at(i)); - if (!doc->sticker()) continue; + for (const auto &item : v) { + const auto document = Auth().data().document(item); + if (!document->sticker()) continue; - it->stickers.push_back(doc); - if (doc->sticker()->set.type() != mtpc_inputStickerSetID) { - doc->sticker()->set = inputSet; + it->stickers.push_back(document); + if (document->sticker()->set.type() != mtpc_inputStickerSetID) { + document->sticker()->set = inputSet; } } it->emoji.clear(); @@ -4678,11 +4717,11 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { auto &order = d.vorder.v; auto &sets = Auth().data().stickerSets(); Stickers::Order result; - for (int i = 0, l = order.size(); i < l; ++i) { - if (sets.constFind(order.at(i).v) == sets.cend()) { + for (const auto &item : order) { + if (sets.constFind(item.v) == sets.cend()) { break; } - result.push_back(order.at(i).v); + result.push_back(item.v); } if (result.size() != Auth().data().stickerSetsOrder().size() || result.size() != order.size()) { Auth().data().setLastStickersUpdate(0); diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index dab9e9ec59..5a67724a90 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once -#include "core/single_timer.h" +#include "base/timer.h" #include "base/weak_ptr.h" #include "ui/rp_widget.h" #include "media/player/media_player_float.h" @@ -257,11 +257,14 @@ public: void feedChannelDifference(const MTPDupdates_channelDifference &data); - // Mayde public for ApiWrap, while it is still here. + // Made public for ApiWrap, while it is still here. // Better would be for this to be moved to ApiWrap. bool requestingDifference() const { return _ptsWaiter.requesting(); } + void getDifference(); + void updateOnline(bool gotOtherOffline = false); + void checkIdleFinish(); bool contentOverlapped(const QRect &globalRect); @@ -307,18 +310,6 @@ public slots: void dialogsCancelled(); - void getDifference(); - void onGetDifferenceTimeByPts(); - void onGetDifferenceTimeAfterFail(); - void mtpPing(); - - void updateOnline(bool gotOtherOffline = false); - void checkIdleFinish(); - - void onCacheBackground(); - - void onViewsIncrement(); - protected: void paintEvent(QPaintEvent *e) override; void resizeEvent(QResizeEvent *e) override; @@ -343,6 +334,11 @@ private: UserData *from; }; + void viewsIncrement(); + void sendPing(); + void getDifferenceByPts(); + void getDifferenceAfterFail(); + void animationCallback(); void handleAdaptiveLayoutUpdate(); void updateWindowAdaptiveLayout(); @@ -429,6 +425,7 @@ private: void showAll(); void clearHider(not_null instance); + void cacheBackground(); void clearCachedBackground(); not_null floatPlayerDelegate(); @@ -510,7 +507,7 @@ private: int32 updDate = 0; int32 updQts = -1; int32 updSeq = 0; - SingleTimer noUpdatesTimer; + base::Timer _noUpdatesTimer; PtsWaiter _ptsWaiter; @@ -518,22 +515,23 @@ private: TimeMs _getDifferenceTimeByPts = 0; TimeMs _getDifferenceTimeAfterFail = 0; - SingleTimer _byPtsTimer; + base::Timer _byPtsTimer; QMap _bySeqUpdates; - SingleTimer _bySeqTimer; + base::Timer _bySeqTimer; - SingleTimer _byMinChannelTimer; + base::Timer _byMinChannelTimer; mtpRequestId _onlineRequest = 0; - SingleTimer _onlineTimer, _idleFinishTimer; + base::Timer _onlineTimer; + base::Timer _idleFinishTimer; bool _lastWasOnline = false; TimeMs _lastSetOnline = 0; bool _isIdle = false; int32 _failDifferenceTimeout = 1; // growing timeout for getDifference calls, if it fails QMap _channelFailDifferenceTimeout; // growing timeout for getChannelDifference calls, if it fails - SingleTimer _failDifferenceTimer; + base::Timer _failDifferenceTimer; TimeMs _lastUpdateTime = 0; bool _handlingChannelDifference = false; @@ -542,7 +540,7 @@ private: QRect _cachedFor, _willCacheFor; int _cachedX = 0; int _cachedY = 0; - SingleTimer _cacheBackgroundTimer; + base::Timer _cacheBackgroundTimer; PhotoData *_deletingPhoto = nullptr; @@ -550,7 +548,7 @@ private: QMap _viewsIncremented, _viewsToIncrement; QMap _viewsIncrementRequests; QMap _viewsIncrementByRequest; - SingleTimer _viewsIncrementTimer; + base::Timer _viewsIncrementTimer; struct SettingBackground; std::unique_ptr _background; diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 4416617331..1e90896d1b 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -714,7 +714,7 @@ void MainWindow::toggleDisplayNotifyFromTray() { } void MainWindow::closeEvent(QCloseEvent *e) { - if (Sandbox::isSavingSession()) { + if (Core::App().isSavingSession()) { e->accept(); App::quit(); } else { diff --git a/Telegram/SourceFiles/mainwindow.h b/Telegram/SourceFiles/mainwindow.h index 6a53d208a6..99a19c5f68 100644 --- a/Telegram/SourceFiles/mainwindow.h +++ b/Telegram/SourceFiles/mainwindow.h @@ -9,7 +9,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "platform/platform_specific.h" #include "platform/platform_main_window.h" -#include "core/single_timer.h" #include "base/unique_qptr.h" class MainWidget; diff --git a/Telegram/SourceFiles/mediaview.cpp b/Telegram/SourceFiles/mediaview.cpp index 51d7db50e5..f33e161f8a 100644 --- a/Telegram/SourceFiles/mediaview.cpp +++ b/Telegram/SourceFiles/mediaview.cpp @@ -906,7 +906,7 @@ void MediaView::onSaveAs() { })); } activateWindow(); - Sandbox::setActiveWindow(this); + Core::App().setActiveWindow(this); setFocus(); } @@ -1777,7 +1777,7 @@ void MediaView::displayFinished() { show(); psShowOverAll(this); activateWindow(); - Sandbox::setActiveWindow(this); + Core::App().setActiveWindow(this); setFocus(); } } diff --git a/Telegram/SourceFiles/messenger.cpp b/Telegram/SourceFiles/messenger.cpp index aa6521b50a..fb847268de 100644 --- a/Telegram/SourceFiles/messenger.cpp +++ b/Telegram/SourceFiles/messenger.cpp @@ -79,6 +79,7 @@ struct Messenger::Private { Messenger::Messenger(not_null launcher) : QObject() , _launcher(launcher) +, _killDownloadSessionsTimer([=] { killDownloadSessions(); }) , _private(std::make_unique()) , _databases(std::make_unique()) , _langpack(std::make_unique()) @@ -95,7 +96,7 @@ Messenger::Messenger(not_null launcher) ThirdParty::start(); Global::start(); - Sandbox::refreshGlobalProxy(); // Depends on Global::started(). + Core::App().refreshGlobalProxy(); // Depends on Global::started(). startLocalStorage(); @@ -110,7 +111,7 @@ Messenger::Messenger(not_null launcher) } _translator = std::make_unique(); - QCoreApplication::instance()->installTranslator(_translator.get()); + qApp->installTranslator(_translator.get()); style::startManager(); anim::startManager(); @@ -120,12 +121,10 @@ Messenger::Messenger(not_null launcher) DEBUG_LOG(("Application Info: inited...")); - QCoreApplication::instance()->installNativeEventFilter(psNativeEventFilter()); + QApplication::instance()->installNativeEventFilter(psNativeEventFilter()); cChangeTimeFormat(QLocale::system().timeFormat(QLocale::ShortFormat)); - connect(&killDownloadSessionsTimer, SIGNAL(timeout()), this, SLOT(killDownloadSessions())); - DEBUG_LOG(("Application Info: starting app...")); // Create mime database, so it won't be slow later. @@ -138,8 +137,12 @@ Messenger::Messenger(not_null launcher) _mediaView = std::make_unique(); _window->setGeometry(currentGeometry); - QCoreApplication::instance()->installEventFilter(this); - Sandbox::connect(SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(onAppStateChanged(Qt::ApplicationState))); + qApp->installEventFilter(this); + connect( + qApp, + SIGNAL(applicationStateChanged(Qt::ApplicationState)), + this, + SLOT(onAppStateChanged(Qt::ApplicationState))); DEBUG_LOG(("Application Info: window created...")); @@ -252,13 +255,13 @@ bool Messenger::eventFilter(QObject *object, QEvent *e) { } break; case QEvent::ApplicationActivate: { - if (object == QCoreApplication::instance()) { + if (object == qApp) { psUserActionDone(); } } break; case QEvent::FileOpen: { - if (object == QCoreApplication::instance()) { + if (object == qApp) { auto url = QString::fromUtf8(static_cast(e)->url().toEncoded().trimmed()); if (url.startsWith(qstr("tg://"), Qt::CaseInsensitive)) { cSetStartUrl(url.mid(0, 8192)); @@ -289,7 +292,7 @@ void Messenger::setCurrentProxy( : ProxyData())); Global::SetSelectedProxy(proxy); Global::SetProxySettings(settings); - Sandbox::refreshGlobalProxy(); + Core::App().refreshGlobalProxy(); if (_mtproto) { _mtproto->restart(); if (previousKey != key(proxy)) { @@ -527,13 +530,18 @@ void Messenger::destroyMtpKeys(MTP::AuthKeysList &&keys) { destroyConfig.mainDcId = MTP::Instance::Config::kNoneMainDc; destroyConfig.keys = std::move(keys); _mtprotoForKeysDestroy = std::make_unique(_dcOptions.get(), MTP::Instance::Mode::KeysDestroyer, std::move(destroyConfig)); - connect(_mtprotoForKeysDestroy.get(), SIGNAL(allKeysDestroyed()), this, SLOT(onAllKeysDestroyed())); + connect( + _mtprotoForKeysDestroy.get(), + &MTP::Instance::allKeysDestroyed, + [=] { allKeysDestroyed(); }); } -void Messenger::onAllKeysDestroyed() { +void Messenger::allKeysDestroyed() { LOG(("MTP Info: all keys scheduled for destroy are destroyed.")); - _mtprotoForKeysDestroy.reset(); - Local::writeMtpData(); + crl::on_main(this, [=] { + _mtprotoForKeysDestroy = nullptr; + Local::writeMtpData(); + }); } void Messenger::suggestMainDcId(MTP::DcId mainDcId) { @@ -601,18 +609,19 @@ void Messenger::startLocalStorage() { } void Messenger::killDownloadSessionsStart(MTP::DcId dcId) { - if (killDownloadSessionTimes.constFind(dcId) == killDownloadSessionTimes.cend()) { - killDownloadSessionTimes.insert(dcId, getms() + MTPAckSendWaiting + MTPKillFileSessionTimeout); + if (!_killDownloadSessionTimes.contains(dcId)) { + _killDownloadSessionTimes.emplace(dcId, getms() + MTPAckSendWaiting + MTPKillFileSessionTimeout); } - if (!killDownloadSessionsTimer.isActive()) { - killDownloadSessionsTimer.start(MTPAckSendWaiting + MTPKillFileSessionTimeout + 5); + if (!_killDownloadSessionsTimer.isActive()) { + _killDownloadSessionsTimer.callOnce(MTPAckSendWaiting + MTPKillFileSessionTimeout + 5); } } void Messenger::killDownloadSessionsStop(MTP::DcId dcId) { - killDownloadSessionTimes.remove(dcId); - if (killDownloadSessionTimes.isEmpty() && killDownloadSessionsTimer.isActive()) { - killDownloadSessionsTimer.stop(); + _killDownloadSessionTimes.erase(dcId); + if (_killDownloadSessionTimes.empty() + && _killDownloadSessionsTimer.isActive()) { + _killDownloadSessionsTimer.cancel(); } } @@ -673,25 +682,25 @@ void Messenger::call_handleObservables() { void Messenger::killDownloadSessions() { auto ms = getms(), left = static_cast(MTPAckSendWaiting) + MTPKillFileSessionTimeout; - for (auto i = killDownloadSessionTimes.begin(); i != killDownloadSessionTimes.end(); ) { - if (i.value() <= ms) { + for (auto i = _killDownloadSessionTimes.begin(); i != _killDownloadSessionTimes.end(); ) { + if (i->second <= ms) { for (int j = 0; j < MTP::kDownloadSessionsCount; ++j) { - MTP::stopSession(MTP::downloadDcId(i.key(), j)); + MTP::stopSession(MTP::downloadDcId(i->first, j)); } - i = killDownloadSessionTimes.erase(i); + i = _killDownloadSessionTimes.erase(i); } else { - if (i.value() - ms < left) { - left = i.value() - ms; + if (i->second - ms < left) { + left = i->second - ms; } ++i; } } - if (!killDownloadSessionTimes.isEmpty()) { - killDownloadSessionsTimer.start(left); + if (!_killDownloadSessionTimes.empty()) { + _killDownloadSessionsTimer.callOnce(left); } } -void Messenger::onSwitchDebugMode() { +void Messenger::switchDebugMode() { if (Logs::DebugEnabled()) { Logs::SetDebugEnabled(false); Sandbox::WriteDebugModeSetting(); @@ -704,14 +713,14 @@ void Messenger::onSwitchDebugMode() { } } -void Messenger::onSwitchWorkMode() { +void Messenger::switchWorkMode() { Global::SetDialogsModeEnabled(!Global::DialogsModeEnabled()); Global::SetDialogsMode(Dialogs::Mode::All); Local::writeUserSettings(); App::restart(); } -void Messenger::onSwitchTestMode() { +void Messenger::switchTestMode() { if (cTestMode()) { QFile(cWorkingDir() + qsl("tdata/withtestmode")).remove(); cSetTestMode(false); @@ -971,7 +980,7 @@ QWidget *Messenger::getFileDialogParent() { void Messenger::checkMediaViewActivation() { if (_mediaView && !_mediaView->isHidden()) { _mediaView->activateWindow(); - Sandbox::setActiveWindow(_mediaView.get()); + Core::App().setActiveWindow(_mediaView.get()); _mediaView->setFocus(); } } @@ -1062,7 +1071,7 @@ void Messenger::unregisterLeaveSubscription(QWidget *widget) { void Messenger::QuitAttempt() { auto prevents = false; - if (!Sandbox::isSavingSession() && AuthSession::Exists()) { + if (AuthSession::Exists() && !Core::App().isSavingSession()) { if (Auth().api().isQuitPrevent()) { prevents = true; } @@ -1073,7 +1082,7 @@ void Messenger::QuitAttempt() { if (prevents) { Instance().quitDelayed(); } else { - QCoreApplication::quit(); + QApplication::quit(); } } @@ -1085,7 +1094,7 @@ void Messenger::quitPreventFinished() { void Messenger::quitDelayed() { if (!_private->quitTimer.isActive()) { - _private->quitTimer.setCallback([] { QCoreApplication::quit(); }); + _private->quitTimer.setCallback([] { QApplication::quit(); }); _private->quitTimer.callOnce(kQuitPreventTimeoutMs); } } diff --git a/Telegram/SourceFiles/messenger.h b/Telegram/SourceFiles/messenger.h index c4185b82fa..29fbd9820d 100644 --- a/Telegram/SourceFiles/messenger.h +++ b/Telegram/SourceFiles/messenger.h @@ -199,6 +199,10 @@ public: void handleAppActivated(); void handleAppDeactivated(); + void switchDebugMode(); + void switchWorkMode(); + void switchTestMode(); + void call_handleUnreadCounterUpdate(); void call_handleDelayedPeerUpdates(); void call_handleObservables(); @@ -211,17 +215,13 @@ protected: bool eventFilter(QObject *object, QEvent *event) override; public slots: - void onAllKeysDestroyed(); - - void onSwitchDebugMode(); - void onSwitchWorkMode(); - void onSwitchTestMode(); - void killDownloadSessions(); void onAppStateChanged(Qt::ApplicationState state); private: void destroyMtpKeys(MTP::AuthKeysList &&keys); + void allKeysDestroyed(); + void startLocalStorage(); void startShortcuts(); @@ -238,8 +238,8 @@ private: not_null _launcher; - QMap killDownloadSessionTimes; - SingleTimer killDownloadSessionsTimer; + base::flat_map _killDownloadSessionTimes; + base::Timer _killDownloadSessionsTimer; // Some fields are just moved from the declaration. struct Private; diff --git a/Telegram/SourceFiles/mtproto/connection.h b/Telegram/SourceFiles/mtproto/connection.h index feb2f61066..bed2668786 100644 --- a/Telegram/SourceFiles/mtproto/connection.h +++ b/Telegram/SourceFiles/mtproto/connection.h @@ -37,6 +37,7 @@ class RSAPublicKey; struct ConnectionOptions; class Thread : public QThread { + // The Q_OBJECT meta info is used for qobject_cast! Q_OBJECT public: diff --git a/Telegram/SourceFiles/mtproto/facade.h b/Telegram/SourceFiles/mtproto/facade.h index 1cc586a793..16ed4c0f9f 100644 --- a/Telegram/SourceFiles/mtproto/facade.h +++ b/Telegram/SourceFiles/mtproto/facade.h @@ -7,7 +7,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once -#include "core/single_timer.h" #include "mtproto/type_utils.h" #include "mtproto/mtp_instance.h" diff --git a/Telegram/SourceFiles/mtproto/mtp_instance.cpp b/Telegram/SourceFiles/mtproto/mtp_instance.cpp index 590fe3cb20..bc0ccd1e69 100644 --- a/Telegram/SourceFiles/mtproto/mtp_instance.cpp +++ b/Telegram/SourceFiles/mtproto/mtp_instance.cpp @@ -361,7 +361,7 @@ void Instance::Private::setGoodProxyDomain( } if (applyToProxy(Global::RefSelectedProxy()) && (Global::ProxySettings() == ProxyData::Settings::Enabled)) { - Sandbox::refreshGlobalProxy(); + Core::App().refreshGlobalProxy(); } } diff --git a/Telegram/SourceFiles/mtproto/session.cpp b/Telegram/SourceFiles/mtproto/session.cpp index 6f5ef367fe..ac52349066 100644 --- a/Telegram/SourceFiles/mtproto/session.cpp +++ b/Telegram/SourceFiles/mtproto/session.cpp @@ -122,13 +122,12 @@ void SessionData::clear(Instance *instance) { Session::Session(not_null instance, ShiftedDcId shiftedDcId) : QObject() , _instance(instance) , data(this) -, dcWithShift(shiftedDcId) { +, dcWithShift(shiftedDcId) +, sender([=] { needToResumeAndSend(); }) { connect(&timeouter, SIGNAL(timeout()), this, SLOT(checkRequestsByTimer())); timeouter.start(1000); refreshOptions(); - - connect(&sender, SIGNAL(timeout()), this, SLOT(needToResumeAndSend())); } void Session::start() { @@ -247,10 +246,10 @@ void Session::sendAnything(qint64 msCanWait) { if (msWait) { DEBUG_LOG(("MTP Info: dcWithShift %1 can wait for %2ms from current %3").arg(dcWithShift).arg(msWait).arg(msSendCall)); msSendCall = ms; - sender.start(msWait); + sender.callOnce(msWait); } else { DEBUG_LOG(("MTP Info: dcWithShift %1 stopped send timer, can wait for %2ms from current %3").arg(dcWithShift).arg(msWait).arg(msSendCall)); - sender.stop(); + sender.cancel(); msSendCall = 0; needToResumeAndSend(); } diff --git a/Telegram/SourceFiles/mtproto/session.h b/Telegram/SourceFiles/mtproto/session.h index ebecb03893..f2004ac596 100644 --- a/Telegram/SourceFiles/mtproto/session.h +++ b/Telegram/SourceFiles/mtproto/session.h @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once -#include "core/single_timer.h" +#include "base/timer.h" #include "mtproto/rpc_sender.h" namespace MTP { @@ -394,7 +394,7 @@ private: bool _ping = false; QTimer timeouter; - SingleTimer sender; + base::Timer sender; }; diff --git a/Telegram/SourceFiles/passport/passport_panel_edit_scans.cpp b/Telegram/SourceFiles/passport/passport_panel_edit_scans.cpp index f91d39ea0f..2628ce44d0 100644 --- a/Telegram/SourceFiles/passport/passport_panel_edit_scans.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_edit_scans.cpp @@ -21,6 +21,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "boxes/abstract_box.h" #include "storage/storage_media_prepare.h" +#include "storage/file_upload.h" // For Storage::kUseBigFilesFrom. #include "styles/style_boxes.h" #include "styles/style_passport.h" @@ -31,7 +32,7 @@ constexpr auto kMaxDimensions = 2048; constexpr auto kMaxSize = 10 * 1024 * 1024; constexpr auto kJpegQuality = 89; -static_assert(kMaxSize <= UseBigFilesFrom); +static_assert(kMaxSize <= Storage::kUseBigFilesFrom); base::variant ProcessImage(QByteArray &&bytes) { auto image = App::readImage(base::take(bytes)); diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.h b/Telegram/SourceFiles/platform/mac/main_window_mac.h index 1c38821eb2..0597ff0864 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.h +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.h @@ -14,7 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Platform { class MainWindow : public Window::MainWindow { - // The Q_OBJECT meta info is used for qobject_cast to MainWindow! + // The Q_OBJECT meta info is used for qobject_cast! Q_OBJECT public: diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.mm b/Telegram/SourceFiles/platform/mac/main_window_mac.mm index 0687965c71..0ea58ab8cf 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.mm +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.mm @@ -10,7 +10,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_window.h" #include "mainwindow.h" #include "mainwidget.h" -#include "application.h" #include "messenger.h" #include "auth_session.h" #include "history/history.h" @@ -70,7 +69,7 @@ public: LayerCreationChecker(NSView * __weak view, Fn callback) : _weakView(view) , _callback(std::move(callback)) { - QCoreApplication::instance()->installEventFilter(this); + QApplication::instance()->installEventFilter(this); } protected: @@ -334,7 +333,7 @@ bool MainWindow::Private::clipboardHasText() { auto currentChangeCount = static_cast([_generalPasteboard changeCount]); if (_generalPasteboardChangeCount != currentChangeCount) { _generalPasteboardChangeCount = currentChangeCount; - _generalPasteboardHasText = !Application::clipboard()->text().isEmpty(); + _generalPasteboardHasText = !QApplication::clipboard()->text().isEmpty(); } return _generalPasteboardHasText; } diff --git a/Telegram/SourceFiles/platform/win/main_window_win.cpp b/Telegram/SourceFiles/platform/win/main_window_win.cpp index 18688de491..ea1937c388 100644 --- a/Telegram/SourceFiles/platform/win/main_window_win.cpp +++ b/Telegram/SourceFiles/platform/win/main_window_win.cpp @@ -179,7 +179,7 @@ public: return false; } - QRect avail(Sandbox::availableGeometry()); + const auto avail = QApplication::desktop()->availableGeometry(); max_w = avail.width(); accumulate_max(max_w, st::windowMinWidth); max_h = avail.height(); diff --git a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp index 236046e621..8935eee484 100644 --- a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp +++ b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp @@ -374,8 +374,8 @@ private: }; Manager::Private::Private(Manager *instance, Type type) -: _guarded(std::make_shared(instance)) -, _cachedUserpics(type) { +: _cachedUserpics(type) +, _guarded(std::make_shared(instance)) { } bool Manager::Private::init() { diff --git a/Telegram/SourceFiles/settings/settings_codes.cpp b/Telegram/SourceFiles/settings/settings_codes.cpp index b9ef596d56..2bcf8adfb8 100644 --- a/Telegram/SourceFiles/settings/settings_codes.cpp +++ b/Telegram/SourceFiles/settings/settings_codes.cpp @@ -34,7 +34,7 @@ auto GenerateCodes() { : qsl("Do you want to enable DEBUG logs?\n\n" "All network events will be logged."); Ui::show(Box(text, [] { - Messenger::Instance().onSwitchDebugMode(); + Messenger::Instance().switchDebugMode(); })); }); codes.emplace(qsl("viewlogs"), [] { @@ -43,7 +43,7 @@ auto GenerateCodes() { codes.emplace(qsl("testmode"), [] { auto text = cTestMode() ? qsl("Do you want to disable TEST mode?") : qsl("Do you want to enable TEST mode?\n\nYou will be switched to test cloud."); Ui::show(Box(text, [] { - Messenger::Instance().onSwitchTestMode(); + Messenger::Instance().switchTestMode(); })); }); if (!Core::UpdaterDisabled()) { @@ -71,7 +71,7 @@ auto GenerateCodes() { codes.emplace(qsl("workmode"), [] { auto text = Global::DialogsModeEnabled() ? qsl("Disable work mode?") : qsl("Enable work mode?"); Ui::show(Box(text, [] { - Messenger::Instance().onSwitchWorkMode(); + Messenger::Instance().switchWorkMode(); })); }); codes.emplace(qsl("moderate"), [] { diff --git a/Telegram/SourceFiles/storage/file_download.cpp b/Telegram/SourceFiles/storage/file_download.cpp index 20f8dece37..fefdacb6c8 100644 --- a/Telegram/SourceFiles/storage/file_download.cpp +++ b/Telegram/SourceFiles/storage/file_download.cpp @@ -1440,18 +1440,18 @@ void WebLoadManager::onFailed(QNetworkReply *reply) { } void WebLoadManager::onProgress(qint64 already, qint64 size) { - QNetworkReply *reply = qobject_cast(QObject::sender()); + const auto reply = qobject_cast(QObject::sender()); if (!reply) return; - Replies::iterator j = _replies.find(reply); + const auto j = _replies.find(reply); if (j == _replies.cend()) { // handled already return; } - webFileLoaderPrivate *loader = j.value(); + const auto loader = j.value(); - WebReplyProcessResult result = WebReplyProcessProgress; - QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); - int32 status = statusCode.isValid() ? statusCode.toInt() : 200; + auto result = WebReplyProcessProgress; + const auto statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); + const auto status = statusCode.isValid() ? statusCode.toInt() : 200; if (status != 200 && status != 206 && status != 416) { if (status == 301 || status == 302) { QString loc = reply->header(QNetworkRequest::LocationHeader).toString(); @@ -1490,20 +1490,19 @@ void WebLoadManager::onProgress(qint64 already, qint64 size) { } void WebLoadManager::onMeta() { - QNetworkReply *reply = qobject_cast(QObject::sender()); + const auto reply = qobject_cast(QObject::sender()); if (!reply) return; - Replies::iterator j = _replies.find(reply); + const auto j = _replies.find(reply); if (j == _replies.cend()) { // handled already return; } - webFileLoaderPrivate *loader = j.value(); + const auto loader = j.value(); - typedef QList Pairs; - Pairs pairs = reply->rawHeaderPairs(); - for (Pairs::iterator i = pairs.begin(), e = pairs.end(); i != e; ++i) { + const auto pairs = reply->rawHeaderPairs(); + for (auto i = pairs.begin(), e = pairs.end(); i != e; ++i) { if (QString::fromUtf8(i->first).toLower() == "content-range") { - QRegularExpressionMatch m = QRegularExpression(qsl("/(\\d+)([^\\d]|$)")).match(QString::fromUtf8(i->second)); + const auto m = QRegularExpression(qsl("/(\\d+)([^\\d]|$)")).match(QString::fromUtf8(i->second)); if (m.hasMatch()) { loader->setProgress(qMax(qint64(loader->data().size()), loader->already()), m.captured(1).toLongLong()); if (!handleReplyResult(loader, WebReplyProcessProgress)) { diff --git a/Telegram/SourceFiles/storage/file_upload.cpp b/Telegram/SourceFiles/storage/file_upload.cpp index 932ff170a2..c5e45b7197 100644 --- a/Telegram/SourceFiles/storage/file_upload.cpp +++ b/Telegram/SourceFiles/storage/file_upload.cpp @@ -17,7 +17,28 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Storage { namespace { -constexpr auto kMaxUploadFileParallelSize = MTP::kUploadSessionsCount * 512 * 1024; // max 512kb uploaded at the same time in each session +// max 512kb uploaded at the same time in each session +constexpr auto kMaxUploadFileParallelSize = MTP::kUploadSessionsCount * 512 * 1024; + +constexpr auto kDocumentMaxPartsCount = 3000; + +// 32kb for tiny document ( < 1mb ) +constexpr auto kDocumentUploadPartSize0 = 32 * 1024; + +// 64kb for little document ( <= 32mb ) +constexpr auto kDocumentUploadPartSize1 = 64 * 1024; + +// 128kb for small document ( <= 375mb ) +constexpr auto kDocumentUploadPartSize2 = 128 * 1024; + +// 256kb for medium document ( <= 750mb ) +constexpr auto kDocumentUploadPartSize3 = 256 * 1024; + +// 512kb for large document ( <= 1500mb ) +constexpr auto kDocumentUploadPartSize4 = 512 * 1024; + +// One part each half second, if not uploaded faster. +constexpr auto kUploadRequestInterval = TimeMs(500); } // namespace @@ -75,11 +96,11 @@ void Uploader::File::setDocSize(int32 size) { docSize = size; constexpr auto limit0 = 1024 * 1024; constexpr auto limit1 = 32 * limit0; - if (docSize >= limit0 || !setPartSize(DocumentUploadPartSize0)) { - if (docSize > limit1 || !setPartSize(DocumentUploadPartSize1)) { - if (!setPartSize(DocumentUploadPartSize2)) { - if (!setPartSize(DocumentUploadPartSize3)) { - if (!setPartSize(DocumentUploadPartSize4)) { + if (docSize >= limit0 || !setPartSize(kDocumentUploadPartSize0)) { + if (docSize > limit1 || !setPartSize(kDocumentUploadPartSize1)) { + if (!setPartSize(kDocumentUploadPartSize2)) { + if (!setPartSize(kDocumentUploadPartSize3)) { + if (!setPartSize(kDocumentUploadPartSize4)) { LOG(("Upload Error: bad doc size: %1").arg(docSize)); } } @@ -92,7 +113,7 @@ bool Uploader::File::setPartSize(uint32 partSize) { docPartSize = partSize; docPartsCount = (docSize / docPartSize) + ((docSize % docPartSize) ? 1 : 0); - return (docPartsCount <= DocumentMaxPartsCount); + return (docPartsCount <= kDocumentMaxPartsCount); } uint64 Uploader::File::id() const { @@ -288,7 +309,7 @@ void Uploader::sendNext() { QByteArray docMd5(32, Qt::Uninitialized); hashMd5Hex(uploadingData.md5Hash.result(), docMd5.data()); - const auto file = (uploadingData.docSize > UseBigFilesFrom) + const auto file = (uploadingData.docSize > kUseBigFilesFrom) ? MTP_inputFileBig( MTP_long(uploadingData.id()), MTP_int(uploadingData.docPartsCount), @@ -347,7 +368,7 @@ void Uploader::sendNext() { } } toSend = uploadingData.docFile->read(uploadingData.docPartSize); - if (uploadingData.docSize <= UseBigFilesFrom) { + if (uploadingData.docSize <= kUseBigFilesFrom) { uploadingData.md5Hash.feed(toSend.constData(), toSend.size()); } } else { @@ -356,7 +377,7 @@ void Uploader::sendNext() { toSend = content.mid(offset, uploadingData.docPartSize); if ((uploadingData.type() == SendMediaType::File || uploadingData.type() == SendMediaType::Audio) - && uploadingData.docSentParts <= UseBigFilesFrom) { + && uploadingData.docSentParts <= kUseBigFilesFrom) { uploadingData.md5Hash.feed(toSend.constData(), toSend.size()); } } @@ -367,7 +388,7 @@ void Uploader::sendNext() { return; } mtpRequestId requestId; - if (uploadingData.docSize > UseBigFilesFrom) { + if (uploadingData.docSize > kUseBigFilesFrom) { requestId = MTP::send( MTPupload_SaveBigFilePart( MTP_long(uploadingData.id()), @@ -411,7 +432,7 @@ void Uploader::sendNext() { parts.erase(part); } - nextTimer.start(UploadRequestInterval); + nextTimer.start(kUploadRequestInterval); } void Uploader::cancel(const FullMsgId &msgId) { diff --git a/Telegram/SourceFiles/storage/file_upload.h b/Telegram/SourceFiles/storage/file_upload.h index 040687dd05..db474df8e5 100644 --- a/Telegram/SourceFiles/storage/file_upload.h +++ b/Telegram/SourceFiles/storage/file_upload.h @@ -12,6 +12,9 @@ struct SendMediaReady; namespace Storage { +// MTP big files methods used for files greater than 10mb. +constexpr auto kUseBigFilesFrom = 10 * 1024 * 1024; + struct UploadedPhoto { FullMsgId fullId; bool silent = false; diff --git a/Telegram/SourceFiles/storage/localimageloader.cpp b/Telegram/SourceFiles/storage/localimageloader.cpp index f630904789..3f9e3b87ca 100644 --- a/Telegram/SourceFiles/storage/localimageloader.cpp +++ b/Telegram/SourceFiles/storage/localimageloader.cpp @@ -25,6 +25,9 @@ namespace { constexpr auto kThumbnailQuality = 87; constexpr auto kThumbnailSize = 320; +constexpr auto kPhotoUploadPartSize = 32 * 1024; + +using Storage::ValidateThumbDimensions; struct PreparedFileThumbnail { uint64 id = 0; @@ -37,7 +40,7 @@ struct PreparedFileThumbnail { PreparedFileThumbnail PrepareFileThumbnail(QImage &&original) { const auto width = original.width(); const auto height = original.height(); - if (!Storage::ValidateThumbDimensions(width, height)) { + if (!ValidateThumbDimensions(width, height)) { return {}; } auto result = PreparedFileThumbnail(); @@ -133,7 +136,43 @@ MTPInputSingleMedia PrepareAlbumItemMedia( } // namespace -using Storage::ValidateThumbDimensions; +SendMediaReady::SendMediaReady( + SendMediaType type, + const QString &file, + const QString &filename, + int32 filesize, + const QByteArray &data, + const uint64 &id, + const uint64 &thumbId, + const QString &thumbExt, + const PeerId &peer, + const MTPPhoto &photo, + const PreparedPhotoThumbs &photoThumbs, + const MTPDocument &document, + const QByteArray &jpeg, + MsgId replyTo) +: replyTo(replyTo) +, type(type) +, file(file) +, filename(filename) +, filesize(filesize) +, data(data) +, thumbExt(thumbExt) +, id(id) +, thumbId(thumbId) +, peer(peer) +, photo(photo) +, document(document) +, photoThumbs(photoThumbs) { + if (!jpeg.isEmpty()) { + int32 size = jpeg.size(); + for (int32 i = 0, part = 0; i < size; i += kPhotoUploadPartSize, ++part) { + parts.insert(part, jpeg.mid(i, kPhotoUploadPartSize)); + } + jpeg_md5.resize(32); + hashMd5Hex(jpeg.constData(), jpeg.size(), jpeg_md5.data()); + } +} SendMediaReady PreparePeerPhoto(PeerId peerId, QImage &&image) { PreparedPhotoThumbs photoThumbs; @@ -290,10 +329,8 @@ void TaskQueue::stop() { _thread->quit(); DEBUG_LOG(("Waiting for taskThread to finish")); _thread->wait(); - delete _worker; - delete _thread; - _worker = 0; - _thread = 0; + delete base::take(_worker); + delete base::take(_thread); } _tasksToProcess.clear(); _tasksToFinish.clear(); @@ -399,6 +436,31 @@ FileLoadResult::FileLoadResult( , caption(caption) { } +void FileLoadResult::setFileData(const QByteArray &filedata) { + if (filedata.isEmpty()) { + partssize = 0; + } else { + partssize = filedata.size(); + for (int32 i = 0, part = 0; i < partssize; i += kPhotoUploadPartSize, ++part) { + fileparts.insert(part, filedata.mid(i, kPhotoUploadPartSize)); + } + filemd5.resize(32); + hashMd5Hex(filedata.constData(), filedata.size(), filemd5.data()); + } +} + +void FileLoadResult::setThumbData(const QByteArray &thumbdata) { + if (!thumbdata.isEmpty()) { + int32 size = thumbdata.size(); + for (int32 i = 0, part = 0; i < size; i += kPhotoUploadPartSize, ++part) { + thumbparts.insert(part, thumbdata.mid(i, kPhotoUploadPartSize)); + } + thumbmd5.resize(32); + hashMd5Hex(thumbdata.constData(), thumbdata.size(), thumbmd5.data()); + } +} + + FileLoadTask::FileLoadTask( const QString &filepath, const QByteArray &content, diff --git a/Telegram/SourceFiles/storage/localimageloader.h b/Telegram/SourceFiles/storage/localimageloader.h index e1106dd99c..6faff9fde5 100644 --- a/Telegram/SourceFiles/storage/localimageloader.h +++ b/Telegram/SourceFiles/storage/localimageloader.h @@ -47,29 +47,22 @@ using SendMediaPrepareList = QList; using UploadFileParts = QMap; struct SendMediaReady { SendMediaReady() = default; // temp - SendMediaReady(SendMediaType type, const QString &file, const QString &filename, int32 filesize, const QByteArray &data, const uint64 &id, const uint64 &thumbId, const QString &thumbExt, const PeerId &peer, const MTPPhoto &photo, const PreparedPhotoThumbs &photoThumbs, const MTPDocument &document, const QByteArray &jpeg, MsgId replyTo) - : replyTo(replyTo) - , type(type) - , file(file) - , filename(filename) - , filesize(filesize) - , data(data) - , thumbExt(thumbExt) - , id(id) - , thumbId(thumbId) - , peer(peer) - , photo(photo) - , document(document) - , photoThumbs(photoThumbs) { - if (!jpeg.isEmpty()) { - int32 size = jpeg.size(); - for (int32 i = 0, part = 0; i < size; i += UploadPartSize, ++part) { - parts.insert(part, jpeg.mid(i, UploadPartSize)); - } - jpeg_md5.resize(32); - hashMd5Hex(jpeg.constData(), jpeg.size(), jpeg_md5.data()); - } - } + SendMediaReady( + SendMediaType type, + const QString &file, + const QString &filename, + int32 filesize, + const QByteArray &data, + const uint64 &id, + const uint64 &thumbId, + const QString &thumbExt, + const PeerId &peer, + const MTPPhoto &photo, + const PreparedPhotoThumbs &photoThumbs, + const MTPDocument &document, + const QByteArray &jpeg, + MsgId replyTo); + MsgId replyTo; SendMediaType type; QString file, filename; @@ -233,28 +226,9 @@ struct FileLoadResult { PreparedPhotoThumbs photoThumbs; TextWithTags caption; - void setFileData(const QByteArray &filedata) { - if (filedata.isEmpty()) { - partssize = 0; - } else { - partssize = filedata.size(); - for (int32 i = 0, part = 0; i < partssize; i += UploadPartSize, ++part) { - fileparts.insert(part, filedata.mid(i, UploadPartSize)); - } - filemd5.resize(32); - hashMd5Hex(filedata.constData(), filedata.size(), filemd5.data()); - } - } - void setThumbData(const QByteArray &thumbdata) { - if (!thumbdata.isEmpty()) { - int32 size = thumbdata.size(); - for (int32 i = 0, part = 0; i < size; i += UploadPartSize, ++part) { - thumbparts.insert(part, thumbdata.mid(i, UploadPartSize)); - } - thumbmd5.resize(32); - hashMd5Hex(thumbdata.constData(), thumbdata.size(), thumbmd5.data()); - } - } + void setFileData(const QByteArray &filedata); + void setThumbData(const QByteArray &thumbdata); + }; struct FileMediaInformation { diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index ce404b169e..a24b55dae7 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -1283,7 +1283,7 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting } else { Global::SetProxiesList({}); } - Sandbox::refreshGlobalProxy(); + Core::App().refreshGlobalProxy(); } break; case dbiConnectionType: { @@ -1383,7 +1383,7 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting Global::SetProxySettings(ProxyData::Settings::System); } } - Sandbox::refreshGlobalProxy(); + Core::App().refreshGlobalProxy(); } break; case dbiThemeKeyOld: { diff --git a/Telegram/SourceFiles/ui/toast/toast_manager.cpp b/Telegram/SourceFiles/ui/toast/toast_manager.cpp index cc6d5af934..346bdcb03b 100644 --- a/Telegram/SourceFiles/ui/toast/toast_manager.cpp +++ b/Telegram/SourceFiles/ui/toast/toast_manager.cpp @@ -20,8 +20,8 @@ NeverFreedPointer> _managers; } // namespace -Manager::Manager(QWidget *parent) : QObject(parent) { - connect(&_hideTimer, SIGNAL(timeout()), this, SLOT(onHideTimeout())); +Manager::Manager(QWidget *parent) : QObject(parent) +, _hideTimer([=] { hideByTimer(); }) { } bool Manager::eventFilter(QObject *o, QEvent *e) { @@ -80,7 +80,7 @@ void Manager::addToast(std::unique_ptr &&toast) { } } -void Manager::onHideTimeout() { +void Manager::hideByTimer() { auto now = getms(true); for (auto i = _toastByHideTime.begin(); i != _toastByHideTime.cend();) { if (i.key() <= now) { @@ -114,9 +114,11 @@ void Manager::startNextHideTimer() { auto ms = getms(true); if (ms >= _toastByHideTime.firstKey()) { - QMetaObject::invokeMethod(this, SLOT("onHideTimeout"), Qt::QueuedConnection); + crl::on_main(this, [=] { + hideByTimer(); + }); } else { - _hideTimer.start(_toastByHideTime.firstKey() - ms); + _hideTimer.callOnce(_toastByHideTime.firstKey() - ms); } } diff --git a/Telegram/SourceFiles/ui/toast/toast_manager.h b/Telegram/SourceFiles/ui/toast/toast_manager.h index 01e273cd4b..ce4b795cfb 100644 --- a/Telegram/SourceFiles/ui/toast/toast_manager.h +++ b/Telegram/SourceFiles/ui/toast/toast_manager.h @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "ui/toast/toast.h" -#include "core/single_timer.h" +#include "base/timer.h" namespace Ui { namespace Toast { @@ -32,14 +32,14 @@ protected: bool eventFilter(QObject *o, QEvent *e); private slots: - void onHideTimeout(); void onToastWidgetDestroyed(QObject *widget); private: Manager(QWidget *parent); void startNextHideTimer(); + void hideByTimer(); - SingleTimer _hideTimer; + base::Timer _hideTimer; TimeMs _nextHide = 0; QMultiMap _toastByHideTime; diff --git a/Telegram/SourceFiles/ui/twidget.cpp b/Telegram/SourceFiles/ui/twidget.cpp index 7f94c6d1da..0184bdf5cd 100644 --- a/Telegram/SourceFiles/ui/twidget.cpp +++ b/Telegram/SourceFiles/ui/twidget.cpp @@ -234,8 +234,7 @@ void ForceFullRepaint(not_null widget) { } void PostponeCall(FnMut &&callable) { - const auto application = static_cast(qApp); - application->postponeCall(std::move(callable)); + Core::App().postponeCall(std::move(callable)); } } // namespace Ui diff --git a/Telegram/SourceFiles/ui/twidget.h b/Telegram/SourceFiles/ui/twidget.h index c6051109f7..93853a446a 100644 --- a/Telegram/SourceFiles/ui/twidget.h +++ b/Telegram/SourceFiles/ui/twidget.h @@ -341,6 +341,7 @@ private: }; class TWidget : public TWidgetHelper { + // The Q_OBJECT meta info is used for qobject_cast! Q_OBJECT public: diff --git a/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp b/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp index afb736d1d8..27e6933a9a 100644 --- a/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp +++ b/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "ui/widgets/continuous_sliders.h" +#include "base/timer.h" + namespace Ui { namespace { @@ -29,14 +31,13 @@ void ContinuousSlider::setDisabled(bool disabled) { void ContinuousSlider::setMoveByWheel(bool move) { if (move != moveByWheel()) { if (move) { - _byWheelFinished = std::make_unique(); - _byWheelFinished->setTimeoutHandler([this] { + _byWheelFinished = std::make_unique([=] { if (_changeFinishedCallback) { _changeFinishedCallback(getCurrentValue()); } }); } else { - _byWheelFinished.reset(); + _byWheelFinished = nullptr; } } } @@ -109,7 +110,7 @@ void ContinuousSlider::wheelEvent(QWheelEvent *e) { if (_changeProgressCallback) { _changeProgressCallback(finalValue); } - _byWheelFinished->start(kByWheelFinishedTimeout); + _byWheelFinished->callOnce(kByWheelFinishedTimeout); } void ContinuousSlider::updateDownValueFromPos(const QPoint &pos) { diff --git a/Telegram/SourceFiles/ui/widgets/continuous_sliders.h b/Telegram/SourceFiles/ui/widgets/continuous_sliders.h index ceb7eab176..a32a0ac0c3 100644 --- a/Telegram/SourceFiles/ui/widgets/continuous_sliders.h +++ b/Telegram/SourceFiles/ui/widgets/continuous_sliders.h @@ -10,6 +10,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_widgets.h" #include "ui/rp_widget.h" +namespace base { +class Timer; +} // namespace base + namespace Ui { class ContinuousSlider : public RpWidget { @@ -87,7 +91,7 @@ private: Direction _direction = Direction::Horizontal; bool _disabled = false; - std::unique_ptr _byWheelFinished; + std::unique_ptr _byWheelFinished; Fn _adjustCallback; Fn _changeProgressCallback; diff --git a/Telegram/SourceFiles/ui/widgets/dropdown_menu.cpp b/Telegram/SourceFiles/ui/widgets/dropdown_menu.cpp index a309dd0796..de0c6ae52d 100644 --- a/Telegram/SourceFiles/ui/widgets/dropdown_menu.cpp +++ b/Telegram/SourceFiles/ui/widgets/dropdown_menu.cpp @@ -231,7 +231,7 @@ void DropdownMenu::hideFinish() { // // auto menuTopLeft = mapFromGlobal(_menu->mapToGlobal(QPoint(0, 0))); // auto w = p - QPoint(0, menuTopLeft.y()); -// auto r = Sandbox::screenGeometry(p); +// auto r = QApplication::desktop()->screenGeometry(p); // if (rtl()) { // if (w.x() - width() < r.x() - _padding.left()) { // if (_parent && w.x() + _parent->width() - _padding.left() - _padding.right() + width() - _padding.right() <= r.x() + r.width()) { diff --git a/Telegram/SourceFiles/ui/widgets/input_fields.cpp b/Telegram/SourceFiles/ui/widgets/input_fields.cpp index b0229b0433..fc9c312194 100644 --- a/Telegram/SourceFiles/ui/widgets/input_fields.cpp +++ b/Telegram/SourceFiles/ui/widgets/input_fields.cpp @@ -528,12 +528,12 @@ public: setParent(QCoreApplication::instance()); } - void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const { + void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override { } - QRect subElementRect(SubElement r, const QStyleOption *opt, const QWidget *widget = 0) const { + QRect subElementRect(SubElement r, const QStyleOption *opt, const QWidget *widget = nullptr) const override { switch (r) { case SE_LineEditContents: - const InputClass *w = widget ? qobject_cast(widget) : 0; + const auto w = widget ? qobject_cast(widget) : nullptr; return w ? w->getTextRect() : QCommonStyle::subElementRect(r, opt, widget); break; } diff --git a/Telegram/SourceFiles/ui/widgets/input_fields.h b/Telegram/SourceFiles/ui/widgets/input_fields.h index 11dac32def..9579896863 100644 --- a/Telegram/SourceFiles/ui/widgets/input_fields.h +++ b/Telegram/SourceFiles/ui/widgets/input_fields.h @@ -41,6 +41,7 @@ enum class InputSubmitSettings { }; class FlatInput : public TWidgetHelper, private base::Subscriber { + // The Q_OBJECT meta info is used for qobject_cast! Q_OBJECT public: @@ -493,6 +494,7 @@ private: class MaskedInputField : public RpWidgetWrap , private base::Subscriber { + // The Q_OBJECT meta info is used for qobject_cast! Q_OBJECT using Parent = RpWidgetWrap; diff --git a/Telegram/SourceFiles/ui/widgets/popup_menu.cpp b/Telegram/SourceFiles/ui/widgets/popup_menu.cpp index 1ac1b367fd..09fa9558dd 100644 --- a/Telegram/SourceFiles/ui/widgets/popup_menu.cpp +++ b/Telegram/SourceFiles/ui/widgets/popup_menu.cpp @@ -447,7 +447,7 @@ void PopupMenu::showMenu(const QPoint &p, PopupMenu *parent, TriggeredSource sou auto origin = PanelAnimation::Origin::TopLeft; auto w = p - QPoint(0, _padding.top()); - auto r = Sandbox::screenGeometry(p); + auto r = QApplication::desktop()->screenGeometry(p); _useTransparency = Platform::TranslucentWindowsSupported(p); setAttribute(Qt::WA_OpaquePaintEvent, !_useTransparency); handleCompositingUpdate(); diff --git a/Telegram/SourceFiles/ui/widgets/scroll_area.cpp b/Telegram/SourceFiles/ui/widgets/scroll_area.cpp index 718b14fb63..7c9e069b33 100644 --- a/Telegram/SourceFiles/ui/widgets/scroll_area.cpp +++ b/Telegram/SourceFiles/ui/widgets/scroll_area.cpp @@ -636,7 +636,7 @@ void ScrollArea::resizeEvent(QResizeEvent *e) { _verticalBar->recountSize(); _topShadow->setGeometry(QRect(0, 0, width(), qAbs(_st.topsh))); _bottomShadow->setGeometry(QRect(0, height() - qAbs(_st.bottomsh), width(), qAbs(_st.bottomsh))); - if (SplittedWidget *w = qobject_cast(widget())) { + if (const auto w = qobject_cast(widget())) { w->resize(width() - w->otherWidth(), w->height()); if (!rtl()) { _other->move(w->width(), w->y()); @@ -720,7 +720,7 @@ void ScrollArea::scrollToY(int toTop, int toBottom) { } void ScrollArea::doSetOwnedWidget(object_ptr w) { - auto splitted = qobject_cast(w.data()); + const auto splitted = qobject_cast(w.data()); if (widget() && _touchEnabled) { widget()->removeEventFilter(this); if (!_widgetAcceptsTouch) widget()->setAttribute(Qt::WA_AcceptTouchEvents, false); diff --git a/Telegram/SourceFiles/ui/widgets/scroll_area.h b/Telegram/SourceFiles/ui/widgets/scroll_area.h index e4715c7bc2..9b8c69b008 100644 --- a/Telegram/SourceFiles/ui/widgets/scroll_area.h +++ b/Telegram/SourceFiles/ui/widgets/scroll_area.h @@ -110,6 +110,7 @@ private: }; class SplittedWidget : public Ui::RpWidget { + // The Q_OBJECT meta info is used for qobject_cast! Q_OBJECT public: diff --git a/Telegram/SourceFiles/window/notifications_manager.cpp b/Telegram/SourceFiles/window/notifications_manager.cpp index 479c6c9c42..93e13665e6 100644 --- a/Telegram/SourceFiles/window/notifications_manager.cpp +++ b/Telegram/SourceFiles/window/notifications_manager.cpp @@ -33,14 +33,12 @@ constexpr auto kMinimalAlertDelay = TimeMs(500); } // namespace -System::System(AuthSession *session) : _authSession(session) { +System::System(AuthSession *session) +: _authSession(session) +, _waitTimer([=] { showNext(); }) { createManager(); - _waitTimer.setTimeoutHandler([this] { - showNext(); - }); - - subscribe(settingsChanged(), [this](ChangeType type) { + subscribe(settingsChanged(), [=](ChangeType type) { if (type == ChangeType::DesktopEnabled) { App::wnd()->updateTrayMenu(); clearAll(); @@ -124,7 +122,7 @@ void System::schedule(History *history, HistoryItem *item) { } if (haveSetting) { if (!_waitTimer.isActive() || _waitTimer.remainingTime() > delay) { - _waitTimer.start(delay); + _waitTimer.callOnce(delay); } } } @@ -150,7 +148,7 @@ void System::clearFromHistory(History *history) { _waiters.remove(history); _settingWaiters.remove(history); - _waitTimer.stop(); + _waitTimer.cancel(); showNext(); } @@ -209,7 +207,7 @@ void System::checkDelayed() { ++i; } } - _waitTimer.stop(); + _waitTimer.cancel(); showNext(); } @@ -259,7 +257,7 @@ void System::showNext() { if (_waiters.isEmpty() || !Global::DesktopNotify() || Platform::Notifications::SkipToast()) { if (nextAlert) { - _waitTimer.start(nextAlert - ms); + _waitTimer.callOnce(nextAlert - ms); } return; } @@ -306,7 +304,7 @@ void System::showNext() { next = nextAlert; nextAlert = 0; } - _waitTimer.start(next - ms); + _waitTimer.callOnce(next - ms); break; } else { auto forwardedItem = notifyItem->Has() ? notifyItem : nullptr; // forwarded notify grouping @@ -366,7 +364,7 @@ void System::showNext() { } } if (nextAlert) { - _waitTimer.start(nextAlert - ms); + _waitTimer.callOnce(nextAlert - ms); } } diff --git a/Telegram/SourceFiles/window/notifications_manager.h b/Telegram/SourceFiles/window/notifications_manager.h index 044b713311..7bd3af0fc2 100644 --- a/Telegram/SourceFiles/window/notifications_manager.h +++ b/Telegram/SourceFiles/window/notifications_manager.h @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "base/timer.h" + class AuthSession; namespace Platform { @@ -96,7 +98,7 @@ private: using Waiters = QMap; Waiters _waiters; Waiters _settingWaiters; - SingleTimer _waitTimer; + base::Timer _waitTimer; QMap> _whenAlerts; diff --git a/Telegram/SourceFiles/window/notifications_manager_default.cpp b/Telegram/SourceFiles/window/notifications_manager_default.cpp index 1912f3a9ce..75037fdc68 100644 --- a/Telegram/SourceFiles/window/notifications_manager_default.cpp +++ b/Telegram/SourceFiles/window/notifications_manager_default.cpp @@ -53,7 +53,9 @@ std::unique_ptr Create(System *system) { return std::make_unique(system); } -Manager::Manager(System *system) : Notifications::Manager(system) { +Manager::Manager(System *system) +: Notifications::Manager(system) +, _inputCheckTimer([=] { checkLastInput(); }) { subscribe(system->authSession()->downloader().taskFinished(), [this] { for_const (auto ¬ification, _notifications) { notification->updatePeerPhoto(); @@ -62,7 +64,6 @@ Manager::Manager(System *system) : Notifications::Manager(system) { subscribe(system->settingsChanged(), [this](ChangeType change) { settingsChanged(change); }); - _inputCheckTimer.setTimeoutHandler([this] { checkLastInput(); }); } Manager::QueuedNotification::QueuedNotification( @@ -144,7 +145,7 @@ void Manager::checkLastInput() { } } if (waiting) { - _inputCheckTimer.start(300); + _inputCheckTimer.callOnce(300); } } @@ -494,15 +495,25 @@ void Background::paintEvent(QPaintEvent *e) { p.fillRect(st::notifyBorderWidth, height() - st::notifyBorderWidth, width() - 2 * st::notifyBorderWidth, st::notifyBorderWidth, st::notifyBorder); } -Notification::Notification(Manager *manager, History *history, PeerData *peer, PeerData *author, HistoryItem *msg, int forwardedCount, QPoint startPosition, int shift, Direction shiftDirection) : Widget(manager, startPosition, shift, shiftDirection) +Notification::Notification( + Manager *manager, + History *history, + PeerData *peer, + PeerData *author, + HistoryItem *msg, + int forwardedCount, + QPoint startPosition, + int shift, + Direction shiftDirection) +: Widget(manager, startPosition, shift, shiftDirection) +#ifdef Q_OS_WIN +, _started(GetTickCount()) +#endif // Q_OS_WIN , _history(history) , _peer(peer) , _author(author) , _item(msg) , _forwardedCount(forwardedCount) -#ifdef Q_OS_WIN -, _started(GetTickCount()) -#endif // Q_OS_WIN , _close(this, st::notifyClose) , _reply(this, langFactory(lng_notification_reply), st::defaultBoxButton) { subscribe(Lang::Current().updated(), [this] { refreshLang(); }); diff --git a/Telegram/SourceFiles/window/notifications_manager_default.h b/Telegram/SourceFiles/window/notifications_manager_default.h index d22defd93b..4c6c51aa6d 100644 --- a/Telegram/SourceFiles/window/notifications_manager_default.h +++ b/Telegram/SourceFiles/window/notifications_manager_default.h @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "window/notifications_manager.h" -#include "core/single_timer.h" +#include "base/timer.h" namespace Ui { class IconButton; @@ -79,7 +79,7 @@ private: std::unique_ptr _hideAll; bool _positionsOutdated = false; - SingleTimer _inputCheckTimer; + base::Timer _inputCheckTimer; struct QueuedNotification { QueuedNotification(not_null item, int forwardedCount); diff --git a/Telegram/SourceFiles/window/notifications_utilities.cpp b/Telegram/SourceFiles/window/notifications_utilities.cpp index 18f9e61d01..24ae98b519 100644 --- a/Telegram/SourceFiles/window/notifications_utilities.cpp +++ b/Telegram/SourceFiles/window/notifications_utilities.cpp @@ -21,8 +21,9 @@ constexpr int kNotifyDeletePhotoAfterMs = 60000; } // namespace -CachedUserpics::CachedUserpics(Type type) : _type(type) { - connect(&_clearTimer, SIGNAL(timeout()), this, SLOT(onClear())); +CachedUserpics::CachedUserpics(Type type) +: _type(type) +, _clearTimer([=] { onClear(); }) { QDir().mkpath(cWorkingDir() + qsl("tdata/temp")); } @@ -87,7 +88,7 @@ void CachedUserpics::clearInMs(int ms) { return; } } - _clearTimer.start(ms); + _clearTimer.callOnce(ms); } void CachedUserpics::onClear() { diff --git a/Telegram/SourceFiles/window/notifications_utilities.h b/Telegram/SourceFiles/window/notifications_utilities.h index 4ed6404ed3..a2be9338e3 100644 --- a/Telegram/SourceFiles/window/notifications_utilities.h +++ b/Telegram/SourceFiles/window/notifications_utilities.h @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "window/notifications_manager.h" -#include "core/single_timer.h" +#include "base/timer.h" namespace Window { namespace Notifications { @@ -42,7 +42,7 @@ private: using Images = QMap; Images _images; bool _someSavedFlag = false; - SingleTimer _clearTimer; + base::Timer _clearTimer; }; diff --git a/Telegram/SourceFiles/window/themes/window_theme_warning.cpp b/Telegram/SourceFiles/window/themes/window_theme_warning.cpp index 8aa89dd08d..610f8efa99 100644 --- a/Telegram/SourceFiles/window/themes/window_theme_warning.cpp +++ b/Telegram/SourceFiles/window/themes/window_theme_warning.cpp @@ -21,13 +21,14 @@ constexpr int kWaitBeforeRevertMs = 15999; } // namespace -WarningWidget::WarningWidget(QWidget *parent) : TWidget(parent) +WarningWidget::WarningWidget(QWidget *parent) +: TWidget(parent) +, _timer([=] { handleTimer(); }) , _secondsLeft(kWaitBeforeRevertMs / 1000) , _keepChanges(this, langFactory(lng_theme_keep_changes), st::defaultBoxButton) , _revert(this, langFactory(lng_theme_revert), st::defaultBoxButton) { _keepChanges->setClickedCallback([] { Window::Theme::KeepApplied(); }); _revert->setClickedCallback([] { Window::Theme::Revert(); }); - _timer.setTimeoutHandler([this] { handleTimer(); }); updateText(); } @@ -52,7 +53,7 @@ void WarningWidget::paintEvent(QPaintEvent *e) { _cache = QPixmap(); showChildren(); _started = getms(true); - _timer.start(100); + _timer.callOnce(100); } return; } @@ -100,7 +101,7 @@ void WarningWidget::setSecondsLeft(int secondsLeft) { updateText(); update(); } - _timer.start(100); + _timer.callOnce(100); } } @@ -119,7 +120,7 @@ void WarningWidget::hideAnimated() { } void WarningWidget::startAnimation(bool hiding) { - _timer.stop(); + _timer.cancel(); _hiding = hiding; if (_cache.isNull()) { showChildren(); diff --git a/Telegram/SourceFiles/window/themes/window_theme_warning.h b/Telegram/SourceFiles/window/themes/window_theme_warning.h index 328460e0b9..165d40b19f 100644 --- a/Telegram/SourceFiles/window/themes/window_theme_warning.h +++ b/Telegram/SourceFiles/window/themes/window_theme_warning.h @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "base/timer.h" + namespace Ui { class RoundButton; } // namespace Ui @@ -43,7 +45,7 @@ private: QPixmap _cache; QRect _inner, _outer; - SingleTimer _timer; + base::Timer _timer; TimeMs _started = 0; int _secondsLeft = 0; QString _text; diff --git a/Telegram/gyp/settings_win.gypi b/Telegram/gyp/settings_win.gypi index ca308612eb..dff01287d3 100644 --- a/Telegram/gyp/settings_win.gypi +++ b/Telegram/gyp/settings_win.gypi @@ -30,6 +30,7 @@ '/MP', # Enable multi process build. '/EHsc', # Catch C++ exceptions only, extern C functions never throw a C++ exception. '/w14834', # [[nodiscard]] + '/w15038', # wrong initialization order ], 'TreatWChar_tAsBuiltInType': 'false', }, diff --git a/Telegram/gyp/telegram_sources.txt b/Telegram/gyp/telegram_sources.txt index fa82c6e840..2a2d718b89 100644 --- a/Telegram/gyp/telegram_sources.txt +++ b/Telegram/gyp/telegram_sources.txt @@ -140,8 +140,6 @@ <(src_loc)/core/mime_type.h <(src_loc)/core/shortcuts.cpp <(src_loc)/core/shortcuts.h -<(src_loc)/core/single_timer.cpp -<(src_loc)/core/single_timer.h <(src_loc)/core/update_checker.cpp <(src_loc)/core/update_checker.h <(src_loc)/core/utils.cpp