bump product version to 4.1.6.2
[LibreOffice.git] / framework / source / classes / droptargetlistener.cxx
blob66e272b18befe49b27bc94ef61e816f0ddbd8333
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 <classes/droptargetlistener.hxx>
21 #include <threadhelp/readguard.hxx>
22 #include <threadhelp/writeguard.hxx>
23 #include <targets.h>
24 #include <services.h>
26 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
27 #include <com/sun/star/frame/XDispatch.hpp>
28 #include <com/sun/star/frame/XDispatchProvider.hpp>
29 #include <com/sun/star/beans/PropertyValue.hpp>
30 #include <com/sun/star/util/URLTransformer.hpp>
31 #include <com/sun/star/util/XURLTransformer.hpp>
33 #include <svtools/transfer.hxx>
34 #include <unotools/localfilehelper.hxx>
35 #include <sot/filelist.hxx>
36 #include <comphelper/processfactory.hxx>
38 #include <osl/file.hxx>
39 #include <vcl/svapp.hxx>
41 namespace framework
44 DropTargetListener::DropTargetListener( const css::uno::Reference< css::lang::XMultiServiceFactory >& xFactory,
45 const css::uno::Reference< css::frame::XFrame >& xFrame )
46 : ThreadHelpBase ( &Application::GetSolarMutex() )
47 , m_xFactory ( xFactory )
48 , m_xTargetFrame ( xFrame )
49 , m_pFormats ( new DataFlavorExVector )
53 // -----------------------------------------------------------------------------
55 DropTargetListener::~DropTargetListener()
57 m_xTargetFrame = css::uno::WeakReference< css::frame::XFrame >();
58 m_xFactory = css::uno::Reference< css::lang::XMultiServiceFactory >();
59 delete m_pFormats;
60 m_pFormats = NULL;
63 // -----------------------------------------------------------------------------
65 void SAL_CALL DropTargetListener::disposing( const css::lang::EventObject& ) throw( css::uno::RuntimeException )
67 m_xTargetFrame = css::uno::WeakReference< css::frame::XFrame >();
68 m_xFactory = css::uno::Reference< css::lang::XMultiServiceFactory >();
71 // -----------------------------------------------------------------------------
73 void SAL_CALL DropTargetListener::drop( const css::datatransfer::dnd::DropTargetDropEvent& dtde ) throw( css::uno::RuntimeException )
75 const sal_Int8 nAction = dtde.DropAction;
77 try
79 if ( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction )
81 TransferableDataHelper aHelper( dtde.Transferable );
82 sal_Bool bFormatFound = sal_False;
83 FileList aFileList;
85 // at first check filelist format
86 if ( aHelper.GetFileList( SOT_FORMAT_FILE_LIST, aFileList ) )
88 sal_uLong i, nCount = aFileList.Count();
89 for ( i = 0; i < nCount; ++i )
90 implts_OpenFile( aFileList.GetFile(i) );
91 bFormatFound = sal_True;
94 // then, if necessary, the file format
95 String aFilePath;
96 if ( !bFormatFound && aHelper.GetString( SOT_FORMAT_FILE, aFilePath ) )
97 implts_OpenFile( aFilePath );
99 dtde.Context->dropComplete( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction );
101 catch( const ::com::sun::star::uno::Exception& )
106 // -----------------------------------------------------------------------------
108 void SAL_CALL DropTargetListener::dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent& dtdee ) throw( css::uno::RuntimeException )
112 implts_BeginDrag( dtdee.SupportedDataFlavors );
114 catch( const ::com::sun::star::uno::Exception& )
118 dragOver( dtdee );
121 // -----------------------------------------------------------------------------
123 void SAL_CALL DropTargetListener::dragExit( const css::datatransfer::dnd::DropTargetEvent& ) throw( css::uno::RuntimeException )
127 implts_EndDrag();
129 catch( const ::com::sun::star::uno::Exception& )
134 // -----------------------------------------------------------------------------
136 void SAL_CALL DropTargetListener::dragOver( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) throw( css::uno::RuntimeException )
140 sal_Bool bAccept = ( implts_IsDropFormatSupported( SOT_FORMAT_FILE ) ||
141 implts_IsDropFormatSupported( SOT_FORMAT_FILE_LIST ) );
143 if ( !bAccept )
144 dtde.Context->rejectDrag();
145 else
146 dtde.Context->acceptDrag( css::datatransfer::dnd::DNDConstants::ACTION_COPY );
148 catch( const ::com::sun::star::uno::Exception& )
153 // -----------------------------------------------------------------------------
155 void SAL_CALL DropTargetListener::dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent& ) throw( css::uno::RuntimeException )
159 void DropTargetListener::implts_BeginDrag( const css::uno::Sequence< css::datatransfer::DataFlavor >& rSupportedDataFlavors )
161 /* SAFE { */
162 WriteGuard aWriteLock(m_aLock);
163 m_pFormats->clear();
164 TransferableDataHelper::FillDataFlavorExVector(rSupportedDataFlavors,*m_pFormats);
165 aWriteLock.unlock();
166 /* } SAFE */
169 void DropTargetListener::implts_EndDrag()
171 /* SAFE { */
172 WriteGuard aWriteLock(m_aLock);
173 m_pFormats->clear();
174 aWriteLock.unlock();
175 /* } SAFE */
178 sal_Bool DropTargetListener::implts_IsDropFormatSupported( SotFormatStringId nFormat )
180 /* SAFE { */
181 ReadGuard aReadLock(m_aLock);
182 DataFlavorExVector::iterator aIter( m_pFormats->begin() ), aEnd( m_pFormats->end() );
183 sal_Bool bRet = sal_False;
185 while ( aIter != aEnd )
187 if ( nFormat == (*aIter++).mnSotId )
189 bRet = sal_True;
190 aIter = aEnd;
193 aReadLock.unlock();
194 /* } SAFE */
196 return bRet;
199 void DropTargetListener::implts_OpenFile( const String& rFilePath )
201 OUString aFileURL;
202 if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFilePath, aFileURL ) )
203 aFileURL = rFilePath;
205 ::osl::FileStatus aStatus( osl_FileStatus_Mask_FileURL );
206 ::osl::DirectoryItem aItem;
207 if( ::osl::FileBase::E_None == ::osl::DirectoryItem::get( aFileURL, aItem ) &&
208 ::osl::FileBase::E_None == aItem.getFileStatus( aStatus ) )
209 aFileURL = aStatus.getFileURL();
211 // open file
212 /* SAFE { */
213 ReadGuard aReadLock(m_aLock);
214 css::uno::Reference< css::frame::XFrame > xTargetFrame( m_xTargetFrame.get(), css::uno::UNO_QUERY );
215 css::uno::Reference< css::util::XURLTransformer > xParser ( css::util::URLTransformer::create(::comphelper::getComponentContext(m_xFactory)) );
216 aReadLock.unlock();
217 /* } SAFE */
218 if (xTargetFrame.is() && xParser.is())
220 css::util::URL aURL;
221 aURL.Complete = aFileURL;
222 xParser->parseStrict(aURL);
224 css::uno::Reference < css::frame::XDispatchProvider > xProvider( xTargetFrame, css::uno::UNO_QUERY );
225 css::uno::Reference< css::frame::XDispatch > xDispatcher = xProvider->queryDispatch( aURL, SPECIALTARGET_DEFAULT, 0 );
226 if ( xDispatcher.is() )
227 xDispatcher->dispatch( aURL, css::uno::Sequence < css::beans::PropertyValue >() );
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */