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 "imageprovider.hxx"
28 #include "dbu_resource.hrc"
29 #include "moduledbu.hxx"
30 #include "dbustrings.hrc"
31 
32 /** === begin UNO includes === **/
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/graphic/XGraphic.hpp>
35 #include <com/sun/star/graphic/GraphicColorMode.hpp>
36 #include <com/sun/star/sdb/application/XTableUIProvider.hpp>
37 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
38 /** === end UNO includes === **/
39 
40 #include <tools/diagnose_ex.h>
41 
42 //........................................................................
43 namespace dbaui
44 {
45 //........................................................................
46 
47 	/** === begin UNO using === **/
48     using ::com::sun::star::uno::Reference;
49     using ::com::sun::star::sdbc::XConnection;
50     using ::com::sun::star::uno::Exception;
51     using ::com::sun::star::uno::UNO_QUERY_THROW;
52     using ::com::sun::star::container::XNameAccess;
53     using ::com::sun::star::beans::XPropertySet;
54     using ::com::sun::star::graphic::XGraphic;
55     using ::com::sun::star::sdb::application::XTableUIProvider;
56     using ::com::sun::star::uno::UNO_QUERY;
57     using ::com::sun::star::sdbcx::XViewsSupplier;
58     using ::com::sun::star::uno::UNO_SET_THROW;
59 	/** === end UNO using === **/
60     namespace GraphicColorMode = ::com::sun::star::graphic::GraphicColorMode;
61 
62 	//====================================================================
63 	//= ImageProvider_Data
64 	//====================================================================
65     struct ImageProvider_Data
66     {
67         /// the connection we work with
68         Reference< XConnection >        xConnection;
69         /// the views of the connection, if the DB supports views
70         Reference< XNameAccess >        xViews;
71         /// interface for providing table's UI
72         Reference< XTableUIProvider >   xTableUI;
73     };
74 
75 	//--------------------------------------------------------------------
76     namespace
77     {
78 	    //................................................................
79         static void lcl_getConnectionProvidedTableIcon_nothrow(  const ImageProvider_Data& _rData,
80             const ::rtl::OUString& _rName, Reference< XGraphic >& _out_rxGraphic, Reference< XGraphic >& _out_rxGraphicHC )
81         {
82             try
83             {
84                 if ( _rData.xTableUI.is() )
85                 {
86                     _out_rxGraphic = _rData.xTableUI->getTableIcon( _rName, GraphicColorMode::NORMAL );
87                     _out_rxGraphicHC = _rData.xTableUI->getTableIcon( _rName, GraphicColorMode::HIGH_CONTRAST );
88                 }
89             }
90             catch( const Exception& )
91             {
92             	DBG_UNHANDLED_EXCEPTION();
93             }
94         }
95 
96 	    //................................................................
97         static void lcl_getTableImageResourceID_nothrow( const ImageProvider_Data& _rData, const ::rtl::OUString& _rName,
98             sal_uInt16& _out_rResourceID, sal_uInt16& _out_rResourceID_HC )
99         {
100             _out_rResourceID = _out_rResourceID_HC = 0;
101             try
102             {
103                 bool bIsView = _rData.xViews.is() && _rData.xViews->hasByName( _rName );
104                 if ( bIsView )
105                 {
106                     _out_rResourceID = VIEW_TREE_ICON;
107                     _out_rResourceID_HC = VIEW_TREE_ICON_SCH;
108                 }
109                 else
110                 {
111                     _out_rResourceID = TABLE_TREE_ICON;
112                     _out_rResourceID_HC = TABLE_TREE_ICON_SCH;
113                 }
114             }
115             catch( const Exception& )
116             {
117             	DBG_UNHANDLED_EXCEPTION();
118             }
119         }
120     }
121 	//====================================================================
122 	//= ImageProvider
123 	//====================================================================
124 	//--------------------------------------------------------------------
125     ImageProvider::ImageProvider()
126         :m_pData( new ImageProvider_Data )
127     {
128     }
129 
130 	//--------------------------------------------------------------------
131     ImageProvider::ImageProvider( const Reference< XConnection >& _rxConnection )
132         :m_pData( new ImageProvider_Data )
133     {
134         m_pData->xConnection = _rxConnection;
135         try
136         {
137             Reference< XViewsSupplier > xSuppViews( m_pData->xConnection, UNO_QUERY );
138             if ( xSuppViews.is() )
139                 m_pData->xViews.set( xSuppViews->getViews(), UNO_SET_THROW );
140 
141             m_pData->xTableUI.set( _rxConnection, UNO_QUERY );
142         }
143         catch( const Exception& )
144         {
145         	DBG_UNHANDLED_EXCEPTION();
146         }
147     }
148 
149 	//--------------------------------------------------------------------
150     void ImageProvider::getImages( const String& _rName, const sal_Int32 _nDatabaseObjectType, Image& _out_rImage, Image& _out_rImageHC )
151     {
152         if ( _nDatabaseObjectType != DatabaseObject::TABLE )
153         {
154             // for types other than tables, the icon does not depend on the concrete object
155             _out_rImage = getDefaultImage( _nDatabaseObjectType, false );
156             _out_rImageHC = getDefaultImage( _nDatabaseObjectType, true );
157         }
158         else
159         {
160             // check whether the connection can give us an icon
161             Reference< XGraphic > xGraphic;
162             Reference< XGraphic > xGraphicHC;
163             lcl_getConnectionProvidedTableIcon_nothrow( *m_pData, _rName, xGraphic, xGraphicHC );
164             if ( xGraphic.is() )
165                 _out_rImage = Image( xGraphic );
166             if ( xGraphicHC.is() )
167                 _out_rImageHC = Image( xGraphicHC );
168 
169             if ( !_out_rImage || !_out_rImageHC )
170             {
171                 // no -> determine by type
172                 sal_uInt16 nImageResourceID = 0;
173                 sal_uInt16 nImageResourceID_HC = 0;
174                 lcl_getTableImageResourceID_nothrow( *m_pData, _rName, nImageResourceID, nImageResourceID_HC );
175 
176                 if ( nImageResourceID && !_out_rImage )
177                     _out_rImage = Image( ModuleRes( nImageResourceID ) );
178                 if ( nImageResourceID_HC && !_out_rImageHC )
179                     _out_rImageHC = Image( ModuleRes( nImageResourceID_HC ) );
180             }
181         }
182     }
183 
184 	//--------------------------------------------------------------------
185     Image ImageProvider::getDefaultImage( sal_Int32 _nDatabaseObjectType, bool _bHighContrast )
186     {
187         Image aObjectImage;
188         sal_uInt16 nImageResourceID( getDefaultImageResourceID( _nDatabaseObjectType, _bHighContrast ) );
189         if ( nImageResourceID )
190             aObjectImage = Image( ModuleRes( nImageResourceID ) );
191         return aObjectImage;
192     }
193 
194 	//--------------------------------------------------------------------
195     sal_uInt16 ImageProvider::getDefaultImageResourceID( sal_Int32 _nDatabaseObjectType, bool _bHighContrast )
196     {
197         sal_uInt16 nImageResourceID( 0 );
198         switch ( _nDatabaseObjectType )
199         {
200         case DatabaseObject::QUERY:
201             nImageResourceID = _bHighContrast ? QUERY_TREE_ICON_SCH : QUERY_TREE_ICON;
202             break;
203         case DatabaseObject::FORM:
204             nImageResourceID = _bHighContrast ? FORM_TREE_ICON_SCH : FORM_TREE_ICON;
205             break;
206         case DatabaseObject::REPORT:
207             nImageResourceID = _bHighContrast ? REPORT_TREE_ICON_SCH : REPORT_TREE_ICON;
208             break;
209         case DatabaseObject::TABLE:
210             nImageResourceID = _bHighContrast ? TABLE_TREE_ICON_SCH : TABLE_TREE_ICON;
211             break;
212         default:
213             OSL_ENSURE( false, "ImageProvider::getDefaultImage: invalid database object type!" );
214             break;
215         }
216         return nImageResourceID;
217     }
218 
219 	//--------------------------------------------------------------------
220     Image ImageProvider::getFolderImage( sal_Int32 _nDatabaseObjectType, bool _bHighContrast )
221     {
222         sal_uInt16 nImageResourceID( 0 );
223         switch ( _nDatabaseObjectType )
224         {
225         case DatabaseObject::QUERY:
226             nImageResourceID = _bHighContrast ? QUERYFOLDER_TREE_ICON_SCH : QUERYFOLDER_TREE_ICON;
227             break;
228         case DatabaseObject::FORM:
229             nImageResourceID = _bHighContrast ? FORMFOLDER_TREE_ICON_SCH : FORMFOLDER_TREE_ICON;
230             break;
231         case DatabaseObject::REPORT:
232             nImageResourceID = _bHighContrast ? REPORTFOLDER_TREE_ICON_SCH : REPORTFOLDER_TREE_ICON;
233             break;
234         case DatabaseObject::TABLE:
235             nImageResourceID = _bHighContrast ? TABLEFOLDER_TREE_ICON_SCH : TABLEFOLDER_TREE_ICON;
236             break;
237         default:
238             OSL_ENSURE( false, "ImageProvider::getDefaultImage: invalid database object type!" );
239             break;
240         }
241 
242         Image aFolderImage;
243         if ( nImageResourceID )
244             aFolderImage = Image( ModuleRes( nImageResourceID ) );
245         return aFolderImage;
246     }
247 
248 	//--------------------------------------------------------------------
249     Image ImageProvider::getDatabaseImage( bool _bHighContrast )
250     {
251         return Image( ModuleRes( _bHighContrast ? DATABASE_TREE_ICON_SCH : DATABASE_TREE_ICON ) );
252     }
253 
254 //........................................................................
255 } // namespace dbaui
256 //........................................................................
257 
258