LCOV - code coverage report
Current view: top level - framework/source/fwe/classes - framelistanalyzer.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 85 96 88.5 %
Date: 2014-11-03 Functions: 5 6 83.3 %
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 <framework/framelistanalyzer.hxx>
      21             : 
      22             : #include <targets.h>
      23             : #include <properties.h>
      24             : #include <services.h>
      25             : 
      26             : #include <com/sun/star/beans/XPropertySet.hpp>
      27             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      28             : #include <com/sun/star/frame/ModuleManager.hpp>
      29             : 
      30             : #include <comphelper/processfactory.hxx>
      31             : #include <vcl/svapp.hxx>
      32             : #include <tools/diagnose_ex.h>
      33             : 
      34             : namespace framework{
      35             : 
      36             : /**
      37             :  */
      38             : 
      39       31708 : FrameListAnalyzer::FrameListAnalyzer( const css::uno::Reference< css::frame::XFramesSupplier >& xSupplier       ,
      40             :                                       const css::uno::Reference< css::frame::XFrame >&          xReferenceFrame ,
      41             :                                             sal_uInt32                                          eDetectMode     )
      42             :     : m_xSupplier      (xSupplier      )
      43             :     , m_xReferenceFrame(xReferenceFrame)
      44       31708 :     , m_eDetectMode    (eDetectMode    )
      45             : {
      46       31708 :     impl_analyze();
      47       31708 : }
      48             : 
      49             : /**
      50             :  */
      51             : 
      52       31708 : FrameListAnalyzer::~FrameListAnalyzer()
      53             : {
      54       31708 : }
      55             : 
      56             : /** returns an analyzed list of all currently opened (top!) frames inside the desktop tree.
      57             : 
      58             :     We try to get a snapshot of all opened frames, which are part of the desktop frame container.
      59             :     Of course we can't access frames, which stands outside of this tree.
      60             :     But it's necessary to collect top frames here only. Otherwise we interpret closing of last
      61             :     frame wrong. Further we analyze this list and split into different parts.
      62             :     E.g. for "CloseDoc" we must know, which frames of the given list referr to the same model.
      63             :     These frames must be closed then. But all other frames must be untouched.
      64             :     In case the request was "CloseWin" these splitted lists can be used too, to decide if the last window
      65             :     or document was closed. Then we have to initialize the backing window ...
      66             :     Last but not least we must know something about our special help frame. It must be handled
      67             :     separately. And last but not least - the backing component frame must be detected too.
      68             : */
      69             : 
      70       31708 : void FrameListAnalyzer::impl_analyze()
      71             : {
      72             :     // reset all members to get a consistent state
      73       31708 :     m_bReferenceIsHidden  = false;
      74       31708 :     m_bReferenceIsHelp    = false;
      75       31708 :     m_bReferenceIsBacking = false;
      76       31708 :     m_xHelp               = css::uno::Reference< css::frame::XFrame >();
      77       31708 :     m_xBackingComponent   = css::uno::Reference< css::frame::XFrame >();
      78             : 
      79             :     // try to get the task container by using the given supplier
      80       31708 :     css::uno::Reference< css::container::XIndexAccess > xFrameContainer(m_xSupplier->getFrames(), css::uno::UNO_QUERY);
      81             : 
      82             :     // All return list get an initial size to include all possible frames.
      83             :     // They will be packed at the end of this method ... using the actual step positions then.
      84       31708 :     sal_Int32 nVisibleStep = 0;
      85       31708 :     sal_Int32 nHiddenStep  = 0;
      86       31708 :     sal_Int32 nModelStep   = 0;
      87       31708 :     sal_Int32 nCount       = xFrameContainer->getCount();
      88             : 
      89       31708 :     m_lOtherVisibleFrames.realloc(nCount);
      90       31708 :     m_lOtherHiddenFrames.realloc(nCount);
      91       31708 :     m_lModelFrames.realloc(nCount);
      92             : 
      93             :     // ask for the model of the given reference frame.
      94             :     // It must be compared with the model of every frame of the container
      95             :     // to sort it into the list of frames with the same model.
      96             :     // Suppress this step, if right detect mode isn't set.
      97       63416 :     css::uno::Reference< css::frame::XModel > xReferenceModel;
      98       31708 :     if ((m_eDetectMode & E_MODEL) == E_MODEL )
      99             :     {
     100           6 :         css::uno::Reference< css::frame::XController > xReferenceController;
     101           6 :         if (m_xReferenceFrame.is())
     102           6 :             xReferenceController = m_xReferenceFrame->getController();
     103           6 :         if (xReferenceController.is())
     104           6 :             xReferenceModel = xReferenceController->getModel();
     105             :     }
     106             : 
     107             :     // check, if the reference frame is in hidden mode.
     108             :     // But look, if this analyze step is really needed.
     109       63416 :     css::uno::Reference< css::beans::XPropertySet > xSet(m_xReferenceFrame, css::uno::UNO_QUERY);
     110       31708 :     if (
     111       59400 :         ((m_eDetectMode & E_HIDDEN) == E_HIDDEN) &&
     112       27692 :         (xSet.is()                             )
     113             :        )
     114             :     {
     115       27692 :         xSet->getPropertyValue(FRAME_PROPNAME_ISHIDDEN) >>= m_bReferenceIsHidden;
     116             :     }
     117             : 
     118             :     // check, if the reference frame includes the backing component.
     119             :     // But look, if this analyze step is really needed.
     120       31708 :     if (((m_eDetectMode & E_BACKINGCOMPONENT) == E_BACKINGCOMPONENT) && m_xReferenceFrame.is() )
     121             :     {
     122             :         try
     123             :         {
     124       27696 :             css::uno::Reference< css::uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
     125       55392 :             css::uno::Reference< css::frame::XModuleManager2 > xModuleMgr = css::frame::ModuleManager::create(xContext);
     126       36668 :             OUString sModule = xModuleMgr->identify(m_xReferenceFrame);
     127       36668 :             m_bReferenceIsBacking = sModule.equals("com.sun.star.frame.StartModule");
     128             :         }
     129       18724 :         catch(const css::frame::UnknownModuleException&)
     130             :         {
     131             :         }
     132           0 :         catch(const css::uno::Exception&)
     133             :         {
     134             :             DBG_UNHANDLED_EXCEPTION();
     135             :         }
     136             :     }
     137             : 
     138             :     // check, if the reference frame includes the help module.
     139             :     // But look, if this analyze step is really needed.
     140       67428 :     if (
     141       59404 :         ((m_eDetectMode & E_HELP)     == E_HELP                ) &&
     142      146504 :         (m_xReferenceFrame.is()                                ) &&
     143      142492 :         (m_xReferenceFrame->getName() == SPECIALTARGET_HELPTASK)
     144             :        )
     145             :     {
     146           0 :         m_bReferenceIsHelp = true;
     147             :     }
     148             : 
     149             :     try
     150             :     {
     151             :         // Step over all frames of the desktop frame container and analyze it.
     152       60851 :         for (sal_Int32 i=0; i<nCount; ++i)
     153             :         {
     154             :             // Ignore invalid items ... and of course the reference frame.
     155             :             // It will be a member of the given frame list too - but it was already
     156             :             // analyzed before!
     157       29143 :             css::uno::Reference< css::frame::XFrame > xFrame;
     158       58286 :             if (
     159      145715 :                 !(xFrameContainer->getByIndex(i) >>= xFrame) ||
     160      116572 :                 !(xFrame.is()                              ) ||
     161       29143 :                  (xFrame==m_xReferenceFrame                )
     162             :                )
     163       27696 :                 continue;
     164             : 
     165        4341 :             if (
     166        1447 :                 ((m_eDetectMode & E_ZOMBIE) == E_ZOMBIE) &&
     167             :                 (
     168        1447 :                  (!xFrame->getContainerWindow().is()) ||
     169        1447 :                  (!xFrame->getComponentWindow().is())
     170             :                 )
     171             :                )
     172             :             {
     173             :                 SAL_INFO("fwk", "FrameListAnalyzer::impl_analyze(): ZOMBIE!");
     174             :             }
     175             : 
     176             :             // a) Is it the special help task?
     177             :             //    Return it separated from any return list.
     178        2904 :             if (
     179        5758 :                 ((m_eDetectMode & E_HELP) == E_HELP      ) &&
     180        7195 :                 (xFrame->getName()==SPECIALTARGET_HELPTASK)
     181             :                )
     182             :             {
     183           0 :                 m_xHelp = xFrame;
     184           0 :                 continue;
     185             :             }
     186             : 
     187             :             // b) Or is includes this task the special backing component?
     188             :             //    Return it separated from any return list.
     189             :             //    But check if the reference task itself is the backing frame.
     190             :             //    Our user mst know it to decide right.
     191        1447 :             if ((m_eDetectMode & E_BACKINGCOMPONENT) == E_BACKINGCOMPONENT)
     192             :             {
     193             :                 try
     194             :                 {
     195        1447 :                     css::uno::Reference< css::uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
     196        2894 :                     css::uno::Reference< css::frame::XModuleManager2 > xModuleMgr = css::frame::ModuleManager::create(xContext);
     197        2740 :                     OUString sModule = xModuleMgr->identify(xFrame);
     198        1293 :                     if (sModule.equals("com.sun.star.frame.StartModule"))
     199             :                     {
     200           0 :                         m_xBackingComponent = xFrame;
     201           0 :                         continue;
     202        1447 :                     }
     203             :                 }
     204         154 :                 catch (const css::uno::Exception&)
     205             :                 {
     206             :                 }
     207             :             }
     208             : 
     209             :             // c) Or is it the a task, which uses the specified model?
     210             :             //    Add it to the list of "model frames".
     211        1447 :             if ((m_eDetectMode & E_MODEL) == E_MODEL)
     212             :             {
     213           4 :                 css::uno::Reference< css::frame::XController > xController = xFrame->getController();
     214           8 :                 css::uno::Reference< css::frame::XModel >      xModel;
     215           4 :                 if (xController.is())
     216           0 :                     xModel = xController->getModel();
     217           4 :                 if (xModel==xReferenceModel)
     218             :                 {
     219           0 :                     m_lModelFrames[nModelStep] = xFrame;
     220           0 :                     ++nModelStep;
     221           0 :                     continue;
     222           4 :                 }
     223             :             }
     224             : 
     225             :             // d) Or is it the a task, which use another or no model at all?
     226             :             //    Add it to the list of "other frames". But look for it's
     227             :             //    visible state ... if it's allowed to do so.
     228             : 
     229        1447 :             bool bHidden = false;
     230        1447 :             if ((m_eDetectMode & E_HIDDEN) == E_HIDDEN )
     231             :             {
     232        1435 :                 xSet = css::uno::Reference< css::beans::XPropertySet >(xFrame, css::uno::UNO_QUERY);
     233        1435 :                 if (xSet.is())
     234             :                 {
     235        1435 :                     xSet->getPropertyValue(FRAME_PROPNAME_ISHIDDEN) >>= bHidden;
     236             :                 }
     237             :             }
     238             : 
     239        1447 :             if (bHidden)
     240             :             {
     241         883 :                 m_lOtherHiddenFrames[nHiddenStep] = xFrame;
     242         883 :                 ++nHiddenStep;
     243             :             }
     244             :             else
     245             :             {
     246         564 :                 m_lOtherVisibleFrames[nVisibleStep] = xFrame;
     247         564 :                 ++nVisibleStep;
     248             :             }
     249        1447 :         }
     250             :     }
     251           0 :     catch (const css::lang::IndexOutOfBoundsException&)
     252             :     {
     253             :         // stop copying if index seems to be wrong.
     254             :         // This interface can't really guarantee its count for multithreaded
     255             :         // environments. So it can occur!
     256             :     }
     257             : 
     258             :     // Pack both lists by using the actual step positions.
     259             :     // All empty or ignorable items should exist at the end of these lists
     260             :     // behind the position pointers. So they will be removed by a reallocation.
     261       31708 :     m_lOtherVisibleFrames.realloc(nVisibleStep);
     262       31708 :     m_lOtherHiddenFrames.realloc(nHiddenStep);
     263       63416 :     m_lModelFrames.realloc(nModelStep);
     264       31708 : }
     265             : 
     266         969 : } //  namespace framework
     267             : 
     268             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10