forked from bicomsystems/outcall2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutCALL.cpp
436 lines (378 loc) · 12.9 KB
/
OutCALL.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
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
#include "OutCALL.h"
#include "DebugInfoDialog.h"
#include "SettingsDialog.h"
#include "AboutDialog.h"
#include "CallHistoryDialog.h"
#include "Global.h"
#include "ContactManager.h"
#include "PlaceCallDialog.h"
#include "AsteriskManager.h"
#include "Notifier.h"
#include "PopupWindow.h"
#include <QMenu>
#include <QDebug>
#include <QTcpSocket>
#include <QApplication>
#include <QDesktopServices>
#include <QUrl>
#include <QVariantList>
#include <QVariantMap>
#include <QMessageBox>
OutCall::OutCall() :
QWidget()
{
m_systemTryIcon = new QSystemTrayIcon(this);
m_menu = new QMenu(this);
m_settingsDialog = new SettingsDialog;
m_aboutDialog = new AboutDialog;
m_debugInfoDialog = new DebugInfoDialog;
m_callHistoryDialog = new CallHistoryDialog;
m_placeCallDialog = new PlaceCallDialog;
connect(m_systemTryIcon, &QSystemTrayIcon::activated, this, &OutCall::onActivated);
connect(g_pContactManager, &ContactManager::syncing, this, &OutCall::onSyncing);
connect(g_pAsteriskManager, &AsteriskManager::messageReceived, this, &OutCall::onMessageReceived);
connect(g_pAsteriskManager, &AsteriskManager::callDeteceted, this, &OutCall::onCallDeteceted);
connect(g_pAsteriskManager, &AsteriskManager::callReceived, this, &OutCall::onCallReceived);
connect(g_pAsteriskManager, &AsteriskManager::error, this, &OutCall::displayError);
connect(g_pAsteriskManager, &AsteriskManager::stateChanged, this, &OutCall::onStateChanged);
connect(&m_timer, &QTimer::timeout, this, &OutCall::changeIcon);
global::setSettingsValue("InstallDir", g_AppDirPath.replace("/", "\\"));
createContextMenu();
m_systemTryIcon->setContextMenu(m_menu);
QString path(":/images/disconnected.png");
m_systemTryIcon->setIcon(QIcon(path));
automaticlySignIn();
}
OutCall::~OutCall()
{
delete m_settingsDialog;
delete m_debugInfoDialog;
delete m_aboutDialog;
delete m_callHistoryDialog;
delete m_placeCallDialog;
}
void OutCall::createContextMenu()
{
// Exit action
QAction* exitAction = new QAction(tr("Exit"), m_menu);
connect(exitAction, &QAction::triggered, this, &OutCall::close);
// Sign In
m_signIn = new QAction(tr("Sign In"), m_menu);
connect(m_signIn, &QAction::triggered, this, &OutCall::signInOut);
// Add Contacts
QAction* addContactAction = new QAction(tr("Add Contact"), m_menu);
connect(addContactAction, &QAction::triggered, this, &OutCall::onAddContact);
// SettingsDialog
QAction* settingsAction = new QAction(tr("Settings"), m_menu);
connect(settingsAction, &QAction::triggered, this, &OutCall::onSettingsDialog);
QAction* debugInfoAction = new QAction(tr("Debug info"), m_menu);
connect(debugInfoAction, &QAction::triggered, this, &OutCall::onDebugInfo);
// About
QAction* aboutAction = new QAction(tr("About"), m_menu);
connect(aboutAction, &QAction::triggered, this, &OutCall::onAboutDialog);
// Help
QMenu *helpMenu = new QMenu(tr("Help"), m_menu);
m_menu->addMenu(helpMenu);
QAction* onlineHelp = new QAction(tr("Online HTML"), helpMenu);
connect(onlineHelp, &QAction::triggered, this, &OutCall::onOnlineHelp);
QAction* onlinePdf = new QAction(tr("Online PDF"), helpMenu);
connect(onlinePdf, &QAction::triggered, this, &OutCall::onOnlinePdf);
// Call History
QAction* callHistoryAction = new QAction(tr("Call History"), m_menu);
connect(callHistoryAction, &QAction::triggered, this, &OutCall::onCallHistory);
// Import Contacts
QMenu *importContactsMenu = new QMenu(tr("Sync Contacts"), m_menu);
QAction* importOutlookAction = new QAction(tr("From Outlook"), importContactsMenu);
connect(importOutlookAction, &QAction::triggered, this, &OutCall::onSyncOutlook);
// Place a Call
m_placeCall = new QAction(tr("Place a Call"), 0);
QFont font = m_placeCall->font();
font.setBold(true);
m_placeCall->setFont(font);
connect(m_placeCall, &QAction::triggered, this, &OutCall::onPlaceCall);
helpMenu->addAction(onlineHelp);
helpMenu->addAction(onlinePdf);
// Add actions
m_menu->addAction(aboutAction);
m_menu->addSeparator();
m_menu->addAction(settingsAction);
m_menu->addAction(debugInfoAction);
m_menu->addSeparator();
m_menu->addAction(m_placeCall);
m_menu->addAction(callHistoryAction);
m_menu->addMenu(importContactsMenu);
importContactsMenu->addAction(importOutlookAction);
m_menu->addSeparator();
m_menu->addAction(m_signIn);
m_menu->addAction(addContactAction);
m_menu->addSeparator();
m_menu->addAction(exitAction);
}
void OutCall::automaticlySignIn()
{
bool autoSingIn = global::getSettingsValue("auto_sign_in", "general").toBool();
if(autoSingIn)
signInOut();
}
void OutCall::signInOut()
{
if (m_signIn->text() == "Cancel Sign In")
{
g_pAsteriskManager->signOut();
return;
}
if (g_pAsteriskManager->isSignedIn() == false)
{
QString server = global::getSettingsValue("servername", "settings").toString();
QString port = global::getSettingsValue("port", "settings", "5038").toString();
g_pAsteriskManager->signIn(server, port.toUInt());
}
else
{
g_pAsteriskManager->signOut();
}
}
void OutCall::onSyncOutlook()
{
g_pContactManager->refreshContacts();
}
void OutCall::displayError(QAbstractSocket::SocketError socketError, const QString &msg)
{
switch (socketError)
{
case QAbstractSocket::RemoteHostClosedError:
MsgBoxInformation(tr("The remote host closed the connection."));
break;
case QAbstractSocket::HostNotFoundError:
MsgBoxInformation(tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
MsgBoxInformation(tr("The connection was refused by the peer. "
"Make sure the server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
MsgBoxInformation(msg);
}
}
void OutCall::onSyncing(bool status)
{
if (status)
{
foreach(QAction *action, m_menu->actions())
{
if (action->text() == tr("Sync Contacts"))
{
m_systemTryIcon->setToolTip(tr("Syncing contacts"));
action->setText(tr("Syncing ..."));
action->setDisabled(true);
}
}
}
else
{
foreach(QAction *action, m_menu->actions())
{
if (action->text() == tr("Syncing ..."))
{
if (g_pAsteriskManager->isSignedIn())
m_systemTryIcon->setToolTip(tr(APP_NAME) + tr(" - ") + tr("Siged In"));
else
m_systemTryIcon->setToolTip(tr(APP_NAME) + tr(" - ") + tr("Not Siged In"));
action->setText(tr("Sync Contacts"));
action->setEnabled(true);
}
}
}
}
void OutCall::onAddContact()
{
g_pContactManager->addOutlookContact();
}
void OutCall::onMessageReceived(const QString &message)
{
if (m_debugInfoDialog)
m_debugInfoDialog->updateDebug(message);
}
void OutCall::onCallDeteceted(const QMap<QString, QVariant> &call, AsteriskManager::CallState state)
{
m_callHistoryDialog->addCall(call, (CallHistoryDialog::Calls)state);
}
void OutCall::onCallReceived(const QMap<QString, QVariant> &call)
{
QString from = call.value("from").toString();
QString callerName = call.value("callerIDName").toString();
bool isMinCallerID = global::getSettingsValue("min_caller_state", "general").toBool();
bool contactOnInboud = global::getSettingsValue("contact_inbound", "outlook").toBool();
bool contactOnUnknown = global::getSettingsValue("contact_unknown", "outlook").toBool();
bool isCallerIDUnknown = true;
int callerLength = from.size();
QList<Contact*> contactList = g_pContactManager->getContacts();
QString outlookContactName;
for(int i = 0; i < contactList.size(); ++i)
{
Contact *contact = contactList[i];
QList<QString> numbers = contact->numbers.values();
if (numbers.contains(from))
{
outlookContactName = contact->name;
isCallerIDUnknown = false;
break;
}
}
if (callerName.isEmpty() || callerName == "<unknown>" || !outlookContactName.isEmpty())
{
callerName = outlookContactName;
}
if (isMinCallerID)
{
int length = global::getSettingsValue("min_caller_id", "general").toInt();
if (callerLength >= length)
{
if (callerName.isEmpty() || callerName == "<unknown>")
{
PopupWindow::showCallNotification(QString("(Nr: %2)").arg(from));
}
else
{
PopupWindow::showCallNotification(QString("%1 (Nr: %2)").arg(callerName).arg(from));
}
}
}
else
{
if (callerName.isEmpty() || callerName == "<unknown>")
{
PopupWindow::showCallNotification(QString("(Nr: %2)").arg(from));
}
else
{
PopupWindow::showCallNotification(QString("%1 (Nr: %2)").arg(callerName).arg(from));
}
}
if (contactOnInboud && !isCallerIDUnknown)
{
g_pContactManager->viewOutlookContact(callerName, "");
}
else if (isCallerIDUnknown && contactOnUnknown)
{
g_pContactManager->addOutlookContact(from, callerName);
}
}
void OutCall::onStateChanged(AsteriskManager::AsteriskState state)
{
if (state == AsteriskManager::CONNECTED)
{
QString path(":/images/connected.png");
m_systemTryIcon->setIcon(QIcon(path));
m_signIn->setText(tr("Sign out"));
PopupWindow::showInformationMessage(tr(APP_NAME), tr("Signed In"));
m_systemTryIcon->setToolTip(tr(APP_NAME) + tr(" - ") + tr("Siged In"));
m_placeCall->setEnabled(true);
m_timer.stop();
}
else if (state == AsteriskManager::CONNECTING)
{
m_signIn->setText(tr("Cancel Sign In"));
m_systemTryIcon->setToolTip(tr(APP_NAME) + tr(" - ") + tr("Signing In"));
m_placeCall->setEnabled(false);
m_timer.start(500);
}
else if (state == AsteriskManager::DISCONNECTED)
{
QString path(":/images/disconnected.png");
m_systemTryIcon->setIcon(QIcon(path));
m_signIn->setText(tr("&Sign In"));
m_systemTryIcon->setToolTip(tr(APP_NAME) + tr(" - ") + tr("Not Siged In"));
m_placeCall->setEnabled(false);
m_timer.stop();
}
else if (state == AsteriskManager::AUTHENTICATION_FAILED)
{
QString path(":/images/started.png");
m_systemTryIcon->setIcon(QIcon(path));
PopupWindow::showInformationMessage(tr(APP_NAME), tr("Authentication failed"));
m_systemTryIcon->setToolTip(tr(APP_NAME) + tr(" - ") + tr("Not configured"));
m_signIn->setText(tr("&Sign In"));
m_placeCall->setEnabled(false);
m_timer.stop();
}
}
void OutCall::changeIcon()
{
if (m_switch)
{
QString path(":/images/connected.png");
m_systemTryIcon->setIcon(QIcon(path));
m_switch = false;
}
else
{
QString path(":/images/disconnected.png");
m_systemTryIcon->setIcon(QIcon(path));
m_switch = true;
}
}
void OutCall::onOnlineHelp()
{
QString link = "http://www.bicomsystems.com/docs/outcall/1.0/html/";
QDesktopServices::openUrl(QUrl(link));
}
void OutCall::onOnlinePdf()
{
QString link = "http://www.bicomsystems.com/docs/outcall/1.0/pdf/outcall.pdf";
QDesktopServices::openUrl(QUrl(link));
}
void OutCall::onCallHistory()
{
m_callHistoryDialog->show();
m_callHistoryDialog->raise();
}
void OutCall::onSettingsDialog()
{
SettingsDialog dialog;
dialog.exec();
}
void OutCall::onAboutDialog()
{
m_aboutDialog->show();
m_aboutDialog->raise();
}
void OutCall::onDebugInfo()
{
m_debugInfoDialog->show();
m_debugInfoDialog->raise();
}
void OutCall::onPlaceCall()
{
m_placeCallDialog->show();
m_placeCallDialog->raise();
}
void OutCall::close()
{
g_pAsteriskManager->signOut();
QApplication::quit();
}
void OutCall::onActivated(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::Trigger)
{
m_debugInfoDialog->activateWindow();
m_aboutDialog->activateWindow();
m_settingsDialog->activateWindow();
m_callHistoryDialog->activateWindow();
g_pContactManager->activateDialog();
m_placeCallDialog->activateWindow();
}
else if (reason == QSystemTrayIcon::DoubleClick)
{
m_placeCallDialog->show();
m_placeCallDialog->activateWindow();
}
}
void OutCall::show()
{
m_systemTryIcon->show();
}