TextMessage.cpp
author Volker Birk <vb@pep-project.org>
Sat, 30 May 2015 19:36:36 +0200
changeset 32 b9e23540d7fd
parent 31 83a083fb4810
permissions -rw-r--r--
locking
vb@8
     1
// TextMessage.cpp : Implementation of CTextMessage
vb@8
     2
vb@8
     3
#include "stdafx.h"
vb@8
     4
#include "TextMessage.h"
vb@8
     5
vb@10
     6
using namespace pEp::utility;
vb@8
     7
vb@8
     8
// CTextMessage
vb@8
     9
vb@8
    10
STDMETHODIMP CTextMessage::InterfaceSupportsErrorInfo(REFIID riid)
vb@8
    11
{
vb@8
    12
	static const IID* const arr[] = 
vb@8
    13
	{
vb@8
    14
		&IID_ITextMessage
vb@8
    15
	};
vb@8
    16
vb@8
    17
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
vb@8
    18
	{
vb@8
    19
		if (InlineIsEqualGUID(*arr[i],riid))
vb@8
    20
			return S_OK;
vb@8
    21
	}
vb@8
    22
	return S_FALSE;
vb@8
    23
}
vb@8
    24
vb@8
    25
vb@8
    26
STDMETHODIMP CTextMessage::get_from(pEp_identity_s* pVal)
vb@8
    27
{
vb@13
    28
    assert(pVal);
vb@13
    29
vb@8
    30
    try {
vb@32
    31
        copy_identity(pVal, msg()->from);
vb@8
    32
    }
vb@22
    33
    catch (bad_alloc&) {
vb@8
    34
        return E_OUTOFMEMORY;
vb@8
    35
    }
vb@22
    36
    catch (exception&) {
vb@8
    37
        return E_FAIL;
vb@8
    38
    }
vb@8
    39
vb@8
    40
    return S_OK;
vb@8
    41
}
vb@8
    42
vb@8
    43
vb@8
    44
STDMETHODIMP CTextMessage::put_from(pEp_identity_s* newVal)
vb@8
    45
{
vb@13
    46
    assert(newVal);
vb@13
    47
vb@8
    48
    ::pEp_identity *_from;
vb@8
    49
    
vb@8
    50
    try {
vb@8
    51
        _from = new_identity(newVal);
vb@8
    52
    }
vb@22
    53
    catch (bad_alloc&) {
vb@8
    54
        return E_OUTOFMEMORY;
vb@8
    55
    }
vb@22
    56
    catch (exception&)
vb@8
    57
    {
vb@8
    58
        return E_FAIL;
vb@8
    59
    }
vb@8
    60
vb@32
    61
    ::free_identity(msg()->from);
vb@32
    62
    msg()->from = _from;
vb@8
    63
    return S_OK;
vb@8
    64
}
vb@13
    65
vb@31
    66
static HRESULT il_get(const identity_list *il, LPSAFEARRAY * sa)
vb@13
    67
{
vb@31
    68
    assert(sa);
vb@31
    69
    int len = identity_list_length(il);
vb@13
    70
vb@31
    71
    LPSAFEARRAY _sa = newSafeArray<pEp_identity_s>(len);
vb@31
    72
    if (_sa == NULL)
vb@13
    73
        return E_OUTOFMEMORY;
vb@13
    74
vb@31
    75
    const identity_list *_il;
vb@31
    76
    LONG i;
vb@31
    77
    for (i = 0, _il = il; _il && _il->ident; _il = _il->next, i++) {
vb@31
    78
        pEp_identity_s cs;
vb@31
    79
        
vb@31
    80
        try {
vb@31
    81
            copy_identity(&cs, _il->ident);
vb@13
    82
vb@31
    83
            ::SafeArrayPutElement(_sa, &i, &cs);
vb@31
    84
vb@31
    85
            IRecordInfo *ir = getRecordInfo<pEp_identity_s>();
vb@31
    86
            ir->RecordClear(&cs);
vb@13
    87
        }
vb@22
    88
        catch (bad_alloc&) {
vb@31
    89
            IRecordInfo *ir = getRecordInfo<pEp_identity_s>();
vb@31
    90
            ir->RecordClear(&cs);
vb@31
    91
            SafeArrayDestroy(_sa);
vb@13
    92
            return E_OUTOFMEMORY;
vb@13
    93
        }
vb@22
    94
        catch (exception&) {
vb@31
    95
            IRecordInfo *ir = getRecordInfo<pEp_identity_s>();
vb@31
    96
            ir->RecordClear(&cs);
vb@31
    97
            SafeArrayDestroy(_sa);
vb@13
    98
            return E_FAIL;
vb@13
    99
        }
vb@13
   100
    }
vb@13
   101
vb@31
   102
    *sa = _sa;
vb@13
   103
    return S_OK;
vb@13
   104
}
vb@13
   105
vb@31
   106
static HRESULT il_put(identity_list **target, SAFEARRAY * newVal)
vb@13
   107
{
vb@13
   108
    assert(newVal);
vb@13
   109
    if (newVal == NULL)
vb@13
   110
        return E_INVALIDARG;
vb@13
   111
vb@13
   112
    identity_list *il = new_identity_list(NULL);
vb@13
   113
    if (il == NULL)
vb@13
   114
        return E_OUTOFMEMORY;
vb@13
   115
vb@13
   116
    identity_list *_il;
vb@22
   117
    LONG lbound, ubound;
vb@23
   118
    LONG i;
vb@13
   119
    SafeArrayGetLBound(newVal, 1, &lbound);
vb@13
   120
    SafeArrayGetUBound(newVal, 1, &ubound);
vb@13
   121
vb@31
   122
    for (i = lbound, _il = il; i <= ubound; i++) {
vb@13
   123
        pEp_identity * ident;
vb@31
   124
        pEp_identity_s cs;
vb@31
   125
        memset(&cs, 0, sizeof(pEp_identity_s));
vb@31
   126
vb@13
   127
        try {
vb@31
   128
            HRESULT hr = ::SafeArrayGetElement(newVal, &i, &cs);
vb@31
   129
            if (hr != S_OK) {
vb@31
   130
                IRecordInfo *ri = getRecordInfo<pEp_identity_s>();
vb@31
   131
                ri->RecordClear(&cs);
vb@31
   132
                free_identity_list(il);
vb@31
   133
                return hr;
vb@31
   134
            }
vb@31
   135
            ident = new_identity(&cs);
vb@31
   136
vb@31
   137
            IRecordInfo *ri = getRecordInfo<pEp_identity_s>();
vb@31
   138
            ri->RecordClear(&cs);
vb@13
   139
        }
vb@22
   140
        catch (bad_alloc&) {
vb@31
   141
            IRecordInfo *ri = getRecordInfo<pEp_identity_s>();
vb@31
   142
            ri->RecordClear(&cs);
vb@13
   143
            free_identity_list(il);
vb@13
   144
            return E_OUTOFMEMORY;
vb@13
   145
        }
vb@22
   146
        catch (exception&) {
vb@31
   147
            IRecordInfo *ri = getRecordInfo<pEp_identity_s>();
vb@31
   148
            ri->RecordClear(&cs);
vb@13
   149
            free_identity_list(il);
vb@13
   150
            return E_FAIL;
vb@13
   151
        }
vb@13
   152
        _il = identity_list_add(_il, ident);
vb@13
   153
        if (_il == NULL) {
vb@13
   154
            free_identity_list(il);
vb@13
   155
            return E_OUTOFMEMORY;
vb@13
   156
        }
vb@13
   157
    }
vb@13
   158
vb@31
   159
    free_identity_list(*target);
vb@31
   160
    *target = il;
vb@13
   161
vb@13
   162
    return S_OK;
vb@13
   163
}
vb@15
   164
vb@31
   165
STDMETHODIMP CTextMessage::get_to(LPSAFEARRAY * pVal)
vb@31
   166
{
vb@32
   167
    return il_get(msg()->to, pVal);
vb@31
   168
}
vb@31
   169
vb@31
   170
STDMETHODIMP CTextMessage::put_to(SAFEARRAY * newVal)
vb@31
   171
{
vb@32
   172
    return il_put(&msg()->to, newVal);
vb@31
   173
}
vb@31
   174
vb@15
   175
STDMETHODIMP CTextMessage::get_recv_by(pEp_identity_s* pVal)
vb@15
   176
{
vb@15
   177
    assert(pVal);
vb@15
   178
vb@15
   179
    try {
vb@32
   180
        copy_identity(pVal, msg()->recv_by);
vb@15
   181
    }
vb@22
   182
    catch (bad_alloc&) {
vb@15
   183
        return E_OUTOFMEMORY;
vb@15
   184
    }
vb@22
   185
    catch (exception&) {
vb@15
   186
        return E_FAIL;
vb@15
   187
    }
vb@15
   188
vb@15
   189
    return S_OK;
vb@15
   190
}
vb@15
   191
vb@15
   192
vb@15
   193
STDMETHODIMP CTextMessage::put_recv_by(pEp_identity_s* newVal)
vb@15
   194
{
vb@15
   195
    assert(newVal);
vb@15
   196
vb@15
   197
    ::pEp_identity *_recv_by;
vb@15
   198
vb@15
   199
    try {
vb@15
   200
        _recv_by = new_identity(newVal);
vb@15
   201
    }
vb@22
   202
    catch (bad_alloc&) {
vb@15
   203
        return E_OUTOFMEMORY;
vb@15
   204
    }
vb@22
   205
    catch (exception&) {
vb@15
   206
        return E_FAIL;
vb@15
   207
    }
vb@15
   208
vb@32
   209
    ::free_identity(msg()->recv_by);
vb@32
   210
    msg()->recv_by = _recv_by;
vb@15
   211
    return S_OK;
vb@15
   212
}
vb@15
   213
vb@15
   214
STDMETHODIMP CTextMessage::get_cc(LPSAFEARRAY * pVal)
vb@15
   215
{
vb@32
   216
    return il_get(msg()->cc, pVal);
vb@15
   217
}
vb@15
   218
vb@15
   219
STDMETHODIMP CTextMessage::put_cc(SAFEARRAY * newVal)
vb@15
   220
{
vb@32
   221
    return il_put(&msg()->cc, newVal);
vb@15
   222
}
vb@15
   223
vb@15
   224
STDMETHODIMP CTextMessage::get_bcc(LPSAFEARRAY * pVal)
vb@15
   225
{
vb@32
   226
    return il_get(msg()->bcc, pVal);
vb@15
   227
}
vb@15
   228
vb@15
   229
STDMETHODIMP CTextMessage::put_bcc(SAFEARRAY * newVal)
vb@15
   230
{
vb@32
   231
    return il_put(&msg()->bcc, newVal);
vb@15
   232
}
vb@15
   233
vb@15
   234
STDMETHODIMP CTextMessage::get_reply_to(LPSAFEARRAY * pVal)
vb@15
   235
{
vb@32
   236
    return il_get(msg()->reply_to, pVal);
vb@15
   237
}
vb@15
   238
vb@15
   239
STDMETHODIMP CTextMessage::put_reply_to(SAFEARRAY * newVal)
vb@15
   240
{
vb@32
   241
    return il_put(&msg()->reply_to, newVal);
vb@15
   242
}
vb@15
   243
vb@15
   244
STDMETHODIMP CTextMessage::get_dir(pEp_msg_direction *pVal)
vb@15
   245
{
vb@15
   246
    assert(pVal);
vb@15
   247
vb@32
   248
    *pVal = (pEp_msg_direction) msg()->dir;
vb@15
   249
vb@15
   250
    return S_OK;
vb@15
   251
}
vb@15
   252
vb@15
   253
STDMETHODIMP CTextMessage::put_dir(pEp_msg_direction newVal)
vb@15
   254
{
vb@32
   255
    msg()->dir = (PEP_msg_direction) newVal;
vb@15
   256
vb@15
   257
    return S_OK;
vb@15
   258
}
vb@15
   259
vb@15
   260
STDMETHODIMP CTextMessage::get_id(BSTR * pVal)
vb@15
   261
{
vb@15
   262
    assert(pVal);
vb@15
   263
vb@15
   264
    _bstr_t val;
vb@15
   265
vb@32
   266
    if (msg()->id) {
vb@15
   267
        try {
vb@32
   268
            val = utf16_bstr(msg()->id);
vb@15
   269
        }
vb@22
   270
        catch (bad_alloc&) {
vb@15
   271
            return E_OUTOFMEMORY;
vb@15
   272
        }
vb@22
   273
        catch (exception&) {
vb@15
   274
            return E_FAIL;
vb@15
   275
        }
vb@21
   276
        *pVal = val.Detach();
vb@15
   277
    }
vb@15
   278
vb@15
   279
    return S_OK;
vb@15
   280
}
vb@15
   281
vb@15
   282
STDMETHODIMP CTextMessage::put_id(BSTR newVal)
vb@15
   283
{
vb@22
   284
    char *_val = NULL;
vb@15
   285
    string val;
vb@15
   286
vb@22
   287
    if (newVal) {
vb@22
   288
        try {
vb@22
   289
            val = utf8_string(newVal);
vb@22
   290
        }
vb@22
   291
        catch (bad_alloc&) {
vb@22
   292
            return E_OUTOFMEMORY;
vb@22
   293
        }
vb@22
   294
        catch (exception&) {
vb@22
   295
            return E_FAIL;
vb@22
   296
        }
vb@22
   297
vb@22
   298
        _val = _strdup(val.c_str());
vb@22
   299
        if (_val == NULL)
vb@22
   300
            return E_OUTOFMEMORY;
vb@15
   301
    }
vb@15
   302
vb@32
   303
    if (msg()->id)
vb@32
   304
        free(msg()->id);
vb@15
   305
vb@32
   306
    msg()->id = _val;
vb@15
   307
    return S_OK;
vb@15
   308
}
vb@15
   309
vb@15
   310
STDMETHODIMP CTextMessage::get_shortmsg(BSTR * pVal)
vb@15
   311
{
vb@15
   312
    assert(pVal);
vb@15
   313
vb@15
   314
    _bstr_t val;
vb@15
   315
vb@32
   316
    if (msg()->shortmsg) {
vb@15
   317
        try {
vb@32
   318
            val = utf16_bstr(msg()->shortmsg);
vb@15
   319
        }
vb@22
   320
        catch (bad_alloc&) {
vb@15
   321
            return E_OUTOFMEMORY;
vb@15
   322
        }
vb@22
   323
        catch (exception&) {
vb@15
   324
            return E_FAIL;
vb@15
   325
        }
vb@21
   326
        *pVal = val.Detach();
vb@15
   327
    }
vb@15
   328
vb@15
   329
    return S_OK;
vb@15
   330
}
vb@15
   331
vb@15
   332
STDMETHODIMP CTextMessage::put_shortmsg(BSTR newVal)
vb@15
   333
{
vb@22
   334
    char *_val = NULL;
vb@15
   335
vb@22
   336
    if (newVal) {
vb@22
   337
        string val;
vb@15
   338
vb@22
   339
        try {
vb@22
   340
            val = utf8_string(newVal);
vb@22
   341
        }
vb@22
   342
        catch (bad_alloc&) {
vb@22
   343
            return E_OUTOFMEMORY;
vb@22
   344
        }
vb@22
   345
        catch (exception&) {
vb@22
   346
            return E_FAIL;
vb@22
   347
        }
vb@22
   348
vb@22
   349
        _val = _strdup(val.c_str());
vb@22
   350
        if (_val == NULL)
vb@22
   351
            return E_OUTOFMEMORY;
vb@15
   352
    }
vb@15
   353
vb@32
   354
    if (msg()->shortmsg)
vb@32
   355
        free(msg()->shortmsg);
vb@15
   356
vb@32
   357
    msg()->shortmsg = _val;
vb@15
   358
    return S_OK;
vb@15
   359
}
vb@15
   360
vb@15
   361
STDMETHODIMP CTextMessage::get_longmsg(BSTR * pVal)
vb@15
   362
{
vb@15
   363
    assert(pVal);
vb@15
   364
vb@15
   365
    _bstr_t val;
vb@15
   366
vb@32
   367
    if (msg()->longmsg) {
vb@15
   368
        try {
vb@32
   369
            val = utf16_bstr(msg()->longmsg);
vb@15
   370
        }
vb@22
   371
        catch (bad_alloc&) {
vb@15
   372
            return E_OUTOFMEMORY;
vb@15
   373
        }
vb@22
   374
        catch (exception&) {
vb@15
   375
            return E_FAIL;
vb@15
   376
        }
vb@21
   377
        *pVal = val.Detach();
vb@15
   378
    }
vb@15
   379
vb@15
   380
    return S_OK;
vb@15
   381
}
vb@15
   382
vb@15
   383
STDMETHODIMP CTextMessage::put_longmsg(BSTR newVal)
vb@15
   384
{
vb@22
   385
    char * _val = NULL;
vb@15
   386
vb@22
   387
    if (newVal) {
vb@22
   388
        string val;
vb@15
   389
vb@22
   390
        try {
vb@22
   391
            val = utf8_string(newVal);
vb@22
   392
        }
vb@22
   393
        catch (bad_alloc&) {
vb@22
   394
            return E_OUTOFMEMORY;
vb@22
   395
        }
vb@22
   396
        catch (exception&) {
vb@22
   397
            return E_FAIL;
vb@22
   398
        }
vb@22
   399
vb@22
   400
        _val = _strdup(val.c_str());
vb@22
   401
        if (_val == NULL)
vb@22
   402
            return E_OUTOFMEMORY;
vb@15
   403
    }
vb@15
   404
vb@32
   405
    if (msg()->longmsg)
vb@32
   406
        free(msg()->longmsg);
vb@15
   407
vb@32
   408
    msg()->longmsg = _val;
vb@15
   409
    return S_OK;
vb@15
   410
}
vb@15
   411
vb@15
   412
STDMETHODIMP CTextMessage::get_longmsg_formatted(BSTR * pVal)
vb@15
   413
{
vb@15
   414
    assert(pVal);
vb@15
   415
vb@15
   416
    _bstr_t val;
vb@15
   417
vb@32
   418
    if (msg()->longmsg_formatted) {
vb@15
   419
        try {
vb@32
   420
            val = utf16_bstr(msg()->longmsg_formatted);
vb@15
   421
        }
vb@22
   422
        catch (bad_alloc&) {
vb@15
   423
            return E_OUTOFMEMORY;
vb@15
   424
        }
vb@22
   425
        catch (exception&) {
vb@15
   426
            return E_FAIL;
vb@15
   427
        }
vb@21
   428
        *pVal = val.Detach();
vb@15
   429
    }
vb@15
   430
vb@15
   431
    return S_OK;
vb@15
   432
}
vb@15
   433
vb@15
   434
STDMETHODIMP CTextMessage::put_longmsg_formatted(BSTR newVal)
vb@15
   435
{
vb@22
   436
    char *_val = NULL;
vb@15
   437
vb@22
   438
    if (newVal) {
vb@22
   439
        string val;
vb@15
   440
vb@22
   441
        try {
vb@22
   442
            val = utf8_string(newVal);
vb@22
   443
        }
vb@22
   444
        catch (bad_alloc&) {
vb@22
   445
            return E_OUTOFMEMORY;
vb@22
   446
        }
vb@22
   447
        catch (exception&) {
vb@22
   448
            return E_FAIL;
vb@22
   449
        }
vb@22
   450
vb@22
   451
        _val = _strdup(val.c_str());
vb@22
   452
        if (_val == NULL)
vb@22
   453
            return E_OUTOFMEMORY;
vb@15
   454
    }
vb@15
   455
vb@32
   456
    if (msg()->longmsg_formatted)
vb@32
   457
        free(msg()->longmsg_formatted);
vb@15
   458
vb@32
   459
    msg()->longmsg_formatted = _val;
vb@15
   460
    return S_OK;
vb@15
   461
}
vb@15
   462
vb@15
   463
STDMETHODIMP CTextMessage::add_attachment(SAFEARRAY * data, BSTR mime_type, BSTR filename)
vb@15
   464
{
vb@15
   465
    assert(data);
vb@15
   466
    if (data == NULL)
vb@15
   467
        return E_INVALIDARG;
vb@15
   468
vb@15
   469
    CComSafeArray<BYTE> sa_data(data);
vb@15
   470
    size_t len = sa_data.GetCount();
vb@15
   471
vb@22
   472
    char *_data = (char *) malloc(sa_data.GetCount() + 1);
vb@15
   473
    assert(_data);
vb@15
   474
    if (_data == NULL)
vb@15
   475
        return E_OUTOFMEMORY;
vb@15
   476
vb@15
   477
    memcpy(_data, sa_data.m_psa->pvData, len);
vb@22
   478
    _data[len] = 0;
vb@15
   479
    sa_data.Detach();
vb@15
   480
vb@15
   481
    string _mime_type;
vb@15
   482
    if (mime_type) {
vb@15
   483
        try {
vb@15
   484
            _mime_type = utf8_string(mime_type);
vb@15
   485
        }
vb@22
   486
        catch (bad_alloc&) {
vb@15
   487
            free(_data);
vb@15
   488
            return E_OUTOFMEMORY;
vb@15
   489
        }
vb@22
   490
        catch (exception&) {
vb@15
   491
            free(_data);
vb@15
   492
            return E_FAIL;
vb@15
   493
        }
vb@15
   494
    }
vb@15
   495
vb@15
   496
    string _filename;
vb@15
   497
    if (filename) {
vb@15
   498
        try {
vb@15
   499
            _filename = utf8_string(filename);
vb@15
   500
        }
vb@22
   501
        catch (bad_alloc&) {
vb@15
   502
            free(_data);
vb@15
   503
            return E_OUTOFMEMORY;
vb@15
   504
        }
vb@22
   505
        catch (exception&) {
vb@15
   506
            free(_data);
vb@15
   507
            return E_FAIL;
vb@15
   508
        }
vb@15
   509
    }
vb@15
   510
vb@32
   511
    bloblist_t *blob = bloblist_add(msg()->attachments, _data, len, _mime_type.c_str(), _filename.c_str());
vb@15
   512
    if (blob == NULL) {
vb@15
   513
        free(_data);
vb@15
   514
        return E_OUTOFMEMORY;
vb@15
   515
    }
vb@15
   516
vb@32
   517
    if (msg()->attachments == NULL)
vb@32
   518
        msg()->attachments = blob;
vb@20
   519
vb@15
   520
    return S_OK;
vb@15
   521
}
vb@15
   522
vb@15
   523
STDMETHODIMP CTextMessage::has_attachments(boolean *result)
vb@15
   524
{
vb@15
   525
    assert(result);
vb@15
   526
vb@32
   527
    if (msg()->attachments == NULL || msg()->attachments->data == NULL) {
vb@15
   528
        m_next_attachment = NULL;
vb@15
   529
        *result = false;
vb@15
   530
        return S_OK;
vb@15
   531
    }
vb@15
   532
vb@32
   533
    m_next_attachment = msg()->attachments;
vb@15
   534
    *result = true;
vb@15
   535
vb@15
   536
    return S_OK;
vb@15
   537
}
vb@15
   538
vb@15
   539
STDMETHODIMP CTextMessage::next_attachment(SAFEARRAY ** data, BSTR * mime_type, BSTR * filename, boolean *result)
vb@15
   540
{
vb@15
   541
    assert(data);
vb@15
   542
    assert(mime_type);
vb@15
   543
    assert(filename);
vb@15
   544
    assert(result);
vb@15
   545
vb@15
   546
    if (m_next_attachment == NULL)
vb@15
   547
        return E_ILLEGAL_METHOD_CALL;
vb@15
   548
vb@15
   549
    CComSafeArray<BYTE> sa;
vb@15
   550
    try {
vb@15
   551
        sa.Create(m_next_attachment->size);
vb@15
   552
    }
vb@22
   553
    catch (bad_alloc&) {
vb@15
   554
        return E_OUTOFMEMORY;
vb@15
   555
    }
vb@15
   556
vb@15
   557
    memcpy(sa.m_psa->pvData, m_next_attachment->data, m_next_attachment->size);
vb@15
   558
vb@15
   559
    _bstr_t _mime_type;
vb@15
   560
    if (m_next_attachment->mime_type) {
vb@15
   561
        try {
vb@15
   562
            _mime_type = utf16_bstr(m_next_attachment->mime_type);
vb@15
   563
        }
vb@22
   564
        catch (bad_alloc&) {
vb@15
   565
            return E_OUTOFMEMORY;
vb@15
   566
        }
vb@22
   567
        catch (exception&) {
vb@15
   568
            return E_FAIL;
vb@15
   569
        }
vb@15
   570
    }
vb@15
   571
vb@15
   572
    _bstr_t _filename;
vb@15
   573
    if (m_next_attachment->filename) {
vb@15
   574
        try {
vb@15
   575
            _filename = utf16_bstr(m_next_attachment->filename);
vb@15
   576
        }
vb@22
   577
        catch (bad_alloc&) {
vb@15
   578
            return E_OUTOFMEMORY;
vb@15
   579
        }
vb@22
   580
        catch (exception&) {
vb@15
   581
            return E_FAIL;
vb@15
   582
        }
vb@15
   583
    }
vb@15
   584
vb@15
   585
    *data = sa.Detach();
vb@15
   586
    *mime_type = _mime_type.Detach();
vb@15
   587
    *filename = _filename.Detach();
vb@15
   588
vb@15
   589
    m_next_attachment = m_next_attachment->next;
vb@15
   590
    *result = m_next_attachment != NULL;
vb@15
   591
vb@15
   592
    return S_OK;
vb@15
   593
}
vb@15
   594
vb@15
   595
STDMETHODIMP CTextMessage::get_sent(hyper * result)
vb@15
   596
{
vb@15
   597
    assert(result);
vb@15
   598
vb@32
   599
    if (msg()->sent == NULL)
vb@15
   600
        return E_NOT_SET;
vb@15
   601
vb@32
   602
    *result = mktime(msg()->sent);
vb@15
   603
    return S_OK;
vb@15
   604
}
vb@15
   605
vb@15
   606
STDMETHODIMP CTextMessage::put_sent(hyper val)
vb@15
   607
{
vb@15
   608
    ::timestamp * ts = new_timestamp(val);
vb@15
   609
    if (ts == NULL)
vb@15
   610
        return E_OUTOFMEMORY;
vb@15
   611
vb@32
   612
    if (msg()->sent)
vb@32
   613
        free_timestamp(msg()->sent);
vb@32
   614
    msg()->sent = ts;
vb@15
   615
vb@15
   616
    return S_OK;
vb@15
   617
}
vb@15
   618
vb@15
   619
STDMETHODIMP CTextMessage::get_recv(hyper * result)
vb@15
   620
{
vb@15
   621
    assert(result);
vb@15
   622
vb@32
   623
    if (msg()->recv == NULL)
vb@15
   624
        return E_NOT_SET;
vb@15
   625
vb@32
   626
    *result = mktime(msg()->recv);
vb@15
   627
    return S_OK;
vb@15
   628
}
vb@15
   629
vb@15
   630
STDMETHODIMP CTextMessage::put_recv(hyper val)
vb@15
   631
{
vb@15
   632
    ::timestamp * ts = new_timestamp(val);
vb@15
   633
    if (ts == NULL)
vb@15
   634
        return E_OUTOFMEMORY;
vb@15
   635
vb@32
   636
    if (msg()->recv)
vb@32
   637
        free_timestamp(msg()->recv);
vb@32
   638
    msg()->recv = ts;
vb@15
   639
vb@15
   640
    return S_OK;
vb@15
   641
}
vb@15
   642
vb@15
   643
STDMETHODIMP CTextMessage::get_references(SAFEARRAY ** pVal)
vb@15
   644
{
vb@15
   645
    assert(pVal);
vb@15
   646
vb@15
   647
    CComSafeArray<BSTR> sa;
vb@15
   648
    try {
vb@32
   649
        if (msg()->references) {
vb@32
   650
            sa = string_array(msg()->references);
vb@15
   651
        }
vb@15
   652
        else {
vb@15
   653
            sa.Create(0UL);
vb@15
   654
        }
vb@15
   655
    }
vb@22
   656
    catch (bad_alloc&) {
vb@15
   657
        return E_OUTOFMEMORY;
vb@15
   658
    }
vb@22
   659
    catch (exception&) {
vb@15
   660
        return E_FAIL;
vb@15
   661
    }
vb@15
   662
    
vb@15
   663
    *pVal = sa.Detach();
vb@15
   664
    return S_OK;
vb@15
   665
}
vb@15
   666
vb@15
   667
STDMETHODIMP CTextMessage::put_references(SAFEARRAY * newVal)
vb@15
   668
{
vb@15
   669
    assert(newVal);
vb@15
   670
vb@15
   671
    stringlist_t *sl;
vb@15
   672
    
vb@15
   673
    try {
vb@15
   674
        sl = new_stringlist(newVal);
vb@15
   675
    }
vb@22
   676
    catch (bad_alloc&) {
vb@15
   677
        return E_OUTOFMEMORY;
vb@15
   678
    }
vb@22
   679
    catch (exception&) {
vb@15
   680
        return E_FAIL;
vb@15
   681
    }
vb@15
   682
vb@32
   683
    if (msg()->references)
vb@32
   684
        free_stringlist(msg()->references);
vb@15
   685
vb@32
   686
    msg()->references = sl;
vb@15
   687
vb@15
   688
    return S_OK;
vb@15
   689
}
vb@15
   690
vb@15
   691
STDMETHODIMP CTextMessage::get_keywords(SAFEARRAY ** pVal)
vb@15
   692
{
vb@15
   693
    assert(pVal);
vb@15
   694
vb@15
   695
    CComSafeArray<BSTR> sa;
vb@15
   696
    try {
vb@32
   697
        if (msg()->keywords) {
vb@32
   698
            sa = string_array(msg()->keywords);
vb@15
   699
        }
vb@15
   700
        else {
vb@15
   701
            sa.Create(0UL);
vb@15
   702
        }
vb@15
   703
    }
vb@22
   704
    catch (bad_alloc&) {
vb@15
   705
        return E_OUTOFMEMORY;
vb@15
   706
    }
vb@22
   707
    catch (exception&) {
vb@15
   708
        return E_FAIL;
vb@15
   709
    }
vb@15
   710
vb@15
   711
    *pVal = sa.Detach();
vb@15
   712
    return S_OK;
vb@15
   713
}
vb@15
   714
vb@15
   715
STDMETHODIMP CTextMessage::put_keywords(SAFEARRAY * newVal)
vb@15
   716
{
vb@15
   717
    assert(newVal);
vb@15
   718
vb@15
   719
    stringlist_t *sl;
vb@15
   720
vb@15
   721
    try {
vb@15
   722
        sl = new_stringlist(newVal);
vb@15
   723
    }
vb@22
   724
    catch (bad_alloc&) {
vb@15
   725
        return E_OUTOFMEMORY;
vb@15
   726
    }
vb@22
   727
    catch (exception&) {
vb@15
   728
        return E_FAIL;
vb@15
   729
    }
vb@15
   730
vb@32
   731
    if (msg()->keywords)
vb@32
   732
        free_stringlist(msg()->keywords);
vb@15
   733
vb@32
   734
    msg()->keywords = sl;
vb@15
   735
vb@15
   736
    return S_OK;
vb@15
   737
}
vb@15
   738
vb@15
   739
STDMETHODIMP CTextMessage::get_comments(BSTR * pVal)
vb@15
   740
{
vb@15
   741
    assert(pVal);
vb@15
   742
vb@15
   743
    _bstr_t val;
vb@15
   744
vb@32
   745
    if (msg()->comments) {
vb@15
   746
        try {
vb@32
   747
            val = utf16_bstr(msg()->comments);
vb@15
   748
        }
vb@22
   749
        catch (bad_alloc&) {
vb@15
   750
            return E_OUTOFMEMORY;
vb@15
   751
        }
vb@22
   752
        catch (exception&) {
vb@15
   753
            return E_FAIL;
vb@15
   754
        }
vb@21
   755
        *pVal = val.Detach();
vb@15
   756
    }
vb@15
   757
vb@15
   758
    return S_OK;
vb@15
   759
}
vb@15
   760
vb@15
   761
STDMETHODIMP CTextMessage::put_comments(BSTR newVal)
vb@15
   762
{
vb@22
   763
    char * _val = NULL;
vb@15
   764
vb@22
   765
    if (newVal) {
vb@22
   766
        string val;
vb@15
   767
vb@22
   768
        try {
vb@22
   769
            val = utf8_string(newVal);
vb@22
   770
        }
vb@22
   771
        catch (bad_alloc&) {
vb@22
   772
            return E_OUTOFMEMORY;
vb@22
   773
        }
vb@22
   774
        catch (exception&) {
vb@22
   775
            return E_FAIL;
vb@22
   776
        }
vb@22
   777
vb@22
   778
        _val = _strdup(val.c_str());
vb@22
   779
        if (_val == NULL)
vb@22
   780
            return E_OUTOFMEMORY;
vb@15
   781
    }
vb@15
   782
vb@32
   783
    if (msg()->comments)
vb@32
   784
        free(msg()->comments);
vb@15
   785
vb@32
   786
    msg()->comments = _val;
vb@22
   787
    return S_OK;
vb@22
   788
}
vb@22
   789
vb@22
   790
STDMETHODIMP CTextMessage::get_opt_fields(LPSAFEARRAY * pVal)
vb@22
   791
{
vb@22
   792
    assert(pVal);
vb@22
   793
vb@32
   794
    if (msg()->opt_fields == NULL)
vb@23
   795
        return S_OK;
vb@23
   796
vb@32
   797
    int len = stringpair_list_length(msg()->opt_fields);
vb@22
   798
vb@23
   799
    LPSAFEARRAY sa = newSafeArray<struct opt_field>(len);
vb@22
   800
    if (sa == NULL)
vb@15
   801
        return E_OUTOFMEMORY;
vb@15
   802
vb@22
   803
    stringpair_list_t *il;
vb@23
   804
    LONG i;
vb@32
   805
    for (i = 0, il = msg()->opt_fields; il && il->value; il = il->next, i++) {
vb@31
   806
        opt_field fld;
vb@31
   807
        memset(&fld, 0, sizeof(opt_field));
vb@31
   808
vb@22
   809
        try {
vb@31
   810
            fld.name = utf16_bstr(il->value->key).Detach();
vb@31
   811
            fld.value = utf16_bstr(il->value->value).Detach();
vb@31
   812
vb@31
   813
            ::SafeArrayPutElement(sa, &i, &fld);
vb@31
   814
vb@31
   815
            IRecordInfo *ir = getRecordInfo<opt_field>();
vb@31
   816
            ir->RecordClear(&fld);
vb@22
   817
        }
vb@22
   818
        catch (bad_alloc&) {
vb@31
   819
            IRecordInfo *ir = getRecordInfo<opt_field>();
vb@31
   820
            ir->RecordClear(&fld);
vb@22
   821
            SafeArrayDestroy(sa);
vb@22
   822
            return E_OUTOFMEMORY;
vb@22
   823
        }
vb@22
   824
        catch (exception&) {
vb@31
   825
            IRecordInfo *ir = getRecordInfo<opt_field>();
vb@31
   826
            ir->RecordClear(&fld);
vb@22
   827
            SafeArrayDestroy(sa);
vb@22
   828
            return E_FAIL;
vb@22
   829
        }
vb@22
   830
    }
vb@22
   831
vb@22
   832
    *pVal = sa;
vb@22
   833
    return S_OK;
vb@22
   834
}
vb@22
   835
vb@22
   836
STDMETHODIMP CTextMessage::put_opt_fields(SAFEARRAY * newVal)
vb@22
   837
{
vb@22
   838
    assert(newVal);
vb@22
   839
vb@22
   840
    if (newVal == NULL)
vb@22
   841
        return E_INVALIDARG;
vb@22
   842
vb@22
   843
    stringpair_list_t *il = new_stringpair_list(NULL);
vb@22
   844
    if (il == NULL)
vb@22
   845
        return E_OUTOFMEMORY;
vb@22
   846
vb@22
   847
    stringpair_list_t *_il;
vb@22
   848
    LONG lbound, ubound;
vb@23
   849
    LONG i;
vb@22
   850
    SafeArrayGetLBound(newVal, 1, &lbound);
vb@22
   851
    SafeArrayGetUBound(newVal, 1, &ubound);
vb@22
   852
vb@31
   853
    for (i = lbound, _il = il; i <= ubound; i++) {
vb@22
   854
        stringpair_t * pair;
vb@22
   855
        try {
vb@31
   856
            struct opt_field cs;
vb@31
   857
            memset(&cs, 0, sizeof(opt_field));
vb@31
   858
            HRESULT hr = ::SafeArrayGetElement(newVal, &i, &cs);
vb@31
   859
            pair = new_stringpair(utf8_string(cs.name).c_str(), utf8_string(cs.value).c_str());
vb@31
   860
            IRecordInfo *ri = getRecordInfo<opt_field>();
vb@31
   861
            ri->RecordClear(&cs);
vb@22
   862
        }
vb@22
   863
        catch (bad_alloc&) {
vb@22
   864
            free_stringpair_list(il);
vb@22
   865
            return E_OUTOFMEMORY;
vb@22
   866
        }
vb@22
   867
        catch (exception&) {
vb@22
   868
            free_stringpair_list(il);
vb@22
   869
            return E_FAIL;
vb@22
   870
        }
vb@22
   871
        _il = stringpair_list_add(_il, pair);
vb@22
   872
        if (_il == NULL) {
vb@22
   873
            free_stringpair_list(il);
vb@22
   874
            return E_OUTOFMEMORY;
vb@22
   875
        }
vb@22
   876
    }
vb@22
   877
vb@32
   878
    free_stringpair_list(msg()->opt_fields);
vb@32
   879
    msg()->opt_fields = il;
vb@22
   880
vb@15
   881
    return S_OK;
vb@15
   882
}