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