LCOV - code coverage report
Current view: top level - framework/source/accelerators - acceleratorcache.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 30 69 43.5 %
Date: 2014-11-03 Functions: 10 16 62.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 <accelerators/acceleratorcache.hxx>
      21             : 
      22             : #include <xml/acceleratorconfigurationreader.hxx>
      23             : 
      24             : #include <com/sun/star/container/ElementExistException.hpp>
      25             : 
      26             : #include <com/sun/star/container/NoSuchElementException.hpp>
      27             : 
      28             : #include <vcl/svapp.hxx>
      29             : 
      30             : namespace framework
      31             : {
      32             : 
      33       22744 : AcceleratorCache::AcceleratorCache()
      34             : {
      35       22744 : }
      36             : 
      37           0 : AcceleratorCache::AcceleratorCache(const AcceleratorCache& rCopy)
      38             : {
      39           0 :     m_lCommand2Keys = rCopy.m_lCommand2Keys;
      40           0 :     m_lKey2Commands = rCopy.m_lKey2Commands;
      41           0 : }
      42             : 
      43       22720 : AcceleratorCache::~AcceleratorCache()
      44             : {
      45             :     // Dont save anything automatically here.
      46             :     // The user has to do that explicitly!
      47       22720 : }
      48             : 
      49       14662 : void AcceleratorCache::takeOver(const AcceleratorCache& rCopy)
      50             : {
      51       14662 :     SolarMutexGuard g;
      52       14662 :     m_lCommand2Keys = rCopy.m_lCommand2Keys;
      53       14662 :     m_lKey2Commands = rCopy.m_lKey2Commands;
      54       14662 : }
      55             : 
      56        8074 : AcceleratorCache& AcceleratorCache::operator=(const AcceleratorCache& rCopy)
      57             : {
      58        8074 :     takeOver(rCopy);
      59        8074 :     return *this;
      60             : }
      61             : 
      62      120578 : bool AcceleratorCache::hasKey(const css::awt::KeyEvent& aKey) const
      63             : {
      64      120578 :     SolarMutexGuard g;
      65      120578 :     return (m_lKey2Commands.find(aKey) != m_lKey2Commands.end());
      66             : }
      67             : 
      68      354870 : bool AcceleratorCache::hasCommand(const OUString& sCommand) const
      69             : {
      70      354870 :     SolarMutexGuard g;
      71      354870 :     return (m_lCommand2Keys.find(sCommand) != m_lCommand2Keys.end());
      72             : }
      73             : 
      74           0 : AcceleratorCache::TKeyList AcceleratorCache::getAllKeys() const
      75             : {
      76           0 :     SolarMutexGuard g;
      77           0 :     TKeyList lKeys;
      78           0 :     lKeys.reserve(m_lKey2Commands.size());
      79             : 
      80           0 :     TKey2Commands::const_iterator pIt;
      81           0 :     TKey2Commands::const_iterator pEnd = m_lKey2Commands.end();
      82           0 :     for (  pIt  = m_lKey2Commands.begin();
      83             :            pIt != pEnd;
      84             :          ++pIt                           )
      85             :     {
      86           0 :         lKeys.push_back(pIt->first);
      87             :     }
      88             : 
      89           0 :     return lKeys;
      90             : }
      91             : 
      92      120578 : void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent& aKey    ,
      93             :                                          const OUString&    sCommand)
      94             : {
      95      120578 :     SolarMutexGuard g;
      96             : 
      97             :     // register command for the specified key
      98      120578 :     m_lKey2Commands[aKey] = sCommand;
      99             : 
     100             :     // update optimized structure to bind multiple keys to one command
     101      120578 :     TKeyList& rKeyList = m_lCommand2Keys[sCommand];
     102      120578 :     rKeyList.push_back(aKey);
     103      120578 : }
     104             : 
     105       85066 : AcceleratorCache::TKeyList AcceleratorCache::getKeysByCommand(const OUString& sCommand) const
     106             : {
     107       85066 :     SolarMutexGuard g;
     108       85066 :     TCommand2Keys::const_iterator pCommand = m_lCommand2Keys.find(sCommand);
     109       85066 :     if (pCommand == m_lCommand2Keys.end())
     110           0 :         throw css::container::NoSuchElementException();
     111       85066 :     return pCommand->second;
     112             : }
     113             : 
     114           0 : OUString AcceleratorCache::getCommandByKey(const css::awt::KeyEvent& aKey) const
     115             : {
     116           0 :     SolarMutexGuard g;
     117           0 :     TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
     118           0 :     if (pKey == m_lKey2Commands.end())
     119           0 :         throw css::container::NoSuchElementException();
     120           0 :     return pKey->second;
     121             : }
     122             : 
     123           0 : void AcceleratorCache::removeKey(const css::awt::KeyEvent& aKey)
     124             : {
     125           0 :     SolarMutexGuard g;
     126             : 
     127             :     // check if key exists
     128           0 :     TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
     129           0 :     if (pKey == m_lKey2Commands.end())
     130           0 :         return;
     131             : 
     132             :     // get its registered command
     133             :     // Because we must know its place inside the optimized
     134             :     // structure, which bind keys to commands, too!
     135           0 :     OUString sCommand = pKey->second;
     136           0 :     pKey = m_lKey2Commands.end(); // nobody should use an undefined value .-)
     137             : 
     138             :     // remove key from primary list
     139           0 :     m_lKey2Commands.erase(aKey);
     140             : 
     141             :     // remove key from optimized command list
     142           0 :     m_lCommand2Keys.erase(sCommand);
     143             : }
     144             : 
     145           0 : void AcceleratorCache::removeCommand(const OUString& sCommand)
     146             : {
     147           0 :     SolarMutexGuard g;
     148             : 
     149           0 :     const TKeyList&                            lKeys = getKeysByCommand(sCommand);
     150           0 :     AcceleratorCache::TKeyList::const_iterator pKey;
     151           0 :     for (  pKey  = lKeys.begin();
     152           0 :            pKey != lKeys.end();
     153             :          ++pKey                 )
     154             :     {
     155           0 :         const css::awt::KeyEvent& rKey = *pKey;
     156           0 :         removeKey(rKey);
     157             :     }
     158           0 :     m_lCommand2Keys.erase(sCommand);
     159           0 : }
     160             : 
     161         951 : } // namespace framework
     162             : 
     163             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10