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