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 :
24 : using namespace ::ooo::vba;
25 : using namespace ::com::sun::star;
26 :
27 : typedef ::cppu::WeakImplHelper2< container::XIndexAccess, container::XEnumerationAccess > TableOfContentsCollectionHelper_Base;
28 : typedef std::vector< uno::Reference< text::XDocumentIndex > > XTocVec;
29 :
30 0 : class TablesOfContentsEnumWrapper : public EnumerationHelper_BASE
31 : {
32 : uno::Reference< container::XIndexAccess > mxIndexAccess;
33 : sal_Int32 nIndex;
34 :
35 : public:
36 0 : TablesOfContentsEnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : mxIndexAccess( xIndexAccess ), nIndex( 0 )
37 : {
38 0 : }
39 0 : virtual sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
40 : {
41 0 : return ( nIndex < mxIndexAccess->getCount() );
42 : }
43 :
44 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
45 : {
46 0 : if( nIndex < mxIndexAccess->getCount() )
47 : {
48 0 : return mxIndexAccess->getByIndex( nIndex++ );
49 : }
50 0 : throw container::NoSuchElementException();
51 : }
52 : };
53 :
54 : class TableOfContentsCollectionHelper : public TableOfContentsCollectionHelper_Base
55 : {
56 : private:
57 : uno::Reference< XHelperInterface > mxParent;
58 : uno::Reference< uno::XComponentContext > mxContext;
59 : uno::Reference< text::XTextDocument > mxTextDocument;
60 : XTocVec maToc;
61 :
62 : public:
63 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 )
64 : {
65 0 : uno::Reference< text::XDocumentIndexesSupplier > xDocIndexSupp( mxTextDocument, uno::UNO_QUERY_THROW );
66 0 : uno::Reference< container::XIndexAccess > xDocIndexes = xDocIndexSupp->getDocumentIndexes();
67 0 : sal_Int32 nCount = xDocIndexes->getCount();
68 0 : for( sal_Int32 i = 0; i < nCount; i++ )
69 : {
70 0 : uno::Reference< text::XDocumentIndex > xToc( xDocIndexes->getByIndex(i), uno::UNO_QUERY_THROW );
71 0 : if( xToc->getServiceName() == "com.sun.star.text.ContentIndex" )
72 : {
73 0 : maToc.push_back( xToc );
74 : }
75 0 : }
76 0 : }
77 :
78 0 : virtual ~TableOfContentsCollectionHelper() {}
79 :
80 0 : virtual sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
81 : {
82 0 : return maToc.size();
83 : }
84 0 : virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
85 : {
86 0 : if ( Index < 0 || Index >= getCount() )
87 0 : throw lang::IndexOutOfBoundsException();
88 :
89 0 : uno::Reference< text::XDocumentIndex > xToc( maToc[Index], uno::UNO_QUERY_THROW );
90 0 : return uno::makeAny( uno::Reference< word::XTableOfContents >( new SwVbaTableOfContents( mxParent, mxContext, mxTextDocument, xToc ) ) );
91 : }
92 0 : virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
93 : {
94 0 : return cppu::UnoType<word::XTableOfContents>::get();
95 : }
96 0 : virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
97 : {
98 0 : return sal_True;
99 : }
100 : // XEnumerationAccess
101 0 : virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
102 : {
103 0 : return new TablesOfContentsEnumWrapper( this );
104 : }
105 : };
106 :
107 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 )
108 : {
109 0 : }
110 :
111 : uno::Reference< word::XTableOfContents > SAL_CALL
112 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)
113 : {
114 0 : uno::Reference< lang::XMultiServiceFactory > xDocMSF( mxTextDocument, uno::UNO_QUERY_THROW );
115 0 : uno::Reference< text::XDocumentIndex > xDocumentIndex( xDocMSF->createInstance("com.sun.star.text.ContentIndex"), uno::UNO_QUERY_THROW );
116 :
117 0 : uno::Reference< beans::XPropertySet > xTocProps( xDocumentIndex, uno::UNO_QUERY_THROW );
118 0 : sal_Bool isProtected = sal_False;
119 0 : xTocProps->setPropertyValue("IsProtected", uno::makeAny( isProtected ) );
120 :
121 0 : uno::Reference< word::XTableOfContents > xToc( new SwVbaTableOfContents( this, mxContext, mxTextDocument, xDocumentIndex ) );
122 :
123 0 : sal_Int32 nLowerHeadingLevel = 9;
124 0 : if( LowerHeadingLevel.hasValue() )
125 0 : LowerHeadingLevel >>= nLowerHeadingLevel;
126 0 : xToc->setLowerHeadingLevel( nLowerHeadingLevel );
127 :
128 0 : sal_Bool bUseFields = sal_False;
129 0 : if( UseFields.hasValue() )
130 0 : UseFields >>= bUseFields;
131 0 : xToc->setUseFields( bUseFields );
132 :
133 0 : sal_Bool bUseOutlineLevels = sal_True;
134 0 : xToc->setUseOutlineLevels( bUseOutlineLevels );
135 :
136 0 : SwVbaRange* pVbaRange = dynamic_cast<SwVbaRange*>( Range.get() );
137 0 : if( !pVbaRange )
138 0 : throw uno::RuntimeException();
139 :
140 0 : uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
141 0 : uno::Reference< text::XText > xText = pVbaRange->getXText();
142 0 : uno::Reference< text::XTextContent > xTextContent( xDocumentIndex, uno::UNO_QUERY_THROW );
143 0 : xText->insertTextContent( xTextRange, xTextContent, sal_False );
144 0 : xToc->Update();
145 :
146 0 : return xToc;
147 : }
148 :
149 : // XEnumerationAccess
150 : uno::Type
151 0 : SwVbaTablesOfContents::getElementType() throw (uno::RuntimeException)
152 : {
153 0 : return cppu::UnoType<word::XTableOfContents>::get();
154 : }
155 : uno::Reference< container::XEnumeration >
156 0 : SwVbaTablesOfContents::createEnumeration() throw (uno::RuntimeException)
157 : {
158 0 : return new TablesOfContentsEnumWrapper( m_xIndexAccess );
159 : }
160 :
161 : uno::Any
162 0 : SwVbaTablesOfContents::createCollectionObject( const uno::Any& aSource )
163 : {
164 0 : return aSource;
165 : }
166 :
167 : OUString
168 0 : SwVbaTablesOfContents::getServiceImplName()
169 : {
170 0 : return OUString("SwVbaTablesOfContents");
171 : }
172 :
173 : uno::Sequence<OUString>
174 0 : SwVbaTablesOfContents::getServiceNames()
175 : {
176 0 : static uno::Sequence< OUString > sNames;
177 0 : if ( sNames.getLength() == 0 )
178 : {
179 0 : sNames.realloc( 1 );
180 0 : sNames[0] = "ooo.vba.word.TablesOfContents";
181 : }
182 0 : return sNames;
183 : }
184 :
185 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|