Line data Source code
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 "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 : // for calling basic
47 : #include <sfx2/app.hxx>
48 : #include <basic/sbx.hxx>
49 : #include <basic/sbuno.hxx>
50 : #include <svtools/ehdl.hxx>
51 : #include <svx/dataaccessdescriptor.hxx>
52 : #include <com/sun/star/container/XHierarchicalNameContainer.hpp>
53 : #include <vcl/waitobj.hxx>
54 : #include <comphelper/mimeconfighelper.hxx>
55 :
56 : #include <cppuhelper/exc_hlp.hxx>
57 : #include <connectivity/dbtools.hxx>
58 : #include <toolkit/helper/vclunohelper.hxx>
59 : #include <com/sun/star/io/WrongFormatException.hpp>
60 : #include "com/sun/star/sdb/RowSetVetoException.hpp"
61 :
62 : namespace dbaui
63 : {
64 :
65 : using namespace ::com::sun::star::uno;
66 : using namespace ::com::sun::star::container;
67 : using namespace ::com::sun::star::lang;
68 : using namespace ::com::sun::star::frame;
69 : using namespace ::com::sun::star::beans;
70 : using namespace ::com::sun::star::util;
71 : using namespace ::com::sun::star::ucb;
72 : using namespace ::com::sun::star::sdbc;
73 : using namespace ::com::sun::star::sdb::application;
74 : using namespace ::com::sun::star::task;
75 : using namespace ::svt;
76 :
77 : namespace
78 : {
79 0 : Sequence< sal_Int8 > lcl_GetSequenceClassID( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
80 : sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
81 : sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 )
82 : {
83 0 : Sequence< sal_Int8 > aResult( 16 );
84 0 : aResult[0] = static_cast<sal_Int8>(n1 >> 24);
85 0 : aResult[1] = static_cast<sal_Int8>(( n1 << 8 ) >> 24);
86 0 : aResult[2] = static_cast<sal_Int8>(( n1 << 16 ) >> 24);
87 0 : aResult[3] = static_cast<sal_Int8>(( n1 << 24 ) >> 24);
88 0 : aResult[4] = static_cast<sal_Int8>(n2 >> 8);
89 0 : aResult[5] = static_cast<sal_Int8>(( n2 << 8 ) >> 8);
90 0 : aResult[6] = static_cast<sal_Int8>(n3 >> 8);
91 0 : aResult[7] = static_cast<sal_Int8>(( n3 << 8 ) >> 8);
92 0 : aResult[8] = b8;
93 0 : aResult[9] = b9;
94 0 : aResult[10] = b10;
95 0 : aResult[11] = b11;
96 0 : aResult[12] = b12;
97 0 : aResult[13] = b13;
98 0 : aResult[14] = b14;
99 0 : aResult[15] = b15;
100 :
101 0 : return aResult;
102 : }
103 : }
104 :
105 : // OLinkedDocumentsAccess
106 0 : OLinkedDocumentsAccess::OLinkedDocumentsAccess( Window* _pDialogParent, const Reference< XDatabaseDocumentUI >& i_rDocumentUI,
107 : const Reference< XComponentContext >& _rxContext, const Reference< XNameAccess >& _rxContainer,
108 : const Reference< XConnection>& _xConnection, const OUString& _sDataSourceName )
109 : :m_xContext(_rxContext)
110 : ,m_xDocumentContainer(_rxContainer)
111 : ,m_xConnection(_xConnection)
112 : ,m_xDocumentUI( i_rDocumentUI )
113 : ,m_pDialogParent(_pDialogParent)
114 0 : ,m_sDataSourceName(_sDataSourceName)
115 : {
116 : OSL_ENSURE(m_xContext.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid service factory!");
117 : OSL_ENSURE(m_pDialogParent, "OLinkedDocumentsAccess::OLinkedDocumentsAccess: really need a dialog parent!");
118 0 : }
119 0 : OLinkedDocumentsAccess::~OLinkedDocumentsAccess()
120 : {
121 0 : }
122 0 : Reference< XComponent> OLinkedDocumentsAccess::impl_open( const OUString& _rLinkName, Reference< XComponent >& _xDefinition,
123 : ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
124 : {
125 0 : Reference< XComponent> xRet;
126 : OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid document container!");
127 0 : Reference< XComponentLoader > xComponentLoader(m_xDocumentContainer,UNO_QUERY);
128 0 : if ( !xComponentLoader.is() )
129 0 : return xRet;
130 :
131 0 : WaitObject aWaitCursor( m_pDialogParent );
132 :
133 0 : ::comphelper::NamedValueCollection aArguments;
134 0 : OUString sOpenMode;
135 0 : switch ( _eOpenMode )
136 : {
137 : case E_OPEN_NORMAL:
138 0 : sOpenMode = "open";
139 0 : break;
140 :
141 : case E_OPEN_FOR_MAIL:
142 0 : aArguments.put( "Hidden", true );
143 : // fall through
144 :
145 : case E_OPEN_DESIGN:
146 0 : sOpenMode = "openDesign";
147 0 : break;
148 :
149 : default:
150 : OSL_FAIL( "OLinkedDocumentsAccess::implOpen: invalid open mode!" );
151 0 : break;
152 : }
153 0 : aArguments.put( "OpenMode", sOpenMode );
154 :
155 0 : aArguments.put( (OUString)PROPERTY_ACTIVE_CONNECTION, m_xConnection );
156 : try
157 : {
158 0 : Reference<XHierarchicalNameContainer> xHier(m_xDocumentContainer,UNO_QUERY);
159 0 : if ( xHier.is() && xHier->hasByHierarchicalName(_rLinkName) )
160 : {
161 0 : _xDefinition.set(xHier->getByHierarchicalName(_rLinkName),UNO_QUERY);
162 : }
163 :
164 0 : aArguments.merge( _rAdditionalArgs, true );
165 :
166 0 : xRet = xComponentLoader->loadComponentFromURL( _rLinkName, OUString(), 0, aArguments.getPropertyValues() );
167 : }
168 0 : catch(const Exception&)
169 : {
170 0 : throw;
171 : }
172 :
173 0 : return xRet;
174 : }
175 0 : void OLinkedDocumentsAccess::impl_newWithPilot( const char* _pWizardService,
176 : const sal_Int32 _nCommandType, const OUString& _rObjectName )
177 : {
178 : try
179 : {
180 0 : ::comphelper::NamedValueCollection aArgs;
181 0 : aArgs.put( "DataSourceName", m_sDataSourceName );
182 :
183 0 : if ( m_xConnection.is() )
184 0 : aArgs.put( "ActiveConnection", m_xConnection );
185 :
186 0 : if ( !_rObjectName.isEmpty() && ( _nCommandType != -1 ) )
187 : {
188 0 : aArgs.put( "CommandType", _nCommandType );
189 0 : aArgs.put( "Command", _rObjectName );
190 : }
191 :
192 0 : aArgs.put( "DocumentUI", m_xDocumentUI );
193 :
194 0 : Reference< XJobExecutor > xWizard;
195 : {
196 0 : WaitObject aWaitCursor( m_pDialogParent );
197 0 : xWizard.set( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
198 : OUString::createFromAscii( _pWizardService ),
199 : aArgs.getWrappedPropertyValues(),
200 : m_xContext
201 0 : ), UNO_QUERY_THROW );
202 : }
203 :
204 0 : xWizard->trigger( OUString( "start" ) );
205 0 : ::comphelper::disposeComponent( xWizard );
206 : }
207 0 : catch(const Exception&)
208 : {
209 : DBG_UNHANDLED_EXCEPTION();
210 : }
211 0 : }
212 0 : void OLinkedDocumentsAccess::newFormWithPilot( const sal_Int32 _nCommandType,const OUString& _rObjectName )
213 : {
214 0 : impl_newWithPilot( "com.sun.star.wizards.form.CallFormWizard", _nCommandType, _rObjectName );
215 0 : }
216 :
217 0 : void OLinkedDocumentsAccess::newReportWithPilot( const sal_Int32 _nCommandType, const OUString& _rObjectName )
218 : {
219 0 : impl_newWithPilot( "com.sun.star.wizards.report.CallReportWizard", _nCommandType, _rObjectName );
220 0 : }
221 0 : void OLinkedDocumentsAccess::newTableWithPilot()
222 : {
223 0 : impl_newWithPilot( "com.sun.star.wizards.table.CallTableWizard", -1, OUString() );
224 0 : }
225 0 : void OLinkedDocumentsAccess::newQueryWithPilot()
226 : {
227 0 : impl_newWithPilot( "com.sun.star.wizards.query.CallQueryWizard", -1, OUString() );
228 0 : }
229 0 : Reference< XComponent > OLinkedDocumentsAccess::newDocument( sal_Int32 i_nActionID,
230 : const ::comphelper::NamedValueCollection& i_rCreationArgs, Reference< XComponent >& o_rDefinition )
231 : {
232 : OSL_ENSURE(m_xDocumentContainer.is(), "OLinkedDocumentsAccess::newDocument: invalid document container!");
233 : // determine the class ID to use for the new document
234 0 : Sequence<sal_Int8> aClassId;
235 0 : if ( !i_rCreationArgs.has( "ClassID" )
236 0 : && !i_rCreationArgs.has( "MediaType" )
237 0 : && !i_rCreationArgs.has( "DocumentServiceName" )
238 : )
239 : {
240 0 : switch ( i_nActionID )
241 : {
242 : case ID_FORM_NEW_TEXT:
243 0 : aClassId = lcl_GetSequenceClassID(SO3_SW_CLASSID);
244 : OSL_ENSURE(aClassId == comphelper::MimeConfigurationHelper::GetSequenceClassID(SO3_SW_CLASSID),"Not equal");
245 0 : break;
246 :
247 : case ID_FORM_NEW_CALC:
248 0 : aClassId = lcl_GetSequenceClassID(SO3_SC_CLASSID);
249 0 : break;
250 :
251 : case ID_FORM_NEW_IMPRESS:
252 0 : aClassId = lcl_GetSequenceClassID(SO3_SIMPRESS_CLASSID);
253 0 : break;
254 :
255 : case ID_REPORT_NEW_TEXT:
256 0 : aClassId = comphelper::MimeConfigurationHelper::GetSequenceClassID(SO3_RPT_CLASSID_90);
257 0 : break;
258 :
259 : default:
260 : OSL_FAIL( "OLinkedDocumentsAccess::newDocument: please use newFormWithPilot!" );
261 0 : return Reference< XComponent >();
262 :
263 : }
264 : }
265 :
266 : // load the document as template
267 0 : Reference< XComponent > xNewDocument;
268 : try
269 : { // get the desktop object
270 :
271 0 : Reference<XMultiServiceFactory> xORB(m_xDocumentContainer,UNO_QUERY);
272 0 : if ( xORB.is() )
273 : {
274 0 : ::comphelper::NamedValueCollection aCreationArgs( i_rCreationArgs );
275 0 : if ( aClassId.getLength() )
276 0 : aCreationArgs.put( "ClassID", aClassId );
277 0 : aCreationArgs.put( (OUString)PROPERTY_ACTIVE_CONNECTION, m_xConnection );
278 :
279 : // separate values which are real creation args from args relevant for opening the doc
280 0 : ::comphelper::NamedValueCollection aCommandArgs;
281 0 : if ( aCreationArgs.has( "Hidden" ) )
282 : {
283 0 : aCommandArgs.put( "Hidden", aCreationArgs.get( "Hidden" ) );
284 0 : aCreationArgs.remove( "Hidden" );
285 : }
286 :
287 0 : Reference< XCommandProcessor > xContent( xORB->createInstanceWithArguments(
288 : SERVICE_SDB_DOCUMENTDEFINITION,
289 : aCreationArgs.getWrappedPropertyValues()
290 0 : ),
291 : UNO_QUERY_THROW
292 0 : );
293 0 : o_rDefinition.set( xContent, UNO_QUERY );
294 :
295 : // put the OpenMode into the OpenArgs
296 0 : OpenCommandArgument aOpenModeArg;
297 0 : aOpenModeArg.Mode = OpenMode::DOCUMENT;
298 0 : aCommandArgs.put( "OpenMode", aOpenModeArg );
299 :
300 0 : Command aCommand;
301 0 : aCommand.Name = "openDesign";
302 0 : aCommand.Argument <<= aCommandArgs.getPropertyValues();
303 0 : WaitObject aWaitCursor( m_pDialogParent );
304 0 : xNewDocument.set( xContent->execute( aCommand, xContent->createCommandIdentifier(), NULL ), UNO_QUERY );
305 0 : }
306 : }
307 0 : catch(const Exception&)
308 : {
309 : DBG_UNHANDLED_EXCEPTION();
310 : }
311 :
312 0 : return xNewDocument;
313 : }
314 :
315 0 : Reference< XComponent > OLinkedDocumentsAccess::open( const OUString& _rLinkName, Reference< XComponent >& _xDefinition,
316 : ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs )
317 : {
318 0 : dbtools::SQLExceptionInfo aInfo;
319 0 : Reference< XComponent > xRet;
320 : try
321 : {
322 0 : xRet = impl_open( _rLinkName, _xDefinition, _eOpenMode, _rAdditionalArgs );
323 0 : if ( !xRet.is() )
324 : {
325 0 : OUString sMessage = ModuleRes(STR_COULDNOTOPEN_LINKEDDOC);
326 0 : sMessage = sMessage.replaceFirst("$file$",_rLinkName);
327 :
328 0 : com::sun::star::sdbc::SQLException aSQLException;
329 0 : aSQLException.Message = sMessage;
330 0 : aInfo = dbtools::SQLExceptionInfo(aSQLException);
331 : }
332 0 : return xRet;
333 : }
334 0 : catch(const com::sun::star::io::WrongFormatException &e)
335 : {
336 0 : com::sun::star::sdbc::SQLException aSQLException;
337 0 : aSQLException.Message = e.Message;
338 0 : aSQLException.Context = e.Context;
339 0 : aInfo = dbtools::SQLExceptionInfo(aSQLException);
340 :
341 : // more like a hack, insert an empty message
342 0 : OUString sText( ModuleRes( RID_STR_EXTENSION_NOT_PRESENT ) );
343 0 : sText = sText.replaceFirst("$file$",_rLinkName);
344 0 : aInfo.prepend(sText);
345 :
346 0 : OUString sMessage = ModuleRes(STR_COULDNOTOPEN_LINKEDDOC);
347 0 : sMessage = sMessage.replaceFirst("$file$",_rLinkName);
348 0 : aInfo.prepend(sMessage);
349 : }
350 0 : catch(const Exception& e)
351 : {
352 0 : Any aAny = ::cppu::getCaughtException();
353 0 : com::sun::star::sdbc::SQLException a;
354 0 : if ( !(aAny >>= a) || (a.ErrorCode != dbtools::ParameterInteractionCancelled) )
355 : {
356 0 : com::sun::star::sdbc::SQLException aSQLException;
357 0 : aSQLException.Message = e.Message;
358 0 : aSQLException.Context = e.Context;
359 0 : aInfo = dbtools::SQLExceptionInfo(aSQLException);
360 :
361 : // more like a hack, insert an empty message
362 0 : aInfo.prepend(OUString(" \n"));
363 :
364 0 : OUString sMessage = ModuleRes(STR_COULDNOTOPEN_LINKEDDOC);
365 0 : sMessage = sMessage.replaceFirst("$file$",_rLinkName);
366 0 : aInfo.prepend(sMessage);
367 0 : }
368 : }
369 0 : if (aInfo.isValid())
370 : {
371 0 : showError(aInfo, VCLUnoHelper::GetInterface(m_pDialogParent), m_xContext );
372 : }
373 0 : return xRet;
374 : }
375 :
376 : } // namespace dbaui
377 :
378 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|