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 <svtools/openfiledroptargetlistener.hxx>
21 :
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>
28 :
29 : #include <svtools/transfer.hxx>
30 : #include <unotools/localfilehelper.hxx>
31 : #include <sot/filelist.hxx>
32 : #include <comphelper/processfactory.hxx>
33 :
34 : #include <osl/file.hxx>
35 : #include <vcl/svapp.hxx>
36 :
37 : // Create a new task or recycle an existing one
38 : const char SPECIALTARGET_DEFAULT[] = "_default";
39 :
40 3284 : 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 3284 : , m_pFormats ( new DataFlavorExVector )
45 : {
46 3284 : }
47 :
48 :
49 :
50 9825 : OpenFileDropTargetListener::~OpenFileDropTargetListener()
51 : {
52 3275 : m_xTargetFrame.clear();
53 3275 : m_xContext.clear();
54 3275 : delete m_pFormats;
55 3275 : m_pFormats = NULL;
56 6550 : }
57 :
58 :
59 :
60 0 : void SAL_CALL OpenFileDropTargetListener::disposing( const css::lang::EventObject& ) throw( css::uno::RuntimeException, std::exception )
61 : {
62 0 : m_xTargetFrame.clear();
63 0 : m_xContext.clear();
64 0 : }
65 :
66 :
67 :
68 0 : void SAL_CALL OpenFileDropTargetListener::drop( const css::datatransfer::dnd::DropTargetDropEvent& dtde ) throw( css::uno::RuntimeException, std::exception )
69 : {
70 0 : const sal_Int8 nAction = dtde.DropAction;
71 :
72 : try
73 : {
74 0 : if ( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction )
75 : {
76 0 : TransferableDataHelper aHelper( dtde.Transferable );
77 0 : bool bFormatFound = false;
78 0 : FileList aFileList;
79 :
80 : // at first check filelist format
81 0 : if ( aHelper.GetFileList( SotClipboardFormatId::FILE_LIST, aFileList ) )
82 : {
83 0 : sal_uLong i, nCount = aFileList.Count();
84 0 : for ( i = 0; i < nCount; ++i )
85 0 : implts_OpenFile( aFileList.GetFile(i) );
86 0 : bFormatFound = true;
87 : }
88 :
89 : // then, if necessary, the file format
90 0 : OUString aFilePath;
91 0 : if ( !bFormatFound && aHelper.GetString( SotClipboardFormatId::SIMPLE_FILE, aFilePath ) )
92 0 : implts_OpenFile( aFilePath );
93 : }
94 0 : dtde.Context->dropComplete( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction );
95 : }
96 0 : catch( const ::com::sun::star::uno::Exception& )
97 : {
98 : }
99 0 : }
100 :
101 :
102 :
103 0 : void SAL_CALL OpenFileDropTargetListener::dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent& dtdee ) throw( css::uno::RuntimeException, std::exception )
104 : {
105 : try
106 : {
107 0 : implts_BeginDrag( dtdee.SupportedDataFlavors );
108 : }
109 0 : catch( const ::com::sun::star::uno::Exception& )
110 : {
111 : }
112 :
113 0 : dragOver( dtdee );
114 0 : }
115 :
116 :
117 :
118 0 : void SAL_CALL OpenFileDropTargetListener::dragExit( const css::datatransfer::dnd::DropTargetEvent& ) throw( css::uno::RuntimeException, std::exception )
119 : {
120 : try
121 : {
122 0 : implts_EndDrag();
123 : }
124 0 : catch( const ::com::sun::star::uno::Exception& )
125 : {
126 : }
127 0 : }
128 :
129 :
130 :
131 0 : void SAL_CALL OpenFileDropTargetListener::dragOver( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) throw( css::uno::RuntimeException, std::exception )
132 : {
133 : try
134 : {
135 0 : bool bAccept = ( implts_IsDropFormatSupported( SotClipboardFormatId::SIMPLE_FILE ) ||
136 0 : implts_IsDropFormatSupported( SotClipboardFormatId::FILE_LIST ) );
137 :
138 0 : if ( !bAccept )
139 0 : dtde.Context->rejectDrag();
140 : else
141 0 : dtde.Context->acceptDrag( css::datatransfer::dnd::DNDConstants::ACTION_COPY );
142 : }
143 0 : catch( const ::com::sun::star::uno::Exception& )
144 : {
145 : }
146 0 : }
147 :
148 :
149 :
150 0 : void SAL_CALL OpenFileDropTargetListener::dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent& ) throw( css::uno::RuntimeException, std::exception )
151 : {
152 0 : }
153 :
154 0 : void OpenFileDropTargetListener::implts_BeginDrag( const css::uno::Sequence< css::datatransfer::DataFlavor >& rSupportedDataFlavors )
155 : {
156 : /* SAFE { */
157 0 : SolarMutexGuard aGuard;
158 :
159 0 : m_pFormats->clear();
160 0 : TransferableDataHelper::FillDataFlavorExVector(rSupportedDataFlavors,*m_pFormats);
161 : /* } SAFE */
162 0 : }
163 :
164 0 : void OpenFileDropTargetListener::implts_EndDrag()
165 : {
166 : /* SAFE { */
167 0 : SolarMutexGuard aGuard;
168 :
169 0 : m_pFormats->clear();
170 : /* } SAFE */
171 0 : }
172 :
173 0 : bool OpenFileDropTargetListener::implts_IsDropFormatSupported( SotClipboardFormatId nFormat )
174 : {
175 : /* SAFE { */
176 0 : SolarMutexGuard aGuard;
177 :
178 0 : DataFlavorExVector::iterator aIter( m_pFormats->begin() ), aEnd( m_pFormats->end() );
179 0 : bool bRet = false;
180 :
181 0 : while ( aIter != aEnd )
182 : {
183 0 : if ( nFormat == (*aIter++).mnSotId )
184 : {
185 0 : bRet = true;
186 0 : aIter = aEnd;
187 : }
188 : }
189 : /* } SAFE */
190 :
191 0 : return bRet;
192 : }
193 :
194 0 : void OpenFileDropTargetListener::implts_OpenFile( const OUString& rFilePath )
195 : {
196 0 : OUString aFileURL;
197 0 : if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFilePath, aFileURL ) )
198 0 : aFileURL = rFilePath;
199 :
200 0 : ::osl::FileStatus aStatus( osl_FileStatus_Mask_FileURL );
201 0 : ::osl::DirectoryItem aItem;
202 0 : if( ::osl::FileBase::E_None == ::osl::DirectoryItem::get( aFileURL, aItem ) &&
203 0 : ::osl::FileBase::E_None == aItem.getFileStatus( aStatus ) )
204 0 : aFileURL = aStatus.getFileURL();
205 :
206 : // open file
207 : /* SAFE { */
208 0 : SolarMutexGuard aGuard;
209 :
210 0 : css::uno::Reference< css::frame::XFrame > xTargetFrame( m_xTargetFrame.get(), css::uno::UNO_QUERY );
211 0 : css::uno::Reference< css::util::XURLTransformer > xParser ( css::util::URLTransformer::create(m_xContext) );
212 :
213 0 : if (xTargetFrame.is() && xParser.is())
214 : {
215 0 : css::util::URL aURL;
216 0 : aURL.Complete = aFileURL;
217 0 : xParser->parseStrict(aURL);
218 :
219 0 : css::uno::Reference < css::frame::XDispatchProvider > xProvider( xTargetFrame, css::uno::UNO_QUERY );
220 0 : css::uno::Reference< css::frame::XDispatch > xDispatcher = xProvider->queryDispatch( aURL, SPECIALTARGET_DEFAULT, 0 );
221 0 : if ( xDispatcher.is() )
222 0 : xDispatcher->dispatch( aURL, css::uno::Sequence < css::beans::PropertyValue >() );
223 0 : }
224 : /* } SAFE */
225 0 : }
226 :
227 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|