LCOV - code coverage report
Current view: top level - framework/source/helper - persistentwindowstate.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 102 118 86.4 %
Date: 2014-04-11 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          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        2073 : PersistentWindowState::PersistentWindowState(const css::uno::Reference< css::uno::XComponentContext >& xContext)
      44             :     : m_xContext              (xContext                     )
      45        2073 :     , m_bWindowStateAlreadySet(false                    )
      46             : {
      47        2073 : }
      48             : 
      49        4120 : PersistentWindowState::~PersistentWindowState()
      50             : {
      51        4120 : }
      52             : 
      53        2073 : 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        2073 :     css::uno::Reference< css::frame::XFrame > xFrame;
      59        2073 :     if (lArguments.getLength() < 1)
      60             :         throw css::lang::IllegalArgumentException(
      61             :                 "Empty argument list!",
      62             :                 static_cast< ::cppu::OWeakObject* >(this),
      63           0 :                 1);
      64             : 
      65        2073 :     lArguments[0] >>= xFrame;
      66        2073 :     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        2073 :         SolarMutexGuard g;
      74        2073 :         m_xFrame = xFrame;
      75             :     }
      76             : 
      77             :     // start listening
      78        2073 :     xFrame->addFrameActionListener(this);
      79        2073 : }
      80             : 
      81       11608 : void SAL_CALL PersistentWindowState::frameAction(const css::frame::FrameActionEvent& aEvent)
      82             :     throw(css::uno::RuntimeException, std::exception)
      83             : {
      84       11608 :     css::uno::Reference< css::uno::XComponentContext >     xContext;
      85       17014 :     css::uno::Reference< css::frame::XFrame >              xFrame;
      86             :     bool                                               bRestoreWindowState;
      87             :     {
      88       11608 :         SolarMutexGuard g;
      89       11608 :         xContext = m_xContext;
      90       11608 :         xFrame.set(m_xFrame.get(), css::uno::UNO_QUERY);
      91       11608 :         bRestoreWindowState = !m_bWindowStateAlreadySet;
      92             :     }
      93             : 
      94             :     // frame already gone ? We hold it weak only ...
      95       11608 :     if (!xFrame.is())
      96           0 :         return;
      97             : 
      98             :     // no window -> no position and size available
      99       17014 :     css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
     100       11608 :     if (!xWindow.is())
     101           0 :         return;
     102             : 
     103             :     // unknown module -> no configuration available!
     104       17014 :     OUString sModuleName = PersistentWindowState::implst_identifyModule(xContext, xFrame);
     105       11608 :     if (sModuleName.isEmpty())
     106        6202 :         return;
     107             : 
     108        5406 :     switch(aEvent.Action)
     109             :     {
     110             :         case css::frame::FrameAction_COMPONENT_ATTACHED :
     111             :             {
     112        2069 :                 if (bRestoreWindowState)
     113             :                 {
     114        2069 :                     OUString sWindowState = PersistentWindowState::implst_getWindowStateFromConfig(xContext, sModuleName);
     115        2069 :                     PersistentWindowState::implst_setWindowStateOnWindow(xWindow,sWindowState);
     116        4138 :                     SolarMutexGuard g;
     117        4138 :                     m_bWindowStateAlreadySet = true;
     118             :                 }
     119             :             }
     120        2069 :             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           9 :             break;
     128             : 
     129             :         case css::frame::FrameAction_COMPONENT_DETACHING :
     130             :             {
     131        2049 :                 OUString sWindowState = PersistentWindowState::implst_getWindowStateFromWindow(xWindow);
     132        2049 :                 PersistentWindowState::implst_setWindowStateOnConfig(xContext, sModuleName, sWindowState);
     133             :             }
     134        2049 :             break;
     135             :         default:
     136        1279 :             break;
     137        5406 :     }
     138             : }
     139             : 
     140        2060 : 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        2060 : }
     145             : 
     146       11608 : OUString PersistentWindowState::implst_identifyModule(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
     147             :                                                              const css::uno::Reference< css::frame::XFrame >&              xFrame)
     148             : {
     149       11608 :     OUString sModuleName;
     150             : 
     151             :     css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
     152       23216 :         css::frame::ModuleManager::create( rxContext );
     153             : 
     154             :     try
     155             :     {
     156       11608 :         sModuleName = xModuleManager->identify(xFrame);
     157             :     }
     158           0 :     catch(const css::uno::RuntimeException&)
     159           0 :         { throw; }
     160       12404 :     catch(const css::uno::Exception&)
     161        6202 :         { sModuleName = OUString(); }
     162             : 
     163       23216 :     return sModuleName;
     164             : }
     165             : 
     166        2069 : OUString PersistentWindowState::implst_getWindowStateFromConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
     167             :                                                                        const OUString&                                    sModuleName)
     168             : {
     169        2069 :     OUString sWindowState;
     170             : 
     171        4138 :     OUStringBuffer sRelPathBuf(256);
     172        2069 :     sRelPathBuf.appendAscii("Office/Factories/*[\"");
     173        2069 :     sRelPathBuf.append     (sModuleName            );
     174        2069 :     sRelPathBuf.appendAscii("\"]"                  );
     175             : 
     176        4138 :     OUString sPackage("org.openoffice.Setup/");
     177        4138 :     OUString sRelPath = sRelPathBuf.makeStringAndClear();
     178        4138 :     OUString sKey("ooSetupFactoryWindowAttributes");
     179             : 
     180             :     try
     181             :     {
     182             :         ::comphelper::ConfigurationHelper::readDirectKey(rxContext,
     183             :                                                                                       sPackage,
     184             :                                                                                       sRelPath,
     185             :                                                                                       sKey,
     186        2069 :                                                                                       ::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        4138 :     return sWindowState;
     194             : }
     195             : 
     196        2049 : void PersistentWindowState::implst_setWindowStateOnConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
     197             :                                                           const OUString&                                    sModuleName ,
     198             :                                                           const OUString&                                    sWindowState)
     199             : {
     200        2049 :     OUStringBuffer sRelPathBuf(256);
     201        2049 :     sRelPathBuf.appendAscii("Office/Factories/*[\"");
     202        2049 :     sRelPathBuf.append     (sModuleName            );
     203        2049 :     sRelPathBuf.appendAscii("\"]"                  );
     204             : 
     205        4098 :     OUString sPackage("org.openoffice.Setup/");
     206        4098 :     OUString sRelPath = sRelPathBuf.makeStringAndClear();
     207        4098 :     OUString sKey("ooSetupFactoryWindowAttributes");
     208             : 
     209             :     try
     210             :     {
     211             :         ::comphelper::ConfigurationHelper::writeDirectKey(rxContext,
     212             :                                                           sPackage,
     213             :                                                           sRelPath,
     214             :                                                           sKey,
     215             :                                                           css::uno::makeAny(sWindowState),
     216        2049 :                                                           ::comphelper::ConfigurationHelper::E_STANDARD);
     217             :     }
     218           0 :     catch(const css::uno::RuntimeException&)
     219           0 :         { throw; }
     220           0 :     catch(const css::uno::Exception&)
     221        2049 :         {}
     222        2049 : }
     223             : 
     224        2049 : OUString PersistentWindowState::implst_getWindowStateFromWindow(const css::uno::Reference< css::awt::XWindow >& xWindow)
     225             : {
     226        2049 :     OUString sWindowState;
     227             : 
     228        2049 :     if (xWindow.is())
     229             :     {
     230             :         // SOLAR SAFE -> ------------------------
     231        2049 :         SolarMutexGuard aSolarGuard;
     232             : 
     233        2049 :         Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
     234             :         // check for system window is necessary to guarantee correct pointer cast!
     235        2049 :         if (
     236        4098 :             (pWindow                  ) &&
     237        2049 :             (pWindow->IsSystemWindow())
     238             :            )
     239             :         {
     240        2049 :             sal_uLong nMask  =   WINDOWSTATE_MASK_ALL;
     241        2049 :                   nMask &= ~(WINDOWSTATE_MASK_MINIMIZED);
     242        4098 :             sWindowState = OStringToOUString(
     243             :                             ((SystemWindow*)pWindow)->GetWindowState(nMask),
     244        2049 :                             RTL_TEXTENCODING_UTF8);
     245        2049 :         }
     246             :         // <- SOLAR SAFE ------------------------
     247             :     }
     248             : 
     249        2049 :     return sWindowState;
     250             : }
     251             : 
     252        2069 : void PersistentWindowState::implst_setWindowStateOnWindow(const css::uno::Reference< css::awt::XWindow >& xWindow     ,
     253             :                                                           const OUString&                          sWindowState)
     254             : {
     255        2069 :     if (
     256        4138 :         (!xWindow.is()                ) ||
     257        2069 :         ( sWindowState.isEmpty() )
     258             :        )
     259         114 :         return;
     260             : 
     261             :     // SOLAR SAFE -> ------------------------
     262        2012 :     SolarMutexGuard aSolarGuard;
     263             : 
     264        2012 :     Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
     265        2012 :     if (!pWindow)
     266           0 :         return;
     267             : 
     268             :     // check for system and work window - its necessary to guarantee correct pointer cast!
     269        2012 :     bool bSystemWindow = pWindow->IsSystemWindow();
     270        2012 :     bool bWorkWindow   = (pWindow->GetType() == WINDOW_WORKWINDOW);
     271             : 
     272        2012 :     if (!bSystemWindow && !bWorkWindow)
     273           0 :         return;
     274             : 
     275        2012 :     SystemWindow* pSystemWindow = (SystemWindow*)pWindow;
     276        2012 :     WorkWindow*   pWorkWindow   = (WorkWindow*  )pWindow;
     277             : 
     278             :     // dont save this special state!
     279        2012 :     if (pWorkWindow->IsMinimized())
     280           0 :         return;
     281             : 
     282        4024 :     OUString sOldWindowState = OStringToOUString( pSystemWindow->GetWindowState(), RTL_TEXTENCODING_ASCII_US );
     283        2012 :     if ( sOldWindowState != sWindowState )
     284        2079 :         pSystemWindow->SetWindowState(OUStringToOString(sWindowState,RTL_TEXTENCODING_UTF8));
     285             :     // <- SOLAR SAFE ------------------------
     286             : }
     287             : 
     288             : } // namespace framework
     289             : 
     290             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10