LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/forms/source/misc - limitedformats.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 109 126 86.5 %
Date: 2013-07-09 Functions: 13 14 92.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "limitedformats.hxx"
      21             : #include "services.hxx"
      22             : #include <osl/diagnose.h>
      23             : #include <comphelper/types.hxx>
      24             : #include <comphelper/extract.hxx>
      25             : #include <com/sun/star/form/FormComponentType.hpp>
      26             : #include <com/sun/star/util/NumberFormatsSupplier.hpp>
      27             : 
      28             : //.........................................................................
      29             : namespace frm
      30             : {
      31             : //.........................................................................
      32             : 
      33             :     using namespace ::com::sun::star::uno;
      34             :     using namespace ::com::sun::star::util;
      35             :     using namespace ::com::sun::star::lang;
      36             :     using namespace ::com::sun::star::form;
      37             :     using namespace ::com::sun::star::beans;
      38             : 
      39             :     sal_Int32                               OLimitedFormats::s_nInstanceCount(0);
      40          24 :     ::osl::Mutex                            OLimitedFormats::s_aMutex;
      41          24 :     Reference< XNumberFormatsSupplier >     OLimitedFormats::s_xStandardFormats;
      42             : 
      43             :     //=====================================================================
      44             :     //=
      45             :     //=====================================================================
      46             :     //---------------------------------------------------------------------
      47             :     enum LocaleType
      48             :     {
      49             :         ltEnglishUS,
      50             :         ltGerman,
      51             :         ltSystem
      52             :     };
      53             : 
      54             :     //---------------------------------------------------------------------
      55         173 :     static const Locale& getLocale(LocaleType _eType)
      56             :     {
      57         173 :         static const Locale s_aEnglishUS( OUString("en"), OUString("us"), OUString() );
      58         173 :         static const Locale s_aGerman( OUString("de"), OUString("DE"), OUString() );
      59         173 :         static const OUString s_sEmptyString;
      60         173 :         static const Locale s_aSystem( s_sEmptyString, s_sEmptyString, s_sEmptyString );
      61             : 
      62         173 :         switch (_eType)
      63             :         {
      64             :             case ltEnglishUS:
      65         101 :                 return s_aEnglishUS;
      66             : 
      67             :             case ltGerman:
      68          72 :                 return s_aGerman;
      69             : 
      70             :             case ltSystem:
      71           0 :                 return s_aSystem;
      72             :         }
      73             : 
      74             :         OSL_FAIL("getLocale: invalid enum value!");
      75           0 :         return s_aSystem;
      76             :     }
      77             : 
      78             :     //---------------------------------------------------------------------
      79             :     struct FormatEntry
      80             :     {
      81             :         const sal_Char* pDescription;
      82             :         sal_Int32       nKey;
      83             :         LocaleType      eLocale;
      84             :     };
      85             : 
      86             :     //---------------------------------------------------------------------
      87         106 :     static const FormatEntry* lcl_getFormatTable(sal_Int16 nTableId)
      88             :     {
      89         106 :         switch (nTableId)
      90             :         {
      91             :             case FormComponentType::TIMEFIELD:
      92             :             {
      93             :                 static FormatEntry s_aFormats[] = {
      94             :                     { "HH:MM", -1, ltEnglishUS },
      95             :                     { "HH:MM:SS", -1, ltEnglishUS },
      96             :                     { "HH:MM AM/PM", -1, ltEnglishUS },
      97             :                     { "HH:MM:SS AM/PM", -1, ltEnglishUS },
      98             :                     { NULL, -1, ltSystem }
      99             :                 };
     100             :                 // don't switch this table here to const. The compiler could be tempted to really place this
     101             :                 // in a non-writeable segment, but we want to fill in the format keys later ....
     102          42 :                 return s_aFormats;
     103             :             }
     104             :             case FormComponentType::DATEFIELD:
     105             :             {
     106             :                 static FormatEntry s_aFormats[] = {
     107             :                     { "T-M-JJ", -1, ltGerman },
     108             :                     { "TT-MM-JJ", -1, ltGerman },
     109             :                     { "TT-MM-JJJJ", -1, ltGerman },
     110             :                     { "NNNNT. MMMM JJJJ", -1, ltGerman },
     111             : 
     112             :                     { "DD/MM/YY", -1, ltEnglishUS },
     113             :                     { "MM/DD/YY", -1, ltEnglishUS },
     114             :                     { "YY/MM/DD", -1, ltEnglishUS },
     115             :                     { "DD/MM/YYYY", -1, ltEnglishUS },
     116             :                     { "MM/DD/YYYY", -1, ltEnglishUS },
     117             :                     { "YYYY/MM/DD", -1, ltEnglishUS },
     118             : 
     119             :                     { "JJ-MM-TT", -1, ltGerman },
     120             :                     { "JJJJ-MM-TT", -1, ltGerman },
     121             : 
     122             :                     { NULL, -1, ltSystem }
     123             :                 };
     124          64 :                 return s_aFormats;
     125             :             }
     126             :         }
     127             : 
     128             :         OSL_FAIL("lcl_getFormatTable: invalid id!");
     129           0 :         return NULL;
     130             :     }
     131             : 
     132             :     //=====================================================================
     133             :     //= OLimitedFormats
     134             :     //=====================================================================
     135             :     //---------------------------------------------------------------------
     136          32 :     OLimitedFormats::OLimitedFormats(const Reference< XComponentContext >& _rxContext, const sal_Int16 _nClassId)
     137             :         :m_nFormatEnumPropertyHandle(-1)
     138          32 :         ,m_nTableId(_nClassId)
     139             :     {
     140             :         OSL_ENSURE(_rxContext.is(), "OLimitedFormats::OLimitedFormats: invalid service factory!");
     141          32 :         acquireSupplier(_rxContext);
     142          32 :         ensureTableInitialized(m_nTableId);
     143          32 :     }
     144             : 
     145             :     //---------------------------------------------------------------------
     146          60 :     OLimitedFormats::~OLimitedFormats()
     147             :     {
     148          30 :         releaseSupplier();
     149          30 :     }
     150             : 
     151             :     //---------------------------------------------------------------------
     152          32 :     void OLimitedFormats::ensureTableInitialized(const sal_Int16 _nTableId)
     153             :     {
     154          32 :         const FormatEntry* pFormatTable = lcl_getFormatTable(_nTableId);
     155          32 :         if (-1 == pFormatTable->nKey)
     156             :         {
     157          11 :             ::osl::MutexGuard aGuard(s_aMutex);
     158          11 :             if (-1 == pFormatTable->nKey)
     159             :             {
     160             :                 // initialize the keys
     161          11 :                 Reference<XNumberFormats> xStandardFormats;
     162          11 :                 if (s_xStandardFormats.is())
     163          11 :                     xStandardFormats = s_xStandardFormats->getNumberFormats();
     164             :                 OSL_ENSURE(xStandardFormats.is(), "OLimitedFormats::ensureTableInitialized: don't have a formats supplier!");
     165             : 
     166          11 :                 if (xStandardFormats.is())
     167             :                 {
     168             :                     // loop through the table
     169          11 :                     FormatEntry* pLoopFormats = const_cast<FormatEntry*>(pFormatTable);
     170         130 :                     while (pLoopFormats->pDescription)
     171             :                     {
     172             :                         // get the key for the description
     173         108 :                         pLoopFormats->nKey = xStandardFormats->queryKey(
     174             :                             OUString::createFromAscii(pLoopFormats->pDescription),
     175         108 :                             getLocale(pLoopFormats->eLocale),
     176             :                             sal_False
     177         216 :                         );
     178             : 
     179         108 :                         if (-1 == pLoopFormats->nKey)
     180             :                         {
     181          56 :                             pLoopFormats->nKey = xStandardFormats->addNew(
     182             :                                 OUString::createFromAscii(pLoopFormats->pDescription),
     183          56 :                                 getLocale(pLoopFormats->eLocale)
     184         112 :                             );
     185             : #ifdef DBG_UTIL
     186             :                             try
     187             :                             {
     188             :                                 xStandardFormats->getByKey(pLoopFormats->nKey);
     189             :                             }
     190             :                             catch(const Exception&)
     191             :                             {
     192             :                                 OSL_FAIL("OLimitedFormats::ensureTableInitialized: adding the key to the formats collection failed!");
     193             :                             }
     194             : #endif
     195             :                         }
     196             : 
     197             :                         // next
     198         108 :                         ++pLoopFormats;
     199             :                     }
     200          11 :                 }
     201          11 :             }
     202             :         }
     203          32 :     }
     204             : 
     205             :     //---------------------------------------------------------------------
     206          16 :     void OLimitedFormats::clearTable(const sal_Int16 _nTableId)
     207             :     {
     208          16 :         ::osl::MutexGuard aGuard(s_aMutex);
     209          16 :         const FormatEntry* pFormats = lcl_getFormatTable(_nTableId);
     210          16 :         FormatEntry* pResetLoop = const_cast<FormatEntry*>(pFormats);
     211         160 :         while (pResetLoop->pDescription)
     212             :         {
     213         128 :             pResetLoop->nKey = -1;
     214         128 :             ++pResetLoop;
     215          16 :         }
     216          16 :     }
     217             : 
     218             :     //---------------------------------------------------------------------
     219          62 :     void OLimitedFormats::setAggregateSet(const Reference< XFastPropertySet >& _rxAggregate, sal_Int32 _nOriginalPropertyHandle)
     220             :     {
     221             :         // changes (NULL -> not NULL) and (not NULL -> NULL) are allowed
     222             :         OSL_ENSURE(!m_xAggregate.is() || !_rxAggregate.is(), "OLimitedFormats::setAggregateSet: already have an aggregate!");
     223             :         OSL_ENSURE(_rxAggregate.is() || m_xAggregate.is(), "OLimitedFormats::setAggregateSet: invalid new aggregate!");
     224             : 
     225          62 :         m_xAggregate = _rxAggregate;
     226          62 :         m_nFormatEnumPropertyHandle = _nOriginalPropertyHandle;
     227             : #ifdef DBG_UTIL
     228             :         if (m_xAggregate.is())
     229             :         {
     230             :             try
     231             :             {
     232             :                 m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
     233             :             }
     234             :             catch(const Exception&)
     235             :             {
     236             :                 OSL_FAIL("OLimitedFormats::setAggregateSet: invalid handle!");
     237             :             }
     238             :         }
     239             : #endif
     240          62 :     }
     241             : 
     242             :     //---------------------------------------------------------------------
     243          56 :     void OLimitedFormats::getFormatKeyPropertyValue( Any& _rValue ) const
     244             :     {
     245          56 :         _rValue.clear();
     246             : 
     247             :         OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::getFormatKeyPropertyValue: not initialized!");
     248          56 :         if (m_xAggregate.is())
     249             :         {
     250             :             // get the aggregate's enum property value
     251          56 :             Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
     252          56 :             sal_Int32 nValue = -1;
     253          56 :             ::cppu::enum2int(nValue, aEnumPropertyValue);
     254             : 
     255             :             // get the translation table
     256          56 :             const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
     257             : 
     258             :             // seek to the nValue'th entry
     259          56 :             sal_Int32 nLookup = 0;
     260         162 :             for (   ;
     261         212 :                     (NULL != pFormats->pDescription) && (nLookup < nValue);
     262             :                     ++pFormats, ++nLookup
     263             :                 )
     264             :                 ;
     265             :             OSL_ENSURE(NULL != pFormats->pDescription, "OLimitedFormats::getFormatKeyPropertyValue: did not find the value!");
     266          56 :             if (pFormats->pDescription)
     267          56 :                 _rValue <<= pFormats->nKey;
     268             :         }
     269             : 
     270             :         // TODO: should use a standard format for the control type we're working for
     271          56 :     }
     272             : 
     273             :     //---------------------------------------------------------------------
     274           2 :     sal_Bool OLimitedFormats::convertFormatKeyPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rNewValue)
     275             :     {
     276             :         OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::convertFormatKeyPropertyValue: not initialized!");
     277             : 
     278           2 :         if (m_xAggregate.is())
     279             :         {
     280             :             // the new format key to set
     281           2 :             sal_Int32 nNewFormat = 0;
     282           2 :             if (!(_rNewValue >>= nNewFormat))
     283           0 :                 throw IllegalArgumentException();
     284             : 
     285             :             // get the old (enum) value from the aggregate
     286           2 :             Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
     287           2 :             sal_Int32 nOldEnumValue = -1;
     288           2 :             ::cppu::enum2int(nOldEnumValue, aEnumPropertyValue);
     289             : 
     290             :             // get the translation table
     291           2 :             const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
     292             : 
     293           2 :             _rOldValue.clear();
     294           2 :             _rConvertedValue.clear();
     295             : 
     296             :             // look for the entry with the given format key
     297           2 :             sal_Int32 nTablePosition = 0;
     298          36 :             for (   ;
     299          34 :                     (NULL != pFormats->pDescription) && (nNewFormat != pFormats->nKey);
     300             :                     ++pFormats, ++nTablePosition
     301             :                 )
     302             :             {
     303          16 :                 if (nTablePosition == nOldEnumValue)
     304           2 :                     _rOldValue <<= pFormats->nKey;
     305             :             }
     306             : 
     307           2 :             sal_Bool bFoundIt = (NULL != pFormats->pDescription);
     308           2 :             sal_Bool bModified = sal_False;
     309           2 :             if (bFoundIt)
     310             :             {
     311           0 :                 _rConvertedValue <<= (sal_Int16)nTablePosition;
     312           0 :                 bModified = nTablePosition != nOldEnumValue;
     313             :             }
     314             : 
     315           2 :             if (!_rOldValue.hasValue())
     316             :             {   // did not reach the end of the table (means we found nNewFormat)
     317             :                 // -> go to the end to ensure that _rOldValue is set
     318           0 :                 while (pFormats->pDescription)
     319             :                 {
     320           0 :                     if (nTablePosition == nOldEnumValue)
     321             :                     {
     322           0 :                         _rOldValue <<= pFormats->nKey;
     323           0 :                         break;
     324             :                     }
     325             : 
     326           0 :                     ++pFormats;
     327           0 :                     ++nTablePosition;
     328             :                 }
     329             :             }
     330             : 
     331             :             OSL_ENSURE(_rOldValue.hasValue(), "OLimitedFormats::convertFormatKeyPropertyValue: did not find the old enum value in the table!");
     332             : 
     333           2 :             if (!bFoundIt)
     334             :             {   // somebody gave us an format which we can't translate
     335           2 :                 OUString sMessage ("This control supports only a very limited number of formats.");
     336           2 :                 throw IllegalArgumentException(sMessage, NULL, 2);
     337             :             }
     338             : 
     339           2 :             return bModified;
     340             :         }
     341             : 
     342           0 :         return sal_False;
     343             :     }
     344             : 
     345             :     //---------------------------------------------------------------------
     346           0 :     void OLimitedFormats::setFormatKeyPropertyValue( const Any& _rNewValue )
     347             :     {
     348             :         OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::setFormatKeyPropertyValue: not initialized!");
     349             : 
     350           0 :         if (m_xAggregate.is())
     351             :         {   // this is to be called after convertFormatKeyPropertyValue, where
     352             :             // we translated the format key into a enum value.
     353             :             // So now we can simply forward this enum value to our aggreate
     354           0 :             m_xAggregate->setFastPropertyValue(m_nFormatEnumPropertyHandle, _rNewValue);
     355             :         }
     356           0 :     }
     357             : 
     358             :     //---------------------------------------------------------------------
     359          32 :     void OLimitedFormats::acquireSupplier(const Reference< XComponentContext >& _rxContext)
     360             :     {
     361          32 :         ::osl::MutexGuard aGuard(s_aMutex);
     362          32 :         if (1 == ++s_nInstanceCount)
     363             :         {   // create the standard formatter
     364           9 :             s_xStandardFormats = NumberFormatsSupplier::createWithLocale(_rxContext, getLocale(ltEnglishUS));
     365          32 :         }
     366          32 :     }
     367             : 
     368             :     //---------------------------------------------------------------------
     369          30 :     void OLimitedFormats::releaseSupplier()
     370             :     {
     371          30 :         ::osl::MutexGuard aGuard(s_aMutex);
     372          30 :         if (0 == --s_nInstanceCount)
     373             :         {
     374           8 :             ::comphelper::disposeComponent(s_xStandardFormats);
     375           8 :             s_xStandardFormats = NULL;
     376             : 
     377           8 :             clearTable(FormComponentType::TIMEFIELD);
     378           8 :             clearTable(FormComponentType::DATEFIELD);
     379          30 :         }
     380          30 :     }
     381             : 
     382             : //.........................................................................
     383          72 : }   // namespace frm
     384             : //.........................................................................
     385             : 
     386             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10