1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_VCL_OSX_DATAFLAVORMAPPING_HXX
21 #define INCLUDED_VCL_OSX_DATAFLAVORMAPPING_HXX
22 
23 #include <com/sun/star/datatransfer/DataFlavor.hpp>
24 #include <com/sun/star/datatransfer/XMimeContentTypeFactory.hpp>
25 #include <com/sun/star/datatransfer/XTransferable.hpp>
26 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
27 
28 #include <premac.h>
29 #import <Cocoa/Cocoa.h>
30 #include <postmac.h>
31 
32 #include <memory>
33 #include <unordered_map>
34 
35 /* An interface to get the clipboard data in either
36    system or OOo format.
37  */
38 class DataProvider
39 {
40 public:
~DataProvider()41   virtual ~DataProvider() {};
42 
43   /* Get the clipboard data in the system format.
44      The caller has to retain/release the returned
45      CFDataRef on demand.
46    */
47   virtual NSData* getSystemData() = 0;
48 
49   /* Get the clipboard data in OOo format.
50    */
51   virtual css::uno::Any getOOoData() = 0;
52 };
53 
54 typedef std::unique_ptr<DataProvider> DataProviderPtr_t;
55 
56 class DataFlavorMapper
57 {
58 public:
59   /* Initialize a DataFavorMapper instance. Throws a RuntimeException in case the XMimeContentTypeFactory service
60      cannot be created.
61    */
62   DataFlavorMapper();
63   ~DataFlavorMapper();
64 
65   /* Map a system data flavor to an OpenOffice data flavor.
66      Return an empty string if there is not suitable
67      mapping from a system data flavor to an LibreOffice data
68      flavor.
69   */
70   css::datatransfer::DataFlavor systemToOpenOfficeFlavor( const NSString* systemDataFlavor) const;
71 
72   /* Map an OpenOffice data flavor to a system data flavor.
73      If there is no suitable mapping available NULL will
74      be returned.
75   */
76   const NSString* openOfficeToSystemFlavor(const css::datatransfer::DataFlavor& oooDataFlavor, bool& rbInternal) const;
77 
78   /* Select the best available image data type
79      If there is no suitable mapping available NULL will
80      be returned.
81   */
82   static NSString* openOfficeImageToSystemFlavor(NSPasteboard* pPasteboard);
83 
84   /* Get a data provider which is able to provide the data 'rTransferable' offers in a format that can
85      be put on to the system clipboard.
86    */
87   DataProviderPtr_t getDataProvider( const NSString* systemFlavor,
88                                     const css::uno::Reference< css::datatransfer::XTransferable > & rTransferable) const;
89 
90   /* Get a data provider which is able to provide 'systemData' in the OOo expected format.
91    */
92   static DataProviderPtr_t getDataProvider( const NSString* systemFlavor, NSArray* systemData);
93 
94   /* Get a data provider which is able to provide 'systemData' in the OOo expected format.
95    */
96   static DataProviderPtr_t getDataProvider( const NSString* systemFlavor, NSData* systemData);
97 
98   /* Translate a sequence of DataFlavors into an NSArray of system types.
99      Only those DataFlavors for which a suitable mapping to a system
100      type exist will be contained in the returned types array.
101    */
102   NSArray* flavorSequenceToTypesArray(const css::uno::Sequence<css::datatransfer::DataFlavor>& flavors) const;
103 
104   /* Translate an NSArray of system types into a sequence of DataFlavors.
105      Only those types for which a suitable mapping to a DataFlavor
106      exist will be contained in the new DataFlavor Sequence.
107   */
108   css::uno::Sequence<css::datatransfer::DataFlavor> typesArrayToFlavorSequence(NSArray* types) const;
109 
110   /* Returns an NSArray containing all pasteboard types supported by OOo
111    */
112   static NSArray* getAllSupportedPboardTypes();
113 
114 private:
115   /* Determines if the provided Mime content type is valid.
116    */
117   bool isValidMimeContentType(const OUString& contentType) const;
118 
119 private:
120   css::uno::Reference< css::datatransfer::XMimeContentTypeFactory> mrXMimeCntFactory;
121   typedef std::unordered_map< OUString, NSString* > OfficeOnlyTypes;
122   mutable OfficeOnlyTypes maOfficeOnlyTypes;
123 };
124 
125 typedef std::shared_ptr<DataFlavorMapper> DataFlavorMapperPtr_t;
126 
127 #endif // INCLUDED_VCL_OSX_DATAFLAVORMAPPING_HXX
128 
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
130