LibreOffice Module sw (master)  1
vbadocuments.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 
20 #include <com/sun/star/text/XTextDocument.hpp>
21 #include <com/sun/star/container/XEnumerationAccess.hpp>
22 
23 #include <tools/urlobj.hxx>
24 #include <rtl/ref.hxx>
25 
26 #include "vbadocument.hxx"
27 #include "vbadocuments.hxx"
28 
29 #include <osl/file.hxx>
30 using namespace ::ooo::vba;
31 using namespace ::com::sun::star;
32 
33 static uno::Any
34 getDocument( uno::Reference< uno::XComponentContext > const & xContext, const uno::Reference< text::XTextDocument > &xDoc, const uno::Any& aApplication )
35 {
36  // FIXME: fine as long as SwVbaDocument is stateless ...
37  if( !xDoc.is() )
38  return uno::Any();
39 
40  rtl::Reference<SwVbaDocument> pWb = new SwVbaDocument( uno::Reference< XHelperInterface >( aApplication, uno::UNO_QUERY_THROW ), xContext, xDoc );
41  return uno::Any( uno::Reference< word::XDocument > (pWb) );
42 }
43 
44 namespace {
45 
46 class DocumentEnumImpl : public EnumerationHelperImpl
47 {
48  uno::Any m_aApplication;
49 public:
51  DocumentEnumImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Any& aApplication ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_aApplication( aApplication ) {}
52 
53  virtual uno::Any SAL_CALL nextElement( ) override
54  {
55  uno::Reference< text::XTextDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
56  return getDocument( m_xContext, xDoc, m_aApplication );
57  }
58 };
59 
60 }
61 
62 SwVbaDocuments::SwVbaDocuments( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext ) : SwVbaDocuments_BASE( xParent, xContext, VbaDocumentsBase::WORD_DOCUMENT )
63 {
64 }
65 // XEnumerationAccess
68 {
70 }
71 uno::Reference< container::XEnumeration >
73 {
74  // #FIXME it's possible the DocumentEnumImpl here doesn't reflect
75  // the state of this object (although it should) would be
76  // safer to create an enumeration based on this objects state
77  // rather than one effectively based of the desktop component
78  uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
79  return new DocumentEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration(), Application() );
80 }
81 
84 {
85  uno::Reference< text::XTextDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
86  return getDocument( mxContext, xDoc, Application() );
87 }
88 
89 uno::Any SAL_CALL
90 SwVbaDocuments::Add( const uno::Any& Template, const uno::Any& /*NewTemplate*/, const uno::Any& /*DocumentType*/, const uno::Any& /*Visible*/ )
91 {
92  OUString sFileName;
93  if( Template.hasValue() && ( Template >>= sFileName ) )
94  {
95  return Open( sFileName, uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any());
96  }
97  uno::Reference <text::XTextDocument> xTextDoc( createDocument() , uno::UNO_QUERY_THROW );
98  return getDocument( mxContext, xTextDoc, Application() );
99 }
100 
101 // #TODO# #FIXME# can any of the unused params below be used?
102 // #TODO# #FIXME# surely we should actually close the document here
103 void SAL_CALL
104 SwVbaDocuments::Close( const uno::Any& /*SaveChanges*/, const uno::Any& /*OriginalFormat*/, const uno::Any& /*RouteDocument*/ )
105 {
106 }
107 
108 // #TODO# #FIXME# can any of the unused params below be used?
109 uno::Any SAL_CALL
110 SwVbaDocuments::Open( const OUString& Filename, const uno::Any& /*ConfirmConversions*/, const uno::Any& ReadOnly, const uno::Any& /*AddToRecentFiles*/, const uno::Any& /*PasswordDocument*/, const uno::Any& /*PasswordTemplate*/, const uno::Any& /*Revert*/, const uno::Any& /*WritePasswordDocument*/, const uno::Any& /*WritePasswordTemplate*/, const uno::Any& /*Format*/, const uno::Any& /*Encoding*/, const uno::Any& /*Visible*/, const uno::Any& /*OpenAndRepair*/, const uno::Any& /*DocumentDirection*/, const uno::Any& /*NoEncodingDialog*/, const uno::Any& /*XMLTransform*/ )
111 {
112  SAL_INFO("sw.vba", "Documents.Open(Filename:=" << Filename << ",ReadOnly:=" << ReadOnly << ")");
113 
114  // we need to detect if this is a URL, if not then assume it's a file path
115  OUString aURL;
116  INetURLObject aObj;
117  aObj.SetURL( Filename );
118  bool bIsURL = aObj.GetProtocol() != INetProtocol::NotValid;
119  if ( bIsURL )
120  aURL = Filename;
121  else
122  osl::FileBase::getFileURLFromSystemPath( Filename, aURL );
123 
124  uno::Reference <text::XTextDocument> xSpreadDoc( openDocument( Filename, ReadOnly, {}), uno::UNO_QUERY_THROW );
125  uno::Any aRet = getDocument( mxContext, xSpreadDoc, Application() );
126  uno::Reference< word::XDocument > xDocument( aRet, uno::UNO_QUERY );
127  if ( xDocument.is() )
128  xDocument->Activate();
129  return aRet;
130 }
131 
132 uno::Any SAL_CALL
133 SwVbaDocuments::OpenNoRepairDialog( const OUString& Filename, const uno::Any& ConfirmConversions, const uno::Any& ReadOnly, const uno::Any& AddToRecentFiles, const uno::Any& PasswordDocument, const uno::Any& PasswordTemplate, const uno::Any& Revert, const uno::Any& WritePasswordDocument, const uno::Any& WritePasswordTemplate, const uno::Any& Format, const uno::Any& Encoding, const uno::Any& Visible, const uno::Any& OpenAndRepair, const uno::Any& DocumentDirection, const uno::Any& NoEncodingDialog, const uno::Any& XMLTransform )
134 {
135  return Open( Filename, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenAndRepair, DocumentDirection, NoEncodingDialog, XMLTransform );
136 }
137 
138 uno::Any SAL_CALL
139 SwVbaDocuments::OpenOld( const OUString& FileName, const uno::Any& ConfirmConversions, const uno::Any& ReadOnly, const uno::Any& AddToRecentFiles, const uno::Any& PasswordDocument, const uno::Any& PasswordTemplate, const uno::Any& Revert, const uno::Any& WritePasswordDocument, const uno::Any& WritePasswordTemplate, const uno::Any& Format )
140 {
141  return Open( FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any() );
142 }
143 
144 OUString
146 {
147  return "SwVbaDocuments";
148 }
149 
150 uno::Sequence<OUString>
152 {
153  static uno::Sequence< OUString > const sNames
154  {
155  "ooo.vba.word.Documents"
156  };
157  return sNames;
158 }
159 
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
bool hasValue()
virtual void SAL_CALL Close(const css::uno::Any &SaveChanges, const css::uno::Any &OriginalFormat, const css::uno::Any &RouteDocument) override
uno::Reference< uno::XComponentContext > mxContext
WeakReference< XInterface > mxParent
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Any SAL_CALL OpenOld(const OUString &FileName, const css::uno::Any &ConfirmConversions, const css::uno::Any &ReadOnly, const css::uno::Any &AddToRecentFiles, const css::uno::Any &PasswordDocument, const css::uno::Any &PasswordTemplate, const css::uno::Any &Revert, const css::uno::Any &WritePasswordDocument, const css::uno::Any &WritePasswordTemplate, const css::uno::Any &Format) override
css::uno::Type const & get()
SwVbaDocuments(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual css::uno::Any SAL_CALL Add(const css::uno::Any &Template, const css::uno::Any &NewTemplate, const css::uno::Any &DocumentType, const css::uno::Any &Visible) override
bool SetURL(std::u16string_view rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
static uno::Any getDocument(uno::Reference< uno::XComponentContext > const &xContext, const uno::Reference< text::XTextDocument > &xDoc, const uno::Any &aApplication)
#define SAL_INFO(area, stream)
INetProtocol GetProtocol() const
virtual css::uno::Any SAL_CALL Open(const OUString &Filename, const css::uno::Any &ConfirmConversions, const css::uno::Any &ReadOnly, const css::uno::Any &AddToRecentFiles, const css::uno::Any &PasswordDocument, const css::uno::Any &PasswordTemplate, const css::uno::Any &Revert, const css::uno::Any &WritePasswordDocument, const css::uno::Any &WritePasswordTemplate, const css::uno::Any &Format, const css::uno::Any &Encoding, const css::uno::Any &Visible, const css::uno::Any &OpenAndRepair, const css::uno::Any &DocumentDirection, const css::uno::Any &NoEncodingDialog, const css::uno::Any &XMLTransform) override
virtual css::uno::Any SAL_CALL OpenNoRepairDialog(const OUString &Filename, const css::uno::Any &ConfirmConversions, const css::uno::Any &ReadOnly, const css::uno::Any &AddToRecentFiles, const css::uno::Any &PasswordDocument, const css::uno::Any &PasswordTemplate, const css::uno::Any &Revert, const css::uno::Any &WritePasswordDocument, const css::uno::Any &WritePasswordTemplate, const css::uno::Any &Format, const css::uno::Any &Encoding, const css::uno::Any &Visible, const css::uno::Any &OpenAndRepair, const css::uno::Any &DocumentDirection, const css::uno::Any &NoEncodingDialog, const css::uno::Any &XMLTransform) override
virtual OUString getServiceImplName() override
cppu::ImplInheritanceHelper< VbaDocumentsBase, ov::word::XDocuments > SwVbaDocuments_BASE