nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / ui / misc / linkeddocuments.cxx
blob13effe4ccd93a22b00fbe7d60303cdc523131252
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <core_resource.hxx>
21 #include <linkeddocuments.hxx>
22 #include <osl/diagnose.h>
23 #include <tools/diagnose_ex.h>
24 #include <unotools/confignode.hxx>
25 #include <comphelper/classids.hxx>
26 #include <comphelper/namedvaluecollection.hxx>
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>
40 #include <comphelper/mimeconfighelper.hxx>
41 #include <vcl/weld.hxx>
43 #include <cppuhelper/exc_hlp.hxx>
44 #include <connectivity/dbtools.hxx>
45 #include <com/sun/star/io/WrongFormatException.hpp>
47 namespace dbaui
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::container;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::frame;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::util;
56 using namespace ::com::sun::star::ucb;
57 using namespace ::com::sun::star::sdbc;
58 using namespace ::com::sun::star::sdb::application;
59 using namespace ::com::sun::star::task;
60 using namespace ::svt;
62 namespace
64 Sequence< sal_Int8 > lcl_GetSequenceClassID( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
65 sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
66 sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 )
68 Sequence< sal_Int8 > aResult( 16 );
69 aResult[0] = static_cast<sal_Int8>(n1 >> 24);
70 aResult[1] = static_cast<sal_Int8>(( n1 << 8 ) >> 24);
71 aResult[2] = static_cast<sal_Int8>(( n1 << 16 ) >> 24);
72 aResult[3] = static_cast<sal_Int8>(( n1 << 24 ) >> 24);
73 aResult[4] = static_cast<sal_Int8>(n2 >> 8);
74 aResult[5] = static_cast<sal_Int8>(( n2 << 8 ) >> 8);
75 aResult[6] = static_cast<sal_Int8>(n3 >> 8);
76 aResult[7] = static_cast<sal_Int8>(( n3 << 8 ) >> 8);
77 aResult[8] = b8;
78 aResult[9] = b9;
79 aResult[10] = b10;
80 aResult[11] = b11;
81 aResult[12] = b12;
82 aResult[13] = b13;
83 aResult[14] = b14;
84 aResult[15] = b15;
86 return aResult;
90 // OLinkedDocumentsAccess
91 OLinkedDocumentsAccess::OLinkedDocumentsAccess( weld::Window* pDialogParent, const Reference< XDatabaseDocumentUI >& i_rDocumentUI,
92 const Reference< XComponentContext >& _rxContext, const Reference< XNameAccess >& _rxContainer,
93 const Reference< XConnection>& _xConnection, const OUString& _sDataSourceName )
94 :m_xContext(_rxContext)
95 ,m_xDocumentContainer(_rxContainer)
96 ,m_xConnection(_xConnection)
97 ,m_xDocumentUI( i_rDocumentUI )
98 ,m_pDialogParent(pDialogParent)
99 ,m_sDataSourceName(_sDataSourceName)
101 OSL_ENSURE(m_xContext.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid service factory!");
102 assert(m_pDialogParent && "OLinkedDocumentsAccess::OLinkedDocumentsAccess: really need a dialog parent!");
104 OLinkedDocumentsAccess::~OLinkedDocumentsAccess()
107 Reference< XComponent> OLinkedDocumentsAccess::impl_open( const OUString& _rLinkName, Reference< XComponent >& _xDefinition,
108 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
110 Reference< XComponent> xRet;
111 OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid document container!");
112 Reference< XComponentLoader > xComponentLoader(m_xDocumentContainer,UNO_QUERY);
113 if ( !xComponentLoader.is() )
114 return xRet;
116 weld::WaitObject aWaitCursor(m_pDialogParent);
118 ::comphelper::NamedValueCollection aArguments;
119 OUString sOpenMode;
120 switch ( _eOpenMode )
122 case E_OPEN_NORMAL:
123 sOpenMode = "open";
124 break;
126 case E_OPEN_FOR_MAIL:
127 aArguments.put( "Hidden", true );
128 [[fallthrough]];
130 case E_OPEN_DESIGN:
131 sOpenMode = "openDesign";
132 break;
134 default:
135 OSL_FAIL( "OLinkedDocumentsAccess::implOpen: invalid open mode!" );
136 break;
138 aArguments.put( "OpenMode", sOpenMode );
140 aArguments.put( OUString(PROPERTY_ACTIVE_CONNECTION), m_xConnection );
142 Reference<XHierarchicalNameContainer> xHier(m_xDocumentContainer,UNO_QUERY);
143 if ( xHier.is() && xHier->hasByHierarchicalName(_rLinkName) )
145 _xDefinition.set(xHier->getByHierarchicalName(_rLinkName),UNO_QUERY);
148 aArguments.merge( _rAdditionalArgs, true );
150 xRet = xComponentLoader->loadComponentFromURL( _rLinkName, OUString(), 0, aArguments.getPropertyValues() );
152 return xRet;
154 void OLinkedDocumentsAccess::impl_newWithPilot( const char* _pWizardService,
155 const sal_Int32 _nCommandType, const OUString& _rObjectName )
159 ::comphelper::NamedValueCollection aArgs;
160 aArgs.put( "DataSourceName", m_sDataSourceName );
162 if ( m_xConnection.is() )
163 aArgs.put( "ActiveConnection", m_xConnection );
165 if ( !_rObjectName.isEmpty() && ( _nCommandType != -1 ) )
167 aArgs.put( "CommandType", _nCommandType );
168 aArgs.put( "Command", _rObjectName );
171 aArgs.put( "DocumentUI", m_xDocumentUI );
173 Reference< XJobExecutor > xWizard;
175 weld::WaitObject aWaitCursor(m_pDialogParent);
176 xWizard.set( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
177 OUString::createFromAscii( _pWizardService ),
178 aArgs.getWrappedPropertyValues(),
179 m_xContext
180 ), UNO_QUERY_THROW );
183 xWizard->trigger( "start" );
184 ::comphelper::disposeComponent( xWizard );
186 catch(const Exception&)
188 DBG_UNHANDLED_EXCEPTION("dbaccess");
191 void OLinkedDocumentsAccess::newFormWithPilot( const sal_Int32 _nCommandType,const OUString& _rObjectName )
193 impl_newWithPilot( "com.sun.star.wizards.form.CallFormWizard", _nCommandType, _rObjectName );
196 void OLinkedDocumentsAccess::newReportWithPilot( const sal_Int32 _nCommandType, const OUString& _rObjectName )
198 impl_newWithPilot( "com.sun.star.wizards.report.CallReportWizard", _nCommandType, _rObjectName );
200 void OLinkedDocumentsAccess::newTableWithPilot()
202 impl_newWithPilot( "com.sun.star.wizards.table.CallTableWizard", -1, OUString() );
204 void OLinkedDocumentsAccess::newQueryWithPilot()
206 impl_newWithPilot( "com.sun.star.wizards.query.CallQueryWizard", -1, OUString() );
208 Reference< XComponent > OLinkedDocumentsAccess::newDocument( sal_Int32 i_nActionID,
209 const ::comphelper::NamedValueCollection& i_rCreationArgs, Reference< XComponent >& o_rDefinition )
211 OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::newDocument: invalid document container!");
212 // determine the class ID to use for the new document
213 Sequence<sal_Int8> aClassId;
214 if ( !i_rCreationArgs.has( "ClassID" )
215 && !i_rCreationArgs.has( "MediaType" )
216 && !i_rCreationArgs.has( "DocumentServiceName" )
219 switch ( i_nActionID )
221 case ID_FORM_NEW_TEXT:
222 aClassId = lcl_GetSequenceClassID(SO3_SW_CLASSID);
223 OSL_ENSURE(aClassId == comphelper::MimeConfigurationHelper::GetSequenceClassID(SO3_SW_CLASSID),"Not equal");
224 break;
226 case ID_FORM_NEW_CALC:
227 aClassId = lcl_GetSequenceClassID(SO3_SC_CLASSID);
228 break;
230 case ID_FORM_NEW_IMPRESS:
231 aClassId = lcl_GetSequenceClassID(SO3_SIMPRESS_CLASSID);
232 break;
234 case ID_REPORT_NEW_TEXT:
235 aClassId = comphelper::MimeConfigurationHelper::GetSequenceClassID(SO3_RPT_CLASSID_90);
236 break;
238 default:
239 OSL_FAIL( "OLinkedDocumentsAccess::newDocument: please use newFormWithPilot!" );
240 return Reference< XComponent >();
245 // load the document as template
246 Reference< XComponent > xNewDocument;
248 { // get the desktop object
250 Reference<XMultiServiceFactory> xORB(m_xDocumentContainer,UNO_QUERY);
251 if ( xORB.is() )
253 ::comphelper::NamedValueCollection aCreationArgs( i_rCreationArgs );
254 if ( aClassId.hasElements() )
255 aCreationArgs.put( "ClassID", aClassId );
256 aCreationArgs.put( OUString(PROPERTY_ACTIVE_CONNECTION), m_xConnection );
258 // separate values which are real creation args from args relevant for opening the doc
259 ::comphelper::NamedValueCollection aCommandArgs;
260 if ( aCreationArgs.has( "Hidden" ) )
262 aCommandArgs.put( "Hidden", aCreationArgs.get( "Hidden" ) );
263 aCreationArgs.remove( "Hidden" );
266 Reference< XCommandProcessor > xContent( xORB->createInstanceWithArguments(
267 SERVICE_SDB_DOCUMENTDEFINITION,
268 aCreationArgs.getWrappedPropertyValues()
270 UNO_QUERY_THROW
272 o_rDefinition.set( xContent, UNO_QUERY );
274 // put the OpenMode into the OpenArgs
275 OpenCommandArgument aOpenModeArg;
276 aOpenModeArg.Mode = OpenMode::DOCUMENT;
277 aCommandArgs.put( "OpenMode", aOpenModeArg );
279 Command aCommand;
280 aCommand.Name = "openDesign";
281 aCommand.Argument <<= aCommandArgs.getPropertyValues();
282 weld::WaitObject aWaitCursor(m_pDialogParent);
283 xNewDocument.set( xContent->execute( aCommand, xContent->createCommandIdentifier(), nullptr ), UNO_QUERY );
286 catch(const Exception&)
288 DBG_UNHANDLED_EXCEPTION("dbaccess");
291 return xNewDocument;
294 Reference< XComponent > OLinkedDocumentsAccess::open( const OUString& _rLinkName, Reference< XComponent >& _xDefinition,
295 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
297 dbtools::SQLExceptionInfo aInfo;
298 Reference< XComponent > xRet;
301 xRet = impl_open( _rLinkName, _xDefinition, _eOpenMode, _rAdditionalArgs );
302 if ( !xRet.is() )
304 OUString sMessage = DBA_RES(STR_COULDNOTOPEN_LINKEDDOC);
305 sMessage = sMessage.replaceFirst("$file$",_rLinkName);
307 css::sdbc::SQLException aSQLException;
308 aSQLException.Message = sMessage;
309 aInfo = dbtools::SQLExceptionInfo(aSQLException);
312 catch(const css::io::WrongFormatException &e)
314 css::sdbc::SQLException aSQLException;
315 aSQLException.Message = e.Message;
316 aSQLException.Context = e.Context;
317 aInfo = dbtools::SQLExceptionInfo(aSQLException);
319 // more like a hack, insert an empty message
320 OUString sText( DBA_RES( RID_STR_EXTENSION_NOT_PRESENT ) );
321 sText = sText.replaceFirst("$file$",_rLinkName);
322 aInfo.prepend(sText);
324 OUString sMessage = DBA_RES(STR_COULDNOTOPEN_LINKEDDOC);
325 sMessage = sMessage.replaceFirst("$file$",_rLinkName);
326 aInfo.prepend(sMessage);
328 catch(const Exception& e)
330 Any aAny = ::cppu::getCaughtException();
331 css::sdbc::SQLException a;
332 if ( !(aAny >>= a) || (a.ErrorCode != dbtools::ParameterInteractionCancelled) )
334 css::sdbc::SQLException aSQLException;
335 aSQLException.Message = e.Message;
336 aSQLException.Context = e.Context;
337 aInfo = dbtools::SQLExceptionInfo(aSQLException);
339 // more like a hack, insert an empty message
340 aInfo.prepend(" \n");
342 OUString sMessage = DBA_RES(STR_COULDNOTOPEN_LINKEDDOC);
343 sMessage = sMessage.replaceFirst("$file$",_rLinkName);
344 aInfo.prepend(sMessage);
347 if (aInfo.isValid())
349 showError(aInfo, m_pDialogParent->GetXWindow(), m_xContext);
351 return xRet;
354 } // namespace dbaui
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */