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