bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / appl / appopen.cxx
blobb3ffefdd6ffacb7063f88d86c6922de6c538fb99
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 <com/sun/star/uno/Reference.h>
21 #include <com/sun/star/beans/PropertyValue.hpp>
22 #include <com/sun/star/frame/FrameSearchFlag.hpp>
23 #include <com/sun/star/frame/XComponentLoader.hpp>
24 #include <com/sun/star/frame/XNotifyingDispatch.hpp>
25 #include <com/sun/star/frame/XDispatchProvider.hpp>
26 #include <com/sun/star/util/XCloseable.hpp>
27 #include <com/sun/star/frame/XFrame.hpp>
28 #include <com/sun/star/frame/Desktop.hpp>
29 #include <com/sun/star/frame/DispatchResultState.hpp>
30 #include <com/sun/star/frame/XDispatchResultListener.hpp>
31 #include <com/sun/star/util/URL.hpp>
32 #include <com/sun/star/util/URLTransformer.hpp>
33 #include <com/sun/star/util/XURLTransformer.hpp>
34 #include <com/sun/star/system/SystemShellExecuteException.hpp>
35 #include <com/sun/star/document/XTypeDetection.hpp>
36 #include <com/sun/star/document/MacroExecMode.hpp>
37 #include <com/sun/star/document/UpdateDocMode.hpp>
38 #include <com/sun/star/task/ErrorCodeRequest.hpp>
39 #include <com/sun/star/task/InteractionHandler.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/embed/ElementModes.hpp>
42 #include <com/sun/star/container/XNameAccess.hpp>
43 #include <com/sun/star/uno/Sequence.h>
44 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
45 #include <cppuhelper/implbase1.hxx>
46 #include <rtl/ustring.hxx>
48 #include <comphelper/configurationhelper.hxx>
49 #include <comphelper/processfactory.hxx>
50 #include <comphelper/sequenceasvector.hxx>
51 #include <comphelper/storagehelper.hxx>
52 #include <comphelper/string.hxx>
53 #include <comphelper/synchronousdispatch.hxx>
55 #include <vcl/wrkwin.hxx>
56 #include <svl/intitem.hxx>
57 #include <vcl/msgbox.hxx>
58 #include <svl/stritem.hxx>
59 #include <svl/eitem.hxx>
60 #include <sfx2/doctempl.hxx>
61 #include <svtools/sfxecode.hxx>
62 #include <framework/preventduplicateinteraction.hxx>
63 #include <svtools/ehdl.hxx>
64 #include <basic/sbxobj.hxx>
65 #include <svl/urihelper.hxx>
66 #include <unotools/localfilehelper.hxx>
67 #include <unotools/pathoptions.hxx>
68 #include <unotools/moduleoptions.hxx>
69 #include <svtools/miscopt.hxx>
70 #include <svtools/templdlg.hxx>
71 #include <osl/file.hxx>
72 #include <unotools/extendedsecurityoptions.hxx>
73 #include <comphelper/docpasswordhelper.hxx>
74 #include <vcl/svapp.hxx>
76 #include <osl/mutex.hxx>
78 #include <rtl/logfile.hxx>
80 #include <sfx2/app.hxx>
81 #include <sfx2/bindings.hxx>
82 #include <sfx2/dispatch.hxx>
83 #include <sfx2/docfile.hxx>
84 #include <sfx2/fcontnr.hxx>
85 #include <sfx2/new.hxx>
86 #include <sfx2/objitem.hxx>
87 #include <sfx2/objsh.hxx>
88 #include <svl/slstitm.hxx>
89 #include "objshimp.hxx"
90 #include "openflag.hxx"
91 #include <sfx2/passwd.hxx>
92 #include "referers.hxx"
93 #include <sfx2/request.hxx>
94 #include "sfx2/sfxresid.hxx"
95 #include <sfx2/viewsh.hxx>
96 #include "app.hrc"
97 #include <sfx2/viewfrm.hxx>
98 #include <sfx2/sfxuno.hxx>
99 #include <sfx2/objface.hxx>
100 #include <sfx2/filedlghelper.hxx>
101 #include <sfx2/docfac.hxx>
102 #include <sfx2/event.hxx>
103 #include "templatedlg.hxx"
104 #include "openuriexternally.hxx"
106 using namespace ::com::sun::star;
107 using namespace ::com::sun::star::beans;
108 using namespace ::com::sun::star::frame;
109 using namespace ::com::sun::star::lang;
110 using namespace ::com::sun::star::uno;
111 using namespace ::com::sun::star::util;
112 using namespace ::com::sun::star::task;
113 using namespace ::com::sun::star::container;
114 using namespace ::cppu;
115 using namespace ::sfx2;
117 //=========================================================================
119 class SfxOpenDocStatusListener_Impl : public WeakImplHelper1< XDispatchResultListener >
121 public:
122 sal_Bool bFinished;
123 sal_Bool bSuccess;
124 virtual void SAL_CALL dispatchFinished( const DispatchResultEvent& Event ) throw(RuntimeException);
125 virtual void SAL_CALL disposing( const EventObject& Source ) throw(RuntimeException);
126 SfxOpenDocStatusListener_Impl()
127 : bFinished( sal_False )
128 , bSuccess( sal_False )
132 void SAL_CALL SfxOpenDocStatusListener_Impl::dispatchFinished( const DispatchResultEvent& aEvent ) throw(RuntimeException)
134 bSuccess = ( aEvent.State == DispatchResultState::SUCCESS );
135 bFinished = sal_True;
138 void SAL_CALL SfxOpenDocStatusListener_Impl::disposing( const EventObject& ) throw(RuntimeException)
142 //====================================================================
144 void SetTemplate_Impl( const String &rFileName,
145 const String &rLongName,
146 SfxObjectShell *pDoc)
148 // write TemplateName to DocumentProperties of document
149 // TemplateDate stays as default (=current date)
150 pDoc->ResetFromTemplate( rLongName, rFileName );
153 //====================================================================
154 class SfxDocPasswordVerifier : public ::comphelper::IDocPasswordVerifier
156 public:
157 inline explicit SfxDocPasswordVerifier( const Reference< embed::XStorage >& rxStorage ) :
158 mxStorage( rxStorage ) {}
160 virtual ::comphelper::DocPasswordVerifierResult
161 verifyPassword( const OUString& rPassword, uno::Sequence< beans::NamedValue >& o_rEncryptionData );
162 virtual ::comphelper::DocPasswordVerifierResult
163 verifyEncryptionData( const uno::Sequence< beans::NamedValue >& rEncryptionData );
166 private:
167 Reference< embed::XStorage > mxStorage;
170 //--------------------------------------------------------------------
171 ::comphelper::DocPasswordVerifierResult SfxDocPasswordVerifier::verifyPassword( const OUString& rPassword, uno::Sequence< beans::NamedValue >& o_rEncryptionData )
173 o_rEncryptionData = ::comphelper::OStorageHelper::CreatePackageEncryptionData( rPassword );
174 return verifyEncryptionData( o_rEncryptionData );
178 //--------------------------------------------------------------------
179 ::comphelper::DocPasswordVerifierResult SfxDocPasswordVerifier::verifyEncryptionData( const uno::Sequence< beans::NamedValue >& rEncryptionData )
181 ::comphelper::DocPasswordVerifierResult eResult = ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
184 // check the encryption data
185 // if the data correct is the stream will be opened successfully
186 // and immediatelly closed
187 ::comphelper::OStorageHelper::SetCommonStorageEncryptionData( mxStorage, rEncryptionData );
189 mxStorage->openStreamElement(
190 OUString( "content.xml" ),
191 embed::ElementModes::READ | embed::ElementModes::NOCREATE );
193 // no exception -> success
194 eResult = ::comphelper::DocPasswordVerifierResult_OK;
196 catch( const packages::WrongPasswordException& )
198 eResult = ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
200 catch( const uno::Exception& )
202 // unknown error, report it as wrong password
203 // TODO/LATER: we need an additional way to report unknown problems in this case
204 eResult = ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
206 return eResult;
209 //====================================================================
211 //--------------------------------------------------------------------
213 sal_uInt32 CheckPasswd_Impl
215 SfxObjectShell* pDoc,
216 SfxItemPool& /*rPool*/, // Pool, if a Set has to be created
217 SfxMedium* pFile // the Medium and its Password shold be obtained
220 /* [Description]
222 Ask for the password for a medium, only works if it concerns storage.
223 If the password flag is set in the Document Info, then the password is
224 requested through a user dialogue and the set at the Set of the medium.
225 If the set does not exist the it is created.
228 sal_uIntPtr nRet = ERRCODE_NONE;
230 if( ( !pFile->GetFilter() || pFile->IsStorage() ) )
232 uno::Reference< embed::XStorage > xStorage = pFile->GetStorage( sal_True );
233 if( xStorage.is() )
235 uno::Reference< beans::XPropertySet > xStorageProps( xStorage, uno::UNO_QUERY );
236 if ( xStorageProps.is() )
238 sal_Bool bIsEncrypted = sal_False;
239 try {
240 xStorageProps->getPropertyValue( OUString("HasEncryptedEntries") )
241 >>= bIsEncrypted;
242 } catch( uno::Exception& )
244 // TODO/LATER:
245 // the storage either has no encrypted elements or it's just
246 // does not allow to detect it, probably it should be implemented laiter
249 if ( bIsEncrypted )
251 Window* pWin = pDoc ? pDoc->GetDialogParent( pFile ) : NULL;
252 if ( pWin )
253 pWin->Show();
255 nRet = ERRCODE_SFX_CANTGETPASSWD;
257 SfxItemSet *pSet = pFile->GetItemSet();
258 if( pSet )
260 Reference< ::com::sun::star::task::XInteractionHandler > xInteractionHandler = pFile->GetInteractionHandler();
261 if( xInteractionHandler.is() )
263 // use the comphelper password helper to request a password
264 OUString aPassword;
265 SFX_ITEMSET_ARG( pSet, pPasswordItem, SfxStringItem, SID_PASSWORD, sal_False);
266 if ( pPasswordItem )
267 aPassword = pPasswordItem->GetValue();
269 uno::Sequence< beans::NamedValue > aEncryptionData;
270 SFX_ITEMSET_ARG( pSet, pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, sal_False);
271 if ( pEncryptionDataItem )
272 pEncryptionDataItem->GetValue() >>= aEncryptionData;
274 OUString aDocumentName = INetURLObject( pFile->GetOrigURL() ).GetMainURL( INetURLObject::DECODE_WITH_CHARSET );
276 SfxDocPasswordVerifier aVerifier( xStorage );
277 aEncryptionData = ::comphelper::DocPasswordHelper::requestAndVerifyDocPassword(
278 aVerifier, aEncryptionData, aPassword, xInteractionHandler, aDocumentName, comphelper::DocPasswordRequestType_STANDARD );
280 pSet->ClearItem( SID_PASSWORD );
281 pSet->ClearItem( SID_ENCRYPTIONDATA );
283 if ( aEncryptionData.getLength() > 0 )
285 pSet->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::makeAny( aEncryptionData ) ) );
289 // update the version list of the medium using the new password
290 pFile->GetVersionList();
292 catch( uno::Exception& )
294 // TODO/LATER: set the error code
297 nRet = ERRCODE_NONE;
299 else
300 nRet = ERRCODE_IO_ABORT;
305 else
307 OSL_FAIL( "A storage must implement XPropertySet interface!" );
308 nRet = ERRCODE_SFX_CANTGETPASSWD;
313 return nRet;
316 //--------------------------------------------------------------------
319 sal_uIntPtr SfxApplication::LoadTemplate( SfxObjectShellLock& xDoc, const String &rFileName, sal_Bool bCopy, SfxItemSet* pSet )
321 const SfxFilter* pFilter = NULL;
322 SfxMedium aMedium( rFileName, ( STREAM_READ | STREAM_SHARE_DENYNONE ) );
324 if ( !aMedium.GetStorage( sal_False ).is() )
325 aMedium.GetInStream();
327 if ( aMedium.GetError() )
329 delete pSet;
330 return aMedium.GetErrorCode();
333 aMedium.UseInteractionHandler( sal_True );
334 sal_uIntPtr nErr = GetFilterMatcher().GuessFilter( aMedium,&pFilter,SFX_FILTER_TEMPLATE, 0 );
335 if ( 0 != nErr)
337 delete pSet;
338 return ERRCODE_SFX_NOTATEMPLATE;
341 if( !pFilter || !pFilter->IsAllowedAsTemplate() )
343 delete pSet;
344 return ERRCODE_SFX_NOTATEMPLATE;
347 if ( pFilter->GetFilterFlags() & SFX_FILTER_STARONEFILTER )
349 DBG_ASSERT( !xDoc.Is(), "Sorry, not implemented!" );
350 delete pSet;
351 SfxStringItem aName( SID_FILE_NAME, rFileName );
352 SfxStringItem aReferer( SID_REFERER, OUString("private:user") );
353 SfxStringItem aFlags( SID_OPTIONS, OUString("T") );
354 SfxBoolItem aHidden( SID_HIDDEN, sal_True );
355 const SfxPoolItem *pRet = GetDispatcher_Impl()->Execute( SID_OPENDOC, SFX_CALLMODE_SYNCHRON, &aName, &aHidden, &aReferer, &aFlags, 0L );
356 const SfxObjectItem *pObj = PTR_CAST( SfxObjectItem, pRet );
357 if ( pObj )
358 xDoc = PTR_CAST( SfxObjectShell, pObj->GetShell() );
359 else
361 const SfxViewFrameItem *pView = PTR_CAST( SfxViewFrameItem, pRet );
362 if ( pView )
364 SfxViewFrame *pFrame = pView->GetFrame();
365 if ( pFrame )
366 xDoc = pFrame->GetObjectShell();
370 if ( !xDoc.Is() )
371 return ERRCODE_SFX_DOLOADFAILED;
373 else
375 if ( !xDoc.Is() )
376 xDoc = SfxObjectShell::CreateObject( pFilter->GetServiceName() );
378 //pMedium takes ownership of pSet
379 SfxMedium *pMedium = new SfxMedium( rFileName, STREAM_STD_READ, pFilter, pSet );
380 if(!xDoc->DoLoad(pMedium))
382 ErrCode nErrCode = xDoc->GetErrorCode();
383 xDoc->DoClose();
384 xDoc.Clear();
385 return nErrCode;
389 if( bCopy )
393 // TODO: introduce error handling
395 uno::Reference< embed::XStorage > xTempStorage = ::comphelper::OStorageHelper::GetTemporaryStorage();
396 if( !xTempStorage.is() )
397 throw uno::RuntimeException();
399 xDoc->GetStorage()->copyToStorage( xTempStorage );
401 if ( !xDoc->DoSaveCompleted( new SfxMedium( xTempStorage, String() ) ) )
402 throw uno::RuntimeException();
404 catch( uno::Exception& )
406 xDoc->DoClose();
407 xDoc.Clear();
409 // TODO: transfer correct error outside
410 return ERRCODE_SFX_GENERAL;
413 SetTemplate_Impl( rFileName, String(), xDoc );
415 else
416 SetTemplate_Impl( rFileName, String(), xDoc );
418 xDoc->SetNoName();
419 xDoc->InvalidateName();
420 xDoc->SetModified(sal_False);
421 xDoc->ResetError();
423 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel > xModel ( xDoc->GetModel(), ::com::sun::star::uno::UNO_QUERY );
424 if ( xModel.is() )
426 SfxItemSet* pNew = xDoc->GetMedium()->GetItemSet()->Clone();
427 pNew->ClearItem( SID_PROGRESS_STATUSBAR_CONTROL );
428 pNew->ClearItem( SID_FILTER_NAME );
429 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs;
430 TransformItems( SID_OPENDOC, *pNew, aArgs );
431 sal_Int32 nLength = aArgs.getLength();
432 aArgs.realloc( nLength + 1 );
433 aArgs[nLength].Name = "Title";
434 aArgs[nLength].Value <<= OUString( xDoc->GetTitle( SFX_TITLE_DETECT ) );
435 xModel->attachResource( OUString(), aArgs );
436 delete pNew;
439 return xDoc->GetErrorCode();
442 //--------------------------------------------------------------------
444 void SfxApplication::NewDocDirectExec_Impl( SfxRequest& rReq )
446 SFX_REQUEST_ARG( rReq, pFactoryItem, SfxStringItem, SID_NEWDOCDIRECT, sal_False);
447 String aFactName;
448 if ( pFactoryItem )
449 aFactName = pFactoryItem->GetValue();
450 else
451 aFactName = SvtModuleOptions().GetDefaultModuleName();
454 SfxRequest aReq( SID_OPENDOC, SFX_CALLMODE_SYNCHRON, GetPool() );
455 String aFact = OUString("private:factory/");
456 aFact += aFactName;
457 aReq.AppendItem( SfxStringItem( SID_FILE_NAME, aFact ) );
458 aReq.AppendItem( SfxFrameItem( SID_DOCFRAME, GetFrame() ) );
459 aReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString( "_default" ) ) );
461 // TODO/LATER: Should the other arguments be transferred as well?
462 SFX_REQUEST_ARG( rReq, pDefaultPathItem, SfxStringItem, SID_DEFAULTFILEPATH, sal_False);
463 if ( pDefaultPathItem )
464 aReq.AppendItem( *pDefaultPathItem );
465 SFX_REQUEST_ARG( rReq, pDefaultNameItem, SfxStringItem, SID_DEFAULTFILENAME, sal_False);
466 if ( pDefaultNameItem )
467 aReq.AppendItem( *pDefaultNameItem );
469 SFX_APP()->ExecuteSlot( aReq );
470 const SfxViewFrameItem* pItem = PTR_CAST( SfxViewFrameItem, aReq.GetReturnValue() );
471 if ( pItem )
472 rReq.SetReturnValue( SfxFrameItem( 0, pItem->GetFrame() ) );
475 //--------------------------------------------------------------------
477 void SfxApplication::NewDocExec_Impl( SfxRequest& rReq )
479 // No Parameter from BASIC only Factory given?
480 SFX_REQUEST_ARG(rReq, pTemplNameItem, SfxStringItem, SID_TEMPLATE_NAME, sal_False);
481 SFX_REQUEST_ARG(rReq, pTemplFileNameItem, SfxStringItem, SID_FILE_NAME, sal_False);
482 SFX_REQUEST_ARG(rReq, pTemplRegionNameItem, SfxStringItem, SID_TEMPLATE_REGIONNAME, sal_False);
484 SfxObjectShellLock xDoc;
486 String aTemplateRegion, aTemplateName, aTemplateFileName;
487 sal_Bool bDirect = sal_False; // through FileName instead of Region/Template
488 SfxErrorContext aEc(ERRCTX_SFX_NEWDOC);
489 if ( !pTemplNameItem && !pTemplFileNameItem )
491 sal_Bool bNewWin = sal_False;
492 Window* pTopWin = GetTopWindow();
494 SfxTemplateManagerDlg aTemplDlg;
495 int nRet = aTemplDlg.Execute();
496 if ( nRet == RET_OK )
498 rReq.Done();
499 if ( pTopWin != GetTopWindow() )
501 // the dialogue opens a document -> a new TopWindow appears
502 pTopWin = GetTopWindow();
503 bNewWin = sal_True;
507 if ( bNewWin && pTopWin )
508 // after the destruction of the dialogue its parent comes to top,
509 // but we want that the new document is on top
510 pTopWin->ToTop();
512 return;
514 else
516 // Template-Name
517 if ( pTemplNameItem )
518 aTemplateName = pTemplNameItem->GetValue();
520 // Template-Region
521 if ( pTemplRegionNameItem )
522 aTemplateRegion = pTemplRegionNameItem->GetValue();
524 // Template-File-Name
525 if ( pTemplFileNameItem )
527 aTemplateFileName = pTemplFileNameItem->GetValue();
528 bDirect = sal_True;
532 sal_uIntPtr lErr = 0;
533 SfxItemSet* pSet = new SfxAllItemSet( GetPool() );
534 pSet->Put( SfxBoolItem( SID_TEMPLATE, sal_True ) );
535 if ( !bDirect )
537 SfxDocumentTemplates aTmpFac;
538 if( !aTemplateFileName.Len() )
539 aTmpFac.GetFull( aTemplateRegion, aTemplateName, aTemplateFileName );
541 if( !aTemplateFileName.Len() )
542 lErr = ERRCODE_SFX_TEMPLATENOTFOUND;
545 INetURLObject aObj( aTemplateFileName );
546 SfxErrorContext aEC( ERRCTX_SFX_LOADTEMPLATE, aObj.PathToFileName() );
548 if ( lErr != ERRCODE_NONE )
550 sal_uIntPtr lFatalErr = ERRCODE_TOERROR(lErr);
551 if ( lFatalErr )
552 ErrorHandler::HandleError(lErr);
554 else
556 SfxCallMode eMode = SFX_CALLMODE_SYNCHRON;
558 const SfxPoolItem *pRet=0;
559 SfxStringItem aReferer( SID_REFERER, "private:user" );
560 SfxStringItem aTarget( SID_TARGETNAME, "_default" );
561 if ( aTemplateFileName.Len() )
563 DBG_ASSERT( aObj.GetProtocol() != INET_PROT_NOT_VALID, "Illegal URL!" );
565 SfxStringItem aName( SID_FILE_NAME, aObj.GetMainURL( INetURLObject::NO_DECODE ) );
566 SfxStringItem aTemplName( SID_TEMPLATE_NAME, aTemplateName );
567 SfxStringItem aTemplRegionName( SID_TEMPLATE_REGIONNAME, aTemplateRegion );
568 pRet = GetDispatcher_Impl()->Execute( SID_OPENDOC, eMode, &aName, &aTarget, &aReferer, &aTemplName, &aTemplRegionName, 0L );
570 else
572 SfxStringItem aName( SID_FILE_NAME, "private:factory" );
573 pRet = GetDispatcher_Impl()->Execute( SID_OPENDOC, eMode, &aName, &aTarget, &aReferer, 0L );
576 if ( pRet )
577 rReq.SetReturnValue( *pRet );
581 //---------------------------------------------------------------------------
583 namespace {
586 * Check if a given filter type should open the hyperlinked document
587 * natively.
589 * @param rFilter filter object
591 bool lcl_isFilterNativelySupported(const SfxFilter& rFilter)
593 if (rFilter.IsOwnFormat())
594 return true;
596 OUString aName = rFilter.GetFilterName();
597 if (aName.indexOf("MS Excel") == 0)
598 // We can handle all Excel variants natively.
599 return true;
601 return false;
606 void SfxApplication::OpenDocExec_Impl( SfxRequest& rReq )
608 OUString aDocService;
609 SFX_REQUEST_ARG(rReq, pDocSrvItem, SfxStringItem, SID_DOC_SERVICE, false);
610 if (pDocSrvItem)
611 aDocService = pDocSrvItem->GetValue();
613 sal_uInt16 nSID = rReq.GetSlot();
614 SFX_REQUEST_ARG( rReq, pFileNameItem, SfxStringItem, SID_FILE_NAME, sal_False );
615 if ( pFileNameItem )
617 String aCommand( pFileNameItem->GetValue() );
618 const SfxSlot* pSlot = GetInterface()->GetSlot( aCommand );
619 if ( pSlot )
621 pFileNameItem = NULL;
623 else
625 sal_Int32 nIndex = aCommand.SearchAscii("slot:");
626 if ( !nIndex )
628 sal_uInt16 nSlotId = (sal_uInt16) String( aCommand, 5, aCommand.Len()-5 ).ToInt32();
629 if ( nSlotId == SID_OPENDOC )
630 pFileNameItem = NULL;
635 if ( !pFileNameItem )
637 // get FileName from dialog
638 std::vector<OUString> pURLList;
639 String aFilter;
640 SfxItemSet* pSet = NULL;
641 String aPath;
642 SFX_REQUEST_ARG( rReq, pFolderNameItem, SfxStringItem, SID_PATH, sal_False );
643 if ( pFolderNameItem )
644 aPath = pFolderNameItem->GetValue();
645 else if ( nSID == SID_OPENTEMPLATE )
647 aPath = SvtPathOptions().GetTemplatePath();
648 sal_Int32 nTokenCount = comphelper::string::getTokenCount(aPath, ';');
649 aPath = aPath.GetToken(
650 sal::static_int_cast< xub_StrLen >(
651 nTokenCount ? ( nTokenCount - 1 ) : 0 ),
652 ';' );
655 sal_Int16 nDialog = SFX2_IMPL_DIALOG_CONFIG;
656 SFX_REQUEST_ARG( rReq, pSystemDialogItem, SfxBoolItem, SID_FILE_DIALOG, sal_False );
657 if ( pSystemDialogItem )
658 nDialog = pSystemDialogItem->GetValue() ? SFX2_IMPL_DIALOG_SYSTEM : SFX2_IMPL_DIALOG_OOO;
660 String sStandardDir;
662 SFX_REQUEST_ARG( rReq, pStandardDirItem, SfxStringItem, SID_STANDARD_DIR, sal_False );
663 if ( pStandardDirItem )
664 sStandardDir = pStandardDirItem->GetValue();
666 ::com::sun::star::uno::Sequence< OUString > aBlackList;
668 SFX_REQUEST_ARG( rReq, pBlackListItem, SfxStringListItem, SID_BLACK_LIST, sal_False );
669 if ( pBlackListItem )
670 pBlackListItem->GetStringList( aBlackList );
673 sal_uIntPtr nErr = sfx2::FileOpenDialog_Impl(
674 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION,
675 SFXWB_MULTISELECTION, String(), pURLList,
676 aFilter, pSet, &aPath, nDialog, sStandardDir, aBlackList );
678 if ( nErr == ERRCODE_ABORT )
680 pURLList.clear();
681 return;
684 rReq.SetArgs( *(SfxAllItemSet*)pSet );
685 if (aFilter.Len() >0 )
686 rReq.AppendItem( SfxStringItem( SID_FILTER_NAME, aFilter ) );
687 rReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString("_default") ) );
688 rReq.AppendItem( SfxStringItem( SID_REFERER, OUString(SFX_REFERER_USER) ) );
689 delete pSet;
691 if(!pURLList.empty())
693 if ( nSID == SID_OPENTEMPLATE )
694 rReq.AppendItem( SfxBoolItem( SID_TEMPLATE, sal_False ) );
696 // This helper wraps an existing (or may new created InteractionHandler)
697 // intercept all incoming interactions and provide useful information
698 // later if the following transaction was finished.
700 ::framework::PreventDuplicateInteraction* pHandler = new ::framework::PreventDuplicateInteraction(::comphelper::getProcessComponentContext());
701 css::uno::Reference< css::task::XInteractionHandler > xHandler (static_cast< css::task::XInteractionHandler* >(pHandler), css::uno::UNO_QUERY);
702 css::uno::Reference< css::task::XInteractionHandler > xWrappedHandler;
704 // wrap existing handler or create new UUI handler
705 SFX_REQUEST_ARG(rReq, pInteractionItem, SfxUnoAnyItem, SID_INTERACTIONHANDLER, sal_False);
706 if (pInteractionItem)
708 pInteractionItem->GetValue() >>= xWrappedHandler;
709 rReq.RemoveItem( SID_INTERACTIONHANDLER );
711 if (xWrappedHandler.is())
712 pHandler->setHandler(xWrappedHandler);
713 else
714 pHandler->useDefaultUUIHandler();
715 rReq.AppendItem( SfxUnoAnyItem(SID_INTERACTIONHANDLER,::com::sun::star::uno::makeAny(xHandler)) );
717 // define rules for this handler
718 css::uno::Type aInteraction = ::getCppuType(static_cast< css::task::ErrorCodeRequest* >(0));
719 ::framework::PreventDuplicateInteraction::InteractionInfo aRule (aInteraction, 1);
720 pHandler->addInteractionRule(aRule);
722 if (!aDocService.isEmpty())
724 rReq.RemoveItem(SID_DOC_SERVICE);
725 rReq.AppendItem(SfxStringItem(SID_DOC_SERVICE, aDocService));
728 for(std::vector<OUString>::const_iterator i = pURLList.begin(); i != pURLList.end(); ++i)
730 rReq.RemoveItem( SID_FILE_NAME );
731 rReq.AppendItem( SfxStringItem( SID_FILE_NAME, *i ) );
733 // Run synchronous, so that not the next document is loaded
734 // when rescheduling
735 // TODO/LATER: use URLList argument and always remove one document after another, each step in asychronous execution, until finished
736 // but only if reschedule is a problem
737 GetDispatcher_Impl()->Execute( SID_OPENDOC, SFX_CALLMODE_SYNCHRON, *rReq.GetArgs() );
739 // check for special interaction "NO MORE DOCUMENTS ALLOWED" and
740 // break loop then. Otherwise we risk showing the same interaction more then once.
741 if ( pHandler->getInteractionInfo(aInteraction, &aRule) )
743 if (aRule.m_nCallCount > 0)
745 if (aRule.m_xRequest.is())
747 css::task::ErrorCodeRequest aRequest;
748 if (aRule.m_xRequest->getRequest() >>= aRequest)
750 if (aRequest.ErrCode ==
751 sal::static_int_cast< sal_Int32 >(
752 ERRCODE_SFX_NOMOREDOCUMENTSALLOWED))
753 break;
760 pURLList.clear();
761 return;
763 pURLList.clear();
766 sal_Bool bHyperlinkUsed = sal_False;
768 if ( SID_OPENURL == nSID )
770 // SID_OPENURL does the same as SID_OPENDOC!
771 rReq.SetSlot( SID_OPENDOC );
772 nSID = SID_OPENDOC;
774 else if ( nSID == SID_OPENTEMPLATE )
776 rReq.AppendItem( SfxBoolItem( SID_TEMPLATE, sal_False ) );
778 // pass URL to OS by using ShellExecuter or open it internal
779 // if it seams to be an own format.
780 /* Attention!
781 There exist two possibilities to open hyperlinks:
782 a) using SID_OPENHYPERLINK (new)
783 b) using SID_BROWSE (old)
785 else if ( nSID == SID_OPENHYPERLINK )
787 rReq.SetSlot( SID_OPENDOC );
788 nSID = SID_OPENDOC;
789 bHyperlinkUsed = sal_True;
792 // no else here! It's optional ...
793 if (!bHyperlinkUsed)
795 SFX_REQUEST_ARG(rReq, pHyperLinkUsedItem, SfxBoolItem, SID_BROWSE, sal_False);
796 if ( pHyperLinkUsedItem )
797 bHyperlinkUsed = pHyperLinkUsedItem->GetValue();
798 // no "official" item, so remove it from ItemSet before using UNO-API
799 rReq.RemoveItem( SID_BROWSE );
802 SFX_REQUEST_ARG( rReq, pFileName, SfxStringItem, SID_FILE_NAME, sal_False );
803 String aFileName = pFileName->GetValue();
805 String aReferer;
806 SFX_REQUEST_ARG( rReq, pRefererItem, SfxStringItem, SID_REFERER, sal_False );
807 if ( pRefererItem )
808 aReferer = pRefererItem->GetValue();
810 SFX_REQUEST_ARG( rReq, pFileFlagsItem, SfxStringItem, SID_OPTIONS, sal_False);
811 if ( pFileFlagsItem )
813 String aFileFlags = pFileFlagsItem->GetValue();
814 aFileFlags.ToUpperAscii();
815 if ( STRING_NOTFOUND != aFileFlags.Search( 0x0054 ) ) // T = 54h
817 rReq.RemoveItem( SID_TEMPLATE );
818 rReq.AppendItem( SfxBoolItem( SID_TEMPLATE, sal_True ) );
821 if ( STRING_NOTFOUND != aFileFlags.Search( 0x0048 ) ) // H = 48h
823 rReq.RemoveItem( SID_HIDDEN );
824 rReq.AppendItem( SfxBoolItem( SID_HIDDEN, sal_True ) );
827 if ( STRING_NOTFOUND != aFileFlags.Search( 0x0052 ) ) // R = 52h
829 rReq.RemoveItem( SID_DOC_READONLY );
830 rReq.AppendItem( SfxBoolItem( SID_DOC_READONLY, sal_True ) );
833 if ( STRING_NOTFOUND != aFileFlags.Search( 0x0042 ) ) // B = 42h
835 rReq.RemoveItem( SID_PREVIEW );
836 rReq.AppendItem( SfxBoolItem( SID_PREVIEW, sal_True ) );
839 rReq.RemoveItem( SID_OPTIONS );
842 // Mark without URL cannot be handled by hyperlink code
843 if ( bHyperlinkUsed && aFileName.Len() && aFileName.GetChar(0) != '#' )
845 Reference< ::com::sun::star::document::XTypeDetection > xTypeDetection(
846 ::comphelper::getProcessServiceFactory()->createInstance(
847 OUString("com.sun.star.document.TypeDetection")),
848 UNO_QUERY );
849 if ( xTypeDetection.is() )
851 URL aURL;
852 OUString aTypeName;
854 aURL.Complete = aFileName;
855 Reference< util::XURLTransformer > xTrans( util::URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
856 xTrans->parseStrict( aURL );
858 INetProtocol aINetProtocol = INetURLObject( aURL.Complete ).GetProtocol();
859 SvtExtendedSecurityOptions aExtendedSecurityOptions;
860 SvtExtendedSecurityOptions::OpenHyperlinkMode eMode = aExtendedSecurityOptions.GetOpenHyperlinkMode();
862 if ( eMode == SvtExtendedSecurityOptions::OPEN_NEVER && aINetProtocol != INET_PROT_VND_SUN_STAR_HELP )
864 SolarMutexGuard aGuard;
865 Window *pWindow = SFX_APP()->GetTopWindow();
867 WarningBox aSecurityWarningBox( pWindow, SfxResId( RID_SECURITY_WARNING_NO_HYPERLINKS ));
868 aSecurityWarningBox.SetText( SfxResId(RID_SECURITY_WARNING_TITLE).toString() );
869 aSecurityWarningBox.Execute();
870 return;
873 aTypeName = xTypeDetection->queryTypeByURL( aURL.Main );
874 SfxFilterMatcher& rMatcher = SFX_APP()->GetFilterMatcher();
875 const SfxFilter* pFilter = rMatcher.GetFilter4EA( aTypeName );
876 if (!pFilter || !lcl_isFilterNativelySupported(*pFilter))
878 // hyperlink does not link to own type => special handling (http, ftp) browser and (other external protocols) OS
879 if ( aINetProtocol == INET_PROT_MAILTO )
881 // don't dispatch mailto hyperlink to desktop dispatcher
882 rReq.RemoveItem( SID_TARGETNAME );
883 rReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString("_self") ) );
885 else if ( aINetProtocol == INET_PROT_FTP ||
886 aINetProtocol == INET_PROT_HTTP ||
887 aINetProtocol == INET_PROT_HTTPS )
889 sfx2::openUriExternally(aURL.Complete, true);
890 return;
892 else
894 // check for "internal" protocols that should not be forwarded to the system
895 Sequence < OUString > aProtocols(2);
897 // add special protocols that always should be treated as internal
898 aProtocols[0] = OUString("private:*");
899 aProtocols[1] = OUString("vnd.sun.star.*");
903 // get registered protocol handlers from configuration
904 Reference < XNameAccess > xAccess( ::comphelper::ConfigurationHelper::openConfig( ::comphelper::getProcessComponentContext(),
905 OUString("org.openoffice.Office.ProtocolHandler/HandlerSet"), ::comphelper::ConfigurationHelper::E_READONLY ), UNO_QUERY );
906 if ( xAccess.is() )
908 Sequence < OUString > aNames = xAccess->getElementNames();
909 for ( sal_Int32 nName = 0; nName < aNames.getLength(); nName ++)
911 Reference < XPropertySet > xSet;
912 Any aRet = xAccess->getByName( aNames[nName] );
913 aRet >>= xSet;
914 if ( xSet.is() )
916 // copy protocols
917 aRet = xSet->getPropertyValue( OUString("Protocols") );
918 Sequence < OUString > aTmp;
919 aRet >>= aTmp;
921 // todo: add operator+= to SequenceAsVector class and use SequenceAsVector for aProtocols
922 sal_Int32 nLength = aProtocols.getLength();
923 aProtocols.realloc( nLength+aTmp.getLength() );
924 for ( sal_Int32 n=0; n<aTmp.getLength(); n++ )
925 aProtocols[(++nLength)-1] = aTmp[n];
930 catch ( Exception& )
932 // registered protocols could not be read
935 sal_Bool bFound = sal_False;
936 for ( sal_Int32 nProt=0; nProt<aProtocols.getLength(); nProt++ )
938 WildCard aPattern(aProtocols[nProt]);
939 if ( aPattern.Matches( aURL.Complete ) )
941 bFound = sal_True;
942 break;
946 if ( !bFound )
948 sal_Bool bLoadInternal = sal_False;
950 // security reservation: => we have to check the referer before executing
951 if (SFX_APP()->IsSecureURL(OUString(), &aReferer))
955 sfx2::openUriExternally(
956 aURL.Complete, pFilter == 0);
958 catch ( ::com::sun::star::system::SystemShellExecuteException& )
960 rReq.RemoveItem( SID_TARGETNAME );
961 rReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString("_default") ) );
962 bLoadInternal = sal_True;
965 else
967 SfxErrorContext aCtx( ERRCTX_SFX_OPENDOC, aURL.Complete );
968 ErrorHandler::HandleError( ERRCODE_IO_ACCESSDENIED );
971 if ( !bLoadInternal )
972 return;
976 else
978 // hyperlink document must be loaded into a new frame
979 rReq.RemoveItem( SID_TARGETNAME );
980 rReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString("_default") ) );
985 if ( !SFX_APP()->IsSecureURL( INetURLObject(aFileName), &aReferer ) )
987 SfxErrorContext aCtx( ERRCTX_SFX_OPENDOC, aFileName );
988 ErrorHandler::HandleError( ERRCODE_IO_ACCESSDENIED );
989 return;
992 SfxFrame* pTargetFrame = NULL;
993 Reference< XFrame > xTargetFrame;
995 SFX_REQUEST_ARG(rReq, pFrameItem, SfxFrameItem, SID_DOCFRAME, sal_False);
996 if ( pFrameItem )
997 pTargetFrame = pFrameItem->GetFrame();
999 if ( !pTargetFrame )
1001 SFX_REQUEST_ARG(rReq, pUnoFrameItem, SfxUnoFrameItem, SID_FILLFRAME, sal_False);
1002 if ( pUnoFrameItem )
1003 xTargetFrame = pUnoFrameItem->GetFrame();
1006 if ( !pTargetFrame && !xTargetFrame.is() && SfxViewFrame::Current() )
1007 pTargetFrame = &SfxViewFrame::Current()->GetFrame();
1009 // check if caller has set a callback
1010 SFX_REQUEST_ARG(rReq, pLinkItem, SfxLinkItem, SID_DONELINK, sal_False );
1012 // remove from Itemset, because it confuses the parameter transformation
1013 if ( pLinkItem )
1014 pLinkItem = (SfxLinkItem*) pLinkItem->Clone();
1016 rReq.RemoveItem( SID_DONELINK );
1018 // check if the view must be hidden
1019 sal_Bool bHidden = sal_False;
1020 SFX_REQUEST_ARG(rReq, pHidItem, SfxBoolItem, SID_HIDDEN, sal_False);
1021 if ( pHidItem )
1022 bHidden = pHidItem->GetValue();
1024 // This request is a UI call. We have to set the right values inside the MediaDescriptor
1025 // for: InteractionHandler, StatusIndicator, MacroExecutionMode and DocTemplate.
1026 // But we have to look for already existing values or for real hidden requests.
1027 SFX_REQUEST_ARG(rReq, pPreviewItem, SfxBoolItem, SID_PREVIEW, sal_False);
1028 if (!bHidden && ( !pPreviewItem || !pPreviewItem->GetValue() ) )
1030 SFX_REQUEST_ARG(rReq, pInteractionItem, SfxUnoAnyItem, SID_INTERACTIONHANDLER, sal_False);
1031 SFX_REQUEST_ARG(rReq, pMacroExecItem , SfxUInt16Item, SID_MACROEXECMODE , sal_False);
1032 SFX_REQUEST_ARG(rReq, pDocTemplateItem, SfxUInt16Item, SID_UPDATEDOCMODE , sal_False);
1034 if (!pInteractionItem)
1036 Reference < task::XInteractionHandler2 > xHdl = task::InteractionHandler::createWithParent( ::comphelper::getProcessComponentContext(), 0 );
1037 rReq.AppendItem( SfxUnoAnyItem(SID_INTERACTIONHANDLER,::com::sun::star::uno::makeAny(xHdl)) );
1039 if (!pMacroExecItem)
1040 rReq.AppendItem( SfxUInt16Item(SID_MACROEXECMODE,::com::sun::star::document::MacroExecMode::USE_CONFIG) );
1041 if (!pDocTemplateItem)
1042 rReq.AppendItem( SfxUInt16Item(SID_UPDATEDOCMODE,::com::sun::star::document::UpdateDocMode::ACCORDING_TO_CONFIG) );
1045 // extract target name
1046 OUString aTarget;
1047 SFX_REQUEST_ARG(rReq, pTargetItem, SfxStringItem, SID_TARGETNAME, sal_False);
1048 if ( pTargetItem )
1049 aTarget = pTargetItem->GetValue();
1050 else
1052 SFX_REQUEST_ARG( rReq, pNewViewItem, SfxBoolItem, SID_OPEN_NEW_VIEW, sal_False );
1053 if ( pNewViewItem && pNewViewItem->GetValue() )
1054 aTarget = OUString("_blank" );
1057 if ( bHidden )
1059 aTarget = OUString("_blank");
1060 DBG_ASSERT( rReq.IsSynchronCall() || pLinkItem, "Hidden load process must be done synchronously!" );
1063 Reference < XController > xController;
1064 // if a frame is given, it must be used for the starting point of the targetting mechanism
1065 // this code is also used if asynchronous loading is possible, because loadComponent always is synchron
1066 if ( !xTargetFrame.is() )
1068 if ( pTargetFrame )
1070 xTargetFrame = pTargetFrame->GetFrameInterface();
1072 else
1074 xTargetFrame.set( Desktop::create(::comphelper::getProcessComponentContext()), UNO_QUERY );
1078 // make URL ready
1079 SFX_REQUEST_ARG( rReq, pURLItem, SfxStringItem, SID_FILE_NAME, sal_False );
1080 aFileName = pURLItem->GetValue();
1081 if( aFileName.Len() && aFileName.GetChar(0) == '#' ) // Mark without URL
1083 SfxViewFrame *pView = pTargetFrame ? pTargetFrame->GetCurrentViewFrame() : 0;
1084 if ( !pView )
1085 pView = SfxViewFrame::Current();
1086 pView->GetViewShell()->JumpToMark( aFileName.Copy(1) );
1087 rReq.SetReturnValue( SfxViewFrameItem( 0, pView ) );
1088 return;
1091 // convert items to properties for framework API calls
1092 Sequence < PropertyValue > aArgs;
1093 TransformItems( SID_OPENDOC, *rReq.GetArgs(), aArgs );
1095 // TODO/LATER: either remove LinkItem or create an asynchronous process for it
1096 if( bHidden || pLinkItem || rReq.IsSynchronCall() )
1098 // if loading must be done synchron, we must wait for completion to get a return value
1099 // find frame by myself; I must konw the exact frame to get the controller for the return value from it
1100 Reference < XComponent > xComp;
1104 xComp = ::comphelper::SynchronousDispatch::dispatch( xTargetFrame, aFileName, aTarget, 0, aArgs );
1106 catch(const RuntimeException&)
1108 throw;
1110 catch(const ::com::sun::star::uno::Exception&)
1114 Reference < XModel > xModel( xComp, UNO_QUERY );
1115 if ( xModel.is() )
1116 xController = xModel->getCurrentController();
1117 else
1118 xController = Reference < XController >( xComp, UNO_QUERY );
1121 else
1123 URL aURL;
1124 aURL.Complete = aFileName;
1125 Reference< util::XURLTransformer > xTrans( util::URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
1126 xTrans->parseStrict( aURL );
1128 Reference < XDispatchProvider > xProv( xTargetFrame, UNO_QUERY );
1129 Reference < XDispatch > xDisp = xProv.is() ? xProv->queryDispatch( aURL, aTarget, FrameSearchFlag::ALL ) : Reference < XDispatch >();;
1130 RTL_LOGFILE_PRODUCT_CONTEXT( aLog2, "PERFORMANCE - SfxApplication::OpenDocExec_Impl" );
1131 if ( xDisp.is() )
1132 xDisp->dispatch( aURL, aArgs );
1135 if ( xController.is() )
1137 // try to find the SfxFrame for the controller
1138 SfxFrame* pCntrFrame = NULL;
1139 for ( SfxViewShell* pShell = SfxViewShell::GetFirst( 0, sal_False ); pShell; pShell = SfxViewShell::GetNext( *pShell, 0, sal_False ) )
1141 if ( pShell->GetController() == xController )
1143 pCntrFrame = &pShell->GetViewFrame()->GetFrame();
1144 break;
1148 if ( pCntrFrame )
1150 SfxObjectShell* pSh = pCntrFrame->GetCurrentDocument();
1151 DBG_ASSERT( pSh, "Controller without ObjectShell ?!" );
1153 rReq.SetReturnValue( SfxViewFrameItem( 0, pCntrFrame->GetCurrentViewFrame() ) );
1155 if ( bHidden )
1156 pSh->RestoreNoDelete();
1160 if ( pLinkItem )
1162 SfxPoolItem* pRet = rReq.GetReturnValue()->Clone();
1163 pLinkItem->GetValue().Call(pRet);
1164 delete pLinkItem;
1168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */