LCOV - code coverage report
Current view: top level - libreoffice/framework/source/dispatch - windowcommanddispatch.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 35 69 50.7 %
Date: 2012-12-27 Functions: 7 8 87.5 %
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 <dispatch/windowcommanddispatch.hxx>
      21             : #include <threadhelp/readguard.hxx>
      22             : #include <threadhelp/writeguard.hxx>
      23             : #include <targets.h>
      24             : #include <services.h>
      25             : 
      26             : #include <com/sun/star/frame/XDispatchProvider.hpp>
      27             : #include <com/sun/star/frame/XDispatch.hpp>
      28             : #include <com/sun/star/util/URLTransformer.hpp>
      29             : #include <com/sun/star/util/XURLTransformer.hpp>
      30             : 
      31             : #include <comphelper/processfactory.hxx>
      32             : #include <vcl/window.hxx>
      33             : #include <vcl/svapp.hxx>
      34             : #include <vcl/cmdevt.hxx>
      35             : #include <osl/mutex.hxx>
      36             : #include <toolkit/helper/vclunohelper.hxx>
      37             : #include <rtl/logfile.hxx>
      38             : 
      39             : 
      40             : namespace framework{
      41             : 
      42             : //-----------------------------------------------
      43         240 : WindowCommandDispatch::WindowCommandDispatch(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
      44             :                          const css::uno::Reference< css::frame::XFrame >&              xFrame)
      45             :     : ThreadHelpBase(                            )
      46             :     , m_xSMGR       (xSMGR                       )
      47             :     , m_xFrame      (xFrame                      )
      48         240 :     , m_xWindow     (xFrame->getContainerWindow())
      49             : {
      50         240 :     impl_startListening();
      51         240 : }
      52             : 
      53             : //-----------------------------------------------
      54         189 : WindowCommandDispatch::~WindowCommandDispatch()
      55             : {
      56          63 :     impl_stopListening();
      57          63 :     m_xSMGR.clear();
      58         126 : }
      59             : 
      60             : //-----------------------------------------------
      61         240 : void WindowCommandDispatch::impl_startListening()
      62             : {
      63         240 :     ReadGuard aReadLock(m_aLock);
      64         240 :     css::uno::Reference< css::awt::XWindow > xWindow( m_xWindow.get(), css::uno::UNO_QUERY );
      65         240 :     aReadLock.unlock();
      66             : 
      67         240 :     if ( ! xWindow.is())
      68             :         return;
      69             : 
      70             :     {
      71         240 :         SolarMutexGuard aSolarLock;
      72             : 
      73         240 :         Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
      74         240 :         if ( ! pWindow)
      75             :             return;
      76             : 
      77         240 :         pWindow->AddEventListener( LINK(this, WindowCommandDispatch, impl_notifyCommand) );
      78         240 :     }
      79             : }
      80             : 
      81          63 : void WindowCommandDispatch::impl_stopListening()
      82             : {
      83          63 :     ReadGuard aReadLock(m_aLock);
      84          63 :     css::uno::Reference< css::awt::XWindow > xWindow( m_xWindow.get(), css::uno::UNO_QUERY );
      85          63 :     aReadLock.unlock();
      86             : 
      87          63 :     if (!xWindow.is())
      88             :         return;
      89             : 
      90             :     {
      91          63 :         SolarMutexGuard aSolarLock;
      92             : 
      93          63 :         Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
      94          63 :         if (!pWindow)
      95             :             return;
      96             : 
      97          63 :         pWindow->RemoveEventListener( LINK(this, WindowCommandDispatch, impl_notifyCommand) );
      98             : 
      99          63 :         m_xWindow.clear();
     100          63 :     }
     101             : }
     102             : 
     103             : 
     104             : //-----------------------------------------------
     105        9366 : IMPL_LINK(WindowCommandDispatch, impl_notifyCommand, void*, pParam)
     106             : {
     107        4683 :     if ( ! pParam)
     108           0 :         return 0L;
     109             : 
     110        4683 :     const VclWindowEvent* pEvent = (VclWindowEvent*)pParam;
     111        4683 :     if (pEvent->GetId() == VCLEVENT_OBJECT_DYING)
     112             :     {
     113           0 :         impl_stopListening();
     114           0 :         return 0L;
     115             :     }
     116        4683 :     if (pEvent->GetId() != VCLEVENT_WINDOW_COMMAND)
     117        4683 :         return 0L;
     118             : 
     119           0 :     const CommandEvent* pCommand = (CommandEvent*)pEvent->GetData();
     120           0 :     if (pCommand->GetCommand() != COMMAND_SHOWDIALOG)
     121           0 :         return 0L;
     122             : 
     123           0 :     const CommandDialogData* pData = pCommand->GetDialogData();
     124           0 :     if ( ! pData)
     125           0 :         return 0L;
     126             : 
     127           0 :     const int nCommand = pData->GetDialogId();
     128           0 :     ::rtl::OUString sCommand;
     129             : 
     130           0 :     switch (nCommand)
     131             :     {
     132             :         case SHOWDIALOG_ID_PREFERENCES :
     133           0 :                 sCommand = rtl::OUString(".uno:OptionsTreeDialog");
     134           0 :                 break;
     135             : 
     136             :         case SHOWDIALOG_ID_ABOUT :
     137           0 :                 sCommand = rtl::OUString(".uno:About");
     138           0 :                 break;
     139             : 
     140             :         default :
     141           0 :                 return 0L;
     142             :     }
     143             : 
     144           0 :     impl_dispatchCommand(sCommand);
     145             : 
     146           0 :     return 0L;
     147             : }
     148             : 
     149             : //-----------------------------------------------
     150           0 : void WindowCommandDispatch::impl_dispatchCommand(const ::rtl::OUString& sCommand)
     151             : {
     152             :     // ignore all errors here. It's clicking a menu entry only ...
     153             :     // The user will try it again, in case nothing happens .-)
     154             :     try
     155             :     {
     156             :         // SYNCHRONIZED ->
     157           0 :         ReadGuard aReadLock(m_aLock);
     158           0 :         css::uno::Reference< css::frame::XDispatchProvider >   xProvider(m_xFrame.get(), css::uno::UNO_QUERY_THROW);
     159           0 :         css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR    = m_xSMGR;
     160           0 :         aReadLock.unlock();
     161             :         // <- SYNCHRONIZED
     162             : 
     163             :         // check provider ... we know it's weak reference only
     164           0 :         if ( ! xProvider.is())
     165           0 :             return;
     166             : 
     167           0 :         css::uno::Reference< css::util::XURLTransformer > xParser(css::util::URLTransformer::create(::comphelper::getComponentContext(xSMGR)));
     168           0 :         css::util::URL aCommand;
     169           0 :         aCommand.Complete = sCommand;
     170           0 :         xParser->parseStrict(aCommand);
     171             : 
     172           0 :         css::uno::Reference< css::frame::XDispatch > xDispatch = xProvider->queryDispatch(aCommand, SPECIALTARGET_SELF, 0);
     173           0 :         if (xDispatch.is())
     174           0 :             xDispatch->dispatch(aCommand, css::uno::Sequence< css::beans::PropertyValue >());
     175             :     }
     176           0 :     catch(const css::uno::Exception&)
     177             :     {}
     178             : }
     179             : 
     180             : } // namespace framework
     181             : 
     182             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10