merged tag ooo/OOO330_m14
[LibreOffice.git] / framework / source / classes / droptargetlistener.cxx
blob69226cba3162e0662dac416662a1aa196a8a3905
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
31 //_________________________________________________________________________________________________________________
32 // my own includes
33 //_________________________________________________________________________________________________________________
34 #include <classes/droptargetlistener.hxx>
35 #include <threadhelp/readguard.hxx>
36 #include <threadhelp/writeguard.hxx>
37 #include <targets.h>
38 #include <services.h>
40 //_________________________________________________________________________________________________________________
41 // interface includes
42 //_________________________________________________________________________________________________________________
43 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
44 #include <com/sun/star/frame/XDispatch.hpp>
45 #include <com/sun/star/frame/XDispatchProvider.hpp>
46 #include <com/sun/star/beans/PropertyValue.hpp>
47 #include <com/sun/star/util/XURLTransformer.hpp>
49 //_________________________________________________________________________________________________________________
50 // includes of other projects
51 //_________________________________________________________________________________________________________________
52 #include <svtools/transfer.hxx>
53 #include <unotools/localfilehelper.hxx>
54 #include <sot/filelist.hxx>
56 #include <osl/file.hxx>
57 #include <vcl/svapp.hxx>
59 //_________________________________________________________________________________________________________________
60 // namespace
61 //_________________________________________________________________________________________________________________
63 namespace framework
66 DropTargetListener::DropTargetListener( const css::uno::Reference< css::lang::XMultiServiceFactory >& xFactory,
67 const css::uno::Reference< css::frame::XFrame >& xFrame )
68 : ThreadHelpBase ( &Application::GetSolarMutex() )
69 , m_xFactory ( xFactory )
70 , m_xTargetFrame ( xFrame )
71 , m_pFormats ( new DataFlavorExVector )
75 // -----------------------------------------------------------------------------
77 DropTargetListener::~DropTargetListener()
79 m_xTargetFrame = css::uno::WeakReference< css::frame::XFrame >();
80 m_xFactory = css::uno::Reference< css::lang::XMultiServiceFactory >();
81 delete m_pFormats;
82 m_pFormats = NULL;
85 // -----------------------------------------------------------------------------
87 void SAL_CALL DropTargetListener::disposing( const css::lang::EventObject& ) throw( css::uno::RuntimeException )
89 m_xTargetFrame = css::uno::WeakReference< css::frame::XFrame >();
90 m_xFactory = css::uno::Reference< css::lang::XMultiServiceFactory >();
93 // -----------------------------------------------------------------------------
95 void SAL_CALL DropTargetListener::drop( const css::datatransfer::dnd::DropTargetDropEvent& dtde ) throw( css::uno::RuntimeException )
97 const sal_Int8 nAction = dtde.DropAction;
99 try
101 if ( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction )
103 TransferableDataHelper aHelper( dtde.Transferable );
104 sal_Bool bFormatFound = sal_False;
105 FileList aFileList;
107 // at first check filelist format
108 if ( aHelper.GetFileList( SOT_FORMAT_FILE_LIST, aFileList ) )
110 ULONG i, nCount = aFileList.Count();
111 for ( i = 0; i < nCount; ++i )
112 implts_OpenFile( aFileList.GetFile(i) );
113 bFormatFound = sal_True;
116 // then, if necessary, the file format
117 String aFilePath;
118 if ( !bFormatFound && aHelper.GetString( SOT_FORMAT_FILE, aFilePath ) )
119 implts_OpenFile( aFilePath );
121 dtde.Context->dropComplete( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction );
123 catch( const ::com::sun::star::uno::Exception& )
128 // -----------------------------------------------------------------------------
130 void SAL_CALL DropTargetListener::dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent& dtdee ) throw( css::uno::RuntimeException )
134 implts_BeginDrag( dtdee.SupportedDataFlavors );
136 catch( const ::com::sun::star::uno::Exception& )
140 dragOver( dtdee );
143 // -----------------------------------------------------------------------------
145 void SAL_CALL DropTargetListener::dragExit( const css::datatransfer::dnd::DropTargetEvent& ) throw( css::uno::RuntimeException )
149 implts_EndDrag();
151 catch( const ::com::sun::star::uno::Exception& )
156 // -----------------------------------------------------------------------------
158 void SAL_CALL DropTargetListener::dragOver( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) throw( css::uno::RuntimeException )
162 sal_Bool bAccept = ( implts_IsDropFormatSupported( SOT_FORMAT_FILE ) ||
163 implts_IsDropFormatSupported( SOT_FORMAT_FILE_LIST ) );
165 if ( !bAccept )
166 dtde.Context->rejectDrag();
167 else
168 dtde.Context->acceptDrag( css::datatransfer::dnd::DNDConstants::ACTION_COPY );
170 catch( const ::com::sun::star::uno::Exception& )
175 // -----------------------------------------------------------------------------
177 void SAL_CALL DropTargetListener::dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent& ) throw( css::uno::RuntimeException )
181 void DropTargetListener::implts_BeginDrag( const css::uno::Sequence< css::datatransfer::DataFlavor >& rSupportedDataFlavors )
183 /* SAFE { */
184 WriteGuard aWriteLock(m_aLock);
185 m_pFormats->clear();
186 TransferableDataHelper::FillDataFlavorExVector(rSupportedDataFlavors,*m_pFormats);
187 aWriteLock.unlock();
188 /* } SAFE */
191 void DropTargetListener::implts_EndDrag()
193 /* SAFE { */
194 WriteGuard aWriteLock(m_aLock);
195 m_pFormats->clear();
196 aWriteLock.unlock();
197 /* } SAFE */
200 sal_Bool DropTargetListener::implts_IsDropFormatSupported( SotFormatStringId nFormat )
202 /* SAFE { */
203 ReadGuard aReadLock(m_aLock);
204 DataFlavorExVector::iterator aIter( m_pFormats->begin() ), aEnd( m_pFormats->end() );
205 sal_Bool bRet = sal_False;
207 while ( aIter != aEnd )
209 if ( nFormat == (*aIter++).mnSotId )
211 bRet = sal_True;
212 aIter = aEnd;
215 aReadLock.unlock();
216 /* } SAFE */
218 return bRet;
221 void DropTargetListener::implts_OpenFile( const String& rFilePath )
223 String aFileURL;
224 if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFilePath, aFileURL ) )
225 aFileURL = rFilePath;
227 ::osl::FileStatus aStatus( FileStatusMask_FileURL );
228 ::osl::DirectoryItem aItem;
229 if( ::osl::FileBase::E_None == ::osl::DirectoryItem::get( aFileURL, aItem ) &&
230 ::osl::FileBase::E_None == aItem.getFileStatus( aStatus ) )
231 aFileURL = aStatus.getFileURL();
233 // open file
234 /* SAFE { */
235 ReadGuard aReadLock(m_aLock);
236 css::uno::Reference< css::frame::XFrame > xTargetFrame( m_xTargetFrame.get() , css::uno::UNO_QUERY );
237 css::uno::Reference< css::util::XURLTransformer > xParser ( m_xFactory->createInstance(SERVICENAME_URLTRANSFORMER), css::uno::UNO_QUERY );
238 aReadLock.unlock();
239 /* } SAFE */
240 if (xTargetFrame.is() && xParser.is())
242 css::util::URL aURL;
243 aURL.Complete = aFileURL;
244 xParser->parseStrict(aURL);
246 css::uno::Reference < css::frame::XDispatchProvider > xProvider( xTargetFrame, css::uno::UNO_QUERY );
247 css::uno::Reference< css::frame::XDispatch > xDispatcher = xProvider->queryDispatch( aURL, SPECIALTARGET_DEFAULT, 0 );
248 if ( xDispatcher.is() )
249 xDispatcher->dispatch( aURL, css::uno::Sequence < css::beans::PropertyValue >() );