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 {
88 debug_printf("Os2Transferable::getTransferData %08x\n", this);
89 debug_printf("Os2Transferable::getTransferData mimetype: %s\n", CHAR_POINTER(rFlavor.MimeType));
90 Any aRet;
91 Sequence< sal_Int8 > aData;
92
93 // retrieve unicode text
94 if( rFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) )
95 {
96 if( UWinOpenClipbrd( hAB ) )
97 {
98 // check if clipboard has text format
99 sal_Unicode* pszText = (sal_Unicode*) UWinQueryClipbrdData( hAB, UCLIP_CF_UNICODETEXT);
100 if (pszText) {
101 // convert to oustring and return it
102 OUString aString( pszText);
103 aRet <<= aString;
104 }
105 UWinCloseClipbrd( hAB );
106 if (pszText)
107 return aRet;
108 }
109 }
110
111 // retrieve bitmap
112 if( rFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ) ) )
113 {
114 if( UWinOpenClipbrd( hAB ) )
115 {
116 // check if clipboard has text format
117 ULONG handle = UWinQueryClipbrdData( hAB, UCLIP_CF_BITMAP);
118 if (handle) {
119 Sequence< sal_Int8 > winDIBStream;
120 // convert to oustring and return it
121 if (OS2HandleToOOoBmp( handle, &winDIBStream))
122 aRet <<= winDIBStream;
123 else
124 handle = 0;
125 }
126 UWinCloseClipbrd( hAB );
127 if (handle)
128 return aRet;
129 }
130 }
131
132 // clipboard format unsupported, throw exception
133 throw UnsupportedFlavorException( rFlavor.MimeType, static_cast < XTransferable * > ( this ) );
134 }
135
136 //==================================================================================================
137
getTransferDataFlavors()138 Sequence< DataFlavor > SAL_CALL Os2Transferable::getTransferDataFlavors()
139 {
140 debug_printf("Os2Transferable::getTransferDataFlavors %08x\n", this);
141 Sequence< DataFlavor > aFlavorList(1);
142 aFlavorList[0] = aFlavor;
143 debug_printf("Os2Transferable::getTransferDataFlavors mimetype: %s\n", CHAR_POINTER(aFlavor.MimeType));
144 return aFlavorList;
145 }
146
147 //==================================================================================================
148
isDataFlavorSupported(const DataFlavor & aFlavor)149 sal_Bool SAL_CALL Os2Transferable::isDataFlavorSupported( const DataFlavor& aFlavor )
150 {
151 debug_printf("Os2Transferable::isDataFlavorSupported %08x\n", this);
152 debug_printf("Os2Transferable::isDataFlavorSupported %s\n", CHAR_POINTER(aFlavor.MimeType));
153
154 if( aFlavor.DataType != getCppuType( (Sequence< sal_Int8 >*)0 ) )
155 {
156 if( ! aFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) &&
157 aFlavor.DataType == getCppuType( (OUString*)0 ) )
158 return false;
159 }
160
161 Sequence< DataFlavor > aFlavors( getTransferDataFlavors() );
162 for( int i = 0; i < aFlavors.getLength(); i++ )
163 if( aFlavor.MimeType.equalsIgnoreAsciiCase( aFlavors.getConstArray()[i].MimeType ) &&
164 aFlavor.DataType == aFlavors.getConstArray()[i].DataType )
165 return sal_True;
166
167 return sal_False;
168 }
169