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 _CONNECTIVITY_MACAB_DRIVER_HXX_
25 #define _CONNECTIVITY_MACAB_DRIVER_HXX_
26 
27 /** === begin UNO includes === **/
28 #include <com/sun/star/sdbc/XDriver.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/frame/XTerminateListener.hpp>
31 /** === end UNO includes === **/
32 #include <cppuhelper/compbase3.hxx>
33 #include <osl/module.h>
34 
35 // the address book driver's version
36 #define MACAB_DRIVER_VERSION		"0.1"
37 #define MACAB_DRIVER_VERSION_MAJOR	0
38 #define MACAB_DRIVER_VERSION_MINOR	1
39 
40 namespace connectivity
41 {
42 	namespace macab
43 	{
44         class MacabConnection;
45         class MacabDriver;
46 
47         typedef void*   (SAL_CALL * ConnectionFactoryFunction)( void* _pDriver );
48 
49         typedef std::vector< ::com::sun::star::uno::WeakReferenceHelper > OWeakRefArray;
50 
51         // ===============================================================
52         // = MacabImplModule
53         // ===============================================================
54         class MacabImplModule
55         {
56         private:
57 			::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
58 										m_xORB;
59 
60             /// Did we already attempt to load the module and to retrieve the symbols?
61             bool    m_bAttemptedLoadModule;
62             oslModule                   m_hConnectorModule;
63             ConnectionFactoryFunction   m_pConnectionFactoryFunc;
64 
65         public:
66             MacabImplModule( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory );
67 
68             /** determines whether there is a mac OS present in the environment
69             */
70             bool isMacOSPresent();
71 
72             /** initializes the implementation module.
73 
74                 @raises ::com::sun::star::uno::RuntimeException
75                     if the module could be loaded, but required symbols are missing
76                 @raises ::com::sun::star::sdbc::SQLException
77                     if no Mac OS was found at all
78             */
79             void init();
80 
81             /** shuts down the impl module
82             */
83             void shutdown();
84 
85             /** creates a new connection
86                 @precond
87                     <member>init</member> has been called before
88                 @raises ::com::sun::star::uno::RuntimeException
89                     if no connection object could be created (which is a severe error, normally impossible)
90             */
91             MacabConnection*  createConnection( MacabDriver* _pDriver ) const;
92 
93         private:
94             /** loads the implementation module and retrieves the needed symbols
95 
96                 Save against being called multiple times.
97 
98                 @return <TRUE/> if the module could be loaded successfully.
99 
100                 @raises ::com::sun::star::uno::RuntimeException
101                     if the module could be loaded, but required symbols are missing
102             */
103             bool    impl_loadModule();
104 
105             /** unloads the implementation module, and resets all function pointers to <NULL/>
106                 @precond m_hConnectorModule is not <NULL/>
107             */
108             void    impl_unloadModule();
109 
110             /** throws an SQLException saying than no Mac OS installation was found
111             */
112             void    impl_throwNoMacOSException();
113 
114             /** throws a generic SQL exception with SQLState S1000 and error code 0
115             */
116             void    impl_throwGenericSQLException( const ::rtl::OUString& _rMessage );
117 
118         };
119 
120         // ===============================================================
121         // = MacabDriver
122         // ===============================================================
123 		typedef ::cppu::WeakComponentImplHelper3<   ::com::sun::star::sdbc::XDriver,
124 													::com::sun::star::lang::XServiceInfo,
125                                                     ::com::sun::star::frame::XTerminateListener > MacabDriver_BASE;
126 		class MacabDriver : public MacabDriver_BASE
127 		{
128 		protected:
129 			::osl::Mutex				m_aMutex;			// mutex is need to control member access
130 			OWeakRefArray				m_xConnections;		// vector containing a list of all the
131 															//  MacabConnection objects for this Driver
132 			::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
133 										m_xMSFactory;		// the multi-service factory
134             MacabImplModule               m_aImplModule;
135 
136         public:
137             static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory) throw( ::com::sun::star::uno::Exception );
138 
139             // XServiceInfo - static versions
140             static ::rtl::OUString getImplementationName_Static(  ) throw(::com::sun::star::uno::RuntimeException);
141             static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static(  ) throw (::com::sun::star::uno::RuntimeException);
142 
143             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&
getMSFactory() const144                     getMSFactory() const { return m_xMSFactory; }
145 
146             /** returns the driver's implementation name (being pure ASCII) for reference in various places
147             */
148             static const sal_Char*  impl_getAsciiImplementationName();
149 
150             /** returns the path of our configuration settings
151             */
152             static ::rtl::OUString  impl_getConfigurationSettingsPath();
153 
154         protected:
155 			MacabDriver(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory);
156 
157 			// OComponentHelper
158 			virtual void SAL_CALL disposing(void);
159 
160 			// XServiceInfo
161 			virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
162 			virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
163 			virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
164 
165 			// XDriver
166 			virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > SAL_CALL connect( const ::rtl::OUString& url, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& info ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
167 			virtual sal_Bool SAL_CALL acceptsURL( const ::rtl::OUString& url ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
168 			virtual ::com::sun::star::uno::Sequence< ::com::sun::star::sdbc::DriverPropertyInfo > SAL_CALL getPropertyInfo( const ::rtl::OUString& url, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& info ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
169 			virtual sal_Int32 SAL_CALL getMajorVersion() throw(::com::sun::star::uno::RuntimeException);
170 			virtual sal_Int32 SAL_CALL getMinorVersion() throw(::com::sun::star::uno::RuntimeException);
171 
172             // XTerminateListener
173             virtual void SAL_CALL queryTermination( const ::com::sun::star::lang::EventObject& Event ) throw (::com::sun::star::frame::TerminationVetoException, ::com::sun::star::uno::RuntimeException);
174             virtual void SAL_CALL notifyTermination( const ::com::sun::star::lang::EventObject& Event ) throw (::com::sun::star::uno::RuntimeException);
175 
176             // XEventListener
177             virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
178 
179         private:
180             /** shuts down the library which contains the real implementations
181 
182                 This method is safe against being called multiple times
183 
184                 @precond our mutex is locked
185             */
186             void impl_shutdownImplementationModule();
187 		};
188 	}
189 
190 }
191 
192 #endif // _CONNECTIVITY_MACAB_DRIVER_HXX_
193