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 #ifndef INCLUDED_DATAFLAVORMAPPING_HXX_
25 #define INCLUDED_DATAFLAVORMAPPING_HXX_
26 
27 #include <com/sun/star/datatransfer/DataFlavor.hpp>
28 #include <com/sun/star/datatransfer/XMimeContentTypeFactory.hpp>
29 #include <com/sun/star/datatransfer/XTransferable.hpp>
30 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
31 
32 #include <premac.h>
33 #import <Cocoa/Cocoa.h>
34 #include <postmac.h>
35 
36 #include <hash_map>
37 #include <memory>
38 #include <boost/shared_ptr.hpp>
39 
40 
41 /* An interface to get the clipboard data in either
42    system or OOo format.
43  */
44 class DataProvider
45 {
46 public:
~DataProvider()47   virtual ~DataProvider() {};
48 
49   /* Get the clipboard data in the system format.
50 	 The caller has to retain/release the returned
51 	 CFDataRef on demand.
52    */
53   virtual NSData* getSystemData() = 0;
54 
55   /* Get the clipboard data in OOo format.
56    */
57   virtual com::sun::star::uno::Any getOOoData() = 0;
58 };
59 
60 typedef std::auto_ptr<DataProvider> DataProviderPtr_t;
61 
62 
63 //################################
64 
65 
66 class DataFlavorMapper
67 {
68 public:
69   /* Initialialize a DataFavorMapper instance. Throws a RuntimeException in case the XMimeContentTypeFactory service
70 	 cannot be created.
71    */
72   DataFlavorMapper();
73   ~DataFlavorMapper();
74 
75 
76   /* Map a system data flavor to an OpenOffice data flavor.
77 	 Return an empty string if there is not suiteable
78 	 mapping from a system data flavor to a OpenOffice data
79 	 flavor.
80   */
81   com::sun::star::datatransfer::DataFlavor systemToOpenOfficeFlavor( const NSString* systemDataFlavor) const;
82 
83 
84   /* Map an OpenOffice data flavor to a system data flavor.
85 	 If there is no suiteable mapping available NULL will
86 	 be returned.
87   */
88   const NSString* openOfficeToSystemFlavor(const com::sun::star::datatransfer::DataFlavor& oooDataFlavor, bool& rbInternal) const;
89 
90   /* Select the best available image data type
91 	 If there is no suiteable mapping available NULL will
92 	 be returned.
93   */
94   NSString* openOfficeImageToSystemFlavor(NSPasteboard* pPasteboard) const;
95 
96   /* Get a data provider which is able to provide the data 'rTransferable' offers in a format that can
97 	 be put on to the system clipboard.
98    */
99   DataProviderPtr_t getDataProvider( const NSString* systemFlavor,
100 									const com::sun::star::uno::Reference< com::sun::star::datatransfer::XTransferable > rTransferable) const;
101 
102 
103 
104   /* Get a data provider which is able to provide 'systemData' in the OOo expected format.
105    */
106   DataProviderPtr_t getDataProvider( const NSString* systemFlavor, NSArray* systemData) const;
107 
108 
109   /* Get a data provider which is able to provide 'systemData' in the OOo expected format.
110    */
111   DataProviderPtr_t getDataProvider( const NSString* systemFlavor, NSData* systemData) const;
112 
113 
114   /* Translate a sequence of DataFlavors into a NSArray of system types.
115 	 Only those DataFlavors for which a suitable mapping to a system
116 	 type exist will be contained in the returned types array.
117    */
118   NSArray* flavorSequenceToTypesArray(const com::sun::star::uno::Sequence<com::sun::star::datatransfer::DataFlavor>& flavors) const;
119 
120   /* Translate a NSArray of system types into a sequence of DataFlavors.
121 	 Only those types for which a suitable mapping to a DataFlavor
122 	 exist will be contained in the new DataFlavor Sequence.
123   */
124   com::sun::star::uno::Sequence<com::sun::star::datatransfer::DataFlavor> typesArrayToFlavorSequence(NSArray* types) const;
125 
126   /* Returns an NSArray containing all pasteboard types supported by OOo
127    */
128   NSArray* getAllSupportedPboardTypes() const;
129 
130 private:
131   /* Determines if the provided Mime content type is valid.
132    */
133   bool isValidMimeContentType(const rtl::OUString& contentType) const;
134 
135 private:
136   ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XMimeContentTypeFactory> mrXMimeCntFactory;
137   typedef std::hash_map< rtl::OUString, NSString*, rtl::OUStringHash > OfficeOnlyTypes;
138   mutable OfficeOnlyTypes maOfficeOnlyTypes;
139 };
140 
141 typedef boost::shared_ptr<DataFlavorMapper> DataFlavorMapperPtr_t;
142 
143 #endif
144 
145