TextMessage.cpp
author Volker Birk <vb@pep-project.org>
Wed, 29 Apr 2015 01:11:07 +0200
changeset 13 d83d94f11e62
parent 10 15fd256d4adb
child 15 b1c2c371379c
permissions -rw-r--r--
to
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@8
    31
        copy_identity(pVal, msg->from);
vb@8
    32
    }
vb@8
    33
    catch (bad_alloc& e) {
vb@8
    34
        return E_OUTOFMEMORY;
vb@8
    35
    }
vb@8
    36
    catch (exception& e) {
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@8
    53
    catch (bad_alloc& e) {
vb@8
    54
        return E_OUTOFMEMORY;
vb@8
    55
    }
vb@8
    56
    catch (exception& e)
vb@8
    57
    {
vb@8
    58
        return E_FAIL;
vb@8
    59
    }
vb@8
    60
vb@8
    61
    ::free_identity(msg->from);
vb@8
    62
    msg->from = _from;
vb@8
    63
    return S_OK;
vb@8
    64
}
vb@13
    65
vb@13
    66
STDMETHODIMP CTextMessage::get_to(LPSAFEARRAY * pVal)
vb@13
    67
{
vb@13
    68
    assert(pVal);
vb@13
    69
vb@13
    70
    int len = identity_list_length(msg->to);
vb@13
    71
vb@13
    72
    LPSAFEARRAY sa = newSafeArray<pEp_identity_s>(len);
vb@13
    73
    if (sa == NULL)
vb@13
    74
        return E_OUTOFMEMORY;
vb@13
    75
vb@13
    76
    pEp_identity_s *cs = accessData<pEp_identity_s>(sa);    
vb@13
    77
vb@13
    78
    identity_list *il = msg->to;
vb@13
    79
    ULONG i;
vb@13
    80
    for (i = 0, il = msg->to; il && il->ident; il = il->next, i++) {
vb@13
    81
        try {
vb@13
    82
            copy_identity(&cs[i], il->ident);
vb@13
    83
        }
vb@13
    84
        catch (bad_alloc& e) {
vb@13
    85
            SafeArrayUnaccessData(sa);
vb@13
    86
            SafeArrayDestroy(sa);
vb@13
    87
            return E_OUTOFMEMORY;
vb@13
    88
        }
vb@13
    89
        catch (exception& e) {
vb@13
    90
            SafeArrayUnaccessData(sa);
vb@13
    91
            SafeArrayDestroy(sa);
vb@13
    92
            return E_FAIL;
vb@13
    93
        }
vb@13
    94
    }
vb@13
    95
vb@13
    96
    SafeArrayUnaccessData(sa);
vb@13
    97
vb@13
    98
    *pVal = sa;
vb@13
    99
    return S_OK;
vb@13
   100
}
vb@13
   101
vb@13
   102
STDMETHODIMP CTextMessage::put_to(SAFEARRAY * newVal)
vb@13
   103
{
vb@13
   104
    assert(newVal);
vb@13
   105
vb@13
   106
    if (newVal == NULL)
vb@13
   107
        return E_INVALIDARG;
vb@13
   108
vb@13
   109
    identity_list *il = new_identity_list(NULL);
vb@13
   110
    if (il == NULL)
vb@13
   111
        return E_OUTOFMEMORY;
vb@13
   112
vb@13
   113
    pEp_identity_s *cs;
vb@13
   114
    HRESULT hr = SafeArrayAccessData(newVal, (void **) &cs);
vb@13
   115
    assert(SUCCEEDED(hr) && cs);
vb@13
   116
    if (cs == NULL) {
vb@13
   117
        free_identity_list(il);
vb@13
   118
        return E_FAIL;
vb@13
   119
    }
vb@13
   120
vb@13
   121
    identity_list *_il;
vb@13
   122
    LONG i, lbound, ubound;
vb@13
   123
    SafeArrayGetLBound(newVal, 1, &lbound);
vb@13
   124
    SafeArrayGetUBound(newVal, 1, &ubound);
vb@13
   125
vb@13
   126
    for (i = lbound, _il = il; i <= ubound; i++) {
vb@13
   127
        pEp_identity * ident;
vb@13
   128
        try {
vb@13
   129
            ident = new_identity(&cs[i]);
vb@13
   130
        }
vb@13
   131
        catch (bad_alloc& e) {
vb@13
   132
            SafeArrayUnaccessData(newVal);
vb@13
   133
            free_identity_list(il);
vb@13
   134
            return E_OUTOFMEMORY;
vb@13
   135
        }
vb@13
   136
        catch (exception& e) {
vb@13
   137
            SafeArrayUnaccessData(newVal);
vb@13
   138
            free_identity_list(il);
vb@13
   139
            return E_FAIL;
vb@13
   140
        }
vb@13
   141
        _il = identity_list_add(_il, ident);
vb@13
   142
        if (_il == NULL) {
vb@13
   143
            SafeArrayUnaccessData(newVal);
vb@13
   144
            free_identity_list(il);
vb@13
   145
            return E_OUTOFMEMORY;
vb@13
   146
        }
vb@13
   147
    }
vb@13
   148
vb@13
   149
    SafeArrayUnaccessData(newVal);
vb@13
   150
    
vb@13
   151
    free_identity_list(msg->to);
vb@13
   152
    msg->to = il;
vb@13
   153
vb@13
   154
    return S_OK;
vb@13
   155
}