1*6d739b60SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*6d739b60SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*6d739b60SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*6d739b60SAndrew Rist * distributed with this work for additional information 6*6d739b60SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*6d739b60SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*6d739b60SAndrew Rist * "License"); you may not use this file except in compliance 9*6d739b60SAndrew Rist * with the License. You may obtain a copy of the License at 10*6d739b60SAndrew Rist * 11*6d739b60SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*6d739b60SAndrew Rist * 13*6d739b60SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*6d739b60SAndrew Rist * software distributed under the License is distributed on an 15*6d739b60SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*6d739b60SAndrew Rist * KIND, either express or implied. See the License for the 17*6d739b60SAndrew Rist * specific language governing permissions and limitations 18*6d739b60SAndrew Rist * under the License. 19*6d739b60SAndrew Rist * 20*6d739b60SAndrew Rist *************************************************************/ 21*6d739b60SAndrew Rist 22*6d739b60SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_framework.hxx" 26cdf0e10cSrcweir #include <accelerators/acceleratorcache.hxx> 27cdf0e10cSrcweir 28cdf0e10cSrcweir //_______________________________________________ 29cdf0e10cSrcweir // own includes 30cdf0e10cSrcweir 31cdf0e10cSrcweir #ifndef __FRAMEWORK_XML_ACCELERATORCONFIGURATIONREADER_HXX_ 32cdf0e10cSrcweir #include <xml/acceleratorconfigurationreader.hxx> 33cdf0e10cSrcweir #endif 34cdf0e10cSrcweir #include <threadhelp/readguard.hxx> 35cdf0e10cSrcweir #include <threadhelp/writeguard.hxx> 36cdf0e10cSrcweir 37cdf0e10cSrcweir //_______________________________________________ 38cdf0e10cSrcweir // interface includes 39cdf0e10cSrcweir 40cdf0e10cSrcweir #ifndef __COM_SUN_STAR_CONTAINER_ELEMENTEXISTEXCEPTION_HPP_ 41cdf0e10cSrcweir #include <com/sun/star/container/ElementExistException.hpp> 42cdf0e10cSrcweir #endif 43cdf0e10cSrcweir 44cdf0e10cSrcweir #ifndef __COM_SUN_STAR_CONTAINER_NOSUCHELEMENTEXCEPTION_HPP_ 45cdf0e10cSrcweir #include <com/sun/star/container/NoSuchElementException.hpp> 46cdf0e10cSrcweir #endif 47cdf0e10cSrcweir 48cdf0e10cSrcweir //_______________________________________________ 49cdf0e10cSrcweir // other includes 50cdf0e10cSrcweir #include <vcl/svapp.hxx> 51cdf0e10cSrcweir 52cdf0e10cSrcweir namespace framework 53cdf0e10cSrcweir { 54cdf0e10cSrcweir 55cdf0e10cSrcweir //----------------------------------------------- 56cdf0e10cSrcweir AcceleratorCache::AcceleratorCache() 57cdf0e10cSrcweir : ThreadHelpBase(&Application::GetSolarMutex()) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir } 60cdf0e10cSrcweir 61cdf0e10cSrcweir //----------------------------------------------- 62cdf0e10cSrcweir AcceleratorCache::AcceleratorCache(const AcceleratorCache& rCopy) 63cdf0e10cSrcweir : ThreadHelpBase(&Application::GetSolarMutex()) 64cdf0e10cSrcweir { 65cdf0e10cSrcweir m_lCommand2Keys = rCopy.m_lCommand2Keys; 66cdf0e10cSrcweir m_lKey2Commands = rCopy.m_lKey2Commands; 67cdf0e10cSrcweir } 68cdf0e10cSrcweir 69cdf0e10cSrcweir //----------------------------------------------- 70cdf0e10cSrcweir AcceleratorCache::~AcceleratorCache() 71cdf0e10cSrcweir { 72cdf0e10cSrcweir // Dont save anything automaticly here. 73cdf0e10cSrcweir // The user has to do that explicitly! 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir //----------------------------------------------- 77cdf0e10cSrcweir void AcceleratorCache::takeOver(const AcceleratorCache& rCopy) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir // SAFE -> ---------------------------------- 80cdf0e10cSrcweir WriteGuard aWriteLock(m_aLock); 81cdf0e10cSrcweir 82cdf0e10cSrcweir m_lCommand2Keys = rCopy.m_lCommand2Keys; 83cdf0e10cSrcweir m_lKey2Commands = rCopy.m_lKey2Commands; 84cdf0e10cSrcweir 85cdf0e10cSrcweir aWriteLock.unlock(); 86cdf0e10cSrcweir // <- SAFE ---------------------------------- 87cdf0e10cSrcweir } 88cdf0e10cSrcweir 89cdf0e10cSrcweir //----------------------------------------------- 90cdf0e10cSrcweir AcceleratorCache& AcceleratorCache::operator=(const AcceleratorCache& rCopy) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir takeOver(rCopy); 93cdf0e10cSrcweir return *this; 94cdf0e10cSrcweir } 95cdf0e10cSrcweir 96cdf0e10cSrcweir //----------------------------------------------- 97cdf0e10cSrcweir sal_Bool AcceleratorCache::hasKey(const css::awt::KeyEvent& aKey) const 98cdf0e10cSrcweir { 99cdf0e10cSrcweir // SAFE -> ---------------------------------- 100cdf0e10cSrcweir ReadGuard aReadLock(m_aLock); 101cdf0e10cSrcweir 102cdf0e10cSrcweir return (m_lKey2Commands.find(aKey) != m_lKey2Commands.end()); 103cdf0e10cSrcweir // <- SAFE ---------------------------------- 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106cdf0e10cSrcweir //----------------------------------------------- 107cdf0e10cSrcweir sal_Bool AcceleratorCache::hasCommand(const ::rtl::OUString& sCommand) const 108cdf0e10cSrcweir { 109cdf0e10cSrcweir // SAFE -> ---------------------------------- 110cdf0e10cSrcweir ReadGuard aReadLock(m_aLock); 111cdf0e10cSrcweir 112cdf0e10cSrcweir return (m_lCommand2Keys.find(sCommand) != m_lCommand2Keys.end()); 113cdf0e10cSrcweir // <- SAFE ---------------------------------- 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir //----------------------------------------------- 117cdf0e10cSrcweir AcceleratorCache::TKeyList AcceleratorCache::getAllKeys() const 118cdf0e10cSrcweir { 119cdf0e10cSrcweir TKeyList lKeys; 120cdf0e10cSrcweir 121cdf0e10cSrcweir // SAFE -> ---------------------------------- 122cdf0e10cSrcweir ReadGuard aReadLock(m_aLock); 123cdf0e10cSrcweir lKeys.reserve(m_lKey2Commands.size()); 124cdf0e10cSrcweir 125cdf0e10cSrcweir TKey2Commands::const_iterator pIt; 126cdf0e10cSrcweir TKey2Commands::const_iterator pEnd = m_lKey2Commands.end(); 127cdf0e10cSrcweir for ( pIt = m_lKey2Commands.begin(); 128cdf0e10cSrcweir pIt != pEnd ; 129cdf0e10cSrcweir ++pIt ) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir lKeys.push_back(pIt->first); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir aReadLock.unlock(); 135cdf0e10cSrcweir // <- SAFE ---------------------------------- 136cdf0e10cSrcweir 137cdf0e10cSrcweir return lKeys; 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir //----------------------------------------------- 141cdf0e10cSrcweir void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent& aKey , 142cdf0e10cSrcweir const ::rtl::OUString& sCommand) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir // SAFE -> ---------------------------------- 145cdf0e10cSrcweir WriteGuard aWriteLock(m_aLock); 146cdf0e10cSrcweir 147cdf0e10cSrcweir // register command for the specified key 148cdf0e10cSrcweir m_lKey2Commands[aKey] = sCommand; 149cdf0e10cSrcweir 150cdf0e10cSrcweir // update optimized structure to bind multiple keys to one command 151cdf0e10cSrcweir TKeyList& rKeyList = m_lCommand2Keys[sCommand]; 152cdf0e10cSrcweir rKeyList.push_back(aKey); 153cdf0e10cSrcweir 154cdf0e10cSrcweir aWriteLock.unlock(); 155cdf0e10cSrcweir // <- SAFE ---------------------------------- 156cdf0e10cSrcweir } 157cdf0e10cSrcweir 158cdf0e10cSrcweir //----------------------------------------------- 159cdf0e10cSrcweir AcceleratorCache::TKeyList AcceleratorCache::getKeysByCommand(const ::rtl::OUString& sCommand) const 160cdf0e10cSrcweir { 161cdf0e10cSrcweir TKeyList lKeys; 162cdf0e10cSrcweir 163cdf0e10cSrcweir // SAFE -> ---------------------------------- 164cdf0e10cSrcweir ReadGuard aReadLock(m_aLock); 165cdf0e10cSrcweir 166cdf0e10cSrcweir TCommand2Keys::const_iterator pCommand = m_lCommand2Keys.find(sCommand); 167cdf0e10cSrcweir if (pCommand == m_lCommand2Keys.end()) 168cdf0e10cSrcweir throw css::container::NoSuchElementException( 169cdf0e10cSrcweir ::rtl::OUString(), css::uno::Reference< css::uno::XInterface >()); 170cdf0e10cSrcweir lKeys = pCommand->second; 171cdf0e10cSrcweir 172cdf0e10cSrcweir aReadLock.unlock(); 173cdf0e10cSrcweir // <- SAFE ---------------------------------- 174cdf0e10cSrcweir 175cdf0e10cSrcweir return lKeys; 176cdf0e10cSrcweir } 177cdf0e10cSrcweir 178cdf0e10cSrcweir //----------------------------------------------- 179cdf0e10cSrcweir ::rtl::OUString AcceleratorCache::getCommandByKey(const css::awt::KeyEvent& aKey) const 180cdf0e10cSrcweir { 181cdf0e10cSrcweir ::rtl::OUString sCommand; 182cdf0e10cSrcweir 183cdf0e10cSrcweir // SAFE -> ---------------------------------- 184cdf0e10cSrcweir ReadGuard aReadLock(m_aLock); 185cdf0e10cSrcweir 186cdf0e10cSrcweir TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey); 187cdf0e10cSrcweir if (pKey == m_lKey2Commands.end()) 188cdf0e10cSrcweir throw css::container::NoSuchElementException( 189cdf0e10cSrcweir ::rtl::OUString(), css::uno::Reference< css::uno::XInterface >()); 190cdf0e10cSrcweir sCommand = pKey->second; 191cdf0e10cSrcweir 192cdf0e10cSrcweir aReadLock.unlock(); 193cdf0e10cSrcweir // <- SAFE ---------------------------------- 194cdf0e10cSrcweir 195cdf0e10cSrcweir return sCommand; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir //----------------------------------------------- 199cdf0e10cSrcweir void AcceleratorCache::removeKey(const css::awt::KeyEvent& aKey) 200cdf0e10cSrcweir { 201cdf0e10cSrcweir // SAFE -> ---------------------------------- 202cdf0e10cSrcweir WriteGuard aWriteLock(m_aLock); 203cdf0e10cSrcweir 204cdf0e10cSrcweir // check if key exists 205cdf0e10cSrcweir TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey); 206cdf0e10cSrcweir if (pKey == m_lKey2Commands.end()) 207cdf0e10cSrcweir return; 208cdf0e10cSrcweir 209cdf0e10cSrcweir // get its registered command 210cdf0e10cSrcweir // Because we must know its place inside the optimized 211cdf0e10cSrcweir // structure, which bind keys to commands, too! 212cdf0e10cSrcweir ::rtl::OUString sCommand = pKey->second; 213cdf0e10cSrcweir pKey = m_lKey2Commands.end(); // nobody should use an undefined value .-) 214cdf0e10cSrcweir 215cdf0e10cSrcweir // remove key from primary list 216cdf0e10cSrcweir m_lKey2Commands.erase(aKey); 217cdf0e10cSrcweir 218cdf0e10cSrcweir // remove key from optimized command list 219cdf0e10cSrcweir m_lCommand2Keys.erase(sCommand); 220cdf0e10cSrcweir 221cdf0e10cSrcweir aWriteLock.unlock(); 222cdf0e10cSrcweir // <- SAFE ---------------------------------- 223cdf0e10cSrcweir } 224cdf0e10cSrcweir 225cdf0e10cSrcweir //----------------------------------------------- 226cdf0e10cSrcweir void AcceleratorCache::removeCommand(const ::rtl::OUString& sCommand) 227cdf0e10cSrcweir { 228cdf0e10cSrcweir // SAFE -> ---------------------------------- 229cdf0e10cSrcweir WriteGuard aWriteLock(m_aLock); 230cdf0e10cSrcweir 231cdf0e10cSrcweir const TKeyList& lKeys = getKeysByCommand(sCommand); 232cdf0e10cSrcweir AcceleratorCache::TKeyList::const_iterator pKey ; 233cdf0e10cSrcweir for ( pKey = lKeys.begin(); 234cdf0e10cSrcweir pKey != lKeys.end() ; 235cdf0e10cSrcweir ++pKey ) 236cdf0e10cSrcweir { 237cdf0e10cSrcweir const css::awt::KeyEvent& rKey = *pKey; 238cdf0e10cSrcweir removeKey(rKey); 239cdf0e10cSrcweir } 240cdf0e10cSrcweir m_lCommand2Keys.erase(sCommand); 241cdf0e10cSrcweir 242cdf0e10cSrcweir aWriteLock.unlock(); 243cdf0e10cSrcweir // <- SAFE ---------------------------------- 244cdf0e10cSrcweir } 245cdf0e10cSrcweir 246cdf0e10cSrcweir } // namespace framework 247