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