2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-09-01 15:05:56 +00:00

Moved AppClass to messenger.cpp:Messenger.

This commit is contained in:
John Preston
2017-02-23 13:59:19 +03:00
parent 63c61637f8
commit c3b3819d9f
40 changed files with 940 additions and 962 deletions

View File

@@ -25,6 +25,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "pspecific.h"
#include "fileuploader.h"
#include "mainwidget.h"
#include "mainwindow.h"
#include "lang.h"
#include "boxes/confirmbox.h"
#include "ui/filedialog.h"
@@ -40,22 +41,12 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "history/history_location_manager.h"
#include "core/task_queue.h"
#include "mtproto/dc_options.h"
#include "core/single_timer.h"
#include "auth_session.h"
#include "messenger.h"
namespace {
void mtpStateChanged(int32 dc, int32 state) {
if (App::wnd()) {
App::wnd()->mtpStateChanged(dc, state);
}
}
void mtpSessionReset(int32 dc) {
if (App::main() && dc == MTP::maindc()) {
App::main()->getDifference();
}
}
QChar _toHex(ushort v) {
v = v & 0x000F;
return QChar::fromLatin1((v >= 10) ? ('a' + (v - 10)) : ('0' + v));
@@ -96,8 +87,6 @@ QString _escapeFrom7bit(const QString &str) {
} // namespace
AppClass *AppObject = nullptr;
Application::Application(int &argc, char **argv) : QApplication(argc, argv) {
QByteArray d(QFile::encodeName(QDir(cWorkingDir()).absolutePath()));
char h[33] = { 0 };
@@ -120,7 +109,8 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv) {
connect(this, SIGNAL(aboutToQuit()), this, SLOT(closeApplication()));
#ifndef TDESKTOP_DISABLE_AUTOUPDATE
connect(&_updateCheckTimer, SIGNAL(timeout()), this, SLOT(updateCheck()));
_updateCheckTimer.create(this);
connect(_updateCheckTimer, SIGNAL(timeout()), this, SLOT(updateCheck()));
connect(this, SIGNAL(updateFailed()), this, SLOT(onUpdateFailed()));
connect(this, SIGNAL(updateReady()), this, SLOT(onUpdateReady()));
#endif // !TDESKTOP_DISABLE_AUTOUPDATE
@@ -134,6 +124,8 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv) {
}
}
Application::~Application() = default;
bool Application::event(QEvent *e) {
if (e->type() == QEvent::Close) {
App::quit();
@@ -334,6 +326,11 @@ void Application::startApplication() {
}
}
void Application::createMessenger() {
t_assert(!App::quitting());
_messengerInstance = std::make_unique<Messenger>();
}
void Application::closeApplication() {
if (App::launchState() == App::QuitProcessed) return;
App::setLaunchState(App::QuitProcessed);
@@ -342,9 +339,9 @@ void Application::closeApplication() {
manager->clearAllFast();
}
if (AppObject) {
AppClass::Instance().joinThreads();
delete base::take(AppObject);
if (_messengerInstance) {
Messenger::Instance().prepareToDestroy();
_messengerInstance.reset();
}
Sandbox::finish();
@@ -429,9 +426,9 @@ void Application::updateFailedCurrent(QNetworkReply::NetworkError e) {
void Application::onUpdateReady() {
if (_updateChecker) {
_updateChecker->deleteLater();
_updateChecker = 0;
_updateChecker = nullptr;
}
_updateCheckTimer.stop();
_updateCheckTimer->stop();
cSetLastUpdateCheck(unixtime());
Local::writeSettings();
@@ -482,7 +479,7 @@ void Application::stopUpdate() {
void Application::startUpdateCheck(bool forceWait) {
if (!Sandbox::started()) return;
_updateCheckTimer.stop();
_updateCheckTimer->stop();
if (_updateThread || _updateReply || !cAutoUpdate()) return;
int32 constDelay = cBetaVersion() ? 600 : UpdateDelayConstPart, randDelay = cBetaVersion() ? 300 : UpdateDelayRandPart;
@@ -520,7 +517,7 @@ void Application::startUpdateCheck(bool forceWait) {
connect(_updateReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(updateFailedCurrent(QNetworkReply::NetworkError)));
emit updateChecking();
} else {
_updateCheckTimer.start((updateInSecs + 5) * 1000);
_updateCheckTimer->start((updateInSecs + 5) * 1000);
}
}
@@ -532,638 +529,154 @@ inline Application *application() {
namespace Sandbox {
QRect availableGeometry() {
if (Application *a = application()) {
return a->desktop()->availableGeometry();
}
return QDesktopWidget().availableGeometry();
QRect availableGeometry() {
if (auto a = application()) {
return a->desktop()->availableGeometry();
}
return QDesktopWidget().availableGeometry();
}
QRect screenGeometry(const QPoint &p) {
if (Application *a = application()) {
return a->desktop()->screenGeometry(p);
}
return QDesktopWidget().screenGeometry(p);
QRect screenGeometry(const QPoint &p) {
if (auto a = application()) {
return a->desktop()->screenGeometry(p);
}
return QDesktopWidget().screenGeometry(p);
}
void setActiveWindow(QWidget *window) {
if (Application *a = application()) {
a->setActiveWindow(window);
}
}
bool isSavingSession() {
if (Application *a = application()) {
return a->isSavingSession();
}
return false;
}
void installEventFilter(QObject *filter) {
if (Application *a = application()) {
a->installEventFilter(filter);
}
}
void removeEventFilter(QObject *filter) {
if (Application *a = application()) {
a->removeEventFilter(filter);
}
}
void execExternal(const QString &cmd) {
DEBUG_LOG(("Application Info: executing external command '%1'").arg(cmd));
if (cmd == "show") {
if (App::wnd()) {
App::wnd()->activate();
} else if (PreLaunchWindow::instance()) {
PreLaunchWindow::instance()->activate();
}
}
}
#ifndef TDESKTOP_DISABLE_AUTOUPDATE
void startUpdateCheck() {
if (Application *a = application()) {
return a->startUpdateCheck(false);
}
}
void stopUpdate() {
if (Application *a = application()) {
return a->stopUpdate();
}
}
Application::UpdatingState updatingState() {
if (Application *a = application()) {
return a->updatingState();
}
return Application::UpdatingNone;
}
int32 updatingSize() {
if (Application *a = application()) {
return a->updatingSize();
}
return 0;
}
int32 updatingReady() {
if (Application *a = application()) {
return a->updatingReady();
}
return 0;
}
void updateChecking() {
if (Application *a = application()) {
emit a->updateChecking();
}
}
void updateLatest() {
if (Application *a = application()) {
emit a->updateLatest();
}
}
void updateProgress(qint64 ready, qint64 total) {
if (Application *a = application()) {
emit a->updateProgress(ready, total);
}
}
void updateFailed() {
if (Application *a = application()) {
emit a->updateFailed();
}
}
void updateReady() {
if (Application *a = application()) {
emit a->updateReady();
}
}
#endif // !TDESKTOP_DISABLE_AUTOUPDATE
void connect(const char *signal, QObject *object, const char *method) {
if (Application *a = application()) {
a->connect(a, signal, object, method);
}
}
void launch() {
t_assert(application() != 0);
float64 dpi = Application::primaryScreen()->logicalDotsPerInch();
if (dpi <= 108) { // 0-96-108
cSetScreenScale(dbisOne);
} else if (dpi <= 132) { // 108-120-132
cSetScreenScale(dbisOneAndQuarter);
} else if (dpi <= 168) { // 132-144-168
cSetScreenScale(dbisOneAndHalf);
} else { // 168-192-inf
cSetScreenScale(dbisTwo);
}
auto devicePixelRatio = application()->devicePixelRatio();
if (devicePixelRatio > 1.) {
if ((cPlatform() != dbipMac && cPlatform() != dbipMacOld) || (devicePixelRatio != 2.)) {
LOG(("Found non-trivial Device Pixel Ratio: %1").arg(devicePixelRatio));
LOG(("Environmental variables: QT_DEVICE_PIXEL_RATIO='%1'").arg(QString::fromLatin1(qgetenv("QT_DEVICE_PIXEL_RATIO"))));
LOG(("Environmental variables: QT_SCALE_FACTOR='%1'").arg(QString::fromLatin1(qgetenv("QT_SCALE_FACTOR"))));
LOG(("Environmental variables: QT_AUTO_SCREEN_SCALE_FACTOR='%1'").arg(QString::fromLatin1(qgetenv("QT_AUTO_SCREEN_SCALE_FACTOR"))));
LOG(("Environmental variables: QT_SCREEN_SCALE_FACTORS='%1'").arg(QString::fromLatin1(qgetenv("QT_SCREEN_SCALE_FACTORS"))));
}
cSetRetina(true);
cSetRetinaFactor(devicePixelRatio);
cSetIntRetinaFactor(int32(cRetinaFactor()));
cSetConfigScale(dbisOne);
cSetRealScale(dbisOne);
}
new AppClass();
void setActiveWindow(QWidget *window) {
if (auto a = application()) {
a->setActiveWindow(window);
}
}
AppClass::AppClass() : QObject() {
AppObject = this;
Fonts::start();
ThirdParty::start();
Global::start();
startLocalStorage();
if (Local::oldSettingsVersion() < AppVersion) {
psNewVersion();
}
if (cLaunchMode() == LaunchModeAutoStart && !cAutoStart()) {
psAutoStart(false, true);
App::quit();
return;
}
if (cRetina()) {
cSetConfigScale(dbisOne);
cSetRealScale(dbisOne);
}
loadLanguage();
style::startManager();
anim::startManager();
historyInit();
Media::Player::start();
Window::Notifications::start();
DEBUG_LOG(("Application Info: inited..."));
application()->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.
QMimeDatabase().mimeTypeForName(qsl("text/plain"));
_window = new MainWindow();
_window->createWinId();
_window->init();
Sandbox::connect(SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(onAppStateChanged(Qt::ApplicationState)));
DEBUG_LOG(("Application Info: window created..."));
Shortcuts::start();
initLocationManager();
App::initMedia();
Local::ReadMapState state = Local::readMap(QByteArray());
if (state == Local::ReadMapPassNeeded) {
Global::SetLocalPasscode(true);
Global::RefLocalPasscodeChanged().notify();
DEBUG_LOG(("Application Info: passcode needed..."));
} else {
DEBUG_LOG(("Application Info: local map read..."));
MTP::start();
}
MTP::setStateChangedHandler(mtpStateChanged);
MTP::setSessionResetHandler(mtpSessionReset);
DEBUG_LOG(("Application Info: MTP started..."));
DEBUG_LOG(("Application Info: showing."));
if (state == Local::ReadMapPassNeeded) {
_window->setupPasscode();
} else {
if (AuthSession::Current()) {
_window->setupMain();
} else {
_window->setupIntro();
}
}
_window->firstShow();
if (cStartToSettings()) {
_window->showSettings();
}
#ifndef TDESKTOP_DISABLE_NETWORK_PROXY
QNetworkProxyFactory::setUseSystemConfiguration(true);
#endif // !TDESKTOP_DISABLE_NETWORK_PROXY
if (state != Local::ReadMapPassNeeded) {
checkMapVersion();
}
_window->updateIsActive(Global::OnlineFocusTimeout());
if (!Shortcuts::errors().isEmpty()) {
const QStringList &errors(Shortcuts::errors());
for (QStringList::const_iterator i = errors.cbegin(), e = errors.cend(); i != e; ++i) {
LOG(("Shortcuts Error: %1").arg(*i));
}
}
}
void AppClass::loadLanguage() {
if (cLang() < languageTest) {
cSetLang(Sandbox::LangSystem());
}
if (cLang() == languageTest) {
if (QFileInfo(cLangFile()).exists()) {
LangLoaderPlain loader(cLangFile());
cSetLangErrors(loader.errors());
if (!cLangErrors().isEmpty()) {
LOG(("Lang load errors: %1").arg(cLangErrors()));
} else if (!loader.warnings().isEmpty()) {
LOG(("Lang load warnings: %1").arg(loader.warnings()));
}
} else {
cSetLang(languageDefault);
}
} else if (cLang() > languageDefault && cLang() < languageCount) {
LangLoaderPlain loader(qsl(":/langs/lang_") + LanguageCodes[cLang()].c_str() + qsl(".strings"));
if (!loader.errors().isEmpty()) {
LOG(("Lang load errors: %1").arg(loader.errors()));
} else if (!loader.warnings().isEmpty()) {
LOG(("Lang load warnings: %1").arg(loader.warnings()));
}
}
application()->installTranslator(_translator = new Translator());
}
void AppClass::startLocalStorage() {
_dcOptions = std::make_unique<MTP::DcOptions>();
_dcOptions->constructFromBuiltIn();
Local::start();
subscribe(_dcOptions->changed(), [](const MTP::DcOptions::Ids &ids) {
Local::writeSettings();
for (auto id : ids) {
MTP::restart(id);
}
});
}
void AppClass::regPhotoUpdate(const PeerId &peer, const FullMsgId &msgId) {
photoUpdates.insert(msgId, peer);
}
bool AppClass::isPhotoUpdating(const PeerId &peer) {
for (QMap<FullMsgId, PeerId>::iterator i = photoUpdates.begin(), e = photoUpdates.end(); i != e; ++i) {
if (i.value() == peer) {
return true;
}
bool isSavingSession() {
if (auto a = application()) {
return a->isSavingSession();
}
return false;
}
void AppClass::cancelPhotoUpdate(const PeerId &peer) {
for (QMap<FullMsgId, PeerId>::iterator i = photoUpdates.begin(), e = photoUpdates.end(); i != e;) {
if (i.value() == peer) {
i = photoUpdates.erase(i);
} else {
++i;
void execExternal(const QString &cmd) {
DEBUG_LOG(("Application Info: executing external command '%1'").arg(cmd));
if (cmd == "show") {
if (App::wnd()) {
App::wnd()->activate();
} else if (PreLaunchWindow::instance()) {
PreLaunchWindow::instance()->activate();
}
}
}
void AppClass::selfPhotoCleared(const MTPUserProfilePhoto &result) {
if (!App::self()) return;
App::self()->setPhoto(result);
emit peerPhotoDone(App::self()->id);
}
void AppClass::chatPhotoCleared(PeerId peer, const MTPUpdates &updates) {
if (App::main()) {
App::main()->sentUpdatesReceived(updates);
void adjustSingleTimers() {
if (auto a = application()) {
a->adjustSingleTimers();
}
cancelPhotoUpdate(peer);
emit peerPhotoDone(peer);
}
void AppClass::selfPhotoDone(const MTPphotos_Photo &result) {
if (!App::self()) return;
const auto &photo(result.c_photos_photo());
App::feedPhoto(photo.vphoto);
App::feedUsers(photo.vusers);
cancelPhotoUpdate(App::self()->id);
emit peerPhotoDone(App::self()->id);
}
#ifndef TDESKTOP_DISABLE_AUTOUPDATE
void AppClass::chatPhotoDone(PeerId peer, const MTPUpdates &updates) {
if (App::main()) {
App::main()->sentUpdatesReceived(updates);
void startUpdateCheck() {
if (auto a = application()) {
return a->startUpdateCheck(false);
}
cancelPhotoUpdate(peer);
emit peerPhotoDone(peer);
}
bool AppClass::peerPhotoFail(PeerId peer, const RPCError &error) {
if (MTP::isDefaultHandledError(error)) return false;
LOG(("Application Error: update photo failed %1: %2").arg(error.type()).arg(error.description()));
cancelPhotoUpdate(peer);
emit peerPhotoFail(peer);
return true;
void stopUpdate() {
if (auto a = application()) {
return a->stopUpdate();
}
}
void AppClass::peerClearPhoto(PeerId id) {
if (!AuthSession::Current()) return;
Application::UpdatingState updatingState() {
if (auto a = application()) {
return a->updatingState();
}
return Application::UpdatingNone;
}
if (id == AuthSession::CurrentUserPeerId()) {
MTP::send(MTPphotos_UpdateProfilePhoto(MTP_inputPhotoEmpty()), rpcDone(&AppClass::selfPhotoCleared), rpcFail(&AppClass::peerPhotoFail, id));
} else if (peerIsChat(id)) {
MTP::send(MTPmessages_EditChatPhoto(peerToBareMTPInt(id), MTP_inputChatPhotoEmpty()), rpcDone(&AppClass::chatPhotoCleared, id), rpcFail(&AppClass::peerPhotoFail, id));
} else if (peerIsChannel(id)) {
if (auto channel = App::channelLoaded(id)) {
MTP::send(MTPchannels_EditPhoto(channel->inputChannel, MTP_inputChatPhotoEmpty()), rpcDone(&AppClass::chatPhotoCleared, id), rpcFail(&AppClass::peerPhotoFail, id));
int32 updatingSize() {
if (auto a = application()) {
return a->updatingSize();
}
return 0;
}
int32 updatingReady() {
if (auto a = application()) {
return a->updatingReady();
}
return 0;
}
void updateChecking() {
if (auto a = application()) {
emit a->updateChecking();
}
}
void updateLatest() {
if (auto a = application()) {
emit a->updateLatest();
}
}
void updateProgress(qint64 ready, qint64 total) {
if (auto a = application()) {
emit a->updateProgress(ready, total);
}
}
void updateFailed() {
if (auto a = application()) {
emit a->updateFailed();
}
}
void updateReady() {
if (auto a = application()) {
emit a->updateReady();
}
}
#endif // !TDESKTOP_DISABLE_AUTOUPDATE
void connect(const char *signal, QObject *object, const char *method) {
if (auto a = application()) {
a->connect(a, signal, object, method);
}
}
void launch() {
t_assert(application() != 0);
float64 dpi = Application::primaryScreen()->logicalDotsPerInch();
if (dpi <= 108) { // 0-96-108
cSetScreenScale(dbisOne);
} else if (dpi <= 132) { // 108-120-132
cSetScreenScale(dbisOneAndQuarter);
} else if (dpi <= 168) { // 132-144-168
cSetScreenScale(dbisOneAndHalf);
} else { // 168-192-inf
cSetScreenScale(dbisTwo);
}
auto devicePixelRatio = application()->devicePixelRatio();
if (devicePixelRatio > 1.) {
if ((cPlatform() != dbipMac && cPlatform() != dbipMacOld) || (devicePixelRatio != 2.)) {
LOG(("Found non-trivial Device Pixel Ratio: %1").arg(devicePixelRatio));
LOG(("Environmental variables: QT_DEVICE_PIXEL_RATIO='%1'").arg(QString::fromLatin1(qgetenv("QT_DEVICE_PIXEL_RATIO"))));
LOG(("Environmental variables: QT_SCALE_FACTOR='%1'").arg(QString::fromLatin1(qgetenv("QT_SCALE_FACTOR"))));
LOG(("Environmental variables: QT_AUTO_SCREEN_SCALE_FACTOR='%1'").arg(QString::fromLatin1(qgetenv("QT_AUTO_SCREEN_SCALE_FACTOR"))));
LOG(("Environmental variables: QT_SCREEN_SCALE_FACTORS='%1'").arg(QString::fromLatin1(qgetenv("QT_SCREEN_SCALE_FACTORS"))));
}
cSetRetina(true);
cSetRetinaFactor(devicePixelRatio);
cSetIntRetinaFactor(int32(cRetinaFactor()));
cSetConfigScale(dbisOne);
cSetRealScale(dbisOne);
}
application()->createMessenger();
}
void AppClass::killDownloadSessionsStart(int32 dc) {
if (killDownloadSessionTimes.constFind(dc) == killDownloadSessionTimes.cend()) {
killDownloadSessionTimes.insert(dc, getms() + MTPAckSendWaiting + MTPKillFileSessionTimeout);
}
if (!killDownloadSessionsTimer.isActive()) {
killDownloadSessionsTimer.start(MTPAckSendWaiting + MTPKillFileSessionTimeout + 5);
}
}
void AppClass::killDownloadSessionsStop(int32 dc) {
killDownloadSessionTimes.remove(dc);
if (killDownloadSessionTimes.isEmpty() && killDownloadSessionsTimer.isActive()) {
killDownloadSessionsTimer.stop();
}
}
void AppClass::checkLocalTime() {
if (App::main()) App::main()->checkLastUpdate(checkms());
}
void AppClass::onAppStateChanged(Qt::ApplicationState state) {
if (state == Qt::ApplicationActive) {
handleAppActivated();
} else {
handleAppDeactivated();
}
}
void AppClass::handleAppActivated() {
checkLocalTime();
if (_window) {
_window->updateIsActive(Global::OnlineFocusTimeout());
}
}
void AppClass::handleAppDeactivated() {
if (_window) {
_window->updateIsActive(Global::OfflineBlurTimeout());
}
Ui::Tooltip::Hide();
}
void AppClass::call_handleHistoryUpdate() {
Notify::handlePendingHistoryUpdate();
}
void AppClass::call_handleUnreadCounterUpdate() {
Global::RefUnreadCounterUpdate().notify(true);
}
void AppClass::call_handleFileDialogQueue() {
while (true) {
if (!FileDialog::processQuery()) {
return;
}
}
}
void AppClass::call_handleDelayedPeerUpdates() {
Notify::peerUpdatedSendDelayed();
}
void AppClass::call_handleObservables() {
base::HandleObservables();
}
void AppClass::killDownloadSessions() {
auto ms = getms(), left = static_cast<TimeMs>(MTPAckSendWaiting) + MTPKillFileSessionTimeout;
for (auto i = killDownloadSessionTimes.begin(); i != killDownloadSessionTimes.end(); ) {
if (i.value() <= ms) {
for (int j = 0; j < MTPDownloadSessionsCount; ++j) {
MTP::stopSession(MTP::dldDcId(i.key(), j));
}
i = killDownloadSessionTimes.erase(i);
} else {
if (i.value() - ms < left) {
left = i.value() - ms;
}
++i;
}
}
if (!killDownloadSessionTimes.isEmpty()) {
killDownloadSessionsTimer.start(left);
}
}
void AppClass::photoUpdated(const FullMsgId &msgId, bool silent, const MTPInputFile &file) {
if (!AuthSession::Current()) return;
auto i = photoUpdates.find(msgId);
if (i != photoUpdates.end()) {
auto id = i.value();
if (id == AuthSession::CurrentUserPeerId()) {
MTP::send(MTPphotos_UploadProfilePhoto(file), rpcDone(&AppClass::selfPhotoDone), rpcFail(&AppClass::peerPhotoFail, id));
} else if (peerIsChat(id)) {
auto history = App::history(id);
history->sendRequestId = MTP::send(MTPmessages_EditChatPhoto(history->peer->asChat()->inputChat, MTP_inputChatUploadedPhoto(file)), rpcDone(&AppClass::chatPhotoDone, id), rpcFail(&AppClass::peerPhotoFail, id), 0, 0, history->sendRequestId);
} else if (peerIsChannel(id)) {
auto history = App::history(id);
history->sendRequestId = MTP::send(MTPchannels_EditPhoto(history->peer->asChannel()->inputChannel, MTP_inputChatUploadedPhoto(file)), rpcDone(&AppClass::chatPhotoDone, id), rpcFail(&AppClass::peerPhotoFail, id), 0, 0, history->sendRequestId);
}
}
}
void AppClass::onSwitchDebugMode() {
if (cDebug()) {
QFile(cWorkingDir() + qsl("tdata/withdebug")).remove();
cSetDebug(false);
App::restart();
} else {
cSetDebug(true);
DEBUG_LOG(("Debug logs started."));
QFile f(cWorkingDir() + qsl("tdata/withdebug"));
if (f.open(QIODevice::WriteOnly)) {
f.write("1");
f.close();
}
Ui::hideLayer();
}
}
void AppClass::onSwitchWorkMode() {
Global::SetDialogsModeEnabled(!Global::DialogsModeEnabled());
Global::SetDialogsMode(Dialogs::Mode::All);
Local::writeUserSettings();
App::restart();
}
void AppClass::onSwitchTestMode() {
if (cTestMode()) {
QFile(cWorkingDir() + qsl("tdata/withtestmode")).remove();
cSetTestMode(false);
} else {
QFile f(cWorkingDir() + qsl("tdata/withtestmode"));
if (f.open(QIODevice::WriteOnly)) {
f.write("1");
f.close();
}
cSetTestMode(true);
}
App::restart();
}
void AppClass::authSessionCreate(UserId userId) {
_authSession = std::make_unique<AuthSession>(userId);
}
void AppClass::authSessionDestroy() {
_authSession.reset();
}
FileUploader *AppClass::uploader() {
if (!_uploader && !App::quitting()) _uploader = new FileUploader();
return _uploader;
}
void AppClass::uploadProfilePhoto(const QImage &tosend, const PeerId &peerId) {
PreparedPhotoThumbs photoThumbs;
QVector<MTPPhotoSize> photoSizes;
auto thumb = App::pixmapFromImageInPlace(tosend.scaled(160, 160, Qt::KeepAspectRatio, Qt::SmoothTransformation));
photoThumbs.insert('a', thumb);
photoSizes.push_back(MTP_photoSize(MTP_string("a"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(thumb.width()), MTP_int(thumb.height()), MTP_int(0)));
auto medium = App::pixmapFromImageInPlace(tosend.scaled(320, 320, Qt::KeepAspectRatio, Qt::SmoothTransformation));
photoThumbs.insert('b', medium);
photoSizes.push_back(MTP_photoSize(MTP_string("b"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(medium.width()), MTP_int(medium.height()), MTP_int(0)));
auto full = QPixmap::fromImage(tosend, Qt::ColorOnly);
photoThumbs.insert('c', full);
photoSizes.push_back(MTP_photoSize(MTP_string("c"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0)));
QByteArray jpeg;
QBuffer jpegBuffer(&jpeg);
full.save(&jpegBuffer, "JPG", 87);
PhotoId id = rand_value<PhotoId>();
MTPDphoto::Flags photoFlags = 0;
auto photo = MTP_photo(MTP_flags(photoFlags), MTP_long(id), MTP_long(0), MTP_int(unixtime()), MTP_vector<MTPPhotoSize>(photoSizes));
QString file, filename;
int32 filesize = 0;
QByteArray data;
SendMediaReady ready(SendMediaType::Photo, file, filename, filesize, data, id, id, qsl("jpg"), peerId, photo, photoThumbs, MTP_documentEmpty(MTP_long(0)), jpeg, 0);
connect(App::uploader(), SIGNAL(photoReady(const FullMsgId&,bool,const MTPInputFile&)), App::app(), SLOT(photoUpdated(const FullMsgId&,bool,const MTPInputFile&)), Qt::UniqueConnection);
FullMsgId newId(peerToChannel(peerId), clientMsgId());
App::app()->regPhotoUpdate(peerId, newId);
App::uploader()->uploadMedia(newId, ready);
}
void AppClass::checkMapVersion() {
if (Local::oldMapVersion() < AppVersion) {
if (Local::oldMapVersion()) {
QString versionFeatures;
if ((cAlphaVersion() || cBetaVersion()) && Local::oldMapVersion() < 1000010) {
versionFeatures = QString::fromUtf8("\xe2\x80\x94 Support for more emoji.\n\xe2\x80\x94 Bug fixes and other minor improvements.");
} else if (!(cAlphaVersion() || cBetaVersion()) && Local::oldMapVersion() < 1000012) {
versionFeatures = langNewVersionText();
} else {
versionFeatures = lang(lng_new_version_minor).trimmed();
}
if (!versionFeatures.isEmpty()) {
versionFeatures = lng_new_version_wrap(lt_version, QString::fromLatin1(AppVersionStr.c_str()), lt_changes, versionFeatures, lt_link, qsl("https://desktop.telegram.org/changelog"));
_window->serviceNotificationLocal(versionFeatures);
}
}
}
}
void AppClass::joinThreads() {
MTP::finish();
}
AppClass::~AppClass() {
Shortcuts::finish();
delete base::take(_window);
App::clearHistories();
Window::Notifications::finish();
anim::stopManager();
stopWebLoadManager();
App::deinitMedia();
deinitLocationManager();
AppObject = nullptr;
delete base::take(_uploader);
delete base::take(_translator);
Window::Theme::Unload();
Media::Player::finish();
style::stopManager();
Local::finish();
Global::finish();
ThirdParty::finish();
}
AppClass *AppClass::app() {
return AppObject;
}
MainWindow *AppClass::wnd() {
return AppObject ? AppObject->_window : nullptr;
}
MainWidget *AppClass::main() {
return (AppObject && AppObject->_window) ? AppObject->_window->mainWidget() : nullptr;
}
} // namespace Sandbox