summaryrefslogtreecommitdiff
path: root/library/resource.cpp
authorwimpie <wimpie>2005-01-09 02:59:13 (UTC)
committer wimpie <wimpie>2005-01-09 02:59:13 (UTC)
commit987bc9a2c5b39ddd4dc2a665cea65688bfd2179e (patch) (unidiff)
tree815fc6d12162f1a5eccc4b1ae0da61dea3811bdf /library/resource.cpp
parente54346d28b19d3ac671802a25e8c03f346693291 (diff)
downloadopie-987bc9a2c5b39ddd4dc2a665cea65688bfd2179e.zip
opie-987bc9a2c5b39ddd4dc2a665cea65688bfd2179e.tar.gz
opie-987bc9a2c5b39ddd4dc2a665cea65688bfd2179e.tar.bz2
applnk : lazy loading of mime type icons (load only when needed)
resource.cpp : print warning when requested image cannot be found and print the name of that image too
Diffstat (limited to 'library/resource.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/resource.cpp13
1 files changed, 9 insertions, 4 deletions
diff --git a/library/resource.cpp b/library/resource.cpp
index cfa0d26..b31876f 100644
--- a/library/resource.cpp
+++ b/library/resource.cpp
@@ -1,168 +1,173 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of the Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#define QTOPIA_INTERNAL_MIMEEXT 21#define QTOPIA_INTERNAL_MIMEEXT
22#include <qpe/qpeapplication.h> 22#include <qpe/qpeapplication.h>
23#include "resource.h" 23#include "resource.h"
24#include "mimetype.h" 24#include "mimetype.h"
25#include <qdir.h> 25#include <qdir.h>
26#include <qpixmapcache.h> 26#include <qpixmapcache.h>
27 27
28// this namespace is just a workaround for a gcc bug 28// this namespace is just a workaround for a gcc bug
29// gcc exports inline functions in the generated file 29// gcc exports inline functions in the generated file
30// inlinepics_p.h 30// inlinepics_p.h
31 31
32namespace { 32namespace {
33#include "inlinepics_p.h" 33#include "inlinepics_p.h"
34} 34}
35 35
36static bool g_notUseSet = ::getenv("OVERWRITE_ICON_SET"); 36static bool g_notUseSet = ::getenv("OVERWRITE_ICON_SET");
37 37
38/*! 38/*!
39 \class Resource resource.h 39 \class Resource resource.h
40 \brief The Resource class provides access to named resources. 40 \brief The Resource class provides access to named resources.
41 41
42 The resources may be provided from files or other sources. 42 The resources may be provided from files or other sources.
43 43
44 The allSounds() function returns a list of all the sounds available. 44 The allSounds() function returns a list of all the sounds available.
45 A particular sound can be searched for using findSound(). 45 A particular sound can be searched for using findSound().
46 46
47 Images can be loaded with loadImage(), loadPixmap(), loadBitmap() 47 Images can be loaded with loadImage(), loadPixmap(), loadBitmap()
48 and loadIconSet(). 48 and loadIconSet().
49 49
50 \ingroup qtopiaemb 50 \ingroup qtopiaemb
51*/ 51*/
52 52
53/*! 53/*!
54 \fn Resource::Resource() 54 \fn Resource::Resource()
55 \internal 55 \internal
56*/ 56*/
57 57
58/*! 58/*!
59 Returns the QPixmap called \a pix. You should avoid including 59 Returns the QPixmap called \a pix. You should avoid including
60 any filename type extension (e.g. .png, .xpm). 60 any filename type extension (e.g. .png, .xpm).
61*/ 61*/
62#include <stdio.h>
62QPixmap Resource::loadPixmap( const QString &pix ) 63QPixmap Resource::loadPixmap( const QString &pix )
63{ 64{
64 QPixmap pm; 65 QPixmap pm; // null pixmap
65 QString key="QPE_"+pix; 66 QString key="QPE_"+pix;
66 if ( !QPixmapCache::find(key,pm) ) { 67 if ( !QPixmapCache::find(key,pm) ) {
67 pm.convertFromImage(loadImage(pix)); 68 QImage I = loadImage(pix);
68 QPixmapCache::insert(key,pm); 69 if( I.isNull() ) {
70 qWarning( "Could not load %s", pix.latin1() );
71 } else {
72 pm.convertFromImage(I);
73 QPixmapCache::insert(key,pm);
74 }
69 } 75 }
70 return pm; 76 return pm;
71} 77}
72 78
73/*! 79/*!
74 Returns the QBitmap called \a pix. You should avoid including 80 Returns the QBitmap called \a pix. You should avoid including
75 any filename type extension (e.g. .png, .xpm). 81 any filename type extension (e.g. .png, .xpm).
76*/ 82*/
77QBitmap Resource::loadBitmap( const QString &pix ) 83QBitmap Resource::loadBitmap( const QString &pix )
78{ 84{
79 QBitmap bm; 85 QBitmap bm;
80 bm = loadPixmap(pix); 86 bm = loadPixmap(pix);
81 return bm; 87 return bm;
82} 88}
83 89
84/*! 90/*!
85 Returns the filename of a pixmap called \a pix. You should avoid including 91 Returns the filename of a pixmap called \a pix. You should avoid including
86 any filename type extension (e.g. .png, .xpm). 92 any filename type extension (e.g. .png, .xpm).
87 93
88 Normally you will use loadPixmap() rather than this function. 94 Normally you will use loadPixmap() rather than this function.
89*/ 95*/
90QString Resource::findPixmap( const QString &pix ) 96QString Resource::findPixmap( const QString &pix )
91{ 97{
92 QString picsPath = QPEApplication::qpeDir() + "pics/"; 98 QString picsPath = QPEApplication::qpeDir() + "pics/";
93 99
94 QString f; 100 QString f;
95 101
96 // Common case optimizations... 102 // Common case optimizations...
97 f = picsPath + pix + ".png"; 103 f = picsPath + pix + ".png";
98 if ( QFile( f ).exists() ) 104 if ( QFile( f ).exists() )
99 return f; 105 return f;
100 f = picsPath + pix + ".xpm"; 106 f = picsPath + pix + ".xpm";
101 if ( QFile( f ).exists() ) 107 if ( QFile( f ).exists() )
102 return f; 108 return f;
103 109
104
105 // All formats... 110 // All formats...
106 QStrList fileFormats = QImageIO::inputFormats(); 111 QStrList fileFormats = QImageIO::inputFormats();
107 QString ff = fileFormats.first(); 112 QString ff = fileFormats.first();
108 while ( fileFormats.current() ) { 113 while ( fileFormats.current() ) {
109 QStringList exts = MimeType("image/"+ff.lower()).extensions(); 114 QStringList exts = MimeType("image/"+ff.lower()).extensions();
110 for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) { 115 for ( QStringList::ConstIterator it = exts.begin(); it!=exts.end(); ++it ) {
111 QString f = picsPath + pix + "." + *it; 116 QString f = picsPath + pix + "." + *it;
112 if ( QFile(f).exists() ) 117 if ( QFile(f).exists() )
113 return f; 118 return f;
114 } 119 }
115 ff = fileFormats.next(); 120 ff = fileFormats.next();
116 } 121 }
117 122
118 // Finally, no (or existing) extension... 123 // Finally, no (or existing) extension...
119 if ( QFile( picsPath + pix ).exists() ) 124 if ( QFile( picsPath + pix ).exists() )
120 return picsPath + pix; 125 return picsPath + pix;
121 126
122 //qDebug("Cannot find pixmap: %s", pix.latin1()); 127 //qDebug("Cannot find pixmap: %s", pix.latin1());
123 return QString(); 128 return QString();
124} 129}
125 130
126/*! 131/*!
127 Returns a sound file for a sound called \a name. 132 Returns a sound file for a sound called \a name.
128 133
129 You should avoid including any filename type extension (e.g. .wav), 134 You should avoid including any filename type extension (e.g. .wav),
130 as the system will search for only those fileformats which are supported 135 as the system will search for only those fileformats which are supported
131 by the library. 136 by the library.
132 137
133 Currently, only WAV files are supported. 138 Currently, only WAV files are supported.
134*/ 139*/
135QString Resource::findSound( const QString &name ) 140QString Resource::findSound( const QString &name )
136{ 141{
137 QString picsPath = QPEApplication::qpeDir() + "sounds/"; 142 QString picsPath = QPEApplication::qpeDir() + "sounds/";
138 143
139 QString result; 144 QString result;
140 if ( QFile( (result = picsPath + name + ".wav") ).exists() ) 145 if ( QFile( (result = picsPath + name + ".wav") ).exists() )
141 return result; 146 return result;
142 147
143 return QString(); 148 return QString();
144} 149}
145 150
146/*! 151/*!
147 Returns a list of all sound names. 152 Returns a list of all sound names.
148*/ 153*/
149QStringList Resource::allSounds() 154QStringList Resource::allSounds()
150{ 155{
151 QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" ); 156 QDir resourcedir( QPEApplication::qpeDir() + "sounds/", "*.wav" );
152 QStringList entries = resourcedir.entryList(); 157 QStringList entries = resourcedir.entryList();
153 QStringList result; 158 QStringList result;
154 for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i) 159 for (QStringList::Iterator i=entries.begin(); i != entries.end(); ++i)
155 result.append((*i).replace(QRegExp("\\.wav"),"")); 160 result.append((*i).replace(QRegExp("\\.wav"),""));
156 return result; 161 return result;
157} 162}
158 163
159static QImage load_image(const QString &name) 164static QImage load_image(const QString &name)
160{ 165{
161 if (g_notUseSet ) { 166 if (g_notUseSet ) {
162 // try file 167 // try file
163 QImage img; 168 QImage img;
164 QString f = Resource::findPixmap(name); 169 QString f = Resource::findPixmap(name);
165 if ( !f.isEmpty() ) 170 if ( !f.isEmpty() )
166 img.load(f); 171 img.load(f);
167 if (img.isNull() ) 172 if (img.isNull() )
168 img = qembed_findImage(name.latin1() ); 173 img = qembed_findImage(name.latin1() );