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 0 : AcceleratorCache::AcceleratorCache()
34 : {
35 0 : }
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 0 : AcceleratorCache::~AcceleratorCache()
44 : {
45 : // Dont save anything automaticly here.
46 : // The user has to do that explicitly!
47 0 : }
48 :
49 0 : void AcceleratorCache::takeOver(const AcceleratorCache& rCopy)
50 : {
51 0 : SolarMutexGuard g;
52 0 : m_lCommand2Keys = rCopy.m_lCommand2Keys;
53 0 : m_lKey2Commands = rCopy.m_lKey2Commands;
54 0 : }
55 :
56 0 : AcceleratorCache& AcceleratorCache::operator=(const AcceleratorCache& rCopy)
57 : {
58 0 : takeOver(rCopy);
59 0 : return *this;
60 : }
61 :
62 0 : bool AcceleratorCache::hasKey(const css::awt::KeyEvent& aKey) const
63 : {
64 0 : SolarMutexGuard g;
65 0 : return (m_lKey2Commands.find(aKey) != m_lKey2Commands.end());
66 : }
67 :
68 0 : bool AcceleratorCache::hasCommand(const OUString& sCommand) const
69 : {
70 0 : SolarMutexGuard g;
71 0 : 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 0 : void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent& aKey ,
93 : const OUString& sCommand)
94 : {
95 0 : SolarMutexGuard g;
96 :
97 : // register command for the specified key
98 0 : m_lKey2Commands[aKey] = sCommand;
99 :
100 : // update optimized structure to bind multiple keys to one command
101 0 : TKeyList& rKeyList = m_lCommand2Keys[sCommand];
102 0 : rKeyList.push_back(aKey);
103 0 : }
104 :
105 0 : AcceleratorCache::TKeyList AcceleratorCache::getKeysByCommand(const OUString& sCommand) const
106 : {
107 0 : SolarMutexGuard g;
108 0 : TCommand2Keys::const_iterator pCommand = m_lCommand2Keys.find(sCommand);
109 0 : if (pCommand == m_lCommand2Keys.end())
110 : throw css::container::NoSuchElementException(
111 0 : OUString(), css::uno::Reference< css::uno::XInterface >());
112 0 : return pCommand->second;
113 : }
114 :
115 0 : OUString AcceleratorCache::getCommandByKey(const css::awt::KeyEvent& aKey) const
116 : {
117 0 : SolarMutexGuard g;
118 0 : TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
119 0 : if (pKey == m_lKey2Commands.end())
120 : throw css::container::NoSuchElementException(
121 0 : OUString(), css::uno::Reference< css::uno::XInterface >());
122 0 : return pKey->second;
123 : }
124 :
125 0 : void AcceleratorCache::removeKey(const css::awt::KeyEvent& aKey)
126 : {
127 0 : SolarMutexGuard g;
128 :
129 : // check if key exists
130 0 : TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey);
131 0 : if (pKey == m_lKey2Commands.end())
132 0 : return;
133 :
134 : // get its registered command
135 : // Because we must know its place inside the optimized
136 : // structure, which bind keys to commands, too!
137 0 : OUString sCommand = pKey->second;
138 0 : pKey = m_lKey2Commands.end(); // nobody should use an undefined value .-)
139 :
140 : // remove key from primary list
141 0 : m_lKey2Commands.erase(aKey);
142 :
143 : // remove key from optimized command list
144 0 : m_lCommand2Keys.erase(sCommand);
145 : }
146 :
147 0 : void AcceleratorCache::removeCommand(const OUString& sCommand)
148 : {
149 0 : SolarMutexGuard g;
150 :
151 0 : const TKeyList& lKeys = getKeysByCommand(sCommand);
152 0 : AcceleratorCache::TKeyList::const_iterator pKey;
153 0 : for ( pKey = lKeys.begin();
154 0 : pKey != lKeys.end();
155 : ++pKey )
156 : {
157 0 : const css::awt::KeyEvent& rKey = *pKey;
158 0 : removeKey(rKey);
159 : }
160 0 : m_lCommand2Keys.erase(sCommand);
161 0 : }
162 :
163 : } // namespace framework
164 :
165 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|