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 #include "precompiled_sfx2.hxx"
23 
24 #include "sidebar/EnumContext.hxx"
25 
26 namespace sfx2 { namespace sidebar {
27 
28 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
29 
30 namespace {
31 
32 typedef ::std::map<rtl::OUString,EnumContext::Application> ApplicationMap;
33 typedef ::std::vector<rtl::OUString> ApplicationVector;
34 static ApplicationMap maApplicationMap;
35 static ApplicationVector maApplicationVector;
36 
37 typedef ::std::map<rtl::OUString,EnumContext::Context> ContextMap;
38 typedef ::std::vector<rtl::OUString> ContextVector;
39 static ContextMap maContextMap;
40 static ContextVector maContextVector;
41 
42 }
43 
44 
45 
46 EnumContext::EnumContext (void)
47     : meApplication(Application_Other),
48       meContext(Context_Default)
49 {
50 }
51 
52 
53 
54 
55 EnumContext::EnumContext (
56     const Application eApplication,
57     const Context eContext)
58     : meApplication(eApplication),
59       meContext(eContext)
60 {
61 }
62 
63 
64 
65 
66 sal_Int32 EnumContext::GetCombinedContext(void) const
67 {
68     return CombinedEnumContext(meApplication, meContext);
69 }
70 
71 
72 
73 
74 bool EnumContext::operator== (const EnumContext aOther)
75 {
76     return meApplication==aOther.meApplication
77         && meContext==aOther.meContext;
78 }
79 
80 
81 
82 
83 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Application eApplication)
84 {
85     maApplicationMap[rsName] = eApplication;
86     OSL_ASSERT(eApplication<=__LastApplicationEnum);
87     if (maApplicationVector.size() <= eApplication)
88         maApplicationVector.resize(eApplication+1);
89     maApplicationVector[eApplication]=rsName;
90 }
91 
92 
93 
94 
95 void EnumContext::ProvideApplicationContainers (void)
96 {
97     if (maApplicationMap.empty())
98     {
99         maApplicationVector.resize(static_cast<size_t>(EnumContext::__LastApplicationEnum)+1);
100         AddEntry(A2S("com.sun.star.text.TextDocument"), EnumContext::Application_Writer);
101         AddEntry(A2S("com.sun.star ?calc?"), EnumContext::Application_Calc);
102         AddEntry(A2S("com.sun.star ?draw?"), EnumContext::Application_Draw);
103         AddEntry(A2S("com.sun.star.presentation.PresentationDocument"), EnumContext::Application_Impress);
104         AddEntry(A2S("other"), EnumContext::Application_Other);
105     }
106 }
107 
108 
109 
110 
111 EnumContext::Application EnumContext::GetApplicationEnum (const ::rtl::OUString& rsApplicationName)
112 {
113     ProvideApplicationContainers();
114 
115     ApplicationMap::const_iterator iApplication(
116         maApplicationMap.find(rsApplicationName));
117     if (iApplication != maApplicationMap.end())
118         return iApplication->second;
119     else
120         return EnumContext::Application_Other;
121 }
122 
123 
124 
125 
126 const ::rtl::OUString& EnumContext::GetApplicationName (const Application eApplication)
127 {
128     ProvideApplicationContainers();
129 
130     const sal_Int32 nIndex (eApplication);
131     if (nIndex<0 || nIndex>= __LastApplicationEnum)
132         return maApplicationVector[Application_Other];
133     else
134         return maApplicationVector[nIndex];
135 }
136 
137 
138 
139 
140 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Context eApplication)
141 {
142     maContextMap[rsName] = eApplication;
143     OSL_ASSERT(eApplication<=__LastContextEnum);
144     if (maContextVector.size() <= eApplication)
145         maContextVector.resize(eApplication+1);
146     maContextVector[eApplication]=rsName;
147 }
148 
149 
150 
151 
152 void EnumContext::ProvideContextContainers (void)
153 {
154     if (maContextMap.empty())
155     {
156         maContextVector.resize(static_cast<size_t>(__LastContextEnum)+1);
157         AddEntry(A2S("text-edit"), Context_Text);
158         AddEntry(A2S("text"), Context_Text);
159         AddEntry(A2S("default"), Context_Default);
160     }
161 }
162 
163 
164 
165 
166 EnumContext::Context EnumContext::GetContextEnum (const ::rtl::OUString& rsContextName)
167 {
168     ProvideContextContainers();
169 
170     ContextMap::const_iterator iContext(
171         maContextMap.find(rsContextName));
172     if (iContext != maContextMap.end())
173         return iContext->second;
174     else
175         return EnumContext::Context_Default;
176 }
177 
178 
179 
180 
181 const ::rtl::OUString& EnumContext::GetContextName (const Context eContext)
182 {
183     ProvideContextContainers();
184 
185     const sal_Int32 nIndex (eContext);
186     if (nIndex<0 || nIndex>= __LastContextEnum)
187         return maContextVector[Context_Default];
188     else
189         return maContextVector[nIndex];
190 }
191 
192 
193 
194 
195 } } // end of namespace sfx2::sidebar
196