1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <svtools/openfiledroptargetlistener.hxx>
22 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
23 #include <com/sun/star/frame/XDispatch.hpp>
24 #include <com/sun/star/frame/XDispatchProvider.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/util/URLTransformer.hpp>
27 #include <com/sun/star/util/XURLTransformer.hpp>
29 #include <svtools/transfer.hxx>
30 #include <unotools/localfilehelper.hxx>
31 #include <sot/filelist.hxx>
32 #include <comphelper/processfactory.hxx>
34 #include <osl/file.hxx>
35 #include <vcl/svapp.hxx>
37 // Create a new task or recycle an existing one
38 const char SPECIALTARGET_DEFAULT
[] = "_default";
40 OpenFileDropTargetListener::OpenFileDropTargetListener( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
41 const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
42 : m_xContext ( xContext
)
43 , m_xTargetFrame ( xFrame
)
44 , m_pFormats ( new DataFlavorExVector
)
50 OpenFileDropTargetListener::~OpenFileDropTargetListener()
52 m_xTargetFrame
.clear();
60 void SAL_CALL
OpenFileDropTargetListener::disposing( const css::lang::EventObject
& ) throw( css::uno::RuntimeException
, std::exception
)
62 m_xTargetFrame
.clear();
68 void SAL_CALL
OpenFileDropTargetListener::drop( const css::datatransfer::dnd::DropTargetDropEvent
& dtde
) throw( css::uno::RuntimeException
, std::exception
)
70 const sal_Int8 nAction
= dtde
.DropAction
;
74 if ( css::datatransfer::dnd::DNDConstants::ACTION_NONE
!= nAction
)
76 TransferableDataHelper
aHelper( dtde
.Transferable
);
77 bool bFormatFound
= false;
80 // at first check filelist format
81 if ( aHelper
.GetFileList( SotClipboardFormatId::FILE_LIST
, aFileList
) )
83 sal_uLong i
, nCount
= aFileList
.Count();
84 for ( i
= 0; i
< nCount
; ++i
)
85 implts_OpenFile( aFileList
.GetFile(i
) );
89 // then, if necessary, the file format
91 if ( !bFormatFound
&& aHelper
.GetString( SotClipboardFormatId::SIMPLE_FILE
, aFilePath
) )
92 implts_OpenFile( aFilePath
);
94 dtde
.Context
->dropComplete( css::datatransfer::dnd::DNDConstants::ACTION_NONE
!= nAction
);
96 catch( const ::com::sun::star::uno::Exception
& )
103 void SAL_CALL
OpenFileDropTargetListener::dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent
& dtdee
) throw( css::uno::RuntimeException
, std::exception
)
107 implts_BeginDrag( dtdee
.SupportedDataFlavors
);
109 catch( const ::com::sun::star::uno::Exception
& )
118 void SAL_CALL
OpenFileDropTargetListener::dragExit( const css::datatransfer::dnd::DropTargetEvent
& ) throw( css::uno::RuntimeException
, std::exception
)
124 catch( const ::com::sun::star::uno::Exception
& )
131 void SAL_CALL
OpenFileDropTargetListener::dragOver( const css::datatransfer::dnd::DropTargetDragEvent
& dtde
) throw( css::uno::RuntimeException
, std::exception
)
135 bool bAccept
= ( implts_IsDropFormatSupported( SotClipboardFormatId::SIMPLE_FILE
) ||
136 implts_IsDropFormatSupported( SotClipboardFormatId::FILE_LIST
) );
139 dtde
.Context
->rejectDrag();
141 dtde
.Context
->acceptDrag( css::datatransfer::dnd::DNDConstants::ACTION_COPY
);
143 catch( const ::com::sun::star::uno::Exception
& )
150 void SAL_CALL
OpenFileDropTargetListener::dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent
& ) throw( css::uno::RuntimeException
, std::exception
)
154 void OpenFileDropTargetListener::implts_BeginDrag( const css::uno::Sequence
< css::datatransfer::DataFlavor
>& rSupportedDataFlavors
)
157 SolarMutexGuard aGuard
;
160 TransferableDataHelper::FillDataFlavorExVector(rSupportedDataFlavors
,*m_pFormats
);
164 void OpenFileDropTargetListener::implts_EndDrag()
167 SolarMutexGuard aGuard
;
173 bool OpenFileDropTargetListener::implts_IsDropFormatSupported( SotClipboardFormatId nFormat
)
176 SolarMutexGuard aGuard
;
178 DataFlavorExVector::iterator
aIter( m_pFormats
->begin() ), aEnd( m_pFormats
->end() );
181 while ( aIter
!= aEnd
)
183 if ( nFormat
== (*aIter
++).mnSotId
)
194 void OpenFileDropTargetListener::implts_OpenFile( const OUString
& rFilePath
)
197 if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFilePath
, aFileURL
) )
198 aFileURL
= rFilePath
;
200 ::osl::FileStatus
aStatus( osl_FileStatus_Mask_FileURL
);
201 ::osl::DirectoryItem aItem
;
202 if( ::osl::FileBase::E_None
== ::osl::DirectoryItem::get( aFileURL
, aItem
) &&
203 ::osl::FileBase::E_None
== aItem
.getFileStatus( aStatus
) )
204 aFileURL
= aStatus
.getFileURL();
208 SolarMutexGuard aGuard
;
210 css::uno::Reference
< css::frame::XFrame
> xTargetFrame( m_xTargetFrame
.get(), css::uno::UNO_QUERY
);
211 css::uno::Reference
< css::util::XURLTransformer
> xParser ( css::util::URLTransformer::create(m_xContext
) );
213 if (xTargetFrame
.is() && xParser
.is())
216 aURL
.Complete
= aFileURL
;
217 xParser
->parseStrict(aURL
);
219 css::uno::Reference
< css::frame::XDispatchProvider
> xProvider( xTargetFrame
, css::uno::UNO_QUERY
);
220 css::uno::Reference
< css::frame::XDispatch
> xDispatcher
= xProvider
->queryDispatch( aURL
, SPECIALTARGET_DEFAULT
, 0 );
221 if ( xDispatcher
.is() )
222 xDispatcher
->dispatch( aURL
, css::uno::Sequence
< css::beans::PropertyValue
>() );
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */