Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / linkeddocuments.cxx
blob2904b81b7eca295d01de7b5edfa4788536199c50
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 "linkeddocuments.hxx"
21 #include <osl/diagnose.h>
22 #include <tools/diagnose_ex.h>
23 #include <unotools/confignode.hxx>
24 #include "dbustrings.hrc"
25 #include <comphelper/classids.hxx>
26 #include <comphelper/namedvaluecollection.hxx>
27 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
28 #include <com/sun/star/frame/XDispatchProvider.hpp>
29 #include <com/sun/star/frame/XComponentLoader.hpp>
30 #include <com/sun/star/util/URL.hpp>
31 #include <com/sun/star/frame/FrameSearchFlag.hpp>
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <com/sun/star/ucb/XCommandProcessor.hpp>
34 #include <com/sun/star/ucb/OpenCommandArgument.hpp>
35 #include <com/sun/star/ucb/OpenMode.hpp>
36 #include <com/sun/star/task/XJobExecutor.hpp>
37 #include <comphelper/extract.hxx>
38 #include <comphelper/types.hxx>
39 #include <vcl/msgbox.hxx>
40 #include <ucbhelper/content.hxx>
41 #include "dbu_misc.hrc"
42 #include <svl/filenotation.hxx>
43 #include "browserids.hxx"
44 #include <sfx2/new.hxx>
45 #include "moduledbu.hxx"
46 #include <sfx2/app.hxx>
47 #include <basic/sbx.hxx>
48 #include <basic/sbuno.hxx>
49 #include <svtools/ehdl.hxx>
50 #include <svx/dataaccessdescriptor.hxx>
51 #include <com/sun/star/container/XHierarchicalNameContainer.hpp>
52 #include <vcl/waitobj.hxx>
53 #include <comphelper/mimeconfighelper.hxx>
55 #include <cppuhelper/exc_hlp.hxx>
56 #include <connectivity/dbtools.hxx>
57 #include <toolkit/helper/vclunohelper.hxx>
58 #include <com/sun/star/io/WrongFormatException.hpp>
59 #include "com/sun/star/sdb/RowSetVetoException.hpp"
61 namespace dbaui
64 using namespace ::com::sun::star::uno;
65 using namespace ::com::sun::star::container;
66 using namespace ::com::sun::star::lang;
67 using namespace ::com::sun::star::frame;
68 using namespace ::com::sun::star::beans;
69 using namespace ::com::sun::star::util;
70 using namespace ::com::sun::star::ucb;
71 using namespace ::com::sun::star::sdbc;
72 using namespace ::com::sun::star::sdb::application;
73 using namespace ::com::sun::star::task;
74 using namespace ::svt;
76 namespace
78 Sequence< sal_Int8 > lcl_GetSequenceClassID( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
79 sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
80 sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 )
82 Sequence< sal_Int8 > aResult( 16 );
83 aResult[0] = static_cast<sal_Int8>(n1 >> 24);
84 aResult[1] = static_cast<sal_Int8>(( n1 << 8 ) >> 24);
85 aResult[2] = static_cast<sal_Int8>(( n1 << 16 ) >> 24);
86 aResult[3] = static_cast<sal_Int8>(( n1 << 24 ) >> 24);
87 aResult[4] = static_cast<sal_Int8>(n2 >> 8);
88 aResult[5] = static_cast<sal_Int8>(( n2 << 8 ) >> 8);
89 aResult[6] = static_cast<sal_Int8>(n3 >> 8);
90 aResult[7] = static_cast<sal_Int8>(( n3 << 8 ) >> 8);
91 aResult[8] = b8;
92 aResult[9] = b9;
93 aResult[10] = b10;
94 aResult[11] = b11;
95 aResult[12] = b12;
96 aResult[13] = b13;
97 aResult[14] = b14;
98 aResult[15] = b15;
100 return aResult;
104 // OLinkedDocumentsAccess
105 OLinkedDocumentsAccess::OLinkedDocumentsAccess( vcl::Window* _pDialogParent, const Reference< XDatabaseDocumentUI >& i_rDocumentUI,
106 const Reference< XComponentContext >& _rxContext, const Reference< XNameAccess >& _rxContainer,
107 const Reference< XConnection>& _xConnection, const OUString& _sDataSourceName )
108 :m_xContext(_rxContext)
109 ,m_xDocumentContainer(_rxContainer)
110 ,m_xConnection(_xConnection)
111 ,m_xDocumentUI( i_rDocumentUI )
112 ,m_pDialogParent(_pDialogParent)
113 ,m_sDataSourceName(_sDataSourceName)
115 OSL_ENSURE(m_xContext.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid service factory!");
116 OSL_ENSURE(m_pDialogParent, "OLinkedDocumentsAccess::OLinkedDocumentsAccess: really need a dialog parent!");
118 OLinkedDocumentsAccess::~OLinkedDocumentsAccess()
121 Reference< XComponent> OLinkedDocumentsAccess::impl_open( const OUString& _rLinkName, Reference< XComponent >& _xDefinition,
122 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
124 Reference< XComponent> xRet;
125 OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid document container!");
126 Reference< XComponentLoader > xComponentLoader(m_xDocumentContainer,UNO_QUERY);
127 if ( !xComponentLoader.is() )
128 return xRet;
130 WaitObject aWaitCursor( m_pDialogParent );
132 ::comphelper::NamedValueCollection aArguments;
133 OUString sOpenMode;
134 switch ( _eOpenMode )
136 case E_OPEN_NORMAL:
137 sOpenMode = "open";
138 break;
140 case E_OPEN_FOR_MAIL:
141 aArguments.put( "Hidden", true );
142 SAL_FALLTHROUGH;
144 case E_OPEN_DESIGN:
145 sOpenMode = "openDesign";
146 break;
148 default:
149 OSL_FAIL( "OLinkedDocumentsAccess::implOpen: invalid open mode!" );
150 break;
152 aArguments.put( "OpenMode", sOpenMode );
154 aArguments.put( OUString(PROPERTY_ACTIVE_CONNECTION), m_xConnection );
157 Reference<XHierarchicalNameContainer> xHier(m_xDocumentContainer,UNO_QUERY);
158 if ( xHier.is() && xHier->hasByHierarchicalName(_rLinkName) )
160 _xDefinition.set(xHier->getByHierarchicalName(_rLinkName),UNO_QUERY);
163 aArguments.merge( _rAdditionalArgs, true );
165 xRet = xComponentLoader->loadComponentFromURL( _rLinkName, OUString(), 0, aArguments.getPropertyValues() );
167 catch(const Exception&)
169 throw;
172 return xRet;
174 void OLinkedDocumentsAccess::impl_newWithPilot( const char* _pWizardService,
175 const sal_Int32 _nCommandType, const OUString& _rObjectName )
179 ::comphelper::NamedValueCollection aArgs;
180 aArgs.put( "DataSourceName", m_sDataSourceName );
182 if ( m_xConnection.is() )
183 aArgs.put( "ActiveConnection", m_xConnection );
185 if ( !_rObjectName.isEmpty() && ( _nCommandType != -1 ) )
187 aArgs.put( "CommandType", _nCommandType );
188 aArgs.put( "Command", _rObjectName );
191 aArgs.put( "DocumentUI", m_xDocumentUI );
193 Reference< XJobExecutor > xWizard;
195 WaitObject aWaitCursor( m_pDialogParent );
196 xWizard.set( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
197 OUString::createFromAscii( _pWizardService ),
198 aArgs.getWrappedPropertyValues(),
199 m_xContext
200 ), UNO_QUERY_THROW );
203 xWizard->trigger( "start" );
204 ::comphelper::disposeComponent( xWizard );
206 catch(const Exception&)
208 DBG_UNHANDLED_EXCEPTION();
211 void OLinkedDocumentsAccess::newFormWithPilot( const sal_Int32 _nCommandType,const OUString& _rObjectName )
213 impl_newWithPilot( "com.sun.star.wizards.form.CallFormWizard", _nCommandType, _rObjectName );
216 void OLinkedDocumentsAccess::newReportWithPilot( const sal_Int32 _nCommandType, const OUString& _rObjectName )
218 impl_newWithPilot( "com.sun.star.wizards.report.CallReportWizard", _nCommandType, _rObjectName );
220 void OLinkedDocumentsAccess::newTableWithPilot()
222 impl_newWithPilot( "com.sun.star.wizards.table.CallTableWizard", -1, OUString() );
224 void OLinkedDocumentsAccess::newQueryWithPilot()
226 impl_newWithPilot( "com.sun.star.wizards.query.CallQueryWizard", -1, OUString() );
228 Reference< XComponent > OLinkedDocumentsAccess::newDocument( sal_Int32 i_nActionID,
229 const ::comphelper::NamedValueCollection& i_rCreationArgs, Reference< XComponent >& o_rDefinition )
231 OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::newDocument: invalid document container!");
232 // determine the class ID to use for the new document
233 Sequence<sal_Int8> aClassId;
234 if ( !i_rCreationArgs.has( "ClassID" )
235 && !i_rCreationArgs.has( "MediaType" )
236 && !i_rCreationArgs.has( "DocumentServiceName" )
239 switch ( i_nActionID )
241 case ID_FORM_NEW_TEXT:
242 aClassId = lcl_GetSequenceClassID(SO3_SW_CLASSID);
243 OSL_ENSURE(aClassId == comphelper::MimeConfigurationHelper::GetSequenceClassID(SO3_SW_CLASSID),"Not equal");
244 break;
246 case ID_FORM_NEW_CALC:
247 aClassId = lcl_GetSequenceClassID(SO3_SC_CLASSID);
248 break;
250 case ID_FORM_NEW_IMPRESS:
251 aClassId = lcl_GetSequenceClassID(SO3_SIMPRESS_CLASSID);
252 break;
254 case ID_REPORT_NEW_TEXT:
255 aClassId = comphelper::MimeConfigurationHelper::GetSequenceClassID(SO3_RPT_CLASSID_90);
256 break;
258 default:
259 OSL_FAIL( "OLinkedDocumentsAccess::newDocument: please use newFormWithPilot!" );
260 return Reference< XComponent >();
265 // load the document as template
266 Reference< XComponent > xNewDocument;
268 { // get the desktop object
270 Reference<XMultiServiceFactory> xORB(m_xDocumentContainer,UNO_QUERY);
271 if ( xORB.is() )
273 ::comphelper::NamedValueCollection aCreationArgs( i_rCreationArgs );
274 if ( aClassId.getLength() )
275 aCreationArgs.put( "ClassID", aClassId );
276 aCreationArgs.put( OUString(PROPERTY_ACTIVE_CONNECTION), m_xConnection );
278 // separate values which are real creation args from args relevant for opening the doc
279 ::comphelper::NamedValueCollection aCommandArgs;
280 if ( aCreationArgs.has( "Hidden" ) )
282 aCommandArgs.put( "Hidden", aCreationArgs.get( "Hidden" ) );
283 aCreationArgs.remove( "Hidden" );
286 Reference< XCommandProcessor > xContent( xORB->createInstanceWithArguments(
287 SERVICE_SDB_DOCUMENTDEFINITION,
288 aCreationArgs.getWrappedPropertyValues()
290 UNO_QUERY_THROW
292 o_rDefinition.set( xContent, UNO_QUERY );
294 // put the OpenMode into the OpenArgs
295 OpenCommandArgument aOpenModeArg;
296 aOpenModeArg.Mode = OpenMode::DOCUMENT;
297 aCommandArgs.put( "OpenMode", aOpenModeArg );
299 Command aCommand;
300 aCommand.Name = "openDesign";
301 aCommand.Argument <<= aCommandArgs.getPropertyValues();
302 WaitObject aWaitCursor( m_pDialogParent );
303 xNewDocument.set( xContent->execute( aCommand, xContent->createCommandIdentifier(), nullptr ), UNO_QUERY );
306 catch(const Exception&)
308 DBG_UNHANDLED_EXCEPTION();
311 return xNewDocument;
314 Reference< XComponent > OLinkedDocumentsAccess::open( const OUString& _rLinkName, Reference< XComponent >& _xDefinition,
315 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
317 dbtools::SQLExceptionInfo aInfo;
318 Reference< XComponent > xRet;
321 xRet = impl_open( _rLinkName, _xDefinition, _eOpenMode, _rAdditionalArgs );
322 if ( !xRet.is() )
324 OUString sMessage = ModuleRes(STR_COULDNOTOPEN_LINKEDDOC);
325 sMessage = sMessage.replaceFirst("$file$",_rLinkName);
327 css::sdbc::SQLException aSQLException;
328 aSQLException.Message = sMessage;
329 aInfo = dbtools::SQLExceptionInfo(aSQLException);
331 return xRet;
333 catch(const css::io::WrongFormatException &e)
335 css::sdbc::SQLException aSQLException;
336 aSQLException.Message = e.Message;
337 aSQLException.Context = e.Context;
338 aInfo = dbtools::SQLExceptionInfo(aSQLException);
340 // more like a hack, insert an empty message
341 OUString sText( ModuleRes( RID_STR_EXTENSION_NOT_PRESENT ) );
342 sText = sText.replaceFirst("$file$",_rLinkName);
343 aInfo.prepend(sText);
345 OUString sMessage = ModuleRes(STR_COULDNOTOPEN_LINKEDDOC);
346 sMessage = sMessage.replaceFirst("$file$",_rLinkName);
347 aInfo.prepend(sMessage);
349 catch(const Exception& e)
351 Any aAny = ::cppu::getCaughtException();
352 css::sdbc::SQLException a;
353 if ( !(aAny >>= a) || (a.ErrorCode != dbtools::ParameterInteractionCancelled) )
355 css::sdbc::SQLException aSQLException;
356 aSQLException.Message = e.Message;
357 aSQLException.Context = e.Context;
358 aInfo = dbtools::SQLExceptionInfo(aSQLException);
360 // more like a hack, insert an empty message
361 aInfo.prepend(" \n");
363 OUString sMessage = ModuleRes(STR_COULDNOTOPEN_LINKEDDOC);
364 sMessage = sMessage.replaceFirst("$file$",_rLinkName);
365 aInfo.prepend(sMessage);
368 if (aInfo.isValid())
370 showError(aInfo, VCLUnoHelper::GetInterface(m_pDialogParent), m_xContext );
372 return xRet;
375 } // namespace dbaui
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */