Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <pattern/window.hxx>
21 : #include <helper/persistentwindowstate.hxx>
22 : #include <threadhelp/writeguard.hxx>
23 : #include <threadhelp/readguard.hxx>
24 : #include <macros/generic.hxx>
25 : #include <services.h>
26 :
27 : #include <com/sun/star/awt/XWindow.hpp>
28 :
29 : #include <com/sun/star/lang/XServiceInfo.hpp>
30 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 : #include <com/sun/star/frame/ModuleManager.hpp>
32 :
33 : #include <comphelper/processfactory.hxx>
34 : #include <comphelper/configurationhelper.hxx>
35 : #include <vcl/window.hxx>
36 : #include <vcl/syswin.hxx>
37 :
38 : #include <toolkit/unohlp.hxx>
39 : #include <vcl/svapp.hxx>
40 : #include <vcl/wrkwin.hxx>
41 : #include <rtl/string.hxx>
42 :
43 :
44 : namespace framework{
45 :
46 :
47 : //*****************************************************************************************************************
48 : // XInterface, XTypeProvider
49 :
50 6120 : DEFINE_XINTERFACE_4(PersistentWindowState ,
51 : OWeakObject ,
52 : DIRECT_INTERFACE (css::lang::XTypeProvider ),
53 : DIRECT_INTERFACE (css::lang::XInitialization ),
54 : DIRECT_INTERFACE (css::frame::XFrameActionListener ),
55 : DERIVED_INTERFACE(css::lang::XEventListener,css::frame::XFrameActionListener))
56 :
57 0 : DEFINE_XTYPEPROVIDER_4(PersistentWindowState ,
58 : css::lang::XTypeProvider ,
59 : css::lang::XInitialization ,
60 : css::frame::XFrameActionListener,
61 : css::lang::XEventListener )
62 :
63 : //*****************************************************************************************************************
64 516 : PersistentWindowState::PersistentWindowState(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
65 516 : : ThreadHelpBase (&Application::GetSolarMutex())
66 : , m_xSMGR (xSMGR )
67 1032 : , m_bWindowStateAlreadySet(sal_False )
68 : {
69 516 : }
70 :
71 : //*****************************************************************************************************************
72 196 : PersistentWindowState::~PersistentWindowState()
73 : {
74 196 : }
75 :
76 : //*****************************************************************************************************************
77 516 : void SAL_CALL PersistentWindowState::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
78 : throw(css::uno::Exception ,
79 : css::uno::RuntimeException)
80 : {
81 : // check arguments
82 516 : css::uno::Reference< css::frame::XFrame > xFrame;
83 516 : if (lArguments.getLength() < 1)
84 : throw css::lang::IllegalArgumentException(
85 : DECLARE_ASCII("Empty argument list!"),
86 : static_cast< ::cppu::OWeakObject* >(this),
87 0 : 1);
88 :
89 516 : lArguments[0] >>= xFrame;
90 516 : if (!xFrame.is())
91 : throw css::lang::IllegalArgumentException(
92 : DECLARE_ASCII("No valid frame specified!"),
93 : static_cast< ::cppu::OWeakObject* >(this),
94 0 : 1);
95 :
96 : // SAFE -> ----------------------------------
97 516 : WriteGuard aWriteLock(m_aLock);
98 : // hold the frame as weak reference(!) so it can die everytimes :-)
99 516 : m_xFrame = xFrame;
100 516 : aWriteLock.unlock();
101 : // <- SAFE ----------------------------------
102 :
103 : // start listening
104 516 : xFrame->addFrameActionListener(this);
105 516 : }
106 :
107 : //*****************************************************************************************************************
108 2366 : void SAL_CALL PersistentWindowState::frameAction(const css::frame::FrameActionEvent& aEvent)
109 : throw(css::uno::RuntimeException)
110 : {
111 : // SAFE -> ----------------------------------
112 2366 : ReadGuard aReadLock(m_aLock);
113 2366 : css::uno::Reference< css::uno::XComponentContext > xContext = comphelper::getComponentContext(m_xSMGR);
114 2366 : css::uno::Reference< css::frame::XFrame > xFrame(m_xFrame.get(), css::uno::UNO_QUERY);
115 2366 : sal_Bool bRestoreWindowState = !m_bWindowStateAlreadySet;
116 2366 : aReadLock.unlock();
117 : // <- SAFE ----------------------------------
118 :
119 : // frame already gone ? We hold it weak only ...
120 2366 : if (!xFrame.is())
121 : return;
122 :
123 : // no window -> no position and size available
124 2366 : css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
125 2366 : if (!xWindow.is())
126 : return;
127 :
128 : // unknown module -> no configuration available!
129 2366 : ::rtl::OUString sModuleName = PersistentWindowState::implst_identifyModule(xContext, xFrame);
130 2366 : if (sModuleName.isEmpty())
131 : return;
132 :
133 1152 : switch(aEvent.Action)
134 : {
135 : case css::frame::FrameAction_COMPONENT_ATTACHED :
136 : {
137 516 : if (bRestoreWindowState)
138 : {
139 516 : ::rtl::OUString sWindowState = PersistentWindowState::implst_getWindowStateFromConfig(xContext, sModuleName);
140 516 : PersistentWindowState::implst_setWindowStateOnWindow(xWindow,sWindowState);
141 : // SAFE -> ----------------------------------
142 516 : WriteGuard aWriteLock(m_aLock);
143 516 : m_bWindowStateAlreadySet = sal_True;
144 516 : aWriteLock.unlock();
145 : // <- SAFE ----------------------------------
146 : }
147 : }
148 516 : break;
149 :
150 : case css::frame::FrameAction_COMPONENT_REATTACHED :
151 : {
152 : // nothing todo here, because its not allowed to change position and size
153 : // of an alredy existing frame!
154 : }
155 0 : break;
156 :
157 : case css::frame::FrameAction_COMPONENT_DETACHING :
158 : {
159 98 : ::rtl::OUString sWindowState = PersistentWindowState::implst_getWindowStateFromWindow(xWindow);
160 98 : PersistentWindowState::implst_setWindowStateOnConfig(xContext, sModuleName, sWindowState);
161 : }
162 98 : break;
163 : default:
164 538 : break;
165 2366 : }
166 : }
167 :
168 : //*****************************************************************************************************************
169 98 : void SAL_CALL PersistentWindowState::disposing(const css::lang::EventObject&)
170 : throw(css::uno::RuntimeException)
171 : {
172 : // nothing todo here - because we hold the frame as weak reference only
173 98 : }
174 :
175 : //*****************************************************************************************************************
176 2366 : ::rtl::OUString PersistentWindowState::implst_identifyModule(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
177 : const css::uno::Reference< css::frame::XFrame >& xFrame)
178 : {
179 2366 : ::rtl::OUString sModuleName;
180 :
181 : css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
182 2366 : css::frame::ModuleManager::create( rxContext );
183 :
184 : try
185 : {
186 2366 : sModuleName = xModuleManager->identify(xFrame);
187 : }
188 0 : catch(const css::uno::RuntimeException&)
189 0 : { throw; }
190 2428 : catch(const css::uno::Exception&)
191 1214 : { sModuleName = ::rtl::OUString(); }
192 :
193 2366 : return sModuleName;
194 : }
195 :
196 : //*****************************************************************************************************************
197 516 : ::rtl::OUString PersistentWindowState::implst_getWindowStateFromConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
198 : const ::rtl::OUString& sModuleName)
199 : {
200 516 : ::rtl::OUString sWindowState;
201 :
202 516 : ::rtl::OUStringBuffer sRelPathBuf(256);
203 516 : sRelPathBuf.appendAscii("Office/Factories/*[\"");
204 516 : sRelPathBuf.append (sModuleName );
205 516 : sRelPathBuf.appendAscii("\"]" );
206 :
207 516 : ::rtl::OUString sPackage("org.openoffice.Setup/");
208 516 : ::rtl::OUString sRelPath = sRelPathBuf.makeStringAndClear();
209 516 : ::rtl::OUString sKey("ooSetupFactoryWindowAttributes");
210 :
211 : try
212 : {
213 : ::comphelper::ConfigurationHelper::readDirectKey(rxContext,
214 : sPackage,
215 : sRelPath,
216 : sKey,
217 516 : ::comphelper::ConfigurationHelper::E_READONLY) >>= sWindowState;
218 : }
219 0 : catch(const css::uno::RuntimeException&)
220 0 : { throw; }
221 16 : catch(const css::uno::Exception&)
222 8 : { sWindowState = ::rtl::OUString(); }
223 :
224 516 : return sWindowState;
225 : }
226 :
227 : //*****************************************************************************************************************
228 98 : void PersistentWindowState::implst_setWindowStateOnConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
229 : const ::rtl::OUString& sModuleName ,
230 : const ::rtl::OUString& sWindowState)
231 : {
232 98 : ::rtl::OUStringBuffer sRelPathBuf(256);
233 98 : sRelPathBuf.appendAscii("Office/Factories/*[\"");
234 98 : sRelPathBuf.append (sModuleName );
235 98 : sRelPathBuf.appendAscii("\"]" );
236 :
237 98 : ::rtl::OUString sPackage("org.openoffice.Setup/");
238 98 : ::rtl::OUString sRelPath = sRelPathBuf.makeStringAndClear();
239 98 : ::rtl::OUString sKey("ooSetupFactoryWindowAttributes");
240 :
241 : try
242 : {
243 : ::comphelper::ConfigurationHelper::writeDirectKey(rxContext,
244 : sPackage,
245 : sRelPath,
246 : sKey,
247 : css::uno::makeAny(sWindowState),
248 98 : ::comphelper::ConfigurationHelper::E_STANDARD);
249 : }
250 0 : catch(const css::uno::RuntimeException&)
251 0 : { throw; }
252 0 : catch(const css::uno::Exception&)
253 98 : {}
254 98 : }
255 :
256 : //*****************************************************************************************************************
257 98 : ::rtl::OUString PersistentWindowState::implst_getWindowStateFromWindow(const css::uno::Reference< css::awt::XWindow >& xWindow)
258 : {
259 98 : ::rtl::OUString sWindowState;
260 :
261 98 : if (xWindow.is())
262 : {
263 : // SOLAR SAFE -> ------------------------
264 98 : SolarMutexGuard aSolarGuard;
265 :
266 98 : Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
267 : // check for system window is neccessary to guarantee correct pointer cast!
268 196 : if (
269 : (pWindow ) &&
270 98 : (pWindow->IsSystemWindow())
271 : )
272 : {
273 98 : sal_uLong nMask = WINDOWSTATE_MASK_ALL;
274 98 : nMask &= ~(WINDOWSTATE_MASK_MINIMIZED);
275 : sWindowState = rtl::OStringToOUString(
276 : ((SystemWindow*)pWindow)->GetWindowState(nMask),
277 98 : RTL_TEXTENCODING_UTF8);
278 98 : }
279 : // <- SOLAR SAFE ------------------------
280 : }
281 :
282 98 : return sWindowState;
283 : }
284 :
285 :
286 : //*********************************************************************************************************
287 516 : void PersistentWindowState::implst_setWindowStateOnWindow(const css::uno::Reference< css::awt::XWindow >& xWindow ,
288 : const ::rtl::OUString& sWindowState)
289 : {
290 1032 : if (
291 516 : (!xWindow.is() ) ||
292 516 : ( sWindowState.isEmpty() )
293 : )
294 : return;
295 :
296 : // SOLAR SAFE -> ------------------------
297 131 : SolarMutexGuard aSolarGuard;
298 :
299 131 : Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
300 131 : if (!pWindow)
301 : return;
302 :
303 : // check for system and work window - its neccessary to guarantee correct pointer cast!
304 131 : sal_Bool bSystemWindow = pWindow->IsSystemWindow();
305 131 : sal_Bool bWorkWindow = (pWindow->GetType() == WINDOW_WORKWINDOW);
306 :
307 131 : if (!bSystemWindow && !bWorkWindow)
308 : return;
309 :
310 131 : SystemWindow* pSystemWindow = (SystemWindow*)pWindow;
311 131 : WorkWindow* pWorkWindow = (WorkWindow* )pWindow;
312 :
313 : // dont save this special state!
314 131 : if (pWorkWindow->IsMinimized())
315 : return;
316 :
317 131 : ::rtl::OUString sOldWindowState = ::rtl::OStringToOUString( pSystemWindow->GetWindowState(), RTL_TEXTENCODING_ASCII_US );
318 131 : if ( sOldWindowState != sWindowState )
319 62 : pSystemWindow->SetWindowState(rtl::OUStringToOString(sWindowState,RTL_TEXTENCODING_UTF8));
320 : // <- SOLAR SAFE ------------------------
321 : }
322 :
323 : } // namespace framework
324 :
325 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|