1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef __FRAMEWORK_ACCELERATORS_ACCELERATORCACHE_HXX_
25 #define __FRAMEWORK_ACCELERATORS_ACCELERATORCACHE_HXX_
26 
27 //__________________________________________
28 // own includes
29 
30 #include <threadhelp/threadhelpbase.hxx>
31 #include <general.h>
32 #include <stdtypes.h>
33 
34 //__________________________________________
35 // interface includes
36 
37 #ifndef __COM_SUN_STAR_AWT_KEYEVENT_HPP_
38 #include <com/sun/star/awt/KeyEvent.hpp>
39 #endif
40 
41 //__________________________________________
42 // other includes
43 #include <comphelper/sequenceasvector.hxx>
44 
45 //__________________________________________
46 // definition
47 
48 namespace framework
49 {
50 
51 //__________________________________________
52 /**
53     @short  implements a cache for any accelerator configuration.
54 
55     @descr  Its implemented threadsafe, supports copy-on-write pattern
56             and a flush mechansim to support concurrent access to the same
57             configuration.
58 
59             copy-on-write ... How? Do the following:
60  */
61 class AcceleratorCache : public ThreadHelpBase // attention! Must be the first base class to guarantee right initialize lock ...
62 {
63     //______________________________________
64     // const, types
65 
66     public:
67 
68         //---------------------------------------
69         /** TODO document me
70             commands -> keys
71         */
72         typedef ::comphelper::SequenceAsVector< css::awt::KeyEvent > TKeyList;
73         typedef BaseHash< TKeyList > TCommand2Keys;
74 
75         //---------------------------------------
76         /** TODO document me
77             keys -> commands
78         */
79         typedef ::std::hash_map< css::awt::KeyEvent ,
80                                  ::rtl::OUString    ,
81                                  KeyEventHashCode   ,
82                                  KeyEventEqualsFunc > TKey2Commands;
83 
84     //______________________________________
85     // member
86 
87     private:
88 
89         //---------------------------------------
90         /** map commands to keys in relation 1:n.
91             First key is interpreted as preferred one! */
92         TCommand2Keys m_lCommand2Keys;
93 
94         //---------------------------------------
95         /** map keys to commands in relation 1:1. */
96         TKey2Commands m_lKey2Commands;
97 
98     //______________________________________
99     // interface
100 
101     public:
102 
103         //---------------------------------------
104         /** @short  creates a new - but empty - cache instance. */
105         AcceleratorCache();
106 
107         //---------------------------------------
108         /** @short  make a copy of this cache.
109             @descr  Used for the copy-on-write feature.
110         */
111         AcceleratorCache(const AcceleratorCache& rCopy);
112 
113         //---------------------------------------
114         /** @short  does nothing real. */
115         virtual ~AcceleratorCache();
116 
117         //---------------------------------------
118         /** @short  write changes back to the original container.
119 
120             @param  rCopy
121                     the (changed!) copy, which should be written
122                     back to this original container.
123           */
124         virtual void takeOver(const AcceleratorCache& rCopy);
125 
126         //---------------------------------------
127         /** TODO document me */
128         virtual AcceleratorCache& operator=(const AcceleratorCache& rCopy);
129 
130         //---------------------------------------
131         /** @short  checks if the specified key exists.
132 
133             @param  aKey
134                     the key, which should be checked.
135 
136             @return [bool]
137                     sal_True if the speicfied key exists inside this container.
138          */
139         virtual sal_Bool hasKey(const css::awt::KeyEvent& aKey) const;
140         virtual sal_Bool hasCommand(const ::rtl::OUString& sCommand) const;
141 
142         //---------------------------------------
143         /** TODO document me */
144         virtual TKeyList getAllKeys() const;
145 
146         //---------------------------------------
147         /** @short  add a new or change an existing key-command pair
148                     of this container.
149 
150             @param  aKey
151                     describe the key.
152 
153             @param  sCommand
154                     describe the command.
155           */
156         virtual void setKeyCommandPair(const css::awt::KeyEvent& aKey    ,
157                                        const ::rtl::OUString&    sCommand);
158 
159         //---------------------------------------
160         /** @short  returns the list of keys, which are registered
161                     for this command.
162 
163             @param  sCommand
164                     describe the command.
165 
166             @return [TKeyList]
167                     the list of registered keys. Can be empty!
168           */
169         virtual TKeyList getKeysByCommand(const ::rtl::OUString& sCommand) const;
170 
171         //---------------------------------------
172         /** TODO */
173         virtual ::rtl::OUString getCommandByKey(const css::awt::KeyEvent& aKey) const;
174 
175         //---------------------------------------
176         /** TODO */
177         virtual void removeKey(const css::awt::KeyEvent& aKey);
178         virtual void removeCommand(const ::rtl::OUString& sCommand);
179 };
180 
181 } // namespace framework
182 
183 #endif // __FRAMEWORK_ACCELERATORS_ACCELERATORCACHE_HXX_
184