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
     1 // TextMessage.cpp : Implementation of CTextMessage
     2 
     3 #include "stdafx.h"
     4 #include "TextMessage.h"
     5 
     6 using namespace pEp::utility;
     7 
     8 // CTextMessage
     9 
    10 STDMETHODIMP CTextMessage::InterfaceSupportsErrorInfo(REFIID riid)
    11 {
    12 	static const IID* const arr[] = 
    13 	{
    14 		&IID_ITextMessage
    15 	};
    16 
    17 	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
    18 	{
    19 		if (InlineIsEqualGUID(*arr[i],riid))
    20 			return S_OK;
    21 	}
    22 	return S_FALSE;
    23 }
    24 
    25 
    26 STDMETHODIMP CTextMessage::get_from(pEp_identity_s* pVal)
    27 {
    28     assert(pVal);
    29 
    30     try {
    31         copy_identity(pVal, msg->from);
    32     }
    33     catch (bad_alloc& e) {
    34         return E_OUTOFMEMORY;
    35     }
    36     catch (exception& e) {
    37         return E_FAIL;
    38     }
    39 
    40     return S_OK;
    41 }
    42 
    43 
    44 STDMETHODIMP CTextMessage::put_from(pEp_identity_s* newVal)
    45 {
    46     assert(newVal);
    47 
    48     ::pEp_identity *_from;
    49     
    50     try {
    51         _from = new_identity(newVal);
    52     }
    53     catch (bad_alloc& e) {
    54         return E_OUTOFMEMORY;
    55     }
    56     catch (exception& e)
    57     {
    58         return E_FAIL;
    59     }
    60 
    61     ::free_identity(msg->from);
    62     msg->from = _from;
    63     return S_OK;
    64 }
    65 
    66 STDMETHODIMP CTextMessage::get_to(LPSAFEARRAY * pVal)
    67 {
    68     assert(pVal);
    69 
    70     int len = identity_list_length(msg->to);
    71 
    72     LPSAFEARRAY sa = newSafeArray<pEp_identity_s>(len);
    73     if (sa == NULL)
    74         return E_OUTOFMEMORY;
    75 
    76     pEp_identity_s *cs = accessData<pEp_identity_s>(sa);    
    77 
    78     identity_list *il = msg->to;
    79     ULONG i;
    80     for (i = 0, il = msg->to; il && il->ident; il = il->next, i++) {
    81         try {
    82             copy_identity(&cs[i], il->ident);
    83         }
    84         catch (bad_alloc& e) {
    85             SafeArrayUnaccessData(sa);
    86             SafeArrayDestroy(sa);
    87             return E_OUTOFMEMORY;
    88         }
    89         catch (exception& e) {
    90             SafeArrayUnaccessData(sa);
    91             SafeArrayDestroy(sa);
    92             return E_FAIL;
    93         }
    94     }
    95 
    96     SafeArrayUnaccessData(sa);
    97 
    98     *pVal = sa;
    99     return S_OK;
   100 }
   101 
   102 STDMETHODIMP CTextMessage::put_to(SAFEARRAY * newVal)
   103 {
   104     assert(newVal);
   105 
   106     if (newVal == NULL)
   107         return E_INVALIDARG;
   108 
   109     identity_list *il = new_identity_list(NULL);
   110     if (il == NULL)
   111         return E_OUTOFMEMORY;
   112 
   113     pEp_identity_s *cs;
   114     HRESULT hr = SafeArrayAccessData(newVal, (void **) &cs);
   115     assert(SUCCEEDED(hr) && cs);
   116     if (cs == NULL) {
   117         free_identity_list(il);
   118         return E_FAIL;
   119     }
   120 
   121     identity_list *_il;
   122     LONG i, lbound, ubound;
   123     SafeArrayGetLBound(newVal, 1, &lbound);
   124     SafeArrayGetUBound(newVal, 1, &ubound);
   125 
   126     for (i = lbound, _il = il; i <= ubound; i++) {
   127         pEp_identity * ident;
   128         try {
   129             ident = new_identity(&cs[i]);
   130         }
   131         catch (bad_alloc& e) {
   132             SafeArrayUnaccessData(newVal);
   133             free_identity_list(il);
   134             return E_OUTOFMEMORY;
   135         }
   136         catch (exception& e) {
   137             SafeArrayUnaccessData(newVal);
   138             free_identity_list(il);
   139             return E_FAIL;
   140         }
   141         _il = identity_list_add(_il, ident);
   142         if (_il == NULL) {
   143             SafeArrayUnaccessData(newVal);
   144             free_identity_list(il);
   145             return E_OUTOFMEMORY;
   146         }
   147     }
   148 
   149     SafeArrayUnaccessData(newVal);
   150     
   151     free_identity_list(msg->to);
   152     msg->to = il;
   153 
   154     return S_OK;
   155 }