Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | // 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 | |
| 11 | using namespace bb::cascades; |
| 12 | |
| 13 | /* appUI singleton */ |
| 14 | ApplicationUI *ApplicationUI::instance_; |
| 15 | |
| 16 | #include "../../pjsua_app_config.h" |
| 17 | |
| 18 | void 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 | |
| 27 | void 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 | |
| 43 | void 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 | |
| 62 | void ApplicationUI::pjsuaOnAppConfigCb(pjsua_app_config *cfg) |
| 63 | { |
| 64 | PJ_UNUSED_ARG(cfg); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | void 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 | |
| 77 | void 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 | |
| 121 | void ApplicationUI::pjsuaDestroy() |
| 122 | { |
| 123 | pjsua_app_destroy(); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | ApplicationUI::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 | |
| 143 | ApplicationUI::~ApplicationUI() |
| 144 | { |
| 145 | instance_ = NULL; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | ApplicationUI* ApplicationUI::instance() |
| 150 | { |
| 151 | return instance_; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | void 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 | |
| 165 | void 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 | |
| 175 | void ApplicationUI::restartPjsua() |
| 176 | { |
| 177 | pjsuaDestroy(); |
| 178 | pjsuaStart(); |
| 179 | } |