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 --------------------------------------------------------------- 32 33 #define _DATETIMEITEM_CXX 34 #include <svl/dateitem.hxx> 35 #include <svl/svldata.hxx> 36 #include <svl/svl.hrc> 37 38 #include <unotools/intlwrapper.hxx> 39 #include <comphelper/processfactory.hxx> 40 41 #include <tools/stream.hxx> 42 #include <tools/debug.hxx> 43 #include <tools/datetime.hxx> 44 #include <com/sun/star/uno/Any.hxx> 45 #include <com/sun/star/util/DateTime.hpp> 46 #include <com/sun/star/lang/Locale.hpp> 47 48 // STATIC DATA ----------------------------------------------------------- 49 50 DBG_NAME(SfxDateTimeItem) 51 52 53 // ----------------------------------------------------------------------- 54 55 TYPEINIT1(SfxDateTimeItem, SfxPoolItem); 56 57 // ----------------------------------------------------------------------- 58 59 SfxDateTimeItem::SfxDateTimeItem( sal_uInt16 which ) : 60 SfxPoolItem( which ) 61 { 62 DBG_CTOR(SfxDateTimeItem, 0); 63 } 64 65 // ----------------------------------------------------------------------- 66 67 SfxDateTimeItem::SfxDateTimeItem( sal_uInt16 which, const DateTime& rDT ) : 68 SfxPoolItem( which ), 69 aDateTime( rDT ) 70 71 { 72 DBG_CTOR(SfxDateTimeItem, 0); 73 } 74 75 // ----------------------------------------------------------------------- 76 77 SfxDateTimeItem::SfxDateTimeItem( const SfxDateTimeItem& rItem ) : 78 SfxPoolItem( rItem ), 79 aDateTime( rItem.aDateTime ) 80 { 81 DBG_CTOR(SfxDateTimeItem, 0); 82 } 83 84 // ----------------------------------------------------------------------- 85 86 int SfxDateTimeItem::operator==( const SfxPoolItem& rItem ) const 87 { 88 DBG_CHKTHIS(SfxDateTimeItem, 0); 89 DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal type" ); 90 return ( ( (SfxDateTimeItem&)rItem ).aDateTime == aDateTime ); 91 } 92 93 // ----------------------------------------------------------------------- 94 95 int SfxDateTimeItem::Compare( const SfxPoolItem& rItem ) const 96 { 97 DBG_CHKTHIS(SfxDateTimeItem, 0); 98 DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal type" ); 99 100 // da X.Compare( Y ) am String einem Compare( Y, X ) entspricht, 101 // vergleichen wir hier Y mit X 102 if ( ( (const SfxDateTimeItem&)rItem ).aDateTime < aDateTime ) 103 return -1; 104 else if ( ( (const SfxDateTimeItem&)rItem ).aDateTime == aDateTime ) 105 return 0; 106 else 107 return 1; 108 } 109 110 // ----------------------------------------------------------------------- 111 112 SfxPoolItem* SfxDateTimeItem::Create( SvStream& rStream, sal_uInt16 ) const 113 { 114 DBG_CHKTHIS(SfxDateTimeItem, 0); 115 sal_uInt32 nDate = 0; 116 sal_Int32 nTime = 0; 117 rStream >> nDate; 118 rStream >> nTime; 119 DateTime aDT(nDate, nTime); 120 return new SfxDateTimeItem( Which(), aDT ); 121 } 122 123 // ----------------------------------------------------------------------- 124 125 SvStream& SfxDateTimeItem::Store( SvStream& rStream, sal_uInt16 ) const 126 { 127 DBG_CHKTHIS(SfxDateTimeItem, 0); 128 rStream << aDateTime.GetDate(); 129 rStream << aDateTime.GetTime(); 130 return rStream; 131 } 132 133 // ----------------------------------------------------------------------- 134 135 SfxPoolItem* SfxDateTimeItem::Clone( SfxItemPool* ) const 136 { 137 DBG_CHKTHIS(SfxDateTimeItem, 0); 138 return new SfxDateTimeItem( *this ); 139 } 140 141 // ----------------------------------------------------------------------- 142 143 SfxItemPresentation SfxDateTimeItem::GetPresentation 144 ( 145 SfxItemPresentation /*ePresentation*/, 146 SfxMapUnit /*eCoreMetric*/, 147 SfxMapUnit /*ePresentationMetric*/, 148 XubString& rText, 149 const IntlWrapper * pIntlWrapper 150 ) const 151 { 152 DBG_CHKTHIS(SfxDateTimeItem, 0); 153 if (aDateTime.IsValid()) 154 if (pIntlWrapper) 155 { 156 rText = pIntlWrapper->getLocaleData()->getDate(aDateTime); 157 rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", ")); 158 rText += pIntlWrapper->getLocaleData()->getTime(aDateTime); 159 } 160 else 161 { 162 DBG_WARNING("SfxDateTimeItem::GetPresentation():" 163 " Using default en_US IntlWrapper"); 164 const IntlWrapper aIntlWrapper( 165 ::comphelper::getProcessServiceFactory(), LANGUAGE_ENGLISH_US ); 166 rText = aIntlWrapper.getLocaleData()->getDate(aDateTime); 167 rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", ")); 168 rText += aIntlWrapper.getLocaleData()->getTime(aDateTime); 169 } 170 else 171 rText.Erase(); 172 return SFX_ITEM_PRESENTATION_NAMELESS; 173 } 174 175 //---------------------------------------------------------------------------- 176 // virtual 177 sal_Bool SfxDateTimeItem::PutValue( const com::sun::star::uno::Any& rVal, 178 sal_uInt8 nMemberId ) 179 { 180 nMemberId &= ~CONVERT_TWIPS; 181 com::sun::star::util::DateTime aValue; 182 if ( rVal >>= aValue ) 183 { 184 aDateTime = DateTime( Date( aValue.Day, 185 aValue.Month, 186 aValue.Year ), 187 Time( aValue.Hours, 188 aValue.Minutes, 189 aValue.Seconds, 190 aValue.HundredthSeconds ) ); 191 return sal_True; 192 } 193 194 DBG_ERROR( "SfxDateTimeItem::PutValue - Wrong type!" ); 195 return sal_False; 196 } 197 198 //---------------------------------------------------------------------------- 199 // virtual 200 sal_Bool SfxDateTimeItem::QueryValue( com::sun::star::uno::Any& rVal, 201 sal_uInt8 nMemberId ) const 202 { 203 nMemberId &= ~CONVERT_TWIPS; 204 com::sun::star::util::DateTime aValue( aDateTime.Get100Sec(), 205 aDateTime.GetSec(), 206 aDateTime.GetMin(), 207 aDateTime.GetHour(), 208 aDateTime.GetDay(), 209 aDateTime.GetMonth(), 210 aDateTime.GetYear() ); 211 rVal <<= aValue; 212 return sal_True; 213 } 214 215 // ----------------------------------------------------------------------- 216 // ----------------------------------------------------------------------- 217 // ----------------------------------------------------------------------- 218 219 TYPEINIT1(SfxColumnDateTimeItem, SfxDateTimeItem); 220 221 222 SfxColumnDateTimeItem::SfxColumnDateTimeItem( sal_uInt16 which ) : 223 SfxDateTimeItem( which ) 224 {} 225 226 SfxColumnDateTimeItem::SfxColumnDateTimeItem( sal_uInt16 which, const DateTime& rDT ) : 227 SfxDateTimeItem( which, rDT ) 228 {} 229 230 SfxColumnDateTimeItem::SfxColumnDateTimeItem( const SfxDateTimeItem& rCpy ) : 231 SfxDateTimeItem( rCpy ) 232 {} 233 234 SfxPoolItem* SfxColumnDateTimeItem::Clone( SfxItemPool* ) const 235 { 236 return new SfxColumnDateTimeItem( *this ); 237 } 238 239 SfxItemPresentation SfxColumnDateTimeItem::GetPresentation 240 ( 241 SfxItemPresentation /*ePresentation*/, 242 SfxMapUnit /*eCoreMetric*/, 243 SfxMapUnit /*ePresentationMetric*/, 244 XubString& rText, 245 const IntlWrapper * pIntlWrapper 246 ) const 247 { 248 DBG_ASSERT(pIntlWrapper, 249 "SfxColumnDateTimeItem::GetPresentation():" 250 " Using default en_US IntlWrapper"); 251 252 ::com::sun::star::lang::Locale aLocale; 253 if (GetDateTime() == DateTime(Date(1, 2, 3), Time(3, 2, 1))) 254 { 255 rText = String(SvtSimpleResId(STR_COLUM_DT_AUTO, 256 pIntlWrapper ? 257 pIntlWrapper->getLocale() : 258 aLocale)); 259 } 260 else if (pIntlWrapper) 261 { 262 rText = pIntlWrapper->getLocaleData()->getDate(GetDateTime()); 263 rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", ")); 264 rText += pIntlWrapper->getLocaleData()->getTime(GetDateTime()); 265 } 266 else 267 { 268 const IntlWrapper aIntlWrapper( 269 ::comphelper::getProcessServiceFactory(), LANGUAGE_ENGLISH_US ); 270 rText = aIntlWrapper.getLocaleData()->getDate(GetDateTime()); 271 rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", ")); 272 rText += aIntlWrapper.getLocaleData()->getTime(GetDateTime()); 273 } 274 return SFX_ITEM_PRESENTATION_NAMELESS; 275 } 276 277 278 279