1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5b190011SAndrew Rist  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5b190011SAndrew Rist  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19*5b190011SAndrew Rist  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sd.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "ShellStackGuard.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "framework/ConfigurationController.hxx"
29cdf0e10cSrcweir #include "framework/FrameworkHelper.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "DrawController.hxx"
32cdf0e10cSrcweir #include "ViewShellBase.hxx"
33cdf0e10cSrcweir #include <sfx2/printer.hxx>
34cdf0e10cSrcweir #include <com/sun/star/drawing/framework/XControllerManager.hpp>
35cdf0e10cSrcweir #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace ::com::sun::star;
38cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using ::rtl::OUString;
42cdf0e10cSrcweir using ::sd::framework::FrameworkHelper;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace sd { namespace framework {
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //===== CenterViewFocusModule ====================================================
48cdf0e10cSrcweir 
ShellStackGuard(Reference<frame::XController> & rxController)49cdf0e10cSrcweir ShellStackGuard::ShellStackGuard (Reference<frame::XController>& rxController)
50cdf0e10cSrcweir     : ShellStackGuardInterfaceBase(m_aMutex),
51cdf0e10cSrcweir       mxConfigurationController(),
52cdf0e10cSrcweir       mpBase(NULL),
53cdf0e10cSrcweir       mpUpdateLock(),
54cdf0e10cSrcweir       maPrinterPollingTimer()
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     Reference<XControllerManager> xControllerManager (rxController, UNO_QUERY);
57cdf0e10cSrcweir     if (xControllerManager.is())
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir         mxConfigurationController = xControllerManager->getConfigurationController();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir         // Tunnel through the controller to obtain a ViewShellBase.
62cdf0e10cSrcweir         Reference<lang::XUnoTunnel> xTunnel (rxController, UNO_QUERY);
63cdf0e10cSrcweir         if (xTunnel.is())
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             ::sd::DrawController* pController = reinterpret_cast<sd::DrawController*>(
66cdf0e10cSrcweir                 xTunnel->getSomething(sd::DrawController::getUnoTunnelId()));
67cdf0e10cSrcweir             if (pController != NULL)
68cdf0e10cSrcweir                 mpBase = pController->GetViewShellBase();
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     if (mxConfigurationController.is())
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         // Listen for update starts so that the following update can be
75cdf0e10cSrcweir         // prevented in case of a printing printer.
76cdf0e10cSrcweir         mxConfigurationController->addConfigurationChangeListener(
77cdf0e10cSrcweir             this,
78cdf0e10cSrcweir             FrameworkHelper::msConfigurationUpdateStartEvent,
79cdf0e10cSrcweir             Any());
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         // Prepare the printer polling.
82cdf0e10cSrcweir         maPrinterPollingTimer.SetTimeoutHdl(LINK(this,ShellStackGuard,TimeoutHandler));
83cdf0e10cSrcweir         maPrinterPollingTimer.SetTimeout(300);
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
~ShellStackGuard(void)90cdf0e10cSrcweir ShellStackGuard::~ShellStackGuard (void)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 
disposing(void)97cdf0e10cSrcweir void SAL_CALL ShellStackGuard::disposing (void)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir     if (mxConfigurationController.is())
100cdf0e10cSrcweir         mxConfigurationController->removeConfigurationChangeListener(this);
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     mxConfigurationController = NULL;
103cdf0e10cSrcweir     mpBase = NULL;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 
notifyConfigurationChange(const ConfigurationChangeEvent & rEvent)109cdf0e10cSrcweir void SAL_CALL ShellStackGuard::notifyConfigurationChange (
110cdf0e10cSrcweir     const ConfigurationChangeEvent& rEvent)
111cdf0e10cSrcweir     throw (RuntimeException)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     if (rEvent.Type.equals(FrameworkHelper::msConfigurationUpdateStartEvent))
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         if (mpUpdateLock.get() == NULL && IsPrinting())
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             // Prevent configuration updates while the printer is printing.
118cdf0e10cSrcweir             mpUpdateLock.reset(new ConfigurationController::Lock(mxConfigurationController));
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             // Start polling for the printer having finished printing.
121cdf0e10cSrcweir             maPrinterPollingTimer.Start();
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 
disposing(const lang::EventObject & rEvent)129cdf0e10cSrcweir void SAL_CALL ShellStackGuard::disposing (
130cdf0e10cSrcweir     const lang::EventObject& rEvent)
131cdf0e10cSrcweir     throw (RuntimeException)
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     if (mxConfigurationController.is())
134cdf0e10cSrcweir         if (rEvent.Source == mxConfigurationController)
135cdf0e10cSrcweir         {
136cdf0e10cSrcweir             mxConfigurationController = NULL;
137cdf0e10cSrcweir             mpBase = NULL;
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 
IMPL_LINK(ShellStackGuard,TimeoutHandler,Timer *,pTimer)144cdf0e10cSrcweir IMPL_LINK(ShellStackGuard, TimeoutHandler, Timer*, pTimer)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir #ifdef DEBUG
147cdf0e10cSrcweir     OSL_ASSERT(pTimer==&maPrinterPollingTimer);
148cdf0e10cSrcweir #else
149cdf0e10cSrcweir     (void)pTimer;
150cdf0e10cSrcweir #endif
151cdf0e10cSrcweir     if (mpUpdateLock.get() != NULL)
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         if ( ! IsPrinting())
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             // Printing finished.  Release the update lock.
156cdf0e10cSrcweir             mpUpdateLock.reset();
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir         else
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             // Wait long for the printing to finish.
161cdf0e10cSrcweir             maPrinterPollingTimer.Start();
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     return 0;
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 
IsPrinting(void) const172cdf0e10cSrcweir bool ShellStackGuard::IsPrinting (void) const
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     if (mpBase != NULL)
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         SfxPrinter* pPrinter = mpBase->GetPrinter();
177cdf0e10cSrcweir         if (pPrinter != NULL
178cdf0e10cSrcweir             && pPrinter->IsPrinting())
179cdf0e10cSrcweir         {
180cdf0e10cSrcweir             return true;
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     return false;
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 
188cdf0e10cSrcweir } } // end of namespace sd::framework
189