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 <macros/generic.hxx>
23 : #include <services.h>
24 :
25 : #include <com/sun/star/awt/XWindow.hpp>
26 :
27 : #include <com/sun/star/lang/XServiceInfo.hpp>
28 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 : #include <com/sun/star/frame/ModuleManager.hpp>
30 :
31 : #include <comphelper/processfactory.hxx>
32 : #include <comphelper/configurationhelper.hxx>
33 : #include <vcl/window.hxx>
34 : #include <vcl/syswin.hxx>
35 :
36 : #include <toolkit/helper/vclunohelper.hxx>
37 : #include <vcl/svapp.hxx>
38 : #include <vcl/wrkwin.hxx>
39 : #include <rtl/string.hxx>
40 :
41 : namespace framework{
42 :
43 0 : PersistentWindowState::PersistentWindowState(const css::uno::Reference< css::uno::XComponentContext >& xContext)
44 : : m_xContext (xContext )
45 0 : , m_bWindowStateAlreadySet(false )
46 : {
47 0 : }
48 :
49 0 : PersistentWindowState::~PersistentWindowState()
50 : {
51 0 : }
52 :
53 0 : void SAL_CALL PersistentWindowState::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
54 : throw(css::uno::Exception ,
55 : css::uno::RuntimeException, std::exception)
56 : {
57 : // check arguments
58 0 : css::uno::Reference< css::frame::XFrame > xFrame;
59 0 : if (lArguments.getLength() < 1)
60 : throw css::lang::IllegalArgumentException(
61 : "Empty argument list!",
62 : static_cast< ::cppu::OWeakObject* >(this),
63 0 : 1);
64 :
65 0 : lArguments[0] >>= xFrame;
66 0 : if (!xFrame.is())
67 : throw css::lang::IllegalArgumentException(
68 : "No valid frame specified!",
69 : static_cast< ::cppu::OWeakObject* >(this),
70 0 : 1);
71 :
72 : {
73 0 : SolarMutexGuard g;
74 0 : m_xFrame = xFrame;
75 : }
76 :
77 : // start listening
78 0 : xFrame->addFrameActionListener(this);
79 0 : }
80 :
81 0 : void SAL_CALL PersistentWindowState::frameAction(const css::frame::FrameActionEvent& aEvent)
82 : throw(css::uno::RuntimeException, std::exception)
83 : {
84 0 : css::uno::Reference< css::uno::XComponentContext > xContext;
85 0 : css::uno::Reference< css::frame::XFrame > xFrame;
86 : bool bRestoreWindowState;
87 : {
88 0 : SolarMutexGuard g;
89 0 : xContext = m_xContext;
90 0 : xFrame.set(m_xFrame.get(), css::uno::UNO_QUERY);
91 0 : bRestoreWindowState = !m_bWindowStateAlreadySet;
92 : }
93 :
94 : // frame already gone ? We hold it weak only ...
95 0 : if (!xFrame.is())
96 0 : return;
97 :
98 : // no window -> no position and size available
99 0 : css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
100 0 : if (!xWindow.is())
101 0 : return;
102 :
103 : // unknown module -> no configuration available!
104 0 : OUString sModuleName = PersistentWindowState::implst_identifyModule(xContext, xFrame);
105 0 : if (sModuleName.isEmpty())
106 0 : return;
107 :
108 0 : switch(aEvent.Action)
109 : {
110 : case css::frame::FrameAction_COMPONENT_ATTACHED :
111 : {
112 0 : if (bRestoreWindowState)
113 : {
114 0 : OUString sWindowState = PersistentWindowState::implst_getWindowStateFromConfig(xContext, sModuleName);
115 0 : PersistentWindowState::implst_setWindowStateOnWindow(xWindow,sWindowState);
116 0 : SolarMutexGuard g;
117 0 : m_bWindowStateAlreadySet = true;
118 : }
119 : }
120 0 : break;
121 :
122 : case css::frame::FrameAction_COMPONENT_REATTACHED :
123 : {
124 : // nothing todo here, because its not allowed to change position and size
125 : // of an alredy existing frame!
126 : }
127 0 : break;
128 :
129 : case css::frame::FrameAction_COMPONENT_DETACHING :
130 : {
131 0 : OUString sWindowState = PersistentWindowState::implst_getWindowStateFromWindow(xWindow);
132 0 : PersistentWindowState::implst_setWindowStateOnConfig(xContext, sModuleName, sWindowState);
133 : }
134 0 : break;
135 : default:
136 0 : break;
137 0 : }
138 : }
139 :
140 0 : void SAL_CALL PersistentWindowState::disposing(const css::lang::EventObject&)
141 : throw(css::uno::RuntimeException, std::exception)
142 : {
143 : // nothing todo here - because we hold the frame as weak reference only
144 0 : }
145 :
146 0 : OUString PersistentWindowState::implst_identifyModule(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
147 : const css::uno::Reference< css::frame::XFrame >& xFrame)
148 : {
149 0 : OUString sModuleName;
150 :
151 : css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
152 0 : css::frame::ModuleManager::create( rxContext );
153 :
154 : try
155 : {
156 0 : sModuleName = xModuleManager->identify(xFrame);
157 : }
158 0 : catch(const css::uno::RuntimeException&)
159 0 : { throw; }
160 0 : catch(const css::uno::Exception&)
161 0 : { sModuleName = OUString(); }
162 :
163 0 : return sModuleName;
164 : }
165 :
166 0 : OUString PersistentWindowState::implst_getWindowStateFromConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
167 : const OUString& sModuleName)
168 : {
169 0 : OUString sWindowState;
170 :
171 0 : OUStringBuffer sRelPathBuf(256);
172 0 : sRelPathBuf.appendAscii("Office/Factories/*[\"");
173 0 : sRelPathBuf.append (sModuleName );
174 0 : sRelPathBuf.appendAscii("\"]" );
175 :
176 0 : OUString sPackage("org.openoffice.Setup/");
177 0 : OUString sRelPath = sRelPathBuf.makeStringAndClear();
178 0 : OUString sKey("ooSetupFactoryWindowAttributes");
179 :
180 : try
181 : {
182 : ::comphelper::ConfigurationHelper::readDirectKey(rxContext,
183 : sPackage,
184 : sRelPath,
185 : sKey,
186 0 : ::comphelper::ConfigurationHelper::E_READONLY) >>= sWindowState;
187 : }
188 0 : catch(const css::uno::RuntimeException&)
189 0 : { throw; }
190 0 : catch(const css::uno::Exception&)
191 0 : { sWindowState = OUString(); }
192 :
193 0 : return sWindowState;
194 : }
195 :
196 0 : void PersistentWindowState::implst_setWindowStateOnConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
197 : const OUString& sModuleName ,
198 : const OUString& sWindowState)
199 : {
200 0 : OUStringBuffer sRelPathBuf(256);
201 0 : sRelPathBuf.appendAscii("Office/Factories/*[\"");
202 0 : sRelPathBuf.append (sModuleName );
203 0 : sRelPathBuf.appendAscii("\"]" );
204 :
205 0 : OUString sPackage("org.openoffice.Setup/");
206 0 : OUString sRelPath = sRelPathBuf.makeStringAndClear();
207 0 : OUString sKey("ooSetupFactoryWindowAttributes");
208 :
209 : try
210 : {
211 : ::comphelper::ConfigurationHelper::writeDirectKey(rxContext,
212 : sPackage,
213 : sRelPath,
214 : sKey,
215 : css::uno::makeAny(sWindowState),
216 0 : ::comphelper::ConfigurationHelper::E_STANDARD);
217 : }
218 0 : catch(const css::uno::RuntimeException&)
219 0 : { throw; }
220 0 : catch(const css::uno::Exception&)
221 0 : {}
222 0 : }
223 :
224 0 : OUString PersistentWindowState::implst_getWindowStateFromWindow(const css::uno::Reference< css::awt::XWindow >& xWindow)
225 : {
226 0 : OUString sWindowState;
227 :
228 0 : if (xWindow.is())
229 : {
230 : // SOLAR SAFE -> ------------------------
231 0 : SolarMutexGuard aSolarGuard;
232 :
233 0 : Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
234 : // check for system window is necessary to guarantee correct pointer cast!
235 0 : if (
236 0 : (pWindow ) &&
237 0 : (pWindow->IsSystemWindow())
238 : )
239 : {
240 0 : sal_uLong nMask = WINDOWSTATE_MASK_ALL;
241 0 : nMask &= ~(WINDOWSTATE_MASK_MINIMIZED);
242 0 : sWindowState = OStringToOUString(
243 : ((SystemWindow*)pWindow)->GetWindowState(nMask),
244 0 : RTL_TEXTENCODING_UTF8);
245 0 : }
246 : // <- SOLAR SAFE ------------------------
247 : }
248 :
249 0 : return sWindowState;
250 : }
251 :
252 0 : void PersistentWindowState::implst_setWindowStateOnWindow(const css::uno::Reference< css::awt::XWindow >& xWindow ,
253 : const OUString& sWindowState)
254 : {
255 0 : if (
256 0 : (!xWindow.is() ) ||
257 0 : ( sWindowState.isEmpty() )
258 : )
259 0 : return;
260 :
261 : // SOLAR SAFE -> ------------------------
262 0 : SolarMutexGuard aSolarGuard;
263 :
264 0 : Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
265 0 : if (!pWindow)
266 0 : return;
267 :
268 : // check for system and work window - its necessary to guarantee correct pointer cast!
269 0 : bool bSystemWindow = pWindow->IsSystemWindow();
270 0 : bool bWorkWindow = (pWindow->GetType() == WINDOW_WORKWINDOW);
271 :
272 0 : if (!bSystemWindow && !bWorkWindow)
273 0 : return;
274 :
275 0 : SystemWindow* pSystemWindow = (SystemWindow*)pWindow;
276 0 : WorkWindow* pWorkWindow = (WorkWindow* )pWindow;
277 :
278 : // dont save this special state!
279 0 : if (pWorkWindow->IsMinimized())
280 0 : return;
281 :
282 0 : OUString sOldWindowState = OStringToOUString( pSystemWindow->GetWindowState(), RTL_TEXTENCODING_ASCII_US );
283 0 : if ( sOldWindowState != sWindowState )
284 0 : pSystemWindow->SetWindowState(OUStringToOString(sWindowState,RTL_TEXTENCODING_UTF8));
285 : // <- SOLAR SAFE ------------------------
286 : }
287 :
288 : } // namespace framework
289 :
290 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|