Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / appl / appopen.cxx
blobbdba6e2f5bd298eea5204cdd4b683fba5cfc57be
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/processfactory.hxx>
49 #include <comphelper/storagehelper.hxx>
50 #include <comphelper/string.hxx>
51 #include <comphelper/synchronousdispatch.hxx>
53 #include <vcl/wrkwin.hxx>
54 #include <svl/intitem.hxx>
55 #include <vcl/layout.hxx>
56 #include <svl/stritem.hxx>
57 #include <svl/eitem.hxx>
58 #include <sfx2/doctempl.hxx>
59 #include <svtools/sfxecode.hxx>
60 #include <framework/preventduplicateinteraction.hxx>
61 #include <svtools/ehdl.hxx>
62 #include <basic/sbxobj.hxx>
63 #include <svl/urihelper.hxx>
64 #include <unotools/localfilehelper.hxx>
65 #include <unotools/pathoptions.hxx>
66 #include <unotools/moduleoptions.hxx>
67 #include <svtools/miscopt.hxx>
68 #include <osl/file.hxx>
69 #include <unotools/extendedsecurityoptions.hxx>
70 #include <comphelper/docpasswordhelper.hxx>
71 #include <vcl/svapp.hxx>
73 #include <osl/mutex.hxx>
76 #include <sfx2/app.hxx>
77 #include <sfx2/bindings.hxx>
78 #include <sfx2/dispatch.hxx>
79 #include <sfx2/docfile.hxx>
80 #include <sfx2/fcontnr.hxx>
81 #include <sfx2/new.hxx>
82 #include <sfx2/objitem.hxx>
83 #include <sfx2/objsh.hxx>
84 #include <svl/slstitm.hxx>
85 #include "appopen.hxx"
86 #include "objshimp.hxx"
87 #include "openflag.hxx"
88 #include <sfx2/passwd.hxx>
89 #include <sfx2/request.hxx>
90 #include <sfx2/sfxresid.hxx>
91 #include <sfx2/viewsh.hxx>
92 #include "app.hrc"
93 #include <sfx2/viewfrm.hxx>
94 #include <sfx2/sfxuno.hxx>
95 #include <sfx2/objface.hxx>
96 #include <sfx2/filedlghelper.hxx>
97 #include <sfx2/docfac.hxx>
98 #include <sfx2/event.hxx>
99 #include <sfx2/templatedlg.hxx>
100 #include "openuriexternally.hxx"
102 #include <officecfg/Office/ProtocolHandler.hxx>
104 using namespace ::com::sun::star;
105 using namespace ::com::sun::star::beans;
106 using namespace ::com::sun::star::frame;
107 using namespace ::com::sun::star::lang;
108 using namespace ::com::sun::star::uno;
109 using namespace ::com::sun::star::util;
110 using namespace ::com::sun::star::task;
111 using namespace ::com::sun::star::container;
112 using namespace ::cppu;
113 using namespace ::sfx2;
115 void SetTemplate_Impl( const OUString &rFileName,
116 const OUString &rLongName,
117 SfxObjectShell *pDoc)
119 // write TemplateName to DocumentProperties of document
120 // TemplateDate stays as default (=current date)
121 pDoc->ResetFromTemplate( rLongName, rFileName );
125 class SfxDocPasswordVerifier : public ::comphelper::IDocPasswordVerifier
127 public:
128 inline explicit SfxDocPasswordVerifier( const Reference< embed::XStorage >& rxStorage ) :
129 mxStorage( rxStorage ) {}
131 virtual ::comphelper::DocPasswordVerifierResult
132 verifyPassword( const OUString& rPassword, uno::Sequence< beans::NamedValue >& o_rEncryptionData ) SAL_OVERRIDE;
133 virtual ::comphelper::DocPasswordVerifierResult
134 verifyEncryptionData( const uno::Sequence< beans::NamedValue >& rEncryptionData ) SAL_OVERRIDE;
137 private:
138 Reference< embed::XStorage > mxStorage;
142 ::comphelper::DocPasswordVerifierResult SfxDocPasswordVerifier::verifyPassword( const OUString& rPassword, uno::Sequence< beans::NamedValue >& o_rEncryptionData )
144 o_rEncryptionData = ::comphelper::OStorageHelper::CreatePackageEncryptionData( rPassword );
145 return verifyEncryptionData( o_rEncryptionData );
150 ::comphelper::DocPasswordVerifierResult SfxDocPasswordVerifier::verifyEncryptionData( const uno::Sequence< beans::NamedValue >& rEncryptionData )
152 ::comphelper::DocPasswordVerifierResult eResult = ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
155 // check the encryption data
156 // if the data correct is the stream will be opened successfully
157 // and immediatelly closed
158 ::comphelper::OStorageHelper::SetCommonStorageEncryptionData( mxStorage, rEncryptionData );
160 mxStorage->openStreamElement(
161 OUString( "content.xml" ),
162 embed::ElementModes::READ | embed::ElementModes::NOCREATE );
164 // no exception -> success
165 eResult = ::comphelper::DocPasswordVerifierResult_OK;
167 catch( const packages::WrongPasswordException& )
169 eResult = ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
171 catch( const uno::Exception& )
173 // unknown error, report it as wrong password
174 // TODO/LATER: we need an additional way to report unknown problems in this case
175 eResult = ::comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
177 return eResult;
184 sal_uInt32 CheckPasswd_Impl
186 SfxObjectShell* pDoc,
187 SfxItemPool& /*rPool*/, // Pool, if a Set has to be created
188 SfxMedium* pFile // the Medium and its Password shold be obtained
191 /* [Description]
193 Ask for the password for a medium, only works if it concerns storage.
194 If the password flag is set in the Document Info, then the password is
195 requested through a user dialogue and the set at the Set of the medium.
196 If the set does not exist the it is created.
199 sal_uIntPtr nRet = ERRCODE_NONE;
201 if( ( !pFile->GetFilter() || pFile->IsStorage() ) )
203 uno::Reference< embed::XStorage > xStorage = pFile->GetStorage( true );
204 if( xStorage.is() )
206 uno::Reference< beans::XPropertySet > xStorageProps( xStorage, uno::UNO_QUERY );
207 if ( xStorageProps.is() )
209 bool bIsEncrypted = false;
210 try {
211 xStorageProps->getPropertyValue("HasEncryptedEntries")
212 >>= bIsEncrypted;
213 } catch( uno::Exception& )
215 // TODO/LATER:
216 // the storage either has no encrypted elements or it's just
217 // does not allow to detect it, probably it should be implemented laiter
220 if ( bIsEncrypted )
222 vcl::Window* pWin = pDoc ? pDoc->GetDialogParent( pFile ) : NULL;
223 if ( pWin )
224 pWin->Show();
226 nRet = ERRCODE_SFX_CANTGETPASSWD;
228 SfxItemSet *pSet = pFile->GetItemSet();
229 if( pSet )
231 Reference< ::com::sun::star::task::XInteractionHandler > xInteractionHandler = pFile->GetInteractionHandler();
232 if( xInteractionHandler.is() )
234 // use the comphelper password helper to request a password
235 OUString aPassword;
236 SFX_ITEMSET_ARG( pSet, pPasswordItem, SfxStringItem, SID_PASSWORD, false);
237 if ( pPasswordItem )
238 aPassword = pPasswordItem->GetValue();
240 uno::Sequence< beans::NamedValue > aEncryptionData;
241 SFX_ITEMSET_ARG( pSet, pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, false);
242 if ( pEncryptionDataItem )
243 pEncryptionDataItem->GetValue() >>= aEncryptionData;
245 OUString aDocumentName = INetURLObject( pFile->GetOrigURL() ).GetMainURL( INetURLObject::DECODE_WITH_CHARSET );
247 SfxDocPasswordVerifier aVerifier( xStorage );
248 aEncryptionData = ::comphelper::DocPasswordHelper::requestAndVerifyDocPassword(
249 aVerifier, aEncryptionData, aPassword, xInteractionHandler, aDocumentName, comphelper::DocPasswordRequestType_STANDARD );
251 pSet->ClearItem( SID_PASSWORD );
252 pSet->ClearItem( SID_ENCRYPTIONDATA );
254 if ( aEncryptionData.getLength() > 0 )
256 pSet->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::makeAny( aEncryptionData ) ) );
260 // update the version list of the medium using the new password
261 pFile->GetVersionList();
263 catch( uno::Exception& )
265 // TODO/LATER: set the error code
268 nRet = ERRCODE_NONE;
270 else
271 nRet = ERRCODE_IO_ABORT;
276 else
278 OSL_FAIL( "A storage must implement XPropertySet interface!" );
279 nRet = ERRCODE_SFX_CANTGETPASSWD;
284 return nRet;
290 sal_uIntPtr SfxApplication::LoadTemplate( SfxObjectShellLock& xDoc, const OUString &rFileName, bool bCopy, SfxItemSet* pSet )
292 const SfxFilter* pFilter = NULL;
293 SfxMedium aMedium( rFileName, ( StreamMode::READ | StreamMode::SHARE_DENYNONE ) );
295 if ( !aMedium.GetStorage( false ).is() )
296 aMedium.GetInStream();
298 if ( aMedium.GetError() )
300 delete pSet;
301 return aMedium.GetErrorCode();
304 aMedium.UseInteractionHandler( true );
305 sal_uIntPtr nErr = GetFilterMatcher().GuessFilter( aMedium,&pFilter,SfxFilterFlags::TEMPLATE, SfxFilterFlags::NONE );
306 if ( 0 != nErr)
308 delete pSet;
309 return ERRCODE_SFX_NOTATEMPLATE;
312 if( !pFilter || !pFilter->IsAllowedAsTemplate() )
314 delete pSet;
315 return ERRCODE_SFX_NOTATEMPLATE;
318 if ( pFilter->GetFilterFlags() & SfxFilterFlags::STARONEFILTER )
320 DBG_ASSERT( !xDoc.Is(), "Sorry, not implemented!" );
321 delete pSet;
322 SfxStringItem aName( SID_FILE_NAME, rFileName );
323 SfxStringItem aReferer( SID_REFERER, OUString("private:user") );
324 SfxStringItem aFlags( SID_OPTIONS, OUString("T") );
325 SfxBoolItem aHidden( SID_HIDDEN, true );
326 const SfxPoolItem *pRet = GetDispatcher_Impl()->Execute( SID_OPENDOC, SfxCallMode::SYNCHRON, &aName, &aHidden, &aReferer, &aFlags, 0L );
327 const SfxObjectItem *pObj = PTR_CAST( SfxObjectItem, pRet );
328 if ( pObj )
329 xDoc = const_cast<SfxObjectShell*>(PTR_CAST( SfxObjectShell, pObj->GetShell() ));
330 else
332 const SfxViewFrameItem *pView = PTR_CAST( SfxViewFrameItem, pRet );
333 if ( pView )
335 SfxViewFrame *pFrame = pView->GetFrame();
336 if ( pFrame )
337 xDoc = pFrame->GetObjectShell();
341 if ( !xDoc.Is() )
342 return ERRCODE_SFX_DOLOADFAILED;
344 else
346 if ( !xDoc.Is() )
347 xDoc = SfxObjectShell::CreateObject( pFilter->GetServiceName() );
349 //pMedium takes ownership of pSet
350 SfxMedium *pMedium = new SfxMedium( rFileName, STREAM_STD_READ, pFilter, pSet );
351 if(!xDoc->DoLoad(pMedium))
353 ErrCode nErrCode = xDoc->GetErrorCode();
354 xDoc->DoClose();
355 xDoc.Clear();
356 return nErrCode;
360 if( bCopy )
364 // TODO: introduce error handling
366 uno::Reference< embed::XStorage > xTempStorage = ::comphelper::OStorageHelper::GetTemporaryStorage();
367 if( !xTempStorage.is() )
368 throw uno::RuntimeException();
370 xDoc->GetStorage()->copyToStorage( xTempStorage );
372 if ( !xDoc->DoSaveCompleted( new SfxMedium( xTempStorage, OUString() ) ) )
373 throw uno::RuntimeException();
375 catch( uno::Exception& )
377 xDoc->DoClose();
378 xDoc.Clear();
380 // TODO: transfer correct error outside
381 return ERRCODE_SFX_GENERAL;
384 SetTemplate_Impl( rFileName, OUString(), xDoc );
386 else
387 SetTemplate_Impl( rFileName, OUString(), xDoc );
389 xDoc->SetNoName();
390 xDoc->InvalidateName();
391 xDoc->SetModified(false);
392 xDoc->ResetError();
394 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel > xModel ( xDoc->GetModel(), ::com::sun::star::uno::UNO_QUERY );
395 if ( xModel.is() )
397 SfxItemSet* pNew = xDoc->GetMedium()->GetItemSet()->Clone();
398 pNew->ClearItem( SID_PROGRESS_STATUSBAR_CONTROL );
399 pNew->ClearItem( SID_FILTER_NAME );
400 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs;
401 TransformItems( SID_OPENDOC, *pNew, aArgs );
402 sal_Int32 nLength = aArgs.getLength();
403 aArgs.realloc( nLength + 1 );
404 aArgs[nLength].Name = "Title";
405 aArgs[nLength].Value <<= OUString( xDoc->GetTitle( SFX_TITLE_DETECT ) );
406 xModel->attachResource( OUString(), aArgs );
407 delete pNew;
410 return xDoc->GetErrorCode();
415 void SfxApplication::NewDocDirectExec_Impl( SfxRequest& rReq )
417 SFX_REQUEST_ARG( rReq, pFactoryItem, SfxStringItem, SID_NEWDOCDIRECT, false);
418 OUString aFactName;
419 if ( pFactoryItem )
420 aFactName = pFactoryItem->GetValue();
421 else
422 aFactName = SvtModuleOptions().GetDefaultModuleName();
425 SfxRequest aReq( SID_OPENDOC, SfxCallMode::SYNCHRON, GetPool() );
426 OUString aFact("private:factory/");
427 aFact += aFactName;
428 aReq.AppendItem( SfxStringItem( SID_FILE_NAME, aFact ) );
429 aReq.AppendItem( SfxFrameItem( SID_DOCFRAME, GetFrame() ) );
430 aReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString( "_default" ) ) );
432 // TODO/LATER: Should the other arguments be transferred as well?
433 SFX_REQUEST_ARG( rReq, pDefaultPathItem, SfxStringItem, SID_DEFAULTFILEPATH, false);
434 if ( pDefaultPathItem )
435 aReq.AppendItem( *pDefaultPathItem );
436 SFX_REQUEST_ARG( rReq, pDefaultNameItem, SfxStringItem, SID_DEFAULTFILENAME, false);
437 if ( pDefaultNameItem )
438 aReq.AppendItem( *pDefaultNameItem );
440 SfxGetpApp()->ExecuteSlot( aReq );
441 const SfxViewFrameItem* pItem = PTR_CAST( SfxViewFrameItem, aReq.GetReturnValue() );
442 if ( pItem )
443 rReq.SetReturnValue( SfxFrameItem( 0, pItem->GetFrame() ) );
448 void SfxApplication::NewDocExec_Impl( SfxRequest& rReq )
450 // No Parameter from BASIC only Factory given?
451 SFX_REQUEST_ARG(rReq, pTemplNameItem, SfxStringItem, SID_TEMPLATE_NAME, false);
452 SFX_REQUEST_ARG(rReq, pTemplFileNameItem, SfxStringItem, SID_FILE_NAME, false);
453 SFX_REQUEST_ARG(rReq, pTemplRegionNameItem, SfxStringItem, SID_TEMPLATE_REGIONNAME, false);
455 SfxObjectShellLock xDoc;
457 OUString aTemplateRegion, aTemplateName, aTemplateFileName;
458 bool bDirect = false; // through FileName instead of Region/Template
459 SfxErrorContext aEc(ERRCTX_SFX_NEWDOC);
460 if ( !pTemplNameItem && !pTemplFileNameItem )
462 bool bNewWin = false;
463 vcl::Window* pTopWin = GetTopWindow();
465 ScopedVclPtrInstance< SfxTemplateManagerDlg > aTemplDlg;
466 int nRet = aTemplDlg->Execute();
467 if ( nRet == RET_OK )
469 rReq.Done();
470 if ( pTopWin != GetTopWindow() )
472 // the dialogue opens a document -> a new TopWindow appears
473 pTopWin = GetTopWindow();
474 bNewWin = true;
478 if ( bNewWin && pTopWin )
479 // after the destruction of the dialogue its parent comes to top,
480 // but we want that the new document is on top
481 pTopWin->ToTop();
483 return;
485 else
487 // Template-Name
488 if ( pTemplNameItem )
489 aTemplateName = pTemplNameItem->GetValue();
491 // Template-Region
492 if ( pTemplRegionNameItem )
493 aTemplateRegion = pTemplRegionNameItem->GetValue();
495 // Template-File-Name
496 if ( pTemplFileNameItem )
498 aTemplateFileName = pTemplFileNameItem->GetValue();
499 bDirect = true;
503 sal_uIntPtr lErr = 0;
504 SfxItemSet* pSet = new SfxAllItemSet( GetPool() );
505 pSet->Put( SfxBoolItem( SID_TEMPLATE, true ) );
506 if ( !bDirect )
508 SfxDocumentTemplates aTmpFac;
509 if( aTemplateFileName.isEmpty() )
510 aTmpFac.GetFull( aTemplateRegion, aTemplateName, aTemplateFileName );
512 if( aTemplateFileName.isEmpty() )
513 lErr = ERRCODE_SFX_TEMPLATENOTFOUND;
516 INetURLObject aObj( aTemplateFileName );
517 SfxErrorContext aEC( ERRCTX_SFX_LOADTEMPLATE, aObj.PathToFileName() );
519 if ( lErr != ERRCODE_NONE )
521 sal_uIntPtr lFatalErr = ERRCODE_TOERROR(lErr);
522 if ( lFatalErr )
523 ErrorHandler::HandleError(lErr);
525 else
527 SfxCallMode eMode = SfxCallMode::SYNCHRON;
529 const SfxPoolItem *pRet=0;
530 SfxStringItem aReferer( SID_REFERER, "private:user" );
531 SfxStringItem aTarget( SID_TARGETNAME, "_default" );
532 if ( !aTemplateFileName.isEmpty() )
534 DBG_ASSERT( aObj.GetProtocol() != INetProtocol::NotValid, "Illegal URL!" );
536 SfxStringItem aName( SID_FILE_NAME, aObj.GetMainURL( INetURLObject::NO_DECODE ) );
537 SfxStringItem aTemplName( SID_TEMPLATE_NAME, aTemplateName );
538 SfxStringItem aTemplRegionName( SID_TEMPLATE_REGIONNAME, aTemplateRegion );
539 pRet = GetDispatcher_Impl()->Execute( SID_OPENDOC, eMode, &aName, &aTarget, &aReferer, &aTemplName, &aTemplRegionName, 0L );
541 else
543 SfxStringItem aName( SID_FILE_NAME, "private:factory" );
544 pRet = GetDispatcher_Impl()->Execute( SID_OPENDOC, eMode, &aName, &aTarget, &aReferer, 0L );
547 if ( pRet )
548 rReq.SetReturnValue( *pRet );
554 namespace {
557 * Check if a given filter type should open the hyperlinked document
558 * natively.
560 * @param rFilter filter object
562 bool lcl_isFilterNativelySupported(const SfxFilter& rFilter)
564 if (rFilter.IsOwnFormat())
565 return true;
567 OUString aName = rFilter.GetFilterName();
568 if (aName.startsWith("MS Excel"))
569 // We can handle all Excel variants natively.
570 return true;
572 return false;
577 void SfxApplication::OpenDocExec_Impl( SfxRequest& rReq )
579 OUString aDocService;
580 SFX_REQUEST_ARG(rReq, pDocSrvItem, SfxStringItem, SID_DOC_SERVICE, false);
581 if (pDocSrvItem)
582 aDocService = pDocSrvItem->GetValue();
584 sal_uInt16 nSID = rReq.GetSlot();
585 SFX_REQUEST_ARG( rReq, pFileNameItem, SfxStringItem, SID_FILE_NAME, false );
586 if ( pFileNameItem )
588 OUString aCommand( pFileNameItem->GetValue() );
589 const SfxSlot* pSlot = GetInterface()->GetSlot( aCommand );
590 if ( pSlot )
592 pFileNameItem = NULL;
594 else
596 if ( aCommand.startsWith("slot:") )
598 sal_uInt16 nSlotId = (sal_uInt16) aCommand.copy(5).toInt32();
599 if ( nSlotId == SID_OPENDOC )
600 pFileNameItem = NULL;
605 if ( !pFileNameItem )
607 // get FileName from dialog
608 std::vector<OUString> pURLList;
609 OUString aFilter;
610 SfxItemSet* pSet = NULL;
611 OUString aPath;
612 SFX_REQUEST_ARG( rReq, pFolderNameItem, SfxStringItem, SID_PATH, false );
613 if ( pFolderNameItem )
614 aPath = pFolderNameItem->GetValue();
615 else if ( nSID == SID_OPENTEMPLATE )
617 aPath = SvtPathOptions().GetTemplatePath();
618 sal_Int32 nTokenCount = comphelper::string::getTokenCount(aPath, ';');
619 aPath = aPath.getToken( nTokenCount ? ( nTokenCount - 1 ) : 0 , ';' );
622 sal_Int16 nDialog = SFX2_IMPL_DIALOG_CONFIG;
623 SFX_REQUEST_ARG( rReq, pSystemDialogItem, SfxBoolItem, SID_FILE_DIALOG, false );
624 if ( pSystemDialogItem )
625 nDialog = pSystemDialogItem->GetValue() ? SFX2_IMPL_DIALOG_SYSTEM : SFX2_IMPL_DIALOG_OOO;
627 OUString sStandardDir;
629 SFX_REQUEST_ARG( rReq, pStandardDirItem, SfxStringItem, SID_STANDARD_DIR, false );
630 if ( pStandardDirItem )
631 sStandardDir = pStandardDirItem->GetValue();
633 ::com::sun::star::uno::Sequence< OUString > aBlackList;
635 SFX_REQUEST_ARG( rReq, pBlackListItem, SfxStringListItem, SID_BLACK_LIST, false );
636 if ( pBlackListItem )
637 pBlackListItem->GetStringList( aBlackList );
640 sal_uIntPtr nErr = sfx2::FileOpenDialog_Impl(
641 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION,
642 SFXWB_MULTISELECTION, OUString(), pURLList,
643 aFilter, pSet, &aPath, nDialog, sStandardDir, aBlackList );
645 if ( nErr == ERRCODE_ABORT )
647 pURLList.clear();
648 return;
651 rReq.SetArgs( *static_cast<SfxAllItemSet*>(pSet) );
652 if ( !aFilter.isEmpty() )
653 rReq.AppendItem( SfxStringItem( SID_FILTER_NAME, aFilter ) );
654 rReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString("_default") ) );
655 rReq.AppendItem( SfxStringItem( SID_REFERER, "private:user" ) );
656 delete pSet;
658 if(!pURLList.empty())
660 if ( nSID == SID_OPENTEMPLATE )
661 rReq.AppendItem( SfxBoolItem( SID_TEMPLATE, false ) );
663 // This helper wraps an existing (or may new created InteractionHandler)
664 // intercept all incoming interactions and provide useful information
665 // later if the following transaction was finished.
667 ::framework::PreventDuplicateInteraction* pHandler = new ::framework::PreventDuplicateInteraction(::comphelper::getProcessComponentContext());
668 css::uno::Reference< css::task::XInteractionHandler > xHandler (static_cast< css::task::XInteractionHandler* >(pHandler), css::uno::UNO_QUERY);
669 css::uno::Reference< css::task::XInteractionHandler > xWrappedHandler;
671 // wrap existing handler or create new UUI handler
672 SFX_REQUEST_ARG(rReq, pInteractionItem, SfxUnoAnyItem, SID_INTERACTIONHANDLER, false);
673 if (pInteractionItem)
675 pInteractionItem->GetValue() >>= xWrappedHandler;
676 rReq.RemoveItem( SID_INTERACTIONHANDLER );
678 if (xWrappedHandler.is())
679 pHandler->setHandler(xWrappedHandler);
680 else
681 pHandler->useDefaultUUIHandler();
682 rReq.AppendItem( SfxUnoAnyItem(SID_INTERACTIONHANDLER,::com::sun::star::uno::makeAny(xHandler)) );
684 // define rules for this handler
685 css::uno::Type aInteraction = ::cppu::UnoType<css::task::ErrorCodeRequest>::get();
686 ::framework::PreventDuplicateInteraction::InteractionInfo aRule (aInteraction, 1);
687 pHandler->addInteractionRule(aRule);
689 if (!aDocService.isEmpty())
691 rReq.RemoveItem(SID_DOC_SERVICE);
692 rReq.AppendItem(SfxStringItem(SID_DOC_SERVICE, aDocService));
695 for(std::vector<OUString>::const_iterator i = pURLList.begin(); i != pURLList.end(); ++i)
697 rReq.RemoveItem( SID_FILE_NAME );
698 rReq.AppendItem( SfxStringItem( SID_FILE_NAME, *i ) );
700 // Run synchronous, so that not the next document is loaded
701 // when rescheduling
702 // TODO/LATER: use URLList argument and always remove one document after another, each step in asychronous execution, until finished
703 // but only if reschedule is a problem
704 GetDispatcher_Impl()->Execute( SID_OPENDOC, SfxCallMode::SYNCHRON, *rReq.GetArgs() );
706 // check for special interaction "NO MORE DOCUMENTS ALLOWED" and
707 // break loop then. Otherwise we risk showing the same interaction more than once.
708 if ( pHandler->getInteractionInfo(aInteraction, &aRule) )
710 if (aRule.m_nCallCount > 0)
712 if (aRule.m_xRequest.is())
714 css::task::ErrorCodeRequest aRequest;
715 if (aRule.m_xRequest->getRequest() >>= aRequest)
717 if (aRequest.ErrCode ==
718 sal::static_int_cast< sal_Int32 >(
719 ERRCODE_SFX_NOMOREDOCUMENTSALLOWED))
720 break;
727 pURLList.clear();
728 return;
730 pURLList.clear();
733 bool bHyperlinkUsed = false;
735 if ( SID_OPENURL == nSID )
737 // SID_OPENURL does the same as SID_OPENDOC!
738 rReq.SetSlot( SID_OPENDOC );
739 nSID = SID_OPENDOC;
741 else if ( nSID == SID_OPENTEMPLATE )
743 rReq.AppendItem( SfxBoolItem( SID_TEMPLATE, false ) );
745 // pass URL to OS by using ShellExecuter or open it internal
746 // if it seems to be an own format.
747 /* Attention!
748 There exist two possibilities to open hyperlinks:
749 a) using SID_OPENHYPERLINK (new)
750 b) using SID_BROWSE (old)
752 else if ( nSID == SID_OPENHYPERLINK )
754 rReq.SetSlot( SID_OPENDOC );
755 nSID = SID_OPENDOC;
756 bHyperlinkUsed = true;
759 // no else here! It's optional ...
760 if (!bHyperlinkUsed)
762 SFX_REQUEST_ARG(rReq, pHyperLinkUsedItem, SfxBoolItem, SID_BROWSE, false);
763 if ( pHyperLinkUsedItem )
764 bHyperlinkUsed = pHyperLinkUsedItem->GetValue();
765 // no "official" item, so remove it from ItemSet before using UNO-API
766 rReq.RemoveItem( SID_BROWSE );
769 SFX_REQUEST_ARG( rReq, pFileName, SfxStringItem, SID_FILE_NAME, false );
770 OUString aFileName = pFileName->GetValue();
772 OUString aReferer;
773 SFX_REQUEST_ARG( rReq, pRefererItem, SfxStringItem, SID_REFERER, false );
774 if ( pRefererItem )
775 aReferer = pRefererItem->GetValue();
777 SFX_REQUEST_ARG( rReq, pFileFlagsItem, SfxStringItem, SID_OPTIONS, false);
778 if ( pFileFlagsItem )
780 OUString aFileFlags = pFileFlagsItem->GetValue();
781 aFileFlags = aFileFlags.toAsciiUpperCase();
782 if ( -1 != aFileFlags.indexOf( 0x0054 ) ) // T = 54h
784 rReq.RemoveItem( SID_TEMPLATE );
785 rReq.AppendItem( SfxBoolItem( SID_TEMPLATE, true ) );
788 if ( -1 != aFileFlags.indexOf( 0x0048 ) ) // H = 48h
790 rReq.RemoveItem( SID_HIDDEN );
791 rReq.AppendItem( SfxBoolItem( SID_HIDDEN, true ) );
794 if ( -1 != aFileFlags.indexOf( 0x0052 ) ) // R = 52h
796 rReq.RemoveItem( SID_DOC_READONLY );
797 rReq.AppendItem( SfxBoolItem( SID_DOC_READONLY, true ) );
800 if ( -1 != aFileFlags.indexOf( 0x0042 ) ) // B = 42h
802 rReq.RemoveItem( SID_PREVIEW );
803 rReq.AppendItem( SfxBoolItem( SID_PREVIEW, true ) );
806 rReq.RemoveItem( SID_OPTIONS );
809 // Mark without URL cannot be handled by hyperlink code
810 if ( bHyperlinkUsed && !aFileName.isEmpty() && aFileName[0] != '#' )
812 Reference< ::com::sun::star::document::XTypeDetection > xTypeDetection(
813 ::comphelper::getProcessServiceFactory()->createInstance(
814 OUString("com.sun.star.document.TypeDetection")),
815 UNO_QUERY );
816 if ( xTypeDetection.is() )
818 URL aURL;
819 OUString aTypeName;
821 aURL.Complete = aFileName;
822 Reference< util::XURLTransformer > xTrans( util::URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
823 xTrans->parseStrict( aURL );
825 INetProtocol aINetProtocol = INetURLObject( aURL.Complete ).GetProtocol();
826 SvtExtendedSecurityOptions aExtendedSecurityOptions;
827 SvtExtendedSecurityOptions::OpenHyperlinkMode eMode = aExtendedSecurityOptions.GetOpenHyperlinkMode();
829 if ( eMode == SvtExtendedSecurityOptions::OPEN_NEVER && aINetProtocol != INetProtocol::VndSunStarHelp )
831 SolarMutexGuard aGuard;
832 vcl::Window *pWindow = SfxGetpApp()->GetTopWindow();
834 ScopedVclPtrInstance<MessageDialog> aSecurityWarningBox(pWindow,
835 SfxResId(STR_SECURITY_WARNING_NO_HYPERLINKS),
836 VCL_MESSAGE_WARNING);
837 aSecurityWarningBox->SetText( SfxResId(RID_SECURITY_WARNING_TITLE).toString() );
838 aSecurityWarningBox->Execute();
839 return;
842 aTypeName = xTypeDetection->queryTypeByURL( aURL.Main );
843 SfxFilterMatcher& rMatcher = SfxGetpApp()->GetFilterMatcher();
844 const SfxFilter* pFilter = rMatcher.GetFilter4EA( aTypeName );
845 if (!pFilter || !lcl_isFilterNativelySupported(*pFilter))
847 // hyperlink does not link to own type => special handling (http, ftp) browser and (other external protocols) OS
848 if ( aINetProtocol == INetProtocol::Mailto )
850 // don't dispatch mailto hyperlink to desktop dispatcher
851 rReq.RemoveItem( SID_TARGETNAME );
852 rReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString("_self") ) );
854 else if ( aINetProtocol == INetProtocol::Ftp ||
855 aINetProtocol == INetProtocol::Http ||
856 aINetProtocol == INetProtocol::Https )
858 sfx2::openUriExternally(aURL.Complete, true);
859 return;
861 else
863 // check for "internal" protocols that should not be forwarded to the system
864 std::vector < OUString > aProtocols(2);
866 // add special protocols that always should be treated as internal
867 aProtocols[0] = "private:*";
868 aProtocols[1] = "vnd.sun.star.*";
870 // get registered protocol handlers from configuration
871 Reference < XNameAccess > xAccess(officecfg::Office::ProtocolHandler::HandlerSet::get());
872 Sequence < OUString > aNames = xAccess->getElementNames();
873 for ( sal_Int32 nName = 0; nName < aNames.getLength(); nName ++)
875 Reference < XPropertySet > xSet;
876 Any aRet = xAccess->getByName( aNames[nName] );
877 aRet >>= xSet;
878 if ( xSet.is() )
880 // copy protocols
881 aRet = xSet->getPropertyValue("Protocols");
882 Sequence < OUString > aTmp;
883 aRet >>= aTmp;
885 aProtocols.insert(aProtocols.end(),aTmp.begin(),aTmp.end());
889 bool bFound = false;
890 for ( size_t nProt=0; nProt<aProtocols.size(); nProt++ )
892 WildCard aPattern(aProtocols[nProt]);
893 if ( aPattern.Matches( aURL.Complete ) )
895 bFound = true;
896 break;
900 if ( !bFound )
902 bool bLoadInternal = false;
905 sfx2::openUriExternally(
906 aURL.Complete, pFilter == 0);
908 catch ( ::com::sun::star::system::SystemShellExecuteException& )
910 rReq.RemoveItem( SID_TARGETNAME );
911 rReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString("_default") ) );
912 bLoadInternal = true;
914 if ( !bLoadInternal )
915 return;
919 else
921 // hyperlink document must be loaded into a new frame
922 rReq.RemoveItem( SID_TARGETNAME );
923 rReq.AppendItem( SfxStringItem( SID_TARGETNAME, OUString("_default") ) );
928 if (!SvtSecurityOptions().isSecureMacroUri(aFileName, aReferer))
930 SfxErrorContext aCtx( ERRCTX_SFX_OPENDOC, aFileName );
931 ErrorHandler::HandleError( ERRCODE_IO_ACCESSDENIED );
932 return;
935 SfxFrame* pTargetFrame = NULL;
936 Reference< XFrame > xTargetFrame;
938 SFX_REQUEST_ARG(rReq, pFrameItem, SfxFrameItem, SID_DOCFRAME, false);
939 if ( pFrameItem )
940 pTargetFrame = pFrameItem->GetFrame();
942 if ( !pTargetFrame )
944 SFX_REQUEST_ARG(rReq, pUnoFrameItem, SfxUnoFrameItem, SID_FILLFRAME, false);
945 if ( pUnoFrameItem )
946 xTargetFrame = pUnoFrameItem->GetFrame();
949 if ( !pTargetFrame && !xTargetFrame.is() && SfxViewFrame::Current() )
950 pTargetFrame = &SfxViewFrame::Current()->GetFrame();
952 // check if caller has set a callback
953 SFX_REQUEST_ARG(rReq, pLinkItem, SfxLinkItem, SID_DONELINK, false );
955 // remove from Itemset, because it confuses the parameter transformation
956 if ( pLinkItem )
957 pLinkItem = static_cast<SfxLinkItem*>( pLinkItem->Clone() );
959 rReq.RemoveItem( SID_DONELINK );
961 // check if the view must be hidden
962 bool bHidden = false;
963 SFX_REQUEST_ARG(rReq, pHidItem, SfxBoolItem, SID_HIDDEN, false);
964 if ( pHidItem )
965 bHidden = pHidItem->GetValue();
967 // This request is a UI call. We have to set the right values inside the MediaDescriptor
968 // for: InteractionHandler, StatusIndicator, MacroExecutionMode and DocTemplate.
969 // But we have to look for already existing values or for real hidden requests.
970 SFX_REQUEST_ARG(rReq, pPreviewItem, SfxBoolItem, SID_PREVIEW, false);
971 if (!bHidden && ( !pPreviewItem || !pPreviewItem->GetValue() ) )
973 SFX_REQUEST_ARG(rReq, pInteractionItem, SfxUnoAnyItem, SID_INTERACTIONHANDLER, false);
974 SFX_REQUEST_ARG(rReq, pMacroExecItem , SfxUInt16Item, SID_MACROEXECMODE , false);
975 SFX_REQUEST_ARG(rReq, pDocTemplateItem, SfxUInt16Item, SID_UPDATEDOCMODE , false);
977 if (!pInteractionItem)
979 Reference < task::XInteractionHandler2 > xHdl = task::InteractionHandler::createWithParent( ::comphelper::getProcessComponentContext(), 0 );
980 rReq.AppendItem( SfxUnoAnyItem(SID_INTERACTIONHANDLER,::com::sun::star::uno::makeAny(xHdl)) );
982 if (!pMacroExecItem)
983 rReq.AppendItem( SfxUInt16Item(SID_MACROEXECMODE,::com::sun::star::document::MacroExecMode::USE_CONFIG) );
984 if (!pDocTemplateItem)
985 rReq.AppendItem( SfxUInt16Item(SID_UPDATEDOCMODE,::com::sun::star::document::UpdateDocMode::ACCORDING_TO_CONFIG) );
988 // extract target name
989 OUString aTarget;
990 SFX_REQUEST_ARG(rReq, pTargetItem, SfxStringItem, SID_TARGETNAME, false);
991 if ( pTargetItem )
992 aTarget = pTargetItem->GetValue();
993 else
995 SFX_REQUEST_ARG( rReq, pNewViewItem, SfxBoolItem, SID_OPEN_NEW_VIEW, false );
996 if ( pNewViewItem && pNewViewItem->GetValue() )
997 aTarget = "_blank" ;
1000 if ( bHidden )
1002 aTarget = "_blank";
1003 DBG_ASSERT( rReq.IsSynchronCall() || pLinkItem, "Hidden load process must be done synchronously!" );
1006 Reference < XController > xController;
1007 // if a frame is given, it must be used for the starting point of the targeting mechanism
1008 // this code is also used if asynchronous loading is possible, because loadComponent always is synchron
1009 if ( !xTargetFrame.is() )
1011 if ( pTargetFrame )
1013 xTargetFrame = pTargetFrame->GetFrameInterface();
1015 else
1017 xTargetFrame.set( Desktop::create(::comphelper::getProcessComponentContext()), UNO_QUERY );
1021 // make URL ready
1022 SFX_REQUEST_ARG( rReq, pURLItem, SfxStringItem, SID_FILE_NAME, false );
1023 aFileName = pURLItem->GetValue();
1024 if( aFileName.startsWith("#") ) // Mark without URL
1026 SfxViewFrame *pView = pTargetFrame ? pTargetFrame->GetCurrentViewFrame() : 0;
1027 if ( !pView )
1028 pView = SfxViewFrame::Current();
1029 pView->GetViewShell()->JumpToMark( aFileName.copy(1) );
1030 rReq.SetReturnValue( SfxViewFrameItem( 0, pView ) );
1031 return;
1034 // convert items to properties for framework API calls
1035 Sequence < PropertyValue > aArgs;
1036 TransformItems( SID_OPENDOC, *rReq.GetArgs(), aArgs );
1037 // Any Referer (that was relevant in the above call to
1038 // SvtSecurityOptions::isSecureMacroUri) is no longer relevant, assuming
1039 // this "open" request is initiated directly by the user:
1040 for (sal_Int32 i = 0; i != aArgs.getLength(); ++i) {
1041 if (aArgs[i].Name == "Referer") {
1042 ++i;
1043 for (; i != aArgs.getLength(); ++i) {
1044 aArgs[i - 1] = aArgs[i];
1046 aArgs.realloc(aArgs.getLength()-1);
1047 break;
1051 // TODO/LATER: either remove LinkItem or create an asynchronous process for it
1052 if( bHidden || pLinkItem || rReq.IsSynchronCall() )
1054 // if loading must be done synchron, we must wait for completion to get a return value
1055 // find frame by myself; I must know the exact frame to get the controller for the return value from it
1056 Reference < XComponent > xComp;
1060 xComp = ::comphelper::SynchronousDispatch::dispatch( xTargetFrame, aFileName, aTarget, 0, aArgs );
1062 catch(const RuntimeException&)
1064 throw;
1066 catch(const ::com::sun::star::uno::Exception&)
1070 Reference < XModel > xModel( xComp, UNO_QUERY );
1071 if ( xModel.is() )
1072 xController = xModel->getCurrentController();
1073 else
1074 xController = Reference < XController >( xComp, UNO_QUERY );
1077 else
1079 URL aURL;
1080 aURL.Complete = aFileName;
1081 Reference< util::XURLTransformer > xTrans( util::URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
1082 xTrans->parseStrict( aURL );
1084 Reference < XDispatchProvider > xProv( xTargetFrame, UNO_QUERY );
1085 Reference < XDispatch > xDisp = xProv.is() ? xProv->queryDispatch( aURL, aTarget, FrameSearchFlag::ALL ) : Reference < XDispatch >();;
1086 if ( xDisp.is() )
1087 xDisp->dispatch( aURL, aArgs );
1090 if ( xController.is() )
1092 // try to find the SfxFrame for the controller
1093 SfxFrame* pCntrFrame = NULL;
1094 for ( SfxViewShell* pShell = SfxViewShell::GetFirst( 0, false ); pShell; pShell = SfxViewShell::GetNext( *pShell, 0, false ) )
1096 if ( pShell->GetController() == xController )
1098 pCntrFrame = &pShell->GetViewFrame()->GetFrame();
1099 break;
1103 if ( pCntrFrame )
1105 SfxObjectShell* pSh = pCntrFrame->GetCurrentDocument();
1106 DBG_ASSERT( pSh, "Controller without ObjectShell ?!" );
1108 rReq.SetReturnValue( SfxViewFrameItem( 0, pCntrFrame->GetCurrentViewFrame() ) );
1110 if ( bHidden )
1111 pSh->RestoreNoDelete();
1115 if (pLinkItem)
1117 const SfxPoolItem* pRetValue = rReq.GetReturnValue();
1118 if (pRetValue)
1120 SfxPoolItem* pClone = pRetValue->Clone();
1121 pLinkItem->GetValue().Call(pClone);
1123 delete pLinkItem;
1127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */