update credits
[LibreOffice.git] / framework / source / accelerators / acceleratorcache.cxx
blob2c1d8d555a6b933b53793ee004a449996bda1cc9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <accelerators/acceleratorcache.hxx>
22 #include <xml/acceleratorconfigurationreader.hxx>
23 #include <threadhelp/readguard.hxx>
24 #include <threadhelp/writeguard.hxx>
26 #include <com/sun/star/container/ElementExistException.hpp>
28 #include <com/sun/star/container/NoSuchElementException.hpp>
30 #include <vcl/svapp.hxx>
32 namespace framework
35 //-----------------------------------------------
36 AcceleratorCache::AcceleratorCache()
37 : ThreadHelpBase(&Application::GetSolarMutex())
41 //-----------------------------------------------
42 AcceleratorCache::AcceleratorCache(const AcceleratorCache& rCopy)
43 : ThreadHelpBase(&Application::GetSolarMutex())
45 m_lCommand2Keys = rCopy.m_lCommand2Keys;
46 m_lKey2Commands = rCopy.m_lKey2Commands;
49 //-----------------------------------------------
50 AcceleratorCache::~AcceleratorCache()
52 // Dont save anything automaticly here.
53 // The user has to do that explicitly!
56 //-----------------------------------------------
57 void AcceleratorCache::takeOver(const AcceleratorCache& rCopy)
59 // SAFE -> ----------------------------------
60 WriteGuard aWriteLock(m_aLock);
62 m_lCommand2Keys = rCopy.m_lCommand2Keys;
63 m_lKey2Commands = rCopy.m_lKey2Commands;
65 aWriteLock.unlock();
66 // <- SAFE ----------------------------------
69 //-----------------------------------------------
70 AcceleratorCache& AcceleratorCache::operator=(const AcceleratorCache& rCopy)
72 takeOver(rCopy);
73 return *this;
76 //-----------------------------------------------
77 sal_Bool AcceleratorCache::hasKey(const css::awt::KeyEvent& aKey) const
79 // SAFE -> ----------------------------------
80 ReadGuard aReadLock(m_aLock);
82 return (m_lKey2Commands.find(aKey) != m_lKey2Commands.end());
83 // <- SAFE ----------------------------------
86 //-----------------------------------------------
87 sal_Bool AcceleratorCache::hasCommand(const OUString& sCommand) const
89 // SAFE -> ----------------------------------
90 ReadGuard aReadLock(m_aLock);
92 return (m_lCommand2Keys.find(sCommand) != m_lCommand2Keys.end());
93 // <- SAFE ----------------------------------
96 //-----------------------------------------------
97 AcceleratorCache::TKeyList AcceleratorCache::getAllKeys() const
99 TKeyList lKeys;
101 // SAFE -> ----------------------------------
102 ReadGuard aReadLock(m_aLock);
103 lKeys.reserve(m_lKey2Commands.size());
105 TKey2Commands::const_iterator pIt;
106 TKey2Commands::const_iterator pEnd = m_lKey2Commands.end();
107 for ( pIt = m_lKey2Commands.begin();
108 pIt != pEnd ;
109 ++pIt )
111 lKeys.push_back(pIt->first);
114 aReadLock.unlock();
115 // <- SAFE ----------------------------------
117 return lKeys;
120 //-----------------------------------------------
121 void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent& aKey ,
122 const OUString& sCommand)
124 // SAFE -> ----------------------------------
125 WriteGuard aWriteLock(m_aLock);
127 // register command for the specified key
128 m_lKey2Commands[aKey] = sCommand;
130 // update optimized structure to bind multiple keys to one command
131 TKeyList& rKeyList = m_lCommand2Keys[sCommand];
132 rKeyList.push_back(aKey);
134 aWriteLock.unlock();
135 // <- SAFE ----------------------------------
138 //-----------------------------------------------
139 AcceleratorCache::TKeyList AcceleratorCache::getKeysByCommand(const OUString& sCommand) const
141 TKeyList lKeys;
143 // SAFE -> ----------------------------------
144 ReadGuard aReadLock(m_aLock);
146 TCommand2Keys::const_iterator pCommand = m_lCommand2Keys.find(sCommand);
147 if (pCommand == m_lCommand2Keys.end())
148 throw css::container::NoSuchElementException(
149 OUString(), css::uno::Reference< css::uno::XInterface >());
150 lKeys = pCommand->second;
152 aReadLock.unlock();
153 // <- SAFE ----------------------------------
155 return lKeys;
158 //-----------------------------------------------
159 OUString AcceleratorCache::getCommandByKey(const css::awt::KeyEvent& aKey) const
161 OUString sCommand;
163 // SAFE -> ----------------------------------
164 ReadGuard aReadLock(m_aLock);
166 TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
167 if (pKey == m_lKey2Commands.end())
168 throw css::container::NoSuchElementException(
169 OUString(), css::uno::Reference< css::uno::XInterface >());
170 sCommand = pKey->second;
172 aReadLock.unlock();
173 // <- SAFE ----------------------------------
175 return sCommand;
178 //-----------------------------------------------
179 void AcceleratorCache::removeKey(const css::awt::KeyEvent& aKey)
181 // SAFE -> ----------------------------------
182 WriteGuard aWriteLock(m_aLock);
184 // check if key exists
185 TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
186 if (pKey == m_lKey2Commands.end())
187 return;
189 // get its registered command
190 // Because we must know its place inside the optimized
191 // structure, which bind keys to commands, too!
192 OUString sCommand = pKey->second;
193 pKey = m_lKey2Commands.end(); // nobody should use an undefined value .-)
195 // remove key from primary list
196 m_lKey2Commands.erase(aKey);
198 // remove key from optimized command list
199 m_lCommand2Keys.erase(sCommand);
201 aWriteLock.unlock();
202 // <- SAFE ----------------------------------
205 //-----------------------------------------------
206 void AcceleratorCache::removeCommand(const OUString& sCommand)
208 // SAFE -> ----------------------------------
209 WriteGuard aWriteLock(m_aLock);
211 const TKeyList& lKeys = getKeysByCommand(sCommand);
212 AcceleratorCache::TKeyList::const_iterator pKey ;
213 for ( pKey = lKeys.begin();
214 pKey != lKeys.end() ;
215 ++pKey )
217 const css::awt::KeyEvent& rKey = *pKey;
218 removeKey(rKey);
220 m_lCommand2Keys.erase(sCommand);
222 aWriteLock.unlock();
223 // <- SAFE ----------------------------------
226 } // namespace framework
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */