1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: smartid.cxx,v $
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_vcl.hxx"
33 #include <vcl/smartid.hxx>
35 struct ImplSmartIdData
44 ImplSmartIdData
* SmartId::GetSmartIdData()
48 mpData
= new ImplSmartIdData
;
51 mpData
->bHasStringId
= FALSE
;
52 mpData
->bHasNumericId
= FALSE
;
58 SmartId::SmartId( const String
& rId
)
61 GetSmartIdData()->aUId
= rId
;
62 GetSmartIdData()->bHasStringId
= TRUE
;
65 SmartId::SmartId( ULONG nId
)
68 GetSmartIdData()->nUId
= nId
;
69 GetSmartIdData()->bHasNumericId
= TRUE
;
72 SmartId::SmartId( const String
& rId
, ULONG nId
)
75 GetSmartIdData()->aUId
= rId
;
76 GetSmartIdData()->bHasStringId
= TRUE
;
77 GetSmartIdData()->nUId
= nId
;
78 GetSmartIdData()->bHasNumericId
= TRUE
;
85 SmartId::SmartId( const SmartId
& rId
)
91 mpData
->aUId
= rId
.mpData
->aUId
;
92 mpData
->bHasStringId
= rId
.mpData
->bHasStringId
;
93 mpData
->nUId
= rId
.mpData
->nUId
;
94 mpData
->bHasNumericId
= rId
.mpData
->bHasNumericId
;
98 SmartId
& SmartId::operator = ( const SmartId
& rId
)
107 if ( mpData
&& rId
.mpData
)
109 mpData
->aUId
= rId
.mpData
->aUId
;
110 mpData
->bHasStringId
= rId
.mpData
->bHasStringId
;
111 mpData
->nUId
= rId
.mpData
->nUId
;
112 mpData
->bHasNumericId
= rId
.mpData
->bHasNumericId
;
123 mpData
= (ImplSmartIdData
*)0xDeadBeef;
127 void SmartId::UpdateId( const SmartId
& rId
, SmartIdUpdateMode aMode
)
129 // Check if ImplData is needed
130 if ( aMode
!= SMART_SET_SMART
|| ( rId
.HasString() || rId
.HasNumeric() ) )
133 if ( aMode
== SMART_SET_STR
|| aMode
== SMART_SET_ALL
|| ( aMode
== SMART_SET_SMART
&& rId
.HasString() ) )
135 GetSmartIdData()->aUId
= rId
.GetStr();
136 GetSmartIdData()->bHasStringId
= rId
.HasString();
138 if ( aMode
== SMART_SET_NUM
|| aMode
== SMART_SET_ALL
|| ( aMode
== SMART_SET_SMART
&& rId
.HasNumeric() ) )
140 GetSmartIdData()->nUId
= rId
.GetNum();
141 GetSmartIdData()->bHasNumericId
= rId
.HasNumeric();
144 // remove ImplData when no IDs are set. This is Important because Implementation of Equals() Matches and HasAny relies on it
145 if ( mpData
&& !mpData
->bHasStringId
&& !mpData
->bHasNumericId
)
152 BOOL
SmartId::HasNumeric() const
157 return mpData
->bHasNumericId
;
160 BOOL
SmartId::HasString() const
165 return mpData
->bHasStringId
;
168 BOOL
SmartId::HasAny() const
170 return mpData
!= NULL
;
173 ULONG
SmartId::GetNum() const
181 String
SmartId::GetStr() const
190 String
SmartId::GetText() const // return String for UI usage
194 aRes
= String::CreateFromInt64( GetNum() );
198 aRes
.AppendAscii( "/" );
199 aRes
.Append( GetStr() );
204 BOOL
SmartId::Matches( const String
&rId
)const
207 return GetStr().EqualsIgnoreCaseAscii( rId
);
212 BOOL
SmartId::Matches( const ULONG nId
) const
215 return GetNum() == nId
;
220 /******************************************************************************
221 If Both Ids have nither Strings nor Numbers they don't match
222 If both Ids have Strings the result of Matching these is returned.
223 Numbers are then Ignored.
224 Else Matching Numbers is attempted.
225 ******************************************************************************/
226 BOOL
SmartId::Matches( const SmartId
&rId
) const
228 if ( !mpData
|| !rId
.mpData
)
230 else if ( HasString() && rId
.HasString() )
231 return Matches( rId
.GetStr() );
233 return rId
.HasNumeric() && Matches( rId
.GetNum() );
236 BOOL
SmartId::Equals( const SmartId
&rId
) const
238 if ( mpData
&& rId
.mpData
)
239 return mpData
->aUId
.EqualsIgnoreCaseAscii( rId
.mpData
->aUId
)
240 && mpData
->bHasStringId
== rId
.mpData
->bHasStringId
241 && mpData
->nUId
== rId
.mpData
->nUId
242 && mpData
->bHasNumericId
== rId
.mpData
->bHasNumericId
;
243 else if ( !mpData
&& !rId
.mpData
)
249 BOOL
SmartId::operator == ( const SmartId
& rRight
) const
251 return Equals( rRight
);
254 BOOL
SmartId::operator < ( const SmartId
& rRight
) const
256 if ( HasString() && rRight
.HasString() && GetStr() != rRight
.GetStr() )
257 return GetStr() < rRight
.GetStr();
258 else if ( HasNumeric() && rRight
.HasNumeric() && GetNum() != rRight
.GetNum() )
259 return GetNum() < rRight
.GetNum();
261 { // Sort Strings to Front
263 return rRight
.HasString() && rRight
.HasNumeric();
265 return rRight
.HasString() || (!HasNumeric() && rRight
.HasNumeric());