pEp_utility.cpp
branchCOM-54-ENGINE-9
changeset 251 ded328cde93f
parent 224 b0290a75f7a5
     1.1 --- a/pEp_utility.cpp	Fri Apr 21 17:11:50 2017 +0200
     1.2 +++ b/pEp_utility.cpp	Wed Jun 14 23:46:37 2017 +0200
     1.3 @@ -205,6 +205,34 @@
     1.4              return _blob;
     1.5          }
     1.6  
     1.7 +        template<> BlobEx *from_C< BlobEx *, bloblist_t >(bloblist_t *tl)
     1.8 +        {
     1.9 +            assert(tl);
    1.10 +
    1.11 +            CComSafeArray<BYTE> sa;
    1.12 +            if (tl) {
    1.13 +                sa.Create(tl->size);
    1.14 +                if (tl->size) {
    1.15 +                    char *data;
    1.16 +                    SafeArrayAccessData(sa, (void **)&data);
    1.17 +                    memcpy(data, tl->value, tl->size);
    1.18 +                    SafeArrayUnaccessData(sa);
    1.19 +                }
    1.20 +            }
    1.21 +            else {
    1.22 +                sa.Create((ULONG)0);
    1.23 +            }
    1.24 +
    1.25 +            BlobEx *_blob = new BlobEx();
    1.26 +
    1.27 +            _blob->value = sa.Detach();
    1.28 +            _blob->MimeType = bstr(tl->mime_type);
    1.29 +            _blob->Filename = bstr(tl->filename);
    1.30 +            _blob->ContentId = bstr(tl->content_id);
    1.31 +
    1.32 +            return _blob;
    1.33 +        }
    1.34 +
    1.35          template< class T > int length(T *);
    1.36  
    1.37          template< class T2, class T > SAFEARRAY * array_from_C(T *tl)
    1.38 @@ -312,6 +340,31 @@
    1.39              memset(msg, 0, sizeof(TextMessage));
    1.40          }
    1.41  
    1.42 +        void clear_text_message(TextMessageEx *msg)
    1.43 +        {
    1.44 +            assert(msg);
    1.45 +            if (!msg)
    1.46 +                return;
    1.47 +
    1.48 +            SysFreeString(msg->Id);
    1.49 +            SysFreeString(msg->ShortMsg);
    1.50 +            SysFreeString(msg->LongMsg);
    1.51 +            SysFreeString(msg->LongMsgFormatted);
    1.52 +            SafeArrayDestroy(msg->Attachments);
    1.53 +            clear_identity_s(msg->From);
    1.54 +            SafeArrayDestroy(msg->To);
    1.55 +            clear_identity_s(msg->RecvBy);
    1.56 +            SafeArrayDestroy(msg->Cc);
    1.57 +            SafeArrayDestroy(msg->Bcc);
    1.58 +            SafeArrayDestroy(msg->ReplyTo);
    1.59 +            SafeArrayDestroy(msg->References);
    1.60 +            SafeArrayDestroy(msg->Keywords);
    1.61 +            SysFreeString(msg->Comments);
    1.62 +            SafeArrayDestroy(msg->OptFields);
    1.63 +
    1.64 +            memset(msg, 0, sizeof(TextMessage));
    1.65 +        }
    1.66 +
    1.67          void text_message_from_C(TextMessage *msg2, const ::message *msg)
    1.68          {
    1.69              assert(msg2);
    1.70 @@ -352,6 +405,46 @@
    1.71              msg2->OptFields = array_from_C<StringPair, stringpair_list_t>(msg->opt_fields);
    1.72          }
    1.73  
    1.74 +        void text_message_from_C(TextMessageEx *msg2, const ::message *msg)
    1.75 +        {
    1.76 +            assert(msg2);
    1.77 +            assert(msg);
    1.78 +
    1.79 +            if (!msg2) {
    1.80 +                msg2 = (TextMessageEx *)calloc(1, sizeof(TextMessageEx));
    1.81 +                assert(msg2);
    1.82 +                if (!msg2)
    1.83 +                    throw bad_alloc();
    1.84 +            }
    1.85 +            else {
    1.86 +                clear_text_message(msg2);
    1.87 +            }
    1.88 +
    1.89 +            if (!msg)
    1.90 +                return;
    1.91 +
    1.92 +            msg2->Dir = (pEpMsgDirection)msg->dir;
    1.93 +            msg2->Id = bstr(msg->id);
    1.94 +            msg2->ShortMsg = bstr(msg->shortmsg);
    1.95 +            msg2->LongMsg = bstr(msg->longmsg);
    1.96 +            msg2->LongMsgFormatted = bstr(msg->longmsg_formatted);
    1.97 +            msg2->Attachments = array_from_C<BlobEx, bloblist_t>(msg->attachments);
    1.98 +            if (msg->sent)
    1.99 +                msg2->Sent = mktime(msg->sent);
   1.100 +            if (msg->recv)
   1.101 +                msg2->Recv = mktime(msg->recv);
   1.102 +            msg2->From = identity_s(msg->from);
   1.103 +            msg2->To = array_from_C<pEpIdentity, identity_list>(msg->to);
   1.104 +            msg2->RecvBy = identity_s(msg->recv_by);
   1.105 +            msg2->Cc = array_from_C<pEpIdentity, identity_list>(msg->cc);
   1.106 +            msg2->Bcc = array_from_C<pEpIdentity, identity_list>(msg->bcc);
   1.107 +            msg2->ReplyTo = array_from_C<pEpIdentity, identity_list>(msg->reply_to);
   1.108 +            msg2->References = string_array(msg->references);
   1.109 +            msg2->Keywords = string_array(msg->keywords);
   1.110 +            msg2->Comments = bstr(msg->comments);
   1.111 +            msg2->OptFields = array_from_C<StringPair, stringpair_list_t>(msg->opt_fields);
   1.112 +        }
   1.113 +
   1.114          char * str(BSTR s)
   1.115          {
   1.116              string str = utf8_string(s);
   1.117 @@ -370,6 +463,15 @@
   1.118              memset(&b, 0, sizeof(Blob));
   1.119          }
   1.120  
   1.121 +        void clear_blob(BlobEx& b)
   1.122 +        {
   1.123 +            SysFreeString(b.Filename);
   1.124 +            SysFreeString(b.MimeType);
   1.125 +            SysFreeString(b.ContentId);
   1.126 +            SafeArrayDestroy(b.value);
   1.127 +            memset(&b, 0, sizeof(Blob));
   1.128 +        }
   1.129 +
   1.130          bloblist_t *bloblist(SAFEARRAY *sa)
   1.131          {
   1.132              if (sa == NULL)
   1.133 @@ -383,7 +485,7 @@
   1.134              if (size <= 0)
   1.135                  return NULL;
   1.136  
   1.137 -            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
   1.138 +            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL, NULL);
   1.139              if (bl == NULL)
   1.140                  throw bad_alloc();
   1.141  
   1.142 @@ -417,7 +519,68 @@
   1.143                          throw bad_alloc();
   1.144  
   1.145                  }
   1.146 -                _bl = bloblist_add(_bl, buffer, size, str(b.MimeType), str(b.Filename));
   1.147 +                _bl = bloblist_add(_bl, buffer, size, str(b.MimeType), str(b.Filename), "");
   1.148 +                if (_bl == NULL) {
   1.149 +                    free(buffer);
   1.150 +                    clear_blob(b);
   1.151 +                    free_bloblist(bl);
   1.152 +                    throw bad_alloc();
   1.153 +                }
   1.154 +
   1.155 +                clear_blob(b);
   1.156 +            }
   1.157 +
   1.158 +            return bl;
   1.159 +        }
   1.160 +
   1.161 +        bloblist_t *bloblistEx(SAFEARRAY *sa)
   1.162 +        {
   1.163 +            if (sa == NULL)
   1.164 +                return NULL;
   1.165 +
   1.166 +            LONG lbound, ubound;
   1.167 +            SafeArrayGetLBound(sa, 1, &lbound);
   1.168 +            SafeArrayGetUBound(sa, 1, &ubound);
   1.169 +
   1.170 +            size_t size = ubound - lbound + 1;
   1.171 +            if (size <= 0)
   1.172 +                return NULL;
   1.173 +
   1.174 +            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL, NULL);
   1.175 +            if (bl == NULL)
   1.176 +                throw bad_alloc();
   1.177 +
   1.178 +            bloblist_t *_bl = bl;
   1.179 +            for (LONG i = lbound; i <= ubound; i++) {
   1.180 +                BlobEx b;
   1.181 +                memset(&b, 0, sizeof(BlobEx));
   1.182 +                SafeArrayGetElement(sa, &i, &b);
   1.183 +
   1.184 +                LONG _lbound, _ubound;
   1.185 +                SafeArrayGetLBound(b.value, 1, &_lbound);
   1.186 +                SafeArrayGetUBound(b.value, 1, &_ubound);
   1.187 +                size_t size = _ubound - _lbound + 1;
   1.188 +
   1.189 +                char *buffer;
   1.190 +                if (size) {
   1.191 +                    buffer = (char *)malloc(size + 1);
   1.192 +                    if (buffer == NULL)
   1.193 +                        throw bad_alloc();
   1.194 +
   1.195 +                    char *data;
   1.196 +
   1.197 +                    SafeArrayAccessData(b.value, (void **)&data);
   1.198 +                    memcpy(buffer, data, size);
   1.199 +                    buffer[size] = 0; // safeguard
   1.200 +                    SafeArrayUnaccessData(sa);
   1.201 +                }
   1.202 +                else {
   1.203 +                    buffer = _strdup("");
   1.204 +                    if (buffer == NULL)
   1.205 +                        throw bad_alloc();
   1.206 +
   1.207 +                }
   1.208 +                _bl = bloblist_add(_bl, buffer, size, str(b.MimeType), str(b.Filename), str(b.ContentId));
   1.209                  if (_bl == NULL) {
   1.210                      free(buffer);
   1.211                      clear_blob(b);
   1.212 @@ -569,6 +732,37 @@
   1.213              return msg2;
   1.214          }
   1.215  
   1.216 +        ::message * text_message_to_C(TextMessageEx *msg)
   1.217 +        {
   1.218 +            assert(msg);
   1.219 +            if (!msg)
   1.220 +                throw invalid_argument("msg");
   1.221 +
   1.222 +            ::message * msg2 = new_message((PEP_msg_direction)msg->Dir);
   1.223 +            if (msg2 == NULL)
   1.224 +                throw bad_alloc();
   1.225 +
   1.226 +            msg2->id = str(msg->Id);
   1.227 +            msg2->shortmsg = str(msg->ShortMsg);
   1.228 +            msg2->longmsg = str(msg->LongMsg);
   1.229 +            msg2->longmsg_formatted = str(msg->LongMsgFormatted);
   1.230 +            msg2->attachments = bloblistEx(msg->Attachments);
   1.231 +            msg2->sent = new_timestamp(msg->Sent);
   1.232 +            msg2->recv = new_timestamp(msg->Recv);
   1.233 +            msg2->from = new_identity(&msg->From);
   1.234 +            msg2->to = identities(msg->To);
   1.235 +            msg2->recv_by = new_identity(&msg->RecvBy);
   1.236 +            msg2->cc = identities(msg->Cc);
   1.237 +            msg2->bcc = identities(msg->Bcc);
   1.238 +            msg2->reply_to = identities(msg->ReplyTo);
   1.239 +            msg2->references = new_stringlist(msg->References);
   1.240 +            msg2->keywords = new_stringlist(msg->Keywords);
   1.241 +            msg2->comments = str(msg->Comments);
   1.242 +            msg2->opt_fields = stringpair_list(msg->OptFields);
   1.243 +
   1.244 +            return msg2;
   1.245 +        }
   1.246 +
   1.247  		void opt_field_array_from_C(stringpair_list_t* spair_list, LPSAFEARRAY* pair_list_out) {
   1.248  			assert(spair_list);
   1.249  			assert(pair_list_out);