merge the formfield patch from ooo-build
[ooovba.git] / sc / source / core / tool / userlist.cxx
blob3b81fafb0fb3dfd3b9c455cdcbd2314c42969a00
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: userlist.cxx,v $
10 * $Revision: 1.11.32.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 //------------------------------------------------------------------------
38 #include <unotools/charclass.hxx>
39 #include <string.h>
41 #include "global.hxx"
42 #include "userlist.hxx"
43 #include <unotools/localedatawrapper.hxx>
44 #include <unotools/calendarwrapper.hxx>
45 #include <unotools/transliterationwrapper.hxx>
47 // STATIC DATA -----------------------------------------------------------
50 //------------------------------------------------------------------------
52 void ScUserListData::InitTokens()
54 sal_Unicode cSep = ScGlobal::cListDelimiter;
55 nTokenCount = (USHORT) aStr.GetTokenCount(cSep);
56 if (nTokenCount)
58 pSubStrings = new String[nTokenCount];
59 pUpperSub = new String[nTokenCount];
60 for (USHORT i=0; i<nTokenCount; i++)
62 pUpperSub[i] = pSubStrings[i] = aStr.GetToken((xub_StrLen)i,cSep);
63 ScGlobal::pCharClass->toUpper(pUpperSub[i]);
66 else
67 pSubStrings = pUpperSub = NULL;
70 ScUserListData::ScUserListData(const String& rStr) :
71 aStr(rStr)
73 InitTokens();
76 ScUserListData::ScUserListData(const ScUserListData& rData) :
77 ScDataObject(),
78 aStr(rData.aStr)
80 InitTokens();
83 __EXPORT ScUserListData::~ScUserListData()
85 delete[] pSubStrings;
86 delete[] pUpperSub;
89 void ScUserListData::SetString( const String& rStr )
91 delete[] pSubStrings;
92 delete[] pUpperSub;
94 aStr = rStr;
95 InitTokens();
98 USHORT ScUserListData::GetSubCount() const
100 return nTokenCount;
103 BOOL ScUserListData::GetSubIndex(const String& rSubStr, USHORT& rIndex) const
105 USHORT i;
106 for (i=0; i<nTokenCount; i++)
107 if (rSubStr == pSubStrings[i])
109 rIndex = i;
110 return TRUE;
113 String aUpStr = rSubStr;
114 ScGlobal::pCharClass->toUpper(aUpStr);
115 for (i=0; i<nTokenCount; i++)
116 if (aUpStr == pUpperSub[i])
118 rIndex = i;
119 return TRUE;
122 return FALSE;
125 String ScUserListData::GetSubStr(USHORT nIndex) const
127 if (nIndex < nTokenCount)
128 return pSubStrings[nIndex];
129 else
130 return EMPTY_STRING;
133 StringCompare ScUserListData::Compare(const String& rSubStr1, const String& rSubStr2) const
135 USHORT nIndex1;
136 USHORT nIndex2;
137 BOOL bFound1 = GetSubIndex(rSubStr1, nIndex1);
138 BOOL bFound2 = GetSubIndex(rSubStr2, nIndex2);
139 if (bFound1)
141 if (bFound2)
143 if (nIndex1 < nIndex2)
144 return COMPARE_LESS;
145 else if (nIndex1 > nIndex2)
146 return COMPARE_GREATER;
147 else
148 return COMPARE_EQUAL;
150 else
151 return COMPARE_LESS;
153 else if (bFound2)
154 return COMPARE_GREATER;
155 else
156 return (StringCompare) ScGlobal::GetCaseTransliteration()->compareString( rSubStr1, rSubStr2 );
159 StringCompare ScUserListData::ICompare(const String& rSubStr1, const String& rSubStr2) const
161 USHORT nIndex1;
162 USHORT nIndex2;
163 BOOL bFound1 = GetSubIndex(rSubStr1, nIndex1);
164 BOOL bFound2 = GetSubIndex(rSubStr2, nIndex2);
165 if (bFound1)
167 if (bFound2)
169 if (nIndex1 < nIndex2)
170 return COMPARE_LESS;
171 else if (nIndex1 > nIndex2)
172 return COMPARE_GREATER;
173 else
174 return COMPARE_EQUAL;
176 else
177 return COMPARE_LESS;
179 else if (bFound2)
180 return COMPARE_GREATER;
181 else
182 return (StringCompare) ScGlobal::GetpTransliteration()->compareString( rSubStr1, rSubStr2 );
185 ScUserList::ScUserList(USHORT nLim, USHORT nDel) :
186 ScCollection ( nLim, nDel )
188 using namespace ::com::sun::star;
190 sal_Unicode cDelimiter = ScGlobal::cListDelimiter;
191 uno::Sequence< i18n::CalendarItem > xCal;
193 uno::Sequence< i18n::Calendar > xCalendars(
194 ScGlobal::pLocaleData->getAllCalendars() );
196 for ( sal_Int32 j = 0; j < xCalendars.getLength(); ++j )
198 xCal = xCalendars[j].Days;
199 if ( xCal.getLength() )
201 String sDayShort, sDayLong;
202 sal_Int32 i;
203 sal_Int32 nLen = xCal.getLength();
204 rtl::OUString sStart = xCalendars[j].StartOfWeek;
205 sal_Int16 nStart = sal::static_int_cast<sal_Int16>(nLen);
206 while (nStart > 0)
208 if (xCal[--nStart].ID == sStart)
209 break;
211 sal_Int16 nLast = sal::static_int_cast<sal_Int16>( (nStart + nLen - 1) % nLen );
212 for (i = nStart; i != nLast; i = (i+1) % nLen)
214 sDayShort += String( xCal[i].AbbrevName );
215 sDayShort += cDelimiter;
216 sDayLong += String( xCal[i].FullName );
217 sDayLong += cDelimiter;
219 sDayShort += String( xCal[i].AbbrevName );
220 sDayLong += String( xCal[i].FullName );
222 if ( !HasEntry( sDayShort ) )
223 Insert( new ScUserListData( sDayShort ));
224 if ( !HasEntry( sDayLong ) )
225 Insert( new ScUserListData( sDayLong ));
228 xCal = xCalendars[j].Months;
229 if ( xCal.getLength() )
231 String sMonthShort, sMonthLong;
232 sal_Int32 i;
233 sal_Int32 nLen = xCal.getLength() - 1;
234 for (i = 0; i < nLen; i++)
236 sMonthShort += String( xCal[i].AbbrevName );
237 sMonthShort += cDelimiter;
238 sMonthLong += String( xCal[i].FullName );
239 sMonthLong += cDelimiter;
241 sMonthShort += String( xCal[i].AbbrevName );
242 sMonthLong += String( xCal[i].FullName );
244 if ( !HasEntry( sMonthShort ) )
245 Insert( new ScUserListData( sMonthShort ));
246 if ( !HasEntry( sMonthLong ) )
247 Insert( new ScUserListData( sMonthLong ));
252 ScDataObject* ScUserList::Clone() const
254 return ( new ScUserList( *this ) );
257 ScUserListData* ScUserList::GetData(const String& rSubStr) const
259 USHORT nIndex;
260 USHORT i = 0;
261 for (i=0; i < nCount; i++)
262 if (((ScUserListData*)pItems[i])->GetSubIndex(rSubStr, nIndex))
263 return (ScUserListData*)pItems[i];
264 return NULL;
267 BOOL ScUserList::operator==( const ScUserList& r ) const
269 BOOL bEqual = (nCount == r.nCount);
271 if ( bEqual )
273 ScUserListData* pMyData = NULL;
274 ScUserListData* pOtherData = NULL;
276 for ( USHORT i=0; i<nCount && bEqual; i++)
278 pMyData = (ScUserListData*)At(i);
279 pOtherData = (ScUserListData*)r.At(i);
281 bEqual =( (pMyData->nTokenCount == pOtherData->nTokenCount)
282 && (pMyData->aStr == pOtherData->aStr) );
286 return bEqual;
290 BOOL ScUserList::HasEntry( const String& rStr ) const
292 for ( USHORT i=0; i<nCount; i++)
294 const ScUserListData* pMyData = (ScUserListData*) At(i);
295 if ( pMyData->aStr == rStr )
296 return TRUE;
298 return FALSE;