[Libreoffice-commits] core.git: desktop/inc desktop/source embedserv/source smoketest/libtest.cxx

Michael Meeks michael.meeks at suse.com
Fri Jul 26 10:23:49 PDT 2013


 desktop/inc/liblibreoffice.hxx       |   17 ++++-
 desktop/inc/liblibreoffice_impl.hxx  |   27 --------
 desktop/source/lib/init.cxx          |  109 +++++++++++++++++++++++++++++++----
 embedserv/source/embed/docholder.cxx |    3 
 smoketest/libtest.cxx                |   23 +++++++
 5 files changed, 133 insertions(+), 46 deletions(-)

New commits:
commit d32b179c3f2b16bb96679816db485d62ce8fe87f
Author: Michael Meeks <michael.meeks at suse.com>
Date:   Fri Jul 26 18:21:45 2013 +0100

    more liblibreoffice pieces.
    
    Change-Id: I21d67de281847321d784cddc652d4a51a437fadf

diff --git a/desktop/inc/liblibreoffice.hxx b/desktop/inc/liblibreoffice.hxx
index 48c0a24..eb7715b 100644
--- a/desktop/inc/liblibreoffice.hxx
+++ b/desktop/inc/liblibreoffice.hxx
@@ -10,17 +10,26 @@
 #ifndef _LIBLIBREOFFICE_HXX
 #define _LIBLIBREOFFICE_HXX
 
-typedef struct _LODocument LODocument;
+class LODocument
+{
+public:
+    virtual ~LODocument() {}
+
+    virtual bool saveAs (const char *url) = 0;
+};
 
 class LibLibreOffice
 {
 public:
-    virtual bool initialize (const char *installPath) = 0;
+    virtual ~LibLibreOffice () {};
+
+    virtual bool        initialize (const char *installPath) = 0;
 
     virtual LODocument *documentLoad (const char *url) = 0;
-    virtual bool   documentSave (const char *url) = 0;
 
-    virtual ~LibLibreOffice () {};
+    // return the last error as a string, free me.
+    virtual char       *getError() = 0;
+
 };
 
 LibLibreOffice *lo_init (const char *install_path);
diff --git a/desktop/inc/liblibreoffice_impl.hxx b/desktop/inc/liblibreoffice_impl.hxx
deleted file mode 100644
index 3f7783e..0000000
--- a/desktop/inc/liblibreoffice_impl.hxx
+++ /dev/null
@@ -1,27 +0,0 @@
-/* -*- 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/.
- */
-
-#ifndef _LIBLIBREOFFICE_IMPL_HXX
-#define _LIBLIBREOFFICE_IMPL_HXX
-
-#include "liblibreoffice.hxx"
-
-class LibLibreOffice_Impl : public LibLibreOffice
-{
-public:
-    virtual bool        initialize (const char *installPath);
-
-    virtual LODocument *documentLoad (const char *url);
-    virtual bool        documentSave (const char *url);
-
-    virtual ~LibLibreOffice_Impl ();
-};
-
-#endif
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index d564c8c..d753f9a 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -7,9 +7,10 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
-#include <liblibreoffice_impl.hxx>
-
 #include <stdio.h>
+#include <string.h>
+
+#include "liblibreoffice.hxx"
 
 #include <tools/errinf.hxx>
 #include <osl/file.hxx>
@@ -20,7 +21,9 @@
 #include <comphelper/processfactory.hxx>
 
 #include <com/sun/star/beans/XPropertySet.hpp>
+#include <com/sun/star/frame/XModel.hpp>
 #include <com/sun/star/frame/Desktop.hpp>
+#include <com/sun/star/frame/XStorable.hpp>
 #include <com/sun/star/lang/Locale.hpp>
 #include <com/sun/star/lang/XComponent.hpp>
 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
@@ -34,34 +37,111 @@
 
 using namespace ::com::sun::star;
 
+class LibLODocument_Impl;
+class LibLibreOffice_Impl;
+
+static LibLibreOffice_Impl *gImpl = NULL;
+
+class LibLODocument_Impl : public LODocument
+{
+    uno::Reference < css::lang::XComponent > mxComponent;
+public:
+    LibLODocument_Impl( const uno::Reference < css::lang::XComponent > &xComponent )
+            : mxComponent( xComponent )
+        { }
+    virtual bool saveAs (const char *url);
+};
+
+class LibLibreOffice_Impl : public LibLibreOffice
+{
+public:
+    rtl::OUString       maLastExceptionMsg;
+
+    virtual bool        initialize (const char *installPath);
+
+    virtual LODocument *documentLoad (const char *url);
+
+    virtual char       *getError();
+
+    virtual ~LibLibreOffice_Impl ();
+};
+
 // Wonder global state ...
 static uno::Reference<css::uno::XComponentContext> xContext;
 static uno::Reference<css::lang::XMultiServiceFactory> xSFactory;
 static uno::Reference<css::lang::XMultiComponentFactory> xFactory;
 
+static OUString getUString( const char *str )
+{
+    if( !str )
+        return OUString( "" );
+    return OStringToOUString( OString( str, strlen (str) ),
+                              RTL_TEXTENCODING_UTF8 );
+}
+
 LODocument *
 LibLibreOffice_Impl::documentLoad( const char *docUrl )
 {
-    OUString sUrl = OUString::createFromAscii (docUrl);
+    OUString sUrl = getUString( docUrl );
     OUString sAbsoluteDocUrl, sWorkingDir, sDocPathUrl;
 
     uno::Reference < css::frame::XDesktop2 > xComponentLoader =
             css::frame::Desktop::create(xContext);
 
     osl_getProcessWorkingDir(&sWorkingDir.pData);
-    osl::FileBase::getFileURLFromSystemPath(sUrl, sDocPathUrl);
+    osl::FileBase::getFileURLFromSystemPath( sUrl, sDocPathUrl );
     osl::FileBase::getAbsoluteFileURL(sWorkingDir, sDocPathUrl, sAbsoluteDocUrl);
 
-    uno::Reference < css::lang::XComponent > xComponent = xComponentLoader->loadComponentFromURL(
-            sAbsoluteDocUrl, OUString("_blank"), 0,
-            uno::Sequence < css::beans::PropertyValue >());
+    maLastExceptionMsg = "";
+    try {
+        uno::Reference < css::lang::XComponent > xComponent =
+            xComponentLoader->loadComponentFromURL(
+                sAbsoluteDocUrl, OUString("_blank"), 0,
+                uno::Sequence < css::beans::PropertyValue >());
+        if( xComponentLoader.is() )
+            return new LibLODocument_Impl( xComponent );
+        else
+            maLastExceptionMsg = "unknown load failure";
+    } catch (const uno::Exception &ex) {
+        maLastExceptionMsg = ex.Message;
+    }
     return NULL;
 }
 
-bool
-LibLibreOffice_Impl::documentSave( const char * )
+bool LibLODocument_Impl::saveAs (const char *url)
 {
-    return 1;
+    OUString sURL = getUString( url );
+
+    try {
+        uno::Reference< frame::XModel > xDocument( mxComponent, uno::UNO_QUERY_THROW );
+        uno::Sequence< beans::PropertyValue > aSeq = xDocument->getArgs();
+
+        OUString aFilterName;
+        for( sal_Int32 i = 0; i < aSeq.getLength(); ++i )
+        {
+            if( aSeq[i].Name == "FilterName" )
+                aSeq[i].Value >>= aFilterName;
+        }
+        aSeq.realloc(2);
+        aSeq[0].Name = "Overwrite";
+        aSeq[0].Value <<= sal_True;
+        aSeq[1].Name = "FilterName";
+        aSeq[1].Value <<= aFilterName;
+
+        uno::Reference< frame::XStorable > xStorable( mxComponent, uno::UNO_QUERY_THROW );
+        xStorable->storeAsURL( sURL, aSeq );
+
+        return true;
+    } catch (const uno::Exception &ex) {
+        gImpl->maLastExceptionMsg = "exception " + ex.Message;
+        return false;
+    }
+}
+
+char *LibLibreOffice_Impl::getError()
+{
+    OString aStr = rtl::OUStringToOString( maLastExceptionMsg, RTL_TEXTENCODING_UTF8 );
+    return strndup( aStr.getStr(), aStr.getLength() );
 }
 
 static void
@@ -154,12 +234,17 @@ extern "C" {
 
 LibLibreOffice *liblibreoffice_hook(void)
 {
-    fprintf( stderr, "create libreoffice object\n" );
-    return new LibLibreOffice_Impl();
+    if( !gImpl )
+    {
+        fprintf( stderr, "create libreoffice object\n" );
+        gImpl = new LibLibreOffice_Impl();
+    }
+    return gImpl;
 }
 
 LibLibreOffice_Impl::~LibLibreOffice_Impl ()
 {
+    gImpl = NULL;
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/embedserv/source/embed/docholder.cxx b/embedserv/source/embed/docholder.cxx
index be1cf8d..7114c85 100644
--- a/embedserv/source/embed/docholder.cxx
+++ b/embedserv/source/embed/docholder.cxx
@@ -942,8 +942,7 @@ void DocumentHolder::setTitle(const OUString& aDocumentName)
                     m_xDocument->getArgs();
                 for(sal_Int32 j = 0; j < aSeq.getLength(); ++j)
                 {
-                    if(aSeq[j].Name ==
-                       OUString("FilterName"))
+                    if(aSeq[j].Name == "FilterName")
                     {
                         aSeq[j].Value >>= aFilterName;
                         break;
diff --git a/smoketest/libtest.cxx b/smoketest/libtest.cxx
index f3d207f..3907749 100644
--- a/smoketest/libtest.cxx
+++ b/smoketest/libtest.cxx
@@ -8,6 +8,7 @@
  */
 
 #include <stdio.h>
+#include <malloc.h>
 #include <assert.h>
 #include <liblibreoffice.hxx>
 
@@ -28,10 +29,30 @@ int main (int argc, char **argv)
     LODocument *pDocument = pOffice->documentLoad( argv[2] );
     if( !pDocument )
     {
-        fprintf( stderr, "failed to load document '%s'\n", argv[2] );
+        char *pError = pOffice->getError();
+        fprintf( stderr, "failed to load document '%s': '%s'\n",
+                 argv[2], pError );
+        free (pError);
         return -1;
     }
+
+    if( argc > 3 )
+    {
+        fprintf( stderr, "save document as '%s'\n", argv[3] );
+        if ( !pDocument->saveAs( argv[ 3 ] ) )
+        {
+            char *pError = pOffice->getError();
+            fprintf( stderr, "failed to save document '%s'\n", pError);
+            free (pError);
+        }
+        else
+            fprintf( stderr, "Save succeeded\n" );
+    }
     fprintf( stderr, "all tests passed." );
+
+    delete pDocument;
+    delete pOffice;
+
     return 0;
 }
 


More information about the Libreoffice-commits mailing list