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 3172 : 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 240 : PersistentWindowState::PersistentWindowState(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
65 240 : : ThreadHelpBase (&Application::GetSolarMutex())
66 : , m_xSMGR (xSMGR )
67 480 : , m_bWindowStateAlreadySet(sal_False )
68 : {
69 240 : }
70 :
71 : //*****************************************************************************************************************
72 126 : PersistentWindowState::~PersistentWindowState()
73 : {
74 126 : }
75 :
76 : //*****************************************************************************************************************
77 240 : 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 240 : css::uno::Reference< css::frame::XFrame > xFrame;
83 240 : 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 240 : lArguments[0] >>= xFrame;
90 240 : 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 240 : WriteGuard aWriteLock(m_aLock);
98 : // hold the frame as weak reference(!) so it can die everytimes :-)
99 240 : m_xFrame = xFrame;
100 240 : aWriteLock.unlock();
101 : // <- SAFE ----------------------------------
102 :
103 : // start listening
104 240 : xFrame->addFrameActionListener(this);
105 240 : }
106 :
107 : //*****************************************************************************************************************
108 1090 : void SAL_CALL PersistentWindowState::frameAction(const css::frame::FrameActionEvent& aEvent)
109 : throw(css::uno::RuntimeException)
110 : {
111 : // SAFE -> ----------------------------------
112 1090 : ReadGuard aReadLock(m_aLock);
113 1090 : css::uno::Reference< css::uno::XComponentContext > xContext = comphelper::getComponentContext(m_xSMGR);
114 1090 : css::uno::Reference< css::frame::XFrame > xFrame(m_xFrame.get(), css::uno::UNO_QUERY);
115 1090 : sal_Bool bRestoreWindowState = !m_bWindowStateAlreadySet;
116 1090 : aReadLock.unlock();
117 : // <- SAFE ----------------------------------
118 :
119 : // frame already gone ? We hold it weak only ...
120 1090 : if (!xFrame.is())
121 : return;
122 :
123 : // no window -> no position and size available
124 1090 : css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
125 1090 : if (!xWindow.is())
126 : return;
127 :
128 : // unknown module -> no configuration available!
129 1090 : ::rtl::OUString sModuleName = PersistentWindowState::implst_identifyModule(xContext, xFrame);
130 1090 : if (sModuleName.isEmpty())
131 : return;
132 :
133 507 : switch(aEvent.Action)
134 : {
135 : case css::frame::FrameAction_COMPONENT_ATTACHED :
136 : {
137 240 : if (bRestoreWindowState)
138 : {
139 240 : ::rtl::OUString sWindowState = PersistentWindowState::implst_getWindowStateFromConfig(xContext, sModuleName);
140 240 : PersistentWindowState::implst_setWindowStateOnWindow(xWindow,sWindowState);
141 : // SAFE -> ----------------------------------
142 240 : WriteGuard aWriteLock(m_aLock);
143 240 : m_bWindowStateAlreadySet = sal_True;
144 240 : aWriteLock.unlock();
145 : // <- SAFE ----------------------------------
146 : }
147 : }
148 240 : 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 63 : ::rtl::OUString sWindowState = PersistentWindowState::implst_getWindowStateFromWindow(xWindow);
160 63 : PersistentWindowState::implst_setWindowStateOnConfig(xContext, sModuleName, sWindowState);
161 : }
162 63 : break;
163 : default:
164 204 : break;
165 1090 : }
166 : }
167 :
168 : //*****************************************************************************************************************
169 63 : 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 63 : }
174 :
175 : //*****************************************************************************************************************
176 1090 : ::rtl::OUString PersistentWindowState::implst_identifyModule(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
177 : const css::uno::Reference< css::frame::XFrame >& xFrame)
178 : {
179 1090 : ::rtl::OUString sModuleName;
180 :
181 : css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
182 1090 : css::frame::ModuleManager::create( rxContext );
183 :
184 : try
185 : {
186 1090 : sModuleName = xModuleManager->identify(xFrame);
187 : }
188 0 : catch(const css::uno::RuntimeException&)
189 0 : { throw; }
190 1166 : catch(const css::uno::Exception&)
191 583 : { sModuleName = ::rtl::OUString(); }
192 :
193 1090 : return sModuleName;
194 : }
195 :
196 : //*****************************************************************************************************************
197 240 : ::rtl::OUString PersistentWindowState::implst_getWindowStateFromConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
198 : const ::rtl::OUString& sModuleName)
199 : {
200 240 : ::rtl::OUString sWindowState;
201 :
202 240 : ::rtl::OUStringBuffer sRelPathBuf(256);
203 240 : sRelPathBuf.appendAscii("Office/Factories/*[\"");
204 240 : sRelPathBuf.append (sModuleName );
205 240 : sRelPathBuf.appendAscii("\"]" );
206 :
207 240 : ::rtl::OUString sPackage("org.openoffice.Setup/");
208 240 : ::rtl::OUString sRelPath = sRelPathBuf.makeStringAndClear();
209 240 : ::rtl::OUString sKey("ooSetupFactoryWindowAttributes");
210 :
211 : try
212 : {
213 : ::comphelper::ConfigurationHelper::readDirectKey(rxContext,
214 : sPackage,
215 : sRelPath,
216 : sKey,
217 240 : ::comphelper::ConfigurationHelper::E_READONLY) >>= sWindowState;
218 : }
219 0 : catch(const css::uno::RuntimeException&)
220 0 : { throw; }
221 8 : catch(const css::uno::Exception&)
222 4 : { sWindowState = ::rtl::OUString(); }
223 :
224 240 : return sWindowState;
225 : }
226 :
227 : //*****************************************************************************************************************
228 63 : void PersistentWindowState::implst_setWindowStateOnConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
229 : const ::rtl::OUString& sModuleName ,
230 : const ::rtl::OUString& sWindowState)
231 : {
232 63 : ::rtl::OUStringBuffer sRelPathBuf(256);
233 63 : sRelPathBuf.appendAscii("Office/Factories/*[\"");
234 63 : sRelPathBuf.append (sModuleName );
235 63 : sRelPathBuf.appendAscii("\"]" );
236 :
237 63 : ::rtl::OUString sPackage("org.openoffice.Setup/");
238 63 : ::rtl::OUString sRelPath = sRelPathBuf.makeStringAndClear();
239 63 : ::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 63 : ::comphelper::ConfigurationHelper::E_STANDARD);
249 : }
250 0 : catch(const css::uno::RuntimeException&)
251 0 : { throw; }
252 0 : catch(const css::uno::Exception&)
253 63 : {}
254 63 : }
255 :
256 : //*****************************************************************************************************************
257 63 : ::rtl::OUString PersistentWindowState::implst_getWindowStateFromWindow(const css::uno::Reference< css::awt::XWindow >& xWindow)
258 : {
259 63 : ::rtl::OUString sWindowState;
260 :
261 63 : if (xWindow.is())
262 : {
263 : // SOLAR SAFE -> ------------------------
264 63 : SolarMutexGuard aSolarGuard;
265 :
266 63 : Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
267 : // check for system window is neccessary to guarantee correct pointer cast!
268 126 : if (
269 : (pWindow ) &&
270 63 : (pWindow->IsSystemWindow())
271 : )
272 : {
273 63 : sal_uLong nMask = WINDOWSTATE_MASK_ALL;
274 63 : nMask &= ~(WINDOWSTATE_MASK_MINIMIZED);
275 : sWindowState = rtl::OStringToOUString(
276 : ((SystemWindow*)pWindow)->GetWindowState(nMask),
277 63 : RTL_TEXTENCODING_UTF8);
278 63 : }
279 : // <- SOLAR SAFE ------------------------
280 : }
281 :
282 63 : return sWindowState;
283 : }
284 :
285 :
286 : //*********************************************************************************************************
287 240 : void PersistentWindowState::implst_setWindowStateOnWindow(const css::uno::Reference< css::awt::XWindow >& xWindow ,
288 : const ::rtl::OUString& sWindowState)
289 : {
290 480 : if (
291 240 : (!xWindow.is() ) ||
292 240 : ( sWindowState.isEmpty() )
293 : )
294 : return;
295 :
296 : // SOLAR SAFE -> ------------------------
297 107 : SolarMutexGuard aSolarGuard;
298 :
299 107 : Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
300 107 : if (!pWindow)
301 : return;
302 :
303 : // check for system and work window - its neccessary to guarantee correct pointer cast!
304 107 : sal_Bool bSystemWindow = pWindow->IsSystemWindow();
305 107 : sal_Bool bWorkWindow = (pWindow->GetType() == WINDOW_WORKWINDOW);
306 :
307 107 : if (!bSystemWindow && !bWorkWindow)
308 : return;
309 :
310 107 : SystemWindow* pSystemWindow = (SystemWindow*)pWindow;
311 107 : WorkWindow* pWorkWindow = (WorkWindow* )pWindow;
312 :
313 : // dont save this special state!
314 107 : if (pWorkWindow->IsMinimized())
315 : return;
316 :
317 107 : ::rtl::OUString sOldWindowState = ::rtl::OStringToOUString( pSystemWindow->GetWindowState(), RTL_TEXTENCODING_ASCII_US );
318 107 : if ( sOldWindowState != sWindowState )
319 35 : 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: */
|