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 #include "precompiled_sd.hxx"
25 
26 #include "BasicToolBarFactory.hxx"
27 
28 #include "ViewTabBar.hxx"
29 #include "framework/FrameworkHelper.hxx"
30 #include <comphelper/mediadescriptor.hxx>
31 
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 #include "DrawController.hxx"
34 
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::drawing::framework;
39 
40 namespace sd { namespace framework {
41 
42 
BasicToolBarFactory_createInstance(const Reference<XComponentContext> & rxContext)43 Reference<XInterface> SAL_CALL BasicToolBarFactory_createInstance (
44     const Reference<XComponentContext>& rxContext)
45 {
46     return static_cast<XWeak*>(new BasicToolBarFactory(rxContext));
47 }
48 
49 
50 
51 
BasicToolBarFactory_getImplementationName(void)52 ::rtl::OUString BasicToolBarFactory_getImplementationName (void) throw(RuntimeException)
53 {
54     return ::rtl::OUString(
55         RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Draw.framework.BasicToolBarFactory"));
56 }
57 
58 
59 
60 
BasicToolBarFactory_getSupportedServiceNames(void)61 Sequence<rtl::OUString> SAL_CALL BasicToolBarFactory_getSupportedServiceNames (void)
62     throw (RuntimeException)
63 {
64 	static const ::rtl::OUString sServiceName(
65         ::rtl::OUString::createFromAscii("com.sun.star.drawing.framework.BasicToolBarFactory"));
66 	return Sequence<rtl::OUString>(&sServiceName, 1);
67 }
68 
69 
70 
71 
72 
73 //===== BasicToolBarFactory ===================================================
74 
BasicToolBarFactory(const Reference<XComponentContext> & rxContext)75 BasicToolBarFactory::BasicToolBarFactory (
76     const Reference<XComponentContext>& rxContext)
77     : BasicToolBarFactoryInterfaceBase(m_aMutex),
78       mxConfigurationController(),
79       mxController(),
80       mpViewShellBase(NULL)
81 {
82     (void)rxContext;
83 }
84 
85 
86 
87 
~BasicToolBarFactory(void)88 BasicToolBarFactory::~BasicToolBarFactory (void)
89 {
90 }
91 
92 
93 
94 
disposing(void)95 void SAL_CALL BasicToolBarFactory::disposing (void)
96 {
97     Shutdown();
98 }
99 
100 
101 
102 
Shutdown(void)103 void BasicToolBarFactory::Shutdown (void)
104 {
105     mpViewShellBase = NULL;
106     Reference<lang::XComponent> xComponent (mxConfigurationController, UNO_QUERY);
107     if (xComponent.is())
108         xComponent->removeEventListener(static_cast<lang::XEventListener*>(this));
109     if (mxConfigurationController.is())
110     {
111         mxConfigurationController->removeResourceFactoryForReference(this);
112         mxConfigurationController = NULL;
113     }
114 }
115 
116 
117 
118 
119 //----- XInitialization -------------------------------------------------------
120 
initialize(const Sequence<Any> & aArguments)121 void SAL_CALL BasicToolBarFactory::initialize (const Sequence<Any>& aArguments)
122     throw (Exception, RuntimeException)
123 {
124     if (aArguments.getLength() > 0)
125     {
126         try
127         {
128             // Get the XController from the first argument.
129             mxController = Reference<frame::XController>(aArguments[0], UNO_QUERY_THROW);
130 
131             // Tunnel through the controller to obtain a ViewShellBase.
132             Reference<lang::XUnoTunnel> xTunnel (mxController, UNO_QUERY_THROW);
133             ::sd::DrawController* pController = reinterpret_cast<sd::DrawController*>(
134                 xTunnel->getSomething(sd::DrawController::getUnoTunnelId()));
135             if (pController != NULL)
136                 mpViewShellBase = pController->GetViewShellBase();
137 
138             ::comphelper::MediaDescriptor aDescriptor (mxController->getModel()->getArgs());
139             if ( ! aDescriptor.getUnpackedValueOrDefault(
140                 ::comphelper::MediaDescriptor::PROP_PREVIEW(),
141                 sal_False))
142             {
143                 // Register the factory for its supported tool bars.
144                 Reference<XControllerManager> xControllerManager(mxController, UNO_QUERY_THROW);
145                 mxConfigurationController = xControllerManager->getConfigurationController();
146                 if (mxConfigurationController.is())
147                 {
148                     mxConfigurationController->addResourceFactory(
149                         FrameworkHelper::msViewTabBarURL, this);
150                 }
151 
152                 Reference<lang::XComponent> xComponent (mxConfigurationController, UNO_QUERY);
153                 if (xComponent.is())
154                     xComponent->addEventListener(static_cast<lang::XEventListener*>(this));
155             }
156             else
157             {
158                 // The view shell is in preview mode and thus does not need
159                 // the view tab bar.
160                 mxConfigurationController = NULL;
161             }
162         }
163         catch (RuntimeException&)
164         {
165             Shutdown();
166             throw;
167         }
168     }
169 }
170 
171 
172 
173 
174 //----- lang::XEventListener --------------------------------------------------
175 
disposing(const lang::EventObject & rEventObject)176 void SAL_CALL BasicToolBarFactory::disposing (
177     const lang::EventObject& rEventObject)
178     throw (RuntimeException)
179 {
180     if (rEventObject.Source == mxConfigurationController)
181         mxConfigurationController = NULL;
182 }
183 
184 
185 
186 
187 //===== XPaneFactory ==========================================================
188 
createResource(const Reference<XResourceId> & rxToolBarId)189 Reference<XResource> SAL_CALL BasicToolBarFactory::createResource (
190     const Reference<XResourceId>& rxToolBarId)
191     throw (RuntimeException, IllegalArgumentException, WrappedTargetException)
192 {
193     ThrowIfDisposed();
194 
195     Reference<XResource> xToolBar;
196 
197     if (rxToolBarId->getResourceURL().equals(FrameworkHelper::msViewTabBarURL))
198     {
199         xToolBar = new ViewTabBar(rxToolBarId, mxController);
200     }
201     else
202         throw lang::IllegalArgumentException();
203 
204 
205     return xToolBar;
206 }
207 
208 
209 
210 
211 
releaseResource(const Reference<XResource> & rxToolBar)212 void SAL_CALL BasicToolBarFactory::releaseResource (
213     const Reference<XResource>& rxToolBar)
214     throw (RuntimeException)
215 {
216     ThrowIfDisposed();
217 
218     Reference<XComponent> xComponent (rxToolBar, UNO_QUERY);
219     if (xComponent.is())
220         xComponent->dispose();
221 }
222 
223 
224 
225 
ThrowIfDisposed(void) const226 void BasicToolBarFactory::ThrowIfDisposed (void) const
227     throw (lang::DisposedException)
228 {
229 	if (rBHelper.bDisposed || rBHelper.bInDispose)
230 	{
231         throw lang::DisposedException (
232             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
233                 "BasicToolBarFactory object has already been disposed")),
234             const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this)));
235     }
236 }
237 
238 
239 
240 
241 } } // end of namespace sd::framework
242