-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
312 lines (273 loc) · 8.51 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
screen = QGuiApplication::screens().first();
connect(screen, SIGNAL(virtualGeometryChanged(QRect)), this, SLOT(getScreen()));
connect(ui->sipUser, SIGNAL(editingFinished()), QGuiApplication::inputMethod(), SLOT(hide()));
connect(ui->sipPass, SIGNAL(editingFinished()), QGuiApplication::inputMethod(), SLOT(hide()));
connect(ui->sipServer, SIGNAL(editingFinished()), QGuiApplication::inputMethod(), SLOT(hide()));
connect(ui->sipPort, SIGNAL(editingFinished()), QGuiApplication::inputMethod(), SLOT(hide()));
getScreen();
getSettings();
if(ui->sipUser->text()!="" && ui->sipPass->text()!="" && ui->sipServer->text()!="" && ui->sipPort->text()!="")
registerSip();
}
void MainWindow::registerSip()
{
metaVoIP = new MetaVoIP(ui->sipProtoBox->currentText(), ui->sipPortText->text().toInt());
sipUser = ui->sipUser->text().simplified();
sipServer = ui->sipServer->text().simplified();
sipPass = ui->sipPass->text().simplified();
sipState = "init";
connect(metaVoIP, SIGNAL(callStateChanged(int, int, int, int, QString)), this, SLOT(on_callState_changed(int, int, int, int, QString)));
if(metaVoIP != nullptr && metaVoIP->isLoaded())
metaVoIP->createAccount("sip:"+sipUser+"@"+sipServer, "sip:"+sipServer, sipUser, sipPass);
else
error("Loading SIP library failed!");
}
void MainWindow::on_b1_clicked()
{
ui->number->setText(ui->number->text()+"1");
if(sipState == "calling")
metaVoIP->sendDtmf("1");
}
void MainWindow::on_b2_clicked()
{
ui->number->setText(ui->number->text()+"2");
if(sipState == "calling")
metaVoIP->sendDtmf("2");
}
void MainWindow::on_b3_clicked()
{
ui->number->setText(ui->number->text()+"3");
if(sipState == "calling")
metaVoIP->sendDtmf("3");
}
void MainWindow::on_b4_clicked()
{
ui->number->setText(ui->number->text()+"4");
if(sipState == "calling")
metaVoIP->sendDtmf("4");
}
void MainWindow::on_b5_clicked()
{
ui->number->setText(ui->number->text()+"5");
if(sipState == "calling")
metaVoIP->sendDtmf("5");
}
void MainWindow::on_b6_clicked()
{
ui->number->setText(ui->number->text()+"6");
if(sipState == "calling")
metaVoIP->sendDtmf("6");
}
void MainWindow::on_b7_clicked()
{
ui->number->setText(ui->number->text()+"7");
if(sipState == "calling")
metaVoIP->sendDtmf("7");
}
void MainWindow::on_b8_clicked()
{
ui->number->setText(ui->number->text()+"8");
if(sipState == "calling")
metaVoIP->sendDtmf("8");
}
void MainWindow::on_b9_clicked()
{
ui->number->setText(ui->number->text()+"9");
if(sipState == "calling")
metaVoIP->sendDtmf("9");
}
void MainWindow::on_b0_clicked()
{
ui->number->setText(ui->number->text()+"0");
if(sipState == "calling")
metaVoIP->sendDtmf("0");
}
void MainWindow::on_hangButton_clicked()
{
ui->number->clear();
metaVoIP->hangupCall(callId);
sipState = "";
ui->info->clear();
}
void MainWindow::on_phoneButton_clicked()
{
if(sipState=="calling")
{
metaVoIP->holdCall(callId);
sipState = "calling";
}
else if(sipState=="localRing")
{
metaVoIP->acceptCall(callId);
sipState = "calling";
ringtone.stop();
}
else if(sipState!="localRing" && ui->number->text()!="")
{
metaVoIP->makeCall("sip:"+ui->number->text()+"@"+sipServer);
ui->info->setText("Calling "+ui->number->text()+"...");
ui->number->clear();
}
}
void MainWindow::on_callState_changed(int role, int callId, int state, int status, QString remoteUri)
{
if(state == PJSIP_INV_STATE_EARLY && status == 180)
{
if(role == 1)
{
int j = 0;
QList<int> namePos;
QList<int> numPos;
while ((j = remoteUri.indexOf("\"", j)) != -1) {
namePos.append(j);
++j;
}
numPos.append(remoteUri.indexOf("sip:"));
numPos.append(remoteUri.indexOf("@"));
QString name = remoteUri.mid(namePos[0]+1, namePos[1]-1);
QString num = remoteUri.mid(numPos[0]+4, numPos[1]-(numPos[0]+4));
QString caller = name;
if(name.size()<=1)
caller = num;
ui->info->setText(caller + " is calling...");
sipState = "localRing";
ringtone.play();
}
else
{
sipState = "remoteRing";
outgoingRing.play();
}
this->callId = callId;
}
else if(state == PJSIP_INV_STATE_CONFIRMED && status == 200)
{
sipState = "calling";
ringtone.stop();
outgoingRing.stop();
}
else if(state == PJSIP_INV_STATE_DISCONNECTED)
{
ui->info->clear();
sipState = "";
ringtone.stop();
outgoingRing.stop();
}
}
void MainWindow::getScreen()
{
// this->geo
QRect rec = QGuiApplication::primaryScreen()->geometry();
// QRect rec = QApplication::desktop()->screenGeometry();
screenY = rec.height();
screenX = rec.width();
ui->hangButton->setIcon(QPixmap(":/images/phone_hang.png"));
ui->phoneButton->setIcon(QPixmap(":/images/phone.png"));
ui->phoneButton->setIconSize(QSize(screenX/15,screenY/15));
ui->hangButton->setIconSize(QSize(screenX/15,screenY/15));
QFont font = ui->b0->font();
font.setPointSize(20);
ui->b0->setFont(font);
ui->b1->setFont(font);
ui->b2->setFont(font);
ui->b3->setFont(font);
ui->b4->setFont(font);
ui->b5->setFont(font);
ui->b6->setFont(font);
ui->b7->setFont(font);
ui->b8->setFont(font);
ui->b9->setFont(font);
ui->bRaute->setFont(font);
ui->bStar->setFont(font);
ui->number->setFont(font);
ui->info->setFont(font);
ui->sipUserText->setFont(font);
ui->sipUser->setFont(font);
ui->sipPassText->setFont(font);
ui->sipPass->setFont(font);
ui->sipServerText->setFont(font);
ui->sipServer->setFont(font);
ui->saveButton->setFont(font);
ui->sipProtoText->setFont(font);
ui->sipProtoBox->setFont(font);
ui->sipPortText->setFont(font);
ui->sipPort->setFont(font);
}
void MainWindow::getSettings()
{
QFile settings("settings.dat");
if(settings.exists())
{
if(settings.open(QIODevice::ReadOnly))
{
ui->sipServer->setText(settings.readLine().simplified());
ui->sipUser->setText(settings.readLine().simplified());
ui->sipPass->setText(settings.readLine().simplified());
ui->sipProtoBox->setCurrentIndex(settings.readLine().toInt());
ui->sipPort->setText(settings.readLine().simplified());
settings.close();
}
else
error("No read permission available");
}
outgoingRing.setSource(QUrl::fromLocalFile(":/sounds/ring.wav"));
outgoingRing.setLoopCount(QSoundEffect::Infinite);
ringtone.setSource(QUrl::fromLocalFile(":/sounds/ring.wav"));
ringtone.setLoopCount(QSoundEffect::Infinite);
}
void MainWindow::on_saveButton_clicked()
{
if(ui->sipUser->text()!="" && ui->sipPass->text()!="" && ui->sipServer->text()!="" && ui->sipPort->text()!="")
{
QFile settings("settings.dat");
settings.remove();
if (settings.open(QIODevice::WriteOnly))
{
QTextStream out(&settings);
out << ui->sipServer->text().simplified();
out << "\n";
out << ui->sipUser->text().simplified();
out << "\n";
out << ui->sipPass->text().simplified();
out << "\n";
out << QString::number(ui->sipProtoBox->currentIndex());
out << "\n";
out << ui->sipPort->text().simplified();
settings.close();
registerSip();
}
else
error("No write permission available");
}
else
error("Please insert necessary values");
}
void MainWindow::on_actionAbout_Qt_triggered()
{
QMessageBox::aboutQt(this);
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::about(this, "MetaVoIP 1.0.0", "Copyright:\nMetaSEC 2015\n\nDeveloper:\n- Dominik Fehr <[email protected]>");
}
void MainWindow::error(QString error)
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(error);
msgBox.setWindowTitle("Error");
msgBox.exec();
}
MainWindow::~MainWindow()
{
delete ui;
}