1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "vbarevisions.hxx"
20 #include "vbarevision.hxx"
21 #include <cppuhelper/implbase.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>
28 using namespace ::ooo::vba
;
29 using namespace ::com::sun::star
;
31 typedef std::vector
< uno::Reference
< beans::XPropertySet
> > RevisionMap
;
35 class RedlinesEnumeration
: public ::cppu::WeakImplHelper
< container::XEnumeration
>
37 RevisionMap mRevisionMap
;
38 RevisionMap::iterator mIt
;
40 explicit RedlinesEnumeration( RevisionMap
&& sMap
) : mRevisionMap( std::move(sMap
) ), mIt( mRevisionMap
.begin() ) {}
41 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
43 return ( mIt
!= mRevisionMap
.end() );
45 virtual uno::Any SAL_CALL
nextElement( ) override
47 if ( !hasMoreElements() )
48 throw container::NoSuchElementException();
49 uno::Reference
< beans::XPropertySet
> xRevision( *mIt
++ );
50 return uno::Any( xRevision
) ;
54 class RevisionCollectionHelper
: public ::cppu::WeakImplHelper
< container::XIndexAccess
,
55 container::XEnumerationAccess
>
57 RevisionMap mRevisionMap
;
59 /// @throws css::uno::RuntimeException
60 RevisionCollectionHelper( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XTextRange
>& xTextRange
);
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() ); }
66 virtual ::sal_Int32 SAL_CALL
getCount( ) override
{ return mRevisionMap
.size(); }
67 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) override
69 if ( Index
< 0 || Index
>= getCount() )
70 throw lang::IndexOutOfBoundsException();
72 return uno::Any( mRevisionMap
[ Index
] );
76 virtual uno::Reference
< container::XEnumeration
> SAL_CALL
createEnumeration( ) override
78 return new RedlinesEnumeration( std::vector(mRevisionMap
) );
84 RevisionCollectionHelper::RevisionCollectionHelper( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XTextRange
>& xTextRange
)
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
++ )
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 )
95 uno::Reference
< beans::XPropertySet
> xRedlineProps( xRedlineRange
, uno::UNO_QUERY_THROW
);
96 mRevisionMap
.push_back( xRedlineProps
);
103 class RevisionsEnumeration
: public EnumerationHelperImpl
105 uno::Reference
< frame::XModel
> m_xModel
;
107 /// @throws uno::RuntimeException
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
)) {}
110 virtual uno::Any SAL_CALL
nextElement( ) override
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
) ) );
120 SwVbaRevisions::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
)
124 SwVbaRevisions::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
))
128 // XEnumerationAccess
130 SwVbaRevisions::getElementType()
132 return cppu::UnoType
<word::XRevision
>::get();
134 uno::Reference
< container::XEnumeration
>
135 SwVbaRevisions::createEnumeration()
137 uno::Reference
< container::XEnumerationAccess
> xEnumAccess( m_xIndexAccess
, uno::UNO_QUERY_THROW
);
138 return new RevisionsEnumeration( this, mxContext
, xEnumAccess
->createEnumeration(), mxModel
);
142 SwVbaRevisions::createCollectionObject( const css::uno::Any
& aSource
)
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
) ) );
148 void SAL_CALL
SwVbaRevisions::AcceptAll( )
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() )
156 uno::Reference
< word::XRevision
> xRevision( xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
157 aRevisions
.push_back( xRevision
);
160 for( const auto& xRevision
: aRevisions
)
164 void SAL_CALL
SwVbaRevisions::RejectAll( )
166 throw uno::RuntimeException();
170 SwVbaRevisions::getServiceImplName()
172 return "SwVbaRevisions";
175 css::uno::Sequence
<OUString
>
176 SwVbaRevisions::getServiceNames()
178 static uno::Sequence
< OUString
> const sNames
180 "ooo.vba.word.Revisions"
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */