summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/pjsua/bb10/src/applicationui.cpp
blob: a4de872af43e73a0b5370a6c1d2f69c9a36e38c6 (plain)
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
// Default empty project template
#include "applicationui.h"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Label>

#define THIS_FILE	"applicationui.cpp"

using namespace bb::cascades;

/* appUI singleton */
ApplicationUI *ApplicationUI::instance_;

#include "../../pjsua_app_config.h"

void ApplicationUI::extDisplayMsg(const char *msg)
{
    /* Qt's way to invoke method from "foreign" thread */
    QMetaObject::invokeMethod((QObject*)ApplicationUI::instance(),
			      "displayMsg", Qt::AutoConnection,
			      Q_ARG(QString,msg));
}


void ApplicationUI::pjsuaOnStartedCb(pj_status_t status, const char* msg)
{
    char errmsg[PJ_ERR_MSG_SIZE];

    if (status != PJ_SUCCESS && (!msg || !*msg)) {
	pj_strerror(status, errmsg, sizeof(errmsg));
	PJ_LOG(3,(THIS_FILE, "Error: %s", errmsg));
	msg = errmsg;
    } else {
	PJ_LOG(3,(THIS_FILE, "Started: %s", msg));
    }

    ApplicationUI::extDisplayMsg(msg);
}


pj_bool_t ApplicationUI::pjsuaOnStoppedCb(pj_bool_t restart,
					  int argc, char** argv)
{
    PJ_LOG(3,("ipjsua", "CLI %s request", (restart? "restart" : "shutdown")));
    if (restart) {
	ApplicationUI::extDisplayMsg("Restarting..");
	pj_thread_sleep(100);
	ApplicationUI::instance()->extRestartRequest(argc, argv);
    } else {
	ApplicationUI::extDisplayMsg("Shutting down..");
	pj_thread_sleep(100);
	ApplicationUI::instance()->isShuttingDown = true;

	bb::cascades::Application *app = bb::cascades::Application::instance();
	app->quit();
    }

    return PJ_TRUE;
}


void ApplicationUI::pjsuaOnAppConfigCb(pjsua_app_config *cfg)
{
    PJ_UNUSED_ARG(cfg);
}

void ApplicationUI::extRestartRequest(int argc, char **argv)
{
    restartArgc = argc;
    restartArgv = argv;
    QMetaObject::invokeMethod((QObject*)this, "restartPjsua",
			      Qt::QueuedConnection);
}

void ApplicationUI::pjsuaStart()
{
    // TODO: read from config?
    const char **argv = pjsua_app_def_argv;
    int argc = PJ_ARRAY_SIZE(pjsua_app_def_argv) -1;
    app_cfg_t app_cfg;
    pj_status_t status;

    isShuttingDown = false;
    displayMsg("Starting..");

    pj_bzero(&app_cfg, sizeof(app_cfg));
    if (restartArgc) {
	app_cfg.argc = restartArgc;
	app_cfg.argv = restartArgv;
    } else {
	app_cfg.argc = argc;
	app_cfg.argv = (char**)argv;
    }
    app_cfg.on_started = &pjsuaOnStartedCb;
    app_cfg.on_stopped = &pjsuaOnStoppedCb;
    app_cfg.on_config_init = &pjsuaOnAppConfigCb;

    status = app_init(&app_cfg);
    if (status != PJ_SUCCESS) {
	char errmsg[PJ_ERR_MSG_SIZE];
	pj_strerror(status, errmsg, sizeof(errmsg));
	displayMsg(QString("Init error:") + errmsg);
	app_destroy();
	return;
    }

    status = app_run(PJ_FALSE);
    if (status != PJ_SUCCESS) {
	char errmsg[PJ_ERR_MSG_SIZE];
	pj_strerror(status, errmsg, sizeof(errmsg));
	displayMsg(QString("Error:") + errmsg);
	app_destroy();
    }

    restartArgv = NULL;
    restartArgc = 0;
}

void ApplicationUI::pjsuaDestroy()
{
    app_destroy();
}

ApplicationUI::ApplicationUI(bb::cascades::Application *app)
: QObject(app), isShuttingDown(false), restartArgv(NULL), restartArgc(0)
{
    instance_ = this;

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    app->setScene(root);

    app->setAutoExit(true);
    connect(app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()));

    pjsuaStart();
}

ApplicationUI::~ApplicationUI()
{
    instance_ = NULL;
}

ApplicationUI* ApplicationUI::instance()
{
    return instance_;
}

void ApplicationUI::aboutToQuit()
{
    if (!isShuttingDown) {
	isShuttingDown = true;
	PJ_LOG(3,(THIS_FILE, "Quit signal from GUI, shutting down pjsua.."));
	pjsuaDestroy();
    }
}

void ApplicationUI::displayMsg(const QString &msg)
{
    bb::cascades::Application *app = bb::cascades::Application::instance();
    Label *telnetMsg = app->scene()->findChild<Label*>("telnetMsg");
    if (telnetMsg) {
	telnetMsg->setText(msg);
    }
}

void ApplicationUI::restartPjsua()
{
    pjsuaDestroy();
    pjsuaStart();
}