1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: droptargetlistener.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
37 #include <classes/droptargetlistener.hxx>
38 #include <threadhelp/readguard.hxx>
39 #include <threadhelp/writeguard.hxx>
43 //_________________________________________________________________________________________________________________
45 //_________________________________________________________________________________________________________________
46 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
47 #include <com/sun/star/frame/XDispatch.hpp>
48 #include <com/sun/star/frame/XDispatchProvider.hpp>
49 #include <com/sun/star/beans/PropertyValue.hpp>
50 #include <com/sun/star/util/XURLTransformer.hpp>
52 //_________________________________________________________________________________________________________________
53 // includes of other projects
54 //_________________________________________________________________________________________________________________
55 #include <svtools/transfer.hxx>
56 #include <unotools/localfilehelper.hxx>
57 #include <sot/filelist.hxx>
59 #include <osl/file.hxx>
60 #include <vcl/svapp.hxx>
62 //_________________________________________________________________________________________________________________
64 //_________________________________________________________________________________________________________________
69 DropTargetListener::DropTargetListener( const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xFactory
,
70 const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
71 : ThreadHelpBase ( &Application::GetSolarMutex() )
72 , m_xFactory ( xFactory
)
73 , m_xTargetFrame ( xFrame
)
74 , m_pFormats ( new DataFlavorExVector
)
78 // -----------------------------------------------------------------------------
80 DropTargetListener::~DropTargetListener()
82 m_xTargetFrame
= css::uno::WeakReference
< css::frame::XFrame
>();
83 m_xFactory
= css::uno::Reference
< css::lang::XMultiServiceFactory
>();
88 // -----------------------------------------------------------------------------
90 void SAL_CALL
DropTargetListener::disposing( const css::lang::EventObject
& ) throw( css::uno::RuntimeException
)
92 m_xTargetFrame
= css::uno::WeakReference
< css::frame::XFrame
>();
93 m_xFactory
= css::uno::Reference
< css::lang::XMultiServiceFactory
>();
96 // -----------------------------------------------------------------------------
98 void SAL_CALL
DropTargetListener::drop( const css::datatransfer::dnd::DropTargetDropEvent
& dtde
) throw( css::uno::RuntimeException
)
100 const sal_Int8 nAction
= dtde
.DropAction
;
104 if ( css::datatransfer::dnd::DNDConstants::ACTION_NONE
!= nAction
)
106 TransferableDataHelper
aHelper( dtde
.Transferable
);
107 sal_Bool bFormatFound
= sal_False
;
110 // at first check filelist format
111 if ( aHelper
.GetFileList( SOT_FORMAT_FILE_LIST
, aFileList
) )
113 ULONG i
, nCount
= aFileList
.Count();
114 for ( i
= 0; i
< nCount
; ++i
)
115 implts_OpenFile( aFileList
.GetFile(i
) );
116 bFormatFound
= sal_True
;
119 // then, if necessary, the file format
121 if ( !bFormatFound
&& aHelper
.GetString( SOT_FORMAT_FILE
, aFilePath
) )
122 implts_OpenFile( aFilePath
);
124 dtde
.Context
->dropComplete( css::datatransfer::dnd::DNDConstants::ACTION_NONE
!= nAction
);
126 catch( const ::com::sun::star::uno::Exception
& )
131 // -----------------------------------------------------------------------------
133 void SAL_CALL
DropTargetListener::dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent
& dtdee
) throw( css::uno::RuntimeException
)
137 implts_BeginDrag( dtdee
.SupportedDataFlavors
);
139 catch( const ::com::sun::star::uno::Exception
& )
146 // -----------------------------------------------------------------------------
148 void SAL_CALL
DropTargetListener::dragExit( const css::datatransfer::dnd::DropTargetEvent
& ) throw( css::uno::RuntimeException
)
154 catch( const ::com::sun::star::uno::Exception
& )
159 // -----------------------------------------------------------------------------
161 void SAL_CALL
DropTargetListener::dragOver( const css::datatransfer::dnd::DropTargetDragEvent
& dtde
) throw( css::uno::RuntimeException
)
165 sal_Bool bAccept
= ( implts_IsDropFormatSupported( SOT_FORMAT_FILE
) ||
166 implts_IsDropFormatSupported( SOT_FORMAT_FILE_LIST
) );
169 dtde
.Context
->rejectDrag();
171 dtde
.Context
->acceptDrag( css::datatransfer::dnd::DNDConstants::ACTION_COPY
);
173 catch( const ::com::sun::star::uno::Exception
& )
178 // -----------------------------------------------------------------------------
180 void SAL_CALL
DropTargetListener::dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent
& ) throw( css::uno::RuntimeException
)
184 void DropTargetListener::implts_BeginDrag( const css::uno::Sequence
< css::datatransfer::DataFlavor
>& rSupportedDataFlavors
)
187 WriteGuard
aWriteLock(m_aLock
);
189 TransferableDataHelper::FillDataFlavorExVector(rSupportedDataFlavors
,*m_pFormats
);
194 void DropTargetListener::implts_EndDrag()
197 WriteGuard
aWriteLock(m_aLock
);
203 sal_Bool
DropTargetListener::implts_IsDropFormatSupported( SotFormatStringId nFormat
)
206 ReadGuard
aReadLock(m_aLock
);
207 DataFlavorExVector::iterator
aIter( m_pFormats
->begin() ), aEnd( m_pFormats
->end() );
208 sal_Bool bRet
= sal_False
;
210 while ( aIter
!= aEnd
)
212 if ( nFormat
== (*aIter
++).mnSotId
)
224 void DropTargetListener::implts_OpenFile( const String
& rFilePath
)
227 if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFilePath
, aFileURL
) )
228 aFileURL
= rFilePath
;
230 ::osl::FileStatus
aStatus( FileStatusMask_FileURL
);
231 ::osl::DirectoryItem aItem
;
232 if( ::osl::FileBase::E_None
== ::osl::DirectoryItem::get( aFileURL
, aItem
) &&
233 ::osl::FileBase::E_None
== aItem
.getFileStatus( aStatus
) )
234 aFileURL
= aStatus
.getFileURL();
238 ReadGuard
aReadLock(m_aLock
);
239 css::uno::Reference
< css::frame::XFrame
> xTargetFrame( m_xTargetFrame
.get() , css::uno::UNO_QUERY
);
240 css::uno::Reference
< css::util::XURLTransformer
> xParser ( m_xFactory
->createInstance(SERVICENAME_URLTRANSFORMER
), css::uno::UNO_QUERY
);
243 if (xTargetFrame
.is() && xParser
.is())
246 aURL
.Complete
= aFileURL
;
247 xParser
->parseStrict(aURL
);
249 css::uno::Reference
< css::frame::XDispatchProvider
> xProvider( xTargetFrame
, css::uno::UNO_QUERY
);
250 css::uno::Reference
< css::frame::XDispatch
> xDispatcher
= xProvider
->queryDispatch( aURL
, SPECIALTARGET_DEFAULT
, 0 );
251 if ( xDispatcher
.is() )
252 xDispatcher
->dispatch( aURL
, css::uno::Sequence
< css::beans::PropertyValue
>() );