pEp_utility.h
author Volker Birk <vb@pep.foundation>
Mon, 22 Oct 2018 10:14:50 +0200
branchsync
changeset 304 00916b0afaac
parent 302 b3444780fa9f
child 332 54d5b01490d6
permissions -rw-r--r--
start COM on sync thread
     1 #pragma once
     2 
     3 #include "stdafx.h"
     4 #include "pEpComServerAdapter_i.h"
     5 #include "utf8_helper.h"
     6 
     7 using namespace std;
     8 
     9 namespace pEp {
    10     namespace utility {
    11 
    12         struct pEp_identity_cpp {
    13             string address;
    14             string fpr;
    15             string user_id;
    16             string username;
    17             pEpComType comm_type;
    18             string lang;
    19             int flags;
    20 
    21             pEp_identity_cpp(
    22                 string _address = string(),
    23                 string _fpr = string(),
    24                 string _user_id = string(),
    25                 string _username = string(),
    26                 pEpComType _comm_type = pEpCtUnknown,
    27                 string _lang = string()
    28             ) : address(_address), fpr(_fpr), user_id(_user_id), username(_username), comm_type(_comm_type), lang(_lang)
    29             { }
    30 
    31             pEp_identity_cpp(const ::pEp_identity *_ident);
    32             pEp_identity_cpp(const pEpIdentity *_ident);
    33 
    34             pEp_identity * to_pEp_identity();
    35             pEpIdentity * to_pEp_identity_s();
    36         };
    37 
    38         void copy_identity(pEpIdentity * ident_s, const pEp_identity * ident);
    39         void free_identity_strings(pEpIdentity * ident_s);
    40         void clear_identity_s(pEpIdentity& ident);
    41         void clear_text_message(TextMessage *msg);
    42         ::pEp_identity *new_identity(const pEpIdentity * ident);
    43 
    44         void opt_field_array_from_C(stringpair_list_t* spair_list, LPSAFEARRAY* pair_list_out);
    45         void clear_opt_field_array(LPSAFEARRAY* pair_list);
    46         template< class T2, class T > SAFEARRAY * array_from_C(T *tl);
    47 
    48         static LPTYPELIB pTypelib = NULL;
    49 
    50         template< class UDType > static IRecordInfo *getRecordInfo()
    51         {
    52             LPTYPEINFO pTypeInfo = NULL;
    53             LPSAFEARRAY psaUDType = NULL;
    54             IRecordInfo* pRecInfo = NULL;
    55             HRESULT hr;
    56 
    57             // Fetch the IRecordInfo interface describing the UDT
    58             if (pTypelib == NULL) {
    59                 hr = LoadRegTypeLib(LIBID_pEpCOMServerAdapterLib, 1, 0, GetUserDefaultLCID(), &pTypelib);
    60                 assert(SUCCEEDED(hr));
    61                 if (!SUCCEEDED(hr))
    62                     throw runtime_error("getRecordInfo(): LoadRegTypeLib cannot load LIBID_pEpCOMServerAdapterLib");
    63             }
    64 
    65             assert(pTypelib);
    66 
    67             GUID guid = __uuidof(UDType);
    68             hr = pTypelib->GetTypeInfoOfGuid(guid, &pTypeInfo);
    69             assert(SUCCEEDED(hr) && pTypeInfo);
    70             hr = GetRecordInfoFromTypeInfo(pTypeInfo, &pRecInfo);
    71             assert(SUCCEEDED(hr) && pRecInfo);
    72             pTypeInfo->Release();
    73 
    74             return pRecInfo;
    75         }
    76 
    77         template< class UDType > LPSAFEARRAY newSafeArray(ULONG cElements)
    78         {
    79             IRecordInfo *pRecInfo = getRecordInfo< UDType >();
    80             SAFEARRAYBOUND rgbounds = { cElements, 0 };
    81             LPSAFEARRAY psaUDType = SafeArrayCreateEx(VT_RECORD, 1, &rgbounds, pRecInfo);
    82             pRecInfo->Release();
    83             assert(psaUDType);
    84 
    85             return psaUDType;
    86         }
    87 
    88         ::message * text_message_to_C(TextMessage *msg);
    89         void text_message_from_C(TextMessage *msg2, const ::message *msg);
    90     }
    91 }