forked from kutegram/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
211 lines (182 loc) · 6.23 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/QCoreApplication>
#include <QInputDialog>
#include "logintypedialog.h"
#include "qrlogindialog.h"
#include "dialogitemdelegate.h"
#include "dialogitemmodel.h"
#include "historywindow.h"
#include "main.h"
#include <QMessageBox>
#include <QtEndian>
#ifndef QT_NO_DEBUG_OUTPUT
#include <QtDebug>
#endif
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
client(new TelegramClient),
ui(new Ui::MainWindow),
phoneNumber(),
dialogModel(0),
flickcharm(),
backAction(this)
{
ui->setupUi(this);
connect(&backAction, SIGNAL(triggered()), this, SLOT(backAction_triggered()));
backAction.setText(QApplication::translate("MainWindow", "Quit", 0, QApplication::UnicodeUTF8));
backAction.setIcon(QIcon(":/icons/back.svg"));
backAction.setSoftKeyRole(QAction::NegativeSoftKey);
addAction(&backAction);
flickcharm.activateOn(ui->dialogView);
ui->tabBar->setDrawBase(false);
ui->dialogView->setModel(dialogModel = new DialogItemModel(client, ui->dialogView));
ui->dialogView->setItemDelegate(new DialogItemDelegate(ui->dialogView));
connect(client, SIGNAL(stateChanged(State)), this, SLOT(client_stateChanged(State)));
connect(client, SIGNAL(gotLoginToken(qint64,qint32,QString)), this, SLOT(client_gotLoginToken(qint64,qint32,QString)));
connect(client, SIGNAL(gotSentCode(qint64,QString)), this, SLOT(client_gotSentCode(qint64,QString)));
connect(client, SIGNAL(gotSocketError(QAbstractSocket::SocketError)), this, SLOT(client_gotSocketError(QAbstractSocket::SocketError)));
connect(client, SIGNAL(gotMTError(qint32)), this, SLOT(client_gotMTError(qint32)));
connect(client, SIGNAL(gotDHError(bool)), this, SLOT(client_gotDHError(bool)));
connect(client, SIGNAL(gotMessageError(qint64,qint32)), this, SLOT(client_gotMessageError(qint64,qint32)));
connect(client, SIGNAL(gotRPCError(qint64,qint32,QString,bool)), this, SLOT(client_gotRPCError(qint64,qint32,QString,bool)));
if (client->isLoggedIn()) client->start();
}
MainWindow::~MainWindow()
{
client->stop();
client->deleteLater();
delete ui;
}
void MainWindow::showTypeDialog()
{
LoginTypeDialog dialog(this);
switch (dialog.exec()) {
case 2:
{
//QR code
client->exportLoginToken();
break;
}
case 3:
{
//Phone number
QInputDialog phoneInput(this);
if (phoneInput.exec()) {
phoneNumber = phoneInput.textValue();
client->sendCode(phoneNumber);
}
break;
}
default:
{
break;
}
}
}
void MainWindow::loginAction_triggered()
{
client->start();
}
void MainWindow::logoutAction_triggered()
{
client->reset();
}
void MainWindow::exitAction_triggered()
{
QApplication::quit();
}
void MainWindow::backAction_triggered()
{
close();
}
void MainWindow::aboutAction_triggered()
{
QMessageBox::about(
this,
QApplication::translate("MainWindow", "Kutegram", 0, QApplication::UnicodeUTF8),
QApplication::translate("MainWindow", "Kutegram by curoviyxru\nAn unofficial Qt-based client for Telegram messenger.\nProject's website: http://kg.curoviyx.ru\nTelegram channel: https://t.me/kutegram\nTelegram chat: https://t.me/kutegramchat", 0, QApplication::UnicodeUTF8)
);
}
void MainWindow::aboutQt_triggered()
{
QMessageBox::aboutQt(this);
}
void MainWindow::client_gotSocketError(QAbstractSocket::SocketError error)
{
QMessageBox::critical(
this,
QApplication::translate("MainWindow", "Error", 0, QApplication::UnicodeUTF8),
QApplication::translate("MainWindow", "Got socket error: %1", 0, QApplication::UnicodeUTF8)
.arg(QString::number(error))
);
}
void MainWindow::client_gotMTError(qint32 error_code)
{
QMessageBox::critical(
this,
QApplication::translate("MainWindow", "Error", 0, QApplication::UnicodeUTF8),
QApplication::translate("MainWindow", "Got MT error: %1", 0, QApplication::UnicodeUTF8)
.arg(QString::number(error_code))
);
}
void MainWindow::client_gotDHError(bool fail)
{
QMessageBox::critical(
this,
QApplication::translate("MainWindow", "Error", 0, QApplication::UnicodeUTF8),
QApplication::translate("MainWindow", "Got DH error: %1", 0, QApplication::UnicodeUTF8)
.arg(QString::number(fail))
);
}
void MainWindow::client_gotMessageError(qint64 mtm, qint32 error_code)
{
//TODO: do we need message error?
}
void MainWindow::client_gotRPCError(qint64 mtm, qint32 error_code, QString error_message, bool handled)
{
if (handled) return;
QMessageBox::critical(
this,
QApplication::translate("MainWindow", "Error", 0, QApplication::UnicodeUTF8),
QApplication::translate("MainWindow", "Got RPC error: %1, %2, %3", 0, QApplication::UnicodeUTF8)
.arg(QString::number(client->messageConstructor(mtm))).arg(QString::number(error_code)).arg(error_message)
);
}
void MainWindow::client_stateChanged(State state)
{
//setWindowTitle("Kutegram (ls: " + QString::number(state) + ")");
switch (state) {
case INITED:
{
if (!client->isLoggedIn()) showTypeDialog();
break;
}
case LOGGED_IN:
{
if (dialogModel->canFetchMore(QModelIndex())) dialogModel->fetchMore(QModelIndex());
break;
}
}
}
void MainWindow::client_gotLoginToken(qint64 mtm, qint32 expired, QString tokenUrl)
{
#ifndef QT_NO_DEBUG_OUTPUT
qDebug() << "Got qr token url:" << tokenUrl;
#endif
QRLoginDialog dialog(tokenUrl, this);
dialog.exec();
}
void MainWindow::client_gotSentCode(qint64 mtm, QString phone_code_hash)
{
QInputDialog phoneInput(this);
if (phoneInput.exec()) {
client->signIn(phoneNumber, phone_code_hash, phoneInput.textValue());
}
}
void MainWindow::dialogView_activated(QModelIndex index)
{
HistoryWindow* window = new HistoryWindow(client, dialogModel->getInputPeer(index.row()), this);
setOrientation(window, ScreenOrientationAuto);
showExpanded(window);
}