aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/addrbook/msoutlook/com/server/Server.cxx
blob: 841c96e0c43e986bbab8e7657e2c9e9808b8130f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
#include "../../MAPIBitness.h"
#include "../../MAPISession.h"
#include "../../MsOutlookAddrBookContactSourceService.h"
#include "../../StringUtils.h"
#include "../MsOutlookAddrBookClient.h"
#include "../MsOutlookAddrBookServerClassFactory.h"
#include "../TypeLib.h"

#include <stdio.h>
#include <TlHelp32.h>

#define MAPI_NO_COINIT 8

void waitParentProcessStop();
static void Server_deleted(LPSTR id);
static void Server_inserted(LPSTR id);
static void Server_updated(LPSTR id);

/**
 * Starts the COM server.
 */
int main(int argc, char** argv)
{
    HRESULT hr = E_FAIL;

    if((hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED)) != S_OK
            && hr != S_FALSE)
    {
        return hr;
    }

    MAPISession_initLock();
    if(MsOutlookAddrBookContactSourceService_NativeMAPIInitialize(
                MAPI_INIT_VERSION,
                MAPI_MULTITHREAD_NOTIFICATIONS | MAPI_NO_COINIT,
                (void*) Server_deleted,
                (void*) Server_inserted,
                (void*) Server_updated)
            != S_OK)
    {
        CoUninitialize();
        return hr;
    }

    WCHAR * path = (WCHAR*) L"IMsOutlookAddrBookServer.tlb"; 
    LPTYPELIB typeLib = TypeLib_loadRegTypeLib(path);
    if(typeLib != NULL)
    {
        ClassFactory *classObject = new MsOutlookAddrBookServerClassFactory();
        if(classObject != NULL)
        {
            hr = classObject->registerClassObject();
            hr = ::CoResumeClassObjects();

            waitParentProcessStop();

            hr = ::CoSuspendClassObjects();
            hr = classObject->revokeClassObject();

            classObject->Release();
        }
        TypeLib_releaseTypeLib(typeLib);
    }
    MsOutlookAddrBookContactSourceService_NativeMAPIUninitialize();
    MAPISession_freeLock();

    CoUninitialize();

    return hr;
}

/**
 * Wait that the parent process stops.
 */
void waitParentProcessStop()
{
    HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if(handle != INVALID_HANDLE_VALUE)
    {
        PROCESSENTRY32 processEntry;
        memset(&processEntry, 0, sizeof(processEntry));
        processEntry.dwSize = sizeof(PROCESSENTRY32);
        DWORD id = GetCurrentProcessId();
        if(Process32First(handle, &processEntry))
        {
            do
            {
                // We have found this process
                if(processEntry.th32ProcessID == id)
                {
                    // Get the parent process handle.
                    HANDLE parentHandle
                        = OpenProcess(
                                SYNCHRONIZE
                                | PROCESS_QUERY_INFORMATION
                                | PROCESS_VM_READ,
                                FALSE,
                                processEntry.th32ParentProcessID);

                    // Wait for our parent to stop.
                    DWORD exitCode;
                    GetExitCodeProcess(parentHandle, &exitCode);
                    while(exitCode == STILL_ACTIVE)
                    {
                        WaitForSingleObject(parentHandle, INFINITE);
                        GetExitCodeProcess(parentHandle, &exitCode);
                    }
                    CloseHandle(parentHandle);
                    return;
                }
            }
            while(Process32Next(handle, &processEntry));
        }
        CloseHandle(handle);
    }
}

/**
 * Invoke the callback function of the COM client when a contact has been
 * deleted from MAPI.
 *
 * @param id The contact identifer.
 */
static void Server_deleted(LPSTR id)
{
    HRESULT hr =  E_FAIL;

    IMsOutlookAddrBookClient * msOutlookAddrBookClient = NULL;
    if((hr = CoCreateInstance(
            CLSID_MsOutlookAddrBookClient,
            NULL,
            CLSCTX_LOCAL_SERVER,
            IID_IMsOutlookAddrBookClient,
            (void**) &msOutlookAddrBookClient)) == S_OK)
    {
        LPWSTR idW = StringUtils::MultiByteToWideChar(id);
        BSTR res = SysAllocString(idW);
        msOutlookAddrBookClient->deleted(res);
        SysFreeString(res);
        free(idW);
        msOutlookAddrBookClient->Release();
    }
}

/**
 * Invoke the callback function of the COM client when a contact has been
 * created from MAPI.
 *
 * @param id The contact identifer.
 */
static void Server_inserted(LPSTR id)
{
    HRESULT hr =  E_FAIL;

    IMsOutlookAddrBookClient * msOutlookAddrBookClient = NULL;
    if((hr = CoCreateInstance(
            CLSID_MsOutlookAddrBookClient,
            NULL,
            CLSCTX_LOCAL_SERVER,
            IID_IMsOutlookAddrBookClient,
            (void**) &msOutlookAddrBookClient)) == S_OK)
    {
        LPWSTR idW = StringUtils::MultiByteToWideChar(id);
        BSTR res = SysAllocString(idW);
        msOutlookAddrBookClient->inserted(res);
        SysFreeString(res);
        free(idW);
        msOutlookAddrBookClient->Release();
    }
}

/**
 * Invoke the callback function of the COM client when a contact has been
 * modified from MAPI.
 *
 * @param id The contact identifer.
 */
static void Server_updated(LPSTR id)
{
    HRESULT hr =  E_FAIL;

    IMsOutlookAddrBookClient * msOutlookAddrBookClient = NULL;
    if((hr = CoCreateInstance(
            CLSID_MsOutlookAddrBookClient,
            NULL,
            CLSCTX_LOCAL_SERVER,
            IID_IMsOutlookAddrBookClient,
            (void**) &msOutlookAddrBookClient)) == S_OK)
    {
        LPWSTR idW = StringUtils::MultiByteToWideChar(id);
        BSTR res = SysAllocString(idW);
        msOutlookAddrBookClient->updated(res);
        SysFreeString(res);
        free(idW);
        msOutlookAddrBookClient->Release();
    }
}