Update ooo320-m1
[ooovba.git] / dbaccess / source / ui / misc / linkeddocuments.cxx
blob5906492a8433215d08eaf4756a77ced03b12a3b8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: linkeddocuments.cxx,v $
10 * $Revision: 1.31.24.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef _DBAUI_LINKEDDOCUMENTS_HXX_
35 #include "linkeddocuments.hxx"
36 #endif
37 #ifndef _OSL_DIAGNOSE_H_
38 #include <osl/diagnose.h>
39 #endif
40 #include <tools/diagnose_ex.h>
41 #include <unotools/confignode.hxx>
42 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
43 #include "dbustrings.hrc"
44 #endif
45 #include <comphelper/classids.hxx>
46 #ifndef COMPHELPER_NAMEDVALUECOLLECTION_HXX
47 #include <comphelper/namedvaluecollection.hxx>
48 #endif
49 #ifndef _COM_SUN_STAR_LANG_XSINGLESERVICEFACTORY_HPP_
50 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
51 #endif
52 #ifndef _COM_SUN_STAR_FRAME_XDISPATCHPROVIDER_HPP_
53 #include <com/sun/star/frame/XDispatchProvider.hpp>
54 #endif
55 #ifndef _COM_SUN_STAR_FRAME_XCOMPONENTLOADER_HPP_
56 #include <com/sun/star/frame/XComponentLoader.hpp>
57 #endif
58 #ifndef _COM_SUN_STAR_UTIL_URL_HPP_
59 #include <com/sun/star/util/URL.hpp>
60 #endif
61 #ifndef _COM_SUN_STAR_FRAME_FRAMESEARCHFLAG_HPP_
62 #include <com/sun/star/frame/FrameSearchFlag.hpp>
63 #endif
64 #ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_
65 #include <com/sun/star/container/XNameContainer.hpp>
66 #endif
67 #ifndef _COM_SUN_STAR_UCB_XCOMMANDPROCESSOR_HPP_
68 #include <com/sun/star/ucb/XCommandProcessor.hpp>
69 #endif
70 #ifndef _COM_SUN_STAR_UCB_OPENCOMMANDARGUMENT2_HPP_
71 #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
72 #endif
73 #ifndef _COM_SUN_STAR_UCB_OPENMODE_HPP_
74 #include <com/sun/star/ucb/OpenMode.hpp>
75 #endif
76 #ifndef _COM_SUN_STAR_TASK_XJOBEXECUTOR_HPP_
77 #include <com/sun/star/task/XJobExecutor.hpp>
78 #endif
79 #ifndef _COMPHELPER_EXTRACT_HXX_
80 #include <cppuhelper/extract.hxx>
81 #endif
82 #ifndef _COMPHELPER_TYPES_HXX_
83 #include <comphelper/types.hxx>
84 #endif
85 #ifndef _SV_MSGBOX_HXX
86 #include <vcl/msgbox.hxx>
87 #endif
88 #ifndef _UCBHELPER_CONTENT_HXX
89 #include <ucbhelper/content.hxx>
90 #endif
91 #ifndef _DBU_MISC_HRC_
92 #include "dbu_misc.hrc"
93 #endif
94 #ifndef SVTOOLS_FILENOTATION_HXX_
95 #include <svtools/filenotation.hxx>
96 #endif
97 #ifndef DBACCESS_UI_BROWSER_ID_HXX
98 #include "browserids.hxx"
99 #endif
100 #ifndef _SFXNEW_HXX
101 #include <sfx2/new.hxx>
102 #endif
103 #ifndef _SVTOOLS_TEMPLDLG_HXX
104 #include <svtools/templdlg.hxx>
105 #endif
106 #ifndef _DBAUI_MODULE_DBU_HXX_
107 #include "moduledbu.hxx"
108 #endif
109 // -----------------
110 // for calling basic
111 #ifndef _SFXAPP_HXX
112 #include <sfx2/app.hxx>
113 #endif
114 #ifndef _SBXCLASS_HXX
115 #include <basic/sbx.hxx>
116 #endif
117 #ifndef _SB_SBUNO_HXX
118 #include <basic/sbuno.hxx>
119 #endif
120 #ifndef _SFX_MACROCONF_HXX
121 #include <sfx2/macrconf.hxx>
122 #endif
123 #ifndef _EHDL_HXX
124 #include <svtools/ehdl.hxx>
125 #endif
126 #ifndef _SVX_DATACCESSDESCRIPTOR_HXX_
127 #include <svx/dataaccessdescriptor.hxx>
128 #endif
129 #ifndef _COM_SUN_STAR_CONTAINER_XHIERARCHICALNAMECONTAINER_HPP_
130 #include <com/sun/star/container/XHierarchicalNameContainer.hpp>
131 #endif
132 #ifndef _SV_WAITOBJ_HXX
133 #include <vcl/waitobj.hxx>
134 #endif
135 #ifndef _COMPHELPER_MIMECONFIGHELPER_HXX_
136 #include <comphelper/mimeconfighelper.hxx>
137 #endif
139 #include <cppuhelper/exc_hlp.hxx>
140 #include <connectivity/dbtools.hxx>
141 #include <toolkit/helper/vclunohelper.hxx>
142 #include <com/sun/star/io/WrongFormatException.hpp>
143 #include "com/sun/star/sdb/RowSetVetoException.hpp"
145 //......................................................................
146 namespace dbaui
148 //......................................................................
150 using namespace ::com::sun::star::uno;
151 using namespace ::com::sun::star::container;
152 using namespace ::com::sun::star::lang;
153 using namespace ::com::sun::star::frame;
154 using namespace ::com::sun::star::beans;
155 using namespace ::com::sun::star::util;
156 using namespace ::com::sun::star::ucb;
157 using namespace ::com::sun::star::sdbc;
158 using namespace ::com::sun::star::task;
159 using namespace ::svt;
161 namespace
163 Sequence< sal_Int8 > lcl_GetSequenceClassID( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
164 sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
165 sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 )
167 Sequence< sal_Int8 > aResult( 16 );
168 aResult[0] = static_cast<sal_Int8>(n1 >> 24);
169 aResult[1] = static_cast<sal_Int8>(( n1 << 8 ) >> 24);
170 aResult[2] = static_cast<sal_Int8>(( n1 << 16 ) >> 24);
171 aResult[3] = static_cast<sal_Int8>(( n1 << 24 ) >> 24);
172 aResult[4] = static_cast<sal_Int8>(n2 >> 8);
173 aResult[5] = static_cast<sal_Int8>(( n2 << 8 ) >> 8);
174 aResult[6] = static_cast<sal_Int8>(n3 >> 8);
175 aResult[7] = static_cast<sal_Int8>(( n3 << 8 ) >> 8);
176 aResult[8] = b8;
177 aResult[9] = b9;
178 aResult[10] = b10;
179 aResult[11] = b11;
180 aResult[12] = b12;
181 aResult[13] = b13;
182 aResult[14] = b14;
183 aResult[15] = b15;
185 return aResult;
190 //==================================================================
191 //= OLinkedDocumentsAccess
192 //==================================================================
193 DBG_NAME(OLinkedDocumentsAccess)
194 //------------------------------------------------------------------
195 OLinkedDocumentsAccess::OLinkedDocumentsAccess(Window* _pDialogParent
196 , const Reference< XFrame >& _rxParentFrame
197 , const Reference< XMultiServiceFactory >& _rxORB
198 , const Reference< XNameAccess >& _rxContainer
199 , const Reference< XConnection>& _xConnection
200 , const ::rtl::OUString& _sDataSourceName
202 :m_xORB(_rxORB)
203 ,m_xDocumentContainer(_rxContainer)
204 ,m_xConnection(_xConnection)
205 ,m_xParentFrame(_rxParentFrame)
206 ,m_pDialogParent(_pDialogParent)
207 ,m_sDataSourceName(_sDataSourceName)
209 DBG_CTOR(OLinkedDocumentsAccess,NULL);
210 OSL_ENSURE(m_xORB.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid service factory!");
211 OSL_ENSURE(m_pDialogParent, "OLinkedDocumentsAccess::OLinkedDocumentsAccess: really need a dialog parent!");
213 //------------------------------------------------------------------
214 OLinkedDocumentsAccess::~OLinkedDocumentsAccess()
216 DBG_DTOR(OLinkedDocumentsAccess,NULL);
218 //------------------------------------------------------------------
219 Reference< XComponent> OLinkedDocumentsAccess::impl_open( const ::rtl::OUString& _rLinkName, Reference< XComponent >& _xDefinition,
220 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
222 Reference< XComponent> xRet;
223 OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid document container!");
224 Reference< XComponentLoader > xComponentLoader(m_xDocumentContainer,UNO_QUERY);
225 if ( !xComponentLoader.is() )
226 return xRet;
228 WaitObject aWaitCursor( m_pDialogParent );
230 ::comphelper::NamedValueCollection aArguments;
231 ::rtl::OUString sOpenMode;
232 switch ( _eOpenMode )
234 case E_OPEN_NORMAL:
235 sOpenMode = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "open" ) );
236 break;
238 case E_OPEN_FOR_MAIL:
239 aArguments.put( "Hidden", true );
240 // fall through
242 case E_OPEN_DESIGN:
243 sOpenMode = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "openDesign" ) );
244 break;
246 default:
247 OSL_ENSURE( false, "OLinkedDocumentsAccess::implOpen: invalid open mode!" );
248 break;
250 aArguments.put( "OpenMode", sOpenMode );
252 aArguments.put( (::rtl::OUString)PROPERTY_ACTIVE_CONNECTION, m_xConnection );
255 Reference<XHierarchicalNameContainer> xHier(m_xDocumentContainer,UNO_QUERY);
256 if ( xHier.is() && xHier->hasByHierarchicalName(_rLinkName) )
258 _xDefinition.set(xHier->getByHierarchicalName(_rLinkName),UNO_QUERY);
261 aArguments.merge( _rAdditionalArgs, true );
263 xRet = xComponentLoader->loadComponentFromURL( _rLinkName, ::rtl::OUString(), 0, aArguments.getPropertyValues() );
265 catch(Exception& e)
267 (void)e;
268 throw;
271 return xRet;
273 //------------------------------------------------------------------
274 Reference< XComponent> OLinkedDocumentsAccess::impl_newWithPilot( const char* _pWizardService,
275 Reference< XComponent >& _xDefinition, const sal_Int32 _nCommandType, const ::rtl::OUString& _rObjectName )
277 Reference< XComponent> xRet;
280 ::svx::ODataAccessDescriptor aDesc;
281 aDesc.setDataSource(m_sDataSourceName);
282 if ( _nCommandType != -1 )
283 aDesc[::svx::daCommandType] <<= _nCommandType;
284 if ( _rObjectName.getLength() )
285 aDesc[::svx::daCommand] <<= _rObjectName;
286 if ( m_xConnection.is() )
287 aDesc[::svx::daConnection] <<= m_xConnection;
289 Sequence<Any> aSeq = aDesc.createAnySequence();
290 const sal_Int32 nLength = aSeq.getLength();
291 aSeq.realloc(nLength + 1 );
292 PropertyValue aVal;
293 aVal.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ParentFrame"));
294 aVal.Value <<= m_xParentFrame;
295 aSeq[nLength] <<= aVal;
297 Reference< XJobExecutor > xFormWizard;
299 WaitObject aWaitCursor( m_pDialogParent );
300 xFormWizard.set(m_xORB->createInstanceWithArguments(::rtl::OUString::createFromAscii(_pWizardService),aSeq),UNO_QUERY);
302 if ( xFormWizard.is() )
304 xFormWizard->trigger(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("start")));
305 Reference<XPropertySet> xProp(xFormWizard,UNO_QUERY);
306 if ( xProp.is() )
308 Reference<XPropertySetInfo> xInfo = xProp->getPropertySetInfo();
309 if ( xInfo->hasPropertyByName(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Document"))) )
311 _xDefinition.set(xProp->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentDefinition"))),UNO_QUERY);
312 xRet.set(xProp->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Document"))),UNO_QUERY);
315 xFormWizard->trigger(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("end")));
316 ::comphelper::disposeComponent(xFormWizard);
319 catch(const Exception& e)
321 (void) e;
322 OSL_ENSURE(sal_False, "OLinkedDocumentsAccess::newWithPilot: caught an exception while loading the object!");
324 return xRet;
326 //------------------------------------------------------------------
327 Reference< XComponent> OLinkedDocumentsAccess::newFormWithPilot(Reference< XComponent >& _xDefinition,const sal_Int32 _nCommandType,const ::rtl::OUString& _rObjectName)
329 return impl_newWithPilot( "com.sun.star.wizards.form.CallFormWizard", _xDefinition, _nCommandType, _rObjectName );
332 //------------------------------------------------------------------
333 Reference< XComponent> OLinkedDocumentsAccess::newReportWithPilot( Reference< XComponent >& _xDefinition, const sal_Int32 _nCommandType, const ::rtl::OUString& _rObjectName )
335 return impl_newWithPilot( "com.sun.star.wizards.report.CallReportWizard", _xDefinition, _nCommandType, _rObjectName );
337 //------------------------------------------------------------------
338 Reference< XComponent> OLinkedDocumentsAccess::newTableWithPilot()
340 Reference< XComponent > xDefinition;
341 return impl_newWithPilot( "com.sun.star.wizards.table.CallTableWizard", xDefinition, -1, ::rtl::OUString() );
343 //------------------------------------------------------------------
344 Reference< XComponent> OLinkedDocumentsAccess::newQueryWithPilot()
346 Reference< XComponent > xDefinition;
347 return impl_newWithPilot( "com.sun.star.wizards.query.CallQueryWizard", xDefinition, -1, ::rtl::OUString() );
349 //------------------------------------------------------------------
350 Reference< XComponent > OLinkedDocumentsAccess::newDocument( sal_Int32 _nNewFormId, Reference< XComponent >& _xDefinition, const sal_Int32 _nCommandType, const ::rtl::OUString& _sObjectName )
352 OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid document container!");
353 // determine the URL to use for the new document
354 Sequence<sal_Int8> aClassId;
355 switch (_nNewFormId)
357 case ID_FORM_NEW_TEXT:
358 aClassId = lcl_GetSequenceClassID(SO3_SW_CLASSID);
359 OSL_ENSURE(aClassId == comphelper::MimeConfigurationHelper::GetSequenceClassID(SO3_SW_CLASSID),"Not equal");
360 break;
362 case ID_FORM_NEW_CALC:
363 aClassId = lcl_GetSequenceClassID(SO3_SC_CLASSID);
364 break;
366 case ID_FORM_NEW_IMPRESS:
367 aClassId = lcl_GetSequenceClassID(SO3_SIMPRESS_CLASSID);
368 break;
369 case ID_REPORT_NEW_TEXT:
370 aClassId = comphelper::MimeConfigurationHelper::GetSequenceClassID(SO3_RPT_CLASSID_90);
371 break;
373 case SID_DB_FORM_NEW_PILOT:
374 default:
375 OSL_ENSURE(sal_False, "OLinkedDocumentsAccess::newForm: pleas use newFormWithPilot!");
376 return Reference< XComponent >();
379 // load the document as template
380 Reference< XComponent > xNewDocument;
382 { // get the desktop object
384 Reference<XMultiServiceFactory> xORB(m_xDocumentContainer,UNO_QUERY);
385 if ( xORB.is() )
387 Sequence< Any > aArguments(2);
389 PropertyValue aValue;
391 aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ClassID"));
392 aValue.Value <<= aClassId;
393 aArguments[0] <<= aValue;
395 aValue.Name = PROPERTY_ACTIVE_CONNECTION;
396 aValue.Value <<= m_xConnection;
397 aArguments[1] <<= aValue;
399 Reference<XCommandProcessor> xContent(xORB->createInstanceWithArguments(SERVICE_SDB_DOCUMENTDEFINITION,aArguments),UNO_QUERY);
400 if ( xContent.is() )
402 _xDefinition.set(xContent,UNO_QUERY);
403 Command aCommand;
404 aCommand.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("openDesign"));
405 OpenCommandArgument2 aOpenCommand;
406 aOpenCommand.Mode = OpenMode::DOCUMENT;
407 aCommand.Argument <<= aOpenCommand;
408 WaitObject aWaitCursor( m_pDialogParent );
409 xNewDocument.set(xContent->execute(aCommand,xContent->createCommandIdentifier(),Reference< XCommandEnvironment >()),UNO_QUERY);
410 Reference<XPropertySet> xProp(xNewDocument,UNO_QUERY);
411 if ( xProp.is() && _sObjectName.getLength() )
413 xProp->setPropertyValue(PROPERTY_COMMAND_TYPE,makeAny(_nCommandType));
414 xProp->setPropertyValue(PROPERTY_COMMAND,makeAny(_sObjectName));
419 catch(const Exception& )
421 DBG_UNHANDLED_EXCEPTION();
424 return xNewDocument;
427 //------------------------------------------------------------------
428 Reference< XComponent > OLinkedDocumentsAccess::open( const ::rtl::OUString& _rLinkName, Reference< XComponent >& _xDefinition,
429 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
431 dbtools::SQLExceptionInfo aInfo;
432 Reference< XComponent > xRet;
433 try
435 xRet = impl_open( _rLinkName, _xDefinition, _eOpenMode, _rAdditionalArgs );
436 if ( !xRet.is() )
438 String sMessage = String(ModuleRes(STR_COULDNOTOPEN_LINKEDDOC));
439 sMessage.SearchAndReplaceAscii("$file$",_rLinkName);
441 com::sun::star::sdbc::SQLException aSQLException;
442 aSQLException.Message = sMessage;
443 // aSQLException.Context = e.Context;
444 aInfo = dbtools::SQLExceptionInfo(aSQLException);
446 return xRet;
448 catch (com::sun::star::io::WrongFormatException e)
450 com::sun::star::sdbc::SQLException aSQLException;
451 aSQLException.Message = e.Message;
452 aSQLException.Context = e.Context;
453 aInfo = dbtools::SQLExceptionInfo(aSQLException);
455 // more like a hack, insert an empty message
456 String sText( ModuleRes( RID_STR_EXTENSION_NOT_PRESENT ) );
457 sText.SearchAndReplaceAscii("$file$",_rLinkName);
458 aInfo.prepend(sText);
460 String sMessage = String(ModuleRes(STR_COULDNOTOPEN_LINKEDDOC));
461 sMessage.SearchAndReplaceAscii("$file$",_rLinkName);
462 aInfo.prepend(sMessage);
464 catch(Exception& e)
466 Any aAny = ::cppu::getCaughtException();
467 com::sun::star::sdbc::SQLException a;
468 if ( !(aAny >>= a) || (a.ErrorCode != dbtools::ParameterInteractionCancelled) )
470 com::sun::star::sdbc::SQLException aSQLException;
471 aSQLException.Message = e.Message;
472 aSQLException.Context = e.Context;
473 aInfo = dbtools::SQLExceptionInfo(aSQLException);
475 // more like a hack, insert an empty message
476 aInfo.prepend(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" \n")));
478 String sMessage = String(ModuleRes(STR_COULDNOTOPEN_LINKEDDOC));
479 sMessage.SearchAndReplaceAscii("$file$",_rLinkName);
480 aInfo.prepend(sMessage);
483 if (aInfo.isValid())
485 showError(aInfo, VCLUnoHelper::GetInterface(m_pDialogParent), m_xORB );
487 return xRet;
491 //......................................................................
492 } // namespace dbaui
493 //......................................................................