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

Generated by: LCOV version 1.10