LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/framework/source/helper - persistentwindowstate.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 108 123 87.8 %
Date: 2013-07-09 Functions: 16 18 88.9 %
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 <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/helper/vclunohelper.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       76039 : 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        1102 : PersistentWindowState::PersistentWindowState(const css::uno::Reference< css::uno::XComponentContext >& xContext)
      65        1102 :     : ThreadHelpBase          (&Application::GetSolarMutex())
      66             :     , m_xContext              (xContext                     )
      67        2204 :     , m_bWindowStateAlreadySet(sal_False                    )
      68             : {
      69        1102 : }
      70             : 
      71             : //*****************************************************************************************************************
      72        2190 : PersistentWindowState::~PersistentWindowState()
      73             : {
      74        2190 : }
      75             : 
      76             : //*****************************************************************************************************************
      77        1102 : 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        1102 :     css::uno::Reference< css::frame::XFrame > xFrame;
      83        1102 :     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        1102 :     lArguments[0] >>= xFrame;
      90        1102 :     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        2204 :     WriteGuard aWriteLock(m_aLock);
      98             :     // hold the frame as weak reference(!) so it can die everytimes :-)
      99        1102 :     m_xFrame = xFrame;
     100        1102 :     aWriteLock.unlock();
     101             :     // <- SAFE ----------------------------------
     102             : 
     103             :     // start listening
     104        2204 :     xFrame->addFrameActionListener(this);
     105        1102 : }
     106             : 
     107             : //*****************************************************************************************************************
     108        5879 : void SAL_CALL PersistentWindowState::frameAction(const css::frame::FrameActionEvent& aEvent)
     109             :     throw(css::uno::RuntimeException)
     110             : {
     111             :     // SAFE -> ----------------------------------
     112        5879 :     ReadGuard aReadLock(m_aLock);
     113        9359 :     css::uno::Reference< css::uno::XComponentContext >     xContext = m_xContext;
     114        9359 :     css::uno::Reference< css::frame::XFrame >              xFrame(m_xFrame.get(), css::uno::UNO_QUERY);
     115        5879 :     sal_Bool                                               bRestoreWindowState = !m_bWindowStateAlreadySet;
     116        5879 :     aReadLock.unlock();
     117             :     // <- SAFE ----------------------------------
     118             : 
     119             :     // frame already gone ? We hold it weak only ...
     120        5879 :     if (!xFrame.is())
     121           0 :         return;
     122             : 
     123             :     // no window -> no position and size available
     124        9359 :     css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
     125        5879 :     if (!xWindow.is())
     126           0 :         return;
     127             : 
     128             :     // unknown module -> no configuration available!
     129        9359 :     OUString sModuleName = PersistentWindowState::implst_identifyModule(xContext, xFrame);
     130        5879 :     if (sModuleName.isEmpty())
     131        2399 :         return;
     132             : 
     133        3480 :     switch(aEvent.Action)
     134             :     {
     135             :         case css::frame::FrameAction_COMPONENT_ATTACHED :
     136             :             {
     137        1098 :                 if (bRestoreWindowState)
     138             :                 {
     139        1098 :                     OUString sWindowState = PersistentWindowState::implst_getWindowStateFromConfig(xContext, sModuleName);
     140        1098 :                     PersistentWindowState::implst_setWindowStateOnWindow(xWindow,sWindowState);
     141             :                     // SAFE -> ----------------------------------
     142        2196 :                     WriteGuard aWriteLock(m_aLock);
     143        1098 :                     m_bWindowStateAlreadySet = sal_True;
     144        2196 :                     aWriteLock.unlock();
     145             :                     // <- SAFE ----------------------------------
     146             :                 }
     147             :             }
     148        1098 :             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           9 :             break;
     156             : 
     157             :         case css::frame::FrameAction_COMPONENT_DETACHING :
     158             :             {
     159        1084 :                 OUString sWindowState = PersistentWindowState::implst_getWindowStateFromWindow(xWindow);
     160        1084 :                 PersistentWindowState::implst_setWindowStateOnConfig(xContext, sModuleName, sWindowState);
     161             :             }
     162        1084 :             break;
     163             :         default:
     164        1289 :             break;
     165        3480 :     }
     166             : }
     167             : 
     168             : //*****************************************************************************************************************
     169        1095 : 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        1095 : }
     174             : 
     175             : //*****************************************************************************************************************
     176        5879 : OUString PersistentWindowState::implst_identifyModule(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
     177             :                                                              const css::uno::Reference< css::frame::XFrame >&              xFrame)
     178             : {
     179        5879 :     OUString sModuleName;
     180             : 
     181             :     css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
     182       11758 :         css::frame::ModuleManager::create( rxContext );
     183             : 
     184             :     try
     185             :     {
     186        5879 :         sModuleName = xModuleManager->identify(xFrame);
     187             :     }
     188           0 :     catch(const css::uno::RuntimeException&)
     189           0 :         { throw; }
     190        4798 :     catch(const css::uno::Exception&)
     191        2399 :         { sModuleName = OUString(); }
     192             : 
     193       11758 :     return sModuleName;
     194             : }
     195             : 
     196             : //*****************************************************************************************************************
     197        1098 : OUString PersistentWindowState::implst_getWindowStateFromConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
     198             :                                                                        const OUString&                                    sModuleName)
     199             : {
     200        1098 :     OUString sWindowState;
     201             : 
     202        2196 :     OUStringBuffer sRelPathBuf(256);
     203        1098 :     sRelPathBuf.appendAscii("Office/Factories/*[\"");
     204        1098 :     sRelPathBuf.append     (sModuleName            );
     205        1098 :     sRelPathBuf.appendAscii("\"]"                  );
     206             : 
     207        2196 :     OUString sPackage("org.openoffice.Setup/");
     208        2196 :     OUString sRelPath = sRelPathBuf.makeStringAndClear();
     209        2196 :     OUString sKey("ooSetupFactoryWindowAttributes");
     210             : 
     211             :     try
     212             :     {
     213             :         ::comphelper::ConfigurationHelper::readDirectKey(rxContext,
     214             :                                                                                       sPackage,
     215             :                                                                                       sRelPath,
     216             :                                                                                       sKey,
     217        1098 :                                                                                       ::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 = OUString(); }
     223             : 
     224        2196 :     return sWindowState;
     225             : }
     226             : 
     227             : //*****************************************************************************************************************
     228        1084 : void PersistentWindowState::implst_setWindowStateOnConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
     229             :                                                           const OUString&                                    sModuleName ,
     230             :                                                           const OUString&                                    sWindowState)
     231             : {
     232        1084 :     OUStringBuffer sRelPathBuf(256);
     233        1084 :     sRelPathBuf.appendAscii("Office/Factories/*[\"");
     234        1084 :     sRelPathBuf.append     (sModuleName            );
     235        1084 :     sRelPathBuf.appendAscii("\"]"                  );
     236             : 
     237        2168 :     OUString sPackage("org.openoffice.Setup/");
     238        2168 :     OUString sRelPath = sRelPathBuf.makeStringAndClear();
     239        2168 :     OUString sKey("ooSetupFactoryWindowAttributes");
     240             : 
     241             :     try
     242             :     {
     243             :         ::comphelper::ConfigurationHelper::writeDirectKey(rxContext,
     244             :                                                           sPackage,
     245             :                                                           sRelPath,
     246             :                                                           sKey,
     247             :                                                           css::uno::makeAny(sWindowState),
     248        1084 :                                                           ::comphelper::ConfigurationHelper::E_STANDARD);
     249             :     }
     250           0 :     catch(const css::uno::RuntimeException&)
     251           0 :         { throw; }
     252           0 :     catch(const css::uno::Exception&)
     253        1084 :         {}
     254        1084 : }
     255             : 
     256             : //*****************************************************************************************************************
     257        1084 : OUString PersistentWindowState::implst_getWindowStateFromWindow(const css::uno::Reference< css::awt::XWindow >& xWindow)
     258             : {
     259        1084 :     OUString sWindowState;
     260             : 
     261        1084 :     if (xWindow.is())
     262             :     {
     263             :         // SOLAR SAFE -> ------------------------
     264        1084 :         SolarMutexGuard aSolarGuard;
     265             : 
     266        1084 :         Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
     267             :         // check for system window is necessary to guarantee correct pointer cast!
     268        1084 :         if (
     269        2168 :             (pWindow                  ) &&
     270        1084 :             (pWindow->IsSystemWindow())
     271             :            )
     272             :         {
     273        1084 :             sal_uLong nMask  =   WINDOWSTATE_MASK_ALL;
     274        1084 :                   nMask &= ~(WINDOWSTATE_MASK_MINIMIZED);
     275        2168 :             sWindowState = OStringToOUString(
     276             :                             ((SystemWindow*)pWindow)->GetWindowState(nMask),
     277        1084 :                             RTL_TEXTENCODING_UTF8);
     278        1084 :         }
     279             :         // <- SOLAR SAFE ------------------------
     280             :     }
     281             : 
     282        1084 :     return sWindowState;
     283             : }
     284             : 
     285             : 
     286             : //*********************************************************************************************************
     287        1098 : void PersistentWindowState::implst_setWindowStateOnWindow(const css::uno::Reference< css::awt::XWindow >& xWindow     ,
     288             :                                                           const OUString&                          sWindowState)
     289             : {
     290        1098 :     if (
     291        2196 :         (!xWindow.is()                ) ||
     292        1098 :         ( sWindowState.isEmpty() )
     293             :        )
     294          90 :         return;
     295             : 
     296             :     // SOLAR SAFE -> ------------------------
     297        1053 :     SolarMutexGuard aSolarGuard;
     298             : 
     299        1053 :     Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
     300        1053 :     if (!pWindow)
     301           0 :         return;
     302             : 
     303             :     // check for system and work window - its necessary to guarantee correct pointer cast!
     304        1053 :     sal_Bool bSystemWindow = pWindow->IsSystemWindow();
     305        1053 :     sal_Bool bWorkWindow   = (pWindow->GetType() == WINDOW_WORKWINDOW);
     306             : 
     307        1053 :     if (!bSystemWindow && !bWorkWindow)
     308           0 :         return;
     309             : 
     310        1053 :     SystemWindow* pSystemWindow = (SystemWindow*)pWindow;
     311        1053 :     WorkWindow*   pWorkWindow   = (WorkWindow*  )pWindow;
     312             : 
     313             :     // dont save this special state!
     314        1053 :     if (pWorkWindow->IsMinimized())
     315           0 :         return;
     316             : 
     317        2106 :     OUString sOldWindowState = OStringToOUString( pSystemWindow->GetWindowState(), RTL_TEXTENCODING_ASCII_US );
     318        1053 :     if ( sOldWindowState != sWindowState )
     319        1114 :         pSystemWindow->SetWindowState(OUStringToOString(sWindowState,RTL_TEXTENCODING_UTF8));
     320             :     // <- SOLAR SAFE ------------------------
     321             : }
     322             : 
     323         402 : } // namespace framework
     324             : 
     325             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10