Kaydet (Commit) 8ba3116a authored tarafından Andrzej Hunt's avatar Andrzej Hunt Kaydeden (comit) Michael Meeks

LOK: Split into instance and class.

Change-Id: I2b6f33eaf12343c7da1328a932eb703bb4e4ef6f
üst 5df0bfdd
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <LibreOfficeKit/LibreOfficeKit.h> #include <LibreOfficeKit/LibreOfficeKit.h>
#include <tools/errinf.hxx> #include <tools/errinf.hxx>
...@@ -42,10 +45,14 @@ ...@@ -42,10 +45,14 @@
using namespace css; using namespace css;
using namespace utl; using namespace utl;
using namespace boost;
struct LibLODocument_Impl; struct LibLODocument_Impl;
struct LibLibreOffice_Impl; struct LibLibreOffice_Impl;
static LibLibreOffice_Impl *gImpl = NULL; static LibLibreOffice_Impl *gImpl = NULL;
static weak_ptr< LibreOfficeKitClass > gOfficeClass;
static weak_ptr< LibreOfficeKitDocumentClass > gDocumentClass;
typedef struct typedef struct
{ {
...@@ -151,15 +158,24 @@ static int doc_saveAsWithOptions(LibreOfficeKitDocument* pThis, const char* pUr ...@@ -151,15 +158,24 @@ static int doc_saveAsWithOptions(LibreOfficeKitDocument* pThis, const char* pUr
struct LibLODocument_Impl : public _LibreOfficeKitDocument struct LibLODocument_Impl : public _LibreOfficeKitDocument
{ {
uno::Reference<css::lang::XComponent> mxComponent; uno::Reference<css::lang::XComponent> mxComponent;
shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass;
LibLODocument_Impl(const uno::Reference <css::lang::XComponent> &xComponent) : LibLODocument_Impl(const uno::Reference <css::lang::XComponent> &xComponent) :
mxComponent( xComponent ) mxComponent( xComponent )
{ {
nSize = sizeof(LibreOfficeKitDocument); if (!(m_pDocumentClass = gDocumentClass.lock()))
{
m_pDocumentClass.reset(new LibreOfficeKitDocumentClass);
m_pDocumentClass->nSize = sizeof(LibreOfficeKitDocument);
m_pDocumentClass->destroy = doc_destroy;
m_pDocumentClass->saveAs = doc_saveAs;
m_pDocumentClass->saveAsWithOptions = doc_saveAsWithOptions;
destroy = doc_destroy; gDocumentClass = m_pDocumentClass;
saveAs = doc_saveAs; }
saveAsWithOptions = doc_saveAsWithOptions; pClass = m_pDocumentClass.get();
} }
~LibLODocument_Impl() ~LibLODocument_Impl()
...@@ -182,15 +198,23 @@ static char * lo_getError (LibreOfficeKit* pThis); ...@@ -182,15 +198,23 @@ static char * lo_getError (LibreOfficeKit* pThis);
struct LibLibreOffice_Impl : public _LibreOfficeKit struct LibLibreOffice_Impl : public _LibreOfficeKit
{ {
OUString maLastExceptionMsg; OUString maLastExceptionMsg;
shared_ptr< LibreOfficeKitClass > m_pOfficeClass;
LibLibreOffice_Impl() LibLibreOffice_Impl()
{ {
nSize = sizeof(LibreOfficeKit); if(!(m_pOfficeClass = gOfficeClass.lock())) {
m_pOfficeClass.reset(new LibreOfficeKitClass);
m_pOfficeClass->nSize = sizeof(LibreOfficeKitClass);
m_pOfficeClass->destroy = lo_destroy;
m_pOfficeClass->initialize = lo_initialize;
m_pOfficeClass->documentLoad = lo_documentLoad;
m_pOfficeClass->getError = lo_getError;
gOfficeClass = m_pOfficeClass;
}
destroy = lo_destroy; pClass = m_pOfficeClass.get();
initialize = lo_initialize;
documentLoad = lo_documentLoad;
getError = lo_getError;
} }
}; };
...@@ -416,7 +440,7 @@ static int lo_initialize(LibreOfficeKit* pThis, const char* pAppPath) ...@@ -416,7 +440,7 @@ static int lo_initialize(LibreOfficeKit* pThis, const char* pAppPath)
return bInitialized; return bInitialized;
} }
SAL_DLLPUBLIC_EXPORT LibreOfficeKit *liblibreoffice_hook(void) SAL_DLLPUBLIC_EXPORT LibreOfficeKit *libreofficekit_hook(void)
{ {
if (!gImpl) if (!gImpl)
{ {
......
...@@ -18,16 +18,24 @@ extern "C" ...@@ -18,16 +18,24 @@ extern "C"
#endif #endif
typedef struct _LibreOfficeKit LibreOfficeKit; typedef struct _LibreOfficeKit LibreOfficeKit;
typedef struct _LibreOfficeKitClass LibreOfficeKitClass;
typedef struct _LibreOfficeKitDocument LibreOfficeKitDocument; typedef struct _LibreOfficeKitDocument LibreOfficeKitDocument;
typedef struct _LibreOfficeKitDocumentClass LibreOfficeKitDocumentClass;
// Do we have an extended member in this struct ? // Do we have an extended member in this struct ?
#define LIBREOFFICEKIT_HAS_MEMBER(strct,member,nSize) \ #define LIBREOFFICEKIT_HAS_MEMBER(strct,member,nSize) \
((((size_t)((unsigned char *)&((strct *) 0)->member) + \ ((((size_t)((unsigned char *)&((strct *) 0)->member) + \
sizeof ((strct *) 0)->member)) <= (nSize)) sizeof ((strct *) 0)->member)) <= (nSize))
#define LIBREOFFICEKIT_HAS(pKit,member) LIBREOFFICEKIT_HAS_MEMBER(LibreOfficeKit,member,(pKit)->nSize) #define LIBREOFFICEKIT_HAS(pKit,member) LIBREOFFICEKIT_HAS_MEMBER(LibreOfficeKitClass,member,(pKit)->pClass->nSize)
struct _LibreOfficeKit struct _LibreOfficeKit
{
LibreOfficeKitClass* pClass;
};
struct _LibreOfficeKitClass
{ {
size_t nSize; size_t nSize;
...@@ -37,9 +45,14 @@ struct _LibreOfficeKit ...@@ -37,9 +45,14 @@ struct _LibreOfficeKit
char* (*getError) (LibreOfficeKit *pThis); char* (*getError) (LibreOfficeKit *pThis);
}; };
#define LIBREOFFICEKIT_DOCUMENT_HAS(pDoc,member) LIBREOFFICEKIT_HAS_MEMBER(LibreOfficeKitDocument,member,(pDoc)->nSize) #define LIBREOFFICEKIT_DOCUMENT_HAS(pDoc,member) LIBREOFFICEKIT_HAS_MEMBER(LibreOfficeKitDocumentClass,member,(pDoc)->pClass->nSize)
struct _LibreOfficeKitDocument struct _LibreOfficeKitDocument
{
LibreOfficeKitDocumentClass* pClass;
};
struct _LibreOfficeKitDocumentClass
{ {
size_t nSize; size_t nSize;
......
...@@ -33,22 +33,18 @@ public: ...@@ -33,22 +33,18 @@ public:
inline ~Document() inline ~Document()
{ {
mpDoc->destroy(mpDoc); mpDoc->pClass->destroy(mpDoc);
} }
// Save as the given format, if format is NULL sniff from ext'n // Save as the given format, if format is NULL sniff from ext'n
inline bool saveAs(const char* pUrl, const char* pFormat = NULL) inline bool saveAs(const char* pUrl, const char* pFormat = NULL)
{ {
return mpDoc->saveAs(mpDoc, pUrl, pFormat); return mpDoc->pClass->saveAs(mpDoc, pUrl, pFormat);
} }
inline bool saveAsWithOptions(const char* pUrl, const char* pFormat = NULL, const char* pFilterOptions = NULL) inline bool saveAsWithOptions(const char* pUrl, const char* pFormat = NULL, const char* pFilterOptions = NULL)
{ {
// available since LibreOffice 4.3 return mpDoc->pClass->saveAsWithOptions(mpDoc, pUrl, pFormat, pFilterOptions);
if (!LIBREOFFICEKIT_DOCUMENT_HAS(mpDoc, saveAsWithOptions))
return false;
return mpDoc->saveAsWithOptions(mpDoc, pUrl, pFormat, pFilterOptions);
} }
inline LibreOfficeKitDocument *get() { return mpDoc; } inline LibreOfficeKitDocument *get() { return mpDoc; }
}; };
...@@ -65,17 +61,17 @@ public: ...@@ -65,17 +61,17 @@ public:
inline ~Office() inline ~Office()
{ {
mpThis->destroy(mpThis); mpThis->pClass->destroy(mpThis);
} }
inline bool initialize(const char* pInstallPath) inline bool initialize(const char* pInstallPath)
{ {
return mpThis->initialize(mpThis, pInstallPath); return mpThis->pClass->initialize(mpThis, pInstallPath);
} }
inline Document* documentLoad(const char* pUrl) inline Document* documentLoad(const char* pUrl)
{ {
LibreOfficeKitDocument* pDoc = mpThis->documentLoad(mpThis, pUrl); LibreOfficeKitDocument* pDoc = mpThis->pClass->documentLoad(mpThis, pUrl);
if (pDoc == NULL) if (pDoc == NULL)
return NULL; return NULL;
return new Document(pDoc); return new Document(pDoc);
...@@ -84,14 +80,14 @@ public: ...@@ -84,14 +80,14 @@ public:
// return the last error as a string, free me. // return the last error as a string, free me.
inline char* getError() inline char* getError()
{ {
return mpThis->getError(mpThis); return mpThis->pClass->getError(mpThis);
} }
}; };
inline Office* lok_cpp_init(const char* pInstallPath) inline Office* lok_cpp_init(const char* pInstallPath)
{ {
LibreOfficeKit* pThis = lok_init(pInstallPath); LibreOfficeKit* pThis = lok_init(pInstallPath);
if (pThis == NULL || pThis->nSize == 0) if (pThis == NULL || pThis->pClass->nSize == 0)
return NULL; return NULL;
return new ::lok::Office(pThis); return new ::lok::Office(pThis);
} }
......
...@@ -50,7 +50,7 @@ SAL_DLLPUBLIC_EXPORT LibreOfficeKit *lok_init( const char *install_path ) ...@@ -50,7 +50,7 @@ SAL_DLLPUBLIC_EXPORT LibreOfficeKit *lok_init( const char *install_path )
return NULL; return NULL;
} }
pSym = (HookFunction *) dlsym( dlhandle, "liblibreoffice_hook" ); pSym = (HookFunction *) dlsym( dlhandle, "libreofficekit_hook" );
if( !pSym ) { if( !pSym ) {
fprintf( stderr, "failed to find hook in library '%s'\n", imp_lib ); fprintf( stderr, "failed to find hook in library '%s'\n", imp_lib );
dlclose( dlhandle ); dlclose( dlhandle );
......
...@@ -79,9 +79,9 @@ int main (int argc, char **argv) ...@@ -79,9 +79,9 @@ int main (int argc, char **argv)
if (!LIBREOFFICEKIT_DOCUMENT_HAS(pDocument->get(), saveAsWithOptions)) if (!LIBREOFFICEKIT_DOCUMENT_HAS(pDocument->get(), saveAsWithOptions))
{ {
fprintf( stderr, "using obsolete LibreOffice %" SAL_PRI_SIZET "d + %" SAL_PRI_SIZET "d vs. %" SAL_PRI_SIZET "d\n", fprintf( stderr, "using obsolete LibreOffice %" SAL_PRI_SIZET "d + %" SAL_PRI_SIZET "d vs. %" SAL_PRI_SIZET "d\n",
(size_t)((unsigned char *)&((LibreOfficeKitDocument *) 0)->saveAsWithOptions), (size_t)((unsigned char *)&((LibreOfficeKitDocumentClass *) 0)->saveAsWithOptions),
sizeof ((LibreOfficeKitDocument *) 0)->saveAsWithOptions, sizeof ((LibreOfficeKitDocumentClass *) 0)->saveAsWithOptions,
pDocument->get()->nSize ); pDocument->get()->pClass->nSize );
return -1; return -1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment