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