Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #include "vbatablesofcontents.hxx"
20 : #include "vbatableofcontents.hxx"
21 : #include "vbarange.hxx"
22 : #include <com/sun/star/text/XDocumentIndexesSupplier.hpp>
23 : #include <cppuhelper/implbase.hxx>
24 :
25 : using namespace ::ooo::vba;
26 : using namespace ::com::sun::star;
27 :
28 : typedef ::cppu::WeakImplHelper< container::XIndexAccess, container::XEnumerationAccess > TableOfContentsCollectionHelper_Base;
29 : typedef std::vector< uno::Reference< text::XDocumentIndex > > XTocVec;
30 :
31 0 : class TablesOfContentsEnumWrapper : public EnumerationHelper_BASE
32 : {
33 : uno::Reference< container::XIndexAccess > mxIndexAccess;
34 : sal_Int32 nIndex;
35 :
36 : public:
37 0 : explicit TablesOfContentsEnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : mxIndexAccess( xIndexAccess ), nIndex( 0 )
38 : {
39 0 : }
40 0 : virtual sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
41 : {
42 0 : return ( nIndex < mxIndexAccess->getCount() );
43 : }
44 :
45 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
46 : {
47 0 : if( nIndex < mxIndexAccess->getCount() )
48 : {
49 0 : return mxIndexAccess->getByIndex( nIndex++ );
50 : }
51 0 : throw container::NoSuchElementException();
52 : }
53 : };
54 :
55 : class TableOfContentsCollectionHelper : public TableOfContentsCollectionHelper_Base
56 : {
57 : private:
58 : uno::Reference< XHelperInterface > mxParent;
59 : uno::Reference< uno::XComponentContext > mxContext;
60 : uno::Reference< text::XTextDocument > mxTextDocument;
61 : XTocVec maToc;
62 :
63 : public:
64 0 : TableOfContentsCollectionHelper( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< text::XTextDocument >& xDoc ) throw ( uno::RuntimeException ): mxParent( xParent ), mxContext( xContext ), mxTextDocument( xDoc )
65 : {
66 0 : uno::Reference< text::XDocumentIndexesSupplier > xDocIndexSupp( mxTextDocument, uno::UNO_QUERY_THROW );
67 0 : uno::Reference< container::XIndexAccess > xDocIndexes = xDocIndexSupp->getDocumentIndexes();
68 0 : sal_Int32 nCount = xDocIndexes->getCount();
69 0 : for( sal_Int32 i = 0; i < nCount; i++ )
70 : {
71 0 : uno::Reference< text::XDocumentIndex > xToc( xDocIndexes->getByIndex(i), uno::UNO_QUERY_THROW );
72 0 : if( xToc->getServiceName() == "com.sun.star.text.ContentIndex" )
73 : {
74 0 : maToc.push_back( xToc );
75 : }
76 0 : }
77 0 : }
78 :
79 0 : virtual ~TableOfContentsCollectionHelper() {}
80 :
81 0 : virtual sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
82 : {
83 0 : return maToc.size();
84 : }
85 0 : virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
86 : {
87 0 : if ( Index < 0 || Index >= getCount() )
88 0 : throw lang::IndexOutOfBoundsException();
89 :
90 0 : uno::Reference< text::XDocumentIndex > xToc( maToc[Index], uno::UNO_QUERY_THROW );
91 0 : return uno::makeAny( uno::Reference< word::XTableOfContents >( new SwVbaTableOfContents( mxParent, mxContext, mxTextDocument, xToc ) ) );
92 : }
93 0 : virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
94 : {
95 0 : return cppu::UnoType<word::XTableOfContents>::get();
96 : }
97 0 : virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
98 : {
99 0 : return sal_True;
100 : }
101 : // XEnumerationAccess
102 0 : virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
103 : {
104 0 : return new TablesOfContentsEnumWrapper( this );
105 : }
106 : };
107 :
108 0 : SwVbaTablesOfContents::SwVbaTablesOfContents( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< text::XTextDocument >& xDoc ) throw (uno::RuntimeException) : SwVbaTablesOfContents_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new TableOfContentsCollectionHelper( xParent, xContext, xDoc ) ) ), mxTextDocument( xDoc )
109 : {
110 0 : }
111 :
112 : uno::Reference< word::XTableOfContents > SAL_CALL
113 0 : SwVbaTablesOfContents::Add( const uno::Reference< word::XRange >& Range, const uno::Any& /*UseHeadingStyles*/, const uno::Any& /*UpperHeadingLevel*/, const uno::Any& LowerHeadingLevel, const uno::Any& UseFields, const uno::Any& /*TableID*/, const uno::Any& /*RightAlignPageNumbers*/, const uno::Any& /*IncludePageNumbers*/, const uno::Any& /*AddedStyles*/, const uno::Any& /*UseHyperlinks*/, const uno::Any& /*HidePageNumbersInWeb*/, const uno::Any& /*UseOutlineLevels*/ ) throw (uno::RuntimeException, std::exception)
114 : {
115 0 : uno::Reference< lang::XMultiServiceFactory > xDocMSF( mxTextDocument, uno::UNO_QUERY_THROW );
116 0 : uno::Reference< text::XDocumentIndex > xDocumentIndex( xDocMSF->createInstance("com.sun.star.text.ContentIndex"), uno::UNO_QUERY_THROW );
117 :
118 0 : uno::Reference< beans::XPropertySet > xTocProps( xDocumentIndex, uno::UNO_QUERY_THROW );
119 0 : bool isProtected = false;
120 0 : xTocProps->setPropertyValue("IsProtected", uno::makeAny( isProtected ) );
121 :
122 0 : uno::Reference< word::XTableOfContents > xToc( new SwVbaTableOfContents( this, mxContext, mxTextDocument, xDocumentIndex ) );
123 :
124 0 : sal_Int32 nLowerHeadingLevel = 9;
125 0 : if( LowerHeadingLevel.hasValue() )
126 0 : LowerHeadingLevel >>= nLowerHeadingLevel;
127 0 : xToc->setLowerHeadingLevel( nLowerHeadingLevel );
128 :
129 0 : bool bUseFields = false;
130 0 : if( UseFields.hasValue() )
131 0 : UseFields >>= bUseFields;
132 0 : xToc->setUseFields( bUseFields );
133 :
134 0 : bool bUseOutlineLevels = true;
135 0 : xToc->setUseOutlineLevels( bUseOutlineLevels );
136 :
137 0 : SwVbaRange* pVbaRange = dynamic_cast<SwVbaRange*>( Range.get() );
138 0 : if( !pVbaRange )
139 0 : throw uno::RuntimeException();
140 :
141 0 : uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
142 0 : uno::Reference< text::XText > xText = pVbaRange->getXText();
143 0 : uno::Reference< text::XTextContent > xTextContent( xDocumentIndex, uno::UNO_QUERY_THROW );
144 0 : xText->insertTextContent( xTextRange, xTextContent, sal_False );
145 0 : xToc->Update();
146 :
147 0 : return xToc;
148 : }
149 :
150 : // XEnumerationAccess
151 : uno::Type
152 0 : SwVbaTablesOfContents::getElementType() throw (uno::RuntimeException)
153 : {
154 0 : return cppu::UnoType<word::XTableOfContents>::get();
155 : }
156 : uno::Reference< container::XEnumeration >
157 0 : SwVbaTablesOfContents::createEnumeration() throw (uno::RuntimeException)
158 : {
159 0 : return new TablesOfContentsEnumWrapper( m_xIndexAccess );
160 : }
161 :
162 : uno::Any
163 0 : SwVbaTablesOfContents::createCollectionObject( const uno::Any& aSource )
164 : {
165 0 : return aSource;
166 : }
167 :
168 : OUString
169 0 : SwVbaTablesOfContents::getServiceImplName()
170 : {
171 0 : return OUString("SwVbaTablesOfContents");
172 : }
173 :
174 : uno::Sequence<OUString>
175 0 : SwVbaTablesOfContents::getServiceNames()
176 : {
177 0 : static uno::Sequence< OUString > sNames;
178 0 : if ( sNames.getLength() == 0 )
179 : {
180 0 : sNames.realloc( 1 );
181 0 : sNames[0] = "ooo.vba.word.TablesOfContents";
182 : }
183 0 : return sNames;
184 : }
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|