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> 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> 42 43 #include <cppuhelper/exc_hlp.hxx> 44 #include <connectivity/dbtools.hxx> 45 #include <com/sun/star/io/WrongFormatException.hpp> 46 47 namespace dbaui 48 { 49 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; 61 62 namespace 63 { lcl_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)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 ) 67 { 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; 85 86 return aResult; 87 } 88 } 89 90 // OLinkedDocumentsAccess OLinkedDocumentsAccess(weld::Window * pDialogParent,const Reference<XDatabaseDocumentUI> & i_rDocumentUI,const Reference<XComponentContext> & _rxContext,const Reference<XNameAccess> & _rxContainer,const Reference<XConnection> & _xConnection,const OUString & _sDataSourceName)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) 100 { 101 OSL_ENSURE(m_xContext.is(), "OLinkedDocumentsAccess::OLinkedDocumentsAccess: invalid service factory!"); 102 assert(m_pDialogParent && "OLinkedDocumentsAccess::OLinkedDocumentsAccess: really need a dialog parent!"); 103 } ~OLinkedDocumentsAccess()104 OLinkedDocumentsAccess::~OLinkedDocumentsAccess() 105 { 106 } impl_open(const OUString & _rLinkName,Reference<XComponent> & _xDefinition,ElementOpenMode _eOpenMode,const::comphelper::NamedValueCollection & _rAdditionalArgs)107 Reference< XComponent> OLinkedDocumentsAccess::impl_open( const OUString& _rLinkName, Reference< XComponent >& _xDefinition, 108 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs ) 109 { 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; 115 116 weld::WaitObject aWaitCursor(m_pDialogParent); 117 118 ::comphelper::NamedValueCollection aArguments; 119 OUString sOpenMode; 120 switch ( _eOpenMode ) 121 { 122 case E_OPEN_NORMAL: 123 sOpenMode = "open"; 124 break; 125 126 case E_OPEN_FOR_MAIL: 127 aArguments.put( "Hidden", true ); 128 [[fallthrough]]; 129 130 case E_OPEN_DESIGN: 131 sOpenMode = "openDesign"; 132 break; 133 134 default: 135 OSL_FAIL( "OLinkedDocumentsAccess::implOpen: invalid open mode!" ); 136 break; 137 } 138 aArguments.put( "OpenMode", sOpenMode ); 139 140 aArguments.put( OUString(PROPERTY_ACTIVE_CONNECTION), m_xConnection ); 141 142 Reference<XHierarchicalNameContainer> xHier(m_xDocumentContainer,UNO_QUERY); 143 if ( xHier.is() && xHier->hasByHierarchicalName(_rLinkName) ) 144 { 145 _xDefinition.set(xHier->getByHierarchicalName(_rLinkName),UNO_QUERY); 146 } 147 148 aArguments.merge( _rAdditionalArgs, true ); 149 150 xRet = xComponentLoader->loadComponentFromURL( _rLinkName, OUString(), 0, aArguments.getPropertyValues() ); 151 152 return xRet; 153 } impl_newWithPilot(const char * _pWizardService,const sal_Int32 _nCommandType,const OUString & _rObjectName)154 void OLinkedDocumentsAccess::impl_newWithPilot( const char* _pWizardService, 155 const sal_Int32 _nCommandType, const OUString& _rObjectName ) 156 { 157 try 158 { 159 ::comphelper::NamedValueCollection aArgs; 160 aArgs.put( "DataSourceName", m_sDataSourceName ); 161 162 if ( m_xConnection.is() ) 163 aArgs.put( "ActiveConnection", m_xConnection ); 164 165 if ( !_rObjectName.isEmpty() && ( _nCommandType != -1 ) ) 166 { 167 aArgs.put( "CommandType", _nCommandType ); 168 aArgs.put( "Command", _rObjectName ); 169 } 170 171 aArgs.put( "DocumentUI", m_xDocumentUI ); 172 173 Reference< XJobExecutor > xWizard; 174 { 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 ); 181 } 182 183 xWizard->trigger( "start" ); 184 ::comphelper::disposeComponent( xWizard ); 185 } 186 catch(const Exception&) 187 { 188 DBG_UNHANDLED_EXCEPTION("dbaccess"); 189 } 190 } newFormWithPilot(const sal_Int32 _nCommandType,const OUString & _rObjectName)191 void OLinkedDocumentsAccess::newFormWithPilot( const sal_Int32 _nCommandType,const OUString& _rObjectName ) 192 { 193 impl_newWithPilot( "com.sun.star.wizards.form.CallFormWizard", _nCommandType, _rObjectName ); 194 } 195 newReportWithPilot(const sal_Int32 _nCommandType,const OUString & _rObjectName)196 void OLinkedDocumentsAccess::newReportWithPilot( const sal_Int32 _nCommandType, const OUString& _rObjectName ) 197 { 198 impl_newWithPilot( "com.sun.star.wizards.report.CallReportWizard", _nCommandType, _rObjectName ); 199 } newTableWithPilot()200 void OLinkedDocumentsAccess::newTableWithPilot() 201 { 202 impl_newWithPilot( "com.sun.star.wizards.table.CallTableWizard", -1, OUString() ); 203 } newQueryWithPilot()204 void OLinkedDocumentsAccess::newQueryWithPilot() 205 { 206 impl_newWithPilot( "com.sun.star.wizards.query.CallQueryWizard", -1, OUString() ); 207 } newDocument(sal_Int32 i_nActionID,const::comphelper::NamedValueCollection & i_rCreationArgs,Reference<XComponent> & o_rDefinition)208 Reference< XComponent > OLinkedDocumentsAccess::newDocument( sal_Int32 i_nActionID, 209 const ::comphelper::NamedValueCollection& i_rCreationArgs, Reference< XComponent >& o_rDefinition ) 210 { 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" ) 217 ) 218 { 219 switch ( i_nActionID ) 220 { 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; 225 226 case ID_FORM_NEW_CALC: 227 aClassId = lcl_GetSequenceClassID(SO3_SC_CLASSID); 228 break; 229 230 case ID_FORM_NEW_IMPRESS: 231 aClassId = lcl_GetSequenceClassID(SO3_SIMPRESS_CLASSID); 232 break; 233 234 case ID_REPORT_NEW_TEXT: 235 aClassId = comphelper::MimeConfigurationHelper::GetSequenceClassID(SO3_RPT_CLASSID_90); 236 break; 237 238 default: 239 OSL_FAIL( "OLinkedDocumentsAccess::newDocument: please use newFormWithPilot!" ); 240 return Reference< XComponent >(); 241 242 } 243 } 244 245 // load the document as template 246 Reference< XComponent > xNewDocument; 247 try 248 { // get the desktop object 249 250 Reference<XMultiServiceFactory> xORB(m_xDocumentContainer,UNO_QUERY); 251 if ( xORB.is() ) 252 { 253 ::comphelper::NamedValueCollection aCreationArgs( i_rCreationArgs ); 254 if ( aClassId.hasElements() ) 255 aCreationArgs.put( "ClassID", aClassId ); 256 aCreationArgs.put( OUString(PROPERTY_ACTIVE_CONNECTION), m_xConnection ); 257 258 // separate values which are real creation args from args relevant for opening the doc 259 ::comphelper::NamedValueCollection aCommandArgs; 260 if ( aCreationArgs.has( "Hidden" ) ) 261 { 262 aCommandArgs.put( "Hidden", aCreationArgs.get( "Hidden" ) ); 263 aCreationArgs.remove( "Hidden" ); 264 } 265 266 Reference< XCommandProcessor > xContent( xORB->createInstanceWithArguments( 267 SERVICE_SDB_DOCUMENTDEFINITION, 268 aCreationArgs.getWrappedPropertyValues() 269 ), 270 UNO_QUERY_THROW 271 ); 272 o_rDefinition.set( xContent, UNO_QUERY ); 273 274 // put the OpenMode into the OpenArgs 275 OpenCommandArgument aOpenModeArg; 276 aOpenModeArg.Mode = OpenMode::DOCUMENT; 277 aCommandArgs.put( "OpenMode", aOpenModeArg ); 278 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 ); 284 } 285 } 286 catch(const Exception&) 287 { 288 DBG_UNHANDLED_EXCEPTION("dbaccess"); 289 } 290 291 return xNewDocument; 292 } 293 open(const OUString & _rLinkName,Reference<XComponent> & _xDefinition,ElementOpenMode _eOpenMode,const::comphelper::NamedValueCollection & _rAdditionalArgs)294 Reference< XComponent > OLinkedDocumentsAccess::open( const OUString& _rLinkName, Reference< XComponent >& _xDefinition, 295 ElementOpenMode _eOpenMode, const ::comphelper::NamedValueCollection& _rAdditionalArgs ) 296 { 297 dbtools::SQLExceptionInfo aInfo; 298 Reference< XComponent > xRet; 299 try 300 { 301 xRet = impl_open( _rLinkName, _xDefinition, _eOpenMode, _rAdditionalArgs ); 302 if ( !xRet.is() ) 303 { 304 OUString sMessage = DBA_RES(STR_COULDNOTOPEN_LINKEDDOC); 305 sMessage = sMessage.replaceFirst("$file$",_rLinkName); 306 307 css::sdbc::SQLException aSQLException; 308 aSQLException.Message = sMessage; 309 aInfo = dbtools::SQLExceptionInfo(aSQLException); 310 } 311 } 312 catch(const css::io::WrongFormatException &e) 313 { 314 css::sdbc::SQLException aSQLException; 315 aSQLException.Message = e.Message; 316 aSQLException.Context = e.Context; 317 aInfo = dbtools::SQLExceptionInfo(aSQLException); 318 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); 323 324 OUString sMessage = DBA_RES(STR_COULDNOTOPEN_LINKEDDOC); 325 sMessage = sMessage.replaceFirst("$file$",_rLinkName); 326 aInfo.prepend(sMessage); 327 } 328 catch(const Exception& e) 329 { 330 Any aAny = ::cppu::getCaughtException(); 331 css::sdbc::SQLException a; 332 if ( !(aAny >>= a) || (a.ErrorCode != dbtools::ParameterInteractionCancelled) ) 333 { 334 css::sdbc::SQLException aSQLException; 335 aSQLException.Message = e.Message; 336 aSQLException.Context = e.Context; 337 aInfo = dbtools::SQLExceptionInfo(aSQLException); 338 339 // more like a hack, insert an empty message 340 aInfo.prepend(" \n"); 341 342 OUString sMessage = DBA_RES(STR_COULDNOTOPEN_LINKEDDOC); 343 sMessage = sMessage.replaceFirst("$file$",_rLinkName); 344 aInfo.prepend(sMessage); 345 } 346 } 347 if (aInfo.isValid()) 348 { 349 showError(aInfo, m_pDialogParent->GetXWindow(), m_xContext); 350 } 351 return xRet; 352 } 353 354 } // namespace dbaui 355 356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 357