1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <svtools/openfiledroptargetlistener.hxx>
#include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
#include <com/sun/star/frame/XDispatch.hpp>
#include <com/sun/star/frame/XDispatchProvider.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/util/URLTransformer.hpp>
#include <com/sun/star/util/XURLTransformer.hpp>
#include <svtools/transfer.hxx>
#include <unotools/localfilehelper.hxx>
#include <sot/filelist.hxx>
#include <comphelper/processfactory.hxx>
#include <osl/file.hxx>
#include <vcl/svapp.hxx>
// Create a new task or recycle an existing one
const char SPECIALTARGET_DEFAULT[] = "_default";
OpenFileDropTargetListener::OpenFileDropTargetListener( const css::uno::Reference< css::uno::XComponentContext >& xContext,
const css::uno::Reference< css::frame::XFrame >& xFrame )
: m_xContext ( xContext )
, m_xTargetFrame ( xFrame )
, m_pFormats ( new DataFlavorExVector )
{
}
// -----------------------------------------------------------------------------
OpenFileDropTargetListener::~OpenFileDropTargetListener()
{
m_xTargetFrame.clear();
m_xContext.clear();
delete m_pFormats;
m_pFormats = NULL;
}
// -----------------------------------------------------------------------------
void SAL_CALL OpenFileDropTargetListener::disposing( const css::lang::EventObject& ) throw( css::uno::RuntimeException )
{
m_xTargetFrame.clear();
m_xContext.clear();
}
// -----------------------------------------------------------------------------
void SAL_CALL OpenFileDropTargetListener::drop( const css::datatransfer::dnd::DropTargetDropEvent& dtde ) throw( css::uno::RuntimeException )
{
const sal_Int8 nAction = dtde.DropAction;
try
{
if ( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction )
{
TransferableDataHelper aHelper( dtde.Transferable );
sal_Bool bFormatFound = sal_False;
FileList aFileList;
// at first check filelist format
if ( aHelper.GetFileList( SOT_FORMAT_FILE_LIST, aFileList ) )
{
sal_uLong i, nCount = aFileList.Count();
for ( i = 0; i < nCount; ++i )
implts_OpenFile( aFileList.GetFile(i) );
bFormatFound = sal_True;
}
// then, if necessary, the file format
OUString aFilePath;
if ( !bFormatFound && aHelper.GetString( SOT_FORMAT_FILE, aFilePath ) )
implts_OpenFile( aFilePath );
}
dtde.Context->dropComplete( css::datatransfer::dnd::DNDConstants::ACTION_NONE != nAction );
}
catch( const ::com::sun::star::uno::Exception& )
{
}
}
// -----------------------------------------------------------------------------
void SAL_CALL OpenFileDropTargetListener::dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent& dtdee ) throw( css::uno::RuntimeException )
{
try
{
implts_BeginDrag( dtdee.SupportedDataFlavors );
}
catch( const ::com::sun::star::uno::Exception& )
{
}
dragOver( dtdee );
}
// -----------------------------------------------------------------------------
void SAL_CALL OpenFileDropTargetListener::dragExit( const css::datatransfer::dnd::DropTargetEvent& ) throw( css::uno::RuntimeException )
{
try
{
implts_EndDrag();
}
catch( const ::com::sun::star::uno::Exception& )
{
}
}
// -----------------------------------------------------------------------------
void SAL_CALL OpenFileDropTargetListener::dragOver( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) throw( css::uno::RuntimeException )
{
try
{
sal_Bool bAccept = ( implts_IsDropFormatSupported( SOT_FORMAT_FILE ) ||
implts_IsDropFormatSupported( SOT_FORMAT_FILE_LIST ) );
if ( !bAccept )
dtde.Context->rejectDrag();
else
dtde.Context->acceptDrag( css::datatransfer::dnd::DNDConstants::ACTION_COPY );
}
catch( const ::com::sun::star::uno::Exception& )
{
}
}
// -----------------------------------------------------------------------------
void SAL_CALL OpenFileDropTargetListener::dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent& ) throw( css::uno::RuntimeException )
{
}
void OpenFileDropTargetListener::implts_BeginDrag( const css::uno::Sequence< css::datatransfer::DataFlavor >& rSupportedDataFlavors )
{
/* SAFE { */
SolarMutexGuard aGuard;
m_pFormats->clear();
TransferableDataHelper::FillDataFlavorExVector(rSupportedDataFlavors,*m_pFormats);
/* } SAFE */
}
void OpenFileDropTargetListener::implts_EndDrag()
{
/* SAFE { */
SolarMutexGuard aGuard;
m_pFormats->clear();
/* } SAFE */
}
sal_Bool OpenFileDropTargetListener::implts_IsDropFormatSupported( SotFormatStringId nFormat )
{
/* SAFE { */
SolarMutexGuard aGuard;
DataFlavorExVector::iterator aIter( m_pFormats->begin() ), aEnd( m_pFormats->end() );
sal_Bool bRet = sal_False;
while ( aIter != aEnd )
{
if ( nFormat == (*aIter++).mnSotId )
{
bRet = sal_True;
aIter = aEnd;
}
}
/* } SAFE */
return bRet;
}
void OpenFileDropTargetListener::implts_OpenFile( const String& rFilePath )
{
OUString aFileURL;
if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( rFilePath, aFileURL ) )
aFileURL = rFilePath;
::osl::FileStatus aStatus( osl_FileStatus_Mask_FileURL );
::osl::DirectoryItem aItem;
if( ::osl::FileBase::E_None == ::osl::DirectoryItem::get( aFileURL, aItem ) &&
::osl::FileBase::E_None == aItem.getFileStatus( aStatus ) )
aFileURL = aStatus.getFileURL();
// open file
/* SAFE { */
SolarMutexGuard aGuard;
css::uno::Reference< css::frame::XFrame > xTargetFrame( m_xTargetFrame.get(), css::uno::UNO_QUERY );
css::uno::Reference< css::util::XURLTransformer > xParser ( css::util::URLTransformer::create(m_xContext) );
if (xTargetFrame.is() && xParser.is())
{
css::util::URL aURL;
aURL.Complete = aFileURL;
xParser->parseStrict(aURL);
css::uno::Reference < css::frame::XDispatchProvider > xProvider( xTargetFrame, css::uno::UNO_QUERY );
css::uno::Reference< css::frame::XDispatch > xDispatcher = xProvider->queryDispatch( aURL, SPECIALTARGET_DEFAULT, 0 );
if ( xDispatcher.is() )
xDispatcher->dispatch( aURL, css::uno::Sequence < css::beans::PropertyValue >() );
}
/* } SAFE */
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|