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 9364 : AcceleratorCache::AcceleratorCache()
34 : {
35 9364 : }
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 9351 : AcceleratorCache::~AcceleratorCache()
44 : {
45 : // Dont save anything automatically here.
46 : // The user has to do that explicitly!
47 9351 : }
48 :
49 6040 : void AcceleratorCache::takeOver(const AcceleratorCache& rCopy)
50 : {
51 6040 : SolarMutexGuard g;
52 6040 : m_lCommand2Keys = rCopy.m_lCommand2Keys;
53 6040 : m_lKey2Commands = rCopy.m_lKey2Commands;
54 6040 : }
55 :
56 3320 : AcceleratorCache& AcceleratorCache::operator=(const AcceleratorCache& rCopy)
57 : {
58 3320 : takeOver(rCopy);
59 3320 : return *this;
60 : }
61 :
62 53868 : bool AcceleratorCache::hasKey(const css::awt::KeyEvent& aKey) const
63 : {
64 53868 : SolarMutexGuard g;
65 53868 : return (m_lKey2Commands.find(aKey) != m_lKey2Commands.end());
66 : }
67 :
68 216622 : bool AcceleratorCache::hasCommand(const OUString& sCommand) const
69 : {
70 216622 : SolarMutexGuard g;
71 216622 : 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 53858 : void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent& aKey ,
93 : const OUString& sCommand)
94 : {
95 53858 : SolarMutexGuard g;
96 :
97 : // register command for the specified key
98 53858 : m_lKey2Commands[aKey] = sCommand;
99 :
100 : // update optimized structure to bind multiple keys to one command
101 53858 : TKeyList& rKeyList = m_lCommand2Keys[sCommand];
102 53858 : rKeyList.push_back(aKey);
103 53858 : }
104 :
105 41259 : AcceleratorCache::TKeyList AcceleratorCache::getKeysByCommand(const OUString& sCommand) const
106 : {
107 41259 : SolarMutexGuard g;
108 41259 : TCommand2Keys::const_iterator pCommand = m_lCommand2Keys.find(sCommand);
109 41259 : if (pCommand == m_lCommand2Keys.end())
110 0 : throw css::container::NoSuchElementException();
111 41259 : 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 : } // namespace framework
162 :
163 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|