1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <svtools/openfiledroptargetlistener.hxx>
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>
31 #include <vcl/transfer.hxx>
32 #include <sot/filelist.hxx>
34 #include <osl/file.hxx>
35 #include <vcl/svapp.hxx>
37 OpenFileDropTargetListener::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
)
45 OpenFileDropTargetListener::~OpenFileDropTargetListener()
47 m_xTargetFrame
.clear();
52 void SAL_CALL
OpenFileDropTargetListener::disposing( const css::lang::EventObject
& )
54 m_xTargetFrame
.clear();
59 void SAL_CALL
OpenFileDropTargetListener::drop( const css::datatransfer::dnd::DropTargetDropEvent
& dtde
)
61 const sal_Int8 nAction
= dtde
.DropAction
;
65 if ( css::datatransfer::dnd::DNDConstants::ACTION_NONE
!= nAction
)
67 TransferableDataHelper
aHelper( dtde
.Transferable
);
68 bool bFormatFound
= false;
71 // at first check filelist format
72 if ( aHelper
.GetFileList( SotClipboardFormatId::FILE_LIST
, aFileList
) )
74 size_t i
, nCount
= aFileList
.Count();
75 for ( i
= 0; i
< nCount
; ++i
)
76 implts_OpenFile( aFileList
.GetFile(i
) );
80 // then, if necessary, the file format
82 if ( !bFormatFound
&& aHelper
.GetString( SotClipboardFormatId::SIMPLE_FILE
, aFilePath
) )
83 implts_OpenFile( aFilePath
);
85 dtde
.Context
->dropComplete( css::datatransfer::dnd::DNDConstants::ACTION_NONE
!= nAction
);
87 catch( const css::uno::Exception
& )
93 void SAL_CALL
OpenFileDropTargetListener::dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent
& dtdee
)
97 implts_BeginDrag( dtdee
.SupportedDataFlavors
);
99 catch( const css::uno::Exception
& )
107 void SAL_CALL
OpenFileDropTargetListener::dragExit( const css::datatransfer::dnd::DropTargetEvent
& )
113 catch( const css::uno::Exception
& )
119 void SAL_CALL
OpenFileDropTargetListener::dragOver( const css::datatransfer::dnd::DropTargetDragEvent
& dtde
)
123 bool bAccept
= ( implts_IsDropFormatSupported( SotClipboardFormatId::SIMPLE_FILE
) ||
124 implts_IsDropFormatSupported( SotClipboardFormatId::FILE_LIST
) );
127 dtde
.Context
->rejectDrag();
129 dtde
.Context
->acceptDrag( css::datatransfer::dnd::DNDConstants::ACTION_COPY
);
131 catch( const css::uno::Exception
& )
137 void SAL_CALL
OpenFileDropTargetListener::dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent
& )
141 void OpenFileDropTargetListener::implts_BeginDrag( const css::uno::Sequence
< css::datatransfer::DataFlavor
>& rSupportedDataFlavors
)
144 SolarMutexGuard aGuard
;
147 TransferableDataHelper::FillDataFlavorExVector(rSupportedDataFlavors
, m_aFormats
);
151 void OpenFileDropTargetListener::implts_EndDrag()
154 SolarMutexGuard aGuard
;
160 bool OpenFileDropTargetListener::implts_IsDropFormatSupported( SotClipboardFormatId nFormat
)
163 SolarMutexGuard aGuard
;
165 for (auto const& format
: m_aFormats
)
167 if (nFormat
== format
.mnSotId
)
177 void OpenFileDropTargetListener::implts_OpenFile( const OUString
& rFilePath
)
180 if ( osl::FileBase::getFileURLFromSystemPath( rFilePath
, aFileURL
) != osl::FileBase::E_None
)
181 aFileURL
= rFilePath
;
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();
191 SolarMutexGuard aGuard
;
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
) );
196 if (xTargetFrame
.is() && xParser
.is())
199 aURL
.Complete
= aFileURL
;
200 xParser
->parseStrict(aURL
);
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
, u
"_default"_ustr
, 0 );
205 if ( xDispatcher
.is() )
206 xDispatcher
->dispatch( aURL
, css::uno::Sequence
< css::beans::PropertyValue
>() );
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */