f325b2
From 642f6386a1a73c13ed3997f54a737385e8894992 Mon Sep 17 00:00:00 2001
f325b2
From: Mihai Varga <mihai.varga@collabora.com>
f325b2
Date: Mon, 17 Aug 2015 18:49:40 +0300
f325b2
Subject: [PATCH 095/398] lok::Document getStyles method
f325b2
f325b2
This method returns a JSON mapping of style families to a list of styles
f325b2
from the corresponding family.
f325b2
Will be used to know and apply styles in tiledrendering.
f325b2
f325b2
Change-Id: I0aa395c40b9573920ade44255f97c077475ae5f1
f325b2
(cherry picked from commit c5a516bd1bf0216ee39f31322369f6bffdf464eb)
f325b2
---
f325b2
 desktop/source/lib/init.cxx               | 34 ++++++++++++++++++++++++++++++
f325b2
 include/LibreOfficeKit/LibreOfficeKit.h   |  3 +++
f325b2
 include/LibreOfficeKit/LibreOfficeKit.hxx |  8 +++++++
f325b2
 libreofficekit/qa/unit/tiledrendering.cxx | 35 +++++++++++++++++++++++++++++++
f325b2
 4 files changed, 80 insertions(+)
f325b2
f325b2
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
f325b2
index abd8ca0b640b..51302d1f8466 100644
f325b2
--- a/desktop/source/lib/init.cxx
f325b2
+++ b/desktop/source/lib/init.cxx
f325b2
@@ -35,11 +35,13 @@
f325b2
 #include <comphelper/processfactory.hxx>
f325b2
 
f325b2
 #include <com/sun/star/beans/XPropertySet.hpp>
f325b2
+#include <com/sun/star/container/XNameAccess.hpp>
f325b2
 #include <com/sun/star/frame/Desktop.hpp>
f325b2
 #include <com/sun/star/frame/XStorable.hpp>
f325b2
 #include <com/sun/star/lang/Locale.hpp>
f325b2
 #include <com/sun/star/lang/XComponent.hpp>
f325b2
 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
f325b2
+#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
f325b2
 #include <com/sun/star/ucb/XContentProvider.hpp>
f325b2
 #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
f325b2
 
f325b2
@@ -233,6 +235,7 @@ static void doc_setGraphicSelection (LibreOfficeKitDocument* pThis,
f325b2
                                   int nX,
f325b2
                                   int nY);
f325b2
 static void doc_resetSelection (LibreOfficeKitDocument* pThis);
f325b2
+static char* doc_getStyles(LibreOfficeKitDocument* pThis);
f325b2
 
f325b2
 struct LibLODocument_Impl : public _LibreOfficeKitDocument
f325b2
 {
f325b2
@@ -267,6 +270,7 @@ struct LibLODocument_Impl : public _LibreOfficeKitDocument
f325b2
             m_pDocumentClass->getTextSelection = doc_getTextSelection;
f325b2
             m_pDocumentClass->setGraphicSelection = doc_setGraphicSelection;
f325b2
             m_pDocumentClass->resetSelection = doc_resetSelection;
f325b2
+            m_pDocumentClass->getStyles = doc_getStyles;
f325b2
 
f325b2
             gDocumentClass = m_pDocumentClass;
f325b2
         }
f325b2
@@ -864,6 +868,36 @@ static void doc_resetSelection(LibreOfficeKitDocument* pThis)
f325b2
     pDoc->resetSelection();
f325b2
 }
f325b2
 
f325b2
+static char* doc_getStyles(LibreOfficeKitDocument* pThis)
f325b2
+{
f325b2
+    LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
f325b2
+
f325b2
+    boost::property_tree::ptree aTree;
f325b2
+    uno::Reference<css::style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(pDocument->mxComponent, uno::UNO_QUERY);
f325b2
+    uno::Reference<container::XNameAccess> xStyleFamilies(xStyleFamiliesSupplier->getStyleFamilies(), uno::UNO_QUERY);
f325b2
+    uno::Sequence<OUString> aStyleFamilies = xStyleFamilies->getElementNames();
f325b2
+
f325b2
+    for (sal_Int32 nStyleFam = 0; nStyleFam < aStyleFamilies.getLength(); ++nStyleFam)
f325b2
+    {
f325b2
+        boost::property_tree::ptree aChildren;
f325b2
+        OUString sStyleFam = aStyleFamilies[nStyleFam];
f325b2
+        uno::Reference<container::XNameAccess> xStyleFamily(xStyleFamilies->getByName(sStyleFam), uno::UNO_QUERY);
f325b2
+        uno::Sequence<OUString> aStyles = xStyleFamily->getElementNames();
f325b2
+        for (sal_Int32 nInd = 0; nInd < aStyles.getLength(); ++nInd)
f325b2
+        {
f325b2
+            boost::property_tree::ptree aChild;
f325b2
+            aChild.put("", aStyles[nInd]);
f325b2
+            aChildren.push_back(std::make_pair("", aChild));
f325b2
+        }
f325b2
+        aTree.add_child(sStyleFam.toUtf8().getStr(), aChildren);
f325b2
+    }
f325b2
+    std::stringstream aStream;
f325b2
+    boost::property_tree::write_json(aStream, aTree);
f325b2
+    char* pJson = static_cast<char*>(malloc(aStream.str().size() + 1));
f325b2
+    strcpy(pJson, aStream.str().c_str());
f325b2
+    pJson[aStream.str().size()] = '\0';
f325b2
+    return pJson;
f325b2
+}
f325b2
 static char* lo_getError (LibreOfficeKit *pThis)
f325b2
 {
f325b2
     LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis);
f325b2
diff --git a/include/LibreOfficeKit/LibreOfficeKit.h b/include/LibreOfficeKit/LibreOfficeKit.h
f325b2
index e3b485052444..af7155c4db67 100644
f325b2
--- a/include/LibreOfficeKit/LibreOfficeKit.h
f325b2
+++ b/include/LibreOfficeKit/LibreOfficeKit.h
f325b2
@@ -159,6 +159,9 @@ struct _LibreOfficeKitDocumentClass
f325b2
 
f325b2
     /// @see lok::Document::resetSelection
f325b2
     void (*resetSelection) (LibreOfficeKitDocument* pThis);
f325b2
+
f325b2
+    /// @see lok::Document:getStyles
f325b2
+    char* (*getStyles) (LibreOfficeKitDocument* pThis);
f325b2
 #endif // LOK_USE_UNSTABLE_API
f325b2
 };
f325b2
 
f325b2
diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx
f325b2
index 816ade5649b2..c526bda95593 100644
f325b2
--- a/include/LibreOfficeKit/LibreOfficeKit.hxx
f325b2
+++ b/include/LibreOfficeKit/LibreOfficeKit.hxx
f325b2
@@ -246,6 +246,14 @@ public:
f325b2
     {
f325b2
         mpDoc->pClass->resetSelection(mpDoc);
f325b2
     }
f325b2
+
f325b2
+    /**
f325b2
+     * Returns a json map, {"familyName1" : ["list of style names in the family1"], etc.}
f325b2
+     */
f325b2
+    inline char* getStyles()
f325b2
+    {
f325b2
+        return mpDoc->pClass->getStyles(mpDoc);
f325b2
+    }
f325b2
 #endif // LOK_USE_UNSTABLE_API
f325b2
 };
f325b2
 
f325b2
diff --git a/libreofficekit/qa/unit/tiledrendering.cxx b/libreofficekit/qa/unit/tiledrendering.cxx
f325b2
index fb2f7416ba34..a0f4bcb5b8eb 100644
f325b2
--- a/libreofficekit/qa/unit/tiledrendering.cxx
f325b2
+++ b/libreofficekit/qa/unit/tiledrendering.cxx
f325b2
@@ -9,6 +9,7 @@
f325b2
 
f325b2
 #include <boost/scoped_array.hpp>
f325b2
 #include <boost/scoped_ptr.hpp>
f325b2
+#include <boost/property_tree/json_parser.hpp>
f325b2
 #include <cppunit/TestFixture.h>
f325b2
 #include <cppunit/plugin/TestPlugIn.h>
f325b2
 #include <cppunit/extensions/HelperMacros.h>
f325b2
@@ -67,6 +68,7 @@ public:
f325b2
     void testDocumentTypes( Office* pOffice );
f325b2
     void testImpressSlideNames( Office* pOffice );
f325b2
     void testCalcSheetNames( Office* pOffice );
f325b2
+    void testGetStyles( Office* pOffice );
f325b2
 #if 0
f325b2
     void testOverlay( Office* pOffice );
f325b2
 #endif
f325b2
@@ -93,6 +95,7 @@ void TiledRenderingTest::runAllTests()
f325b2
     testDocumentTypes( pOffice.get() );
f325b2
     testImpressSlideNames( pOffice.get() );
f325b2
     testCalcSheetNames( pOffice.get() );
f325b2
+    testGetStyles( pOffice.get() );
f325b2
 #if 0
f325b2
     testOverlay( pOffice.get() );
f325b2
 #endif
f325b2
@@ -181,6 +184,38 @@ void TiledRenderingTest::testCalcSheetNames( Office* pOffice )
f325b2
     CPPUNIT_ASSERT( strcmp( pDocument->getPartName( 2 ), "Sheet3" ) == 0 );
f325b2
 }
f325b2
 
f325b2
+void TiledRenderingTest::testGetStyles( Office* pOffice )
f325b2
+{
f325b2
+    const string sDocPath = m_sSrcRoot + "/libreofficekit/qa/data/blank_text.odt";
f325b2
+    const string sLockFile = m_sSrcRoot +"/libreofficekit/qa/data/.~lock.blank_text.odt#";
f325b2
+
f325b2
+    // FIXME: LOK will fail when trying to open a locked file
f325b2
+    remove( sLockFile.c_str() );
f325b2
+
f325b2
+    scoped_ptr< Document> pDocument( pOffice->documentLoad( sDocPath.c_str() ) );
f325b2
+
f325b2
+    boost::property_tree::ptree aTree;
f325b2
+    char* pJSON = pDocument->getStyles();
f325b2
+    std::stringstream aStream(pJSON);
f325b2
+    boost::property_tree::read_json(aStream, aTree);
f325b2
+    CPPUNIT_ASSERT( aTree.size() > 0 );
f325b2
+
f325b2
+    for (const std::pair<std::string, boost::property_tree::ptree>& rPair : aTree)
f325b2
+    {
f325b2
+        CPPUNIT_ASSERT( rPair.second.size() > 0);
f325b2
+        if (rPair.first != "CharacterStyles" &&
f325b2
+            rPair.first != "ParagraphStyles" &&
f325b2
+            rPair.first != "FrameStyles" &&
f325b2
+            rPair.first != "PageStyles" &&
f325b2
+            rPair.first != "NumberingStyles" &&
f325b2
+            rPair.first != "CellStyles" &&
f325b2
+            rPair.first != "ShapeStyles")
f325b2
+        {
f325b2
+            CPPUNIT_FAIL("Unknown style family: " + rPair.first);
f325b2
+        }
f325b2
+    }
f325b2
+}
f325b2
+
f325b2
 #if 0
f325b2
 static void dumpRGBABitmap( const OUString& rPath, const unsigned char* pBuffer,
f325b2
                             const int nWidth, const int nHeight )
f325b2
-- 
f325b2
2.12.0
f325b2