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