1 /*****************************************************************************
2  * art_manager.cpp
3  *****************************************************************************
4  * Copyright (C) 2010 the VideoLAN team
5  * $Id: f4dec63de64e2126469c67cfd6f83b3b1c8faaa4 $
6  *
7  * Author: Erwan Tulou      <erwan10@vidoelan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 
28 #include "art_manager.hpp"
29 #include <vlc_image.h>
30 
31 #include <new>
32 
33 #define MAX_ART_CACHED    2
34 
35 
instance(intf_thread_t * pIntf)36 ArtManager *ArtManager::instance( intf_thread_t *pIntf )
37 {
38     if( pIntf->p_sys->p_artManager == NULL )
39     {
40         pIntf->p_sys->p_artManager = new ArtManager( pIntf );
41     }
42 
43     return pIntf->p_sys->p_artManager;
44 }
45 
46 
destroy(intf_thread_t * pIntf)47 void ArtManager::destroy( intf_thread_t *pIntf )
48 {
49     delete pIntf->p_sys->p_artManager;
50     pIntf->p_sys->p_artManager = NULL;
51 }
52 
53 
ArtManager(intf_thread_t * pIntf)54 ArtManager::ArtManager( intf_thread_t* pIntf ) : SkinObject( pIntf )
55 {
56     // initialize handler
57     m_pImageHandler = image_HandlerCreate( pIntf );
58 
59     if( !m_pImageHandler )
60         msg_Err( getIntf(), "initialization of art manager failed" );
61 }
62 
63 
~ArtManager()64 ArtManager::~ArtManager( )
65 {
66     if( m_pImageHandler )
67     {
68         image_HandlerDelete( m_pImageHandler );
69         m_pImageHandler = NULL;
70     }
71 
72     std::list<ArtBitmap*>::const_iterator it;
73     for( it = m_listBitmap.begin(); it != m_listBitmap.end(); ++it )
74         delete *it;
75     m_listBitmap.clear();
76 }
77 
78 
getArtBitmap(std::string uriName)79 ArtBitmap* ArtManager::getArtBitmap( std::string uriName )
80 {
81     if( !uriName.size() )
82         return NULL;
83 
84     if( !m_pImageHandler )
85         return NULL;
86 
87     // check whether art is already loaded
88     std::list<ArtBitmap*>::const_iterator it;
89     for( it = m_listBitmap.begin(); it != m_listBitmap.end(); ++it )
90     {
91         if( (*it)->getUriName() == uriName )
92             return *it;
93     }
94 
95     // create and retain a new ArtBitmap since uri is not yet known
96     ArtBitmap* pArt = new (std::nothrow) ArtBitmap( getIntf(), m_pImageHandler, uriName );
97     if( pArt && pArt->getWidth() && pArt->getHeight() )
98     {
99         if( m_listBitmap.size() == MAX_ART_CACHED )
100         {
101             ArtBitmap* pOldest = *(m_listBitmap.begin());
102             delete pOldest;
103             m_listBitmap.pop_front();
104         }
105         m_listBitmap.push_back( pArt );
106         return pArt;
107     }
108     else
109     {
110         delete pArt;
111         return NULL;
112     }
113 }
114