2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
an unofficial desktop 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://tdesktop.com
|
|
|
|
*/
|
|
|
|
#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"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
Application *mainApp = 0;
|
|
|
|
FileUploader *uploader = 0;
|
|
|
|
QString lng;
|
|
|
|
|
|
|
|
void mtpStateChanged(int32 dc, int32 state) {
|
|
|
|
if (App::wnd()) {
|
|
|
|
App::wnd()->mtpStateChanged(dc, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-01 18:49:43 +00:00
|
|
|
void mtpSessionReset(int32 dc) {
|
|
|
|
if (App::main() && dc == MTP::maindc()) {
|
|
|
|
App::main()->getDifference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-09-26 23:48:19 +00:00
|
|
|
|
|
|
|
if (cPlatform() == dbipMac && ev->key() == Qt::Key_W && (ev->modifiers() & (Qt::MetaModifier | Qt::ControlModifier))) {
|
|
|
|
if (cWorkMode() == dbiwmTrayOnly || cWorkMode() == dbiwmWindowAndTray) {
|
|
|
|
App::wnd()->minimizeToTray();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-06-14 19:32:11 +00:00
|
|
|
Application::Application(int &argc, char **argv) : PsApplication(argc, argv),
|
2014-05-30 08:53:19 +00:00
|
|
|
serverName(psServerPrefix() + cGUIDStr()), closing(false),
|
2014-06-14 19:32:11 +00:00
|
|
|
updateRequestId(0), updateReply(0), updateThread(0), updateDownloader(0) {
|
2014-07-18 10:37:34 +00:00
|
|
|
|
|
|
|
QByteArray d(QDir((cPlatform() == dbipWindows ? cExeDir() : cWorkingDir()).toLower()).absolutePath().toUtf8());
|
|
|
|
char h[33] = { 0 };
|
|
|
|
hashMd5Hex(d.constData(), d.size(), h);
|
|
|
|
serverName = psServerPrefix() + h + '-' + cGUIDStr();
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
if (mainApp) {
|
|
|
|
DEBUG_LOG(("Application Error: another Application was created, terminating.."));
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
mainApp = this;
|
|
|
|
|
|
|
|
installEventFilter(new _DebugWaiter(this));
|
|
|
|
|
2014-07-14 05:16:21 +00:00
|
|
|
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"));
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-06-15 12:31:03 +00:00
|
|
|
if (devicePixelRatio() > 1) {
|
|
|
|
cSetRetina(true);
|
|
|
|
cSetRetinaFactor(devicePixelRatio());
|
|
|
|
cSetIntRetinaFactor(int32(cRetinaFactor()));
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
style::startManager();
|
|
|
|
anim::startManager();
|
|
|
|
historyInit();
|
|
|
|
|
2014-07-14 05:16:21 +00:00
|
|
|
window = new Window();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2014-07-09 05:54:12 +00:00
|
|
|
psInstallEventFilter();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
updateCheckTimer.setSingleShot(true);
|
|
|
|
|
|
|
|
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(&writeUserConfigTimer, SIGNAL(timeout()), this, SLOT(onWriteUserConfig()));
|
|
|
|
writeUserConfigTimer.setSingleShot(true);
|
|
|
|
|
|
|
|
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) {
|
2014-07-10 08:53:23 +00:00
|
|
|
updateRequestId = 0;
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
cSetLastUpdateCheck(unixtime());
|
|
|
|
App::writeConfig();
|
|
|
|
if (response.type() == mtpc_help_noAppUpdate) {
|
|
|
|
startUpdateCheck();
|
|
|
|
} else {
|
|
|
|
updateThread = new QThread();
|
|
|
|
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();
|
|
|
|
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) {
|
2014-07-17 09:42:06 +00:00
|
|
|
if (QRegularExpression("^(tupdate|tmacupd|tlinuxupd|tlinux32upd)\\d+$", QRegularExpression::CaseInsensitiveOption).match(i->fileName()).hasMatch()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
QFile(i->absoluteFilePath()).remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit updateLatest();
|
|
|
|
}
|
|
|
|
startUpdateCheck(true);
|
2014-06-25 07:25:55 +00:00
|
|
|
App::writeConfig();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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->deleteLater();
|
|
|
|
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::onWriteUserConfig() {
|
|
|
|
App::writeUserConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::onEnableDebugMode() {
|
|
|
|
if (!cDebug()) {
|
|
|
|
logsInitDebug();
|
|
|
|
cSetDebug(true);
|
|
|
|
}
|
|
|
|
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)));
|
|
|
|
|
2014-08-15 11:19:32 +00:00
|
|
|
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)));
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
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->deleteLater();
|
|
|
|
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) {
|
2014-07-17 09:42:06 +00:00
|
|
|
if (QRegularExpression("^(tupdate|tmacupd|tlinuxupd|tlinux32upd)\\d+$", QRegularExpression::CaseInsensitiveOption).match(i->fileName()).hasMatch()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
sendRequest = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-14 05:16:21 +00:00
|
|
|
if (cManyInstance() && !cDebug()) return; // only main instance is updating
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
if (sendRequest) {
|
2014-06-25 07:25:55 +00:00
|
|
|
QNetworkRequest checkVersion(cUpdateURL());
|
2014-05-30 08:53:19 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 10:37:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void Application::socketConnected() {
|
|
|
|
DEBUG_LOG(("Application Info: socket connected, this is not the first application instance, sending show command.."));
|
|
|
|
closing = true;
|
2014-07-18 10:37:34 +00:00
|
|
|
QString commands;
|
|
|
|
const QStringList &lst(cSendPaths());
|
|
|
|
for (QStringList::const_iterator i = lst.cbegin(), e = lst.cend(); i != e; ++i) {
|
|
|
|
commands += qsl("SEND:") + _escapeTo7bit(*i) + ';';
|
|
|
|
}
|
|
|
|
commands += qsl("CMD:show;");
|
|
|
|
DEBUG_LOG(("Application Info: writing commands %1").arg(commands));
|
|
|
|
socket.write(commands.toLocal8Bit());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2014-07-18 10:37:34 +00:00
|
|
|
DEBUG_LOG(("Application Error: failed to start listening to %1 server, error %2").arg(serverName).arg(int(server.serverError())));
|
2014-05-30 08:53:19 +00:00
|
|
|
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() {
|
|
|
|
App::readUserConfig();
|
|
|
|
if (!MTP::localKey().created()) {
|
|
|
|
MTP::createLocalKey(QByteArray());
|
|
|
|
cSetNeedConfigResave(true);
|
|
|
|
}
|
|
|
|
if (cNeedConfigResave()) {
|
|
|
|
App::writeConfig();
|
|
|
|
App::writeUserConfig();
|
|
|
|
cSetNeedConfigResave(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
window->createWinId();
|
|
|
|
window->init();
|
|
|
|
|
|
|
|
readSupportTemplates();
|
|
|
|
|
|
|
|
MTP::setLayer(mtpLayerMax);
|
|
|
|
MTP::start();
|
|
|
|
|
|
|
|
MTP::setStateChangedHandler(mtpStateChanged);
|
2014-08-01 18:49:43 +00:00
|
|
|
MTP::setSessionResetHandler(mtpSessionReset);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
App::initMedia();
|
|
|
|
|
|
|
|
if (MTP::authedId()) {
|
|
|
|
window->setupMain(false);
|
|
|
|
} else {
|
|
|
|
window->setupIntro(false);
|
|
|
|
}
|
|
|
|
|
2014-09-29 02:47:30 +00:00
|
|
|
window->firstShow();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
if (cStartToSettings()) {
|
|
|
|
window->showSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2014-07-18 10:37:34 +00:00
|
|
|
QStringList toSend;
|
2014-05-30 08:53:19 +00:00
|
|
|
for (ClientSockets::iterator i = clients.begin(), e = clients.end(); i != e; ++i) {
|
|
|
|
i->second.append(i->first->readAll());
|
|
|
|
if (i->second.size()) {
|
2014-07-18 10:37:34 +00:00
|
|
|
QString cmds(QString::fromLocal8Bit(i->second));
|
2014-05-30 08:53:19 +00:00
|
|
|
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);
|
2014-08-22 09:53:53 +00:00
|
|
|
if (cmd.startsWith(qsl("CMD:"))) {
|
2014-05-30 08:53:19 +00:00
|
|
|
execExternal(cmds.mid(from + 4, to - from - 4));
|
2014-08-22 09:53:53 +00:00
|
|
|
QByteArray response(qsl("RES:%1;").arg(QCoreApplication::applicationPid()).toUtf8());
|
2014-05-30 08:53:19 +00:00
|
|
|
i->first->write(response.data(), response.size());
|
2014-08-22 09:53:53 +00:00
|
|
|
} else if (cmd.startsWith(qsl("SEND:"))) {
|
2014-07-18 10:37:34 +00:00
|
|
|
if (cSendPaths().isEmpty()) {
|
|
|
|
toSend.append(_escapeFrom7bit(cmds.mid(from + 5, to - from - 5)));
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 10:37:34 +00:00
|
|
|
if (!toSend.isEmpty()) {
|
|
|
|
QStringList paths(cSendPaths());
|
|
|
|
paths.append(toSend);
|
|
|
|
cSetSendPaths(paths);
|
|
|
|
}
|
|
|
|
if (!cSendPaths().isEmpty()) {
|
|
|
|
if (App::wnd()) {
|
|
|
|
App::wnd()->sendPaths();
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
mainApp = 0;
|
|
|
|
delete updateReply;
|
|
|
|
delete ::uploader;
|
|
|
|
updateReply = 0;
|
|
|
|
delete updateDownloader;
|
|
|
|
updateDownloader = 0;
|
|
|
|
delete updateThread;
|
|
|
|
updateThread = 0;
|
|
|
|
|
|
|
|
delete window;
|
|
|
|
|
|
|
|
style::stopManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|