xref: /trunk/main/framework/source/dispatch/startmoduledispatcher.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 #include <dispatch/startmoduledispatcher.hxx>
28 
29 //_______________________________________________
30 // my own includes
31 
32 #include <pattern/frame.hxx>
33 #include <threadhelp/readguard.hxx>
34 #include <threadhelp/writeguard.hxx>
35 #include <framework/framelistanalyzer.hxx>
36 #include <dispatchcommands.h>
37 #include <targets.h>
38 #include <services.h>
39 #include <general.h>
40 
41 //_______________________________________________
42 // interface includes
43 #include <com/sun/star/frame/XDesktop.hpp>
44 #include <com/sun/star/frame/XController.hpp>
45 #include <com/sun/star/frame/CommandGroup.hpp>
46 #include <com/sun/star/awt/XTopWindow.hpp>
47 #include "com/sun/star/beans/XFastPropertySet.hpp"
48 #include <com/sun/star/frame/XModuleManager.hpp>
49 
50 //_______________________________________________
51 // includes of other projects
52 #include <toolkit/helper/vclunohelper.hxx>
53 #include <vcl/window.hxx>
54 #include <vcl/svapp.hxx>
55 #include <vos/mutex.hxx>
56 #include <unotools/moduleoptions.hxx>
57 
58 //_______________________________________________
59 // namespace
60 
61 namespace framework{
62 
63 #ifdef fpf
64     #error "Who uses \"fpf\" as define. It will overwrite my namespace alias ..."
65 #endif
66 namespace fpf = ::framework::pattern::frame;
67 
68 //_______________________________________________
69 // declarations
70 
71 DEFINE_XINTERFACE_4(StartModuleDispatcher                                     ,
72                     OWeakObject                                               ,
73                     DIRECT_INTERFACE(css::lang::XTypeProvider                ),
74                     DIRECT_INTERFACE(css::frame::XNotifyingDispatch          ),
75                     DIRECT_INTERFACE(css::frame::XDispatch                   ),
76                     DIRECT_INTERFACE(css::frame::XDispatchInformationProvider))
77 
78 // Note: XStatusListener is an implementation detail. Hide it for scripting!
79 DEFINE_XTYPEPROVIDER_4(StartModuleDispatcher                   ,
80                        css::lang::XTypeProvider                ,
81                        css::frame::XDispatchInformationProvider,
82                        css::frame::XNotifyingDispatch          ,
83                        css::frame::XDispatch                   )
84 
85 //-----------------------------------------------
86 StartModuleDispatcher::StartModuleDispatcher(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR  ,
87                                              const css::uno::Reference< css::frame::XFrame >&              xFrame ,
88                                              const ::rtl::OUString&                                        sTarget)
89     : ThreadHelpBase     (&Application::GetSolarMutex() )
90     , ::cppu::OWeakObject(                              )
91     , m_xSMGR            (xSMGR                         )
92     , m_xOwner           (xFrame                        )
93     , m_sDispatchTarget  (sTarget                       )
94     , m_lStatusListener  (m_aLock.getShareableOslMutex())
95 {
96 }
97 
98 //-----------------------------------------------
99 StartModuleDispatcher::~StartModuleDispatcher()
100 {
101 }
102 
103 //-----------------------------------------------
104 void SAL_CALL StartModuleDispatcher::dispatch(const css::util::URL&                                  aURL      ,
105                                               const css::uno::Sequence< css::beans::PropertyValue >& lArguments)
106 {
107     dispatchWithNotification(aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >());
108 }
109 
110 //-----------------------------------------------
111 void SAL_CALL StartModuleDispatcher::dispatchWithNotification(const css::util::URL&                                             aURL      ,
112                                                               const css::uno::Sequence< css::beans::PropertyValue >&            /*lArguments*/,
113                                                               const css::uno::Reference< css::frame::XDispatchResultListener >& xListener )
114 {
115     ::sal_Int16 nResult = css::frame::DispatchResultState::DONTKNOW;
116     if (aURL.Complete.equals (CMD_UNO_SHOWSTARTMODULE))
117     {
118         nResult = css::frame::DispatchResultState::FAILURE;
119         if (implts_isBackingModePossible ())
120         {
121             if (implts_establishBackingMode ())
122                 nResult = css::frame::DispatchResultState::SUCCESS;
123         }
124     }
125 
126     implts_notifyResultListener(xListener, nResult, css::uno::Any());
127 }
128 
129 //-----------------------------------------------
130 css::uno::Sequence< ::sal_Int16 > SAL_CALL StartModuleDispatcher::getSupportedCommandGroups()
131 {
132     return css::uno::Sequence< ::sal_Int16 >();
133 }
134 
135 //-----------------------------------------------
136 css::uno::Sequence< css::frame::DispatchInformation > SAL_CALL StartModuleDispatcher::getConfigurableDispatchInformation(::sal_Int16 /*nCommandGroup*/)
137 {
138     return css::uno::Sequence< css::frame::DispatchInformation >();
139 }
140 
141 //-----------------------------------------------
142 void SAL_CALL StartModuleDispatcher::addStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/,
143                                                        const css::util::URL&                                     /*aURL*/     )
144 {
145 }
146 
147 //-----------------------------------------------
148 void SAL_CALL StartModuleDispatcher::removeStatusListener(const css::uno::Reference< css::frame::XStatusListener >& /*xListener*/,
149                                                           const css::util::URL&                                     /*aURL*/     )
150 {
151 }
152 
153 //-----------------------------------------------
154 ::sal_Bool StartModuleDispatcher::implts_isBackingModePossible ()
155 {
156     if ( ! SvtModuleOptions().IsModuleInstalled(SvtModuleOptions::E_SSTARTMODULE))
157         return sal_False;
158 
159     // SAFE -> ----------------------------------
160     ReadGuard aReadLock(m_aLock);
161     css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
162     aReadLock.unlock();
163     // <- SAFE ----------------------------------
164 
165     css::uno::Reference< css::frame::XFramesSupplier > xDesktop(
166         xSMGR->createInstance(SERVICENAME_DESKTOP), css::uno::UNO_QUERY);
167 
168     FrameListAnalyzer aCheck(
169         xDesktop,
170         css::uno::Reference< css::frame::XFrame >(),
171         FrameListAnalyzer::E_HELP | FrameListAnalyzer::E_BACKINGCOMPONENT);
172 
173     ::sal_Bool  bIsPossible    = sal_False;
174     ::sal_Int32 nVisibleFrames = aCheck.m_lOtherVisibleFrames.getLength ();
175 
176     if (
177         ( ! aCheck.m_xBackingComponent.is ()) &&
178         (   nVisibleFrames < 1              )
179        )
180     {
181         bIsPossible = sal_True;
182     }
183 
184     return bIsPossible;
185 }
186 
187 //-----------------------------------------------
188 ::sal_Bool StartModuleDispatcher::implts_establishBackingMode()
189 {
190     // SAFE -> ----------------------------------
191     ReadGuard aReadLock(m_aLock);
192     css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR  = m_xSMGR;
193     aReadLock.unlock();
194     // <- SAFE ----------------------------------
195 
196     css::uno::Reference< css::frame::XFrame > xDesktop         (xSMGR->createInstance(SERVICENAME_DESKTOP), css::uno::UNO_QUERY);
197     css::uno::Reference< css::frame::XFrame > xFrame           = xDesktop->findFrame (SPECIALTARGET_BLANK, 0);
198     css::uno::Reference< css::awt::XWindow  > xContainerWindow = xFrame->getContainerWindow ();
199 
200     css::uno::Sequence< css::uno::Any > lArgs(1);
201     lArgs[0] <<= xContainerWindow;
202 
203     css::uno::Reference< css::frame::XController > xStartModule(
204         xSMGR->createInstanceWithArguments(SERVICENAME_STARTMODULE, lArgs),
205         css::uno::UNO_QUERY_THROW);
206 
207     css::uno::Reference< css::awt::XWindow > xComponentWindow(xStartModule, css::uno::UNO_QUERY);
208     xFrame->setComponent(xComponentWindow, xStartModule);
209     xStartModule->attachFrame(xFrame);
210     xContainerWindow->setVisible(sal_True);
211 
212     return sal_True;
213 }
214 
215 //-----------------------------------------------
216 void StartModuleDispatcher::implts_notifyResultListener(const css::uno::Reference< css::frame::XDispatchResultListener >& xListener,
217                                                               ::sal_Int16                                                 nState   ,
218                                                         const css::uno::Any&                                              aResult  )
219 {
220     if ( ! xListener.is())
221         return;
222 
223     css::frame::DispatchResultEvent aEvent(
224         css::uno::Reference< css::uno::XInterface >(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY),
225         nState,
226         aResult);
227 
228     xListener->dispatchFinished(aEvent);
229 }
230 
231 } // namespace framework
232