1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #if OSL_DEBUG_LEVEL > 1
28 #include <stdio.h>
29 #endif
30 
31 #include <X11_transferable.hxx>
32 #include <X11/Xatom.h>
33 #include <com/sun/star/io/IOException.hpp>
34 
35 using namespace com::sun::star::datatransfer;
36 using namespace com::sun::star::lang;
37 using namespace com::sun::star::io;
38 using namespace com::sun::star::uno;
39 using namespace cppu;
40 using namespace osl;
41 using namespace rtl;
42 
43 
44 using namespace x11;
45 
46 
X11Transferable(SelectionManager & rManager,const Reference<XInterface> & xCreator,Atom selection)47 X11Transferable::X11Transferable(
48 	SelectionManager& rManager,
49 	const Reference< XInterface >& xCreator,
50 	Atom selection
51 	) :
52 		m_rManager( rManager ),
53 		m_xCreator( xCreator ),
54 		m_aSelection( selection )
55 {
56 }
57 
58 //==================================================================================================
59 
~X11Transferable()60 X11Transferable::~X11Transferable()
61 {
62 }
63 
64 //==================================================================================================
65 
getTransferData(const DataFlavor & rFlavor)66 Any SAL_CALL X11Transferable::getTransferData( const DataFlavor& rFlavor )
67     throw(UnsupportedFlavorException, IOException, RuntimeException)
68 {
69 	Any aRet;
70 	Sequence< sal_Int8 > aData;
71 	bool bSuccess = m_rManager.getPasteData( m_aSelection ? m_aSelection : XA_PRIMARY, rFlavor.MimeType, aData );
72 	if( ! bSuccess && m_aSelection == 0 )
73 		bSuccess = m_rManager.getPasteData( m_rManager.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ), rFlavor.MimeType, aData );
74 
75 	if( ! bSuccess )
76 	{
77 		throw UnsupportedFlavorException( rFlavor.MimeType, static_cast < XTransferable * > ( this ) );
78 	}
79 	if( rFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) )
80 	{
81 		int nLen = aData.getLength()/2;
82 		if( ((sal_Unicode*)aData.getConstArray())[nLen-1] == 0 )
83 			nLen--;
84 		OUString aString( (sal_Unicode*)aData.getConstArray(), nLen );
85 #if OSL_DEBUG_LEVEL > 1
86 	fprintf( stderr, "X11Transferable::getTransferData( \"%s\" )\n -> \"%s\"\n",
87 			 OUStringToOString( rFlavor.MimeType, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
88 			 OUStringToOString( aString, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
89 #endif
90 		aRet <<= aString;
91 	}
92 	else
93 		aRet <<= aData;
94 	return aRet;
95 }
96 
97 //==================================================================================================
98 
getTransferDataFlavors()99 Sequence< DataFlavor > SAL_CALL X11Transferable::getTransferDataFlavors()
100     throw(RuntimeException)
101 {
102 	Sequence< DataFlavor > aFlavorList;
103 	bool bSuccess = m_rManager.getPasteDataTypes( m_aSelection ? m_aSelection : XA_PRIMARY, aFlavorList );
104 	if( ! bSuccess && m_aSelection == 0 )
105 		bSuccess = m_rManager.getPasteDataTypes( m_rManager.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ), aFlavorList );
106 
107 	return aFlavorList;
108 }
109 
110 //==================================================================================================
111 
isDataFlavorSupported(const DataFlavor & aFlavor)112 sal_Bool SAL_CALL X11Transferable::isDataFlavorSupported( const DataFlavor& aFlavor )
113     throw(RuntimeException)
114 {
115 	if( aFlavor.DataType != getCppuType( (Sequence< sal_Int8 >*)0 ) )
116 	{
117 		if( ! aFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) &&
118 			aFlavor.DataType == getCppuType( (OUString*)0 ) )
119 			return false;
120 	}
121 
122 	Sequence< DataFlavor > aFlavors( getTransferDataFlavors() );
123 	for( int i = 0; i < aFlavors.getLength(); i++ )
124 		if( aFlavor.MimeType.equalsIgnoreAsciiCase( aFlavors.getConstArray()[i].MimeType ) &&
125 			aFlavor.DataType == aFlavors.getConstArray()[i].DataType )
126 			return sal_True;
127 
128 	return sal_False;
129 }
130 
131