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_dbaccess.hxx"
26 
27 #include "propertystorage.hxx"
28 
29 /** === begin UNO includes === **/
30 /** === end UNO includes === **/
31 
32 #include <svl/itemset.hxx>
33 #include <svl/stritem.hxx>
34 #include <svl/eitem.hxx>
35 
36 #include <memory>
37 
38 //........................................................................
39 namespace dbaui
40 {
41 //........................................................................
42 
43 	/** === begin UNO using === **/
44 	using ::com::sun::star::uno::Reference;
45 	using ::com::sun::star::uno::XInterface;
46 	using ::com::sun::star::uno::UNO_QUERY;
47 	using ::com::sun::star::uno::Exception;
48 	using ::com::sun::star::uno::RuntimeException;
49 	using ::com::sun::star::uno::Any;
50 	using ::com::sun::star::uno::makeAny;
51 	/** === end UNO using === **/
52 
53 	//====================================================================
54 	//= PropertyStorage
55 	//====================================================================
56 	//--------------------------------------------------------------------
57     PropertyStorage::~PropertyStorage()
58     {
59     }
60 
61 	//====================================================================
62 	//= helper
63 	//====================================================================
64     namespace
65     {
66 	    //----------------------------------------------------------------
67         template < class ITEMTYPE, class UNOTYPE >
68         class ItemAdapter
69         {
70         public:
71             static bool trySet( SfxItemSet& _rSet, ItemId _nItemId, const Any& _rValue )
72             {
73                 const SfxPoolItem& rItem( _rSet.Get( _nItemId ) );
74                 const ITEMTYPE* pTypedItem = dynamic_cast< const ITEMTYPE* >( &rItem );
75                 if ( !pTypedItem )
76                     return false;
77 
78                 UNOTYPE aValue( pTypedItem->GetValue() );
79                 OSL_VERIFY( _rValue >>= aValue );
80                 // TODO: one could throw an IllegalArgumentException here - finally, this method
81                 // is (to be) used from within an XPropertySet::setPropertyValue implementation,
82                 // where this would be the appropriate reaction on wrong value types
83                 ::std::auto_ptr< ITEMTYPE > pClone( dynamic_cast< ITEMTYPE* >( pTypedItem->Clone() ) );
84                 pClone->SetValue( aValue );
85                 _rSet.Put( *pClone );
86                 return true;
87             }
88 
89             static bool tryGet( const SfxPoolItem& _rItem, Any& _out_rValue )
90             {
91                 const ITEMTYPE* pTypedItem = dynamic_cast< const ITEMTYPE* >( &_rItem );
92                 if ( !pTypedItem )
93                     return false;
94 
95                 _out_rValue <<= UNOTYPE( pTypedItem->GetValue() );
96                 return true;
97             }
98         };
99     }
100 
101 	//====================================================================
102 	//= SetItemPropertyStorage
103 	//====================================================================
104 	//--------------------------------------------------------------------
105     void SetItemPropertyStorage::getPropertyValue( Any& _out_rValue ) const
106     {
107         const SfxPoolItem& rItem( m_rItemSet.Get( m_nItemID ) );
108 
109         // try some known item types
110         if  (   ItemAdapter< SfxBoolItem, sal_Bool >::tryGet( rItem, _out_rValue )
111             ||  ItemAdapter< SfxStringItem, ::rtl::OUString >::tryGet( rItem, _out_rValue )
112             )
113             return;
114 
115         OSL_ENSURE( false, "SetItemPropertyStorage::getPropertyValue: unsupported item type!" );
116     }
117 
118 	//--------------------------------------------------------------------
119     void SetItemPropertyStorage::setPropertyValue( const Any& _rValue )
120     {
121         // try some known item types
122         if  (   ItemAdapter< SfxBoolItem, sal_Bool >::trySet( m_rItemSet, m_nItemID, _rValue )
123             ||  ItemAdapter< SfxStringItem, ::rtl::OUString >::trySet( m_rItemSet, m_nItemID, _rValue )
124             )
125             return;
126 
127         OSL_ENSURE( false, "SetItemPropertyStorage::setPropertyValue: unsupported item type!" );
128     }
129 
130 //........................................................................
131 } // namespace dbaui
132 //........................................................................
133