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