xref: /aoo42x/main/sc/source/core/tool/userlist.cxx (revision b3f79822)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 //------------------------------------------------------------------------
30 
31 #include <unotools/charclass.hxx>
32 #include <string.h>
33 
34 #include "global.hxx"
35 #include "userlist.hxx"
36 #include <unotools/localedatawrapper.hxx>
37 #include <unotools/calendarwrapper.hxx>
38 #include <unotools/transliterationwrapper.hxx>
39 
40 // STATIC DATA -----------------------------------------------------------
41 
42 
43 //------------------------------------------------------------------------
44 
InitTokens()45 void ScUserListData::InitTokens()
46 {
47 	sal_Unicode cSep = ScGlobal::cListDelimiter;
48 	nTokenCount = (sal_uInt16) aStr.GetTokenCount(cSep);
49 	if (nTokenCount)
50 	{
51 		pSubStrings = new String[nTokenCount];
52 		pUpperSub   = new String[nTokenCount];
53 		for (sal_uInt16 i=0; i<nTokenCount; i++)
54 		{
55 			pUpperSub[i] = pSubStrings[i] = aStr.GetToken((xub_StrLen)i,cSep);
56 			ScGlobal::pCharClass->toUpper(pUpperSub[i]);
57 		}
58 	}
59 	else
60 		pSubStrings = pUpperSub = NULL;
61 }
62 
ScUserListData(const String & rStr)63 ScUserListData::ScUserListData(const String& rStr) :
64 	aStr(rStr)
65 {
66 	InitTokens();
67 }
68 
ScUserListData(const ScUserListData & rData)69 ScUserListData::ScUserListData(const ScUserListData& rData) :
70     ScDataObject(),
71 	aStr(rData.aStr)
72 {
73 	InitTokens();
74 }
75 
~ScUserListData()76 __EXPORT ScUserListData::~ScUserListData()
77 {
78 	delete[] pSubStrings;
79 	delete[] pUpperSub;
80 }
81 
SetString(const String & rStr)82 void ScUserListData::SetString( const String& rStr )
83 {
84 	delete[] pSubStrings;
85 	delete[] pUpperSub;
86 
87 	aStr = rStr;
88 	InitTokens();
89 }
90 
GetSubCount() const91 sal_uInt16 ScUserListData::GetSubCount() const
92 {
93 	return nTokenCount;
94 }
95 
GetSubIndex(const String & rSubStr,sal_uInt16 & rIndex) const96 sal_Bool ScUserListData::GetSubIndex(const String& rSubStr, sal_uInt16& rIndex) const
97 {
98 	sal_uInt16 i;
99 	for (i=0; i<nTokenCount; i++)
100 		if (rSubStr == pSubStrings[i])
101 		{
102 			rIndex = i;
103 			return sal_True;
104 		}
105 
106 	String aUpStr = rSubStr;
107 	ScGlobal::pCharClass->toUpper(aUpStr);
108 	for (i=0; i<nTokenCount; i++)
109 		if (aUpStr == pUpperSub[i])
110 		{
111 			rIndex = i;
112 			return sal_True;
113 		}
114 
115 	return sal_False;
116 }
117 
GetSubStr(sal_uInt16 nIndex) const118 String ScUserListData::GetSubStr(sal_uInt16 nIndex) const
119 {
120 	if (nIndex < nTokenCount)
121 		return pSubStrings[nIndex];
122 	else
123 		return EMPTY_STRING;
124 }
125 
Compare(const String & rSubStr1,const String & rSubStr2) const126 StringCompare ScUserListData::Compare(const String& rSubStr1, const String& rSubStr2) const
127 {
128 	sal_uInt16 nIndex1;
129 	sal_uInt16 nIndex2;
130 	sal_Bool bFound1 = GetSubIndex(rSubStr1, nIndex1);
131 	sal_Bool bFound2 = GetSubIndex(rSubStr2, nIndex2);
132 	if (bFound1)
133 	{
134 		if (bFound2)
135 		{
136 			if (nIndex1 < nIndex2)
137 				return COMPARE_LESS;
138 			else if (nIndex1 > nIndex2)
139 				return COMPARE_GREATER;
140 			else
141 				return COMPARE_EQUAL;
142 		}
143 		else
144 			return COMPARE_LESS;
145 	}
146 	else if (bFound2)
147 		return COMPARE_GREATER;
148 	else
149         return (StringCompare) ScGlobal::GetCaseTransliteration()->compareString( rSubStr1, rSubStr2 );
150 }
151 
ICompare(const String & rSubStr1,const String & rSubStr2) const152 StringCompare ScUserListData::ICompare(const String& rSubStr1, const String& rSubStr2) const
153 {
154 	sal_uInt16 nIndex1;
155 	sal_uInt16 nIndex2;
156 	sal_Bool bFound1 = GetSubIndex(rSubStr1, nIndex1);
157 	sal_Bool bFound2 = GetSubIndex(rSubStr2, nIndex2);
158 	if (bFound1)
159 	{
160 		if (bFound2)
161 		{
162 			if (nIndex1 < nIndex2)
163 				return COMPARE_LESS;
164 			else if (nIndex1 > nIndex2)
165 				return COMPARE_GREATER;
166 			else
167 				return COMPARE_EQUAL;
168 		}
169 		else
170 			return COMPARE_LESS;
171 	}
172 	else if (bFound2)
173 		return COMPARE_GREATER;
174 	else
175         return (StringCompare) ScGlobal::GetpTransliteration()->compareString( rSubStr1, rSubStr2 );
176 }
177 
ScUserList(sal_uInt16 nLim,sal_uInt16 nDel)178 ScUserList::ScUserList(sal_uInt16 nLim, sal_uInt16 nDel) :
179 	ScCollection	( nLim, nDel )
180 {
181     using namespace ::com::sun::star;
182 
183 	sal_Unicode cDelimiter = ScGlobal::cListDelimiter;
184 	uno::Sequence< i18n::CalendarItem > xCal;
185 
186     uno::Sequence< i18n::Calendar > xCalendars(
187             ScGlobal::pLocaleData->getAllCalendars() );
188 
189     for ( sal_Int32 j = 0; j < xCalendars.getLength(); ++j )
190     {
191         xCal = xCalendars[j].Days;
192         if ( xCal.getLength() )
193         {
194             String sDayShort, sDayLong;
195             sal_Int32 i;
196             sal_Int32 nLen = xCal.getLength();
197             rtl::OUString sStart = xCalendars[j].StartOfWeek;
198             sal_Int16 nStart = sal::static_int_cast<sal_Int16>(nLen);
199             while (nStart > 0)
200             {
201                 if (xCal[--nStart].ID == sStart)
202                     break;
203             }
204             sal_Int16 nLast = sal::static_int_cast<sal_Int16>( (nStart + nLen - 1) % nLen );
205             for (i = nStart; i != nLast; i = (i+1) % nLen)
206             {
207                 sDayShort += String( xCal[i].AbbrevName );
208                 sDayShort += cDelimiter;
209                 sDayLong  += String( xCal[i].FullName );
210                 sDayLong  += cDelimiter;
211             }
212             sDayShort += String( xCal[i].AbbrevName );
213             sDayLong  += String( xCal[i].FullName );
214 
215             if ( !HasEntry( sDayShort ) )
216                 Insert( new ScUserListData( sDayShort ));
217             if ( !HasEntry( sDayLong ) )
218                 Insert( new ScUserListData( sDayLong ));
219         }
220 
221         xCal = xCalendars[j].Months;
222         if ( xCal.getLength() )
223         {
224             String sMonthShort, sMonthLong;
225             sal_Int32 i;
226             sal_Int32 nLen = xCal.getLength() - 1;
227             for (i = 0; i < nLen; i++)
228             {
229                 sMonthShort += String( xCal[i].AbbrevName );
230                 sMonthShort += cDelimiter;
231                 sMonthLong  += String( xCal[i].FullName );
232                 sMonthLong  += cDelimiter;
233             }
234             sMonthShort += String( xCal[i].AbbrevName );
235             sMonthLong  += String( xCal[i].FullName );
236 
237             if ( !HasEntry( sMonthShort ) )
238                 Insert( new ScUserListData( sMonthShort ));
239             if ( !HasEntry( sMonthLong ) )
240                 Insert( new ScUserListData( sMonthLong ));
241         }
242     }
243 }
244 
Clone() const245 ScDataObject* ScUserList::Clone() const
246 {
247 	return ( new ScUserList( *this ) );
248 }
249 
GetData(const String & rSubStr) const250 ScUserListData* ScUserList::GetData(const String& rSubStr) const
251 {
252 	sal_uInt16	nIndex;
253 	sal_uInt16	i = 0;
254 	for (i=0; i < nCount; i++)
255 		if (((ScUserListData*)pItems[i])->GetSubIndex(rSubStr, nIndex))
256 			return (ScUserListData*)pItems[i];
257 	return NULL;
258 }
259 
operator ==(const ScUserList & r) const260 sal_Bool ScUserList::operator==( const ScUserList& r ) const
261 {
262 	sal_Bool bEqual = (nCount == r.nCount);
263 
264 	if ( bEqual )
265 	{
266 		ScUserListData* pMyData    = NULL;
267 		ScUserListData* pOtherData = NULL;
268 
269 		for ( sal_uInt16 i=0; i<nCount && bEqual; i++)
270 		{
271 			pMyData    = (ScUserListData*)At(i);
272 			pOtherData = (ScUserListData*)r.At(i);
273 
274 			bEqual =(   (pMyData->nTokenCount == pOtherData->nTokenCount)
275 					 && (pMyData->aStr		  == pOtherData->aStr) );
276 		}
277 	}
278 
279 	return bEqual;
280 }
281 
282 
HasEntry(const String & rStr) const283 sal_Bool ScUserList::HasEntry( const String& rStr ) const
284 {
285     for ( sal_uInt16 i=0; i<nCount; i++)
286     {
287         const ScUserListData* pMyData = (ScUserListData*) At(i);
288         if ( pMyData->aStr == rStr )
289             return sal_True;
290     }
291     return sal_False;
292 }
293 
294