/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org

Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014 John Preston, https://desktop.telegram.org
*/
#include "stdafx.h"
#include "application.h"
#include "style.h"

#include "pspecific.h"
#include "fileuploader.h"
#include "mainwidget.h"
#include "supporttl.h"

#include "lang.h"
#include "boxes/confirmbox.h"
#include "langloaderplain.h"

#include "localstorage.h"

namespace {
	Application *mainApp = 0;
	FileUploader *uploader = 0;
	QString lng;

	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();
		}
	}

	class _DebugWaiter : public QObject {
	public:

		_DebugWaiter(QObject *parent) : QObject(parent), _debugState(0) {

		}
		bool eventFilter(QObject *o, QEvent *e) {
			if (e->type() == QEvent::KeyPress) {
				QKeyEvent *ev = static_cast<QKeyEvent*>(e);
				switch (_debugState) {
				case 0: if (ev->key() == Qt::Key_F12) _debugState = 1; break;
				case 1: if (ev->key() == Qt::Key_F11) _debugState = 2; else if (ev->key() != Qt::Key_F12) _debugState = 0; break;
				case 2: if (ev->key() == Qt::Key_F10) _debugState = 3; else if (ev->key() != Qt::Key_F11) _debugState = 0; break;
				case 3: if (ev->key() == Qt::Key_F11) _debugState = 4; else if (ev->key() != Qt::Key_F10) _debugState = 0; break;
				case 4: if (ev->key() == Qt::Key_F12) offerDebug(); if (ev->key() != Qt::Key_F11) _debugState = 0; break;
				}

				if (cPlatform() == dbipMac && ev->key() == Qt::Key_W && (ev->modifiers() & (Qt::MetaModifier | Qt::ControlModifier))) {
					if (cWorkMode() == dbiwmTrayOnly || cWorkMode() == dbiwmWindowAndTray) {
						App::wnd()->minimizeToTray();
						return true;
					}
				}
			}
			return QObject::eventFilter(o, e);
		}
		void offerDebug() {
			ConfirmBox *box = new ConfirmBox(lang(lng_sure_enable_debug));
			connect(box, SIGNAL(confirmed()), App::app(), SLOT(onEnableDebugMode()));
			App::wnd()->showLayer(box);
		}

	private:

		int _debugState;
	};
}

Application::Application(int &argc, char **argv) : PsApplication(argc, argv),
    serverName(psServerPrefix() + cGUIDStr()), closing(false),
	updateRequestId(0), updateReply(0), updateThread(0), updateDownloader(0) {

	DEBUG_LOG(("Application Info: creation.."));

	QByteArray d(QDir((cPlatform() == dbipWindows ? cExeDir() : cWorkingDir()).toLower()).absolutePath().toUtf8());
	char h[33] = { 0 };
	hashMd5Hex(d.constData(), d.size(), h);
	serverName = psServerPrefix() + h + '-' + cGUIDStr();

	if (mainApp) {
		DEBUG_LOG(("Application Error: another Application was created, terminating.."));
		exit(0);
	}
	mainApp = this;

	installEventFilter(new _DebugWaiter(this));

    QFontDatabase::addApplicationFont(qsl(":/gui/art/fonts/DejaVuSans.ttf"));
    QFontDatabase::addApplicationFont(qsl(":/gui/art/fonts/OpenSans-Regular.ttf"));
    QFontDatabase::addApplicationFont(qsl(":/gui/art/fonts/OpenSans-Bold.ttf"));
    QFontDatabase::addApplicationFont(qsl(":/gui/art/fonts/OpenSans-Semibold.ttf"));

	float64 dpi = 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);
	}

    if (devicePixelRatio() > 1) {
        cSetRetina(true);
        cSetRetinaFactor(devicePixelRatio());
        cSetIntRetinaFactor(int32(cRetinaFactor()));
    }

	if (!cLangFile().isEmpty()) {
		LangLoaderPlain loader(cLangFile());
		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()));
		}
	}

	Local::start();
	style::startManager();
	anim::startManager();
	historyInit();

	DEBUG_LOG(("Application Info: inited.."));

    window = new Window();

	psInstallEventFilter();

	connect(&socket, SIGNAL(connected()), this, SLOT(socketConnected()));
	connect(&socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
	connect(&socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(socketError(QLocalSocket::LocalSocketError)));
	connect(&socket, SIGNAL(bytesWritten(qint64)), this, SLOT(socketWritten(qint64)));
	connect(&socket, SIGNAL(readyRead()), this, SLOT(socketReading()));
	connect(&server, SIGNAL(newConnection()), this, SLOT(newInstanceConnected()));
	connect(this, SIGNAL(aboutToQuit()), this, SLOT(closeApplication()));
	connect(&updateCheckTimer, SIGNAL(timeout()), this, SLOT(startUpdateCheck()));
	connect(this, SIGNAL(updateFailed()), this, SLOT(onUpdateFailed()));
	connect(this, SIGNAL(updateReady()), this, SLOT(onUpdateReady()));
	connect(this, SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(onAppStateChanged(Qt::ApplicationState)));
	connect(&writeUserConfigTimer, SIGNAL(timeout()), this, SLOT(onWriteUserConfig()));
	writeUserConfigTimer.setSingleShot(true);

	connect(&killDownloadSessionsTimer, SIGNAL(timeout()), this, SLOT(killDownloadSessions()));

	if (cManyInstance()) {
		startApp();
	} else {
        DEBUG_LOG(("Application Info: connecting local socket to %1..").arg(serverName));
		socket.connectToServer(serverName);
	}
}

void Application::onAppUpdate(const MTPhelp_AppUpdate &response) {
    updateRequestId = 0;

	cSetLastUpdateCheck(unixtime());
	App::writeConfig();
	if (response.type() == mtpc_help_noAppUpdate) {
		startUpdateCheck();
	} else {
		updateThread = new QThread();
		connect(updateThread, SIGNAL(finished()), updateThread, SLOT(deleteLater()));
		updateDownloader = new PsUpdateDownloader(updateThread, response.c_help_appUpdate());
		updateThread->start();
	}
}

bool Application::onAppUpdateFail() {
	updateRequestId = 0;
	cSetLastUpdateCheck(unixtime());
	App::writeConfig();
	startUpdateCheck();
	return true;
}

void Application::updateGotCurrent() {
	if (!updateReply || updateThread) return;

	cSetLastUpdateCheck(unixtime());
	QRegularExpressionMatch m = QRegularExpression(qsl("^\\s*(\\d+)\\s*:\\s*([\\x21-\\x7f]+)\\s*$")).match(QString::fromUtf8(updateReply->readAll()));
	if (m.hasMatch()) {
		int32 currentVersion = m.captured(1).toInt();
		if (currentVersion > AppVersion) {
			updateThread = new QThread();
			connect(updateThread, SIGNAL(finished()), updateThread, SLOT(deleteLater()));
			updateDownloader = new PsUpdateDownloader(updateThread, m.captured(2));
			updateThread->start();
		}
	}
	if (updateReply) updateReply->deleteLater();
	updateReply = 0;
	if (!updateThread) {
		QDir updates(cWorkingDir() + "tupdates");
		if (updates.exists()) {
			QFileInfoList list = updates.entryInfoList(QDir::Files);
			for (QFileInfoList::iterator i = list.begin(), e = list.end(); i != e; ++i) {
                if (QRegularExpression("^(tupdate|tmacupd|tlinuxupd|tlinux32upd)\\d+$", QRegularExpression::CaseInsensitiveOption).match(i->fileName()).hasMatch()) {
					QFile(i->absoluteFilePath()).remove();
				}
			}
		}
		emit updateLatest();
	}
	startUpdateCheck(true);
	App::writeConfig();
}

void Application::updateFailedCurrent(QNetworkReply::NetworkError e) {
	LOG(("App Error: could not get current version (update check): %1").arg(e));
	if (updateReply) updateReply->deleteLater();
	updateReply = 0;

	emit updateFailed();
	startUpdateCheck(true);
}

void Application::onUpdateReady() {
	if (updateDownloader) {
		updateDownloader->deleteLater();
		updateDownloader = 0;
	}
	updateCheckTimer.stop();

	cSetLastUpdateCheck(unixtime());
	App::writeConfig();
}

void Application::onUpdateFailed() {
	if (updateDownloader) {
		updateDownloader->deleteLater();
		updateDownloader = 0;
		if (updateThread) updateThread->quit();
		updateThread = 0;
	}

	cSetLastUpdateCheck(unixtime());
	App::writeConfig();
}

void Application::regPhotoUpdate(const PeerId &peer, MsgId msgId) {
	photoUpdates.insert(msgId, peer);
}

void Application::clearPhotoUpdates() {
	photoUpdates.clear();
}

bool Application::isPhotoUpdating(const PeerId &peer) {
	for (QMap<MsgId, PeerId>::iterator i = photoUpdates.begin(), e = photoUpdates.end(); i != e; ++i) {
		if (i.value() == peer) {
			return true;
		}
	}
	return false;
}

void Application::cancelPhotoUpdate(const PeerId &peer) {
	for (QMap<MsgId, PeerId>::iterator i = photoUpdates.begin(), e = photoUpdates.end(); i != e;) {
		if (i.value() == peer) {
			i = photoUpdates.erase(i);
		} else {
			++i;
		}
	}
}

void Application::selfPhotoCleared(const MTPUserProfilePhoto &result) {
	if (!App::self()) return;
	App::self()->setPhoto(result);
	emit peerPhotoDone(App::self()->id);
}

void Application::chatPhotoCleared(PeerId peer, const MTPmessages_StatedMessage &result) {
	if (App::main()) {
		App::main()->sentFullDataReceived(0, result);
	}
	cancelPhotoUpdate(peer);
	emit peerPhotoDone(peer);
}

void Application::selfPhotoDone(const MTPphotos_Photo &result) {
	if (!App::self()) return;
	const MTPDphotos_photo &photo(result.c_photos_photo());
	App::feedPhoto(photo.vphoto);
	App::feedUsers(photo.vusers);
	cancelPhotoUpdate(App::self()->id);
	emit peerPhotoDone(App::self()->id);
}

void Application::chatPhotoDone(PeerId peer, const MTPmessages_StatedMessage &result) {
	if (App::main()) {
		App::main()->sentFullDataReceived(0, result);
	}
	cancelPhotoUpdate(peer);
	emit peerPhotoDone(peer);
}

bool Application::peerPhotoFail(PeerId peer, const RPCError &e) {
	LOG(("Application Error: update photo failed %1: %2").arg(e.type()).arg(e.description()));
	cancelPhotoUpdate(peer);
	emit peerPhotoFail(peer);
	return true;
}

void Application::peerClearPhoto(PeerId peer) {
	if (App::self() && App::self()->id == peer) {
		MTP::send(MTPphotos_UpdateProfilePhoto(MTP_inputPhotoEmpty(), MTP_inputPhotoCropAuto()), rpcDone(&Application::selfPhotoCleared), rpcFail(&Application::peerPhotoFail, peer));
	} else {
		MTP::send(MTPmessages_EditChatPhoto(MTP_int(int32(peer & 0xFFFFFFFF)), MTP_inputChatPhotoEmpty()), rpcDone(&Application::chatPhotoCleared, peer), rpcFail(&Application::peerPhotoFail, peer));
	}
}

void Application::writeUserConfigIn(uint64 ms) {
	if (!writeUserConfigTimer.isActive()) {
		writeUserConfigTimer.start(ms);
	}
}

void Application::killDownloadSessionsStart(int32 dc) {
	if (killDownloadSessionTimes.constFind(dc) == killDownloadSessionTimes.cend()) {
		killDownloadSessionTimes.insert(dc, getms() + MTPAckSendWaiting + MTPKillFileSessionTimeout);
	}
	if (!killDownloadSessionsTimer.isActive()) {
		killDownloadSessionsTimer.start(MTPAckSendWaiting + MTPKillFileSessionTimeout + 5);
	}
}

void Application::killDownloadSessionsStop(int32 dc) {
	killDownloadSessionTimes.remove(dc);
	if (killDownloadSessionTimes.isEmpty() && killDownloadSessionsTimer.isActive()) {
		killDownloadSessionsTimer.stop();
	}
}

void Application::checkLocalTime() {
	if (App::main()) App::main()->checkLastUpdate(checkms());
}

void Application::onWriteUserConfig() {
	App::writeUserConfig();
}

void Application::onAppStateChanged(Qt::ApplicationState state) {
	checkLocalTime();
}

void Application::killDownloadSessions() {
	uint64 ms = getms(), left = MTPAckSendWaiting + MTPKillFileSessionTimeout;
	for (QMap<int32, uint64>::iterator i = killDownloadSessionTimes.begin(); i != killDownloadSessionTimes.end(); ) {
		if (i.value() <= ms) {
			for (int j = 0; j < MTPDownloadSessionsCount; ++j) {
				MTP::stopSession(MTP::dld[j] + i.key());
			}
			i = killDownloadSessionTimes.erase(i);
		} else {
			if (i.value() - ms < left) {
				left = i.value() - ms;
			}
			++i;
		}
	}
	if (!killDownloadSessionTimes.isEmpty()) {
		killDownloadSessionsTimer.start(left);
	}
}

void Application::photoUpdated(MsgId msgId, const MTPInputFile &file) {
	if (!App::self()) return;

	QMap<MsgId, PeerId>::iterator i = photoUpdates.find(msgId);
	if (i != photoUpdates.end()) {
		PeerId peer = i.value();
		if (peer == App::self()->id) {
			MTP::send(MTPphotos_UploadProfilePhoto(file, MTP_string(""), MTP_inputGeoPointEmpty(), MTP_inputPhotoCrop(MTP_double(0), MTP_double(0), MTP_double(100))), rpcDone(&Application::selfPhotoDone), rpcFail(&Application::peerPhotoFail, peer));
		} else {
			History *hist = App::history(peer);
			hist->sendRequestId = MTP::send(MTPmessages_EditChatPhoto(MTP_int(peer & 0xFFFFFFFF), MTP_inputChatUploadedPhoto(file, MTP_inputPhotoCrop(MTP_double(0), MTP_double(0), MTP_double(100)))), rpcDone(&Application::chatPhotoDone, peer), rpcFail(&Application::peerPhotoFail, peer), 0, 0, hist->sendRequestId);
		}
	}
}

void Application::onEnableDebugMode() {
	if (!cDebug()) {
		logsInitDebug();
		cSetDebug(true);
		QFile f(cWorkingDir() + qsl("tdata/withdebug"));
		if (f.open(QIODevice::WriteOnly)) {
			f.write("1");
			f.close();
		}
	}
	App::wnd()->hideLayer();
}

Application::UpdatingState Application::updatingState() {
	if (!updateThread) return Application::UpdatingNone;
	if (!updateDownloader) return Application::UpdatingReady;
	return Application::UpdatingDownload;
}

int32 Application::updatingSize() {
	if (!updateDownloader) return 0;
	return updateDownloader->size();
}

int32 Application::updatingReady() {
	if (!updateDownloader) return 0;
	return updateDownloader->ready();
}

FileUploader *Application::uploader() {
	if (!::uploader) ::uploader = new FileUploader();
	return ::uploader;
}

void Application::uploadProfilePhoto(const QImage &tosend, const PeerId &peerId) {
	PreparedPhotoThumbs photoThumbs;
	QVector<MTPPhotoSize> photoSizes;

	QPixmap thumb = QPixmap::fromImage(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)));

	QPixmap medium = QPixmap::fromImage(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)));

	QPixmap full = QPixmap::fromImage(tosend);
	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 = MTP::nonce<PhotoId>();

	MTPPhoto photo(MTP_photo(MTP_long(id), MTP_long(0), MTP_int(MTP::authedId()), MTP_int(unixtime()), MTP_string(""), MTP_geoPointEmpty(), MTP_vector<MTPPhotoSize>(photoSizes)));

	QString file, filename;
	int32 filesize = 0;
	QByteArray data;

	ReadyLocalMedia ready(ToPreparePhoto, file, filename, filesize, data, id, id, peerId, photo, photoThumbs, MTP_documentEmpty(MTP_long(0)), jpeg, false);

	connect(App::uploader(), SIGNAL(photoReady(MsgId, const MTPInputFile &)), App::app(), SLOT(photoUpdated(MsgId, const MTPInputFile &)), Qt::UniqueConnection);

	MsgId newId = clientMsgId();
	App::app()->regPhotoUpdate(peerId, newId);
	App::uploader()->uploadMedia(newId, ready);
}

void Application::stopUpdate() {
	if (updateReply) {
		updateReply->abort();
		updateReply->deleteLater();
		updateReply = 0;
	}
	if (updateDownloader) {
		updateDownloader->deleteLater();
		updateDownloader = 0;
		if (updateThread) updateThread->quit();
		updateThread = 0;
	}
}

void Application::startUpdateCheck(bool forceWait) {
	updateCheckTimer.stop();
	if (updateRequestId || updateThread || updateReply || !cAutoUpdate()) return;
	
	int32 updateInSecs = cLastUpdateCheck() + 3600 + (rand() % 3600) - unixtime();
	bool sendRequest = (updateInSecs <= 0 || updateInSecs > 7200);
	if (!sendRequest && !forceWait) {
		QDir updates(cWorkingDir() + "tupdates");
		if (updates.exists()) {
			QFileInfoList list = updates.entryInfoList(QDir::Files);
			for (QFileInfoList::iterator i = list.begin(), e = list.end(); i != e; ++i) {
                if (QRegularExpression("^(tupdate|tmacupd|tlinuxupd|tlinux32upd)\\d+$", QRegularExpression::CaseInsensitiveOption).match(i->fileName()).hasMatch()) {
					sendRequest = true;
				}
			}
		}
	}
    if (cManyInstance() && !cDebug()) return; // only main instance is updating

	if (sendRequest) {
		QNetworkRequest checkVersion(cUpdateURL());
		if (updateReply) updateReply->deleteLater();

		App::setProxySettings(updateManager);
		updateReply = updateManager.get(checkVersion);
		connect(updateReply, SIGNAL(finished()), this, SLOT(updateGotCurrent()));
		connect(updateReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(updateFailedCurrent(QNetworkReply::NetworkError)));
//		updateRequestId = MTP::send(MTPhelp_GetAppUpdate(MTP_string(cApiDeviceModel()), MTP_string(cApiSystemVersion()), MTP_string(cApiAppVersion()), MTP_string(cApiLang())), rpcDone(&Application::onAppUpdate), rpcFail(&Application::onAppUpdateFail);
		emit updateChecking();
	} else {
		updateCheckTimer.start((updateInSecs + 5) * 1000);
	}
}

namespace {
	QChar _toHex(ushort v) {
		v = v & 0x000F;
		return QChar::fromLatin1((v >= 10) ? ('a' + (v - 10)) : ('0' + v));
	}
	ushort _fromHex(QChar c) {
		return ((c.unicode() >= uchar('a')) ? (c.unicode() - uchar('a') + 10) : (c.unicode() - uchar('0'))) & 0x000F;
	}

	QString _escapeTo7bit(const QString &str) {
		QString result;
		result.reserve(str.size() * 2);
		for (int i = 0, l = str.size(); i != l; ++i) {
			QChar ch(str.at(i));
			ushort uch(ch.unicode());
			if (uch < 32 || uch > 127 || uch == ushort(uchar('%'))) {
				result.append('%').append(_toHex(uch >> 12)).append(_toHex(uch >> 8)).append(_toHex(uch >> 4)).append(_toHex(uch));
			} else {
				result.append(ch);
			}
		}
		return result;
	}

	QString _escapeFrom7bit(const QString &str) {
		QString result;
		result.reserve(str.size());
		for (int i = 0, l = str.size(); i != l; ++i) {
			QChar ch(str.at(i));
			if (ch == QChar::fromLatin1('%') && i + 4 < l) {
				result.append(QChar(ushort((_fromHex(str.at(i + 1)) << 12) | (_fromHex(str.at(i + 2)) << 8) | (_fromHex(str.at(i + 3)) << 4) | _fromHex(str.at(i + 4)))));
				i += 4;
			} else {
				result.append(ch);
			}
		}
		return result;
	}
}

void Application::socketConnected() {
	DEBUG_LOG(("Application Info: socket connected, this is not the first application instance, sending show command.."));
	closing = true;
	QString commands;
	const QStringList &lst(cSendPaths());
	for (QStringList::const_iterator i = lst.cbegin(), e = lst.cend(); i != e; ++i) {
		commands += qsl("SEND:") + _escapeTo7bit(*i) + ';';
	}
	if (!cStartUrl().isEmpty()) {
		commands += qsl("OPEN:") + _escapeTo7bit(cStartUrl()) + ';';
	}
	commands += qsl("CMD:show;");
	DEBUG_LOG(("Application Info: writing commands %1").arg(commands));
	socket.write(commands.toLocal8Bit());
}

void Application::socketWritten(qint64/* bytes*/) {
	if (socket.state() != QLocalSocket::ConnectedState) {
		DEBUG_LOG(("Application Error: socket is not connected %1").arg(socket.state()));
		return;
	}
	if (socket.bytesToWrite()) {
		return;
	}
	DEBUG_LOG(("Application Info: show command written, waiting response.."));
}

void Application::socketReading() {
	if (socket.state() != QLocalSocket::ConnectedState) {
		DEBUG_LOG(("Application Error: socket is not connected %1").arg(socket.state()));
		return;
	}
	socketRead.append(socket.readAll());
	if (QRegularExpression("RES:(\\d+);").match(socketRead).hasMatch()) {
		uint64 pid = socketRead.mid(4, socketRead.length() - 5).toULongLong();
		psActivateProcess(pid);
		DEBUG_LOG(("Application Info: show command response received, pid = %1, activating and quiting..").arg(pid));
		return App::quit();
	}
}

void Application::socketError(QLocalSocket::LocalSocketError e) {
	if (closing) {
		DEBUG_LOG(("Application Error: could not write show command, error %1, quiting..").arg(e));
		return App::quit();
	}

	if (e == QLocalSocket::ServerNotFoundError) {
		DEBUG_LOG(("Application Info: this is the only instance of Telegram, starting server and app.."));
	} else {
		DEBUG_LOG(("Application Info: socket connect error %1, starting server and app..").arg(e));
	}
	socket.close();

	psCheckLocalSocket(serverName);
  
	if (!server.listen(serverName)) {
		DEBUG_LOG(("Application Error: failed to start listening to %1 server, error %2").arg(serverName).arg(int(server.serverError())));
		return App::quit();
	}

	if (!cNoStartUpdate() && psCheckReadyUpdate()) {
		cSetRestartingUpdate(true);
		DEBUG_LOG(("Application Info: installing update instead of starting app.."));
		return App::quit();
	}

	startApp();
}

void Application::startApp() {
	DEBUG_LOG(("Application Info: starting app.."));

	Local::ReadMapState state = Local::readMap(QByteArray());

	DEBUG_LOG(("Application Info: local map read.."));
	App::readUserConfig();
	if (!Local::oldKey().created()) {
		Local::createOldKey();
		cSetNeedConfigResave(true);
	}
	if (cNeedConfigResave()) {
		App::writeConfig();
		App::writeUserConfig();
		cSetNeedConfigResave(false);
	}
	DEBUG_LOG(("Application Info: user config read.."));

	window->createWinId();
	window->init();

	DEBUG_LOG(("Application Info: window created.."));
	readSupportTemplates();

	MTP::start();
	
	MTP::setStateChangedHandler(mtpStateChanged);
	MTP::setSessionResetHandler(mtpSessionReset);

	DEBUG_LOG(("Application Info: MTP started.."));

	initImageLinkManager();
	App::initMedia();

	DEBUG_LOG(("Application Info: showing."));

	if (MTP::authedId()) {
		window->setupMain(false);
	} else {
		window->setupIntro(false);
	}

	window->firstShow();

	if (cStartToSettings()) {
		window->showSettings();
	}

	QNetworkProxyFactory::setUseSystemConfiguration(true);
	if (Local::oldMapVersion() < AppVersion) {
		psRegisterCustomScheme();
		if (Local::oldMapVersion() && Local::oldMapVersion() <= FeaturesNotifyVersionFrom && AppVersion == FeaturesNotifyVersion) {
			QString versionFeatures(QString::fromUtf8(FeaturesNotify));
			if (!versionFeatures.isEmpty()) {
				window->serviceNotification(versionFeatures);
			}
		}
	}
}

void Application::socketDisconnected() {
	if (closing) {
		DEBUG_LOG(("Application Error: socket disconnected before command response received, quiting.."));
		return App::quit();
	}
}

void Application::newInstanceConnected() {
	DEBUG_LOG(("Application Info: new local socket connected"));
	for (QLocalSocket *client = server.nextPendingConnection(); client; client = server.nextPendingConnection()) {
		clients.push_back(ClientSocket(client, QByteArray()));
		connect(client, SIGNAL(readyRead()), this, SLOT(readClients()));
		connect(client, SIGNAL(disconnected()), this, SLOT(removeClients()));
	}
}

void Application::readClients() {
	QString startUrl;
	QStringList toSend;
	for (ClientSockets::iterator i = clients.begin(), e = clients.end(); i != e; ++i) {
		i->second.append(i->first->readAll());
		if (i->second.size()) {
			QString cmds(QString::fromLocal8Bit(i->second));
			int32 from = 0, l = cmds.length();
			for (int32 to = cmds.indexOf(QChar(';'), from); to >= from; to = (from < l) ? cmds.indexOf(QChar(';'), from) : -1) {
				QStringRef cmd(&cmds, from, to - from);
				if (cmd.startsWith(qsl("CMD:"))) {
					execExternal(cmds.mid(from + 4, to - from - 4));
					QByteArray response(qsl("RES:%1;").arg(QCoreApplication::applicationPid()).toUtf8());
					i->first->write(response.data(), response.size());
				} else if (cmd.startsWith(qsl("SEND:"))) {
					if (cSendPaths().isEmpty()) {
						toSend.append(_escapeFrom7bit(cmds.mid(from + 5, to - from - 5)));
					}
				} else if (cmd.startsWith(qsl("OPEN:"))) {
					if (cStartUrl().isEmpty()) {
						startUrl = _escapeFrom7bit(cmds.mid(from + 5, to - from - 5));
					}
				} else {
					LOG(("Application Error: unknown command %1 passed in local socket").arg(QString(cmd.constData(), cmd.length())));
				}
				from = to + 1;
			}
			if (from > 0) {
				i->second = i->second.mid(from);
			}
		}
	}
	if (!toSend.isEmpty()) {
		QStringList paths(cSendPaths());
		paths.append(toSend);
		cSetSendPaths(paths);
	}
	if (!cSendPaths().isEmpty()) {
		if (App::wnd()) {
			App::wnd()->sendPaths();
		}
	}
	if (!startUrl.isEmpty()) {
		cSetStartUrl(startUrl);
	}
	if (!cStartUrl().isEmpty() && App::main() && App::self()) {
		App::main()->openLocalUrl(cStartUrl());
		cSetStartUrl(QString());
	}
}

void Application::removeClients() {
	DEBUG_LOG(("Application Info: remove clients slot called, clients %1").arg(clients.size()));
	for (ClientSockets::iterator i = clients.begin(), e = clients.end(); i != e;) {
		if (i->first->state() != QLocalSocket::ConnectedState) {
			DEBUG_LOG(("Application Info: removing client"));
			i = clients.erase(i);
			e = clients.end();
		} else {
			++i;
		}
	}
}

void Application::execExternal(const QString &cmd) {
	DEBUG_LOG(("Application Info: executing external command '%1'").arg(cmd));
	if (cmd == "show") {
		window->activate();
	}
}

void Application::closeApplication() {
	// close server
	server.close();
	for (ClientSockets::iterator i = clients.begin(), e = clients.end(); i != e; ++i) {
		disconnect(i->first, SIGNAL(disconnected()), this, SLOT(removeClients()));
		i->first->close();
	}
	clients.clear();

	MTP::stop();
}

Application::~Application() {
	App::setQuiting();
	window->setParent(0);

	anim::stopManager();

	socket.close();
	closeApplication();
	App::deinitMedia();
	deinitImageLinkManager();
	mainApp = 0;
	delete updateReply;
	delete ::uploader;
	updateReply = 0;
	if (updateDownloader) updateDownloader->deleteLater();
	updateDownloader = 0;
	if (updateThread) updateThread->quit();
	updateThread = 0;

	delete window;

	style::stopManager();
	Local::stop();
}

Application *Application::app() {
	return mainApp;
}

Window *Application::wnd() {
	return mainApp ? mainApp->window : 0;
}

QString Application::lang() {
	if (!lng.length()) {
		lng = psCurrentLanguage();
	}
	if (!lng.length()) {
		lng = "en";
	}
	return lng;
}

MainWidget *Application::main() {
	return mainApp ? mainApp->window->mainWidget() : 0;
}
