Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / sidebar / EnumContext.cxx
blob9f8a63077f180469c1b670f1aafc6f2a57ff4b1c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <sfx2/sidebar/EnumContext.hxx>
21 #include <osl/diagnose.h>
23 #include <map>
25 namespace sfx2 { namespace sidebar {
27 namespace {
29 typedef ::std::map<rtl::OUString,EnumContext::Application> ApplicationMap;
30 typedef ::std::vector<rtl::OUString> ApplicationVector;
31 static ApplicationMap maApplicationMap;
32 static ApplicationVector maApplicationVector;
34 typedef ::std::map<rtl::OUString,EnumContext::Context> ContextMap;
35 typedef ::std::vector<rtl::OUString> ContextVector;
36 static ContextMap maContextMap;
37 static ContextVector maContextVector;
41 const sal_Int32 EnumContext::NoMatch = 4;
42 const sal_Int32 EnumContext::OptimalMatch = 0; // Neither application nor context name is "any".
44 EnumContext::EnumContext()
45 : meApplication(Application_None),
46 meContext(Context_Unknown)
50 EnumContext::EnumContext (
51 const Application eApplication,
52 const Context eContext)
53 : meApplication(eApplication),
54 meContext(eContext)
58 EnumContext::EnumContext (
59 const ::rtl::OUString& rsApplicationName,
60 const ::rtl::OUString& rsContextName)
61 : meApplication(GetApplicationEnum(rsApplicationName)),
62 meContext(GetContextEnum(rsContextName))
66 sal_Int32 EnumContext::GetCombinedContext_DI() const
68 return CombinedEnumContext(GetApplication_DI(), meContext);
71 EnumContext::Application EnumContext::GetApplication_DI() const
73 switch (meApplication)
75 case Application_Draw:
76 case Application_Impress:
77 return Application_DrawImpress;
79 case Application_Writer:
80 case Application_WriterGlobal:
81 case Application_WriterWeb:
82 case Application_WriterXML:
83 case Application_WriterForm:
84 case Application_WriterReport:
85 return Application_WriterVariants;
87 default:
88 return meApplication;
92 bool EnumContext::operator== (const EnumContext& rOther)
94 return meApplication==rOther.meApplication
95 && meContext==rOther.meContext;
98 bool EnumContext::operator!= (const EnumContext& rOther)
100 return meApplication!=rOther.meApplication
101 || meContext!=rOther.meContext;
104 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Application eApplication)
106 maApplicationMap[rsName] = eApplication;
107 OSL_ASSERT(eApplication<=__LastApplicationEnum);
108 if (maApplicationVector.size() <= size_t(eApplication))
109 maApplicationVector.resize(eApplication+1);
110 maApplicationVector[eApplication]=rsName;
113 void EnumContext::ProvideApplicationContainers()
115 if (maApplicationMap.empty())
117 maApplicationVector.resize(static_cast<size_t>(EnumContext::__LastApplicationEnum)+1);
118 AddEntry(OUString("com.sun.star.text.TextDocument"), EnumContext::Application_Writer);
119 AddEntry(OUString("com.sun.star.text.GlobalDocument"), EnumContext::Application_WriterGlobal);
120 AddEntry(OUString("com.sun.star.text.WebDocument"), EnumContext::Application_WriterWeb);
121 AddEntry(OUString("com.sun.star.xforms.XMLFormDocument"), EnumContext::Application_WriterXML);
122 AddEntry(OUString("com.sun.star.sdb.FormDesign"), EnumContext::Application_WriterForm);
123 AddEntry(OUString("com.sun.star.sdb.TextReportDesign"), EnumContext::Application_WriterReport);
124 AddEntry(OUString("com.sun.star.sheet.SpreadsheetDocument"), EnumContext::Application_Calc);
125 AddEntry(OUString("com.sun.star.drawing.DrawingDocument"), EnumContext::Application_Draw);
126 AddEntry(OUString("com.sun.star.presentation.PresentationDocument"), EnumContext::Application_Impress);
128 AddEntry(OUString("any"), EnumContext::Application_Any);
129 AddEntry(OUString("none"), EnumContext::Application_None);
133 EnumContext::Application EnumContext::GetApplicationEnum (const ::rtl::OUString& rsApplicationName)
135 ProvideApplicationContainers();
137 ApplicationMap::const_iterator iApplication(
138 maApplicationMap.find(rsApplicationName));
139 if (iApplication != maApplicationMap.end())
140 return iApplication->second;
141 else
142 return EnumContext::Application_None;
145 const ::rtl::OUString& EnumContext::GetApplicationName (const Application eApplication)
147 ProvideApplicationContainers();
149 const sal_Int32 nIndex (eApplication);
150 if (nIndex<0 || nIndex>= __LastApplicationEnum)
151 return maApplicationVector[Application_None];
152 else
153 return maApplicationVector[nIndex];
156 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Context eApplication)
158 maContextMap[rsName] = eApplication;
159 OSL_ASSERT(eApplication<=__LastContextEnum);
160 if (maContextVector.size() <= size_t(eApplication))
161 maContextVector.resize(eApplication+1);
162 maContextVector[eApplication] = rsName;
165 void EnumContext::ProvideContextContainers()
167 if (maContextMap.empty())
169 maContextVector.resize(static_cast<size_t>(__LastContextEnum)+1);
170 AddEntry(OUString("any"), Context_Any);
171 AddEntry(OUString("default"), Context_Default);
172 AddEntry(OUString("empty"), Context_Empty);
173 #define AddContext(context) AddEntry(OUString(#context), Context_##context);
174 AddContext(3DObject);
175 AddContext(Annotation);
176 AddContext(Auditing);
177 AddContext(Cell);
178 AddContext(Chart);
179 AddContext(Chart);
180 AddContext(Draw);
181 AddContext(DrawPage);
182 AddContext(DrawText);
183 AddContext(EditCell);
184 AddContext(Form);
185 AddContext(Frame);
186 AddContext(Graphic);
187 AddContext(HandoutPage);
188 AddContext(MasterPage);
189 AddContext(Media);
190 AddContext(MultiObject);
191 AddContext(NotesPage);
192 AddContext(OLE);
193 AddContext(OutlineText);
194 AddContext(Pivot);
195 AddContext(SlidesorterPage);
196 AddContext(Table);
197 AddContext(Text);
198 AddContext(TextObject);
199 #undef AddContext
203 EnumContext::Context EnumContext::GetContextEnum (const ::rtl::OUString& rsContextName)
205 ProvideContextContainers();
207 ContextMap::const_iterator iContext(
208 maContextMap.find(rsContextName));
209 if (iContext != maContextMap.end())
210 return iContext->second;
211 else
212 return EnumContext::Context_Unknown;
215 const ::rtl::OUString& EnumContext::GetContextName (const Context eContext)
217 ProvideContextContainers();
219 const sal_Int32 nIndex (eContext);
220 if (nIndex<0 || nIndex>= __LastContextEnum)
221 return maContextVector[Context_Unknown];
222 else
223 return maContextVector[nIndex];
226 } } // end of namespace sfx2::sidebar
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */