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 "vbabookmarks.hxx"
20 : #include "vbabookmark.hxx"
21 : #include <com/sun/star/container/XNamed.hpp>
22 : #include <com/sun/star/text/XTextDocument.hpp>
23 : #include <com/sun/star/text/XTextViewCursor.hpp>
24 : #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
25 : #include <ooo/vba/word/WdBookmarkSortBy.hpp>
26 : #include "vbarange.hxx"
27 : #include "wordvbahelper.hxx"
28 : #include <cppuhelper/implbase2.hxx>
29 :
30 : using namespace ::ooo::vba;
31 : using namespace ::com::sun::star;
32 :
33 0 : class BookmarksEnumeration : public EnumerationHelperImpl
34 : {
35 : uno::Reference< frame::XModel > mxModel;
36 : public:
37 0 : BookmarksEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), mxModel( xModel ) {}
38 :
39 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
40 : {
41 0 : uno::Reference< container::XNamed > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
42 0 : OUString aName = xNamed->getName();
43 0 : return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( m_xParent, m_xContext, mxModel, aName ) ) );
44 : }
45 :
46 : };
47 :
48 : // Bookmarks use case-insensitive name lookup in MS Word.
49 : typedef ::cppu::WeakImplHelper2< container::XNameAccess, container::XIndexAccess > BookmarkCollectionHelper_BASE;
50 0 : class BookmarkCollectionHelper : public BookmarkCollectionHelper_BASE
51 : {
52 : private:
53 : uno::Reference< container::XNameAccess > mxNameAccess;
54 : uno::Reference< container::XIndexAccess > mxIndexAccess;
55 : uno::Any cachePos;
56 : public:
57 0 : BookmarkCollectionHelper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) throw (uno::RuntimeException) : mxIndexAccess( xIndexAccess )
58 : {
59 0 : mxNameAccess.set( mxIndexAccess, uno::UNO_QUERY_THROW );
60 0 : }
61 : // XElementAccess
62 0 : virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return mxIndexAccess->getElementType(); }
63 0 : virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return mxIndexAccess->hasElements(); }
64 : // XNameAcess
65 0 : virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
66 : {
67 0 : if ( !hasByName(aName) )
68 0 : throw container::NoSuchElementException();
69 0 : return cachePos;
70 : }
71 0 : virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
72 : {
73 0 : return mxNameAccess->getElementNames();
74 : }
75 0 : virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
76 : {
77 0 : if( mxNameAccess->hasByName( aName ) )
78 : {
79 0 : cachePos = mxNameAccess->getByName( aName );
80 0 : return sal_True;
81 : }
82 : else
83 : {
84 0 : for( sal_Int32 nIndex = 0; nIndex < mxIndexAccess->getCount(); nIndex++ )
85 : {
86 0 : uno::Reference< container::XNamed > xNamed( mxIndexAccess->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
87 0 : OUString aBookmarkName = xNamed->getName();
88 0 : if( aName.equalsIgnoreAsciiCase( aBookmarkName ) )
89 : {
90 0 : cachePos <<= xNamed;
91 0 : return sal_True;
92 : }
93 0 : }
94 : }
95 0 : return sal_False;
96 : }
97 : // XIndexAccess
98 0 : virtual ::sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
99 : {
100 0 : return mxIndexAccess->getCount();
101 : }
102 0 : virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception ) SAL_OVERRIDE
103 : {
104 0 : return mxIndexAccess->getByIndex( Index );
105 : }
106 : };
107 :
108 0 : SwVbaBookmarks::SwVbaBookmarks( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< container::XIndexAccess >& xBookmarks, const uno::Reference< frame::XModel >& xModel ): SwVbaBookmarks_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new BookmarkCollectionHelper( xBookmarks ) ) ), mxModel( xModel )
109 : {
110 0 : mxBookmarksSupplier.set( mxModel, uno::UNO_QUERY_THROW );
111 0 : uno::Reference< text::XTextDocument > xDocument( mxModel, uno::UNO_QUERY_THROW );
112 0 : }
113 : // XEnumerationAccess
114 : uno::Type
115 0 : SwVbaBookmarks::getElementType() throw (uno::RuntimeException)
116 : {
117 0 : return cppu::UnoType<word::XBookmark>::get();
118 : }
119 : uno::Reference< container::XEnumeration >
120 0 : SwVbaBookmarks::createEnumeration() throw (uno::RuntimeException)
121 : {
122 0 : uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
123 0 : return new BookmarksEnumeration( getParent(), mxContext,xEnumAccess->createEnumeration(), mxModel );
124 : }
125 :
126 : uno::Any
127 0 : SwVbaBookmarks::createCollectionObject( const css::uno::Any& aSource )
128 : {
129 0 : uno::Reference< container::XNamed > xNamed( aSource, uno::UNO_QUERY_THROW );
130 0 : OUString aName = xNamed->getName();
131 0 : return uno::makeAny( uno::Reference< word::XBookmark > ( new SwVbaBookmark( getParent(), mxContext, mxModel, aName ) ) );
132 : }
133 :
134 0 : void SwVbaBookmarks::removeBookmarkByName( const OUString& rName ) throw (uno::RuntimeException)
135 : {
136 0 : uno::Reference< text::XTextContent > xBookmark( m_xNameAccess->getByName( rName ), uno::UNO_QUERY_THROW );
137 0 : word::getXTextViewCursor( mxModel )->getText()->removeTextContent( xBookmark );
138 0 : }
139 :
140 0 : void SwVbaBookmarks::addBookmarkByName( const uno::Reference< frame::XModel >& xModel, const OUString& rName, const uno::Reference< text::XTextRange >& rTextRange ) throw (uno::RuntimeException)
141 : {
142 0 : uno::Reference< lang::XMultiServiceFactory > xDocMSF( xModel, uno::UNO_QUERY_THROW );
143 0 : uno::Reference< text::XTextContent > xBookmark( xDocMSF->createInstance("com.sun.star.text.Bookmark"), uno::UNO_QUERY_THROW );
144 0 : uno::Reference< container::XNamed > xNamed( xBookmark, uno::UNO_QUERY_THROW );
145 0 : xNamed->setName( rName );
146 0 : rTextRange->getText()->insertTextContent( rTextRange, xBookmark, sal_False );
147 0 : }
148 :
149 : uno::Any SAL_CALL
150 0 : SwVbaBookmarks::Add( const OUString& rName, const uno::Any& rRange ) throw (uno::RuntimeException, std::exception)
151 : {
152 0 : uno::Reference< text::XTextRange > xTextRange;
153 0 : uno::Reference< word::XRange > xRange;
154 0 : if( rRange >>= xRange )
155 : {
156 0 : SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( xRange.get() );
157 0 : if( pRange )
158 0 : xTextRange = pRange->getXTextRange();
159 : }
160 : else
161 : {
162 : // FIXME: insert the bookmark into current view cursor
163 0 : xTextRange.set( word::getXTextViewCursor( mxModel ), uno::UNO_QUERY_THROW );
164 : }
165 :
166 : // remove the exist bookmark
167 0 : OUString aName = rName;
168 0 : if( m_xNameAccess->hasByName( aName ) )
169 0 : removeBookmarkByName( aName );
170 :
171 0 : addBookmarkByName( mxModel, aName, xTextRange );
172 :
173 0 : return uno::makeAny( uno::Reference< word::XBookmark >( new SwVbaBookmark( getParent(), mxContext, mxModel, aName ) ) );
174 : }
175 :
176 : sal_Int32 SAL_CALL
177 0 : SwVbaBookmarks::getDefaultSorting() throw (css::uno::RuntimeException, std::exception)
178 : {
179 0 : return word::WdBookmarkSortBy::wdSortByName;
180 : }
181 :
182 : void SAL_CALL
183 0 : SwVbaBookmarks::setDefaultSorting( sal_Int32/* _type*/ ) throw (css::uno::RuntimeException, std::exception)
184 : {
185 : // not support in Writer
186 0 : }
187 :
188 : sal_Bool SAL_CALL
189 0 : SwVbaBookmarks::getShowHidden() throw (css::uno::RuntimeException, std::exception)
190 : {
191 0 : return sal_True;
192 : }
193 :
194 : void SAL_CALL
195 0 : SwVbaBookmarks::setShowHidden( sal_Bool /*_hidden*/ ) throw (css::uno::RuntimeException, std::exception)
196 : {
197 : // not support in Writer
198 0 : }
199 :
200 : sal_Bool SAL_CALL
201 0 : SwVbaBookmarks::Exists( const OUString& rName ) throw (css::uno::RuntimeException, std::exception)
202 : {
203 0 : sal_Bool bExist = m_xNameAccess->hasByName( rName );
204 0 : return bExist;
205 : }
206 :
207 : OUString
208 0 : SwVbaBookmarks::getServiceImplName()
209 : {
210 0 : return OUString("SwVbaBookmarks");
211 : }
212 :
213 : css::uno::Sequence<OUString>
214 0 : SwVbaBookmarks::getServiceNames()
215 : {
216 0 : static uno::Sequence< OUString > sNames;
217 0 : if ( sNames.getLength() == 0 )
218 : {
219 0 : sNames.realloc( 1 );
220 0 : sNames[0] = "ooo.vba.word.Bookmarks";
221 : }
222 0 : return sNames;
223 : }
224 :
225 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|