aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/addrbook/msoutlook/MsOutlookAddrBookContactSourceService.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'src/native/addrbook/msoutlook/MsOutlookAddrBookContactSourceService.cxx')
-rw-r--r--src/native/addrbook/msoutlook/MsOutlookAddrBookContactSourceService.cxx320
1 files changed, 173 insertions, 147 deletions
diff --git a/src/native/addrbook/msoutlook/MsOutlookAddrBookContactSourceService.cxx b/src/native/addrbook/msoutlook/MsOutlookAddrBookContactSourceService.cxx
index 88754d1..d6166c8 100644
--- a/src/native/addrbook/msoutlook/MsOutlookAddrBookContactSourceService.cxx
+++ b/src/native/addrbook/msoutlook/MsOutlookAddrBookContactSourceService.cxx
@@ -8,10 +8,14 @@
#include "MsOutlookAddrBookContactSourceService.h"
#include "com/ComClient.h"
+#include "com/MsOutlookAddrBookServerClassFactory.h"
+#include "com/MsOutlookAddrBookClientClassFactory.h"
#include "MAPINotification.h"
#include "MAPISession.h"
#include "MAPIBitness.h"
+#include "MsOutlookUtils.h"
#include <Tchar.h>
+#include "StringUtils.h"
typedef BOOL (STDAPICALLTYPE *LPFBINFROMHEX)(LPSTR, LPBYTE);
typedef void (STDAPICALLTYPE *LPFREEPROWS)(LPSRowSet);
@@ -35,9 +39,6 @@ static LPMAPIUNINITIALIZE
MsOutlookAddrBookContactSourceService_mapiUninitialize;
static HMODULE MsOutlookAddrBookContactSourceService_hMapiLib = NULL;
-static jboolean
-MsOutlookAddrBookContactSourceService_isValidDefaultMailClient
- (LPCTSTR name, DWORD nameLength);
HRESULT MsOutlookAddrBookContactSourceService_startComServer(void);
HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
@@ -66,7 +67,7 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
255 // The size limit of key name as documented in MSDN
+ 20 // \Outlook\InstallRoot
+ 1]; // The terminating null character
-
+ MsOutlookUtils_logInfo("Searching for outlook InstallRoot.");
while (1)
{
LONG regEnumKeyEx;
@@ -86,15 +87,23 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
NULL,
NULL);
if (ERROR_NO_MORE_ITEMS == regEnumKeyEx)
+ {
+ MsOutlookUtils_logInfo("No more Software\\Microsoft\\Office items.");
break;
+ }
i++;
if (ERROR_SUCCESS != regEnumKeyEx)
+ {
+ MsOutlookUtils_logInfo("Error quering the next Software\\Microsoft\\Office item.");
continue;
+ }
str = installRootKeyName + subkeyNameLength;
memcpy(str, _T("\\Outlook\\InstallRoot"), 20 * sizeof(TCHAR));
*(str + 20) = 0;
+ MsOutlookUtils_log("Trying to open the following key:");
+ MsOutlookUtils_log(installRootKeyName);
if (ERROR_SUCCESS
== RegOpenKeyEx(
regKey,
@@ -103,6 +112,7 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
KEY_QUERY_VALUE,
&installRootKey))
{
+ MsOutlookUtils_logInfo("The key is opened successfully.");
if ((ERROR_SUCCESS
== RegQueryValueEx(
installRootKey,
@@ -114,6 +124,7 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
&& (REG_SZ == pathValueType)
&& pathValueSize)
{
+ MsOutlookUtils_logInfo("Path value found.");
LPTSTR pathValue;
// MSDN says "the string may not have been stored with the
@@ -129,7 +140,10 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
{
pathValue = (LPTSTR)::malloc(pathValueSize);
if (!pathValue)
+ {
+ MsOutlookUtils_logInfo("Error with memory allocation for the pathValue.");
continue;
+ }
}
if (ERROR_SUCCESS
@@ -140,10 +154,12 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
NULL,
(LPBYTE) pathValue, &pathValueSize))
{
+ MsOutlookUtils_logInfo("The path value is retrieved");
DWORD pathValueLength = pathValueSize / sizeof(TCHAR);
if (pathValueLength)
{
+ MsOutlookUtils_logInfo("The path value is retrieved successfully. The length is not 0.");
DWORD fileAttributes;
str = pathValue + (pathValueLength - 1);
@@ -151,18 +167,42 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
str++;
memcpy(str, _T("\\Outlook.exe"), 12 * sizeof(TCHAR));
*(str + 12) = 0;
-
+ MsOutlookUtils_logInfo("Trying to retrieve atributes for:");
+ MsOutlookUtils_logInfo(pathValue);
fileAttributes = GetFileAttributes(pathValue);
if (INVALID_FILE_ATTRIBUTES != fileAttributes)
+ {
+ MsOutlookUtils_logInfo("The file exists.");
hResult = S_OK;
+ }
+ else
+ {
+ MsOutlookUtils_logInfo("The file doesn't exists");
+ }
}
+ else
+ {
+ MsOutlookUtils_logInfo("Error - the length of the path value is 0.");
+ }
}
+ else
+ {
+ MsOutlookUtils_logInfo("Error retrieving the pathValue.");
+ }
if (pathValue != installRootKeyName)
free(pathValue);
}
+ else
+ {
+ MsOutlookUtils_logInfo("Error Path value not found.");
+ }
RegCloseKey(installRootKey);
}
+ else
+ {
+ MsOutlookUtils_logInfo("Error openning the key.");
+ }
}
RegCloseKey(regKey);
@@ -171,119 +211,36 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
// client.
if (HR_SUCCEEDED(hResult))
{
- DWORD defaultValueType;
- // The buffer installRootKeyName is long enough to receive
- // "Microsoft Outlook" so use it in order to not have to allocate
- // more memory.
- LPTSTR defaultValue = (LPTSTR) installRootKeyName;
- DWORD defaultValueCapacity = sizeof(installRootKeyName);
- jboolean checkHKeyLocalMachine;
-
- hResult = MAPI_E_NO_SUPPORT;
- if (ERROR_SUCCESS
- == RegOpenKeyEx(
- HKEY_CURRENT_USER,
- _T("Software\\Clients\\Mail"),
- 0,
- KEY_QUERY_VALUE,
- &regKey))
- {
- DWORD defaultValueSize = defaultValueCapacity;
- LONG regQueryValueEx = RegQueryValueEx(
- regKey,
- NULL,
- NULL,
- &defaultValueType,
- (LPBYTE) defaultValue,
- &defaultValueSize);
-
- switch (regQueryValueEx)
- {
- case ERROR_SUCCESS:
- {
- if (REG_SZ == defaultValueType)
- {
- DWORD defaultValueLength
- = defaultValueSize / sizeof(TCHAR);
-
- if (JNI_TRUE
- == MsOutlookAddrBookContactSourceService_isValidDefaultMailClient(
- defaultValue,
- defaultValueLength))
- {
- checkHKeyLocalMachine = JNI_FALSE;
- if (_tcsnicmp(
- _T("Microsoft Outlook"), defaultValue,
- defaultValueLength)
- == 0)
- hResult = S_OK;
- }
- else
- checkHKeyLocalMachine = JNI_TRUE;
- }
- else
- checkHKeyLocalMachine = JNI_FALSE;
- break;
- }
- case ERROR_FILE_NOT_FOUND:
- checkHKeyLocalMachine = JNI_TRUE;
- break;
- case ERROR_MORE_DATA:
- checkHKeyLocalMachine = JNI_FALSE;
- break;
- default:
- checkHKeyLocalMachine = JNI_FALSE;
- break;
- }
- RegCloseKey(regKey);
- }
- else
- checkHKeyLocalMachine = JNI_TRUE;
- if ((JNI_TRUE == checkHKeyLocalMachine)
- && (ERROR_SUCCESS
- == RegOpenKeyEx(
- HKEY_LOCAL_MACHINE,
- _T("Software\\Clients\\Mail"),
- 0,
- KEY_QUERY_VALUE,
- &regKey)))
- {
- DWORD defaultValueSize = defaultValueCapacity;
- LONG regQueryValueEx
- = RegQueryValueEx(
- regKey,
- NULL,
- NULL,
- &defaultValueType,
- (LPBYTE) defaultValue, &defaultValueSize);
-
- if ((ERROR_SUCCESS == regQueryValueEx)
- && (REG_SZ == defaultValueType))
- {
- DWORD defaultValueLength = defaultValueSize / sizeof(TCHAR);
-
- if ((_tcsnicmp(
- _T("Microsoft Outlook"), defaultValue,
- defaultValueLength)
- == 0)
- && (JNI_TRUE
- == MsOutlookAddrBookContactSourceService_isValidDefaultMailClient(_T("Microsoft Outlook"), 17)))
- hResult = S_OK;
- }
- RegCloseKey(regKey);
- }
+ if(MsOutlookUtils_isOutlookDefaultMailClient())
+ {
+ hResult = S_OK;
+ }
+ else
+ {
+ hResult = MAPI_E_NO_SUPPORT;
+ }
}
+ else
+ {
+ MsOutlookUtils_logInfo("Outlook is not installed.");
+ }
}
+ else
+ {
+ MsOutlookUtils_logInfo("Error opening HKLM\\Software\\Microsoft\\Office registry.");
+ }
// If we've determined that we'd like to go on with MAPI, try to load it.
if (HR_SUCCEEDED(hResult))
{
+ MsOutlookUtils_logInfo("Loading MAPI.");
MsOutlookAddrBookContactSourceService_hMapiLib
= ::LoadLibrary(_T("mapi32.dll"));
hResult = MAPI_E_NO_SUPPORT;
if(MsOutlookAddrBookContactSourceService_hMapiLib)
{
+ MsOutlookUtils_logInfo("Loading MAPI functions");
// get and check function pointers
MsOutlookAddrBookContactSourceService_mapiInitialize
= (LPMAPIINITIALIZE) GetProcAddress(
@@ -398,6 +355,7 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
if(HR_SUCCEEDED(hResult)
&& MAPISession_getMapiSession() == NULL)
{
+ MsOutlookUtils_logInfo("MAPI logon.");
LPMAPISESSION mapiSession = NULL;
hResult = MsOutlookAddrBook_mapiLogonEx(
0,
@@ -408,11 +366,20 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
&mapiSession);
if(HR_SUCCEEDED(hResult))
{
+ MsOutlookUtils_logInfo("MAPI logon success.");
// Register the notification of contact changed,
// created and deleted.
MAPINotification_registerNotifyAllMsgStores(
mapiSession);
}
+ else
+ {
+ MsOutlookUtils_logInfo("MAPI logon error.");
+ }
+ }
+ else
+ {
+ MsOutlookUtils_logInfo("Error calling MAPI init from MAPI library.");
}
::SetCurrentDirectory(lpszWorkingDir);
MAPISession_unlock();
@@ -420,6 +387,7 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
else
{
hResult = HRESULT_FROM_WIN32(::GetLastError());
+ MsOutlookUtils_logInfo("Error getting current directory.[1]");
}
::free(lpszWorkingDir);
@@ -427,15 +395,30 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
else
{
hResult = HRESULT_FROM_WIN32(::GetLastError());
+ MsOutlookUtils_logInfo("Error getting current directory.[2]");
}
}
+ else
+ {
+ MsOutlookUtils_logInfo("Cannot get MAPI functions.");
+ }
+ }
+ else
+ {
+ MsOutlookUtils_logInfo("Error while loading MAPI library.");
}
}
+ else
+ {
+ MsOutlookUtils_logInfo("ERROR - we won't load MAPI.");
+ }
if (HR_FAILED(hResult))
{
+ MsOutlookUtils_logInfo("ERROR - in MAPI native init.");
if(MsOutlookAddrBookContactSourceService_hMapiLib)
{
+ MsOutlookUtils_logInfo("ERROR - free MAPI library.");
FreeLibrary(MsOutlookAddrBookContactSourceService_hMapiLib);
MsOutlookAddrBookContactSourceService_hMapiLib = NULL;
}
@@ -454,13 +437,40 @@ HRESULT MsOutlookAddrBookContactSourceService_MAPIInitializeCOMServer(void)
HRESULT hr = E_FAIL;
MAPISession_lock();
+ MsOutlookUtils_log("Init com server.");
+
+ IMsOutlookAddrBookServer * ComClient_iServer = NULL;
+ if((hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)) == S_OK
+ || hr == S_FALSE)
+ {
+ if((hr = CoCreateInstance(
+ CLSID_MsOutlookAddrBookServer,
+ NULL,
+ CLSCTX_LOCAL_SERVER,
+ IID_IMsOutlookAddrBookServer,
+ (void**) &ComClient_iServer)) == S_OK)
+ {
+ MsOutlookUtils_log("COM Server already started");
+ if(ComClient_iServer)
+ {
+ ComClient_iServer->Release();
+ ComClient_iServer = NULL;
+ }
+ return E_FAIL;
+ }
+ }
// Start COM service
if((hr = MsOutlookAddrBookContactSourceService_startComServer()) == S_OK)
{
+ MsOutlookUtils_log("COM Server started.");
// Start COM client
ComClient_start();
}
+ else
+ {
+ MsOutlookUtils_log("Failed to start COM Server.");
+ }
MAPISession_unlock();
@@ -537,6 +547,7 @@ HRESULT MsOutlookAddrBookContactSourceService_NativeMAPIInitialize
(jlong version, jlong flags,
void * deletedMethod, void * insertedMethod, void * updatedMethod)
{
+ MsOutlookUtils_logInfo("MAPI native init.");
MAPINotification_registerNativeNotificationsDelegate(
deletedMethod, insertedMethod, updatedMethod);
@@ -550,43 +561,6 @@ void MsOutlookAddrBookContactSourceService_NativeMAPIUninitialize(void)
MsOutlookAddrBookContactSourceService_MAPIUninitialize();
}
-static jboolean
-MsOutlookAddrBookContactSourceService_isValidDefaultMailClient
- (LPCTSTR name, DWORD nameLength)
-{
- jboolean validDefaultMailClient = JNI_FALSE;
-
- if ((0 != nameLength) && (0 != name[0]))
- {
- LPTSTR str;
- TCHAR keyName[
- 22 /* Software\Clients\Mail\ */
- + 255
- + 1 /* The terminating null character */];
- HKEY key;
-
- str = keyName;
- _tcsncpy(str, _T("Software\\Clients\\Mail\\"), 22);
- str += 22;
- if (nameLength > 255)
- nameLength = 255;
- _tcsncpy(str, name, nameLength);
- *(str + nameLength) = 0;
-
- if (ERROR_SUCCESS
- == RegOpenKeyEx(
- HKEY_LOCAL_MACHINE,
- keyName,
- 0,
- KEY_QUERY_VALUE,
- &key))
- {
- validDefaultMailClient = JNI_TRUE;
- RegCloseKey(key);
- }
- }
- return validDefaultMailClient;
-}
/**
* Starts the COM server.
@@ -620,11 +594,50 @@ HRESULT MsOutlookAddrBookContactSourceService_startComServer(void)
memset(&processInfo, 0, sizeof(processInfo));
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_HIDE;
-
+ char* loggerPath = MsOutlookUtils_getLoggerPath();
+ int loggerPathLenght = 0;
+ char* comServerWithLogger;
+ char* appNameWithLogger;
+ char* loggerPathEscaped = NULL;
+ if(loggerPath != NULL)
+ {
+ int loggerLevel = MsOutlookUtils_getLoggerLevel();
+ char* loggerPathEscaped = (char* ) malloc(strlen(loggerPath) *
+ sizeof(char) * 2);
+ int i = 0;
+ while(*loggerPath != '\0')
+ {
+ *(loggerPathEscaped + i) = *loggerPath;
+ i++;
+ if(*loggerPath == '\\')
+ {
+ *(loggerPathEscaped + i) = '\\';
+ i++;
+ }
+ loggerPath++;
+ }
+ *(loggerPathEscaped + i) = '\0';
+ loggerPathLenght = strlen(loggerPathEscaped);
+ comServerWithLogger
+ = (char*) malloc(
+ (FILENAME_MAX + loggerPathLenght) * sizeof(char));
+ appNameWithLogger
+ = (char*) malloc(
+ (FILENAME_MAX + loggerPathLenght) * sizeof(char));
+ sprintf(comServerWithLogger, "%s \"%s\" %d", comServer,
+ loggerPathEscaped, loggerLevel);
+ sprintf(appNameWithLogger, "%s \"%s\" %d", applicationName
+ , loggerPathEscaped, loggerLevel);
+ }
+ else
+ {
+ comServerWithLogger = comServer;
+ appNameWithLogger = applicationName;
+ }
// Test 2 files: 0 for the build version, 1 for the git source version.
char * serverExec[2];
- serverExec[0] = comServer;
- serverExec[1] = applicationName;
+ serverExec[0] = comServerWithLogger;
+ serverExec[1] = appNameWithLogger;
for(int i = 0; i < 2; ++i)
{
// Create the COM server
@@ -637,10 +650,23 @@ HRESULT MsOutlookAddrBookContactSourceService_startComServer(void)
{
MsOutlookAddrBookContactSourceService_comServerHandle
= processInfo.hProcess;
-
+ MsOutlookUtils_logInfo(serverExec[i]);
+ MsOutlookUtils_logInfo("COM Server started successful.[1]");
+ if(loggerPath != NULL)
+ {
+ free(comServerWithLogger);
+ free(appNameWithLogger);
+ }
+ MsOutlookUtils_logInfo("COM Server started successful.[2]");
return S_OK;
}
}
+ if(loggerPath != NULL)
+ {
+ free(comServerWithLogger);
+ free(appNameWithLogger);
+ free(loggerPathEscaped);
+ }
}
return E_FAIL;