Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <classes/droptargetlistener.hxx>
21 : #include <threadhelp/readguard.hxx>
22 : #include <threadhelp/writeguard.hxx>
23 : #include <targets.h>
24 : #include <services.h>
25 :
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>
32 :
33 : #include <svtools/transfer.hxx>
34 : #include <unotools/localfilehelper.hxx>
35 : #include <sot/filelist.hxx>
36 : #include <comphelper/processfactory.hxx>
37 :
38 : #include <osl/file.hxx>
39 : #include <vcl/svapp.hxx>
40 :
41 : namespace framework
42 : {
43 :
44 1107 : DropTargetListener::DropTargetListener( const css::uno::Reference< css::uno::XComponentContext >& xContext,
45 : const css::uno::Reference< css::frame::XFrame >& xFrame )
46 1107 : : ThreadHelpBase ( &Application::GetSolarMutex() )
47 : , m_xContext ( xContext )
48 : , m_xTargetFrame ( xFrame )
49 1107 : , m_pFormats ( new DataFlavorExVector )
50 : {
51 1107 : }
52 :
53 : // -----------------------------------------------------------------------------
54 :
55 3300 : DropTargetListener::~DropTargetListener()
56 : {
57 1100 : m_xTargetFrame.clear();
58 1100 : m_xContext.clear();
59 1100 : delete m_pFormats;
60 1100 : m_pFormats = NULL;
61 2200 : }
62 :
63 : // -----------------------------------------------------------------------------
64 :
65 0 : void SAL_CALL DropTargetListener::disposing( const css::lang::EventObject& ) throw( css::uno::RuntimeException )
66 : {
67 0 : m_xTargetFrame.clear();
68 0 : m_xContext.clear();
69 0 : }
70 :
71 : // -----------------------------------------------------------------------------
72 :
73 0 : void SAL_CALL DropTargetListener::drop( const css::datatransfer::dnd::DropTargetDropEvent& dtde ) throw( css::uno::RuntimeException )
74 : {
75 0 : const sal_Int8 nAction = dtde.DropAction;
76 :
77 : try
78 : {
79 0 : if ( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction )
80 : {
81 0 : TransferableDataHelper aHelper( dtde.Transferable );
82 0 : sal_Bool bFormatFound = sal_False;
83 0 : FileList aFileList;
84 :
85 : // at first check filelist format
86 0 : if ( aHelper.GetFileList( SOT_FORMAT_FILE_LIST, aFileList ) )
87 : {
88 0 : sal_uLong i, nCount = aFileList.Count();
89 0 : for ( i = 0; i < nCount; ++i )
90 0 : implts_OpenFile( aFileList.GetFile(i) );
91 0 : bFormatFound = sal_True;
92 : }
93 :
94 : // then, if necessary, the file format
95 0 : String aFilePath;
96 0 : if ( !bFormatFound && aHelper.GetString( SOT_FORMAT_FILE, aFilePath ) )
97 0 : implts_OpenFile( aFilePath );
98 : }
99 0 : dtde.Context->dropComplete( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction );
100 : }
101 0 : catch( const ::com::sun::star::uno::Exception& )
102 : {
103 : }
104 0 : }
105 :
106 : // -----------------------------------------------------------------------------
107 :
108 0 : void SAL_CALL DropTargetListener::dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent& dtdee ) throw( css::uno::RuntimeException )
109 : {
110 : try
111 : {
112 0 : implts_BeginDrag( dtdee.SupportedDataFlavors );
113 : }
114 0 : catch( const ::com::sun::star::uno::Exception& )
115 : {
116 : }
117 :
118 0 : dragOver( dtdee );
119 0 : }
120 :
121 : // -----------------------------------------------------------------------------
122 :
123 0 : void SAL_CALL DropTargetListener::dragExit( const css::datatransfer::dnd::DropTargetEvent& ) throw( css::uno::RuntimeException )
124 : {
125 : try
126 : {
127 0 : implts_EndDrag();
128 : }
129 0 : catch( const ::com::sun::star::uno::Exception& )
130 : {
131 : }
132 0 : }
133 :
134 : // -----------------------------------------------------------------------------
135 :
136 0 : void SAL_CALL DropTargetListener::dragOver( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) throw( css::uno::RuntimeException )
137 : {
138 : try
139 : {
140 0 : sal_Bool bAccept = ( implts_IsDropFormatSupported( SOT_FORMAT_FILE ) ||
141 0 : implts_IsDropFormatSupported( SOT_FORMAT_FILE_LIST ) );
142 :
143 0 : if ( !bAccept )
144 0 : dtde.Context->rejectDrag();
145 : else
146 0 : dtde.Context->acceptDrag( css::datatransfer::dnd::DNDConstants::ACTION_COPY );
147 : }
148 0 : catch( const ::com::sun::star::uno::Exception& )
149 : {
150 : }
151 0 : }
152 :
153 : // -----------------------------------------------------------------------------
154 :
155 0 : void SAL_CALL DropTargetListener::dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent& ) throw( css::uno::RuntimeException )
156 : {
157 0 : }
158 :
159 0 : void DropTargetListener::implts_BeginDrag( const css::uno::Sequence< css::datatransfer::DataFlavor >& rSupportedDataFlavors )
160 : {
161 : /* SAFE { */
162 0 : WriteGuard aWriteLock(m_aLock);
163 0 : m_pFormats->clear();
164 0 : TransferableDataHelper::FillDataFlavorExVector(rSupportedDataFlavors,*m_pFormats);
165 0 : aWriteLock.unlock();
166 : /* } SAFE */
167 0 : }
168 :
169 0 : void DropTargetListener::implts_EndDrag()
170 : {
171 : /* SAFE { */
172 0 : WriteGuard aWriteLock(m_aLock);
173 0 : m_pFormats->clear();
174 0 : aWriteLock.unlock();
175 : /* } SAFE */
176 0 : }
177 :
178 0 : sal_Bool DropTargetListener::implts_IsDropFormatSupported( SotFormatStringId nFormat )
179 : {
180 : /* SAFE { */
181 0 : ReadGuard aReadLock(m_aLock);
182 0 : DataFlavorExVector::iterator aIter( m_pFormats->begin() ), aEnd( m_pFormats->end() );
183 0 : sal_Bool bRet = sal_False;
184 :
185 0 : while ( aIter != aEnd )
186 : {
187 0 : if ( nFormat == (*aIter++).mnSotId )
188 : {
189 0 : bRet = sal_True;
190 0 : aIter = aEnd;
191 : }
192 : }
193 0 : aReadLock.unlock();
194 : /* } SAFE */
195 :
196 0 : return bRet;
197 : }
198 :
199 0 : void DropTargetListener::implts_OpenFile( const String& rFilePath )
200 : {
201 0 : OUString aFileURL;
202 0 : if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFilePath, aFileURL ) )
203 0 : aFileURL = rFilePath;
204 :
205 0 : ::osl::FileStatus aStatus( osl_FileStatus_Mask_FileURL );
206 0 : ::osl::DirectoryItem aItem;
207 0 : if( ::osl::FileBase::E_None == ::osl::DirectoryItem::get( aFileURL, aItem ) &&
208 0 : ::osl::FileBase::E_None == aItem.getFileStatus( aStatus ) )
209 0 : aFileURL = aStatus.getFileURL();
210 :
211 : // open file
212 : /* SAFE { */
213 0 : ReadGuard aReadLock(m_aLock);
214 0 : css::uno::Reference< css::frame::XFrame > xTargetFrame( m_xTargetFrame.get(), css::uno::UNO_QUERY );
215 0 : css::uno::Reference< css::util::XURLTransformer > xParser ( css::util::URLTransformer::create(m_xContext) );
216 0 : aReadLock.unlock();
217 : /* } SAFE */
218 0 : if (xTargetFrame.is() && xParser.is())
219 : {
220 0 : css::util::URL aURL;
221 0 : aURL.Complete = aFileURL;
222 0 : xParser->parseStrict(aURL);
223 :
224 0 : css::uno::Reference < css::frame::XDispatchProvider > xProvider( xTargetFrame, css::uno::UNO_QUERY );
225 0 : css::uno::Reference< css::frame::XDispatch > xDispatcher = xProvider->queryDispatch( aURL, SPECIALTARGET_DEFAULT, 0 );
226 0 : if ( xDispatcher.is() )
227 0 : xDispatcher->dispatch( aURL, css::uno::Sequence < css::beans::PropertyValue >() );
228 0 : }
229 0 : }
230 :
231 402 : }
232 :
233 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|