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 "ShellStackGuard.hxx"
27 
28 #include "framework/ConfigurationController.hxx"
29 #include "framework/FrameworkHelper.hxx"
30 
31 #include "DrawController.hxx"
32 #include "ViewShellBase.hxx"
33 #include <sfx2/printer.hxx>
34 #include <com/sun/star/drawing/framework/XControllerManager.hpp>
35 #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
36 
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::drawing::framework;
40 
41 using ::rtl::OUString;
42 using ::sd::framework::FrameworkHelper;
43 
44 
45 namespace sd { namespace framework {
46 
47 //===== CenterViewFocusModule ====================================================
48 
49 ShellStackGuard::ShellStackGuard (Reference<frame::XController>& rxController)
50     : ShellStackGuardInterfaceBase(m_aMutex),
51       mxConfigurationController(),
52       mpBase(NULL),
53       mpUpdateLock(),
54       maPrinterPollingTimer()
55 {
56     Reference<XControllerManager> xControllerManager (rxController, UNO_QUERY);
57     if (xControllerManager.is())
58     {
59         mxConfigurationController = xControllerManager->getConfigurationController();
60 
61         // Tunnel through the controller to obtain a ViewShellBase.
62         Reference<lang::XUnoTunnel> xTunnel (rxController, UNO_QUERY);
63         if (xTunnel.is())
64         {
65             ::sd::DrawController* pController = reinterpret_cast<sd::DrawController*>(
66                 xTunnel->getSomething(sd::DrawController::getUnoTunnelId()));
67             if (pController != NULL)
68                 mpBase = pController->GetViewShellBase();
69         }
70     }
71 
72     if (mxConfigurationController.is())
73     {
74         // Listen for update starts so that the following update can be
75         // prevented in case of a printing printer.
76         mxConfigurationController->addConfigurationChangeListener(
77             this,
78             FrameworkHelper::msConfigurationUpdateStartEvent,
79             Any());
80 
81         // Prepare the printer polling.
82         maPrinterPollingTimer.SetTimeoutHdl(LINK(this,ShellStackGuard,TimeoutHandler));
83         maPrinterPollingTimer.SetTimeout(300);
84     }
85 }
86 
87 
88 
89 
90 ShellStackGuard::~ShellStackGuard (void)
91 {
92 }
93 
94 
95 
96 
97 void SAL_CALL ShellStackGuard::disposing (void)
98 {
99     if (mxConfigurationController.is())
100         mxConfigurationController->removeConfigurationChangeListener(this);
101 
102     mxConfigurationController = NULL;
103     mpBase = NULL;
104 }
105 
106 
107 
108 
109 void SAL_CALL ShellStackGuard::notifyConfigurationChange (
110     const ConfigurationChangeEvent& rEvent)
111     throw (RuntimeException)
112 {
113     if (rEvent.Type.equals(FrameworkHelper::msConfigurationUpdateStartEvent))
114     {
115         if (mpUpdateLock.get() == NULL && IsPrinting())
116         {
117             // Prevent configuration updates while the printer is printing.
118             mpUpdateLock.reset(new ConfigurationController::Lock(mxConfigurationController));
119 
120             // Start polling for the printer having finished printing.
121             maPrinterPollingTimer.Start();
122         }
123     }
124 }
125 
126 
127 
128 
129 void SAL_CALL ShellStackGuard::disposing (
130     const lang::EventObject& rEvent)
131     throw (RuntimeException)
132 {
133     if (mxConfigurationController.is())
134         if (rEvent.Source == mxConfigurationController)
135         {
136             mxConfigurationController = NULL;
137             mpBase = NULL;
138         }
139 }
140 
141 
142 
143 
144 IMPL_LINK(ShellStackGuard, TimeoutHandler, Timer*, pTimer)
145 {
146 #ifdef DEBUG
147     OSL_ASSERT(pTimer==&maPrinterPollingTimer);
148 #else
149     (void)pTimer;
150 #endif
151     if (mpUpdateLock.get() != NULL)
152     {
153         if ( ! IsPrinting())
154         {
155             // Printing finished.  Release the update lock.
156             mpUpdateLock.reset();
157         }
158         else
159         {
160             // Wait long for the printing to finish.
161             maPrinterPollingTimer.Start();
162         }
163     }
164 
165     return 0;
166 }
167 
168 
169 
170 
171 
172 bool ShellStackGuard::IsPrinting (void) const
173 {
174     if (mpBase != NULL)
175     {
176         SfxPrinter* pPrinter = mpBase->GetPrinter();
177         if (pPrinter != NULL
178             && pPrinter->IsPrinting())
179         {
180             return true;
181         }
182     }
183 
184     return false;
185 }
186 
187 
188 } } // end of namespace sd::framework
189