xref: /aoo41x/main/svl/source/config/srchcfg.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svl.hxx"
30 
31 #include <svl/srchcfg.hxx>
32 #include <svl/svarray.hxx>
33 #include <com/sun/star/uno/Any.hxx>
34 #include <com/sun/star/uno/Sequence.hxx>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <tools/debug.hxx>
37 #include <unotools/configpathes.hxx>
38 
39 //-----------------------------------------------------------------------------
40 using namespace utl;
41 using namespace rtl;
42 using namespace com::sun::star;
43 using namespace com::sun::star::uno;
44 using namespace com::sun::star::beans;
45 
46 #define C2U(cChar) OUString::createFromAscii(cChar)
47 
48 //-----------------------------------------------------------------------------
49 typedef SvxSearchEngineData* SvxSearchEngineDataPtr;
50 SV_DECL_PTRARR_DEL(SvxSearchEngineArr, SvxSearchEngineDataPtr, 2, 2)
51 SV_IMPL_PTRARR(SvxSearchEngineArr, SvxSearchEngineDataPtr);
52 //-----------------------------------------------------------------------------
53 struct SvxSearchConfig_Impl
54 {
55     SvxSearchEngineArr   aEngineArr;
56 };
57 /* -----------------------------19.03.01 14:00--------------------------------
58 
59  ---------------------------------------------------------------------------*/
60 sal_Bool SvxSearchEngineData::operator==(const SvxSearchEngineData& rData)
61 {
62     return sEngineName      == rData.sEngineName     &&
63             sAndPrefix       == rData.sAndPrefix      &&
64             sAndSuffix       == rData.sAndSuffix      &&
65             sAndSeparator    == rData.sAndSeparator   &&
66             nAndCaseMatch    == rData.nAndCaseMatch   &&
67             sOrPrefix        == rData.sOrPrefix       &&
68             sOrSuffix        == rData.sOrSuffix       &&
69             sOrSeparator     == rData.sOrSeparator    &&
70             nOrCaseMatch     == rData.nOrCaseMatch    &&
71             sExactPrefix     == rData.sExactPrefix    &&
72             sExactSuffix     == rData.sExactSuffix    &&
73             sExactSeparator  == rData.sExactSeparator &&
74             nExactCaseMatch  == rData.nExactCaseMatch;
75 }
76 /* -----------------------------16.01.01 15:36--------------------------------
77 
78  ---------------------------------------------------------------------------*/
79 const Sequence<OUString>& lcl_GetSearchPropertyNames_Impl()
80 {
81     static Sequence<OUString> aNames;
82     if(!aNames.getLength())
83     {
84         aNames.realloc(12);
85         OUString* pNames = aNames.getArray();
86         pNames[0] = C2U("And/ooInetPrefix");
87         pNames[1] = C2U("And/ooInetSuffix");
88         pNames[2] = C2U("And/ooInetSeparator");
89         pNames[3] = C2U("And/ooInetCaseMatch");
90         pNames[4] = C2U("Or/ooInetPrefix");
91         pNames[5] = C2U("Or/ooInetSuffix");
92         pNames[6] = C2U("Or/ooInetSeparator");
93         pNames[7] = C2U("Or/ooInetCaseMatch");
94         pNames[8] = C2U("Exact/ooInetPrefix");
95         pNames[9] = C2U("Exact/ooInetSuffix");
96         pNames[10] = C2U("Exact/ooInetSeparator");
97         pNames[11] = C2U("Exact/ooInetCaseMatch");
98     }
99     return aNames;
100 }
101 // ---------------------------------------------------------------------------
102 SvxSearchConfig::SvxSearchConfig(sal_Bool bEnableNotify) :
103     utl::ConfigItem(C2U("Inet/SearchEngines"), CONFIG_MODE_DELAYED_UPDATE),
104     pImpl(new SvxSearchConfig_Impl)
105 {
106 	if(bEnableNotify)
107     {
108         //request notifications from the node
109         Sequence<OUString> aEnable(1);
110         EnableNotification(aEnable);
111     }
112 	Load();
113 }
114 /* -----------------------------16.01.01 15:36--------------------------------
115 
116  ---------------------------------------------------------------------------*/
117 SvxSearchConfig::~SvxSearchConfig()
118 {
119 	delete pImpl;
120 }
121 /* -----------------------------17.01.01 09:57--------------------------------
122 
123  ---------------------------------------------------------------------------*/
124 void SvxSearchConfig::Load()
125 {
126     pImpl->aEngineArr.DeleteAndDestroy(0, pImpl->aEngineArr.Count());
127     Sequence<OUString> aNodeNames = GetNodeNames(OUString());
128     const OUString* pNodeNames = aNodeNames.getConstArray();
129     for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength(); nNode++)
130     {
131         SvxSearchEngineDataPtr pNew = new SvxSearchEngineData;
132         pNew->sEngineName = pNodeNames[nNode];
133         const Sequence<OUString>& rPropNames = lcl_GetSearchPropertyNames_Impl();
134         const OUString* pPropNames = rPropNames.getConstArray();
135         Sequence<OUString> aPropertyNames(rPropNames.getLength());
136         OUString* pPropertyNames = aPropertyNames.getArray();
137         const OUString sSlash(C2U("/"));
138         sal_Int32 nProp;
139         for(nProp = 0; nProp < rPropNames.getLength(); nProp++)
140         {
141             pPropertyNames[nProp] = wrapConfigurationElementName(pNodeNames[nNode]);
142             pPropertyNames[nProp] += sSlash;
143             pPropertyNames[nProp] += pPropNames[nProp];
144         }
145         Sequence<Any> aValues = GetProperties(aPropertyNames);
146         const Any* pValues = aValues.getConstArray();
147         for(nProp = 0; nProp < rPropNames.getLength(); nProp++)
148         {
149             switch(nProp)
150             {
151                 case 0 : pValues[nProp] >>= pNew->sAndPrefix;      break;
152                 case 1 : pValues[nProp] >>= pNew->sAndSuffix;      break;
153                 case 2 : pValues[nProp] >>= pNew->sAndSeparator;   break;
154                 case 3 : pValues[nProp] >>= pNew->nAndCaseMatch;   break;
155 
156                 case 4 : pValues[nProp] >>= pNew->sOrPrefix;       break;
157                 case 5 : pValues[nProp] >>= pNew->sOrSuffix;       break;
158                 case 6 : pValues[nProp] >>= pNew->sOrSeparator;    break;
159                 case 7 : pValues[nProp] >>= pNew->nOrCaseMatch;    break;
160 
161                 case 8 : pValues[nProp] >>= pNew->sExactPrefix;    break;
162                 case 9 : pValues[nProp] >>= pNew->sExactSuffix;    break;
163                 case 10: pValues[nProp] >>= pNew->sExactSeparator; break;
164                 case 11: pValues[nProp] >>= pNew->nExactCaseMatch; break;
165             }
166         }
167         pImpl->aEngineArr.Insert(pNew, pImpl->aEngineArr.Count());
168     }
169 }
170 /* -----------------------------17.01.01 09:57--------------------------------
171 
172  ---------------------------------------------------------------------------*/
173 void    SvxSearchConfig::Notify( const Sequence<OUString>& )
174 {
175 	Load();
176 }
177 /* -----------------------------16.01.01 15:36--------------------------------
178 
179  ---------------------------------------------------------------------------*/
180 void SvxSearchConfig::Commit()
181 {
182     OUString sNode;
183     if(!pImpl->aEngineArr.Count())
184         ClearNodeSet(sNode);
185     else
186     {
187         Sequence<PropertyValue> aSetValues(12 * pImpl->aEngineArr.Count());
188 		PropertyValue* pSetValues = aSetValues.getArray();
189 
190         const Sequence<OUString>& rPropNames = lcl_GetSearchPropertyNames_Impl();
191         const OUString* pPropNames = rPropNames.getConstArray();
192         const OUString sSlash(C2U("/"));
193         for(sal_uInt16 i = 0; i < pImpl->aEngineArr.Count(); i++)
194 		{
195             SvxSearchEngineDataPtr pSave = pImpl->aEngineArr[i];
196             for(sal_Int16 nProp = 0; nProp < rPropNames.getLength(); nProp++)
197             {
198                 OUString sTmpName = sSlash;
199                 sTmpName += wrapConfigurationElementName(pSave->sEngineName);
200                 sTmpName += sSlash;
201                 sTmpName += pPropNames[nProp];
202                 pSetValues[nProp].Name = sTmpName;
203                 switch(nProp)
204                 {
205                     case 0 : pSetValues[nProp].Value <<= pSave->sAndPrefix;      break;
206                     case 1 : pSetValues[nProp].Value <<= pSave->sAndSuffix;      break;
207                     case 2 : pSetValues[nProp].Value <<= pSave->sAndSeparator;   break;
208                     case 3 : pSetValues[nProp].Value <<= pSave->nAndCaseMatch;   break;
209 
210                     case 4 : pSetValues[nProp].Value <<= pSave->sOrPrefix;       break;
211                     case 5 : pSetValues[nProp].Value <<= pSave->sOrSuffix;       break;
212                     case 6 : pSetValues[nProp].Value <<= pSave->sOrSeparator;    break;
213                     case 7 : pSetValues[nProp].Value <<= pSave->nOrCaseMatch;    break;
214 
215                     case 8 : pSetValues[nProp].Value <<= pSave->sExactPrefix;    break;
216                     case 9 : pSetValues[nProp].Value <<= pSave->sExactSuffix;    break;
217                     case 10: pSetValues[nProp].Value <<= pSave->sExactSeparator; break;
218                     case 11: pSetValues[nProp].Value <<= pSave->nExactCaseMatch; break;
219                 }
220             }
221             pSetValues+= 12;
222         }
223 		ReplaceSetProperties(sNode, aSetValues);
224     }
225 }
226 /* -----------------------------19.03.01 10:02--------------------------------
227 
228  ---------------------------------------------------------------------------*/
229 sal_uInt16 SvxSearchConfig::Count()
230 {
231     return pImpl->aEngineArr.Count();
232 }
233 /* -----------------------------19.03.01 10:02--------------------------------
234 
235  ---------------------------------------------------------------------------*/
236 const SvxSearchEngineData&  SvxSearchConfig::GetData(sal_uInt16 nPos)
237 {
238     DBG_ASSERT(nPos < pImpl->aEngineArr.Count(), "wrong array index");
239     return *pImpl->aEngineArr[nPos];
240 }
241 /* -----------------------------19.03.01 10:38--------------------------------
242 
243  ---------------------------------------------------------------------------*/
244 const SvxSearchEngineData*  SvxSearchConfig::GetData(const rtl::OUString& rEngineName)
245 {
246     for(sal_uInt16 nPos = 0; nPos < pImpl->aEngineArr.Count(); nPos++)
247     {
248         if(pImpl->aEngineArr[nPos]->sEngineName == rEngineName)
249             return pImpl->aEngineArr[nPos];
250     }
251     return 0;
252 }
253 /* -----------------------------19.03.01 10:02--------------------------------
254 
255  ---------------------------------------------------------------------------*/
256 void  SvxSearchConfig::SetData(const SvxSearchEngineData& rData)
257 {
258     for(sal_uInt16 nPos = 0; nPos < pImpl->aEngineArr.Count(); nPos++)
259     {
260         if(pImpl->aEngineArr[nPos]->sEngineName == rData.sEngineName)
261         {
262             if((*pImpl->aEngineArr[nPos]) == rData)
263                 return;
264             pImpl->aEngineArr.DeleteAndDestroy(nPos, 1);
265             break;
266         }
267     }
268     SvxSearchEngineDataPtr pInsert = new SvxSearchEngineData(rData);
269     pImpl->aEngineArr.Insert(pInsert, pImpl->aEngineArr.Count());
270     SetModified();
271 }
272 /* -----------------------------19.03.01 10:38--------------------------------
273 
274  ---------------------------------------------------------------------------*/
275 void SvxSearchConfig::RemoveData(const rtl::OUString& rEngineName)
276 {
277     for(sal_uInt16 nPos = 0; nPos < pImpl->aEngineArr.Count(); nPos++)
278     {
279         if(pImpl->aEngineArr[nPos]->sEngineName == rEngineName)
280         {
281             pImpl->aEngineArr.DeleteAndDestroy(nPos, 1);
282             SetModified();
283             return ;
284         }
285     }
286 }
287 
288