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