summaryrefslogtreecommitdiff
path: root/xmerge/source/activesync/XMergeFilter.cpp
blob: e7d1a91e2ccee8c41e65f9b45f16c82a4fc2263d (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
// XMergeFilter.cpp: implementation of the CXMergeFilter class.
//
//////////////////////////////////////////////////////////////////////


#include "stdafx.h"

#include "XMergeFilter.h"

#include <string>


#define ERR_NOJAVA       1
#define ERR_BADCLASSPATH 2
#define ERR_INITJAVA     3


const LPTSTR CXMergeFilter::m_pszPSWExportCLSID     = _T("{BDD611C3-7BAB-460F-8711-5B9AC9EF6020}");
const LPTSTR CXMergeFilter::m_pszPSWExportExt       = _T("sxw");
const LPTSTR CXMergeFilter::m_pszPSWExportDesc      = _T("OpenOffice.org Writer XML Document");
const LPTSTR CXMergeFilter::m_pszPSWExportShortDesc = _T("OpenOffice.org Writer");

const LPTSTR CXMergeFilter::m_pszPSWImportCLSID     = _T("{CB43F086-838D-4FA4-B5F6-3406B9A57439}");
const LPTSTR CXMergeFilter::m_pszPSWImportExt       = _T("psw");
const LPTSTR CXMergeFilter::m_pszPSWImportDesc      = _T("Pocket Word Document - Pocket PC");
const LPTSTR CXMergeFilter::m_pszPSWImportShortDesc = _T("Pocket Word");

const LPTSTR CXMergeFilter::m_pszPXLExportCLSID     = _T("{C6AB3E74-9F4F-4370-8120-A8A6FABB7A7C}");
const LPTSTR CXMergeFilter::m_pszPXLExportExt       = _T("sxc");
const LPTSTR CXMergeFilter::m_pszPXLExportDesc      = _T("OpenOffice.org Calc XML Document");
const LPTSTR CXMergeFilter::m_pszPXLExportShortDesc = _T("OpenOffice.org Calc");

const LPTSTR CXMergeFilter::m_pszPXLImportCLSID     = _T("{43887C67-4D5D-4127-BAAC-87A288494C7C}");
const LPTSTR CXMergeFilter::m_pszPXLImportExt       = _T("pxl");
const LPTSTR CXMergeFilter::m_pszPXLImportDesc      = _T("Pocket Excel Document - Pocket PC");
const LPTSTR CXMergeFilter::m_pszPXLImportShortDesc = _T("Pocket Excel");


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CXMergeFilter::CXMergeFilter() : m_cRef(1)
{
    m_bHaveExcel = FALSE;
    m_bHaveWord  = FALSE;

    m_szClasspath   = NULL;
    m_szJavaBaseDir = NULL;
}

CXMergeFilter::~CXMergeFilter()
{
    if (m_szClasspath != NULL)
    {
        delete m_szClasspath;
    }

    if (m_szJavaBaseDir != NULL)
    {
        delete m_szJavaBaseDir;
    }

}


//////////////////////////////////////////////////////////////////////
// IUnknown Methods
//////////////////////////////////////////////////////////////////////

STDMETHODIMP CXMergeFilter::QueryInterface(REFIID riid, void **ppvObject)
{
    if(ppvObject == NULL)
        return E_INVALIDARG;

    if (::IsEqualIID(riid, IID_IUnknown))
    {
        *ppvObject = static_cast<IUnknown *>(this);
    }
    else if (::IsEqualIID(riid, IID_ICeFileFilter))
    {
        *ppvObject = static_cast<ICeFileFilter *>(this);
    }
    else
    {
        *ppvObject = NULL;
        return E_NOINTERFACE;
    }

    reinterpret_cast<IUnknown *>(*ppvObject)->AddRef();
    return S_OK;
}


STDMETHODIMP_(ULONG) CXMergeFilter::AddRef()
{
    return ::InterlockedIncrement(&m_cRef);
}


STDMETHODIMP_(ULONG) CXMergeFilter::Release()
{
    if(::InterlockedDecrement(&m_cRef) == 0)
    {
        delete this;
        return 0;
    }
    return m_cRef;
}


//////////////////////////////////////////////////////////////////////
// ICeFileFilter
//////////////////////////////////////////////////////////////////////

STDMETHODIMP CXMergeFilter::FilterOptions(HWND hwndParent)
{
    // We don't currently allow any options
    return HRESULT_FROM_WIN32(NOERROR);
}

STDMETHODIMP CXMergeFilter::FormatMessage(DWORD dwFlags, DWORD dwMessageId,
                        DWORD dwLanguageId, LPTSTR lpBuffer, DWORD nSize,
                        va_list *Arguments, DWORD *pcb)
{
    TCHAR errMsg[1024];

    HKEY  hKey   = NULL;
    DWORD dwSize = 1024;


    long lRet = 0;

    // Attempt to find the messages in the registry
    lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Sun Microsystems\\StarOffice\\XMergeSync\\Messages\\Error"),
                            0, KEY_READ, &hKey);
    if (lRet != ERROR_SUCCESS)
    {
        // Try the user's portion of the registry
        lRet = ::RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Sun Microsystems\\StarOffice\\XMergeSync\\Messages\\Error"),
                            0, KEY_READ, &hKey);
        if (lRet != ERROR_SUCCESS)
        {
            hKey = NULL;
        }
    }


    switch(dwMessageId)
    {
    case ERR_NOJAVA:
        lRet = ::RegQueryValueEx(hKey, _T("Java"), 0, NULL, (LPBYTE)errMsg, &dwSize);
        if (lRet != ERROR_SUCCESS)
        {
            lstrcpy(errMsg, "Unable to locate Java 1.4/1.5 installation.");
        }
        break;

    case ERR_BADCLASSPATH:
        lRet = ::RegQueryValueEx(hKey, _T("Classpath"), 0, NULL, (LPBYTE)errMsg, &dwSize);
        if (lRet != ERROR_SUCCESS)
        {
            lstrcpy(errMsg, "Unable to locate XMerge Jar files.");
        }
        break;

    case ERR_INITJAVA:
        lRet = ::RegQueryValueEx(hKey, _T("JavaInit"), 0, NULL, (LPBYTE)errMsg, &dwSize);
        if (lRet != ERROR_SUCCESS)
        {
            lstrcpy(errMsg, "Error initialising the Java Runtime Environment.");
        }
        break;
    }

    char* buf = (char*)LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, (lstrlen(errMsg) + 1) * sizeof(TCHAR));
    lstrcpyn(buf, errMsg, lstrlen(errMsg));

    *(char**)lpBuffer = buf;
    *pcb = strlen(errMsg);

    return HRESULT_FROM_WIN32(NOERROR);
}


STDMETHODIMP CXMergeFilter::NextConvertFile(int nConversion, CFF_CONVERTINFO *pci,
                             CFF_SOURCEFILE *psf, CFF_DESTINATIONFILE *pdf,
                             volatile BOOL *pbCancel, CF_ERROR *perr)
{
    std::string appArgs;
    std::string appName;

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    ZeroMemory( &pi, sizeof(pi) );

    si.cb = sizeof(si);


    /*
     * First step: Locate Java and establish the classpath.  If these can't
     *             be done successfully, then avoid all further processing.
     */

    // Locate Java Home if it hasn't already been done.
    if (m_szJavaBaseDir == NULL)
    {
        m_szJavaBaseDir = GetJavaBaseDir();

        if (m_szJavaBaseDir == NULL)
        {
            *perr = ERR_NOJAVA;
            return HRESULT_FROM_WIN32(E_FAIL);
        }
    }

    // Get the StarOffice/OpenOffice class directory
    if (m_szClasspath == NULL)
    {
        m_szClasspath = GetXMergeClassPath();

        if (m_szClasspath == NULL)
        {
            *perr = ERR_BADCLASSPATH;
            return HRESULT_FROM_WIN32(E_FAIL);
        }
    }


    /*
     * Second step:  Check the files we're going to process.  If we don't have
     *               an XMerge plugin for the file then we can't convert.
     */
    if ((!lstrcmp(psf->szExtension, "sxw")  || !lstrcmp(psf->szExtension, "psw"))
            && !m_bHaveWord)
    {
        *perr = ERR_BADCLASSPATH;
        return HRESULT_FROM_WIN32(E_FAIL);
    }
    else if ((!lstrcmp(psf->szExtension, "sxc")  || !lstrcmp(psf->szExtension, "pxl"))
                 && !m_bHaveExcel)
    {
        *perr = ERR_BADCLASSPATH;
        return HRESULT_FROM_WIN32(E_FAIL);
    }


    /*
     * Third step:  Locate the Java executable and build and execute the command
     *              line to carry out the conversion.
     */

    // Find the Java executable and make sure it exists
    appName += m_szJavaBaseDir;
    appName += "\\bin\\javaw.exe";

    if (GetFileAttributes(appName.c_str()) == INVALID_FILE_SIZE)
    {
        *perr = ERR_NOJAVA;
        return HRESULT_FROM_WIN32(E_FAIL);
    }

    // Wrap the executable path in quotes in case of spaces
    appName.insert(0, "\"");
    appName.append("\"");



    // Need to build the entire command line for calling out to Java
    appArgs =  appName + " -Djava.class.path=";
    appArgs += m_szClasspath;
    appArgs += " org.openoffice.xmerge.util.ActiveSyncDriver ";

    if (!lstrcmp(psf->szExtension, "sxw"))
    {
        appArgs += "staroffice/sxw ";
        appArgs += "application/x-pocket-word ";
    }
    else if(!lstrcmp(psf->szExtension, "psw"))
    {
        appArgs += "application/x-pocket-word ";
        appArgs += "staroffice/sxw ";
    }
    else if(!lstrcmp(psf->szExtension, "sxc"))
    {
        appArgs += "staroffice/sxc ";
        appArgs += "application/x-pocket-excel ";
    }
    else if(!lstrcmp(psf->szExtension, "pxl"))
    {
        appArgs += "application/x-pocket-excel ";
        appArgs += "staroffice/sxc ";
    }


    // ActiveSync sometimes gives out long file names, especially when automatically syncing
    appArgs += "\"";
    appArgs += psf->szFullpath;
    appArgs += "\" \"";
    appArgs += pdf->szFullpath;
    appArgs += "\"";

    if(!CreateProcess(NULL,
                  (char*)appArgs.c_str(),
                  NULL,             // No Process Attributes
                  NULL,             // No Thread Attributes
                  FALSE,            // Don't want this process getting handles
                  CREATE_NO_WINDOW, // No console
                  NULL,             // No special environment
                  NULL,             // Current Working Directory is okay
                  &si,
                  &pi))
    {
        *perr = ERR_INITJAVA;
        return HRESULT_FROM_WIN32(E_FAIL);
    }

    // Wait for the new process to work
    WaitForSingleObject(pi.hProcess, INFINITE);

    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
}


typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD, LPTSTR );


TCHAR* CXMergeFilter::GetJavaBaseDir()
{
    HRESULT lRet;

    HKEY hKey = NULL;
    HKEY hDataKey = NULL;

    TCHAR szCurrentJava[_MAX_PATH] = "\0";
    DWORD dwClassName            = _MAX_PATH;
    DWORD dwKeyName              = _MAX_PATH;

    /*
     * Java leaves registry keys at HKLM\SOFTWARE\JavaSoft.
     *
     * Check for a JRE installation first
     */
    lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\JavaSoft\\Java Runtime Environment"), 0, KEY_READ, &hKey);
    if (lRet != ERROR_SUCCESS)
        return NULL;

    // Locations shouldn't be greater than _MAX_PATH
    TCHAR*  szJavaHome = new TCHAR[_MAX_PATH + 1];
    DWORD dwSize = _MAX_PATH + 1;

    /* use current version */
    lRet = ::RegQueryValueEx(hKey, _T("CurrentVersion"), 0, NULL, (LPBYTE)szCurrentJava, &dwSize);

    lRet = ::RegOpenKeyEx(hKey, _T(szCurrentJava), 0, KEY_READ, &hDataKey);
    if (lRet != ERROR_SUCCESS)
    {
        RegCloseKey(hKey);
        delete [] szJavaHome;
        return NULL;
    }


    // Now read the JavaHome value
    dwSize = _MAX_PATH + 1;
    lRet = ::RegQueryValueEx(hDataKey, _T("JavaHome"), 0, NULL, (LPBYTE)szJavaHome, &dwSize);
    if (lRet != ERROR_SUCCESS)
    {
        RegCloseKey(hDataKey);
        RegCloseKey(hKey);
        delete [] szJavaHome;
        return NULL;
    }

    RegCloseKey(hDataKey);
    RegCloseKey(hKey);


    // Check that the directory exists before returning it
    DWORD dwAttrs = GetFileAttributes(szJavaHome);

    if (((dwAttrs & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) || dwAttrs == INVALID_FILE_SIZE)
    {
        delete [] szJavaHome;
        return NULL;
    }

    return szJavaHome;
}



TCHAR* CXMergeFilter::GetXMergeClassPath()
{
    /*
     * The DLL will be installed by setup in the program directory of
     * the installation.  The XMerge Jar files, if present, will be
     * located in the classes directory below program.
     */

    TCHAR szJarPath[MAX_PATH];
    TCHAR szTmpPath[MAX_PATH];

    ZeroMemory(szJarPath, MAX_PATH);
    ZeroMemory(szTmpPath, MAX_PATH);

    WIN32_FILE_ATTRIBUTE_DATA fInfo;

    std::string clsPath;


    // Get the location of the module.
    GetModuleFileName(_Module.m_hInst, szTmpPath, MAX_PATH);

    // Strip off the xmergesync.dll component
    _strlwr(szTmpPath);
    char* modName = strstr(szTmpPath, "xmergesync.dll");
    strncpy(szJarPath, szTmpPath, modName - szTmpPath);

    // Append the classes directory
    strncat(szJarPath, "classes\\", 8);


    // The core xmerge.jar must be present
    ZeroMemory(szTmpPath, MAX_PATH);
    _snprintf(szTmpPath, MAX_PATH, "%s%s\0", szJarPath, "xmerge.jar");

    if (!GetFileAttributesEx(szTmpPath, GetFileExInfoStandard, &fInfo))
    {
        return NULL;
    }
    else
    {
        clsPath += szTmpPath;
        clsPath += ";";
    }


    // Now check for Pocket Word
    ZeroMemory(szTmpPath, MAX_PATH);
    _snprintf(szTmpPath, MAX_PATH, "%s%s\0", szJarPath, "pocketword.jar");

    if (!GetFileAttributesEx(szTmpPath, GetFileExInfoStandard, &fInfo))
    {
        m_bHaveWord = FALSE;
    }
    else
    {
        m_bHaveWord = TRUE;
        clsPath += szTmpPath;
        clsPath += ";";
    }

    // Now check for Pocket Excel
    ZeroMemory(szTmpPath, MAX_PATH);
    _snprintf(szTmpPath, MAX_PATH, "%s%s\0", szJarPath, "pexcel.jar");

    if (!GetFileAttributesEx(szTmpPath, GetFileExInfoStandard, &fInfo))
    {
        m_bHaveExcel = FALSE;
    }
    else
    {
        m_bHaveExcel = TRUE;
        clsPath += szTmpPath;
        clsPath += ";";
    }

    // Quotes may be need around the ClassPath
    clsPath.insert(0, "\"");
    clsPath += "\"";


    // Return the data
    return _strdup(clsPath.c_str());
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */