-
Notifications
You must be signed in to change notification settings - Fork 18
/
LocalServer.cpp
97 lines (79 loc) · 2.64 KB
/
LocalServer.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
#include "LocalServer.h"
#include "Global.h"
#include "ContactManager.h"
#include "OutCALL.h"
#include "AsteriskManager.h"
#include <QTcpSocket>
#include <QLocalSocket>
#include <QStringList>
#include <QFile>
#include <QMessageBox>
LocalServer::LocalServer(QObject *parent) :
QLocalServer(parent)
{
removeServer(LOCAL_SERVER_NAME);
if (!listen(LOCAL_SERVER_NAME))
global::log(QString("Failed listen() on local server. %1").arg(errorString()), LOG_ERROR);
connect(this, SIGNAL(newConnection()), this, SLOT(newSocketConnection()));
m_box = new QMessageBox;
m_box->setWindowIcon(QIcon(":images/outcall-logo.png"));
}
LocalServer::~LocalServer()
{
close();
removeServer(LOCAL_SERVER_NAME);
delete m_box;
}
void LocalServer::newSocketConnection()
{
QLocalSocket *s = nextPendingConnection();
if (!s)
return;
s->setProperty("incoming_data", "");
s->write("OK");
connect(s, SIGNAL(disconnected()), s, SLOT(deleteLater()));
connect(s, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
void LocalServer::readyRead()
{
QLocalSocket *s = (QLocalSocket*)sender();
QString data = s->property("incoming_data").toString() + s->readAll();
global::log("LOCAL SERVER", LOG_INFORMATION);
if (data.endsWith("\n"))
{
s->disconnectFromServer();
QStringList cmd = data.split(" ");
if (cmd.count()==3)
{
if (cmd[0]=="outlook_call")
{
QString contactName = QString(QByteArray::fromBase64(cmd[1].toLatin1()));
QStringList numbers = QString(QByteArray::fromBase64(cmd[2].toLatin1())).split(" | ");
numbers.removeAll("");
if (!g_pAsteriskManager->isSignedIn())
{
m_box->setWindowTitle(tr(APP_NAME));
m_box->setText(tr("%1 is not logged in.\nPlease log in and try calling the contact again.").arg(APP_NAME));
m_box->setIcon(QMessageBox::Warning);
m_box->activateWindow();
m_box->show();
return;
}
QHash<QString, QString> numbers_hash;
QStringList info;
for (int i = 0; i < numbers.count(); ++i)
{
info = numbers[i].split(":");
if (info.count() != 2)
continue;
numbers_hash.insert(info[0], info[1]);
}
g_pContactManager->openContactDialog(contactName, numbers_hash);
}
}
}
else
{
s->setProperty("incoming_data", data);
}
}