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