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