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