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 : #include <threadhelp/readguard.hxx>
24 : #include <threadhelp/writeguard.hxx>
25 :
26 : #include <com/sun/star/container/ElementExistException.hpp>
27 :
28 : #include <com/sun/star/container/NoSuchElementException.hpp>
29 :
30 : #include <vcl/svapp.hxx>
31 :
32 : namespace framework
33 : {
34 :
35 : //-----------------------------------------------
36 0 : AcceleratorCache::AcceleratorCache()
37 0 : : ThreadHelpBase(&Application::GetSolarMutex())
38 : {
39 0 : }
40 :
41 : //-----------------------------------------------
42 0 : AcceleratorCache::AcceleratorCache(const AcceleratorCache& rCopy)
43 0 : : ThreadHelpBase(&Application::GetSolarMutex())
44 : {
45 0 : m_lCommand2Keys = rCopy.m_lCommand2Keys;
46 0 : m_lKey2Commands = rCopy.m_lKey2Commands;
47 0 : }
48 :
49 : //-----------------------------------------------
50 0 : AcceleratorCache::~AcceleratorCache()
51 : {
52 : // Dont save anything automaticly here.
53 : // The user has to do that explicitly!
54 0 : }
55 :
56 : //-----------------------------------------------
57 0 : void AcceleratorCache::takeOver(const AcceleratorCache& rCopy)
58 : {
59 : // SAFE -> ----------------------------------
60 0 : WriteGuard aWriteLock(m_aLock);
61 :
62 0 : m_lCommand2Keys = rCopy.m_lCommand2Keys;
63 0 : m_lKey2Commands = rCopy.m_lKey2Commands;
64 :
65 0 : aWriteLock.unlock();
66 : // <- SAFE ----------------------------------
67 0 : }
68 :
69 : //-----------------------------------------------
70 0 : AcceleratorCache& AcceleratorCache::operator=(const AcceleratorCache& rCopy)
71 : {
72 0 : takeOver(rCopy);
73 0 : return *this;
74 : }
75 :
76 : //-----------------------------------------------
77 0 : sal_Bool AcceleratorCache::hasKey(const css::awt::KeyEvent& aKey) const
78 : {
79 : // SAFE -> ----------------------------------
80 0 : ReadGuard aReadLock(m_aLock);
81 :
82 0 : return (m_lKey2Commands.find(aKey) != m_lKey2Commands.end());
83 : // <- SAFE ----------------------------------
84 : }
85 :
86 : //-----------------------------------------------
87 0 : sal_Bool AcceleratorCache::hasCommand(const ::rtl::OUString& sCommand) const
88 : {
89 : // SAFE -> ----------------------------------
90 0 : ReadGuard aReadLock(m_aLock);
91 :
92 0 : return (m_lCommand2Keys.find(sCommand) != m_lCommand2Keys.end());
93 : // <- SAFE ----------------------------------
94 : }
95 :
96 : //-----------------------------------------------
97 0 : AcceleratorCache::TKeyList AcceleratorCache::getAllKeys() const
98 : {
99 0 : TKeyList lKeys;
100 :
101 : // SAFE -> ----------------------------------
102 0 : ReadGuard aReadLock(m_aLock);
103 0 : lKeys.reserve(m_lKey2Commands.size());
104 :
105 0 : TKey2Commands::const_iterator pIt;
106 0 : TKey2Commands::const_iterator pEnd = m_lKey2Commands.end();
107 0 : for ( pIt = m_lKey2Commands.begin();
108 : pIt != pEnd ;
109 : ++pIt )
110 : {
111 0 : lKeys.push_back(pIt->first);
112 : }
113 :
114 0 : aReadLock.unlock();
115 : // <- SAFE ----------------------------------
116 :
117 0 : return lKeys;
118 : }
119 :
120 : //-----------------------------------------------
121 0 : void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent& aKey ,
122 : const ::rtl::OUString& sCommand)
123 : {
124 : // SAFE -> ----------------------------------
125 0 : WriteGuard aWriteLock(m_aLock);
126 :
127 : // register command for the specified key
128 0 : m_lKey2Commands[aKey] = sCommand;
129 :
130 : // update optimized structure to bind multiple keys to one command
131 0 : TKeyList& rKeyList = m_lCommand2Keys[sCommand];
132 0 : rKeyList.push_back(aKey);
133 :
134 0 : aWriteLock.unlock();
135 : // <- SAFE ----------------------------------
136 0 : }
137 :
138 : //-----------------------------------------------
139 0 : AcceleratorCache::TKeyList AcceleratorCache::getKeysByCommand(const ::rtl::OUString& sCommand) const
140 : {
141 0 : TKeyList lKeys;
142 :
143 : // SAFE -> ----------------------------------
144 0 : ReadGuard aReadLock(m_aLock);
145 :
146 0 : TCommand2Keys::const_iterator pCommand = m_lCommand2Keys.find(sCommand);
147 0 : if (pCommand == m_lCommand2Keys.end())
148 : throw css::container::NoSuchElementException(
149 0 : ::rtl::OUString(), css::uno::Reference< css::uno::XInterface >());
150 0 : lKeys = pCommand->second;
151 :
152 0 : aReadLock.unlock();
153 : // <- SAFE ----------------------------------
154 :
155 0 : return lKeys;
156 : }
157 :
158 : //-----------------------------------------------
159 0 : ::rtl::OUString AcceleratorCache::getCommandByKey(const css::awt::KeyEvent& aKey) const
160 : {
161 0 : ::rtl::OUString sCommand;
162 :
163 : // SAFE -> ----------------------------------
164 0 : ReadGuard aReadLock(m_aLock);
165 :
166 0 : TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
167 0 : if (pKey == m_lKey2Commands.end())
168 : throw css::container::NoSuchElementException(
169 0 : ::rtl::OUString(), css::uno::Reference< css::uno::XInterface >());
170 0 : sCommand = pKey->second;
171 :
172 0 : aReadLock.unlock();
173 : // <- SAFE ----------------------------------
174 :
175 0 : return sCommand;
176 : }
177 :
178 : //-----------------------------------------------
179 0 : void AcceleratorCache::removeKey(const css::awt::KeyEvent& aKey)
180 : {
181 : // SAFE -> ----------------------------------
182 0 : WriteGuard aWriteLock(m_aLock);
183 :
184 : // check if key exists
185 0 : TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
186 0 : if (pKey == m_lKey2Commands.end())
187 0 : return;
188 :
189 : // get its registered command
190 : // Because we must know its place inside the optimized
191 : // structure, which bind keys to commands, too!
192 0 : ::rtl::OUString sCommand = pKey->second;
193 0 : pKey = m_lKey2Commands.end(); // nobody should use an undefined value .-)
194 :
195 : // remove key from primary list
196 0 : m_lKey2Commands.erase(aKey);
197 :
198 : // remove key from optimized command list
199 0 : m_lCommand2Keys.erase(sCommand);
200 :
201 0 : aWriteLock.unlock();
202 : // <- SAFE ----------------------------------
203 : }
204 :
205 : //-----------------------------------------------
206 0 : void AcceleratorCache::removeCommand(const ::rtl::OUString& sCommand)
207 : {
208 : // SAFE -> ----------------------------------
209 0 : WriteGuard aWriteLock(m_aLock);
210 :
211 0 : const TKeyList& lKeys = getKeysByCommand(sCommand);
212 0 : AcceleratorCache::TKeyList::const_iterator pKey ;
213 0 : for ( pKey = lKeys.begin();
214 0 : pKey != lKeys.end() ;
215 : ++pKey )
216 : {
217 0 : const css::awt::KeyEvent& rKey = *pKey;
218 0 : removeKey(rKey);
219 : }
220 0 : m_lCommand2Keys.erase(sCommand);
221 :
222 0 : aWriteLock.unlock();
223 : // <- SAFE ----------------------------------
224 0 : }
225 :
226 : } // namespace framework
227 :
228 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|