Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / imageprovider.cxx
blobba570bfa62ab221ecc64440e045ae493e0a7efbd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "imageprovider.hxx"
21 #include "dbu_resource.hrc"
22 #include "moduledbu.hxx"
23 #include "dbustrings.hrc"
25 #include <com/sun/star/graphic/XGraphic.hpp>
26 #include <com/sun/star/graphic/GraphicColorMode.hpp>
27 #include <com/sun/star/sdb/application/XTableUIProvider.hpp>
28 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
30 #include <tools/diagnose_ex.h>
32 namespace dbaui
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::sdbc::XConnection;
37 using ::com::sun::star::uno::Exception;
38 using ::com::sun::star::container::XNameAccess;
39 using ::com::sun::star::graphic::XGraphic;
40 using ::com::sun::star::sdb::application::XTableUIProvider;
41 using ::com::sun::star::uno::UNO_QUERY;
42 using ::com::sun::star::sdbcx::XViewsSupplier;
43 using ::com::sun::star::uno::UNO_SET_THROW;
45 namespace GraphicColorMode = css::graphic::GraphicColorMode;
46 namespace DatabaseObject = css::sdb::application::DatabaseObject;
48 // ImageProvider_Data
49 struct ImageProvider_Data
51 /// the connection we work with
52 Reference< XConnection > xConnection;
53 /// the views of the connection, if the DB supports views
54 Reference< XNameAccess > xViews;
55 /// interface for providing table's UI
56 Reference< XTableUIProvider > xTableUI;
59 namespace
61 void lcl_getConnectionProvidedTableIcon_nothrow( const ImageProvider_Data& _rData,
62 const OUString& _rName, Reference< XGraphic >& _out_rxGraphic )
64 try
66 if ( _rData.xTableUI.is() )
67 _out_rxGraphic = _rData.xTableUI->getTableIcon( _rName, GraphicColorMode::NORMAL );
69 catch( const Exception& )
71 DBG_UNHANDLED_EXCEPTION();
75 void lcl_getTableImageResourceID_nothrow( const ImageProvider_Data& _rData, const OUString& _rName,
76 sal_uInt16& _out_rResourceID)
78 _out_rResourceID = 0;
79 try
81 bool bIsView = _rData.xViews.is() && _rData.xViews->hasByName( _rName );
82 if ( bIsView )
84 _out_rResourceID = VIEW_TREE_ICON;
86 else
88 _out_rResourceID = TABLE_TREE_ICON;
91 catch( const Exception& )
93 DBG_UNHANDLED_EXCEPTION();
97 // ImageProvider
98 ImageProvider::ImageProvider()
99 :m_pData( new ImageProvider_Data )
103 ImageProvider::ImageProvider( const Reference< XConnection >& _rxConnection )
104 :m_pData( new ImageProvider_Data )
106 m_pData->xConnection = _rxConnection;
109 Reference< XViewsSupplier > xSuppViews( m_pData->xConnection, UNO_QUERY );
110 if ( xSuppViews.is() )
111 m_pData->xViews.set( xSuppViews->getViews(), UNO_SET_THROW );
113 m_pData->xTableUI.set( _rxConnection, UNO_QUERY );
115 catch( const Exception& )
117 DBG_UNHANDLED_EXCEPTION();
121 void ImageProvider::getImages( const OUString& _rName, const sal_Int32 _nDatabaseObjectType, Image& _out_rImage )
123 if ( _nDatabaseObjectType != DatabaseObject::TABLE )
125 // for types other than tables, the icon does not depend on the concrete object
126 _out_rImage = getDefaultImage( _nDatabaseObjectType );
128 else
130 // check whether the connection can give us an icon
131 Reference< XGraphic > xGraphic;
132 lcl_getConnectionProvidedTableIcon_nothrow( *m_pData, _rName, xGraphic );
133 if ( xGraphic.is() )
134 _out_rImage = Image( xGraphic );
136 if ( !_out_rImage )
138 // no -> determine by type
139 sal_uInt16 nImageResourceID = 0;
140 lcl_getTableImageResourceID_nothrow( *m_pData, _rName, nImageResourceID );
142 if ( nImageResourceID && !_out_rImage )
143 _out_rImage = Image( ModuleRes( nImageResourceID ) );
148 Image ImageProvider::getDefaultImage( sal_Int32 _nDatabaseObjectType )
150 Image aObjectImage;
151 sal_uInt16 nImageResourceID( getDefaultImageResourceID( _nDatabaseObjectType) );
152 if ( nImageResourceID )
153 aObjectImage = Image( ModuleRes( nImageResourceID ) );
154 return aObjectImage;
157 sal_uInt16 ImageProvider::getDefaultImageResourceID( sal_Int32 _nDatabaseObjectType)
159 sal_uInt16 nImageResourceID( 0 );
160 switch ( _nDatabaseObjectType )
162 case DatabaseObject::QUERY:
163 nImageResourceID = QUERY_TREE_ICON;
164 break;
165 case DatabaseObject::FORM:
166 nImageResourceID = FORM_TREE_ICON;
167 break;
168 case DatabaseObject::REPORT:
169 nImageResourceID = REPORT_TREE_ICON;
170 break;
171 case DatabaseObject::TABLE:
172 nImageResourceID = TABLE_TREE_ICON;
173 break;
174 default:
175 OSL_FAIL( "ImageProvider::getDefaultImage: invalid database object type!" );
176 break;
178 return nImageResourceID;
181 Image ImageProvider::getFolderImage( sal_Int32 _nDatabaseObjectType )
183 sal_uInt16 nImageResourceID( 0 );
184 switch ( _nDatabaseObjectType )
186 case DatabaseObject::QUERY:
187 nImageResourceID = QUERYFOLDER_TREE_ICON;
188 break;
189 case DatabaseObject::FORM:
190 nImageResourceID = FORMFOLDER_TREE_ICON;
191 break;
192 case DatabaseObject::REPORT:
193 nImageResourceID = REPORTFOLDER_TREE_ICON;
194 break;
195 case DatabaseObject::TABLE:
196 nImageResourceID = TABLEFOLDER_TREE_ICON;
197 break;
198 default:
199 OSL_FAIL( "ImageProvider::getDefaultImage: invalid database object type!" );
200 break;
203 Image aFolderImage;
204 if ( nImageResourceID )
205 aFolderImage = Image( ModuleRes( nImageResourceID ) );
206 return aFolderImage;
209 Image ImageProvider::getDatabaseImage()
211 return Image( ModuleRes( DATABASE_TREE_ICON ) );
214 } // namespace dbaui
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */