LCOV - code coverage report
Current view: top level - forms/source/misc - limitedformats.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 109 126 86.5 %
Date: 2014-04-11 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          28 :     ::osl::Mutex                            OLimitedFormats::s_aMutex;
      41          28 :     Reference< XNumberFormatsSupplier >     OLimitedFormats::s_xStandardFormats;
      42             : 
      43             : 
      44             :     //=
      45             : 
      46             : 
      47             :     enum LocaleType
      48             :     {
      49             :         ltEnglishUS,
      50             :         ltGerman,
      51             :         ltSystem
      52             :     };
      53             : 
      54             : 
      55         292 :     static const Locale& getLocale(LocaleType _eType)
      56             :     {
      57         292 :         static const Locale s_aEnglishUS( OUString("en"), OUString("us"), OUString() );
      58         292 :         static const Locale s_aGerman( OUString("de"), OUString("DE"), OUString() );
      59         292 :         static const OUString s_sEmptyString;
      60         292 :         static const Locale s_aSystem( s_sEmptyString, s_sEmptyString, s_sEmptyString );
      61             : 
      62         292 :         switch (_eType)
      63             :         {
      64             :             case ltEnglishUS:
      65         166 :                 return s_aEnglishUS;
      66             : 
      67             :             case ltGerman:
      68         126 :                 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         124 :     static FormatEntry* lcl_getFormatTable(sal_Int16 nTableId)
      88             :     {
      89         124 :         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          47 :                 return s_aFormats;
     101             :             }
     102             :             case FormComponentType::DATEFIELD:
     103             :             {
     104             :                 static FormatEntry s_aFormats[] = {
     105             :                     { "T-M-JJ", -1, ltGerman },
     106             :                     { "TT-MM-JJ", -1, ltGerman },
     107             :                     { "TT-MM-JJJJ", -1, ltGerman },
     108             :                     { "NNNNT. MMMM JJJJ", -1, ltGerman },
     109             : 
     110             :                     { "DD/MM/YY", -1, ltEnglishUS },
     111             :                     { "MM/DD/YY", -1, ltEnglishUS },
     112             :                     { "YY/MM/DD", -1, ltEnglishUS },
     113             :                     { "DD/MM/YYYY", -1, ltEnglishUS },
     114             :                     { "MM/DD/YYYY", -1, ltEnglishUS },
     115             :                     { "YYYY/MM/DD", -1, ltEnglishUS },
     116             : 
     117             :                     { "JJ-MM-TT", -1, ltGerman },
     118             :                     { "JJJJ-MM-TT", -1, ltGerman },
     119             : 
     120             :                     { NULL, -1, ltSystem }
     121             :                 };
     122          77 :                 return s_aFormats;
     123             :             }
     124             :         }
     125             : 
     126             :         OSL_FAIL("lcl_getFormatTable: invalid id!");
     127           0 :         return NULL;
     128             :     }
     129             : 
     130             : 
     131             :     //= OLimitedFormats
     132             : 
     133             : 
     134          39 :     OLimitedFormats::OLimitedFormats(const Reference< XComponentContext >& _rxContext, const sal_Int16 _nClassId)
     135             :         :m_nFormatEnumPropertyHandle(-1)
     136          39 :         ,m_nTableId(_nClassId)
     137             :     {
     138             :         OSL_ENSURE(_rxContext.is(), "OLimitedFormats::OLimitedFormats: invalid service factory!");
     139          39 :         acquireSupplier(_rxContext);
     140          39 :         ensureTableInitialized(m_nTableId);
     141          39 :     }
     142             : 
     143             : 
     144          74 :     OLimitedFormats::~OLimitedFormats()
     145             :     {
     146          37 :         releaseSupplier();
     147          37 :     }
     148             : 
     149             : 
     150          39 :     void OLimitedFormats::ensureTableInitialized(const sal_Int16 _nTableId)
     151             :     {
     152          39 :         FormatEntry* pFormatTable = lcl_getFormatTable(_nTableId);
     153          39 :         if (-1 == pFormatTable->nKey)
     154             :         {
     155          17 :             ::osl::MutexGuard aGuard(s_aMutex);
     156          17 :             if (-1 == pFormatTable->nKey)
     157             :             {
     158             :                 // initialize the keys
     159          17 :                 Reference<XNumberFormats> xStandardFormats;
     160          17 :                 if (s_xStandardFormats.is())
     161          17 :                     xStandardFormats = s_xStandardFormats->getNumberFormats();
     162             :                 OSL_ENSURE(xStandardFormats.is(), "OLimitedFormats::ensureTableInitialized: don't have a formats supplier!");
     163             : 
     164          17 :                 if (xStandardFormats.is())
     165             :                 {
     166             :                     // loop through the table
     167          17 :                     FormatEntry* pLoopFormats = pFormatTable;
     168         214 :                     while (pLoopFormats->pDescription)
     169             :                     {
     170             :                         // get the key for the description
     171         180 :                         pLoopFormats->nKey = xStandardFormats->queryKey(
     172             :                             OUString::createFromAscii(pLoopFormats->pDescription),
     173         180 :                             getLocale(pLoopFormats->eLocale),
     174             :                             sal_False
     175         360 :                         );
     176             : 
     177         180 :                         if (-1 == pLoopFormats->nKey)
     178             :                         {
     179          98 :                             pLoopFormats->nKey = xStandardFormats->addNew(
     180             :                                 OUString::createFromAscii(pLoopFormats->pDescription),
     181          98 :                                 getLocale(pLoopFormats->eLocale)
     182         196 :                             );
     183             : #ifdef DBG_UTIL
     184             :                             try
     185             :                             {
     186             :                                 xStandardFormats->getByKey(pLoopFormats->nKey);
     187             :                             }
     188             :                             catch(const Exception&)
     189             :                             {
     190             :                                 OSL_FAIL("OLimitedFormats::ensureTableInitialized: adding the key to the formats collection failed!");
     191             :                             }
     192             : #endif
     193             :                         }
     194             : 
     195             :                         // next
     196         180 :                         ++pLoopFormats;
     197             :                     }
     198          17 :                 }
     199          17 :             }
     200             :         }
     201          39 :     }
     202             : 
     203             : 
     204          26 :     void OLimitedFormats::clearTable(const sal_Int16 _nTableId)
     205             :     {
     206          26 :         ::osl::MutexGuard aGuard(s_aMutex);
     207          26 :         FormatEntry* pFormats = lcl_getFormatTable(_nTableId);
     208          26 :         FormatEntry* pResetLoop = pFormats;
     209         260 :         while (pResetLoop->pDescription)
     210             :         {
     211         208 :             pResetLoop->nKey = -1;
     212         208 :             ++pResetLoop;
     213          26 :         }
     214          26 :     }
     215             : 
     216             : 
     217          76 :     void OLimitedFormats::setAggregateSet(const Reference< XFastPropertySet >& _rxAggregate, sal_Int32 _nOriginalPropertyHandle)
     218             :     {
     219             :         // changes (NULL -> not NULL) and (not NULL -> NULL) are allowed
     220             :         OSL_ENSURE(!m_xAggregate.is() || !_rxAggregate.is(), "OLimitedFormats::setAggregateSet: already have an aggregate!");
     221             :         OSL_ENSURE(_rxAggregate.is() || m_xAggregate.is(), "OLimitedFormats::setAggregateSet: invalid new aggregate!");
     222             : 
     223          76 :         m_xAggregate = _rxAggregate;
     224          76 :         m_nFormatEnumPropertyHandle = _nOriginalPropertyHandle;
     225             : #ifdef DBG_UTIL
     226             :         if (m_xAggregate.is())
     227             :         {
     228             :             try
     229             :             {
     230             :                 m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
     231             :             }
     232             :             catch(const Exception&)
     233             :             {
     234             :                 OSL_FAIL("OLimitedFormats::setAggregateSet: invalid handle!");
     235             :             }
     236             :         }
     237             : #endif
     238          76 :     }
     239             : 
     240             : 
     241          57 :     void OLimitedFormats::getFormatKeyPropertyValue( Any& _rValue ) const
     242             :     {
     243          57 :         _rValue.clear();
     244             : 
     245             :         OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::getFormatKeyPropertyValue: not initialized!");
     246          57 :         if (m_xAggregate.is())
     247             :         {
     248             :             // get the aggregate's enum property value
     249          57 :             Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
     250          57 :             sal_Int32 nValue = -1;
     251          57 :             ::cppu::enum2int(nValue, aEnumPropertyValue);
     252             : 
     253             :             // get the translation table
     254          57 :             const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
     255             : 
     256             :             // seek to the nValue'th entry
     257          57 :             sal_Int32 nLookup = 0;
     258         172 :             for (   ;
     259         230 :                     (NULL != pFormats->pDescription) && (nLookup < nValue);
     260             :                     ++pFormats, ++nLookup
     261             :                 )
     262             :                 ;
     263             :             OSL_ENSURE(NULL != pFormats->pDescription, "OLimitedFormats::getFormatKeyPropertyValue: did not find the value!");
     264          57 :             if (pFormats->pDescription)
     265          57 :                 _rValue <<= pFormats->nKey;
     266             :         }
     267             : 
     268             :         // TODO: should use a standard format for the control type we're working for
     269          57 :     }
     270             : 
     271             : 
     272           2 :     sal_Bool OLimitedFormats::convertFormatKeyPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rNewValue)
     273             :     {
     274             :         OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::convertFormatKeyPropertyValue: not initialized!");
     275             : 
     276           2 :         if (m_xAggregate.is())
     277             :         {
     278             :             // the new format key to set
     279           2 :             sal_Int32 nNewFormat = 0;
     280           2 :             if (!(_rNewValue >>= nNewFormat))
     281           0 :                 throw IllegalArgumentException();
     282             : 
     283             :             // get the old (enum) value from the aggregate
     284           2 :             Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
     285           2 :             sal_Int32 nOldEnumValue = -1;
     286           2 :             ::cppu::enum2int(nOldEnumValue, aEnumPropertyValue);
     287             : 
     288             :             // get the translation table
     289           2 :             const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
     290             : 
     291           2 :             _rOldValue.clear();
     292           2 :             _rConvertedValue.clear();
     293             : 
     294             :             // look for the entry with the given format key
     295           2 :             sal_Int32 nTablePosition = 0;
     296          36 :             for (   ;
     297          34 :                     (NULL != pFormats->pDescription) && (nNewFormat != pFormats->nKey);
     298             :                     ++pFormats, ++nTablePosition
     299             :                 )
     300             :             {
     301          16 :                 if (nTablePosition == nOldEnumValue)
     302           2 :                     _rOldValue <<= pFormats->nKey;
     303             :             }
     304             : 
     305           2 :             sal_Bool bFoundIt = (NULL != pFormats->pDescription);
     306           2 :             sal_Bool bModified = sal_False;
     307           2 :             if (bFoundIt)
     308             :             {
     309           0 :                 _rConvertedValue <<= (sal_Int16)nTablePosition;
     310           0 :                 bModified = nTablePosition != nOldEnumValue;
     311             :             }
     312             : 
     313           2 :             if (!_rOldValue.hasValue())
     314             :             {   // did not reach the end of the table (means we found nNewFormat)
     315             :                 // -> go to the end to ensure that _rOldValue is set
     316           0 :                 while (pFormats->pDescription)
     317             :                 {
     318           0 :                     if (nTablePosition == nOldEnumValue)
     319             :                     {
     320           0 :                         _rOldValue <<= pFormats->nKey;
     321           0 :                         break;
     322             :                     }
     323             : 
     324           0 :                     ++pFormats;
     325           0 :                     ++nTablePosition;
     326             :                 }
     327             :             }
     328             : 
     329             :             OSL_ENSURE(_rOldValue.hasValue(), "OLimitedFormats::convertFormatKeyPropertyValue: did not find the old enum value in the table!");
     330             : 
     331           2 :             if (!bFoundIt)
     332             :             {   // somebody gave us a format which we can't translate
     333           2 :                 OUString sMessage ("This control supports only a very limited number of formats.");
     334           2 :                 throw IllegalArgumentException(sMessage, NULL, 2);
     335             :             }
     336             : 
     337           2 :             return bModified;
     338             :         }
     339             : 
     340           0 :         return sal_False;
     341             :     }
     342             : 
     343             : 
     344           0 :     void OLimitedFormats::setFormatKeyPropertyValue( const Any& _rNewValue )
     345             :     {
     346             :         OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::setFormatKeyPropertyValue: not initialized!");
     347             : 
     348           0 :         if (m_xAggregate.is())
     349             :         {   // this is to be called after convertFormatKeyPropertyValue, where
     350             :             // we translated the format key into an enum value.
     351             :             // So now we can simply forward this enum value to our aggregate
     352           0 :             m_xAggregate->setFastPropertyValue(m_nFormatEnumPropertyHandle, _rNewValue);
     353             :         }
     354           0 :     }
     355             : 
     356             : 
     357          39 :     void OLimitedFormats::acquireSupplier(const Reference< XComponentContext >& _rxContext)
     358             :     {
     359          39 :         ::osl::MutexGuard aGuard(s_aMutex);
     360          39 :         if (1 == ++s_nInstanceCount)
     361             :         {   // create the standard formatter
     362          14 :             s_xStandardFormats = NumberFormatsSupplier::createWithLocale(_rxContext, getLocale(ltEnglishUS));
     363          39 :         }
     364          39 :     }
     365             : 
     366             : 
     367          37 :     void OLimitedFormats::releaseSupplier()
     368             :     {
     369          37 :         ::osl::MutexGuard aGuard(s_aMutex);
     370          37 :         if (0 == --s_nInstanceCount)
     371             :         {
     372          13 :             ::comphelper::disposeComponent(s_xStandardFormats);
     373          13 :             s_xStandardFormats = NULL;
     374             : 
     375          13 :             clearTable(FormComponentType::TIMEFIELD);
     376          13 :             clearTable(FormComponentType::DATEFIELD);
     377          37 :         }
     378          37 :     }
     379             : 
     380             : 
     381          84 : }   // namespace frm
     382             : 
     383             : 
     384             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10