xref: /AOO41X/main/sd/source/ui/framework/module/ShellStackGuard.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "precompiled_sd.hxx"
29 
30 #include "ShellStackGuard.hxx"
31 
32 #include "framework/ConfigurationController.hxx"
33 #include "framework/FrameworkHelper.hxx"
34 
35 #include "DrawController.hxx"
36 #include "ViewShellBase.hxx"
37 #include <sfx2/printer.hxx>
38 #include <com/sun/star/drawing/framework/XControllerManager.hpp>
39 #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
40 
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::drawing::framework;
44 
45 using ::rtl::OUString;
46 using ::sd::framework::FrameworkHelper;
47 
48 
49 namespace sd { namespace framework {
50 
51 //===== CenterViewFocusModule ====================================================
52 
53 ShellStackGuard::ShellStackGuard (Reference<frame::XController>& rxController)
54     : ShellStackGuardInterfaceBase(m_aMutex),
55       mxConfigurationController(),
56       mpBase(NULL),
57       mpUpdateLock(),
58       maPrinterPollingTimer()
59 {
60     Reference<XControllerManager> xControllerManager (rxController, UNO_QUERY);
61     if (xControllerManager.is())
62     {
63         mxConfigurationController = xControllerManager->getConfigurationController();
64 
65         // Tunnel through the controller to obtain a ViewShellBase.
66         Reference<lang::XUnoTunnel> xTunnel (rxController, UNO_QUERY);
67         if (xTunnel.is())
68         {
69             ::sd::DrawController* pController = reinterpret_cast<sd::DrawController*>(
70                 xTunnel->getSomething(sd::DrawController::getUnoTunnelId()));
71             if (pController != NULL)
72                 mpBase = pController->GetViewShellBase();
73         }
74     }
75 
76     if (mxConfigurationController.is())
77     {
78         // Listen for update starts so that the following update can be
79         // prevented in case of a printing printer.
80         mxConfigurationController->addConfigurationChangeListener(
81             this,
82             FrameworkHelper::msConfigurationUpdateStartEvent,
83             Any());
84 
85         // Prepare the printer polling.
86         maPrinterPollingTimer.SetTimeoutHdl(LINK(this,ShellStackGuard,TimeoutHandler));
87         maPrinterPollingTimer.SetTimeout(300);
88     }
89 }
90 
91 
92 
93 
94 ShellStackGuard::~ShellStackGuard (void)
95 {
96 }
97 
98 
99 
100 
101 void SAL_CALL ShellStackGuard::disposing (void)
102 {
103     if (mxConfigurationController.is())
104         mxConfigurationController->removeConfigurationChangeListener(this);
105 
106     mxConfigurationController = NULL;
107     mpBase = NULL;
108 }
109 
110 
111 
112 
113 void SAL_CALL ShellStackGuard::notifyConfigurationChange (
114     const ConfigurationChangeEvent& rEvent)
115     throw (RuntimeException)
116 {
117     if (rEvent.Type.equals(FrameworkHelper::msConfigurationUpdateStartEvent))
118     {
119         if (mpUpdateLock.get() == NULL && IsPrinting())
120         {
121             // Prevent configuration updates while the printer is printing.
122             mpUpdateLock.reset(new ConfigurationController::Lock(mxConfigurationController));
123 
124             // Start polling for the printer having finished printing.
125             maPrinterPollingTimer.Start();
126         }
127     }
128 }
129 
130 
131 
132 
133 void SAL_CALL ShellStackGuard::disposing (
134     const lang::EventObject& rEvent)
135     throw (RuntimeException)
136 {
137     if (mxConfigurationController.is())
138         if (rEvent.Source == mxConfigurationController)
139         {
140             mxConfigurationController = NULL;
141             mpBase = NULL;
142         }
143 }
144 
145 
146 
147 
148 IMPL_LINK(ShellStackGuard, TimeoutHandler, Timer*, pTimer)
149 {
150 #ifdef DEBUG
151     OSL_ASSERT(pTimer==&maPrinterPollingTimer);
152 #else
153     (void)pTimer;
154 #endif
155     if (mpUpdateLock.get() != NULL)
156     {
157         if ( ! IsPrinting())
158         {
159             // Printing finished.  Release the update lock.
160             mpUpdateLock.reset();
161         }
162         else
163         {
164             // Wait long for the printing to finish.
165             maPrinterPollingTimer.Start();
166         }
167     }
168 
169     return 0;
170 }
171 
172 
173 
174 
175 
176 bool ShellStackGuard::IsPrinting (void) const
177 {
178     if (mpBase != NULL)
179     {
180         SfxPrinter* pPrinter = mpBase->GetPrinter();
181         if (pPrinter != NULL
182             && pPrinter->IsPrinting())
183         {
184             return true;
185         }
186     }
187 
188     return false;
189 }
190 
191 
192 } } // end of namespace sd::framework
193