LibreOffice Module dbaccess (master) 1
linkeddocuments.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 <core_resource.hxx>
21#include <linkeddocuments.hxx>
22#include <osl/diagnose.h>
27#include <com/sun/star/lang/XMultiServiceFactory.hpp>
28#include <com/sun/star/frame/XComponentLoader.hpp>
29#include <com/sun/star/sdbc/SQLException.hpp>
30#include <com/sun/star/ucb/XCommandProcessor.hpp>
31#include <com/sun/star/ucb/OpenCommandArgument.hpp>
32#include <com/sun/star/ucb/OpenMode.hpp>
33#include <com/sun/star/task/XJobExecutor.hpp>
34#include <comphelper/types.hxx>
35#include <strings.hrc>
36#include <strings.hxx>
37#include <svl/filenotation.hxx>
38#include <browserids.hxx>
39#include <com/sun/star/container/XHierarchicalNameContainer.hpp>
41#include <utility>
42#include <vcl/weld.hxx>
43
46#include <com/sun/star/io/WrongFormatException.hpp>
47
48namespace dbaui
49{
50
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::frame;
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::util;
57 using namespace ::com::sun::star::ucb;
58 using namespace ::com::sun::star::sdbc;
59 using namespace ::com::sun::star::sdb::application;
60 using namespace ::com::sun::star::task;
61 using namespace ::svt;
62
63 namespace
64 {
65 Sequence< sal_Int8 > lcl_GetSequenceClassID( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
66 sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
67 sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 )
68 {
69 Sequence< sal_Int8 > aResult{ /* [ 0] */ static_cast<sal_Int8>(n1 >> 24),
70 /* [ 1] */ static_cast<sal_Int8>(( n1 << 8 ) >> 24),
71 /* [ 2] */ static_cast<sal_Int8>(( n1 << 16 ) >> 24),
72 /* [ 3] */ static_cast<sal_Int8>(( n1 << 24 ) >> 24),
73 /* [ 4] */ static_cast<sal_Int8>(n2 >> 8),
74 /* [ 5] */ static_cast<sal_Int8>(( n2 << 8 ) >> 8),
75 /* [ 6] */ static_cast<sal_Int8>(n3 >> 8),
76 /* [ 7] */ static_cast<sal_Int8>(( n3 << 8 ) >> 8),
77 /* [ 8] */ static_cast<sal_Int8>(b8),
78 /* [ 9] */ static_cast<sal_Int8>(b9),
79 /* [10] */ static_cast<sal_Int8>(b10),
80 /* [11] */ static_cast<sal_Int8>(b11),
81 /* [12] */ static_cast<sal_Int8>(b12),
82 /* [13] */ static_cast<sal_Int8>(b13),
83 /* [14] */ static_cast<sal_Int8>(b14),
84 /* [15] */ static_cast<sal_Int8>(b15) };
85 return aResult;
86 }
87 }
88
89 // OLinkedDocumentsAccess
91 const Reference< XComponentContext >& _rxContext, const Reference< XNameAccess >& _rxContainer,
92 const Reference< XConnection>& _xConnection, OUString _sDataSourceName )
93 :m_xContext(_rxContext)
94 ,m_xDocumentContainer(_rxContainer)
95 ,m_xConnection(_xConnection)
96 ,m_xDocumentUI( i_rDocumentUI )
97 ,m_pDialogParent(pDialogParent)
98 ,m_sDataSourceName(std::move(_sDataSourceName))
99 {
100 OSL_ENSURE(m_xContext.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid service factory!");
101 assert(m_pDialogParent && "OLinkedDocumentsAccess::OLinkedDocumentsAccess: really need a dialog parent!");
102 }
104 {
105 }
107 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
108 {
110 OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid document container!");
111 Reference< XComponentLoader > xComponentLoader(m_xDocumentContainer,UNO_QUERY);
112 if ( !xComponentLoader.is() )
113 return xRet;
114
116
118 OUString sOpenMode;
119 switch ( _eOpenMode )
120 {
122 sOpenMode = "open";
123 break;
124
126 aArguments.put( "Hidden", true );
127 [[fallthrough]];
128
130 sOpenMode = "openDesign";
131 break;
132
133 default:
134 OSL_FAIL( "OLinkedDocumentsAccess::implOpen: invalid open mode!" );
135 break;
136 }
137 aArguments.put( "OpenMode", sOpenMode );
138
140
142 if ( xHier.is() && xHier->hasByHierarchicalName(_rLinkName) )
143 {
144 _xDefinition.set(xHier->getByHierarchicalName(_rLinkName),UNO_QUERY);
145 }
146
147 aArguments.merge( _rAdditionalArgs, true );
148
149 xRet = xComponentLoader->loadComponentFromURL( _rLinkName, OUString(), 0, aArguments.getPropertyValues() );
150
151 return xRet;
152 }
153 void OLinkedDocumentsAccess::impl_newWithPilot( const char* _pWizardService,
154 const sal_Int32 _nCommandType, const OUString& _rObjectName )
155 {
156 try
157 {
159 aArgs.put( "DataSourceName", m_sDataSourceName );
160
161 if ( m_xConnection.is() )
162 aArgs.put( "ActiveConnection", m_xConnection );
163
164 if ( !_rObjectName.isEmpty() && ( _nCommandType != -1 ) )
165 {
166 aArgs.put( "CommandType", _nCommandType );
167 aArgs.put( "Command", _rObjectName );
168 }
169
170 aArgs.put( "DocumentUI", m_xDocumentUI );
171
173 {
175 xWizard.set( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
176 OUString::createFromAscii( _pWizardService ),
179 ), UNO_QUERY_THROW );
180 }
181
182 xWizard->trigger( "start" );
183 ::comphelper::disposeComponent( xWizard );
184 }
185 catch(const Exception&)
186 {
187 DBG_UNHANDLED_EXCEPTION("dbaccess");
188 }
189 }
190 void OLinkedDocumentsAccess::newFormWithPilot( const sal_Int32 _nCommandType,const OUString& _rObjectName )
191 {
192 impl_newWithPilot( "com.sun.star.wizards.form.CallFormWizard", _nCommandType, _rObjectName );
193 }
194
195 void OLinkedDocumentsAccess::newReportWithPilot( const sal_Int32 _nCommandType, const OUString& _rObjectName )
196 {
197 impl_newWithPilot( "com.sun.star.wizards.report.CallReportWizard", _nCommandType, _rObjectName );
198 }
200 {
201 impl_newWithPilot( "com.sun.star.wizards.table.CallTableWizard", -1, OUString() );
202 }
204 {
205 impl_newWithPilot( "com.sun.star.wizards.query.CallQueryWizard", -1, OUString() );
206 }
208 const ::comphelper::NamedValueCollection& i_rCreationArgs, Reference< XComponent >& o_rDefinition )
209 {
210 OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::newDocument: invalid document container!");
211 // determine the class ID to use for the new document
212 Sequence<sal_Int8> aClassId;
213 if ( !i_rCreationArgs.has( "ClassID" )
214 && !i_rCreationArgs.has( "MediaType" )
215 && !i_rCreationArgs.has( "DocumentServiceName" )
216 )
217 {
218 switch ( i_nActionID )
219 {
220 case ID_FORM_NEW_TEXT:
221 aClassId = lcl_GetSequenceClassID(SO3_SW_CLASSID);
223 break;
224
225 case ID_FORM_NEW_CALC:
226 aClassId = lcl_GetSequenceClassID(SO3_SC_CLASSID);
227 break;
228
230 aClassId = lcl_GetSequenceClassID(SO3_SIMPRESS_CLASSID);
231 break;
232
235 break;
236
237 default:
238 OSL_FAIL( "OLinkedDocumentsAccess::newDocument: please use newFormWithPilot!" );
240
241 }
242 }
243
244 // load the document as template
245 Reference< XComponent > xNewDocument;
246 try
247 { // get the desktop object
248
250 if ( xORB.is() )
251 {
252 ::comphelper::NamedValueCollection aCreationArgs( i_rCreationArgs );
253 if ( aClassId.hasElements() )
254 aCreationArgs.put( "ClassID", aClassId );
256
257 // separate values which are real creation args from args relevant for opening the doc
259 if ( aCreationArgs.has( "Hidden" ) )
260 {
261 aCommandArgs.put( "Hidden", aCreationArgs.get( "Hidden" ) );
262 aCreationArgs.remove( "Hidden" );
263 }
264
265 Reference< XCommandProcessor > xContent( xORB->createInstanceWithArguments(
267 aCreationArgs.getWrappedPropertyValues()
268 ),
269 UNO_QUERY_THROW
270 );
271 o_rDefinition.set( xContent, UNO_QUERY );
272
273 // put the OpenMode into the OpenArgs
274 OpenCommandArgument aOpenModeArg;
275 aOpenModeArg.Mode = OpenMode::DOCUMENT;
276 aCommandArgs.put( "OpenMode", aOpenModeArg );
277
279 aCommand.Name = "openDesign";
280 aCommand.Argument <<= aCommandArgs.getPropertyValues();
282 xNewDocument.set( xContent->execute( aCommand, xContent->createCommandIdentifier(), nullptr ), UNO_QUERY );
283 }
284 }
285 catch(const Exception&)
286 {
287 DBG_UNHANDLED_EXCEPTION("dbaccess");
288 }
289
290 return xNewDocument;
291 }
292
294 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
295 {
298 try
299 {
300 xRet = impl_open( _rLinkName, _xDefinition, _eOpenMode, _rAdditionalArgs );
301 if ( !xRet.is() )
302 {
303 OUString sMessage = DBA_RES(STR_COULDNOTOPEN_LINKEDDOC);
304 sMessage = sMessage.replaceFirst("$file$",_rLinkName);
305
306 css::sdbc::SQLException aSQLException;
307 aSQLException.Message = sMessage;
308 aInfo = dbtools::SQLExceptionInfo(aSQLException);
309 }
310 }
311 catch(const css::io::WrongFormatException &e)
312 {
313 css::sdbc::SQLException aSQLException;
314 aSQLException.Message = e.Message;
315 aSQLException.Context = e.Context;
316 aInfo = dbtools::SQLExceptionInfo(aSQLException);
317
318 // more like a hack, insert an empty message
319 OUString sText( DBA_RES( RID_STR_EXTENSION_NOT_PRESENT ) );
320 sText = sText.replaceFirst("$file$",_rLinkName);
321 aInfo.prepend(sText);
322
323 OUString sMessage = DBA_RES(STR_COULDNOTOPEN_LINKEDDOC);
324 sMessage = sMessage.replaceFirst("$file$",_rLinkName);
325 aInfo.prepend(sMessage);
326 }
327 catch(const Exception& e)
328 {
329 Any aAny = ::cppu::getCaughtException();
330 css::sdbc::SQLException a;
331 if ( !(aAny >>= a) || (a.ErrorCode != dbtools::ParameterInteractionCancelled) )
332 {
333 css::sdbc::SQLException aSQLException;
334 aSQLException.Message = e.Message;
335 aSQLException.Context = e.Context;
336 aInfo = dbtools::SQLExceptionInfo(aSQLException);
337
338 // more like a hack, insert an empty message
339 aInfo.prepend(" \n");
340
341 OUString sMessage = DBA_RES(STR_COULDNOTOPEN_LINKEDDOC);
342 sMessage = sMessage.replaceFirst("$file$",_rLinkName);
343 aInfo.prepend(sMessage);
344 }
345 }
346 if (aInfo.isValid())
347 {
349 }
350 return xRet;
351 }
352
353} // namespace dbaui
354
355/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define ID_FORM_NEW_IMPRESS
Definition: browserids.hxx:67
#define ID_FORM_NEW_CALC
Definition: browserids.hxx:66
#define ID_FORM_NEW_TEXT
Definition: browserids.hxx:65
#define ID_REPORT_NEW_TEXT
Definition: browserids.hxx:64
static css::uno::Sequence< sal_Int8 > GetSequenceClassID(sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3, sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11, sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15)
css::uno::Sequence< css::uno::Any > getWrappedPropertyValues() const
bool has(const OUString &_rValueName) const
const css::uno::Any & get(const OUString &_rValueName) const
bool remove(const OUString &_rValueName)
bool put(const OUString &_rValueName, const VALUE_TYPE &_rValue)
css::uno::Sequence< css::beans::PropertyValue > getPropertyValues() const
css::uno::Reference< css::uno::XComponentContext > m_xContext
css::uno::Reference< css::lang::XComponent > impl_open(const OUString &_rLinkName, css::uno::Reference< css::lang::XComponent > &_xDefinition, ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection &_rAdditionalArgs)
void newFormWithPilot(const sal_Int32 _nCommandType, const OUString &_rObjectName)
css::uno::Reference< css::container::XNameAccess > m_xDocumentContainer
css::uno::Reference< css::sdbc::XConnection > m_xConnection
css::uno::Reference< css::lang::XComponent > newDocument(sal_Int32 i_nActionID, const ::comphelper::NamedValueCollection &i_rCreationArgs, css::uno::Reference< css::lang::XComponent > &o_rDefinition)
void newReportWithPilot(const sal_Int32 _nCommandType, const OUString &_rObjectName)
css::uno::Reference< css::lang::XComponent > open(const OUString &_rLinkName, css::uno::Reference< css::lang::XComponent > &_xDefinition, ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection &_rAdditionalArgs)
void impl_newWithPilot(const char *_pWizardService, const sal_Int32 _nCommandType, const OUString &_rObjectName)
OLinkedDocumentsAccess(weld::Window *pDialogParent, const css::uno::Reference< css::sdb::application::XDatabaseDocumentUI > &i_rDocumentUI, const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const css::uno::Reference< css::container::XNameAccess > &_rxContainer, const css::uno::Reference< css::sdbc::XConnection > &_xConnection, OUString _sDataSourceName)
css::uno::Reference< css::sdb::application::XDatabaseDocumentUI > m_xDocumentUI
void prepend(const OUString &_rErrorMessage)
#define SO3_SIMPRESS_CLASSID
#define SO3_SC_CLASSID
#define SO3_SW_CLASSID
#define SO3_RPT_CLASSID_90
virtual css::uno::Reference< css::awt::XWindow > GetXWindow()=0
Reference< XComponentContext > m_xContext
#define DBA_RES(id)
#define DBG_UNHANDLED_EXCEPTION(...)
Sequence< PropertyValue > aArguments
Definition: intercept.cxx:88
uno_Any a
int n1
@ Exception
void showError(const SQLExceptionInfo &_rInfo, const Reference< XWindow > &_xParent, const Reference< XComponentContext > &_rxContext)
ParameterInteractionCancelled
Reference< XConnection > m_xConnection
Definition: objectnames.cxx:79
OUString sMessage
Definition: sqlmessage.cxx:159
constexpr OUStringLiteral SERVICE_SDB_DOCUMENTDEFINITION
Definition: strings.hxx:196
constexpr OUStringLiteral PROPERTY_ACTIVE_CONNECTION(u"ActiveConnection")
OUString aCommand
unsigned char sal_uInt8
signed char sal_Int8