TextMessage.cpp
author Volker Birk <vb@pep-project.org>
Fri, 01 May 2015 00:24:12 +0200
changeset 15 b1c2c371379c
parent 13 d83d94f11e62
child 20 f77dd13806ff
permissions -rw-r--r--
implementing Message API
     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 }
   156 
   157 STDMETHODIMP CTextMessage::get_recv_by(pEp_identity_s* pVal)
   158 {
   159     assert(pVal);
   160 
   161     try {
   162         copy_identity(pVal, msg->recv_by);
   163     }
   164     catch (bad_alloc& e) {
   165         return E_OUTOFMEMORY;
   166     }
   167     catch (exception& e) {
   168         return E_FAIL;
   169     }
   170 
   171     return S_OK;
   172 }
   173 
   174 
   175 STDMETHODIMP CTextMessage::put_recv_by(pEp_identity_s* newVal)
   176 {
   177     assert(newVal);
   178 
   179     ::pEp_identity *_recv_by;
   180 
   181     try {
   182         _recv_by = new_identity(newVal);
   183     }
   184     catch (bad_alloc& e) {
   185         return E_OUTOFMEMORY;
   186     }
   187     catch (exception& e) {
   188         return E_FAIL;
   189     }
   190 
   191     ::free_identity(msg->recv_by);
   192     msg->recv_by = _recv_by;
   193     return S_OK;
   194 }
   195 
   196 STDMETHODIMP CTextMessage::get_cc(LPSAFEARRAY * pVal)
   197 {
   198     assert(pVal);
   199 
   200     int len = identity_list_length(msg->cc);
   201 
   202     LPSAFEARRAY sa = newSafeArray<pEp_identity_s>(len);
   203     if (sa == NULL)
   204         return E_OUTOFMEMORY;
   205 
   206     pEp_identity_s *cs = accessData<pEp_identity_s>(sa);
   207 
   208     identity_list *il = msg->cc;
   209     ULONG i;
   210     for (i = 0, il = msg->cc; il && il->ident; il = il->next, i++) {
   211         try {
   212             copy_identity(&cs[i], il->ident);
   213         }
   214         catch (bad_alloc& e) {
   215             SafeArrayUnaccessData(sa);
   216             SafeArrayDestroy(sa);
   217             return E_OUTOFMEMORY;
   218         }
   219         catch (exception& e) {
   220             SafeArrayUnaccessData(sa);
   221             SafeArrayDestroy(sa);
   222             return E_FAIL;
   223         }
   224     }
   225 
   226     SafeArrayUnaccessData(sa);
   227 
   228     *pVal = sa;
   229     return S_OK;
   230 }
   231 
   232 STDMETHODIMP CTextMessage::put_cc(SAFEARRAY * newVal)
   233 {
   234     assert(newVal);
   235 
   236     if (newVal == NULL)
   237         return E_INVALIDARG;
   238 
   239     identity_list *il = new_identity_list(NULL);
   240     if (il == NULL)
   241         return E_OUTOFMEMORY;
   242 
   243     pEp_identity_s *cs;
   244     HRESULT hr = SafeArrayAccessData(newVal, (void **) &cs);
   245     assert(SUCCEEDED(hr) && cs);
   246     if (cs == NULL) {
   247         free_identity_list(il);
   248         return E_FAIL;
   249     }
   250 
   251     identity_list *_il;
   252     LONG i, lbound, ubound;
   253     SafeArrayGetLBound(newVal, 1, &lbound);
   254     SafeArrayGetUBound(newVal, 1, &ubound);
   255 
   256     for (i = lbound, _il = il; i <= ubound; i++) {
   257         pEp_identity * ident;
   258         try {
   259             ident = new_identity(&cs[i]);
   260         }
   261         catch (bad_alloc& e) {
   262             SafeArrayUnaccessData(newVal);
   263             free_identity_list(il);
   264             return E_OUTOFMEMORY;
   265         }
   266         catch (exception& e) {
   267             SafeArrayUnaccessData(newVal);
   268             free_identity_list(il);
   269             return E_FAIL;
   270         }
   271         _il = identity_list_add(_il, ident);
   272         if (_il == NULL) {
   273             SafeArrayUnaccessData(newVal);
   274             free_identity_list(il);
   275             return E_OUTOFMEMORY;
   276         }
   277     }
   278 
   279     SafeArrayUnaccessData(newVal);
   280 
   281     free_identity_list(msg->cc);
   282     msg->cc = il;
   283 
   284     return S_OK;
   285 }
   286 
   287 STDMETHODIMP CTextMessage::get_bcc(LPSAFEARRAY * pVal)
   288 {
   289     assert(pVal);
   290 
   291     int len = identity_list_length(msg->bcc);
   292 
   293     LPSAFEARRAY sa = newSafeArray<pEp_identity_s>(len);
   294     if (sa == NULL)
   295         return E_OUTOFMEMORY;
   296 
   297     pEp_identity_s *cs = accessData<pEp_identity_s>(sa);
   298 
   299     identity_list *il = msg->bcc;
   300     ULONG i;
   301     for (i = 0, il = msg->bcc; il && il->ident; il = il->next, i++) {
   302         try {
   303             copy_identity(&cs[i], il->ident);
   304         }
   305         catch (bad_alloc& e) {
   306             SafeArrayUnaccessData(sa);
   307             SafeArrayDestroy(sa);
   308             return E_OUTOFMEMORY;
   309         }
   310         catch (exception& e) {
   311             SafeArrayUnaccessData(sa);
   312             SafeArrayDestroy(sa);
   313             return E_FAIL;
   314         }
   315     }
   316 
   317     SafeArrayUnaccessData(sa);
   318 
   319     *pVal = sa;
   320     return S_OK;
   321 }
   322 
   323 STDMETHODIMP CTextMessage::put_bcc(SAFEARRAY * newVal)
   324 {
   325     assert(newVal);
   326 
   327     if (newVal == NULL)
   328         return E_INVALIDARG;
   329 
   330     identity_list *il = new_identity_list(NULL);
   331     if (il == NULL)
   332         return E_OUTOFMEMORY;
   333 
   334     pEp_identity_s *cs;
   335     HRESULT hr = SafeArrayAccessData(newVal, (void **) &cs);
   336     assert(SUCCEEDED(hr) && cs);
   337     if (cs == NULL) {
   338         free_identity_list(il);
   339         return E_FAIL;
   340     }
   341 
   342     identity_list *_il;
   343     LONG i, lbound, ubound;
   344     SafeArrayGetLBound(newVal, 1, &lbound);
   345     SafeArrayGetUBound(newVal, 1, &ubound);
   346 
   347     for (i = lbound, _il = il; i <= ubound; i++) {
   348         pEp_identity * ident;
   349         try {
   350             ident = new_identity(&cs[i]);
   351         }
   352         catch (bad_alloc& e) {
   353             SafeArrayUnaccessData(newVal);
   354             free_identity_list(il);
   355             return E_OUTOFMEMORY;
   356         }
   357         catch (exception& e) {
   358             SafeArrayUnaccessData(newVal);
   359             free_identity_list(il);
   360             return E_FAIL;
   361         }
   362         _il = identity_list_add(_il, ident);
   363         if (_il == NULL) {
   364             SafeArrayUnaccessData(newVal);
   365             free_identity_list(il);
   366             return E_OUTOFMEMORY;
   367         }
   368     }
   369 
   370     SafeArrayUnaccessData(newVal);
   371 
   372     free_identity_list(msg->bcc);
   373     msg->bcc = il;
   374 
   375     return S_OK;
   376 }
   377 
   378 STDMETHODIMP CTextMessage::get_reply_to(LPSAFEARRAY * pVal)
   379 {
   380     assert(pVal);
   381 
   382     int len = identity_list_length(msg->reply_to);
   383 
   384     LPSAFEARRAY sa = newSafeArray<pEp_identity_s>(len);
   385     if (sa == NULL)
   386         return E_OUTOFMEMORY;
   387 
   388     pEp_identity_s *cs = accessData<pEp_identity_s>(sa);
   389 
   390     identity_list *il = msg->reply_to;
   391     ULONG i;
   392     for (i = 0, il = msg->reply_to; il && il->ident; il = il->next, i++) {
   393         try {
   394             copy_identity(&cs[i], il->ident);
   395         }
   396         catch (bad_alloc& e) {
   397             SafeArrayUnaccessData(sa);
   398             SafeArrayDestroy(sa);
   399             return E_OUTOFMEMORY;
   400         }
   401         catch (exception& e) {
   402             SafeArrayUnaccessData(sa);
   403             SafeArrayDestroy(sa);
   404             return E_FAIL;
   405         }
   406     }
   407 
   408     SafeArrayUnaccessData(sa);
   409 
   410     *pVal = sa;
   411     return S_OK;
   412 }
   413 
   414 STDMETHODIMP CTextMessage::put_reply_to(SAFEARRAY * newVal)
   415 {
   416     assert(newVal);
   417 
   418     if (newVal == NULL)
   419         return E_INVALIDARG;
   420 
   421     identity_list *il = new_identity_list(NULL);
   422     if (il == NULL)
   423         return E_OUTOFMEMORY;
   424 
   425     pEp_identity_s *cs;
   426     HRESULT hr = SafeArrayAccessData(newVal, (void **) &cs);
   427     assert(SUCCEEDED(hr) && cs);
   428     if (cs == NULL) {
   429         free_identity_list(il);
   430         return E_FAIL;
   431     }
   432 
   433     identity_list *_il;
   434     LONG i, lbound, ubound;
   435     SafeArrayGetLBound(newVal, 1, &lbound);
   436     SafeArrayGetUBound(newVal, 1, &ubound);
   437 
   438     for (i = lbound, _il = il; i <= ubound; i++) {
   439         pEp_identity * ident;
   440         try {
   441             ident = new_identity(&cs[i]);
   442         }
   443         catch (bad_alloc& e) {
   444             SafeArrayUnaccessData(newVal);
   445             free_identity_list(il);
   446             return E_OUTOFMEMORY;
   447         }
   448         catch (exception& e) {
   449             SafeArrayUnaccessData(newVal);
   450             free_identity_list(il);
   451             return E_FAIL;
   452         }
   453         _il = identity_list_add(_il, ident);
   454         if (_il == NULL) {
   455             SafeArrayUnaccessData(newVal);
   456             free_identity_list(il);
   457             return E_OUTOFMEMORY;
   458         }
   459     }
   460 
   461     SafeArrayUnaccessData(newVal);
   462 
   463     free_identity_list(msg->reply_to);
   464     msg->reply_to = il;
   465 
   466     return S_OK;
   467 }
   468 
   469 STDMETHODIMP CTextMessage::get_dir(pEp_msg_direction *pVal)
   470 {
   471     assert(pVal);
   472 
   473     *pVal = (pEp_msg_direction) msg->dir;
   474 
   475     return S_OK;
   476 }
   477 
   478 STDMETHODIMP CTextMessage::put_dir(pEp_msg_direction newVal)
   479 {
   480     msg->dir = (PEP_msg_direction) newVal;
   481 
   482     return S_OK;
   483 }
   484 
   485 STDMETHODIMP CTextMessage::get_id(BSTR * pVal)
   486 {
   487     assert(pVal);
   488 
   489     _bstr_t val;
   490 
   491     if (msg->id) {
   492         try {
   493             val = utf16_bstr(msg->id);
   494         }
   495         catch (bad_alloc& e) {
   496             return E_OUTOFMEMORY;
   497         }
   498         catch (exception& e) {
   499             return E_FAIL;
   500         }
   501     }
   502 
   503     *pVal = val.Detach();
   504 
   505     return S_OK;
   506 }
   507 
   508 STDMETHODIMP CTextMessage::put_id(BSTR newVal)
   509 {
   510     assert(newVal);
   511 
   512     string val;
   513 
   514     try {
   515         val = utf8_string(newVal);
   516     }
   517     catch (bad_alloc& e) {
   518         return E_OUTOFMEMORY;
   519     }
   520     catch (exception& e) {
   521         return E_FAIL;
   522     }
   523 
   524     if (msg->id)
   525         free(msg->id);
   526 
   527     msg->id = _strdup(val.c_str());
   528     if (msg->id == NULL)
   529         return E_OUTOFMEMORY;
   530 
   531     return S_OK;
   532 }
   533 
   534 STDMETHODIMP CTextMessage::get_shortmsg(BSTR * pVal)
   535 {
   536     assert(pVal);
   537 
   538     _bstr_t val;
   539 
   540     if (msg->shortmsg) {
   541         try {
   542             val = utf16_bstr(msg->shortmsg);
   543         }
   544         catch (bad_alloc& e) {
   545             return E_OUTOFMEMORY;
   546         }
   547         catch (exception& e) {
   548             return E_FAIL;
   549         }
   550     }
   551 
   552     *pVal = val.Detach();
   553 
   554     return S_OK;
   555 }
   556 
   557 STDMETHODIMP CTextMessage::put_shortmsg(BSTR newVal)
   558 {
   559     assert(newVal);
   560 
   561     string val;
   562 
   563     try {
   564         val = utf8_string(newVal);
   565     }
   566     catch (bad_alloc& e) {
   567         return E_OUTOFMEMORY;
   568     }
   569     catch (exception& e) {
   570         return E_FAIL;
   571     }
   572 
   573     if (msg->shortmsg)
   574         free(msg->shortmsg);
   575 
   576     msg->shortmsg = _strdup(val.c_str());
   577     if (msg->shortmsg == NULL)
   578         return E_OUTOFMEMORY;
   579 
   580     return S_OK;
   581 }
   582 
   583 STDMETHODIMP CTextMessage::get_longmsg(BSTR * pVal)
   584 {
   585     assert(pVal);
   586 
   587     _bstr_t val;
   588 
   589     if (msg->longmsg) {
   590         try {
   591             val = utf16_bstr(msg->longmsg);
   592         }
   593         catch (bad_alloc& e) {
   594             return E_OUTOFMEMORY;
   595         }
   596         catch (exception& e) {
   597             return E_FAIL;
   598         }
   599     }
   600 
   601     *pVal = val.Detach();
   602 
   603     return S_OK;
   604 }
   605 
   606 STDMETHODIMP CTextMessage::put_longmsg(BSTR newVal)
   607 {
   608     assert(newVal);
   609 
   610     string val;
   611 
   612     try {
   613         val = utf8_string(newVal);
   614     }
   615     catch (bad_alloc& e) {
   616         return E_OUTOFMEMORY;
   617     }
   618     catch (exception& e) {
   619         return E_FAIL;
   620     }
   621 
   622     if (msg->longmsg)
   623         free(msg->longmsg);
   624 
   625     msg->longmsg = _strdup(val.c_str());
   626     if (msg->longmsg == NULL)
   627         return E_OUTOFMEMORY;
   628 
   629     return S_OK;
   630 }
   631 
   632 STDMETHODIMP CTextMessage::get_longmsg_formatted(BSTR * pVal)
   633 {
   634     assert(pVal);
   635 
   636     _bstr_t val;
   637 
   638     if (msg->longmsg_formatted) {
   639         try {
   640             val = utf16_bstr(msg->longmsg_formatted);
   641         }
   642         catch (bad_alloc& e) {
   643             return E_OUTOFMEMORY;
   644         }
   645         catch (exception& e) {
   646             return E_FAIL;
   647         }
   648     }
   649 
   650     *pVal = val.Detach();
   651 
   652     return S_OK;
   653 }
   654 
   655 STDMETHODIMP CTextMessage::put_longmsg_formatted(BSTR newVal)
   656 {
   657     assert(newVal);
   658 
   659     string val;
   660 
   661     try {
   662         val = utf8_string(newVal);
   663     }
   664     catch (bad_alloc& e) {
   665         return E_OUTOFMEMORY;
   666     }
   667     catch (exception& e) {
   668         return E_FAIL;
   669     }
   670 
   671     if (msg->longmsg_formatted)
   672         free(msg->longmsg_formatted);
   673 
   674     msg->longmsg_formatted = _strdup(val.c_str());
   675     if (msg->longmsg_formatted == NULL)
   676         return E_OUTOFMEMORY;
   677 
   678     return S_OK;
   679 }
   680 
   681 STDMETHODIMP CTextMessage::add_attachment(SAFEARRAY * data, BSTR mime_type, BSTR filename)
   682 {
   683     assert(data);
   684     if (data == NULL)
   685         return E_INVALIDARG;
   686 
   687     CComSafeArray<BYTE> sa_data(data);
   688     size_t len = sa_data.GetCount();
   689 
   690     char *_data = (char *) malloc(sa_data.GetCount());
   691     assert(_data);
   692     if (_data == NULL)
   693         return E_OUTOFMEMORY;
   694 
   695     memcpy(_data, sa_data.m_psa->pvData, len);
   696     sa_data.Detach();
   697 
   698     string _mime_type;
   699     if (mime_type) {
   700         try {
   701             _mime_type = utf8_string(mime_type);
   702         }
   703         catch (bad_alloc& e) {
   704             free(_data);
   705             return E_OUTOFMEMORY;
   706         }
   707         catch (exception& e) {
   708             free(_data);
   709             return E_FAIL;
   710         }
   711     }
   712 
   713     string _filename;
   714     if (filename) {
   715         try {
   716             _filename = utf8_string(filename);
   717         }
   718         catch (bad_alloc& e) {
   719             free(_data);
   720             return E_OUTOFMEMORY;
   721         }
   722         catch (exception& e) {
   723             free(_data);
   724             return E_FAIL;
   725         }
   726     }
   727 
   728     bloblist_t *blob = bloblist_add(msg->attachments, _data, len, _mime_type.c_str(), _filename.c_str());
   729     if (blob == NULL) {
   730         free(_data);
   731         return E_OUTOFMEMORY;
   732     }
   733 
   734     return S_OK;
   735 }
   736 
   737 STDMETHODIMP CTextMessage::has_attachments(boolean *result)
   738 {
   739     assert(result);
   740 
   741     if (msg->attachments == NULL || msg->attachments->data == NULL) {
   742         m_next_attachment = NULL;
   743         *result = false;
   744         return S_OK;
   745     }
   746 
   747     m_next_attachment = msg->attachments;
   748     *result = true;
   749 
   750     return S_OK;
   751 }
   752 
   753 STDMETHODIMP CTextMessage::next_attachment(SAFEARRAY ** data, BSTR * mime_type, BSTR * filename, boolean *result)
   754 {
   755     assert(data);
   756     assert(mime_type);
   757     assert(filename);
   758     assert(result);
   759 
   760     if (m_next_attachment == NULL)
   761         return E_ILLEGAL_METHOD_CALL;
   762 
   763     CComSafeArray<BYTE> sa;
   764     try {
   765         sa.Create(m_next_attachment->size);
   766     }
   767     catch (bad_alloc& e) {
   768         return E_OUTOFMEMORY;
   769     }
   770 
   771     memcpy(sa.m_psa->pvData, m_next_attachment->data, m_next_attachment->size);
   772 
   773     _bstr_t _mime_type;
   774     if (m_next_attachment->mime_type) {
   775         try {
   776             _mime_type = utf16_bstr(m_next_attachment->mime_type);
   777         }
   778         catch (bad_alloc& e) {
   779             return E_OUTOFMEMORY;
   780         }
   781         catch (exception& e) {
   782             return E_FAIL;
   783         }
   784     }
   785 
   786     _bstr_t _filename;
   787     if (m_next_attachment->filename) {
   788         try {
   789             _filename = utf16_bstr(m_next_attachment->filename);
   790         }
   791         catch (bad_alloc& e) {
   792             return E_OUTOFMEMORY;
   793         }
   794         catch (exception& e) {
   795             return E_FAIL;
   796         }
   797     }
   798 
   799     *data = sa.Detach();
   800     *mime_type = _mime_type.Detach();
   801     *filename = _filename.Detach();
   802 
   803     m_next_attachment = m_next_attachment->next;
   804     *result = m_next_attachment != NULL;
   805 
   806     return S_OK;
   807 }
   808 
   809 STDMETHODIMP CTextMessage::get_sent(hyper * result)
   810 {
   811     assert(result);
   812 
   813     if (msg->sent == NULL)
   814         return E_NOT_SET;
   815 
   816     *result = mktime(msg->sent);
   817     return S_OK;
   818 }
   819 
   820 STDMETHODIMP CTextMessage::put_sent(hyper val)
   821 {
   822     ::timestamp * ts = new_timestamp(val);
   823     if (ts == NULL)
   824         return E_OUTOFMEMORY;
   825 
   826     if (msg->sent)
   827         free_timestamp(msg->sent);
   828     msg->sent = ts;
   829 
   830     return S_OK;
   831 }
   832 
   833 STDMETHODIMP CTextMessage::get_recv(hyper * result)
   834 {
   835     assert(result);
   836 
   837     if (msg->recv == NULL)
   838         return E_NOT_SET;
   839 
   840     *result = mktime(msg->recv);
   841     return S_OK;
   842 }
   843 
   844 STDMETHODIMP CTextMessage::put_recv(hyper val)
   845 {
   846     ::timestamp * ts = new_timestamp(val);
   847     if (ts == NULL)
   848         return E_OUTOFMEMORY;
   849 
   850     if (msg->recv)
   851         free_timestamp(msg->recv);
   852     msg->recv = ts;
   853 
   854     return S_OK;
   855 }
   856 
   857 STDMETHODIMP CTextMessage::get_references(SAFEARRAY ** pVal)
   858 {
   859     assert(pVal);
   860 
   861     CComSafeArray<BSTR> sa;
   862     try {
   863         if (msg->references) {
   864             sa = string_array(msg->references);
   865         }
   866         else {
   867             sa.Create(0UL);
   868         }
   869     }
   870     catch (bad_alloc& e) {
   871         return E_OUTOFMEMORY;
   872     }
   873     catch (exception& e) {
   874         return E_FAIL;
   875     }
   876     
   877     *pVal = sa.Detach();
   878     return S_OK;
   879 }
   880 
   881 STDMETHODIMP CTextMessage::put_references(SAFEARRAY * newVal)
   882 {
   883     assert(newVal);
   884 
   885     stringlist_t *sl;
   886     
   887     try {
   888         sl = new_stringlist(newVal);
   889     }
   890     catch (bad_alloc& e) {
   891         return E_OUTOFMEMORY;
   892     }
   893     catch (exception& e) {
   894         return E_FAIL;
   895     }
   896 
   897     if (msg->references)
   898         free_stringlist(msg->references);
   899 
   900     msg->references = sl;
   901 
   902     return S_OK;
   903 }
   904 
   905 STDMETHODIMP CTextMessage::get_keywords(SAFEARRAY ** pVal)
   906 {
   907     assert(pVal);
   908 
   909     CComSafeArray<BSTR> sa;
   910     try {
   911         if (msg->keywords) {
   912             sa = string_array(msg->keywords);
   913         }
   914         else {
   915             sa.Create(0UL);
   916         }
   917     }
   918     catch (bad_alloc& e) {
   919         return E_OUTOFMEMORY;
   920     }
   921     catch (exception& e) {
   922         return E_FAIL;
   923     }
   924 
   925     *pVal = sa.Detach();
   926     return S_OK;
   927 }
   928 
   929 STDMETHODIMP CTextMessage::put_keywords(SAFEARRAY * newVal)
   930 {
   931     assert(newVal);
   932 
   933     stringlist_t *sl;
   934 
   935     try {
   936         sl = new_stringlist(newVal);
   937     }
   938     catch (bad_alloc& e) {
   939         return E_OUTOFMEMORY;
   940     }
   941     catch (exception& e) {
   942         return E_FAIL;
   943     }
   944 
   945     if (msg->keywords)
   946         free_stringlist(msg->keywords);
   947 
   948     msg->keywords = sl;
   949 
   950     return S_OK;
   951 }
   952 
   953 STDMETHODIMP CTextMessage::get_comments(BSTR * pVal)
   954 {
   955     assert(pVal);
   956 
   957     _bstr_t val;
   958 
   959     if (msg->comments) {
   960         try {
   961             val = utf16_bstr(msg->comments);
   962         }
   963         catch (bad_alloc& e) {
   964             return E_OUTOFMEMORY;
   965         }
   966         catch (exception& e) {
   967             return E_FAIL;
   968         }
   969     }
   970 
   971     *pVal = val.Detach();
   972 
   973     return S_OK;
   974 }
   975 
   976 STDMETHODIMP CTextMessage::put_comments(BSTR newVal)
   977 {
   978     assert(newVal);
   979 
   980     string val;
   981 
   982     try {
   983         val = utf8_string(newVal);
   984     }
   985     catch (bad_alloc& e) {
   986         return E_OUTOFMEMORY;
   987     }
   988     catch (exception& e) {
   989         return E_FAIL;
   990     }
   991 
   992     if (msg->comments)
   993         free(msg->comments);
   994 
   995     msg->comments = _strdup(val.c_str());
   996     if (msg->comments == NULL)
   997         return E_OUTOFMEMORY;
   998 
   999     return S_OK;
  1000 }