ensure that closeApplication() is called only once

This commit is contained in:
John Preston 2016-03-02 20:34:42 +02:00
parent cde264e5a1
commit b277f5cdb7
9 changed files with 35 additions and 37 deletions

View File

@ -456,7 +456,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara
ShellExecute(0, 0, (updateTo + L"Telegram.exe").c_str(), (L"-noupdate" + targs).c_str(), 0, SW_SHOWNORMAL); ShellExecute(0, 0, (updateTo + L"Telegram.exe").c_str(), (L"-noupdate" + targs).c_str(), 0, SW_SHOWNORMAL);
} }
writeLog(L"Executed Telegram.exe, closing log and quiting.."); writeLog(L"Executed Telegram.exe, closing log and quitting..");
closeLog(); closeLog();
return 0; return 0;

View File

@ -430,7 +430,7 @@ int main(int argc, char *argv[]) {
case 0: execv(path, args); return 1; case 0: execv(path, args); return 1;
} }
writeLog("Executed Telegram, closing log and quiting.."); writeLog("Executed Telegram, closing log and quitting..");
closeLog(); closeLog();
return 0; return 0;

View File

@ -33,7 +33,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
#include "numbers.h" #include "numbers.h"
namespace { namespace {
bool quiting = false; App::LaunchState _launchState = App::Launched;
UserData *self = 0; UserData *self = 0;
@ -1791,7 +1791,7 @@ namespace App {
} }
::repliesTo.erase(j); ::repliesTo.erase(j);
} }
if (App::main() && !App::quiting()) { if (App::main() && !App::quitting()) {
App::main()->itemRemoved(item); App::main()->itemRemoved(item);
} }
} }
@ -2155,21 +2155,22 @@ namespace App {
} }
void quit() { void quit() {
if (quiting()) return; if (quitting()) return;
setLaunchState(QuitRequested);
setQuiting();
if (wnd()) {
wnd()->quit();
}
Application::quit(); Application::quit();
} }
bool quiting() { bool quitting() {
return ::quiting; return _launchState != Launched;
} }
void setQuiting() { LaunchState launchState() {
::quiting = true; return _launchState;
}
void setLaunchState(LaunchState state) {
_launchState = state;
} }
QImage readImage(QByteArray data, QByteArray *format, bool opaque, bool *animated) { QImage readImage(QByteArray data, QByteArray *format, bool opaque, bool *animated) {

View File

@ -191,9 +191,15 @@ namespace App {
bool isValidPhone(QString phone); bool isValidPhone(QString phone);
enum LaunchState {
Launched = 0,
QuitRequested = 1,
QuitProcessed = 2,
};
void quit(); void quit();
bool quiting(); bool quitting();
void setQuiting(); LaunchState launchState();
void setLaunchState(LaunchState state);
QImage readImage(QByteArray data, QByteArray *format = 0, bool opaque = true, bool *animated = 0); QImage readImage(QByteArray data, QByteArray *format = 0, bool opaque = true, bool *animated = 0);
QImage readImage(const QString &file, QByteArray *format = 0, bool opaque = true, bool *animated = 0, QByteArray *content = 0); QImage readImage(const QString &file, QByteArray *format = 0, bool opaque = true, bool *animated = 0, QByteArray *content = 0);

View File

@ -166,16 +166,16 @@ void Application::socketReading() {
if (QRegularExpression("RES:(\\d+);").match(_localSocketReadData).hasMatch()) { if (QRegularExpression("RES:(\\d+);").match(_localSocketReadData).hasMatch()) {
uint64 pid = _localSocketReadData.mid(4, _localSocketReadData.length() - 5).toULongLong(); uint64 pid = _localSocketReadData.mid(4, _localSocketReadData.length() - 5).toULongLong();
psActivateProcess(pid); psActivateProcess(pid);
LOG(("Show command response received, pid = %1, activating and quiting..").arg(pid)); LOG(("Show command response received, pid = %1, activating and quitting..").arg(pid));
return App::quit(); return App::quit();
} }
} }
void Application::socketError(QLocalSocket::LocalSocketError e) { void Application::socketError(QLocalSocket::LocalSocketError e) {
if (App::quiting()) return; if (App::quitting()) return;
if (_secondInstance) { if (_secondInstance) {
LOG(("Could not write show command, error %1, quiting..").arg(e)); LOG(("Could not write show command, error %1, quitting..").arg(e));
return App::quit(); return App::quit();
} }
@ -235,7 +235,7 @@ void Application::singleInstanceChecked() {
void Application::socketDisconnected() { void Application::socketDisconnected() {
if (_secondInstance) { if (_secondInstance) {
DEBUG_LOG(("Application Error: socket disconnected before command response received, quiting..")); DEBUG_LOG(("Application Error: socket disconnected before command response received, quitting.."));
return App::quit(); return App::quit();
} }
} }
@ -314,14 +314,15 @@ void Application::removeClients() {
} }
void Application::startApplication() { void Application::startApplication() {
if (App::quiting()) { if (App::quitting()) {
quit(); quit();
} }
} }
void Application::closeApplication() { void Application::closeApplication() {
if (App::launchState() != App::QuitProcessed);
App::setLaunchState(App::QuitProcessed);
App::quit(); App::quit();
delete AppObject; delete AppObject;
AppObject = 0; AppObject = 0;
@ -973,7 +974,7 @@ void AppClass::onSwitchTestMode() {
} }
FileUploader *AppClass::uploader() { FileUploader *AppClass::uploader() {
if (!_uploader && !App::quiting()) _uploader = new FileUploader(); if (!_uploader && !App::quitting()) _uploader = new FileUploader();
return _uploader; return _uploader;
} }

View File

@ -154,7 +154,7 @@ namespace Ui {
} }
bool hideWindowNoQuit() { bool hideWindowNoQuit() {
if (!App::quiting()) { if (!App::quitting()) {
if (Window *w = App::wnd()) { if (Window *w = App::wnd()) {
if (cWorkMode() == dbiwmTrayOnly || cWorkMode() == dbiwmWindowAndTray) { if (cWorkMode() == dbiwmTrayOnly || cWorkMode() == dbiwmWindowAndTray) {
return w->minimizeToTray(); return w->minimizeToTray();

View File

@ -2526,7 +2526,7 @@ void History::clear(bool leaveItems) {
} }
overview[i].clear(); overview[i].clear();
overviewIds[i].clear(); overviewIds[i].clear();
if (App::wnd() && !App::quiting()) App::wnd()->mediaOverviewUpdated(peer, MediaOverviewType(i)); if (App::wnd() && !App::quitting()) App::wnd()->mediaOverviewUpdated(peer, MediaOverviewType(i));
} }
} }
Blocks lst = blocks; Blocks lst = blocks;

View File

@ -1105,7 +1105,7 @@ void Window::mouseReleaseEvent(QMouseEvent *e) {
} }
bool Window::minimizeToTray() { bool Window::minimizeToTray() {
if (App::quiting() || !psHasTrayIcon()) return false; if (App::quitting() || !psHasTrayIcon()) return false;
hide(); hide();
if (cPlatform() == dbipWindows && trayIcon && !cSeenTrayTooltip()) { if (cPlatform() == dbipWindows && trayIcon && !cSeenTrayTooltip()) {
@ -1372,16 +1372,8 @@ void Window::onClearFailed(int task, void *manager) {
emit tempDirClearFailed(task); emit tempDirClearFailed(task);
} }
void Window::quit() {
delete _mediaView;
_mediaView = 0;
delete main;
main = 0;
notifyClearFast();
}
void Window::notifySchedule(History *history, HistoryItem *item) { void Window::notifySchedule(History *history, HistoryItem *item) {
if (App::quiting() || !history->currentNotification() || !main) return; if (App::quitting() || !history->currentNotification() || !main) return;
PeerData *notifyByFrom = (!history->peer->isUser() && item->mentionsMe()) ? item->from() : 0; PeerData *notifyByFrom = (!history->peer->isUser() && item->mentionsMe()) ? item->from() : 0;
@ -1514,7 +1506,7 @@ void Window::notifySettingGot() {
} }
void Window::notifyShowNext(NotifyWindow *remove) { void Window::notifyShowNext(NotifyWindow *remove) {
if (App::quiting()) return; if (App::quitting()) return;
int32 count = NotifyWindowsCount; int32 count = NotifyWindowsCount;
if (remove) { if (remove) {

View File

@ -206,8 +206,6 @@ public:
TempDirState localStorageState(); TempDirState localStorageState();
void tempDirDelete(int task); void tempDirDelete(int task);
void quit();
void notifySettingGot(); void notifySettingGot();
void notifySchedule(History *history, HistoryItem *item); void notifySchedule(History *history, HistoryItem *item);
void notifyClear(History *history = 0); void notifyClear(History *history = 0);