1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #if OSL_DEBUG_LEVEL > 1
32 #include <stdio.h>
33 #endif
34 
35 #include <X11_transferable.hxx>
36 #include <X11/Xatom.h>
37 #include <com/sun/star/io/IOException.hpp>
38 
39 using namespace com::sun::star::datatransfer;
40 using namespace com::sun::star::lang;
41 using namespace com::sun::star::io;
42 using namespace com::sun::star::uno;
43 using namespace cppu;
44 using namespace osl;
45 using namespace rtl;
46 
47 
48 using namespace x11;
49 
50 
51 X11Transferable::X11Transferable(
52 	SelectionManager& rManager,
53 	const Reference< XInterface >& xCreator,
54 	Atom selection
55 	) :
56 		m_rManager( rManager ),
57 		m_xCreator( xCreator ),
58 		m_aSelection( selection )
59 {
60 }
61 
62 //==================================================================================================
63 
64 X11Transferable::~X11Transferable()
65 {
66 }
67 
68 //==================================================================================================
69 
70 Any SAL_CALL X11Transferable::getTransferData( const DataFlavor& rFlavor )
71     throw(UnsupportedFlavorException, IOException, RuntimeException)
72 {
73 	Any aRet;
74 	Sequence< sal_Int8 > aData;
75 	bool bSuccess = m_rManager.getPasteData( m_aSelection ? m_aSelection : XA_PRIMARY, rFlavor.MimeType, aData );
76 	if( ! bSuccess && m_aSelection == 0 )
77 		bSuccess = m_rManager.getPasteData( m_rManager.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ), rFlavor.MimeType, aData );
78 
79 	if( ! bSuccess )
80 	{
81 		throw UnsupportedFlavorException( rFlavor.MimeType, static_cast < XTransferable * > ( this ) );
82 	}
83 	if( rFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) )
84 	{
85 		int nLen = aData.getLength()/2;
86 		if( ((sal_Unicode*)aData.getConstArray())[nLen-1] == 0 )
87 			nLen--;
88 		OUString aString( (sal_Unicode*)aData.getConstArray(), nLen );
89 #if OSL_DEBUG_LEVEL > 1
90 	fprintf( stderr, "X11Transferable::getTransferData( \"%s\" )\n -> \"%s\"\n",
91 			 OUStringToOString( rFlavor.MimeType, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
92 			 OUStringToOString( aString, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
93 #endif
94 		aRet <<= aString;
95 	}
96 	else
97 		aRet <<= aData;
98 	return aRet;
99 }
100 
101 //==================================================================================================
102 
103 Sequence< DataFlavor > SAL_CALL X11Transferable::getTransferDataFlavors()
104     throw(RuntimeException)
105 {
106 	Sequence< DataFlavor > aFlavorList;
107 	bool bSuccess = m_rManager.getPasteDataTypes( m_aSelection ? m_aSelection : XA_PRIMARY, aFlavorList );
108 	if( ! bSuccess && m_aSelection == 0 )
109 		bSuccess = m_rManager.getPasteDataTypes( m_rManager.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ), aFlavorList );
110 
111 	return aFlavorList;
112 }
113 
114 //==================================================================================================
115 
116 sal_Bool SAL_CALL X11Transferable::isDataFlavorSupported( const DataFlavor& aFlavor )
117     throw(RuntimeException)
118 {
119 	if( aFlavor.DataType != getCppuType( (Sequence< sal_Int8 >*)0 ) )
120 	{
121 		if( ! aFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) &&
122 			aFlavor.DataType == getCppuType( (OUString*)0 ) )
123 			return false;
124 	}
125 
126 	Sequence< DataFlavor > aFlavors( getTransferDataFlavors() );
127 	for( int i = 0; i < aFlavors.getLength(); i++ )
128 		if( aFlavor.MimeType.equalsIgnoreAsciiCase( aFlavors.getConstArray()[i].MimeType ) &&
129 			aFlavor.DataType == aFlavors.getConstArray()[i].DataType )
130 			return sal_True;
131 
132 	return sal_False;
133 }
134 
135