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