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