CpEpEngine.h
author Volker Birk <vb@pep-project.org>
Thu, 16 Apr 2015 20:43:31 +0200
changeset 0 0aa9866d751c
child 3 a81ac65b3f71
permissions -rw-r--r--
initial commit
vb@0
     1
// CpEpEngine.h : Declaration of the CpEpEngine
vb@0
     2
vb@0
     3
#pragma once
vb@0
     4
#include "resource.h"       // main symbols
vb@0
     5
vb@0
     6
vb@0
     7
vb@0
     8
#include "pEpComServerAdapter_i.h"
vb@0
     9
#include "_IpEpEngineEvents_CP.h"
vb@0
    10
#include "locked_queue.hh"
vb@0
    11
#include "utf8_helper.h"
vb@0
    12
vb@0
    13
vb@0
    14
vb@0
    15
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
vb@0
    16
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
vb@0
    17
#endif
vb@0
    18
vb@0
    19
using namespace ATL;
vb@0
    20
using namespace utility;
vb@0
    21
vb@0
    22
vb@0
    23
// CpEpEngine
vb@0
    24
vb@0
    25
class ATL_NO_VTABLE CpEpEngine :
vb@0
    26
	public CComObjectRootEx<CComSingleThreadModel>,
vb@0
    27
	public CComCoClass<CpEpEngine, &CLSID_pEpEngine>,
vb@0
    28
	public ISupportErrorInfo,
vb@0
    29
	public IConnectionPointContainerImpl<CpEpEngine>,
vb@0
    30
	public CProxy_IpEpEngineEvents<CpEpEngine>,
vb@0
    31
	public IpEpEngine
vb@0
    32
{
vb@0
    33
public:
vb@0
    34
	CpEpEngine()
vb@0
    35
	{
vb@0
    36
        PEP_STATUS status = ::init(&m_session);
vb@0
    37
        assert(status == PEP_STATUS_OK);
vb@0
    38
        ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
vb@0
    39
        identity_queue = new identity_queue_t();
vb@0
    40
        keymanagement_thread = new thread(::do_keymanagement, retrieve_next_identity, (void *) identity_queue);
vb@0
    41
        keymanagement_thread->detach();
vb@0
    42
    }
vb@0
    43
    ~CpEpEngine()
vb@0
    44
    {
vb@0
    45
        pEp_identity_cpp shutdown;
vb@0
    46
        identity_queue->push_front(shutdown);
vb@0
    47
        ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
vb@0
    48
        ::release(m_session);
vb@0
    49
    }
vb@0
    50
vb@0
    51
DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
vb@0
    52
vb@0
    53
DECLARE_NOT_AGGREGATABLE(CpEpEngine)
vb@0
    54
vb@0
    55
BEGIN_COM_MAP(CpEpEngine)
vb@0
    56
	COM_INTERFACE_ENTRY(IpEpEngine)
vb@0
    57
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
vb@0
    58
	COM_INTERFACE_ENTRY(IConnectionPointContainer)
vb@0
    59
END_COM_MAP()
vb@0
    60
vb@0
    61
BEGIN_CONNECTION_POINT_MAP(CpEpEngine)
vb@0
    62
	CONNECTION_POINT_ENTRY(__uuidof(_IpEpEngineEvents))
vb@0
    63
END_CONNECTION_POINT_MAP()
vb@0
    64
// ISupportsErrorInfo
vb@0
    65
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
vb@0
    66
vb@0
    67
vb@0
    68
	DECLARE_PROTECT_FINAL_CONSTRUCT()
vb@0
    69
vb@0
    70
	HRESULT FinalConstruct()
vb@0
    71
	{
vb@0
    72
		return S_OK;
vb@0
    73
	}
vb@0
    74
vb@0
    75
	void FinalRelease()
vb@0
    76
	{
vb@0
    77
	}
vb@0
    78
vb@0
    79
vb@0
    80
protected:
vb@0
    81
    struct pEp_identity_cpp {
vb@0
    82
        std::string address;
vb@0
    83
        std::string fpr;
vb@0
    84
        std::string user_id;
vb@0
    85
        std::string username;
vb@0
    86
        pEp_comm_type comm_type;
vb@0
    87
        std::string lang;
vb@0
    88
        bool me;
vb@0
    89
vb@0
    90
        pEp_identity_cpp(
vb@0
    91
            std::string _address = std::string(),
vb@0
    92
            std::string _fpr = std::string(),
vb@0
    93
            std::string _user_id = std::string(),
vb@0
    94
            std::string _username = std::string(),
vb@0
    95
            pEp_comm_type _comm_type = pEp_ct_unknown,
vb@0
    96
            std::string _lang = std::string()
vb@0
    97
            ) : address(_address), fpr(_fpr), user_id(_user_id), username(_username), comm_type(_comm_type), lang(_lang), me(false)
vb@0
    98
        { }
vb@0
    99
vb@0
   100
        pEp_identity_cpp(const ::pEp_identity *_ident)
vb@0
   101
            : me(false)
vb@0
   102
        {
vb@0
   103
            if (_ident->address)
vb@0
   104
                address = _ident->address;
vb@0
   105
            if (_ident->fpr)
vb@0
   106
                fpr = _ident->fpr;
vb@0
   107
            if (_ident->user_id)
vb@0
   108
                user_id = _ident->user_id;
vb@0
   109
            if (_ident->username)
vb@0
   110
                username = _ident->username;
vb@0
   111
            comm_type = (pEp_comm_type) _ident->comm_type;
vb@0
   112
            lang = _ident->lang;
vb@0
   113
        }
vb@0
   114
vb@0
   115
        pEp_identity_cpp(const pEp_identity_s *_ident)
vb@0
   116
            : me(false)
vb@0
   117
        {
vb@0
   118
            if (_ident->address)
vb@0
   119
                address = utf8_string(_ident->address);
vb@0
   120
            if (_ident->fpr)
vb@0
   121
                fpr = utf8_string(_ident->fpr);
vb@0
   122
            if (_ident->user_id)
vb@0
   123
                user_id = utf8_string(_ident->user_id);
vb@0
   124
            if (_ident->username)
vb@0
   125
                username = utf8_string(_ident->username);
vb@0
   126
            comm_type = _ident->comm_type;
vb@0
   127
            if (_ident->lang)
vb@0
   128
                lang = utf8_string(_ident->lang);
vb@0
   129
        }
vb@0
   130
    };
vb@0
   131
vb@0
   132
    class session
vb@0
   133
    {
vb@0
   134
    private:
vb@0
   135
        CpEpEngine *me;
vb@0
   136
vb@0
   137
    public:
vb@0
   138
        session(CpEpEngine *myself)
vb@0
   139
        {
vb@0
   140
            me = myself;
vb@0
   141
            me->session_mutex.lock();
vb@0
   142
        }
vb@0
   143
vb@0
   144
        ~session()
vb@0
   145
        {
vb@0
   146
            me->session_mutex.unlock();
vb@0
   147
        }
vb@0
   148
vb@0
   149
        operator PEP_SESSION const () 
vb@0
   150
        {
vb@0
   151
            return me->m_session;
vb@0
   152
        }
vb@0
   153
    };
vb@0
   154
vb@0
   155
    session get_session()
vb@0
   156
    {
vb@0
   157
        return session(this);
vb@0
   158
    }
vb@0
   159
vb@0
   160
    static ::pEp_identity *new_identity(const pEp_identity_cpp&);
vb@0
   161
vb@0
   162
    typedef locked_queue<pEp_identity_cpp> identity_queue_t;
vb@0
   163
    static ::pEp_identity * retrieve_next_identity(void *management);
vb@0
   164
    HRESULT error(_bstr_t msg);
vb@0
   165
vb@0
   166
private:
vb@0
   167
    PEP_SESSION m_session;
vb@0
   168
    mutex session_mutex;
vb@0
   169
    identity_queue_t *identity_queue;
vb@0
   170
    thread *keymanagement_thread;
vb@0
   171
vb@0
   172
public:
vb@0
   173
    STDMETHOD(log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
vb@0
   174
    STDMETHOD(decrypt)(BSTR ctext, BSTR * ptext, LPSAFEARRAY * key_list, pEp_STATUS * decrypt_status);
vb@0
   175
    STDMETHOD(decrypt_b)(BSTR ctext, LPSAFEARRAY * ptext, LPSAFEARRAY * key_list, pEp_STATUS * decrypt_status);
vb@0
   176
    STDMETHOD(encrypt)(SAFEARRAY * key_list, BSTR ptext, BSTR * ctext, pEp_STATUS * status);
vb@0
   177
    STDMETHOD(encrypt_b)(SAFEARRAY * key_list, SAFEARRAY * ptext, BSTR * ctext, pEp_STATUS * status);
vb@0
   178
    STDMETHOD(safeword)(LONG value, BSTR lang, BSTR * word);
vb@0
   179
    STDMETHOD(safewords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
vb@0
   180
    STDMETHOD(get_identity)(BSTR address, pEp_identity_s * ident);
vb@0
   181
    STDMETHOD(set_identity)(pEp_identity_s * ident);
vb@0
   182
    STDMETHOD(generate_keypair)(pEp_identity_s * ident, BSTR * fpr);
vb@0
   183
    STDMETHOD(delete_keypair)(BSTR fpr);
vb@0
   184
    STDMETHOD(import_key)(BSTR key_data);
vb@0
   185
    STDMETHOD(import_key_b)(SAFEARRAY * key_data);
vb@0
   186
    STDMETHOD(export_key)(BSTR fpr, BSTR * key_data);
vb@0
   187
    STDMETHOD(recv_key)(BSTR pattern);
vb@0
   188
    STDMETHOD(find_keys)(BSTR pattern, LPSAFEARRAY * key_list);
vb@0
   189
    STDMETHOD(send_key)(BSTR pattern);
vb@0
   190
    STDMETHOD(examine_identity)(pEp_identity_s * ident);
vb@0
   191
    STDMETHOD(examine_myself)(pEp_identity_s * myself);
vb@0
   192
    STDMETHOD(verify)(BSTR text, BSTR signature, LPSAFEARRAY * key_list, pEp_STATUS * verify_status);
vb@0
   193
    STDMETHOD(myself)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
vb@0
   194
    STDMETHOD(update_identity)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
vb@0
   195
};
vb@0
   196
vb@0
   197
OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)