blob: 73c0d3c4e261d7ea407bb7b2bf8bb2cb07410e9c [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001// Default empty project template
2#include "applicationui.h"
3
4#include <bb/cascades/Application>
5#include <bb/cascades/QmlDocument>
6#include <bb/cascades/AbstractPane>
7#include <bb/cascades/Label>
8
9#define THIS_FILE "applicationui.cpp"
10
11using namespace bb::cascades;
12
13/* appUI singleton */
14ApplicationUI *ApplicationUI::instance_;
15
16#include "../../pjsua_app_config.h"
17
18void ApplicationUI::extDisplayMsg(const char *msg)
19{
20 /* Qt's way to invoke method from "foreign" thread */
21 QMetaObject::invokeMethod((QObject*)ApplicationUI::instance(),
22 "displayMsg", Qt::AutoConnection,
23 Q_ARG(QString,msg));
24}
25
26
27void ApplicationUI::pjsuaOnStartedCb(pj_status_t status, const char* msg)
28{
29 char errmsg[PJ_ERR_MSG_SIZE];
30
31 if (status != PJ_SUCCESS && (!msg || !*msg)) {
32 pj_strerror(status, errmsg, sizeof(errmsg));
33 PJ_LOG(3,(THIS_FILE, "Error: %s", errmsg));
34 msg = errmsg;
35 } else {
36 PJ_LOG(3,(THIS_FILE, "Started: %s", msg));
37 }
38
39 ApplicationUI::extDisplayMsg(msg);
40}
41
42
43void ApplicationUI::pjsuaOnStoppedCb(pj_bool_t restart,
44 int argc, char** argv)
45{
46 PJ_LOG(3,("ipjsua", "CLI %s request", (restart? "restart" : "shutdown")));
47 if (restart) {
48 ApplicationUI::extDisplayMsg("Restarting..");
49 pj_thread_sleep(100);
50 ApplicationUI::instance()->extRestartRequest(argc, argv);
51 } else {
52 ApplicationUI::extDisplayMsg("Shutting down..");
53 pj_thread_sleep(100);
54 ApplicationUI::instance()->isShuttingDown = true;
55
56 bb::cascades::Application *app = bb::cascades::Application::instance();
57 app->quit();
58 }
59}
60
61
62void ApplicationUI::pjsuaOnAppConfigCb(pjsua_app_config *cfg)
63{
64 PJ_UNUSED_ARG(cfg);
65}
66
67
68void ApplicationUI::extRestartRequest(int argc, char **argv)
69{
70 restartArgc = argc;
71 restartArgv = argv;
72 QMetaObject::invokeMethod((QObject*)this, "restartPjsua",
73 Qt::QueuedConnection);
74}
75
76
77void ApplicationUI::pjsuaStart()
78{
79 // TODO: read from config?
80 const char **argv = pjsua_app_def_argv;
81 int argc = PJ_ARRAY_SIZE(pjsua_app_def_argv) -1;
82 pjsua_app_cfg_t app_cfg;
83 pj_status_t status;
84
85 isShuttingDown = false;
86 displayMsg("Starting..");
87
88 pj_bzero(&app_cfg, sizeof(app_cfg));
89 if (restartArgc) {
90 app_cfg.argc = restartArgc;
91 app_cfg.argv = restartArgv;
92 } else {
93 app_cfg.argc = argc;
94 app_cfg.argv = (char**)argv;
95 }
96 app_cfg.on_started = &pjsuaOnStartedCb;
97 app_cfg.on_stopped = &pjsuaOnStoppedCb;
98 app_cfg.on_config_init = &pjsuaOnAppConfigCb;
99
100 status = pjsua_app_init(&app_cfg);
101 if (status != PJ_SUCCESS) {
102 char errmsg[PJ_ERR_MSG_SIZE];
103 pj_strerror(status, errmsg, sizeof(errmsg));
104 displayMsg(QString("Init error:") + errmsg);
105 pjsua_app_destroy();
106 return;
107 }
108
109 status = pjsua_app_run(PJ_FALSE);
110 if (status != PJ_SUCCESS) {
111 char errmsg[PJ_ERR_MSG_SIZE];
112 pj_strerror(status, errmsg, sizeof(errmsg));
113 displayMsg(QString("Error:") + errmsg);
114 pjsua_app_destroy();
115 }
116
117 restartArgv = NULL;
118 restartArgc = 0;
119}
120
121void ApplicationUI::pjsuaDestroy()
122{
123 pjsua_app_destroy();
124}
125
126
127ApplicationUI::ApplicationUI(bb::cascades::Application *app)
128: QObject(app), isShuttingDown(false), restartArgv(NULL), restartArgc(0)
129{
130 instance_ = this;
131
132 QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
133 AbstractPane *root = qml->createRootObject<AbstractPane>();
134 app->setScene(root);
135
136 app->setAutoExit(true);
137 connect(app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()));
138
139 pjsuaStart();
140}
141
142
143ApplicationUI::~ApplicationUI()
144{
145 instance_ = NULL;
146}
147
148
149ApplicationUI* ApplicationUI::instance()
150{
151 return instance_;
152}
153
154
155void ApplicationUI::aboutToQuit()
156{
157 if (!isShuttingDown) {
158 isShuttingDown = true;
159 PJ_LOG(3,(THIS_FILE, "Quit signal from GUI, shutting down pjsua.."));
160 pjsuaDestroy();
161 }
162}
163
164
165void ApplicationUI::displayMsg(const QString &msg)
166{
167 bb::cascades::Application *app = bb::cascades::Application::instance();
168 Label *telnetMsg = app->scene()->findChild<Label*>("telnetMsg");
169 if (telnetMsg) {
170 telnetMsg->setText(msg);
171 }
172}
173
174
175void ApplicationUI::restartPjsua()
176{
177 pjsuaDestroy();
178 pjsuaStart();
179}