mms_engine.c 20.1 KB
Newer Older
Slava Monich's avatar
Slava Monich committed
1
/*
2
 * Copyright (C) 2013-2016 Jolla Ltd.
3
 * Contact: Slava Monich <slava.monich@jolla.com>
Slava Monich's avatar
Slava Monich committed
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "mms_engine.h"
#include "mms_dispatcher.h"
17
#include "mms_settings.h"
Slava Monich's avatar
Slava Monich committed
18 19
#include "mms_lib_util.h"
#include "mms_handler_dbus.h"
20
#include "mms_settings_dconf.h"
21
#include "mms_transfer_list_dbus.h"
Slava Monich's avatar
Slava Monich committed
22

23
#ifdef SAILFISH
24 25 26 27 28 29 30
#  include "mms_connman_nemo.h"
#  define mms_connman_new() mms_connman_nemo_new()
#else
#  include "mms_connman_ofono.h"
#  define mms_connman_new() mms_connman_ofono_new()
#endif

Slava Monich's avatar
Slava Monich committed
31 32 33
/* Generated code */
#include "org.nemomobile.MmsEngine.h"

34
#include <gutil_macros.h>
35 36 37
#include <gutil_misc.h>
#include <gutil_log.h>

Slava Monich's avatar
Slava Monich committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* Signals D-Bus proxy */
enum mms_engine_dbus_methods {
    MMS_ENGINE_METHOD_SEND_MESSAGE,
    MMS_ENGINE_METHOD_PUSH,
    MMS_ENGINE_METHOD_PUSH_NOTIFY,
    MMS_ENGINE_METHOD_CANCEL,
    MMS_ENGINE_METHOD_RECEIVE_MESSAGE,
    MMS_ENGINE_METHOD_SEND_READ_REPORT,
    MMS_ENGINE_METHOD_SET_LOG_LEVEL,
    MMS_ENGINE_METHOD_SET_LOG_TYPE,
    MMS_ENGINE_METHOD_GET_VERSION,
    MMS_ENGINE_METHOD_MIGRATE_SETTINGS,
    MMS_ENGINE_METHOD_COUNT
};

Slava Monich's avatar
Slava Monich committed
53 54 55
struct mms_engine {
    GObject parent;
    const MMSConfig* config;
56 57
    MMSConnMan* cm;
    MMSSettings* settings;
Slava Monich's avatar
Slava Monich committed
58 59 60 61 62
    MMSDispatcher* dispatcher;
    MMSDispatcherDelegate dispatcher_delegate;
    MMSLogModule** log_modules;
    int log_count;
    GDBusConnection* engine_bus;
Slava Monich's avatar
Slava Monich committed
63
    OrgNemomobileMmsEngine* proxy;
Slava Monich's avatar
Slava Monich committed
64 65
    GMainLoop* loop;
    gboolean stopped;
66
    gboolean stop_requested;
Slava Monich's avatar
Slava Monich committed
67 68
    gboolean keep_running;
    guint start_timeout_id;
Slava Monich's avatar
Slava Monich committed
69
    gulong proxy_signal_id[MMS_ENGINE_METHOD_COUNT];
Slava Monich's avatar
Slava Monich committed
70 71 72
};

typedef GObjectClass MMSEngineClass;
Slava Monich's avatar
Slava Monich committed
73
G_DEFINE_TYPE(MMSEngine, mms_engine, G_TYPE_OBJECT)
74
#define MMS_TYPE_ENGINE (mms_engine_get_type())
Slava Monich's avatar
Slava Monich committed
75
#define MMS_ENGINE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
76
        MMS_TYPE_ENGINE, MMSEngine))
Slava Monich's avatar
Slava Monich committed
77 78 79

inline static MMSEngine*
mms_engine_from_dispatcher_delegate(MMSDispatcherDelegate* delegate)
80
    { return G_CAST(delegate,MMSEngine,dispatcher_delegate); }
Slava Monich's avatar
Slava Monich committed
81 82 83 84 85 86 87

static
gboolean
mms_engine_stop_callback(
    gpointer data)
{
    MMSEngine* engine = data;
88 89
    engine->stopped = TRUE;
    if (engine->loop) g_main_loop_quit(engine->loop);
Slava Monich's avatar
Slava Monich committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    mms_engine_unref(engine);
    return FALSE;
}

static
void
mms_engine_stop_schedule(
    MMSEngine* engine)
{
    g_idle_add(mms_engine_stop_callback, mms_engine_ref(engine));
}

static
gboolean
mms_engine_start_timeout_callback(
    gpointer data)
{
    MMSEngine* engine = data;
108 109
    GASSERT(engine->start_timeout_id);
    GINFO("Shutting down due to inactivity...");
Slava Monich's avatar
Slava Monich committed
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
    engine->start_timeout_id = 0;
    mms_engine_stop_schedule(engine);
    return FALSE;
}

static
void
mms_engine_start_timeout_cancel(
    MMSEngine* engine)
{
    if (engine->start_timeout_id) {
        g_source_remove(engine->start_timeout_id);
        engine->start_timeout_id = 0;
    }
}

static
void
mms_engine_start_timeout_schedule(
    MMSEngine* engine)
{
    mms_engine_start_timeout_cancel(engine);
    engine->start_timeout_id = g_timeout_add_seconds(engine->config->idle_secs,
        mms_engine_start_timeout_callback, engine);
}

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
/* org.nemomobile.MmsEngine.sendMessage */
static
gboolean
mms_engine_handle_send_message(
    OrgNemomobileMmsEngine* proxy,
    GDBusMethodInvocation* call,
    int database_id,
    const char* imsi_to,
    const char* const* to,
    const char* const* cc,
    const char* const* bcc,
    const char* subject,
    guint flags,
    GVariant* attachments,
    MMSEngine* engine)
{
    if (to && *to) {
        unsigned int i;
        char* to_list = g_strjoinv(",", (char**)to);
        char* cc_list = NULL;
        char* bcc_list = NULL;
        char* id = NULL;
        char* imsi;
        MMSAttachmentInfo* parts;
        GArray* info = g_array_sized_new(FALSE, FALSE, sizeof(*parts), 0);
        GError* error = NULL;

        /* Extract attachment info */
        char* fn = NULL;
        char* ct = NULL;
        char* cid = NULL;
        GVariantIter* iter = NULL;
        g_variant_get(attachments, "a(sss)", &iter);
        while (g_variant_iter_loop(iter, "(sss)", &fn, &ct, &cid)) {
            MMSAttachmentInfo part;
            part.file_name = g_strdup(fn);
            part.content_type = g_strdup(ct);
            part.content_id = g_strdup(cid);
            g_array_append_vals(info, &part, 1);
        }

        /* Convert address lists into comma-separated strings
         * expected by mms_dispatcher_send_message and mms_codec */
        if (cc && *cc) cc_list = g_strjoinv(",", (char**)cc);
        if (bcc && *bcc) bcc_list = g_strjoinv(",", (char**)bcc);
        if (database_id > 0) id = g_strdup_printf("%u", database_id);

        /* Queue the message */
        parts = (void*)info->data;
        imsi = mms_dispatcher_send_message(engine->dispatcher, id,
            imsi_to, to_list, cc_list, bcc_list, subject, flags, parts,
            info->len, &error);
        if (imsi) {
            if (mms_dispatcher_start(engine->dispatcher)) {
                mms_engine_start_timeout_cancel(engine);
            }
            org_nemomobile_mms_engine_complete_send_message(proxy, call, imsi);
            g_free(imsi);
        } else {
            g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
196
                G_DBUS_ERROR_FAILED, "%s", GERRMSG(error));
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
            g_error_free(error);
        }

        for (i=0; i<info->len; i++) {
            g_free((void*)parts[i].file_name);
            g_free((void*)parts[i].content_type);
            g_free((void*)parts[i].content_id);
        }

        g_free(to_list);
        g_free(cc_list);
        g_free(bcc_list);
        g_free(id);
        g_array_unref(info);
        g_variant_iter_free(iter);
    } else {
        g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
            G_DBUS_ERROR_FAILED, "Missing recipient");
    }
    return TRUE;
}

Slava Monich's avatar
Slava Monich committed
219 220 221 222
/* org.nemomobile.MmsEngine.receiveMessage */
static
gboolean
mms_engine_handle_receive_message(
Slava Monich's avatar
Slava Monich committed
223
    OrgNemomobileMmsEngine* proxy,
Slava Monich's avatar
Slava Monich committed
224 225 226 227 228 229 230 231 232
    GDBusMethodInvocation* call,
    int database_id,
    const char* imsi,
    gboolean automatic,
    GVariant* data,
    MMSEngine* engine)
{
    gsize len = 0;
    const guint8* bytes = g_variant_get_fixed_array(data, &len, 1);
233
    GDEBUG("Processing push %u bytes from %s", (guint)len, imsi);
Slava Monich's avatar
Slava Monich committed
234 235 236
    if (imsi && bytes && len) {
        char* id = g_strdup_printf("%d", database_id);
        GBytes* push = g_bytes_new(bytes, len);
237
        GError* error = NULL;
Slava Monich's avatar
Slava Monich committed
238
        if (mms_dispatcher_receive_message(engine->dispatcher, id, imsi,
239
            automatic, push, &error)) {
Slava Monich's avatar
Slava Monich committed
240 241 242
            if (mms_dispatcher_start(engine->dispatcher)) {
                mms_engine_start_timeout_cancel(engine);
            }
Slava Monich's avatar
Slava Monich committed
243
            org_nemomobile_mms_engine_complete_receive_message(proxy, call);
Slava Monich's avatar
Slava Monich committed
244 245
        } else {
            g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
246
                G_DBUS_ERROR_FAILED, "%s", GERRMSG(error));
247
            g_error_free(error);
Slava Monich's avatar
Slava Monich committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261
        }
        g_bytes_unref(push);
        g_free(id);
    } else {
        g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
            G_DBUS_ERROR_FAILED, "Invalid parameters");
    }
    return TRUE;
}

/* org.nemomobile.MmsEngine.sendReadReport */
static
gboolean
mms_engine_handle_send_read_report(
Slava Monich's avatar
Slava Monich committed
262
    OrgNemomobileMmsEngine* proxy,
Slava Monich's avatar
Slava Monich committed
263
    GDBusMethodInvocation* call,
264
    int database_id,
Slava Monich's avatar
Slava Monich committed
265 266 267 268 269 270
    const char* imsi,
    const char* message_id,
    const char* to,
    int read_status, /*  0: Read  1: Deleted without reading */
    MMSEngine* engine)
{
271
    GError* error = NULL;
272
    char* id = g_strdup_printf("%d", database_id);
273
    GDEBUG_("%s %s %s %s %d", id, imsi, message_id, to, read_status);
Slava Monich's avatar
Slava Monich committed
274 275
    if (mms_dispatcher_send_read_report(engine->dispatcher, id, imsi,
        message_id, to, (read_status == 1) ? MMS_READ_STATUS_DELETED :
276
        MMS_READ_STATUS_READ, &error)) {
Slava Monich's avatar
Slava Monich committed
277 278 279
        if (mms_dispatcher_start(engine->dispatcher)) {
            mms_engine_start_timeout_cancel(engine);
        }
Slava Monich's avatar
Slava Monich committed
280
        org_nemomobile_mms_engine_complete_send_read_report(proxy, call);
Slava Monich's avatar
Slava Monich committed
281 282
    } else {
        g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
283
            G_DBUS_ERROR_FAILED, "%s", GERRMSG(error));
284
        g_error_free(error);
Slava Monich's avatar
Slava Monich committed
285
    }
286
    g_free(id);
Slava Monich's avatar
Slava Monich committed
287 288 289 290 291 292 293
    return TRUE;
}

/* org.nemomobile.MmsEngine.cancel */
static
gboolean
mms_engine_handle_cancel(
Slava Monich's avatar
Slava Monich committed
294
    OrgNemomobileMmsEngine* proxy,
Slava Monich's avatar
Slava Monich committed
295
    GDBusMethodInvocation* call,
296
    int database_id,
Slava Monich's avatar
Slava Monich committed
297 298
    MMSEngine* engine)
{
299
    char* id = NULL;
300
    if (database_id > 0) id = g_strdup_printf("%u", database_id);
301
    GDEBUG_("%s", id);
Slava Monich's avatar
Slava Monich committed
302
    mms_dispatcher_cancel(engine->dispatcher, id);
Slava Monich's avatar
Slava Monich committed
303
    org_nemomobile_mms_engine_complete_cancel(proxy, call);
304
    g_free(id);
Slava Monich's avatar
Slava Monich committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    return TRUE;
}

/* org.nemomobile.MmsEngine.pushNotify */
static
gboolean
mms_engine_handle_push_notify(
    OrgNemomobileMmsEngine* proxy,
    GDBusMethodInvocation* call,
    const char* imsi,
    const char* type,
    GVariant* data,
    MMSEngine* engine)
{
    gsize len = 0;
    const guint8* bytes = g_variant_get_fixed_array(data, &len, 1);
321
    GDEBUG("Received %u bytes from %s", (guint)len, imsi);
Slava Monich's avatar
Slava Monich committed
322
    if (!type || g_ascii_strcasecmp(type, MMS_CONTENT_TYPE)) {
323
        GERR("Unsupported content type %s", type);
Slava Monich's avatar
Slava Monich committed
324 325 326
        g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
            G_DBUS_ERROR_FAILED, "Unsupported content type");
    } else if (!imsi || !imsi[0]) {
327
        GERR_("IMSI is missing");
Slava Monich's avatar
Slava Monich committed
328 329 330
        g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
            G_DBUS_ERROR_FAILED, "IMSI is missing");
    } else if (!bytes || !len) {
331
        GERR_("No data provided");
Slava Monich's avatar
Slava Monich committed
332 333 334
        g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
            G_DBUS_ERROR_FAILED, "No data provided");
    } else {
335
        GError* err = NULL;
Slava Monich's avatar
Slava Monich committed
336
        GBytes* msg = g_bytes_new(bytes, len);
337
        if (mms_dispatcher_handle_push(engine->dispatcher, imsi, msg, &err)) {
Slava Monich's avatar
Slava Monich committed
338 339 340 341 342 343
            if (mms_dispatcher_start(engine->dispatcher)) {
                mms_engine_start_timeout_cancel(engine);
            }
            org_nemomobile_mms_engine_complete_push(proxy, call);
        } else {
            g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
344
                G_DBUS_ERROR_FAILED, "%s", GERRMSG(err));
345
            g_error_free(err);
Slava Monich's avatar
Slava Monich committed
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
        }
        g_bytes_unref(msg);
    }
    return TRUE;
}

/* org.nemomobile.MmsEngine.push */
static
gboolean
mms_engine_handle_push(
    OrgNemomobileMmsEngine* proxy,
    GDBusMethodInvocation* call,
    const char* imsi,
    const char* from,
    guint32 remote_time,
    guint32 local_time,
    int dst_port,
    int src_port,
    const char* type,
    GVariant* data,
    MMSEngine* eng)
{
    return mms_engine_handle_push_notify(proxy, call, imsi, type, data, eng);
}

/* org.nemomobile.MmsEngine.setLogLevel */
static
gboolean
mms_engine_handle_set_log_level(
Slava Monich's avatar
Slava Monich committed
375
    OrgNemomobileMmsEngine* proxy,
Slava Monich's avatar
Slava Monich committed
376 377 378 379 380
    GDBusMethodInvocation* call,
    const char* module,
    gint level,
    MMSEngine* engine)
{
381
    GDEBUG_("%s:%d", module, level);
Slava Monich's avatar
Slava Monich committed
382 383 384 385 386 387 388 389 390 391
    if (module && module[0]) {
        int i;
        for (i=0; i<engine->log_count; i++) {
            MMSLogModule* log = engine->log_modules[i];
            if (log->name && log->name[0] && !strcmp(log->name, module)) {
                log->level = level;
                break;
            }
        }
    } else {
392
        gutil_log_default.level = level;
Slava Monich's avatar
Slava Monich committed
393
    }
Slava Monich's avatar
Slava Monich committed
394
    org_nemomobile_mms_engine_complete_set_log_level(proxy, call);
Slava Monich's avatar
Slava Monich committed
395 396 397 398 399 400 401
    return TRUE;
}

/* org.nemomobile.MmsEngine.setLogType */
static
gboolean
mms_engine_handle_set_log_type(
Slava Monich's avatar
Slava Monich committed
402
    OrgNemomobileMmsEngine* proxy,
Slava Monich's avatar
Slava Monich committed
403 404 405 406
    GDBusMethodInvocation* call,
    const char* type,
    MMSEngine* engine)
{
407 408
    GDEBUG_("%s", type);
    gutil_log_set_type(type, MMS_APP_LOG_PREFIX);
Slava Monich's avatar
Slava Monich committed
409
    org_nemomobile_mms_engine_complete_set_log_type(proxy, call);
Slava Monich's avatar
Slava Monich committed
410 411 412
    return TRUE;
}

413 414 415 416 417 418 419 420 421 422 423 424 425
/* org.nemomobile.MmsEngine.getVersion */
static
gboolean
mms_engine_handle_get_version(
    OrgNemomobileMmsEngine* proxy,
    GDBusMethodInvocation* call,
    MMSEngine* engine)
{
#ifdef MMS_VERSION_STRING
    int v1 = 0, v2 = 0, v3 = 0;
    char* s = g_malloc(strlen(MMS_VERSION_STRING)+1);
    s[0] = 0;
    if (sscanf(MMS_VERSION_STRING, "%d.%d.%d%s", &v1, &v2, &v3, s) < 3) {
426
        GWARN_("unable to parse version %s", MMS_VERSION_STRING);
427
    } else {
428
        GDEBUG_("version %d.%d.%d%s", v1, v2, v3, s);
429 430 431 432
    }
    org_nemomobile_mms_engine_complete_get_version(proxy, call, v1, v2, v3, s);
    g_free(s);
#else
433
    GDEBUG_("oops");
434 435 436 437 438
    org_nemomobile_mms_engine_complete_get_version(proxy, call, 0, 0, 0, "");
#endif
    return TRUE;
}

439 440 441 442 443 444 445 446 447 448 449 450
/* org.nemomobile.MmsEngine.migrateSettings */
static
gboolean
mms_engine_handle_migrate_settings(
    OrgNemomobileMmsEngine* proxy,
    GDBusMethodInvocation* call,
    const char* imsi,
    MMSEngine* engine)
{
    char* tmp = NULL;
    /* Querying settings will migrate per-SIM settings after upgrading
     * from 1.0.21 or older version of mme-engine */
451
    GDEBUG_("%s", imsi);
452 453 454 455 456 457 458 459 460 461 462
    if (!imsi || !imsi[0]) {
        imsi = tmp = mms_connman_default_imsi(engine->cm);
    }
    if (imsi) {
        mms_settings_get_sim_data(engine->settings, imsi);
    }
    org_nemomobile_mms_engine_complete_migrate_settings(proxy, call);
    g_free(tmp);
    return TRUE;
}

Slava Monich's avatar
Slava Monich committed
463 464 465
MMSEngine*
mms_engine_new(
    const MMSConfig* config,
466
    const MMSSettingsSimData* defaults,
Slava Monich's avatar
Slava Monich committed
467 468 469 470
    unsigned int flags,
    MMSLogModule* log_modules[],
    int log_count)
{
471
    MMSConnMan* cm = mms_connman_new();
Slava Monich's avatar
Slava Monich committed
472
    if (cm) {
473
        MMSEngine* mms = g_object_new(MMS_TYPE_ENGINE, NULL);
Slava Monich's avatar
Slava Monich committed
474
        MMSHandler* handler = mms_handler_dbus_new();
475
        MMSSettings* settings = mms_settings_dconf_new(config, defaults);
476
        MMSTransferList* txlist = mms_transfer_list_dbus_new();
477 478 479 480 481 482 483 484 485 486 487 488
        static const struct _mms_engine_settings_flags_map {
#define MAP_(x) \
    MMS_ENGINE_FLAG_OVERRIDE_##x, \
    MMS_SETTINGS_FLAG_OVERRIDE_##x
            int engine_flag;
            int settings_flag;
        } flags_map [] = {
            { MAP_(USER_AGENT)},
            { MAP_(UAPROF) },
            { MAP_(SIZE_LIMIT) },
            { MAP_(MAX_PIXELS) }
        };
489

490 491 492 493 494
        unsigned int i;
        for (i=0; i<G_N_ELEMENTS(flags_map); i++) {
            if (flags & flags_map[i].engine_flag) {
                settings->flags |= flags_map[i].settings_flag;
            }
495 496
        }

497
        mms->dispatcher = mms_dispatcher_new(settings, cm, handler, txlist);
Slava Monich's avatar
Slava Monich committed
498
        mms_handler_unref(handler);
499
        mms_transfer_list_unref(txlist);
Slava Monich's avatar
Slava Monich committed
500 501 502 503 504 505 506
        mms_dispatcher_set_delegate(mms->dispatcher,
            &mms->dispatcher_delegate);

        if (flags & MMS_ENGINE_FLAG_KEEP_RUNNING) {
            mms->keep_running = TRUE;
        }

507
        mms->cm = cm;
Slava Monich's avatar
Slava Monich committed
508
        mms->config = config;
509
        mms->settings = settings;
Slava Monich's avatar
Slava Monich committed
510 511
        mms->log_modules = log_modules;
        mms->log_count = log_count;
Slava Monich's avatar
Slava Monich committed
512
        mms->proxy = org_nemomobile_mms_engine_skeleton_new();
Slava Monich's avatar
Slava Monich committed
513
        mms->proxy_signal_id[MMS_ENGINE_METHOD_SEND_MESSAGE] =
514 515
            g_signal_connect(mms->proxy, "handle-send-message",
            G_CALLBACK(mms_engine_handle_send_message), mms);
Slava Monich's avatar
Slava Monich committed
516
        mms->proxy_signal_id[MMS_ENGINE_METHOD_PUSH] =
Slava Monich's avatar
Slava Monich committed
517
            g_signal_connect(mms->proxy, "handle-push",
Slava Monich's avatar
Slava Monich committed
518
            G_CALLBACK(mms_engine_handle_push), mms);
Slava Monich's avatar
Slava Monich committed
519
        mms->proxy_signal_id[MMS_ENGINE_METHOD_PUSH_NOTIFY] =
Slava Monich's avatar
Slava Monich committed
520
            g_signal_connect(mms->proxy, "handle-push-notify",
Slava Monich's avatar
Slava Monich committed
521
            G_CALLBACK(mms_engine_handle_push_notify), mms);
Slava Monich's avatar
Slava Monich committed
522
        mms->proxy_signal_id[MMS_ENGINE_METHOD_CANCEL] =
Slava Monich's avatar
Slava Monich committed
523
            g_signal_connect(mms->proxy, "handle-cancel",
Slava Monich's avatar
Slava Monich committed
524
            G_CALLBACK(mms_engine_handle_cancel), mms);
Slava Monich's avatar
Slava Monich committed
525
        mms->proxy_signal_id[MMS_ENGINE_METHOD_RECEIVE_MESSAGE] =
Slava Monich's avatar
Slava Monich committed
526
            g_signal_connect(mms->proxy, "handle-receive-message",
Slava Monich's avatar
Slava Monich committed
527
            G_CALLBACK(mms_engine_handle_receive_message), mms);
Slava Monich's avatar
Slava Monich committed
528
        mms->proxy_signal_id[MMS_ENGINE_METHOD_SEND_READ_REPORT] =
Slava Monich's avatar
Slava Monich committed
529
            g_signal_connect(mms->proxy, "handle-send-read-report",
Slava Monich's avatar
Slava Monich committed
530
            G_CALLBACK(mms_engine_handle_send_read_report), mms);
Slava Monich's avatar
Slava Monich committed
531
        mms->proxy_signal_id[MMS_ENGINE_METHOD_SET_LOG_LEVEL] =
Slava Monich's avatar
Slava Monich committed
532
            g_signal_connect(mms->proxy, "handle-set-log-level",
Slava Monich's avatar
Slava Monich committed
533
            G_CALLBACK(mms_engine_handle_set_log_level), mms);
Slava Monich's avatar
Slava Monich committed
534
        mms->proxy_signal_id[MMS_ENGINE_METHOD_SET_LOG_TYPE] =
Slava Monich's avatar
Slava Monich committed
535
            g_signal_connect(mms->proxy, "handle-set-log-type",
Slava Monich's avatar
Slava Monich committed
536
            G_CALLBACK(mms_engine_handle_set_log_type), mms);
Slava Monich's avatar
Slava Monich committed
537
        mms->proxy_signal_id[MMS_ENGINE_METHOD_GET_VERSION] =
538 539
            g_signal_connect(mms->proxy, "handle-get-version",
            G_CALLBACK(mms_engine_handle_get_version), mms);
Slava Monich's avatar
Slava Monich committed
540
        mms->proxy_signal_id[MMS_ENGINE_METHOD_MIGRATE_SETTINGS] =
541 542
            g_signal_connect(mms->proxy, "handle-migrate-settings",
            G_CALLBACK(mms_engine_handle_migrate_settings), mms);
Slava Monich's avatar
Slava Monich committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

        return mms;
    }

    return NULL;
}

MMSEngine*
mms_engine_ref(
    MMSEngine* engine)
{
    return g_object_ref(MMS_ENGINE(engine));
}

void
mms_engine_unref(
    MMSEngine* engine)
{
    if (engine) g_object_unref(MMS_ENGINE(engine));
}

void
mms_engine_run(
    MMSEngine* engine,
    GMainLoop* loop)
{
569
    GASSERT(!engine->loop);
Slava Monich's avatar
Slava Monich committed
570 571
    engine->loop = loop;
    engine->stopped = FALSE;
572
    engine->stop_requested = FALSE;
Slava Monich's avatar
Slava Monich committed
573 574 575 576 577 578 579 580 581 582 583 584
    if (!mms_dispatcher_start(engine->dispatcher) && !engine->keep_running) {
        mms_engine_start_timeout_schedule(engine);
    }
    g_main_loop_run(loop);
    mms_engine_start_timeout_cancel(engine);
    engine->loop = NULL;
}

void
mms_engine_stop(
    MMSEngine* engine)
{
585 586 587 588 589 590
    if (mms_dispatcher_is_active(engine->dispatcher)) {
        engine->stop_requested = TRUE;
        mms_dispatcher_cancel(engine->dispatcher, NULL);
    } else {
        mms_engine_stop_schedule(engine);
    }
Slava Monich's avatar
Slava Monich committed
591 592 593 594 595 596 597 598
}

void
mms_engine_unregister(
    MMSEngine* engine)
{
    if (engine->engine_bus) {
        g_dbus_interface_skeleton_unexport(
Slava Monich's avatar
Slava Monich committed
599
            G_DBUS_INTERFACE_SKELETON(engine->proxy));
Slava Monich's avatar
Slava Monich committed
600 601 602 603 604 605 606 607 608 609 610 611 612
        g_object_unref(engine->engine_bus);
        engine->engine_bus = NULL;
    }
}

gboolean
mms_engine_register(
    MMSEngine* engine,
    GDBusConnection* bus,
    GError** error)
{
    mms_engine_unregister(engine);
    if (g_dbus_interface_skeleton_export(
Slava Monich's avatar
Slava Monich committed
613
        G_DBUS_INTERFACE_SKELETON(engine->proxy), bus,
Slava Monich's avatar
Slava Monich committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
        MMS_ENGINE_PATH, error)) {
        g_object_ref(engine->engine_bus = bus);
        return TRUE;
    } else {
        return FALSE;
    }
}

static
void
mms_engine_delegate_dispatcher_done(
    MMSDispatcherDelegate* delegate,
    MMSDispatcher* dispatcher)
{
    MMSEngine* engine = mms_engine_from_dispatcher_delegate(delegate);
629
    GDEBUG("All done");
630 631 632
    if (!engine->keep_running || engine->stop_requested) {
        mms_engine_stop_schedule(engine);
    }
Slava Monich's avatar
Slava Monich committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
}

/**
 * Per object initializer
 *
 * Only sets up internal state (all values set to zero)
 */
static
void
mms_engine_init(
    MMSEngine* engine)
{
    engine->dispatcher_delegate.fn_done = mms_engine_delegate_dispatcher_done;
}

/**
 * First stage of deinitialization (release all references).
 * May be called more than once in the lifetime of the object.
 */
static
void
mms_engine_dispose(
    GObject* object)
{
Slava Monich's avatar
Slava Monich committed
657
    MMSEngine* mms = MMS_ENGINE(object);
658 659
    GVERBOSE_("%p", mms);
    GASSERT(!mms->loop);
Slava Monich's avatar
Slava Monich committed
660 661 662 663 664 665 666
    mms_engine_unregister(mms);
    mms_engine_start_timeout_cancel(mms);
    if (mms->proxy) {
        gutil_disconnect_handlers(mms->proxy, mms->proxy_signal_id,
            G_N_ELEMENTS(mms->proxy_signal_id));
        g_object_unref(mms->proxy);
        mms->proxy = NULL;
Slava Monich's avatar
Slava Monich committed
667
    }
Slava Monich's avatar
Slava Monich committed
668 669 670 671
    if (mms->dispatcher) {
        mms_dispatcher_set_delegate(mms->dispatcher, NULL);
        mms_dispatcher_unref(mms->dispatcher);
        mms->dispatcher = NULL;
Slava Monich's avatar
Slava Monich committed
672
    }
Slava Monich's avatar
Slava Monich committed
673 674 675
    if (mms->settings) {
        mms_settings_unref(mms->settings);
        mms->settings = NULL;
676
    }
Slava Monich's avatar
Slava Monich committed
677 678 679
    if (mms->cm) {
        mms_connman_unref(mms->cm);
        mms->cm = NULL;
680
    }
Slava Monich's avatar
Slava Monich committed
681 682 683 684 685 686 687 688 689 690 691 692
    G_OBJECT_CLASS(mms_engine_parent_class)->dispose(object);
}

/**
 * Per class initializer
 */
static
void
mms_engine_class_init(
    MMSEngineClass* klass)
{
    GObjectClass* object_class = G_OBJECT_CLASS(klass);
693
    GASSERT(object_class);
Slava Monich's avatar
Slava Monich committed
694
    object_class->dispose = mms_engine_dispose;
695
    GVERBOSE_("done");
Slava Monich's avatar
Slava Monich committed
696 697 698 699 700 701 702 703 704
}

/*
 * Local Variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */