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 	// clipboard format unsupported, throw exception
117 	throw UnsupportedFlavorException( rFlavor.MimeType, static_cast < XTransferable * > ( this ) );
118 }
119 
120 //==================================================================================================
121 
122 Sequence< DataFlavor > SAL_CALL Os2Transferable::getTransferDataFlavors()
123     throw(RuntimeException)
124 {
125 	debug_printf("Os2Transferable::getTransferDataFlavors %08x\n", this);
126 	Sequence< DataFlavor > aFlavorList(1);
127 	aFlavorList[0] = aFlavor;
128 	debug_printf("Os2Transferable::getTransferDataFlavors mimetype: %s\n", CHAR_POINTER(aFlavor.MimeType));
129 	return aFlavorList;
130 }
131 
132 //==================================================================================================
133 
134 sal_Bool SAL_CALL Os2Transferable::isDataFlavorSupported( const DataFlavor& aFlavor )
135     throw(RuntimeException)
136 {
137 	debug_printf("Os2Transferable::isDataFlavorSupported %08x\n", this);
138 	debug_printf("Os2Transferable::isDataFlavorSupported %s\n", CHAR_POINTER(aFlavor.MimeType));
139 
140 	if( aFlavor.DataType != getCppuType( (Sequence< sal_Int8 >*)0 ) )
141 	{
142 		if( ! aFlavor.MimeType.equalsIgnoreAsciiCase( OUString::createFromAscii( "text/plain;charset=utf-16" ) ) &&
143 			aFlavor.DataType == getCppuType( (OUString*)0 ) )
144 			return false;
145 	}
146 
147 	Sequence< DataFlavor > aFlavors( getTransferDataFlavors() );
148 	for( int i = 0; i < aFlavors.getLength(); i++ )
149 		if( aFlavor.MimeType.equalsIgnoreAsciiCase( aFlavors.getConstArray()[i].MimeType ) &&
150 			aFlavor.DataType == aFlavors.getConstArray()[i].DataType )
151 			return sal_True;
152 
153 	return sal_False;
154 }
155 
156