xref: /trunk/main/vcl/unx/generic/dtrans/X11_transferable.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_vcl.hxx"
24 
25 #if OSL_DEBUG_LEVEL > 1
26 #include <stdio.h>
27 #endif
28 
29 #include <X11_transferable.hxx>
30 #include <X11/Xatom.h>
31 #include <com/sun/star/io/IOException.hpp>
32 
33 using namespace com::sun::star::datatransfer;
34 using namespace com::sun::star::lang;
35 using namespace com::sun::star::io;
36 using namespace com::sun::star::uno;
37 using namespace cppu;
38 using namespace osl;
39 using namespace rtl;
40 
41 
42 using namespace x11;
43 
44 
45 X11Transferable::X11Transferable(
46     SelectionManager& rManager,
47     const Reference< XInterface >& xCreator,
48     Atom selection
49     ) :
50         m_rManager( rManager ),
51         m_xCreator( xCreator ),
52         m_aSelection( selection )
53 {
54 }
55 
56 //==================================================================================================
57 
58 X11Transferable::~X11Transferable()
59 {
60 }
61 
62 //==================================================================================================
63 
64 Any SAL_CALL X11Transferable::getTransferData( const DataFlavor& rFlavor )
65 {
66     Any aRet;
67     Sequence< sal_Int8 > aData;
68     bool bSuccess = m_rManager.getPasteData( m_aSelection ? m_aSelection : XA_PRIMARY, rFlavor.MimeType, aData );
69     if( ! bSuccess && m_aSelection == 0 )
70         bSuccess = m_rManager.getPasteData( m_rManager.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ), rFlavor.MimeType, aData );
71 
72     if( ! bSuccess )
73     {
74         throw UnsupportedFlavorException( rFlavor.MimeType, static_cast < XTransferable * > ( this ) );
75     }
76     if( rFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) )
77     {
78         int nLen = aData.getLength()/2;
79         if( ((sal_Unicode*)aData.getConstArray())[nLen-1] == 0 )
80             nLen--;
81         OUString aString( (sal_Unicode*)aData.getConstArray(), nLen );
82 #if OSL_DEBUG_LEVEL > 1
83     fprintf( stderr, "X11Transferable::getTransferData( \"%s\" )\n -> \"%s\"\n",
84              OUStringToOString( rFlavor.MimeType, RTL_TEXTENCODING_ISO_8859_1 ).getStr(),
85              OUStringToOString( aString, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
86 #endif
87         aRet <<= aString;
88     }
89     else
90         aRet <<= aData;
91     return aRet;
92 }
93 
94 //==================================================================================================
95 
96 Sequence< DataFlavor > SAL_CALL X11Transferable::getTransferDataFlavors()
97 {
98     Sequence< DataFlavor > aFlavorList;
99     bool bSuccess = m_rManager.getPasteDataTypes( m_aSelection ? m_aSelection : XA_PRIMARY, aFlavorList );
100     if( ! bSuccess && m_aSelection == 0 )
101         bSuccess = m_rManager.getPasteDataTypes( m_rManager.getAtom( OUString::createFromAscii( "CLIPBOARD" ) ), aFlavorList );
102 
103     return aFlavorList;
104 }
105 
106 //==================================================================================================
107 
108 sal_Bool SAL_CALL X11Transferable::isDataFlavorSupported( const DataFlavor& aFlavor )
109 {
110     if( aFlavor.DataType != getCppuType( (Sequence< sal_Int8 >*)0 ) )
111     {
112         if( ! aFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) &&
113             aFlavor.DataType == getCppuType( (OUString*)0 ) )
114             return false;
115     }
116 
117     Sequence< DataFlavor > aFlavors( getTransferDataFlavors() );
118     for( int i = 0; i < aFlavors.getLength(); i++ )
119         if( aFlavor.MimeType.equalsIgnoreAsciiCase( aFlavors.getConstArray()[i].MimeType ) &&
120             aFlavor.DataType == aFlavors.getConstArray()[i].DataType )
121             return sal_True;
122 
123     return sal_False;
124 }
125 
126 /* vim: set noet sw=4 ts=4: */
127