pEp_utility.cpp
author Volker Birk <vb@pep.foundation>
Mon, 22 Oct 2018 10:14:50 +0200
branchsync
changeset 304 00916b0afaac
parent 302 b3444780fa9f
child 326 83b46d64c11d
permissions -rw-r--r--
start COM on sync thread
vb@10
     1
#include "stdafx.h"
vb@33
     2
#include "pEp_utility.h"
vb@33
     3
vb@33
     4
using namespace ATL;
vb@10
     5
vb@10
     6
namespace pEp {
vb@10
     7
    namespace utility {
vb@10
     8
        pEp_identity_cpp::pEp_identity_cpp(const ::pEp_identity *_ident)
vb@10
     9
        {
markus@269
    10
            if (!_ident)
markus@269
    11
                return;
vb@212
    12
vb@10
    13
            if (_ident->address)
vb@10
    14
                address = _ident->address;
vb@10
    15
            if (_ident->fpr)
vb@10
    16
                fpr = _ident->fpr;
vb@10
    17
            if (_ident->user_id)
vb@10
    18
                user_id = _ident->user_id;
vb@10
    19
            if (_ident->username)
vb@10
    20
                username = _ident->username;
markus@269
    21
            comm_type = (pEpComType)_ident->comm_type;
vb@10
    22
            lang = _ident->lang;
markus@269
    23
            flags = (int)_ident->flags;
vb@10
    24
        }
vb@10
    25
markus@172
    26
        pEp_identity_cpp::pEp_identity_cpp(const pEpIdentity *_ident)
vb@10
    27
        {
markus@269
    28
            if (!_ident)
markus@269
    29
                return;
vb@212
    30
markus@269
    31
            if (_ident->Address)
markus@180
    32
                address = utf8_string(_ident->Address);
markus@180
    33
            if (_ident->Fpr)
markus@180
    34
                fpr = utf8_string(_ident->Fpr);
markus@180
    35
            if (_ident->UserId)
markus@180
    36
                user_id = utf8_string(_ident->UserId);
markus@180
    37
            if (_ident->UserName)
markus@180
    38
                username = utf8_string(_ident->UserName);
markus@180
    39
            comm_type = _ident->CommType;
markus@180
    40
            if (_ident->Lang)
markus@180
    41
                lang = utf8_string(_ident->Lang);
markus@269
    42
            flags = (int)_ident->Flags;
vb@10
    43
        }
vb@10
    44
vb@10
    45
        pEp_identity * pEp_identity_cpp::to_pEp_identity()
vb@10
    46
        {
vb@10
    47
            ::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
vb@10
    48
            assert(_ident);
vb@10
    49
            if (_ident == NULL)
vb@10
    50
                throw bad_alloc();
vb@10
    51
vb@10
    52
            _ident->comm_type = (::PEP_comm_type) this->comm_type;
vb@10
    53
vb@10
    54
            assert(this->lang.size() == 0 || this->lang.size() == 2);
vb@10
    55
vb@10
    56
            if (this->lang.size()) {
vb@10
    57
                _ident->lang[0] = this->lang[0];
vb@10
    58
                _ident->lang[1] = this->lang[1];
vb@10
    59
            }
vb@10
    60
markus@269
    61
            _ident->flags = (identity_flags) this->flags;
vb@217
    62
vb@10
    63
            return _ident;
vb@10
    64
        }
vb@10
    65
markus@172
    66
        pEpIdentity * pEp_identity_cpp::to_pEp_identity_s()
vb@10
    67
        {
markus@269
    68
            pEpIdentity *_ident = (pEpIdentity *)calloc(1, sizeof(pEpIdentity));
vb@10
    69
            assert(_ident);
vb@10
    70
            if (_ident == NULL)
vb@10
    71
                throw bad_alloc();
vb@10
    72
markus@180
    73
            _ident->Address = utf16_bstr(this->address);
markus@180
    74
            _ident->CommType = this->comm_type;
markus@180
    75
            _ident->Fpr = utf16_bstr(this->fpr);
markus@180
    76
            _ident->Lang = utf16_bstr(this->lang);
markus@180
    77
            _ident->UserName = utf16_bstr(this->username);
markus@180
    78
            _ident->UserId = utf16_bstr(this->user_id);
markus@269
    79
            _ident->Flags = (pEpIdentityFlags) this->flags;
vb@10
    80
vb@10
    81
            return _ident;
vb@10
    82
        }
vb@10
    83
markus@172
    84
        void copy_identity(pEpIdentity * ident_s, const pEp_identity * ident)
vb@10
    85
        {
vb@10
    86
            assert(ident_s);
markus@269
    87
            if (!ident_s)
markus@269
    88
                throw invalid_argument("ident_s");
vb@10
    89
markus@172
    90
            ::memset(ident_s, 0, sizeof(pEpIdentity));
vb@10
    91
            if (ident) {
vb@10
    92
                if (ident->address)
markus@180
    93
                    ident_s->Address = utf16_bstr(ident->address);
vb@10
    94
                if (ident->fpr)
markus@180
    95
                    ident_s->Fpr = utf16_bstr(ident->fpr);
vb@10
    96
                if (ident->user_id)
markus@180
    97
                    ident_s->UserId = utf16_bstr(ident->user_id);
vb@10
    98
                if (ident->username)
markus@180
    99
                    ident_s->UserName = utf16_bstr(ident->username);
markus@269
   100
                ident_s->CommType = (pEpComType)ident->comm_type;
vb@10
   101
                if (ident->lang)
markus@180
   102
                    ident_s->Lang = utf16_bstr(ident->lang);
markus@269
   103
                ident_s->Flags = (pEpIdentityFlags)ident->flags;
vb@10
   104
            }
vb@10
   105
        }
vb@10
   106
markus@172
   107
        ::pEp_identity *new_identity(const pEpIdentity * ident)
vb@10
   108
        {
vb@37
   109
            if (ident == NULL)
vb@37
   110
                return NULL;
vb@37
   111
vb@10
   112
            ::pEp_identity *_ident;
vb@10
   113
vb@10
   114
            string _address;
vb@10
   115
            string _fpr;
vb@10
   116
            string _user_id;
vb@10
   117
            string _username;
vb@10
   118
markus@180
   119
            if (ident->Address)
markus@180
   120
                _address = utf8_string(ident->Address);
markus@180
   121
            if (ident->Fpr) {
markus@180
   122
                _fpr = utf8_string(ident->Fpr);
vb@10
   123
                for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
vb@10
   124
                    if (*p >= 'A' && *p <= 'Z')
vb@10
   125
                        continue;
vb@10
   126
                    if (*p >= '0' && *p <= '9')
vb@10
   127
                        continue;
vb@10
   128
                    throw invalid_argument("invalid hex digits in fingerprint");
vb@10
   129
                }
vb@10
   130
            }
markus@180
   131
            if (ident->UserId)
markus@180
   132
                _user_id = utf8_string(ident->UserId);
markus@180
   133
            if (ident->UserName)
markus@180
   134
                _username = utf8_string(ident->UserName);
vb@10
   135
vb@10
   136
            _ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
vb@10
   137
            assert(_ident);
vb@10
   138
            if (_ident == NULL)
vb@10
   139
                throw bad_alloc();
vb@10
   140
markus@269
   141
            _ident->comm_type = (PEP_comm_type)ident->CommType;
vb@10
   142
markus@180
   143
            if (ident->Lang) {
markus@180
   144
                string _lang = utf8_string(ident->Lang);
vb@10
   145
                if (_lang.length() != 0) {
vb@10
   146
                    if (_lang.length() != 2) {
vb@10
   147
                        ::free_identity(_ident);
vb@10
   148
                        throw invalid_argument("invalid language code");
vb@10
   149
                    }
vb@10
   150
                    if (_lang[0] < 'a' || _lang[0] > 'z') {
vb@10
   151
                        ::free_identity(_ident);
vb@10
   152
                        throw invalid_argument("invalid language code");
vb@10
   153
                    }
vb@10
   154
                    if (_lang[1] < 'a' || _lang[1] > 'z') {
vb@10
   155
                        ::free_identity(_ident);
vb@10
   156
                        throw invalid_argument("invalid language code");
vb@10
   157
                    }
vb@10
   158
                    _ident->lang[0] = _lang[0];
vb@10
   159
                    _ident->lang[1] = _lang[1];
vb@10
   160
                }
vb@10
   161
            }
vb@10
   162
markus@269
   163
            _ident->flags = (identity_flags_t)ident->Flags;
vb@217
   164
vb@10
   165
            return _ident;
vb@10
   166
        }
vb@33
   167
vb@33
   168
        template< class T2, class T > T2 from_C(T *tl);
vb@33
   169
vb@41
   170
        BSTR bstr(char *s)
vb@33
   171
        {
vb@42
   172
            if (s == NULL)
vb@33
   173
                return _bstr_t(L"").Detach();
vb@42
   174
vb@42
   175
            return utf16_bstr(s);
vb@33
   176
        }
vb@33
   177
markus@172
   178
        template<> Blob *from_C< Blob *, bloblist_t >(bloblist_t *tl)
vb@33
   179
        {
markus@269
   180
            assert(tl);
vb@212
   181
vb@33
   182
            CComSafeArray<BYTE> sa;
markus@269
   183
            if (tl) {
markus@269
   184
                sa.Create(tl->size);
markus@269
   185
                if (tl->size) {
markus@269
   186
                    char *data;
markus@269
   187
                    SafeArrayAccessData(sa, (void **)&data);
markus@269
   188
                    memcpy(data, tl->value, tl->size);
markus@269
   189
                    SafeArrayUnaccessData(sa);
markus@269
   190
                }
markus@269
   191
            }
markus@269
   192
            else {
markus@269
   193
                sa.Create((ULONG)0);
markus@269
   194
            }
vb@33
   195
markus@172
   196
            Blob *_blob = new Blob();
vb@33
   197
vb@33
   198
            _blob->value = sa.Detach();
markus@180
   199
            _blob->MimeType = bstr(tl->mime_type);
markus@180
   200
            _blob->Filename = bstr(tl->filename);
vb@33
   201
vb@33
   202
            return _blob;
vb@33
   203
        }
vb@33
   204
vb@33
   205
        template< class T > int length(T *);
vb@33
   206
vb@33
   207
        template< class T2, class T > SAFEARRAY * array_from_C(T *tl)
vb@33
   208
        {
vb@39
   209
            if (tl == NULL)
vb@39
   210
                return newSafeArray<T2>(0);
vb@39
   211
vb@33
   212
            int len = length<T>(tl);
vb@33
   213
vb@33
   214
            LPSAFEARRAY sa = newSafeArray<T2>(len);
vb@33
   215
            LONG lbound, ubound;
vb@33
   216
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   217
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   218
vb@33
   219
            T *_tl = tl;
vb@302
   220
            for (LONG i = lbound; i <= ubound; _tl = _tl->next, i++) {
vb@302
   221
                HRESULT result = SafeArrayPutElement(sa, &i, from_C<T2 *, T>(_tl));
vb@302
   222
                if (!SUCCEEDED(result))
vb@302
   223
                    throw bad_alloc();
vb@302
   224
            }
vb@33
   225
vb@33
   226
            return sa;
vb@33
   227
        }
vb@33
   228
markus@172
   229
        void clear_identity_s(pEpIdentity& ident)
vb@41
   230
        {
markus@180
   231
            SysFreeString(ident.Address);
markus@180
   232
            SysFreeString(ident.Fpr);
markus@180
   233
            SysFreeString(ident.Lang);
markus@180
   234
            SysFreeString(ident.UserName);
markus@180
   235
            SysFreeString(ident.UserId);
vb@41
   236
markus@172
   237
            memset(&ident, 0, sizeof(pEpIdentity));
vb@41
   238
        }
vb@41
   239
markus@172
   240
        template<> pEpIdentity from_C< pEpIdentity, pEp_identity >(pEp_identity *tl)
vb@33
   241
        {
markus@172
   242
            pEpIdentity _ident;
vb@41
   243
            memset(&_ident, 0, sizeof(_ident));
vb@41
   244
vb@39
   245
            if (tl)
vb@39
   246
                copy_identity(&_ident, tl);
vb@33
   247
            return _ident;
vb@33
   248
        }
vb@33
   249
markus@172
   250
        pEpIdentity identity_s(pEp_identity *ident)
vb@33
   251
        {
markus@172
   252
            return from_C< pEpIdentity, pEp_identity >(ident);
vb@33
   253
        }
vb@33
   254
markus@172
   255
        template<> pEpIdentity *from_C< pEpIdentity *, identity_list >(identity_list *il)
vb@33
   256
        {
markus@172
   257
            pEpIdentity *ident = new pEpIdentity();
markus@172
   258
            memset(ident, 0, sizeof(pEpIdentity));
vb@41
   259
vb@39
   260
            if (il)
vb@39
   261
                copy_identity(ident, il->ident);
vb@33
   262
            return ident;
vb@33
   263
        }
vb@33
   264
markus@171
   265
        template<> StringPair *from_C< StringPair *, stringpair_list_t >(stringpair_list_t * sp)
vb@33
   266
        {
markus@171
   267
            StringPair *fld = new StringPair();
vb@39
   268
            if (sp) {
markus@180
   269
                fld->Name = bstr(sp->value->key);
markus@180
   270
                fld->Value = bstr(sp->value->value);
vb@39
   271
            }
vb@33
   272
            return fld;
vb@33
   273
        }
vb@33
   274
vb@33
   275
        template<> int length<identity_list>(identity_list *tl)
vb@33
   276
        {
vb@33
   277
            return identity_list_length(tl);
vb@33
   278
        }
vb@33
   279
vb@33
   280
        template<> int length<bloblist_t>(bloblist_t *tl)
vb@33
   281
        {
vb@33
   282
            return bloblist_length(tl);
vb@33
   283
        }
vb@33
   284
vb@33
   285
        template<> int length<stringpair_list_t>(stringpair_list_t *tl)
vb@33
   286
        {
vb@33
   287
            return stringpair_list_length(tl);
vb@33
   288
        }
vb@33
   289
markus@172
   290
        void clear_text_message(TextMessage *msg)
vb@41
   291
        {
markus@269
   292
            assert(msg);
markus@269
   293
            if (!msg)
markus@269
   294
                return;
vb@210
   295
markus@180
   296
            SysFreeString(msg->Id);
markus@182
   297
            SysFreeString(msg->ShortMsg);
markus@182
   298
            SysFreeString(msg->LongMsg);
markus@182
   299
            SysFreeString(msg->LongMsgFormatted);
markus@180
   300
            SafeArrayDestroy(msg->Attachments);
markus@180
   301
            clear_identity_s(msg->From);
markus@180
   302
            SafeArrayDestroy(msg->To);
markus@180
   303
            clear_identity_s(msg->RecvBy);
markus@180
   304
            SafeArrayDestroy(msg->Cc);
markus@180
   305
            SafeArrayDestroy(msg->Bcc);
markus@180
   306
            SafeArrayDestroy(msg->ReplyTo);
markus@180
   307
            SafeArrayDestroy(msg->References);
markus@180
   308
            SafeArrayDestroy(msg->Keywords);
markus@180
   309
            SysFreeString(msg->Comments);
markus@180
   310
            SafeArrayDestroy(msg->OptFields);
vb@41
   311
markus@172
   312
            memset(msg, 0, sizeof(TextMessage));
vb@41
   313
        }
vb@41
   314
markus@172
   315
        void text_message_from_C(TextMessage *msg2, const ::message *msg)
vb@33
   316
        {
vb@37
   317
            assert(msg2);
vb@33
   318
            assert(msg);
vb@33
   319
markus@269
   320
            if (!msg2) {
markus@269
   321
                msg2 = (TextMessage *)calloc(1, sizeof(TextMessage));
markus@269
   322
                assert(msg2);
markus@269
   323
                if (!msg2)
markus@269
   324
                    throw bad_alloc();
markus@269
   325
            }
markus@269
   326
            else {
markus@269
   327
                clear_text_message(msg2);
markus@269
   328
            }
vb@211
   329
markus@269
   330
            if (!msg)
markus@269
   331
                return;
vb@41
   332
markus@269
   333
            msg2->Dir = (pEpMsgDirection)msg->dir;
markus@180
   334
            msg2->Id = bstr(msg->id);
markus@182
   335
            msg2->ShortMsg = bstr(msg->shortmsg);
markus@182
   336
            msg2->LongMsg = bstr(msg->longmsg);
markus@182
   337
            msg2->LongMsgFormatted = bstr(msg->longmsg_formatted);
markus@180
   338
            msg2->Attachments = array_from_C<Blob, bloblist_t>(msg->attachments);
vb@39
   339
            if (msg->sent)
markus@180
   340
                msg2->Sent = mktime(msg->sent);
vb@39
   341
            if (msg->recv)
markus@180
   342
                msg2->Recv = mktime(msg->recv);
markus@180
   343
            msg2->From = identity_s(msg->from);
markus@180
   344
            msg2->To = array_from_C<pEpIdentity, identity_list>(msg->to);
markus@180
   345
            msg2->RecvBy = identity_s(msg->recv_by);
markus@180
   346
            msg2->Cc = array_from_C<pEpIdentity, identity_list>(msg->cc);
markus@180
   347
            msg2->Bcc = array_from_C<pEpIdentity, identity_list>(msg->bcc);
markus@180
   348
            msg2->ReplyTo = array_from_C<pEpIdentity, identity_list>(msg->reply_to);
markus@180
   349
            msg2->References = string_array(msg->references);
markus@180
   350
            msg2->Keywords = string_array(msg->keywords);
markus@180
   351
            msg2->Comments = bstr(msg->comments);
markus@180
   352
            msg2->OptFields = array_from_C<StringPair, stringpair_list_t>(msg->opt_fields);
vb@33
   353
        }
vb@33
   354
vb@37
   355
        char * str(BSTR s)
vb@33
   356
        {
vb@37
   357
            string str = utf8_string(s);
vb@37
   358
            char *_s = _strdup(str.c_str());
vb@33
   359
            if (_s == NULL)
vb@33
   360
                throw bad_alloc();
vb@33
   361
vb@37
   362
            return _s;
vb@33
   363
        }
vb@33
   364
markus@172
   365
        void clear_blob(Blob& b)
vb@41
   366
        {
markus@180
   367
            SysFreeString(b.Filename);
markus@180
   368
            SysFreeString(b.MimeType);
vb@41
   369
            SafeArrayDestroy(b.value);
markus@172
   370
            memset(&b, 0, sizeof(Blob));
vb@41
   371
        }
vb@41
   372
vb@33
   373
        bloblist_t *bloblist(SAFEARRAY *sa)
vb@33
   374
        {
vb@37
   375
            if (sa == NULL)
vb@37
   376
                return NULL;
vb@37
   377
vb@33
   378
            LONG lbound, ubound;
vb@33
   379
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   380
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   381
vb@33
   382
            size_t size = ubound - lbound + 1;
vb@33
   383
            if (size <= 0)
vb@33
   384
                return NULL;
vb@33
   385
vb@33
   386
            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
vb@33
   387
            if (bl == NULL)
vb@33
   388
                throw bad_alloc();
vb@33
   389
vb@33
   390
            bloblist_t *_bl = bl;
vb@33
   391
            for (LONG i = lbound; i <= ubound; i++) {
markus@172
   392
                Blob b;
markus@172
   393
                memset(&b, 0, sizeof(Blob));
vb@33
   394
                SafeArrayGetElement(sa, &i, &b);
vb@33
   395
vb@38
   396
                LONG _lbound, _ubound;
vb@38
   397
                SafeArrayGetLBound(b.value, 1, &_lbound);
vb@38
   398
                SafeArrayGetUBound(b.value, 1, &_ubound);
vb@38
   399
                size_t size = _ubound - _lbound + 1;
vb@38
   400
vb@136
   401
                char *buffer;
vb@136
   402
                if (size) {
markus@269
   403
                    buffer = (char *)malloc(size + 1);
vb@136
   404
                    if (buffer == NULL)
vb@136
   405
                        throw bad_alloc();
vb@33
   406
vb@136
   407
                    char *data;
vb@33
   408
markus@269
   409
                    SafeArrayAccessData(b.value, (void **)&data);
vb@136
   410
                    memcpy(buffer, data, size);
markus@269
   411
                    buffer[size] = 0; // safeguard
vb@136
   412
                    SafeArrayUnaccessData(sa);
vb@136
   413
                }
vb@136
   414
                else {
vb@136
   415
                    buffer = _strdup("");
vb@136
   416
                    if (buffer == NULL)
vb@136
   417
                        throw bad_alloc();
vb@33
   418
vb@136
   419
                }
markus@180
   420
                _bl = bloblist_add(_bl, buffer, size, str(b.MimeType), str(b.Filename));
vb@33
   421
                if (_bl == NULL) {
vb@42
   422
                    free(buffer);
vb@41
   423
                    clear_blob(b);
vb@33
   424
                    free_bloblist(bl);
vb@33
   425
                    throw bad_alloc();
vb@33
   426
                }
vb@33
   427
vb@41
   428
                clear_blob(b);
vb@33
   429
            }
vb@33
   430
vb@33
   431
            return bl;
vb@33
   432
        }
vb@33
   433
vb@33
   434
        identity_list *identities(SAFEARRAY * sa)
vb@33
   435
        {
vb@37
   436
            if (sa == NULL)
vb@37
   437
                return NULL;
vb@37
   438
vb@33
   439
            LONG lbound, ubound;
vb@33
   440
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   441
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   442
vb@33
   443
            size_t size = ubound - lbound + 1;
vb@33
   444
            if (size <= 0)
vb@33
   445
                return NULL;
vb@33
   446
vb@33
   447
            identity_list *il = new_identity_list(NULL);
vb@33
   448
vb@33
   449
            identity_list *_il = il;
vb@33
   450
            for (LONG i = lbound; i <= ubound; i++) {
markus@172
   451
                pEpIdentity s;
markus@172
   452
                memset(&s, 0, sizeof(pEpIdentity));
vb@33
   453
                SafeArrayGetElement(sa, &i, &s);
vb@33
   454
vb@33
   455
                pEp_identity *ident;
vb@33
   456
                try {
vb@33
   457
                    ident = new_identity(&s);
vb@33
   458
                }
vb@33
   459
                catch (bad_alloc&) {
vb@41
   460
                    clear_identity_s(s);
vb@33
   461
                    throw bad_alloc();
vb@33
   462
                }
vb@33
   463
vb@41
   464
                clear_identity_s(s);
vb@33
   465
vb@33
   466
                _il = identity_list_add(_il, ident);
vb@33
   467
                if (_il == NULL) {
vb@33
   468
                    free_identity_list(il);
vb@33
   469
                    throw bad_alloc();
vb@33
   470
                }
vb@33
   471
            }
vb@33
   472
vb@33
   473
            return il;
vb@33
   474
        }
vb@33
   475
markus@171
   476
        stringpair_t *new_stringpair(StringPair *fld)
vb@33
   477
        {
markus@269
   478
            stringpair_t *pair;
vb@212
   479
markus@269
   480
            if (!fld) {
markus@269
   481
                pair = ::new_stringpair(NULL, NULL);
markus@269
   482
            }
markus@269
   483
            else {
markus@269
   484
                pair = ::new_stringpair(str(fld->Name), str(fld->Value));
markus@269
   485
            }
vb@33
   486
            if (pair == NULL)
vb@33
   487
                throw bad_alloc();
vb@33
   488
vb@33
   489
            return pair;
vb@33
   490
        }
vb@33
   491
markus@171
   492
        void clear_opt_field(StringPair& f)
vb@41
   493
        {
markus@180
   494
            SysFreeString(f.Name);
markus@180
   495
            SysFreeString(f.Value);
markus@171
   496
            memset(&f, 0, sizeof(StringPair));
vb@41
   497
        }
vb@41
   498
vb@33
   499
        stringpair_list_t *stringpair_list(SAFEARRAY * sa)
vb@33
   500
        {
vb@37
   501
            if (sa == NULL)
vb@37
   502
                return NULL;
vb@37
   503
vb@33
   504
            LONG lbound, ubound;
vb@33
   505
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   506
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   507
vb@33
   508
            size_t size = ubound - lbound + 1;
vb@33
   509
            if (size <= 0)
vb@33
   510
                return NULL;
vb@33
   511
vb@33
   512
            stringpair_list_t *il = new_stringpair_list(NULL);
vb@33
   513
vb@33
   514
            stringpair_list_t *_il = il;
vb@33
   515
            for (LONG i = lbound; i <= ubound; i++) {
markus@171
   516
                StringPair s;
markus@171
   517
                memset(&s, 0, sizeof(StringPair));
vb@33
   518
                SafeArrayGetElement(sa, &i, &s);
vb@33
   519
vb@33
   520
                stringpair_t *pair;
vb@33
   521
                try {
vb@33
   522
                    pair = new_stringpair(&s);
vb@33
   523
                }
vb@33
   524
                catch (bad_alloc&) {
vb@41
   525
                    clear_opt_field(s);
vb@33
   526
                    throw bad_alloc();
vb@33
   527
                }
vb@33
   528
vb@41
   529
                clear_opt_field(s);
vb@33
   530
vb@33
   531
                _il = stringpair_list_add(_il, pair);
vb@33
   532
                if (_il == NULL) {
vb@33
   533
                    free_stringpair_list(il);
vb@33
   534
                    throw bad_alloc();
vb@33
   535
                }
vb@33
   536
            }
vb@33
   537
vb@33
   538
            return il;
vb@33
   539
        }
markus@269
   540
markus@172
   541
        ::message * text_message_to_C(TextMessage *msg)
vb@33
   542
        {
vb@33
   543
            assert(msg);
markus@269
   544
            if (!msg)
markus@269
   545
                throw invalid_argument("msg");
vb@33
   546
markus@269
   547
            ::message * msg2 = new_message((PEP_msg_direction)msg->Dir);
vb@37
   548
            if (msg2 == NULL)
vb@37
   549
                throw bad_alloc();
vb@33
   550
markus@180
   551
            msg2->id = str(msg->Id);
markus@182
   552
            msg2->shortmsg = str(msg->ShortMsg);
markus@182
   553
            msg2->longmsg = str(msg->LongMsg);
markus@182
   554
            msg2->longmsg_formatted = str(msg->LongMsgFormatted);
markus@180
   555
            msg2->attachments = bloblist(msg->Attachments);
markus@180
   556
            msg2->sent = new_timestamp(msg->Sent);
markus@180
   557
            msg2->recv = new_timestamp(msg->Recv);
markus@180
   558
            msg2->from = new_identity(&msg->From);
markus@180
   559
            msg2->to = identities(msg->To);
markus@180
   560
            msg2->recv_by = new_identity(&msg->RecvBy);
markus@180
   561
            msg2->cc = identities(msg->Cc);
markus@180
   562
            msg2->bcc = identities(msg->Bcc);
markus@180
   563
            msg2->reply_to = identities(msg->ReplyTo);
markus@180
   564
            msg2->references = new_stringlist(msg->References);
markus@180
   565
            msg2->keywords = new_stringlist(msg->Keywords);
markus@180
   566
            msg2->comments = str(msg->Comments);
markus@180
   567
            msg2->opt_fields = stringpair_list(msg->OptFields);
vb@33
   568
vb@33
   569
            return msg2;
vb@33
   570
        }
markus@177
   571
markus@269
   572
        void opt_field_array_from_C(stringpair_list_t* spair_list, LPSAFEARRAY* pair_list_out) {
markus@269
   573
            assert(spair_list);
markus@269
   574
            assert(pair_list_out);
krista@157
   575
markus@269
   576
            if (!spair_list)
markus@269
   577
                return;
markus@177
   578
markus@269
   579
            *pair_list_out = array_from_C<StringPair, stringpair_list_t>(spair_list);
markus@269
   580
        }
markus@269
   581
markus@269
   582
        void clear_opt_field_array(LPSAFEARRAY* opt_field_array) {
markus@269
   583
            if (opt_field_array) {
markus@269
   584
                SafeArrayDestroy(*opt_field_array);
markus@269
   585
                *opt_field_array = NULL;
markus@269
   586
            }
markus@269
   587
        }
vb@10
   588
    }
vb@10
   589
}