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 _AQUA_CLIPBOARD_HXX_
25 #define _AQUA_CLIPBOARD_HXX_
26 
27 #include "DataFlavorMapping.hxx"
28 #include <rtl/ustring.hxx>
29 #include <sal/types.h>
30 #include <cppuhelper/compbase4.hxx>
31 #include <com/sun/star/datatransfer/XTransferable.hpp>
32 #include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
33 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
34 #include <com/sun/star/datatransfer/clipboard/XClipboardListener.hpp>
35 #include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
36 #include <com/sun/star/datatransfer/XMimeContentTypeFactory.hpp>
37 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <cppuhelper/basemutex.hxx>
40 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
41 
42 #include <boost/utility.hpp>
43 #include <list>
44 
45 #include <premac.h>
46 #import <Cocoa/Cocoa.h>
47 #include <postmac.h>
48 
49 class AquaClipboard;
50 
51 @interface EventListener : NSObject
52 {
53      AquaClipboard* pAquaClipboard;
54 }
55 
56 // Init the pasteboard change listener with a reference to the OfficeClipboard
57 // instance
58 - (EventListener*)initWithAquaClipboard: (AquaClipboard*) pcb;
59 
60 // Promiss resolver function
61 - (void)pasteboard:(NSPasteboard*)sender provideDataForType:(const NSString *)type;
62 
63 -(void)applicationDidBecomeActive:(NSNotification*)aNotification;
64 
65 -(void)disposing;
66 @end
67 
68 
69 class AquaClipboard : public ::cppu::BaseMutex,
70 					  public ::cppu::WeakComponentImplHelper4< com::sun::star::datatransfer::clipboard::XClipboardEx,
71 															   com::sun::star::datatransfer::clipboard::XClipboardNotifier,
72 															   com::sun::star::datatransfer::clipboard::XFlushableClipboard,
73 															   com::sun::star::lang::XServiceInfo >,
74 					  private ::boost::noncopyable
75 {
76 public:
77   /* Create a clipboard instance.
78 
79 	 @param pasteboard
80 	 If not equal NULL the instance will be instantiated with the provided
81 	 pasteboard reference and 'bUseSystemClipboard' will be ignored
82 
83 	 @param bUseSystemClipboard
84 	 If 'pasteboard' is NULL 'bUseSystemClipboard' determines whether the
85 	 system clipboard will be created (bUseSystemClipboard == true) or if
86 	 the DragPasteboard if bUseSystemClipboard == false
87    */
88   AquaClipboard(NSPasteboard* pasteboard = NULL,
89 				bool bUseSystemClipboard = true);
90 
91   ~AquaClipboard();
92 
93   //------------------------------------------------
94   // XClipboard
95   //------------------------------------------------
96 
97   virtual ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable > SAL_CALL getContents()
98 	throw( ::com::sun::star::uno::RuntimeException );
99 
100   virtual void SAL_CALL setContents( const ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable >& xTransferable,
101 									 const ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::clipboard::XClipboardOwner >& xClipboardOwner )
102 	throw( ::com::sun::star::uno::RuntimeException );
103 
104   virtual ::rtl::OUString SAL_CALL getName()
105 	throw( ::com::sun::star::uno::RuntimeException );
106 
107   //------------------------------------------------
108   // XClipboardEx
109   //------------------------------------------------
110 
111   virtual sal_Int8 SAL_CALL getRenderingCapabilities()
112 	throw( ::com::sun::star::uno::RuntimeException );
113 
114   //------------------------------------------------
115   // XClipboardNotifier
116   //------------------------------------------------
117 
118   virtual void SAL_CALL addClipboardListener( const ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::clipboard::XClipboardListener >& listener )
119 	throw( ::com::sun::star::uno::RuntimeException );
120 
121   virtual void SAL_CALL removeClipboardListener( const ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::clipboard::XClipboardListener >& listener )
122 	throw( ::com::sun::star::uno::RuntimeException );
123 
124   //------------------------------------------------
125   // XFlushableClipboard
126   //------------------------------------------------
127 
128   virtual void SAL_CALL flushClipboard( ) throw( com::sun::star::uno::RuntimeException );
129 
130   //------------------------------------------------
131   // XServiceInfo
132   //------------------------------------------------
133 
134   virtual ::rtl::OUString SAL_CALL getImplementationName()
135 	throw(::com::sun::star::uno::RuntimeException);
136 
137   virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
138 	throw(::com::sun::star::uno::RuntimeException);
139 
140   virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames()
141 	throw(::com::sun::star::uno::RuntimeException);
142 
143   /* Get a reference to the used pastboard.
144    */
145   NSPasteboard* getPasteboard() const;
146 
147   /* Notify the current clipboard owner that he is no longer the clipboard owner.
148    */
149   void fireLostClipboardOwnershipEvent(::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::clipboard::XClipboardOwner> oldOwner,
150 									   ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable > oldContent);
151 
152   void pasteboardChangedOwner();
153 
154   void provideDataForType(NSPasteboard* sender, const NSString* type);
155 
156   void applicationDidBecomeActive(NSNotification* aNotification);
157 
158 private:
159 
160   /* Notify all registered XClipboardListener that the clipboard content
161 	 has changed.
162   */
163   void fireClipboardChangedEvent();
164 
165 private:
166   ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XMimeContentTypeFactory > mrXMimeCntFactory;
167   ::std::list< ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::clipboard::XClipboardListener > > mClipboardListeners;
168   ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable > mXClipboardContent;
169   com::sun::star::uno::Reference< com::sun::star::datatransfer::clipboard::XClipboardOwner > mXClipboardOwner;
170   DataFlavorMapperPtr_t mpDataFlavorMapper;
171   bool mIsSystemPasteboard;
172   NSPasteboard* mPasteboard;
173   int mPasteboardChangeCount;
174   EventListener* mEventListener;
175 };
176 
177 #endif
178