summaryrefslogtreecommitdiff
path: root/src/mm-sms-list.c
blob: dea6fd5d53bce8ae50accf39f47f3f29d1b4c65c (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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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:
 *
 * Copyright (C) 2012 Google, Inc.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

#include <ModemManager.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>

#include "mm-iface-modem-messaging.h"
#include "mm-sms-list.h"
#include "mm-sms.h"
#include "mm-log.h"

G_DEFINE_TYPE (MMSmsList, mm_sms_list, G_TYPE_OBJECT);

enum {
    PROP_0,
    PROP_MODEM,
    PROP_LAST
};
static GParamSpec *properties[PROP_LAST];

enum {
    SIGNAL_ADDED,
    SIGNAL_DELETED,
    SIGNAL_LAST
};
static guint signals[SIGNAL_LAST];

struct _MMSmsListPrivate {
    /* The owner modem */
    MMBaseModem *modem;
    /* List of sms objects */
    GList *list;
};

/*****************************************************************************/

gboolean
mm_sms_list_has_local_multipart_reference (MMSmsList *self,
                                           const gchar *number,
                                           guint8 reference)
{
    GList *l;

    /* No one should look for multipart reference 0, which isn't valid */
    g_assert (reference != 0);

    for (l = self->priv->list; l; l = g_list_next (l)) {
        MMSms *sms = MM_SMS (l->data);

        if (mm_sms_is_multipart (sms) &&
            mm_gdbus_sms_get_pdu_type (MM_GDBUS_SMS (sms)) == MM_SMS_PDU_TYPE_SUBMIT &&
            mm_sms_get_storage (sms) != MM_SMS_STORAGE_UNKNOWN &&
            mm_sms_get_multipart_reference (sms) == reference &&
            g_str_equal (mm_gdbus_sms_get_number (MM_GDBUS_SMS (sms)), number)) {
            /* Yes, the SMS list has an SMS with the same destination number
             * and multipart reference */
            return TRUE;
        }
    }

    return FALSE;
}

/*****************************************************************************/

guint
mm_sms_list_get_count (MMSmsList *self)
{
    return g_list_length (self->priv->list);
}

GStrv
mm_sms_list_get_paths (MMSmsList *self)
{
    GStrv path_list = NULL;
    GList *l;
    guint i;

    path_list = g_new0 (gchar *,
                        1 + g_list_length (self->priv->list));

    for (i = 0, l = self->priv->list; l; l = g_list_next (l)) {
        const gchar *path;

        /* Don't try to add NULL paths (not yet exported SMS objects) */
        path = mm_sms_get_path (MM_SMS (l->data));
        if (path)
            path_list[i++] = g_strdup (path);
    }

    return path_list;
}

/*****************************************************************************/

typedef struct {
    MMSmsList *self;
    GSimpleAsyncResult *result;
    gchar *path;
} DeleteSmsContext;

static void
delete_sms_context_complete_and_free (DeleteSmsContext *ctx)
{
    g_simple_async_result_complete (ctx->result);
    g_object_unref (ctx->result);
    g_object_unref (ctx->self);
    g_free (ctx->path);
    g_free (ctx);
}

gboolean
mm_sms_list_delete_sms_finish (MMSmsList *self,
                               GAsyncResult *res,
                               GError **error)
{
    return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}

static guint
cmp_sms_by_path (MMSms *sms,
                 const gchar *path)
{
    return g_strcmp0 (mm_sms_get_path (sms), path);
}

static void
delete_ready (MMSms *sms,
              GAsyncResult *res,
              DeleteSmsContext *ctx)
{
    GError *error = NULL;
    GList *l;

    if (!mm_sms_delete_finish (sms, res, &error)) {
        /* We report the error */
        g_simple_async_result_take_error (ctx->result, error);
        delete_sms_context_complete_and_free (ctx);
        return;
    }

    /* The SMS was properly deleted, we now remove it from our list */
    l = g_list_find_custom (ctx->self->priv->list,
                            ctx->path,
                            (GCompareFunc)cmp_sms_by_path);
    if (l) {
        g_object_unref (MM_SMS (l->data));
        ctx->self->priv->list = g_list_delete_link (ctx->self->priv->list, l);
    }

    /* We don't need to unref the SMS any more, but we can use the
     * reference we got in the method, which is the one kept alive
     * during the async operation. */
    mm_sms_unexport (sms);

    g_signal_emit (ctx->self,
                   signals[SIGNAL_DELETED], 0,
                   ctx->path);

    g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
    delete_sms_context_complete_and_free (ctx);
}

void
mm_sms_list_delete_sms (MMSmsList *self,
                        const gchar *sms_path,
                        GAsyncReadyCallback callback,
                        gpointer user_data)
{
    DeleteSmsContext *ctx;
    GList *l;

    l = g_list_find_custom (self->priv->list,
                            (gpointer)sms_path,
                            (GCompareFunc)cmp_sms_by_path);
    if (!l) {
        g_simple_async_report_error_in_idle (G_OBJECT (self),
                                             callback,
                                             user_data,
                                             MM_CORE_ERROR,
                                             MM_CORE_ERROR_NOT_FOUND,
                                             "No SMS found with path '%s'",
                                             sms_path);
        return;
    }

    /* Delete all SMS parts */
    ctx = g_new0 (DeleteSmsContext, 1);
    ctx->self = g_object_ref (self);
    ctx->path = g_strdup (sms_path);
    ctx->result = g_simple_async_result_new (G_OBJECT (self),
                                             callback,
                                             user_data,
                                             mm_sms_list_delete_sms);

    mm_sms_delete (MM_SMS (l->data),
                   (GAsyncReadyCallback)delete_ready,
                   ctx);
}

/*****************************************************************************/

void
mm_sms_list_add_sms (MMSmsList *self,
                     MMSms *sms)
{
    self->priv->list = g_list_prepend (self->priv->list, g_object_ref (sms));
    g_signal_emit (self, signals[SIGNAL_ADDED], 0,
                   mm_sms_get_path (sms),
                   FALSE);
}

/*****************************************************************************/

static guint
cmp_sms_by_concat_reference (MMSms *sms,
                             gpointer user_data)
{
    if (!mm_sms_is_multipart (sms))
        return -1;

    return (GPOINTER_TO_UINT (user_data) - mm_sms_get_multipart_reference (sms));
}

typedef struct {
    guint part_index;
    MMSmsStorage storage;
} PartIndexAndStorage;

static guint
cmp_sms_by_part_index_and_storage (MMSms *sms,
                                   PartIndexAndStorage *ctx)
{
    return !(mm_sms_get_storage (sms) == ctx->storage &&
             mm_sms_has_part_index (sms, ctx->part_index));
}

static gboolean
take_singlepart (MMSmsList *self,
                 MMSmsPart *part,
                 MMSmsState state,
                 MMSmsStorage storage,
                 GError **error)
{
    MMSms *sms;

    sms = mm_sms_singlepart_new (self->priv->modem,
                                 state,
                                 storage,
                                 part,
                                 error);
    if (!sms)
        return FALSE;

    self->priv->list = g_list_prepend (self->priv->list, sms);
    g_signal_emit (self, signals[SIGNAL_ADDED], 0,
                   mm_sms_get_path (sms),
                   state == MM_SMS_STATE_RECEIVED);
    return TRUE;
}

static gboolean
take_multipart (MMSmsList *self,
                MMSmsPart *part,
                MMSmsState state,
                MMSmsStorage storage,
                GError **error)
{
    GList *l;
    MMSms *sms;
    guint concat_reference;

    concat_reference = mm_sms_part_get_concat_reference (part);
    l = g_list_find_custom (self->priv->list,
                            GUINT_TO_POINTER (concat_reference),
                            (GCompareFunc)cmp_sms_by_concat_reference);
    if (l)
        /* Try to take the part */
        return mm_sms_multipart_take_part (MM_SMS (l->data), part, error);

    /* Create new Multipart */
    sms = mm_sms_multipart_new (self->priv->modem,
                                state,
                                storage,
                                concat_reference,
                                mm_sms_part_get_concat_max (part),
                                part,
                                error);
    if (!sms)
        return FALSE;

    self->priv->list = g_list_prepend (self->priv->list, sms);
    g_signal_emit (self, signals[SIGNAL_ADDED], 0,
                   mm_sms_get_path (sms),
                   (state == MM_SMS_STATE_RECEIVED ||
                    state == MM_SMS_STATE_RECEIVING));

    return TRUE;
}

gboolean
mm_sms_list_has_part (MMSmsList *self,
                      MMSmsStorage storage,
                      guint index)
{
    PartIndexAndStorage ctx;

    if (storage == MM_SMS_STORAGE_UNKNOWN ||
        index == SMS_PART_INVALID_INDEX)
        return FALSE;

    ctx.part_index = index;
    ctx.storage = storage;

    return !!g_list_find_custom (self->priv->list,
                                 &ctx,
                                 (GCompareFunc)cmp_sms_by_part_index_and_storage);
}

gboolean
mm_sms_list_take_part (MMSmsList *self,
                       MMSmsPart *part,
                       MMSmsState state,
                       MMSmsStorage storage,
                       GError **error)
{
    PartIndexAndStorage ctx;

    ctx.part_index = mm_sms_part_get_index (part);
    ctx.storage = storage;

    /* Ensure we don't have already taken a part with the same index */
    if (mm_sms_list_has_part (self,
                              storage,
                              mm_sms_part_get_index (part))) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_FAILED,
                     "A part with index %u was already taken",
                     mm_sms_part_get_index (part));
        return FALSE;
    }

    /* Did we just get a part of a multi-part SMS? */
    if (mm_sms_part_should_concat (part)) {
        if (mm_sms_part_get_index (part) != SMS_PART_INVALID_INDEX)
            mm_dbg ("SMS part at '%s/%u' is from a multipart SMS (reference: '%u', sequence: '%u')",
                    mm_sms_storage_get_string (storage),
                    mm_sms_part_get_index (part),
                    mm_sms_part_get_concat_reference (part),
                    mm_sms_part_get_concat_sequence (part));
        else
            mm_dbg ("SMS part (not stored) is from a multipart SMS (reference: '%u', sequence: '%u')",
                    mm_sms_part_get_concat_reference (part),
                    mm_sms_part_get_concat_sequence (part));

        return take_multipart (self, part, state, storage, error);
    }

    /* Otherwise, we build a whole new single-part MMSms just from this part */
    if (mm_sms_part_get_index (part) != SMS_PART_INVALID_INDEX)
        mm_dbg ("SMS part at '%s/%u' is from a singlepart SMS",
                mm_sms_storage_get_string (storage),
                mm_sms_part_get_index (part));
    else
        mm_dbg ("SMS part (not stored) is from a singlepart SMS");
    return take_singlepart (self, part, state, storage, error);
}

/*****************************************************************************/

MMSmsList *
mm_sms_list_new (MMBaseModem *modem)
{
    /* Create the object */
    return g_object_new  (MM_TYPE_SMS_LIST,
                          MM_SMS_LIST_MODEM, modem,
                          NULL);
}

static void
set_property (GObject *object,
              guint prop_id,
              const GValue *value,
              GParamSpec *pspec)
{
    MMSmsList *self = MM_SMS_LIST (object);

    switch (prop_id) {
    case PROP_MODEM:
        g_clear_object (&self->priv->modem);
        self->priv->modem = g_value_dup_object (value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
get_property (GObject *object,
              guint prop_id,
              GValue *value,
              GParamSpec *pspec)
{
    MMSmsList *self = MM_SMS_LIST (object);

    switch (prop_id) {
    case PROP_MODEM:
        g_value_set_object (value, self->priv->modem);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
mm_sms_list_init (MMSmsList *self)
{
    /* Initialize private data */
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                              MM_TYPE_SMS_LIST,
                                              MMSmsListPrivate);
}

static void
dispose (GObject *object)
{
    MMSmsList *self = MM_SMS_LIST (object);

    g_clear_object (&self->priv->modem);
    g_list_free_full (self->priv->list, (GDestroyNotify)g_object_unref);

    G_OBJECT_CLASS (mm_sms_list_parent_class)->dispose (object);
}

static void
mm_sms_list_class_init (MMSmsListClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMSmsListPrivate));

    /* Virtual methods */
    object_class->get_property = get_property;
    object_class->set_property = set_property;
    object_class->dispose = dispose;

    /* Properties */
    properties[PROP_MODEM] =
        g_param_spec_object (MM_SMS_LIST_MODEM,
                             "Modem",
                             "The Modem which owns this SMS list",
                             MM_TYPE_BASE_MODEM,
                             G_PARAM_READWRITE);
    g_object_class_install_property (object_class, PROP_MODEM, properties[PROP_MODEM]);

    /* Signals */
    signals[SIGNAL_ADDED] =
        g_signal_new (MM_SMS_ADDED,
                      G_OBJECT_CLASS_TYPE (object_class),
                      G_SIGNAL_RUN_FIRST,
                      G_STRUCT_OFFSET (MMSmsListClass, sms_added),
                      NULL, NULL,
                      g_cclosure_marshal_generic,
                      G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_BOOLEAN);

    signals[SIGNAL_DELETED] =
        g_signal_new (MM_SMS_DELETED,
                      G_OBJECT_CLASS_TYPE (object_class),
                      G_SIGNAL_RUN_FIRST,
                      G_STRUCT_OFFSET (MMSmsListClass, sms_deleted),
                      NULL, NULL,
                      g_cclosure_marshal_generic,
                      G_TYPE_NONE, 1, G_TYPE_STRING);
}