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 #if OSL_DEBUG_LEVEL > 1
25 #include <stdio.h>
26 #endif
27
28 #define INCL_WIN
29 #include <svpm.h>
30
31 #include <string.h>
32 #include <com/sun/star/io/IOException.hpp>
33 #include "Os2Transferable.hxx"
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 using namespace os2;
43
44 // =======================================================================
45
Os2Transferable(const Reference<XInterface> & xCreator)46 Os2Transferable::Os2Transferable(
47 const Reference< XInterface >& xCreator ) :
48 m_xCreator( xCreator )
49 {
50 debug_printf("Os2Transferable::Os2Transferable %08x\n", this);
51 hAB = WinQueryAnchorBlock( HWND_DESKTOP );
52
53 // query clipboard data to get mimetype
54 if( UWinOpenClipbrd( hAB ) )
55 {
56 ULONG handle = UWinQueryClipbrdData( hAB, UCLIP_CF_UNICODETEXT);
57 if (handle) {
58 aFlavor.MimeType = OUString::createFromAscii( "text/plain;charset=utf-16" );
59 aFlavor.DataType = getCppuType( (OUString*)0 );
60 //debug_printf("Os2Transferable::Os2Transferable pszText %s\n", pszText);
61 }
62 handle = UWinQueryClipbrdData( hAB, UCLIP_CF_BITMAP);
63 if (handle) {
64 aFlavor.MimeType = OUString::createFromAscii( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" );
65 aFlavor.DataType = getCppuType( (OUString*)0 );
66 //debug_printf("Os2Transferable::Os2Transferable pszText %s\n", pszText);
67 }
68 UWinCloseClipbrd( hAB);
69 }
70 else
71 {
72 debug_printf("Os2Transferable::Os2Transferable failed to open clipboard\n");
73 }
74
75 }
76
77 //==================================================================================================
78
~Os2Transferable()79 Os2Transferable::~Os2Transferable()
80 {
81 debug_printf("Os2Transferable::~Os2Transferable %08x\n", this);
82 }
83
84 //==================================================================================================
85
getTransferData(const DataFlavor & rFlavor)86 Any SAL_CALL Os2Transferable::getTransferData( const DataFlavor& rFlavor )
87 throw(UnsupportedFlavorException, IOException, RuntimeException)
88 {
89 debug_printf("Os2Transferable::getTransferData %08x\n", this);
90 debug_printf("Os2Transferable::getTransferData mimetype: %s\n", CHAR_POINTER(rFlavor.MimeType));
91 Any aRet;
92 Sequence< sal_Int8 > aData;
93
94 // retrieve unicode text
95 if( rFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) )
96 {
97 if( UWinOpenClipbrd( hAB ) )
98 {
99 // check if clipboard has text format
100 sal_Unicode* pszText = (sal_Unicode*) UWinQueryClipbrdData( hAB, UCLIP_CF_UNICODETEXT);
101 if (pszText) {
102 // convert to oustring and return it
103 OUString aString( pszText);
104 aRet <<= aString;
105 }
106 UWinCloseClipbrd( hAB );
107 if (pszText)
108 return aRet;
109 }
110 }
111
112 // retrieve bitmap
113 if( rFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ) ) )
114 {
115 if( UWinOpenClipbrd( hAB ) )
116 {
117 // check if clipboard has text format
118 ULONG handle = UWinQueryClipbrdData( hAB, UCLIP_CF_BITMAP);
119 if (handle) {
120 Sequence< sal_Int8 > winDIBStream;
121 // convert to oustring and return it
122 if (OS2HandleToOOoBmp( handle, &winDIBStream))
123 aRet <<= winDIBStream;
124 else
125 handle = 0;
126 }
127 UWinCloseClipbrd( hAB );
128 if (handle)
129 return aRet;
130 }
131 }
132
133 // clipboard format unsupported, throw exception
134 throw UnsupportedFlavorException( rFlavor.MimeType, static_cast < XTransferable * > ( this ) );
135 }
136
137 //==================================================================================================
138
getTransferDataFlavors()139 Sequence< DataFlavor > SAL_CALL Os2Transferable::getTransferDataFlavors()
140 throw(RuntimeException)
141 {
142 debug_printf("Os2Transferable::getTransferDataFlavors %08x\n", this);
143 Sequence< DataFlavor > aFlavorList(1);
144 aFlavorList[0] = aFlavor;
145 debug_printf("Os2Transferable::getTransferDataFlavors mimetype: %s\n", CHAR_POINTER(aFlavor.MimeType));
146 return aFlavorList;
147 }
148
149 //==================================================================================================
150
isDataFlavorSupported(const DataFlavor & aFlavor)151 sal_Bool SAL_CALL Os2Transferable::isDataFlavorSupported( const DataFlavor& aFlavor )
152 throw(RuntimeException)
153 {
154 debug_printf("Os2Transferable::isDataFlavorSupported %08x\n", this);
155 debug_printf("Os2Transferable::isDataFlavorSupported %s\n", CHAR_POINTER(aFlavor.MimeType));
156
157 if( aFlavor.DataType != getCppuType( (Sequence< sal_Int8 >*)0 ) )
158 {
159 if( ! aFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) &&
160 aFlavor.DataType == getCppuType( (OUString*)0 ) )
161 return false;
162 }
163
164 Sequence< DataFlavor > aFlavors( getTransferDataFlavors() );
165 for( int i = 0; i < aFlavors.getLength(); i++ )
166 if( aFlavor.MimeType.equalsIgnoreAsciiCase( aFlavors.getConstArray()[i].MimeType ) &&
167 aFlavor.DataType == aFlavors.getConstArray()[i].DataType )
168 return sal_True;
169
170 return sal_False;
171 }
172
173