tdf#161411 - UI: Add Better wording for ASCII-only characters
[LibreOffice.git] / svtools / source / misc / openfiledroptargetlistener.cxx
blob4d4528a39e819c461b18e61a9e10b6da95ad0d73
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 <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>
30 #include <utility>
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();
48 m_xContext.clear();
52 void SAL_CALL OpenFileDropTargetListener::disposing( const css::lang::EventObject& )
54 m_xTargetFrame.clear();
55 m_xContext.clear();
59 void SAL_CALL OpenFileDropTargetListener::drop( const css::datatransfer::dnd::DropTargetDropEvent& dtde )
61 const sal_Int8 nAction = dtde.DropAction;
63 try
65 if ( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction )
67 TransferableDataHelper aHelper( dtde.Transferable );
68 bool bFormatFound = false;
69 FileList aFileList;
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) );
77 bFormatFound = true;
80 // then, if necessary, the file format
81 OUString aFilePath;
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 )
95 try
97 implts_BeginDrag( dtdee.SupportedDataFlavors );
99 catch( const css::uno::Exception& )
103 dragOver( dtdee );
107 void SAL_CALL OpenFileDropTargetListener::dragExit( const css::datatransfer::dnd::DropTargetEvent& )
111 implts_EndDrag();
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 ) );
126 if ( !bAccept )
127 dtde.Context->rejectDrag();
128 else
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 )
143 /* SAFE { */
144 SolarMutexGuard aGuard;
146 m_aFormats.clear();
147 TransferableDataHelper::FillDataFlavorExVector(rSupportedDataFlavors, m_aFormats);
148 /* } SAFE */
151 void OpenFileDropTargetListener::implts_EndDrag()
153 /* SAFE { */
154 SolarMutexGuard aGuard;
156 m_aFormats.clear();
157 /* } SAFE */
160 bool OpenFileDropTargetListener::implts_IsDropFormatSupported( SotClipboardFormatId nFormat )
162 /* SAFE { */
163 SolarMutexGuard aGuard;
165 for (auto const& format : m_aFormats)
167 if (nFormat == format.mnSotId)
169 return true;
172 /* } SAFE */
174 return false;
177 void OpenFileDropTargetListener::implts_OpenFile( const OUString& rFilePath )
179 OUString aFileURL;
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();
189 // open file
190 /* SAFE { */
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())
198 css::util::URL aURL;
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 >() );
208 /* } SAFE */
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */