LibreOffice Module sw (master) 1
vbarevisions.cxx
Go to the documentation of this file.
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 "vbarevisions.hxx"
20#include "vbarevision.hxx"
22#include <com/sun/star/beans/XPropertySet.hpp>
23#include <com/sun/star/document/XRedlinesSupplier.hpp>
24#include <com/sun/star/frame/XModel.hpp>
25#include <com/sun/star/text/XTextRangeCompare.hpp>
26#include <utility>
27
28using namespace ::ooo::vba;
29using namespace ::com::sun::star;
30
31typedef std::vector< uno::Reference< beans::XPropertySet > > RevisionMap;
32
33namespace {
34
35class RedlinesEnumeration : public ::cppu::WeakImplHelper< container::XEnumeration >
36{
37 RevisionMap mRevisionMap;
38 RevisionMap::iterator mIt;
39public:
40 explicit RedlinesEnumeration( RevisionMap&& sMap ) : mRevisionMap( std::move(sMap) ), mIt( mRevisionMap.begin() ) {}
41 virtual sal_Bool SAL_CALL hasMoreElements( ) override
42 {
43 return ( mIt != mRevisionMap.end() );
44 }
45 virtual uno::Any SAL_CALL nextElement( ) override
46 {
47 if ( !hasMoreElements() )
48 throw container::NoSuchElementException();
49 uno::Reference< beans::XPropertySet > xRevision( *mIt++ );
50 return uno::Any( xRevision ) ;
51 }
52};
53
54class RevisionCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
55 container::XEnumerationAccess >
56{
57 RevisionMap mRevisionMap;
58public:
60RevisionCollectionHelper( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XTextRange >& xTextRange );
61
62 // XElementAccess
63 virtual uno::Type SAL_CALL getElementType( ) override { return cppu::UnoType<beans::XPropertySet>::get(); }
64 virtual sal_Bool SAL_CALL hasElements( ) override { return ( !mRevisionMap.empty() ); }
65 // XIndexAccess
66 virtual ::sal_Int32 SAL_CALL getCount( ) override { return mRevisionMap.size(); }
67 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
68 {
69 if ( Index < 0 || Index >= getCount() )
70 throw lang::IndexOutOfBoundsException();
71
72 return uno::Any( mRevisionMap[ Index ] );
73
74 }
75 // XEnumerationAccess
76 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
77 {
78 return new RedlinesEnumeration( std::vector(mRevisionMap) );
79 }
80};
81
82}
83
84RevisionCollectionHelper::RevisionCollectionHelper( const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XTextRange >& xTextRange )
85 {
86 uno::Reference< text::XTextRangeCompare > xTRC( xTextRange->getText(), uno::UNO_QUERY_THROW );
87 uno::Reference< document::XRedlinesSupplier > xRedlinesSupp( xModel, uno::UNO_QUERY_THROW );
88 uno::Reference< container::XIndexAccess > xRedlines( xRedlinesSupp->getRedlines(), uno::UNO_QUERY_THROW );
89 sal_Int32 nCount = xRedlines->getCount();
90 for( sal_Int32 index = 0; index < nCount; index++ )
91 {
92 uno::Reference< text::XTextRange > xRedlineRange( xRedlines->getByIndex( index ), uno::UNO_QUERY_THROW );
93 if( xTRC->compareRegionStarts( xTextRange, xRedlineRange ) >= 0 && xTRC->compareRegionEnds( xTextRange, xRedlineRange ) <= 0 )
94 {
95 uno::Reference< beans::XPropertySet > xRedlineProps( xRedlineRange, uno::UNO_QUERY_THROW );
96 mRevisionMap.push_back( xRedlineProps );
97 }
98 }
99 }
100
101namespace {
102
103class RevisionsEnumeration : public EnumerationHelperImpl
104{
105 uno::Reference< frame::XModel > m_xModel;
106public:
108 RevisionsEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, uno::Reference< frame::XModel > xModel ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_xModel(std::move( xModel )) {}
109
110 virtual uno::Any SAL_CALL nextElement( ) override
111 {
112 uno::Reference< beans::XPropertySet > xRevision( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
113 return uno::Any( uno::Reference< word::XRevision > ( new SwVbaRevision( m_xParent, m_xContext, m_xModel, xRevision ) ) );
114 }
115
116};
117
118}
119
120SwVbaRevisions::SwVbaRevisions( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XTextRange >& xTextRange ): SwVbaRevisions_BASE( xParent, xContext, new RevisionCollectionHelper( xModel, xTextRange ) ), mxModel( xModel )
121{
122}
123
124SwVbaRevisions::SwVbaRevisions( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, uno::Reference< frame::XModel > xModel, const uno::Reference< container::XIndexAccess >& xIndexAccess ): SwVbaRevisions_BASE( xParent, xContext, xIndexAccess ), mxModel(std::move( xModel ))
125{
126}
127
128// XEnumerationAccess
131{
133}
134uno::Reference< container::XEnumeration >
136{
137 uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
138 return new RevisionsEnumeration( this, mxContext, xEnumAccess->createEnumeration(), mxModel );
139}
140
142SwVbaRevisions::createCollectionObject( const css::uno::Any& aSource )
143{
144 uno::Reference< beans::XPropertySet > xRevision( aSource, uno::UNO_QUERY_THROW );
145 return uno::Any( uno::Reference< word::XRevision > ( new SwVbaRevision( this, mxContext, mxModel, xRevision ) ) );
146}
147
149{
150 // First we need to put all the redline into a vector, because if the redline is accepted,
151 // it will auto delete in the document.
152 std::vector< uno::Reference< word::XRevision > > aRevisions;
153 uno::Reference< container::XEnumeration > xEnumeration = createEnumeration();
154 while( xEnumeration->hasMoreElements() )
155 {
156 uno::Reference< word::XRevision > xRevision( xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
157 aRevisions.push_back( xRevision );
158 }
159
160 for( const auto& xRevision : aRevisions )
161 xRevision->Accept();
162}
163
165{
166 throw uno::RuntimeException();
167}
168
169OUString
171{
172 return "SwVbaRevisions";
173}
174
175css::uno::Sequence<OUString>
177{
178 static uno::Sequence< OUString > const sNames
179 {
180 "ooo.vba.word.Revisions"
181 };
182 return sNames;
183}
184
185/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
css::uno::Reference< css::frame::XModel2 > mxModel
css::uno::Reference< css::uno::XComponentContext > mxContext
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual OUString getServiceImplName() override
virtual css::uno::Type SAL_CALL getElementType() override
css::uno::Reference< css::frame::XModel > mxModel
virtual void SAL_CALL AcceptAll() override
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
SwVbaRevisions(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xModel, const css::uno::Reference< css::text::XTextRange > &xTextRange)
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
virtual void SAL_CALL RejectAll() override
css::uno::Type const & get()
int nCount
Reference< frame::XModel > m_xModel
index
Reference< XModel > xModel
unsigned char sal_Bool
std::vector< uno::Reference< beans::XPropertySet > > RevisionMap