1800f9471SAndrew Rist /**************************************************************
2800f9471SAndrew Rist  *
3800f9471SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4800f9471SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5800f9471SAndrew Rist  * distributed with this work for additional information
6800f9471SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7800f9471SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8800f9471SAndrew Rist  * "License"); you may not use this file except in compliance
9800f9471SAndrew Rist  * with the License.  You may obtain a copy of the License at
10800f9471SAndrew Rist  *
11800f9471SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12800f9471SAndrew Rist  *
13800f9471SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14800f9471SAndrew Rist  * software distributed under the License is distributed on an
15800f9471SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16800f9471SAndrew Rist  * KIND, either express or implied.  See the License for the
17800f9471SAndrew Rist  * specific language governing permissions and limitations
18800f9471SAndrew Rist  * under the License.
19800f9471SAndrew Rist  *
20800f9471SAndrew Rist  *************************************************************/
21800f9471SAndrew Rist 
22cdf0e10cSrcweir // XMergeFilter.cpp: implementation of the CXMergeFilter class.
23cdf0e10cSrcweir //
24cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
25cdf0e10cSrcweir 
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "stdafx.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "XMergeFilter.h"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <string>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #define ERR_NOJAVA       1
35cdf0e10cSrcweir #define ERR_BADCLASSPATH 2
36cdf0e10cSrcweir #define ERR_INITJAVA     3
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPSWExportCLSID		= _T("{BDD611C3-7BAB-460F-8711-5B9AC9EF6020}");
40cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPSWExportExt		= _T("sxw");
41ff3f4ebcSOliver-Rainer Wittmann const LPTSTR CXMergeFilter::m_pszPSWExportDesc		= _T("OpenOffice.org XML Writer Document");
42ff3f4ebcSOliver-Rainer Wittmann const LPTSTR CXMergeFilter::m_pszPSWExportShortDesc	= _T("OpenOffice.org XML Writer");
43cdf0e10cSrcweir 
44cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPSWImportCLSID		= _T("{CB43F086-838D-4FA4-B5F6-3406B9A57439}");
45cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPSWImportExt		= _T("psw");
46cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPSWImportDesc		= _T("Pocket Word Document - Pocket PC");
47cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPSWImportShortDesc	= _T("Pocket Word");
48cdf0e10cSrcweir 
49cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPXLExportCLSID		= _T("{C6AB3E74-9F4F-4370-8120-A8A6FABB7A7C}");
50cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPXLExportExt		= _T("sxc");
51ff3f4ebcSOliver-Rainer Wittmann const LPTSTR CXMergeFilter::m_pszPXLExportDesc		= _T("OpenOffice.org XML Calc Document");
52ff3f4ebcSOliver-Rainer Wittmann const LPTSTR CXMergeFilter::m_pszPXLExportShortDesc	= _T("OpenOffice.org XML Calc");
53cdf0e10cSrcweir 
54cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPXLImportCLSID		= _T("{43887C67-4D5D-4127-BAAC-87A288494C7C}");
55cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPXLImportExt		= _T("pxl");
56cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPXLImportDesc		= _T("Pocket Excel Document - Pocket PC");
57cdf0e10cSrcweir const LPTSTR CXMergeFilter::m_pszPXLImportShortDesc	= _T("Pocket Excel");
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
61cdf0e10cSrcweir // Construction/Destruction
62cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
63cdf0e10cSrcweir 
CXMergeFilter()64cdf0e10cSrcweir CXMergeFilter::CXMergeFilter() : m_cRef(1)
65cdf0e10cSrcweir {
66cdf0e10cSrcweir 	m_bHaveExcel = FALSE;
67cdf0e10cSrcweir 	m_bHaveWord  = FALSE;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	m_szClasspath   = NULL;
70cdf0e10cSrcweir 	m_szJavaBaseDir = NULL;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
~CXMergeFilter()73cdf0e10cSrcweir CXMergeFilter::~CXMergeFilter()
74cdf0e10cSrcweir {
75cdf0e10cSrcweir 	if (m_szClasspath != NULL)
76cdf0e10cSrcweir 	{
77cdf0e10cSrcweir 		delete m_szClasspath;
78cdf0e10cSrcweir 	}
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	if (m_szJavaBaseDir != NULL)
81cdf0e10cSrcweir 	{
82cdf0e10cSrcweir 		delete m_szJavaBaseDir;
83cdf0e10cSrcweir 	}
84cdf0e10cSrcweir 
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 
88cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
89cdf0e10cSrcweir // IUnknown Methods
90cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
91cdf0e10cSrcweir 
QueryInterface(REFIID riid,void ** ppvObject)92cdf0e10cSrcweir STDMETHODIMP CXMergeFilter::QueryInterface(REFIID riid, void **ppvObject)
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	if(ppvObject == NULL)
95cdf0e10cSrcweir 		return E_INVALIDARG;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     if (::IsEqualIID(riid, IID_IUnknown))
98cdf0e10cSrcweir 	{
99cdf0e10cSrcweir         *ppvObject = static_cast<IUnknown *>(this);
100cdf0e10cSrcweir 	}
101cdf0e10cSrcweir     else if (::IsEqualIID(riid, IID_ICeFileFilter))
102cdf0e10cSrcweir 	{
103cdf0e10cSrcweir         *ppvObject = static_cast<ICeFileFilter *>(this);
104cdf0e10cSrcweir 	}
105cdf0e10cSrcweir 	else
106cdf0e10cSrcweir 	{
107cdf0e10cSrcweir 		*ppvObject = NULL;
108cdf0e10cSrcweir 		return E_NOINTERFACE;
109cdf0e10cSrcweir 	}
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	reinterpret_cast<IUnknown *>(*ppvObject)->AddRef();
112cdf0e10cSrcweir 	return S_OK;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 
STDMETHODIMP_(ULONG)116cdf0e10cSrcweir STDMETHODIMP_(ULONG) CXMergeFilter::AddRef()
117cdf0e10cSrcweir {
118cdf0e10cSrcweir 	return ::InterlockedIncrement(&m_cRef);
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 
STDMETHODIMP_(ULONG)122cdf0e10cSrcweir STDMETHODIMP_(ULONG) CXMergeFilter::Release()
123cdf0e10cSrcweir {
124cdf0e10cSrcweir 	if(::InterlockedDecrement(&m_cRef) == 0)
125cdf0e10cSrcweir 	{
126cdf0e10cSrcweir 		delete this;
127cdf0e10cSrcweir 		return 0;
128cdf0e10cSrcweir 	}
129cdf0e10cSrcweir 	return m_cRef;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
133cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
134cdf0e10cSrcweir // ICeFileFilter
135cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////
136cdf0e10cSrcweir 
FilterOptions(HWND hwndParent)137cdf0e10cSrcweir STDMETHODIMP CXMergeFilter::FilterOptions(HWND hwndParent)
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	// We don't currently allow any options
140cdf0e10cSrcweir 	return HRESULT_FROM_WIN32(NOERROR);
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
FormatMessage(DWORD dwFlags,DWORD dwMessageId,DWORD dwLanguageId,LPTSTR lpBuffer,DWORD nSize,va_list * Arguments,DWORD * pcb)143cdf0e10cSrcweir STDMETHODIMP CXMergeFilter::FormatMessage(DWORD dwFlags, DWORD dwMessageId,
144cdf0e10cSrcweir 						DWORD dwLanguageId, LPTSTR lpBuffer, DWORD nSize,
145cdf0e10cSrcweir 						va_list *Arguments, DWORD *pcb)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir 	TCHAR errMsg[1024];
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	HKEY  hKey   = NULL;
150cdf0e10cSrcweir 	DWORD dwSize = 1024;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	long lRet = 0;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	// Attempt to find the messages in the registry
156cdf0e10cSrcweir 	lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Sun Microsystems\\StarOffice\\XMergeSync\\Messages\\Error"),
157cdf0e10cSrcweir 							0, KEY_READ, &hKey);
158cdf0e10cSrcweir 	if (lRet != ERROR_SUCCESS)
159cdf0e10cSrcweir 	{
160cdf0e10cSrcweir 		// Try the user's portion of the registry
161cdf0e10cSrcweir 		lRet = ::RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Sun Microsystems\\StarOffice\\XMergeSync\\Messages\\Error"),
162cdf0e10cSrcweir 							0, KEY_READ, &hKey);
163cdf0e10cSrcweir 		if (lRet != ERROR_SUCCESS)
164cdf0e10cSrcweir 		{
165cdf0e10cSrcweir 			hKey = NULL;
166cdf0e10cSrcweir 		}
167cdf0e10cSrcweir 	}
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 	switch(dwMessageId)
171cdf0e10cSrcweir 	{
172cdf0e10cSrcweir 	case ERR_NOJAVA:
173cdf0e10cSrcweir 		lRet = ::RegQueryValueEx(hKey, _T("Java"), 0, NULL, (LPBYTE)errMsg, &dwSize);
174cdf0e10cSrcweir 		if (lRet != ERROR_SUCCESS)
175cdf0e10cSrcweir 		{
176cdf0e10cSrcweir 			lstrcpy(errMsg, "Unable to locate Java 1.4/1.5 installation.");
177cdf0e10cSrcweir 		}
178cdf0e10cSrcweir 		break;
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	case ERR_BADCLASSPATH:
181cdf0e10cSrcweir 		lRet = ::RegQueryValueEx(hKey, _T("Classpath"), 0, NULL, (LPBYTE)errMsg, &dwSize);
182cdf0e10cSrcweir 		if (lRet != ERROR_SUCCESS)
183cdf0e10cSrcweir 		{
184cdf0e10cSrcweir 			lstrcpy(errMsg, "Unable to locate XMerge Jar files.");
185cdf0e10cSrcweir 		}
186cdf0e10cSrcweir 		break;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	case ERR_INITJAVA:
189cdf0e10cSrcweir 		lRet = ::RegQueryValueEx(hKey, _T("JavaInit"), 0, NULL, (LPBYTE)errMsg, &dwSize);
190cdf0e10cSrcweir 		if (lRet != ERROR_SUCCESS)
191cdf0e10cSrcweir 		{
192cdf0e10cSrcweir 			lstrcpy(errMsg, "Error initialising the Java Runtime Environment.");
193cdf0e10cSrcweir 		}
194cdf0e10cSrcweir 		break;
195cdf0e10cSrcweir 	}
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	char* buf = (char*)LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, (lstrlen(errMsg) + 1) * sizeof(TCHAR));
198cdf0e10cSrcweir 	lstrcpyn(buf, errMsg, lstrlen(errMsg));
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 	*(char**)lpBuffer = buf;
201cdf0e10cSrcweir 	*pcb = strlen(errMsg);
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 	return HRESULT_FROM_WIN32(NOERROR);
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 
NextConvertFile(int nConversion,CFF_CONVERTINFO * pci,CFF_SOURCEFILE * psf,CFF_DESTINATIONFILE * pdf,volatile BOOL * pbCancel,CF_ERROR * perr)207cdf0e10cSrcweir STDMETHODIMP CXMergeFilter::NextConvertFile(int nConversion, CFF_CONVERTINFO *pci,
208cdf0e10cSrcweir 							 CFF_SOURCEFILE *psf, CFF_DESTINATIONFILE *pdf,
209cdf0e10cSrcweir 							 volatile BOOL *pbCancel, CF_ERROR *perr)
210cdf0e10cSrcweir {
211cdf0e10cSrcweir 	std::string appArgs;
212cdf0e10cSrcweir 	std::string appName;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	STARTUPINFO si;
215cdf0e10cSrcweir     PROCESS_INFORMATION pi;
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     ZeroMemory( &si, sizeof(si) );
218cdf0e10cSrcweir 	ZeroMemory( &pi, sizeof(pi) );
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 	si.cb = sizeof(si);
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 	/*
224cdf0e10cSrcweir 	 * First step: Locate Java and establish the classpath.  If these can't
22586e1cf34SPedro Giffuni 	 *             be done successfully, then avoid all further processing.
226cdf0e10cSrcweir 	 */
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 	// Locate Java Home if it hasn't already been done.
229cdf0e10cSrcweir 	if (m_szJavaBaseDir == NULL)
230cdf0e10cSrcweir 	{
231cdf0e10cSrcweir 		m_szJavaBaseDir = GetJavaBaseDir();
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 		if (m_szJavaBaseDir == NULL)
234cdf0e10cSrcweir 		{
235cdf0e10cSrcweir 			*perr = ERR_NOJAVA;
236cdf0e10cSrcweir 			return HRESULT_FROM_WIN32(E_FAIL);
237cdf0e10cSrcweir 		}
238cdf0e10cSrcweir 	}
239cdf0e10cSrcweir 
240ff3f4ebcSOliver-Rainer Wittmann 	// Get the Apache OpenOffice class directory
241cdf0e10cSrcweir 	if (m_szClasspath == NULL)
242cdf0e10cSrcweir 	{
243cdf0e10cSrcweir 		m_szClasspath = GetXMergeClassPath();
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 		if (m_szClasspath == NULL)
246cdf0e10cSrcweir 		{
247cdf0e10cSrcweir 			*perr = ERR_BADCLASSPATH;
248cdf0e10cSrcweir 			return HRESULT_FROM_WIN32(E_FAIL);
249cdf0e10cSrcweir 		}
250cdf0e10cSrcweir 	}
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 	/*
254cdf0e10cSrcweir 	 * Second step:  Check the files we're going to process.  If we don't have
255cdf0e10cSrcweir 	 *				 an XMerge plugin for the file then we can't convert.
256cdf0e10cSrcweir 	 */
257cdf0e10cSrcweir 	if ((!lstrcmp(psf->szExtension, "sxw")  || !lstrcmp(psf->szExtension, "psw"))
258cdf0e10cSrcweir 		    && !m_bHaveWord)
259cdf0e10cSrcweir 	{
260cdf0e10cSrcweir 		*perr = ERR_BADCLASSPATH;
261cdf0e10cSrcweir 		return HRESULT_FROM_WIN32(E_FAIL);
262cdf0e10cSrcweir 	}
263cdf0e10cSrcweir 	else if ((!lstrcmp(psf->szExtension, "sxc")  || !lstrcmp(psf->szExtension, "pxl"))
264cdf0e10cSrcweir 				 && !m_bHaveExcel)
265cdf0e10cSrcweir 	{
266cdf0e10cSrcweir 		*perr = ERR_BADCLASSPATH;
267cdf0e10cSrcweir 		return HRESULT_FROM_WIN32(E_FAIL);
268cdf0e10cSrcweir 	}
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 	/*
272cdf0e10cSrcweir 	 * Third step:  Locate the Java executable and build and execute the command
273cdf0e10cSrcweir 	 *				line to carry out the conversion.
274cdf0e10cSrcweir 	 */
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 	// Find the Java executable and make sure it exists
277cdf0e10cSrcweir 	appName += m_szJavaBaseDir;
278cdf0e10cSrcweir 	appName += "\\bin\\javaw.exe";
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 	if (GetFileAttributes(appName.c_str()) == INVALID_FILE_SIZE)
281cdf0e10cSrcweir 	{
282cdf0e10cSrcweir 		*perr = ERR_NOJAVA;
283cdf0e10cSrcweir 		return HRESULT_FROM_WIN32(E_FAIL);
284cdf0e10cSrcweir 	}
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 	// Wrap the executable path in quotes in case of spaces
287cdf0e10cSrcweir 	appName.insert(0, "\"");
288cdf0e10cSrcweir 	appName.append("\"");
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 	// Need to build the entire command line for calling out to Java
293*55c2ab0dSArrigo Marchiori 	appArgs = appName;
294*55c2ab0dSArrigo Marchiori         if (*m_szClasspath) {
295*55c2ab0dSArrigo Marchiori             appArgs += " -Djava.class.path=";
296*55c2ab0dSArrigo Marchiori             appArgs += m_szClasspath;
297*55c2ab0dSArrigo Marchiori         } // else avoid empty class path
298cdf0e10cSrcweir 	appArgs += " org.openoffice.xmerge.util.ActiveSyncDriver ";
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	if (!lstrcmp(psf->szExtension, "sxw"))
301cdf0e10cSrcweir 	{
302cdf0e10cSrcweir 		appArgs += "staroffice/sxw ";
303cdf0e10cSrcweir 		appArgs += "application/x-pocket-word ";
304cdf0e10cSrcweir 	}
305cdf0e10cSrcweir 	else if(!lstrcmp(psf->szExtension, "psw"))
306cdf0e10cSrcweir 	{
307cdf0e10cSrcweir 		appArgs += "application/x-pocket-word ";
308cdf0e10cSrcweir 		appArgs += "staroffice/sxw ";
309cdf0e10cSrcweir 	}
310cdf0e10cSrcweir 	else if(!lstrcmp(psf->szExtension, "sxc"))
311cdf0e10cSrcweir 	{
312cdf0e10cSrcweir 		appArgs += "staroffice/sxc ";
313cdf0e10cSrcweir 		appArgs += "application/x-pocket-excel ";
314cdf0e10cSrcweir 	}
315cdf0e10cSrcweir 	else if(!lstrcmp(psf->szExtension, "pxl"))
316cdf0e10cSrcweir 	{
317cdf0e10cSrcweir 		appArgs += "application/x-pocket-excel ";
318cdf0e10cSrcweir 		appArgs += "staroffice/sxc ";
319cdf0e10cSrcweir 	}
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	// ActiveSync sometimes gives out long file names, especially when automatically syncing
323cdf0e10cSrcweir 	appArgs += "\"";
324cdf0e10cSrcweir 	appArgs += psf->szFullpath;
325cdf0e10cSrcweir 	appArgs += "\" \"";
326cdf0e10cSrcweir 	appArgs += pdf->szFullpath;
327cdf0e10cSrcweir 	appArgs += "\"";
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 	if(!CreateProcess(NULL,
330cdf0e10cSrcweir 				  (char*)appArgs.c_str(),
331cdf0e10cSrcweir 				  NULL,				// No Process Attributes
332cdf0e10cSrcweir 				  NULL,				// No Thread Attributes
333cdf0e10cSrcweir 				  FALSE,			// Don't want this process getting handles
334cdf0e10cSrcweir 				  CREATE_NO_WINDOW,	// No console
335cdf0e10cSrcweir 				  NULL,				// No special environment
336cdf0e10cSrcweir 				  NULL,				// Current Working Directory is okay
337cdf0e10cSrcweir 				  &si,
338cdf0e10cSrcweir 				  &pi))
339cdf0e10cSrcweir 	{
340cdf0e10cSrcweir 		*perr = ERR_INITJAVA;
341cdf0e10cSrcweir 		return HRESULT_FROM_WIN32(E_FAIL);
342cdf0e10cSrcweir 	}
343cdf0e10cSrcweir 
344cdf0e10cSrcweir 	// Wait for the new process to work
345cdf0e10cSrcweir 	WaitForSingleObject(pi.hProcess, INFINITE);
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 	CloseHandle(pi.hProcess);
348cdf0e10cSrcweir 	CloseHandle(pi.hThread);
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 	return HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
351cdf0e10cSrcweir }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 
354cdf0e10cSrcweir typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD, LPTSTR );
355cdf0e10cSrcweir 
356cdf0e10cSrcweir 
GetJavaBaseDir()357cdf0e10cSrcweir TCHAR* CXMergeFilter::GetJavaBaseDir()
358cdf0e10cSrcweir {
359cdf0e10cSrcweir 	HRESULT lRet;
360cdf0e10cSrcweir 
361cdf0e10cSrcweir 	HKEY hKey = NULL;
362cdf0e10cSrcweir 	HKEY hDataKey = NULL;
363cdf0e10cSrcweir 
364cdf0e10cSrcweir 	TCHAR szClassName[_MAX_PATH] = "\0";
365cdf0e10cSrcweir 	TCHAR szKeyName[_MAX_PATH]   = "\0";
366cdf0e10cSrcweir     TCHAR szCurrentJava[_MAX_PATH] = "\0";
367cdf0e10cSrcweir 	DWORD dwClassName            = _MAX_PATH;
368cdf0e10cSrcweir 	DWORD dwKeyName              = _MAX_PATH;
369cdf0e10cSrcweir 
370cdf0e10cSrcweir 	/*
371cdf0e10cSrcweir 	 * Java leaves registry keys at HKLM\SOFTWARE\JavaSoft.
372cdf0e10cSrcweir 	 *
373cdf0e10cSrcweir 	 * Check for a JRE installation first
374cdf0e10cSrcweir 	 */
375cdf0e10cSrcweir 	lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\JavaSoft\\Java Runtime Environment"), 0, KEY_READ, &hKey);
376cdf0e10cSrcweir 	if (lRet != ERROR_SUCCESS)
377cdf0e10cSrcweir 		return NULL;
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 	// Locations shouldn't be greater than _MAX_PATH
380cdf0e10cSrcweir 	TCHAR*  szJavaHome = new TCHAR[_MAX_PATH + 1];
381cdf0e10cSrcweir 	DWORD dwSize = _MAX_PATH + 1;
382cdf0e10cSrcweir 
383cdf0e10cSrcweir     /* use current version */
384cdf0e10cSrcweir     lRet = ::RegQueryValueEx(hKey, _T("CurrentVersion"), 0, NULL, (LPBYTE)szCurrentJava, &dwSize);
385cdf0e10cSrcweir 
386cdf0e10cSrcweir     /*
387cdf0e10cSrcweir 	for (DWORD i = 0; lRet != ERROR_NO_MORE_ITEMS; i++)
388cdf0e10cSrcweir 	{
389cdf0e10cSrcweir 		lRet = ::RegEnumKeyEx(hKey, i, szKeyName, &dwKeyName, 0, szClassName, &dwClassName, NULL);
390cdf0e10cSrcweir 		if(!strncmp(szKeyName, "1.4", 3))
391cdf0e10cSrcweir 			break;
392cdf0e10cSrcweir 		dwKeyName = _MAX_PATH;
393cdf0e10cSrcweir 	}
394cdf0e10cSrcweir     // Found a Java 1.4 installation.  Can now read its home directory.
395cdf0e10cSrcweir     */
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 
398cdf0e10cSrcweir 	lRet = ::RegOpenKeyEx(hKey, _T(szCurrentJava), 0, KEY_READ, &hDataKey);
399cdf0e10cSrcweir 	if (lRet != ERROR_SUCCESS)
400cdf0e10cSrcweir 	{
401cdf0e10cSrcweir 		RegCloseKey(hKey);
402cdf0e10cSrcweir 		delete [] szJavaHome;
403cdf0e10cSrcweir 		return NULL;
404cdf0e10cSrcweir 	}
405cdf0e10cSrcweir 
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 	// Now read the JavaHome value
408cdf0e10cSrcweir     dwSize = _MAX_PATH + 1;
409cdf0e10cSrcweir 	lRet = ::RegQueryValueEx(hDataKey, _T("JavaHome"), 0, NULL, (LPBYTE)szJavaHome, &dwSize);
410cdf0e10cSrcweir 	if (lRet != ERROR_SUCCESS)
411cdf0e10cSrcweir 	{
412cdf0e10cSrcweir 		RegCloseKey(hDataKey);
413cdf0e10cSrcweir 		RegCloseKey(hKey);
414cdf0e10cSrcweir 		delete [] szJavaHome;
415cdf0e10cSrcweir 		return NULL;
416cdf0e10cSrcweir 	}
417cdf0e10cSrcweir 
418cdf0e10cSrcweir 	RegCloseKey(hDataKey);
419cdf0e10cSrcweir 	RegCloseKey(hKey);
420cdf0e10cSrcweir 
421cdf0e10cSrcweir 
422cdf0e10cSrcweir 	// Check that the directory exists before returning it
423cdf0e10cSrcweir 	DWORD dwAttrs = GetFileAttributes(szJavaHome);
424cdf0e10cSrcweir 
425cdf0e10cSrcweir 	if (((dwAttrs & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) || dwAttrs == INVALID_FILE_SIZE)
426cdf0e10cSrcweir 	{
427cdf0e10cSrcweir 		delete [] szJavaHome;
428cdf0e10cSrcweir 		return NULL;
429cdf0e10cSrcweir 	}
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 	return szJavaHome;
432cdf0e10cSrcweir }
433cdf0e10cSrcweir 
434cdf0e10cSrcweir 
435cdf0e10cSrcweir 
GetXMergeClassPath()436cdf0e10cSrcweir TCHAR* CXMergeFilter::GetXMergeClassPath()
437cdf0e10cSrcweir {
438cdf0e10cSrcweir 	/*
439cdf0e10cSrcweir 	 * The DLL will be installed by setup in the program directory of
440cdf0e10cSrcweir 	 * the installation.  The XMerge Jar files, if present, will be
441cdf0e10cSrcweir 	 * located in the classes directory below program.
442cdf0e10cSrcweir 	 */
443cdf0e10cSrcweir 
444cdf0e10cSrcweir 	TCHAR szJarPath[MAX_PATH];
445cdf0e10cSrcweir 	TCHAR szTmpPath[MAX_PATH];
446cdf0e10cSrcweir 
447cdf0e10cSrcweir 	ZeroMemory(szJarPath, MAX_PATH);
448cdf0e10cSrcweir 	ZeroMemory(szTmpPath, MAX_PATH);
449cdf0e10cSrcweir 
450cdf0e10cSrcweir 	WIN32_FILE_ATTRIBUTE_DATA fInfo;
451cdf0e10cSrcweir 
452cdf0e10cSrcweir 	std::string clsPath;
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 
455cdf0e10cSrcweir 	// Get the location of the module.
456cdf0e10cSrcweir 	GetModuleFileName(_Module.m_hInst, szTmpPath, MAX_PATH);
457cdf0e10cSrcweir 
458cdf0e10cSrcweir 	// Strip off the xmergesync.dll component
459cdf0e10cSrcweir 	_strlwr(szTmpPath);
460cdf0e10cSrcweir 	char* modName = strstr(szTmpPath, "xmergesync.dll");
461cdf0e10cSrcweir 	strncpy(szJarPath, szTmpPath, modName - szTmpPath);
462cdf0e10cSrcweir 
463cdf0e10cSrcweir 	// Append the classes directory
464cdf0e10cSrcweir 	strncat(szJarPath, "classes\\", 8);
465cdf0e10cSrcweir 
466cdf0e10cSrcweir 
467cdf0e10cSrcweir 	// The core xmerge.jar must be present
468cdf0e10cSrcweir 	ZeroMemory(szTmpPath, MAX_PATH);
469cdf0e10cSrcweir 	_snprintf(szTmpPath, MAX_PATH, "%s%s\0", szJarPath, "xmerge.jar");
470cdf0e10cSrcweir 
471cdf0e10cSrcweir 	if (!GetFileAttributesEx(szTmpPath, GetFileExInfoStandard, &fInfo))
472cdf0e10cSrcweir 	{
473cdf0e10cSrcweir 		return NULL;
474cdf0e10cSrcweir 	}
475cdf0e10cSrcweir 	else
476cdf0e10cSrcweir 	{
477cdf0e10cSrcweir 		clsPath += szTmpPath;
478cdf0e10cSrcweir 		clsPath += ";";
479cdf0e10cSrcweir 	}
480cdf0e10cSrcweir 
481cdf0e10cSrcweir 
482cdf0e10cSrcweir 	// Now check for Pocket Word
483cdf0e10cSrcweir 	ZeroMemory(szTmpPath, MAX_PATH);
484cdf0e10cSrcweir 	_snprintf(szTmpPath, MAX_PATH, "%s%s\0", szJarPath, "pocketword.jar");
485cdf0e10cSrcweir 
486cdf0e10cSrcweir 	if (!GetFileAttributesEx(szTmpPath, GetFileExInfoStandard, &fInfo))
487cdf0e10cSrcweir 	{
488cdf0e10cSrcweir 		m_bHaveWord = FALSE;
489cdf0e10cSrcweir 	}
490cdf0e10cSrcweir 	else
491cdf0e10cSrcweir 	{
492cdf0e10cSrcweir 		m_bHaveWord = TRUE;
493cdf0e10cSrcweir 		clsPath += szTmpPath;
494cdf0e10cSrcweir 		clsPath += ";";
495cdf0e10cSrcweir 	}
496cdf0e10cSrcweir 
497cdf0e10cSrcweir 	// Now check for Pocket Excel
498cdf0e10cSrcweir 	ZeroMemory(szTmpPath, MAX_PATH);
499cdf0e10cSrcweir 	_snprintf(szTmpPath, MAX_PATH, "%s%s\0", szJarPath, "pexcel.jar");
500cdf0e10cSrcweir 
501cdf0e10cSrcweir 	if (!GetFileAttributesEx(szTmpPath, GetFileExInfoStandard, &fInfo))
502cdf0e10cSrcweir 	{
503cdf0e10cSrcweir 		m_bHaveExcel = FALSE;
504cdf0e10cSrcweir 	}
505cdf0e10cSrcweir 	else
506cdf0e10cSrcweir 	{
507cdf0e10cSrcweir 		m_bHaveExcel = TRUE;
508cdf0e10cSrcweir 		clsPath += szTmpPath;
509cdf0e10cSrcweir 		clsPath += ";";
510cdf0e10cSrcweir 	}
511cdf0e10cSrcweir 
512cdf0e10cSrcweir 	// Quotes may be need around the ClassPath
513cdf0e10cSrcweir 	clsPath.insert(0, "\"");
514cdf0e10cSrcweir 	clsPath += "\"";
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 
517cdf0e10cSrcweir 	// Return the data
518cdf0e10cSrcweir 	return _strdup(clsPath.c_str());
519cdf0e10cSrcweir }
520