FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tdesktop/Telegram/SourceFiles/application.cpp at master · NandoKstroNet/tdesktop · GitHub
NandoKstroNet
/
tdesktop
Public
forked from
telegramdesktop/tdesktop
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
application.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
731 lines (620 loc) · 22.5 KB
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
application.cpp
Copy path
File metadata and controls
731 lines (620 loc) · 22.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
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);
}
}
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
;
}
}
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
) {
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
()));
}
}
style::startManager
();
anim::startManager
();
historyInit
();
window =
new
Window
();
psInstallEventFilter
();
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) {
updateRequestId =
0
;
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) {
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->
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
)));
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);
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) {
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) +
'
;
'
;
}
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
() {
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);
MTP::setSessionResetHandler
(mtpSessionReset);
App::initMedia
();
if
(
MTP::authedId
()) {
window->
setupMain
(
false
);
}
else
{
window->
setupIntro
(
false
);
}
window->
psFirstShow
();
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
() {
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
{
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
();
}
}
}
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
;
}
Back
|
FazBrowse Home
|
New Git URL